devloop.js
7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* DevLoop · REST Routes (P1)
*
* POST /api/devloop/projects
* body: { company_id, name?, requirement, mode?, target_path?, mock? }
* → 创建项目 + 跑 PM / Architect 两个 Agent (mock 模式产出 PRD/SDD)
* → 返 { pid, root_path, pm: {...}, architect: {...} }
*
* GET /api/devloop/projects/:pid/state
* → 返 { pid, project, units, prd_rev, sdd_rev, prd_site, sdd_site, timeline_tail }
*
* GET /api/devloop/projects (额外:列项目)
* → 返 { projects: [...] }
*
* 规格: docs/prd/newfunction/_spec/plan-migration.md · P1 验收
*/
const express = require('express');
const router = express.Router();
const loopController = require('../src/core/devloop/loop-controller');
const pathResolver = require('../services/devloop/path-resolver');
const sseBroker = require('../services/devloop/sse-broker');
const bugTracker = require('../services/devloop/bug-tracker');
const h = (fn) => async (req, res) => {
try { await fn(req, res); }
catch (e) {
console.error(`[DevLoop] ${req.method} ${req.path}:`, e.message);
res.status(e.statusCode || 500).json({ error: e.message });
}
};
// POST /api/devloop/projects
router.post('/projects', h(async (req, res) => {
const { company_id, requirement, mode, target_path, mock, name, mockFixtureDir } = req.body || {};
if (!company_id) {
return res.status(400).json({ error: 'missing field: company_id' });
}
if (!requirement || typeof requirement !== 'string') {
return res.status(400).json({ error: 'missing field: requirement' });
}
// 1. 创建项目 (sandbox 默认)
const proj = await loopController.createProject({
company_id,
mode: mode || 'sandbox',
target_path,
idea: name ? `${name}: ${requirement}` : requirement,
});
// 2. PM → Architect (mock 模式) — mockFixtureDir 可选,P3/P4 scenario 测试用
const phase = await loopController.runDocsPhase(proj.pid, requirement, {
mock: mock !== false,
mockFixtureDir,
});
res.json({
ok: true,
pid: proj.pid,
root_path: proj.root_path,
mode: proj.mode,
pm: {
files_written: phase.pm.files_written.length,
commit: phase.pm.commit?.sha || null,
mock: phase.pm.mock,
},
architect: {
files_written: phase.architect.files_written.length,
commit: phase.architect.commit?.sha || null,
mock: phase.architect.mock,
},
});
}));
// GET /api/devloop/projects
router.get('/projects', h(async (_req, res) => {
const projects = pathResolver.listProjects();
res.json({ projects });
}));
// POST /api/devloop/projects/:pid/run[?only=<unitId>] [body: { mockFixtureDir? }]
// - 不带 only: 跑完整 runProject (P3)
// - only=<unitId>: 单 unit drill-down (P2 兼容)
router.post('/projects/:pid/run', h(async (req, res) => {
const { pid } = req.params;
const { only } = req.query;
const { mockFixtureDir } = req.body || {};
try {
if (only) {
const result = await loopController.runUnit(pid, only, { mock: true, mockFixtureDir });
return res.json({
ok: result.ok, pid, unit_id: only,
attempts: result.attempts, final_state: result.final_state,
history: result.history.map(h => ({
agent: h.agent, attempt: h.attempt,
ok: h.ok, files: h.files?.length || 0,
commit: h.commit || null,
})),
});
}
// 完整 runProject
const result = await loopController.runProject(pid, { mock: true, mockFixtureDir });
return res.json({
ok: result.ok, pid,
final_state: result.final_state,
l2_rounds: result.l2_rounds,
run_id: result.run_id || null,
reason: result.reason || null,
history: result.history,
});
} catch (e) {
if (/Unknown project/.test(e.message)) {
return res.status(404).json({ error: 'unknown project: ' + pid });
}
if (/not found in project/.test(e.message)) {
return res.status(404).json({ error: 'unknown unit: ' + (only || '?') });
}
throw e;
}
}));
// GET /api/devloop/projects/:pid/stream — SSE
// client may send Last-Event-ID header → broker replays from there
router.get('/projects/:pid/stream', (req, res) => {
const { pid } = req.params;
try { pathResolver.resolveProjectRoot(pid); }
catch { return res.status(404).json({ error: 'unknown project: ' + pid }); }
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
res.flushHeaders?.();
const lastEventId = req.headers['last-event-id'] || req.query.lastEventId || null;
sseBroker.subscribe(pid, res, {
replayFromOffset: lastEventId != null ? lastEventId : undefined,
});
});
// P4: POST /api/devloop/projects/:pid/sign-off
// body: { by?, note? }
// 400 if status !== 'shipped'
router.post('/projects/:pid/sign-off', h(async (req, res) => {
const { pid } = req.params;
try { pathResolver.resolveProjectRoot(pid); }
catch { return res.status(404).json({ error: 'unknown project: ' + pid }); }
try {
const meta = await loopController.signOff(pid, req.body || {});
res.json({ ok: true, pid, status: meta.status });
} catch (e) {
if (e.name === 'InvalidProjectStatusTransition') {
return res.status(400).json({ error: e.message, from: e.from, to: e.to });
}
throw e;
}
}));
// P4: POST /api/devloop/projects/:pid/abandon
// body: { by?, reason?, note? }
router.post('/projects/:pid/abandon', h(async (req, res) => {
const { pid } = req.params;
try { pathResolver.resolveProjectRoot(pid); }
catch { return res.status(404).json({ error: 'unknown project: ' + pid }); }
try {
const meta = await loopController.abandon(pid, req.body || {});
res.json({ ok: true, pid, status: meta.status });
} catch (e) {
if (e.name === 'InvalidProjectStatusTransition') {
return res.status(400).json({ error: e.message, from: e.from, to: e.to });
}
throw e;
}
}));
// P4: POST /api/devloop/projects/:pid/feedback
// body: { text (required), by?, mockFixtureDir? }
// 400 if status ∈ {shipped-final, abandoned}
router.post('/projects/:pid/feedback', h(async (req, res) => {
const { pid } = req.params;
const { text, by, mockFixtureDir } = req.body || {};
if (!text) return res.status(400).json({ error: 'missing field: text' });
try { pathResolver.resolveProjectRoot(pid); }
catch { return res.status(404).json({ error: 'unknown project: ' + pid }); }
try {
const r = await loopController.submitFeedback(pid, text, { by, mockFixtureDir });
res.json(r);
} catch (e) {
if (e.name === 'InvalidProjectStatusTransition') {
return res.status(400).json({ error: e.message, from: e.from, to: e.to });
}
throw e;
}
}));
// GET /api/devloop/projects/:pid/bugs?status=open&unit_id=todo-be
router.get('/projects/:pid/bugs', h(async (req, res) => {
const { pid } = req.params;
try { pathResolver.resolveProjectRoot(pid); }
catch { return res.status(404).json({ error: 'unknown project: ' + pid }); }
const filter = {};
if (req.query.status) filter.status = String(req.query.status).split(',');
if (req.query.unit_id) filter.unit_id = req.query.unit_id;
if (req.query.severity) filter.severity = String(req.query.severity).split(',');
const bugs = bugTracker.list(pid, filter);
res.json({ bugs, total: bugs.length });
}));
// GET /api/devloop/projects/:pid/state
router.get('/projects/:pid/state', h(async (req, res) => {
try {
const state = loopController.getProjectState(req.params.pid);
res.json(state);
} catch (e) {
if (/Unknown project/.test(e.message)) {
return res.status(404).json({ error: 'unknown project: ' + req.params.pid });
}
throw e;
}
}));
module.exports = router;