@ai-sdk/tool-bash 1.0.1 → 1.0.2

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 CHANGED
@@ -1,5 +1,12 @@
1
1
  # @ai-sdk/tool-bash
2
2
 
3
+ ## 1.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - e5b95d3: Add an optional per-command `timeout` in milliseconds to the bash tool.
8
+ - ai@7.0.148
9
+
3
10
  ## 1.0.1
4
11
 
5
12
  ### Patch Changes
package/README.md CHANGED
@@ -98,5 +98,18 @@ Raw runtime instances need their respective adapters; a custom
98
98
  Pass `maxOutputLength` to limit characters retained from each output stream
99
99
  (default: 30,000). Cancellation is forwarded to the supplied session.
100
100
 
101
+ Pass `timeout` to set a timeout in milliseconds for each command execution:
102
+
103
+ ```ts
104
+ const bash = await createBashTool({ sandbox, timeout: 10_000 });
105
+ ```
106
+
107
+ The value must be an integer from `1` to `2147483647`. There is no tool-level
108
+ timeout by default. Each execution gets a fresh timeout; initialization and
109
+ waiting for the model or approval are excluded. The timeout is combined with
110
+ the caller's abort signal, and the sandbox must honor cancellation. The
111
+ sandbox's result or error is passed through (just-bash returns exit code `124`
112
+ when a running command is cancelled).
113
+
101
114
  See the [Bash Tool documentation](https://ai-sdk.dev/docs/ai-sdk-core/tools#bash-tool)
102
115
  for options and file handling.
package/dist/index.d.ts CHANGED
@@ -23,11 +23,14 @@ declare const outputSchema: z.ZodObject<{
23
23
  * command, created if needed. When omitted, uses the sandbox's default directory.
24
24
  * @param maxOutputLength - Maximum characters retained from each output stream
25
25
  * before appending a truncation notice. Defaults to 30,000.
26
+ * @param timeout - Optional timeout in milliseconds for each command execution.
27
+ * Cancellation is forwarded to the sandbox along with the caller's abort signal.
26
28
  */
27
- declare function createBashTool({ sandbox, workingDirectory, maxOutputLength }: {
29
+ declare function createBashTool({ sandbox, workingDirectory, maxOutputLength, timeout }: {
28
30
  sandbox: Experimental_SandboxSession;
29
31
  workingDirectory?: string;
30
32
  maxOutputLength?: number;
33
+ timeout?: number;
31
34
  }): Promise<Tool<BashInput, BashOutput, {}> & {
32
35
  execute: ToolExecuteFunction<BashInput, BashOutput, {}>;
33
36
  }>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":["SandboxSession"],"sources":["../src/bash-tool.ts"],"mappings":";;;KAUK,YAAY,EAAE,aAAa;KAE3B,aAAa,EAAE,aAAa;cAE3B,aAAW,EAAA;;GAEf,EAAA,KAAA;cAEI,cAAY,EAAA;;;;GAIhB,EAAA,KAAA;;;;;;;;;;;;;;iBAuBoB,iBACpB,SACA,kBACA;EAEA,SAASA;EACT;EACA;IACE,QACF,KAAK,WAAW;EACd,SAAS,oBAAoB,WAAW"}
1
+ {"version":3,"file":"index.d.ts","names":["SandboxSession"],"sources":["../src/bash-tool.ts"],"mappings":";;;KAUK,YAAY,EAAE,aAAa;KAE3B,aAAa,EAAE,aAAa;cAE3B,aAAW,EAAA;;GAEf,EAAA,KAAA;cAEI,cAAY,EAAA;;;;GAIhB,EAAA,KAAA;;;;;;;;;;;;;;;;iBAyBoB,iBACpB,SACA,kBACA,iBACA;EAEA,SAASA;EACT;EACA;EACA;IACE,QACF,KAAK,WAAW;EACd,SAAS,oBAAoB,WAAW"}
package/dist/index.js CHANGED
@@ -24,8 +24,15 @@ function limitOutput(output, maxLength) {
24
24
  * command, created if needed. When omitted, uses the sandbox's default directory.
25
25
  * @param maxOutputLength - Maximum characters retained from each output stream
26
26
  * before appending a truncation notice. Defaults to 30,000.
27
+ * @param timeout - Optional timeout in milliseconds for each command execution.
28
+ * Cancellation is forwarded to the sandbox along with the caller's abort signal.
27
29
  */
28
- async function createBashTool({ sandbox, workingDirectory, maxOutputLength = 3e4 }) {
30
+ async function createBashTool({ sandbox, workingDirectory, maxOutputLength = 3e4, timeout }) {
31
+ if (timeout !== void 0 && (!Number.isInteger(timeout) || timeout < 1 || timeout > 2147483647)) throw new InvalidArgumentError({
32
+ parameter: "timeout",
33
+ value: timeout,
34
+ message: "Must be an integer between 1 and 2147483647 milliseconds."
35
+ });
29
36
  if (!Number.isSafeInteger(maxOutputLength) || maxOutputLength < 1) throw new InvalidArgumentError({
30
37
  parameter: "maxOutputLength",
31
38
  value: maxOutputLength,
@@ -46,10 +53,12 @@ async function createBashTool({ sandbox, workingDirectory, maxOutputLength = 3e4
46
53
  inputSchema,
47
54
  outputSchema,
48
55
  execute: async ({ command }, { abortSignal }) => {
56
+ const timeoutSignal = timeout === void 0 ? void 0 : AbortSignal.timeout(timeout);
57
+ const commandSignal = timeoutSignal === void 0 ? abortSignal : abortSignal === void 0 ? timeoutSignal : AbortSignal.any([abortSignal, timeoutSignal]);
49
58
  const { stdout, stderr, exitCode } = await sandbox.run({
50
59
  command,
51
60
  ...workingDirectory !== void 0 ? { workingDirectory } : {},
52
- abortSignal
61
+ abortSignal: commandSignal
53
62
  });
54
63
  return {
55
64
  stdout: limitOutput(stdout, maxOutputLength),
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/bash-tool.ts"],"sourcesContent":["import {\n InvalidArgumentError,\n tool,\n type Tool,\n type ToolExecuteFunction,\n type Experimental_SandboxSession as SandboxSession,\n} from 'ai';\nimport { posix } from 'node:path';\nimport { z } from 'zod/v4';\n\ntype BashInput = z.infer<typeof inputSchema>;\n\ntype BashOutput = z.infer<typeof outputSchema>;\n\nconst inputSchema = z.object({\n command: z.string().describe('The Bash command or script to execute.'),\n});\n\nconst outputSchema = z.object({\n stdout: z.string(),\n stderr: z.string(),\n exitCode: z.number(),\n});\n\nfunction limitOutput(output: string, maxLength: number): string {\n if (output.length <= maxLength) {\n return output;\n }\n\n return `${output.slice(0, maxLength)}\\n[Output truncated: ${output.length - maxLength} characters omitted.]`;\n}\n\n/**\n * Create a provider-independent bash tool backed by a supplied sandbox session.\n *\n * The caller owns the sandbox's lifecycle. Tools using the same session share\n * its files. Available commands and persistence depend on the sandbox.\n * Returns the tool; use the supplied sandbox for file access.\n *\n * @param sandbox - Sandbox session used for commands and file operations.\n * @param workingDirectory - Optional absolute starting directory for every\n * command, created if needed. When omitted, uses the sandbox's default directory.\n * @param maxOutputLength - Maximum characters retained from each output stream\n * before appending a truncation notice. Defaults to 30,000.\n */\nexport async function createBashTool({\n sandbox,\n workingDirectory,\n maxOutputLength = 30_000,\n}: {\n sandbox: SandboxSession;\n workingDirectory?: string;\n maxOutputLength?: number;\n}): Promise<\n Tool<BashInput, BashOutput, {}> & {\n execute: ToolExecuteFunction<BashInput, BashOutput, {}>;\n }\n> {\n if (!Number.isSafeInteger(maxOutputLength) || maxOutputLength < 1) {\n throw new InvalidArgumentError({\n parameter: 'maxOutputLength',\n value: maxOutputLength,\n message: 'Must be a positive safe integer.',\n });\n }\n\n if (workingDirectory !== undefined && !posix.isAbsolute(workingDirectory)) {\n throw new InvalidArgumentError({\n parameter: 'workingDirectory',\n value: workingDirectory,\n message: 'Must be an absolute path in the sandbox.',\n });\n }\n\n if (workingDirectory !== undefined) {\n workingDirectory = posix.normalize(workingDirectory);\n }\n\n if (workingDirectory !== undefined) {\n const directoryResult = await sandbox.run({\n command: `mkdir -p -- '${workingDirectory.replace(/'/g, \"'\\\\''\")}'`,\n });\n if (directoryResult.exitCode !== 0) {\n throw new Error(\n `Failed to create working directory: ${directoryResult.stderr}`,\n );\n }\n }\n\n return tool<BashInput, BashOutput, {}>({\n description:\n 'Run Bash commands in the sandbox. ' +\n (workingDirectory !== undefined\n ? `Every command starts in ${workingDirectory}. `\n : \"Commands run in the sandbox's default working directory. \") +\n 'Use ls to discover files and commands such as cat, grep, sort, awk, ' +\n 'and jq to inspect and transform them. Pipes and redirections are supported. ' +\n 'Returns stdout, stderr, and exitCode; long output is truncated.\\n' +\n sandbox.description,\n inputSchema,\n outputSchema,\n execute: async ({ command }, { abortSignal }) => {\n const { stdout, stderr, exitCode } = await sandbox.run({\n command,\n ...(workingDirectory !== undefined ? { workingDirectory } : {}),\n abortSignal,\n });\n\n return {\n stdout: limitOutput(stdout, maxOutputLength),\n stderr: limitOutput(stderr, maxOutputLength),\n exitCode,\n };\n },\n });\n}\n"],"mappings":";;;;AAcA,MAAM,cAAc,EAAE,OAAO,EAC3B,SAAS,EAAE,OAAO,CAAC,CAAC,SAAS,wCAAwC,EACvE,CAAC;AAED,MAAM,eAAe,EAAE,OAAO;CAC5B,QAAQ,EAAE,OAAO;CACjB,QAAQ,EAAE,OAAO;CACjB,UAAU,EAAE,OAAO;AACrB,CAAC;AAED,SAAS,YAAY,QAAgB,WAA2B;CAC9D,IAAI,OAAO,UAAU,WACnB,OAAO;CAGT,OAAO,GAAG,OAAO,MAAM,GAAG,SAAS,EAAE,uBAAuB,OAAO,SAAS,UAAU;AACxF;;;;;;;;;;;;;;AAeA,eAAsB,eAAe,EACnC,SACA,kBACA,kBAAkB,OASlB;CACA,IAAI,CAAC,OAAO,cAAc,eAAe,KAAK,kBAAkB,GAC9D,MAAM,IAAI,qBAAqB;EAC7B,WAAW;EACX,OAAO;EACP,SAAS;CACX,CAAC;CAGH,IAAI,qBAAqB,KAAA,KAAa,CAAC,MAAM,WAAW,gBAAgB,GACtE,MAAM,IAAI,qBAAqB;EAC7B,WAAW;EACX,OAAO;EACP,SAAS;CACX,CAAC;CAGH,IAAI,qBAAqB,KAAA,GACvB,mBAAmB,MAAM,UAAU,gBAAgB;CAGrD,IAAI,qBAAqB,KAAA,GAAW;EAClC,MAAM,kBAAkB,MAAM,QAAQ,IAAI,EACxC,SAAS,gBAAgB,iBAAiB,QAAQ,MAAM,OAAO,EAAE,GACnE,CAAC;EACD,IAAI,gBAAgB,aAAa,GAC/B,MAAM,IAAI,MACR,uCAAuC,gBAAgB,QACzD;CAEJ;CAEA,OAAO,KAAgC;EACrC,aACE,wCACC,qBAAqB,KAAA,IAClB,2BAA2B,iBAAiB,MAC5C,+DACJ,sNAGA,QAAQ;EACV;EACA;EACA,SAAS,OAAO,EAAE,WAAW,EAAE,kBAAkB;GAC/C,MAAM,EAAE,QAAQ,QAAQ,aAAa,MAAM,QAAQ,IAAI;IACrD;IACA,GAAI,qBAAqB,KAAA,IAAY,EAAE,iBAAiB,IAAI,CAAC;IAC7D;GACF,CAAC;GAED,OAAO;IACL,QAAQ,YAAY,QAAQ,eAAe;IAC3C,QAAQ,YAAY,QAAQ,eAAe;IAC3C;GACF;EACF;CACF,CAAC;AACH"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/bash-tool.ts"],"sourcesContent":["import {\n InvalidArgumentError,\n tool,\n type Tool,\n type ToolExecuteFunction,\n type Experimental_SandboxSession as SandboxSession,\n} from 'ai';\nimport { posix } from 'node:path';\nimport { z } from 'zod/v4';\n\ntype BashInput = z.infer<typeof inputSchema>;\n\ntype BashOutput = z.infer<typeof outputSchema>;\n\nconst inputSchema = z.object({\n command: z.string().describe('The Bash command or script to execute.'),\n});\n\nconst outputSchema = z.object({\n stdout: z.string(),\n stderr: z.string(),\n exitCode: z.number(),\n});\n\nfunction limitOutput(output: string, maxLength: number): string {\n if (output.length <= maxLength) {\n return output;\n }\n\n return `${output.slice(0, maxLength)}\\n[Output truncated: ${output.length - maxLength} characters omitted.]`;\n}\n\n/**\n * Create a provider-independent bash tool backed by a supplied sandbox session.\n *\n * The caller owns the sandbox's lifecycle. Tools using the same session share\n * its files. Available commands and persistence depend on the sandbox.\n * Returns the tool; use the supplied sandbox for file access.\n *\n * @param sandbox - Sandbox session used for commands and file operations.\n * @param workingDirectory - Optional absolute starting directory for every\n * command, created if needed. When omitted, uses the sandbox's default directory.\n * @param maxOutputLength - Maximum characters retained from each output stream\n * before appending a truncation notice. Defaults to 30,000.\n * @param timeout - Optional timeout in milliseconds for each command execution.\n * Cancellation is forwarded to the sandbox along with the caller's abort signal.\n */\nexport async function createBashTool({\n sandbox,\n workingDirectory,\n maxOutputLength = 30_000,\n timeout,\n}: {\n sandbox: SandboxSession;\n workingDirectory?: string;\n maxOutputLength?: number;\n timeout?: number;\n}): Promise<\n Tool<BashInput, BashOutput, {}> & {\n execute: ToolExecuteFunction<BashInput, BashOutput, {}>;\n }\n> {\n if (\n timeout !== undefined &&\n (!Number.isInteger(timeout) || timeout < 1 || timeout > 2_147_483_647)\n ) {\n throw new InvalidArgumentError({\n parameter: 'timeout',\n value: timeout,\n message: 'Must be an integer between 1 and 2147483647 milliseconds.',\n });\n }\n\n if (!Number.isSafeInteger(maxOutputLength) || maxOutputLength < 1) {\n throw new InvalidArgumentError({\n parameter: 'maxOutputLength',\n value: maxOutputLength,\n message: 'Must be a positive safe integer.',\n });\n }\n\n if (workingDirectory !== undefined && !posix.isAbsolute(workingDirectory)) {\n throw new InvalidArgumentError({\n parameter: 'workingDirectory',\n value: workingDirectory,\n message: 'Must be an absolute path in the sandbox.',\n });\n }\n\n if (workingDirectory !== undefined) {\n workingDirectory = posix.normalize(workingDirectory);\n }\n\n if (workingDirectory !== undefined) {\n const directoryResult = await sandbox.run({\n command: `mkdir -p -- '${workingDirectory.replace(/'/g, \"'\\\\''\")}'`,\n });\n if (directoryResult.exitCode !== 0) {\n throw new Error(\n `Failed to create working directory: ${directoryResult.stderr}`,\n );\n }\n }\n\n return tool<BashInput, BashOutput, {}>({\n description:\n 'Run Bash commands in the sandbox. ' +\n (workingDirectory !== undefined\n ? `Every command starts in ${workingDirectory}. `\n : \"Commands run in the sandbox's default working directory. \") +\n 'Use ls to discover files and commands such as cat, grep, sort, awk, ' +\n 'and jq to inspect and transform them. Pipes and redirections are supported. ' +\n 'Returns stdout, stderr, and exitCode; long output is truncated.\\n' +\n sandbox.description,\n inputSchema,\n outputSchema,\n execute: async ({ command }, { abortSignal }) => {\n const timeoutSignal =\n timeout === undefined ? undefined : AbortSignal.timeout(timeout);\n const commandSignal =\n timeoutSignal === undefined\n ? abortSignal\n : abortSignal === undefined\n ? timeoutSignal\n : AbortSignal.any([abortSignal, timeoutSignal]);\n\n const { stdout, stderr, exitCode } = await sandbox.run({\n command,\n ...(workingDirectory !== undefined ? { workingDirectory } : {}),\n abortSignal: commandSignal,\n });\n\n return {\n stdout: limitOutput(stdout, maxOutputLength),\n stderr: limitOutput(stderr, maxOutputLength),\n exitCode,\n };\n },\n });\n}\n"],"mappings":";;;;AAcA,MAAM,cAAc,EAAE,OAAO,EAC3B,SAAS,EAAE,OAAO,CAAC,CAAC,SAAS,wCAAwC,EACvE,CAAC;AAED,MAAM,eAAe,EAAE,OAAO;CAC5B,QAAQ,EAAE,OAAO;CACjB,QAAQ,EAAE,OAAO;CACjB,UAAU,EAAE,OAAO;AACrB,CAAC;AAED,SAAS,YAAY,QAAgB,WAA2B;CAC9D,IAAI,OAAO,UAAU,WACnB,OAAO;CAGT,OAAO,GAAG,OAAO,MAAM,GAAG,SAAS,EAAE,uBAAuB,OAAO,SAAS,UAAU;AACxF;;;;;;;;;;;;;;;;AAiBA,eAAsB,eAAe,EACnC,SACA,kBACA,kBAAkB,KAClB,WAUA;CACA,IACE,YAAY,KAAA,MACX,CAAC,OAAO,UAAU,OAAO,KAAK,UAAU,KAAK,UAAU,aAExD,MAAM,IAAI,qBAAqB;EAC7B,WAAW;EACX,OAAO;EACP,SAAS;CACX,CAAC;CAGH,IAAI,CAAC,OAAO,cAAc,eAAe,KAAK,kBAAkB,GAC9D,MAAM,IAAI,qBAAqB;EAC7B,WAAW;EACX,OAAO;EACP,SAAS;CACX,CAAC;CAGH,IAAI,qBAAqB,KAAA,KAAa,CAAC,MAAM,WAAW,gBAAgB,GACtE,MAAM,IAAI,qBAAqB;EAC7B,WAAW;EACX,OAAO;EACP,SAAS;CACX,CAAC;CAGH,IAAI,qBAAqB,KAAA,GACvB,mBAAmB,MAAM,UAAU,gBAAgB;CAGrD,IAAI,qBAAqB,KAAA,GAAW;EAClC,MAAM,kBAAkB,MAAM,QAAQ,IAAI,EACxC,SAAS,gBAAgB,iBAAiB,QAAQ,MAAM,OAAO,EAAE,GACnE,CAAC;EACD,IAAI,gBAAgB,aAAa,GAC/B,MAAM,IAAI,MACR,uCAAuC,gBAAgB,QACzD;CAEJ;CAEA,OAAO,KAAgC;EACrC,aACE,wCACC,qBAAqB,KAAA,IAClB,2BAA2B,iBAAiB,MAC5C,+DACJ,sNAGA,QAAQ;EACV;EACA;EACA,SAAS,OAAO,EAAE,WAAW,EAAE,kBAAkB;GAC/C,MAAM,gBACJ,YAAY,KAAA,IAAY,KAAA,IAAY,YAAY,QAAQ,OAAO;GACjE,MAAM,gBACJ,kBAAkB,KAAA,IACd,cACA,gBAAgB,KAAA,IACd,gBACA,YAAY,IAAI,CAAC,aAAa,aAAa,CAAC;GAEpD,MAAM,EAAE,QAAQ,QAAQ,aAAa,MAAM,QAAQ,IAAI;IACrD;IACA,GAAI,qBAAqB,KAAA,IAAY,EAAE,iBAAiB,IAAI,CAAC;IAC7D,aAAa;GACf,CAAC;GAED,OAAO;IACL,QAAQ,YAAY,QAAQ,eAAe;IAC3C,QAAQ,YAAY,QAAQ,eAAe;IAC3C;GACF;EACF;CACF,CAAC;AACH"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/tool-bash",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Provider-independent bash tool for the AI SDK",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -28,15 +28,15 @@
28
28
  "zod": "3.25.76"
29
29
  },
30
30
  "peerDependencies": {
31
- "ai": "^7.0.147"
31
+ "ai": "^7.0.148"
32
32
  },
33
33
  "devDependencies": {
34
- "@ai-sdk/sandbox-just-bash": "1.0.158",
35
- "@ai-sdk/sandbox-vercel": "1.0.158",
34
+ "@ai-sdk/sandbox-just-bash": "1.0.159",
35
+ "@ai-sdk/sandbox-vercel": "1.0.159",
36
36
  "@types/node": "22.19.19",
37
37
  "@vercel/ai-tsconfig": "0.0.0",
38
38
  "@vercel/ai-tsdown-config": "0.0.0",
39
- "ai": "7.0.147",
39
+ "ai": "7.0.148",
40
40
  "tsdown": "^0.23.0",
41
41
  "typescript": "6.0.3"
42
42
  },
package/src/bash-tool.ts CHANGED
@@ -42,20 +42,35 @@ function limitOutput(output: string, maxLength: number): string {
42
42
  * command, created if needed. When omitted, uses the sandbox's default directory.
43
43
  * @param maxOutputLength - Maximum characters retained from each output stream
44
44
  * before appending a truncation notice. Defaults to 30,000.
45
+ * @param timeout - Optional timeout in milliseconds for each command execution.
46
+ * Cancellation is forwarded to the sandbox along with the caller's abort signal.
45
47
  */
46
48
  export async function createBashTool({
47
49
  sandbox,
48
50
  workingDirectory,
49
51
  maxOutputLength = 30_000,
52
+ timeout,
50
53
  }: {
51
54
  sandbox: SandboxSession;
52
55
  workingDirectory?: string;
53
56
  maxOutputLength?: number;
57
+ timeout?: number;
54
58
  }): Promise<
55
59
  Tool<BashInput, BashOutput, {}> & {
56
60
  execute: ToolExecuteFunction<BashInput, BashOutput, {}>;
57
61
  }
58
62
  > {
63
+ if (
64
+ timeout !== undefined &&
65
+ (!Number.isInteger(timeout) || timeout < 1 || timeout > 2_147_483_647)
66
+ ) {
67
+ throw new InvalidArgumentError({
68
+ parameter: 'timeout',
69
+ value: timeout,
70
+ message: 'Must be an integer between 1 and 2147483647 milliseconds.',
71
+ });
72
+ }
73
+
59
74
  if (!Number.isSafeInteger(maxOutputLength) || maxOutputLength < 1) {
60
75
  throw new InvalidArgumentError({
61
76
  parameter: 'maxOutputLength',
@@ -100,10 +115,19 @@ export async function createBashTool({
100
115
  inputSchema,
101
116
  outputSchema,
102
117
  execute: async ({ command }, { abortSignal }) => {
118
+ const timeoutSignal =
119
+ timeout === undefined ? undefined : AbortSignal.timeout(timeout);
120
+ const commandSignal =
121
+ timeoutSignal === undefined
122
+ ? abortSignal
123
+ : abortSignal === undefined
124
+ ? timeoutSignal
125
+ : AbortSignal.any([abortSignal, timeoutSignal]);
126
+
103
127
  const { stdout, stderr, exitCode } = await sandbox.run({
104
128
  command,
105
129
  ...(workingDirectory !== undefined ? { workingDirectory } : {}),
106
- abortSignal,
130
+ abortSignal: commandSignal,
107
131
  });
108
132
 
109
133
  return {