]> git.giacomolli.uk - onechat/blobdiff - tools.go
Import WhatsApp history into a child
[onechat] / tools.go
index 0811981250aadef77453a31f17d53248eb4a067e..a75514f984964224f0e1075d06281cda3be79cb0 100644 (file)
--- a/tools.go
+++ b/tools.go
@@ -49,11 +49,13 @@ type ToolChat interface {
 // the log as it happens: the call as a tool message, the result as an echo
 // message. The call is durable before its tool runs, so the log never misses an
 // action outside the agent. Commands and files run as the user in the working
-// 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. A child has no WhatsApp link. The toolbox of a subagent has none of
-// them, so these are unknown tools there.
+// directory, with no sandbox. Zoom with a name reads a child's view or a
+// subagent's log. 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. Each permanent agent has the parent-child queue. Only
+// the root has child creation and source routes. A child has neither of them
+// nor a WhatsApp link. The toolbox of a subagent has none of them, so these are
+// unknown tools there.
 type Toolbox struct {
        log       ToolLog
        chat      ToolChat
@@ -62,6 +64,10 @@ type Toolbox struct {
        whatsapp  *WhatsApp
        schedules *Schedules
        workdir   string
+       agentName string
+       messages  *DaemonMessages
+       children  *DaemonChildren
+       routes    *Routes
 }
 
 // Call logs the call of the tool name with its JSON arguments, makes the log
@@ -156,6 +162,46 @@ func (b *Toolbox) run(ctx context.Context, name, arguments string) string {
                case err == nil:
                        output, err = b.runs.Spawn(ctx, args.Tasks, args.Effort)
                }
+       case "agent_create":
+               var args ToolAgentCreateArguments
+
+               err = toolDecodeArguments(arguments, &args)
+               switch {
+               case b.children == nil:
+                       err = fmt.Errorf("unknown tool %q", name)
+               case err == nil:
+                       output, err = b.children.Create(args.Name, args.Responsibility)
+               }
+       case "route", "unroute", "routes":
+               var args ToolRouteArguments
+
+               switch name {
+               case "routes":
+                       err = toolDecodeArguments(arguments, &struct{}{})
+               case "unroute":
+                       var source ToolRouteSourceArguments
+
+                       err = toolDecodeArguments(arguments, &source)
+                       args.Source = source.Source
+               default:
+                       err = toolDecodeArguments(arguments, &args)
+               }
+
+               switch {
+               case b.routes == nil:
+                       err = fmt.Errorf("unknown tool %q", name)
+               case err != nil:
+               case name == "routes":
+                       output = b.routes.List()
+               case name == "route" && (b.messages == nil || b.messages.Child(b.agentName, args.Agent) == nil):
+                       err = errors.New("the agent must be a child of the root")
+               default:
+                       err = b.routes.Set(args.Source, args.Agent)
+                       output = "Route removed."
+                       if name == "route" {
+                               output = "Route set."
+                       }
+               }
        case "tell":
                var args ToolTellArguments
 
@@ -163,6 +209,9 @@ func (b *Toolbox) run(ctx context.Context, name, arguments string) string {
                switch {
                case b.runs == nil:
                        err = fmt.Errorf("unknown tool %q", name)
+               case err == nil && b.messages != nil && b.messages.Child(b.agentName, args.Name) != nil:
+                       err = b.messages.Tell(b.agentName, args.Name, args.Message)
+                       output = "Sent. " + args.Name + "'s reply will reach you as a message."
                case err == nil:
                        output, err = b.runs.Tell(ctx, args.Name, args.Message)
                }
@@ -225,8 +274,15 @@ func (b *Toolbox) run(ctx context.Context, name, arguments string) string {
        return output
 }
 
-// zoomAgent returns the whole log of the subagent name.
+// zoomAgent returns a child's view or the whole log of a subagent.
 func (b *Toolbox) zoomAgent(ctx context.Context, name string) (string, error) {
+       if b.messages != nil {
+               child := b.messages.Child(b.agentName, name)
+               if child != nil {
+                       return strings.Join(child.memory.ViewLines(nil), ""), nil
+               }
+       }
+
        subagent, err := b.subagents.Get(ctx, name)
        if err != nil {
                return "", err
@@ -410,8 +466,9 @@ func (b *Toolbox) path(path string) string {
        return filepath.Join(b.workdir, path)
 }
 
-// ToolZoomArguments are the arguments of zoom. A Name that is not nil names
-// the subagent whose whole log zoom gives, and then ID and N are not used.
+// ToolZoomArguments are the arguments of zoom. A Name that is not nil names the
+// child whose view or the subagent whose whole log zoom gives. ID and N are
+// then not used.
 type ToolZoomArguments struct {
        ID   int     `json:"id"`
        N    int     `json:"n"`
@@ -453,6 +510,23 @@ type ToolSpawnArguments struct {
        Effort *string  `json:"effort"`
 }
 
+// ToolAgentCreateArguments are the arguments of agent_create.
+type ToolAgentCreateArguments struct {
+       Name           string `json:"name"`
+       Responsibility string `json:"responsibility"`
+}
+
+// ToolRouteArguments are the arguments of route.
+type ToolRouteArguments struct {
+       Source string `json:"source"`
+       Agent  string `json:"agent"`
+}
+
+// ToolRouteSourceArguments are the arguments of unroute.
+type ToolRouteSourceArguments struct {
+       Source string `json:"source"`
+}
+
 // ToolTellArguments are the arguments of tell.
 type ToolTellArguments struct {
        Name    string `json:"name"`