This repository has been archived on 2026-05-07. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
ccrm/server/internal/api/prompt.go

61 lines
1.4 KiB
Go

package api
import (
"encoding/json"
"net/http"
"strings"
"github.com/fmartingr/ccrm/server/internal/ws"
)
// PromptHandler handles prompt-related API endpoints.
type PromptHandler struct {
Hub *ws.Hub
}
type promptRequest struct {
Prompt string `json:"prompt"`
}
// Send handles POST /api/sessions/{id}/prompt
func (h *PromptHandler) Send(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/api/sessions/")
parts := strings.SplitN(path, "/", 2)
if len(parts) < 1 {
writeError(w, http.StatusBadRequest, "session ID required")
return
}
sessionName := parts[0]
var req promptRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
return
}
if req.Prompt == "" {
writeError(w, http.StatusBadRequest, "prompt is required")
return
}
// Send to daemon via WebSocket hub
daemonIDs := h.Hub.GetConnectedDaemonIDs()
if len(daemonIDs) == 0 {
writeError(w, http.StatusServiceUnavailable, "no daemon connected")
return
}
msg := map[string]string{
"type": "prompt.send",
"session_name": sessionName,
"prompt": req.Prompt,
}
// Send to first connected daemon (Phase 1: single machine)
h.Hub.SendToDaemon(daemonIDs[0], msg)
writeJSON(w, http.StatusAccepted, map[string]string{
"status": "sent",
"session": sessionName,
})
}