]> git.giacomolli.uk - onechat/blobdiff - tools.go
Name timeouts for the operations they limit
[onechat] / tools.go
index a75514f984964224f0e1075d06281cda3be79cb0..bbd34cf67a7c82389e35aca4944bab7e33024fff 100644 (file)
--- a/tools.go
+++ b/tools.go
@@ -27,9 +27,9 @@ const (
        // output, enough for toolClipCharacters/2 characters of 4 bytes.
        toolClipBytes = 4 * toolClipCharacters / 2
 
        // output, enough for toolClipCharacters/2 characters of 4 bytes.
        toolClipBytes = 4 * toolClipCharacters / 2
 
-       // toolShellWaitDelay is how long a command can keep its output open
+       // toolShellOutputTimeout is how long a command can keep its output open
        // after it ends or after its kill.
        // after it ends or after its kill.
-       toolShellWaitDelay = 2 * time.Second
+       toolShellOutputTimeout = 2 * time.Second
 )
 
 // ToolLog is the log that the tools write their calls and results to.
 )
 
 // ToolLog is the log that the tools write their calls and results to.
@@ -68,13 +68,15 @@ type Toolbox struct {
        messages  *DaemonMessages
        children  *DaemonChildren
        routes    *Routes
        messages  *DaemonMessages
        children  *DaemonChildren
        routes    *Routes
+       images    *ImageStore
 }
 
 // Call logs the call of the tool name with its JSON arguments, makes the log
 }
 
 // Call logs the call of the tool name with its JSON arguments, makes the log
-// durable, runs the tool, and logs its clipped output, which it returns. A
-// failed tool gives its error as the output. An error of Call itself is an
-// error of the log. When ctx ends before the flush, the tool does not run.
-func (b *Toolbox) Call(ctx context.Context, name, arguments string) (string, error) {
+// durable, runs the tool, and logs its clipped text. It returns that text or
+// the text and image parts of a single-message zoom. Pixels never enter the
+// log. A failed tool gives its error as the output. An error of Call itself is
+// an error of the log. When ctx ends before the flush, the tool does not run.
+func (b *Toolbox) Call(ctx context.Context, name, arguments string) (any, error) {
        _, err := b.log.Append("tool", toolCallText(name, arguments))
        if err != nil {
                return "", fmt.Errorf("append tool call: %w", err)
        _, err := b.log.Append("tool", toolCallText(name, arguments))
        if err != nil {
                return "", fmt.Errorf("append tool call: %w", err)
@@ -90,12 +92,26 @@ func (b *Toolbox) Call(ctx context.Context, name, arguments string) (string, err
                output = toolClip(b.run(ctx, name, arguments))
        }
 
                output = toolClip(b.run(ctx, name, arguments))
        }
 
+       var (
+               result    any = output
+               resultErr error
+       )
+
+       if err == nil {
+               result, resultErr = imageToolOutput(b, name, arguments, output)
+       }
+
+       if resultErr != nil {
+               output = "error: " + resultErr.Error()
+               result = output
+       }
+
        _, err = b.log.Append("echo", output)
        if err != nil {
                return "", fmt.Errorf("append tool result: %w", err)
        }
 
        _, err = b.log.Append("echo", output)
        if err != nil {
                return "", fmt.Errorf("append tool result: %w", err)
        }
 
-       return output, nil
+       return result, nil
 }
 
 // run runs one tool and returns its output, or its error as text.
 }
 
 // run runs one tool and returns its output, or its error as text.
@@ -363,7 +379,7 @@ func (b *Toolbox) shell(ctx context.Context, command string) string {
        cmd.Cancel = func() error {
                return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
        }
        cmd.Cancel = func() error {
                return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
        }
-       cmd.WaitDelay = toolShellWaitDelay
+       cmd.WaitDelay = toolShellOutputTimeout
 
        output := &ToolHeadTail{}
        cmd.Stdout = output
 
        output := &ToolHeadTail{}
        cmd.Stdout = output