]> git.giacomolli.uk - onechat/blobdiff - tools.go
Deliver messages between parent and child agents
[onechat] / tools.go
index 1cb098a564a0d968d6b447bba55f8c1b11defab7..0811981250aadef77453a31f17d53248eb4a067e 100644 (file)
--- a/tools.go
+++ b/tools.go
@@ -52,8 +52,8 @@ type ToolChat interface {
 // directory, with no sandbox. Zoom with a name reads the log of a subagent. The
 // toolbox of the agent has the runs of its subagents for spawn and tell, the
 // WhatsApp link for the WhatsApp tools, and the schedules for the schedule
 // directory, with no sandbox. Zoom with a name reads the log of a subagent. The
 // toolbox of the agent has the runs of its subagents for spawn and tell, the
 // WhatsApp link for the WhatsApp tools, and the schedules for the schedule
-// tools. The toolbox of a subagent has none of them, so these are unknown tools
-// there.
+// tools. A child has no WhatsApp link. The toolbox of a subagent has none of
+// them, so these are unknown tools there.
 type Toolbox struct {
        log       ToolLog
        chat      ToolChat
 type Toolbox struct {
        log       ToolLog
        chat      ToolChat
@@ -94,11 +94,15 @@ func (b *Toolbox) Call(ctx context.Context, name, arguments string) (string, err
 
 // run runs one tool and returns its output, or its error as text.
 func (b *Toolbox) run(ctx context.Context, name, arguments string) string {
 
 // run runs one tool and returns its output, or its error as text.
 func (b *Toolbox) run(ctx context.Context, name, arguments string) string {
-       var output string
-       var err error
+       var (
+               output string
+               err    error
+       )
+
        switch name {
        case "zoom":
                var args ToolZoomArguments
        switch name {
        case "zoom":
                var args ToolZoomArguments
+
                err = toolDecodeArguments(arguments, &args)
                switch {
                case err != nil:
                err = toolDecodeArguments(arguments, &args)
                switch {
                case err != nil:
@@ -109,36 +113,42 @@ func (b *Toolbox) run(ctx context.Context, name, arguments string) string {
                }
        case "date":
                var args ToolDateArguments
                }
        case "date":
                var args ToolDateArguments
+
                err = toolDecodeArguments(arguments, &args)
                if err == nil {
                        output, err = b.date(args.ID)
                }
        case "shell":
                var args ToolShellArguments
                err = toolDecodeArguments(arguments, &args)
                if err == nil {
                        output, err = b.date(args.ID)
                }
        case "shell":
                var args ToolShellArguments
+
                err = toolDecodeArguments(arguments, &args)
                if err == nil {
                        output = b.shell(ctx, args.Command)
                }
        case "read":
                var args ToolPathArguments
                err = toolDecodeArguments(arguments, &args)
                if err == nil {
                        output = b.shell(ctx, args.Command)
                }
        case "read":
                var args ToolPathArguments
+
                err = toolDecodeArguments(arguments, &args)
                if err == nil {
                        output, err = b.read(args.Path)
                }
        case "write":
                var args ToolWriteArguments
                err = toolDecodeArguments(arguments, &args)
                if err == nil {
                        output, err = b.read(args.Path)
                }
        case "write":
                var args ToolWriteArguments
+
                err = toolDecodeArguments(arguments, &args)
                if err == nil {
                        output, err = b.write(args.Path, args.Text)
                }
        case "edit":
                var args ToolEditArguments
                err = toolDecodeArguments(arguments, &args)
                if err == nil {
                        output, err = b.write(args.Path, args.Text)
                }
        case "edit":
                var args ToolEditArguments
+
                err = toolDecodeArguments(arguments, &args)
                if err == nil {
                        output, err = b.edit(args.Path, args.Old, args.New)
                }
        case "spawn":
                var args ToolSpawnArguments
                err = toolDecodeArguments(arguments, &args)
                if err == nil {
                        output, err = b.edit(args.Path, args.Old, args.New)
                }
        case "spawn":
                var args ToolSpawnArguments
+
                err = toolDecodeArguments(arguments, &args)
                switch {
                case b.runs == nil:
                err = toolDecodeArguments(arguments, &args)
                switch {
                case b.runs == nil:
@@ -148,6 +158,7 @@ func (b *Toolbox) run(ctx context.Context, name, arguments string) string {
                }
        case "tell":
                var args ToolTellArguments
                }
        case "tell":
                var args ToolTellArguments
+
                err = toolDecodeArguments(arguments, &args)
                switch {
                case b.runs == nil:
                err = toolDecodeArguments(arguments, &args)
                switch {
                case b.runs == nil:
@@ -157,6 +168,7 @@ func (b *Toolbox) run(ctx context.Context, name, arguments string) string {
                }
        case "whatsapp_find":
                var args ToolWhatsAppFindArguments
                }
        case "whatsapp_find":
                var args ToolWhatsAppFindArguments
+
                err = toolDecodeArguments(arguments, &args)
                switch {
                case b.whatsapp == nil:
                err = toolDecodeArguments(arguments, &args)
                switch {
                case b.whatsapp == nil:
@@ -166,6 +178,7 @@ func (b *Toolbox) run(ctx context.Context, name, arguments string) string {
                }
        case "whatsapp_send":
                var args ToolWhatsAppSendArguments
                }
        case "whatsapp_send":
                var args ToolWhatsAppSendArguments
+
                err = toolDecodeArguments(arguments, &args)
                switch {
                case b.whatsapp == nil:
                err = toolDecodeArguments(arguments, &args)
                switch {
                case b.whatsapp == nil:
@@ -175,6 +188,7 @@ func (b *Toolbox) run(ctx context.Context, name, arguments string) string {
                }
        case "schedule_set":
                var args ToolScheduleSetArguments
                }
        case "schedule_set":
                var args ToolScheduleSetArguments
+
                err = toolDecodeArguments(arguments, &args)
                switch {
                case b.schedules == nil:
                err = toolDecodeArguments(arguments, &args)
                switch {
                case b.schedules == nil:
@@ -192,6 +206,7 @@ func (b *Toolbox) run(ctx context.Context, name, arguments string) string {
                }
        case "schedule_remove":
                var args ToolScheduleRemoveArguments
                }
        case "schedule_remove":
                var args ToolScheduleRemoveArguments
+
                err = toolDecodeArguments(arguments, &args)
                switch {
                case b.schedules == nil:
                err = toolDecodeArguments(arguments, &args)
                switch {
                case b.schedules == nil:
@@ -241,7 +256,9 @@ func (b *Toolbox) zoom(id, n int) (string, error) {
        }
 
        level := bits.TrailingZeros(uint(n)) - 1
        }
 
        level := bits.TrailingZeros(uint(n)) - 1
+
        var lines []string
        var lines []string
+
        for _, index := range []int{2 * id / n, 2*id/n + 1} {
                node, built, err := b.chat.Node(level, index)
                if err != nil {
        for _, index := range []int{2 * id / n, 2*id/n + 1} {
                node, built, err := b.chat.Node(level, index)
                if err != nil {
@@ -298,7 +315,9 @@ func (b *Toolbox) shell(ctx context.Context, command string) string {
        err := cmd.Run()
 
        text := output.String()
        err := cmd.Run()
 
        text := output.String()
+
        var exit *exec.ExitError
        var exit *exec.ExitError
+
        switch {
        case ctx.Err() != nil:
                text += "\n[interrupted]"
        switch {
        case ctx.Err() != nil:
                text += "\n[interrupted]"
@@ -530,6 +549,7 @@ func toolClip(text string) string {
 func toolDecodeArguments(arguments string, args any) error {
        decoder := json.NewDecoder(strings.NewReader(arguments))
        decoder.DisallowUnknownFields()
 func toolDecodeArguments(arguments string, args any) error {
        decoder := json.NewDecoder(strings.NewReader(arguments))
        decoder.DisallowUnknownFields()
+
        err := decoder.Decode(args)
        if err != nil {
                return fmt.Errorf("arguments: %w", err)
        err := decoder.Decode(args)
        if err != nil {
                return fmt.Errorf("arguments: %w", err)