@@ -20,6 +20,7 @@ import (
2020
2121 "github.com/gptscript-ai/gptscript/pkg/assemble"
2222 "github.com/gptscript-ai/gptscript/pkg/builtin"
23+ "github.com/gptscript-ai/gptscript/pkg/engine"
2324 "github.com/gptscript-ai/gptscript/pkg/parser"
2425 "github.com/gptscript-ai/gptscript/pkg/types"
2526)
@@ -137,7 +138,7 @@ func loadURL(ctx context.Context, base *Source, name string) (*Source, bool, err
137138 }, true , nil
138139}
139140
140- func loadProgram (data []byte , into * types.Program ) (types.Tool , error ) {
141+ func loadProgram (data []byte , into * types.Program , targetToolName string ) (types.Tool , error ) {
141142 var (
142143 ext types.Program
143144 id string
@@ -158,7 +159,19 @@ func loadProgram(data []byte, into *types.Program) (types.Tool, error) {
158159 into .ToolSet [v .ID ] = v
159160 }
160161
161- return into .ToolSet [ext .EntryToolID + id ], nil
162+ tool := into .ToolSet [ext .EntryToolID + id ]
163+ if targetToolName == "" {
164+ return tool , nil
165+ }
166+
167+ tool , ok := into.ToolSet [tool.LocalTools [targetToolName ]]
168+ if ! ok {
169+ return tool , & engine.ErrToolNotFound {
170+ ToolName : targetToolName ,
171+ }
172+ }
173+
174+ return tool , nil
162175}
163176
164177func ReadTool (ctx context.Context , prg * types.Program , base * Source , targetToolName string ) (types.Tool , error ) {
@@ -169,7 +182,7 @@ func ReadTool(ctx context.Context, prg *types.Program, base *Source, targetToolN
169182 _ = base .Content .Close ()
170183
171184 if bytes .HasPrefix (data , assemble .Header ) {
172- return loadProgram (data , prg )
185+ return loadProgram (data , prg , targetToolName )
173186 }
174187
175188 tools , err := parser .Parse (bytes .NewReader (data ))
@@ -263,6 +276,7 @@ func link(ctx context.Context, prg *types.Program, base *Source, tool types.Tool
263276 }
264277
265278 tool .ToolMapping = map [string ]string {}
279+ tool .LocalTools = map [string ]string {}
266280 toolNames := map [string ]struct {}{}
267281
268282 // Add now to break circular loops, but later we will update this tool and copy the new
@@ -315,6 +329,10 @@ func link(ctx context.Context, prg *types.Program, base *Source, tool types.Tool
315329 tool .Tools [i ] = newToolName
316330 }
317331
332+ for _ , localTool := range localTools {
333+ tool .LocalTools [localTool .Name ] = localTool .ID
334+ }
335+
318336 tool = builtin .SetDefaults (tool )
319337 prg .ToolSet [tool .ID ] = tool
320338
0 commit comments