cladogram 0.5.5-beta.6 → 0.5.5-beta.8
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/bin/clado.js +33 -10
- package/docs/release.md +13 -0
- package/package.json +2 -2
- package/plugins/agent/index.js +2378 -461
- package/plugins/agent/style.css +1267 -422
- package/plugins/brand/index.js +211 -2
- package/plugins/directory/index.js +18 -1
- package/src/agent/context.js +430 -0
- package/src/agent/probe.js +58 -0
- package/src/agent/providers/index.js +197 -14
- package/src/agent/runtime.js +418 -28
- package/src/agent/sessions.js +239 -32
- package/src/html/canvas-ui.css +75 -11
- package/src/html/plugin-ui.css +378 -1
- package/src/server/agent.js +1049 -64
- package/src/server/assets/library-canvas.js +305 -1
- package/src/server/assets/library.js +564 -7
- package/src/server/plugin-registry.js +98 -0
- package/src/server/plugin-settings.js +29 -3
- package/src/server/workspace-hub.js +223 -0
- package/src/server/workspace.js +213 -18
- package/src/workspace/objects.js +125 -0
|
@@ -69,6 +69,31 @@
|
|
|
69
69
|
reloadWithMode(true);
|
|
70
70
|
},
|
|
71
71
|
},
|
|
72
|
+
market: config.canInstallPlugins ? {
|
|
73
|
+
load: async () => {
|
|
74
|
+
const response = await fetch(withWorkspace('/library/plugin-registry'), { cache: 'no-store' });
|
|
75
|
+
const result = await response.json();
|
|
76
|
+
if (!response.ok) throw new Error(result.error || '加载插件市场失败');
|
|
77
|
+
return result;
|
|
78
|
+
},
|
|
79
|
+
install: async id => {
|
|
80
|
+
const response = await fetch(withWorkspace('/library/plugin-registry'), {
|
|
81
|
+
method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ id }),
|
|
82
|
+
});
|
|
83
|
+
const result = await response.json();
|
|
84
|
+
if (!response.ok) throw new Error(result.error || '安装失败');
|
|
85
|
+
return result;
|
|
86
|
+
},
|
|
87
|
+
uninstall: async id => {
|
|
88
|
+
const response = await fetch(withWorkspace('/library/install-plugin?id=' + encodeURIComponent(id)), {
|
|
89
|
+
method: 'DELETE',
|
|
90
|
+
});
|
|
91
|
+
const result = await response.json();
|
|
92
|
+
if (!response.ok) throw new Error(result.error || '卸载失败');
|
|
93
|
+
return result;
|
|
94
|
+
},
|
|
95
|
+
} : null,
|
|
96
|
+
openWorkspacePicker: config.canSwitchWorkspace && !offline ? () => openWorkspacePicker() : null,
|
|
72
97
|
setTheme: theme => {
|
|
73
98
|
if (modalLocked()) return;
|
|
74
99
|
try { localStorage.setItem('clado-theme', theme); } catch {}
|
|
@@ -97,6 +122,8 @@
|
|
|
97
122
|
openFromCapability,
|
|
98
123
|
workspaceUrl,
|
|
99
124
|
switchWorkspace: id => { location.href = workspaceUrl(id); },
|
|
125
|
+
openWorkspacePicker: config.canSwitchWorkspace && !offline ? () => openWorkspacePicker() : null,
|
|
126
|
+
createCanvas: config.canSwitchWorkspace && !offline ? () => createCanvasFlow() : null,
|
|
100
127
|
refresh: () => scan(true),
|
|
101
128
|
back: () => {
|
|
102
129
|
if (modalLocked()) return;
|
|
@@ -119,6 +146,507 @@
|
|
|
119
146
|
}
|
|
120
147
|
const selectionTab = config.selection ? crypto.randomUUID() : null;
|
|
121
148
|
let aiSelection = null, selectionRequest = Promise.resolve(), pageHidden = false;
|
|
149
|
+
|
|
150
|
+
// ============ 本机工作区中心(选择/浏览/新建工作区 + 新项目首屏启动台) ============
|
|
151
|
+
let wsPickerOverlay = null;
|
|
152
|
+
async function createCanvasFlow(defaultTitle = '') {
|
|
153
|
+
if (!config.canSwitchWorkspace || offline) return;
|
|
154
|
+
const title = defaultTitle || window.prompt('输入新画布标题:', '系统架构概览');
|
|
155
|
+
if (!title || !title.trim()) return;
|
|
156
|
+
try {
|
|
157
|
+
const res = await fetch(withWorkspace('/library/fs-workspaces'), {
|
|
158
|
+
method: 'POST',
|
|
159
|
+
headers: { 'content-type': 'application/json' },
|
|
160
|
+
body: JSON.stringify({ action: 'create_canvas', title: title.trim() }),
|
|
161
|
+
});
|
|
162
|
+
const data = await res.json();
|
|
163
|
+
if (!res.ok) throw new Error(data.error || '创建画布失败');
|
|
164
|
+
pendingOpen = data.file;
|
|
165
|
+
await scan(true);
|
|
166
|
+
} catch (err) {
|
|
167
|
+
directory.showMessage('新建画布失败', err.message || '请稍后重试');
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async function applyWorkspaceSwitch(targetPath, { createIfMissing = false, initStarterCanvas = false } = {}) {
|
|
172
|
+
if (!targetPath || !targetPath.trim()) return;
|
|
173
|
+
const res = await fetch('/library/fs-workspaces', {
|
|
174
|
+
method: 'POST',
|
|
175
|
+
headers: { 'content-type': 'application/json' },
|
|
176
|
+
body: JSON.stringify({
|
|
177
|
+
action: 'select',
|
|
178
|
+
path: targetPath.trim(),
|
|
179
|
+
createIfMissing,
|
|
180
|
+
initStarterCanvas,
|
|
181
|
+
}),
|
|
182
|
+
});
|
|
183
|
+
const data = await res.json();
|
|
184
|
+
if (!res.ok) throw new Error(data.error || '切换工作区失败');
|
|
185
|
+
try { sessionStorage.removeItem(storageKey); } catch {}
|
|
186
|
+
location.href = '/';
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function ensureWorkspacePicker() {
|
|
190
|
+
if (wsPickerOverlay || !document.body) return wsPickerOverlay;
|
|
191
|
+
wsPickerOverlay = document.createElement('div');
|
|
192
|
+
wsPickerOverlay.id = 'clado-workspace-picker';
|
|
193
|
+
wsPickerOverlay.className = 'clado-market-overlay';
|
|
194
|
+
wsPickerOverlay.innerHTML = `
|
|
195
|
+
<div class="clado-market-sheet" style="max-width:500px;">
|
|
196
|
+
<div class="clado-market-well" style="padding:12px 14px;display:flex;align-items:center;justify-content:space-between;gap:8px;">
|
|
197
|
+
<div>
|
|
198
|
+
<strong style="font-size:15px;">选择或新建工作区</strong>
|
|
199
|
+
<div data-ws-cur style="font-size:11px;color:var(--clado-ui-muted);margin-top:2px;"></div>
|
|
200
|
+
</div>
|
|
201
|
+
<button type="button" class="clado-market-tab active" data-ws-close style="padding:5px 11px;">关闭</button>
|
|
202
|
+
</div>
|
|
203
|
+
|
|
204
|
+
<div class="clado-market-well" data-ws-recents-wrap style="padding:11px 12px;display:none;flex-direction:column;gap:5px;">
|
|
205
|
+
<div style="font-size:11px;font-weight:650;color:var(--clado-ui-muted);">最近打开的项目</div>
|
|
206
|
+
<div data-ws-recents style="display:flex;flex-direction:column;gap:3px;"></div>
|
|
207
|
+
</div>
|
|
208
|
+
|
|
209
|
+
<div class="clado-market-well" style="padding:11px 12px;display:flex;flex-direction:column;gap:8px;">
|
|
210
|
+
<div style="display:flex;align-items:center;justify-content:space-between;gap:6px;">
|
|
211
|
+
<span style="font-size:11.5px;font-weight:650;color:var(--clado-ui-muted);">浏览本机目录</span>
|
|
212
|
+
<div style="display:flex;gap:4px;">
|
|
213
|
+
<button type="button" class="clado-market-tab active" data-ws-up style="font-size:11px;padding:3px 8px;">↑ 上级</button>
|
|
214
|
+
<button type="button" class="clado-market-tab active" data-ws-home style="font-size:11px;padding:3px 8px;">⌂ 主目录</button>
|
|
215
|
+
</div>
|
|
216
|
+
</div>
|
|
217
|
+
<div data-ws-browse-list style="max-height:190px;overflow-y:auto;display:flex;flex-direction:column;gap:2px;"></div>
|
|
218
|
+
</div>
|
|
219
|
+
|
|
220
|
+
<div class="clado-market-well" style="padding:12px;display:flex;flex-direction:column;gap:8px;">
|
|
221
|
+
<div style="font-size:11.5px;color:var(--clado-ui-muted);">目标工作区路径(回车浏览目录,或直接输入新目录路径自动创建)</div>
|
|
222
|
+
<div style="display:flex;gap:6px;">
|
|
223
|
+
<input type="text" data-ws-input style="flex:1;height:35px;padding:0 11px;border-radius:10px;border:1px solid var(--clado-ui-well-border);background:var(--clado-ui-well);color:var(--clado-ui-text);font:inherit;font-size:12.5px;outline:none;" placeholder="/path/to/your-project" />
|
|
224
|
+
<button type="button" class="clado-market-tab active" data-ws-go style="height:35px;padding:0 11px;font-size:12px;">浏览</button>
|
|
225
|
+
</div>
|
|
226
|
+
<div data-ws-err style="font-size:11.5px;color:#e11d48;display:none;"></div>
|
|
227
|
+
<div style="display:flex;gap:8px;flex-wrap:wrap;">
|
|
228
|
+
<button type="button" class="clado-market-tab active" data-ws-open style="flex:1;height:35px;font-size:12.5px;">打开此目录</button>
|
|
229
|
+
<button type="button" data-ws-init style="flex:1.2;height:35px;border:0;border-radius:10px;background:var(--clado-ui-brand);color:#fff;font:inherit;font-size:12.5px;font-weight:600;cursor:pointer;">在此新建工作区并建首张画布</button>
|
|
230
|
+
</div>
|
|
231
|
+
</div>
|
|
232
|
+
</div>`;
|
|
233
|
+
document.body.append(wsPickerOverlay);
|
|
234
|
+
|
|
235
|
+
let parentDir = '';
|
|
236
|
+
let homeDir = '';
|
|
237
|
+
const curEl = wsPickerOverlay.querySelector('[data-ws-cur]');
|
|
238
|
+
const recentsWrap = wsPickerOverlay.querySelector('[data-ws-recents-wrap]');
|
|
239
|
+
const recentsList = wsPickerOverlay.querySelector('[data-ws-recents]');
|
|
240
|
+
const browseList = wsPickerOverlay.querySelector('[data-ws-browse-list]');
|
|
241
|
+
const pathInput = wsPickerOverlay.querySelector('[data-ws-input]');
|
|
242
|
+
const errEl = wsPickerOverlay.querySelector('[data-ws-err]');
|
|
243
|
+
|
|
244
|
+
const showErr = msg => {
|
|
245
|
+
errEl.textContent = msg || '';
|
|
246
|
+
errEl.style.display = msg ? 'block' : 'none';
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const loadDir = async (dir = '') => {
|
|
250
|
+
showErr('');
|
|
251
|
+
try {
|
|
252
|
+
const qs = dir ? '?dir=' + encodeURIComponent(dir) : '';
|
|
253
|
+
const res = await fetch('/library/fs-workspaces' + qs, { cache: 'no-store' });
|
|
254
|
+
const data = await res.json();
|
|
255
|
+
if (!res.ok) throw new Error(data.error || '读取目录失败');
|
|
256
|
+
parentDir = data.parentDir || '';
|
|
257
|
+
homeDir = data.homeDir || '';
|
|
258
|
+
curEl.textContent = data.hubMode ? '请选择一个项目目录开始协作' : `当前:${data.currentWorkspace}`;
|
|
259
|
+
pathInput.value = data.currentDir || data.currentWorkspace || '';
|
|
260
|
+
|
|
261
|
+
const recents = Array.isArray(data.recentWorkspaces) ? data.recentWorkspaces : [];
|
|
262
|
+
if (recents.length) {
|
|
263
|
+
recentsWrap.style.display = 'flex';
|
|
264
|
+
recentsList.replaceChildren();
|
|
265
|
+
for (const r of recents.slice(0, 5)) {
|
|
266
|
+
const row = document.createElement('button');
|
|
267
|
+
row.type = 'button';
|
|
268
|
+
row.className = 'clado-folder-row';
|
|
269
|
+
row.innerHTML = `<span style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"><strong>${r.name}</strong> <small style="color:var(--clado-ui-muted);">${r.path}</small></span><span style="font-size:11px;color:var(--clado-ui-brand);flex:none;">${r.documents ?? 0} 画布</span>`;
|
|
270
|
+
row.onclick = () => applyWorkspaceSwitch(r.path, { createIfMissing: false }).catch(e => showErr(e.message));
|
|
271
|
+
recentsList.append(row);
|
|
272
|
+
}
|
|
273
|
+
} else {
|
|
274
|
+
recentsWrap.style.display = 'none';
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
browseList.replaceChildren();
|
|
278
|
+
const entries = Array.isArray(data.entries) ? data.entries : [];
|
|
279
|
+
if (!entries.length) {
|
|
280
|
+
const empty = document.createElement('div');
|
|
281
|
+
empty.style.cssText = 'padding:12px;text-align:center;color:var(--clado-ui-muted);font-size:12px;';
|
|
282
|
+
empty.textContent = '当前文件夹下暂无子目录,可直接打开此目录或下方输入新子目录名新建。';
|
|
283
|
+
browseList.append(empty);
|
|
284
|
+
} else {
|
|
285
|
+
for (const item of entries) {
|
|
286
|
+
const row = document.createElement('button');
|
|
287
|
+
row.type = 'button';
|
|
288
|
+
row.className = 'clado-folder-row';
|
|
289
|
+
row.dataset.folderPath = item.path;
|
|
290
|
+
const badges = [
|
|
291
|
+
item.hasClado ? '<span style="color:#059669;font-weight:600;">Clado</span>' : '',
|
|
292
|
+
item.isGit ? '<span style="color:var(--clado-ui-muted);">Git</span>' : '',
|
|
293
|
+
item.mdCount ? `<span>${item.mdCount} md</span>` : '',
|
|
294
|
+
].filter(Boolean).join(' · ');
|
|
295
|
+
row.innerHTML = `<span style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">📁 ${item.name}</span><small style="color:var(--clado-ui-subtle);flex:none;">${badges || '进入 ›'}</small>`;
|
|
296
|
+
row.onclick = () => loadDir(item.path);
|
|
297
|
+
browseList.append(row);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
} catch (err) {
|
|
301
|
+
showErr(err.message || '读取目录失败');
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
wsPickerOverlay.addEventListener('click', ev => {
|
|
306
|
+
if (ev.target === wsPickerOverlay || ev.target.closest('[data-ws-close]')) {
|
|
307
|
+
wsPickerOverlay.classList.remove('show');
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
pathInput.addEventListener('keydown', ev => {
|
|
311
|
+
if (ev.key === 'Enter') {
|
|
312
|
+
ev.preventDefault();
|
|
313
|
+
if (pathInput.value.trim()) loadDir(pathInput.value.trim());
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
wsPickerOverlay.querySelector('[data-ws-go]').onclick = () => {
|
|
317
|
+
if (pathInput.value.trim()) loadDir(pathInput.value.trim());
|
|
318
|
+
};
|
|
319
|
+
wsPickerOverlay.querySelector('[data-ws-up]').onclick = () => { if (parentDir) loadDir(parentDir); };
|
|
320
|
+
wsPickerOverlay.querySelector('[data-ws-home]').onclick = () => { if (homeDir) loadDir(homeDir); };
|
|
321
|
+
wsPickerOverlay.querySelector('[data-ws-open]').onclick = () => {
|
|
322
|
+
applyWorkspaceSwitch(pathInput.value, { createIfMissing: true, initStarterCanvas: false }).catch(e => showErr(e.message));
|
|
323
|
+
};
|
|
324
|
+
wsPickerOverlay.querySelector('[data-ws-init]').onclick = () => {
|
|
325
|
+
applyWorkspaceSwitch(pathInput.value, { createIfMissing: true, initStarterCanvas: true }).catch(e => showErr(e.message));
|
|
326
|
+
};
|
|
327
|
+
wsPickerOverlay._loadDir = loadDir;
|
|
328
|
+
return wsPickerOverlay;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function openWorkspacePicker(initialDir = '') {
|
|
332
|
+
if (!config.canSwitchWorkspace || offline || !document.body) return;
|
|
333
|
+
const modal = ensureWorkspacePicker();
|
|
334
|
+
if (!modal) return;
|
|
335
|
+
modal.classList.add('show');
|
|
336
|
+
modal._loadDir(initialDir);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (config.canSwitchWorkspace && !offline && document.body) {
|
|
340
|
+
window.addEventListener('clado:open-workspace-picker', () => openWorkspacePicker());
|
|
341
|
+
const emptyEl = $('#emptyState');
|
|
342
|
+
if (emptyEl) {
|
|
343
|
+
const wsTitle = config.hubMode ? '欢迎使用 Clado · 请先选择工作区' : `当前工作区:${config.workspaceName || '未命名项目'}`;
|
|
344
|
+
const wsSub = config.hubMode
|
|
345
|
+
? '选择一个已有代码仓库或新建空白工作区,配置好模型供应商即可开始与多角色分身协作设计。'
|
|
346
|
+
: `路径:${config.workspacePath || '.'} · 当前目录尚无画布文档,你可以一键新建首张画布或直接在右下角向 Agent 提问。`;
|
|
347
|
+
emptyEl.style.padding = '0';
|
|
348
|
+
emptyEl.innerHTML = `
|
|
349
|
+
<div class="clado-launchpad-wrap">
|
|
350
|
+
<div class="clado-launchpad-card">
|
|
351
|
+
<div>
|
|
352
|
+
<div style="font-size:16px;font-weight:700;color:var(--clado-ui-text);">${wsTitle}</div>
|
|
353
|
+
<div style="font-size:12px;line-height:1.55;color:var(--clado-ui-muted);margin-top:5px;">${wsSub}</div>
|
|
354
|
+
</div>
|
|
355
|
+
<div class="clado-launchpad-actions">
|
|
356
|
+
<button type="button" class="clado-launchpad-btn primary" data-lp-pick>
|
|
357
|
+
<span><strong>📂 选择或切换本机工作区</strong><br/><small style="color:var(--clado-ui-muted);">浏览电脑目录、打开已有仓库或新建项目</small></span>
|
|
358
|
+
<span style="color:var(--clado-ui-brand);font-weight:700;">›</span>
|
|
359
|
+
</button>
|
|
360
|
+
<button type="button" class="clado-launchpad-btn" data-lp-new-canvas>
|
|
361
|
+
<span><strong>✨ 在当前工作区新建首张画布</strong><br/><small style="color:var(--clado-ui-muted);">立即创建初始架构画布进入三视图协作</small></span>
|
|
362
|
+
<span style="opacity:.5;">+</span>
|
|
363
|
+
</button>
|
|
364
|
+
<button type="button" class="clado-launchpad-btn" data-lp-llm>
|
|
365
|
+
<span><strong>⚙ 配置供应商与主力模型</strong><br/><small style="color:var(--clado-ui-muted);">支持 OpenAI / Anthropic / DeepSeek / OpenRouter / Ollama</small></span>
|
|
366
|
+
<span style="opacity:.5;">›</span>
|
|
367
|
+
</button>
|
|
368
|
+
</div>
|
|
369
|
+
</div>
|
|
370
|
+
</div>`;
|
|
371
|
+
emptyEl.querySelector('[data-lp-pick]').onclick = () => openWorkspacePicker();
|
|
372
|
+
emptyEl.querySelector('[data-lp-new-canvas]').onclick = () => createCanvasFlow('系统架构概览');
|
|
373
|
+
emptyEl.querySelector('[data-lp-llm]').onclick = () => window.dispatchEvent(new CustomEvent('clado:open-llm-settings'));
|
|
374
|
+
}
|
|
375
|
+
if (config.hubMode) {
|
|
376
|
+
setTimeout(() => openWorkspacePicker(), 60);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
const liveObjectsEnabled = Boolean(config.selection && !offline && document.body);
|
|
380
|
+
let canvasObjectsList = [];
|
|
381
|
+
const objectSelection = new Map();
|
|
382
|
+
let selectionBarEl = null;
|
|
383
|
+
let canvasCtxMenuEl = null;
|
|
384
|
+
|
|
385
|
+
function syncFrameObjects() {
|
|
386
|
+
if (!liveObjectsEnabled || !frame?.contentWindow) return;
|
|
387
|
+
try {
|
|
388
|
+
frame.contentWindow.postMessage({ type: 'clado-workspace:objects', objects: canvasObjectsList }, location.origin);
|
|
389
|
+
frame.contentWindow.postMessage({ type: 'clado-workspace:object-selection', keys: [...objectSelection.keys()] }, location.origin);
|
|
390
|
+
} catch {}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function collectSelectionLabels() {
|
|
394
|
+
const cardLabels = (aiSelection?.nodes || []).map(node => Array.isArray(node) ? node[node.length - 1] : String(node));
|
|
395
|
+
const objLabels = [...objectSelection.values()].map(item => item.title || item.id);
|
|
396
|
+
return [...cardLabels, ...objLabels];
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function renderSelectionSummary() {
|
|
400
|
+
if (!selectionBarEl) return;
|
|
401
|
+
const labels = collectSelectionLabels();
|
|
402
|
+
if (!labels.length) {
|
|
403
|
+
selectionBarEl.classList.remove('show');
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
const preview = labels.slice(0, 3).join(' / ') + (labels.length > 3 ? '…' : '');
|
|
407
|
+
selectionBarEl.querySelector('[data-sel-summary]').textContent = `已选 ${labels.length} 项 · ${preview}`;
|
|
408
|
+
selectionBarEl.classList.add('show');
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
async function loadObjects() {
|
|
412
|
+
if (!liveObjectsEnabled) return;
|
|
413
|
+
try {
|
|
414
|
+
const res = await fetch(withWorkspace('/library/objects'), { cache: 'no-store' });
|
|
415
|
+
if (!res.ok) return;
|
|
416
|
+
const data = await res.json();
|
|
417
|
+
if (Array.isArray(data.objects)) {
|
|
418
|
+
canvasObjectsList = data.objects;
|
|
419
|
+
const validIds = new Set(canvasObjectsList.map(o => o.id));
|
|
420
|
+
for (const [k, v] of objectSelection) {
|
|
421
|
+
if (!validIds.has(v.id)) objectSelection.delete(k);
|
|
422
|
+
}
|
|
423
|
+
syncFrameObjects();
|
|
424
|
+
renderSelectionSummary();
|
|
425
|
+
}
|
|
426
|
+
} catch {}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
async function saveObject(payload) {
|
|
430
|
+
if (!liveObjectsEnabled) return null;
|
|
431
|
+
try {
|
|
432
|
+
const res = await fetch(withWorkspace('/library/objects'), {
|
|
433
|
+
method: 'POST',
|
|
434
|
+
headers: { 'content-type': 'application/json' },
|
|
435
|
+
body: JSON.stringify(payload),
|
|
436
|
+
});
|
|
437
|
+
if (!res.ok) return null;
|
|
438
|
+
const data = await res.json();
|
|
439
|
+
if (data.object) {
|
|
440
|
+
const idx = canvasObjectsList.findIndex(o => o.id === data.object.id);
|
|
441
|
+
if (idx >= 0) canvasObjectsList[idx] = data.object;
|
|
442
|
+
else canvasObjectsList.push(data.object);
|
|
443
|
+
syncFrameObjects();
|
|
444
|
+
renderSelectionSummary();
|
|
445
|
+
}
|
|
446
|
+
return data.object;
|
|
447
|
+
} catch { return null; }
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
async function deleteObject(id) {
|
|
451
|
+
if (!liveObjectsEnabled || !id) return;
|
|
452
|
+
try {
|
|
453
|
+
const res = await fetch(withWorkspace('/library/objects'), {
|
|
454
|
+
method: 'POST',
|
|
455
|
+
headers: { 'content-type': 'application/json' },
|
|
456
|
+
body: JSON.stringify({ action: 'delete', id }),
|
|
457
|
+
});
|
|
458
|
+
if (!res.ok) return;
|
|
459
|
+
canvasObjectsList = canvasObjectsList.filter(o => o.id !== id);
|
|
460
|
+
for (const [k, v] of objectSelection) {
|
|
461
|
+
if (v.id === id) objectSelection.delete(k);
|
|
462
|
+
}
|
|
463
|
+
syncFrameObjects();
|
|
464
|
+
renderSelectionSummary();
|
|
465
|
+
} catch {}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function closeCanvasCtxMenu() {
|
|
469
|
+
canvasCtxMenuEl?.classList.remove('show');
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function openCanvasCtxMenu(clientX, clientY, detail) {
|
|
473
|
+
if (!canvasCtxMenuEl || !detail) return;
|
|
474
|
+
canvasCtxMenuEl.innerHTML = '';
|
|
475
|
+
const addLabel = text => {
|
|
476
|
+
const lbl = document.createElement('div');
|
|
477
|
+
lbl.className = 'clado-canvas-ctx-head';
|
|
478
|
+
lbl.textContent = text;
|
|
479
|
+
canvasCtxMenuEl.append(lbl);
|
|
480
|
+
};
|
|
481
|
+
const addSep = () => {
|
|
482
|
+
const hr = document.createElement('div');
|
|
483
|
+
hr.className = 'clado-canvas-ctx-sep';
|
|
484
|
+
canvasCtxMenuEl.append(hr);
|
|
485
|
+
};
|
|
486
|
+
const addItem = (label, onClick, danger = false) => {
|
|
487
|
+
const btn = document.createElement('button');
|
|
488
|
+
btn.type = 'button';
|
|
489
|
+
btn.className = 'clado-canvas-ctx-item' + (danger ? ' danger' : '');
|
|
490
|
+
btn.textContent = label;
|
|
491
|
+
btn.onclick = () => {
|
|
492
|
+
closeCanvasCtxMenu();
|
|
493
|
+
onClick();
|
|
494
|
+
};
|
|
495
|
+
canvasCtxMenuEl.append(btn);
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
if (detail.kind === 'subtarget' && detail.object) {
|
|
499
|
+
const obj = canvasObjectsList.find(o => o.id === detail.object.id) || detail.object;
|
|
500
|
+
const shortTitle = String(detail.trackTitle || '').split(' · ')[0] || detail.trackId;
|
|
501
|
+
const selKey = `sub:${obj.id}:${detail.trackId}`;
|
|
502
|
+
const isSelected = objectSelection.has(selKey);
|
|
503
|
+
const isCustom = obj.data?.selRenderMode === 'custom';
|
|
504
|
+
addLabel(`子目标 · ${shortTitle}`);
|
|
505
|
+
addItem(detail.liked ? '♥ 移除喜欢' : '♡ 加入喜欢', () => {
|
|
506
|
+
const tracks = (obj.data?.tracks || []).map(t => t.id === detail.trackId ? { ...t, liked: !t.liked } : t);
|
|
507
|
+
saveObject({ ...obj, data: { ...(obj.data || {}), tracks } });
|
|
508
|
+
});
|
|
509
|
+
addItem(isSelected ? '取消选中这首歌' : '选中这首歌', () => {
|
|
510
|
+
if (isSelected) objectSelection.delete(selKey);
|
|
511
|
+
else objectSelection.set(selKey, { id: obj.id, kind: obj.kind, title: shortTitle, pluginId: obj.pluginId, subtarget: detail.trackId });
|
|
512
|
+
syncFrameObjects();
|
|
513
|
+
renderSelectionSummary();
|
|
514
|
+
});
|
|
515
|
+
addSep();
|
|
516
|
+
addItem(isCustom ? '切换为默认高亮选态' : '切换为自绘音波选态', () => {
|
|
517
|
+
saveObject({ ...obj, data: { ...(obj.data || {}), selRenderMode: isCustom ? 'default' : 'custom' } });
|
|
518
|
+
});
|
|
519
|
+
} else if (detail.kind === 'object' && detail.object) {
|
|
520
|
+
const obj = canvasObjectsList.find(o => o.id === detail.object.id) || detail.object;
|
|
521
|
+
const selKey = `obj:${obj.id}`;
|
|
522
|
+
const isSelected = objectSelection.has(selKey);
|
|
523
|
+
if (obj.kind === 'sticky') {
|
|
524
|
+
addLabel(`便签 · #${obj.id}`);
|
|
525
|
+
addItem(isSelected ? '取消选中便签' : '选中此便签', () => {
|
|
526
|
+
if (isSelected) objectSelection.delete(selKey);
|
|
527
|
+
else objectSelection.set(selKey, { id: obj.id, kind: 'sticky', title: obj.title || `便签 #${obj.id}`, pluginId: obj.pluginId });
|
|
528
|
+
syncFrameObjects();
|
|
529
|
+
renderSelectionSummary();
|
|
530
|
+
});
|
|
531
|
+
addItem('编辑便签内容', () => {
|
|
532
|
+
try { frame.contentWindow.postMessage({ type: 'clado-workspace:edit-sticky', id: obj.id }, location.origin); } catch {}
|
|
533
|
+
});
|
|
534
|
+
addItem('切换便签颜色', () => {
|
|
535
|
+
const tints = ['amber', 'mint', 'lavender', 'sky'];
|
|
536
|
+
const nextTint = tints[(tints.indexOf(obj.tint || 'amber') + 1) % tints.length];
|
|
537
|
+
saveObject({ ...obj, tint: nextTint });
|
|
538
|
+
});
|
|
539
|
+
addSep();
|
|
540
|
+
addItem('删除此便签', () => deleteObject(obj.id), true);
|
|
541
|
+
} else {
|
|
542
|
+
const isCustom = obj.data?.selRenderMode === 'custom';
|
|
543
|
+
const isExpanded = Boolean(obj.data?.expanded);
|
|
544
|
+
addLabel(`组件 · ${obj.title || obj.id}`);
|
|
545
|
+
addItem(isSelected ? '取消选中组件' : '选中此组件', () => {
|
|
546
|
+
if (isSelected) objectSelection.delete(selKey);
|
|
547
|
+
else objectSelection.set(selKey, { id: obj.id, kind: obj.kind, title: obj.title || obj.id, pluginId: obj.pluginId });
|
|
548
|
+
syncFrameObjects();
|
|
549
|
+
renderSelectionSummary();
|
|
550
|
+
});
|
|
551
|
+
addItem(isCustom ? '切换为默认高亮选态' : '切换为自绘音波选态', () => {
|
|
552
|
+
saveObject({ ...obj, data: { ...(obj.data || {}), selRenderMode: isCustom ? 'default' : 'custom' } });
|
|
553
|
+
});
|
|
554
|
+
addItem(isExpanded ? '还原组件大小' : '放大组件卡片', () => {
|
|
555
|
+
saveObject({ ...obj, data: { ...(obj.data || {}), expanded: !isExpanded } });
|
|
556
|
+
});
|
|
557
|
+
addSep();
|
|
558
|
+
addItem('删除此组件', () => deleteObject(obj.id), true);
|
|
559
|
+
}
|
|
560
|
+
} else if (detail.kind === 'card' && detail.node) {
|
|
561
|
+
const nodes = [...(aiSelection?.nodes || [])];
|
|
562
|
+
const at = nodes.findIndex(n => JSON.stringify(n) === JSON.stringify(detail.node));
|
|
563
|
+
addLabel(`卡片 · ${detail.title || detail.node[detail.node.length - 1]}`);
|
|
564
|
+
addItem(at >= 0 ? '取消选中此卡片' : '选中此卡片', () => {
|
|
565
|
+
const nextNodes = [...(aiSelection?.nodes || [])];
|
|
566
|
+
const idx = nextNodes.findIndex(n => JSON.stringify(n) === JSON.stringify(detail.node));
|
|
567
|
+
if (idx >= 0) nextNodes.splice(idx, 1);
|
|
568
|
+
else nextNodes.push(detail.node);
|
|
569
|
+
setSelection(nextNodes.length ? { file: selected, version, nodes: nextNodes } : null);
|
|
570
|
+
});
|
|
571
|
+
addItem('旁边贴一张便签', () => {
|
|
572
|
+
saveObject({ kind: 'sticky', title: '设计便签', pluginId: 'starter.sticky-notes', x: detail.wx, y: detail.wy, tint: 'amber', text: '记录设计要点…' });
|
|
573
|
+
});
|
|
574
|
+
} else {
|
|
575
|
+
addLabel('画布空白处');
|
|
576
|
+
addItem('+ 在此处新建便签', () => {
|
|
577
|
+
saveObject({ kind: 'sticky', title: '灵感便签', pluginId: 'starter.sticky-notes', x: detail.wx, y: detail.wy, tint: 'amber', text: '记录一条新想法…' });
|
|
578
|
+
});
|
|
579
|
+
addItem('+ 添加专注播放器组件', () => {
|
|
580
|
+
saveObject({
|
|
581
|
+
kind: 'music',
|
|
582
|
+
title: 'Lo-Fi 专注播放器',
|
|
583
|
+
pluginId: 'starter.lofi-player',
|
|
584
|
+
x: detail.wx,
|
|
585
|
+
y: detail.wy,
|
|
586
|
+
width: 326,
|
|
587
|
+
data: {
|
|
588
|
+
expanded: false,
|
|
589
|
+
selRenderMode: 'custom',
|
|
590
|
+
tracks: [
|
|
591
|
+
{ id: 't1', title: 'Midnight Rain · 深夜雨声', duration: '03:24', liked: true },
|
|
592
|
+
{ id: 't2', title: 'Glass Horizon · 玻璃天际线', duration: '02:58', liked: false },
|
|
593
|
+
{ id: 't3', title: 'Soft Focus · 柔焦白噪音', duration: '04:12', liked: false },
|
|
594
|
+
],
|
|
595
|
+
},
|
|
596
|
+
});
|
|
597
|
+
});
|
|
598
|
+
addSep();
|
|
599
|
+
addItem('居中复位画布', () => frame.contentWindow.CladoPrototype?.fit());
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
canvasCtxMenuEl.style.left = '10px';
|
|
603
|
+
canvasCtxMenuEl.style.top = '10px';
|
|
604
|
+
canvasCtxMenuEl.classList.add('show');
|
|
605
|
+
const menuW = canvasCtxMenuEl.offsetWidth || 186;
|
|
606
|
+
const menuH = canvasCtxMenuEl.offsetHeight || 160;
|
|
607
|
+
const clampedX = Math.max(10, Math.min(clientX, window.innerWidth - menuW - 10));
|
|
608
|
+
const clampedY = Math.max(10, Math.min(clientY, window.innerHeight - menuH - 10));
|
|
609
|
+
canvasCtxMenuEl.style.left = clampedX + 'px';
|
|
610
|
+
canvasCtxMenuEl.style.top = clampedY + 'px';
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
if (liveObjectsEnabled) {
|
|
614
|
+
selectionBarEl = document.createElement('div');
|
|
615
|
+
selectionBarEl.id = 'clado-selection-bar';
|
|
616
|
+
selectionBarEl.className = 'clado-selection-bar';
|
|
617
|
+
selectionBarEl.innerHTML = `
|
|
618
|
+
<span class="clado-selection-dot"></span>
|
|
619
|
+
<span class="clado-selection-text" data-sel-summary>已选 0 项</span>
|
|
620
|
+
<button type="button" class="clado-selection-send" data-sel-send>发给当前会话</button>
|
|
621
|
+
<button type="button" class="clado-selection-clear" data-sel-clear>完成</button>`;
|
|
622
|
+
selectionBarEl.querySelector('[data-sel-send]').onclick = () => {
|
|
623
|
+
const names = collectSelectionLabels().join('、');
|
|
624
|
+
window.dispatchEvent(new CustomEvent('clado:send-selection', { detail: { names } }));
|
|
625
|
+
};
|
|
626
|
+
selectionBarEl.querySelector('[data-sel-clear]').onclick = () => {
|
|
627
|
+
objectSelection.clear();
|
|
628
|
+
if (aiSelection) setSelection(null);
|
|
629
|
+
syncFrameObjects();
|
|
630
|
+
renderSelectionSummary();
|
|
631
|
+
};
|
|
632
|
+
document.body.append(selectionBarEl);
|
|
633
|
+
|
|
634
|
+
canvasCtxMenuEl = document.createElement('div');
|
|
635
|
+
canvasCtxMenuEl.id = 'clado-canvas-ctx-menu';
|
|
636
|
+
canvasCtxMenuEl.className = 'clado-canvas-ctx-menu';
|
|
637
|
+
document.body.append(canvasCtxMenuEl);
|
|
638
|
+
|
|
639
|
+
document.addEventListener('pointerdown', ev => {
|
|
640
|
+
if (canvasCtxMenuEl.classList.contains('show') && !ev.target.closest?.('#clado-canvas-ctx-menu')) {
|
|
641
|
+
closeCanvasCtxMenu();
|
|
642
|
+
}
|
|
643
|
+
}, true);
|
|
644
|
+
document.addEventListener('keydown', ev => {
|
|
645
|
+
if (ev.key === 'Escape') closeCanvasCtxMenu();
|
|
646
|
+
});
|
|
647
|
+
loadObjects();
|
|
648
|
+
}
|
|
649
|
+
|
|
122
650
|
let readyCanvas = null;
|
|
123
651
|
const canvasWaiters = new Set();
|
|
124
652
|
function readScreenshotCanvas() {
|
|
@@ -160,10 +688,11 @@
|
|
|
160
688
|
setFollowAi,
|
|
161
689
|
focusTarget,
|
|
162
690
|
readCanvas() {
|
|
163
|
-
|
|
691
|
+
const objSel = objectSelection.size ? [...objectSelection.values()] : undefined;
|
|
692
|
+
if (readyCanvas?.frame !== frame) return objSel ? { objectSelection: objSel } : undefined;
|
|
164
693
|
const { file, revision } = readyCanvas;
|
|
165
694
|
const selection = aiSelection?.file === file && aiSelection.version === revision ? aiSelection.nodes : [];
|
|
166
|
-
return { file, revision, selection };
|
|
695
|
+
return { file, revision, selection, ...(objSel ? { objectSelection: objSel } : {}) };
|
|
167
696
|
},
|
|
168
697
|
viewId: capabilities?.viewId,
|
|
169
698
|
});
|
|
@@ -180,6 +709,7 @@
|
|
|
180
709
|
});
|
|
181
710
|
function showSelection() {
|
|
182
711
|
if (selectionTab) frame.contentWindow.postMessage({ type: 'clado-workspace:selection', nodes: aiSelection?.nodes || [] }, location.origin);
|
|
712
|
+
renderSelectionSummary();
|
|
183
713
|
}
|
|
184
714
|
async function publishSelection(selection) {
|
|
185
715
|
const response = await fetch(withWorkspace('/library/selection'), { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ tab: selectionTab, ...selection }), keepalive: true });
|
|
@@ -246,7 +776,7 @@
|
|
|
246
776
|
if (!container) {
|
|
247
777
|
container = document.createElement('div');
|
|
248
778
|
container.className = 'empty-workspaces';
|
|
249
|
-
$('#emptyState').append(container);
|
|
779
|
+
($('#emptyState .clado-launchpad-card') || $('#emptyState')).append(container);
|
|
250
780
|
}
|
|
251
781
|
container.replaceChildren();
|
|
252
782
|
const title = document.createElement('div');
|
|
@@ -385,7 +915,7 @@
|
|
|
385
915
|
else if (!doc) { $('#updateError').textContent = '当前 Markdown 已删除或暂时无法解析,保留上次画面。有效文档仍可从目录打开。'; $('#updateError').hidden = false; status('当前画布未更新', true); $('#canvasLoading').hidden = true; post('ready'); }
|
|
386
916
|
else { $('#updateError').hidden = true; if (force || doc.version !== version) { open(selected, version ? '' : pendingAnchor, { update: true }); } else { flushPendingTouch(); status('已同步 ' + new Date(doc.modified).toLocaleTimeString('zh-CN', { hour12: false })); } }
|
|
387
917
|
}
|
|
388
|
-
} catch { status('连接中断,正在重连', true); $('#updateError').textContent = '目录连接中断,保留当前画面。服务恢复后自动重连。'; $('#updateError').hidden = false; $('#canvasLoading').hidden = true; post('ready'); }
|
|
918
|
+
} catch { if (pageHidden) return; status('连接中断,正在重连', true); $('#updateError').textContent = '目录连接中断,保留当前画面。服务恢复后自动重连。'; $('#updateError').hidden = false; $('#canvasLoading').hidden = true; post('ready'); }
|
|
389
919
|
finally {
|
|
390
920
|
busy = false;
|
|
391
921
|
if (pendingRescan) { pendingRescan = false; scan(true); }
|
|
@@ -404,16 +934,33 @@
|
|
|
404
934
|
setSelection(nodes.length ? { file: selected, version, nodes } : null);
|
|
405
935
|
return;
|
|
406
936
|
}
|
|
937
|
+
if (liveObjectsEnabled && data?.type === 'clado-library:object-save' && data.object) {
|
|
938
|
+
const existing = canvasObjectsList.find(o => o.id === data.object.id);
|
|
939
|
+
saveObject(existing ? { ...existing, ...data.object } : data.object);
|
|
940
|
+
return;
|
|
941
|
+
}
|
|
942
|
+
if (liveObjectsEnabled && data?.type === 'clado-library:object-toggle-select' && data.key) {
|
|
943
|
+
if (objectSelection.has(data.key)) objectSelection.delete(data.key);
|
|
944
|
+
else if (data.item) objectSelection.set(data.key, data.item);
|
|
945
|
+
syncFrameObjects();
|
|
946
|
+
renderSelectionSummary();
|
|
947
|
+
return;
|
|
948
|
+
}
|
|
949
|
+
if (liveObjectsEnabled && data?.type === 'clado-library:context-menu' && data.detail) {
|
|
950
|
+
const rect = frame.getBoundingClientRect?.() || { left: 0, top: 0 };
|
|
951
|
+
openCanvasCtxMenu((rect.left || 0) + (data.x || 0), (rect.top || 0) + (data.y || 0), data.detail);
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
407
954
|
if (data?.type === 'clado-library:chain-modal') { chainLock(data.value, data.mode); return; }
|
|
408
955
|
if (data?.type === 'clado-library:lens-modal') {
|
|
409
956
|
lensOpen = !!data.value;
|
|
410
|
-
if (lensOpen) { brand.close(); directory.close(); }
|
|
957
|
+
if (lensOpen) { brand.close(); directory.close(); closeCanvasCtxMenu(); }
|
|
411
958
|
host.setHidden(chainModal || lensOpen);
|
|
412
959
|
return;
|
|
413
960
|
}
|
|
414
961
|
if(modalLocked() && ['clado-library:open','clado-library:ready','clado-library:missing'].includes(data?.type))return;
|
|
415
|
-
if (data?.type === 'clado-library:open') { directory.close(); open(data.target, data.anchor); }
|
|
416
|
-
else if (data?.type === 'clado-library:dismiss-directory') { brand.close(); directory.close(); }
|
|
962
|
+
if (data?.type === 'clado-library:open') { directory.close(); closeCanvasCtxMenu(); open(data.target, data.anchor); }
|
|
963
|
+
else if (data?.type === 'clado-library:dismiss-directory') { brand.close(); directory.close(); closeCanvasCtxMenu(); }
|
|
417
964
|
else if (data?.type === 'clado-library:missing') directory.showMessage('链接超出当前目录', '请选择当前工作区中的文档。链接:' + data.target);
|
|
418
965
|
else if (data?.type === 'clado-library:view-state') syncChrome();
|
|
419
966
|
else if (data?.type === 'clado-library:ready') {
|
|
@@ -435,6 +982,7 @@
|
|
|
435
982
|
setSelection(nodes.length ? { file: selected, version: data.version, nodes } : null, nodes.length > 0);
|
|
436
983
|
}
|
|
437
984
|
showSelection();
|
|
985
|
+
syncFrameObjects();
|
|
438
986
|
if(offline){frame.contentWindow.postMessage({type:'clado-workspace:display',mode,theme:design.theme,anchor:pendingAnchor},'*');pendingAnchor='';}
|
|
439
987
|
else if (mode !== data.mode) frame.contentWindow.CladoPrototype?.setMode?.(mode);
|
|
440
988
|
saveView(); $('#canvasLoading').hidden = true; syncChrome();
|
|
@@ -469,6 +1017,7 @@
|
|
|
469
1017
|
scan(true);
|
|
470
1018
|
});
|
|
471
1019
|
es.addEventListener('workspace.updated', () => scan(true));
|
|
1020
|
+
es.addEventListener('objects.updated', () => { if (liveObjectsEnabled) loadObjects(); });
|
|
472
1021
|
es.addEventListener('agent.activity', event => {
|
|
473
1022
|
try {
|
|
474
1023
|
const payload = JSON.parse(event.data || '{}');
|
|
@@ -484,6 +1033,14 @@
|
|
|
484
1033
|
focusTarget(payload.file || selected, payload.nodes?.[0] || null, payload.label || '');
|
|
485
1034
|
} catch {}
|
|
486
1035
|
});
|
|
1036
|
+
es.addEventListener('agent.sessions', event => {
|
|
1037
|
+
try {
|
|
1038
|
+
const payload = JSON.parse(event.data || '{}');
|
|
1039
|
+
if (typeof CustomEvent === 'function') {
|
|
1040
|
+
dispatchEvent(new CustomEvent('clado:agent-sessions', { detail: payload }));
|
|
1041
|
+
}
|
|
1042
|
+
} catch {}
|
|
1043
|
+
});
|
|
487
1044
|
es.onerror = () => { sseReady = false; };
|
|
488
1045
|
addEventListener('pagehide', () => es.close());
|
|
489
1046
|
} catch {}
|