agent-tool.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package agent
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/kujtimiihoxha/termai/internal/app"
  7. "github.com/kujtimiihoxha/termai/internal/llm/tools"
  8. "github.com/kujtimiihoxha/termai/internal/message"
  9. )
  10. type agentTool struct {
  11. parentSessionID string
  12. app *app.App
  13. }
  14. const (
  15. AgentToolName = "agent"
  16. )
  17. type AgentParams struct {
  18. Prompt string `json:"prompt"`
  19. }
  20. func (b *agentTool) Info() tools.ToolInfo {
  21. return tools.ToolInfo{
  22. Name: AgentToolName,
  23. 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.",
  24. Parameters: map[string]any{
  25. "prompt": map[string]any{
  26. "type": "string",
  27. "description": "The task for the agent to perform",
  28. },
  29. },
  30. Required: []string{"prompt"},
  31. }
  32. }
  33. func (b *agentTool) Run(ctx context.Context, call tools.ToolCall) (tools.ToolResponse, error) {
  34. var params AgentParams
  35. if err := json.Unmarshal([]byte(call.Input), &params); err != nil {
  36. return tools.NewTextErrorResponse(fmt.Sprintf("error parsing parameters: %s", err)), nil
  37. }
  38. if params.Prompt == "" {
  39. return tools.NewTextErrorResponse("prompt is required"), nil
  40. }
  41. agent, err := NewTaskAgent(b.app)
  42. if err != nil {
  43. return tools.NewTextErrorResponse(fmt.Sprintf("error creating agent: %s", err)), nil
  44. }
  45. session, err := b.app.Sessions.CreateTaskSession(call.ID, b.parentSessionID, "New Agent Session")
  46. if err != nil {
  47. return tools.NewTextErrorResponse(fmt.Sprintf("error creating session: %s", err)), nil
  48. }
  49. err = agent.Generate(ctx, session.ID, params.Prompt)
  50. if err != nil {
  51. return tools.NewTextErrorResponse(fmt.Sprintf("error generating agent: %s", err)), nil
  52. }
  53. messages, err := b.app.Messages.List(session.ID)
  54. if err != nil {
  55. return tools.NewTextErrorResponse(fmt.Sprintf("error listing messages: %s", err)), nil
  56. }
  57. if len(messages) == 0 {
  58. return tools.NewTextErrorResponse("no messages found"), nil
  59. }
  60. response := messages[len(messages)-1]
  61. if response.Role != message.Assistant {
  62. return tools.NewTextErrorResponse("no assistant message found"), nil
  63. }
  64. updatedSession, err := b.app.Sessions.Get(session.ID)
  65. if err != nil {
  66. return tools.NewTextErrorResponse(fmt.Sprintf("error: %s", err)), nil
  67. }
  68. parentSession, err := b.app.Sessions.Get(b.parentSessionID)
  69. if err != nil {
  70. return tools.NewTextErrorResponse(fmt.Sprintf("error: %s", err)), nil
  71. }
  72. parentSession.Cost += updatedSession.Cost
  73. parentSession.PromptTokens += updatedSession.PromptTokens
  74. parentSession.CompletionTokens += updatedSession.CompletionTokens
  75. _, err = b.app.Sessions.Save(parentSession)
  76. if err != nil {
  77. return tools.NewTextErrorResponse(fmt.Sprintf("error: %s", err)), nil
  78. }
  79. return tools.NewTextResponse(response.Content().String()), nil
  80. }
  81. func NewAgentTool(parentSessionID string, app *app.App) tools.BaseTool {
  82. return &agentTool{
  83. parentSessionID: parentSessionID,
  84. app: app,
  85. }
  86. }