dsh-plugin-inspiration-workbench 0.10.0 → 0.13.3

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.
@@ -15,6 +15,12 @@ import { createMarkdown } from './markdown';
15
15
  import { createRenderers } from './render';
16
16
  import { createCardTools } from './card-tools';
17
17
  import { createModelMenu, createArrangeMenu, createSettingsMenu, createWorkspaceMenu } from './popovers';
18
+ import { createFlowRecorder } from './flow-recorder';
19
+ import { createTaskStore } from './task-store';
20
+ import { createTaskController } from './task-controller.js';
21
+ import { createTaskView } from './task-view.js';
22
+ import { createFileStore } from './file-store.js';
23
+ import { openFilePreview, fileKind, formatBytes } from './file-preview.js';
18
24
 
19
25
  const STORAGE_KEY = 'dsh.inspiration-workbench.v1';
20
26
  const CANVAS_KEY = 'dsh.inspiration-workbench.canvases.v1';
@@ -40,7 +46,7 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
40
46
  stacks: [],
41
47
  seq: 0,
42
48
  stackSeq: 0,
43
- settings: { inlineApprovals: false, chainWheel: true },
49
+ settings: { inlineApprovals: false, chainWheel: false },
44
50
  selected: new Set(),
45
51
  zTop: 20,
46
52
  focusedId: null,
@@ -48,8 +54,20 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
48
54
  board: { active: false, key: 'status', saved: new Map() },
49
55
  canvases: [],
50
56
  activeCanvas: null,
57
+ mode: 'canvas',
51
58
  };
52
59
 
60
+ const taskStore = createTaskStore();
61
+ const fileStore = createFileStore();
62
+ const flow = createFlowRecorder({
63
+ onChange: (events) => taskStore.setFlowEvents(events),
64
+ });
65
+ const taskLoadPromise = taskStore.load().then((saved) => {
66
+ flow.replace(saved.flowEvents);
67
+ schedulePipelines();
68
+ return saved;
69
+ }).catch(() => null);
70
+
53
71
  // Turn expansion survives a remote layout apply (which rebuilds card DOM):
54
72
  // the state lives on the board keyed by session + turn, not on the card object.
55
73
  const expandedTurns = new Map();
@@ -60,6 +78,7 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
60
78
  toggleSearch,
61
79
  runSearch,
62
80
  stepSearch,
81
+ highlight,
63
82
  closeOutline,
64
83
  openOutline,
65
84
  closeRefPicker,
@@ -123,6 +142,13 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
123
142
  btnNew.type = 'button';
124
143
  const btnCanvas = el('button', 'iwb-btn', icon('canvas', 13) + '<span>' + t('canvas') + '</span>');
125
144
  btnCanvas.type = 'button';
145
+ const btnBlank = el('button', 'iwb-btn iwb-btn-icon', icon('plus', 13));
146
+ btnBlank.type = 'button';
147
+ btnBlank.title = '新建空白画布(自动保存当前画布)';
148
+ btnBlank.setAttribute('aria-label', '新建空白画布');
149
+ const btnFile = el('button', 'iwb-btn', icon('clip', 13) + '<span>文件</span>');
150
+ btnFile.type = 'button';
151
+ btnFile.title = '添加图片或文件到画布';
126
152
  const btnArrange = el('button', 'iwb-btn', icon('arrange', 13) + '<span>' + t('arrange') + '</span>');
127
153
  btnArrange.type = 'button';
128
154
  const btnBoard = el('button', 'iwb-btn', icon('board', 13) + '<span>' + t('boardMode') + '</span>');
@@ -139,8 +165,32 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
139
165
  const btnSettings = el('button', 'iwb-btn', icon('sliders', 13) + '<span>' + t('settings') + '</span>');
140
166
  btnSettings.type = 'button';
141
167
  const actions = el('div', 'iwb-toolbar-actions');
142
- actions.append(todoBar, btnNew, btnNote, btnCanvas, btnArrange, btnBoard, btnFit, btnCollapse, btnSettings);
143
- toolbar.append(titles, hint, actions);
168
+ actions.append(todoBar, btnNew, btnNote, btnFile, btnCanvas, btnBlank, btnArrange, btnBoard, btnFit, btnCollapse, btnSettings);
169
+ const modeSwitch = el('div', 'iwb-mode-switch');
170
+ modeSwitch.setAttribute('role', 'tablist');
171
+ modeSwitch.setAttribute('aria-label', '工作视图');
172
+ const btnCanvasMode = el('button', 'iwb-mode-tab active', '<span>画布</span>');
173
+ btnCanvasMode.type = 'button';
174
+ btnCanvasMode.setAttribute('role', 'tab');
175
+ btnCanvasMode.setAttribute('aria-selected', 'true');
176
+ const btnTaskMode = el('button', 'iwb-mode-tab', '<span>任务</span>');
177
+ btnTaskMode.type = 'button';
178
+ btnTaskMode.setAttribute('role', 'tab');
179
+ btnTaskMode.setAttribute('aria-selected', 'false');
180
+ modeSwitch.append(btnCanvasMode, btnTaskMode);
181
+ toolbar.append(titles, modeSwitch, hint, actions);
182
+
183
+ const fileInput = document.createElement('input');
184
+ fileInput.type = 'file';
185
+ fileInput.multiple = true;
186
+ fileInput.hidden = true;
187
+ fileInput.accept = 'image/*,.pdf,.docx,.pptx,.txt,.md,.markdown,.json,.jsonl,.csv,.log,.html,.css,.js,.ts,.tsx,.jsx,.py,.java,.go,.rs,.sql,.sh,.yaml,.yml';
188
+ fileInput.addEventListener('change', async () => {
189
+ for (const file of [...fileInput.files]) await addFileCard(file);
190
+ fileInput.value = '';
191
+ });
192
+ document.body.appendChild(fileInput);
193
+ btnFile.addEventListener('click', () => fileInput.click());
144
194
 
145
195
  const canvas = el('div', 'iwb-canvas');
146
196
  canvas.tabIndex = 0;
@@ -529,26 +579,158 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
529
579
  function noteBody(card) {
530
580
  const body = card.el.querySelector('.iwb-body');
531
581
  body.textContent = '';
532
- const wrap = el('div', 'iwb-note');
582
+ card.viewMode = card.viewMode || 'preview';
583
+
584
+ const toggle = el('div', 'iwb-note-toggle');
585
+ const previewButton = el('button', 'iwb-note-toggle-button', '预览');
586
+ previewButton.type = 'button';
587
+ previewButton.title = '查看 Markdown 渲染效果';
588
+ const sourceButton = el('button', 'iwb-note-toggle-button', '源码');
589
+ sourceButton.type = 'button';
590
+ sourceButton.title = '查看 Markdown 源码';
591
+ toggle.append(previewButton, sourceButton);
592
+ body.appendChild(toggle);
593
+
594
+ const preview = el('div', 'iwb-note-preview');
595
+ body.appendChild(preview);
533
596
  const area = document.createElement('textarea');
534
597
  area.className = 'iwb-note-input';
535
598
  area.value = card.note || '';
536
599
  area.placeholder = t('note.placeholder');
600
+ body.appendChild(area);
601
+
602
+ const sync = () => {
603
+ preview.hidden = card.viewMode === 'source';
604
+ area.hidden = card.viewMode !== 'source';
605
+ previewButton.classList.toggle('active', card.viewMode === 'preview');
606
+ sourceButton.classList.toggle('active', card.viewMode === 'source');
607
+ previewButton.setAttribute('aria-pressed', String(card.viewMode === 'preview'));
608
+ sourceButton.setAttribute('aria-pressed', String(card.viewMode === 'source'));
609
+ if (card.viewMode === 'preview') {
610
+ preview.textContent = '';
611
+ appendMarkdownLite(preview, card.note || '');
612
+ }
613
+ };
614
+ previewButton.addEventListener('click', () => {
615
+ card.viewMode = 'preview';
616
+ sync();
617
+ saveSoon();
618
+ });
619
+ sourceButton.addEventListener('click', () => {
620
+ card.viewMode = 'source';
621
+ sync();
622
+ saveSoon();
623
+ });
537
624
  area.addEventListener('input', () => {
538
625
  card.note = area.value;
626
+ sync();
539
627
  saveSoon();
540
628
  });
541
- wrap.appendChild(area);
629
+ sync();
630
+ }
631
+
632
+ function fileCardBody(card) {
633
+ const body = card.el.querySelector('.iwb-body');
634
+ body.textContent = '';
635
+ const wrap = el('div', 'iwb-file-body');
636
+ const kind = fileKind({ name: card.title, type: card.type });
637
+ if (kind === 'image') {
638
+ const image = el('img', 'iwb-file-thumb');
639
+ image.alt = card.title || '图片文件';
640
+ wrap.appendChild(image);
641
+ fileStore.get(card.fileId).then((record) => {
642
+ if (record?.blob) image.src = URL.createObjectURL(record.blob);
643
+ });
644
+ } else {
645
+ const iconNode = el('div', 'iwb-file-icon', fileKindIcon(kind));
646
+ wrap.appendChild(iconNode);
647
+ }
648
+ const name = el('div', 'iwb-file-name');
649
+ name.textContent = card.title || '未命名文件';
650
+ const meta = el('div', 'iwb-file-meta');
651
+ meta.textContent = (card.type || kind.toUpperCase()) + ' · ' + formatBytes(card.size);
652
+ const open = el('button', 'iwb-btn primary iwb-file-open', icon('eye', 13) + '<span>预览</span>');
653
+ open.type = 'button';
654
+ open.addEventListener('click', (event) => {
655
+ event.stopPropagation();
656
+ void openStoredFile(card);
657
+ });
658
+ wrap.append(name, meta, open);
542
659
  body.appendChild(wrap);
543
660
  }
544
661
 
545
- function addNote({ id, note, title, x, y, w, h, focus }) {
662
+ function fileKindIcon(kind) {
663
+ return ({ image: 'IMG', pdf: 'PDF', docx: 'DOC', pptx: 'PPT', html: 'HTML', text: 'TXT', other: 'FILE' })[kind] || 'FILE';
664
+ }
665
+
666
+ async function openStoredFile(card) {
667
+ const record = await fileStore.get(card.fileId);
668
+ if (!record) {
669
+ toast('文件内容不可用,请重新添加');
670
+ return;
671
+ }
672
+ openFilePreview({ ...record, name: card.title, type: card.type, size: card.size });
673
+ }
674
+
675
+ async function addFileCard(fileOrMeta) {
676
+ const fileId = fileOrMeta.fileId || await fileStore.put(fileOrMeta);
677
+ const kind = fileKind(fileOrMeta);
678
+ const card = {
679
+ id: 'f' + ++state.seq,
680
+ kind: 'file',
681
+ fileId,
682
+ title: fileOrMeta.name || fileOrMeta.title || '未命名文件',
683
+ type: fileOrMeta.type || kind,
684
+ size: Number(fileOrMeta.size) || 0,
685
+ x: typeof fileOrMeta.x === 'number' ? fileOrMeta.x : nextSlot().x,
686
+ y: typeof fileOrMeta.y === 'number' ? fileOrMeta.y : nextSlot().y,
687
+ w: fileOrMeta.w || DEFAULT_W,
688
+ h: fileOrMeta.h || DEFAULT_H,
689
+ stackId: null,
690
+ running: false,
691
+ composerDraft: '',
692
+ pipeline: null,
693
+ };
694
+ const node = el('article', 'iwb-card iwb-file-card', cardMarkup(card));
695
+ node.tabIndex = -1;
696
+ node.setAttribute('role', 'listitem');
697
+ node.style.left = card.x + 'px';
698
+ node.style.top = card.y + 'px';
699
+ node.style.width = card.w + 'px';
700
+ node.style.height = card.h + 'px';
701
+ card.el = node;
702
+ world.appendChild(node);
703
+ node.querySelector('.iwb-card-title').textContent = card.title;
704
+ for (const selector of ['.iwb-meta', '.iwb-elapsed', '.iwb-foot', '.iwb-pipe-badge', '[data-act="search"]', '[data-act="outline"]', '[data-act="pipe"]']) {
705
+ node.querySelector(selector)?.remove();
706
+ }
707
+ const previewButton = el('button', 'iwb-file-preview-button', icon('eye', 13));
708
+ previewButton.type = 'button';
709
+ previewButton.title = '预览文件';
710
+ previewButton.setAttribute('aria-label', '预览文件');
711
+ previewButton.addEventListener('click', (event) => {
712
+ event.stopPropagation();
713
+ void openStoredFile(card);
714
+ });
715
+ node.querySelector('.iwb-actions').insertBefore(previewButton, node.querySelector('[data-act="close"]'));
716
+ bindCard(card);
717
+ fileCardBody(card);
718
+ state.cards.push(card);
719
+ updateCounts();
720
+ updateEmptyState();
721
+ saveSoon();
722
+ if (fileOrMeta.focus !== false) centerCard(card);
723
+ return card;
724
+ }
725
+
726
+ function addNote({ id, note, title, x, y, w, h, focus, viewMode }) {
546
727
  if (disposed) return null;
547
728
  const slot = nextSlot();
548
729
  const card = {
549
730
  id: id || 'n' + ++state.seq,
550
731
  kind: 'note',
551
732
  note: note || '',
733
+ viewMode: viewMode || 'preview',
552
734
  title: title || t('note.title'),
553
735
  x: typeof x === 'number' ? x : slot.x,
554
736
  y: typeof y === 'number' ? y : slot.y,
@@ -586,7 +768,7 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
586
768
  return card;
587
769
  }
588
770
 
589
- function addSession({ sessionId, title, workspace, x, y, w, h, focus, pipeline }) {
771
+ function addSession({ sessionId, title, workspace, x, y, w, h, focus, pipeline, composerDraft }) {
590
772
  if (disposed) return null;
591
773
  const existing = state.cards.find((card) => card.sessionId === sessionId);
592
774
  if (existing) {
@@ -606,9 +788,10 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
606
788
  w: w || DEFAULT_W,
607
789
  h: h || DEFAULT_H,
608
790
  stackId: null,
791
+ kind: 'session',
609
792
  running: false,
610
793
  startedAt: 0,
611
- composerDraft: '',
794
+ composerDraft: composerDraft || '',
612
795
  toolOpen: new Map(),
613
796
  view: null,
614
797
  pipeline: pipeline || null,
@@ -969,6 +1152,28 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
969
1152
 
970
1153
  // ── named canvases ───────────────────────────────────────────────────
971
1154
  let canvasPop = null;
1155
+ let taskView = null;
1156
+ let taskController = null;
1157
+ let canvasSaveTimer = 0;
1158
+ let switchingCanvas = false;
1159
+
1160
+ function syncActiveCanvas() {
1161
+ clearTimeout(canvasSaveTimer);
1162
+ canvasSaveTimer = 0;
1163
+ if (switchingCanvas || !state.activeCanvas) return false;
1164
+ const active = syncCanvases().find((canvas) => canvas.id === state.activeCanvas);
1165
+ if (!active) return false;
1166
+ active.layout = layoutPayload();
1167
+ active.savedAt = Date.now();
1168
+ storeCanvases();
1169
+ return true;
1170
+ }
1171
+
1172
+ function scheduleActiveCanvasSave() {
1173
+ if (switchingCanvas || !state.activeCanvas) return;
1174
+ clearTimeout(canvasSaveTimer);
1175
+ canvasSaveTimer = setTimeout(syncActiveCanvas, 450);
1176
+ }
972
1177
 
973
1178
  function closeCanvasMenu() {
974
1179
  canvasPop?.remove();
@@ -1010,6 +1215,7 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
1010
1215
  btnCanvas.querySelector('span').textContent = active ? t('canvas') + ' · ' + active.name : t('canvas');
1011
1216
  btnCanvas.title = active ? t('canvas.active').replace('{name}', active.name) : t('canvas.title');
1012
1217
  btnCanvas.classList.toggle('iwb-btn-active', Boolean(active));
1218
+ if (state.mode === 'task') taskView?.refresh();
1013
1219
  }
1014
1220
 
1015
1221
  /** Only canvases that actually carry a board are switchable. */
@@ -1080,15 +1286,9 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
1080
1286
 
1081
1287
  function switchCanvas(id) {
1082
1288
  const target = syncCanvases().find((canvas) => canvas.id === id);
1083
- if (!target) return;
1084
- // Keep unsaved work: fold the current board back into its canvas first.
1085
- if (state.activeCanvas && state.activeCanvas !== id) {
1086
- const current = syncCanvases().find((canvas) => canvas.id === state.activeCanvas);
1087
- if (current) {
1088
- current.layout = layoutPayload();
1089
- current.savedAt = Date.now();
1090
- }
1091
- }
1289
+ if (!target || switchingCanvas) return;
1290
+ if (state.activeCanvas && state.activeCanvas !== id) syncActiveCanvas();
1291
+ switchingCanvas = true;
1092
1292
  state.activeCanvas = id;
1093
1293
  storeCanvases();
1094
1294
  closeCanvasMenu();
@@ -1098,9 +1298,90 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
1098
1298
  void fitView();
1099
1299
  } catch {
1100
1300
  toast(t('canvas.broken'));
1301
+ } finally {
1302
+ switchingCanvas = false;
1101
1303
  }
1102
1304
  }
1103
1305
 
1306
+ function createBlankCanvas() {
1307
+ if (state.activeCanvas) syncActiveCanvas();
1308
+ switchingCanvas = true;
1309
+ try {
1310
+ state.activeCanvas = null;
1311
+ reset();
1312
+ state.zoom = 1;
1313
+ state.pan = { x: 24, y: 24 };
1314
+ applyView();
1315
+ storeCanvases();
1316
+ save();
1317
+ toast('已新建空白画布,原画布仍保留在画布列表中');
1318
+ } finally {
1319
+ switchingCanvas = false;
1320
+ }
1321
+ }
1322
+
1323
+ function confirmUnsavedCanvas() {
1324
+ return new Promise((resolve) => {
1325
+ const dialog = document.createElement('dialog');
1326
+ dialog.className = 'iwb-confirm-dialog';
1327
+ dialog.setAttribute('aria-labelledby', 'iwb-unsaved-title');
1328
+ dialog.innerHTML = `
1329
+ <form method="dialog" class="iwb-confirm-form">
1330
+ <h2 id="iwb-unsaved-title">保存当前画布?</h2>
1331
+ <p>当前画布还没有命名。继续后会清空当前视图,但不会停止或删除任何 DSH 会话。</p>
1332
+ <label>画布名称<input name="canvasName" maxlength="60" value="未命名画布" autocomplete="off"></label>
1333
+ <div class="iwb-confirm-actions">
1334
+ <button type="button" data-choice="cancel">取消</button>
1335
+ <button type="button" data-choice="discard">不保存,继续</button>
1336
+ <button type="button" class="primary" data-choice="save">保存并继续</button>
1337
+ </div>
1338
+ </form>`;
1339
+ document.body.appendChild(dialog);
1340
+ const finish = (choice) => {
1341
+ const name = dialog.querySelector('input').value.trim();
1342
+ if (choice === 'save' && !name) {
1343
+ dialog.querySelector('input').focus();
1344
+ return;
1345
+ }
1346
+ dialog.remove();
1347
+ resolve({ choice, name });
1348
+ };
1349
+ dialog.querySelector('[data-choice="cancel"]').addEventListener('click', () => finish('cancel'));
1350
+ dialog.querySelector('[data-choice="discard"]').addEventListener('click', () => finish('discard'));
1351
+ dialog.querySelector('[data-choice="save"]').addEventListener('click', () => finish('save'));
1352
+ dialog.addEventListener('cancel', (event) => {
1353
+ event.preventDefault();
1354
+ finish('cancel');
1355
+ });
1356
+ dialog.showModal();
1357
+ const input = dialog.querySelector('input');
1358
+ input.focus();
1359
+ input.select();
1360
+ });
1361
+ }
1362
+
1363
+ async function requestNewBlankCanvas() {
1364
+ if (state.activeCanvas || !state.cards.length) {
1365
+ createBlankCanvas();
1366
+ return true;
1367
+ }
1368
+ const result = await confirmUnsavedCanvas();
1369
+ if (result.choice === 'cancel') return false;
1370
+ if (result.choice === 'save' && !saveCanvasAs(result.name)) return false;
1371
+ createBlankCanvas();
1372
+ return true;
1373
+ }
1374
+
1375
+ async function requestSwitchCanvas(id) {
1376
+ if (!state.activeCanvas && state.cards.length) {
1377
+ const result = await confirmUnsavedCanvas();
1378
+ if (result.choice === 'cancel') return false;
1379
+ if (result.choice === 'save' && !saveCanvasAs(result.name)) return false;
1380
+ }
1381
+ switchCanvas(id);
1382
+ return true;
1383
+ }
1384
+
1104
1385
  function deleteCanvas(id) {
1105
1386
  const target = syncCanvases().find((canvas) => canvas.id === id);
1106
1387
  state.canvases = syncCanvases().filter((canvas) => canvas.id !== id);
@@ -1184,7 +1465,7 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
1184
1465
  .replace('{time}', canvas.savedAt ? canvasStamp(canvas.savedAt) : '');
1185
1466
  pick.addEventListener('click', (event) => {
1186
1467
  event.stopPropagation();
1187
- switchCanvas(canvas.id);
1468
+ void requestSwitchCanvas(canvas.id);
1188
1469
  });
1189
1470
  const remove = el('button', 'iwb-canvas-remove');
1190
1471
  remove.type = 'button';
@@ -1444,14 +1725,32 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
1444
1725
  return target?.id || '';
1445
1726
  }
1446
1727
 
1728
+ function recordFlow(card, target, status, extra) {
1729
+ const active = syncCanvases().find((canvas) => canvas.id === state.activeCanvas);
1730
+ const event = flow.record({
1731
+ sourceSessionId: card.sessionId,
1732
+ targetId: target.id,
1733
+ targetKind: target.kind === 'note' ? 'note' : 'session',
1734
+ canvasId: state.activeCanvas || '',
1735
+ taskId: active?.taskId || '',
1736
+ status,
1737
+ mode: target.mode === 'draft' ? 'draft' : 'send',
1738
+ ...extra,
1739
+ });
1740
+ schedulePipelines();
1741
+ return event;
1742
+ }
1743
+
1447
1744
  function addPipeTarget(card, targetCard, fromSide) {
1448
1745
  const kind = targetCard.kind === 'note' ? 'note' : 'session';
1449
1746
  const id = kind === 'note' ? targetCard.id : targetCard.sessionId;
1450
1747
  if (!id || targetCard === card) return false;
1451
1748
  const targets = pipeTargets(card).filter((target) => !(target.kind === kind && target.id === id));
1452
1749
  const expanded = targets.length !== pipeTargets(card).length;
1453
- targets.push({ kind, id, mode: 'send', from: fromSide || 'right' });
1750
+ const target = { kind, id, mode: 'send', from: fromSide || 'right' };
1751
+ targets.push(target);
1454
1752
  setPipeTargets(card, targets);
1753
+ if (!expanded) recordFlow(card, target, 'waiting', { message: '连线已创建,等待上游完成' });
1455
1754
  return !expanded;
1456
1755
  }
1457
1756
 
@@ -1519,7 +1818,17 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
1519
1818
  });
1520
1819
  const path = document.createElementNS(SVG_NS, 'path');
1521
1820
  path.setAttribute('d', d);
1522
- path.setAttribute('class', 'iwb-link' + (card.running ? ' iwb-link-active' : ''));
1821
+ const edgeKey = card.sessionId + '=>' + (target.kind === 'note' ? 'note' : 'session') + ':' + target.id;
1822
+ const latest = flow.latestByEdge().get(edgeKey);
1823
+ const flowStatus = latest?.status || 'waiting';
1824
+ path.setAttribute('class', 'iwb-link iwb-link-' + flowStatus + (card.running ? ' iwb-link-active' : ''));
1825
+ path.setAttribute('data-flow-status', flowStatus);
1826
+ path.setAttribute('data-edge-key', edgeKey);
1827
+ if (latest?.message) {
1828
+ const title = document.createElementNS(SVG_NS, 'title');
1829
+ title.textContent = latest.message + (latest.at ? ' · ' + new Date(latest.at).toLocaleString() : '');
1830
+ path.appendChild(title);
1831
+ }
1523
1832
  if (target.mode === 'draft') path.setAttribute('stroke-dasharray', '4 4');
1524
1833
  const arrow = document.createElementNS(SVG_NS, 'path');
1525
1834
  arrow.setAttribute('d', arrowPath(edgeAnchor(targetCard, toSide), toSide));
@@ -1685,6 +1994,22 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
1685
1994
  }
1686
1995
  }
1687
1996
 
1997
+ function maybeDeliverCompleted(card) {
1998
+ if (card.running || card.kind === 'note') return;
1999
+ const text = lastAssistantText(card);
2000
+ if (!text) return;
2001
+ const fingerprint = text.slice(0, 160);
2002
+ if (card.catchUpFingerprint === fingerprint) return;
2003
+ const pending = pipeTargets(card).some((target) => (card.pipedFingerprints || {})[target.kind + ':' + target.id] !== fingerprint);
2004
+ if (!pending) return;
2005
+ card.catchUpFingerprint = fingerprint;
2006
+ // A completed upstream can predate the edge or a page restore. Deliver it once
2007
+ // instead of requiring a new running → idle transition.
2008
+ setTimeout(() => {
2009
+ if (!disposed && !card.running) void deliverPipeline(card);
2010
+ }, 0);
2011
+ }
2012
+
1688
2013
  async function deliverPipeline(card) {
1689
2014
  const targets = pipeTargets(card);
1690
2015
  if (!targets.length) return;
@@ -1721,6 +2046,7 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
1721
2046
  area.dispatchEvent(new Event('input', { bubbles: true }));
1722
2047
  targetCard.pipeDepth = depth;
1723
2048
  card.pipedFingerprints[key] = fingerprint;
2049
+ recordFlow(card, target, 'delivered', { fingerprint, message: '结果已写入便签' });
1724
2050
  ensureVisible(targetCard);
1725
2051
  toast(t(replace ? 'pipe.notedReplaced' : 'pipe.noted').replace('{name}', targetCard.title || t('untitled')));
1726
2052
  continue;
@@ -1734,16 +2060,20 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
1734
2060
  ensureVisible(targetCard);
1735
2061
  targetCard.pipeDepth = depth;
1736
2062
  card.pipedFingerprints[key] = fingerprint;
2063
+ recordFlow(card, target, 'drafted', { fingerprint, message: '已生成草稿,等待用户发送' });
1737
2064
  toast(t('pipe.drafted').replace('{name}', pipeTargetLabel(target)));
1738
2065
  continue;
1739
2066
  }
1740
2067
 
1741
2068
  try {
2069
+ recordFlow(card, target, 'delivering', { fingerprint, message: '正在投递到目标会话' });
1742
2070
  await adapter.prompt(target.id, text, 'queue');
1743
2071
  card.pipedFingerprints[key] = fingerprint;
1744
2072
  if (targetCard) targetCard.pipeDepth = depth;
2073
+ recordFlow(card, target, 'delivered', { fingerprint, message: '已发送到目标会话' });
1745
2074
  toast(t('pipe.sent').replace('{name}', pipeTargetLabel(target)));
1746
2075
  } catch (error) {
2076
+ recordFlow(card, target, 'failed', { fingerprint, message: error?.message || '投递失败' });
1747
2077
  toast(t('pipe.failed').replace('{error}', error?.message || ''));
1748
2078
  }
1749
2079
  }
@@ -1866,6 +2196,7 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
1866
2196
  } catch {
1867
2197
  /* ignore */
1868
2198
  }
2199
+ if (card.kind === 'file') void fileStore.remove(card.fileId);
1869
2200
  card.el.remove();
1870
2201
  try {
1871
2202
  card.detachApprovals?.();
@@ -1884,7 +2215,7 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
1884
2215
  renderPipeBadge(other);
1885
2216
  }
1886
2217
  }
1887
- schedulePipelines();
2218
+ renderPipelines();
1888
2219
  if (state.selected.delete(card.id)) renderBatchBar();
1889
2220
  if (state.focusedId === card.id) {
1890
2221
  state.focusedId = null;
@@ -1923,7 +2254,7 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
1923
2254
  }
1924
2255
 
1925
2256
  function renderCard(card) {
1926
- if (card.kind === 'note') return;
2257
+ if (card.kind === 'note' || card.kind === 'file') return;
1927
2258
  let view;
1928
2259
  try {
1929
2260
  view = adapter.read(card.sessionId);
@@ -2032,6 +2363,8 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
2032
2363
 
2033
2364
  renderBody(card, view);
2034
2365
  renderQueue(card, view);
2366
+ if (!card.running) maybeDeliverCompleted(card);
2367
+ taskController?.noteSessionRuntime(card);
2035
2368
  if (wasRunning !== card.running) {
2036
2369
  renderFoot(card);
2037
2370
  if (wasRunning && !card.running) void deliverPipeline(card);
@@ -2395,8 +2728,14 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
2395
2728
  switch (event.key) {
2396
2729
  case 'Enter':
2397
2730
  event.preventDefault();
2398
- if (card.kind === 'note') node.querySelector('.iwb-note-input')?.focus();
2399
- else node.querySelector('.iwb-foot textarea')?.focus();
2731
+ if (card.kind === 'note') {
2732
+ if (card.viewMode !== 'source') {
2733
+ card.viewMode = 'source';
2734
+ noteBody(card);
2735
+ saveSoon();
2736
+ }
2737
+ node.querySelector('.iwb-note-input')?.focus();
2738
+ } else node.querySelector('.iwb-foot textarea')?.focus();
2400
2739
  break;
2401
2740
  case 'Escape':
2402
2741
  event.preventDefault();
@@ -2649,10 +2988,17 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
2649
2988
  }
2650
2989
 
2651
2990
  function updateCounts() {
2991
+ if (state.mode === 'task') {
2992
+ const tasks = taskController?.snapshot().tasks || [];
2993
+ const actionable = tasks.filter((task) => ['needs_input', 'blocked', 'review'].includes(task.status)).length;
2994
+ countsNode.textContent = tasks.length + ' 项任务' + (actionable ? ' · ' + actionable + ' 项待处理' : '');
2995
+ return;
2996
+ }
2652
2997
  const running = state.cards.filter((c) => c.running).length;
2653
2998
  const parts = [t('count.cards').replace('{n}', String(state.cards.length))];
2654
2999
  if (running) parts.push(t('count.running').replace('{n}', String(running)));
2655
3000
  countsNode.textContent = parts.join(' · ');
3001
+ updateTodoBar();
2656
3002
  }
2657
3003
 
2658
3004
  function updateEmptyState() {
@@ -2674,6 +3020,7 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
2674
3020
  const payload = layoutPayload();
2675
3021
  localUpdatedAt = payload.updatedAt;
2676
3022
  window.localStorage.setItem(STORAGE_KEY, JSON.stringify(payload));
3023
+ scheduleActiveCanvasSave();
2677
3024
  if (!applyingRemote) {
2678
3025
  void pushRemoteLayout(payload, clientId).then((result) => {
2679
3026
  if (result?.rev) remoteRev = result.rev;
@@ -2684,7 +3031,7 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
2684
3031
  }
2685
3032
  }
2686
3033
 
2687
- function restore() {
3034
+ async function restore() {
2688
3035
  if (restoreDone) return;
2689
3036
  restoreDone = true;
2690
3037
  let saved = null;
@@ -2729,6 +3076,10 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
2729
3076
  }
2730
3077
  continue;
2731
3078
  }
3079
+ if (card?.kind === 'file') {
3080
+ await addFileCard({ ...card, focus: false });
3081
+ continue;
3082
+ }
2732
3083
  if (!card?.sessionId) continue;
2733
3084
  addSession({
2734
3085
  sessionId: card.sessionId,
@@ -2784,6 +3135,11 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
2784
3135
  workspace: card.workspace || '',
2785
3136
  kind: card.kind || 'session',
2786
3137
  note: card.note || '',
3138
+ viewMode: card.viewMode || 'preview',
3139
+ fileId: card.fileId || '',
3140
+ type: card.type || '',
3141
+ size: Number(card.size) || 0,
3142
+ composerDraft: card.composerDraft || '',
2787
3143
  pinned: Boolean(card.pinned),
2788
3144
  pipeline: pipeTargets(card).length ? { targets: pipeTargets(card) } : null,
2789
3145
  x: card.x,
@@ -2815,7 +3171,7 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
2815
3171
  }
2816
3172
 
2817
3173
  /** Replace the whole board with a saved layout document. */
2818
- function applyLayout(saved, options) {
3174
+ async function applyLayout(saved, options) {
2819
3175
  if (!saved || !Array.isArray(saved.cards)) throw new Error(t('settings.importInvalid'));
2820
3176
  reset();
2821
3177
  if (typeof saved.zoom === 'number') state.zoom = clamp(saved.zoom, 0.3, 1.6);
@@ -2828,6 +3184,8 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
2828
3184
  note.pinned = true;
2829
3185
  note.el?.classList.add('iwb-pinned');
2830
3186
  }
3187
+ } else if (card?.kind === 'file') {
3188
+ await addFileCard({ ...card, focus: false });
2831
3189
  } else if (card.sessionId) {
2832
3190
  addSession({ ...card });
2833
3191
  }
@@ -2929,6 +3287,9 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
2929
3287
  });
2930
3288
  state.cards = [];
2931
3289
  state.stacks = [];
3290
+ if (pipelineFrame) cancelAnimationFrame(pipelineFrame);
3291
+ pipelineFrame = 0;
3292
+ renderPipelines();
2932
3293
  try {
2933
3294
  window.localStorage.removeItem(STORAGE_KEY);
2934
3295
  } catch {
@@ -2955,6 +3316,23 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
2955
3316
  focusCard(current || cardOrder()[0], { announce: false });
2956
3317
  });
2957
3318
 
3319
+ canvas.addEventListener('paste', (event) => {
3320
+ if (event.target.closest('textarea')) return;
3321
+ const files = [...(event.clipboardData?.files || [])];
3322
+ if (!files.length) return;
3323
+ event.preventDefault();
3324
+ for (const file of files) void addFileCard(file);
3325
+ });
3326
+ canvas.addEventListener('dragover', (event) => {
3327
+ if (event.dataTransfer?.types?.includes('Files')) event.preventDefault();
3328
+ });
3329
+ canvas.addEventListener('drop', (event) => {
3330
+ const files = [...(event.dataTransfer?.files || [])];
3331
+ if (!files.length) return;
3332
+ event.preventDefault();
3333
+ for (const file of files) void addFileCard(file);
3334
+ });
3335
+
2958
3336
  canvas.addEventListener('pointerdown', (event) => {
2959
3337
  if (event.target.closest('.iwb-card') || event.target.closest('.iwb-zoom')) return;
2960
3338
  if (event.button !== 0 && event.button !== 1) return;
@@ -2984,15 +3362,20 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
2984
3362
  (event) => {
2985
3363
  const overCard = Boolean(event.target?.closest?.('.iwb-card'));
2986
3364
  if (overCard && !(event.ctrlKey || event.metaKey)) {
2987
- // In-card scrolling belongs to the browser. Once the transcript is at the
2988
- // edge the wheel chains back to the canvas, so paging never dead-ends.
3365
+ // Native card scrolling always wins. By default the canvas must not jump
3366
+ // when a transcript or note reaches its vertical edge. The legacy chain
3367
+ // remains available only as an explicit settings opt-in.
2989
3368
  const body = event.target.closest('.iwb-body');
2990
3369
  if (body && state.settings.chainWheel) {
2991
3370
  const atTop = body.scrollTop <= 0;
2992
3371
  const atBottom = body.scrollTop + body.clientHeight >= body.scrollHeight - 1;
2993
3372
  const scrollingUp = event.deltaY < 0;
2994
- if (!(atTop && scrollingUp) && !(atBottom && !scrollingUp)) return;
3373
+ if (!(atTop && scrollingUp) && !(atBottom && !scrollingUp)) {
3374
+ event.stopPropagation();
3375
+ return;
3376
+ }
2995
3377
  } else {
3378
+ event.stopPropagation();
2996
3379
  return;
2997
3380
  }
2998
3381
  }
@@ -3047,6 +3430,11 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
3047
3430
  openCanvasMenu();
3048
3431
  });
3049
3432
 
3433
+ btnBlank.addEventListener('click', (event) => {
3434
+ event.stopPropagation();
3435
+ void requestNewBlankCanvas();
3436
+ });
3437
+
3050
3438
  const onCanvasPointerDown = (event) => {
3051
3439
  if (!canvasPop) return;
3052
3440
  if (event.target?.closest?.('.iwb-canvas-menu') || event.target?.closest?.('.iwb-toolbar')) return;
@@ -3103,6 +3491,68 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
3103
3491
  };
3104
3492
  document.addEventListener('keydown', onKey);
3105
3493
 
3494
+ function setMode(mode) {
3495
+ state.mode = mode === 'task' ? 'task' : 'canvas';
3496
+ const isTask = state.mode === 'task';
3497
+ canvas.hidden = isTask;
3498
+ if (taskView) taskView.root.hidden = !isTask;
3499
+ for (const control of [btnNew, btnNote, btnCanvas, btnBlank, btnArrange, btnBoard, btnFit, btnCollapse, hint, todoBar]) {
3500
+ control.hidden = isTask;
3501
+ }
3502
+ btnCanvasMode.classList.toggle('active', !isTask);
3503
+ btnTaskMode.classList.toggle('active', isTask);
3504
+ btnCanvasMode.setAttribute('aria-selected', String(!isTask));
3505
+ btnTaskMode.setAttribute('aria-selected', String(isTask));
3506
+ if (isTask) taskView?.refresh();
3507
+ updateCounts();
3508
+ }
3509
+
3510
+ taskController = createTaskController({
3511
+ adapter,
3512
+ taskStore,
3513
+ flow,
3514
+ t,
3515
+ toast,
3516
+ bridge: {
3517
+ getCards: () => state.cards,
3518
+ getCanvases: syncCanvases,
3519
+ getActiveCanvas: () => state.activeCanvas,
3520
+ requestNewBlankCanvas,
3521
+ requestSwitchCanvas,
3522
+ saveCanvasAs,
3523
+ storeCanvases,
3524
+ addSession,
3525
+ addNote,
3526
+ setPipeTargets,
3527
+ renderFoot,
3528
+ fitView,
3529
+ centerCard,
3530
+ flash,
3531
+ switchCanvas,
3532
+ setMode,
3533
+ save,
3534
+ schedulePipelines,
3535
+ recordFlow,
3536
+ },
3537
+ });
3538
+ taskView = createTaskView({
3539
+ host: root,
3540
+ t,
3541
+ el,
3542
+ getSnapshot: () => taskController.snapshot(),
3543
+ actions: taskController,
3544
+ toast,
3545
+ });
3546
+ taskStore.subscribe(() => {
3547
+ if (state.mode === 'task') taskView.refresh();
3548
+ });
3549
+ taskLoadPromise.then(() => {
3550
+ if (state.mode === 'task') taskView.refresh();
3551
+ });
3552
+ btnCanvasMode.addEventListener('click', () => setMode('canvas'));
3553
+ btnTaskMode.addEventListener('click', () => setMode('task'));
3554
+ setMode('canvas');
3555
+
3106
3556
  applyView();
3107
3557
  updateEmptyState();
3108
3558
 
@@ -3133,6 +3583,8 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
3133
3583
  addSession,
3134
3584
  restore,
3135
3585
  reset,
3586
+ setMode,
3587
+ getTaskSnapshot: () => taskController?.snapshot(),
3136
3588
  dispose() {
3137
3589
  disposed = true;
3138
3590
  // Unmount happens while the host is tearing the page down: a throw here
@@ -3140,7 +3592,11 @@ export function createBoard({ host, adapter, approvals, t, onOpenSession }) {
3140
3592
  try {
3141
3593
  clearInterval(ticker);
3142
3594
  clearTimeout(saveTimer);
3595
+ clearTimeout(canvasSaveTimer);
3143
3596
  clearTimeout(toastTimer);
3597
+ taskStore.dispose();
3598
+ taskController?.dispose();
3599
+ taskView?.dispose();
3144
3600
  settingsMenu.close();
3145
3601
  tools.dispose();
3146
3602
  stopApprovalWatch?.();