@suifei/dsh-ai-team 0.4.89 → 0.4.91

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.
@@ -1,307 +1,317 @@
1
- /**
2
- * The member-facing Team tool. It is registered globally and becomes visible
3
- * once the AI Team runtime booted (see index.js `when`); the tool itself still
4
- * refuses any session that is not a live Team member (`E_NOT_MEMBER`).
5
- *
6
- * Mutations require `expectedRevision` (Task CAS) and `baseSeq` (Thread stale
7
- * reply protection); errors are returned as data so the member can re-read.
8
- */
9
- import { taskCard } from './views.js';
10
- import { isQaMember } from '../domain/member-profile.js';
11
-
12
- const ACTIONS = ['tasks', 'members', 'inbox', 'recall', 'create', 'askHuman', 'member', 'evolve', 'distill', 'note', 'skill', 'claim', 'submit', 'thread', 'reply', 'accept', 'requestChanges'];
13
-
14
- function failure(error) {
15
- return {
16
- ok: false,
17
- error: {
18
- code: error?.code ?? 'E_TOOL',
19
- message: error?.message ?? String(error),
20
- details: error?.details ?? null,
21
- },
22
- };
23
- }
24
-
25
- function memberForSession(state, sessionId) {
26
- return Object.values(state.members ?? {}).find((member) => member.sessionId === sessionId) ?? null;
27
- }
28
-
29
- function projectId(state, requested) {
30
- if (typeof requested === 'string' && state.projects?.[requested] !== undefined) return requested;
31
- return Object.keys(state.projects ?? {})[0] ?? null;
32
- }
33
-
34
- function resolveMember(state, value) {
35
- if (typeof value !== 'string' || value === '') return null;
36
- const exact = state.members?.[value];
37
- if (exact?.kind === 'agent') return exact;
38
- return Object.values(state.members ?? {}).find((member) => member.kind === 'agent' && member.name === value) ?? null;
39
- }
40
-
41
- function rosterText(state) {
42
- return Object.values(state.members ?? {})
43
- .filter((member) => member.kind === 'agent' && member.status !== 'retired')
44
- .map((member) => `${member.name}=${member.id}`)
45
- .join('、');
46
- }
47
-
48
- /**
49
- * @param {{
50
- * getState: () => any,
51
- * dispatch: (command: any) => Promise<any>,
52
- * maxThreadEntries?: number
53
- * }} options
54
- */
55
- export function createTeamTool(options) {
56
- return {
57
- name: 'team',
58
- description: 'Read and act on the AI Team board. The Project owner can create tasks with action=create and must set resultDefinition and ownerId. Use members for the exact roster id/name/role; use tasks for ids/revisions. When Human must decide something, call askHuman with every question and options—do not bury questions in a report. Use recall to search your notes/skills and your past Tasks/Threads. Claim before working; read a Thread before replying; submit evidence before acceptance.',
59
- parameters: {
60
- type: 'object',
61
- properties: {
62
- action: { type: 'string', enum: ACTIONS, description: 'Operation to perform.' },
63
- taskId: { type: 'string', description: 'Task id from tasks.' },
64
- expectedRevision: { type: 'integer', description: 'Task revision from tasks/claim; required for claim/submit/accept/requestChanges.' },
65
- baseSeq: { type: 'integer', description: 'Thread headSeq read before replying; required for reply.' },
66
- afterSeq: { type: 'integer', description: 'Return Thread entries after this seq.' },
67
- summary: { type: 'string', description: 'What was done, for submit.' },
68
- evidence: { type: 'array', items: { type: 'string' }, description: 'Evidence strings/paths, at least one for submit.' },
69
- note: { type: 'string', description: 'Acceptance or changes-requested note.' },
70
- body: { type: 'string', description: 'Thread reply body.' },
71
- title: { type: 'string', description: 'Task title for create; decision title for askHuman.' },
72
- resultDefinition: { type: 'string', description: 'Acceptance definition for create.' },
73
- ownerId: { type: 'string', description: 'Accountable owner id from members for create; defaults to you.' },
74
- assigneeId: { type: 'string', description: 'Optional assignee id from members for create.' },
75
- qaRequired: { type: 'boolean', description: 'Require a QA member to accept this task.' },
76
- qaChecklist: { type: 'array', items: { type: 'string' }, description: 'QA checklist items; all must pass before acceptance; required when qaRequired is true.' },
77
- allowDuplicate: { type: 'boolean', description: 'Set true only when an identical active Task is intentionally required.' },
78
- checks: {
79
- type: 'array',
80
- description: 'Acceptance checks for accept; include every qaChecklist item with passed=true/false.',
81
- items: {
82
- type: 'object',
83
- properties: {
84
- item: { type: 'string', description: 'Exact qaChecklist item.' },
85
- passed: { type: 'boolean' },
86
- },
87
- required: ['item', 'passed'],
88
- },
89
- },
90
- questions: {
91
- type: 'array',
92
- description: 'Human decision questions for askHuman; include every question instead of pointing at a report.',
93
- items: {
94
- type: 'object',
95
- properties: {
96
- text: { type: 'string', description: 'The question.' },
97
- options: { type: 'array', items: { type: 'string' }, description: 'Optional answer choices.' },
98
- recommendation: { type: 'string', description: 'Optional recommended answer.' },
99
- blocking: { type: 'string', description: 'What this decision blocks.' },
100
- },
101
- required: ['text'],
102
- },
103
- },
104
- personality: { type: 'string', description: 'New working personality for evolve/distill.' },
105
- reason: { type: 'string', description: 'Why the personality changed.' },
106
- memberId: { type: 'string', description: 'Target member for member/distill/note/skill.' },
107
- text: { type: 'string', description: 'Note text.' },
108
- query: { type: 'string', description: 'Keyword for recall.' },
109
- skill: { type: 'string', description: 'Skill name to add.' },
110
- },
111
- required: ['action'],
112
- additionalProperties: false,
113
- },
114
- output: {
115
- schema: { type: 'object', additionalProperties: true },
116
- render(_args, value) {
117
- return [{ type: 'text', text: JSON.stringify(value, null, 2) }];
118
- },
119
- },
120
- async execute(args, exec) {
121
- const state = options.getState(exec);
122
- const member = memberForSession(state, exec?.agent?.id);
123
- if (member === null) return failure({ code: 'E_NOT_MEMBER', message: 'this session is not a Team member' });
124
- const actor = { kind: 'agent', memberId: member.id };
125
- const pid = projectId(state, exec?.projectId);
126
- if (pid === null) return failure({ code: 'E_NOT_FOUND', message: 'no Project is configured' });
127
- const command = (type, payload) => options.dispatch({ type, payload, actor, projectId: pid }, exec);
128
- try {
129
- if (args.action === 'tasks') {
130
- const tasks = Object.values(state.tasks ?? {})
131
- .filter((task) => task.tombstone !== true)
132
- .map((task) => taskCard(state, task));
133
- return { ok: true, projectId: pid, tasks };
134
- }
135
- if (args.action === 'members') {
136
- const members = Object.values(state.members ?? {})
137
- .filter((candidate) => candidate.kind === 'agent' && candidate.status !== 'retired')
138
- .map((candidate) => ({
139
- id: candidate.id,
140
- name: candidate.name,
141
- role: candidate.role,
142
- status: candidate.status,
143
- qa: isQaMember(candidate),
144
- }));
145
- return { ok: true, projectId: pid, members };
146
- }
147
- if (args.action === 'recall') {
148
- const query = typeof args.query === 'string' ? args.query.trim().toLowerCase() : '';
149
- if (query === '') return failure({ code: 'E_INVALID_ARGUMENT', message: 'query is required' });
150
- const own = state.members?.[member.id] ?? {};
151
- const hits = [];
152
- for (const note of own.notes ?? []) {
153
- if (note.toLowerCase().includes(query)) hits.push({ kind: 'note', text: note });
154
- }
155
- for (const skill of own.skills ?? []) {
156
- if (skill.toLowerCase().includes(query)) hits.push({ kind: 'skill', text: skill });
157
- }
158
- for (const task of Object.values(state.tasks ?? {})) {
159
- const ownTask = task.ownerId === member.id || task.assigneeId === member.id;
160
- if (!ownTask) continue;
161
- const taskText = `${task.title ?? ''} ${task.resultDefinition ?? ''}`.toLowerCase();
162
- if (taskText.includes(query)) hits.push({ kind: 'task', ref: task.id, text: task.title });
163
- const entries = state.threads?.[task.id]?.entries ?? [];
164
- for (const entry of entries) {
165
- if (String(entry.body ?? '').toLowerCase().includes(query)) {
166
- hits.push({ kind: 'thread', ref: task.id, text: `#${entry.seq} ${String(entry.body).slice(0, 200)}` });
167
- }
168
- }
169
- }
170
- return { ok: true, query: args.query, hits: hits.slice(-10) };
171
- }
172
- if (args.action === 'member') {
173
- const target = state.members?.[args.memberId ?? member.id];
174
- if (target === undefined || target.kind !== 'agent') return failure({ code: 'E_NOT_FOUND', message: 'agent not found' });
175
- const tasks = Object.values(state.tasks ?? {})
176
- .filter((task) => task.ownerId === target.id || task.assigneeId === target.id)
177
- .slice(-10)
178
- .map((task) => ({ id: task.id, number: task.number, title: task.title, status: task.status, ownerId: task.ownerId, assigneeId: task.assigneeId }));
179
- return {
180
- ok: true,
181
- member: {
182
- id: target.id, name: target.name, role: target.role, personality: target.personality ?? '',
183
- notes: target.notes ?? [], skills: target.skills ?? [], profileVersion: target.profileVersion ?? 1,
184
- distillVersion: target.distillVersion ?? 0, status: target.status,
185
- },
186
- tasks,
187
- };
188
- }
189
- if (args.action === 'note') {
190
- const result = await command('member.note', {
191
- ...(typeof args.memberId === 'string' ? { memberId: args.memberId } : {}),
192
- text: typeof args.text === 'string' ? args.text : '',
193
- });
194
- return { ok: true, member: result.member };
195
- }
196
- if (args.action === 'skill') {
197
- const result = await command('member.skill', {
198
- ...(typeof args.memberId === 'string' ? { memberId: args.memberId } : {}),
199
- name: typeof args.skill === 'string' ? args.skill : '',
200
- });
201
- return { ok: true, member: result.member };
202
- }
203
- if (args.action === 'distill') {
204
- const result = await command('member.distill', {
205
- ...(typeof args.memberId === 'string' ? { memberId: args.memberId } : {}),
206
- personality: typeof args.personality === 'string' ? args.personality : '',
207
- reason: typeof args.reason === 'string' ? args.reason : 'agent distill',
208
- });
209
- return { ok: true, member: result.member };
210
- }
211
- if (args.action === 'evolve') {
212
- if (typeof args.personality !== 'string' || args.personality.trim() === '') {
213
- return failure({ code: 'E_INVALID_ARGUMENT', message: 'personality is required' });
214
- }
215
- const result = await command('member.evolve', {
216
- personality: args.personality,
217
- reason: typeof args.reason === 'string' ? args.reason : 'self-evolution',
218
- });
219
- return { ok: true, member: result.member };
220
- }
221
- if (args.action === 'create') {
222
- const project = state.projects?.[pid];
223
- if (project?.ownerMemberId !== member.id && project?.ownerId !== member.id) {
224
- return failure({ code: 'E_UNAUTHORIZED', message: 'only the Project owner may create tasks' });
225
- }
226
- if (typeof args.title !== 'string' || args.title.trim() === '') return failure({ code: 'E_INVALID_ARGUMENT', message: 'title is required' });
227
- if (typeof args.resultDefinition !== 'string' || args.resultDefinition.trim() === '') {
228
- return failure({ code: 'E_INVALID_ARGUMENT', message: 'resultDefinition is required' });
229
- }
230
- const owner = args.ownerId === undefined ? member : resolveMember(state, args.ownerId);
231
- if (owner === null) {
232
- return failure({ code: 'E_INVALID_ARGUMENT', message: `ownerId ${String(args.ownerId)} is not a Team member; use members to get ids (${rosterText(state)})` });
233
- }
234
- const assignee = args.assigneeId === undefined ? null : resolveMember(state, args.assigneeId);
235
- if (args.assigneeId !== undefined && assignee === null) {
236
- return failure({ code: 'E_INVALID_ARGUMENT', message: `assigneeId ${String(args.assigneeId)} is not a Team member; use members to get ids (${rosterText(state)})` });
237
- }
238
- const result = await command('task.create', {
239
- title: args.title,
240
- resultDefinition: args.resultDefinition,
241
- ownerId: owner.id,
242
- ...(assignee === null ? {} : { assigneeId: assignee.id }),
243
- ...(args.allowDuplicate === true ? { allowDuplicate: true } : {}),
244
- ...(args.qaRequired === true ? { qaRequired: true } : {}),
245
- ...(Array.isArray(args.qaChecklist) ? { qaChecklist: args.qaChecklist } : {}),
246
- });
247
- return { ok: true, task: taskCard(options.getState(exec), result.task) };
248
- }
249
- if (args.action === 'askHuman') {
250
- if (!Array.isArray(args.questions) || args.questions.length === 0) {
251
- return failure({ code: 'E_INVALID_ARGUMENT', message: 'questions is required; list every question Human must answer' });
252
- }
253
- const result = await command('decision.request', {
254
- title: typeof args.title === 'string' && args.title.trim() !== '' ? args.title.trim() : '需要你决策',
255
- taskId: typeof args.taskId === 'string' && args.taskId !== '' ? args.taskId : null,
256
- questions: args.questions,
257
- source: 'team',
258
- });
259
- return { ok: true, item: result.item, duplicate: result.duplicate === true };
260
- }
261
- if (args.action === 'inbox') {
262
- const items = Object.values(state.inbox ?? {})
263
- .filter((item) => item.memberId === member.id && !['resolved', 'dismissed', 'superseded'].includes(item.state));
264
- return { ok: true, memberId: member.id, items };
265
- }
266
- if (args.action === 'claim' || args.action === 'submit' || args.action === 'accept' || args.action === 'requestChanges') {
267
- if (typeof args.taskId !== 'string' || args.taskId === '') return failure({ code: 'E_INVALID_ARGUMENT', message: 'taskId is required' });
268
- if (!Number.isSafeInteger(args.expectedRevision)) return failure({ code: 'E_INVALID_ARGUMENT', message: 'expectedRevision is required; read tasks first' });
269
- const payload = { taskId: args.taskId, expectedRevision: args.expectedRevision };
270
- if (args.action === 'submit') {
271
- const evidence = Array.isArray(args.evidence) ? args.evidence.filter((item) => typeof item === 'string' && item !== '') : [];
272
- if (evidence.length === 0) return failure({ code: 'E_INVALID_ARGUMENT', message: 'at least one evidence entry is required' });
273
- payload.summary = typeof args.summary === 'string' ? args.summary : '';
274
- payload.evidence = evidence.map((value) => ({ kind: 'text', value }));
275
- }
276
- if (args.action === 'accept' || args.action === 'requestChanges') payload.note = typeof args.note === 'string' ? args.note : '';
277
- if (args.action === 'accept') {
278
- const checks = Array.isArray(args.checks)
279
- ? args.checks.filter((check) => typeof check?.item === 'string' && check.item !== '' && typeof check.passed === 'boolean')
280
- : [];
281
- if (checks.length > 0) payload.checks = checks;
282
- }
283
- const type = args.action === 'requestChanges' ? 'task.request_changes' : `task.${args.action}`;
284
- const result = await command(type, payload);
285
- return { ok: true, task: taskCard(options.getState(exec), result.task), event: result.event?.type ?? null };
286
- }
287
- if (args.action === 'thread') {
288
- if (typeof args.taskId !== 'string') return failure({ code: 'E_INVALID_ARGUMENT', message: 'taskId is required' });
289
- const thread = state.threads?.[args.taskId] ?? { taskId: args.taskId, headSeq: 0, entries: [] };
290
- const after = Number.isSafeInteger(args.afterSeq) ? args.afterSeq : 0;
291
- const entries = thread.entries.filter((entry) => entry.seq > after).slice(-(options.maxThreadEntries ?? 20));
292
- return { ok: true, taskId: args.taskId, headSeq: thread.headSeq, entries };
293
- }
294
- if (args.action === 'reply') {
295
- if (typeof args.taskId !== 'string') return failure({ code: 'E_INVALID_ARGUMENT', message: 'taskId is required' });
296
- if (!Number.isSafeInteger(args.baseSeq)) return failure({ code: 'E_INVALID_ARGUMENT', message: 'baseSeq is required; read thread first' });
297
- if (typeof args.body !== 'string' || args.body.trim() === '') return failure({ code: 'E_INVALID_ARGUMENT', message: 'body is required' });
298
- const result = await command('thread.post', { taskId: args.taskId, baseSeq: args.baseSeq, body: args.body });
299
- return { ok: true, taskId: args.taskId, headSeq: result.thread.headSeq, entry: result.entry };
300
- }
301
- return failure({ code: 'E_INVALID_ARGUMENT', message: `unknown action: ${String(args.action)}` });
302
- } catch (error) {
303
- return failure(error);
304
- }
305
- },
306
- };
307
- }
1
+ /**
2
+ * The member-facing Team tool. It is registered globally and becomes visible
3
+ * once the AI Team runtime booted (see index.js `when`); the tool itself still
4
+ * refuses any session that is not a live Team member (`E_NOT_MEMBER`).
5
+ *
6
+ * Mutations require `expectedRevision` (Task CAS) and `baseSeq` (Thread stale
7
+ * reply protection); errors are returned as data so the member can re-read.
8
+ */
9
+ import { taskCard } from './views.js';
10
+ import { isQaMember } from '../domain/member-profile.js';
11
+
12
+ const ACTIONS = ['tasks', 'members', 'inbox', 'recall', 'create', 'askHuman', 'member', 'evolve', 'distill', 'note', 'skill', 'claim', 'submit', 'thread', 'reply', 'accept', 'requestChanges', 'say'];
13
+
14
+ function failure(error) {
15
+ return {
16
+ ok: false,
17
+ error: {
18
+ code: error?.code ?? 'E_TOOL',
19
+ message: error?.message ?? String(error),
20
+ details: error?.details ?? null,
21
+ },
22
+ };
23
+ }
24
+
25
+ function memberForSession(state, sessionId) {
26
+ return Object.values(state.members ?? {}).find((member) => member.sessionId === sessionId) ?? null;
27
+ }
28
+
29
+ function projectId(state, requested) {
30
+ if (typeof requested === 'string' && state.projects?.[requested] !== undefined) return requested;
31
+ return Object.keys(state.projects ?? {})[0] ?? null;
32
+ }
33
+
34
+ function resolveMember(state, value) {
35
+ if (typeof value !== 'string' || value === '') return null;
36
+ const exact = state.members?.[value];
37
+ if (exact?.kind === 'agent') return exact;
38
+ return Object.values(state.members ?? {}).find((member) => member.kind === 'agent' && member.name === value) ?? null;
39
+ }
40
+
41
+ function rosterText(state) {
42
+ return Object.values(state.members ?? {})
43
+ .filter((member) => member.kind === 'agent' && member.status !== 'retired')
44
+ .map((member) => `${member.name}=${member.id}`)
45
+ .join('、');
46
+ }
47
+
48
+ /**
49
+ * @param {{
50
+ * getState: () => any,
51
+ * dispatch: (command: any) => Promise<any>,
52
+ * maxThreadEntries?: number
53
+ * }} options
54
+ */
55
+ export function createTeamTool(options) {
56
+ return {
57
+ name: 'team',
58
+ description: 'Read and act on the AI Team board. The Project owner (the PMO) is the only member who speaks to Human in the Main stream, with action=say (optionally listing taskIds); everyone else talks in Task Threads. The Project owner can create tasks with action=create and must set resultDefinition and ownerId. Use members for the exact roster id/name/role; use tasks for ids/revisions. When Human must decide something, call askHuman with every question and options—do not bury questions in a report. Use recall to search your notes/skills and your past Tasks/Threads. Claim before working; read a Thread before replying; submit evidence before acceptance.',
59
+ parameters: {
60
+ type: 'object',
61
+ properties: {
62
+ action: { type: 'string', enum: ACTIONS, description: 'Operation to perform.' },
63
+ taskId: { type: 'string', description: 'Task id from tasks.' },
64
+ expectedRevision: { type: 'integer', description: 'Task revision from tasks/claim; required for claim/submit/accept/requestChanges.' },
65
+ baseSeq: { type: 'integer', description: 'Thread headSeq read before replying; required for reply.' },
66
+ afterSeq: { type: 'integer', description: 'Return Thread entries after this seq.' },
67
+ summary: { type: 'string', description: 'What was done, for submit.' },
68
+ evidence: { type: 'array', items: { type: 'string' }, description: 'Evidence strings/paths, at least one for submit.' },
69
+ note: { type: 'string', description: 'Acceptance or changes-requested note.' },
70
+ body: { type: 'string', description: 'Thread reply body, or the Main stream message for say.' },
71
+ taskIds: { type: 'array', items: { type: 'string' }, description: 'Task ids to show as cards under a say message.' },
72
+ title: { type: 'string', description: 'Task title for create; decision title for askHuman.' },
73
+ resultDefinition: { type: 'string', description: 'Acceptance definition for create.' },
74
+ ownerId: { type: 'string', description: 'Accountable owner id from members for create; defaults to you.' },
75
+ assigneeId: { type: 'string', description: 'Optional assignee id from members for create.' },
76
+ qaRequired: { type: 'boolean', description: 'Require a QA member to accept this task.' },
77
+ qaChecklist: { type: 'array', items: { type: 'string' }, description: 'QA checklist items; all must pass before acceptance; required when qaRequired is true.' },
78
+ allowDuplicate: { type: 'boolean', description: 'Set true only when an identical active Task is intentionally required.' },
79
+ checks: {
80
+ type: 'array',
81
+ description: 'Acceptance checks for accept; include every qaChecklist item with passed=true/false.',
82
+ items: {
83
+ type: 'object',
84
+ properties: {
85
+ item: { type: 'string', description: 'Exact qaChecklist item.' },
86
+ passed: { type: 'boolean' },
87
+ },
88
+ required: ['item', 'passed'],
89
+ },
90
+ },
91
+ questions: {
92
+ type: 'array',
93
+ description: 'Human decision questions for askHuman; include every question instead of pointing at a report.',
94
+ items: {
95
+ type: 'object',
96
+ properties: {
97
+ text: { type: 'string', description: 'The question.' },
98
+ options: { type: 'array', items: { type: 'string' }, description: 'Optional answer choices.' },
99
+ recommendation: { type: 'string', description: 'Optional recommended answer.' },
100
+ blocking: { type: 'string', description: 'What this decision blocks.' },
101
+ },
102
+ required: ['text'],
103
+ },
104
+ },
105
+ personality: { type: 'string', description: 'New working personality for evolve/distill.' },
106
+ reason: { type: 'string', description: 'Why the personality changed.' },
107
+ memberId: { type: 'string', description: 'Target member for member/distill/note/skill.' },
108
+ text: { type: 'string', description: 'Note text.' },
109
+ query: { type: 'string', description: 'Keyword for recall.' },
110
+ skill: { type: 'string', description: 'Skill name to add.' },
111
+ },
112
+ required: ['action'],
113
+ additionalProperties: false,
114
+ },
115
+ output: {
116
+ schema: { type: 'object', additionalProperties: true },
117
+ render(_args, value) {
118
+ return [{ type: 'text', text: JSON.stringify(value, null, 2) }];
119
+ },
120
+ },
121
+ async execute(args, exec) {
122
+ const state = options.getState(exec);
123
+ const member = memberForSession(state, exec?.agent?.id);
124
+ if (member === null) return failure({ code: 'E_NOT_MEMBER', message: 'this session is not a Team member' });
125
+ const actor = { kind: 'agent', memberId: member.id };
126
+ const pid = projectId(state, exec?.projectId);
127
+ if (pid === null) return failure({ code: 'E_NOT_FOUND', message: 'no Project is configured' });
128
+ const command = (type, payload) => options.dispatch({ type, payload, actor, projectId: pid }, exec);
129
+ try {
130
+ if (args.action === 'tasks') {
131
+ const tasks = Object.values(state.tasks ?? {})
132
+ .filter((task) => task.tombstone !== true)
133
+ .map((task) => taskCard(state, task));
134
+ return { ok: true, projectId: pid, tasks };
135
+ }
136
+ if (args.action === 'members') {
137
+ const members = Object.values(state.members ?? {})
138
+ .filter((candidate) => candidate.kind === 'agent' && candidate.status !== 'retired')
139
+ .map((candidate) => ({
140
+ id: candidate.id,
141
+ name: candidate.name,
142
+ role: candidate.role,
143
+ status: candidate.status,
144
+ qa: isQaMember(candidate),
145
+ }));
146
+ return { ok: true, projectId: pid, members };
147
+ }
148
+ if (args.action === 'recall') {
149
+ const query = typeof args.query === 'string' ? args.query.trim().toLowerCase() : '';
150
+ if (query === '') return failure({ code: 'E_INVALID_ARGUMENT', message: 'query is required' });
151
+ const own = state.members?.[member.id] ?? {};
152
+ const hits = [];
153
+ for (const note of own.notes ?? []) {
154
+ if (note.toLowerCase().includes(query)) hits.push({ kind: 'note', text: note });
155
+ }
156
+ for (const skill of own.skills ?? []) {
157
+ if (skill.toLowerCase().includes(query)) hits.push({ kind: 'skill', text: skill });
158
+ }
159
+ for (const task of Object.values(state.tasks ?? {})) {
160
+ const ownTask = task.ownerId === member.id || task.assigneeId === member.id;
161
+ if (!ownTask) continue;
162
+ const taskText = `${task.title ?? ''} ${task.resultDefinition ?? ''}`.toLowerCase();
163
+ if (taskText.includes(query)) hits.push({ kind: 'task', ref: task.id, text: task.title });
164
+ const entries = state.threads?.[task.id]?.entries ?? [];
165
+ for (const entry of entries) {
166
+ if (String(entry.body ?? '').toLowerCase().includes(query)) {
167
+ hits.push({ kind: 'thread', ref: task.id, text: `#${entry.seq} ${String(entry.body).slice(0, 200)}` });
168
+ }
169
+ }
170
+ }
171
+ return { ok: true, query: args.query, hits: hits.slice(-10) };
172
+ }
173
+ if (args.action === 'member') {
174
+ const target = state.members?.[args.memberId ?? member.id];
175
+ if (target === undefined || target.kind !== 'agent') return failure({ code: 'E_NOT_FOUND', message: 'agent not found' });
176
+ const tasks = Object.values(state.tasks ?? {})
177
+ .filter((task) => task.ownerId === target.id || task.assigneeId === target.id)
178
+ .slice(-10)
179
+ .map((task) => ({ id: task.id, number: task.number, title: task.title, status: task.status, ownerId: task.ownerId, assigneeId: task.assigneeId }));
180
+ return {
181
+ ok: true,
182
+ member: {
183
+ id: target.id, name: target.name, role: target.role, personality: target.personality ?? '',
184
+ notes: target.notes ?? [], skills: target.skills ?? [], profileVersion: target.profileVersion ?? 1,
185
+ distillVersion: target.distillVersion ?? 0, status: target.status,
186
+ },
187
+ tasks,
188
+ };
189
+ }
190
+ if (args.action === 'note') {
191
+ const result = await command('member.note', {
192
+ ...(typeof args.memberId === 'string' ? { memberId: args.memberId } : {}),
193
+ text: typeof args.text === 'string' ? args.text : '',
194
+ });
195
+ return { ok: true, member: result.member };
196
+ }
197
+ if (args.action === 'skill') {
198
+ const result = await command('member.skill', {
199
+ ...(typeof args.memberId === 'string' ? { memberId: args.memberId } : {}),
200
+ name: typeof args.skill === 'string' ? args.skill : '',
201
+ });
202
+ return { ok: true, member: result.member };
203
+ }
204
+ if (args.action === 'distill') {
205
+ const result = await command('member.distill', {
206
+ ...(typeof args.memberId === 'string' ? { memberId: args.memberId } : {}),
207
+ personality: typeof args.personality === 'string' ? args.personality : '',
208
+ reason: typeof args.reason === 'string' ? args.reason : 'agent distill',
209
+ });
210
+ return { ok: true, member: result.member };
211
+ }
212
+ if (args.action === 'evolve') {
213
+ if (typeof args.personality !== 'string' || args.personality.trim() === '') {
214
+ return failure({ code: 'E_INVALID_ARGUMENT', message: 'personality is required' });
215
+ }
216
+ const result = await command('member.evolve', {
217
+ personality: args.personality,
218
+ reason: typeof args.reason === 'string' ? args.reason : 'self-evolution',
219
+ });
220
+ return { ok: true, member: result.member };
221
+ }
222
+ if (args.action === 'create') {
223
+ const project = state.projects?.[pid];
224
+ if (project?.ownerMemberId !== member.id && project?.ownerId !== member.id) {
225
+ return failure({ code: 'E_UNAUTHORIZED', message: 'only the Project owner may create tasks' });
226
+ }
227
+ if (typeof args.title !== 'string' || args.title.trim() === '') return failure({ code: 'E_INVALID_ARGUMENT', message: 'title is required' });
228
+ if (typeof args.resultDefinition !== 'string' || args.resultDefinition.trim() === '') {
229
+ return failure({ code: 'E_INVALID_ARGUMENT', message: 'resultDefinition is required' });
230
+ }
231
+ const owner = args.ownerId === undefined ? member : resolveMember(state, args.ownerId);
232
+ if (owner === null) {
233
+ return failure({ code: 'E_INVALID_ARGUMENT', message: `ownerId ${String(args.ownerId)} is not a Team member; use members to get ids (${rosterText(state)})` });
234
+ }
235
+ const assignee = args.assigneeId === undefined ? null : resolveMember(state, args.assigneeId);
236
+ if (args.assigneeId !== undefined && assignee === null) {
237
+ return failure({ code: 'E_INVALID_ARGUMENT', message: `assigneeId ${String(args.assigneeId)} is not a Team member; use members to get ids (${rosterText(state)})` });
238
+ }
239
+ const result = await command('task.create', {
240
+ title: args.title,
241
+ resultDefinition: args.resultDefinition,
242
+ ownerId: owner.id,
243
+ ...(assignee === null ? {} : { assigneeId: assignee.id }),
244
+ ...(args.allowDuplicate === true ? { allowDuplicate: true } : {}),
245
+ ...(args.qaRequired === true ? { qaRequired: true } : {}),
246
+ ...(Array.isArray(args.qaChecklist) ? { qaChecklist: args.qaChecklist } : {}),
247
+ });
248
+ return { ok: true, task: taskCard(options.getState(exec), result.task) };
249
+ }
250
+ if (args.action === 'askHuman') {
251
+ if (!Array.isArray(args.questions) || args.questions.length === 0) {
252
+ return failure({ code: 'E_INVALID_ARGUMENT', message: 'questions is required; list every question Human must answer' });
253
+ }
254
+ const result = await command('decision.request', {
255
+ title: typeof args.title === 'string' && args.title.trim() !== '' ? args.title.trim() : '需要你决策',
256
+ taskId: typeof args.taskId === 'string' && args.taskId !== '' ? args.taskId : null,
257
+ questions: args.questions,
258
+ source: 'team',
259
+ });
260
+ return { ok: true, item: result.item, duplicate: result.duplicate === true };
261
+ }
262
+ if (args.action === 'inbox') {
263
+ const items = Object.values(state.inbox ?? {})
264
+ .filter((item) => item.memberId === member.id && !['resolved', 'dismissed', 'superseded'].includes(item.state));
265
+ return { ok: true, memberId: member.id, items };
266
+ }
267
+ if (args.action === 'claim' || args.action === 'submit' || args.action === 'accept' || args.action === 'requestChanges') {
268
+ if (typeof args.taskId !== 'string' || args.taskId === '') return failure({ code: 'E_INVALID_ARGUMENT', message: 'taskId is required' });
269
+ if (!Number.isSafeInteger(args.expectedRevision)) return failure({ code: 'E_INVALID_ARGUMENT', message: 'expectedRevision is required; read tasks first' });
270
+ const payload = { taskId: args.taskId, expectedRevision: args.expectedRevision };
271
+ if (args.action === 'submit') {
272
+ const evidence = Array.isArray(args.evidence) ? args.evidence.filter((item) => typeof item === 'string' && item !== '') : [];
273
+ if (evidence.length === 0) return failure({ code: 'E_INVALID_ARGUMENT', message: 'at least one evidence entry is required' });
274
+ payload.summary = typeof args.summary === 'string' ? args.summary : '';
275
+ payload.evidence = evidence.map((value) => ({ kind: 'text', value }));
276
+ }
277
+ if (args.action === 'accept' || args.action === 'requestChanges') payload.note = typeof args.note === 'string' ? args.note : '';
278
+ if (args.action === 'accept') {
279
+ const checks = Array.isArray(args.checks)
280
+ ? args.checks.filter((check) => typeof check?.item === 'string' && check.item !== '' && typeof check.passed === 'boolean')
281
+ : [];
282
+ if (checks.length > 0) payload.checks = checks;
283
+ }
284
+ const type = args.action === 'requestChanges' ? 'task.request_changes' : `task.${args.action}`;
285
+ const result = await command(type, payload);
286
+ return { ok: true, task: taskCard(options.getState(exec), result.task), event: result.event?.type ?? null };
287
+ }
288
+ if (args.action === 'thread') {
289
+ if (typeof args.taskId !== 'string') return failure({ code: 'E_INVALID_ARGUMENT', message: 'taskId is required' });
290
+ const thread = state.threads?.[args.taskId] ?? { taskId: args.taskId, headSeq: 0, entries: [] };
291
+ const after = Number.isSafeInteger(args.afterSeq) ? args.afterSeq : 0;
292
+ const entries = thread.entries.filter((entry) => entry.seq > after).slice(-(options.maxThreadEntries ?? 20));
293
+ return { ok: true, taskId: args.taskId, headSeq: thread.headSeq, entries };
294
+ }
295
+ if (args.action === 'reply') {
296
+ if (typeof args.taskId !== 'string') return failure({ code: 'E_INVALID_ARGUMENT', message: 'taskId is required' });
297
+ if (!Number.isSafeInteger(args.baseSeq)) return failure({ code: 'E_INVALID_ARGUMENT', message: 'baseSeq is required; read thread first' });
298
+ if (typeof args.body !== 'string' || args.body.trim() === '') return failure({ code: 'E_INVALID_ARGUMENT', message: 'body is required' });
299
+ const result = await command('thread.post', { taskId: args.taskId, baseSeq: args.baseSeq, body: args.body });
300
+ return { ok: true, taskId: args.taskId, headSeq: result.thread.headSeq, entry: result.entry };
301
+ }
302
+ if (args.action === 'say') {
303
+ // 核心模型:只有项目 PMO 面向 Human 说话(主对话);成员在各自 Task Thread 里协作。
304
+ const ownerId = state.projects?.[pid]?.ownerMemberId ?? state.projects?.[pid]?.ownerId;
305
+ if (ownerId !== member.id) return failure({ code: 'E_UNAUTHORIZED', message: 'only the Project owner (PMO) speaks in the Main stream; use reply in the Task Thread instead' });
306
+ if (typeof args.body !== 'string' || args.body.trim() === '') return failure({ code: 'E_INVALID_ARGUMENT', message: 'body is required' });
307
+ const taskIds = Array.isArray(args.taskIds) ? args.taskIds.filter((id) => typeof id === 'string' && id !== '') : [];
308
+ const result = await command('stream.post', { body: args.body, taskIds });
309
+ return { ok: true, seq: result.entry?.seq ?? null };
310
+ }
311
+ return failure({ code: 'E_INVALID_ARGUMENT', message: `unknown action: ${String(args.action)}` });
312
+ } catch (error) {
313
+ return failure(error);
314
+ }
315
+ },
316
+ };
317
+ }