-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathagent_tool.go
More file actions
296 lines (264 loc) · 8.42 KB
/
agent_tool.go
File metadata and controls
296 lines (264 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package agent
import (
"context"
"fmt"
"strings"
utcp "github.com/universal-tool-calling-protocol/go-utcp"
"github.com/universal-tool-calling-protocol/go-utcp/src/providers/base"
"github.com/universal-tool-calling-protocol/go-utcp/src/providers/cli"
"github.com/universal-tool-calling-protocol/go-utcp/src/repository"
"github.com/universal-tool-calling-protocol/go-utcp/src/tools"
"github.com/universal-tool-calling-protocol/go-utcp/src/transports"
)
// SubAgentTool adapts a SubAgent to the Tool interface.
type SubAgentTool struct {
subAgent SubAgent
}
// NewSubAgentTool creates a new tool that wraps a SubAgent.
func NewSubAgentTool(sa SubAgent) Tool {
return &SubAgentTool{subAgent: sa}
}
func (t *SubAgentTool) Spec() ToolSpec {
return ToolSpec{
Name: t.subAgent.Name(),
Description: t.subAgent.Description(),
InputSchema: map[string]any{
"type": "object",
"properties": map[string]any{
"instruction": map[string]any{
"type": "string",
"description": "The instruction or query for the sub-agent.",
},
},
"required": []string{"instruction"},
},
}
}
func (t *SubAgentTool) Invoke(ctx context.Context, req ToolRequest) (ToolResponse, error) {
instruction, ok := req.Arguments["instruction"].(string)
if !ok {
return ToolResponse{}, fmt.Errorf("missing or invalid 'instruction' argument")
}
result, err := t.subAgent.Run(ctx, instruction)
if err != nil {
return ToolResponse{}, err
}
return ToolResponse{
Content: result,
}, nil
}
// AgentToolAdapter adapts an Agent to the Tool interface.
type AgentToolAdapter struct {
agent *Agent
name string
description string
}
type agentCLITransport struct {
inner repository.ClientTransport
tools map[string][]tools.Tool
}
func (t *agentCLITransport) RegisterToolProvider(ctx context.Context, prov base.Provider) ([]tools.Tool, error) {
p, ok := prov.(*cli.CliProvider)
if !ok {
if t.inner != nil {
return t.inner.RegisterToolProvider(ctx, prov)
}
return nil, fmt.Errorf("unsupported provider type %T", prov)
}
if t.tools == nil {
t.tools = make(map[string][]tools.Tool)
}
list, ok := t.tools[p.Name]
if !ok {
if t.inner != nil {
return t.inner.RegisterToolProvider(ctx, prov)
}
return nil, fmt.Errorf("agent tools not found for provider %s", p.Name)
}
return list, nil
}
func (t *agentCLITransport) DeregisterToolProvider(ctx context.Context, prov base.Provider) error {
if p, ok := prov.(*cli.CliProvider); ok {
if _, ok := t.tools[p.Name]; ok {
delete(t.tools, p.Name)
return nil
}
}
if t.inner != nil {
return t.inner.DeregisterToolProvider(ctx, prov)
}
return nil
}
func (t *agentCLITransport) CallTool(ctx context.Context, toolName string, args map[string]any, prov base.Provider, _ *string) (any, error) {
if p, ok := prov.(*cli.CliProvider); ok {
if list, ok := t.tools[p.Name]; ok {
for _, tool := range list {
if tool.Name == toolName || strings.HasSuffix(tool.Name, "."+toolName) {
if tool.Handler == nil {
return nil, fmt.Errorf("tool %s has no handler", toolName)
}
return tool.Handler(ctx, args)
}
}
}
if t.inner != nil {
return t.inner.CallTool(ctx, toolName, args, prov, nil)
}
return nil, fmt.Errorf("tool %s not found for provider %s", toolName, p.Name)
}
if t.inner != nil {
return t.inner.CallTool(ctx, toolName, args, prov, nil)
}
return nil, fmt.Errorf("unsupported provider type %T", prov)
}
func (t *agentCLITransport) CallToolStream(ctx context.Context, toolName string, args map[string]any, prov base.Provider) (transports.StreamResult, error) {
if p, ok := prov.(*cli.CliProvider); ok {
if _, ok := t.tools[p.Name]; ok {
return nil, fmt.Errorf("streaming not supported for tool %s (provider %s)", toolName, p.Name)
}
}
if t.inner != nil {
return t.inner.CallToolStream(ctx, toolName, args, prov)
}
return nil, fmt.Errorf("unsupported provider type %T", prov)
}
// NewAgentTool creates a new tool that wraps an Agent.
func NewAgentTool(name, description string, agent *Agent) Tool {
return &AgentToolAdapter{
agent: agent,
name: name,
description: description,
}
}
func (t *AgentToolAdapter) Spec() ToolSpec {
return ToolSpec{
Name: t.name,
Description: t.description,
InputSchema: map[string]any{
"type": "object",
"properties": map[string]any{
"instruction": map[string]any{
"type": "string",
"description": "The instruction or query for the sub-agent.",
},
},
"required": []string{"instruction"},
},
}
}
func (t *AgentToolAdapter) Invoke(ctx context.Context, req ToolRequest) (ToolResponse, error) {
instruction, ok := req.Arguments["instruction"].(string)
if !ok {
return ToolResponse{}, fmt.Errorf("missing or invalid 'instruction' argument")
}
// Create a sub-session ID to keep context separate but related
subSessionID := fmt.Sprintf("%s.sub.%s", req.SessionID, t.name)
result, err := t.agent.Generate(ctx, subSessionID, instruction)
if err != nil {
return ToolResponse{}, err
}
return ToolResponse{
Content: fmt.Sprint(result),
}, nil
}
// AsTool returns a Tool representation of the Agent.
func (a *Agent) AsTool(name, description string) Tool {
return NewAgentTool(name, description, a)
}
// AsUTCPTool exposes the agent as a UTCP tool with an in-process handler.
// The tool accepts:
// - instruction (required): user query for the agent
// - session_id (optional): custom session id; defaults to a namespaced value derived from the tool name
func (a *Agent) AsUTCPTool(name, description string) tools.Tool {
providerName := strings.TrimSpace(name)
if parts := strings.Split(name, "."); len(parts) > 1 {
providerName = parts[0]
}
defaultSession := fmt.Sprintf("%s.session", providerName)
return tools.Tool{
Name: name,
Description: description,
Provider: &base.BaseProvider{
Name: providerName,
ProviderType: base.ProviderCLI, // in-process handler, no remote transport
},
Inputs: tools.ToolInputOutputSchema{
Type: "object",
Properties: map[string]any{
"instruction": map[string]any{
"type": "string",
"description": "The instruction or query for the agent.",
},
"session_id": map[string]any{
"type": "string",
"description": "Optional session id; defaults to the provider-derived session.",
},
},
Required: []string{"instruction"},
},
Outputs: tools.ToolInputOutputSchema{
Type: "object",
Properties: map[string]any{
"response": map[string]any{"type": "string"},
"session_id": map[string]any{"type": "string"},
},
},
Handler: tools.ToolHandler(func(ctx context.Context, inputs map[string]interface{}) (any, error) {
rawInstruction, ok := inputs["instruction"].(string)
if !ok || strings.TrimSpace(rawInstruction) == "" {
return nil, fmt.Errorf("missing or invalid 'instruction'")
}
sessionID, _ := inputs["session_id"].(string)
sessionID = strings.TrimSpace(sessionID)
if sessionID == "" {
sessionID = defaultSession
}
execCtx := ctx
if execCtx == nil {
execCtx = context.Background()
}
out, err := a.Generate(execCtx, sessionID, rawInstruction)
if err != nil {
return nil, err
}
return fmt.Sprint(out), nil
}),
}
}
// RegisterAsUTCPProvider registers the agent as a UTCP tool on the provided client.
// It installs a lightweight in-process transport under the "text" provider type
// to route CallTool invocations directly to the agent's Generate method.
func (a *Agent) RegisterAsUTCPProvider(ctx context.Context, client utcp.UtcpClientInterface, name, description string) error {
if client == nil {
return fmt.Errorf("utcp client is nil")
}
tool := a.AsUTCPTool(name, description)
providerName := strings.TrimSpace(name)
if parts := strings.Split(name, "."); len(parts) > 1 {
providerName = parts[0]
}
tp := &cli.CliProvider{
BaseProvider: base.BaseProvider{
Name: providerName,
ProviderType: base.ProviderCLI,
},
}
transportsMap := client.GetTransports()
if transportsMap == nil {
return fmt.Errorf("utcp client transports map is nil")
}
existing := transportsMap[string(base.ProviderCLI)]
var shim *agentCLITransport
if maybe, ok := existing.(*agentCLITransport); ok {
shim = maybe
} else {
shim = &agentCLITransport{inner: existing}
transportsMap[string(base.ProviderCLI)] = shim
}
if shim.tools == nil {
shim.tools = make(map[string][]tools.Tool)
}
shim.tools[tp.Name] = []tools.Tool{tool}
_, err := client.RegisterToolProvider(ctx, tp)
return err
}