cladogram 0.5.2 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -2
- package/bin/clado.js +55 -3
- package/docs/plugin-development.md +135 -0
- package/docs/plugin-ui.md +43 -0
- package/docs/release.md +26 -0
- package/package.json +8 -1
- package/plugins/agent/index.js +181 -0
- package/plugins/agent/style.css +105 -0
- package/plugins/brand/index.js +262 -0
- package/plugins/brand/style.css +162 -0
- package/plugins/directory/capabilities.json +27 -0
- package/plugins/directory/index.js +146 -0
- package/plugins/directory/style.css +48 -0
- package/plugins/view-controls/index.js +58 -0
- package/plugins/view-controls/style.css +46 -0
- package/skills/clado/SKILL.md +31 -17
- package/src/html/app-css.js +11 -42
- package/src/html/canvas-chain.js +5 -6
- package/src/html/canvas-focus.js +3 -4
- package/src/html/canvas-screenshot.js +199 -0
- package/src/html/canvas-ui.css +37 -9
- package/src/html/canvas-ui.js +35 -27
- package/src/html/page.js +11 -37
- package/src/html/plugin-assets.js +33 -0
- package/src/html/plugin-ui.css +176 -0
- package/src/html/plugin-ui.js +45 -0
- package/src/html/runtime-api.js +47 -0
- package/src/html/runtime.js +15 -12
- package/src/server/agent.js +222 -0
- package/src/server/assets/clado-plugin-sdk.js +111 -0
- package/src/server/assets/library-canvas.js +72 -11
- package/src/server/assets/library-plugin-bridge.js +84 -0
- package/src/server/assets/library-plugins.css +39 -0
- package/src/server/assets/library-plugins.js +200 -0
- package/src/server/assets/library.css +3 -6
- package/src/server/assets/library.html +2 -12
- package/src/server/assets/library.js +280 -44
- package/src/server/canvas-capture.js +47 -0
- package/src/server/export.js +5 -2
- package/src/server/mcp.js +44 -19
- package/src/server/plugin-capabilities.js +172 -0
- package/src/server/plugin-settings.js +117 -0
- package/src/server/plugins.js +236 -0
- package/src/server/workspace.js +157 -12
- package/src/skill/targets.js +1 -1
- package/src/util/update.js +58 -0
- package/src/vendor/html-to-image.min.js +24 -0
- package/src/workspace/content.js +3 -2
- package/src/workspace/service.js +81 -21
- package/src/workspace/tree.js +38 -0
- package/src/server/assets/tree.js +0 -26
|
@@ -4,11 +4,65 @@
|
|
|
4
4
|
let designKey = '', cardsInitialized = false, deferredDisplay = null, deferredDesign = null;
|
|
5
5
|
const chainOpen = () => !!runtime.isChainOpen?.();
|
|
6
6
|
const post = (type, detail = {}) => parent.postMessage({ type: 'clado-library:' + type, file, ...detail }, '*');
|
|
7
|
-
const labels = { mindmap: '脑图', cards: '卡片', doc: '文档' };
|
|
8
|
-
const modes = document.querySelector('#modes');
|
|
9
|
-
['mindmap','cards','doc'].forEach(mode => { const button = modes.querySelector('[data-mode="' + mode + '"]'); button.textContent = labels[mode]; modes.append(button); });
|
|
10
7
|
document.addEventListener('pointerdown', () => post('dismiss-directory'), true);
|
|
11
8
|
document.addEventListener('keydown', event => { if (event.key === 'Escape') post('dismiss-directory'); });
|
|
9
|
+
if (!window.__CLADO_OFFLINE__ && window.__CLADO_FILE_VERSION__) {
|
|
10
|
+
let paths = [];
|
|
11
|
+
const frames = new WeakSet();
|
|
12
|
+
const marked = '#world > .card,#world > .card .slice[data-id],#mind .mnode,#article .slice[data-path]';
|
|
13
|
+
function paint(doc, api) {
|
|
14
|
+
for (const el of doc.querySelectorAll(marked)) {
|
|
15
|
+
const path = api.selectionPath(el);
|
|
16
|
+
const selected = path && paths.some(node => node.length <= path.length && node.every((part, i) => part === path[i]));
|
|
17
|
+
const coveredByCard = el.matches('#world .slice') && paths.some(node => node.length === 1 && node[0] === path?.[0]);
|
|
18
|
+
el.classList.toggle('clado-ai-selected', !!selected && !coveredByCard);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function bindSelection(doc, api) {
|
|
22
|
+
if (frames.has(doc)) return;
|
|
23
|
+
frames.add(doc);
|
|
24
|
+
const apple = /Mac|iPhone|iPad/.test(navigator.platform);
|
|
25
|
+
function selectionTarget(event) {
|
|
26
|
+
if (event.button !== 0 || !(apple ? event.metaKey : event.ctrlKey)) return;
|
|
27
|
+
const title = event.target.closest('.card-title h1,.slice-head,#toc a[data-path]');
|
|
28
|
+
const node = title && api.selectionPath(title);
|
|
29
|
+
const blank = event.target.closest('#stage,#doc') && !event.target.closest('.canvas-card,.slice,.markdown-body,#toc,button,a,dialog');
|
|
30
|
+
if (node && api.hasSelectionPath(node) || blank) return { node: node || null };
|
|
31
|
+
}
|
|
32
|
+
const win = doc.defaultView;
|
|
33
|
+
win.addEventListener('pointerdown', event => {
|
|
34
|
+
if (selectionTarget(event)) event.stopPropagation();
|
|
35
|
+
}, true);
|
|
36
|
+
win.addEventListener('click', event => {
|
|
37
|
+
const target = selectionTarget(event);
|
|
38
|
+
if (!target) return;
|
|
39
|
+
event.preventDefault(); event.stopImmediatePropagation();
|
|
40
|
+
post('selection', { ...target, version: window.__CLADO_FILE_VERSION__ });
|
|
41
|
+
}, true);
|
|
42
|
+
win.addEventListener('dblclick', event => {
|
|
43
|
+
if (selectionTarget(event)) { event.preventDefault(); event.stopImmediatePropagation(); }
|
|
44
|
+
}, true);
|
|
45
|
+
const world = doc.querySelector('#world');
|
|
46
|
+
new MutationObserver(() => { if (paths.length) paint(doc, api); }).observe(world, { childList: true, subtree: true });
|
|
47
|
+
paint(doc, api);
|
|
48
|
+
}
|
|
49
|
+
function bindChain() {
|
|
50
|
+
const win = document.querySelector('.canvas-chain-dialog iframe')?.contentWindow;
|
|
51
|
+
if (win?.CladoRuntime) bindSelection(win.document, win.CladoRuntime);
|
|
52
|
+
}
|
|
53
|
+
document.addEventListener('clado:chain-open', () => {
|
|
54
|
+
document.querySelector('.canvas-chain-dialog iframe')?.addEventListener('load', bindChain, { once: true });
|
|
55
|
+
bindChain();
|
|
56
|
+
});
|
|
57
|
+
bindSelection(document, runtime);
|
|
58
|
+
window.addEventListener('message', event => {
|
|
59
|
+
if (event.source !== parent || event.origin !== new URL(document.baseURI).origin || event.data?.type !== 'clado-workspace:selection') return;
|
|
60
|
+
paths = event.data.nodes;
|
|
61
|
+
paint(document, runtime);
|
|
62
|
+
const win = document.querySelector('.canvas-chain-dialog iframe')?.contentWindow;
|
|
63
|
+
if (win?.CladoRuntime) paint(win.document, win.CladoRuntime);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
12
66
|
function resourceUrl(href) {
|
|
13
67
|
const target = new URL(href,'https://clado.local/documents/'+file);
|
|
14
68
|
if (!target.pathname.startsWith('/documents/')) return null;
|
|
@@ -58,8 +112,14 @@
|
|
|
58
112
|
let targetFile; try { targetFile = decodeURIComponent(target.pathname.slice('/documents/'.length)); } catch { return; }
|
|
59
113
|
post('open', { target: targetFile, anchor: parts.slice(1).join('#') });
|
|
60
114
|
}, true);
|
|
61
|
-
|
|
62
|
-
document.
|
|
115
|
+
document.addEventListener('clado:view-state', () => post('view-state'));
|
|
116
|
+
document.addEventListener('clado:lens-open', () => post('lens-modal', { value: true }));
|
|
117
|
+
document.addEventListener('clado:lens-close', () => post('lens-modal', { value: false }));
|
|
118
|
+
async function setMode(mode) {
|
|
119
|
+
if (chainOpen()) return;
|
|
120
|
+
runtime.setMode(mode);
|
|
121
|
+
await settle();
|
|
122
|
+
}
|
|
63
123
|
function setDesign(design) {
|
|
64
124
|
if (chainOpen()) { deferredDesign=design;return; }
|
|
65
125
|
const key = JSON.stringify(design); if (key === designKey) return; designKey = key;
|
|
@@ -80,17 +140,18 @@
|
|
|
80
140
|
if(event.source!==parent || event.data?.type!=='clado-workspace:display')return;
|
|
81
141
|
if (runtime.isChainOpen?.()) { deferredDisplay=event.data;return; }
|
|
82
142
|
const {mode,theme,anchor}=event.data;
|
|
83
|
-
if(['doc','cards','mindmap'].includes(mode))runtime.setMode(mode);
|
|
143
|
+
if(['doc','cards','mindmap'].includes(mode)) runtime.setMode(mode);
|
|
84
144
|
if(['light','dark'].includes(theme))runtime.setTheme(theme,false);
|
|
85
145
|
await settle();
|
|
86
146
|
if(anchor) { let title=anchor;try{title=decodeURIComponent(anchor);}catch{}runtime.focusTitle(title); }
|
|
87
|
-
post('
|
|
147
|
+
post('view-state');
|
|
88
148
|
});
|
|
89
|
-
window.CladoPrototype = { setDesign, isChainOpen: chainOpen, fit: () => { if(!chainOpen())runtime.refresh(); }, closeLens: runtime.closeLens, captureView: runtime.captureView, restoreView: runtime.restoreView };
|
|
149
|
+
window.CladoPrototype = { setDesign, setMode, isChainOpen: chainOpen, fit: () => { if(!chainOpen())runtime.refresh(); }, closeLens: runtime.closeLens, captureView: runtime.captureView, restoreView: runtime.restoreView };
|
|
90
150
|
let resizeTimer; addEventListener('resize', () => { clearTimeout(resizeTimer); resizeTimer = setTimeout(() => { if (!chainOpen() && document.body.dataset.mode !== 'doc') runtime.refresh(); }, 120); });
|
|
91
|
-
|
|
151
|
+
const ready = () => post('ready', { mode: document.body.dataset.mode, version: window.__CLADO_FILE_VERSION__ });
|
|
152
|
+
window.CladoPrototype.whenReady = settle().then(() => {
|
|
92
153
|
let anchor; try { anchor = decodeURIComponent(window.__CLADO_ANCHOR__ || params.get('anchor') || ''); } catch { anchor = ''; }
|
|
93
|
-
|
|
154
|
+
ready();
|
|
94
155
|
if (anchor) requestAnimationFrame(() => requestAnimationFrame(() => runtime.focusTitle(anchor)));
|
|
95
|
-
}).catch(
|
|
156
|
+
}).catch(ready);
|
|
96
157
|
})();
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
window.CladoCapabilityBridge = () => {
|
|
3
|
+
const viewId = crypto.randomUUID();
|
|
4
|
+
const endpoint = '/library/plugin-bridge?viewId=' + encodeURIComponent(viewId);
|
|
5
|
+
const handlers = new Map();
|
|
6
|
+
const keyOf = ({ pluginId, mountId }) => JSON.stringify([pluginId, mountId]);
|
|
7
|
+
let stream, connected = false;
|
|
8
|
+
|
|
9
|
+
async function send(message, captureId) {
|
|
10
|
+
const response = await fetch(captureId ? endpoint + '&captureId=' + encodeURIComponent(captureId) : endpoint, {
|
|
11
|
+
method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(message),
|
|
12
|
+
});
|
|
13
|
+
const result = await response.json();
|
|
14
|
+
if (!response.ok) throw Object.assign(new Error(result.error), { data: result.details });
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function run(call) {
|
|
19
|
+
const handler = handlers.get(keyOf(call.to))?.invoke;
|
|
20
|
+
let response;
|
|
21
|
+
try {
|
|
22
|
+
if (!handler) throw Object.assign(new Error('插件能力已断开。'), { kind: 'NO_PROVIDER' });
|
|
23
|
+
response = { action: 'result', callId: call.id, output: await handler(call) };
|
|
24
|
+
} catch (cause) {
|
|
25
|
+
response = {
|
|
26
|
+
action: 'error', callId: call.id, kind: cause.kind || 'PROVIDER_ERROR',
|
|
27
|
+
message: String(cause.message || '插件执行失败。').slice(0, 500),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const captureId = response.action === 'result' && call.to.pluginId === 'clado.host' && call.to.capability === 'canvas.capture'
|
|
31
|
+
? call.id : undefined;
|
|
32
|
+
try { await send(response, captureId); }
|
|
33
|
+
catch {
|
|
34
|
+
await send({ action: 'error', callId: call.id, kind: 'PROVIDER_ERROR', message: '插件响应无法传回服务。' }).catch(() => {});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function connect() {
|
|
39
|
+
stream = new EventSource(endpoint);
|
|
40
|
+
stream.addEventListener('ready', () => {
|
|
41
|
+
connected = true;
|
|
42
|
+
for (const { registration, active } of handlers.values()) {
|
|
43
|
+
if (active) send(registration).catch(error => console.warn('Clado 能力注册失败', error));
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
stream.addEventListener('call', event => { void run(JSON.parse(event.data)); });
|
|
47
|
+
stream.addEventListener('error', () => { connected = false; });
|
|
48
|
+
}
|
|
49
|
+
connect();
|
|
50
|
+
addEventListener('pagehide', () => { connected = false; stream.close(); });
|
|
51
|
+
addEventListener('pageshow', event => { if (event.persisted) connect(); });
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
viewId,
|
|
55
|
+
async provide(identity, invoke) {
|
|
56
|
+
const registration = { action: 'register', ...identity };
|
|
57
|
+
const key = keyOf(identity);
|
|
58
|
+
const entry = { registration, invoke, active: false };
|
|
59
|
+
handlers.set(key, entry);
|
|
60
|
+
try {
|
|
61
|
+
const result = connected ? await send(registration) : await new Promise((resolve, reject) => {
|
|
62
|
+
const pendingStream = stream;
|
|
63
|
+
const timeout = setTimeout(() => {
|
|
64
|
+
pendingStream.removeEventListener('ready', onReady);
|
|
65
|
+
reject(new Error('插件服务连接超时。'));
|
|
66
|
+
}, 5000);
|
|
67
|
+
function onReady() {
|
|
68
|
+
clearTimeout(timeout);
|
|
69
|
+
send(registration).then(resolve, reject);
|
|
70
|
+
}
|
|
71
|
+
pendingStream.addEventListener('ready', onReady, { once: true });
|
|
72
|
+
});
|
|
73
|
+
entry.active = true;
|
|
74
|
+
return result;
|
|
75
|
+
} catch (error) {
|
|
76
|
+
if (handlers.get(key) === entry) handlers.delete(key);
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
list(identity, query = {}) { return send({ action: 'list', ...identity, query }); },
|
|
81
|
+
call(identity, to, input) { return send({ action: 'call', ...identity, to, input }); },
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
})();
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
.clado-plugin-window {
|
|
2
|
+
min-width: 0;
|
|
3
|
+
border-radius: 12px;
|
|
4
|
+
}
|
|
5
|
+
.clado-plugin-frame {
|
|
6
|
+
display: block;
|
|
7
|
+
border: 0;
|
|
8
|
+
border-radius: inherit;
|
|
9
|
+
}
|
|
10
|
+
.clado-plugin-slot[data-slot="topbar"] {
|
|
11
|
+
top: 68px;
|
|
12
|
+
left: 50%;
|
|
13
|
+
transform: translateX(-50%);
|
|
14
|
+
max-width: 26vw;
|
|
15
|
+
overflow: auto hidden;
|
|
16
|
+
scrollbar-width: none;
|
|
17
|
+
}
|
|
18
|
+
.clado-plugin-slot[data-slot="topbar"] .clado-plugin-window { flex: 0 0 116px; height: 44px; }
|
|
19
|
+
.clado-plugin-slot[data-slot="top-left"] .clado-plugin-window,
|
|
20
|
+
.clado-plugin-slot[data-slot="top-right"] .clado-plugin-window,
|
|
21
|
+
.clado-plugin-slot[data-slot="bottom-right"] .clado-plugin-window { flex: 0 0 44px; height: 44px; width: 84px; }
|
|
22
|
+
.clado-plugin-slot[data-slot="bottombar"] {
|
|
23
|
+
bottom: 16px;
|
|
24
|
+
left: 50%;
|
|
25
|
+
max-width: min(56vw, calc(100vw - 158px));
|
|
26
|
+
transform: translateX(-50%);
|
|
27
|
+
overflow: auto hidden;
|
|
28
|
+
}
|
|
29
|
+
.clado-plugin-slot[data-slot="bottombar"] .clado-plugin-window { flex: 0 0 160px; height: 44px; }
|
|
30
|
+
.clado-plugin-slot[data-slot="sidebar"],
|
|
31
|
+
.clado-plugin-slot[data-slot="canvas-overlay"] { flex-direction: column; overflow: auto; max-height: 40vh; right: 16px; }
|
|
32
|
+
.clado-plugin-slot[data-slot="sidebar"] { top: 72px; width: min(220px, 24vw); }
|
|
33
|
+
.clado-plugin-slot[data-slot="canvas-overlay"] { bottom: 16px; width: min(220px, 22vw); }
|
|
34
|
+
.clado-plugin-slot[data-slot="sidebar"] .clado-plugin-window,
|
|
35
|
+
.clado-plugin-slot[data-slot="canvas-overlay"] .clado-plugin-window { width: 100%; height: 145px; flex: none; }
|
|
36
|
+
.clado-plugin-slot[data-slot="bottom-left"] .clado-plugin-window { width: 148px; height: 42px; }
|
|
37
|
+
@media (max-width: 640px) {
|
|
38
|
+
.clado-plugin-slot[data-slot="topbar"] { top: 64px; max-width: 52vw; }
|
|
39
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
const maxMessageBytes = 262144;
|
|
3
|
+
const maxPart = value => Number.isSafeInteger(value) && value >= 0;
|
|
4
|
+
const isObject = value => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
5
|
+
const sameList = (value, expected) => Array.isArray(value) && value.length === expected.length &&
|
|
6
|
+
new Set(value).size === value.length && value.every(item => expected.includes(item));
|
|
7
|
+
const byteLength = value => new TextEncoder().encode(value).length;
|
|
8
|
+
|
|
9
|
+
window.CladoPluginHost = ({ host, readView, capabilities, isEnabled }) => {
|
|
10
|
+
const viewId = capabilities.viewId;
|
|
11
|
+
let catalog;
|
|
12
|
+
const tickets = new Map(), registered = new Set();
|
|
13
|
+
const theme = () => document.documentElement.dataset.theme || 'light';
|
|
14
|
+
function syncTheme(instance) {
|
|
15
|
+
instance.port.postMessage(JSON.stringify({ jsonrpc: '2.0', method: 'clado.ui.theme', params: { theme: theme() } }));
|
|
16
|
+
}
|
|
17
|
+
const observeTheme = new MutationObserver(() => {
|
|
18
|
+
for (const instance of registered) syncTheme(instance);
|
|
19
|
+
});
|
|
20
|
+
observeTheme.observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme'] });
|
|
21
|
+
function remove(instance) {
|
|
22
|
+
clearTimeout(instance.timer);
|
|
23
|
+
instance.port?.close();
|
|
24
|
+
tickets.delete(instance.ticket);
|
|
25
|
+
instance.unmount();
|
|
26
|
+
}
|
|
27
|
+
function mount(plugin, contribution) {
|
|
28
|
+
const surface = document.createElement('div');
|
|
29
|
+
surface.className = 'clado-plugin-window clado-ui-glass';
|
|
30
|
+
if (['sidebar', 'canvas-overlay'].includes(contribution.slot)) surface.classList.add('clado-ui-glass-panel');
|
|
31
|
+
surface.hidden = true;
|
|
32
|
+
const frame = document.createElement('iframe');
|
|
33
|
+
frame.className = 'clado-plugin-frame';
|
|
34
|
+
frame.title = plugin.name + ' · ' + contribution.id;
|
|
35
|
+
frame.dataset.mountId = contribution.id;
|
|
36
|
+
frame.setAttribute('sandbox', 'allow-scripts');
|
|
37
|
+
frame.referrerPolicy = 'no-referrer';
|
|
38
|
+
const ticket = crypto.randomUUID();
|
|
39
|
+
const instance = { plugin, contribution, frame, surface, ticket, instanceId: crypto.randomUUID(), state: 'connecting', pending: new Map() };
|
|
40
|
+
tickets.set(ticket, instance);
|
|
41
|
+
instance.timer = setTimeout(() => {
|
|
42
|
+
console.warn('Clado 插件启动超时', plugin.id, contribution.id);
|
|
43
|
+
remove(instance);
|
|
44
|
+
}, 10000);
|
|
45
|
+
frame.src = plugin.entry + '#' + new URLSearchParams({ ticket, mount: contribution.id, theme: theme() });
|
|
46
|
+
surface.append(frame);
|
|
47
|
+
instance.unmount = host.mount(contribution.slot, surface, plugin.id);
|
|
48
|
+
}
|
|
49
|
+
function context(instance, params) {
|
|
50
|
+
const { to, scope, input } = params;
|
|
51
|
+
if (!isObject(scope) || scope.workspaceId !== catalog.workspaceId || scope.viewId !== viewId) {
|
|
52
|
+
return error(-32001, 'FORBIDDEN', '工作区或视图不属于当前插件实例。');
|
|
53
|
+
}
|
|
54
|
+
if (!isObject(to) || to.capability !== 'canvas.context.get' ||
|
|
55
|
+
(to.pluginId !== undefined && to.pluginId !== 'clado.host') || to.instanceId !== undefined) {
|
|
56
|
+
return error(-32002, 'NO_PROVIDER', '能力未注册。');
|
|
57
|
+
}
|
|
58
|
+
if (to.major !== 1) return error(-32004, 'UNSUPPORTED_VERSION', '仅支持能力版本 1。');
|
|
59
|
+
if (!isObject(input) || !Array.isArray(input.fields) || !input.fields.length ||
|
|
60
|
+
!input.fields.every(field => catalog.fields.includes(field)) ||
|
|
61
|
+
new Set(input.fields).size !== input.fields.length) return invalidParams();
|
|
62
|
+
const { fields, offset = 0, limit = 100, revision } = input;
|
|
63
|
+
if (!fields.every(field => instance.plugin.permissions.includes('canvas.' + field + '.read'))) {
|
|
64
|
+
return error(-32001, 'FORBIDDEN', '插件未声明所请求字段的读取权限。');
|
|
65
|
+
}
|
|
66
|
+
if (!maxPart(offset) || !maxPart(limit) || limit < 1 || limit > 200 ||
|
|
67
|
+
(offset > 0 && (!fields.includes('graph') || typeof revision !== 'string')) ||
|
|
68
|
+
(revision !== undefined && (!fields.includes('graph') || typeof revision !== 'string'))) return invalidParams();
|
|
69
|
+
const view = readView();
|
|
70
|
+
// File identity matters when distinct documents have identical source bytes.
|
|
71
|
+
const graphRevision = view ? JSON.stringify([view.file, view.revision]) : null;
|
|
72
|
+
if (revision !== undefined && revision !== graphRevision) {
|
|
73
|
+
return error(-32005, 'STALE_CONTEXT', '文档已变化,请从第一页重新读取。', { revision: graphRevision });
|
|
74
|
+
}
|
|
75
|
+
const output = view?.runtime.readPluginContext(fields, view.selection, { offset, limit }) ||
|
|
76
|
+
Object.fromEntries(fields.map(field => [field, field === 'selection' ? [] : null]));
|
|
77
|
+
if (output.document) Object.assign(output.document, { file: view.file, revision: view.revision });
|
|
78
|
+
if (output.graph) output.graph.revision = graphRevision;
|
|
79
|
+
return { result: { provider: { pluginId: 'clado.host' }, version: '1.0.0', output } };
|
|
80
|
+
}
|
|
81
|
+
function error(code, kind, message, data) {
|
|
82
|
+
return { error: { code, message, data: { kind, ...data } } };
|
|
83
|
+
}
|
|
84
|
+
const invalidParams = () => error(-32602, 'INVALID_PARAMS', '参数无效。');
|
|
85
|
+
function invokePlugin(instance, call) {
|
|
86
|
+
return new Promise((resolve, reject) => {
|
|
87
|
+
const timeout = setTimeout(() => {
|
|
88
|
+
instance.pending.delete(call.id);
|
|
89
|
+
reject(new Error('插件挂载未在时限内响应。'));
|
|
90
|
+
}, 7000);
|
|
91
|
+
instance.pending.set(call.id, { resolve, reject, timeout });
|
|
92
|
+
instance.port.postMessage(JSON.stringify({ jsonrpc: '2.0', method: 'clado.invoke', id: call.id, params: call }));
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
function handleReply(instance, response) {
|
|
96
|
+
if (response?.jsonrpc !== '2.0' || response.method !== undefined) return false;
|
|
97
|
+
const pending = instance.pending.get(response.id);
|
|
98
|
+
if (!pending) return false;
|
|
99
|
+
instance.pending.delete(response.id);
|
|
100
|
+
clearTimeout(pending.timeout);
|
|
101
|
+
if (response.error) pending.reject(new Error(String(response.error.message || '插件执行失败。').slice(0, 500)));
|
|
102
|
+
else pending.resolve(response.result);
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
async function dispatch(instance, method, params) {
|
|
106
|
+
if (!isObject(params)) return invalidParams();
|
|
107
|
+
if (method === 'clado.hello' && instance.state === 'connecting') {
|
|
108
|
+
if (params.ticket !== instance.ticket || params.entrypoint !== 'ui') return error(-32001, 'FORBIDDEN', '握手凭据无效。');
|
|
109
|
+
if (!Array.isArray(params.protocols) || !params.protocols.includes(catalog.protocol)) {
|
|
110
|
+
return error(-32004, 'UNSUPPORTED_VERSION', '无法协商插件协议。');
|
|
111
|
+
}
|
|
112
|
+
instance.state = 'identified';
|
|
113
|
+
return { result: {
|
|
114
|
+
protocol: catalog.protocol, instanceId: instance.instanceId, grants: instance.plugin.permissions,
|
|
115
|
+
settings: instance.plugin.settings,
|
|
116
|
+
scope: { workspaceId: catalog.workspaceId, viewId }, limits: { maxMessageBytes },
|
|
117
|
+
} };
|
|
118
|
+
}
|
|
119
|
+
if (method === 'clado.register' && instance.state === 'identified') {
|
|
120
|
+
if (params.entrypoint !== 'ui' || !sameList(params.mounts, [instance.contribution.id]) ||
|
|
121
|
+
!sameList(params.capabilities, []) || !sameList(params.events, [])) return invalidParams();
|
|
122
|
+
instance.state = 'registered';
|
|
123
|
+
clearTimeout(instance.timer);
|
|
124
|
+
registered.add(instance);
|
|
125
|
+
syncTheme(instance);
|
|
126
|
+
instance.surface.hidden = false;
|
|
127
|
+
return { result: { mounts: params.mounts, capabilities: [], events: [] } };
|
|
128
|
+
}
|
|
129
|
+
if (instance.state === 'registered') {
|
|
130
|
+
if (method === 'clado.call' && params.to?.capability === 'canvas.context.get' &&
|
|
131
|
+
(params.to.pluginId === undefined || params.to.pluginId === 'clado.host')) return context(instance, params);
|
|
132
|
+
if (['clado.capabilities.register', 'clado.capabilities.list', 'clado.call'].includes(method)) {
|
|
133
|
+
if (params.scope?.workspaceId !== catalog.workspaceId || params.scope.viewId !== viewId) {
|
|
134
|
+
return error(-32001, 'FORBIDDEN', '工作区或视图不属于当前插件实例。');
|
|
135
|
+
}
|
|
136
|
+
const identity = { pluginId: instance.plugin.id, mountId: instance.contribution.id };
|
|
137
|
+
if (method === 'clado.capabilities.register') {
|
|
138
|
+
const expected = instance.plugin.capabilities
|
|
139
|
+
.filter(provider => provider.mountId === identity.mountId)
|
|
140
|
+
.map(provider => provider.capability + '@' + provider.major);
|
|
141
|
+
if (instance.provided || !expected.length || !sameList(params.capabilities, expected)) return invalidParams();
|
|
142
|
+
instance.provided = true;
|
|
143
|
+
try { return { result: await capabilities.provide(identity, call => invokePlugin(instance, call)) || {} }; }
|
|
144
|
+
catch (cause) { instance.provided = false; throw cause; }
|
|
145
|
+
}
|
|
146
|
+
if (method === 'clado.capabilities.list') return { result: await capabilities.list(identity, params.query) };
|
|
147
|
+
return { result: await capabilities.call(identity, params.to, params.input) };
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return error(-32601, 'METHOD_NOT_FOUND', '方法不可用。');
|
|
151
|
+
}
|
|
152
|
+
function accept(event) {
|
|
153
|
+
const { type, ticket } = event.data || {};
|
|
154
|
+
if (type !== 'clado-plugin:connect' || event.origin !== 'null') return;
|
|
155
|
+
const instance = tickets.get(ticket);
|
|
156
|
+
if (!instance || event.source !== instance.frame.contentWindow || event.ports.length) return;
|
|
157
|
+
tickets.delete(ticket);
|
|
158
|
+
const channel = new MessageChannel();
|
|
159
|
+
instance.port = channel.port1;
|
|
160
|
+
channel.port1.onmessage = async ({ data }) => {
|
|
161
|
+
if (typeof data !== 'string' || data.length > maxMessageBytes || byteLength(data) > maxMessageBytes) return;
|
|
162
|
+
let request, response;
|
|
163
|
+
try { request = JSON.parse(data); }
|
|
164
|
+
catch { response = error(-32700, 'PARSE_ERROR', '消息不是有效 JSON。'); }
|
|
165
|
+
if (handleReply(instance, request)) return;
|
|
166
|
+
const valid = isObject(request) && request.jsonrpc === '2.0' && typeof request.method === 'string' &&
|
|
167
|
+
typeof request.id === 'string' && request.id.length > 0 && request.id.length <= 128;
|
|
168
|
+
try {
|
|
169
|
+
response ||= valid ? await dispatch(instance, request.method, request.params)
|
|
170
|
+
: error(-32600, 'INVALID_REQUEST', '需要 JSON-RPC 请求和字符串 ID。');
|
|
171
|
+
} catch (cause) {
|
|
172
|
+
response = error(cause.data?.kind === 'FORBIDDEN' || cause.data?.code === 'FORBIDDEN' ? -32001 : -32000,
|
|
173
|
+
cause.data?.code || 'INTERNAL_ERROR', cause.message || '宿主暂时无法处理请求。',
|
|
174
|
+
cause.data?.issues && { issues: cause.data.issues });
|
|
175
|
+
}
|
|
176
|
+
const id = valid ? request.id : null;
|
|
177
|
+
let reply = JSON.stringify({ jsonrpc: '2.0', id, ...response });
|
|
178
|
+
const provider = response.result?.provider;
|
|
179
|
+
const replyLimit = provider?.pluginId === 'clado.host' && provider.capability === 'canvas.capture'
|
|
180
|
+
? Math.ceil(4 * 1024 * 1024 / 3) * 4 + 65536 : maxMessageBytes;
|
|
181
|
+
if (byteLength(reply) > replyLimit) {
|
|
182
|
+
reply = JSON.stringify({ jsonrpc: '2.0', id, ...error(-32009, 'MESSAGE_TOO_LARGE', '响应过大,请减少字段或分页大小。') });
|
|
183
|
+
}
|
|
184
|
+
channel.port1.postMessage(reply);
|
|
185
|
+
};
|
|
186
|
+
event.source.postMessage({ type: 'clado-plugin:port', ticket }, '*', [channel.port2]);
|
|
187
|
+
}
|
|
188
|
+
addEventListener('message', accept);
|
|
189
|
+
fetch('/library/plugins.json', { cache: 'no-store' }).then(response => {
|
|
190
|
+
if (!response.ok) throw new Error('插件清单读取失败。');
|
|
191
|
+
return response.json();
|
|
192
|
+
}).then(data => {
|
|
193
|
+
catalog = data;
|
|
194
|
+
for (const plugin of catalog.plugins) {
|
|
195
|
+
if (isEnabled(plugin.id)) for (const mountPoint of plugin.mounts) mount(plugin, mountPoint);
|
|
196
|
+
}
|
|
197
|
+
}).catch(error => console.error('Clado 插件加载失败', error));
|
|
198
|
+
|
|
199
|
+
};
|
|
200
|
+
})();
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
:root{color-scheme:light;--panel:#fff;--bg:#f6f8fb;--ink:#263140;--muted:#8290a0;--line:#e2e7ef;--accent:#0969da;--soft:#edf4ff;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC","Microsoft YaHei",sans-serif;font-size:12px}
|
|
2
|
-
*{box-sizing:border-box}body{margin:0;height:100vh;overflow:hidden;color:var(--ink);background:var(--panel)}
|
|
3
|
-
.viewer,.viewer main{position:absolute;inset:0}iframe{height:100%;width:100%;display:block;border:0}
|
|
4
|
-
#directory{position:fixed;left:18px;bottom:68px;width:288px;max-height:calc(100vh - 94px);overflow:auto;padding:17px 10px 10px;border:1px solid var(--line);border-radius:12px;background:var(--panel);z-index:25;box-shadow:0 12px 40px #15243b24}.directory-heading{padding:0 9px 13px}.directory-heading strong{font-size:12px;font-weight:600}.directory-heading span{display:block;font-size:10px;color:var(--muted);margin-top:5px}#documentList{display:flex;flex-direction:column;gap:4px}.document-button{display:block;text-align:left;padding:9px 10px;white-space:normal;border-color:transparent;width:100%}.document-button strong{font-size:11px;font-weight:500;display:block;color:var(--ink)}.document-button small{font-size:9px;color:var(--muted);display:block;margin-top:4px;overflow-wrap:anywhere}.document-button[aria-current="page"]{background:var(--soft);border-color:var(--soft)}.document-button[aria-current="page"] strong{color:var(--accent)}.directory-current{padding:12px 10px 8px;margin-top:10px;border-top:1px solid var(--line)}.directory-current strong{font-size:10px;font-weight:500;display:block;color:var(--muted)}.directory-current span{display:block;color:var(--muted);font-size:9px;margin-top:4px;overflow-wrap:anywhere}.directory-actions{display:flex;gap:7px;padding:0 10px}.directory-actions button{font-size:10px;padding:4px 7px}.directory-actions #sourceButton{margin-left:auto}.skipped{margin-top:10px;border:0;border-top:1px solid var(--line);border-radius:0;width:100%;text-align:left;padding:11px 10px 2px;font-size:9px;color:var(--muted);white-space:normal}
|
|
2
|
+
*{box-sizing:border-box}body{margin:0;height:100vh;overflow:hidden;color:var(--ink);background:var(--panel)}
|
|
3
|
+
.viewer,.viewer main{position:absolute;inset:0}iframe{height:100%;width:100%;display:block;border:0}
|
|
5
4
|
.canvas-loading{position:absolute;inset:0;background:var(--panel);color:var(--muted);display:grid;place-items:center;font-size:12px;z-index:2}.canvas-loading[hidden],#directory[hidden],.update-error[hidden],.empty-state[hidden],.skipped[hidden]{display:none}.update-error{position:absolute;top:60px;left:16px;right:16px;z-index:5;background:#fff6dd;color:#896213;border:1px solid #ecd9a6;border-radius:7px;padding:10px 13px;font-size:11px;box-shadow:0 3px 12px #0001}.empty-state{position:absolute;inset:0;background:var(--panel);padding:70px 35px;text-align:center}.empty-state h1{font-size:17px;font-weight:550}.empty-state p{font-size:12px;line-height:1.9;color:var(--muted);max-width:360px;margin:15px auto}
|
|
6
|
-
|
|
7
|
-
[data-theme="dark"]{color-scheme:dark;--panel:#1c222b;--bg:#151a22;--ink:#dfe7ef;--muted:#9ba8b8;--line:#343e4c;--soft:#203753}@media(max-width:450px){.directory-toggle{left:12px;bottom:12px;padding:8px 12px}#directory{left:12px;bottom:58px;width:min(288px,calc(100vw - 24px));max-height:calc(100vh - 80px)}}
|
|
5
|
+
[data-theme="dark"]{color-scheme:dark;--panel:#1c222b;--bg:#151a22;--ink:#dfe7ef;--muted:#9ba8b8;--line:#343e4c;--soft:#203753}
|
|
8
6
|
[hidden]{display:none!important}
|
|
9
|
-
.directory-search{display:block;padding:0 9px 10px}.directory-search input{box-sizing:border-box;width:100%;border:1px solid var(--line);border-radius:7px;background:var(--bg);color:var(--ink);font:inherit;padding:8px 10px;outline-color:var(--accent)}.tree-folder>summary{cursor:pointer;padding:8px;color:var(--ink);font-size:12px;font-weight:550}.tree-children{margin-left:11px;padding-left:5px;border-left:1px solid var(--line)}.tree-empty{padding:8px;color:var(--muted);font-size:12px}
|
|
@@ -1,20 +1,10 @@
|
|
|
1
1
|
<!doctype html>
|
|
2
2
|
<html lang="zh-CN" data-theme="light">
|
|
3
|
-
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Clado · Markdown 目录预览</title><link rel="stylesheet" href="library.css"
|
|
3
|
+
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Clado · Markdown 目录预览</title><link rel="stylesheet" href="library.css"><!--CLADO-PLUGIN-STYLES--></head>
|
|
4
4
|
<body>
|
|
5
|
-
<button id="directoryButton" class="directory-toggle" aria-expanded="false" aria-controls="directory" title="切换 Markdown 画布">文档 <span id="fileCount">…</span></button>
|
|
6
|
-
<aside id="directory" aria-label="Markdown 画布目录" hidden>
|
|
7
|
-
<div class="directory-heading"><strong>Markdown/</strong><span>选择文档,打开对应画布</span></div>
|
|
8
|
-
<label class="directory-search"><input id="documentSearch" type="search" aria-label="搜索文档" placeholder="搜索名称或路径…"></label>
|
|
9
|
-
<nav id="documentList" aria-label="可用 Markdown"></nav>
|
|
10
|
-
<div class="directory-current"><strong id="currentTitle" hidden>正在扫描 Markdown…</strong><span id="currentPath" hidden>Markdown/</span><span id="syncStatus" role="status">连接本地目录</span></div>
|
|
11
|
-
<div class="directory-actions"><button id="backButton" disabled>← 返回</button><button id="refreshButton" title="重新扫描目录并刷新当前文档">刷新</button><button id="sourceButton">源文</button></div>
|
|
12
|
-
<button id="skippedButton" class="skipped" hidden></button>
|
|
13
|
-
</aside>
|
|
14
5
|
<div class="viewer">
|
|
15
6
|
<main><div id="updateError" class="update-error" role="alert" hidden></div><iframe id="canvas" title="当前 Markdown 的脑图、卡片与文档"></iframe><div id="canvasLoading" class="canvas-loading">正在读取 Markdown…</div><div id="emptyState" class="empty-state" hidden><h1>目录中还没有可展示的画布</h1><p>让 AI 连接当前服务的 MCP,读取模板并创建 .mdc 文档,保存后会自动出现在这里。</p></div></main>
|
|
16
7
|
</div>
|
|
17
|
-
|
|
18
|
-
<script src="/tree.js"></script>
|
|
8
|
+
<!--CLADO-PLUGIN-SCRIPTS-->
|
|
19
9
|
<script src="library.js"></script>
|
|
20
10
|
</body></html>
|