Skip to content

Commit 1447777

Browse files
committed
feat(agent): add --prompt flag for direct prompt execution
- Add --prompt flag to allow sending a prompt to the agent directly - Add sendPrompt() method to handle HTTP request to agent - AgentHub URL was already using correct format (/agents/%s.json) Addresses PR review feedback for PR mudler#8891 Signed-off-by: localai-bot <localai-bot@users.noreply.github.com>
1 parent cd2cf9a commit 1447777

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

core/cli/agent.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"net/http"
99
"os"
1010
"os/signal"
11+
"bytes"
12+
"time"
1113
"strings"
1214
"syscall"
1315

@@ -44,6 +46,8 @@ type AgentRunCMD struct {
4446

4547
// Registry settings
4648
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"`
4751
}
4852

4953
func (a *AgentRunCMD) Run(ctx *cliContext.Context) error {
@@ -111,6 +115,11 @@ func (a *AgentRunCMD) Run(ctx *cliContext.Context) error {
111115

112116
xlog.Info("Agent started successfully", "name", agentConfig.Name)
113117

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+
114123
// Optionally, if the user specifies a prompt, we will ask the agent directly and exit.
115124
// No background service in that case.
116125

@@ -224,3 +233,50 @@ func isJSONFile(ref string) bool {
224233
info, err := os.Stat(ref)
225234
return err == nil && !info.IsDir()
226235
}
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

Comments
 (0)