pi-web-ui 0.100.0 → 0.102.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 +53 -1
- package/README.md +46 -44
- package/README.zh-CN.md +41 -39
- package/bin/pi-web-ui.mjs +17 -19
- package/dist/server/agent-formatters.js +191 -0
- package/dist/server/agent-service-daemon.js +1266 -0
- package/dist/server/agent-service.js +9884 -12815
- package/dist/server/approval-default-rules.js +158 -0
- package/dist/server/approval-rules.js +41 -167
- package/dist/server/ask-user-question-tool.js +104 -0
- package/dist/server/attachment-store.js +38 -1
- package/dist/server/bash-tool.js +83 -0
- package/dist/server/browser-page-tool.js +207 -0
- package/dist/server/client-id-utils.js +51 -0
- package/dist/server/client-state.js +38 -2
- package/dist/server/compaction-markers.js +34 -1
- package/dist/server/context-budget-runner.js +43 -0
- package/dist/server/dangling-tools.js +18 -5
- package/dist/server/delegate-mode.js +1 -0
- package/dist/server/dispatch-domain-handlers.js +631 -0
- package/dist/server/dispatch-plugin-handlers.js +300 -0
- package/dist/server/dsh/dsh-agent-service.js +30 -5
- package/dist/server/dsh/runtime/goal-rpc.mjs +5 -1
- package/dist/server/goal-evidence.js +9 -2
- package/dist/server/goal-service.js +1 -1
- package/dist/server/hashline-engine.js +1 -1
- package/dist/server/index.js +191 -969
- package/dist/server/left-panel-running.js +115 -0
- package/dist/server/lsp-tool.js +40 -12
- package/dist/server/markers-list-tool.js +34 -0
- package/dist/server/market-service.js +277 -0
- package/dist/server/mcp-bridge.js +137 -30
- package/dist/server/mcp-hot-reload.js +84 -27
- package/dist/server/mcp-manager.js +215 -0
- package/dist/server/message-entry-resolver.js +32 -0
- package/dist/server/orphan-manager.js +44 -0
- package/dist/server/persona-chain.js +63 -0
- package/dist/server/plan-mode.js +2 -0
- package/dist/server/plan-update-tool.js +96 -0
- package/dist/server/plugin-api-catalog.js +20 -0
- package/dist/server/plugin-facilities.js +13 -3
- package/dist/server/plugin-installer.js +6 -1
- package/dist/server/plugin-ui-slots.js +39 -0
- package/dist/server/plugins.js +72 -38
- package/dist/server/preset-fields.js +35 -0
- package/dist/server/preset-share.js +9 -0
- package/dist/server/process-utils.js +30 -0
- package/dist/server/prompt-assembler.js +80 -0
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/scm-commitmsg.js +74 -0
- package/dist/server/serialize.js +97 -7
- package/dist/server/session-branch.js +367 -0
- package/dist/server/session-parser.js +235 -0
- package/dist/server/session-search.js +158 -0
- package/dist/server/settings-service.js +28 -1
- package/dist/server/skill-content-filler.js +33 -0
- package/dist/server/subagent-handoff.js +18 -0
- package/dist/server/subagent-helpers.js +88 -0
- package/dist/server/terminals.js +155 -58
- package/dist/server/tool-guards.js +527 -0
- package/dist/server/tool-host-factories.js +217 -0
- package/dist/server/tool-manager.js +30 -5
- package/dist/server/wait-subscription-scan.js +46 -1
- package/dist/server/workspace-snapshot.js +18 -0
- package/extensions/webui.ts +90 -24
- package/package.json +1 -1
- package/plugin-sdk/README.md +17 -0
- package/plugin-sdk/index.d.ts +17 -0
- package/plugin-sdk/index.mjs +5 -0
- package/plugins/catalog.json +20 -0
- package/themes/aetheris.css +3 -1
- package/themes/translucent.css +8 -1
- package/themes/transparent.css +9 -1
- package/themes/zhupi-dark.css +3 -14
- package/themes/zhupi.css +2 -16
- package/web/dist/assets/{TerminalPanel-C3I1Mc-C.js → TerminalPanel-CYGOzbPT.js} +1 -1
- package/web/dist/assets/index-CP71PHb6.js +680 -0
- package/web/dist/assets/index-ytRwL6j0.css +1 -0
- package/web/dist/index.html +2 -2
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/index-B-E2EM_0.css +0 -1
- package/web/dist/assets/index-BYumAaGi.js +0 -379
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 内置默认审批规则(与 server/tool-approval.ts 既有内置检测表保持 100% 同口径与同一 id)。
|
|
3
|
+
*/
|
|
4
|
+
export const DEFAULT_APPROVAL_RULES = [
|
|
5
|
+
{
|
|
6
|
+
id: "builtin.bash.rm-rf",
|
|
7
|
+
enabled: true,
|
|
8
|
+
tools: ["bash"],
|
|
9
|
+
field: "command",
|
|
10
|
+
match: "regex",
|
|
11
|
+
value: "\\brm\\s+((-[a-zA-Z0-9]*[rf][a-zA-Z0-9]*|--recursive|--force)\\s+)+(((\\/)|(~)|(\\.\\.)|(\\*)|(\\.\\/))|[a-zA-Z]:[\\\\/])",
|
|
12
|
+
action: "ask",
|
|
13
|
+
label: "递归/强制删除 (rm -rf)",
|
|
14
|
+
labelEn: "Recursive/force delete (rm -rf)",
|
|
15
|
+
reason: "检测到高风险的递归/强制删除大范围路径命令 (rm -rf)",
|
|
16
|
+
reasonEn: "Detected high-risk recursive/force deletion of wide paths (rm -rf)",
|
|
17
|
+
categoryId: "bash.rm-rf",
|
|
18
|
+
builtin: true,
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
id: "builtin.bash.win-del",
|
|
22
|
+
enabled: true,
|
|
23
|
+
tools: ["bash"],
|
|
24
|
+
field: "command",
|
|
25
|
+
match: "regex",
|
|
26
|
+
value: "\\b(del|rmdir|rd)\\s+[/\\-][fsq]",
|
|
27
|
+
action: "ask",
|
|
28
|
+
label: "Windows 强制删除 (del/rmdir/rd)",
|
|
29
|
+
labelEn: "Windows force delete (del/rmdir/rd)",
|
|
30
|
+
reason: "检测到高风险的 Windows 强制/递归删除目录命令 (del/rmdir/rd /s /q)",
|
|
31
|
+
reasonEn: "Detected high-risk Windows force/recursive deletion command (del/rmdir/rd /s /q)",
|
|
32
|
+
categoryId: "bash.win-del",
|
|
33
|
+
builtin: true,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
id: "builtin.bash.disk",
|
|
37
|
+
enabled: true,
|
|
38
|
+
tools: ["bash"],
|
|
39
|
+
field: "command",
|
|
40
|
+
match: "regex",
|
|
41
|
+
value: "\\b(mkfs|format\\s+[a-zA-Z]:|dd\\s+if=)",
|
|
42
|
+
action: "ask",
|
|
43
|
+
label: "磁盘格式化/底层写入",
|
|
44
|
+
labelEn: "Disk format / raw block write",
|
|
45
|
+
reason: "检测到磁盘格式化或底层块写入危险命令 (format/mkfs/dd)",
|
|
46
|
+
reasonEn: "Detected dangerous disk formatting or raw block write command (format/mkfs/dd)",
|
|
47
|
+
categoryId: "bash.disk",
|
|
48
|
+
builtin: true,
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
id: "builtin.bash.git-destructive",
|
|
52
|
+
enabled: true,
|
|
53
|
+
tools: ["bash"],
|
|
54
|
+
field: "command",
|
|
55
|
+
match: "regex",
|
|
56
|
+
// git 全局选项前缀(-C <path> / -c k=v / --git-dir=…)、git.exe;push 强推 / +refspec / :branch 删远端;
|
|
57
|
+
// checkout . / restore . / stash drop|clear 丢弃未提交修改(restore --staged 只取消暂存,排除)。
|
|
58
|
+
// push 的参数区不跨 ; & | 换行,避免把后续命令里的 -f(如 `rm -f`)算进来(#578)。
|
|
59
|
+
value: "\\bgit(?:\\.exe)?(?:\\s+(?:-[cC]\\s+\\S+|--[a-zA-Z-]+=\\S+))*\\s+(?:push\\s+(?:[^;&|\\n]*?\\s)?(?:--force|-[a-zA-Z0-9]*f)\\b|push\\s+(?:[^;&|\\n]*?\\s)?(?:\\+\\S+|:[^\\s/]\\S*)|reset\\s+--hard|clean\\s+(?:-[a-zA-Z]+\\s+)*-[a-zA-Z0-9]*f[a-zA-Z0-9]*|branch\\s+-[dD]\\b|checkout\\s+(?:--\\s+)?\\.(?=\\s|$|[;&|])|restore\\s+(?:--(?!staged\\b)\\S+\\s+)*\\.(?=\\s|$|[;&|])|stash\\s+(?:drop|clear)\\b)",
|
|
60
|
+
action: "ask",
|
|
61
|
+
label: "破坏性 Git 操作",
|
|
62
|
+
labelEn: "Destructive git operation",
|
|
63
|
+
reason: "检测到不可逆的破坏性 Git 操作 (force push / reset --hard / clean -f / checkout . / stash drop|clear / branch -D)",
|
|
64
|
+
reasonEn: "Detected irreversible destructive Git operation (force push / reset --hard / clean -f / checkout . / stash drop|clear / branch -D)",
|
|
65
|
+
categoryId: "bash.git-destructive",
|
|
66
|
+
builtin: true,
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
id: "builtin.bash.chmod",
|
|
70
|
+
enabled: true,
|
|
71
|
+
tools: ["bash"],
|
|
72
|
+
field: "command",
|
|
73
|
+
match: "regex",
|
|
74
|
+
value: "\\bchmod\\s+(-R\\s+)?(777|000)\\b",
|
|
75
|
+
action: "ask",
|
|
76
|
+
label: "危险权限修改 (chmod)",
|
|
77
|
+
labelEn: "Dangerous permission change (chmod)",
|
|
78
|
+
reason: "检测到过度开放或全局破坏性的文件权限修改 (chmod 777/000)",
|
|
79
|
+
reasonEn: "Detected overly permissive or globally destructive file permission change (chmod 777/000)",
|
|
80
|
+
categoryId: "bash.chmod",
|
|
81
|
+
builtin: true,
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
id: "builtin.bash.system-redirect",
|
|
85
|
+
enabled: true,
|
|
86
|
+
tools: ["bash"],
|
|
87
|
+
field: "command",
|
|
88
|
+
match: "regex",
|
|
89
|
+
value: ">\\s*(\\/etc\\/|\\/boot\\/|C:\\\\Windows)",
|
|
90
|
+
action: "ask",
|
|
91
|
+
label: "写入系统关键目录",
|
|
92
|
+
labelEn: "Write into system directories",
|
|
93
|
+
reason: "检测到重定向写入系统关键目录的危险操作",
|
|
94
|
+
reasonEn: "Detected dangerous redirection writing into critical system directories",
|
|
95
|
+
categoryId: "bash.system-redirect",
|
|
96
|
+
builtin: true,
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
id: "builtin.file.sensitive.env",
|
|
100
|
+
enabled: true,
|
|
101
|
+
tools: ["write", "edit", "edit_soft"],
|
|
102
|
+
field: "path",
|
|
103
|
+
match: "regex",
|
|
104
|
+
value: "(^|[\\/\\\\])\\.env(\\.[a-zA-Z0-9_-]+)?$",
|
|
105
|
+
action: "ask",
|
|
106
|
+
label: "敏感配置 (.env)",
|
|
107
|
+
labelEn: "Sensitive config (.env)",
|
|
108
|
+
reason: "尝试修改敏感环境变量/密钥配置文件 (.env)",
|
|
109
|
+
reasonEn: "Attempting to modify sensitive environment/secret configuration (.env)",
|
|
110
|
+
categoryId: "file.sensitive.env",
|
|
111
|
+
builtin: true,
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
id: "builtin.file.sensitive.ssh",
|
|
115
|
+
enabled: true,
|
|
116
|
+
tools: ["write", "edit", "edit_soft"],
|
|
117
|
+
field: "path",
|
|
118
|
+
match: "regex",
|
|
119
|
+
value: "(^|[\\/\\\\])(id_rsa|id_ed25519|authorized_keys|known_hosts)$",
|
|
120
|
+
action: "ask",
|
|
121
|
+
label: "SSH 密钥/凭据",
|
|
122
|
+
labelEn: "SSH keys / credentials",
|
|
123
|
+
reason: "尝试修改 SSH 密钥或认证凭据文件",
|
|
124
|
+
reasonEn: "Attempting to modify SSH keys or authentication credentials",
|
|
125
|
+
categoryId: "file.sensitive.ssh",
|
|
126
|
+
builtin: true,
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
id: "builtin.file.sensitive.shell",
|
|
130
|
+
enabled: true,
|
|
131
|
+
tools: ["write", "edit", "edit_soft"],
|
|
132
|
+
field: "path",
|
|
133
|
+
match: "regex",
|
|
134
|
+
value: "(^|[\\/\\\\])(\\.bashrc|\\.zshrc|\\.profile|\\.bash_profile)$",
|
|
135
|
+
action: "ask",
|
|
136
|
+
label: "Shell 启动配置",
|
|
137
|
+
labelEn: "Shell profile",
|
|
138
|
+
reason: "尝试修改用户全局 Shell 启动配置文件",
|
|
139
|
+
reasonEn: "Attempting to modify user global Shell profile configuration",
|
|
140
|
+
categoryId: "file.sensitive.shell",
|
|
141
|
+
builtin: true,
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
id: "builtin.file.outside-workspace",
|
|
145
|
+
enabled: true,
|
|
146
|
+
tools: ["write", "edit", "edit_soft"],
|
|
147
|
+
field: "path",
|
|
148
|
+
match: "outside_workspace",
|
|
149
|
+
value: "",
|
|
150
|
+
action: "ask",
|
|
151
|
+
label: "工作区外写入",
|
|
152
|
+
labelEn: "Write outside workspace",
|
|
153
|
+
reason: "尝试在工作区外部写入/修改文件",
|
|
154
|
+
reasonEn: "Attempting to write/modify file outside the workspace",
|
|
155
|
+
categoryId: "file.outside-workspace",
|
|
156
|
+
builtin: true,
|
|
157
|
+
},
|
|
158
|
+
];
|
|
@@ -32,6 +32,8 @@
|
|
|
32
32
|
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
33
33
|
import { resolve, sep } from "node:path";
|
|
34
34
|
import { writeJsonAtomicSync } from "./atomic-file.js";
|
|
35
|
+
import { DEFAULT_APPROVAL_RULES } from "./approval-default-rules.js";
|
|
36
|
+
export { DEFAULT_APPROVAL_RULES };
|
|
35
37
|
/**
|
|
36
38
|
* 跨平台判定 target 路径是否严格位于 root 目录内部或就是 root 本身。
|
|
37
39
|
* 针对 Windows 盘符大小写不敏感及正反斜杠统一进行规范化,防止 .. 路径穿越逃逸。
|
|
@@ -125,6 +127,38 @@ export function extractRuleFieldValue(field, toolName, params) {
|
|
|
125
127
|
* 单条规则针对具体工具调用的匹配详情判定(纯函数,issue #566)。
|
|
126
128
|
* 匹配成功时返回匹配区间信息 { index, length, text },否则返回 null。
|
|
127
129
|
*/
|
|
130
|
+
/** 单条规则在一段字段文本里的全部命中区间(issue #578)。regex 规则逐次扫描、不只取首个。 */
|
|
131
|
+
export function matchApprovalRuleDetails(rule, toolName, params, cwd, workspaceRoots = []) {
|
|
132
|
+
if (!rule.enabled)
|
|
133
|
+
return [];
|
|
134
|
+
const targetTools = rule.tools.map((t) => t.trim().toLowerCase());
|
|
135
|
+
const toolMatches = targetTools.includes("*") || targetTools.includes(toolName.trim().toLowerCase());
|
|
136
|
+
if (!toolMatches)
|
|
137
|
+
return [];
|
|
138
|
+
if (rule.match === "regex") {
|
|
139
|
+
const fieldValue = extractRuleFieldValue(rule.field, toolName, params);
|
|
140
|
+
if (!fieldValue)
|
|
141
|
+
return [];
|
|
142
|
+
let re;
|
|
143
|
+
try {
|
|
144
|
+
re = new RegExp(rule.value, "gi");
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
return [];
|
|
148
|
+
}
|
|
149
|
+
const out = [];
|
|
150
|
+
for (let m = re.exec(fieldValue); m; m = re.exec(fieldValue)) {
|
|
151
|
+
if (m[0].length === 0) {
|
|
152
|
+
re.lastIndex++; // 空匹配推进一位,防死循环
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
out.push({ index: m.index, length: m[0].length, text: m[0] });
|
|
156
|
+
}
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
const single = matchApprovalRuleDetail(rule, toolName, params, cwd, workspaceRoots);
|
|
160
|
+
return single ? [single] : [];
|
|
161
|
+
}
|
|
128
162
|
export function matchApprovalRuleDetail(rule, toolName, params, cwd, workspaceRoots = []) {
|
|
129
163
|
if (!rule.enabled)
|
|
130
164
|
return null;
|
|
@@ -221,21 +255,19 @@ export function evaluateApprovalRules(rules, toolName, params, cwd, workspaceRoo
|
|
|
221
255
|
let primary = null;
|
|
222
256
|
const hits = [];
|
|
223
257
|
for (const rule of rules) {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const catId = rule.categoryId || rule.id;
|
|
227
|
-
const category = {
|
|
228
|
-
id: catId,
|
|
229
|
-
label: rule.label,
|
|
230
|
-
labelEn: rule.labelEn || rule.label,
|
|
231
|
-
};
|
|
258
|
+
// 同一规则可在一段命令里出现多次(如 `git reset --hard && git clean -fd`),全部列出(#578)。
|
|
259
|
+
for (const detail of matchApprovalRuleDetails(rule, toolName, params, cwd, workspaceRoots)) {
|
|
232
260
|
if (!primary) {
|
|
233
261
|
primary = {
|
|
234
262
|
action: rule.action,
|
|
235
263
|
matchedRule: rule,
|
|
236
264
|
reason: rule.reason || rule.label,
|
|
237
265
|
reasonEn: rule.reasonEn || rule.labelEn || rule.label,
|
|
238
|
-
category
|
|
266
|
+
category: {
|
|
267
|
+
id: rule.categoryId || rule.id,
|
|
268
|
+
label: rule.label,
|
|
269
|
+
labelEn: rule.labelEn || rule.label,
|
|
270
|
+
},
|
|
239
271
|
};
|
|
240
272
|
}
|
|
241
273
|
if (hits.length < 10) {
|
|
@@ -261,170 +293,12 @@ export function evaluateApprovalRules(rules, toolName, params, cwd, workspaceRoo
|
|
|
261
293
|
}
|
|
262
294
|
return { action: "none" };
|
|
263
295
|
}
|
|
264
|
-
/**
|
|
265
|
-
* 纯命令文本的高危规则快速检测(用于对话卡片终端行即时高亮与审查证据打标,issue #566)。
|
|
266
|
-
*/
|
|
267
296
|
export function checkBashCommandDanger(command, rules = DEFAULT_APPROVAL_RULES) {
|
|
268
297
|
if (!command || typeof command !== "string")
|
|
269
298
|
return [];
|
|
270
299
|
const res = evaluateApprovalRules(rules, "bash", { command }, "");
|
|
271
300
|
return res.hits ?? [];
|
|
272
301
|
}
|
|
273
|
-
/**
|
|
274
|
-
* 内置默认审批规则(与 server/tool-approval.ts 既有内置检测表保持 100% 同口径与同一 id)。
|
|
275
|
-
*/
|
|
276
|
-
export const DEFAULT_APPROVAL_RULES = [
|
|
277
|
-
{
|
|
278
|
-
id: "builtin.bash.rm-rf",
|
|
279
|
-
enabled: true,
|
|
280
|
-
tools: ["bash"],
|
|
281
|
-
field: "command",
|
|
282
|
-
match: "regex",
|
|
283
|
-
value: "\\brm\\s+((-[a-zA-Z0-9]*[rf][a-zA-Z0-9]*|--recursive|--force)\\s+)+(((\\/)|(~)|(\\.\\.)|(\\*)|(\\.\\/))|[a-zA-Z]:[\\\\/])",
|
|
284
|
-
action: "ask",
|
|
285
|
-
label: "递归/强制删除 (rm -rf)",
|
|
286
|
-
labelEn: "Recursive/force delete (rm -rf)",
|
|
287
|
-
reason: "检测到高风险的递归/强制删除大范围路径命令 (rm -rf)",
|
|
288
|
-
reasonEn: "Detected high-risk recursive/force deletion of wide paths (rm -rf)",
|
|
289
|
-
categoryId: "bash.rm-rf",
|
|
290
|
-
builtin: true,
|
|
291
|
-
},
|
|
292
|
-
{
|
|
293
|
-
id: "builtin.bash.win-del",
|
|
294
|
-
enabled: true,
|
|
295
|
-
tools: ["bash"],
|
|
296
|
-
field: "command",
|
|
297
|
-
match: "regex",
|
|
298
|
-
value: "\\b(del|rmdir|rd)\\s+[/\\-][fsq]",
|
|
299
|
-
action: "ask",
|
|
300
|
-
label: "Windows 强制删除 (del/rmdir/rd)",
|
|
301
|
-
labelEn: "Windows force delete (del/rmdir/rd)",
|
|
302
|
-
reason: "检测到高风险的 Windows 强制/递归删除目录命令 (del/rmdir/rd /s /q)",
|
|
303
|
-
reasonEn: "Detected high-risk Windows force/recursive deletion command (del/rmdir/rd /s /q)",
|
|
304
|
-
categoryId: "bash.win-del",
|
|
305
|
-
builtin: true,
|
|
306
|
-
},
|
|
307
|
-
{
|
|
308
|
-
id: "builtin.bash.disk",
|
|
309
|
-
enabled: true,
|
|
310
|
-
tools: ["bash"],
|
|
311
|
-
field: "command",
|
|
312
|
-
match: "regex",
|
|
313
|
-
value: "\\b(mkfs|format\\s+[a-zA-Z]:|dd\\s+if=)",
|
|
314
|
-
action: "ask",
|
|
315
|
-
label: "磁盘格式化/底层写入",
|
|
316
|
-
labelEn: "Disk format / raw block write",
|
|
317
|
-
reason: "检测到磁盘格式化或底层块写入危险命令 (format/mkfs/dd)",
|
|
318
|
-
reasonEn: "Detected dangerous disk formatting or raw block write command (format/mkfs/dd)",
|
|
319
|
-
categoryId: "bash.disk",
|
|
320
|
-
builtin: true,
|
|
321
|
-
},
|
|
322
|
-
{
|
|
323
|
-
id: "builtin.bash.git-destructive",
|
|
324
|
-
enabled: true,
|
|
325
|
-
tools: ["bash"],
|
|
326
|
-
field: "command",
|
|
327
|
-
match: "regex",
|
|
328
|
-
value: "\\bgit\\s+(push\\s+.*?(--force|-[a-zA-Z0-9]*f)\\b|reset\\s+--hard|clean\\s+-[a-zA-Z0-9]*f|branch\\s+-[dD]\\b)",
|
|
329
|
-
action: "ask",
|
|
330
|
-
label: "破坏性 Git 操作",
|
|
331
|
-
labelEn: "Destructive git operation",
|
|
332
|
-
reason: "检测到不可逆的破坏性 Git 操作 (force push / reset --hard / clean -f)",
|
|
333
|
-
reasonEn: "Detected irreversible destructive Git operation (force push / reset --hard / clean -f)",
|
|
334
|
-
categoryId: "bash.git-destructive",
|
|
335
|
-
builtin: true,
|
|
336
|
-
},
|
|
337
|
-
{
|
|
338
|
-
id: "builtin.bash.chmod",
|
|
339
|
-
enabled: true,
|
|
340
|
-
tools: ["bash"],
|
|
341
|
-
field: "command",
|
|
342
|
-
match: "regex",
|
|
343
|
-
value: "\\bchmod\\s+(-R\\s+)?(777|000)\\b",
|
|
344
|
-
action: "ask",
|
|
345
|
-
label: "危险权限修改 (chmod)",
|
|
346
|
-
labelEn: "Dangerous permission change (chmod)",
|
|
347
|
-
reason: "检测到过度开放或全局破坏性的文件权限修改 (chmod 777/000)",
|
|
348
|
-
reasonEn: "Detected overly permissive or globally destructive file permission change (chmod 777/000)",
|
|
349
|
-
categoryId: "bash.chmod",
|
|
350
|
-
builtin: true,
|
|
351
|
-
},
|
|
352
|
-
{
|
|
353
|
-
id: "builtin.bash.system-redirect",
|
|
354
|
-
enabled: true,
|
|
355
|
-
tools: ["bash"],
|
|
356
|
-
field: "command",
|
|
357
|
-
match: "regex",
|
|
358
|
-
value: ">\\s*(\\/etc\\/|\\/boot\\/|C:\\\\Windows)",
|
|
359
|
-
action: "ask",
|
|
360
|
-
label: "写入系统关键目录",
|
|
361
|
-
labelEn: "Write into system directories",
|
|
362
|
-
reason: "检测到重定向写入系统关键目录的危险操作",
|
|
363
|
-
reasonEn: "Detected dangerous redirection writing into critical system directories",
|
|
364
|
-
categoryId: "bash.system-redirect",
|
|
365
|
-
builtin: true,
|
|
366
|
-
},
|
|
367
|
-
{
|
|
368
|
-
id: "builtin.file.sensitive.env",
|
|
369
|
-
enabled: true,
|
|
370
|
-
tools: ["write", "edit", "edit_soft"],
|
|
371
|
-
field: "path",
|
|
372
|
-
match: "regex",
|
|
373
|
-
value: "(^|[\\/\\\\])\\.env(\\.[a-zA-Z0-9_-]+)?$",
|
|
374
|
-
action: "ask",
|
|
375
|
-
label: "敏感配置 (.env)",
|
|
376
|
-
labelEn: "Sensitive config (.env)",
|
|
377
|
-
reason: "尝试修改敏感环境变量/密钥配置文件 (.env)",
|
|
378
|
-
reasonEn: "Attempting to modify sensitive environment/secret configuration (.env)",
|
|
379
|
-
categoryId: "file.sensitive.env",
|
|
380
|
-
builtin: true,
|
|
381
|
-
},
|
|
382
|
-
{
|
|
383
|
-
id: "builtin.file.sensitive.ssh",
|
|
384
|
-
enabled: true,
|
|
385
|
-
tools: ["write", "edit", "edit_soft"],
|
|
386
|
-
field: "path",
|
|
387
|
-
match: "regex",
|
|
388
|
-
value: "(^|[\\/\\\\])(id_rsa|id_ed25519|authorized_keys|known_hosts)$",
|
|
389
|
-
action: "ask",
|
|
390
|
-
label: "SSH 密钥/凭据",
|
|
391
|
-
labelEn: "SSH keys / credentials",
|
|
392
|
-
reason: "尝试修改 SSH 密钥或认证凭据文件",
|
|
393
|
-
reasonEn: "Attempting to modify SSH keys or authentication credentials",
|
|
394
|
-
categoryId: "file.sensitive.ssh",
|
|
395
|
-
builtin: true,
|
|
396
|
-
},
|
|
397
|
-
{
|
|
398
|
-
id: "builtin.file.sensitive.shell",
|
|
399
|
-
enabled: true,
|
|
400
|
-
tools: ["write", "edit", "edit_soft"],
|
|
401
|
-
field: "path",
|
|
402
|
-
match: "regex",
|
|
403
|
-
value: "(^|[\\/\\\\])(\\.bashrc|\\.zshrc|\\.profile|\\.bash_profile)$",
|
|
404
|
-
action: "ask",
|
|
405
|
-
label: "Shell 启动配置",
|
|
406
|
-
labelEn: "Shell profile",
|
|
407
|
-
reason: "尝试修改用户全局 Shell 启动配置文件",
|
|
408
|
-
reasonEn: "Attempting to modify user global Shell profile configuration",
|
|
409
|
-
categoryId: "file.sensitive.shell",
|
|
410
|
-
builtin: true,
|
|
411
|
-
},
|
|
412
|
-
{
|
|
413
|
-
id: "builtin.file.outside-workspace",
|
|
414
|
-
enabled: true,
|
|
415
|
-
tools: ["write", "edit", "edit_soft"],
|
|
416
|
-
field: "path",
|
|
417
|
-
match: "outside_workspace",
|
|
418
|
-
value: "",
|
|
419
|
-
action: "ask",
|
|
420
|
-
label: "工作区外写入",
|
|
421
|
-
labelEn: "Write outside workspace",
|
|
422
|
-
reason: "尝试在工作区外部写入/修改文件",
|
|
423
|
-
reasonEn: "Attempting to write/modify file outside the workspace",
|
|
424
|
-
categoryId: "file.outside-workspace",
|
|
425
|
-
builtin: true,
|
|
426
|
-
},
|
|
427
|
-
];
|
|
428
302
|
/** 归一化输入规则。脏数据或非法规则返回 null。 */
|
|
429
303
|
export function normalizeApprovalRule(raw) {
|
|
430
304
|
if (!raw || typeof raw !== "object")
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ask_user_question customTool:人机协作提问问卷工具。
|
|
3
|
+
*
|
|
4
|
+
* 模型需要澄清歧义需求、做重大技术路线决策时调用,向客户端弹出结构化问卷卡片。
|
|
5
|
+
* 从 agent-service.ts 抽出为独立模块。
|
|
6
|
+
*/
|
|
7
|
+
import { Type } from "typebox";
|
|
8
|
+
/**
|
|
9
|
+
* askUser 签名带 {aborted} 快照而非完整 AbortSignal:customTool 的 execute 信号
|
|
10
|
+
* 服务于整个 agent 生命周期,这里按「已中止即拒绝」的最小语义处理,避免与其它
|
|
11
|
+
* 工具的取消逻辑纠缠。
|
|
12
|
+
*/
|
|
13
|
+
export function makeAskUserQuestionTool(clientSession,
|
|
14
|
+
/** 本 runtime 所属会话:提问跟着对话走,快照只把当前对话的问卷推给客户端。 */
|
|
15
|
+
ownerId) {
|
|
16
|
+
const QuestionOptionSchema = Type.Object({
|
|
17
|
+
label: Type.String({ description: "Display label (1-5 words)" }),
|
|
18
|
+
description: Type.Optional(Type.String({
|
|
19
|
+
description: "Short explanation of impact/tradeoff",
|
|
20
|
+
})),
|
|
21
|
+
preview: Type.Optional(Type.String({
|
|
22
|
+
description: "Optional preview (markdown/HTML/code)",
|
|
23
|
+
})),
|
|
24
|
+
recommended: Type.Optional(Type.Boolean({
|
|
25
|
+
description: "Highlight as recommended option",
|
|
26
|
+
})),
|
|
27
|
+
});
|
|
28
|
+
const QuestionSchema = Type.Object({
|
|
29
|
+
id: Type.String({ description: "Unique identifier (snake_case)" }),
|
|
30
|
+
question: Type.String({ description: "Question text (markdown/HTML ok)" }),
|
|
31
|
+
detail: Type.Optional(Type.String({ description: "Optional context under question" })),
|
|
32
|
+
header: Type.Optional(Type.String({ description: "Optional short header" })),
|
|
33
|
+
options: Type.Optional(Type.Array(QuestionOptionSchema, {
|
|
34
|
+
description: "2-4 choices (recommended option first)",
|
|
35
|
+
minItems: 2,
|
|
36
|
+
maxItems: 4,
|
|
37
|
+
})),
|
|
38
|
+
multiSelect: Type.Optional(Type.Boolean({ description: "Allow multiple choices (default: false)" })),
|
|
39
|
+
dependsOn: Type.Optional(Type.Object({
|
|
40
|
+
questionId: Type.String({ description: "Prior question ID" }),
|
|
41
|
+
value: Type.Optional(Type.Union([Type.String(), Type.Array(Type.String())], {
|
|
42
|
+
description: "Show when prior answer matches",
|
|
43
|
+
})),
|
|
44
|
+
})),
|
|
45
|
+
optionsMap: Type.Optional(Type.Record(Type.String(), Type.Array(QuestionOptionSchema), {
|
|
46
|
+
description: "Dynamic options keyed by prior answer",
|
|
47
|
+
})),
|
|
48
|
+
});
|
|
49
|
+
return {
|
|
50
|
+
name: "ask_user_question",
|
|
51
|
+
label: "Ask the user",
|
|
52
|
+
description: "Ask the user 1-3 focused questions to clarify requirements, confirm decisions, or choose options. " +
|
|
53
|
+
"Renders a rich browser dialog; resumes on submit or cancel.",
|
|
54
|
+
promptSnippet: "clarify ambiguous requirements or confirm a decision with the user",
|
|
55
|
+
promptGuidelines: [
|
|
56
|
+
"When requirements are ambiguous, clarify with ask_user_question: 1 to 3 focused questions (prefer 1), 2-4 choices with the recommended option first, each with a concise impact/tradeoff",
|
|
57
|
+
"A cancelled question comes back as a tool error — respect it without immediately re-asking",
|
|
58
|
+
],
|
|
59
|
+
parameters: Type.Object({
|
|
60
|
+
questions: Type.Array(QuestionSchema, {
|
|
61
|
+
description: "Questions to ask (prefer 1).",
|
|
62
|
+
minItems: 1,
|
|
63
|
+
maxItems: 3,
|
|
64
|
+
}),
|
|
65
|
+
}),
|
|
66
|
+
execute: async (_id, params, signal) => {
|
|
67
|
+
const qs = params.questions;
|
|
68
|
+
if (!Array.isArray(qs) || qs.length === 0) {
|
|
69
|
+
throw new Error("ask_user_question requires at least one question");
|
|
70
|
+
}
|
|
71
|
+
if (qs.length > 3) {
|
|
72
|
+
throw new Error("ask_user_question allows at most 3 questions per call to prevent question fatigue (单次提问最多不得超过 3 个问题)");
|
|
73
|
+
}
|
|
74
|
+
const res = await clientSession.askUser(qs, {
|
|
75
|
+
aborted: signal?.aborted,
|
|
76
|
+
}, ownerId);
|
|
77
|
+
if (res === null) {
|
|
78
|
+
throw new Error("User cancelled the question.\n用户取消了提问。");
|
|
79
|
+
}
|
|
80
|
+
if (!Array.isArray(res) && res.cancelled) {
|
|
81
|
+
const cancelReason = res.reason?.trim();
|
|
82
|
+
const note = cancelReason ? `\nUser note / 附言: ${cancelReason}` : "";
|
|
83
|
+
const noteZh = cancelReason ? `\n附言:${cancelReason}` : "";
|
|
84
|
+
throw new Error(`User cancelled the question.${note}\n用户取消了提问。${noteZh}`);
|
|
85
|
+
}
|
|
86
|
+
const answers = res;
|
|
87
|
+
// 工具结果:把每道题的回答拼成简洁文本给模型,同时留 details 供 UI 展示。
|
|
88
|
+
const lines = answers.map((a) => {
|
|
89
|
+
const q = qs.find((q) => q.id === a.id);
|
|
90
|
+
const label = a.selected.join(", ");
|
|
91
|
+
const custom = a.custom?.trim() ? ` (wrote: ${a.custom.trim()})` : "";
|
|
92
|
+
return `${q?.header ?? q?.id ?? a.id}: ${label || "(no selection)"}${custom}`;
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
95
|
+
content: [{ type: "text", text: lines.join("\n") }],
|
|
96
|
+
details: { answers },
|
|
97
|
+
};
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/** 判定是否应当即时向前端推送问卷弹窗(只推给当前前台会话,未指定会话按全局放行)。 */
|
|
102
|
+
export function shouldPopQuestion(conversationId, activeId) {
|
|
103
|
+
return conversationId === undefined || conversationId === activeId;
|
|
104
|
+
}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* - 避免快照和消息列表随会话进行出现数兆字节 base64 的膨胀。
|
|
9
9
|
*/
|
|
10
10
|
import { createHash, randomUUID } from "node:crypto";
|
|
11
|
-
import { mkdirSync, existsSync, readdirSync } from "node:fs";
|
|
11
|
+
import { mkdirSync, existsSync, readdirSync, writeFileSync, renameSync, rmSync } from "node:fs";
|
|
12
12
|
import { readFile, rename, rm, writeFile, stat } from "node:fs/promises";
|
|
13
13
|
import { join, extname } from "node:path";
|
|
14
14
|
let storeDir = "";
|
|
@@ -79,6 +79,43 @@ export async function saveAttachment(buffer, mimeType = "application/octet-strea
|
|
|
79
79
|
filePath,
|
|
80
80
|
};
|
|
81
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* 将二进制内容写入附件存储(同步版本,供消息序列化等纯同步链路使用):
|
|
84
|
+
* 已存在同哈希文件则直接返回元数据,不重复写盘。
|
|
85
|
+
*/
|
|
86
|
+
export function saveAttachmentSync(buffer, mimeType = "application/octet-stream", preferredExt) {
|
|
87
|
+
if (!storeDir) {
|
|
88
|
+
throw new Error("AttachmentStore has not been initialized with initAttachmentStore(dataDir)");
|
|
89
|
+
}
|
|
90
|
+
const hash = createHash("sha256").update(buffer).digest("hex");
|
|
91
|
+
const ext = preferredExt || MIME_EXT_MAP[mimeType] || ".bin";
|
|
92
|
+
const fileName = `${hash}${ext}`;
|
|
93
|
+
const filePath = join(storeDir, fileName);
|
|
94
|
+
if (!existsSync(filePath)) {
|
|
95
|
+
const tmpPath = `${filePath}.${randomUUID()}.tmp`;
|
|
96
|
+
try {
|
|
97
|
+
writeFileSync(tmpPath, buffer);
|
|
98
|
+
renameSync(tmpPath, filePath);
|
|
99
|
+
}
|
|
100
|
+
catch (err) {
|
|
101
|
+
try {
|
|
102
|
+
rmSync(tmpPath, { force: true });
|
|
103
|
+
}
|
|
104
|
+
catch { }
|
|
105
|
+
const code = err.code;
|
|
106
|
+
if (code !== "EEXIST" && code !== "EPERM")
|
|
107
|
+
throw err;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
hash,
|
|
112
|
+
size: buffer.length,
|
|
113
|
+
mimeType,
|
|
114
|
+
ext,
|
|
115
|
+
url: `/api/attachment/${hash}`,
|
|
116
|
+
filePath,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
82
119
|
/**
|
|
83
120
|
* 根据哈希查找已存储的附件文件路径及元数据。
|
|
84
121
|
*/
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* bash-tool — bash 工具的原生可中断版本与动态终端分流版本。
|
|
3
|
+
*
|
|
4
|
+
* - makeKillableBashTool: 包装 SDK 原生进程版 bash,带 AbortController 集合与 head/tail 后处理。
|
|
5
|
+
* - makeAdaptiveBashTool: 根据用户设置在进程版与可见终端版之间动态分流执行。
|
|
6
|
+
*
|
|
7
|
+
* 从 agent-service.ts 抽出为独立模块。
|
|
8
|
+
*/
|
|
9
|
+
import { createBashToolDefinition, createLocalBashOperations, } from "@earendil-works/pi-coding-agent";
|
|
10
|
+
import { applyHeadTail } from "./terminals.js";
|
|
11
|
+
import { BASH_DESCRIPTION, BASH_PARAMETERS, BASH_PROMPT_GUIDELINES, BASH_PROMPT_SNIPPET } from "./tool-prompts.js";
|
|
12
|
+
/**
|
|
13
|
+
* Killable bash tool: wraps the SDK bash tool (native process spawn, NO terminal).
|
|
14
|
+
* Used when the「默认 bash 覆盖」setting is OFF. Registers its own AbortController
|
|
15
|
+
* into a client-level set (kills) so abortBash() kills only these commands while the
|
|
16
|
+
* agent run and the conversation continue. Exposes persist (ignored — native has no
|
|
17
|
+
* terminal) plus head/tail (post-processed on the returned output) so the parameter
|
|
18
|
+
* schema stays consistent with the terminal-backed tool.
|
|
19
|
+
*/
|
|
20
|
+
export function makeKillableBashTool(cwd, kills,
|
|
21
|
+
/** per-call 返回文本的服务端语言(默认英文);工具 definition 为纯英文。 */
|
|
22
|
+
lang = () => "en") {
|
|
23
|
+
const base = createLocalBashOperations();
|
|
24
|
+
const tool = createBashToolDefinition(cwd, {
|
|
25
|
+
operations: {
|
|
26
|
+
exec: async (command, c, opts) => {
|
|
27
|
+
const ac = new AbortController();
|
|
28
|
+
kills.add(ac);
|
|
29
|
+
try {
|
|
30
|
+
const signals = [opts.signal, ac.signal].filter((s) => s !== undefined);
|
|
31
|
+
return await base.exec(command, c, {
|
|
32
|
+
...opts,
|
|
33
|
+
signal: signals.length > 1 ? AbortSignal.any(signals) : signals[0],
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
kills.delete(ac);
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
// Keep the SDK definition so execute receives the current session context.
|
|
43
|
+
return {
|
|
44
|
+
name: tool.name,
|
|
45
|
+
label: tool.label,
|
|
46
|
+
description: BASH_DESCRIPTION,
|
|
47
|
+
promptSnippet: BASH_PROMPT_SNIPPET,
|
|
48
|
+
promptGuidelines: BASH_PROMPT_GUIDELINES,
|
|
49
|
+
parameters: BASH_PARAMETERS,
|
|
50
|
+
prepareArguments: tool.prepareArguments,
|
|
51
|
+
executionMode: tool.executionMode,
|
|
52
|
+
execute: async (toolCallId, params, signal, onUpdate, ctx) => {
|
|
53
|
+
const result = (await tool.execute(toolCallId, params, signal, onUpdate, ctx));
|
|
54
|
+
// head/tail 后处理(native 无终端,直接截返回行即可)。
|
|
55
|
+
const p = params;
|
|
56
|
+
if ((p?.head || p?.tail) && result?.content?.[0]?.text != null) {
|
|
57
|
+
result.content[0].text = applyHeadTail(result.content[0].text, p.head, p.tail, lang());
|
|
58
|
+
}
|
|
59
|
+
return result;
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 动态分流 bash:按「默认 bash 覆盖」设置(terminalBash)在调用时决定走哪套——
|
|
65
|
+
* 关 = 原生 SDK bash(纯进程、不开终端);开 = 终端接管 bash(persist 决定一次性/
|
|
66
|
+
* 持久)。开关因此即时生效(customTools 固定于 runtime 创建,不能在创建时二选一)。
|
|
67
|
+
*
|
|
68
|
+
* 提示词不在此覆盖:两条路径共用 server/tool-prompts.ts 的那一份(单源),
|
|
69
|
+
* 覆盖 description 正是当年终端版文案被写死却不生效的成因。
|
|
70
|
+
*/
|
|
71
|
+
export function makeAdaptiveBashTool(killable, terminalBacked, useTerminal) {
|
|
72
|
+
return {
|
|
73
|
+
...killable,
|
|
74
|
+
execute: (id, params, signal, onUpdate, ctx) => {
|
|
75
|
+
const p = params;
|
|
76
|
+
// Windows 下 ConPTY 架构限制(MSYS2 全局控制台上限 128,且高频创建销毁容易句柄耗尽/卡顿):
|
|
77
|
+
// 一次性命令(persist !== true)走原生 spawn(基于 pipe,无需分配控制台,速度快 60 倍且免死锁,issue #269);
|
|
78
|
+
// 只有明确需要持久交互(persist === true)才进可见终端 ai-bash。
|
|
79
|
+
const shouldUseTerminal = useTerminal() && (process.platform !== "win32" || p?.persist === true);
|
|
80
|
+
return (shouldUseTerminal ? terminalBacked : killable).execute(id, params, signal, onUpdate, ctx);
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|