|
8 | 8 | "net/http" |
9 | 9 | "os" |
10 | 10 | "os/signal" |
| 11 | + "bytes" |
| 12 | + "time" |
11 | 13 | "strings" |
12 | 14 | "syscall" |
13 | 15 |
|
@@ -44,6 +46,8 @@ type AgentRunCMD struct { |
44 | 46 |
|
45 | 47 | // Registry settings |
46 | 48 | AgentHubURL string `env:"LOCALAI_AGENT_HUB_URL" default:"https://agenthub.localai.io" help:"Agent hub URL for registry lookups" group:"registry"` |
| 49 | + // Direct prompt execution |
| 50 | + Prompt string `flag:"prompt" env:"AGENT_PROMPT" help:"Optional prompt to send to the agent directly and exit"` |
47 | 51 | } |
48 | 52 |
|
49 | 53 | func (a *AgentRunCMD) Run(ctx *cliContext.Context) error { |
@@ -111,6 +115,11 @@ func (a *AgentRunCMD) Run(ctx *cliContext.Context) error { |
111 | 115 |
|
112 | 116 | xlog.Info("Agent started successfully", "name", agentConfig.Name) |
113 | 117 |
|
| 118 | + // If a prompt was provided, send it to the agent and exit |
| 119 | + if a.Prompt != "" { |
| 120 | + return a.sendPrompt(agentConfig.Name) |
| 121 | + } |
| 122 | + |
114 | 123 | // Optionally, if the user specifies a prompt, we will ask the agent directly and exit. |
115 | 124 | // No background service in that case. |
116 | 125 |
|
@@ -224,3 +233,50 @@ func isJSONFile(ref string) bool { |
224 | 233 | info, err := os.Stat(ref) |
225 | 234 | return err == nil && !info.IsDir() |
226 | 235 | } |
| 236 | + |
| 237 | +// sendPrompt sends a prompt to the agent and prints the response. |
| 238 | +func (a *AgentRunCMD) sendPrompt(agentName string) error { |
| 239 | + xlog.Info("Sending prompt to agent", "name", agentName, "prompt", a.Prompt) |
| 240 | + |
| 241 | + // Construct the API request to send a message to the agent |
| 242 | + // The agent pool service exposes an endpoint for this |
| 243 | + url := fmt.Sprintf("%s/agent/%s/message", a.APIURL, agentName) |
| 244 | + |
| 245 | + reqBody := map[string]string{ |
| 246 | + "content": a.Prompt, |
| 247 | + } |
| 248 | + |
| 249 | + jsonData, err := json.Marshal(reqBody) |
| 250 | + if err != nil { |
| 251 | + return fmt.Errorf("failed to marshal prompt request: %w", err) |
| 252 | + } |
| 253 | + |
| 254 | + req, err := http.NewRequest("POST", url, strings.NewReader(string(jsonData))) |
| 255 | + if err != nil { |
| 256 | + return fmt.Errorf("failed to create request: %w", err) |
| 257 | + } |
| 258 | + |
| 259 | + req.Header.Set("Content-Type", "application/json") |
| 260 | + if a.APIKey != "" { |
| 261 | + req.Header.Set("Authorization", "Bearer " + a.APIKey) |
| 262 | + } |
| 263 | + |
| 264 | + client := &http.Client{Timeout: 5 * time.Minute} |
| 265 | + resp, err := client.Do(req) |
| 266 | + if err != nil { |
| 267 | + return fmt.Errorf("failed to send prompt: %w", err) |
| 268 | + } |
| 269 | + defer resp.Body.Close() |
| 270 | + |
| 271 | + if resp.StatusCode != http.StatusOK { |
| 272 | + body, _ := io.ReadAll(resp.Body) |
| 273 | + return fmt.Errorf("failed to send prompt: HTTP %d - %s", resp.StatusCode, string(body)) |
| 274 | + } |
| 275 | + |
| 276 | + // Stream the response |
| 277 | + fmt.Println("\nAgent response:") |
| 278 | + fmt.Println(string(bytes.Buffer{})) |
| 279 | + |
| 280 | + _, err = io.Copy(os.Stdout, resp.Body) |
| 281 | + return err |
| 282 | +} |
0 commit comments