-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands_agent.go
More file actions
334 lines (302 loc) · 8.98 KB
/
Copy pathcommands_agent.go
File metadata and controls
334 lines (302 loc) · 8.98 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/google/uuid"
"github.com/modelrelay/modelrelay/sdk/go"
"github.com/modelrelay/modelrelay/sdk/go/generated"
"github.com/modelrelay/modelrelay/sdk/go/llm"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
func newAgentCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "agent",
Short: "Run and test agents",
}
cmd.AddCommand(newAgentRunCmd(), newAgentTestCmd(), newAgentLoopCmd())
return cmd
}
type agentFlags struct {
inputText string
inputFile string
mockToolsPath string
outputPath string
trace bool
model string
maxSteps int
maxDuration int
stepTimeout int
toolFailurePolicy string
toolRetryCount int
captureToolIO bool
dryRun bool
customerID string
}
func newAgentRunCmd() *cobra.Command {
flags := &agentFlags{}
cmd := &cobra.Command{
Use: "run <slug>",
Short: "Run an agent",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runAgent(cmd, args[0], flags, false)
},
}
bindAgentFlags(cmd, flags, false)
cmd.Flags().StringVar(&flags.customerID, "customer", "", "Customer ID")
return cmd
}
func newAgentTestCmd() *cobra.Command {
flags := &agentFlags{}
cmd := &cobra.Command{
Use: "test <slug>",
Short: "Test an agent with mocked tools",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runAgent(cmd, args[0], flags, true)
},
}
bindAgentFlags(cmd, flags, true)
return cmd
}
func bindAgentFlags(cmd *cobra.Command, flags *agentFlags, includeMock bool) {
cmd.Flags().StringVar(&flags.inputText, "input", "", "Inline user input")
cmd.Flags().StringVar(&flags.inputFile, "input-file", "", "Path to JSON array of input items")
if includeMock {
cmd.Flags().StringVar(&flags.mockToolsPath, "mock-tools", "", "Path to JSON object of mock tool outputs")
}
cmd.Flags().StringVar(&flags.outputPath, "output", "", "Write JSON response to file")
cmd.Flags().BoolVar(&flags.trace, "trace", false, "Print step summaries")
cmd.Flags().StringVar(&flags.model, "model", "", "Override model")
cmd.Flags().IntVar(&flags.maxSteps, "max-steps", -1, "Override max tool steps")
cmd.Flags().IntVar(&flags.maxDuration, "max-duration-sec", -1, "Override max run duration")
cmd.Flags().IntVar(&flags.stepTimeout, "step-timeout-sec", -1, "Override per-step timeout")
cmd.Flags().StringVar(&flags.toolFailurePolicy, "tool-failure-policy", "", "stop | continue | retry")
cmd.Flags().IntVar(&flags.toolRetryCount, "tool-retry-count", -1, "Retry count when policy is retry")
cmd.Flags().BoolVar(&flags.captureToolIO, "capture-tool-io", false, "Include step logs in response")
cmd.Flags().BoolVar(&flags.dryRun, "dry-run", false, "Error if a tool is not mocked")
}
func runAgent(cmd *cobra.Command, slug string, flags *agentFlags, useTestEndpoint bool) error {
cfg, err := runtimeConfigFrom(cmd)
if err != nil {
return err
}
if strings.TrimSpace(cfg.ProjectID) == "" {
return errors.New("project id required")
}
projectID, err := uuid.Parse(cfg.ProjectID)
if err != nil {
return errors.New("invalid project id")
}
client, err := newAgentClient(cfg)
if err != nil {
return err
}
input, err := resolveInput(flags.inputText, flags.inputFile, cmd.Flags().Args()[1:])
if err != nil {
return err
}
options := buildAgentOptions(flags, cmd.Flags())
ctx, cancel := contextWithTimeout(cfg.Timeout)
defer cancel()
if useTestEndpoint {
mockTools, err := readMockTools(flags.mockToolsPath)
if err != nil {
return err
}
req := sdk.AgentTestRequest{
Input: input,
MockTools: mockTools,
Options: options,
}
resp, err := client.Agents.Test(ctx, projectID, slug, req)
if err != nil {
return err
}
return handleAgentResponse(cfg, resp, flags)
}
req := sdk.AgentRunRequest{
Input: input,
Options: options,
CustomerID: optionalString(flags.customerID),
}
resp, err := client.Agents.Run(ctx, projectID, slug, req)
if err != nil {
return err
}
return handleAgentResponse(cfg, resp, flags)
}
func newAgentClient(cfg runtimeConfig) (*sdk.Client, error) {
opts := []sdk.Option{sdk.WithClientHeader(clientHeader())}
if strings.TrimSpace(cfg.BaseURL) != "" {
opts = append(opts, sdk.WithBaseURL(cfg.BaseURL))
}
if strings.TrimSpace(cfg.APIKey) == "" {
return nil, errors.New("api key required")
}
key, err := sdk.ParseAPIKeyAuth(cfg.APIKey)
if err != nil {
return nil, err
}
return sdk.NewClientWithKey(key, opts...)
}
func buildAgentOptions(flags *agentFlags, flagset *pflag.FlagSet) *sdk.AgentRunOptions {
opts := &sdk.AgentRunOptions{}
if strings.TrimSpace(flags.model) != "" {
clean := strings.TrimSpace(flags.model)
opts.Model = &clean
}
if flags.maxSteps >= 0 {
val := flags.maxSteps
opts.MaxSteps = &val
}
if flags.maxDuration >= 0 {
val := flags.maxDuration
opts.MaxDurationSec = &val
}
if flags.stepTimeout >= 0 {
val := flags.stepTimeout
opts.StepTimeoutSec = &val
}
if strings.TrimSpace(flags.toolFailurePolicy) != "" {
clean := sdk.AgentRunOptionsToolFailurePolicy(strings.TrimSpace(flags.toolFailurePolicy))
opts.ToolFailurePolicy = &clean
}
if flags.toolRetryCount >= 0 {
val := flags.toolRetryCount
opts.ToolRetryCount = &val
}
if flagset != nil {
if flagset.Changed("capture-tool-io") {
val := flags.captureToolIO
opts.CaptureToolIo = &val
}
if flagset.Changed("dry-run") {
val := flags.dryRun
opts.DryRun = &val
}
}
if opts.Model == nil && opts.MaxSteps == nil && opts.MaxDurationSec == nil && opts.StepTimeoutSec == nil &&
opts.ToolFailurePolicy == nil && opts.ToolRetryCount == nil && opts.CaptureToolIo == nil && opts.DryRun == nil {
return nil
}
return opts
}
func resolveInput(inputText, inputFile string, tail []string) ([]llm.InputItem, error) {
if inputFile != "" {
return readInputFile(inputFile)
}
text := strings.TrimSpace(inputText)
if text == "" && len(tail) > 0 {
text = strings.TrimSpace(strings.Join(tail, " "))
}
if text == "" {
return nil, errors.New("input is required (use --input, --input-file, or trailing args)")
}
return []llm.InputItem{llm.NewUserText(text)}, nil
}
func readInputFile(path string) ([]llm.InputItem, error) {
raw, err := os.ReadFile(path)
if err != nil {
return nil, err
}
if len(raw) == 0 {
return nil, errors.New("input file is empty")
}
var items []llm.InputItem
if err := json.Unmarshal(raw, &items); err == nil && len(items) > 0 {
return items, nil
}
var wrapper struct {
Input []llm.InputItem `json:"input"`
}
if err := json.Unmarshal(raw, &wrapper); err == nil && len(wrapper.Input) > 0 {
return wrapper.Input, nil
}
return nil, errors.New("input file must be a JSON array of input items or an object with {input: [...]} ")
}
func readMockTools(path string) (map[string]any, error) {
if strings.TrimSpace(path) == "" {
return nil, nil
}
raw, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var payload map[string]any
if err := json.Unmarshal(raw, &payload); err != nil {
return nil, fmt.Errorf("mock-tools must be a JSON object: %w", err)
}
return payload, nil
}
func handleAgentResponse(cfg runtimeConfig, resp sdk.AgentRunResponse, flags *agentFlags) error {
jsonPayload, _ := json.MarshalIndent(resp, "", " ")
if flags.outputPath != "" {
if err := os.MkdirAll(filepath.Dir(flags.outputPath), 0o755); err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}
if err := os.WriteFile(flags.outputPath, jsonPayload, 0o600); err != nil {
return fmt.Errorf("failed to write output: %w", err)
}
}
if cfg.Output == outputFormatJSON {
fmt.Println(string(jsonPayload))
return nil
}
fmt.Printf("Run ID: %s\n", resp.RunId)
fmt.Printf("Steps: %d | LLM calls: %d | Tool calls: %d | Cost: %0.2f\n",
resp.Usage.TotalSteps,
resp.Usage.TotalLlmCalls,
resp.Usage.TotalToolCalls,
float64(resp.Usage.TotalCostCents)/100.0,
)
if outputText := renderOutputText(resp.Output); outputText != "" {
fmt.Println("\nOutput:\n" + outputText)
}
if flags.trace && resp.Steps != nil {
fmt.Println("\nTrace:")
for _, step := range *resp.Steps {
toolCalls := 0
toolResults := 0
if step.ToolCalls != nil {
toolCalls = len(*step.ToolCalls)
}
if step.ToolResults != nil {
toolResults = len(*step.ToolResults)
}
fmt.Printf("- node=%s step=%d tool_calls=%d tool_results=%d\n", step.NodeId, step.Step, toolCalls, toolResults)
}
}
return nil
}
func renderOutputText(output *[]generated.OutputItem) string {
if output == nil {
return ""
}
var builder strings.Builder
for _, item := range *output {
if item.Content == nil {
continue
}
for _, part := range *item.Content {
textPart, err := part.AsContentPartText()
if err != nil {
continue
}
builder.WriteString(textPart.Text)
}
}
return strings.TrimSpace(builder.String())
}
func optionalString(val string) *string {
if strings.TrimSpace(val) == "" {
return nil
}
clean := strings.TrimSpace(val)
return &clean
}