95 lines
3.0 KiB
Go
95 lines
3.0 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"codex-agent-manager/internal/agents"
|
|
"codex-agent-manager/internal/app"
|
|
"codex-agent-manager/internal/projects"
|
|
"codex-agent-manager/internal/runtime"
|
|
"codex-agent-manager/internal/workflow"
|
|
)
|
|
|
|
func New(cfg app.Config) http.Handler {
|
|
mux := http.NewServeMux()
|
|
agentStore := agents.Store{CodexHome: cfg.CodexHome}
|
|
projectStore := projects.Store{CodexHome: cfg.CodexHome}
|
|
runtimeStore := runtime.Store{CodexHome: cfg.CodexHome}
|
|
workspaceRoot := cfg.WorkspaceRoot
|
|
if workspaceRoot == "" {
|
|
workspaceRoot = "."
|
|
}
|
|
workflowStore := workflow.Store{WorkspaceRoot: workspaceRoot, Runtime: runtimeStore}
|
|
|
|
mux.HandleFunc("/api/health", func(w http.ResponseWriter, r *http.Request) {
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
|
})
|
|
mux.HandleFunc("/api/agents", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "方法不允许"})
|
|
return
|
|
}
|
|
items, err := agentStore.List()
|
|
if err != nil {
|
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"items": items})
|
|
})
|
|
mux.HandleFunc("/api/projects", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "方法不允许"})
|
|
return
|
|
}
|
|
items, err := projectStore.List()
|
|
if err != nil {
|
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"items": items})
|
|
})
|
|
mux.HandleFunc("/api/runtime/threads", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "方法不允许"})
|
|
return
|
|
}
|
|
snapshot, err := runtimeStore.Snapshot()
|
|
if err != nil {
|
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"items": snapshot.Threads,
|
|
"edges": snapshot.SpawnEdges,
|
|
"goals": snapshot.Goals,
|
|
"source": snapshot.Source,
|
|
"sources": snapshot.Sources,
|
|
})
|
|
})
|
|
mux.HandleFunc("/api/workflow/events", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "方法不允许"})
|
|
return
|
|
}
|
|
view, err := workflowStore.View()
|
|
if err != nil {
|
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"items": view.Events,
|
|
"handoffEdges": view.HandoffEdges,
|
|
"phases": view.Phases,
|
|
"source": view.Source,
|
|
})
|
|
})
|
|
return mux
|
|
}
|
|
|
|
func writeJSON(w http.ResponseWriter, status int, body any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(body)
|
|
}
|