Skip to content

Commit 281c22b

Browse files
author
linyuan.yang
committed
支持 write_process
1 parent 68fdd48 commit 281c22b

14 files changed

Lines changed: 172 additions & 21 deletions

File tree

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
Runs a PowerShell code snippet (string) by writing it to a temp .ps1 file and executing it. Using Windows PowerShell (powershell.exe) — Windows-only, PS 5.1 syntax; avoid PS 7+ features like ternary operator (?:) or null coalescing (??=). Use execute_ps_script when the script already exists on disk.
1+
Runs a PowerShell code snippet (string) by writing it to a temp .ps1 file and executing it. Using Windows PowerShell (powershell.exe) — Windows-only, PS 5.1 syntax; avoid PS 7+ features like ternary operator (?:) or null coalescing (??=). Use execute_ps_script when the script already exists on disk.
2+
3+
In background mode, set interactive=true only when the script prompts on stdin and you plan to continue with write_process.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
Runs a PowerShell code snippet (string) by writing it to a temp .ps1 file and executing it. Using PowerShell Core (pwsh) — cross-platform, PS 7+ syntax (e.g. ternary operator, null coalescing, foreach-object -Parallel). Use execute_ps_script when the script already exists on disk.
1+
Runs a PowerShell code snippet (string) by writing it to a temp .ps1 file and executing it. Using PowerShell Core (pwsh) — cross-platform, PS 7+ syntax (e.g. ternary operator, null coalescing, foreach-object -Parallel). Use execute_ps_script when the script already exists on disk.
2+
3+
In background mode, set interactive=true only when the script prompts on stdin and you plan to continue with write_process.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
Runs a Python code snippet (string) by writing it to a temp file and executing it. Use this for inline scripts or quick calculations. Use execute_python_script when the script already exists on disk.
2+
3+
In background mode, set interactive=true only when the script prompts on stdin and you plan to continue with write_process.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
Reads new output from a background process started by shell or a script tool with mode="background".
22

33
Pass the exact numeric processId returned by the tool. The result contains only output produced since the previous read. If the process is still running, call read_process again later with the same processId. If the process has exited, the result includes the final exit status.
4+
5+
If the process was started with interactive=true and is waiting for stdin, use write_process with the same processId to send input.

‎packages/sbot/prompts/tools/command/shell.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Executes shell code in a given working directory. Put the command or multi-line
22

33
Use mode="background" for commands that may take longer than a normal tool call, such as builds, test suites, servers, or long scripts. In background mode, the tool waits briefly for completion; if the process is still running, it returns current output plus a numeric processId. Use read_process with that processId to read later output.
44

5+
For programs that prompt on stdin, set interactive=true in background mode, then use write_process with the returned processId to send input. Include a trailing newline for line prompts. This is pipe-based stdin, not a full terminal/TTY.
6+
57
The code field supports all forms:
68
- Single command: "git status"
79
- Chained commands: "npm install && npm run build"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Writes stdin to a background process started with mode="background" and interactive=true.
2+
3+
Pass the exact numeric processId returned by shell or a script tool. Put the input in stdin. Include a trailing newline for line prompts, for example "y\n" or "2\n".
4+
5+
Use close=true when the process needs EOF after the input, such as programs that read all stdin before continuing. This tool writes to a normal stdin pipe; it is not a full terminal/TTY and may not work for terminal UIs, password prompts, or tools that require a real interactive terminal.

‎packages/sbot/src/Tools/Command/index.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import { type StructuredToolInterface } from '@langchain/core/tools';
22

3-
export { createShellTool, createReadProcessTool } from './shell';
3+
export { createShellTool, createReadProcessTool, createWriteProcessTool } from './shell';
44
export { createPythonCodeTool } from './python';
55
export { createPsCodeTool } from './powershell';
66

7-
import { createShellTool, createReadProcessTool } from './shell';
7+
import { createShellTool, createReadProcessTool, createWriteProcessTool } from './shell';
88
import { createPythonCodeTool } from './python';
99
import { createPsCodeTool } from './powershell';
1010

1111
export function createCommandTools(): StructuredToolInterface[] {
1212
return [
1313
createShellTool(),
1414
createReadProcessTool(),
15+
createWriteProcessTool(),
1516
createPythonCodeTool(),
1617
createPsCodeTool(),
1718
].filter((t): t is StructuredToolInterface => t !== null);

‎packages/sbot/src/Tools/Command/shell.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
createShellTool as createCoreShellTool,
33
createReadProcessTool as createCoreReadProcessTool,
4+
createWriteProcessTool as createCoreWriteProcessTool,
45
} from 'scorpio.ai';
56
import { type StructuredToolInterface } from '@langchain/core/tools';
67
import { loadPrompt } from '../../Core/PromptLoader';
@@ -16,3 +17,9 @@ export function createReadProcessTool(): StructuredToolInterface {
1617
description: loadPrompt('tools/command/read_process.txt'),
1718
});
1819
}
20+
21+
export function createWriteProcessTool(): StructuredToolInterface {
22+
return createCoreWriteProcessTool({
23+
description: loadPrompt('tools/command/write_process.txt'),
24+
});
25+
}

‎packages/scorpio.ai/src/Tools/Process/codeTool.ts‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ function withExecutionMode<T extends z.ZodRawShape>(base: z.ZodObject<T>) {
3535
timeout: z.number().optional().default(60000).describe('Timeout in milliseconds, default 60000 (60 s)'),
3636
}),
3737
base.extend({
38-
mode: z.literal(CodeToolMode.Background).describe('Run in the background and use read_process for later output'),
39-
yieldMs: z.number().optional().default(1000).describe('How long to wait for immediate completion before returning a processid, default 1000 ms'),
38+
mode: z.literal(CodeToolMode.Background).describe('Run in the background and use read_process for later output'),
39+
yieldMs: z.number().optional().default(1000).describe('How long to wait for immediate completion before returning a processId, default 1000 ms'),
40+
interactive: z.boolean().optional().default(false).describe('Keep stdin open so write_process can send more input later. Use only for programs that prompt for stdin; this is pipe-based, not a full TTY.'),
4041
}),
4142
]);
4243
}
@@ -61,9 +62,16 @@ export const readProcessToolSchema = z.object({
6162
yieldMs: z.number().optional().default(1000).describe('How long to wait for new output, default 1000 ms'),
6263
});
6364

65+
export const writeProcessToolSchema = z.object({
66+
processId: z.number().int().positive().describe('The numeric processId returned by shell/script mode="background" with interactive=true'),
67+
stdin: z.any().optional().describe('Data to write to the background process stdin. Prefer a string and include a trailing newline for line prompts; objects/arrays are auto-serialized to JSON.'),
68+
close: z.boolean().optional().default(false).describe('Whether to close stdin after writing. Use true when the process needs EOF to continue or finish.'),
69+
});
70+
6471
type ShellToolInput = z.infer<typeof shellToolSchema>;
6572
type ScriptCodeInput = z.infer<typeof scriptCodeSchema>;
6673
type ReadProcessToolInput = z.infer<typeof readProcessToolSchema>;
74+
type WriteProcessToolInput = z.infer<typeof writeProcessToolSchema>;
6775
type CodeToolInput = ShellToolInput | ScriptCodeInput;
6876

6977
export interface ShellToolOptions {
@@ -76,6 +84,11 @@ export interface ReadProcessToolOptions {
7684
description: string;
7785
}
7886

87+
export interface WriteProcessToolOptions {
88+
name?: string;
89+
description: string;
90+
}
91+
7992
export interface ScriptCodeToolOptions {
8093
name: string;
8194
description: string;
@@ -100,7 +113,7 @@ async function executeCode(input: CodeToolInput, opts: ExecuteCodeOptions): Prom
100113
const stdin = normalizeStdin(input.stdin);
101114
if (opts.runtime === CodeRuntime.Shell) {
102115
if (input.mode === CodeToolMode.Background) {
103-
const result = await processManager.exec(input.code, cwd!, input.yieldMs ?? 1000, stdin);
116+
const result = await processManager.exec(input.code, cwd!, input.yieldMs ?? 1000, stdin, input.interactive ?? false);
104117
return formatProcessResult(result);
105118
}
106119
return runShellCommand(input.code, cwd!, input.timeout ?? 60000, `command "${input.code}"`, stdin);
@@ -133,6 +146,7 @@ async function executeCode(input: CodeToolInput, opts: ExecuteCodeOptions): Prom
133146
input.yieldMs ?? 1000,
134147
stdin,
135148
cleanupTempFile,
149+
input.interactive ?? false,
136150
);
137151
return formatProcessResult(result);
138152
}
@@ -181,3 +195,15 @@ export function createReadProcessTool({ name = 'read_process', description }: Re
181195
},
182196
});
183197
}
198+
199+
export function createWriteProcessTool({ name = 'write_process', description }: WriteProcessToolOptions): StructuredToolInterface {
200+
return new DynamicStructuredTool({
201+
name,
202+
description,
203+
schema: writeProcessToolSchema as any,
204+
func: async ({ processId, stdin, close = false }: WriteProcessToolInput): Promise<MCPToolResult> => {
205+
const result = await processManager.writeProcess(processId, normalizeStdin(stdin) ?? '', close);
206+
return formatProcessResult(result);
207+
},
208+
});
209+
}

‎packages/scorpio.ai/src/Tools/Process/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ export { getCurrentShell, isCommandAvailable, validatePath, resolveWorkingDir }
33
export {
44
createShellTool,
55
createReadProcessTool,
6+
createWriteProcessTool,
67
shellToolSchema,
78
readProcessToolSchema,
9+
writeProcessToolSchema,
810
scriptCodeSchema,
911
CodeRuntime,
1012
CodeToolMode,
1113
ShellToolMode,
1214
ScriptCodeMode,
1315
type ShellToolOptions,
1416
type ReadProcessToolOptions,
17+
type WriteProcessToolOptions,
1518
createScriptCodeTool,
1619
type ScriptCodeToolOptions,
1720
} from './codeTool';

0 commit comments

Comments
 (0)