cladogram 0.5.5-beta.6 → 0.5.5-beta.7
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/docs/release.md +7 -0
- package/package.json +2 -2
- package/plugins/agent/index.js +1795 -407
- package/plugins/agent/style.css +1123 -420
- package/plugins/brand/index.js +199 -2
- package/src/agent/context.js +430 -0
- package/src/agent/probe.js +58 -0
- package/src/agent/runtime.js +418 -28
- package/src/agent/sessions.js +237 -32
- package/src/html/canvas-ui.css +75 -11
- package/src/html/plugin-ui.css +284 -0
- package/src/server/agent.js +818 -50
- package/src/server/assets/library-canvas.js +305 -1
- package/src/server/assets/library.js +329 -5
- package/src/server/plugin-registry.js +98 -0
- package/src/server/plugin-settings.js +1 -1
- package/src/server/workspace.js +113 -8
- package/src/workspace/objects.js +125 -0
package/src/server/workspace.js
CHANGED
|
@@ -7,10 +7,11 @@ import { renderHtml } from '../html/page.js';
|
|
|
7
7
|
import { pluginMarkup, workspacePluginAssets } from '../html/plugin-assets.js';
|
|
8
8
|
import { scanDirectory, workspacePath, readDocument, DocumentError } from '../workspace/documents.js';
|
|
9
9
|
import { contextFor, discoverWorkspaces, initialize } from '../workspace/storage.js';
|
|
10
|
+
import { deleteCanvasObject, listCanvasObjects, readCanvasObject, saveCanvasObject } from '../workspace/objects.js';
|
|
10
11
|
import { createWorkspaceService } from '../workspace/service.js';
|
|
11
12
|
import { createMcpHandler } from './mcp.js';
|
|
12
13
|
import { builtInCapabilities, builtInGrants, inspectWorkspacePlugins, installWorkspacePlugin, loadWorkspacePlugins } from './plugins.js';
|
|
13
|
-
import { loadRegistryIndex, removeWorkspacePlugin, searchRegistry } from './plugin-registry.js';
|
|
14
|
+
import { installCatalogPlugin, listInstalledWorkspacePlugins, loadRegistryIndex, removeWorkspacePlugin, searchRegistry } from './plugin-registry.js';
|
|
14
15
|
import { createCapabilityRegistry } from './plugin-capabilities.js';
|
|
15
16
|
import { createPluginSettings } from './plugin-settings.js';
|
|
16
17
|
import { createWorkspaceEvents, broadcastMcpEvent } from './events.js';
|
|
@@ -71,7 +72,11 @@ export function createWorkspaceHandler(directory, options = {}) {
|
|
|
71
72
|
}
|
|
72
73
|
if (options.agent && url.pathname === '/library/agent/sessions') {
|
|
73
74
|
const target = resolveTarget(url);
|
|
74
|
-
options.agent.handleSessions(req, res, url, target.workspace); return true;
|
|
75
|
+
await options.agent.handleSessions(req, res, url, target.workspace); return true;
|
|
76
|
+
}
|
|
77
|
+
if (options.agent && url.pathname === '/library/agent/config' && options.agent.handleConfig) {
|
|
78
|
+
const target = resolveTarget(url);
|
|
79
|
+
await options.agent.handleConfig(req, res, target.workspace); return true;
|
|
75
80
|
}
|
|
76
81
|
if (options.agent && url.pathname === '/library/agent') {
|
|
77
82
|
const target = resolveTarget(url);
|
|
@@ -116,10 +121,62 @@ export function createWorkspaceHandler(directory, options = {}) {
|
|
|
116
121
|
return true;
|
|
117
122
|
}
|
|
118
123
|
if (url.pathname === '/library/plugin-registry' && options.readPluginRegistry) {
|
|
119
|
-
if (req.method !== 'GET') throw new DocumentError('插件市场索引仅支持 GET。', 405);
|
|
120
124
|
const target = resolveTarget(url);
|
|
121
|
-
|
|
122
|
-
|
|
125
|
+
if (req.method === 'GET') {
|
|
126
|
+
json(res, 200, await options.readPluginRegistry(target.workspace, url.searchParams.get('q') || ''));
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
if (req.method === 'POST' && options.installCatalog) {
|
|
130
|
+
if (req.headers.origin !== 'http://' + host || req.headers['content-type']?.split(';')[0] !== 'application/json') {
|
|
131
|
+
throw new DocumentError('插件市场安装只接受同源页面请求。', 403);
|
|
132
|
+
}
|
|
133
|
+
const chunks = []; let size = 0;
|
|
134
|
+
for await (const chunk of req) {
|
|
135
|
+
size += chunk.length;
|
|
136
|
+
if (size > 65536) throw new DocumentError('请求体过大。', 413);
|
|
137
|
+
chunks.push(chunk);
|
|
138
|
+
}
|
|
139
|
+
const body = JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}');
|
|
140
|
+
if (!body?.id || typeof body.id !== 'string') throw new DocumentError('缺少插件 id。', 400);
|
|
141
|
+
json(res, 201, await options.installCatalog(target.workspace, body.id));
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
throw new DocumentError('插件市场索引仅支持 GET 或 POST。', 405);
|
|
145
|
+
}
|
|
146
|
+
if (url.pathname === '/library/objects' && options.canvasObjects) {
|
|
147
|
+
const target = resolveTarget(url);
|
|
148
|
+
if (req.method === 'GET') {
|
|
149
|
+
const objId = url.searchParams.get('id');
|
|
150
|
+
if (objId) {
|
|
151
|
+
const obj = options.canvasObjects.read(target.workspace, objId);
|
|
152
|
+
if (!obj) throw new DocumentError('未找到该画布对象。', 404);
|
|
153
|
+
json(res, 200, { object: obj });
|
|
154
|
+
} else {
|
|
155
|
+
json(res, 200, { objects: options.canvasObjects.list(target.workspace) });
|
|
156
|
+
}
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
if (req.method === 'POST') {
|
|
160
|
+
if (req.headers.origin !== 'http://' + host || req.headers['content-type']?.split(';')[0] !== 'application/json') {
|
|
161
|
+
throw new DocumentError('画布组件操作只接受同源页面请求。', 403);
|
|
162
|
+
}
|
|
163
|
+
const chunks = []; let size = 0;
|
|
164
|
+
for await (const chunk of req) {
|
|
165
|
+
size += chunk.length;
|
|
166
|
+
if (size > 131072) throw new DocumentError('画布对象数据过大。', 413);
|
|
167
|
+
chunks.push(chunk);
|
|
168
|
+
}
|
|
169
|
+
const body = JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}');
|
|
170
|
+
if (body?.action === 'delete') {
|
|
171
|
+
const deleted = options.canvasObjects.remove(target.workspace, body.id);
|
|
172
|
+
json(res, 200, { deleted, id: body.id });
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
const saved = options.canvasObjects.save(target.workspace, body);
|
|
176
|
+
json(res, 200, { object: saved });
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
throw new DocumentError('画布对象接口仅支持 GET 或 POST。', 405);
|
|
123
180
|
}
|
|
124
181
|
if (options.capabilities && url.pathname === '/library/plugin-bridge') {
|
|
125
182
|
const view = z.uuid().safeParse(url.searchParams.get('viewId'));
|
|
@@ -336,17 +393,65 @@ export async function startWorkspace(input, { port = 4317, mode = 'cards', theme
|
|
|
336
393
|
}
|
|
337
394
|
function removePlugin(targetWorkspace, id) {
|
|
338
395
|
const rt = runtimeFor(contextFor(workspace, targetWorkspace, root));
|
|
339
|
-
|
|
396
|
+
const removed = removeWorkspacePlugin(rt.ctx, id);
|
|
397
|
+
if (rt.plugins) {
|
|
398
|
+
rt.plugins.descriptor.plugins = rt.plugins.descriptor.plugins.filter(p => p.id !== removed.id);
|
|
399
|
+
rt.grants.delete(removed.id);
|
|
400
|
+
rt.providers = rt.providers.filter(p => p.pluginId !== removed.id);
|
|
401
|
+
}
|
|
402
|
+
return removed;
|
|
340
403
|
}
|
|
341
404
|
async function readPluginRegistry(targetWorkspace, query) {
|
|
342
405
|
const rt = runtimeFor(contextFor(workspace, targetWorkspace, root));
|
|
343
406
|
const reg = await loadRegistryIndex({ stateDir: rt.ctx.state });
|
|
344
|
-
|
|
407
|
+
const installedIds = listInstalledWorkspacePlugins(rt.ctx.state).map(p => p.id);
|
|
408
|
+
return {
|
|
409
|
+
name: reg.name,
|
|
410
|
+
origin: reg.origin,
|
|
411
|
+
installedIds: [...new Set(installedIds)],
|
|
412
|
+
plugins: searchRegistry(reg, query),
|
|
413
|
+
};
|
|
345
414
|
}
|
|
415
|
+
async function installCatalog(targetWorkspace, id) {
|
|
416
|
+
const rt = runtimeFor(contextFor(workspace, targetWorkspace, root));
|
|
417
|
+
const previous = rt.plugins?.capabilities.length || 0;
|
|
418
|
+
const installed = await installCatalogPlugin(rt.ctx, id, { runtime: rt.plugins });
|
|
419
|
+
if (rt.plugins) {
|
|
420
|
+
const descriptor = rt.plugins.descriptor.plugins.find(plugin => plugin.id === installed.id);
|
|
421
|
+
if (descriptor) {
|
|
422
|
+
rt.grants.set(installed.id, descriptor.permissions);
|
|
423
|
+
rt.providers.push(...rt.plugins.capabilities.slice(previous));
|
|
424
|
+
rt.settings.register(installed.id, rt.plugins.configurations.get(installed.id));
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return installed;
|
|
428
|
+
}
|
|
429
|
+
const canvasObjects = {
|
|
430
|
+
list(targetWorkspace) {
|
|
431
|
+
const rt = runtimeFor(contextFor(workspace, targetWorkspace, root));
|
|
432
|
+
return listCanvasObjects(rt.ctx.state);
|
|
433
|
+
},
|
|
434
|
+
read(targetWorkspace, id) {
|
|
435
|
+
const rt = runtimeFor(contextFor(workspace, targetWorkspace, root));
|
|
436
|
+
return readCanvasObject(rt.ctx.state, id);
|
|
437
|
+
},
|
|
438
|
+
save(targetWorkspace, payload) {
|
|
439
|
+
const rt = runtimeFor(contextFor(workspace, targetWorkspace, root));
|
|
440
|
+
const saved = saveCanvasObject(rt.ctx.state, payload);
|
|
441
|
+
events.emit(rt.workspace, 'objects.updated', { action: 'save', object: saved });
|
|
442
|
+
return saved;
|
|
443
|
+
},
|
|
444
|
+
remove(targetWorkspace, id) {
|
|
445
|
+
const rt = runtimeFor(contextFor(workspace, targetWorkspace, root));
|
|
446
|
+
const deleted = deleteCanvasObject(rt.ctx.state, id);
|
|
447
|
+
if (deleted) events.emit(rt.workspace, 'objects.updated', { action: 'delete', id });
|
|
448
|
+
return deleted;
|
|
449
|
+
},
|
|
450
|
+
};
|
|
346
451
|
const handle = createWorkspaceHandler(root,{
|
|
347
452
|
entrypoint:true,mode,theme,entry:entryFile,publishSelection,events,
|
|
348
453
|
plugins:rootRuntime.plugins,experimentalPlugins,servePluginAsset,
|
|
349
|
-
agent,agentProfile:agentName,capabilities,settings:rootRuntime.settings,installPlugin,removePlugin,readPluginRegistry,
|
|
454
|
+
agent,agentProfile:agentName,capabilities,settings:rootRuntime.settings,installPlugin,removePlugin,readPluginRegistry,installCatalog,canvasObjects,
|
|
350
455
|
resolveWorkspace:resolveWorkspaceParam,
|
|
351
456
|
listWorkspaces:() => discoverWorkspaces(workspace, root),
|
|
352
457
|
scopeName:basename(workspace),
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 多实例画布对象持久化存储(`<子工作区>/.clado/objects/<id>.json`)。
|
|
3
|
+
*
|
|
4
|
+
* 支持:
|
|
5
|
+
* - 多张画布便签(sticky):各自拥有独立 ID、世界坐标 (x, y)、色卡(amber / mint / lavender)与文本内容;
|
|
6
|
+
* - 多实例视图+工具混合组件(如专注播放器 widget):支持容器级属性与子区域靶点状态(data.tracks 等);
|
|
7
|
+
* - AI 与前端画布可按 ID 单独或批量读取、创建、修改与删除。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { randomUUID } from 'node:crypto';
|
|
11
|
+
import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
|
|
14
|
+
const SAFE_OBJECT_ID = /^[a-zA-Z0-9_-]{2,80}$/;
|
|
15
|
+
const ALLOWED_TINTS = new Set(['amber', 'mint', 'lavender', 'sky']);
|
|
16
|
+
const ALLOWED_KINDS = new Set(['sticky', 'music', 'widget']);
|
|
17
|
+
|
|
18
|
+
export function safeObjectId(id) {
|
|
19
|
+
const s = String(id || '').trim();
|
|
20
|
+
return SAFE_OBJECT_ID.test(s) ? s : null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function objectsDir(stateDir) {
|
|
24
|
+
const dir = join(stateDir, 'objects');
|
|
25
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
26
|
+
return dir;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function readCanvasObject(stateDir, id) {
|
|
30
|
+
const clean = safeObjectId(id);
|
|
31
|
+
if (!stateDir || !clean) return null;
|
|
32
|
+
const full = join(stateDir, 'objects', clean + '.json');
|
|
33
|
+
if (!existsSync(full)) return null;
|
|
34
|
+
try {
|
|
35
|
+
const data = JSON.parse(readFileSync(full, 'utf8'));
|
|
36
|
+
return data && data.id === clean ? data : null;
|
|
37
|
+
} catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function listCanvasObjects(stateDir, limit = 120) {
|
|
43
|
+
if (!stateDir) return [];
|
|
44
|
+
const dir = join(stateDir, 'objects');
|
|
45
|
+
if (!existsSync(dir) || !lstatSync(dir).isDirectory()) return [];
|
|
46
|
+
const items = [];
|
|
47
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
48
|
+
if (!entry.isFile() || !entry.name.endsWith('.json')) continue;
|
|
49
|
+
try {
|
|
50
|
+
const data = JSON.parse(readFileSync(join(dir, entry.name), 'utf8'));
|
|
51
|
+
if (data && safeObjectId(data.id)) items.push(data);
|
|
52
|
+
} catch {}
|
|
53
|
+
}
|
|
54
|
+
return items
|
|
55
|
+
.sort((a, b) => String(a.createdAt || '').localeCompare(String(b.createdAt || '')) || a.id.localeCompare(b.id))
|
|
56
|
+
.slice(0, limit);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function saveCanvasObject(stateDir, input = {}) {
|
|
60
|
+
if (!stateDir) throw new Error('缺少工作区状态目录。');
|
|
61
|
+
const existingCount = listCanvasObjects(stateDir, 200).length;
|
|
62
|
+
const rawKind = ALLOWED_KINDS.has(input.kind) ? input.kind : 'sticky';
|
|
63
|
+
const cleanId = safeObjectId(input.id) || `${rawKind === 'sticky' ? 'note' : rawKind}-${existingCount + 1}-${randomUUID().slice(0, 4)}`;
|
|
64
|
+
const prev = readCanvasObject(stateDir, cleanId);
|
|
65
|
+
const nowIso = new Date().toISOString();
|
|
66
|
+
|
|
67
|
+
const kind = ALLOWED_KINDS.has(input.kind) ? input.kind : (prev?.kind || 'sticky');
|
|
68
|
+
const pluginId = String(input.pluginId || prev?.pluginId || (kind === 'music' ? 'starter.lofi-player' : 'starter.sticky-notes')).slice(0, 80);
|
|
69
|
+
const x = Number.isFinite(Number(input.x)) ? Math.round(Number(input.x)) : (prev?.x ?? 180);
|
|
70
|
+
const y = Number.isFinite(Number(input.y)) ? Math.round(Number(input.y)) : (prev?.y ?? 140);
|
|
71
|
+
const width = Number.isFinite(Number(input.width))
|
|
72
|
+
? Math.min(560, Math.max(140, Math.round(Number(input.width))))
|
|
73
|
+
: (prev?.width ?? (kind === 'music' ? 320 : 184));
|
|
74
|
+
const tint = ALLOWED_TINTS.has(input.tint) ? input.tint : (prev?.tint || 'amber');
|
|
75
|
+
const title = String(
|
|
76
|
+
input.title || prev?.title || (kind === 'music' ? 'Lo-Fi 专注播放器' : `便签 #${cleanId}`)
|
|
77
|
+
).trim().slice(0, 100);
|
|
78
|
+
const text = input.text !== undefined
|
|
79
|
+
? String(input.text || '').slice(0, 4000)
|
|
80
|
+
: (prev?.text ?? (kind === 'sticky' ? '记录一条设计备忘…' : ''));
|
|
81
|
+
|
|
82
|
+
const defaultMusicData = kind === 'music' ? {
|
|
83
|
+
expanded: false,
|
|
84
|
+
selRenderMode: 'default',
|
|
85
|
+
tracks: [
|
|
86
|
+
{ id: 'song-1', title: '晴天 · 周杰伦', duration: '04:29', liked: false },
|
|
87
|
+
{ id: 'song-2', title: 'Midnight Coding · Synth', duration: '03:18', liked: true },
|
|
88
|
+
],
|
|
89
|
+
} : {};
|
|
90
|
+
|
|
91
|
+
const data = (input.data && typeof input.data === 'object' && !Array.isArray(input.data))
|
|
92
|
+
? { ...(prev?.data || defaultMusicData), ...input.data }
|
|
93
|
+
: (prev?.data || defaultMusicData);
|
|
94
|
+
|
|
95
|
+
const record = {
|
|
96
|
+
id: cleanId,
|
|
97
|
+
kind,
|
|
98
|
+
pluginId,
|
|
99
|
+
title,
|
|
100
|
+
x,
|
|
101
|
+
y,
|
|
102
|
+
width,
|
|
103
|
+
tint,
|
|
104
|
+
text,
|
|
105
|
+
data,
|
|
106
|
+
createdAt: prev?.createdAt || nowIso,
|
|
107
|
+
updatedAt: nowIso,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
writeFileSync(join(objectsDir(stateDir), cleanId + '.json'), JSON.stringify(record, null, 2) + '\n');
|
|
111
|
+
return record;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function deleteCanvasObject(stateDir, id) {
|
|
115
|
+
const clean = safeObjectId(id);
|
|
116
|
+
if (!stateDir || !clean) return false;
|
|
117
|
+
const full = join(stateDir, 'objects', clean + '.json');
|
|
118
|
+
if (!existsSync(full)) return false;
|
|
119
|
+
try {
|
|
120
|
+
unlinkSync(full);
|
|
121
|
+
return true;
|
|
122
|
+
} catch {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
}
|