@trim21/personal-pi-extensions 0.1.20261022015401 → 0.1.20261031020401
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/README.md +7 -0
- package/package.json +1 -1
- package/src/bwrap/exec.ts +96 -37
- package/src/bwrap/runtime.ts +13 -2
- package/src/bwrap/sandbox.ts +22 -8
- package/src/claude-code/index.ts +3 -1
- package/src/index.ts +1 -1
- package/src/lib/tool-services.ts +3 -1
- package/src/lib/tools-config.ts +51 -1
- package/src/opencode/bash.ts +6 -1
- package/src/opencode/index.ts +3 -1
- package/src/spawn-agent.ts +4 -1
package/README.md
CHANGED
|
@@ -104,6 +104,8 @@
|
|
|
104
104
|
"enabledTools": [{ "tools": ["web_search"], "models": ["glm-*"] }],
|
|
105
105
|
// 只进 codemode、不交给模型的工具(须声明结构化输出才会生效)
|
|
106
106
|
"codemodeOnlyTools": [{ "tools": ["aft_*"], "models": ["deepseek-*"] }],
|
|
107
|
+
// bash 命令的额外环境变量:沙箱内外都生效,同名变量覆盖基础值
|
|
108
|
+
"shell": { "env": { "GIT_EDITOR": "true", "GIT_SEQUENCE_EDITOR": "true" } },
|
|
107
109
|
},
|
|
108
110
|
}
|
|
109
111
|
```
|
|
@@ -121,6 +123,11 @@
|
|
|
121
123
|
有些工具由代码固定为 codemode-only(例如 gh-readonly 那一组,见
|
|
122
124
|
[GitHub 只读工具](#github-只读工具)),不需要这条配置;`disabledTools` 对它们同样生效。
|
|
123
125
|
|
|
126
|
+
`shell.env` 是 bash 工具的额外环境变量表,沙箱内执行与不经沙箱的本地执行都注入,同名
|
|
127
|
+
变量以它为准(可以覆盖沙箱默认的 `HOME` / `PATH` / `LANG` 等;`bash -lc` 之后 profile
|
|
128
|
+
仍可能再改动)。典型用法是让命令不要拉起编辑器。它只影响 bash 工具,LSP、`gh`、codemode
|
|
129
|
+
脚本等其它子进程的环境不变。
|
|
130
|
+
|
|
124
131
|
**判定时机是每个会话启动**:pi 在启动 / `/new` / `resume` / `/fork` / `/reload` 时重建
|
|
125
132
|
扩展,注册就发生在 `session_start` 里(模型在扩展加载期读不到),因此新会话按自己的
|
|
126
133
|
模型重新判定;**同一会话内 `/model` 切换不会改变工具集合**,换模型请开新会话。
|
package/package.json
CHANGED
package/src/bwrap/exec.ts
CHANGED
|
@@ -41,20 +41,40 @@ export interface BwrapInvocation {
|
|
|
41
41
|
needsNetworkStack: boolean;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
/**
|
|
45
|
+
* 执行层自己的基础环境:不继承父进程 env/PATH,由 `bash -lc` 从 /etc/profile
|
|
46
|
+
* 与用户 profile 重建。沙箱与本地两条路径共用这一份,差异只在权限。
|
|
47
|
+
*
|
|
48
|
+
* `extraEnv` 是配置注入的额外环境变量(`personalExtensions.shell.env`),同名时覆盖基础值。
|
|
49
|
+
*/
|
|
50
|
+
export function shellBaseEnv(extraEnv?: Readonly<Record<string, string>>): Record<string, string> {
|
|
51
|
+
const home = process.env.HOME;
|
|
52
|
+
if (home === undefined) {
|
|
53
|
+
throw new Error("HOME is not set; refusing to run bash in a clean environment");
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
HOME: home,
|
|
57
|
+
SHELL: "/bin/bash",
|
|
58
|
+
TERM: "dumb",
|
|
59
|
+
LANG: "C.UTF-8",
|
|
60
|
+
// 基础 PATH:profile 加载阶段(设置 PATH 前)需要系统命令(如 id),由 profile 随后覆盖;不含 sbin
|
|
61
|
+
PATH: "/usr/local/bin:/usr/bin:/bin",
|
|
62
|
+
...extraEnv,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
44
66
|
/**
|
|
45
67
|
* 组装一次 bwrap 调用。实际执行(`execInvocation`)与调试打印共用这里,
|
|
46
68
|
* 保证 `--print-args` 输出的命令行与真正跑的那条完全一致。
|
|
69
|
+
*
|
|
70
|
+
* `extraEnv` 是配置注入的额外环境变量:合并进这份干净环境,同名时覆盖基础值。
|
|
47
71
|
*/
|
|
48
72
|
export async function buildBwrapInvocation(
|
|
49
73
|
resolved: ResolvedBwrap,
|
|
50
74
|
workspace: string,
|
|
51
75
|
command: string,
|
|
76
|
+
extraEnv?: Readonly<Record<string, string>>,
|
|
52
77
|
): Promise<BwrapInvocation> {
|
|
53
|
-
// 干净环境:不继承父进程 env/PATH,由 bash -lc 从 /etc/profile 与用户 profile 重建
|
|
54
|
-
const home = process.env.HOME;
|
|
55
|
-
if (home === undefined) {
|
|
56
|
-
throw new Error("HOME is not set; refusing to run bash in a clean environment");
|
|
57
|
-
}
|
|
58
78
|
return {
|
|
59
79
|
// 沙箱内不透传 PATH,execvp 的默认路径可能找不到 bash(如 NixOS),故在父进程解析绝对路径
|
|
60
80
|
shell: getShellConfig().shell,
|
|
@@ -70,14 +90,7 @@ export async function buildBwrapInvocation(
|
|
|
70
90
|
"/proc",
|
|
71
91
|
],
|
|
72
92
|
command,
|
|
73
|
-
env:
|
|
74
|
-
HOME: home,
|
|
75
|
-
SHELL: "/bin/bash",
|
|
76
|
-
TERM: "dumb",
|
|
77
|
-
LANG: "C.UTF-8",
|
|
78
|
-
// 基础 PATH:profile 加载阶段(设置 PATH 前)需要系统命令(如 id),由 profile 随后覆盖;不含 sbin
|
|
79
|
-
PATH: "/usr/local/bin:/usr/bin:/bin",
|
|
80
|
-
},
|
|
93
|
+
env: shellBaseEnv(extraEnv),
|
|
81
94
|
needsNetworkStack: resolved.network === "limited",
|
|
82
95
|
};
|
|
83
96
|
}
|
|
@@ -122,11 +135,9 @@ function killChild(pid: number | undefined): void {
|
|
|
122
135
|
}
|
|
123
136
|
}
|
|
124
137
|
|
|
125
|
-
export interface
|
|
126
|
-
/** 命令执行目录(进程 cwd
|
|
138
|
+
export interface ExecOptions {
|
|
139
|
+
/** 命令执行目录(进程 cwd)。 */
|
|
127
140
|
cwd: string;
|
|
128
|
-
/** netns holder pid:invocation 需要网络栈时必填,缺失即抛错。 */
|
|
129
|
-
holderPid?: number;
|
|
130
141
|
/** 流式输出回调(stdout 与 stderr 已合并)。 */
|
|
131
142
|
onData: (data: Buffer) => void;
|
|
132
143
|
/** 取消:终止整个进程组并以 signal.reason(默认 AbortError)结束。 */
|
|
@@ -135,28 +146,28 @@ export interface ExecInvocationOptions {
|
|
|
135
146
|
timeout?: number;
|
|
136
147
|
}
|
|
137
148
|
|
|
149
|
+
export interface ExecInvocationOptions extends ExecOptions {
|
|
150
|
+
/** netns holder pid:invocation 需要网络栈时必填,缺失即抛错。 */
|
|
151
|
+
holderPid?: number;
|
|
152
|
+
}
|
|
153
|
+
|
|
138
154
|
/**
|
|
139
|
-
*
|
|
155
|
+
* 执行一条组装好的 argv(唯一一处 spawn 与生命周期)。沙箱路径传 bwrap 形式的
|
|
156
|
+
* argv,本地路径传 `[shell, "-lc", command]`,超时 / 中断 / 进程组 kill 语义共用。
|
|
140
157
|
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
158
|
+
* 进程以独立进程组启动,超时与取消都终止整组,启动失败(spawn error)立即结束
|
|
159
|
+
* 且不残留超时定时器。
|
|
143
160
|
*/
|
|
144
|
-
export function
|
|
145
|
-
|
|
146
|
-
|
|
161
|
+
export function execArgv(
|
|
162
|
+
argv: string[],
|
|
163
|
+
env: Readonly<Record<string, string>>,
|
|
164
|
+
options: ExecOptions,
|
|
147
165
|
): Promise<{ exitCode: number | null }> {
|
|
148
|
-
if (invocation.needsNetworkStack && options.holderPid === undefined) {
|
|
149
|
-
return Promise.reject(new Error("Network stack is not initialized for network limited mode"));
|
|
150
|
-
}
|
|
151
|
-
const argv = invocationArgv(
|
|
152
|
-
invocation,
|
|
153
|
-
invocation.needsNetworkStack ? options.holderPid : undefined,
|
|
154
|
-
);
|
|
155
166
|
const child = spawn(argv[0], argv.slice(1), {
|
|
156
167
|
cwd: options.cwd,
|
|
157
168
|
detached: true,
|
|
158
169
|
stdio: ["ignore", "pipe", "pipe"],
|
|
159
|
-
env
|
|
170
|
+
env,
|
|
160
171
|
});
|
|
161
172
|
|
|
162
173
|
return new Promise<{ exitCode: number | null }>((resolve, reject) => {
|
|
@@ -216,24 +227,49 @@ export function execInvocation(
|
|
|
216
227
|
});
|
|
217
228
|
}
|
|
218
229
|
|
|
230
|
+
/**
|
|
231
|
+
* 执行一次组装好的 bwrap 调用:需要网络栈时自动前置 `nsenter` 前缀,其余交给
|
|
232
|
+
* `execArgv`(与本地路径共用的 spawn 实现)。
|
|
233
|
+
*/
|
|
234
|
+
export function execInvocation(
|
|
235
|
+
invocation: BwrapInvocation,
|
|
236
|
+
options: ExecInvocationOptions,
|
|
237
|
+
): Promise<{ exitCode: number | null }> {
|
|
238
|
+
if (invocation.needsNetworkStack && options.holderPid === undefined) {
|
|
239
|
+
return Promise.reject(new Error("Network stack is not initialized for network limited mode"));
|
|
240
|
+
}
|
|
241
|
+
const argv = invocationArgv(
|
|
242
|
+
invocation,
|
|
243
|
+
invocation.needsNetworkStack ? options.holderPid : undefined,
|
|
244
|
+
);
|
|
245
|
+
return execArgv(argv, invocation.env, options);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** 两条路径共用的执行前检查:目录必须存在,已中断则不再启动进程。 */
|
|
249
|
+
async function assertExecutable(cwd: string, signal: AbortSignal | undefined): Promise<void> {
|
|
250
|
+
await fsAccess(cwd, constants.F_OK).catch(() => {
|
|
251
|
+
throw new Error(`Working directory does not exist: ${cwd}\nCannot execute bash commands.`);
|
|
252
|
+
});
|
|
253
|
+
// 已中断(signal.reason 是 name=AbortError 的 DOMException):直接抛,不再执行
|
|
254
|
+
signal?.throwIfAborted();
|
|
255
|
+
}
|
|
256
|
+
|
|
219
257
|
/**
|
|
220
258
|
* @param workspace session 工作区:writablePaths 的 "." 与 PROTECTED_DIRS 都基于它解析,
|
|
221
259
|
* 与当次命令的 cwd(仅作为进程执行目录)解耦,避免 workdir 参数漂移可写边界。
|
|
260
|
+
* @param extraEnv 配置注入的额外环境变量,覆盖沙箱环境里的同名基础值。
|
|
222
261
|
*/
|
|
223
262
|
export function createBwrapBashOperations(
|
|
224
263
|
resolved: ResolvedBwrap,
|
|
225
264
|
workspace: string,
|
|
226
265
|
networkStack?: NetworkStack,
|
|
266
|
+
extraEnv?: Readonly<Record<string, string>>,
|
|
227
267
|
): BashOperations {
|
|
228
268
|
return {
|
|
229
269
|
async exec(command, cwd, { onData, signal, timeout }) {
|
|
230
|
-
await
|
|
231
|
-
throw new Error(`Working directory does not exist: ${cwd}\nCannot execute bash commands.`);
|
|
232
|
-
});
|
|
233
|
-
// 已中断(signal.reason 是 name=AbortError 的 DOMException):直接抛,不再执行
|
|
234
|
-
signal?.throwIfAborted();
|
|
270
|
+
await assertExecutable(cwd, signal);
|
|
235
271
|
|
|
236
|
-
const invocation = await buildBwrapInvocation(resolved, workspace, command);
|
|
272
|
+
const invocation = await buildBwrapInvocation(resolved, workspace, command, extraEnv);
|
|
237
273
|
return execInvocation(invocation, {
|
|
238
274
|
cwd,
|
|
239
275
|
holderPid: networkStack?.holderPid,
|
|
@@ -244,3 +280,26 @@ export function createBwrapBashOperations(
|
|
|
244
280
|
},
|
|
245
281
|
};
|
|
246
282
|
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 不经沙箱的 bash 执行:与 bwrap 路径共用同一份基础环境(`shellBaseEnv`)与同一段
|
|
286
|
+
* spawn 实现(`execArgv`),argv 换成 `[shell, "-lc", command]`。两条路径的差异只有
|
|
287
|
+
* 权限,工具版本因此都按 cwd 由登录 shell 的 profile 解析。
|
|
288
|
+
*
|
|
289
|
+
* @param extraEnv 配置注入的额外环境变量,覆盖基础环境里的同名值。
|
|
290
|
+
*/
|
|
291
|
+
export function createLocalBashOperations(
|
|
292
|
+
extraEnv?: Readonly<Record<string, string>>,
|
|
293
|
+
): BashOperations {
|
|
294
|
+
return {
|
|
295
|
+
async exec(command, cwd, { onData, signal, timeout }) {
|
|
296
|
+
await assertExecutable(cwd, signal);
|
|
297
|
+
return execArgv([getShellConfig().shell, "-lc", command], shellBaseEnv(extraEnv), {
|
|
298
|
+
cwd,
|
|
299
|
+
onData,
|
|
300
|
+
signal,
|
|
301
|
+
timeout,
|
|
302
|
+
});
|
|
303
|
+
},
|
|
304
|
+
};
|
|
305
|
+
}
|
package/src/bwrap/runtime.ts
CHANGED
|
@@ -431,10 +431,17 @@ export class BwrapRuntime {
|
|
|
431
431
|
* sandbox.json 加载(主会话入口的现状行为)。
|
|
432
432
|
*/
|
|
433
433
|
private readonly config: BwrapConfig | undefined;
|
|
434
|
+
/** 注入给每条 bash 命令的额外环境变量(`personalExtensions.shell.env`),沙箱内外都生效。 */
|
|
435
|
+
private readonly env: Readonly<Record<string, string>>;
|
|
434
436
|
|
|
435
|
-
constructor(
|
|
437
|
+
constructor(
|
|
438
|
+
policy: RequestPolicy,
|
|
439
|
+
config?: BwrapConfig,
|
|
440
|
+
env: Readonly<Record<string, string>> = {},
|
|
441
|
+
) {
|
|
436
442
|
this.policy = policy;
|
|
437
443
|
this.config = config;
|
|
444
|
+
this.env = env;
|
|
438
445
|
}
|
|
439
446
|
|
|
440
447
|
setup(pi: ExtensionAPI): void {
|
|
@@ -635,6 +642,7 @@ export class BwrapRuntime {
|
|
|
635
642
|
commandCwd: execCwd,
|
|
636
643
|
command: request.command,
|
|
637
644
|
unsandboxed: local,
|
|
645
|
+
env: this.env,
|
|
638
646
|
onData: (data) => {
|
|
639
647
|
output.append(data);
|
|
640
648
|
emitUpdate();
|
|
@@ -1108,10 +1116,13 @@ export class BwrapRuntime {
|
|
|
1108
1116
|
* config 为完整配置(completeBwrapConfig 的产物):加载配置与创建解耦,传入后不再
|
|
1109
1117
|
* 读盘,且沙箱固定(不注册 /bwrap-* 命令、非沙盒请求直接拒绝)。缺省从全局/项目
|
|
1110
1118
|
* sandbox.json 加载。
|
|
1119
|
+
* env 为注入给每条命令的额外环境变量(`personalExtensions.shell.env`):由入口显式
|
|
1120
|
+
* 传入,缺省不注入——测试里创建 runtime 因此读不到开发机的 settings.json。
|
|
1111
1121
|
*/
|
|
1112
1122
|
export function createBwrapRuntime(
|
|
1113
1123
|
policy: RequestPolicy = createRequestPolicy(),
|
|
1114
1124
|
config?: BwrapConfig,
|
|
1125
|
+
env?: Readonly<Record<string, string>>,
|
|
1115
1126
|
): BwrapRuntime {
|
|
1116
|
-
return new BwrapRuntime(policy, config);
|
|
1127
|
+
return new BwrapRuntime(policy, config, env);
|
|
1117
1128
|
}
|
package/src/bwrap/sandbox.ts
CHANGED
|
@@ -12,8 +12,6 @@
|
|
|
12
12
|
|
|
13
13
|
import { existsSync } from "node:fs";
|
|
14
14
|
|
|
15
|
-
import { createLocalBashOperations } from "@earendil-works/pi-coding-agent";
|
|
16
|
-
|
|
17
15
|
import { expandHome } from "../lib/path.js";
|
|
18
16
|
import {
|
|
19
17
|
type BwrapConfig,
|
|
@@ -27,7 +25,12 @@ import {
|
|
|
27
25
|
resolveBwrapPath,
|
|
28
26
|
type ResolvedBwrap,
|
|
29
27
|
} from "./core.js";
|
|
30
|
-
import {
|
|
28
|
+
import {
|
|
29
|
+
buildBwrapInvocation,
|
|
30
|
+
createBwrapBashOperations,
|
|
31
|
+
createLocalBashOperations,
|
|
32
|
+
invocationArgv,
|
|
33
|
+
} from "./exec.js";
|
|
31
34
|
import type { NetworkStack, NetworkStackStopResult } from "./network-stack.js";
|
|
32
35
|
|
|
33
36
|
export interface SandboxConfigInput {
|
|
@@ -54,8 +57,16 @@ export interface SandboxRunOptions {
|
|
|
54
57
|
signal?: AbortSignal;
|
|
55
58
|
/** 超时秒数:kill 整个进程组并以 TimeoutError 结束。 */
|
|
56
59
|
timeout?: number;
|
|
57
|
-
/**
|
|
60
|
+
/**
|
|
61
|
+
* 调用方已决定不经沙箱(审批通过的全权限;fs 与 network 均 allow-all 时无需显式设置)。
|
|
62
|
+
* 两条路径的环境构造与 spawn 实现相同,差异只有权限。
|
|
63
|
+
*/
|
|
58
64
|
unsandboxed?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* 额外环境变量(`personalExtensions.shell.env`):两条路径共用同一份干净环境,
|
|
67
|
+
* 注入项覆盖同名基础值。
|
|
68
|
+
*/
|
|
69
|
+
env?: Readonly<Record<string, string>>;
|
|
59
70
|
/** 网络栈子进程输出转发,仅诊断用(默认丢弃)。 */
|
|
60
71
|
log?: NetworkStackLog;
|
|
61
72
|
/** 网络栈就绪回调:取 holder pid 与 mihomo 配置路径手动排查。 */
|
|
@@ -126,8 +137,8 @@ export async function runInSandbox(
|
|
|
126
137
|
options.onNetworkStack?.(stack);
|
|
127
138
|
}
|
|
128
139
|
const operations = local
|
|
129
|
-
? createLocalBashOperations()
|
|
130
|
-
: createBwrapBashOperations(resolved, workspace, stack);
|
|
140
|
+
? createLocalBashOperations(options.env)
|
|
141
|
+
: createBwrapBashOperations(resolved, workspace, stack, options.env);
|
|
131
142
|
const { exitCode } = await operations.exec(options.command, commandCwd, {
|
|
132
143
|
onData: options.onData,
|
|
133
144
|
signal: options.signal,
|
|
@@ -192,12 +203,15 @@ export interface SandboxPreview {
|
|
|
192
203
|
*/
|
|
193
204
|
export async function previewSandboxCommand(
|
|
194
205
|
resolved: ResolvedBwrap,
|
|
195
|
-
options: Pick<
|
|
206
|
+
options: Pick<
|
|
207
|
+
SandboxRunOptions,
|
|
208
|
+
"workspace" | "command" | "commandCwd" | "unsandboxed" | "env"
|
|
209
|
+
> & {
|
|
196
210
|
holderPid?: number;
|
|
197
211
|
},
|
|
198
212
|
): Promise<SandboxPreview> {
|
|
199
213
|
const workspace = expandHome(options.workspace);
|
|
200
|
-
const invocation = await buildBwrapInvocation(resolved, workspace, options.command);
|
|
214
|
+
const invocation = await buildBwrapInvocation(resolved, workspace, options.command, options.env);
|
|
201
215
|
if (!invocation.needsNetworkStack || options.unsandboxed === true) {
|
|
202
216
|
return { argv: invocationArgv(invocation), env: invocation.env, needsNetworkStack: false };
|
|
203
217
|
}
|
package/src/claude-code/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type { LspManager } from "../lib/lsp/lsp.js";
|
|
|
5
5
|
import { createRequestPolicy, type RequestPolicy } from "../lib/request-policy.js";
|
|
6
6
|
import type { ToolBus } from "../lib/tool-bus.js";
|
|
7
7
|
import { registerToolsOnSessionStart } from "../lib/tool-registration.js";
|
|
8
|
+
import { readToolsConfig } from "../lib/tools-config.js";
|
|
8
9
|
import { createClaudeCodeFileTools } from "./files.js";
|
|
9
10
|
import { registerSearchTools } from "./search.js";
|
|
10
11
|
import { registerSessionTools } from "./session-tools.js";
|
|
@@ -29,7 +30,8 @@ export interface ClaudeCodeToolsetOptions {
|
|
|
29
30
|
/** claude-code 风格工具集的共享部件:入口与独立默认导出都从这里取。 */
|
|
30
31
|
export function createClaudeCodeTools(pi: ExtensionAPI, options?: ClaudeCodeToolsetOptions) {
|
|
31
32
|
const policy = options?.policy ?? createRequestPolicy(pi.events);
|
|
32
|
-
const runtime =
|
|
33
|
+
const runtime =
|
|
34
|
+
options?.runtime ?? createBwrapRuntime(policy, undefined, readToolsConfig().shellEnv);
|
|
33
35
|
// 独立入口(不经 src/index.ts 的共享服务)要自己接上 bwrap 的会话钩子与 /bwrap* 命令。
|
|
34
36
|
if (options?.runtime === undefined) {
|
|
35
37
|
runtime.setup(pi);
|
package/src/index.ts
CHANGED
|
@@ -36,7 +36,7 @@ export default function personalExtensions(pi: ExtensionAPI): void {
|
|
|
36
36
|
const registration = createToolRegistration(pi);
|
|
37
37
|
// 共享服务要在 registration 之后创建:LSP manager 的 session_start handler 得
|
|
38
38
|
// 排在「记录本会话模型」的 handler 之后。
|
|
39
|
-
const services = createToolServices(pi);
|
|
39
|
+
const services = createToolServices(pi, { shellEnv: registration.config.shellEnv });
|
|
40
40
|
|
|
41
41
|
// 与文件工具集无关的模块(各自在 create 里注册自己的会话钩子与提示)。
|
|
42
42
|
const modules: { name: string; module: ToolModule }[] = [];
|
package/src/lib/tool-services.ts
CHANGED
|
@@ -34,11 +34,13 @@ export interface ToolServices {
|
|
|
34
34
|
export interface ToolServicesOptions extends LspServiceOptions {
|
|
35
35
|
/** 固定沙箱配置(子代理用);不传则跟随用户配置与 /bwrap-* 命令。 */
|
|
36
36
|
sandbox?: BwrapConfig;
|
|
37
|
+
/** 注入给 bash 命令的额外环境变量(`personalExtensions.shell.env`)。 */
|
|
38
|
+
shellEnv?: Readonly<Record<string, string>>;
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
export function createToolServices(pi: ExtensionAPI, options?: ToolServicesOptions): ToolServices {
|
|
40
42
|
const policy = createRequestPolicy(pi.events);
|
|
41
|
-
const runtime = createBwrapRuntime(policy, options?.sandbox);
|
|
43
|
+
const runtime = createBwrapRuntime(policy, options?.sandbox, options?.shellEnv);
|
|
42
44
|
// 注册 /bwrap* 命令、沙箱系统提示与 session 生命周期钩子;固定沙箱
|
|
43
45
|
// (子代理)不注册命令,见 BwrapRuntime.setup。
|
|
44
46
|
runtime.setup(pi);
|
package/src/lib/tools-config.ts
CHANGED
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
* "fileIoByModel": [{ "models": ["glm-*"], "fileIo": "opencode" }],
|
|
9
9
|
* "disabledTools": ["talk-*", { "tools": ["web_*"], "models": ["gpt-*"] }],
|
|
10
10
|
* "enabledTools": [{ "tools": ["web_search"], "models": ["glm-*"] }],
|
|
11
|
-
* "codemodeOnlyTools": [{ "tools": ["read-github-*"], "models": ["glm-*"] }]
|
|
11
|
+
* "codemodeOnlyTools": [{ "tools": ["read-github-*"], "models": ["glm-*"] }],
|
|
12
|
+
* "shell": { "env": { "GIT_EDITOR": "true", "GIT_SEQUENCE_EDITOR": "true" } }
|
|
12
13
|
* }
|
|
13
14
|
* }
|
|
14
15
|
* ```
|
|
@@ -45,6 +46,9 @@ const toolRuleSchema = Type.Object({
|
|
|
45
46
|
models: Type.Optional(Type.Array(Type.String())),
|
|
46
47
|
});
|
|
47
48
|
|
|
49
|
+
/** `shell.env`:环境变量名 → 值,注入给 bash 工具执行的命令。 */
|
|
50
|
+
const shellEnvSchema = Type.Record(Type.String(), Type.String());
|
|
51
|
+
|
|
48
52
|
// ── 类型 ─────────────────────────────────────────────────────────────────────
|
|
49
53
|
|
|
50
54
|
export type FileIoToolset = "claude-code" | "opencode";
|
|
@@ -71,6 +75,8 @@ export interface ToolsConfig {
|
|
|
71
75
|
enabledTools: readonly ToolPatternRule[];
|
|
72
76
|
/** 命中且声明了 `structuredSchema` 的工具只进 codemode、不交给 pi。 */
|
|
73
77
|
codemodeOnlyTools: readonly ToolPatternRule[];
|
|
78
|
+
/** `shell.env`:注入给 bash 命令的额外环境变量(沙箱内外都生效)。 */
|
|
79
|
+
shellEnv: Readonly<Record<string, string>>;
|
|
74
80
|
/** 解析阶段发现的问题,由调用方在能提示用户时上报。 */
|
|
75
81
|
warnings: readonly string[];
|
|
76
82
|
}
|
|
@@ -163,6 +169,47 @@ function parseToolRules(value: unknown, fieldPath: string, warnings: string[]):
|
|
|
163
169
|
return rules;
|
|
164
170
|
}
|
|
165
171
|
|
|
172
|
+
/** 解析 `shell.env`:非法条目逐键告警并跳过,其余键照常生效。 */
|
|
173
|
+
function parseShellEnv(value: unknown, field: string, warnings: string[]): Record<string, string> {
|
|
174
|
+
if (value === undefined) {
|
|
175
|
+
return {};
|
|
176
|
+
}
|
|
177
|
+
if (Value.Check(shellEnvSchema, value)) {
|
|
178
|
+
return { ...value };
|
|
179
|
+
}
|
|
180
|
+
if (!isRecord(value)) {
|
|
181
|
+
warnings.push(`${field}: expected an object mapping names to string values, ignored`);
|
|
182
|
+
return {};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const env: Record<string, string> = {};
|
|
186
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
187
|
+
if (typeof entry === "string") {
|
|
188
|
+
env[key] = entry;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
warnings.push(
|
|
192
|
+
`${field}.${key}: invalid value ${JSON.stringify(entry)} ignored (expected a string)`,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
return env;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function parseShellSection(
|
|
199
|
+
value: unknown,
|
|
200
|
+
field: string,
|
|
201
|
+
warnings: string[],
|
|
202
|
+
): Record<string, string> {
|
|
203
|
+
if (value === undefined) {
|
|
204
|
+
return {};
|
|
205
|
+
}
|
|
206
|
+
if (!isRecord(value)) {
|
|
207
|
+
warnings.push(`${field}: expected an object, ignored`);
|
|
208
|
+
return {};
|
|
209
|
+
}
|
|
210
|
+
return parseShellEnv(value.env, `${field}.env`, warnings);
|
|
211
|
+
}
|
|
212
|
+
|
|
166
213
|
/** 解析 `personalExtensions` section(纯函数,便于测试)。 */
|
|
167
214
|
export function parseToolsConfig(section: unknown, source = "personalExtensions"): ToolsConfig {
|
|
168
215
|
const warnings: string[] = [];
|
|
@@ -174,6 +221,7 @@ export function parseToolsConfig(section: unknown, source = "personalExtensions"
|
|
|
174
221
|
disabledTools: [],
|
|
175
222
|
enabledTools: [],
|
|
176
223
|
codemodeOnlyTools: [],
|
|
224
|
+
shellEnv: {},
|
|
177
225
|
warnings,
|
|
178
226
|
};
|
|
179
227
|
}
|
|
@@ -185,6 +233,7 @@ export function parseToolsConfig(section: unknown, source = "personalExtensions"
|
|
|
185
233
|
disabledTools: [],
|
|
186
234
|
enabledTools: [],
|
|
187
235
|
codemodeOnlyTools: [],
|
|
236
|
+
shellEnv: {},
|
|
188
237
|
warnings,
|
|
189
238
|
};
|
|
190
239
|
}
|
|
@@ -199,6 +248,7 @@ export function parseToolsConfig(section: unknown, source = "personalExtensions"
|
|
|
199
248
|
`${source}.codemodeOnlyTools`,
|
|
200
249
|
warnings,
|
|
201
250
|
),
|
|
251
|
+
shellEnv: parseShellSection(section.shell, `${source}.shell`, warnings),
|
|
202
252
|
warnings,
|
|
203
253
|
};
|
|
204
254
|
}
|
package/src/opencode/bash.ts
CHANGED
|
@@ -22,6 +22,7 @@ import { resolveWorkdir } from "../lib/path.js";
|
|
|
22
22
|
import { createRequestPolicy } from "../lib/request-policy.js";
|
|
23
23
|
import { defineStructuredTool, type ToolBus } from "../lib/tool-bus.js";
|
|
24
24
|
import { registerToolsOnSessionStart } from "../lib/tool-registration.js";
|
|
25
|
+
import { readToolsConfig } from "../lib/tools-config.js";
|
|
25
26
|
|
|
26
27
|
/** 对齐上游 opencode 的截断提示文案(tools/BashTool/bash.ts)。 */
|
|
27
28
|
const CAPTURE_TRUNCATED_NOTICE = "[output capture truncated at the in-memory safety limit]";
|
|
@@ -52,7 +53,11 @@ function appendTruncationNotice(
|
|
|
52
53
|
*/
|
|
53
54
|
export default function opencodeBash(
|
|
54
55
|
pi: ExtensionAPI,
|
|
55
|
-
runtime: BwrapRuntime = createBwrapRuntime(
|
|
56
|
+
runtime: BwrapRuntime = createBwrapRuntime(
|
|
57
|
+
createRequestPolicy(pi.events),
|
|
58
|
+
undefined,
|
|
59
|
+
readToolsConfig().shellEnv,
|
|
60
|
+
),
|
|
56
61
|
): void {
|
|
57
62
|
registerToolsOnSessionStart(pi, (bus) => registerBashTool(bus, pi, runtime));
|
|
58
63
|
}
|
package/src/opencode/index.ts
CHANGED
|
@@ -18,6 +18,7 @@ import type { LspManager } from "../lib/lsp/lsp.js";
|
|
|
18
18
|
import { createRequestPolicy, type RequestPolicy } from "../lib/request-policy.js";
|
|
19
19
|
import type { ToolBus } from "../lib/tool-bus.js";
|
|
20
20
|
import { registerToolsOnSessionStart } from "../lib/tool-registration.js";
|
|
21
|
+
import { readToolsConfig } from "../lib/tools-config.js";
|
|
21
22
|
import { registerBashTool } from "./bash.js";
|
|
22
23
|
import { createOpencodeFileTools } from "./files.js";
|
|
23
24
|
import { registerGlobTool } from "./glob.js";
|
|
@@ -36,7 +37,8 @@ export interface OpencodeToolsetOptions {
|
|
|
36
37
|
export function createOpencodeTools(pi: ExtensionAPI, options?: OpencodeToolsetOptions) {
|
|
37
38
|
// 非沙盒请求策略由本工具集创建,注入文件工具与 bash runtime 共享同一份。
|
|
38
39
|
const policy = options?.policy ?? createRequestPolicy(pi.events);
|
|
39
|
-
const runtime =
|
|
40
|
+
const runtime =
|
|
41
|
+
options?.runtime ?? createBwrapRuntime(policy, undefined, readToolsConfig().shellEnv);
|
|
40
42
|
// 独立入口(不经 src/index.ts 的共享服务)要自己接上 bwrap 的会话钩子与 /bwrap* 命令。
|
|
41
43
|
if (options?.runtime === undefined) {
|
|
42
44
|
runtime.setup(pi);
|
package/src/spawn-agent.ts
CHANGED
|
@@ -229,7 +229,10 @@ export function subagentToolsExtension(
|
|
|
229
229
|
): ExtensionFactory {
|
|
230
230
|
return (pi) => {
|
|
231
231
|
const registration = createToolRegistration(pi);
|
|
232
|
-
const services = createToolServices(pi, {
|
|
232
|
+
const services = createToolServices(pi, {
|
|
233
|
+
sandbox: agent.sandbox ?? SUBAGENT_DEFAULT_SANDBOX,
|
|
234
|
+
shellEnv: registration.config.shellEnv,
|
|
235
|
+
});
|
|
233
236
|
const kind = toolsetsForToolNames(tools);
|
|
234
237
|
if (!kind) {
|
|
235
238
|
return;
|