cladogram 0.5.5-beta.9 → 0.6.0-beta.10

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 (56) hide show
  1. package/README.md +106 -80
  2. package/bin/clado.js +35 -3
  3. package/docs/agent-context.md +63 -0
  4. package/docs/agent-reference.md +219 -0
  5. package/docs/release.md +123 -0
  6. package/package.json +5 -3
  7. package/plugins/agent/index.js +2485 -2982
  8. package/plugins/agent/settings.js +3592 -0
  9. package/plugins/agent/style.css +1441 -1315
  10. package/plugins/brand/index.js +84 -466
  11. package/plugins/brand/style.css +39 -172
  12. package/plugins/directory/index.js +10 -10
  13. package/plugins/view-controls/index.js +6 -1
  14. package/plugins/view-controls/style.css +41 -30
  15. package/skills/clado/SKILL.md +4 -2
  16. package/src/agent/automations.js +454 -0
  17. package/src/agent/clado-tools.js +792 -0
  18. package/src/agent/pi-bridge.js +2559 -0
  19. package/src/agent/pi-config.js +2411 -0
  20. package/src/agent/pi-hooks-perms.js +621 -0
  21. package/src/html/app-css.js +69 -1
  22. package/src/html/canvas-chain.js +32 -6
  23. package/src/html/canvas-focus.js +19 -13
  24. package/src/html/canvas-live-diff.js +1373 -0
  25. package/src/html/canvas-ui.css +503 -2
  26. package/src/html/canvas-wires.js +5 -1
  27. package/src/html/page.js +4 -2
  28. package/src/html/plugin-assets.js +5 -1
  29. package/src/html/plugin-ui.css +159 -134
  30. package/src/html/plugin-ui.js +3 -2
  31. package/src/html/runtime-api.js +152 -10
  32. package/src/html/runtime.js +546 -37
  33. package/src/server/agent.js +862 -1566
  34. package/src/server/assets/library-canvas.js +256 -9
  35. package/src/server/assets/library.css +951 -11
  36. package/src/server/assets/library.html +39 -4
  37. package/src/server/assets/library.js +1445 -224
  38. package/src/server/code-inspect.js +183 -0
  39. package/src/server/mcp.js +90 -22
  40. package/src/server/plugin-registry.js +671 -9
  41. package/src/server/plugins.js +1 -1
  42. package/src/server/workspace-hub.js +50 -0
  43. package/src/server/workspace.js +554 -39
  44. package/src/workspace/graph-snapshot.js +695 -0
  45. package/src/workspace/service.js +201 -9
  46. package/src/agent/commands.js +0 -204
  47. package/src/agent/context.js +0 -445
  48. package/src/agent/executors.js +0 -213
  49. package/src/agent/mcp-client.js +0 -308
  50. package/src/agent/probe.js +0 -234
  51. package/src/agent/providers/anthropic.js +0 -220
  52. package/src/agent/providers/index.js +0 -235
  53. package/src/agent/providers/openai.js +0 -146
  54. package/src/agent/runtime.js +0 -995
  55. package/src/agent/sessions.js +0 -336
  56. package/src/agent/skills.js +0 -190
@@ -44,7 +44,16 @@
44
44
  if (selectionTarget(event)) { event.preventDefault(); event.stopImmediatePropagation(); }
45
45
  }, true);
46
46
  const world = doc.querySelector('#world');
47
- new MutationObserver(() => { if (paths.length) paint(doc, api); }).observe(world, { childList: true, subtree: true });
47
+ let paintRaf = 0;
48
+ new MutationObserver(records => {
49
+ if (!paths.length || paintRaf) return;
50
+ const relevant = records.some(r => {
51
+ const t = r.target;
52
+ return t && t.closest && t.id !== 'wires' && t.id !== 'mwires' && !t.closest('#wires,#mwires,.port,.clado-obj');
53
+ });
54
+ if (!relevant) return;
55
+ paintRaf = requestAnimationFrame(() => { paintRaf = 0; if (paths.length) paint(doc, api); });
56
+ }).observe(world, { childList: true, subtree: true });
48
57
  paint(doc, api);
49
58
  }
50
59
  function bindChain() {
@@ -73,13 +82,13 @@
73
82
  return window.__CLADO_RESOURCES__?.[path] || (window.__CLADO_OFFLINE__ ? null : '/library/resource?file='+encodeURIComponent(path)+workspace);
74
83
  }
75
84
  function rewriteDocumentLinks() {
76
- document.querySelectorAll('img[src]:not([data-resource])').forEach(img=>{
77
- const src=img.getAttribute('src');if (!src || /^(?:[a-z][a-z0-9+.-]*:|\/\/|\/)/i.test(src))return;
78
- try { const url=resourceUrl(src);if(url){img.dataset.resource='1';img.src=url;} } catch {}
85
+ document.querySelectorAll('img[src]:not([data-resource]),video[src]:not([data-resource]),audio[src]:not([data-resource])').forEach(el=>{
86
+ const src=el.getAttribute('src');if (!src || /^(?:[a-z][a-z0-9+.-]*:|\/\/|\/)/i.test(src))return;
87
+ try { const url=resourceUrl(src);if(url){el.dataset.resource='1';el.src=url;} } catch {}
79
88
  });
80
89
  document.querySelectorAll('a[href]:not(.edge):not([data-resource])').forEach(a=>{
81
90
  const href=a.getAttribute('href');if (!href || /^(?:[a-z][a-z0-9+.-]*:|\/\/|\/|#)/i.test(href) || /\.mdc?(?:#|$)/i.test(href))return;
82
- if (!/\.(?:png|jpe?g|gif|webp|svg|pdf|txt|csv|json)(?:#|$)/i.test(href))return;
91
+ if (!/\.(?:png|jpe?g|gif|webp|svg|pdf|txt|csv|json|mp4|webm|mov|ogv|mp3|wav|m4a|ogg|flac)(?:#|$)/i.test(href))return;
83
92
  try { const url=resourceUrl(href);if(url){a.dataset.resource='1';a.href=url;} } catch {}
84
93
  });
85
94
  document.querySelectorAll('a[href]:not(.edge):not([data-canvas-file])').forEach(a => {
@@ -94,7 +103,16 @@
94
103
  a.removeAttribute('target');
95
104
  });
96
105
  }
97
- new MutationObserver(rewriteDocumentLinks).observe(document.querySelector('#world'), { childList: true, subtree: true });
106
+ let rewriteRaf = 0;
107
+ new MutationObserver(records => {
108
+ if (rewriteRaf) return;
109
+ const relevant = records.some(r => {
110
+ const t = r.target;
111
+ return t && t.closest && t.id !== 'wires' && t.id !== 'mwires' && !t.closest('#wires,#mwires,.port,.clado-obj');
112
+ });
113
+ if (!relevant) return;
114
+ rewriteRaf = requestAnimationFrame(() => { rewriteRaf = 0; rewriteDocumentLinks(); });
115
+ }).observe(document.querySelector('#world'), { childList: true, subtree: true });
98
116
  rewriteDocumentLinks();
99
117
  async function settle() {
100
118
  await new Promise(resolve => requestAnimationFrame(resolve));
@@ -351,6 +369,69 @@
351
369
  });
352
370
  }
353
371
 
372
+ const CODE_REF_RE = /^(?:[\w./-]+\.(?:js|ts|tsx|jsx|mjs|cjs|py|go|rs|java|kt|cpp|c|h|rb|php|cs|vue|svelte|css|html|json|yaml|yml|toml|sql)|[A-Za-z_]\w*(?:#|\.)[A-Za-z_]\w*)$/;
373
+
374
+ function extractCodeRef(text) {
375
+ const raw = String(text || '').trim();
376
+ if (!raw) return '';
377
+ if (CODE_REF_RE.test(raw)) return raw;
378
+ const m = raw.match(/(?:[\w./-]+\.(?:js|ts|tsx|jsx|mjs|cjs|py|go|rs|java|kt|cpp|c|h|rb|php|cs|vue|svelte|css|html|json)|[A-Za-z_]\w*#[A-Za-z_]\w*)/);
379
+ return m ? m[0] : '';
380
+ }
381
+
382
+ function resolveCodeBlockLine(targetEl, clientY) {
383
+ const pre = targetEl?.closest?.('pre');
384
+ const code = pre?.querySelector?.('code');
385
+ if (!pre || !code) return null;
386
+ const container = targetEl.closest('.slice[data-id]') || targetEl.closest('#world > .card');
387
+ if (!container) return null;
388
+ const pres = Array.from(container.querySelectorAll('pre')).filter(p => !p.closest('.slice') || p.closest('.slice') === container);
389
+ const paragraph = Math.max(1, pres.indexOf(pre) + 1);
390
+ const lines = String(code.textContent || '').replace(/\r\n?/g, '\n').split('\n');
391
+ const rect = code.getBoundingClientRect();
392
+ const cs = getComputedStyle(code);
393
+ const lh = parseFloat(cs.lineHeight) || (parseFloat(cs.fontSize) || 12) * 1.5;
394
+ let line = 1;
395
+ if (rect.height > 0 && lines.length > 1) {
396
+ const approx = Math.floor((clientY - rect.top) / Math.max(10, lh)) + 1;
397
+ line = Math.max(1, Math.min(lines.length, approx));
398
+ }
399
+ return { paragraph, line, lineText: (lines[line - 1] || '').trim().slice(0, 80) };
400
+ }
401
+
402
+ function resolveMermaidNode(targetEl) {
403
+ const mmd = targetEl?.closest?.('.mermaid');
404
+ if (!mmd) return null;
405
+ const container = targetEl.closest('.slice[data-id]') || targetEl.closest('#world > .card');
406
+ if (!container) return null;
407
+ const mmds = Array.from(container.querySelectorAll('.mermaid'));
408
+ const paragraph = Math.max(1, mmds.indexOf(mmd) + 1);
409
+ const g = targetEl.closest('g.node, g.cluster');
410
+ if (!g) return null;
411
+ const rawId = String(g.id || g.getAttribute('data-id') || '').trim();
412
+ const cleanId = rawId.replace(/^flowchart-/, '').replace(/-\d+$/, '') || rawId;
413
+ const label = String(g.textContent || '').replace(/\s+/g, ' ').trim().slice(0, 60);
414
+ if (!cleanId && !label) return null;
415
+ return { paragraph, nodeId: cleanId || label, nodeLabel: label || cleanId };
416
+ }
417
+
418
+ function getCardTextSelection() {
419
+ const sel = window.getSelection?.();
420
+ if (!sel || sel.isCollapsed || !sel.rangeCount) return null;
421
+ const text = sel.toString().replace(/\s+/g, ' ').trim();
422
+ if (!text || text.length > 240) return null;
423
+ const range = sel.getRangeAt(0);
424
+ const ancestor = range.commonAncestorContainer;
425
+ const el = ancestor.nodeType === 1 ? ancestor : ancestor.parentElement;
426
+ if (!el || el.closest('input, textarea, [contenteditable="true"], .clado-link-pick-banner, .clado-sel-quickbar')) return null;
427
+ const mdBody = el.closest('.markdown-body');
428
+ const cardOrSlice = el.closest('.slice[data-id]') || el.closest('#world > .card') || el.closest('#article .slice[data-path]');
429
+ if (!mdBody || !cardOrSlice) return null;
430
+ const node = runtime.selectionPath?.(cardOrSlice);
431
+ if (!node || !node.length) return null;
432
+ return { text, node, rect: range.getBoundingClientRect(), el: cardOrSlice };
433
+ }
434
+
354
435
  function resolveContextTarget(targetEl, clientX, clientY) {
355
436
  const { wx, wy } = clientToWorld(clientX, clientY);
356
437
  const subTrack = targetEl?.closest?.('[data-subtarget="track"]');
@@ -373,16 +454,72 @@
373
454
  const obj = currentObjects.find(o => o.id === objEl.dataset.objId);
374
455
  if (obj) return { kind: 'object', object: obj, wx, wy };
375
456
  }
457
+ const selHit = getCardTextSelection();
458
+ if (selHit) {
459
+ return {
460
+ kind: 'text-selection',
461
+ node: selHit.node,
462
+ selectedText: selHit.text,
463
+ title: selHit.node[selHit.node.length - 1] || '选中文本',
464
+ label: '「' + selHit.text.slice(0, 28) + '」',
465
+ codeRef: extractCodeRef(selHit.text),
466
+ version: window.__CLADO_FILE_VERSION__,
467
+ wx,
468
+ wy,
469
+ };
470
+ }
376
471
  const cardEl = targetEl?.closest?.('#world > .card');
377
472
  if (cardEl && document.body.dataset.mode === 'cards') {
378
- const path = runtime.selectionPath?.(cardEl);
473
+ const sliceEl = targetEl?.closest?.('.slice[data-id]');
474
+ const path = runtime.selectionPath?.(sliceEl || cardEl);
475
+ const cardPath = runtime.selectionPath?.(cardEl);
379
476
  const cardX = parseFloat(cardEl.style.left) || wx;
380
477
  const cardY = parseFloat(cardEl.style.top) || wy;
381
478
  const cardW = cardEl.offsetWidth || 360;
479
+ const title = path?.[path.length - 1] || cardEl.querySelector('.card-title h1')?.textContent || '卡片';
480
+
481
+ const codeHit = resolveCodeBlockLine(targetEl, clientY);
482
+ if (codeHit && path) {
483
+ return {
484
+ kind: 'code-line',
485
+ node: path,
486
+ title,
487
+ line: codeHit.line,
488
+ lineText: codeHit.lineText,
489
+ sourcePoint: { kind: 'code', paragraph: codeHit.paragraph, line: codeHit.line },
490
+ label: `${title} · 代码第 ${codeHit.line} 行`,
491
+ codeRef: extractCodeRef(codeHit.lineText) || extractCodeRef(title),
492
+ version: window.__CLADO_FILE_VERSION__,
493
+ wx: Math.round(cardX + cardW + 24),
494
+ wy: Math.round(cardY + 12),
495
+ };
496
+ }
497
+
498
+ const mmdHit = resolveMermaidNode(targetEl);
499
+ if (mmdHit && path) {
500
+ return {
501
+ kind: 'mermaid-node',
502
+ node: path,
503
+ title,
504
+ nodeId: mmdHit.nodeId,
505
+ nodeLabel: mmdHit.nodeLabel,
506
+ sourcePoint: { kind: 'mermaid', paragraph: mmdHit.paragraph, node: mmdHit.nodeId },
507
+ label: `图节点 · ${mmdHit.nodeLabel}`,
508
+ codeRef: extractCodeRef(mmdHit.nodeLabel) || extractCodeRef(title),
509
+ version: window.__CLADO_FILE_VERSION__,
510
+ wx: Math.round(cardX + cardW + 24),
511
+ wy: Math.round(cardY + 12),
512
+ };
513
+ }
514
+
515
+ const clickedCode = targetEl?.closest?.('code')?.textContent || '';
382
516
  return {
383
517
  kind: 'card',
384
- node: path || null,
385
- title: path?.[0] || cardEl.querySelector('.card-title h1')?.textContent || '卡片',
518
+ node: path || cardPath || null,
519
+ cardNode: cardPath || null,
520
+ title,
521
+ isIsolated: Boolean(cardEl.querySelector('.clado-card-isolated-pill')),
522
+ codeRef: extractCodeRef(clickedCode) || extractCodeRef(title),
386
523
  version: window.__CLADO_FILE_VERSION__,
387
524
  wx: Math.round(cardX + cardW + 24),
388
525
  wy: Math.round(cardY + 12),
@@ -391,10 +528,88 @@
391
528
  return { kind: 'blank', wx, wy };
392
529
  }
393
530
 
531
+ // ============ 卡内划词极简操作条 + 代码符号快速跳转 ============
532
+ let selQuickbarEl = null;
533
+ let activeSelectionHit = null;
534
+
535
+ function hideSelQuickbar() {
536
+ if (selQuickbarEl) selQuickbarEl.remove();
537
+ selQuickbarEl = null;
538
+ activeSelectionHit = null;
539
+ }
540
+
541
+ function showSelQuickbar(hit) {
542
+ hideSelQuickbar();
543
+ if (!hit || window.__CLADO_OFFLINE__ || chainOpen() || document.body.classList.contains('clado-link-picking')) return;
544
+ activeSelectionHit = hit;
545
+ const bar = document.createElement('div');
546
+ bar.className = 'clado-sel-quickbar';
547
+ bar.innerHTML =
548
+ '<button type="button" data-sq="ask">问 AI</button>'
549
+ + '<span class="sep" aria-hidden="true"></span>'
550
+ + '<button type="button" data-sq="edit">让 AI 修改</button>'
551
+ + '<span class="sep" aria-hidden="true"></span>'
552
+ + '<button type="button" data-sq="link">连接到…</button>';
553
+ document.body.appendChild(bar);
554
+ const bw = bar.offsetWidth || 210;
555
+ const bh = bar.offsetHeight || 32;
556
+ const cx = hit.rect.left + hit.rect.width / 2 - bw / 2;
557
+ const cy = hit.rect.top - bh - 8;
558
+ bar.style.left = Math.max(10, Math.min(window.innerWidth - bw - 10, cx)) + 'px';
559
+ bar.style.top = (cy > 10 ? cy : hit.rect.bottom + 8) + 'px';
560
+
561
+ bar.addEventListener('pointerdown', ev => { ev.stopPropagation(); });
562
+ bar.addEventListener('click', ev => {
563
+ const btn = ev.target.closest('button[data-sq]');
564
+ if (!btn || !activeSelectionHit) return;
565
+ ev.preventDefault();
566
+ ev.stopPropagation();
567
+ const action = btn.dataset.sq;
568
+ const cur = activeSelectionHit;
569
+ hideSelQuickbar();
570
+ if (action === 'link') {
571
+ runtime.startLinkPick?.({
572
+ kind: 'text-selection',
573
+ node: cur.node,
574
+ selectedText: cur.text,
575
+ label: '「' + cur.text.slice(0, 28) + '」',
576
+ });
577
+ } else {
578
+ post('selection-action', { mode: action, node: cur.node, selectedText: cur.text });
579
+ }
580
+ });
581
+ selQuickbarEl = bar;
582
+ }
583
+
584
+ if (!window.__CLADO_OFFLINE__) {
585
+ document.addEventListener('pointerdown', ev => {
586
+ if (selQuickbarEl && !ev.target.closest('.clado-sel-quickbar')) hideSelQuickbar();
587
+ }, true);
588
+ document.addEventListener('mouseup', ev => {
589
+ if (ev.button !== 0 || ev.target.closest('.clado-sel-quickbar')) return;
590
+ setTimeout(() => {
591
+ const hit = getCardTextSelection();
592
+ if (hit && hit.text.length >= 2) showSelQuickbar(hit);
593
+ else hideSelQuickbar();
594
+ }, 24);
595
+ });
596
+ document.addEventListener('click', ev => {
597
+ if (!(ev.shiftKey || ev.altKey)) return;
598
+ const codeEl = ev.target.closest('.markdown-body code');
599
+ if (!codeEl) return;
600
+ const ref = extractCodeRef(codeEl.textContent);
601
+ if (!ref) return;
602
+ ev.preventDefault();
603
+ ev.stopImmediatePropagation();
604
+ post('open-code-ref', { ref });
605
+ }, true);
606
+ }
607
+
394
608
  if (!window.__CLADO_OFFLINE__ && stageEl) {
395
609
  stageEl.addEventListener('contextmenu', ev => {
396
610
  if (document.body.dataset.mode !== 'cards' || chainOpen()) return;
397
611
  ev.preventDefault();
612
+ hideSelQuickbar();
398
613
  post('context-menu', {
399
614
  x: ev.clientX,
400
615
  y: ev.clientY,
@@ -447,10 +662,42 @@
447
662
  runtime.highlightActivity?.(event.data);
448
663
  return;
449
664
  }
665
+ if (event.data?.type === 'clado-workspace:presence') {
666
+ runtime.setCardPresence?.(event.data.presence);
667
+ return;
668
+ }
669
+ if (event.data?.type === 'clado-workspace:presence-sync') {
670
+ runtime.syncCardPresence?.(event.data.entries || []);
671
+ return;
672
+ }
673
+ if (event.data?.type === 'clado-workspace:presence-config') {
674
+ runtime.setPresenceConfig?.(event.data.config);
675
+ return;
676
+ }
677
+ if (event.data?.type === 'clado-workspace:live-diff') {
678
+ runtime.playLiveDiff?.(event.data.prevDoc, event.data.touch, event.data.follow);
679
+ return;
680
+ }
681
+ if (event.data?.type === 'clado-workspace:canvas-draft') {
682
+ runtime.setDraftCard?.(event.data.draft);
683
+ return;
684
+ }
685
+ if (event.data?.type === 'clado-workspace:start-link-pick') {
686
+ runtime.startLinkPick?.(event.data.source);
687
+ return;
688
+ }
689
+ if (event.data?.type === 'clado-workspace:cancel-link-pick') {
690
+ runtime.cancelLinkPick?.();
691
+ return;
692
+ }
450
693
  if (event.data?.type === 'clado-workspace:focus-node') {
451
694
  runtime.focusNodePath?.(event.data.node, event.data.label);
452
695
  return;
453
696
  }
697
+ if (event.data?.type === 'clado-workspace:focus-tag') {
698
+ runtime.focusTag?.(event.data.tagId);
699
+ return;
700
+ }
454
701
  if (event.data?.type !== 'clado-workspace:display') return;
455
702
  if (runtime.isChainOpen?.()) { deferredDisplay=event.data;return; }
456
703
  const {mode,theme,anchor}=event.data;