|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + // Packages |
| 8 | + markdown "github.com/MichaelMure/go-term-markdown" |
| 9 | + agent "github.com/mutablelogic/go-client/pkg/agent" |
| 10 | +) |
| 11 | + |
| 12 | +///////////////////////////////////////////////////////////////////// |
| 13 | +// TYPES |
| 14 | + |
| 15 | +type ChatCmd struct { |
| 16 | + Prompt string `arg:"" optional:"" help:"The prompt to generate a response for"` |
| 17 | + Agent string `flag:"agent" help:"The agent to use"` |
| 18 | + Model string `flag:"model" help:"The model to use"` |
| 19 | + Stream bool `flag:"stream" help:"Stream the response"` |
| 20 | +} |
| 21 | + |
| 22 | +///////////////////////////////////////////////////////////////////// |
| 23 | +// PUBLIC METHODS |
| 24 | + |
| 25 | +func (cmd *ChatCmd) Run(globals *Globals) error { |
| 26 | + // Get the agent and the model |
| 27 | + model_agent, model := globals.getModel(globals.ctx, cmd.Agent, cmd.Model) |
| 28 | + if model_agent == nil || model == nil { |
| 29 | + return fmt.Errorf("model %q not found, or not set on command line", globals.state.Model) |
| 30 | + } |
| 31 | + |
| 32 | + // Generate the options |
| 33 | + opts := make([]agent.Opt, 0) |
| 34 | + if cmd.Stream { |
| 35 | + opts = append(opts, agent.OptStream(func(r agent.Response) { |
| 36 | + fmt.Println(r) |
| 37 | + })) |
| 38 | + } |
| 39 | + |
| 40 | + // Add tools |
| 41 | + if tools := globals.getTools(); len(tools) > 0 { |
| 42 | + opts = append(opts, agent.OptTools(tools...)) |
| 43 | + } |
| 44 | + |
| 45 | + // If the prompt is empty, then we're in interative mode |
| 46 | + context := []agent.Context{} |
| 47 | + if cmd.Prompt == "" { |
| 48 | + if globals.term == nil { |
| 49 | + return fmt.Errorf("prompt is empty and not in interactive mode") |
| 50 | + } |
| 51 | + } else { |
| 52 | + context = append(context, model_agent.UserPrompt(cmd.Prompt)) |
| 53 | + } |
| 54 | + |
| 55 | +FOR_LOOP: |
| 56 | + for { |
| 57 | + // When there is no context, create some |
| 58 | + if len(context) == 0 { |
| 59 | + if prompt, err := globals.term.ReadLine(model.Name() + "> "); err != nil { |
| 60 | + return err |
| 61 | + } else if prompt == "" { |
| 62 | + break FOR_LOOP |
| 63 | + } else { |
| 64 | + context = append(context, model_agent.UserPrompt(prompt)) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Generate a chat completion |
| 69 | + response, err := model_agent.Generate(globals.ctx, model, context, opts...) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + // If the response is a tool call, then run the tool |
| 75 | + if response.ToolCall != nil { |
| 76 | + result, err := globals.runTool(globals.ctx, response.ToolCall) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + response.Context = append(response.Context, result) |
| 81 | + } else { |
| 82 | + if globals.term != nil { |
| 83 | + w, _ := globals.term.Size() |
| 84 | + fmt.Println(string(markdown.Render(response.Text, w, 0))) |
| 85 | + } else { |
| 86 | + fmt.Println(response.Text) |
| 87 | + } |
| 88 | + |
| 89 | + // Make empty context |
| 90 | + response.Context = []agent.Context{} |
| 91 | + } |
| 92 | + |
| 93 | + // Context comes from the response |
| 94 | + context = response.Context |
| 95 | + } |
| 96 | + |
| 97 | + // Return success |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +///////////////////////////////////////////////////////////////////// |
| 102 | +// PRIVATE METHODS |
| 103 | + |
| 104 | +// Get the model, either from state or from the command-line flags. |
| 105 | +// If the model is not found, or there is another error, return nil |
| 106 | +func (globals *Globals) getModel(ctx context.Context, agent, model string) (agent.Agent, agent.Model) { |
| 107 | + state := globals.state |
| 108 | + if agent != "" { |
| 109 | + state.Agent = agent |
| 110 | + } |
| 111 | + if model != "" { |
| 112 | + state.Model = model |
| 113 | + } |
| 114 | + |
| 115 | + // Cycle through the agents and models to find the one we want |
| 116 | + for _, agent := range globals.agents { |
| 117 | + // Filter by agent |
| 118 | + if state.Agent != "" && agent.Name() != state.Agent { |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + // Retrieve the models for this agent |
| 123 | + models, err := agent.Models(ctx) |
| 124 | + if err != nil { |
| 125 | + continue |
| 126 | + } |
| 127 | + |
| 128 | + // Filter by model |
| 129 | + for _, model := range models { |
| 130 | + if state.Model != "" && model.Name() != state.Model { |
| 131 | + continue |
| 132 | + } |
| 133 | + |
| 134 | + // This is the model we're using.... |
| 135 | + state.Agent = agent.Name() |
| 136 | + state.Model = model.Name() |
| 137 | + return agent, model |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // No model found |
| 142 | + return nil, nil |
| 143 | +} |
| 144 | + |
| 145 | +// Get the tools |
| 146 | +func (globals *Globals) getTools() []agent.Tool { |
| 147 | + return globals.tools |
| 148 | +} |
| 149 | + |
| 150 | +// Return a tool by name. If the tool is not found, return nil |
| 151 | +func (globals *Globals) getTool(name string) agent.Tool { |
| 152 | + for _, tool := range globals.tools { |
| 153 | + if tool.Name() == name { |
| 154 | + return tool |
| 155 | + } |
| 156 | + } |
| 157 | + return nil |
| 158 | +} |
| 159 | + |
| 160 | +// Run a tool from a tool call, and return the result |
| 161 | +func (globals *Globals) runTool(ctx context.Context, call *agent.ToolCall) (*agent.ToolResult, error) { |
| 162 | + tool := globals.getTool(call.Name) |
| 163 | + if tool == nil { |
| 164 | + return nil, fmt.Errorf("tool %q not found", call.Name) |
| 165 | + } |
| 166 | + |
| 167 | + // Run the tool |
| 168 | + return tool.Run(ctx, call) |
| 169 | +} |
0 commit comments