cladogram 0.6.0-beta.7 → 0.6.0-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.
@@ -234,6 +234,9 @@ export function resolvePiAgentDir() {
234
234
  const dir = process.env.CLADO_PI_AGENT_DIR
235
235
  ? resolve(process.env.CLADO_PI_AGENT_DIR)
236
236
  : getAgentDir();
237
+ if (process.env.CLADO_PI_AGENT_DIR && process.env.PI_CODING_AGENT_DIR !== dir) {
238
+ process.env.PI_CODING_AGENT_DIR = dir;
239
+ }
237
240
  if (!existsSync(dir)) mkdirSync(dir, { recursive: true, mode: 0o700 });
238
241
  return dir;
239
242
  }
@@ -687,6 +690,8 @@ export function deleteRole(roleId, agentDir = resolvePiAgentDir()) {
687
690
  // 3. Pi Native MCP (mcp.json), Skills, Prompt Templates (/Commands), Packages
688
691
  // ============================================================================
689
692
 
693
+ const MCP_EXPOSURES = ['direct', 'codemode', 'deferred', 'hidden'];
694
+
690
695
  function readMcpFile(filePath, scope) {
691
696
  const raw = readJsonSafe(filePath, null);
692
697
  const servers = raw?.mcpServers && typeof raw.mcpServers === 'object' ? raw.mcpServers : {};
@@ -696,10 +701,14 @@ function readMcpFile(filePath, scope) {
696
701
  type: cfg?.url ? 'http' : 'stdio',
697
702
  command: cfg?.command || '',
698
703
  args: Array.isArray(cfg?.args) ? cfg.args : [],
704
+ cwd: cfg?.cwd || '',
699
705
  url: cfg?.url || '',
700
- env: cfg?.env || {},
701
- headers: cfg?.headers || {},
702
- enabled: cfg?.disabled !== true && cfg?.enabled !== false,
706
+ env: cfg?.env && typeof cfg.env === 'object' ? cfg.env : {},
707
+ headers: cfg?.headers && typeof cfg.headers === 'object' ? cfg.headers : {},
708
+ timeout: typeof cfg?.timeout === 'number' ? cfg.timeout : undefined,
709
+ exposure: MCP_EXPOSURES.includes(cfg?.exposure) ? cfg.exposure : 'codemode',
710
+ description: typeof cfg?.description === 'string' ? cfg.description : '',
711
+ enabled: cfg?.enabled !== false && cfg?.disabled !== true,
703
712
  }));
704
713
  }
705
714
 
@@ -712,6 +721,35 @@ export function listPiMcpServers(workspace = process.cwd(), agentDir = resolvePi
712
721
  return [...byName.values()];
713
722
  }
714
723
 
724
+ export function readPiMcpRawJson(workspace = process.cwd(), agentDir = resolvePiAgentDir()) {
725
+ const userPath = join(agentDir, 'mcp.json');
726
+ const projectPath = join(workspace, '.pi', 'mcp.json');
727
+ const readRaw = p => {
728
+ if (!existsSync(p)) return JSON.stringify({ mcpServers: {} }, null, 2) + '\n';
729
+ try { return readFileSync(p, 'utf8'); } catch { return JSON.stringify({ mcpServers: {} }, null, 2) + '\n'; }
730
+ };
731
+ return {
732
+ userPath,
733
+ projectPath,
734
+ user: readRaw(userPath),
735
+ project: readRaw(projectPath),
736
+ };
737
+ }
738
+
739
+ export function savePiMcpRawJson(scope = 'user', jsonText = '', workspace = process.cwd(), agentDir = resolvePiAgentDir()) {
740
+ const parsed = typeof jsonText === 'string' ? JSON.parse(jsonText || '{"mcpServers":{}}') : jsonText;
741
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
742
+ throw new Error('mcp.json 顶层必须是 JSON 对象。');
743
+ }
744
+ const normalized = parsed.mcpServers && typeof parsed.mcpServers === 'object'
745
+ ? parsed
746
+ : { mcpServers: parsed };
747
+ const targetScope = scope === 'project' ? 'project' : 'user';
748
+ const filePath = targetScope === 'project' ? join(workspace, '.pi', 'mcp.json') : join(agentDir, 'mcp.json');
749
+ writeJsonSafe(filePath, normalized);
750
+ return { ok: true, scope: targetScope, path: filePath, count: Object.keys(normalized.mcpServers || {}).length };
751
+ }
752
+
715
753
  export function savePiMcpServer(input, workspace = process.cwd(), agentDir = resolvePiAgentDir()) {
716
754
  const name = String(input?.name || '').trim().replace(/[^a-zA-Z0-9._-]/g, '-');
717
755
  if (!name) throw new Error('MCP 服务器名称不能为空。');
@@ -719,33 +757,51 @@ export function savePiMcpServer(input, workspace = process.cwd(), agentDir = res
719
757
  const filePath = scope === 'project' ? join(workspace, '.pi', 'mcp.json') : join(agentDir, 'mcp.json');
720
758
  const doc = readJsonSafe(filePath, { mcpServers: {} });
721
759
  if (!doc.mcpServers || typeof doc.mcpServers !== 'object') doc.mcpServers = {};
760
+ const prevName = String(input?.originalName || '').trim();
761
+ if (prevName && prevName !== name && prevName in doc.mcpServers) {
762
+ delete doc.mcpServers[prevName];
763
+ }
722
764
  const entry = {};
723
- if (input?.url && String(input.url).trim()) {
724
- entry.url = String(input.url).trim();
725
- if (input.headers && typeof input.headers === 'object') entry.headers = input.headers;
765
+ if (input?.type === 'http' || (input?.url && String(input.url).trim() && input?.type !== 'stdio')) {
766
+ const urlStr = String(input?.url || '').trim();
767
+ if (!urlStr) throw new Error('请填写远程 MCP 端点 URL。');
768
+ entry.url = urlStr;
769
+ if (input.headers && typeof input.headers === 'object' && Object.keys(input.headers).length) {
770
+ entry.headers = input.headers;
771
+ }
726
772
  } else {
727
773
  const cmd = String(input?.command || '').trim();
728
774
  if (!cmd) throw new Error('请填写 MCP 启动命令(command)或远程 URL。');
729
775
  entry.command = cmd;
730
776
  entry.args = Array.isArray(input?.args)
731
- ? input.args.map(String)
777
+ ? input.args.map(String).filter(Boolean)
732
778
  : typeof input?.args === 'string' && input.args.trim()
733
779
  ? input.args.trim().split(/\s+/)
734
780
  : [];
781
+ if (input?.cwd && String(input.cwd).trim()) entry.cwd = String(input.cwd).trim();
735
782
  if (input.env && typeof input.env === 'object' && Object.keys(input.env).length) entry.env = input.env;
736
783
  }
737
- if (input?.enabled === false) entry.disabled = true;
784
+ if (typeof input?.timeout === 'number' && Number.isFinite(input.timeout) && input.timeout > 0) {
785
+ entry.timeout = Math.round(input.timeout);
786
+ }
787
+ if (input?.description && String(input.description).trim()) {
788
+ entry.description = String(input.description).trim().slice(0, 250);
789
+ }
790
+ const exposure = MCP_EXPOSURES.includes(input?.exposure) ? input.exposure : 'direct';
791
+ entry.exposure = exposure;
792
+ if (input?.enabled === false) entry.enabled = false;
738
793
  doc.mcpServers[name] = entry;
739
794
  writeJsonSafe(filePath, doc);
740
- return { ok: true, name, scope };
795
+ return { ok: true, name, scope, exposure };
741
796
  }
742
797
 
743
798
  export function togglePiMcpServer(name, enabled, workspace = process.cwd(), agentDir = resolvePiAgentDir()) {
744
799
  for (const filePath of [join(workspace, '.pi', 'mcp.json'), join(agentDir, 'mcp.json')]) {
745
800
  const doc = readJsonSafe(filePath, null);
746
801
  if (doc?.mcpServers && name in doc.mcpServers) {
747
- if (enabled) delete doc.mcpServers[name].disabled;
748
- else doc.mcpServers[name].disabled = true;
802
+ delete doc.mcpServers[name].disabled;
803
+ if (enabled) delete doc.mcpServers[name].enabled;
804
+ else doc.mcpServers[name].enabled = false;
749
805
  writeJsonSafe(filePath, doc);
750
806
  return { ok: true, name, enabled: Boolean(enabled) };
751
807
  }
@@ -783,6 +839,76 @@ export function importPiMcpJson(jsonText, scope = 'user', workspace = process.cw
783
839
  return { ok: true, count };
784
840
  }
785
841
 
842
+ // ============================================================================
843
+ // 3.5 Plugin / Extension Injected Parameters (.clado/plugin-params.json + user)
844
+ // ============================================================================
845
+
846
+ export function readPluginParams(workspace = process.cwd(), stateDir = '', agentDir = resolvePiAgentDir()) {
847
+ const userPath = join(agentDir, 'clado-plugin-params.json');
848
+ const wsPath = stateDir ? join(stateDir, 'plugin-params.json') : join(workspace, '.clado', 'plugin-params.json');
849
+ const userRaw = readJsonSafe(userPath, {});
850
+ const wsRaw = readJsonSafe(wsPath, {});
851
+ const merged = {};
852
+ for (const [id, val] of Object.entries(userRaw && typeof userRaw === 'object' ? userRaw : {})) {
853
+ if (val && typeof val === 'object') merged[id] = { scope: 'user', env: val.env || {}, params: val.params || {} };
854
+ }
855
+ for (const [id, val] of Object.entries(wsRaw && typeof wsRaw === 'object' ? wsRaw : {})) {
856
+ if (val && typeof val === 'object') {
857
+ const prev = merged[id] || { env: {}, params: {} };
858
+ merged[id] = {
859
+ scope: 'project',
860
+ env: { ...(prev.env || {}), ...(val.env || {}) },
861
+ params: { ...(prev.params || {}), ...(val.params || {}) },
862
+ };
863
+ }
864
+ }
865
+ return { userPath, projectPath: wsPath, items: merged };
866
+ }
867
+
868
+ export function savePluginParams({ pluginId, scope = 'project', env = {}, params = {} } = {}, workspace = process.cwd(), stateDir = '', agentDir = resolvePiAgentDir()) {
869
+ const id = String(pluginId || '').trim();
870
+ if (!id) throw new Error('插件或扩展标识不能为空。');
871
+ const targetScope = scope === 'user' ? 'user' : 'project';
872
+ const filePath = targetScope === 'user'
873
+ ? join(agentDir, 'clado-plugin-params.json')
874
+ : (stateDir ? join(stateDir, 'plugin-params.json') : join(workspace, '.clado', 'plugin-params.json'));
875
+ const doc = readJsonSafe(filePath, {});
876
+ const cleanEnv = {};
877
+ if (env && typeof env === 'object' && !Array.isArray(env)) {
878
+ for (const [k, v] of Object.entries(env)) {
879
+ const ck = String(k || '').trim();
880
+ if (ck) cleanEnv[ck] = String(v ?? '');
881
+ }
882
+ }
883
+ const cleanParams = (params && typeof params === 'object' && !Array.isArray(params)) ? params : {};
884
+ doc[id] = { env: cleanEnv, params: cleanParams, updatedAt: new Date().toISOString() };
885
+ writeJsonSafe(filePath, doc);
886
+ return { ok: true, pluginId: id, scope: targetScope, path: filePath };
887
+ }
888
+
889
+ export function deletePluginParams(pluginId, scope = 'all', workspace = process.cwd(), stateDir = '', agentDir = resolvePiAgentDir()) {
890
+ const id = String(pluginId || '').trim();
891
+ if (!id) return { ok: false };
892
+ const paths = scope === 'user'
893
+ ? [join(agentDir, 'clado-plugin-params.json')]
894
+ : scope === 'project'
895
+ ? [stateDir ? join(stateDir, 'plugin-params.json') : join(workspace, '.clado', 'plugin-params.json')]
896
+ : [
897
+ join(agentDir, 'clado-plugin-params.json'),
898
+ stateDir ? join(stateDir, 'plugin-params.json') : join(workspace, '.clado', 'plugin-params.json'),
899
+ ];
900
+ let removed = false;
901
+ for (const p of paths) {
902
+ const doc = readJsonSafe(p, null);
903
+ if (doc && typeof doc === 'object' && id in doc) {
904
+ delete doc[id];
905
+ writeJsonSafe(p, doc);
906
+ removed = true;
907
+ }
908
+ }
909
+ return { ok: removed, pluginId: id };
910
+ }
911
+
786
912
  export function savePiSkill({ name, description, content, scope = 'user' }, workspace = process.cwd(), agentDir = resolvePiAgentDir()) {
787
913
  const slug = String(name || '').trim().toLowerCase().replace(/[^a-z0-9_-]+/g, '-').replace(/^-+|-+$/g, '');
788
914
  if (!slug) throw new Error('Skill 名称不能为空。');
@@ -15,6 +15,7 @@ import {
15
15
  deletePiMcpServer,
16
16
  deletePiPrompt,
17
17
  deletePiSkill,
18
+ deletePluginParams,
18
19
  deleteProviderConfig,
19
20
  deleteRole,
20
21
  ensureCladoPlatformDocs,
@@ -25,6 +26,8 @@ import {
25
26
  listRoles,
26
27
  readAgentsMemory,
27
28
  readCanvasComponentsConfig,
29
+ readPiMcpRawJson,
30
+ readPluginParams,
28
31
  readRuntimeSettings,
29
32
  removePiPackage,
30
33
  resetCladoPlatformDocs,
@@ -32,9 +35,11 @@ import {
32
35
  resolveRole,
33
36
  saveAgentsMemory,
34
37
  saveCladoPlatformContext,
38
+ savePiMcpRawJson,
35
39
  savePiMcpServer,
36
40
  savePiPrompt,
37
41
  savePiSkill,
42
+ savePluginParams,
38
43
  saveProviderConfig,
39
44
  saveRole,
40
45
  saveRuntimeSettings,
@@ -42,6 +47,7 @@ import {
42
47
  toggleCanvasComponent,
43
48
  togglePiMcpServer,
44
49
  } from '../agent/pi-config.js';
50
+ import { listInstalledWorkspacePlugins } from './plugin-registry.js';
45
51
  import {
46
52
  archiveSession,
47
53
  createPiBridge,
@@ -253,8 +259,23 @@ export function createAgentHandler({
253
259
  const memory = readAgentsMemory(rt.workspace);
254
260
  const platformDocs = ensureCladoPlatformDocs(agentDir);
255
261
  const canvasComponents = readCanvasComponentsConfig(agentDir);
256
- const sessions = listSessions(rt.stateDir, { includeArchived: true, messageLimit: 30, agentDir });
262
+ const rawSessions = listSessions(rt.stateDir, { includeArchived: true, messageLimit: 30, agentDir });
263
+ const sessions = bridge.overlayLiveSessions(rt.workspace, rawSessions);
257
264
  const automations = readAutomationsState(rt.stateDir);
265
+ const mcpRawJson = readPiMcpRawJson(rt.workspace, agentDir);
266
+ const pluginParams = readPluginParams(rt.workspace, rt.stateDir, agentDir);
267
+ const installedWsPlugins = listInstalledWorkspacePlugins(rt.stateDir);
268
+ const descriptorById = new Map((rt.pluginDescriptors || []).map(p => [p.id, p]));
269
+ const workspacePluginsMap = new Map();
270
+ for (const p of rt.pluginDescriptors || []) workspacePluginsMap.set(p.id, { ...p });
271
+ for (const inst of installedWsPlugins) {
272
+ const prev = workspacePluginsMap.get(inst.id) || {};
273
+ workspacePluginsMap.set(inst.id, {
274
+ ...prev,
275
+ ...inst,
276
+ active: descriptorById.has(inst.id),
277
+ });
278
+ }
258
279
 
259
280
  return {
260
281
  engine: 'pi',
@@ -272,19 +293,26 @@ export function createAgentHandler({
272
293
  configuredModels: modelsInfo.configuredModels,
273
294
  roles,
274
295
  ecosystem,
296
+ mcpRawJson,
297
+ pluginParams,
275
298
  canvasComponents,
276
- workspacePlugins: rt.pluginDescriptors,
299
+ workspacePlugins: [...workspacePluginsMap.values()],
277
300
  memory,
278
301
  platformDocs,
279
302
  automations,
280
303
  eventCatalog: SYSTEM_EVENT_CATALOG,
281
304
  activeRuns: bridge.listActiveRuns(rt.workspace),
305
+ liveRuntime: bridge.inspectRuntime(rt.workspace),
282
306
  sessions,
283
307
  };
284
308
  }
285
309
 
286
310
  return {
287
311
  automationEngine,
312
+ async reloadWorkspace(targetWorkspace) {
313
+ const rt = getRuntime(targetWorkspace);
314
+ return await bridge.reloadWorkspaceSessions(rt.workspace);
315
+ },
288
316
  notifySystemEvent(targetWorkspace, eventType, payload = {}) {
289
317
  const rt = getRuntime(targetWorkspace);
290
318
  automationEngine.notifyEvent(rt.workspace, eventType, payload);
@@ -388,18 +416,22 @@ export function createAgentHandler({
388
416
  limit,
389
417
  before: beforeParam !== null ? Number(beforeParam) : null,
390
418
  });
419
+ const merged = beforeParam !== null
420
+ ? { ...item, messages: page.messages, totalMessages: page.totalMessages, oldestIndex: page.oldestIndex, hasMoreBefore: page.hasMoreBefore }
421
+ : bridge.overlayLiveSession(rt.workspace, { ...item, messages: page.messages, totalMessages: page.totalMessages, oldestIndex: page.oldestIndex, hasMoreBefore: page.hasMoreBefore });
391
422
  sendJson(res, 200, {
392
- session: { ...item, messages: page.messages, totalMessages: page.totalMessages, oldestIndex: page.oldestIndex, hasMoreBefore: page.hasMoreBefore },
423
+ session: merged,
393
424
  ...page,
394
425
  });
395
426
  return;
396
427
  }
397
- sendJson(res, 200, { session: item });
428
+ sendJson(res, 200, { session: bridge.overlayLiveSession(rt.workspace, item) });
398
429
  return;
399
430
  }
400
431
  const includeArchived = url.searchParams.get('archived') === '1';
401
432
  const sinceMinutes = Number(url.searchParams.get('sinceMinutes') || 0);
402
- sendJson(res, 200, { sessions: listSessions(rt.stateDir, { includeArchived, sinceMinutes, messageLimit: 30, agentDir }) });
433
+ const list = listSessions(rt.stateDir, { includeArchived, sinceMinutes, messageLimit: 30, agentDir });
434
+ sendJson(res, 200, { sessions: bridge.overlayLiveSessions(rt.workspace, list) });
403
435
  return;
404
436
  }
405
437
 
@@ -433,6 +465,15 @@ export function createAgentHandler({
433
465
  sendJson(res, 200, result);
434
466
  return;
435
467
  }
468
+ if (body?.action === 'reload') {
469
+ const result = await bridge.reloadSession({
470
+ workspace: rt.workspace,
471
+ stateDir: rt.stateDir,
472
+ sessionId: body.id || '',
473
+ });
474
+ sendJson(res, 200, result);
475
+ return;
476
+ }
436
477
  if (body?.action === 'abort_all') {
437
478
  const stoppedCount = await bridge.abortAllRuns(rt.workspace);
438
479
  sendJson(res, 200, { ok: true, stoppedCount });
@@ -470,6 +511,7 @@ export function createAgentHandler({
470
511
  const id = url.searchParams.get('id');
471
512
  if (!id) throw new DocumentError('缺少会话 ID。', 400);
472
513
  await bridge.cancel(rt.workspace, id);
514
+ await bridge.disposeWarmSession(rt.workspace, id);
473
515
  sendJson(res, 200, deleteSession(rt.stateDir, id, agentDir));
474
516
  return;
475
517
  }
@@ -524,54 +566,91 @@ export function createAgentHandler({
524
566
  sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
525
567
  return;
526
568
  }
569
+ if (action === 'reload_pi') {
570
+ const result = await bridge.reloadSession({
571
+ workspace: rt.workspace,
572
+ stateDir: rt.stateDir,
573
+ sessionId: body.sessionId || '',
574
+ });
575
+ sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
576
+ return;
577
+ }
527
578
  if (action === 'save_mcp') {
528
579
  const result = savePiMcpServer(body, rt.workspace, agentDir);
529
- sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
580
+ const reload = await bridge.reloadWorkspaceSessions(rt.workspace);
581
+ sendJson(res, 200, { ...result, reload, meta: await buildFullMeta(targetWorkspace) });
530
582
  return;
531
583
  }
532
584
  if (action === 'toggle_mcp') {
533
585
  const result = togglePiMcpServer(body.name, body.enabled, rt.workspace, agentDir);
534
- sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
586
+ const reload = await bridge.reloadWorkspaceSessions(rt.workspace);
587
+ sendJson(res, 200, { ...result, reload, meta: await buildFullMeta(targetWorkspace) });
535
588
  return;
536
589
  }
537
590
  if (action === 'delete_mcp') {
538
591
  const result = deletePiMcpServer(body.name, rt.workspace, agentDir);
539
- sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
592
+ const reload = await bridge.reloadWorkspaceSessions(rt.workspace);
593
+ sendJson(res, 200, { ...result, reload, meta: await buildFullMeta(targetWorkspace) });
540
594
  return;
541
595
  }
542
596
  if (action === 'import_mcp_json') {
543
597
  const result = importPiMcpJson(body.json, body.scope || 'user', rt.workspace, agentDir);
544
- sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
598
+ const reload = await bridge.reloadWorkspaceSessions(rt.workspace);
599
+ sendJson(res, 200, { ...result, reload, meta: await buildFullMeta(targetWorkspace) });
600
+ return;
601
+ }
602
+ if (action === 'save_mcp_raw') {
603
+ const result = savePiMcpRawJson(body.scope || 'user', body.json, rt.workspace, agentDir);
604
+ const reload = await bridge.reloadWorkspaceSessions(rt.workspace);
605
+ sendJson(res, 200, { ...result, reload, meta: await buildFullMeta(targetWorkspace) });
606
+ return;
607
+ }
608
+ if (action === 'save_plugin_params') {
609
+ const result = savePluginParams(body, rt.workspace, rt.stateDir, agentDir);
610
+ const reload = await bridge.reloadWorkspaceSessions(rt.workspace);
611
+ sendJson(res, 200, { ...result, reload, meta: await buildFullMeta(targetWorkspace) });
612
+ return;
613
+ }
614
+ if (action === 'delete_plugin_params') {
615
+ const result = deletePluginParams(body.pluginId || body.id, body.scope || 'all', rt.workspace, rt.stateDir, agentDir);
616
+ const reload = await bridge.reloadWorkspaceSessions(rt.workspace);
617
+ sendJson(res, 200, { ...result, reload, meta: await buildFullMeta(targetWorkspace) });
545
618
  return;
546
619
  }
547
620
  if (action === 'save_skill') {
548
621
  const result = savePiSkill(body, rt.workspace, agentDir);
549
- sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
622
+ const reload = await bridge.reloadWorkspaceSessions(rt.workspace);
623
+ sendJson(res, 200, { ...result, reload, meta: await buildFullMeta(targetWorkspace) });
550
624
  return;
551
625
  }
552
626
  if (action === 'delete_skill') {
553
627
  const result = deletePiSkill(body.name, rt.workspace, agentDir);
554
- sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
628
+ const reload = await bridge.reloadWorkspaceSessions(rt.workspace);
629
+ sendJson(res, 200, { ...result, reload, meta: await buildFullMeta(targetWorkspace) });
555
630
  return;
556
631
  }
557
632
  if (action === 'save_command') {
558
633
  const result = savePiPrompt(body, rt.workspace, agentDir);
559
- sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
634
+ const reload = await bridge.reloadWorkspaceSessions(rt.workspace);
635
+ sendJson(res, 200, { ...result, reload, meta: await buildFullMeta(targetWorkspace) });
560
636
  return;
561
637
  }
562
638
  if (action === 'delete_command') {
563
639
  const result = deletePiPrompt(body.name, rt.workspace, agentDir);
564
- sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
640
+ const reload = await bridge.reloadWorkspaceSessions(rt.workspace);
641
+ sendJson(res, 200, { ...result, reload, meta: await buildFullMeta(targetWorkspace) });
565
642
  return;
566
643
  }
567
644
  if (action === 'install_pi_package') {
568
645
  const result = await installPiPackage(body.source, { local: Boolean(body.local) }, rt.workspace, agentDir);
569
- sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
646
+ const reload = await bridge.reloadWorkspaceSessions(rt.workspace);
647
+ sendJson(res, 200, { ...result, reload, meta: await buildFullMeta(targetWorkspace) });
570
648
  return;
571
649
  }
572
650
  if (action === 'remove_pi_package') {
573
651
  const result = await removePiPackage(body.source, { local: Boolean(body.local) }, rt.workspace, agentDir);
574
- sendJson(res, 200, { ...result, meta: await buildFullMeta(targetWorkspace) });
652
+ const reload = await bridge.reloadWorkspaceSessions(rt.workspace);
653
+ sendJson(res, 200, { ...result, reload, meta: await buildFullMeta(targetWorkspace) });
575
654
  return;
576
655
  }
577
656
  if (action === 'save_memory') {
@@ -2224,8 +2224,16 @@
2224
2224
  }
2225
2225
  } catch {}
2226
2226
  });
2227
+ es.addEventListener('agent.stream', event => {
2228
+ try {
2229
+ const payload = JSON.parse(event.data || '{}');
2230
+ if (typeof CustomEvent === 'function') {
2231
+ dispatchEvent(new CustomEvent('clado:agent-stream', { detail: payload }));
2232
+ }
2233
+ } catch {}
2234
+ });
2227
2235
  es.onerror = () => { sseReady = false; pushDebugLog('sse', 'SSE 连接中断,正在重试'); };
2228
- for (const evtName of ['ready', 'doc.updated', 'agent.activity', 'agent.presence', 'agent.stage', 'canvas.draft', 'code.activity', 'code.diff']) {
2236
+ for (const evtName of ['ready', 'doc.updated', 'agent.activity', 'agent.presence', 'agent.stage', 'agent.sessions', 'canvas.draft', 'code.activity', 'code.diff']) {
2229
2237
  es.addEventListener(evtName, ev => pushDebugLog('sse', evtName + (ev.data ? ' ' + String(ev.data).slice(0, 140) : '')));
2230
2238
  }
2231
2239
  addEventListener('pagehide', () => es.close());