1package agent
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7
8 "github.com/kujtimiihoxha/termai/internal/app"
9 "github.com/kujtimiihoxha/termai/internal/llm/tools"
10 "github.com/kujtimiihoxha/termai/internal/message"
11)
12
13type agentTool struct {
14 parentSessionID string
15 app *app.App
16}
17
18const (
19 AgentToolName = "agent"
20)
21
22type AgentParams struct {
23 Prompt string `json:"prompt"`
24}
25
26func (b *agentTool) Info() tools.ToolInfo {
27 return tools.ToolInfo{
28 Name: AgentToolName,
29 Description: "Launch a new agent that has access to the following tools: GlobTool, GrepTool, LS, View. When you are searching for a keyword or file and are not confident that you will find the right match on the first try, use the Agent tool to perform the search for you. For example:\n\n- If you are searching for a keyword like \"config\" or \"logger\", or for questions like \"which file does X?\", the Agent tool is strongly recommended\n- If you want to read a specific file path, use the View or GlobTool tool instead of the Agent tool, to find the match more quickly\n- If you are searching for a specific class definition like \"class Foo\", use the GlobTool tool instead, to find the match more quickly\n\nUsage notes:\n1. Launch multiple agents concurrently whenever possible, to maximize performance; to do that, use a single message with multiple tool uses\n2. When the agent is done, it will return a single message back to you. The result returned by the agent is not visible to the user. To show the user the result, you should send a text message back to the user with a concise summary of the result.\n3. Each agent invocation is stateless. You will not be able to send additional messages to the agent, nor will the agent be able to communicate with you outside of its final report. Therefore, your prompt should contain a highly detailed task description for the agent to perform autonomously and you should specify exactly what information the agent should return back to you in its final and only message to you.\n4. The agent's outputs should generally be trusted\n5. IMPORTANT: The agent can not use Bash, Replace, Edit, so can not modify files. If you want to use these tools, use them directly instead of going through the agent.",
30 Parameters: map[string]any{
31 "prompt": map[string]any{
32 "type": "string",
33 "description": "The task for the agent to perform",
34 },
35 },
36 Required: []string{"prompt"},
37 }
38}
39
40func (b *agentTool) Run(ctx context.Context, call tools.ToolCall) (tools.ToolResponse, error) {
41 var params AgentParams
42 if err := json.Unmarshal([]byte(call.Input), ¶ms); err != nil {
43 return tools.NewTextErrorResponse(fmt.Sprintf("error parsing parameters: %s", err)), nil
44 }
45 if params.Prompt == "" {
46 return tools.NewTextErrorResponse("prompt is required"), nil
47 }
48
49 agent, err := NewTaskAgent(b.app)
50 if err != nil {
51 return tools.NewTextErrorResponse(fmt.Sprintf("error creating agent: %s", err)), nil
52 }
53
54 session, err := b.app.Sessions.CreateTaskSession(ctx, call.ID, b.parentSessionID, "New Agent Session")
55 if err != nil {
56 return tools.NewTextErrorResponse(fmt.Sprintf("error creating session: %s", err)), nil
57 }
58
59 err = agent.Generate(ctx, session.ID, params.Prompt)
60 if err != nil {
61 return tools.NewTextErrorResponse(fmt.Sprintf("error generating agent: %s", err)), nil
62 }
63
64 messages, err := b.app.Messages.List(ctx, session.ID)
65 if err != nil {
66 return tools.NewTextErrorResponse(fmt.Sprintf("error listing messages: %s", err)), nil
67 }
68 if len(messages) == 0 {
69 return tools.NewTextErrorResponse("no messages found"), nil
70 }
71
72 response := messages[len(messages)-1]
73 if response.Role != message.Assistant {
74 return tools.NewTextErrorResponse("no assistant message found"), nil
75 }
76
77 updatedSession, err := b.app.Sessions.Get(ctx, session.ID)
78 if err != nil {
79 return tools.NewTextErrorResponse(fmt.Sprintf("error: %s", err)), nil
80 }
81 parentSession, err := b.app.Sessions.Get(ctx, b.parentSessionID)
82 if err != nil {
83 return tools.NewTextErrorResponse(fmt.Sprintf("error: %s", err)), nil
84 }
85
86 parentSession.Cost += updatedSession.Cost
87 parentSession.PromptTokens += updatedSession.PromptTokens
88 parentSession.CompletionTokens += updatedSession.CompletionTokens
89
90 _, err = b.app.Sessions.Save(ctx, parentSession)
91 if err != nil {
92 return tools.NewTextErrorResponse(fmt.Sprintf("error: %s", err)), nil
93 }
94 return tools.NewTextResponse(response.Content().String()), nil
95}
96
97func NewAgentTool(parentSessionID string, app *app.App) tools.BaseTool {
98 return &agentTool{
99 parentSessionID: parentSessionID,
100 app: app,
101 }
102}