cladogram 0.5.4 → 0.5.5-beta.2

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.
Files changed (54) hide show
  1. package/README.md +8 -6
  2. package/bin/clado.js +5 -5
  3. package/docs/release.md +13 -0
  4. package/package.json +1 -1
  5. package/plugins/agent/index.js +458 -25
  6. package/plugins/agent/style.css +299 -8
  7. package/plugins/directory/capabilities.json +1 -1
  8. package/plugins/directory/index.js +57 -2
  9. package/plugins/directory/style.css +11 -0
  10. package/plugins/view-controls/index.js +1 -1
  11. package/plugins/view-controls/style.css +30 -10
  12. package/skills/clado/SKILL.md +17 -16
  13. package/src/agent/executors.js +213 -0
  14. package/src/agent/probe.js +176 -0
  15. package/src/agent/providers/anthropic.js +220 -0
  16. package/src/agent/providers/index.js +52 -0
  17. package/src/agent/providers/openai.js +146 -0
  18. package/src/agent/runtime.js +557 -0
  19. package/src/agent/sessions.js +129 -0
  20. package/src/html/app-css.js +62 -9
  21. package/src/html/canvas-chain.js +2 -1
  22. package/src/html/canvas-ui.css +63 -0
  23. package/src/html/page.js +4 -0
  24. package/src/html/plugin-ui.css +12 -0
  25. package/src/html/plugin-ui.js +8 -2
  26. package/src/html/runtime-api.js +136 -5
  27. package/src/html/runtime.js +160 -290
  28. package/src/html/view-registry.js +95 -0
  29. package/src/html/views/view-cards.js +28 -0
  30. package/src/html/views/view-doc.js +51 -0
  31. package/src/html/views/view-mindmap.js +173 -0
  32. package/src/server/agent.js +335 -17
  33. package/src/server/assets/library-canvas.js +13 -3
  34. package/src/server/assets/library-plugin-bridge.js +2 -2
  35. package/src/server/assets/library-plugins.js +2 -2
  36. package/src/server/assets/library.css +5 -0
  37. package/src/server/assets/library.html +1 -1
  38. package/src/server/assets/library.js +157 -20
  39. package/src/server/events.js +189 -0
  40. package/src/server/export.js +1 -1
  41. package/src/server/mcp.js +16 -16
  42. package/src/server/plugin-capabilities.js +38 -22
  43. package/src/server/plugin-settings.js +27 -1
  44. package/src/server/plugins.js +17 -4
  45. package/src/server/workspace.js +171 -56
  46. package/src/skill/install.js +1 -1
  47. package/src/skill/targets.js +3 -3
  48. package/src/util/update.js +3 -2
  49. package/src/workspace/service.js +23 -15
  50. package/src/workspace/storage.js +88 -20
  51. package/templates/business-flow.mdc +1 -1
  52. package/templates/call-chain.mdc +1 -1
  53. package/templates/data-design.mdc +1 -1
  54. package/templates/test-cases.mdc +47 -0
@@ -1,5 +1,21 @@
1
1
  (() => {
2
- window.CladoAgentPlugin = (host, { readCanvas, viewId, placeholder, agentName = 'ZCode' }) => {
2
+ window.CladoAgentPlugin = (host, {
3
+ readCanvas,
4
+ viewId,
5
+ placeholder,
6
+ agentName = 'ZCode',
7
+ endpoint = '/library/agent',
8
+ followAi: initialFollowAi = true,
9
+ setFollowAi = () => {},
10
+ focusTarget = () => {},
11
+ }) => {
12
+ const metaEndpoint = endpoint.replace(/(\/library\/agent)(?=\?|$)/, '$1/meta');
13
+ const sessionsEndpoint = endpoint.replace(/(\/library\/agent)(?=\?|$)/, '$1/sessions');
14
+ const sessionUrl = id => {
15
+ if (!id) return sessionsEndpoint;
16
+ return sessionsEndpoint + (sessionsEndpoint.includes('?') ? '&' : '?') + 'id=' + encodeURIComponent(id);
17
+ };
18
+
3
19
  const root = document.createElement('div');
4
20
  root.className = 'clado-agent';
5
21
  root.innerHTML = `
@@ -9,48 +25,292 @@
9
25
  </button>
10
26
  <section id="clado-agent-panel" class="clado-ui-glass clado-ui-glass-panel clado-agent-panel" aria-labelledby="clado-agent-title" hidden>
11
27
  <header>
12
- <div><h2 id="clado-agent-title">灵感</h2><small class="clado-agent-provider"></small></div>
13
- <div>
28
+ <div class="clado-agent-heading"><h2 id="clado-agent-title">灵感</h2><small class="clado-agent-provider"></small></div>
29
+ <div class="clado-agent-header-actions">
30
+ <button type="button" class="clado-ui-button clado-agent-follow" aria-pressed="${initialFollowAi ? 'true' : 'false'}" title="跟随 AI 视角:AI 定位或修改卡片时平滑平移镜头">跟随</button>
31
+ <button type="button" class="clado-ui-button clado-agent-history" aria-expanded="false" title="切换历史会话">历史</button>
14
32
  <button type="button" class="clado-ui-button clado-agent-new">新对话</button>
15
33
  <button type="button" class="clado-ui-button clado-agent-close" aria-label="关闭 灵感 对话">×</button>
16
34
  </div>
17
35
  </header>
36
+ <div class="clado-agent-sessions" hidden></div>
37
+ <div class="clado-agent-modes" role="group" aria-label="工作模式">
38
+ <button type="button" class="clado-ui-button clado-agent-chip" data-workflow="design" aria-pressed="true">架构设计</button>
39
+ <button type="button" class="clado-ui-button clado-agent-chip" data-workflow="qa" aria-pressed="false">测试用例</button>
40
+ <button type="button" class="clado-ui-button clado-agent-chip" data-workflow="audit" aria-pressed="false">Diff 验收</button>
41
+ <button type="button" class="clado-ui-button clado-agent-chip" data-workflow="dispatch" aria-pressed="false">派发开发</button>
42
+ </div>
43
+ <div class="clado-agent-persona-bar">
44
+ <label for="clado-agent-persona">人设</label>
45
+ <select id="clado-agent-persona" class="clado-agent-select">
46
+ <option value="architect">研发架构师</option>
47
+ <option value="qa">QA 测试专家</option>
48
+ <option value="auditor">验收审计官</option>
49
+ </select>
50
+ </div>
51
+ <div class="clado-agent-executor-bar" hidden>
52
+ <label for="clado-agent-executor">执行器</label>
53
+ <select id="clado-agent-executor" class="clado-agent-select"></select>
54
+ </div>
18
55
  <div class="clado-agent-log" role="log" aria-label="对话消息" aria-live="off"></div>
19
56
  <p class="clado-agent-status" role="status" hidden></p>
20
57
  <form>
21
- <textarea aria-label="给 灵感 的消息" placeholder="给 灵感 发一条消息…" rows="2" maxlength="12000" required></textarea>
58
+ <textarea aria-label="给 灵感 的消息" placeholder="给 灵感 发一条消息…" rows="2" maxlength="12000"></textarea>
22
59
  <button class="clado-ui-button clado-agent-stop" type="button" hidden>停止</button>
23
60
  <button class="clado-ui-button clado-ui-button-primary clado-agent-send" type="submit" disabled>发送</button>
24
61
  </form>
25
62
  </section>`;
26
63
  host.mount('bottom-right', root, 'clado.agent');
64
+
27
65
  const find = selector => root.querySelector(selector);
28
66
  const panel = find('section'), trigger = find('.clado-agent-launch'), log = find('.clado-agent-log');
29
67
  const input = find('textarea'), send = find('.clado-agent-send'), stop = find('.clado-agent-stop');
30
- if (placeholder) input.placeholder = placeholder;
31
- find('.clado-agent-provider').textContent = agentName;
68
+ const providerLabel = find('.clado-agent-provider');
69
+ const followBtn = find('.clado-agent-follow'), historyBtn = find('.clado-agent-history');
70
+ const sessionsDrawer = find('.clado-agent-sessions');
71
+ const personaBar = find('.clado-agent-persona-bar'), personaSelect = find('#clado-agent-persona');
72
+ const executorBar = find('.clado-agent-executor-bar'), executorSelect = find('#clado-agent-executor');
73
+ const chips = [...root.querySelectorAll('.clado-agent-chip')];
32
74
  const reset = find('.clado-agent-new'), status = find('.clado-agent-status'), form = find('form');
33
- let session = crypto.randomUUID(), running = false, stopping = false;
75
+
76
+ const defaultPlaceholder = placeholder || '给 灵感 发一条消息…';
77
+ const workflowHints = {
78
+ design: defaultPlaceholder,
79
+ qa: '描述需要补充或评审的测试用例范围…',
80
+ audit: '输入要核对的变更点,或直接回车核对当前 Git Diff…',
81
+ dispatch: '可选补充开发要求;留空将按当前画布规格直接派发给 ACP 执行器…',
82
+ };
83
+ const workflowDefaultPersona = { design: 'architect', qa: 'qa', audit: 'auditor' };
84
+ input.placeholder = workflowHints.design;
85
+ providerLabel.textContent = agentName;
86
+
87
+ let session = crypto.randomUUID(), running = false, stopping = false, workflow = 'design';
88
+ let persona = 'architect', personaLockedByUser = false;
89
+ let followAi = !!initialFollowAi;
90
+ let meta = { engine: 'acp', provider: 'openai', builtinConfigured: false, model: '', executors: [], personas: [], sessions: [] };
34
91
  const toolStates = { pending: '等待中', in_progress: '运行中', completed: '已完成', failed: '失败' };
35
- const request = (data, keepalive = false) => fetch('/library/agent', {
92
+
93
+ const request = (data, keepalive = false) => fetch(endpoint, {
36
94
  method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(data), keepalive,
37
95
  });
96
+ const canSubmit = () => !running && (workflow === 'dispatch' || workflow === 'audit' || !!input.value.trim());
38
97
  const syncActions = () => {
39
- send.disabled = running || !input.value.trim();
98
+ send.disabled = !canSubmit();
99
+ send.textContent = workflow === 'dispatch' ? '派发' : '发送';
40
100
  stop.hidden = !running;
41
101
  stop.disabled = stopping;
42
102
  reset.disabled = running;
103
+ historyBtn.disabled = running;
43
104
  input.readOnly = running;
105
+ const isDispatch = workflow === 'dispatch';
106
+ executorBar.hidden = !isDispatch;
107
+ personaBar.hidden = isDispatch;
108
+ executorSelect.disabled = running;
109
+ personaSelect.disabled = running;
110
+ for (const chip of chips) chip.disabled = running;
44
111
  };
45
112
  const showStatus = text => {
46
113
  if (status.textContent !== text) status.textContent = text;
47
114
  status.hidden = !text;
48
115
  };
49
116
  function scroll() { log.scrollTop = log.scrollHeight; }
117
+
118
+ followBtn.onclick = () => {
119
+ followAi = !followAi;
120
+ followBtn.setAttribute('aria-pressed', String(followAi));
121
+ setFollowAi(followAi);
122
+ };
123
+
124
+ personaSelect.onchange = () => {
125
+ persona = personaSelect.value || 'architect';
126
+ personaLockedByUser = true;
127
+ };
128
+
129
+ function selectWorkflow(nextWorkflow) {
130
+ workflow = nextWorkflow;
131
+ for (const item of chips) item.setAttribute('aria-pressed', String(item.dataset.workflow === workflow));
132
+ input.placeholder = workflowHints[workflow] || defaultPlaceholder;
133
+ if (!personaLockedByUser && workflowDefaultPersona[workflow]) {
134
+ persona = workflowDefaultPersona[workflow];
135
+ personaSelect.value = persona;
136
+ }
137
+ syncActions();
138
+ }
139
+
140
+ async function loadMeta() {
141
+ try {
142
+ const res = await fetch(metaEndpoint, { cache: 'no-store' });
143
+ if (!res.ok) return;
144
+ meta = await res.json();
145
+ const providerTag = meta.provider === 'anthropic' ? 'Anthropic' : 'OpenAI';
146
+ providerLabel.textContent = meta.engine === 'builtin'
147
+ ? `${providerTag} · ${meta.model || 'LLM'}`
148
+ : `ACP · ${agentName}`;
149
+ if (Array.isArray(meta.personas) && meta.personas.length) {
150
+ const prev = personaSelect.value || persona;
151
+ personaSelect.replaceChildren();
152
+ for (const p of meta.personas) {
153
+ const opt = document.createElement('option');
154
+ opt.value = p.id;
155
+ opt.textContent = p.name;
156
+ opt.title = p.summary || '';
157
+ personaSelect.append(opt);
158
+ }
159
+ if (meta.personas.some(p => p.id === prev)) personaSelect.value = prev;
160
+ else if (meta.defaultPersona && meta.personas.some(p => p.id === meta.defaultPersona)) {
161
+ persona = meta.defaultPersona;
162
+ personaSelect.value = persona;
163
+ }
164
+ }
165
+ const list = Array.isArray(meta.executors) ? meta.executors : [];
166
+ const previous = executorSelect.value;
167
+ executorSelect.replaceChildren();
168
+ if (!list.length) {
169
+ const opt = document.createElement('option');
170
+ opt.value = '';
171
+ opt.textContent = '未配置 ACP 执行器';
172
+ executorSelect.append(opt);
173
+ } else {
174
+ for (const item of list) {
175
+ const opt = document.createElement('option');
176
+ opt.value = item.id;
177
+ opt.textContent = item.name + (item.source === 'plugin' ? '(插件)' : '');
178
+ executorSelect.append(opt);
179
+ }
180
+ if (previous && list.some(item => item.id === previous)) executorSelect.value = previous;
181
+ }
182
+ } catch {}
183
+ }
184
+
185
+ async function refreshSessionsDrawer() {
186
+ sessionsDrawer.replaceChildren();
187
+ const loading = document.createElement('div');
188
+ loading.className = 'clado-agent-sessions-empty';
189
+ loading.textContent = '加载历史会话…';
190
+ sessionsDrawer.append(loading);
191
+ try {
192
+ const res = await fetch(sessionsEndpoint, { cache: 'no-store' });
193
+ const data = res.ok ? await res.json() : { sessions: [] };
194
+ const items = Array.isArray(data.sessions) ? data.sessions : [];
195
+ sessionsDrawer.replaceChildren();
196
+ if (!items.length) {
197
+ const empty = document.createElement('div');
198
+ empty.className = 'clado-agent-sessions-empty';
199
+ empty.textContent = '暂无已保存的对话记录';
200
+ sessionsDrawer.append(empty);
201
+ return;
202
+ }
203
+ for (const item of items) {
204
+ const row = document.createElement('div');
205
+ row.className = 'clado-agent-session-row' + (item.id === session ? ' active' : '');
206
+ const openBtn = document.createElement('button');
207
+ openBtn.type = 'button';
208
+ openBtn.className = 'clado-agent-session-open';
209
+ const titleSpan = document.createElement('span');
210
+ titleSpan.className = 'clado-agent-session-title';
211
+ titleSpan.textContent = item.title || '未命名会话';
212
+ const metaSpan = document.createElement('small');
213
+ metaSpan.textContent = [
214
+ item.hasPendingQuestion ? '待确认' : '',
215
+ item.messageCount ? item.messageCount + ' 条消息' : '',
216
+ ].filter(Boolean).join(' · ') || '空会话';
217
+ if (item.hasPendingQuestion) metaSpan.classList.add('waiting');
218
+ openBtn.append(titleSpan, metaSpan);
219
+ openBtn.onclick = () => loadSession(item.id);
220
+ const delBtn = document.createElement('button');
221
+ delBtn.type = 'button';
222
+ delBtn.className = 'clado-agent-session-del';
223
+ delBtn.setAttribute('aria-label', '删除会话');
224
+ delBtn.textContent = '×';
225
+ delBtn.onclick = async ev => {
226
+ ev.stopPropagation();
227
+ try {
228
+ const delRes = await fetch(sessionUrl(item.id), { method: 'DELETE' });
229
+ if (delRes.ok) {
230
+ if (item.id === session) {
231
+ session = crypto.randomUUID();
232
+ log.replaceChildren();
233
+ }
234
+ refreshSessionsDrawer();
235
+ }
236
+ } catch {}
237
+ };
238
+ row.append(openBtn, delBtn);
239
+ sessionsDrawer.append(row);
240
+ }
241
+ } catch {
242
+ sessionsDrawer.replaceChildren();
243
+ const err = document.createElement('div');
244
+ err.className = 'clado-agent-sessions-empty';
245
+ err.textContent = '读取历史会话失败';
246
+ sessionsDrawer.append(err);
247
+ }
248
+ }
249
+
250
+ function setHistoryOpen(open) {
251
+ sessionsDrawer.hidden = !open;
252
+ historyBtn.setAttribute('aria-expanded', String(open));
253
+ if (open) refreshSessionsDrawer();
254
+ }
255
+ historyBtn.onclick = () => setHistoryOpen(sessionsDrawer.hidden);
256
+
257
+ async function loadSession(id) {
258
+ if (running) return;
259
+ try {
260
+ const res = await fetch(sessionUrl(id), { cache: 'no-store' });
261
+ if (!res.ok) return;
262
+ const { session: record } = await res.json();
263
+ if (!record) return;
264
+ session = record.id;
265
+ if (record.mode && workflowHints[record.mode]) selectWorkflow(record.mode);
266
+ if (record.persona) {
267
+ persona = record.persona;
268
+ personaSelect.value = persona;
269
+ }
270
+ log.replaceChildren();
271
+ for (const msg of record.uiMessages || []) {
272
+ if (msg.role === 'human') {
273
+ const bubble = message('human', msg.text || '', msg.isAnswer ? '你(已确认)' : '你');
274
+ if (msg.canvas?.file) {
275
+ const ctx = document.createElement('small');
276
+ ctx.textContent = [msg.canvas.file.split('/').pop(), msg.canvas.selectionCount ? msg.canvas.selectionCount + ' 个选中标题' : ''].filter(Boolean).join(' · ');
277
+ ctx.title = msg.canvas.file;
278
+ bubble.item.append(ctx);
279
+ }
280
+ } else if (msg.role === 'assistant') {
281
+ const bubble = message('assistant', msg.text || '');
282
+ if (msg.thinking) {
283
+ const details = createThinkingBlock(msg.thinking);
284
+ bubble.item.insertBefore(details, bubble.body);
285
+ }
286
+ if (!msg.text) bubble.body.hidden = true;
287
+ if (Array.isArray(msg.changes) && msg.changes.length) {
288
+ bubble.item.append(createChangesCard(msg.changes));
289
+ }
290
+ if (msg.question) {
291
+ bubble.item.append(createQuestionCard(msg.question));
292
+ }
293
+ }
294
+ }
295
+ setHistoryOpen(false);
296
+ showStatus(record.pendingQuestion ? '等待你的确认后继续执行' : '');
297
+ scroll();
298
+ } catch {}
299
+ }
300
+
301
+ for (const chip of chips) {
302
+ chip.onclick = () => {
303
+ if (running) return;
304
+ selectWorkflow(chip.dataset.workflow);
305
+ if (workflow === 'dispatch') loadMeta();
306
+ };
307
+ }
308
+
50
309
  function setOpen(open) {
51
310
  panel.hidden = !open;
52
311
  trigger.setAttribute('aria-expanded', String(open));
53
- if (open) { input.focus(); scroll(); }
312
+ if (open) { loadMeta(); syncActions(); input.focus(); scroll(); }
313
+ else setHistoryOpen(false);
54
314
  }
55
315
  trigger.onclick = () => setOpen(panel.hidden);
56
316
  find('.clado-agent-close').onclick = () => { setOpen(false); trigger.focus(); };
@@ -67,6 +327,8 @@
67
327
  reset.onclick = () => {
68
328
  request({ action: 'close', session }, true).catch(() => {});
69
329
  session = crypto.randomUUID();
330
+ personaLockedByUser = false;
331
+ setHistoryOpen(false);
70
332
  log.replaceChildren();
71
333
  showStatus('');
72
334
  input.focus();
@@ -87,11 +349,11 @@
87
349
  if (!event.persisted) request({ action: 'close', session }, true).catch(() => {});
88
350
  });
89
351
 
90
- function message(author, text) {
352
+ function message(author, text, labelOverride) {
91
353
  const item = document.createElement('div');
92
354
  item.className = 'clado-agent-message ' + author;
93
355
  const label = document.createElement('strong');
94
- label.textContent = author === 'human' ? '你' : agentName;
356
+ label.textContent = labelOverride || (author === 'human' ? '你' : (meta.engine === 'builtin' ? 'Clado Agent' : agentName));
95
357
  const body = document.createElement('p');
96
358
  body.textContent = text;
97
359
  item.append(label, body);
@@ -99,31 +361,175 @@
99
361
  return { item, body };
100
362
  }
101
363
 
102
- form.onsubmit = async event => {
103
- event.preventDefault();
104
- const text = input.value.trim();
105
- if (!text || running) return;
106
- const canvas = readCanvas();
364
+ function createThinkingBlock(initialText = '') {
365
+ const details = document.createElement('details');
366
+ details.className = 'clado-agent-thinking';
367
+ const summary = document.createElement('summary');
368
+ summary.textContent = '思考过程';
369
+ const pre = document.createElement('pre');
370
+ pre.textContent = initialText;
371
+ details.append(summary, pre);
372
+ return details;
373
+ }
374
+
375
+ function createChangesCard(items) {
376
+ const card = document.createElement('div');
377
+ card.className = 'clado-agent-changes';
378
+ const title = document.createElement('div');
379
+ title.className = 'clado-agent-changes-title';
380
+ title.textContent = `本轮画布变更(${items.length} 项 · 点击定位)`;
381
+ card.append(title);
382
+ const list = document.createElement('div');
383
+ list.className = 'clado-agent-changes-list';
384
+ for (const change of items) {
385
+ const btn = document.createElement('button');
386
+ btn.type = 'button';
387
+ btn.className = 'clado-agent-change-item';
388
+ const dot = document.createElement('span');
389
+ dot.className = 'dot';
390
+ const text = document.createElement('span');
391
+ text.textContent = change.label || change.file || '画布更新';
392
+ btn.append(dot, text);
393
+ btn.onclick = () => focusTarget(change.file, change.node, change.label);
394
+ list.append(btn);
395
+ }
396
+ card.append(list);
397
+ return card;
398
+ }
399
+
400
+ function createQuestionCard(q) {
401
+ const card = document.createElement('div');
402
+ card.className = 'clado-agent-question' + (q.answered ? ' answered' : '');
403
+ const badge = document.createElement('div');
404
+ badge.className = 'clado-agent-question-badge';
405
+ badge.textContent = q.answered ? '已确认方案' : '需要你拍板';
406
+ const prompt = document.createElement('div');
407
+ prompt.className = 'clado-agent-question-text';
408
+ prompt.textContent = q.question || '请选择下一步方案:';
409
+ card.append(badge, prompt);
410
+
411
+ if (q.answered) {
412
+ const chosen = document.createElement('div');
413
+ chosen.className = 'clado-agent-question-answered';
414
+ chosen.textContent = '已选:' + q.answered;
415
+ card.append(chosen);
416
+ return card;
417
+ }
418
+
419
+ const controls = [];
420
+ const submitChoice = (choiceId, answerText) => {
421
+ if (running) return;
422
+ const clean = String(answerText || '').trim();
423
+ if (!clean) return;
424
+ for (const el of controls) el.disabled = true;
425
+ card.classList.add('answered');
426
+ badge.textContent = '已确认方案';
427
+ const chosen = document.createElement('div');
428
+ chosen.className = 'clado-agent-question-answered';
429
+ chosen.textContent = '已选:' + clean;
430
+ card.append(chosen);
431
+ const canvas = readCanvas();
432
+ streamTurn({
433
+ action: 'answer',
434
+ session,
435
+ questionId: q.id,
436
+ choiceId: choiceId || undefined,
437
+ answer: clean,
438
+ mode: workflow === 'dispatch' ? 'design' : workflow,
439
+ persona,
440
+ canvas,
441
+ viewId,
442
+ }, {
443
+ humanText: clean,
444
+ humanLabel: '你(已确认)',
445
+ isDispatch: false,
446
+ });
447
+ };
448
+
449
+ if (Array.isArray(q.options) && q.options.length) {
450
+ const optsWrap = document.createElement('div');
451
+ optsWrap.className = 'clado-agent-question-options';
452
+ for (const opt of q.options) {
453
+ const btn = document.createElement('button');
454
+ btn.type = 'button';
455
+ btn.className = 'clado-agent-option-btn';
456
+ const lbl = document.createElement('span');
457
+ lbl.className = 'clado-agent-option-label';
458
+ lbl.textContent = opt.label;
459
+ btn.append(lbl);
460
+ if (opt.detail) {
461
+ const det = document.createElement('small');
462
+ det.textContent = opt.detail;
463
+ btn.append(det);
464
+ }
465
+ btn.onclick = () => submitChoice(opt.id, opt.label);
466
+ controls.push(btn);
467
+ optsWrap.append(btn);
468
+ }
469
+ card.append(optsWrap);
470
+ }
471
+
472
+ if (q.allowCustom !== false) {
473
+ const customRow = document.createElement('div');
474
+ customRow.className = 'clado-agent-question-custom';
475
+ const field = document.createElement('input');
476
+ field.type = 'text';
477
+ field.placeholder = '或输入你的补充决策…';
478
+ const okBtn = document.createElement('button');
479
+ okBtn.type = 'button';
480
+ okBtn.className = 'clado-ui-button clado-agent-option-submit';
481
+ okBtn.textContent = '确认';
482
+ okBtn.onclick = () => submitChoice('custom', field.value);
483
+ field.onkeydown = ev => {
484
+ if (ev.key === 'Enter' && !ev.isComposing) { ev.preventDefault(); submitChoice('custom', field.value); }
485
+ };
486
+ controls.push(field, okBtn);
487
+ customRow.append(field, okBtn);
488
+ card.append(customRow);
489
+ }
490
+ return card;
491
+ }
492
+
493
+ async function streamTurn(payload, { humanText, humanLabel, canvas, assistantLabel, isDispatch = false }) {
107
494
  running = true;
108
- input.value = '';
495
+ setHistoryOpen(false);
109
496
  syncActions();
110
- const user = message('human', text);
497
+
498
+ const user = message('human', humanText, humanLabel);
111
499
  if (canvas) {
112
500
  const context = document.createElement('small');
113
501
  context.textContent = [canvas.file.split('/').pop(), canvas.selection.length ? canvas.selection.length + ' 个选中标题' : ''].filter(Boolean).join(' · ');
114
502
  context.title = canvas.file;
115
503
  user.item.append(context);
116
504
  }
117
- const reply = message('assistant', '');
505
+ const reply = message('assistant', '', assistantLabel);
118
506
  reply.item.hidden = true;
507
+ reply.body.hidden = true;
119
508
  const replyText = reply.body.appendChild(document.createTextNode(''));
509
+ let thinkingPre = null;
120
510
  const tools = new Map();
121
511
  let finished = false;
512
+
122
513
  const apply = update => {
123
514
  const follow = log.scrollHeight - log.scrollTop - log.clientHeight < 48;
124
- if (update.type === 'text') {
515
+ if (update.type === 'thinking' && update.text) {
516
+ reply.item.hidden = false;
517
+ if (!thinkingPre) {
518
+ const details = createThinkingBlock('');
519
+ reply.item.insertBefore(details, reply.body);
520
+ thinkingPre = details.querySelector('pre');
521
+ }
522
+ thinkingPre.appendData ? thinkingPre.appendData(update.text) : (thinkingPre.textContent += update.text);
523
+ } else if (update.type === 'text') {
125
524
  reply.item.hidden = false;
525
+ reply.body.hidden = false;
126
526
  replyText.appendData(update.text);
527
+ } else if (update.type === 'changes' && Array.isArray(update.items) && update.items.length) {
528
+ reply.item.hidden = false;
529
+ reply.item.append(createChangesCard(update.items));
530
+ } else if (update.type === 'question' && update.question) {
531
+ reply.item.hidden = false;
532
+ reply.item.append(createQuestionCard(update.question));
127
533
  } else if (update.type === 'status') {
128
534
  showStatus(update.text);
129
535
  } else if (update.type === 'tool') {
@@ -136,15 +542,22 @@
136
542
  throw new Error(update.text);
137
543
  } else if (update.type === 'done') {
138
544
  finished = true;
139
- const labels = { cancelled: '已停止', max_tokens: '已达到本轮输出上限', max_turn_requests: '已达到本轮请求上限', refusal: '灵感 拒绝执行本轮请求' };
140
- showStatus(labels[update.reason] || '');
545
+ const labels = {
546
+ cancelled: '已停止',
547
+ awaiting_user: '等待你的确认后继续执行',
548
+ max_tokens: '已达到本轮输出上限',
549
+ max_turn_requests: '已达到本轮请求上限',
550
+ refusal: '拒绝执行本轮请求',
551
+ };
552
+ showStatus(labels[update.reason] || (isDispatch ? '执行器已完成,可切到「Diff 验收」核对代码变更' : ''));
141
553
  }
142
554
  if (follow) scroll();
143
555
  };
144
- showStatus('正在连接 灵感');
556
+
557
+ showStatus(isDispatch ? `正在派发给 ${assistantLabel || agentName}` : '正在连接 灵感');
145
558
  scroll();
146
559
  try {
147
- const response = await request({ action: 'prompt', session, text, canvas, viewId });
560
+ const response = await request(payload);
148
561
  if (!response.ok) {
149
562
  const failure = await response.json();
150
563
  throw new Error(failure.error || '发送失败。');
@@ -176,6 +589,26 @@
176
589
  syncActions();
177
590
  if (!panel.hidden) input.focus();
178
591
  }
592
+ }
593
+
594
+ form.onsubmit = async event => {
595
+ event.preventDefault();
596
+ if (!canSubmit()) return;
597
+ const isDispatch = workflow === 'dispatch';
598
+ const rawText = input.value.trim();
599
+ const text = rawText || (workflow === 'audit' ? '请核对当前本地 Git Diff 与画布设计契约、测试用例的差异,并给出验收结论。' : '');
600
+ const canvas = readCanvas();
601
+ input.value = '';
602
+ const executorName = executorSelect.selectedOptions?.[0]?.textContent || agentName;
603
+ const payload = isDispatch
604
+ ? { action: 'dispatch', session, executorId: executorSelect.value || undefined, text: rawText, canvas, viewId }
605
+ : { action: 'prompt', session, mode: workflow, persona, text, canvas, viewId };
606
+ await streamTurn(payload, {
607
+ humanText: isDispatch ? `[派发开发 → ${executorName}] ${rawText || '按当前画布规格实现代码并运行测试'}` : text,
608
+ canvas,
609
+ assistantLabel: isDispatch ? executorName : undefined,
610
+ isDispatch,
611
+ });
179
612
  };
180
613
  };
181
614
  })();