Skip to content

Commit c672c0f

Browse files
committed
feat(bash-tools): 添加自定义shell路径支持
允许在执行命令时指定自定义shell路径,增强在不同环境下的兼容性。修改包括类型定义、配置验证和执行逻辑的调整。
1 parent 7f44bc5 commit c672c0f

7 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/agents/bash-tools.exec-runtime.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ export async function runExecProcess(opts: {
270270
scopeKey?: string;
271271
sessionKey?: string;
272272
timeoutSec: number | null;
273+
shell?: string;
273274
onUpdate?: (partialResult: AgentToolResult<ExecToolDetails>) => void;
274275
}): Promise<ExecProcessHandle> {
275276
const startedAt = Date.now();
@@ -382,7 +383,7 @@ export async function runExecProcess(opts: {
382383
stdinMode: opts.usePty ? ("pipe-open" as const) : ("pipe-closed" as const),
383384
};
384385
}
385-
const { shell, args: shellArgs } = getShellConfig();
386+
const { shell, args: shellArgs } = getShellConfig(opts.shell);
386387
const childArgv = [shell, ...shellArgs, execCommand];
387388
if (opts.usePty) {
388389
return {

src/agents/bash-tools.exec-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export type ExecToolDefaults = {
2727
notifyOnExit?: boolean;
2828
notifyOnExitEmptySuccess?: boolean;
2929
cwd?: string;
30+
shell?: string;
3031
};
3132

3233
export type ExecElevatedDefaults = {

src/agents/bash-tools.exec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ export function createExecTool(
485485
scopeKey: defaults?.scopeKey,
486486
sessionKey: notifySessionKey,
487487
timeoutSec: effectiveTimeout,
488+
shell: defaults?.shell,
488489
onUpdate,
489490
});
490491

src/agents/pi-tools.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ function resolveExecConfig(params: { cfg?: OpenClawConfig; agentId?: string }) {
155155
notifyOnExitEmptySuccess:
156156
agentExec?.notifyOnExitEmptySuccess ?? globalExec?.notifyOnExitEmptySuccess,
157157
applyPatch: agentExec?.applyPatch ?? globalExec?.applyPatch,
158+
shell: agentExec?.shell ?? globalExec?.shell,
158159
};
159160
}
160161

@@ -417,6 +418,7 @@ export function createOpenClawCodingTools(options?: {
417418
notifyOnExit: options?.exec?.notifyOnExit ?? execConfig.notifyOnExit,
418419
notifyOnExitEmptySuccess:
419420
options?.exec?.notifyOnExitEmptySuccess ?? execConfig.notifyOnExitEmptySuccess,
421+
shell: options?.exec?.shell ?? execConfig.shell,
420422
sandbox: sandbox
421423
? {
422424
containerName: sandbox.containerName,

src/agents/shell-utils.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,33 @@ export function resolvePowerShellPath(): string {
3939
return "powershell.exe";
4040
}
4141

42-
export function getShellConfig(): { shell: string; args: string[] } {
42+
export function getShellConfig(configuredShell?: string): { shell: string; args: string[] } {
43+
if (configuredShell) {
44+
if (process.platform === "win32") {
45+
const base = path.basename(configuredShell, path.extname(configuredShell)).toLowerCase();
46+
if (base === "pwsh" || base === "powershell") {
47+
return {
48+
shell: configuredShell,
49+
args: ["-NoProfile", "-NonInteractive", "-Command"],
50+
};
51+
}
52+
if (base === "cmd") {
53+
return {
54+
shell: configuredShell,
55+
args: ["/d", "/s", "/c"],
56+
};
57+
}
58+
return {
59+
shell: configuredShell,
60+
args: ["-c"],
61+
};
62+
}
63+
return {
64+
shell: configuredShell,
65+
args: ["-c"],
66+
};
67+
}
68+
4369
if (process.platform === "win32") {
4470
// Use PowerShell instead of cmd.exe on Windows.
4571
// Problem: Many Windows system utilities (ipconfig, systeminfo, etc.) write

src/config/types.tools.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ export type ExecToolConfig = {
271271
* Accepts either raw ids (e.g. "gpt-5.2") or full ids (e.g. "openai/gpt-5.2").
272272
*/
273273
allowModels?: string[];
274+
/** Custom shell path to use for executing commands. */
275+
shell?: string;
274276
};
275277
};
276278

src/config/zod-schema.agent-runtime.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ const ToolExecBaseShape = {
409409
notifyOnExit: z.boolean().optional(),
410410
notifyOnExitEmptySuccess: z.boolean().optional(),
411411
applyPatch: ToolExecApplyPatchSchema,
412+
shell: z.string().optional(),
412413
} as const;
413414

414415
const AgentToolExecSchema = z

0 commit comments

Comments
 (0)