diff --git a/pkg/nodes/registry_server.go b/pkg/nodes/registry_server.go index 6b2dae1..8af4d4a 100644 --- a/pkg/nodes/registry_server.go +++ b/pkg/nodes/registry_server.go @@ -15,6 +15,7 @@ import ( "path/filepath" "regexp" "runtime/debug" + "sort" "strconv" "strings" "syscall" @@ -90,6 +91,7 @@ func (s *RegistryServer) Start(ctx context.Context) error { mux.HandleFunc("/webui/api/memory", s.handleWebUIMemory) mux.HandleFunc("/webui/api/task_audit", s.handleWebUITaskAudit) mux.HandleFunc("/webui/api/task_queue", s.handleWebUITaskQueue) + mux.HandleFunc("/webui/api/tasks", s.handleWebUITasks) mux.HandleFunc("/webui/api/exec_approvals", s.handleWebUIExecApprovals) mux.HandleFunc("/webui/api/logs/stream", s.handleWebUILogsStream) mux.HandleFunc("/webui/api/logs/recent", s.handleWebUILogsRecent) @@ -1557,6 +1559,111 @@ func (s *RegistryServer) handleWebUITaskQueue(w http.ResponseWriter, r *http.Req _ = json.NewEncoder(w).Encode(map[string]interface{}{"ok": true, "running": running, "items": items, "stats": stats}) } +func (s *RegistryServer) handleWebUITasks(w http.ResponseWriter, r *http.Request) { + if !s.checkAuth(r) { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + tasksPath := filepath.Join(strings.TrimSpace(s.workspacePath), "memory", "tasks.json") + if r.Method == http.MethodGet { + b, err := os.ReadFile(tasksPath) + if err != nil { + _ = json.NewEncoder(w).Encode(map[string]interface{}{"ok": true, "items": []map[string]interface{}{}}) + return + } + var items []map[string]interface{} + if err := json.Unmarshal(b, &items); err != nil { + http.Error(w, "invalid tasks file", http.StatusInternalServerError) + return + } + sort.Slice(items, func(i, j int) bool { return fmt.Sprintf("%v", items[i]["updated_at"]) > fmt.Sprintf("%v", items[j]["updated_at"]) }) + _ = json.NewEncoder(w).Encode(map[string]interface{}{"ok": true, "items": items}) + return + } + if r.Method != http.MethodPost { + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + return + } + var body map[string]interface{} + if err := json.NewDecoder(r.Body).Decode(&body); err != nil { + http.Error(w, "invalid json", http.StatusBadRequest) + return + } + action := fmt.Sprintf("%v", body["action"]) + now := time.Now().UTC().Format(time.RFC3339) + items := []map[string]interface{}{} + if b, err := os.ReadFile(tasksPath); err == nil { + _ = json.Unmarshal(b, &items) + } + switch action { + case "create": + it, _ := body["item"].(map[string]interface{}) + if it == nil { + http.Error(w, "item required", http.StatusBadRequest) + return + } + id := fmt.Sprintf("%v", it["id"]) + if id == "" { + id = fmt.Sprintf("task_%d", time.Now().UnixNano()) + } + it["id"] = id + if fmt.Sprintf("%v", it["status"]) == "" { + it["status"] = "todo" + } + if fmt.Sprintf("%v", it["source"]) == "" { + it["source"] = "manual" + } + it["updated_at"] = now + items = append(items, it) + case "update": + id := fmt.Sprintf("%v", body["id"]) + it, _ := body["item"].(map[string]interface{}) + if id == "" || it == nil { + http.Error(w, "id and item required", http.StatusBadRequest) + return + } + updated := false + for _, row := range items { + if fmt.Sprintf("%v", row["id"]) == id { + for k, v := range it { + row[k] = v + } + row["id"] = id + row["updated_at"] = now + updated = true + break + } + } + if !updated { + http.Error(w, "task not found", http.StatusNotFound) + return + } + case "delete": + id := fmt.Sprintf("%v", body["id"]) + if id == "" { + http.Error(w, "id required", http.StatusBadRequest) + return + } + filtered := make([]map[string]interface{}, 0, len(items)) + for _, row := range items { + if fmt.Sprintf("%v", row["id"]) != id { + filtered = append(filtered, row) + } + } + items = filtered + default: + http.Error(w, "unsupported action", http.StatusBadRequest) + return + } + _ = os.MkdirAll(filepath.Dir(tasksPath), 0755) + out, _ := json.MarshalIndent(items, "", " ") + if err := os.WriteFile(tasksPath, out, 0644); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + _ = json.NewEncoder(w).Encode(map[string]interface{}{"ok": true}) +} + func (s *RegistryServer) handleWebUIExecApprovals(w http.ResponseWriter, r *http.Request) { if !s.checkAuth(r) { http.Error(w, "unauthorized", http.StatusUnauthorized) diff --git a/webui/src/i18n/index.ts b/webui/src/i18n/index.ts index 92d0713..7948bb9 100644 --- a/webui/src/i18n/index.ts +++ b/webui/src/i18n/index.ts @@ -27,6 +27,10 @@ const resources = { retryTask: 'Retry', completeTask: 'Complete', ignoreTask: 'Ignore', + taskCrud: 'Task CRUD', + createTask: 'Create', + updateTask: 'Update', + deleteTask: 'Delete', error: 'Error', noTaskAudit: 'No task audit records', selectTask: 'Select a task from the left list', @@ -188,6 +192,10 @@ const resources = { retryTask: '重试', completeTask: '完成', ignoreTask: '忽略', + taskCrud: '任务 CRUD', + createTask: '新建', + updateTask: '更新', + deleteTask: '删除', error: '错误', noTaskAudit: '暂无任务审计记录', selectTask: '请从左侧选择任务', diff --git a/webui/src/pages/TaskAudit.tsx b/webui/src/pages/TaskAudit.tsx index 7b197e1..a8e9dc0 100644 --- a/webui/src/pages/TaskAudit.tsx +++ b/webui/src/pages/TaskAudit.tsx @@ -32,6 +32,7 @@ const TaskAudit: React.FC = () => { const [loading, setLoading] = useState(false); const [sourceFilter, setSourceFilter] = useState('all'); const [statusFilter, setStatusFilter] = useState('all'); + const [draft, setDraft] = useState({ id: '', content: '', priority: 'normal', status: 'todo', source: 'manual', due_at: '' }); const fetchData = async () => { setLoading(true); @@ -73,6 +74,21 @@ const TaskAudit: React.FC = () => { console.error(e); } }; + + const saveTask = async (action: 'create'|'update'|'delete') => { + try { + const url = `/webui/api/tasks${q}`; + const payload: any = { action }; + if (action === 'create') payload.item = draft; + if (action === 'update') { payload.id = draft.id; payload.item = draft; } + if (action === 'delete') payload.id = draft.id; + const r = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); + if (!r.ok) throw new Error(await r.text()); + await fetchData(); + } catch (e) { + console.error(e); + } + }; const selectedPretty = useMemo(() => selected ? JSON.stringify(selected, null, 2) : '', [selected]); return ( @@ -111,7 +127,7 @@ const TaskAudit: React.FC = () => { return ( )} +
+
{t('taskCrud')}
+ setDraft({ ...draft, id: e.target.value })} placeholder="id" className="w-full px-2 py-1 text-xs bg-zinc-900 border border-zinc-700 rounded" /> +