feat: add phase 3 readonly models

This commit is contained in:
Yoilun
2026-05-25 18:21:02 +08:00
parent 37e3d77110
commit d573bde194
18 changed files with 964 additions and 12 deletions

View File

@@ -6,11 +6,21 @@ import (
"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"})
@@ -27,6 +37,52 @@ func New(cfg app.Config) http.Handler {
}
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,
})
})
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
}

View File

@@ -26,7 +26,7 @@ description = "负责实现"
req := httptest.NewRequest(http.MethodGet, "/api/agents", nil)
rec := httptest.NewRecorder()
New(app.Config{CodexHome: root, HTTPAddr: "127.0.0.1:0"}).ServeHTTP(rec, req)
New(app.Config{CodexHome: root, HTTPAddr: "127.0.0.1:0", WorkspaceRoot: root}).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", rec.Code, rec.Body.String())
@@ -56,3 +56,101 @@ func TestAgentsEndpointRejectsUnsupportedMethod(t *testing.T) {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusMethodNotAllowed)
}
}
func TestProjectsEndpointReturnsProjects(t *testing.T) {
root := t.TempDir()
projectPath := filepath.Join(root, "repo")
if err := os.MkdirAll(projectPath, 0o755); err != nil {
t.Fatal(err)
}
config := `[projects."` + projectPath + `"]
trust_level = "trusted"
display_name = "Repo"
`
if err := os.WriteFile(filepath.Join(root, "config.toml"), []byte(config), 0o644); err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodGet, "/api/projects", nil)
rec := httptest.NewRecorder()
New(app.Config{CodexHome: root, HTTPAddr: "127.0.0.1:0", WorkspaceRoot: root}).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", rec.Code, rec.Body.String())
}
var body struct {
Items []struct {
Path string `json:"path"`
DisplayName string `json:"displayName"`
TrustLevel string `json:"trustLevel"`
DirectoryExists bool `json:"directoryExists"`
} `json:"items"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("invalid json: %v", err)
}
if len(body.Items) != 1 || body.Items[0].Path != projectPath || body.Items[0].DisplayName != "Repo" || !body.Items[0].DirectoryExists {
t.Fatalf("unexpected response: %#v", body)
}
}
func TestRuntimeThreadsEndpointReturnsEmptyWhenSQLiteMissing(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/api/runtime/threads", nil)
rec := httptest.NewRecorder()
New(app.Config{CodexHome: t.TempDir(), HTTPAddr: "127.0.0.1:0"}).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", rec.Code, rec.Body.String())
}
var body struct {
Items []any `json:"items"`
Source struct {
Kind string `json:"kind"`
Confidence string `json:"confidence"`
} `json:"source"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("invalid json: %v", err)
}
if len(body.Items) != 0 || body.Source.Kind != "sqlite_missing" || body.Source.Confidence != "low" {
t.Fatalf("unexpected response: %#v", body)
}
}
func TestWorkflowEventsEndpointReturnsEvents(t *testing.T) {
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "task_plan.md"), []byte("| 3 | in_progress | Runtime model |\n"), 0o644); err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodGet, "/api/workflow/events", nil)
rec := httptest.NewRecorder()
New(app.Config{CodexHome: root, HTTPAddr: "127.0.0.1:0", WorkspaceRoot: root}).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", rec.Code, rec.Body.String())
}
var body struct {
Items []struct {
Kind string `json:"kind"`
} `json:"items"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("invalid json: %v", err)
}
if len(body.Items) != 1 || body.Items[0].Kind != "plan_file" {
t.Fatalf("unexpected response: %#v", body)
}
}
func TestReadOnlyEndpointsRejectUnsupportedMethods(t *testing.T) {
for _, path := range []string{"/api/projects", "/api/runtime/threads", "/api/workflow/events"} {
req := httptest.NewRequest(http.MethodPost, path, nil)
rec := httptest.NewRecorder()
New(app.Config{CodexHome: t.TempDir(), HTTPAddr: "127.0.0.1:0"}).ServeHTTP(rec, req)
if rec.Code != http.StatusMethodNotAllowed {
t.Fatalf("%s status = %d, want %d", path, rec.Code, http.StatusMethodNotAllowed)
}
}
}