mpt-workflow 0.0.0-stage → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +144 -0
- package/LICENSE +21 -0
- package/README.md +323 -2
- package/README.zh-CN.md +272 -0
- package/dist/cli.js +479 -0
- package/dist/cli.js.map +1 -0
- package/dist/core/acl.js +120 -0
- package/dist/core/acl.js.map +1 -0
- package/dist/core/config.js +100 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/demo.js +50 -0
- package/dist/core/demo.js.map +1 -0
- package/dist/core/docs.js +291 -0
- package/dist/core/docs.js.map +1 -0
- package/dist/core/fsutil.js +89 -0
- package/dist/core/fsutil.js.map +1 -0
- package/dist/core/git.js +126 -0
- package/dist/core/git.js.map +1 -0
- package/dist/core/glob.js +36 -0
- package/dist/core/glob.js.map +1 -0
- package/dist/core/hooks.js +277 -0
- package/dist/core/hooks.js.map +1 -0
- package/dist/core/init.js +382 -0
- package/dist/core/init.js.map +1 -0
- package/dist/core/markdown.js +109 -0
- package/dist/core/markdown.js.map +1 -0
- package/dist/core/messages.js +365 -0
- package/dist/core/messages.js.map +1 -0
- package/dist/core/questions.js +240 -0
- package/dist/core/questions.js.map +1 -0
- package/dist/core/state.js +139 -0
- package/dist/core/state.js.map +1 -0
- package/dist/core/types.js +8 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/workflow.js +1050 -0
- package/dist/core/workflow.js.map +1 -0
- package/dist/mcp/server.js +852 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/version.js +2 -0
- package/dist/version.js.map +1 -0
- package/dist/web/public/index.html +1094 -0
- package/dist/web/server.js +249 -0
- package/dist/web/server.js.map +1 -0
- package/package.json +63 -4
|
@@ -0,0 +1,852 @@
|
|
|
1
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
2
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { Workflow } from '../core/workflow.js';
|
|
5
|
+
import { VERSION } from '../version.js';
|
|
6
|
+
import { describeAcl } from '../core/acl.js';
|
|
7
|
+
import { loadConfig } from '../core/config.js';
|
|
8
|
+
import { setRoleLease } from '../core/hooks.js';
|
|
9
|
+
import { questionsFile, appendQuestion, nextQuestionId } from '../core/questions.js';
|
|
10
|
+
import { logEvent } from '../core/state.js';
|
|
11
|
+
import { actorKey, formatMessage, canSend } from '../core/messages.js';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
const READ_ROLES = ['main', 'plan', 'todo', 'user'];
|
|
14
|
+
function text(s) {
|
|
15
|
+
return { content: [{ type: 'text', text: s }] };
|
|
16
|
+
}
|
|
17
|
+
function err(s) {
|
|
18
|
+
return { content: [{ type: 'text', text: s }], isError: true };
|
|
19
|
+
}
|
|
20
|
+
function statusText(wf, role, repo) {
|
|
21
|
+
const st = wf.programStatus();
|
|
22
|
+
const lines = [];
|
|
23
|
+
const me = role ? { role, repo: repo ?? undefined } : null;
|
|
24
|
+
const unread = me ? wf.msgs.unreadCount(me) : 0;
|
|
25
|
+
const blocking = me ? wf.msgs.blockingFor(me) : [];
|
|
26
|
+
if (me && (unread || blocking.length)) {
|
|
27
|
+
lines.push(`未读 ${unread} 条${blocking.length ? ` · blocking ${blocking.length} 条` : ''}(用 inbox 查看)`);
|
|
28
|
+
for (const m of blocking) {
|
|
29
|
+
lines.push(` [blocking] ${m.id} ${m.subject}`);
|
|
30
|
+
}
|
|
31
|
+
lines.push('');
|
|
32
|
+
}
|
|
33
|
+
lines.push(`程序: ${st.program}`);
|
|
34
|
+
lines.push(`根目录: ${st.root}`);
|
|
35
|
+
lines.push(`active repo: ${st.activeRepo ?? '(none)'}`);
|
|
36
|
+
if (role)
|
|
37
|
+
lines.push(`本会话角色: ${role}${repo ? ` (绑定 ${repo})` : ''}`);
|
|
38
|
+
lines.push('');
|
|
39
|
+
for (const p of st.phases) {
|
|
40
|
+
lines.push(`阶段 ${p.id}「${p.title}」 = ${p.status} [${p.repos.join(', ')}]`);
|
|
41
|
+
}
|
|
42
|
+
lines.push('');
|
|
43
|
+
lines.push('仓库:');
|
|
44
|
+
for (const r of st.repos) {
|
|
45
|
+
const m = r.currentMilestone;
|
|
46
|
+
const done = r.milestones.filter((x) => ['accepted', 'committed'].includes(x.status)).length;
|
|
47
|
+
lines.push(` ${r.id} ${done}/${r.milestones.length} M` +
|
|
48
|
+
(m
|
|
49
|
+
? ` 当前 ${m.id}「${m.title}」= ${m.status}` +
|
|
50
|
+
(m.evidence.length ? ` (证据 ${m.evidence.length})` : '')
|
|
51
|
+
: r.milestones.length === 0
|
|
52
|
+
? ' 无 M(缺 PLAN)'
|
|
53
|
+
: ' 全部完成') +
|
|
54
|
+
` [${r.git.branch}@${r.git.shortHead}${r.git.dirty ? ', dirty' : ''}]`);
|
|
55
|
+
for (const mm of r.milestones) {
|
|
56
|
+
lines.push(` ${mm.id} ${mm.status.padEnd(12)} ${mm.title}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const qs = st.questions;
|
|
60
|
+
const open = qs.filter((q) => q.status === 'open' || q.status === 'answered');
|
|
61
|
+
if (open.length) {
|
|
62
|
+
lines.push('');
|
|
63
|
+
lines.push('未闭合问题:');
|
|
64
|
+
for (const q of open) {
|
|
65
|
+
lines.push(` ${q.id} [${q.status}]${q.blocking ? ' [blocking]' : ''} ${q.from} → ${q.to}: ${q.context.split('\n')[0].slice(0, 100)}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (st.next.length) {
|
|
69
|
+
lines.push('');
|
|
70
|
+
lines.push('建议下一步:');
|
|
71
|
+
for (const n of st.next) {
|
|
72
|
+
lines.push(` [${n.actor}] ${n.action}${n.repo ? ` (${n.repo}${n.milestone ? '/' + n.milestone : ''})` : ''}`);
|
|
73
|
+
lines.push(` ${n.detail}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return lines.join('\n');
|
|
77
|
+
}
|
|
78
|
+
/* --------------------------------------------------------------------- */
|
|
79
|
+
function buildTools(_opts) {
|
|
80
|
+
const repoParam = z.string().optional().describe('仓库 id;默认用绑定仓或 active repo');
|
|
81
|
+
const msParam = z.string().describe('里程碑编号,如 M2');
|
|
82
|
+
const tools = [
|
|
83
|
+
{
|
|
84
|
+
name: 'bootstrap',
|
|
85
|
+
description: '声明本会话的 agent 角色并取得权限矩阵、当前状态和建议下一步。开始工作前先调用一次。',
|
|
86
|
+
roles: READ_ROLES,
|
|
87
|
+
schema: {
|
|
88
|
+
role: z.enum(['main', 'plan', 'todo', 'user']).describe('本会话角色'),
|
|
89
|
+
repo: z.string().optional().describe('绑定的仓库 id(plan/todo 必填)'),
|
|
90
|
+
},
|
|
91
|
+
handler: async (args, ctx) => {
|
|
92
|
+
const role = args.role;
|
|
93
|
+
const repo = args.repo || ctx.repo || null;
|
|
94
|
+
if ((role === 'plan' || role === 'todo') && !repo) {
|
|
95
|
+
return err(`角色 ${role} 必须指定 repo`);
|
|
96
|
+
}
|
|
97
|
+
if (repo)
|
|
98
|
+
ctx.wf.repoRef(repo);
|
|
99
|
+
setRoleLease(ctx.wf.root, { role, repo: repo ?? undefined, at: new Date().toISOString() });
|
|
100
|
+
ctx.role = role;
|
|
101
|
+
ctx.repo = repo;
|
|
102
|
+
const me = { role, repo: repo ?? undefined };
|
|
103
|
+
ctx.wf.msgs.ping(me);
|
|
104
|
+
ctx.wf.msgs.promoteQueued(me);
|
|
105
|
+
ctx.wf.msgs.expireDue();
|
|
106
|
+
const unread = ctx.wf.msgs.unreadCount(me);
|
|
107
|
+
const blocking = ctx.wf.msgs.blockingFor(me);
|
|
108
|
+
const recent = ctx.wf.msgs.forActor(me, { limit: 3 });
|
|
109
|
+
logEvent(ctx.wf.root, { role, tool: 'bootstrap', repo: repo ?? undefined, ok: true });
|
|
110
|
+
return [
|
|
111
|
+
`已绑定角色 ${role}${repo ? ` @ ${repo}` : ''}。`,
|
|
112
|
+
'',
|
|
113
|
+
describeAcl(role),
|
|
114
|
+
'',
|
|
115
|
+
unread
|
|
116
|
+
? `未读 ${unread} 条${blocking.length ? `(blocking ${blocking.length})` : ''}:`
|
|
117
|
+
: '没有未读消息。',
|
|
118
|
+
...blocking.map((m) => ` [blocking] ${m.id} ${m.subject}`),
|
|
119
|
+
...(unread ? recent.map((m) => ` ${m.id} [${m.status}] ${m.subject}`) : []),
|
|
120
|
+
'',
|
|
121
|
+
'下一步:status 看全局,next 看该做什么,inbox 看消息。',
|
|
122
|
+
].join('\n');
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
name: 'status',
|
|
127
|
+
description: '返回程序/仓库/里程碑/问题/下一步的完整状态。新会话第一件事。',
|
|
128
|
+
roles: READ_ROLES,
|
|
129
|
+
schema: {
|
|
130
|
+
repo: z.string().optional().describe('只看某个仓库'),
|
|
131
|
+
},
|
|
132
|
+
handler: (args, ctx) => {
|
|
133
|
+
const repo = args.repo;
|
|
134
|
+
if (!repo)
|
|
135
|
+
return statusText(ctx.wf, ctx.role, ctx.repo);
|
|
136
|
+
const st = ctx.wf.repoState(repo);
|
|
137
|
+
const lines = [
|
|
138
|
+
`${st.id} [${st.git.branch}@${st.git.shortHead}${st.git.dirty ? ', dirty' : ''}]`,
|
|
139
|
+
`PLAN: ${st.plan?.file ?? '(none)'} 阶段: ${st.phase ?? '?'}`,
|
|
140
|
+
'',
|
|
141
|
+
'里程碑:',
|
|
142
|
+
...st.milestones.map((m) => ` ${m.id} ${m.status.padEnd(12)} ${m.title}` +
|
|
143
|
+
(m.evidence.length ? ` 证据=${m.evidence.length}` : '') +
|
|
144
|
+
(m.sha ? ` sha=${m.sha}` : '')),
|
|
145
|
+
];
|
|
146
|
+
if (st.questions.length) {
|
|
147
|
+
lines.push('', '问题:');
|
|
148
|
+
for (const q of st.questions) {
|
|
149
|
+
lines.push(` ${q.id} [${q.status}] ${q.from} → ${q.to}${q.blocking ? ' [blocking]' : ''}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (st.problems.length)
|
|
153
|
+
lines.push('', '注意:', ...st.problems.map((p) => ` ! ${p}`));
|
|
154
|
+
return lines.join('\n');
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
name: 'next',
|
|
159
|
+
description: '根据状态机计算下一步该谁做什么,附理由。',
|
|
160
|
+
roles: READ_ROLES,
|
|
161
|
+
schema: {},
|
|
162
|
+
handler: (_a, ctx) => {
|
|
163
|
+
const me = { role: ctx.role, repo: ctx.repo ?? undefined };
|
|
164
|
+
const blocking = ctx.wf.msgs.blockingFor(me);
|
|
165
|
+
const st = ctx.wf.programStatus();
|
|
166
|
+
const lines = [];
|
|
167
|
+
if (blocking.length) {
|
|
168
|
+
lines.push('先处理 blocking 消息(ack 后才能推进):');
|
|
169
|
+
for (const m of blocking)
|
|
170
|
+
lines.push(` ${m.id} ${m.subject}`);
|
|
171
|
+
lines.push('');
|
|
172
|
+
}
|
|
173
|
+
if (!st.next.length) {
|
|
174
|
+
lines.push('没有识别到待办;检查程序配置。');
|
|
175
|
+
return lines.join('\n');
|
|
176
|
+
}
|
|
177
|
+
lines.push(st.next
|
|
178
|
+
.map((n) => `[${n.actor}] ${n.action}${n.repo ? ` — ${n.repo}${n.milestone ? '/' + n.milestone : ''}` : ''}\n ${n.detail}`)
|
|
179
|
+
.join('\n'));
|
|
180
|
+
return lines.join('\n');
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
name: 'doctor',
|
|
185
|
+
description: '检查环境、git 状态、缺文件、阻塞问题、待批准提交,给出 OK/WARN/FAIL。',
|
|
186
|
+
roles: READ_ROLES,
|
|
187
|
+
schema: {},
|
|
188
|
+
handler: (_a, ctx) => {
|
|
189
|
+
const findings = ctx.wf.doctor();
|
|
190
|
+
const order = { FAIL: 0, WARN: 1, OK: 2 };
|
|
191
|
+
findings.sort((a, b) => order[a.level] - order[b.level]);
|
|
192
|
+
return findings
|
|
193
|
+
.map((f) => `${f.level.padEnd(4)} ${f.code}${f.repo ? ` [${f.repo}]` : ''}: ${f.message}` +
|
|
194
|
+
(f.hint ? `\n → ${f.hint}` : ''))
|
|
195
|
+
.join('\n');
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
name: 'questions',
|
|
200
|
+
description: '列出问题,可按 to/status/blocking 过滤。',
|
|
201
|
+
roles: READ_ROLES,
|
|
202
|
+
schema: {
|
|
203
|
+
to: z.string().optional(),
|
|
204
|
+
status: z.string().optional(),
|
|
205
|
+
blocking: z.boolean().optional(),
|
|
206
|
+
all: z.boolean().optional().describe('包含已关闭的问题'),
|
|
207
|
+
},
|
|
208
|
+
handler: (args, ctx) => {
|
|
209
|
+
let qs = ctx.wf.allQuestions();
|
|
210
|
+
if (args.to)
|
|
211
|
+
qs = qs.filter((q) => q.to === args.to);
|
|
212
|
+
if (args.status)
|
|
213
|
+
qs = qs.filter((q) => q.status === args.status);
|
|
214
|
+
if (args.blocking !== undefined)
|
|
215
|
+
qs = qs.filter((q) => q.blocking === args.blocking);
|
|
216
|
+
if (!args.all)
|
|
217
|
+
qs = qs.filter((q) => q.status !== 'closed' && q.status !== 'canceled');
|
|
218
|
+
if (!qs.length)
|
|
219
|
+
return '没有匹配的问题。';
|
|
220
|
+
return qs
|
|
221
|
+
.map((q) => {
|
|
222
|
+
const head = `${q.id} [${q.status}]${q.blocking ? ' [blocking]' : ''} ${q.from} → ${q.to} (${q.repo ?? 'root'})`;
|
|
223
|
+
const opts = q.options.length
|
|
224
|
+
? '\n 选项: ' + q.options.map((o) => `${o.id}) ${o.text}`).join(' | ')
|
|
225
|
+
: '';
|
|
226
|
+
const rec = q.recommendation ? `\n 建议: ${q.recommendation}` : '';
|
|
227
|
+
const ans = q.answer ? `\n 回答: ${q.answer}` : '';
|
|
228
|
+
return `${head}\n 背景: ${q.context.split('\n')[0].slice(0, 200)}${opts}${rec}${ans}`;
|
|
229
|
+
})
|
|
230
|
+
.join('\n\n');
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
name: 'ask',
|
|
235
|
+
description: '创建问题并写入对应 docs/agent-questions.md。blocking=yes 时实现必须停。',
|
|
236
|
+
roles: READ_ROLES,
|
|
237
|
+
schema: {
|
|
238
|
+
to: z.enum(['main', 'PLAN', 'user']).describe('问谁'),
|
|
239
|
+
context: z.string().describe('为什么需要决定;相关文件/PLAN 条目'),
|
|
240
|
+
blocking: z.boolean().default(false),
|
|
241
|
+
blocking_note: z.string().optional(),
|
|
242
|
+
options: z.array(z.string()).optional().describe('候选方案,每项形如 "A: ..."'),
|
|
243
|
+
recommendation: z.string().optional(),
|
|
244
|
+
repo: repoParam,
|
|
245
|
+
},
|
|
246
|
+
handler: (args, ctx) => {
|
|
247
|
+
const to = String(args.to);
|
|
248
|
+
const toUserFromMain = ctx.role === 'main' && to === 'user';
|
|
249
|
+
const repoId = toUserFromMain
|
|
250
|
+
? args.repo || null
|
|
251
|
+
: args.repo || ctx.repo || ctx.wf.cfg.active_repo;
|
|
252
|
+
if (!toUserFromMain && !repoId)
|
|
253
|
+
return err('需要指定 repo');
|
|
254
|
+
if (repoId)
|
|
255
|
+
ctx.wf.repoRef(repoId);
|
|
256
|
+
const toActor = to === 'PLAN'
|
|
257
|
+
? { role: 'plan', repo: repoId || undefined }
|
|
258
|
+
: to === 'main'
|
|
259
|
+
? { role: 'main' }
|
|
260
|
+
: { role: 'user' };
|
|
261
|
+
const acl = canSend(meActor(ctx), toActor, 'question');
|
|
262
|
+
if (!acl.ok)
|
|
263
|
+
return err(`不能向 ${actorKey(toActor)} 提问:${acl.reason}`);
|
|
264
|
+
const file = toUserFromMain && !repoId
|
|
265
|
+
? path.join(ctx.wf.root, 'docs', 'agent-questions.md')
|
|
266
|
+
: questionsFile(ctx.wf.repoRef(repoId).abs);
|
|
267
|
+
const existing = ctx.wf.allQuestions();
|
|
268
|
+
const id = nextQuestionId(existing);
|
|
269
|
+
const rawOptions = args.options || [];
|
|
270
|
+
const options = rawOptions.map((s, i) => {
|
|
271
|
+
const m = /^([A-Za-z0-9]+)\s*[::]\s*(.*)$/.exec(s);
|
|
272
|
+
return m ? { id: m[1], text: m[2] } : { id: String.fromCharCode(65 + i), text: s };
|
|
273
|
+
});
|
|
274
|
+
appendQuestion(file, {
|
|
275
|
+
id,
|
|
276
|
+
to,
|
|
277
|
+
from: ctx.role,
|
|
278
|
+
blocking: Boolean(args.blocking),
|
|
279
|
+
blockingNote: args.blocking_note,
|
|
280
|
+
context: String(args.context),
|
|
281
|
+
options,
|
|
282
|
+
recommendation: args.recommendation,
|
|
283
|
+
});
|
|
284
|
+
logEvent(ctx.wf.root, {
|
|
285
|
+
role: ctx.role,
|
|
286
|
+
tool: 'question_ask',
|
|
287
|
+
repo: repoId ?? undefined,
|
|
288
|
+
question: id,
|
|
289
|
+
ok: true,
|
|
290
|
+
detail: { to, blocking: Boolean(args.blocking) },
|
|
291
|
+
});
|
|
292
|
+
// notify the addressee (best-effort; the question file stays the source of truth)
|
|
293
|
+
try {
|
|
294
|
+
const { message } = ctx.wf.msgs.send({
|
|
295
|
+
from: meActor(ctx),
|
|
296
|
+
to: toActor,
|
|
297
|
+
kind: 'question',
|
|
298
|
+
subject: `${id} ${String(args.context).split('\n')[0].slice(0, 90)}`,
|
|
299
|
+
body: String(args.context),
|
|
300
|
+
refs: { question_id: id, repo: repoId || '' },
|
|
301
|
+
blocking: Boolean(args.blocking),
|
|
302
|
+
repo: repoId ?? undefined,
|
|
303
|
+
idempotency_key: `${ctx.wf.cfg.program}:q:${id}`,
|
|
304
|
+
});
|
|
305
|
+
ctx.wf.msgs.ping(meActor(ctx));
|
|
306
|
+
return [
|
|
307
|
+
`已创建 ${id} → ${to}(${path.relative(ctx.wf.root, file)}),已通知 ${actorKey(toActor)}(${message.id})。`,
|
|
308
|
+
Boolean(args.blocking) ? 'blocking: yes —— 现在停下等待回答,不要继续实现。' : '',
|
|
309
|
+
'用户可以在看板里直接回答,或转发给对方 agent。',
|
|
310
|
+
]
|
|
311
|
+
.filter(Boolean)
|
|
312
|
+
.join('\n');
|
|
313
|
+
}
|
|
314
|
+
catch (e) {
|
|
315
|
+
return [
|
|
316
|
+
`已创建 ${id} → ${to}(${path.relative(ctx.wf.root, file)})。`,
|
|
317
|
+
`⚠️ 通知投递失败:${e.message}(消息未丢失,对方可用 inbox 查询)`,
|
|
318
|
+
].join('\n');
|
|
319
|
+
}
|
|
320
|
+
},
|
|
321
|
+
},
|
|
322
|
+
{
|
|
323
|
+
name: 'answer',
|
|
324
|
+
description: '回答一个问题(只有被问方可以)。写入 answer,并提示需要回写的 PLAN/TODO。',
|
|
325
|
+
roles: READ_ROLES,
|
|
326
|
+
schema: {
|
|
327
|
+
id: z.string(),
|
|
328
|
+
answer: z.string(),
|
|
329
|
+
resolution: z.string().optional().describe('回写到 PLAN/TODO/decisions 的位置'),
|
|
330
|
+
},
|
|
331
|
+
handler: (args, ctx) => {
|
|
332
|
+
const q = ctx.wf.findQuestion(String(args.id));
|
|
333
|
+
if (!q)
|
|
334
|
+
return err(`找不到问题 ${args.id}`);
|
|
335
|
+
const to = String(q.to).toLowerCase();
|
|
336
|
+
const allowed = ctx.role === 'user' ||
|
|
337
|
+
(to === 'plan' && ctx.role === 'plan') ||
|
|
338
|
+
(to === 'main' && ctx.role === 'main');
|
|
339
|
+
if (!allowed)
|
|
340
|
+
return err(`问题 ${q.id} 是问 ${q.to} 的,你不能回答`);
|
|
341
|
+
if (q.status === 'closed')
|
|
342
|
+
return err(`问题 ${q.id} 已关闭`);
|
|
343
|
+
ctx.wf.answerQuestion(q, String(args.answer), args.resolution ? String(args.resolution) : undefined, meActor(ctx));
|
|
344
|
+
logEvent(ctx.wf.root, {
|
|
345
|
+
role: ctx.role,
|
|
346
|
+
tool: 'question_answer',
|
|
347
|
+
question: q.id,
|
|
348
|
+
repo: q.repo,
|
|
349
|
+
ok: true,
|
|
350
|
+
});
|
|
351
|
+
return [
|
|
352
|
+
`已回答 ${q.id}。`,
|
|
353
|
+
'提醒:回答必须回写 PLAN/TODO/decisions,不能只留在问答文件里。',
|
|
354
|
+
'提问方应用后再 close。',
|
|
355
|
+
].join('\n');
|
|
356
|
+
},
|
|
357
|
+
},
|
|
358
|
+
{
|
|
359
|
+
name: 'close',
|
|
360
|
+
description: '关闭问题(提问方或用户),要求已写 resolution。',
|
|
361
|
+
roles: READ_ROLES,
|
|
362
|
+
schema: {
|
|
363
|
+
id: z.string(),
|
|
364
|
+
resolution: z.string().optional(),
|
|
365
|
+
},
|
|
366
|
+
handler: (args, ctx) => {
|
|
367
|
+
const q = ctx.wf.findQuestion(String(args.id));
|
|
368
|
+
if (!q)
|
|
369
|
+
return err(`找不到问题 ${args.id}`);
|
|
370
|
+
const isAsker = String(q.from).toLowerCase() === ctx.role;
|
|
371
|
+
if (!isAsker && ctx.role !== 'user' && ctx.role !== 'main') {
|
|
372
|
+
return err(`只有提问方(${q.from})或用户/主 agent 能关闭 ${q.id}`);
|
|
373
|
+
}
|
|
374
|
+
if (!q.status.startsWith('answered') && !q.answer && ctx.role !== 'user' && ctx.role !== 'main') {
|
|
375
|
+
return err(`${q.id} 还没有回答,不能关闭`);
|
|
376
|
+
}
|
|
377
|
+
ctx.wf.closeQuestion(q, args.resolution ? String(args.resolution) : q.resolution, meActor(ctx));
|
|
378
|
+
logEvent(ctx.wf.root, { role: ctx.role, tool: 'question_close', question: q.id, repo: q.repo, ok: true });
|
|
379
|
+
return `已关闭 ${q.id},已通知对方。记得确认 PLAN/TODO 已经反映结论。`;
|
|
380
|
+
},
|
|
381
|
+
},
|
|
382
|
+
{
|
|
383
|
+
name: 'send_message',
|
|
384
|
+
description: '向另一个 agent 定向发送工作流消息(持久化、可查、可 ack)。受发送 ACL 限制。',
|
|
385
|
+
roles: READ_ROLES,
|
|
386
|
+
schema: {
|
|
387
|
+
to: z.enum(['main', 'plan', 'todo', 'user']),
|
|
388
|
+
to_repo: z.string().optional().describe('plan/todo 的仓库;默认自己的仓'),
|
|
389
|
+
kind: z
|
|
390
|
+
.enum(['question', 'answer', 'event', 'handoff', 'alert', 'approval_request'])
|
|
391
|
+
.default('event'),
|
|
392
|
+
subject: z.string(),
|
|
393
|
+
body: z.string().optional(),
|
|
394
|
+
refs: z.record(z.string(), z.string()).optional(),
|
|
395
|
+
blocking: z.boolean().default(false),
|
|
396
|
+
expires_at: z.string().optional(),
|
|
397
|
+
idempotency_key: z.string().optional(),
|
|
398
|
+
},
|
|
399
|
+
handler: (args, ctx) => {
|
|
400
|
+
const to = resolveActor(String(args.to), args.to_repo, ctx);
|
|
401
|
+
const { message, duplicate } = ctx.wf.msgs.send({
|
|
402
|
+
from: meActor(ctx),
|
|
403
|
+
to,
|
|
404
|
+
kind: args.kind,
|
|
405
|
+
subject: String(args.subject),
|
|
406
|
+
body: args.body ? String(args.body) : '',
|
|
407
|
+
refs: args.refs || {},
|
|
408
|
+
blocking: Boolean(args.blocking),
|
|
409
|
+
expires_at: args.expires_at,
|
|
410
|
+
idempotency_key: args.idempotency_key,
|
|
411
|
+
repo: args.to_repo || ctx.repo || undefined,
|
|
412
|
+
});
|
|
413
|
+
ctx.wf.msgs.ping(meActor(ctx));
|
|
414
|
+
return `${duplicate ? '已存在(幂等命中)' : '已发送'} ${message.id} → ${actorKey(to)} [${message.status}]`;
|
|
415
|
+
},
|
|
416
|
+
},
|
|
417
|
+
{
|
|
418
|
+
name: 'inbox',
|
|
419
|
+
description: '列出消息。默认当前 actor;main/user 可查任意 role。会顺带把 queued 提升为 unread。',
|
|
420
|
+
roles: READ_ROLES,
|
|
421
|
+
schema: {
|
|
422
|
+
role: z.enum(['main', 'plan', 'todo', 'user']).optional(),
|
|
423
|
+
repo: z.string().optional(),
|
|
424
|
+
unread_only: z.boolean().optional(),
|
|
425
|
+
include_acked: z.boolean().optional(),
|
|
426
|
+
kind: z
|
|
427
|
+
.enum(['question', 'answer', 'event', 'handoff', 'alert', 'approval_request'])
|
|
428
|
+
.optional(),
|
|
429
|
+
limit: z.number().optional(),
|
|
430
|
+
},
|
|
431
|
+
handler: (args, ctx) => {
|
|
432
|
+
const target = args.role
|
|
433
|
+
? resolveActor(String(args.role), args.repo, ctx)
|
|
434
|
+
: meActor(ctx);
|
|
435
|
+
assertCanQuery(ctx, target);
|
|
436
|
+
ctx.wf.msgs.promoteQueued(target);
|
|
437
|
+
ctx.wf.msgs.expireDue();
|
|
438
|
+
const list = ctx.wf.msgs.forActor(target, {
|
|
439
|
+
unreadOnly: Boolean(args.unread_only),
|
|
440
|
+
includeAcked: Boolean(args.include_acked),
|
|
441
|
+
kind: args.kind,
|
|
442
|
+
limit: args.limit,
|
|
443
|
+
});
|
|
444
|
+
const unread = ctx.wf.msgs.unreadCount(target);
|
|
445
|
+
const blocking = ctx.wf.msgs.blockingFor(target).length;
|
|
446
|
+
if (!list.length)
|
|
447
|
+
return `${actorKey(target)}: 没有消息(未读 0)。`;
|
|
448
|
+
return [
|
|
449
|
+
`${actorKey(target)}: ${list.length} 条 · 未读 ${unread} · blocking ${blocking}`,
|
|
450
|
+
'',
|
|
451
|
+
...list.map((m) => formatMessage(m)),
|
|
452
|
+
].join('\n');
|
|
453
|
+
},
|
|
454
|
+
},
|
|
455
|
+
{
|
|
456
|
+
name: 'read_message',
|
|
457
|
+
description: '读取一条消息并标记为已读。',
|
|
458
|
+
roles: READ_ROLES,
|
|
459
|
+
schema: { id: z.string() },
|
|
460
|
+
handler: (args, ctx) => {
|
|
461
|
+
const m = ctx.wf.msgs.get(String(args.id));
|
|
462
|
+
if (!m)
|
|
463
|
+
return err(`找不到消息 ${args.id}`);
|
|
464
|
+
assertCanQuery(ctx, m.to);
|
|
465
|
+
const updated = ctx.wf.msgs.markRead(m.id);
|
|
466
|
+
return formatMessage(updated, { full: true });
|
|
467
|
+
},
|
|
468
|
+
},
|
|
469
|
+
{
|
|
470
|
+
name: 'ack_message',
|
|
471
|
+
description: '确认已处理一条消息(ack)。blocking 消息 ack 后才会解除拦截。',
|
|
472
|
+
roles: READ_ROLES,
|
|
473
|
+
schema: { id: z.string(), note: z.string().optional() },
|
|
474
|
+
handler: (args, ctx) => {
|
|
475
|
+
const m = ctx.wf.msgs.get(String(args.id));
|
|
476
|
+
if (!m)
|
|
477
|
+
return err(`找不到消息 ${args.id}`);
|
|
478
|
+
assertCanQuery(ctx, m.to);
|
|
479
|
+
const updated = ctx.wf.msgs.ack(m.id, args.note);
|
|
480
|
+
logEvent(ctx.wf.root, {
|
|
481
|
+
role: ctx.role,
|
|
482
|
+
tool: 'message_ack',
|
|
483
|
+
repo: m.repo,
|
|
484
|
+
ok: true,
|
|
485
|
+
detail: { message: m.id },
|
|
486
|
+
});
|
|
487
|
+
return `已 ack ${updated.id}(${updated.status})${updated.blocking ? ',blocking 已解除' : ''}`;
|
|
488
|
+
},
|
|
489
|
+
},
|
|
490
|
+
{
|
|
491
|
+
name: 'wait_message',
|
|
492
|
+
description: '长轮询等待新消息(默认 30s,最长 120s)。收到后自动标记已读。适合空闲的 plan/todo 会话。',
|
|
493
|
+
roles: READ_ROLES,
|
|
494
|
+
schema: {
|
|
495
|
+
role: z.enum(['main', 'plan', 'todo', 'user']).optional(),
|
|
496
|
+
repo: z.string().optional(),
|
|
497
|
+
timeout_ms: z.number().optional(),
|
|
498
|
+
},
|
|
499
|
+
handler: async (args, ctx) => {
|
|
500
|
+
const target = args.role
|
|
501
|
+
? resolveActor(String(args.role), args.repo, ctx)
|
|
502
|
+
: meActor(ctx);
|
|
503
|
+
assertCanQuery(ctx, target);
|
|
504
|
+
const timeout = Math.min(Number(args.timeout_ms ?? 30_000), 120_000);
|
|
505
|
+
const deadline = Date.now() + timeout;
|
|
506
|
+
ctx.wf.msgs.ping(target);
|
|
507
|
+
while (Date.now() < deadline) {
|
|
508
|
+
ctx.wf.msgs.expireDue();
|
|
509
|
+
ctx.wf.msgs.promoteQueued(target);
|
|
510
|
+
const [next] = ctx.wf.msgs.forActor(target, { unreadOnly: true, limit: 1 });
|
|
511
|
+
if (next) {
|
|
512
|
+
const read = ctx.wf.msgs.markRead(next.id);
|
|
513
|
+
return `收到消息:\n${formatMessage(read, { full: true })}`;
|
|
514
|
+
}
|
|
515
|
+
await new Promise((r) => setTimeout(r, 500));
|
|
516
|
+
}
|
|
517
|
+
return `timeout: ${actorKey(target)} 在 ${Math.round(timeout / 1000)}s 内没有新消息。`;
|
|
518
|
+
},
|
|
519
|
+
},
|
|
520
|
+
{
|
|
521
|
+
name: 'broadcast',
|
|
522
|
+
description: '向所有 plan/todo 广播(仅 main)。',
|
|
523
|
+
roles: ['main'],
|
|
524
|
+
schema: {
|
|
525
|
+
kind: z.enum(['event', 'alert']).default('event'),
|
|
526
|
+
subject: z.string(),
|
|
527
|
+
body: z.string().optional(),
|
|
528
|
+
refs: z.record(z.string(), z.string()).optional(),
|
|
529
|
+
},
|
|
530
|
+
handler: (args, ctx) => {
|
|
531
|
+
const targets = ctx.wf.cfg.repos.flatMap((r) => [
|
|
532
|
+
{ role: 'plan', repo: r.id },
|
|
533
|
+
{ role: 'todo', repo: r.id },
|
|
534
|
+
]);
|
|
535
|
+
const msgs = ctx.wf.msgs.emit({
|
|
536
|
+
from: meActor(ctx),
|
|
537
|
+
to: targets,
|
|
538
|
+
kind: args.kind,
|
|
539
|
+
subject: String(args.subject),
|
|
540
|
+
body: args.body ? String(args.body) : '',
|
|
541
|
+
refs: args.refs || {},
|
|
542
|
+
idempotencyBase: `broadcast:${Date.now()}`,
|
|
543
|
+
});
|
|
544
|
+
return `已广播 ${msgs.length} 条:${msgs.map((m) => m.id).join(', ')}`;
|
|
545
|
+
},
|
|
546
|
+
},
|
|
547
|
+
{
|
|
548
|
+
name: 'presence',
|
|
549
|
+
description: '登记/查看 agent 在线状态。',
|
|
550
|
+
roles: READ_ROLES,
|
|
551
|
+
schema: { session: z.string().optional() },
|
|
552
|
+
handler: (args, ctx) => {
|
|
553
|
+
ctx.wf.msgs.ping(meActor(ctx), args.session);
|
|
554
|
+
const list = ctx.wf.msgs.presence();
|
|
555
|
+
if (!list.length)
|
|
556
|
+
return '没有 presence 记录。';
|
|
557
|
+
return list
|
|
558
|
+
.map((p) => `${p.online ? '●' : '○'} ${actorKey(p.actor)} ${p.last_seen}` +
|
|
559
|
+
(p.session ? ` [${p.session}]` : ''))
|
|
560
|
+
.join('\n');
|
|
561
|
+
},
|
|
562
|
+
},
|
|
563
|
+
{
|
|
564
|
+
name: 'escalate',
|
|
565
|
+
description: '把一条消息升级给用户(并记录原因)。',
|
|
566
|
+
roles: READ_ROLES,
|
|
567
|
+
schema: { id: z.string(), reason: z.string() },
|
|
568
|
+
handler: (args, ctx) => {
|
|
569
|
+
const m = ctx.wf.msgs.escalate(String(args.id), String(args.reason), meActor(ctx));
|
|
570
|
+
if (!m)
|
|
571
|
+
return err(`找不到消息 ${args.id}`);
|
|
572
|
+
return `已升级 ${args.id} → ${m.id}(→ user)`;
|
|
573
|
+
},
|
|
574
|
+
},
|
|
575
|
+
{
|
|
576
|
+
name: 'milestone_claim',
|
|
577
|
+
description: '领取一个 M,状态 todo_ready → implementing。',
|
|
578
|
+
roles: ['todo', 'main'],
|
|
579
|
+
schema: { milestone: msParam, repo: repoParam },
|
|
580
|
+
handler: (args, ctx) => {
|
|
581
|
+
const repo = requireRepo(args, ctx);
|
|
582
|
+
const m = ctx.wf.claim(repo, String(args.milestone), ctx.role);
|
|
583
|
+
return `${m.id} 进入 implementing。只按 TODO 改;有歧义先 ask(to: PLAN)。`;
|
|
584
|
+
},
|
|
585
|
+
},
|
|
586
|
+
{
|
|
587
|
+
name: 'milestone_complete',
|
|
588
|
+
description: '实现完成,状态 implementing → review。要求至少一条证据。',
|
|
589
|
+
roles: ['todo', 'main'],
|
|
590
|
+
schema: { milestone: msParam, repo: repoParam },
|
|
591
|
+
handler: (args, ctx) => {
|
|
592
|
+
const repo = requireRepo(args, ctx);
|
|
593
|
+
const m = ctx.wf.complete(repo, String(args.milestone), ctx.role);
|
|
594
|
+
return `${m.id} 进入 review,等待 PLAN agent 验收。${m.evidence.length} 条证据。`;
|
|
595
|
+
},
|
|
596
|
+
},
|
|
597
|
+
{
|
|
598
|
+
name: 'evidence_run',
|
|
599
|
+
description: '在仓库内执行命令并自动记录证据(退出码、SHA、tree oid、输出),写入 TODO 的「证据记录」。用这个而不是手写证据。',
|
|
600
|
+
roles: ['todo', 'plan', 'main'],
|
|
601
|
+
schema: {
|
|
602
|
+
command: z.string().describe('要执行的命令,如 cargo test --workspace --locked'),
|
|
603
|
+
milestone: msParam,
|
|
604
|
+
repo: repoParam,
|
|
605
|
+
cwd: z.string().optional().describe('相对仓库根目录的工作目录'),
|
|
606
|
+
timeout_ms: z.number().optional(),
|
|
607
|
+
},
|
|
608
|
+
handler: async (args, ctx) => {
|
|
609
|
+
const repo = requireRepo(args, ctx);
|
|
610
|
+
const r = await ctx.wf.runEvidence({
|
|
611
|
+
repoId: repo,
|
|
612
|
+
milestone: String(args.milestone),
|
|
613
|
+
command: String(args.command),
|
|
614
|
+
role: ctx.role,
|
|
615
|
+
cwd: args.cwd,
|
|
616
|
+
timeoutMs: args.timeout_ms,
|
|
617
|
+
});
|
|
618
|
+
return [
|
|
619
|
+
`${r.id} exit=${r.exitCode} head=${r.head} tree=${r.tree || '(clean)'} dirty=${r.dirty}`,
|
|
620
|
+
`已写入 ${r.todoFile}`,
|
|
621
|
+
'',
|
|
622
|
+
r.output ? r.output.slice(-2000) : '(no output)',
|
|
623
|
+
].join('\n');
|
|
624
|
+
},
|
|
625
|
+
},
|
|
626
|
+
{
|
|
627
|
+
name: 'milestone_accept',
|
|
628
|
+
description: 'PLAN/主 agent 验收 M(需处于 review 且有成功证据)。实现者不能自验。',
|
|
629
|
+
roles: ['plan', 'main'],
|
|
630
|
+
schema: {
|
|
631
|
+
milestone: msParam,
|
|
632
|
+
repo: repoParam,
|
|
633
|
+
note: z.string().optional(),
|
|
634
|
+
force: z.boolean().optional().describe('跳过状态/证据检查(仅限明确授权)'),
|
|
635
|
+
},
|
|
636
|
+
handler: (args, ctx) => {
|
|
637
|
+
const repo = requireRepo(args, ctx);
|
|
638
|
+
const m = ctx.wf.accept(repo, String(args.milestone), ctx.role, {
|
|
639
|
+
note: args.note,
|
|
640
|
+
force: Boolean(args.force),
|
|
641
|
+
});
|
|
642
|
+
return `${m.id} 已验收。受验 SHA ${m.sha ?? '?'}。下一步由用户 review diff 后批准提交。`;
|
|
643
|
+
},
|
|
644
|
+
},
|
|
645
|
+
{
|
|
646
|
+
name: 'milestone_reject',
|
|
647
|
+
description: 'PLAN/主 agent 打回 M,必须给具体原因。',
|
|
648
|
+
roles: ['plan', 'main'],
|
|
649
|
+
schema: { milestone: msParam, reason: z.string(), repo: repoParam },
|
|
650
|
+
handler: (args, ctx) => {
|
|
651
|
+
const repo = requireRepo(args, ctx);
|
|
652
|
+
const m = ctx.wf.reject(repo, String(args.milestone), ctx.role, String(args.reason));
|
|
653
|
+
return `${m.id} 已打回。原因已写入 TODO 的「验收记录」。`;
|
|
654
|
+
},
|
|
655
|
+
},
|
|
656
|
+
{
|
|
657
|
+
name: 'scope_check',
|
|
658
|
+
description: '检查当前改动是否越界:TODO 声明的「影响文件」+(todo 角色)ACL。',
|
|
659
|
+
roles: READ_ROLES,
|
|
660
|
+
schema: {
|
|
661
|
+
milestone: msParam.optional(),
|
|
662
|
+
repo: repoParam,
|
|
663
|
+
as_role: z
|
|
664
|
+
.enum(['todo', 'plan', 'main'])
|
|
665
|
+
.optional()
|
|
666
|
+
.describe('按哪个角色检查 ACL;默认实现者(todo),PLAN 验收时用这个看实现方视角'),
|
|
667
|
+
},
|
|
668
|
+
handler: (args, ctx) => {
|
|
669
|
+
const repo = requireRepo(args, ctx);
|
|
670
|
+
const asRole = args.as_role || (ctx.role === 'todo' ? 'todo' : 'plan');
|
|
671
|
+
const r = ctx.wf.scopeCheck(repo, args.milestone, asRole, {
|
|
672
|
+
enforceAcl: asRole === 'todo',
|
|
673
|
+
});
|
|
674
|
+
const lines = [
|
|
675
|
+
`${r.pass ? 'PASS' : 'FAIL'} ${r.repo}${r.milestone ? '/' + r.milestone : ''}`,
|
|
676
|
+
`改动 ${r.changed.length} 个文件:`,
|
|
677
|
+
...r.changed.map((f) => ` - ${f}`),
|
|
678
|
+
];
|
|
679
|
+
if (r.violations.length) {
|
|
680
|
+
lines.push('', '违规:');
|
|
681
|
+
for (const v of r.violations)
|
|
682
|
+
lines.push(` ✗ ${v.file}: ${v.reason}`);
|
|
683
|
+
}
|
|
684
|
+
if (r.warnings.length) {
|
|
685
|
+
lines.push('', '提示:');
|
|
686
|
+
for (const w of r.warnings)
|
|
687
|
+
lines.push(` ! ${w}`);
|
|
688
|
+
}
|
|
689
|
+
return lines.join('\n');
|
|
690
|
+
},
|
|
691
|
+
},
|
|
692
|
+
{
|
|
693
|
+
name: 'commit_request',
|
|
694
|
+
description: '请求提交:校验 scope 后生成一次性审批 id,需要用户执行 mpt-workflow approve <id>。',
|
|
695
|
+
roles: READ_ROLES,
|
|
696
|
+
schema: {
|
|
697
|
+
message: z.string().describe('commit message'),
|
|
698
|
+
repo: repoParam,
|
|
699
|
+
},
|
|
700
|
+
handler: (args, ctx) => {
|
|
701
|
+
const repo = requireRepo(args, ctx);
|
|
702
|
+
const r = ctx.wf.requestCommit({ repoId: repo, message: String(args.message), role: ctx.role });
|
|
703
|
+
return [
|
|
704
|
+
`审批已创建: ${r.approval.id}`,
|
|
705
|
+
`仓库: ${repo} 文件: ${r.files.length}`,
|
|
706
|
+
'',
|
|
707
|
+
r.diffStat,
|
|
708
|
+
'',
|
|
709
|
+
`请用户执行: mpt-workflow approve ${r.approval.id}`,
|
|
710
|
+
'(或在前端看板点“批准提交”)批准后再调用 commit。',
|
|
711
|
+
].join('\n');
|
|
712
|
+
},
|
|
713
|
+
},
|
|
714
|
+
{
|
|
715
|
+
name: 'commit',
|
|
716
|
+
description: '使用已批准的审批执行本地 commit;不会 push。',
|
|
717
|
+
roles: READ_ROLES,
|
|
718
|
+
schema: {
|
|
719
|
+
approval: z.string(),
|
|
720
|
+
repo: repoParam,
|
|
721
|
+
dry_run: z.boolean().optional(),
|
|
722
|
+
},
|
|
723
|
+
handler: (args, ctx) => {
|
|
724
|
+
const repo = requireRepo(args, ctx);
|
|
725
|
+
const r = ctx.wf.commitApproved({
|
|
726
|
+
repoId: repo,
|
|
727
|
+
approvalId: String(args.approval),
|
|
728
|
+
role: ctx.role,
|
|
729
|
+
dryRun: Boolean(args.dry_run),
|
|
730
|
+
});
|
|
731
|
+
if ('dryRun' in r)
|
|
732
|
+
return `dry-run: ${JSON.stringify(r, null, 2)}`;
|
|
733
|
+
return `已提交 ${repo} @ ${r.head}\n${r.message}`;
|
|
734
|
+
},
|
|
735
|
+
},
|
|
736
|
+
{
|
|
737
|
+
name: 'phase_activate',
|
|
738
|
+
description: '主 agent 激活一个仓(串行锁),或设置为 null 暂停。',
|
|
739
|
+
roles: ['main', 'user'],
|
|
740
|
+
schema: {
|
|
741
|
+
repo: z.string().describe('仓库 id;传空字符串表示无'),
|
|
742
|
+
phase: z.string().optional(),
|
|
743
|
+
},
|
|
744
|
+
handler: (args, ctx) => {
|
|
745
|
+
const repo = String(args.repo || '');
|
|
746
|
+
ctx.wf.setActiveRepo(repo || null);
|
|
747
|
+
if (args.phase)
|
|
748
|
+
ctx.wf.updatePhase(String(args.phase), 'active');
|
|
749
|
+
logEvent(ctx.wf.root, { role: ctx.role, tool: 'phase_activate', repo: repo || undefined, ok: true });
|
|
750
|
+
return repo
|
|
751
|
+
? `active repo = ${repo}${args.phase ? `,阶段 ${args.phase} = active` : ''}`
|
|
752
|
+
: 'active repo 已清空';
|
|
753
|
+
},
|
|
754
|
+
},
|
|
755
|
+
];
|
|
756
|
+
return tools;
|
|
757
|
+
}
|
|
758
|
+
function requireRepo(args, ctx) {
|
|
759
|
+
const repo = args.repo || ctx.repo || ctx.wf.cfg.active_repo;
|
|
760
|
+
if (!repo)
|
|
761
|
+
throw new Error('需要指定 repo(或先 bootstrap 绑定仓库)');
|
|
762
|
+
ctx.wf.repoRef(repo);
|
|
763
|
+
return repo;
|
|
764
|
+
}
|
|
765
|
+
function meActor(ctx) {
|
|
766
|
+
return { role: ctx.role, repo: ctx.repo ?? undefined };
|
|
767
|
+
}
|
|
768
|
+
function resolveActor(role, repo, ctx) {
|
|
769
|
+
if (role === 'plan' || role === 'todo') {
|
|
770
|
+
return { role, repo: repo || ctx.repo || ctx.wf.cfg.active_repo || undefined };
|
|
771
|
+
}
|
|
772
|
+
return { role: role };
|
|
773
|
+
}
|
|
774
|
+
function assertCanQuery(ctx, target) {
|
|
775
|
+
if (ctx.role === 'main' || ctx.role === 'user')
|
|
776
|
+
return;
|
|
777
|
+
if (actorKey(meActor(ctx)) === actorKey(target))
|
|
778
|
+
return;
|
|
779
|
+
throw new Error(`角色 ${actorKey(meActor(ctx))} 不能读取 ${actorKey(target)} 的消息`);
|
|
780
|
+
}
|
|
781
|
+
export async function runMcpServer(opts) {
|
|
782
|
+
const cfg = loadConfig(opts.root);
|
|
783
|
+
if (!cfg) {
|
|
784
|
+
process.stderr.write(`[workflow] 未在 ${opts.root} 找到 .workflow/program.yaml\n`);
|
|
785
|
+
process.exit(1);
|
|
786
|
+
}
|
|
787
|
+
const wf = new Workflow(opts.root, cfg);
|
|
788
|
+
const ctx = { wf, role: opts.role || 'user', repo: opts.repo || null };
|
|
789
|
+
// Do not write the machine-wide role lease at startup: several role sessions
|
|
790
|
+
// may run at once. Hooks should get their role from WF_ROLE/WF_REPO; the
|
|
791
|
+
// bootstrap tool may declare it explicitly for single-session use.
|
|
792
|
+
const server = new McpServer({ name: 'workflow', version: VERSION }, {
|
|
793
|
+
capabilities: { tools: {} },
|
|
794
|
+
instructions: [
|
|
795
|
+
'这是多 agent / 多仓开发的工作流状态机。开始任何工作前,先调用 bootstrap 声明本会话角色:',
|
|
796
|
+
'bootstrap(role="main" | "plan" | "todo" | "user", repo="<仓库 id>")。',
|
|
797
|
+
'然后调用 status 看全局、next 看下一步。',
|
|
798
|
+
'规则:不要手改 PLAN.md / TODO-*.md 里的状态(由工具维护);有歧义用 ask 提问;',
|
|
799
|
+
'实现完成必须用 evidence_run 记录证据;验收用 milestone_accept;提交必须先 commit_request,',
|
|
800
|
+
'等用户批准后再 commit;永远不要 push。',
|
|
801
|
+
'写文件仍用原生 Edit/Write,但会被角色 ACL 和 git pre-commit 检查。',
|
|
802
|
+
].join(''),
|
|
803
|
+
});
|
|
804
|
+
const tools = buildTools(opts);
|
|
805
|
+
const registered = new Set();
|
|
806
|
+
const register = (t) => {
|
|
807
|
+
if (registered.has(t.name))
|
|
808
|
+
return;
|
|
809
|
+
registered.add(t.name);
|
|
810
|
+
server.registerTool(t.name, {
|
|
811
|
+
title: t.name,
|
|
812
|
+
description: t.description,
|
|
813
|
+
inputSchema: t.schema,
|
|
814
|
+
}, async (args) => {
|
|
815
|
+
const out = await invokeTool(tools, t.name, args, ctx);
|
|
816
|
+
return typeof out === 'string' ? text(out) : out;
|
|
817
|
+
});
|
|
818
|
+
};
|
|
819
|
+
const publicTools = new Set(['bootstrap', 'status', 'next', 'doctor', 'questions']);
|
|
820
|
+
for (const t of tools)
|
|
821
|
+
if (publicTools.has(t.name))
|
|
822
|
+
register(t);
|
|
823
|
+
// Once the role is bootstrapped, expose the rest through the same server.
|
|
824
|
+
// (Tools are registered up-front but gated per call, so the list stays stable.)
|
|
825
|
+
for (const t of tools)
|
|
826
|
+
if (!publicTools.has(t.name))
|
|
827
|
+
register(t);
|
|
828
|
+
const transport = new StdioServerTransport();
|
|
829
|
+
await server.connect(transport);
|
|
830
|
+
process.stderr.write(`[workflow] MCP server ready: ${wf.root} role=${ctx.role}${ctx.repo ? ' repo=' + ctx.repo : ''}\n`);
|
|
831
|
+
}
|
|
832
|
+
export { buildTools };
|
|
833
|
+
/**
|
|
834
|
+
* Single entry point for a tool call, shared by the MCP registration and tests.
|
|
835
|
+
* Enforces role gating and converts handler output to an MCP tool result.
|
|
836
|
+
*/
|
|
837
|
+
export async function invokeTool(tools, name, args, ctx) {
|
|
838
|
+
const t = tools.find((x) => x.name === name);
|
|
839
|
+
if (!t)
|
|
840
|
+
return err(`未知工具 ${name}`);
|
|
841
|
+
if (!t.roles.includes(ctx.role)) {
|
|
842
|
+
return err(`角色 ${ctx.role} 不能调用 ${name}(允许: ${t.roles.join(', ')})。` +
|
|
843
|
+
`如果角色不对,重新 bootstrap。`);
|
|
844
|
+
}
|
|
845
|
+
try {
|
|
846
|
+
return await t.handler(args, ctx);
|
|
847
|
+
}
|
|
848
|
+
catch (e) {
|
|
849
|
+
return err(`[${name}] ${e.message}`);
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
//# sourceMappingURL=server.js.map
|