Skip to content

Commit cfd1a20

Browse files
author
linyuan.yang
committed
后台shell
1 parent e34cf79 commit cfd1a20

9 files changed

Lines changed: 481 additions & 10 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Reads new output from a task started by shell with background=true or a script tool with mode=background.
2+
3+
Pass the exact taskid returned by the tool. The result contains only output produced since the previous read. If the task is still running, call read_task again later with the same taskid. If the process has exited, the result includes the final exit status.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Executes a shell command or multi-line shell script in a given working directory. Returns stdout and stderr separately. Default timeout 60 s.
22

3+
Set background=true 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 taskid. Use read_task with that taskid to read later output.
4+
35
Supports all forms:
46
- Single command: "git status"
57
- Chained commands: "npm install && npm run build"

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

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

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

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

1111
export function createCommandTools(): StructuredToolInterface[] {
1212
return [
1313
createShellTool(),
14+
createReadTaskTool(),
1415
createPythonCodeTool(),
1516
createPsCodeTool(),
1617
].filter((t): t is StructuredToolInterface => t !== null);

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { DynamicStructuredTool, type StructuredToolInterface } from '@langchain/core/tools';
22
import { z } from 'zod';
3-
import { createErrorResult } from 'scorpio.ai';
3+
import {
4+
ShellManager,
5+
createErrorResult,
6+
formatBackgroundResult,
7+
type MCPToolResult,
8+
} from 'scorpio.ai';
49
import { loadPrompt } from '../../Core/PromptLoader';
510
import { resolveWorkingDir, runShellCommand } from './utils';
611

@@ -13,14 +18,35 @@ export function createShellTool(): StructuredToolInterface {
1318
workingDir: z.string().describe('Absolute path of the working directory'),
1419
stdin: z.any().optional().describe('Data to pipe into the command via stdin. Prefer a string; objects/arrays are auto-serialized to JSON.'),
1520
timeout: z.number().optional().default(60000).describe('Timeout in milliseconds, default 60000 (60 s)'),
21+
background: z.boolean().optional().default(false).describe('Run as a background task. If still running after yieldMs, returns existing output plus a taskid for read_task.'),
22+
yieldMs: z.number().optional().default(1000).describe('Background mode only: how long to wait for immediate completion before returning a taskid, default 1000 ms'),
1623
}) as any,
17-
func: async ({ command, workingDir, stdin, timeout = 60000 }: any) => {
24+
func: async ({ command, workingDir, stdin, timeout = 60000, background = false, yieldMs = 1000 }: any) => {
1825
// schema 用 z.any() 是为了同时满足两点:(1) Zod v4 的 toJSONSchema 不接受 transform/preprocess;
1926
// (2) 模型偶尔不遵守 string 约束、直接塞 object/array。这里在 func 入口统一序列化兜底。
2027
if (stdin != null && typeof stdin !== 'string') stdin = JSON.stringify(stdin);
2128
const { cwd, error } = await resolveWorkingDir(workingDir);
2229
if (error) return createErrorResult(error);
30+
if (background) {
31+
const result = await ShellManager.exec(command, cwd!, yieldMs, `background command "${command}"`, stdin);
32+
return formatBackgroundResult(result);
33+
}
2334
return runShellCommand(command, cwd!, timeout, `command "${command}"`, stdin);
2435
},
2536
});
2637
}
38+
39+
export function createReadTaskTool(): StructuredToolInterface {
40+
return new DynamicStructuredTool({
41+
name: 'read_task',
42+
description: loadPrompt('tools/command/read_task.txt'),
43+
schema: z.object({
44+
taskid: z.string().min(1).describe('The taskid returned by shell background=true or script mode=background'),
45+
yieldMs: z.number().optional().default(1000).describe('How long to wait for new output, default 1000 ms'),
46+
}) as any,
47+
func: async ({ taskid, yieldMs = 1000 }: any): Promise<MCPToolResult> => {
48+
const result = await ShellManager.readTask(taskid, yieldMs);
49+
return formatBackgroundResult(result);
50+
},
51+
});
52+
}

0 commit comments

Comments
 (0)