pi-web-ui 0.100.0 → 0.101.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 +39 -1
- package/README.md +46 -44
- package/README.zh-CN.md +41 -39
- package/bin/pi-web-ui.mjs +4 -10
- package/dist/server/agent-formatters.js +191 -0
- package/dist/server/agent-service-daemon.js +1266 -0
- package/dist/server/agent-service.js +9883 -12935
- 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 +94 -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 +23 -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 +9 -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-ui-slots.js +39 -0
- package/dist/server/plugins.js +71 -38
- package/dist/server/preset-fields.js +7 -0
- package/dist/server/preset-share.js +1 -0
- package/dist/server/process-utils.js +30 -0
- package/dist/server/prompt-assembler.js +80 -0
- 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 +13 -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/web/dist/assets/{TerminalPanel-C3I1Mc-C.js → TerminalPanel-DD7I5_aC.js} +1 -1
- package/web/dist/assets/index-CpjA1YdT.css +1 -0
- package/web/dist/assets/index-nKn87VuM.js +678 -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,527 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具守卫与模式拦截门控(Tool Guards & Mode Gates)
|
|
3
|
+
*
|
|
4
|
+
* 包含:
|
|
5
|
+
* - 计划模式硬闸门(withPlanModeGate)
|
|
6
|
+
* - 目标审查回合闸门(withGoalReviewGate)
|
|
7
|
+
* - 审查者委派模式闸门(withDelegationGate)
|
|
8
|
+
* - 插件守卫与人机协同审批(withToolGuard / makeLateToolGuard)
|
|
9
|
+
* - 只读 / 工作区沙箱权限包装器(wrapWriteToolWithPermission / wrapEditToolWithPermission 等)
|
|
10
|
+
*
|
|
11
|
+
* 从 agent-service.ts 抽出为独立模块。
|
|
12
|
+
*/
|
|
13
|
+
import { resolve } from "node:path";
|
|
14
|
+
import { createWriteToolDefinition, createEditToolDefinition, } from "@earendil-works/pi-coding-agent";
|
|
15
|
+
import { pick } from "./i18n.js";
|
|
16
|
+
import { planModeDenial } from "./plan-mode.js";
|
|
17
|
+
import { goalReviewDenial } from "./goal-review-gate.js";
|
|
18
|
+
import { delegationDenial } from "./delegate-mode.js";
|
|
19
|
+
import { isPathInsideRoot, extractTargetPath } from "./approval-rules.js";
|
|
20
|
+
import { checkDangerousToolCall, pluginApprovalCategory } from "./tool-approval.js";
|
|
21
|
+
import { denialText, } from "./plugin-tool-guard.js";
|
|
22
|
+
/**
|
|
23
|
+
* 计划模式闸门装饰(通用):套在任何工具定义外层,只在**执行前**判定一次。
|
|
24
|
+
* 与权限沙箱/插件守卫平行且更靠外 —— 计划模式是会话级硬约束,不给任何
|
|
25
|
+
* 「记住同类/全部允许」的口子(允许了就等于放开了实施)。
|
|
26
|
+
* 关闭时零开销(返回原定义,不包 execute)。
|
|
27
|
+
*/
|
|
28
|
+
export function withPlanModeGate(def, planMode, getLang) {
|
|
29
|
+
return {
|
|
30
|
+
...def,
|
|
31
|
+
execute: (async (...args) => {
|
|
32
|
+
if (planMode()) {
|
|
33
|
+
const params = args[1];
|
|
34
|
+
const denied = planModeDenial(def.name, params);
|
|
35
|
+
if (denied) {
|
|
36
|
+
const reason = pick(getLang(), denied.reason, denied.reasonEn);
|
|
37
|
+
const err = new Error(reason);
|
|
38
|
+
err.details = {
|
|
39
|
+
guardDenied: true,
|
|
40
|
+
planModeDenied: true,
|
|
41
|
+
kind: denied.kind,
|
|
42
|
+
};
|
|
43
|
+
throw err;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return def.execute(...args);
|
|
47
|
+
}),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* 目标审查回合闸门(server/goal-review-gate.ts):主对话在 `awaitingVerdict`
|
|
52
|
+
* 置位期间是审查者 —— 写类工具、非常规 bash、派发类工具、向用户提问一律拒。
|
|
53
|
+
* 形状与 withPlanModeGate / withDelegationGate 同构,串在最外层(理由最贴合
|
|
54
|
+
* 此刻:跟模型说「这是审查回合」比「这是计划/审查者模式」更有用)。闸门无状态
|
|
55
|
+
* (每次执行只读 `awaitingVerdict`),verdict 落定即自动恢复,不存在钉死。
|
|
56
|
+
*/
|
|
57
|
+
export function withGoalReviewGate(def, reviewTurn, getLang) {
|
|
58
|
+
return {
|
|
59
|
+
...def,
|
|
60
|
+
execute: (async (...args) => {
|
|
61
|
+
if (reviewTurn()) {
|
|
62
|
+
const params = args[1];
|
|
63
|
+
const denied = goalReviewDenial(def.name, params);
|
|
64
|
+
if (denied) {
|
|
65
|
+
return {
|
|
66
|
+
content: [{ type: "text", text: pick(getLang(), denied.reason, denied.reasonEn) }],
|
|
67
|
+
details: { guardDenied: true, goalReviewDenied: true, kind: denied.kind },
|
|
68
|
+
isError: true,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return def.execute(...args);
|
|
73
|
+
}),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* 审查者模式闸门(server/delegate-mode.ts):主对话只审阅不施工 ——
|
|
78
|
+
* 写类工具、非常规 bash、派发类工具一律拒。形状与 withPlanModeGate 同构,
|
|
79
|
+
* 串在它**外层**(两者都开时,先问计划模式还是审查者模式都不影响结果:
|
|
80
|
+
* 都是拒,只是理由不同)。
|
|
81
|
+
*/
|
|
82
|
+
export function withDelegationGate(def, delegateMode, getLang) {
|
|
83
|
+
return {
|
|
84
|
+
...def,
|
|
85
|
+
execute: (async (...args) => {
|
|
86
|
+
if (delegateMode()) {
|
|
87
|
+
const params = args[1];
|
|
88
|
+
const denied = delegationDenial(def.name, params);
|
|
89
|
+
if (denied) {
|
|
90
|
+
return {
|
|
91
|
+
content: [{ type: "text", text: pick(getLang(), denied.reason, denied.reasonEn) }],
|
|
92
|
+
details: { guardDenied: true, delegateDenied: true, kind: denied.kind },
|
|
93
|
+
isError: true,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return def.execute(...args);
|
|
98
|
+
}),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* 把「可能晚一点才注入」的插件守卫包成**调用时解析**的代理。
|
|
103
|
+
*
|
|
104
|
+
* 为什么需要它(P1-5 的真实缺口):`withToolGuard` 在建工具定义时就把 `opts.guard`
|
|
105
|
+
* 捕进了闭包,而 runtime 是在 `ClientSession.create()` 里建的、`cs.toolGuard` 却是在
|
|
106
|
+
* `AgentService.attach()` 末尾才赋值 —— 直接传 `this.toolGuard` 会把 `undefined`
|
|
107
|
+
* 永久固化进那条对话的工具里,于是「attach 时恢复出来的那条对话」永远不受
|
|
108
|
+
* 插件 `onToolPre/onToolPost` 约束(只有新建/切换对话才带上)。代理每次调用才解析,
|
|
109
|
+
* 与赋值时序无关;未注入时逐字等价于直通(allow / undefined)。
|
|
110
|
+
*/
|
|
111
|
+
export function makeLateToolGuard(get) {
|
|
112
|
+
return {
|
|
113
|
+
pre: (req, lang) => get()?.pre(req, lang) ?? Promise.resolve({ verdict: { decision: "allow" } }),
|
|
114
|
+
post: (req, lang) => get()?.post(req, lang) ?? Promise.resolve(undefined),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
export function withToolGuard(def, opts) {
|
|
118
|
+
const guard = opts.guard;
|
|
119
|
+
const toolName = opts.toolName;
|
|
120
|
+
if (!guard && !opts.askApproval)
|
|
121
|
+
return def;
|
|
122
|
+
return {
|
|
123
|
+
...def,
|
|
124
|
+
execute: (async (toolCallId, params, signal, onUpdate, ctx) => {
|
|
125
|
+
const lang = opts.getLang();
|
|
126
|
+
const conversationId = opts.conversationId?.();
|
|
127
|
+
let pre;
|
|
128
|
+
if (guard) {
|
|
129
|
+
try {
|
|
130
|
+
pre = await guard.pre({ toolName, params, conversationId }, lang);
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
pre = { verdict: { decision: "allow" } };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// 检查是否需要人工协同审批(Human-in-the-Loop)
|
|
137
|
+
let needApproval = false;
|
|
138
|
+
let approvalReason;
|
|
139
|
+
let approvalReasonEn;
|
|
140
|
+
let approvalCategory;
|
|
141
|
+
// 1. 系统核心安全:内置高危操作检测与自定义审批规则先行(优先级最高,防短路)
|
|
142
|
+
let danger;
|
|
143
|
+
if (opts.cwd && opts.askApproval) {
|
|
144
|
+
danger = checkDangerousToolCall(toolName, params, opts.cwd, opts.getRoots?.() ?? [], opts.getRules?.());
|
|
145
|
+
if (danger.denied) {
|
|
146
|
+
const reasonText = danger.reason ? ` 原因:${danger.reason}` : "";
|
|
147
|
+
const reasonTextEn = danger.reasonEn ? ` Reason: ${danger.reasonEn}` : "";
|
|
148
|
+
const text = pick(lang, `【工具执行被审批规则直接阻断】${reasonText}`, `[Tool execution blocked by approval rule]${reasonTextEn}`);
|
|
149
|
+
return {
|
|
150
|
+
content: [{ type: "text", text }],
|
|
151
|
+
details: { guardDenied: true, ruleDenied: true, reason: danger.reason },
|
|
152
|
+
isError: true,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// 2. 插件前置守卫 deny 拦截(带 isError 标记)
|
|
157
|
+
if (pre?.verdict.decision === "deny") {
|
|
158
|
+
const text = denialText(pre.verdict, pre.pluginId ?? "plugin", lang);
|
|
159
|
+
return {
|
|
160
|
+
content: [{ type: "text", text }],
|
|
161
|
+
details: { guardDenied: true, decision: pre.verdict.decision, pluginId: pre.pluginId },
|
|
162
|
+
isError: true,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
// 3. 决定是否需要弹窗审批(系统高危 ask 优先于插件通用 ask,防止恶意或低危插件掩盖高危告警)
|
|
166
|
+
let approvalHits;
|
|
167
|
+
if (danger?.dangerous) {
|
|
168
|
+
needApproval = true;
|
|
169
|
+
approvalReason = danger.reason;
|
|
170
|
+
approvalReasonEn = danger.reasonEn;
|
|
171
|
+
approvalCategory = danger.category;
|
|
172
|
+
approvalHits = danger.hits;
|
|
173
|
+
}
|
|
174
|
+
else if (pre?.verdict.decision === "ask") {
|
|
175
|
+
needApproval = true;
|
|
176
|
+
approvalReason = pre.verdict.reason ?? "插件要求确认本次操作";
|
|
177
|
+
approvalReasonEn = pre.verdict.reasonEn ?? "Plugin requested confirmation for this operation";
|
|
178
|
+
// 插件档位按插件 id 分:用户可以「允许同类」= 该插件的确认以后不再问。
|
|
179
|
+
approvalCategory = pluginApprovalCategory(pre.pluginId ?? "plugin");
|
|
180
|
+
}
|
|
181
|
+
let effectiveParams = params;
|
|
182
|
+
let userEdited = false;
|
|
183
|
+
if (needApproval && opts.askApproval) {
|
|
184
|
+
const res = await opts.askApproval(toolCallId, toolName, params, approvalReason, approvalReasonEn, conversationId, approvalCategory, approvalHits);
|
|
185
|
+
if (res.decision === "deny") {
|
|
186
|
+
const reasonText = res.reason ? ` 原因:${res.reason}` : "";
|
|
187
|
+
const reasonTextEn = res.reason ? ` Reason: ${res.reason}` : "";
|
|
188
|
+
return {
|
|
189
|
+
content: [
|
|
190
|
+
{
|
|
191
|
+
type: "text",
|
|
192
|
+
text: pick(lang, `【操作被用户拒绝】${reasonText}`, `[Operation denied by user]${reasonTextEn}`),
|
|
193
|
+
},
|
|
194
|
+
],
|
|
195
|
+
details: { guardDenied: true, userDenied: true, reason: res.reason },
|
|
196
|
+
isError: true,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
else if (res.decision === "edit") {
|
|
200
|
+
effectiveParams = res.editedParams ?? params;
|
|
201
|
+
userEdited = true;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else if (pre?.verdict.decision === "ask") {
|
|
205
|
+
// 无审批通道时回落至原先的阻断行为
|
|
206
|
+
const text = denialText(pre.verdict, pre.pluginId ?? "plugin", lang);
|
|
207
|
+
return {
|
|
208
|
+
content: [{ type: "text", text }],
|
|
209
|
+
details: { guardDenied: true, decision: pre.verdict.decision, pluginId: pre.pluginId },
|
|
210
|
+
isError: true,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
const result = (await def.execute(toolCallId, effectiveParams, signal, onUpdate, ctx));
|
|
214
|
+
if (userEdited && result && typeof result === "object") {
|
|
215
|
+
result.details = {
|
|
216
|
+
...result.details,
|
|
217
|
+
userEdited: true,
|
|
218
|
+
originalParams: params,
|
|
219
|
+
executedParams: effectiveParams,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
if (guard) {
|
|
223
|
+
let post;
|
|
224
|
+
try {
|
|
225
|
+
post = await guard.post({ toolName, params: effectiveParams, result, conversationId }, lang);
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
post = undefined;
|
|
229
|
+
}
|
|
230
|
+
if (post?.content)
|
|
231
|
+
return { ...result, content: post.content };
|
|
232
|
+
}
|
|
233
|
+
return result;
|
|
234
|
+
}),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
/** 校验目标路径是否在工作区(或多根工作区)内。严格规范化防止 ".." 逃逸与 Windows 盘符大小写不一致。 */
|
|
238
|
+
export function isInsideWorkspaceRoots(targetPath, cwd, roots = []) {
|
|
239
|
+
const abs = resolve(cwd, targetPath);
|
|
240
|
+
const allRoots = [resolve(cwd), ...roots.map((r) => resolve(r))];
|
|
241
|
+
return allRoots.some((r) => isPathInsideRoot(abs, r));
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* 为 write 工具包装会话级权限沙箱与人机协同审批。
|
|
245
|
+
* `base` = 覆盖基底:第三方扩展注册的同名 write 优先(见 tool-overrides.ts),
|
|
246
|
+
* 省略则是 SDK 内置实现 —— 于是权限门禁叠在扩展实现之上,而不是把它顶掉。
|
|
247
|
+
*/
|
|
248
|
+
export function wrapWriteToolWithPermission(cwd, getPermission, getRoots, getLang, askApproval, getConversationId, getRules, base = createWriteToolDefinition(cwd)) {
|
|
249
|
+
return {
|
|
250
|
+
...base,
|
|
251
|
+
execute: async (toolCallId, params, signal, onUpdate, ctx) => {
|
|
252
|
+
const perm = getPermission();
|
|
253
|
+
if (perm === "read-only") {
|
|
254
|
+
return {
|
|
255
|
+
content: [
|
|
256
|
+
{
|
|
257
|
+
type: "text",
|
|
258
|
+
text: pick(getLang(), "【权限被拒绝】当前会话处于「只读模式」(read-only),禁止写入文件。若需修改请切换权限预设。", "[Permission Denied] The current session is in 'read-only' mode; writing files is forbidden. Switch permission preset if needed."),
|
|
259
|
+
},
|
|
260
|
+
],
|
|
261
|
+
isError: true,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
if (perm === "workspace-write-never") {
|
|
265
|
+
const p = extractTargetPath(params);
|
|
266
|
+
if (!isInsideWorkspaceRoots(p, cwd, getRoots())) {
|
|
267
|
+
return {
|
|
268
|
+
content: [
|
|
269
|
+
{
|
|
270
|
+
type: "text",
|
|
271
|
+
text: pick(getLang(), `【权限被拒绝】当前会话处于「工作区内修改」(workspace-write-never)模式,禁止修改工作区外部文件("${p}")。若需修改请切换至「完全权限」。`, `[Permission Denied] The current session is in 'workspace-write-never' mode; writing files outside the workspace ("${p}") is forbidden. Switch to 'danger-full-access' if needed.`),
|
|
272
|
+
},
|
|
273
|
+
],
|
|
274
|
+
isError: true,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
// 高危写操作人机协同审批拦截(如敏感配置修改)与规则匹配
|
|
279
|
+
let effectiveParams = params;
|
|
280
|
+
let userEdited = false;
|
|
281
|
+
if (askApproval) {
|
|
282
|
+
const danger = checkDangerousToolCall("write", params, cwd, getRoots(), getRules?.());
|
|
283
|
+
if (danger.denied) {
|
|
284
|
+
return {
|
|
285
|
+
content: [
|
|
286
|
+
{
|
|
287
|
+
type: "text",
|
|
288
|
+
text: pick(getLang(), `【文件写入被审批规则直接阻断】${danger.reason ? ` 原因:${danger.reason}` : ""}`, `[File write blocked by approval rule]${danger.reason ? ` Reason: ${danger.reason}` : ""}`),
|
|
289
|
+
},
|
|
290
|
+
],
|
|
291
|
+
details: { guardDenied: true, ruleDenied: true, reason: danger.reason },
|
|
292
|
+
isError: true,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
if (danger.dangerous) {
|
|
296
|
+
const res = await askApproval(toolCallId, "write", params, danger.reason, danger.reasonEn, getConversationId?.(), danger.category, danger.hits);
|
|
297
|
+
if (res.decision === "deny") {
|
|
298
|
+
return {
|
|
299
|
+
content: [
|
|
300
|
+
{
|
|
301
|
+
type: "text",
|
|
302
|
+
text: pick(getLang(), `【文件写入被用户拒绝】${res.reason ? ` 原因:${res.reason}` : ""}`, `[File write denied by user]${res.reason ? ` Reason: ${res.reason}` : ""}`),
|
|
303
|
+
},
|
|
304
|
+
],
|
|
305
|
+
details: { guardDenied: true, userDenied: true, reason: res.reason },
|
|
306
|
+
isError: true,
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
else if (res.decision === "edit") {
|
|
310
|
+
effectiveParams = res.editedParams ?? params;
|
|
311
|
+
userEdited = true;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
const result = (await base.execute(toolCallId, effectiveParams, signal, onUpdate, ctx));
|
|
316
|
+
if (userEdited && result && typeof result === "object") {
|
|
317
|
+
result.details = {
|
|
318
|
+
...result.details,
|
|
319
|
+
userEdited: true,
|
|
320
|
+
originalParams: params,
|
|
321
|
+
executedParams: effectiveParams,
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
return result;
|
|
325
|
+
},
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* `edit` 的模型可见主描述(仅无扩展覆盖时的基底用;有扩展 edit 时保留它自己的文案)。
|
|
330
|
+
*
|
|
331
|
+
* SDK 自带的描述与其自带 guidelines 几乎逐条复述(oldText 要唯一匹配 / 多处改动合成一次调用 /
|
|
332
|
+
* 不要垫大段未改区域都说两遍),单条信息重复进上下文。这里只留一句「做什么」,细节交给
|
|
333
|
+
* guidelines(它们是 SDK 的,本模块不碰)。
|
|
334
|
+
*/
|
|
335
|
+
export const EDIT_DESCRIPTION = "Edit a single file by exact text replacement: every edits[].oldText must uniquely match the original file.";
|
|
336
|
+
/** `edit` 的精简 guidelines(同上:只用于无扩展覆盖的基底;与 SDK 四条规则一一对应,只去冗余措辞)。 */
|
|
337
|
+
export const EDIT_GUIDELINES = [
|
|
338
|
+
"Use edit for precise changes; oldText must match exactly",
|
|
339
|
+
"When changing several separate locations in one file, put them in ONE call's edits[] instead of multiple calls",
|
|
340
|
+
"Each oldText matches the original file, not earlier edits in the same call — never overlapping or nested",
|
|
341
|
+
"Keep oldText small but unique; never pad with large unchanged regions",
|
|
342
|
+
];
|
|
343
|
+
/**
|
|
344
|
+
* 为 edit 工具包装会话级权限沙箱与人机协同审批(基底语义同 write)。
|
|
345
|
+
*/
|
|
346
|
+
export function wrapEditToolWithPermission(cwd, getPermission, getRoots, getLang, askApproval, getConversationId, getRules, base = createEditToolDefinition(cwd)) {
|
|
347
|
+
return {
|
|
348
|
+
...base,
|
|
349
|
+
execute: async (toolCallId, params, signal, onUpdate, ctx) => {
|
|
350
|
+
const perm = getPermission();
|
|
351
|
+
if (perm === "read-only") {
|
|
352
|
+
return {
|
|
353
|
+
content: [
|
|
354
|
+
{
|
|
355
|
+
type: "text",
|
|
356
|
+
text: pick(getLang(), "【权限被拒绝】当前会话处于「只读模式」(read-only),禁止编辑文件。若需修改请切换权限预设。", "[Permission Denied] The current session is in 'read-only' mode; editing files is forbidden. Switch permission preset if needed."),
|
|
357
|
+
},
|
|
358
|
+
],
|
|
359
|
+
isError: true,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
if (perm === "workspace-write-never") {
|
|
363
|
+
const p = extractTargetPath(params);
|
|
364
|
+
if (!isInsideWorkspaceRoots(p, cwd, getRoots())) {
|
|
365
|
+
return {
|
|
366
|
+
content: [
|
|
367
|
+
{
|
|
368
|
+
type: "text",
|
|
369
|
+
text: pick(getLang(), `【权限被拒绝】当前会话处于「工作区内修改」(workspace-write-never)模式,禁止修改工作区外部文件("${p}")。若需修改请切换至「完全权限」。`, `[Permission Denied] The current session is in 'workspace-write-never' mode; editing files outside the workspace ("${p}") is forbidden. Switch to 'danger-full-access' if needed.`),
|
|
370
|
+
},
|
|
371
|
+
],
|
|
372
|
+
isError: true,
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
// 高危修改人机协同审批拦截与规则匹配
|
|
377
|
+
let effectiveParams = params;
|
|
378
|
+
let userEdited = false;
|
|
379
|
+
if (askApproval) {
|
|
380
|
+
const danger = checkDangerousToolCall("edit", params, cwd, getRoots(), getRules?.());
|
|
381
|
+
if (danger.denied) {
|
|
382
|
+
return {
|
|
383
|
+
content: [
|
|
384
|
+
{
|
|
385
|
+
type: "text",
|
|
386
|
+
text: pick(getLang(), `【文件编辑被审批规则直接阻断】${danger.reason ? ` 原因:${danger.reason}` : ""}`, `[File edit blocked by approval rule]${danger.reason ? ` Reason: ${danger.reason}` : ""}`),
|
|
387
|
+
},
|
|
388
|
+
],
|
|
389
|
+
details: { guardDenied: true, ruleDenied: true, reason: danger.reason },
|
|
390
|
+
isError: true,
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
if (danger.dangerous) {
|
|
394
|
+
const res = await askApproval(toolCallId, "edit", params, danger.reason, danger.reasonEn, getConversationId?.(), danger.category, danger.hits);
|
|
395
|
+
if (res.decision === "deny") {
|
|
396
|
+
return {
|
|
397
|
+
content: [
|
|
398
|
+
{
|
|
399
|
+
type: "text",
|
|
400
|
+
text: pick(getLang(), `【文件编辑被用户拒绝】${res.reason ? ` 原因:${res.reason}` : ""}`, `[File edit denied by user]${res.reason ? ` Reason: ${res.reason}` : ""}`),
|
|
401
|
+
},
|
|
402
|
+
],
|
|
403
|
+
details: { guardDenied: true, userDenied: true, reason: res.reason },
|
|
404
|
+
isError: true,
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
else if (res.decision === "edit") {
|
|
408
|
+
effectiveParams = res.editedParams ?? params;
|
|
409
|
+
userEdited = true;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
const result = (await base.execute(toolCallId, effectiveParams, signal, onUpdate, ctx));
|
|
414
|
+
if (userEdited && result && typeof result === "object") {
|
|
415
|
+
result.details = {
|
|
416
|
+
...result.details,
|
|
417
|
+
userEdited: true,
|
|
418
|
+
originalParams: params,
|
|
419
|
+
executedParams: effectiveParams,
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
return result;
|
|
423
|
+
},
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
/** 为 edit_soft 工具包装会话级权限沙箱与人机协同审批。 */
|
|
427
|
+
export function wrapEditSoftToolWithPermission(tool, cwd, getPermission, getRoots, getLang, askApproval, getConversationId, getRules) {
|
|
428
|
+
return {
|
|
429
|
+
...tool,
|
|
430
|
+
execute: async (toolCallId, params, signal, onUpdate, ctx) => {
|
|
431
|
+
const perm = getPermission();
|
|
432
|
+
if (perm === "read-only") {
|
|
433
|
+
return {
|
|
434
|
+
content: [
|
|
435
|
+
{
|
|
436
|
+
type: "text",
|
|
437
|
+
text: pick(getLang(), "【权限被拒绝】当前会话处于「只读模式」(read-only),禁止编辑文件。若需修改请切换权限预设。", "[Permission Denied] The current session is in 'read-only' mode; editing files is forbidden. Switch permission preset if needed."),
|
|
438
|
+
},
|
|
439
|
+
],
|
|
440
|
+
isError: true,
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
if (perm === "workspace-write-never") {
|
|
444
|
+
const p = params?.path ?? "";
|
|
445
|
+
if (!isInsideWorkspaceRoots(p, cwd, getRoots())) {
|
|
446
|
+
return {
|
|
447
|
+
content: [
|
|
448
|
+
{
|
|
449
|
+
type: "text",
|
|
450
|
+
text: pick(getLang(), `【权限被拒绝】当前会话处于「工作区内修改」(workspace-write-never)模式,禁止修改工作区外部文件("${p}")。若需修改请切换至「完全权限」。`, `[Permission Denied] The current session is in 'workspace-write-never' mode; editing files outside the workspace ("${p}") is forbidden. Switch to 'danger-full-access' if needed.`),
|
|
451
|
+
},
|
|
452
|
+
],
|
|
453
|
+
isError: true,
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
// 高危修改人机协同审批拦截与规则匹配
|
|
458
|
+
let effectiveParams = params;
|
|
459
|
+
let userEdited = false;
|
|
460
|
+
if (askApproval) {
|
|
461
|
+
const danger = checkDangerousToolCall("edit_soft", params, cwd, getRoots(), getRules?.());
|
|
462
|
+
if (danger.denied) {
|
|
463
|
+
return {
|
|
464
|
+
content: [
|
|
465
|
+
{
|
|
466
|
+
type: "text",
|
|
467
|
+
text: pick(getLang(), `【文件编辑被审批规则直接阻断】${danger.reason ? ` 原因:${danger.reason}` : ""}`, `[File edit blocked by approval rule]${danger.reason ? ` Reason: ${danger.reason}` : ""}`),
|
|
468
|
+
},
|
|
469
|
+
],
|
|
470
|
+
details: { guardDenied: true, ruleDenied: true, reason: danger.reason },
|
|
471
|
+
isError: true,
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
if (danger.dangerous) {
|
|
475
|
+
const res = await askApproval(toolCallId, "edit_soft", params, danger.reason, danger.reasonEn, getConversationId?.(), danger.category, danger.hits);
|
|
476
|
+
if (res.decision === "deny") {
|
|
477
|
+
return {
|
|
478
|
+
content: [
|
|
479
|
+
{
|
|
480
|
+
type: "text",
|
|
481
|
+
text: pick(getLang(), `【文件编辑被用户拒绝】${res.reason ? ` 原因:${res.reason}` : ""}`, `[File edit denied by user]${res.reason ? ` Reason: ${res.reason}` : ""}`),
|
|
482
|
+
},
|
|
483
|
+
],
|
|
484
|
+
details: { guardDenied: true, userDenied: true, reason: res.reason },
|
|
485
|
+
isError: true,
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
else if (res.decision === "edit") {
|
|
489
|
+
effectiveParams = res.editedParams ?? params;
|
|
490
|
+
userEdited = true;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
const result = (await tool.execute(toolCallId, effectiveParams, signal, onUpdate, ctx));
|
|
495
|
+
if (userEdited && result && typeof result === "object") {
|
|
496
|
+
result.details = {
|
|
497
|
+
...result.details,
|
|
498
|
+
userEdited: true,
|
|
499
|
+
originalParams: params,
|
|
500
|
+
executedParams: effectiveParams,
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
return result;
|
|
504
|
+
},
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
/** 为 bash 工具包装只读拦截。 */
|
|
508
|
+
export function wrapBashToolWithPermission(tool, getPermission, getLang) {
|
|
509
|
+
return {
|
|
510
|
+
...tool,
|
|
511
|
+
execute: async (toolCallId, params, signal, onUpdate, ctx) => {
|
|
512
|
+
const perm = getPermission();
|
|
513
|
+
if (perm === "read-only") {
|
|
514
|
+
return {
|
|
515
|
+
content: [
|
|
516
|
+
{
|
|
517
|
+
type: "text",
|
|
518
|
+
text: pick(getLang(), "【权限被拒绝】当前会话处于「只读模式」(read-only),禁止执行终端命令。若需执行请切换权限预设。", "[Permission Denied] The current session is in 'read-only' mode; running shell commands is forbidden. Switch permission preset if needed."),
|
|
519
|
+
},
|
|
520
|
+
],
|
|
521
|
+
isError: true,
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
return tool.execute(toolCallId, params, signal, onUpdate, ctx);
|
|
525
|
+
},
|
|
526
|
+
};
|
|
527
|
+
}
|