22 // toolClipCharacters is the most characters of a tool output: its head
23 // and its tail, half each.
24 toolClipCharacters = 30000
26 // toolClipBytes is the most bytes that a tool keeps of each end of an
27 // output, enough for toolClipCharacters/2 characters of 4 bytes.
28 toolClipBytes = 4 * toolClipCharacters / 2
30 // toolShellWaitDelay is how long a command can keep its output open
31 // after it ends or after its kill.
32 toolShellWaitDelay = 2 * time.Second
35 // ToolLog is the log that the tools write their calls and results to.
36 type ToolLog interface {
37 Append(kind, text string) (Message, error)
38 Flush(ctx context.Context) error
41 // ToolChat gives the messages and the nodes that zoom and date read.
42 type ToolChat interface {
44 Message(id int) (Message, error)
45 Node(level, index int) (Node, bool, error)
48 // Toolbox runs the tool calls of one agent. Each call and each result goes to
49 // the log as it happens: the call as a tool message, the result as an echo
50 // message. The call is durable before its tool runs, so the log never misses an
51 // action outside the agent. Commands and files run as the user in the working
52 // directory, with no sandbox. Zoom with a name reads the log of a subagent.
53 // The toolbox of the agent has the runs of its subagents for spawn and tell,
54 // and the WhatsApp link for the WhatsApp tools. The toolbox of a subagent has
55 // neither, so these are unknown tools there.
65 // Call logs the call of the tool name with its JSON arguments, makes the log
66 // durable, runs the tool, and logs its clipped output, which it returns. A
67 // failed tool gives its error as the output. An error of Call itself is an
68 // error of the log. When ctx ends before the flush, the tool does not run.
69 func (b *Toolbox) Call(ctx context.Context, name, arguments string) (string, error) {
70 _, err := b.log.Append("tool", toolCallText(name, arguments))
75 err = b.log.Flush(ctx)
76 if err != nil && ctx.Err() == nil {
80 output := "not run: the turn was interrupted"
82 output = toolClip(b.run(ctx, name, arguments))
85 _, err = b.log.Append("echo", output)
93 // run runs one tool and returns its output, or its error as text.
94 func (b *Toolbox) run(ctx context.Context, name, arguments string) string {
99 var args ToolZoomArguments
100 err = toolDecodeArguments(arguments, &args)
103 case args.Name != nil:
104 output, err = b.zoomAgent(ctx, *args.Name)
106 output, err = b.zoom(args.ID, args.N)
109 var args ToolDateArguments
110 err = toolDecodeArguments(arguments, &args)
112 output, err = b.date(args.ID)
115 var args ToolShellArguments
116 err = toolDecodeArguments(arguments, &args)
118 output = b.shell(ctx, args.Command)
121 var args ToolPathArguments
122 err = toolDecodeArguments(arguments, &args)
124 output, err = b.read(args.Path)
127 var args ToolWriteArguments
128 err = toolDecodeArguments(arguments, &args)
130 output, err = b.write(args.Path, args.Text)
133 var args ToolEditArguments
134 err = toolDecodeArguments(arguments, &args)
136 output, err = b.edit(args.Path, args.Old, args.New)
139 var args ToolSpawnArguments
140 err = toolDecodeArguments(arguments, &args)
143 err = fmt.Errorf("unknown tool %q", name)
145 output, err = b.runs.Spawn(ctx, args.Tasks, args.Effort)
148 var args ToolTellArguments
149 err = toolDecodeArguments(arguments, &args)
152 err = fmt.Errorf("unknown tool %q", name)
154 output, err = b.runs.Tell(ctx, args.Name, args.Message)
156 case "whatsapp_find":
157 var args ToolWhatsAppFindArguments
158 err = toolDecodeArguments(arguments, &args)
160 case b.whatsapp == nil:
161 err = fmt.Errorf("unknown tool %q", name)
163 output, err = b.whatsapp.Find(ctx, args.Name)
165 case "whatsapp_send":
166 var args ToolWhatsAppSendArguments
167 err = toolDecodeArguments(arguments, &args)
169 case b.whatsapp == nil:
170 err = fmt.Errorf("unknown tool %q", name)
172 output, err = b.whatsapp.Send(ctx, args.ID, args.Chat, args.Text)
175 err = fmt.Errorf("unknown tool %q", name)
179 return "error: " + err.Error()
185 // zoomAgent returns the whole log of the subagent name.
186 func (b *Toolbox) zoomAgent(ctx context.Context, name string) (string, error) {
187 subagent, err := b.subagents.Get(ctx, name)
192 return subagent.Show()
195 // zoom returns message id whole for n = 1, or else the two lines under the line
197 func (b *Toolbox) zoom(id, n int) (string, error) {
198 if n < 1 || bits.OnesCount(uint(n)) != 1 || id < 0 || id%n != 0 {
199 return "", fmt.Errorf("line %d+%d does not exist: n must be a power of 2 and id a multiple of n", id, n)
202 if id+n > b.chat.Len() {
203 return "", fmt.Errorf("line %d+%d goes past the last message, %d", id, n, b.chat.Len()-1)
207 message, err := b.chat.Message(id)
212 return leafSource(message), nil
215 level := bits.TrailingZeros(uint(n)) - 1
217 for _, index := range []int{2 * id / n, 2*id/n + 1} {
218 node, built, err := b.chat.Node(level, index)
225 text = strings.ReplaceAll(node.Text, "\n", " ")
228 lines = append(lines, fmt.Sprintf("%d+%d|%s", index<<level, 1<<level, text))
231 return strings.Join(lines, "\n"), nil
234 // date returns the date and time of message id, with its weekday.
235 func (b *Toolbox) date(id int) (string, error) {
236 if id < 0 || id >= b.chat.Len() {
237 return "", fmt.Errorf("message %d does not exist", id)
240 message, err := b.chat.Message(id)
245 date, err := time.Parse(time.RFC3339Nano, message.Date)
250 return date.Local().Format("Monday 2006-01-02 15:04:05 -07:00"), nil
253 // shell runs command with bash in the working directory and returns its output
254 // and error output. A failed command adds its exit status. When ctx ends, the
255 // whole process group of the command is killed.
256 func (b *Toolbox) shell(ctx context.Context, command string) string {
257 cmd := exec.CommandContext(ctx, "bash", "-c", command)
259 cmd.SysProcAttr = &syscall.SysProcAttr{
262 cmd.Cancel = func() error {
263 return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
265 cmd.WaitDelay = toolShellWaitDelay
267 output := &ToolHeadTail{}
272 text := output.String()
273 var exit *exec.ExitError
275 case ctx.Err() != nil:
276 text += "\n[interrupted]"
277 case errors.As(err, &exit):
278 text += "\n[" + exit.String() + "]"
280 text += "\n[error: " + err.Error() + "]"
288 // read returns the text of the file at path.
289 func (b *Toolbox) read(path string) (text string, err error) {
290 file, err := os.Open(b.path(path))
296 err = errors.Join(err, file.Close())
299 output := &ToolHeadTail{}
300 _, err = io.Copy(output, file)
305 return output.String(), nil
308 // write replaces the file at path with text, and makes its missing
310 func (b *Toolbox) write(path, text string) (string, error) {
312 err := os.MkdirAll(filepath.Dir(path), 0o755)
317 err = os.WriteFile(path, []byte(text), 0o644)
322 return fmt.Sprintf("wrote %d bytes to %s", len(text), path), nil
325 // edit replaces the one occurrence of old in the file at path with
326 // replacement. It keeps the mode of the file.
327 func (b *Toolbox) edit(path, old, replacement string) (string, error) {
329 data, err := os.ReadFile(path)
335 return "", errors.New("old is empty")
338 count := strings.Count(string(data), old)
340 return "", fmt.Errorf("old occurs %d times in %s, not once", count, path)
343 info, err := os.Stat(path)
348 err = os.WriteFile(path, []byte(strings.Replace(string(data), old, replacement, 1)), info.Mode().Perm())
353 return "edited " + path, nil
356 // path returns path, relative to the working directory when it is not
358 func (b *Toolbox) path(path string) string {
359 if filepath.IsAbs(path) {
363 return filepath.Join(b.workdir, path)
366 // ToolZoomArguments are the arguments of zoom. A Name that is not nil names
367 // the subagent whose whole log zoom gives, and then ID and N are not used.
368 type ToolZoomArguments struct {
371 Name *string `json:"name"`
374 // ToolDateArguments are the arguments of date.
375 type ToolDateArguments struct {
379 // ToolShellArguments are the arguments of shell.
380 type ToolShellArguments struct {
381 Command string `json:"command"`
384 // ToolPathArguments are the arguments of read.
385 type ToolPathArguments struct {
386 Path string `json:"path"`
389 // ToolWriteArguments are the arguments of write.
390 type ToolWriteArguments struct {
391 Path string `json:"path"`
392 Text string `json:"text"`
395 // ToolEditArguments are the arguments of edit.
396 type ToolEditArguments struct {
397 Path string `json:"path"`
398 Old string `json:"old"`
399 New string `json:"new"`
402 // ToolSpawnArguments are the arguments of spawn. A nil Effort is the effort of
404 type ToolSpawnArguments struct {
405 Tasks []string `json:"tasks"`
406 Effort *string `json:"effort"`
409 // ToolTellArguments are the arguments of tell.
410 type ToolTellArguments struct {
411 Name string `json:"name"`
412 Message string `json:"message"`
415 // ToolWhatsAppFindArguments are the arguments of whatsapp_find.
416 type ToolWhatsAppFindArguments struct {
417 Name string `json:"name"`
420 // ToolWhatsAppSendArguments are the arguments of whatsapp_send.
421 type ToolWhatsAppSendArguments struct {
422 ID string `json:"id"`
423 Chat string `json:"chat"`
424 Text string `json:"text"`
427 // ToolHeadTail keeps the first and the last toolClipBytes bytes written to it,
428 // and counts the bytes between them that it drops.
429 type ToolHeadTail struct {
435 // String returns the output, clipped when it is too long. Invalid UTF-8 becomes
436 // the replacement character.
437 func (h *ToolHeadTail) String() string {
438 replacement := string(utf8.RuneError)
440 return toolClip(strings.ToValidUTF8(string(h.head)+string(h.tail), replacement))
443 head := toolFirstCharacters(strings.ToValidUTF8(string(h.head), replacement), toolClipCharacters/2)
444 tail := toolLastCharacters(strings.ToValidUTF8(string(h.tail), replacement), toolClipCharacters/2)
445 cut := len(h.head) + h.dropped + len(h.tail) - len(head) - len(tail)
447 return toolJoinClipped(head, tail, cut)
450 // Write keeps the bytes of p at the ends of the output.
451 func (h *ToolHeadTail) Write(p []byte) (int, error) {
453 room := min(toolClipBytes-len(h.head), len(p))
454 h.head = append(h.head, p[:room]...)
457 h.tail = append(h.tail, p...)
458 if len(h.tail) > toolClipBytes {
459 cut := len(h.tail) - toolClipBytes
461 h.tail = bytes.Clone(h.tail[cut:])
467 // toolCallText returns the log text of a tool call: the name and the JSON
469 func toolCallText(name, arguments string) string {
470 return name + " " + arguments
473 // toolClip returns text when it has at most toolClipCharacters characters.
474 // Otherwise it returns the first and the last toolClipCharacters/2 characters,
475 // with a mark of the cut between them.
476 func toolClip(text string) string {
477 if utf8.RuneCountInString(text) <= toolClipCharacters {
481 head := toolFirstCharacters(text, toolClipCharacters/2)
482 tail := toolLastCharacters(text, toolClipCharacters/2)
484 return toolJoinClipped(head, tail, len(text)-len(head)-len(tail))
487 // toolDecodeArguments decodes the JSON arguments of a call into args. An
488 // unknown field is an error.
489 func toolDecodeArguments(arguments string, args any) error {
490 decoder := json.NewDecoder(strings.NewReader(arguments))
491 decoder.DisallowUnknownFields()
492 err := decoder.Decode(args)
494 return fmt.Errorf("arguments: %w", err)
500 // toolFirstCharacters returns the first count characters of text.
501 func toolFirstCharacters(text string, count int) string {
504 if end >= len(text) {
508 _, size := utf8.DecodeRuneInString(text[end:])
515 // toolLastCharacters returns the last count characters of text.
516 func toolLastCharacters(text string, count int) string {
523 _, size := utf8.DecodeLastRuneInString(text[:start])
530 // toolJoinClipped joins the head and the tail of a clipped output with a mark
532 func toolJoinClipped(head, tail string, cut int) string {
533 return head + "\n[... " + strconv.Itoa(cut) + " bytes cut ...]\n" + tail