pi-web-ui 0.103.2 → 0.104.0
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/CHANGELOG.md +37 -0
- package/dist/server/agent-formatters.js +18 -2
- package/dist/server/agent-service-daemon.js +4 -3
- package/dist/server/agent-service.js +773 -181
- package/dist/server/compaction-markers.js +111 -3
- package/dist/server/delegate-mode.js +2 -0
- package/dist/server/dispatch-domain-handlers.js +25 -0
- package/dist/server/dsh/dsh-agent-service.js +265 -11
- package/dist/server/goal-review-gate.js +2 -0
- package/dist/server/goal-service.js +59 -2
- package/dist/server/index.js +1 -0
- package/dist/server/markers-list-tool.js +1 -1
- package/dist/server/market-service.js +36 -9
- package/dist/server/mcp-agent-tool.js +164 -0
- package/dist/server/mcp-manager.js +182 -0
- package/dist/server/plan-mode.js +3 -0
- package/dist/server/plugin-api-catalog.js +24 -0
- package/dist/server/plugins.js +113 -0
- package/dist/server/preset-share.js +52 -1
- package/dist/server/remote-ssh-service.js +90 -25
- package/dist/server/sandbox-service.js +705 -0
- package/dist/server/sandbox-tool.js +365 -0
- package/dist/server/session-parser.js +48 -21
- package/dist/server/subagent-helpers.js +32 -1
- package/dist/server/terminals.js +210 -10
- package/dist/server/tool-manager.js +24 -0
- package/dist/server/webui-context.js +16 -2
- package/dist/server/workspace-snapshot.js +51 -13
- package/package.json +1 -1
- package/plugin-sdk/index.d.ts +39 -0
- package/plugin-sdk/index.mjs +14 -0
- package/plugins/catalog.json +110 -0
- package/themes/zhujian-dark.css +711 -0
- package/themes/zhujian.css +760 -0
- package/web/dist/assets/{TerminalPanel-DQFDRz0A.js → TerminalPanel-28naZdcs.js} +1 -1
- package/web/dist/assets/index-DbGGBf5I.js +680 -0
- package/web/dist/assets/index-RqIdFOsh.css +1 -0
- package/web/dist/index.html +2 -2
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/index-CYQdZEbx.css +0 -1
- package/web/dist/assets/index-aGRL6x3h.js +0 -680
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sandbox — AI 智能体侧的隔离沙盒项目管理工具。
|
|
3
|
+
*
|
|
4
|
+
* 支持动作:
|
|
5
|
+
* - list: 列出当前受管的所有沙盒项目,并指出当前工作区是否处于沙盒内;
|
|
6
|
+
* - create: 创建「空白沙盒」或「导入现有项目」的隔离沙盒副本(可选 switchTo 切换工作区,或将返回的 path 传给 subagent cwd);
|
|
7
|
+
* - diff: 对比沙盒与源项目之间的新增/修改/删除文件清单;
|
|
8
|
+
* - reset: 将沙盒重置回初始干净状态(清空或重新从源项目复制);
|
|
9
|
+
* - persist: 将临时沙盒转为永久沙盒;
|
|
10
|
+
* - promote: 将沙盒导出为外部正式项目目录;
|
|
11
|
+
* - apply_to_source: 将导入型沙盒的修改同步写回源项目(强制触发用户审批确认 askApproval);
|
|
12
|
+
* - destroy: 销毁指定沙盒并回收磁盘空间。
|
|
13
|
+
*/
|
|
14
|
+
import { Type } from "typebox";
|
|
15
|
+
import { pick } from "./i18n.js";
|
|
16
|
+
export const SANDBOX_TOOL_NAME = "sandbox";
|
|
17
|
+
const SandboxParameters = Type.Object({
|
|
18
|
+
action: Type.Union([
|
|
19
|
+
Type.Literal("list"),
|
|
20
|
+
Type.Literal("create"),
|
|
21
|
+
Type.Literal("diff"),
|
|
22
|
+
Type.Literal("reset"),
|
|
23
|
+
Type.Literal("persist"),
|
|
24
|
+
Type.Literal("promote"),
|
|
25
|
+
Type.Literal("apply_to_source"),
|
|
26
|
+
Type.Literal("destroy"),
|
|
27
|
+
], {
|
|
28
|
+
description: "Operation to perform: list | create | diff | reset | persist | promote | apply_to_source | destroy.",
|
|
29
|
+
}),
|
|
30
|
+
mode: Type.Optional(Type.Union([Type.Literal("blank"), Type.Literal("import")], {
|
|
31
|
+
description: 'create: "import" clones an existing project into an isolated sandbox (default); "blank" creates an empty sandbox.',
|
|
32
|
+
})),
|
|
33
|
+
sourcePath: Type.Optional(Type.String({
|
|
34
|
+
description: 'create (when mode="import"): source directory path to clone. Defaults to the current workspace (or its original sourcePath if already in a sandbox).',
|
|
35
|
+
})),
|
|
36
|
+
name: Type.Optional(Type.String({
|
|
37
|
+
description: "create: optional display name for the sandbox.",
|
|
38
|
+
})),
|
|
39
|
+
ephemeral: Type.Optional(Type.Boolean({
|
|
40
|
+
description: "create: true (default) for a temporary sandbox; false for a permanent sandbox.",
|
|
41
|
+
})),
|
|
42
|
+
switchTo: Type.Optional(Type.Boolean({
|
|
43
|
+
description: "create/promote: true to switch the active workspace to the created/exported directory immediately; false (default for create) keeps the current workspace and returns the sandbox path (useful for spawning a subagent with cwd).",
|
|
44
|
+
})),
|
|
45
|
+
sandbox: Type.Optional(Type.String({
|
|
46
|
+
description: "diff/reset/persist/promote/apply_to_source/destroy: target sandbox ID or path. Defaults to the current workspace if it is a sandbox.",
|
|
47
|
+
})),
|
|
48
|
+
targetPath: Type.Optional(Type.String({
|
|
49
|
+
description: "promote: destination directory path outside sandboxes (must be a new or empty directory).",
|
|
50
|
+
})),
|
|
51
|
+
});
|
|
52
|
+
/** details 摘录中最多携带的文件路径条目数(严格守卫 details ≤ 64KB 预算)。 */
|
|
53
|
+
const MAX_DIFF_FILES_IN_DETAILS = 100;
|
|
54
|
+
export function makeSandboxTool(host, getLang) {
|
|
55
|
+
return {
|
|
56
|
+
name: SANDBOX_TOOL_NAME,
|
|
57
|
+
label: "Manage isolated sandbox workspaces",
|
|
58
|
+
description: "Create, inspect, diff, reset, export, or destroy isolated copy-on-write sandbox workspaces. " +
|
|
59
|
+
"Supports blank sandboxes or imported project copies (auto-skipping node_modules and build caches) where edits never touch the original files.",
|
|
60
|
+
promptSnippet: "create or manage isolated sandbox workspaces for safe experimentation",
|
|
61
|
+
promptGuidelines: [
|
|
62
|
+
"Use sandbox(action='create') for risky refactors, dependency upgrades, or standalone prototypes so original project files remain untouched",
|
|
63
|
+
"Run sandbox(action='diff') to review changed files before calling sandbox(action='apply_to_source')",
|
|
64
|
+
],
|
|
65
|
+
parameters: SandboxParameters,
|
|
66
|
+
async execute(toolCallId, params) {
|
|
67
|
+
const lang = getLang();
|
|
68
|
+
const cwd = host.getCwd();
|
|
69
|
+
const activeSbx = host.sandboxes.getByCwd(cwd);
|
|
70
|
+
try {
|
|
71
|
+
switch (params.action) {
|
|
72
|
+
case "list": {
|
|
73
|
+
const list = host.sandboxes.list();
|
|
74
|
+
if (list.length === 0) {
|
|
75
|
+
return {
|
|
76
|
+
content: [
|
|
77
|
+
{
|
|
78
|
+
type: "text",
|
|
79
|
+
text: pick(lang, "当前没有任何沙盒项目。可调用 sandbox(action='create') 创建空白或导入型隔离沙盒。", "No sandbox projects exist. Call sandbox(action='create') to create a blank or imported isolated sandbox."),
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
details: { action: "list", count: 0, activeSandboxId: null },
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const lines = list.map((s) => {
|
|
86
|
+
const activeMark = activeSbx?.id === s.id ? " [ACTIVE]" : "";
|
|
87
|
+
const life = s.ephemeral ? "ephemeral" : "permanent";
|
|
88
|
+
const src = s.sourcePath ? ` (source: ${s.sourcePath})` : " (blank)";
|
|
89
|
+
return `- ${s.id}${activeMark}: "${s.name}" [${life}] -> ${s.path}${src}`;
|
|
90
|
+
});
|
|
91
|
+
return {
|
|
92
|
+
content: [
|
|
93
|
+
{
|
|
94
|
+
type: "text",
|
|
95
|
+
text: pick(lang, `共 ${list.length} 个沙盒项目${activeSbx ? `(当前处于沙盒 ${activeSbx.id})` : ""}:\n`, `Found ${list.length} sandbox project(s)${activeSbx ? ` (currently inside ${activeSbx.id})` : ""}:\n`) + lines.join("\n"),
|
|
96
|
+
},
|
|
97
|
+
],
|
|
98
|
+
details: {
|
|
99
|
+
action: "list",
|
|
100
|
+
count: list.length,
|
|
101
|
+
activeSandboxId: activeSbx?.id ?? null,
|
|
102
|
+
sandboxes: list.slice(0, 30),
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
case "create": {
|
|
107
|
+
const mode = params.mode ?? "import";
|
|
108
|
+
let sourcePath;
|
|
109
|
+
if (mode === "import") {
|
|
110
|
+
sourcePath = params.sourcePath?.trim() || activeSbx?.sourcePath || cwd;
|
|
111
|
+
}
|
|
112
|
+
const { sandbox, copiedFiles, skippedFiles } = await host.sandboxes.create({
|
|
113
|
+
name: params.name,
|
|
114
|
+
sourcePath,
|
|
115
|
+
ephemeral: params.ephemeral ?? true,
|
|
116
|
+
});
|
|
117
|
+
const shouldSwitch = params.switchTo === true;
|
|
118
|
+
if (shouldSwitch) {
|
|
119
|
+
await host.setCwd(sandbox.path);
|
|
120
|
+
}
|
|
121
|
+
await host.onSandboxesChanged();
|
|
122
|
+
const summaryZh = `🧪 已创建沙盒「${sandbox.name}」(ID: ${sandbox.id})\n` +
|
|
123
|
+
`- 沙盒路径: ${sandbox.path}\n` +
|
|
124
|
+
`- 模式: ${sandbox.sourcePath ? `从源项目导入(${sandbox.sourcePath},共复制 ${copiedFiles} 个文件)` : "空白沙盒"}\n` +
|
|
125
|
+
`- 生命周期: ${sandbox.ephemeral ? "临时沙盒" : "永久沙盒"}\n` +
|
|
126
|
+
`- 当前工作区: ${shouldSwitch ? `已切换到该沙盒 (${sandbox.path})` : `保持原工作区 (${cwd});你可将 cwd="${sandbox.path}" 传给 subagent 或调用 set_cwd`}`;
|
|
127
|
+
const summaryEn = `🧪 Created sandbox "${sandbox.name}" (ID: ${sandbox.id})\n` +
|
|
128
|
+
`- Path: ${sandbox.path}\n` +
|
|
129
|
+
`- Mode: ${sandbox.sourcePath ? `imported from "${sandbox.sourcePath}" (${copiedFiles} files copied)` : "blank sandbox"}\n` +
|
|
130
|
+
`- Lifecycle: ${sandbox.ephemeral ? "ephemeral" : "permanent"}\n` +
|
|
131
|
+
`- Active workspace: ${shouldSwitch ? `switched to sandbox (${sandbox.path})` : `unchanged (${cwd}); pass cwd="${sandbox.path}" to subagent or switch workspace`}`;
|
|
132
|
+
return {
|
|
133
|
+
content: [{ type: "text", text: pick(lang, summaryZh, summaryEn) }],
|
|
134
|
+
details: {
|
|
135
|
+
action: "create",
|
|
136
|
+
sandbox,
|
|
137
|
+
copiedFiles,
|
|
138
|
+
skippedFiles,
|
|
139
|
+
switched: shouldSwitch,
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
case "diff": {
|
|
144
|
+
const target = params.sandbox?.trim() || activeSbx?.id || cwd;
|
|
145
|
+
const diff = await host.sandboxes.diffWithSource(target);
|
|
146
|
+
const totalChanges = diff.added.length + diff.modified.length + diff.deleted.length;
|
|
147
|
+
const formatList = (label, items) => {
|
|
148
|
+
if (items.length === 0)
|
|
149
|
+
return "";
|
|
150
|
+
const shown = items
|
|
151
|
+
.slice(0, 50)
|
|
152
|
+
.map((f) => ` ${label} ${f}`)
|
|
153
|
+
.join("\n");
|
|
154
|
+
const more = items.length > 50 ? `\n ... (+${items.length - 50} more)` : "";
|
|
155
|
+
return `${shown}${more}\n`;
|
|
156
|
+
};
|
|
157
|
+
const headerZh = `📊 沙盒「${diff.sandbox.name}」(${diff.sandbox.id}) 变更对比:\n` +
|
|
158
|
+
(diff.sourcePath ? `- 源项目: ${diff.sourcePath}\n` : `- 类型: 空白沙盒\n`) +
|
|
159
|
+
`- 统计: 新增 ${diff.added.length} · 修改 ${diff.modified.length} · 删除 ${diff.deleted.length} · 未变 ${diff.unchangedCount}\n`;
|
|
160
|
+
const headerEn = `📊 Sandbox "${diff.sandbox.name}" (${diff.sandbox.id}) diff summary:\n` +
|
|
161
|
+
(diff.sourcePath ? `- Source: ${diff.sourcePath}\n` : `- Type: blank sandbox\n`) +
|
|
162
|
+
`- Stats: +${diff.added.length} added · ~${diff.modified.length} modified · -${diff.deleted.length} deleted · ${diff.unchangedCount} unchanged\n`;
|
|
163
|
+
const body = totalChanges === 0
|
|
164
|
+
? pick(lang, "(与源项目完全一致,无文件变更)", "(Identical to source project; no file changes)")
|
|
165
|
+
: `${formatList("+", diff.added)}${formatList("~", diff.modified)}${formatList("-", diff.deleted)}`.trimEnd();
|
|
166
|
+
return {
|
|
167
|
+
content: [{ type: "text", text: `${pick(lang, headerZh, headerEn)}\n${body}` }],
|
|
168
|
+
details: {
|
|
169
|
+
action: "diff",
|
|
170
|
+
sandboxId: diff.sandbox.id,
|
|
171
|
+
sourcePath: diff.sourcePath,
|
|
172
|
+
addedCount: diff.added.length,
|
|
173
|
+
modifiedCount: diff.modified.length,
|
|
174
|
+
deletedCount: diff.deleted.length,
|
|
175
|
+
unchangedCount: diff.unchangedCount,
|
|
176
|
+
added: diff.added.slice(0, MAX_DIFF_FILES_IN_DETAILS),
|
|
177
|
+
modified: diff.modified.slice(0, MAX_DIFF_FILES_IN_DETAILS),
|
|
178
|
+
deleted: diff.deleted.slice(0, MAX_DIFF_FILES_IN_DETAILS),
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
case "reset": {
|
|
183
|
+
const target = params.sandbox?.trim() || activeSbx?.id || cwd;
|
|
184
|
+
const { sandbox, copiedFiles } = await host.sandboxes.reset(target);
|
|
185
|
+
await host.onSandboxesChanged();
|
|
186
|
+
return {
|
|
187
|
+
content: [
|
|
188
|
+
{
|
|
189
|
+
type: "text",
|
|
190
|
+
text: pick(lang, `🔄 已重置沙盒「${sandbox.name}」(${sandbox.sourcePath ? `已从源项目重新同步 ${copiedFiles} 个文件` : "已清空恢复初始状态"})。`, `🔄 Reset sandbox "${sandbox.name}" (${sandbox.sourcePath ? `re-synced ${copiedFiles} files from source` : "cleared to initial blank state"}).`),
|
|
191
|
+
},
|
|
192
|
+
],
|
|
193
|
+
details: { action: "reset", sandbox, copiedFiles },
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
case "persist": {
|
|
197
|
+
const target = params.sandbox?.trim() || activeSbx?.id || cwd;
|
|
198
|
+
const sandbox = host.sandboxes.persist(target);
|
|
199
|
+
await host.onSandboxesChanged();
|
|
200
|
+
return {
|
|
201
|
+
content: [
|
|
202
|
+
{
|
|
203
|
+
type: "text",
|
|
204
|
+
text: pick(lang, `📌 沙盒「${sandbox.name}」(${sandbox.id})已转为永久沙盒。`, `📌 Sandbox "${sandbox.name}" (${sandbox.id}) is now permanent.`),
|
|
205
|
+
},
|
|
206
|
+
],
|
|
207
|
+
details: { action: "persist", sandbox },
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
case "promote": {
|
|
211
|
+
const targetPath = params.targetPath?.trim();
|
|
212
|
+
if (!targetPath) {
|
|
213
|
+
return {
|
|
214
|
+
content: [
|
|
215
|
+
{
|
|
216
|
+
type: "text",
|
|
217
|
+
text: pick(lang, "promote 操作需要提供 targetPath(目标正式项目路径)。", "promote requires a targetPath (destination directory path)."),
|
|
218
|
+
},
|
|
219
|
+
],
|
|
220
|
+
details: {},
|
|
221
|
+
isError: true,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
const target = params.sandbox?.trim() || activeSbx?.id || cwd;
|
|
225
|
+
const { sandbox, targetPath: resolvedTarget, copiedFiles, } = await host.sandboxes.promote(target, targetPath);
|
|
226
|
+
if (params.switchTo) {
|
|
227
|
+
await host.setCwd(resolvedTarget);
|
|
228
|
+
}
|
|
229
|
+
await host.onSandboxesChanged();
|
|
230
|
+
return {
|
|
231
|
+
content: [
|
|
232
|
+
{
|
|
233
|
+
type: "text",
|
|
234
|
+
text: pick(lang, `📤 已将沙盒「${sandbox.name}」导出为正式项目:${resolvedTarget}(共 ${copiedFiles} 个文件)。`, `📤 Exported sandbox "${sandbox.name}" to formal project: ${resolvedTarget} (${copiedFiles} files).`),
|
|
235
|
+
},
|
|
236
|
+
],
|
|
237
|
+
details: { action: "promote", sandbox, targetPath: resolvedTarget, copiedFiles },
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
case "apply_to_source": {
|
|
241
|
+
const target = params.sandbox?.trim() || activeSbx?.id || cwd;
|
|
242
|
+
const diff = await host.sandboxes.diffWithSource(target);
|
|
243
|
+
if (!diff.sourcePath) {
|
|
244
|
+
return {
|
|
245
|
+
content: [
|
|
246
|
+
{
|
|
247
|
+
type: "text",
|
|
248
|
+
text: pick(lang, `沙盒「${diff.sandbox.name}」是空白沙盒,没有关联的源项目,请改用 promote 导出到新目录。`, `Sandbox "${diff.sandbox.name}" is a blank sandbox with no sourcePath; use promote to export it to a new directory instead.`),
|
|
249
|
+
},
|
|
250
|
+
],
|
|
251
|
+
details: {},
|
|
252
|
+
isError: true,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
// 强制触发人工审批:写回源项目会修改真实工作区文件,绝不允许静默执行
|
|
256
|
+
if (host.askApproval) {
|
|
257
|
+
const reasonZh = `将沙盒「${diff.sandbox.name}」的改动(新增 ${diff.added.length}、修改 ${diff.modified.length} 个文件)写回源项目 ${diff.sourcePath}`;
|
|
258
|
+
const reasonEn = `Sync changes from sandbox "${diff.sandbox.name}" (+${diff.added.length} added, ~${diff.modified.length} modified) back to source project ${diff.sourcePath}`;
|
|
259
|
+
const decision = await host.askApproval(toolCallId, SANDBOX_TOOL_NAME, {
|
|
260
|
+
action: "apply_to_source",
|
|
261
|
+
sandbox: diff.sandbox.id,
|
|
262
|
+
sourcePath: diff.sourcePath,
|
|
263
|
+
added: diff.added.slice(0, 30),
|
|
264
|
+
modified: diff.modified.slice(0, 30),
|
|
265
|
+
}, reasonZh, reasonEn, host.getConversationId?.(), {
|
|
266
|
+
id: "sandbox.apply-to-source",
|
|
267
|
+
label: "沙盒修改写回源项目",
|
|
268
|
+
labelEn: "Apply sandbox changes to source",
|
|
269
|
+
});
|
|
270
|
+
if (decision.decision === "deny") {
|
|
271
|
+
return {
|
|
272
|
+
content: [
|
|
273
|
+
{
|
|
274
|
+
type: "text",
|
|
275
|
+
text: pick(lang, `【写回源项目被用户拒绝】${decision.reason ? `原因:${decision.reason}` : ""}`, `[Sync to source project denied by user]${decision.reason ? ` Reason: ${decision.reason}` : ""}`),
|
|
276
|
+
},
|
|
277
|
+
],
|
|
278
|
+
details: { guardDenied: true, userDenied: true },
|
|
279
|
+
isError: true,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
const res = await host.sandboxes.applyToSource(diff.sandbox.id);
|
|
284
|
+
await host.onSandboxesChanged();
|
|
285
|
+
return {
|
|
286
|
+
content: [
|
|
287
|
+
{
|
|
288
|
+
type: "text",
|
|
289
|
+
text: pick(lang, `✅ 已将沙盒「${res.sandbox.name}」的变更(共 ${res.copiedFiles} 个文件)同步写回源项目:${res.sourcePath}`, `✅ Synced changes from sandbox "${res.sandbox.name}" (${res.copiedFiles} files) back to source project: ${res.sourcePath}`),
|
|
290
|
+
},
|
|
291
|
+
],
|
|
292
|
+
details: {
|
|
293
|
+
action: "apply_to_source",
|
|
294
|
+
sandbox: res.sandbox,
|
|
295
|
+
sourcePath: res.sourcePath,
|
|
296
|
+
copiedFiles: res.copiedFiles,
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
case "destroy": {
|
|
301
|
+
const target = params.sandbox?.trim();
|
|
302
|
+
if (!target) {
|
|
303
|
+
return {
|
|
304
|
+
content: [
|
|
305
|
+
{
|
|
306
|
+
type: "text",
|
|
307
|
+
text: pick(lang, "destroy 操作需要显式指定要销毁的沙盒 ID 或路径(参数 sandbox)。", "destroy requires an explicit target sandbox ID or path (parameter 'sandbox')."),
|
|
308
|
+
},
|
|
309
|
+
],
|
|
310
|
+
details: {},
|
|
311
|
+
isError: true,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
const targetInfo = host.sandboxes.get(target) ?? host.sandboxes.getByCwd(target);
|
|
315
|
+
if (!targetInfo) {
|
|
316
|
+
return {
|
|
317
|
+
content: [
|
|
318
|
+
{
|
|
319
|
+
type: "text",
|
|
320
|
+
text: pick(lang, `未找到指定的沙盒:${target}`, `Sandbox not found: ${target}`),
|
|
321
|
+
},
|
|
322
|
+
],
|
|
323
|
+
details: {},
|
|
324
|
+
isError: true,
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
// 若当前工作区正好在该沙盒内,先切回源项目
|
|
328
|
+
if (activeSbx?.id === targetInfo.id && targetInfo.sourcePath) {
|
|
329
|
+
await host.setCwd(targetInfo.sourcePath);
|
|
330
|
+
}
|
|
331
|
+
const destroyed = await host.sandboxes.destroy(targetInfo.id);
|
|
332
|
+
await host.onSandboxesChanged();
|
|
333
|
+
return {
|
|
334
|
+
content: [
|
|
335
|
+
{
|
|
336
|
+
type: "text",
|
|
337
|
+
text: pick(lang, `🗑️ 已销毁沙盒「${destroyed?.name ?? target}」并清理其隔离目录。`, `🗑️ Destroyed sandbox "${destroyed?.name ?? target}" and removed its isolated directory.`),
|
|
338
|
+
},
|
|
339
|
+
],
|
|
340
|
+
details: { action: "destroy", destroyed },
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
default:
|
|
344
|
+
return {
|
|
345
|
+
content: [{ type: "text", text: `Unknown sandbox action: ${String(params.action)}` }],
|
|
346
|
+
details: {},
|
|
347
|
+
isError: true,
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
catch (err) {
|
|
352
|
+
return {
|
|
353
|
+
content: [
|
|
354
|
+
{
|
|
355
|
+
type: "text",
|
|
356
|
+
text: pick(lang, `沙盒操作失败:${err.message}`, `Sandbox operation failed: ${err.message}`),
|
|
357
|
+
},
|
|
358
|
+
],
|
|
359
|
+
details: {},
|
|
360
|
+
isError: true,
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
},
|
|
364
|
+
};
|
|
365
|
+
}
|
|
@@ -91,18 +91,23 @@ export async function discoverRecentProjectsFromDisk(sessionRoots, limit = 30) {
|
|
|
91
91
|
const jsonls = files.filter((f) => f.endsWith(".jsonl"));
|
|
92
92
|
if (jsonls.length === 0)
|
|
93
93
|
return null;
|
|
94
|
-
|
|
95
|
-
let newestMtime = 0;
|
|
96
|
-
for (const f of jsonls) {
|
|
94
|
+
const fileStats = await Promise.all(jsonls.map(async (f) => {
|
|
97
95
|
const fp = join(d.path, f);
|
|
98
96
|
try {
|
|
99
97
|
const st = await fsPromises.stat(fp);
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
98
|
+
return { fp, mtimeMs: st.mtimeMs };
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}));
|
|
104
|
+
let newestFile = "";
|
|
105
|
+
let newestMtime = 0;
|
|
106
|
+
for (const item of fileStats) {
|
|
107
|
+
if (item && item.mtimeMs > newestMtime) {
|
|
108
|
+
newestMtime = item.mtimeMs;
|
|
109
|
+
newestFile = item.fp;
|
|
104
110
|
}
|
|
105
|
-
catch { }
|
|
106
111
|
}
|
|
107
112
|
if (!newestFile)
|
|
108
113
|
return null;
|
|
@@ -158,9 +163,25 @@ export async function parseSessionInfoFast(filePath, fileMtime, withAllMessagesT
|
|
|
158
163
|
let lastActivityTime = 0;
|
|
159
164
|
let headerTime = 0;
|
|
160
165
|
const allMessages = withAllMessagesText ? [] : null;
|
|
166
|
+
let collectedChars = 0;
|
|
161
167
|
for (const line of lines) {
|
|
162
168
|
if (!line.trim())
|
|
163
169
|
continue;
|
|
170
|
+
// 列表路径(withAllMessagesText=false)已拿到 id 与首条消息后,跳过对大块长行
|
|
171
|
+
// (如数百 KB 的 toolResult/assistant 消息)的全量 JSON.parse:仅轻量计数并提取时间戳
|
|
172
|
+
if (!withAllMessagesText && id && firstMessage && line.length > 512 && !line.includes('"session_info"')) {
|
|
173
|
+
const head = line.slice(0, 140);
|
|
174
|
+
if (/"type"\s*:\s*"message"/.test(head)) {
|
|
175
|
+
messageCount++;
|
|
176
|
+
const tsMatch = /"timestamp"\s*:\s*(\d{10,16})/.exec(line);
|
|
177
|
+
if (tsMatch) {
|
|
178
|
+
const ts = Number(tsMatch[1]);
|
|
179
|
+
if (ts > lastActivityTime)
|
|
180
|
+
lastActivityTime = ts;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
164
185
|
let entry;
|
|
165
186
|
try {
|
|
166
187
|
entry = JSON.parse(line);
|
|
@@ -189,21 +210,27 @@ export async function parseSessionInfoFast(filePath, fileMtime, withAllMessagesT
|
|
|
189
210
|
if (msg) {
|
|
190
211
|
if (typeof msg.timestamp === "number")
|
|
191
212
|
lastActivityTime = Math.max(lastActivityTime, msg.timestamp);
|
|
192
|
-
|
|
193
|
-
if (
|
|
194
|
-
text =
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
213
|
+
const needText = (!firstMessage && msg.role === "user") || (allMessages !== null && collectedChars < MAX_SEARCH_TEXT_CHARS);
|
|
214
|
+
if (needText) {
|
|
215
|
+
let text = "";
|
|
216
|
+
if (typeof msg.content === "string") {
|
|
217
|
+
text = msg.content;
|
|
218
|
+
}
|
|
219
|
+
else if (Array.isArray(msg.content)) {
|
|
220
|
+
for (const part of msg.content) {
|
|
221
|
+
if (part && typeof part.text === "string") {
|
|
222
|
+
text += (text ? " " : "") + part.text;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (text) {
|
|
227
|
+
if (!firstMessage && msg.role === "user")
|
|
228
|
+
firstMessage = text;
|
|
229
|
+
if (allMessages && collectedChars < MAX_SEARCH_TEXT_CHARS) {
|
|
230
|
+
allMessages.push(text);
|
|
231
|
+
collectedChars += text.length + 1;
|
|
200
232
|
}
|
|
201
233
|
}
|
|
202
|
-
}
|
|
203
|
-
if (text) {
|
|
204
|
-
if (!firstMessage && msg.role === "user")
|
|
205
|
-
firstMessage = text;
|
|
206
|
-
allMessages?.push(text);
|
|
207
234
|
}
|
|
208
235
|
}
|
|
209
236
|
}
|
|
@@ -1,4 +1,28 @@
|
|
|
1
1
|
import { SUBAGENT_PROMPT_SNAPSHOT_CAP } from "./agent-formatters.js";
|
|
2
|
+
/** 子代理是否处于活跃(流式运行中或首轮投递启动中)状态。
|
|
3
|
+
* #589:名额与可见性解耦——仅活跃(非终态)子代理占用 MAX_SUBAGENTS 并发名额;
|
|
4
|
+
* 已结束(done / canceled / error)的子代理保留在左栏供用户审阅追溯,不再占名额。 */
|
|
5
|
+
export function isSubagentActive(conv) {
|
|
6
|
+
if (!conv.isSubagent)
|
|
7
|
+
return false;
|
|
8
|
+
if (conv.promptInFlight)
|
|
9
|
+
return true;
|
|
10
|
+
try {
|
|
11
|
+
return Boolean(conv.session.isStreaming);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** 统计当前占用并发名额的活跃子代理数(含正在创建 runtime 的在途 spawn,不含已终态子代理,#589)。 */
|
|
18
|
+
export function countActiveSubagents(convs, spawningCount = 0) {
|
|
19
|
+
let count = Math.max(0, Math.floor(spawningCount));
|
|
20
|
+
for (const c of convs) {
|
|
21
|
+
if (isSubagentActive(c))
|
|
22
|
+
count++;
|
|
23
|
+
}
|
|
24
|
+
return count;
|
|
25
|
+
}
|
|
2
26
|
/** 子代理最近一次运行的结局:最后一条 assistant 消息的 errorMessage / stopReason。
|
|
3
27
|
* 报错 > 中止 > 正常,三者互斥;无 assistant 消息时返回空。 */
|
|
4
28
|
export function subagentRunOutcome(conv) {
|
|
@@ -39,7 +63,14 @@ export function roleOutcomeOf(conv) {
|
|
|
39
63
|
}
|
|
40
64
|
/** 序列化对话为前端可消费的子代理快照 */
|
|
41
65
|
export function toSubagentSnapshot(conv) {
|
|
42
|
-
|
|
66
|
+
let sessionStreaming = false;
|
|
67
|
+
try {
|
|
68
|
+
sessionStreaming = Boolean(conv.session.isStreaming);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
sessionStreaming = false;
|
|
72
|
+
}
|
|
73
|
+
const streaming = Boolean(sessionStreaming || conv.promptInFlight);
|
|
43
74
|
const { error, canceled } = subagentRunOutcome(conv);
|
|
44
75
|
const state = streaming ? "running" : canceled ? "canceled" : "done";
|
|
45
76
|
let messageCount = 0;
|