diff --git a/cmd/Assist/main.go b/cmd/Assist/main.go index 073fb6a..065df71 100644 --- a/cmd/Assist/main.go +++ b/cmd/Assist/main.go @@ -3,6 +3,7 @@ package main import ( "anvillm/internal/sandbox" + "encoding/json" "flag" "fmt" "log" @@ -354,6 +355,42 @@ func sendPrompt(id, prompt string) error { if !isConnected() { return fmt.Errorf("not connected to anvilsrv") } + + // Create message JSON + msg := map[string]interface{}{ + "to": id, + "type": "PROMPT", + "subject": "User prompt", + "body": prompt, + } + msgJSON, err := json.Marshal(msg) + if err != nil { + return fmt.Errorf("failed to marshal message: %w", err) + } + + // Write to user outbox + timestamp := time.Now().Unix() + filename := fmt.Sprintf("msg-%d.json", timestamp) + path := filepath.Join("user/outbox", filename) + + fid, err := fs.Create(path, plan9.OWRITE, 0644) + if err != nil { + return fmt.Errorf("failed to create message file: %w", err) + } + defer fid.Close() + + _, err = fid.Write(msgJSON) + if err != nil { + return fmt.Errorf("failed to write message: %w", err) + } + + return nil +} + +func sendPromptDirect(id, prompt string) error { + if !isConnected() { + return fmt.Errorf("not connected to anvilsrv") + } path := filepath.Join(id, "in") fid, err := fs.Open(path, plan9.OWRITE) if err != nil { @@ -570,8 +607,15 @@ func handlePromptWindow(w *acme.Win, sess *SessionInfo) { } prompt := strings.TrimSpace(string(body)) if prompt != "" { - if err := sendPrompt(sess.ID, prompt); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send prompt: %v\n", err) + var sendErr error + if cmd == "Prompt" { + sendErr = sendPromptDirect(sess.ID, prompt) + } else { + sendErr = sendPrompt(sess.ID, prompt) + } + if sendErr != nil { + fmt.Fprintf(os.Stderr, "Failed to send %s: %v\n", strings.ToLower(cmd), sendErr) + continue } w.Ctl("delete") return