@@ -11,7 +11,7 @@ import (
11
11
)
12
12
13
13
func normalize (key string ) string {
14
- return strings .ToLower (strings .ReplaceAll (key , " " , "" ))
14
+ return strings .TrimSpace ( strings . ToLower (strings .ReplaceAll (key , " " , "" ) ))
15
15
}
16
16
17
17
func toBool (line string ) (bool , error ) {
@@ -156,6 +156,17 @@ func (c *context) finish(tools *[]types.Tool) {
156
156
* c = context {}
157
157
}
158
158
159
+ func commentEmbedded (line string ) (string , bool ) {
160
+ for _ , i := range []string {"#" , "# " , "//" , "// " } {
161
+ prefix := i + "gptscript:"
162
+ cut , ok := strings .CutPrefix (line , prefix )
163
+ if ok {
164
+ return cut , ok
165
+ }
166
+ }
167
+ return line , false
168
+ }
169
+
159
170
func Parse (input io.Reader ) ([]types.Tool , error ) {
160
171
scan := bufio .NewScanner (input )
161
172
@@ -179,6 +190,16 @@ func Parse(input io.Reader) ([]types.Tool, error) {
179
190
}
180
191
181
192
if ! context .inBody {
193
+ // Strip special comments to allow embedding the preamble in python or other interpreted languages
194
+ if newLine , ok := commentEmbedded (line ); ok {
195
+ line = newLine
196
+ }
197
+
198
+ // If the very first line is #! just skip because this is a unix interpreter declaration
199
+ if strings .HasPrefix (line , "#!" ) && lineNo == 1 {
200
+ continue
201
+ }
202
+
182
203
// This is a comment
183
204
if strings .HasPrefix (line , "#" ) && ! strings .HasPrefix (line , "#!" ) {
184
205
continue
0 commit comments