@@ -63,59 +63,8 @@ type ToolReference struct {
63
63
ToolID string
64
64
}
65
65
66
- func (p Program ) GetContextToolRefs (toolID string ) (result []ToolReference , _ error ) {
67
- seen := map [struct {
68
- toolID string
69
- arg string
70
- }]struct {}{}
71
- tool := p .ToolSet [toolID ]
72
-
73
- subToolRefs , err := tool .GetToolRefsFromNames (tool .Tools )
74
- if err != nil {
75
- return nil , err
76
- }
77
-
78
- for _ , subToolRef := range subToolRefs {
79
- subTool := p .ToolSet [subToolRef .ToolID ]
80
- exportContextToolRefs , err := subTool .GetToolRefsFromNames (subTool .ExportContext )
81
- if err != nil {
82
- return nil , err
83
- }
84
- for _ , exportContextToolRef := range exportContextToolRefs {
85
- key := struct {
86
- toolID string
87
- arg string
88
- }{
89
- toolID : exportContextToolRef .ToolID ,
90
- arg : exportContextToolRef .Arg ,
91
- }
92
- if _ , ok := seen [key ]; ! ok {
93
- seen [key ] = struct {}{}
94
- result = append (result , exportContextToolRef )
95
- }
96
- }
97
- }
98
-
99
- contextToolRefs , err := p .ToolSet [toolID ].GetToolRefsFromNames (p .ToolSet [toolID ].Context )
100
- if err != nil {
101
- return nil , err
102
- }
103
-
104
- for _ , contextToolRef := range contextToolRefs {
105
- key := struct {
106
- toolID string
107
- arg string
108
- }{
109
- toolID : contextToolRef .ToolID ,
110
- arg : contextToolRef .Arg ,
111
- }
112
- if _ , ok := seen [key ]; ! ok {
113
- seen [key ] = struct {}{}
114
- result = append (result , contextToolRef )
115
- }
116
- }
117
-
118
- return
66
+ func (p Program ) GetContextToolRefs (toolID string ) ([]ToolReference , error ) {
67
+ return p .ToolSet [toolID ].GetContextTools (p )
119
68
}
120
69
121
70
func (p Program ) GetCompletionTools () (result []CompletionTool , err error ) {
@@ -295,105 +244,139 @@ func (t Tool) String() string {
295
244
return buf .String ()
296
245
}
297
246
298
- func (t Tool ) GetCompletionTools (prg Program ) (result [] CompletionTool , err error ) {
299
- toolNames := map [ string ] struct {} {}
247
+ func (t Tool ) GetExportedContext (prg Program ) ([] ToolReference , error ) {
248
+ result := & toolRefSet {}
300
249
301
- subToolRefs , err := t .GetToolRefsFromNames (t .Parameters . Tools )
250
+ exportRefs , err := t .GetToolRefsFromNames (t .ExportContext )
302
251
if err != nil {
303
252
return nil , err
304
253
}
305
254
306
- for _ , subToolRef := range subToolRefs {
307
- result , err = appendTool ( result , prg , t , subToolRef . Reference , toolNames , subToolRef . Named )
308
- if err != nil {
309
- return nil , err
310
- }
255
+ for _ , exportRef := range exportRefs {
256
+ result . Add ( exportRef )
257
+
258
+ tool := prg . ToolSet [ exportRef . ToolID ]
259
+ result . AddAll ( tool . GetExportedContext ( prg ))
311
260
}
312
261
313
- for _ , subToolName := range t .Parameters .Context {
314
- result , err = appendExports (result , prg , t , subToolName , toolNames )
315
- if err != nil {
316
- return nil , err
317
- }
262
+ return result .List ()
263
+ }
264
+
265
+ func (t Tool ) GetExportedTools (prg Program ) ([]ToolReference , error ) {
266
+ result := & toolRefSet {}
267
+
268
+ exportRefs , err := t .GetToolRefsFromNames (t .Export )
269
+ if err != nil {
270
+ return nil , err
271
+ }
272
+
273
+ for _ , exportRef := range exportRefs {
274
+ result .Add (exportRef )
275
+ result .AddAll (prg .ToolSet [exportRef .ToolID ].GetExportedTools (prg ))
318
276
}
319
277
320
- return result , nil
278
+ return result . List ()
321
279
}
322
280
323
- func getTool ( prg Program , parent Tool , name string ) (Tool , error ) {
324
- toolID , ok := parent . ToolMapping [ name ]
325
- if ! ok {
326
- return Tool {}, & ErrToolNotFound {
327
- ToolName : name ,
328
- }
281
+ func ( t Tool ) GetContextTools ( prg Program ) ([] ToolReference , error ) {
282
+ result := & toolRefSet {}
283
+
284
+ contextRefs , err := t . GetToolRefsFromNames ( t . Context )
285
+ if err != nil {
286
+ return nil , err
329
287
}
330
- tool , ok := prg .ToolSet [toolID ]
331
- if ! ok {
332
- return Tool {}, & ErrToolNotFound {
333
- ToolName : name ,
334
- }
288
+
289
+ for _ , contextRef := range contextRefs {
290
+ result .AddAll (prg .ToolSet [contextRef .ToolID ].GetExportedContext (prg ))
291
+ result .Add (contextRef )
335
292
}
336
- return tool , nil
293
+
294
+ return result .List ()
337
295
}
338
296
339
- func appendExports ( completionTools [] CompletionTool , prg Program , parentTool Tool , subToolName string , toolNames map [ string ] struct {} ) ([]CompletionTool , error ) {
340
- subTool , err := getTool (prg , parentTool , subToolName )
297
+ func ( t Tool ) GetCompletionTools ( prg Program ) (result []CompletionTool , err error ) {
298
+ refs , err := t . getCompletionToolRefs (prg )
341
299
if err != nil {
342
300
return nil , err
343
301
}
302
+ return toolRefsToCompletionTools (refs , prg ), nil
303
+ }
304
+
305
+ func (t Tool ) addReferencedTools (prg Program , result * toolRefSet ) error {
306
+ subToolRefs , err := t .GetToolRefsFromNames (t .Parameters .Tools )
307
+ if err != nil {
308
+ return err
309
+ }
344
310
345
- for _ , export := range subTool .Export {
346
- completionTools , err = appendTool (completionTools , prg , subTool , export , toolNames , "" )
347
- if err != nil {
348
- return nil , err
349
- }
311
+ for _ , subToolRef := range subToolRefs {
312
+ // Add the tool
313
+ result .Add (subToolRef )
314
+
315
+ // Get all tools exports
316
+ result .AddAll (prg .ToolSet [subToolRef .ToolID ].GetExportedTools (prg ))
350
317
}
351
318
352
- return completionTools , nil
319
+ return nil
353
320
}
354
321
355
- func appendTool ( completionTools [] CompletionTool , prg Program , parentTool Tool , subToolName string , toolNames map [ string ] struct {}, asName string ) ([] CompletionTool , error ) {
356
- subTool , err := getTool (prg , parentTool , subToolName )
322
+ func ( t Tool ) addContextExportedTools ( prg Program , result * toolRefSet ) error {
323
+ contextTools , err := t . GetContextTools (prg )
357
324
if err != nil {
325
+ return err
326
+ }
327
+
328
+ for _ , contextTool := range contextTools {
329
+ result .AddAll (prg .ToolSet [contextTool .ToolID ].GetExportedTools (prg ))
330
+ }
331
+
332
+ return nil
333
+ }
334
+
335
+ func (t Tool ) getCompletionToolRefs (prg Program ) ([]ToolReference , error ) {
336
+ result := toolRefSet {}
337
+
338
+ if err := t .addReferencedTools (prg , & result ); err != nil {
358
339
return nil , err
359
340
}
360
341
361
- args := subTool .Parameters .Arguments
362
- if args == nil && ! subTool .IsCommand () && ! subTool .Chat {
363
- args = & system .DefaultToolSchema
342
+ if err := t .addContextExportedTools (prg , & result ); err != nil {
343
+ return nil , err
364
344
}
365
345
366
- for _ , existingTool := range completionTools {
367
- if existingTool .Function .ToolID == subTool .ID {
368
- return completionTools , nil
346
+ return result .List ()
347
+ }
348
+
349
+ func toolRefsToCompletionTools (completionTools []ToolReference , prg Program ) (result []CompletionTool ) {
350
+ toolNames := map [string ]struct {}{}
351
+
352
+ for _ , subToolRef := range completionTools {
353
+ subTool := prg .ToolSet [subToolRef .ToolID ]
354
+
355
+ subToolName := subToolRef .Reference
356
+ if subToolRef .Named != "" {
357
+ subToolName = subToolRef .Named
369
358
}
370
- }
371
359
372
- if subTool .Instructions == "" {
373
- log .Debugf ("Skipping zero instruction tool %s (%s)" , subToolName , subTool .ID )
374
- } else {
375
- name := subToolName
376
- if asName != "" {
377
- name = asName
360
+ args := subTool .Parameters .Arguments
361
+ if args == nil && ! subTool .IsCommand () && ! subTool .Chat {
362
+ args = & system .DefaultToolSchema
378
363
}
379
- completionTools = append (completionTools , CompletionTool {
380
- Function : CompletionFunctionDefinition {
381
- ToolID : subTool .ID ,
382
- Name : PickToolName (name , toolNames ),
383
- Description : subTool .Parameters .Description ,
384
- Parameters : args ,
385
- },
386
- })
387
- }
388
364
389
- for _ , export := range subTool .Export {
390
- completionTools , err = appendTool (completionTools , prg , subTool , export , toolNames , "" )
391
- if err != nil {
392
- return nil , err
365
+ if subTool .Instructions == "" {
366
+ log .Debugf ("Skipping zero instruction tool %s (%s)" , subToolName , subTool .ID )
367
+ } else {
368
+ result = append (result , CompletionTool {
369
+ Function : CompletionFunctionDefinition {
370
+ ToolID : subTool .ID ,
371
+ Name : PickToolName (subToolName , toolNames ),
372
+ Description : subTool .Parameters .Description ,
373
+ Parameters : args ,
374
+ },
375
+ })
393
376
}
394
377
}
395
378
396
- return completionTools , nil
379
+ return
397
380
}
398
381
399
382
type Repo struct {
0 commit comments