cladogram 0.6.0-beta.7 → 0.6.0-beta.9

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.
@@ -14,9 +14,21 @@ import { deleteCanvasObject, listCanvasObjects, readCanvasObject, saveCanvasObje
14
14
  import { createWorkspaceService } from '../workspace/service.js';
15
15
  import { createMcpHandler } from './mcp.js';
16
16
  import { builtInCapabilities, builtInGrants, inspectWorkspacePlugins, installWorkspacePlugin, loadWorkspacePlugins } from './plugins.js';
17
- import { installCatalogPlugin, listInstalledWorkspacePlugins, loadRegistryIndex, removeWorkspacePlugin, searchRegistry } from './plugin-registry.js';
17
+ import {
18
+ addPluginSource,
19
+ installCatalogPlugin,
20
+ installLocalMarketItem,
21
+ listInstalledWorkspacePlugins,
22
+ loadRegistryIndex,
23
+ removePluginSource,
24
+ removeWorkspacePlugin,
25
+ scanAllPluginSources,
26
+ searchRegistry,
27
+ togglePluginSource,
28
+ uninstallLocalMarketItem,
29
+ } from './plugin-registry.js';
18
30
  import { createCapabilityRegistry } from './plugin-capabilities.js';
19
- import { createPluginSettings } from './plugin-settings.js';
31
+ import { compileConfiguration, createPluginSettings } from './plugin-settings.js';
20
32
  import { createWorkspaceEvents, broadcastMcpEvent } from './events.js';
21
33
  import {
22
34
  browseLocalFolders,
@@ -263,6 +275,28 @@ export function createWorkspaceHandler(directory, options = {}) {
263
275
  }
264
276
  throw new DocumentError('插件市场索引仅支持 GET 或 POST。', 405);
265
277
  }
278
+ if (url.pathname === '/library/plugin-sources' && options.pluginSources) {
279
+ const target = resolveTarget(url);
280
+ if (req.method === 'GET') {
281
+ json(res, 200, await options.pluginSources.scan(target.workspace, url.searchParams.get('q') || ''));
282
+ return true;
283
+ }
284
+ if (req.method === 'POST') {
285
+ if (!hasTrustedSameOrigin(req, remote) || req.headers['content-type']?.split(';')[0] !== 'application/json') {
286
+ throw new DocumentError('插件市场源操作只接受同源页面请求。', 403);
287
+ }
288
+ const chunks = []; let size = 0;
289
+ for await (const chunk of req) {
290
+ size += chunk.length;
291
+ if (size > 131072) throw new DocumentError('请求体过大。', 413);
292
+ chunks.push(chunk);
293
+ }
294
+ const body = JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}');
295
+ json(res, 200, await options.pluginSources.handleAction(target.workspace, body));
296
+ return true;
297
+ }
298
+ throw new DocumentError('插件市场源接口仅支持 GET 或 POST。', 405);
299
+ }
266
300
  if (url.pathname === '/library/objects' && options.canvasObjects) {
267
301
  const target = resolveTarget(url);
268
302
  if (req.method === 'GET') {
@@ -523,11 +557,30 @@ export async function startWorkspace(input, { port = 4317, host = '', remote = f
523
557
  let rootCtx = contextFor(workspace, workspace, root); initialize(rootCtx);
524
558
  if (!activeHubMode) recordRecentWorkspace(workspace);
525
559
  const runtimes = new Map();
560
+ function registerInstalledPluginConfigs(stateDir, wsSettings) {
561
+ if (!wsSettings || !stateDir) return;
562
+ const pluginsDir = join(stateDir, 'plugins');
563
+ if (!existsSync(pluginsDir)) return;
564
+ try {
565
+ for (const ent of readdirSync(pluginsDir, { withFileTypes: true })) {
566
+ if (!ent.isDirectory() || ent.name.startsWith('.')) continue;
567
+ const mPath = join(pluginsDir, ent.name, 'clado-plugin.json');
568
+ if (!existsSync(mPath)) continue;
569
+ try {
570
+ const manifest = JSON.parse(readFileSync(mPath, 'utf8'));
571
+ if (manifest?.id && manifest?.configuration?.schema) {
572
+ wsSettings.register(manifest.id, compileConfiguration(manifest.configuration));
573
+ }
574
+ } catch {}
575
+ }
576
+ } catch {}
577
+ }
526
578
  function runtimeFor(ctx) {
527
579
  const existing = runtimes.get(ctx.workspace);
528
580
  if (existing) { existing.ctx = ctx; existing.root = ctx.documents; return existing; }
529
581
  const wsPlugins = experimentalPlugins ? loadWorkspacePlugins(ctx.state) : null;
530
582
  const wsSettings = createPluginSettings(ctx, wsPlugins?.configurations);
583
+ registerInstalledPluginConfigs(ctx.state, wsSettings);
531
584
  const wsGrants = builtInGrants();
532
585
  for (const { id, permissions } of wsPlugins?.descriptor.plugins || []) wsGrants.set(id, permissions);
533
586
  const wsProviders = [...builtInCapabilities(agentName !== 'off'), ...(wsPlugins?.capabilities || [])];
@@ -783,8 +836,72 @@ export async function startWorkspace(input, { port = 4317, host = '', remote = f
783
836
  rt.settings.register(installed.id, rt.plugins.configurations.get(installed.id));
784
837
  }
785
838
  }
839
+ registerInstalledPluginConfigs(rt.ctx.state, rt.settings);
786
840
  return installed;
787
841
  }
842
+ const pluginSources = {
843
+ async scan(targetWorkspace, query = '') {
844
+ const rt = runtimeFor(contextFor(workspace, targetWorkspace, root));
845
+ return await scanAllPluginSources({ workspace: rt.workspace, stateDir: rt.ctx.state, query });
846
+ },
847
+ async handleAction(targetWorkspace, body = {}) {
848
+ const rt = runtimeFor(contextFor(workspace, targetWorkspace, root));
849
+ const action = String(body?.action || '').trim();
850
+ if (action === 'add') {
851
+ const res = addPluginSource(body);
852
+ const scan = await scanAllPluginSources({ workspace: rt.workspace, stateDir: rt.ctx.state, query: body.q || '' });
853
+ return { ok: true, ...res, ...scan };
854
+ }
855
+ if (action === 'remove') {
856
+ const res = removePluginSource(body.id);
857
+ const scan = await scanAllPluginSources({ workspace: rt.workspace, stateDir: rt.ctx.state, query: body.q || '' });
858
+ return { ok: true, ...res, ...scan };
859
+ }
860
+ if (action === 'toggle') {
861
+ const res = togglePluginSource(body.id, body.enabled);
862
+ const scan = await scanAllPluginSources({ workspace: rt.workspace, stateDir: rt.ctx.state, query: body.q || '' });
863
+ return { ok: true, ...res, ...scan };
864
+ }
865
+ if (action === 'install_item') {
866
+ const item = body.item || body;
867
+ const previous = rt.plugins?.capabilities.length || 0;
868
+ const installed = await installLocalMarketItem(item, {
869
+ ctx: rt.ctx,
870
+ runtime: rt.plugins,
871
+ workspace: rt.workspace,
872
+ stateDir: rt.ctx.state,
873
+ scope: body.scope || 'user',
874
+ });
875
+ if (item.kind === 'clado-plugin') {
876
+ const pluginId = installed?.result?.id || item.id;
877
+ if (rt.plugins && pluginId) {
878
+ const descriptor = rt.plugins.descriptor.plugins.find(p => p.id === pluginId);
879
+ if (descriptor) {
880
+ rt.grants.set(pluginId, descriptor.permissions);
881
+ rt.providers.push(...rt.plugins.capabilities.slice(previous));
882
+ rt.settings.register(pluginId, rt.plugins.configurations.get(pluginId));
883
+ }
884
+ }
885
+ registerInstalledPluginConfigs(rt.ctx.state, rt.settings);
886
+ }
887
+ await agent?.reloadWorkspace?.(rt.workspace).catch(() => {});
888
+ const scan = await scanAllPluginSources({ workspace: rt.workspace, stateDir: rt.ctx.state, query: body.q || '' });
889
+ return { ok: true, installed, ...scan };
890
+ }
891
+ if (action === 'uninstall_item') {
892
+ const item = body.item || body;
893
+ const uninstalled = await uninstallLocalMarketItem(item, {
894
+ ctx: rt.ctx,
895
+ removePluginFn: id => removePlugin(rt.workspace, id),
896
+ workspace: rt.workspace,
897
+ });
898
+ await agent?.reloadWorkspace?.(rt.workspace).catch(() => {});
899
+ const scan = await scanAllPluginSources({ workspace: rt.workspace, stateDir: rt.ctx.state, query: body.q || '' });
900
+ return { ok: true, uninstalled, ...scan };
901
+ }
902
+ throw new DocumentError(`不支持的插件市场源操作:${action}`, 400);
903
+ },
904
+ };
788
905
  const canvasObjects = {
789
906
  list(targetWorkspace) {
790
907
  const rt = runtimeFor(contextFor(workspace, targetWorkspace, root));
@@ -959,7 +1076,7 @@ export async function startWorkspace(input, { port = 4317, host = '', remote = f
959
1076
  entrypoint:true,mode,theme,entry:entryFile,publishSelection,events,linkNode,
960
1077
  remote:isRemote,remoteToken,
961
1078
  plugins:rootRuntime.plugins,experimentalPlugins,servePluginAsset,
962
- agent,agentProfile:agentName,capabilities,settings:rootRuntime.settings,installPlugin,removePlugin,readPluginRegistry,installCatalog,canvasObjects,workspaceHub,
1079
+ agent,agentProfile:agentName,capabilities,settings:rootRuntime.settings,installPlugin,removePlugin,readPluginRegistry,installCatalog,pluginSources,canvasObjects,workspaceHub,
963
1080
  mcpUrl:liveMcpUrl,
964
1081
  hubMode:() => activeHubMode,
965
1082
  resolveWorkspace:resolveWorkspaceParam,