]> git.giacomolli.uk - onechat/blob - tools.go
Order model and chat declarations
[onechat] / tools.go
1 package main
2
3 import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "errors"
8 "fmt"
9 "io"
10 "math/bits"
11 "os"
12 "os/exec"
13 "path/filepath"
14 "strconv"
15 "strings"
16 "syscall"
17 "time"
18 "unicode/utf8"
19 )
20
21 const (
22 // toolClipCharacters is the most characters of a tool output: its head
23 // and its tail, half each.
24 toolClipCharacters = 30000
25
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
29
30 // toolShellOutputTimeout is how long a command can keep its output open
31 // after it ends or after its kill.
32 toolShellOutputTimeout = 2 * time.Second
33 )
34
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
39 }
40
41 // ToolChat gives the messages and the nodes that zoom and date read.
42 type ToolChat interface {
43 Len() int
44 Message(id int) (Message, error)
45 Node(level, index int) (Node, bool, error)
46 }
47
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 a child's view or a
53 // subagent's log. The toolbox of the agent has the runs of its subagents for
54 // spawn and tell, the WhatsApp link for the WhatsApp tools, and the schedules
55 // for the schedule tools. Each permanent agent has the parent-child queue. Only
56 // the root has child creation and source routes. A child has neither of them
57 // nor a WhatsApp link. Only the root can show images to the user. The toolbox
58 // of a subagent has none of them, so these are unknown tools there.
59 type Toolbox struct {
60 log ToolLog
61 chat ToolChat
62 subagents *Subagents
63 runs *SubagentRuns
64 whatsapp *WhatsApp
65 schedules *Schedules
66 workdir string
67 agentName string
68 messages *DaemonMessages
69 children *DaemonChildren
70 routes *Routes
71 images *ImageStore
72 imageShow *ImageShow
73 }
74
75 // ToolZoomArguments are the arguments of zoom. A Name that is not nil names the
76 // child whose view or the subagent whose whole log zoom gives. ID and N are
77 // then not used.
78 type ToolZoomArguments struct {
79 ID int `json:"id"`
80 N int `json:"n"`
81 Name *string `json:"name"`
82 }
83
84 // ToolDateArguments are the arguments of date.
85 type ToolDateArguments struct {
86 ID int `json:"id"`
87 }
88
89 // ToolShellArguments are the arguments of shell.
90 type ToolShellArguments struct {
91 Command string `json:"command"`
92 }
93
94 // ToolPathArguments are the arguments of read.
95 type ToolPathArguments struct {
96 Path string `json:"path"`
97 }
98
99 // ToolWriteArguments are the arguments of write.
100 type ToolWriteArguments struct {
101 Path string `json:"path"`
102 Text string `json:"text"`
103 }
104
105 // ToolEditArguments are the arguments of edit.
106 type ToolEditArguments struct {
107 Path string `json:"path"`
108 Old string `json:"old"`
109 New string `json:"new"`
110 }
111
112 // ToolSpawnArguments are the arguments of spawn. A nil Effort is the effort of
113 // the agent.
114 type ToolSpawnArguments struct {
115 Tasks []string `json:"tasks"`
116 Effort *string `json:"effort"`
117 }
118
119 // ToolAgentCreateArguments are the arguments of agent_create.
120 type ToolAgentCreateArguments struct {
121 Name string `json:"name"`
122 Responsibility string `json:"responsibility"`
123 }
124
125 // ToolRouteArguments are the arguments of route.
126 type ToolRouteArguments struct {
127 Source string `json:"source"`
128 Agent string `json:"agent"`
129 }
130
131 // ToolRouteSourceArguments are the arguments of unroute.
132 type ToolRouteSourceArguments struct {
133 Source string `json:"source"`
134 }
135
136 // ToolTellArguments are the arguments of tell.
137 type ToolTellArguments struct {
138 Name string `json:"name"`
139 Message string `json:"message"`
140 }
141
142 // ToolImageShowArguments are the arguments of image_show.
143 type ToolImageShowArguments struct {
144 Image string `json:"image"`
145 Caption string `json:"caption"`
146 }
147
148 // ToolWhatsAppFindArguments are the arguments of whatsapp_find.
149 type ToolWhatsAppFindArguments struct {
150 Name string `json:"name"`
151 }
152
153 // ToolWhatsAppSendArguments are the arguments of whatsapp_send.
154 type ToolWhatsAppSendArguments struct {
155 ID string `json:"id"`
156 Chat string `json:"chat"`
157 Text string `json:"text"`
158 Image string `json:"image"`
159 }
160
161 // ToolScheduleSetArguments are the arguments of schedule_set. Exactly one of At
162 // and Cron is not nil.
163 type ToolScheduleSetArguments struct {
164 Text string `json:"text"`
165 At *string `json:"at"`
166 Cron *string `json:"cron"`
167 }
168
169 // ToolScheduleRemoveArguments are the arguments of schedule_remove.
170 type ToolScheduleRemoveArguments struct {
171 ID int `json:"id"`
172 }
173
174 // ToolHeadTail keeps the first and the last toolClipBytes bytes written to it,
175 // and counts the bytes between them that it drops.
176 type ToolHeadTail struct {
177 head []byte
178 tail []byte
179 dropped int
180 }
181
182 // Call logs the call of the tool name with its JSON arguments, makes the log
183 // durable, runs the tool, and logs its clipped text. It returns that text or
184 // the text and image parts of a single-message zoom. Pixels never enter the
185 // log. A failed tool gives its error as the output. An error of Call itself is
186 // an error of the log. When ctx ends before the flush, the tool does not run.
187 func (b *Toolbox) Call(ctx context.Context, name, arguments string) (any, error) {
188 _, err := b.log.Append("tool", toolCallText(name, arguments))
189 if err != nil {
190 return "", fmt.Errorf("append tool call: %w", err)
191 }
192
193 err = b.log.Flush(ctx)
194 if err != nil && ctx.Err() == nil {
195 return "", fmt.Errorf("flush tool call: %w", err)
196 }
197
198 output := "not run: the turn was interrupted"
199 if err == nil {
200 output = toolClip(b.run(ctx, name, arguments))
201 }
202
203 var (
204 result any = output
205 resultErr error
206 )
207
208 if err == nil {
209 result, resultErr = imageToolOutput(b, name, arguments, output)
210 }
211
212 if resultErr != nil {
213 output = "error: " + resultErr.Error()
214 result = output
215 }
216
217 _, err = b.log.Append("echo", output)
218 if err != nil {
219 return "", fmt.Errorf("append tool result: %w", err)
220 }
221
222 return result, nil
223 }
224
225 // run runs one tool and returns its output, or its error as text.
226 func (b *Toolbox) run(ctx context.Context, name, arguments string) string {
227 var (
228 output string
229 err error
230 )
231
232 switch name {
233 case "zoom":
234 var args ToolZoomArguments
235
236 err = toolDecodeArguments(arguments, &args)
237 switch {
238 case err != nil:
239 case args.Name != nil:
240 output, err = b.zoomAgent(ctx, *args.Name)
241 default:
242 output, err = b.zoom(args.ID, args.N)
243 }
244 case "date":
245 var args ToolDateArguments
246
247 err = toolDecodeArguments(arguments, &args)
248 if err == nil {
249 output, err = b.date(args.ID)
250 }
251 case "shell":
252 var args ToolShellArguments
253
254 err = toolDecodeArguments(arguments, &args)
255 if err == nil {
256 output = b.shell(ctx, args.Command)
257 }
258 case "read":
259 var args ToolPathArguments
260
261 err = toolDecodeArguments(arguments, &args)
262 if err == nil {
263 output, err = b.read(args.Path)
264 }
265 case "write":
266 var args ToolWriteArguments
267
268 err = toolDecodeArguments(arguments, &args)
269 if err == nil {
270 output, err = b.write(args.Path, args.Text)
271 }
272 case "edit":
273 var args ToolEditArguments
274
275 err = toolDecodeArguments(arguments, &args)
276 if err == nil {
277 output, err = b.edit(args.Path, args.Old, args.New)
278 }
279 case "spawn":
280 var args ToolSpawnArguments
281
282 err = toolDecodeArguments(arguments, &args)
283 switch {
284 case b.runs == nil:
285 err = fmt.Errorf("unknown tool %q", name)
286 case err == nil:
287 output, err = b.runs.Spawn(ctx, args.Tasks, args.Effort)
288 }
289 case "agent_create":
290 var args ToolAgentCreateArguments
291
292 err = toolDecodeArguments(arguments, &args)
293 switch {
294 case b.children == nil:
295 err = fmt.Errorf("unknown tool %q", name)
296 case err == nil:
297 output, err = b.children.Create(args.Name, args.Responsibility)
298 }
299 case "route", "unroute", "routes":
300 var args ToolRouteArguments
301
302 switch name {
303 case "routes":
304 err = toolDecodeArguments(arguments, &struct{}{})
305 case "unroute":
306 var source ToolRouteSourceArguments
307
308 err = toolDecodeArguments(arguments, &source)
309 args.Source = source.Source
310 default:
311 err = toolDecodeArguments(arguments, &args)
312 }
313
314 switch {
315 case b.routes == nil:
316 err = fmt.Errorf("unknown tool %q", name)
317 case err != nil:
318 case name == "routes":
319 output = b.routes.List()
320 case name == "route" && (b.messages == nil || b.messages.Child(b.agentName, args.Agent) == nil):
321 err = errors.New("the agent must be a child of the root")
322 default:
323 err = b.routes.Set(args.Source, args.Agent)
324 output = "Route removed."
325 if name == "route" {
326 output = "Route set."
327 }
328 }
329 case "tell":
330 var args ToolTellArguments
331
332 err = toolDecodeArguments(arguments, &args)
333 switch {
334 case b.runs == nil:
335 err = fmt.Errorf("unknown tool %q", name)
336 case err == nil && b.messages != nil && b.messages.Child(b.agentName, args.Name) != nil:
337 err = b.messages.Tell(b.agentName, args.Name, args.Message)
338 output = "Sent. " + args.Name + "'s reply will reach you as a message."
339 case err == nil:
340 output, err = b.runs.Tell(ctx, args.Name, args.Message)
341 }
342 case "image_show":
343 var args ToolImageShowArguments
344
345 err = toolDecodeArguments(arguments, &args)
346 switch {
347 case b.imageShow == nil:
348 err = fmt.Errorf("unknown tool %q", name)
349 case err == nil:
350 output, err = b.imageShow.Show(ctx, args.Image, args.Caption)
351 }
352 case "whatsapp_find":
353 var args ToolWhatsAppFindArguments
354
355 err = toolDecodeArguments(arguments, &args)
356 switch {
357 case b.whatsapp == nil:
358 err = fmt.Errorf("unknown tool %q", name)
359 case err == nil:
360 output, err = b.whatsapp.Find(ctx, args.Name)
361 }
362 case "whatsapp_send":
363 var args ToolWhatsAppSendArguments
364
365 err = toolDecodeArguments(arguments, &args)
366 switch {
367 case b.whatsapp == nil:
368 err = fmt.Errorf("unknown tool %q", name)
369 case err == nil:
370 output, err = b.whatsapp.Send(ctx, args.ID, args.Chat, args.Text, args.Image)
371 }
372 case "schedule_set":
373 var args ToolScheduleSetArguments
374
375 err = toolDecodeArguments(arguments, &args)
376 switch {
377 case b.schedules == nil:
378 err = fmt.Errorf("unknown tool %q", name)
379 case err == nil:
380 output, err = b.schedules.Set(args.Text, args.At, args.Cron)
381 }
382 case "schedule_list":
383 err = toolDecodeArguments(arguments, &struct{}{})
384 switch {
385 case b.schedules == nil:
386 err = fmt.Errorf("unknown tool %q", name)
387 case err == nil:
388 output = b.schedules.List()
389 }
390 case "schedule_remove":
391 var args ToolScheduleRemoveArguments
392
393 err = toolDecodeArguments(arguments, &args)
394 switch {
395 case b.schedules == nil:
396 err = fmt.Errorf("unknown tool %q", name)
397 case err == nil:
398 output, err = b.schedules.Remove(args.ID)
399 }
400 default:
401 err = fmt.Errorf("unknown tool %q", name)
402 }
403
404 if err != nil {
405 return "error: " + err.Error()
406 }
407
408 return output
409 }
410
411 // zoomAgent returns a child's view or the whole log of a subagent.
412 func (b *Toolbox) zoomAgent(ctx context.Context, name string) (string, error) {
413 if b.messages != nil {
414 child := b.messages.Child(b.agentName, name)
415 if child != nil {
416 return strings.Join(child.memory.ViewLines(nil), ""), nil
417 }
418 }
419
420 subagent, err := b.subagents.Get(ctx, name)
421 if err != nil {
422 return "", err
423 }
424
425 return subagent.Show()
426 }
427
428 // zoom returns message id whole for n = 1, or else the two lines under the line
429 // id+n.
430 func (b *Toolbox) zoom(id, n int) (string, error) {
431 if n < 1 || bits.OnesCount(uint(n)) != 1 || id < 0 || id%n != 0 {
432 return "", fmt.Errorf("line %d+%d does not exist: n must be a power of 2 and id a multiple of n", id, n)
433 }
434
435 if id+n > b.chat.Len() {
436 return "", fmt.Errorf("line %d+%d goes past the last message, %d", id, n, b.chat.Len()-1)
437 }
438
439 if n == 1 {
440 message, err := b.chat.Message(id)
441 if err != nil {
442 return "", fmt.Errorf("read zoom message: %w", err)
443 }
444
445 return leafSource(message), nil
446 }
447
448 level := bits.TrailingZeros(uint(n)) - 1
449
450 var lines []string
451
452 for _, index := range []int{2 * id / n, 2*id/n + 1} {
453 node, built, err := b.chat.Node(level, index)
454 if err != nil {
455 return "", fmt.Errorf("read zoom node: %w", err)
456 }
457
458 text := viewPending
459 if built {
460 text = strings.ReplaceAll(node.Text, "\n", " ")
461 }
462
463 lines = append(lines, fmt.Sprintf("%d+%d|%s", index<<level, 1<<level, text))
464 }
465
466 return strings.Join(lines, "\n"), nil
467 }
468
469 // date returns the date and time of message id, with its weekday.
470 func (b *Toolbox) date(id int) (string, error) {
471 if id < 0 || id >= b.chat.Len() {
472 return "", fmt.Errorf("message %d does not exist", id)
473 }
474
475 message, err := b.chat.Message(id)
476 if err != nil {
477 return "", fmt.Errorf("read dated message: %w", err)
478 }
479
480 date, err := time.Parse(time.RFC3339Nano, message.Date)
481 if err != nil {
482 return "", fmt.Errorf("parse message date: %w", err)
483 }
484
485 return date.Local().Format("Monday 2006-01-02 15:04:05 -07:00"), nil
486 }
487
488 // shell runs command with bash in the working directory and returns its output
489 // and error output. A failed command adds its exit status. When ctx ends, the
490 // whole process group of the command is killed.
491 func (b *Toolbox) shell(ctx context.Context, command string) string {
492 cmd := exec.CommandContext(ctx, "bash", "-c", command)
493 cmd.Dir = b.workdir
494 cmd.SysProcAttr = &syscall.SysProcAttr{
495 Setpgid: true,
496 }
497 cmd.Cancel = func() error {
498 return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
499 }
500 cmd.WaitDelay = toolShellOutputTimeout
501
502 output := &ToolHeadTail{}
503 cmd.Stdout = output
504 cmd.Stderr = output
505 err := cmd.Run()
506
507 text := output.String()
508
509 var exit *exec.ExitError
510
511 switch {
512 case ctx.Err() != nil:
513 text += "\n[interrupted]"
514 case errors.As(err, &exit):
515 text += "\n[" + exit.String() + "]"
516 case err != nil:
517 text += "\n[error: " + err.Error() + "]"
518 case text == "":
519 text = "(no output)"
520 }
521
522 return text
523 }
524
525 // read returns the text of the file at path.
526 func (b *Toolbox) read(path string) (text string, err error) {
527 file, err := os.Open(b.path(path))
528 if err != nil {
529 return "", fmt.Errorf("open tool file: %w", err)
530 }
531
532 defer func() {
533 err = errors.Join(err, file.Close())
534 }()
535
536 output := &ToolHeadTail{}
537 _, err = io.Copy(output, file)
538 if err != nil {
539 return "", fmt.Errorf("read tool file: %w", err)
540 }
541
542 return output.String(), nil
543 }
544
545 // write replaces the file at path with text, and makes its missing
546 // directories.
547 func (b *Toolbox) write(path, text string) (string, error) {
548 path = b.path(path)
549 err := os.MkdirAll(filepath.Dir(path), 0o755)
550 if err != nil {
551 return "", fmt.Errorf("create tool file directory: %w", err)
552 }
553
554 err = os.WriteFile(path, []byte(text), 0o644)
555 if err != nil {
556 return "", fmt.Errorf("write tool file: %w", err)
557 }
558
559 return fmt.Sprintf("wrote %d bytes to %s", len(text), path), nil
560 }
561
562 // edit replaces the one occurrence of old in the file at path with
563 // replacement. It keeps the mode of the file.
564 func (b *Toolbox) edit(path, old, replacement string) (string, error) {
565 path = b.path(path)
566 data, err := os.ReadFile(path)
567 if err != nil {
568 return "", fmt.Errorf("read file to edit: %w", err)
569 }
570
571 if old == "" {
572 return "", errors.New("old is empty")
573 }
574
575 count := strings.Count(string(data), old)
576 if count != 1 {
577 return "", fmt.Errorf("old occurs %d times in %s, not once", count, path)
578 }
579
580 info, err := os.Stat(path)
581 if err != nil {
582 return "", fmt.Errorf("read file mode: %w", err)
583 }
584
585 err = os.WriteFile(path, []byte(strings.Replace(string(data), old, replacement, 1)), info.Mode().Perm())
586 if err != nil {
587 return "", fmt.Errorf("write edited file: %w", err)
588 }
589
590 return "edited " + path, nil
591 }
592
593 // path returns path, relative to the working directory when it is not
594 // absolute.
595 func (b *Toolbox) path(path string) string {
596 if filepath.IsAbs(path) {
597 return path
598 }
599
600 return filepath.Join(b.workdir, path)
601 }
602
603 // String returns the output, clipped when it is too long. Invalid UTF-8 becomes
604 // the replacement character.
605 func (h *ToolHeadTail) String() string {
606 replacement := string(utf8.RuneError)
607 if h.dropped == 0 {
608 return toolClip(strings.ToValidUTF8(string(h.head)+string(h.tail), replacement))
609 }
610
611 head := toolFirstCharacters(strings.ToValidUTF8(string(h.head), replacement), toolClipCharacters/2)
612 tail := toolLastCharacters(strings.ToValidUTF8(string(h.tail), replacement), toolClipCharacters/2)
613 cut := len(h.head) + h.dropped + len(h.tail) - len(head) - len(tail)
614
615 return toolJoinClipped(head, tail, cut)
616 }
617
618 // Write keeps the bytes of p at the ends of the output.
619 func (h *ToolHeadTail) Write(p []byte) (int, error) {
620 n := len(p)
621 room := min(toolClipBytes-len(h.head), len(p))
622 h.head = append(h.head, p[:room]...)
623 p = p[room:]
624
625 h.tail = append(h.tail, p...)
626 if len(h.tail) > toolClipBytes {
627 cut := len(h.tail) - toolClipBytes
628 h.dropped += cut
629 h.tail = bytes.Clone(h.tail[cut:])
630 }
631
632 return n, nil
633 }
634
635 // toolCallText returns the log text of a tool call: the name and the JSON
636 // arguments.
637 func toolCallText(name, arguments string) string {
638 return name + " " + arguments
639 }
640
641 // toolClip returns text when it has at most toolClipCharacters characters.
642 // Otherwise it returns the first and the last toolClipCharacters/2 characters,
643 // with a mark of the cut between them.
644 func toolClip(text string) string {
645 if utf8.RuneCountInString(text) <= toolClipCharacters {
646 return text
647 }
648
649 head := toolFirstCharacters(text, toolClipCharacters/2)
650 tail := toolLastCharacters(text, toolClipCharacters/2)
651
652 return toolJoinClipped(head, tail, len(text)-len(head)-len(tail))
653 }
654
655 // toolDecodeArguments decodes the JSON arguments of a call into args. An
656 // unknown field is an error.
657 func toolDecodeArguments(arguments string, args any) error {
658 decoder := json.NewDecoder(strings.NewReader(arguments))
659 decoder.DisallowUnknownFields()
660
661 err := decoder.Decode(args)
662 if err != nil {
663 return fmt.Errorf("arguments: %w", err)
664 }
665
666 return nil
667 }
668
669 // toolFirstCharacters returns the first count characters of text.
670 func toolFirstCharacters(text string, count int) string {
671 end := 0
672 for range count {
673 if end >= len(text) {
674 break
675 }
676
677 _, size := utf8.DecodeRuneInString(text[end:])
678 end += size
679 }
680
681 return text[:end]
682 }
683
684 // toolLastCharacters returns the last count characters of text.
685 func toolLastCharacters(text string, count int) string {
686 start := len(text)
687 for range count {
688 if start <= 0 {
689 break
690 }
691
692 _, size := utf8.DecodeLastRuneInString(text[:start])
693 start -= size
694 }
695
696 return text[start:]
697 }
698
699 // toolJoinClipped joins the head and the tail of a clipped output with a mark
700 // of the cut bytes.
701 func toolJoinClipped(head, tail string, cut int) string {
702 return head + "\n[... " + strconv.Itoa(cut) + " bytes cut ...]\n" + tail
703 }