@langchain/openai 1.1.3 → 1.2.0-dev-1765432861398
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 +583 -2
- package/dist/azure/chat_models/common.d.cts.map +1 -0
- package/dist/azure/chat_models/common.d.ts.map +1 -0
- package/dist/azure/chat_models/completions.d.cts.map +1 -0
- package/dist/azure/chat_models/completions.d.ts.map +1 -0
- package/dist/azure/chat_models/index.d.cts +1 -1
- package/dist/azure/chat_models/index.d.cts.map +1 -0
- package/dist/azure/chat_models/index.d.ts.map +1 -0
- package/dist/azure/chat_models/responses.d.cts.map +1 -0
- package/dist/azure/chat_models/responses.d.ts.map +1 -0
- package/dist/azure/embeddings.d.cts.map +1 -0
- package/dist/azure/embeddings.d.ts.map +1 -0
- package/dist/azure/llms.d.cts +0 -1
- package/dist/azure/llms.d.cts.map +1 -0
- package/dist/azure/llms.d.ts +0 -1
- package/dist/azure/llms.d.ts.map +1 -0
- package/dist/chat_models/base.cjs +5 -1
- package/dist/chat_models/base.cjs.map +1 -1
- package/dist/chat_models/base.d.cts +8 -22
- package/dist/chat_models/base.d.cts.map +1 -0
- package/dist/chat_models/base.d.ts +6 -20
- package/dist/chat_models/base.d.ts.map +1 -0
- package/dist/chat_models/base.js +6 -2
- package/dist/chat_models/base.js.map +1 -1
- package/dist/chat_models/completions.cjs.map +1 -1
- package/dist/chat_models/completions.d.cts +1 -3
- package/dist/chat_models/completions.d.cts.map +1 -0
- package/dist/chat_models/completions.d.ts +1 -3
- package/dist/chat_models/completions.d.ts.map +1 -0
- package/dist/chat_models/completions.js.map +1 -1
- package/dist/chat_models/index.d.cts +42 -4
- package/dist/chat_models/index.d.cts.map +1 -0
- package/dist/chat_models/index.d.ts +42 -4
- package/dist/chat_models/index.d.ts.map +1 -0
- package/dist/chat_models/responses.d.cts +2 -2
- package/dist/chat_models/responses.d.cts.map +1 -0
- package/dist/chat_models/responses.d.ts +2 -2
- package/dist/chat_models/responses.d.ts.map +1 -0
- package/dist/converters/completions.cjs +22 -12
- package/dist/converters/completions.cjs.map +1 -1
- package/dist/converters/completions.d.cts +0 -1
- package/dist/converters/completions.d.cts.map +1 -0
- package/dist/converters/completions.d.ts +0 -1
- package/dist/converters/completions.d.ts.map +1 -0
- package/dist/converters/completions.js +23 -13
- package/dist/converters/completions.js.map +1 -1
- package/dist/converters/responses.cjs +37 -4
- package/dist/converters/responses.cjs.map +1 -1
- package/dist/converters/responses.d.cts.map +1 -0
- package/dist/converters/responses.d.ts.map +1 -0
- package/dist/converters/responses.js +39 -6
- package/dist/converters/responses.js.map +1 -1
- package/dist/embeddings.d.cts +0 -1
- package/dist/embeddings.d.cts.map +1 -0
- package/dist/embeddings.d.ts +0 -1
- package/dist/embeddings.d.ts.map +1 -0
- package/dist/index.cjs +2 -1
- package/dist/index.d.cts +12 -2
- package/dist/index.d.ts +12 -3
- package/dist/index.js +2 -2
- package/dist/llms.d.cts +0 -1
- package/dist/llms.d.cts.map +1 -0
- package/dist/llms.d.ts +0 -1
- package/dist/llms.d.ts.map +1 -0
- package/dist/tools/applyPatch.cjs +116 -0
- package/dist/tools/applyPatch.cjs.map +1 -0
- package/dist/tools/applyPatch.d.cts +148 -0
- package/dist/tools/applyPatch.d.cts.map +1 -0
- package/dist/tools/applyPatch.d.ts +148 -0
- package/dist/tools/applyPatch.d.ts.map +1 -0
- package/dist/tools/applyPatch.js +115 -0
- package/dist/tools/applyPatch.js.map +1 -0
- package/dist/tools/codeInterpreter.cjs +95 -0
- package/dist/tools/codeInterpreter.cjs.map +1 -0
- package/dist/tools/codeInterpreter.d.cts +125 -0
- package/dist/tools/codeInterpreter.d.cts.map +1 -0
- package/dist/tools/codeInterpreter.d.ts +125 -0
- package/dist/tools/codeInterpreter.d.ts.map +1 -0
- package/dist/tools/codeInterpreter.js +94 -0
- package/dist/tools/codeInterpreter.js.map +1 -0
- package/dist/tools/computerUse.cjs +240 -0
- package/dist/tools/computerUse.cjs.map +1 -0
- package/dist/tools/computerUse.d.cts +234 -0
- package/dist/tools/computerUse.d.cts.map +1 -0
- package/dist/tools/computerUse.d.ts +234 -0
- package/dist/tools/computerUse.d.ts.map +1 -0
- package/dist/tools/computerUse.js +239 -0
- package/dist/tools/computerUse.js.map +1 -0
- package/dist/tools/custom.d.cts +1 -1
- package/dist/tools/custom.d.cts.map +1 -0
- package/dist/tools/custom.d.ts.map +1 -0
- package/dist/tools/dalle.d.cts +1 -2
- package/dist/tools/dalle.d.cts.map +1 -0
- package/dist/tools/dalle.d.ts +1 -2
- package/dist/tools/dalle.d.ts.map +1 -0
- package/dist/tools/fileSearch.cjs +137 -0
- package/dist/tools/fileSearch.cjs.map +1 -0
- package/dist/tools/fileSearch.d.cts +224 -0
- package/dist/tools/fileSearch.d.cts.map +1 -0
- package/dist/tools/fileSearch.d.ts +224 -0
- package/dist/tools/fileSearch.d.ts.map +1 -0
- package/dist/tools/fileSearch.js +136 -0
- package/dist/tools/fileSearch.js.map +1 -0
- package/dist/tools/imageGeneration.cjs +128 -0
- package/dist/tools/imageGeneration.cjs.map +1 -0
- package/dist/tools/imageGeneration.d.cts +199 -0
- package/dist/tools/imageGeneration.d.cts.map +1 -0
- package/dist/tools/imageGeneration.d.ts +199 -0
- package/dist/tools/imageGeneration.d.ts.map +1 -0
- package/dist/tools/imageGeneration.js +127 -0
- package/dist/tools/imageGeneration.js.map +1 -0
- package/dist/tools/index.cjs +26 -0
- package/dist/tools/index.cjs.map +1 -0
- package/dist/tools/index.d.cts +27 -0
- package/dist/tools/index.d.cts.map +1 -0
- package/dist/tools/index.d.ts +27 -1
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +26 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/localShell.cjs +145 -0
- package/dist/tools/localShell.cjs.map +1 -0
- package/dist/tools/localShell.d.cts +167 -0
- package/dist/tools/localShell.d.cts.map +1 -0
- package/dist/tools/localShell.d.ts +167 -0
- package/dist/tools/localShell.d.ts.map +1 -0
- package/dist/tools/localShell.js +144 -0
- package/dist/tools/localShell.js.map +1 -0
- package/dist/tools/mcp.cjs +53 -0
- package/dist/tools/mcp.cjs.map +1 -0
- package/dist/tools/mcp.d.cts +177 -0
- package/dist/tools/mcp.d.cts.map +1 -0
- package/dist/tools/mcp.d.ts +177 -0
- package/dist/tools/mcp.d.ts.map +1 -0
- package/dist/tools/mcp.js +52 -0
- package/dist/tools/mcp.js.map +1 -0
- package/dist/tools/shell.cjs +163 -0
- package/dist/tools/shell.cjs.map +1 -0
- package/dist/tools/shell.d.cts +221 -0
- package/dist/tools/shell.d.cts.map +1 -0
- package/dist/tools/shell.d.ts +221 -0
- package/dist/tools/shell.d.ts.map +1 -0
- package/dist/tools/shell.js +162 -0
- package/dist/tools/shell.js.map +1 -0
- package/dist/tools/webSearch.cjs +73 -0
- package/dist/tools/webSearch.cjs.map +1 -0
- package/dist/tools/webSearch.d.cts +130 -0
- package/dist/tools/webSearch.d.cts.map +1 -0
- package/dist/tools/webSearch.d.ts +130 -0
- package/dist/tools/webSearch.d.ts.map +1 -0
- package/dist/tools/webSearch.js +72 -0
- package/dist/tools/webSearch.js.map +1 -0
- package/dist/types.d.cts +2 -4
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +1 -3
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/azure.d.cts +1 -5
- package/dist/utils/azure.d.cts.map +1 -0
- package/dist/utils/azure.d.ts +1 -5
- package/dist/utils/azure.d.ts.map +1 -0
- package/dist/utils/client.d.cts.map +1 -0
- package/dist/utils/client.d.ts.map +1 -0
- package/dist/utils/misc.cjs +6 -0
- package/dist/utils/misc.cjs.map +1 -1
- package/dist/utils/misc.d.cts +1 -1
- package/dist/utils/misc.d.cts.map +1 -0
- package/dist/utils/misc.d.ts +1 -1
- package/dist/utils/misc.d.ts.map +1 -0
- package/dist/utils/misc.js +6 -1
- package/dist/utils/misc.js.map +1 -1
- package/dist/utils/prompts.d.cts +0 -1
- package/dist/utils/prompts.d.cts.map +1 -0
- package/dist/utils/prompts.d.ts +0 -1
- package/dist/utils/prompts.d.ts.map +1 -0
- package/dist/utils/tools.cjs +39 -1
- package/dist/utils/tools.cjs.map +1 -1
- package/dist/utils/tools.d.cts.map +1 -0
- package/dist/utils/tools.d.ts.map +1 -0
- package/dist/utils/tools.js +37 -2
- package/dist/utils/tools.js.map +1 -1
- package/package.json +8 -6
- package/dist/langchain-core/dist/callbacks/base.d.ts +0 -1
- package/dist/langchain-core/dist/callbacks/manager.d.ts +0 -2
- package/dist/langchain-core/dist/documents/transformers.d.ts +0 -2
- package/dist/langchain-core/dist/language_models/base.d.ts +0 -7
- package/dist/langchain-core/dist/language_models/chat_models.d.cts +0 -13
- package/dist/langchain-core/dist/language_models/chat_models.d.ts +0 -23
- package/dist/langchain-core/dist/messages/index.d.ts +0 -1
- package/dist/langchain-core/dist/messages/transformers.d.ts +0 -3
- package/dist/langchain-core/dist/runnables/base.d.ts +0 -7
- package/dist/langchain-core/dist/runnables/config.d.ts +0 -2
- package/dist/langchain-core/dist/runnables/graph.d.ts +0 -1
- package/dist/langchain-core/dist/runnables/types.d.ts +0 -2
- package/dist/langchain-core/dist/tools/index.d.ts +0 -8
- package/dist/langchain-core/dist/tools/types.d.ts +0 -7
- package/dist/langchain-core/dist/tracers/base.d.ts +0 -2
- package/dist/langchain-core/dist/tracers/event_stream.d.ts +0 -2
- package/dist/langchain-core/dist/tracers/log_stream.d.ts +0 -3
- package/dist/langchain-core/dist/types/_internal.d.cts +0 -11
- package/dist/langchain-core/dist/types/_internal.d.ts +0 -11
- package/dist/langchain-core/dist/utils/json_schema.d.ts +0 -1
- package/dist/langchain-core/dist/utils/stream.d.cts +0 -26
- package/dist/langchain-core/dist/utils/stream.d.ts +0 -26
- package/dist/langchain-core/dist/utils/types/index.d.ts +0 -1
- package/dist/langchain-core/dist/utils/types/zod.d.ts +0 -1
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const __langchain_core_tools = require_rolldown_runtime.__toESM(require("@langchain/core/tools"));
|
|
3
|
+
const zod_v4 = require_rolldown_runtime.__toESM(require("zod/v4"));
|
|
4
|
+
|
|
5
|
+
//#region src/tools/shell.ts
|
|
6
|
+
const ShellActionSchema = zod_v4.z.object({
|
|
7
|
+
commands: zod_v4.z.array(zod_v4.z.string()).describe("Array of shell commands to execute"),
|
|
8
|
+
timeout_ms: zod_v4.z.number().optional().describe("Optional timeout in milliseconds for the commands"),
|
|
9
|
+
max_output_length: zod_v4.z.number().optional().describe("Optional maximum number of characters to return from each command")
|
|
10
|
+
});
|
|
11
|
+
const TOOL_NAME = "shell";
|
|
12
|
+
/**
|
|
13
|
+
* Creates a Shell tool that allows models to run shell commands through your integration.
|
|
14
|
+
*
|
|
15
|
+
* The shell tool allows the model to interact with your local computer through a controlled
|
|
16
|
+
* command-line interface. The model proposes shell commands; your integration executes them
|
|
17
|
+
* and returns the outputs. This creates a simple plan-execute loop that lets models inspect
|
|
18
|
+
* the system, run utilities, and gather data until they can finish the task.
|
|
19
|
+
*
|
|
20
|
+
* **Important**: The shell tool is available through the Responses API for use with `GPT-5.1`.
|
|
21
|
+
* It is not available on other models, or via the Chat Completions API.
|
|
22
|
+
*
|
|
23
|
+
* **When to use**:
|
|
24
|
+
* - **Automating filesystem or process diagnostics** – For example, "find the largest PDF
|
|
25
|
+
* under ~/Documents" or "show running gunicorn processes."
|
|
26
|
+
* - **Extending the model's capabilities** – Using built-in UNIX utilities, python runtime
|
|
27
|
+
* and other CLIs in your environment.
|
|
28
|
+
* - **Running multi-step build and test flows** – Chaining commands like `pip install` and `pytest`.
|
|
29
|
+
* - **Complex agentic coding workflows** – Using other tools like `apply_patch` to complete
|
|
30
|
+
* workflows that involve complex file operations.
|
|
31
|
+
*
|
|
32
|
+
* **How it works**:
|
|
33
|
+
* The tool operates in a continuous loop:
|
|
34
|
+
* 1. Model sends shell commands (`shell_call` with `commands` array)
|
|
35
|
+
* 2. Your code executes the commands (can be concurrent)
|
|
36
|
+
* 3. You return stdout, stderr, and outcome for each command
|
|
37
|
+
* 4. Repeat until the task is complete
|
|
38
|
+
*
|
|
39
|
+
* **Security Warning**: Running arbitrary shell commands can be dangerous.
|
|
40
|
+
* Always sandbox execution or add strict allow/deny-lists before forwarding
|
|
41
|
+
* a command to the system shell.
|
|
42
|
+
*
|
|
43
|
+
* @see {@link https://platform.openai.com/docs/guides/tools-shell | OpenAI Shell Documentation}
|
|
44
|
+
* @see {@link https://github.com/openai/codex | Codex CLI} for reference implementation.
|
|
45
|
+
*
|
|
46
|
+
* @param options - Configuration for the Shell tool
|
|
47
|
+
* @returns A Shell tool that can be passed to `bindTools`
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* import { ChatOpenAI, tools } from "@langchain/openai";
|
|
52
|
+
* import { exec } from "child_process/promises";
|
|
53
|
+
*
|
|
54
|
+
* const model = new ChatOpenAI({ model: "gpt-5.1" });
|
|
55
|
+
*
|
|
56
|
+
* // With execute callback for automatic command handling
|
|
57
|
+
* const shellTool = tools.shell({
|
|
58
|
+
* execute: async (action) => {
|
|
59
|
+
* const outputs = await Promise.all(
|
|
60
|
+
* action.commands.map(async (cmd) => {
|
|
61
|
+
* try {
|
|
62
|
+
* const { stdout, stderr } = await exec(cmd, {
|
|
63
|
+
* timeout: action.timeout_ms ?? undefined,
|
|
64
|
+
* });
|
|
65
|
+
* return {
|
|
66
|
+
* stdout,
|
|
67
|
+
* stderr,
|
|
68
|
+
* outcome: { type: "exit" as const, exit_code: 0 },
|
|
69
|
+
* };
|
|
70
|
+
* } catch (error) {
|
|
71
|
+
* const timedOut = error.killed && error.signal === "SIGTERM";
|
|
72
|
+
* return {
|
|
73
|
+
* stdout: error.stdout ?? "",
|
|
74
|
+
* stderr: error.stderr ?? String(error),
|
|
75
|
+
* outcome: timedOut
|
|
76
|
+
* ? { type: "timeout" as const }
|
|
77
|
+
* : { type: "exit" as const, exit_code: error.code ?? 1 },
|
|
78
|
+
* };
|
|
79
|
+
* }
|
|
80
|
+
* })
|
|
81
|
+
* );
|
|
82
|
+
* return {
|
|
83
|
+
* output: outputs,
|
|
84
|
+
* maxOutputLength: action.max_output_length,
|
|
85
|
+
* };
|
|
86
|
+
* },
|
|
87
|
+
* });
|
|
88
|
+
*
|
|
89
|
+
* const llmWithShell = model.bindTools([shellTool]);
|
|
90
|
+
* const response = await llmWithShell.invoke(
|
|
91
|
+
* "Find the largest PDF file in ~/Documents"
|
|
92
|
+
* );
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* // Full shell loop example
|
|
98
|
+
* async function shellLoop(model, task) {
|
|
99
|
+
* let response = await model.invoke(task, {
|
|
100
|
+
* tools: [tools.shell({ execute: myExecutor })],
|
|
101
|
+
* });
|
|
102
|
+
*
|
|
103
|
+
* while (true) {
|
|
104
|
+
* const shellCall = response.additional_kwargs.tool_outputs?.find(
|
|
105
|
+
* (output) => output.type === "shell_call"
|
|
106
|
+
* );
|
|
107
|
+
*
|
|
108
|
+
* if (!shellCall) break;
|
|
109
|
+
*
|
|
110
|
+
* // Execute commands (with proper sandboxing!)
|
|
111
|
+
* const result = await executeCommands(shellCall.action);
|
|
112
|
+
*
|
|
113
|
+
* // Send output back to model
|
|
114
|
+
* response = await model.invoke([
|
|
115
|
+
* response,
|
|
116
|
+
* {
|
|
117
|
+
* type: "shell_call_output",
|
|
118
|
+
* call_id: shellCall.call_id,
|
|
119
|
+
* output: result.output,
|
|
120
|
+
* max_output_length: result.maxOutputLength,
|
|
121
|
+
* },
|
|
122
|
+
* ], {
|
|
123
|
+
* tools: [tools.shell({ execute: myExecutor })],
|
|
124
|
+
* });
|
|
125
|
+
* }
|
|
126
|
+
*
|
|
127
|
+
* return response;
|
|
128
|
+
* }
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* @remarks
|
|
132
|
+
* - Only available through the Responses API (not Chat Completions)
|
|
133
|
+
* - Designed for use with `gpt-5.1` model
|
|
134
|
+
* - Commands are provided as an array of strings that can be executed concurrently
|
|
135
|
+
* - Action includes: `commands`, `timeout_ms`, `max_output_length`
|
|
136
|
+
* - Always sandbox or validate commands before execution
|
|
137
|
+
* - The `timeout_ms` from the model is only a hint—enforce your own limits
|
|
138
|
+
* - If `max_output_length` exists in the action, always pass it back in the output
|
|
139
|
+
* - Many CLI tools return non-zero exit codes for warnings; still capture stdout/stderr
|
|
140
|
+
*/
|
|
141
|
+
function shell(options) {
|
|
142
|
+
const executeWrapper = async (action) => {
|
|
143
|
+
const result = await options.execute(action);
|
|
144
|
+
return JSON.stringify({
|
|
145
|
+
output: result.output,
|
|
146
|
+
max_output_length: result.maxOutputLength
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
const shellTool = (0, __langchain_core_tools.tool)(executeWrapper, {
|
|
150
|
+
name: TOOL_NAME,
|
|
151
|
+
description: "Execute shell commands in a managed environment. Commands can be run concurrently.",
|
|
152
|
+
schema: ShellActionSchema
|
|
153
|
+
});
|
|
154
|
+
shellTool.extras = {
|
|
155
|
+
...shellTool.extras ?? {},
|
|
156
|
+
providerToolDefinition: { type: "shell" }
|
|
157
|
+
};
|
|
158
|
+
return shellTool;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
//#endregion
|
|
162
|
+
exports.shell = shell;
|
|
163
|
+
//# sourceMappingURL=shell.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell.cjs","names":["z","options: ShellOptions","action: ShellAction"],"sources":["../../src/tools/shell.ts"],"sourcesContent":["import { z } from \"zod/v4\";\nimport { OpenAI as OpenAIClient } from \"openai\";\nimport { tool, type DynamicStructuredTool } from \"@langchain/core/tools\";\n\n/**\n * Re-export action type from OpenAI SDK for convenience.\n * The action contains command details like commands array, timeout, and max output length.\n */\nexport type ShellAction =\n OpenAIClient.Responses.ResponseFunctionShellToolCall.Action;\n\n// Zod schema for shell action\nexport const ShellActionSchema = z.object({\n commands: z.array(z.string()).describe(\"Array of shell commands to execute\"),\n timeout_ms: z\n .number()\n .optional()\n .describe(\"Optional timeout in milliseconds for the commands\"),\n max_output_length: z\n .number()\n .optional()\n .describe(\n \"Optional maximum number of characters to return from each command\"\n ),\n});\n\n/**\n * Result of a single shell command execution.\n * Contains stdout, stderr, and the outcome (exit code or timeout).\n */\nexport type ShellCommandOutput =\n OpenAIClient.Responses.ResponseFunctionShellCallOutputContent;\n\n/**\n * Outcome type for shell command execution - either exit with code or timeout.\n */\nexport type ShellCallOutcome = ShellCommandOutput[\"outcome\"];\n\n/**\n * Result of executing shell commands.\n * Contains an array of outputs (one per command) and the max_output_length parameter.\n */\nexport interface ShellResult {\n /**\n * Array of command outputs. Each entry corresponds to a command from the action.\n * The order should match the order of commands in the action.\n */\n output: ShellCommandOutput[];\n /**\n * The max_output_length from the action, which must be passed back to the API.\n * If not provided in the action, can be omitted.\n */\n maxOutputLength?: number | null;\n}\n\n/**\n * Options for the Shell tool.\n */\nexport interface ShellOptions {\n /**\n * Execute function that handles shell command execution.\n * This function receives the action input containing the commands and limits,\n * and should return a ShellResult with stdout, stderr, and outcome for each command.\n *\n * @example\n * ```typescript\n * execute: async (action) => {\n * const outputs = await Promise.all(\n * action.commands.map(async (cmd) => {\n * try {\n * const { stdout, stderr } = await exec(cmd, {\n * timeout: action.timeout_ms ?? undefined,\n * });\n * return {\n * stdout,\n * stderr,\n * outcome: { type: \"exit\" as const, exit_code: 0 },\n * };\n * } catch (error) {\n * const timedOut = error.killed && error.signal === \"SIGTERM\";\n * return {\n * stdout: error.stdout ?? \"\",\n * stderr: error.stderr ?? String(error),\n * outcome: timedOut\n * ? { type: \"timeout\" as const }\n * : { type: \"exit\" as const, exit_code: error.code ?? 1 },\n * };\n * }\n * })\n * );\n * return {\n * output: outputs,\n * maxOutputLength: action.max_output_length,\n * };\n * }\n * ```\n */\n execute: (action: ShellAction) => ShellResult | Promise<ShellResult>;\n}\n\n/**\n * OpenAI Shell tool type for the Responses API.\n */\nexport type ShellTool = OpenAIClient.Responses.FunctionShellTool;\n\nconst TOOL_NAME = \"shell\";\n\n/**\n * Creates a Shell tool that allows models to run shell commands through your integration.\n *\n * The shell tool allows the model to interact with your local computer through a controlled\n * command-line interface. The model proposes shell commands; your integration executes them\n * and returns the outputs. This creates a simple plan-execute loop that lets models inspect\n * the system, run utilities, and gather data until they can finish the task.\n *\n * **Important**: The shell tool is available through the Responses API for use with `GPT-5.1`.\n * It is not available on other models, or via the Chat Completions API.\n *\n * **When to use**:\n * - **Automating filesystem or process diagnostics** – For example, \"find the largest PDF\n * under ~/Documents\" or \"show running gunicorn processes.\"\n * - **Extending the model's capabilities** – Using built-in UNIX utilities, python runtime\n * and other CLIs in your environment.\n * - **Running multi-step build and test flows** – Chaining commands like `pip install` and `pytest`.\n * - **Complex agentic coding workflows** – Using other tools like `apply_patch` to complete\n * workflows that involve complex file operations.\n *\n * **How it works**:\n * The tool operates in a continuous loop:\n * 1. Model sends shell commands (`shell_call` with `commands` array)\n * 2. Your code executes the commands (can be concurrent)\n * 3. You return stdout, stderr, and outcome for each command\n * 4. Repeat until the task is complete\n *\n * **Security Warning**: Running arbitrary shell commands can be dangerous.\n * Always sandbox execution or add strict allow/deny-lists before forwarding\n * a command to the system shell.\n *\n * @see {@link https://platform.openai.com/docs/guides/tools-shell | OpenAI Shell Documentation}\n * @see {@link https://github.com/openai/codex | Codex CLI} for reference implementation.\n *\n * @param options - Configuration for the Shell tool\n * @returns A Shell tool that can be passed to `bindTools`\n *\n * @example\n * ```typescript\n * import { ChatOpenAI, tools } from \"@langchain/openai\";\n * import { exec } from \"child_process/promises\";\n *\n * const model = new ChatOpenAI({ model: \"gpt-5.1\" });\n *\n * // With execute callback for automatic command handling\n * const shellTool = tools.shell({\n * execute: async (action) => {\n * const outputs = await Promise.all(\n * action.commands.map(async (cmd) => {\n * try {\n * const { stdout, stderr } = await exec(cmd, {\n * timeout: action.timeout_ms ?? undefined,\n * });\n * return {\n * stdout,\n * stderr,\n * outcome: { type: \"exit\" as const, exit_code: 0 },\n * };\n * } catch (error) {\n * const timedOut = error.killed && error.signal === \"SIGTERM\";\n * return {\n * stdout: error.stdout ?? \"\",\n * stderr: error.stderr ?? String(error),\n * outcome: timedOut\n * ? { type: \"timeout\" as const }\n * : { type: \"exit\" as const, exit_code: error.code ?? 1 },\n * };\n * }\n * })\n * );\n * return {\n * output: outputs,\n * maxOutputLength: action.max_output_length,\n * };\n * },\n * });\n *\n * const llmWithShell = model.bindTools([shellTool]);\n * const response = await llmWithShell.invoke(\n * \"Find the largest PDF file in ~/Documents\"\n * );\n * ```\n *\n * @example\n * ```typescript\n * // Full shell loop example\n * async function shellLoop(model, task) {\n * let response = await model.invoke(task, {\n * tools: [tools.shell({ execute: myExecutor })],\n * });\n *\n * while (true) {\n * const shellCall = response.additional_kwargs.tool_outputs?.find(\n * (output) => output.type === \"shell_call\"\n * );\n *\n * if (!shellCall) break;\n *\n * // Execute commands (with proper sandboxing!)\n * const result = await executeCommands(shellCall.action);\n *\n * // Send output back to model\n * response = await model.invoke([\n * response,\n * {\n * type: \"shell_call_output\",\n * call_id: shellCall.call_id,\n * output: result.output,\n * max_output_length: result.maxOutputLength,\n * },\n * ], {\n * tools: [tools.shell({ execute: myExecutor })],\n * });\n * }\n *\n * return response;\n * }\n * ```\n *\n * @remarks\n * - Only available through the Responses API (not Chat Completions)\n * - Designed for use with `gpt-5.1` model\n * - Commands are provided as an array of strings that can be executed concurrently\n * - Action includes: `commands`, `timeout_ms`, `max_output_length`\n * - Always sandbox or validate commands before execution\n * - The `timeout_ms` from the model is only a hint—enforce your own limits\n * - If `max_output_length` exists in the action, always pass it back in the output\n * - Many CLI tools return non-zero exit codes for warnings; still capture stdout/stderr\n */\nexport function shell(options: ShellOptions) {\n // Wrapper that converts ShellResult to string for LangChain tool compatibility\n const executeWrapper = async (action: ShellAction): Promise<string> => {\n const result = await options.execute(action);\n // Return a JSON string representation for the tool result\n return JSON.stringify({\n output: result.output,\n max_output_length: result.maxOutputLength,\n });\n };\n\n const shellTool = tool(executeWrapper, {\n name: TOOL_NAME,\n description:\n \"Execute shell commands in a managed environment. Commands can be run concurrently.\",\n schema: ShellActionSchema,\n });\n\n shellTool.extras = {\n ...(shellTool.extras ?? {}),\n providerToolDefinition: {\n type: \"shell\",\n } satisfies ShellTool,\n };\n\n return shellTool as DynamicStructuredTool<\n typeof ShellActionSchema,\n ShellAction,\n unknown,\n string\n >;\n}\n"],"mappings":";;;;;AAYA,MAAa,oBAAoBA,SAAE,OAAO;CACxC,UAAUA,SAAE,MAAMA,SAAE,QAAQ,CAAC,CAAC,SAAS,qCAAqC;CAC5E,YAAYA,SACT,QAAQ,CACR,UAAU,CACV,SAAS,oDAAoD;CAChE,mBAAmBA,SAChB,QAAQ,CACR,UAAU,CACV,SACC,oEACD;AACJ,EAAC;AAiFF,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmIlB,SAAgB,MAAMC,SAAuB;CAE3C,MAAM,iBAAiB,OAAOC,WAAyC;EACrE,MAAM,SAAS,MAAM,QAAQ,QAAQ,OAAO;AAE5C,SAAO,KAAK,UAAU;GACpB,QAAQ,OAAO;GACf,mBAAmB,OAAO;EAC3B,EAAC;CACH;CAED,MAAM,6CAAiB,gBAAgB;EACrC,MAAM;EACN,aACE;EACF,QAAQ;CACT,EAAC;CAEF,UAAU,SAAS;EACjB,GAAI,UAAU,UAAU,CAAE;EAC1B,wBAAwB,EACtB,MAAM,QACP;CACF;AAED,QAAO;AAMR"}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { OpenAI as OpenAI$1 } from "openai";
|
|
2
|
+
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
3
|
+
import { z } from "zod/v4";
|
|
4
|
+
|
|
5
|
+
//#region src/tools/shell.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Re-export action type from OpenAI SDK for convenience.
|
|
9
|
+
* The action contains command details like commands array, timeout, and max output length.
|
|
10
|
+
*/
|
|
11
|
+
type ShellAction = OpenAI$1.Responses.ResponseFunctionShellToolCall.Action;
|
|
12
|
+
/**
|
|
13
|
+
* Result of a single shell command execution.
|
|
14
|
+
* Contains stdout, stderr, and the outcome (exit code or timeout).
|
|
15
|
+
*/
|
|
16
|
+
type ShellCommandOutput = OpenAI$1.Responses.ResponseFunctionShellCallOutputContent;
|
|
17
|
+
/**
|
|
18
|
+
* Outcome type for shell command execution - either exit with code or timeout.
|
|
19
|
+
*/
|
|
20
|
+
type ShellCallOutcome = ShellCommandOutput["outcome"];
|
|
21
|
+
/**
|
|
22
|
+
* Result of executing shell commands.
|
|
23
|
+
* Contains an array of outputs (one per command) and the max_output_length parameter.
|
|
24
|
+
*/
|
|
25
|
+
interface ShellResult {
|
|
26
|
+
/**
|
|
27
|
+
* Array of command outputs. Each entry corresponds to a command from the action.
|
|
28
|
+
* The order should match the order of commands in the action.
|
|
29
|
+
*/
|
|
30
|
+
output: ShellCommandOutput[];
|
|
31
|
+
/**
|
|
32
|
+
* The max_output_length from the action, which must be passed back to the API.
|
|
33
|
+
* If not provided in the action, can be omitted.
|
|
34
|
+
*/
|
|
35
|
+
maxOutputLength?: number | null;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Options for the Shell tool.
|
|
39
|
+
*/
|
|
40
|
+
interface ShellOptions {
|
|
41
|
+
/**
|
|
42
|
+
* Execute function that handles shell command execution.
|
|
43
|
+
* This function receives the action input containing the commands and limits,
|
|
44
|
+
* and should return a ShellResult with stdout, stderr, and outcome for each command.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* execute: async (action) => {
|
|
49
|
+
* const outputs = await Promise.all(
|
|
50
|
+
* action.commands.map(async (cmd) => {
|
|
51
|
+
* try {
|
|
52
|
+
* const { stdout, stderr } = await exec(cmd, {
|
|
53
|
+
* timeout: action.timeout_ms ?? undefined,
|
|
54
|
+
* });
|
|
55
|
+
* return {
|
|
56
|
+
* stdout,
|
|
57
|
+
* stderr,
|
|
58
|
+
* outcome: { type: "exit" as const, exit_code: 0 },
|
|
59
|
+
* };
|
|
60
|
+
* } catch (error) {
|
|
61
|
+
* const timedOut = error.killed && error.signal === "SIGTERM";
|
|
62
|
+
* return {
|
|
63
|
+
* stdout: error.stdout ?? "",
|
|
64
|
+
* stderr: error.stderr ?? String(error),
|
|
65
|
+
* outcome: timedOut
|
|
66
|
+
* ? { type: "timeout" as const }
|
|
67
|
+
* : { type: "exit" as const, exit_code: error.code ?? 1 },
|
|
68
|
+
* };
|
|
69
|
+
* }
|
|
70
|
+
* })
|
|
71
|
+
* );
|
|
72
|
+
* return {
|
|
73
|
+
* output: outputs,
|
|
74
|
+
* maxOutputLength: action.max_output_length,
|
|
75
|
+
* };
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
execute: (action: ShellAction) => ShellResult | Promise<ShellResult>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* OpenAI Shell tool type for the Responses API.
|
|
83
|
+
*/
|
|
84
|
+
type ShellTool = OpenAI$1.Responses.FunctionShellTool;
|
|
85
|
+
/**
|
|
86
|
+
* Creates a Shell tool that allows models to run shell commands through your integration.
|
|
87
|
+
*
|
|
88
|
+
* The shell tool allows the model to interact with your local computer through a controlled
|
|
89
|
+
* command-line interface. The model proposes shell commands; your integration executes them
|
|
90
|
+
* and returns the outputs. This creates a simple plan-execute loop that lets models inspect
|
|
91
|
+
* the system, run utilities, and gather data until they can finish the task.
|
|
92
|
+
*
|
|
93
|
+
* **Important**: The shell tool is available through the Responses API for use with `GPT-5.1`.
|
|
94
|
+
* It is not available on other models, or via the Chat Completions API.
|
|
95
|
+
*
|
|
96
|
+
* **When to use**:
|
|
97
|
+
* - **Automating filesystem or process diagnostics** – For example, "find the largest PDF
|
|
98
|
+
* under ~/Documents" or "show running gunicorn processes."
|
|
99
|
+
* - **Extending the model's capabilities** – Using built-in UNIX utilities, python runtime
|
|
100
|
+
* and other CLIs in your environment.
|
|
101
|
+
* - **Running multi-step build and test flows** – Chaining commands like `pip install` and `pytest`.
|
|
102
|
+
* - **Complex agentic coding workflows** – Using other tools like `apply_patch` to complete
|
|
103
|
+
* workflows that involve complex file operations.
|
|
104
|
+
*
|
|
105
|
+
* **How it works**:
|
|
106
|
+
* The tool operates in a continuous loop:
|
|
107
|
+
* 1. Model sends shell commands (`shell_call` with `commands` array)
|
|
108
|
+
* 2. Your code executes the commands (can be concurrent)
|
|
109
|
+
* 3. You return stdout, stderr, and outcome for each command
|
|
110
|
+
* 4. Repeat until the task is complete
|
|
111
|
+
*
|
|
112
|
+
* **Security Warning**: Running arbitrary shell commands can be dangerous.
|
|
113
|
+
* Always sandbox execution or add strict allow/deny-lists before forwarding
|
|
114
|
+
* a command to the system shell.
|
|
115
|
+
*
|
|
116
|
+
* @see {@link https://platform.openai.com/docs/guides/tools-shell | OpenAI Shell Documentation}
|
|
117
|
+
* @see {@link https://github.com/openai/codex | Codex CLI} for reference implementation.
|
|
118
|
+
*
|
|
119
|
+
* @param options - Configuration for the Shell tool
|
|
120
|
+
* @returns A Shell tool that can be passed to `bindTools`
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* import { ChatOpenAI, tools } from "@langchain/openai";
|
|
125
|
+
* import { exec } from "child_process/promises";
|
|
126
|
+
*
|
|
127
|
+
* const model = new ChatOpenAI({ model: "gpt-5.1" });
|
|
128
|
+
*
|
|
129
|
+
* // With execute callback for automatic command handling
|
|
130
|
+
* const shellTool = tools.shell({
|
|
131
|
+
* execute: async (action) => {
|
|
132
|
+
* const outputs = await Promise.all(
|
|
133
|
+
* action.commands.map(async (cmd) => {
|
|
134
|
+
* try {
|
|
135
|
+
* const { stdout, stderr } = await exec(cmd, {
|
|
136
|
+
* timeout: action.timeout_ms ?? undefined,
|
|
137
|
+
* });
|
|
138
|
+
* return {
|
|
139
|
+
* stdout,
|
|
140
|
+
* stderr,
|
|
141
|
+
* outcome: { type: "exit" as const, exit_code: 0 },
|
|
142
|
+
* };
|
|
143
|
+
* } catch (error) {
|
|
144
|
+
* const timedOut = error.killed && error.signal === "SIGTERM";
|
|
145
|
+
* return {
|
|
146
|
+
* stdout: error.stdout ?? "",
|
|
147
|
+
* stderr: error.stderr ?? String(error),
|
|
148
|
+
* outcome: timedOut
|
|
149
|
+
* ? { type: "timeout" as const }
|
|
150
|
+
* : { type: "exit" as const, exit_code: error.code ?? 1 },
|
|
151
|
+
* };
|
|
152
|
+
* }
|
|
153
|
+
* })
|
|
154
|
+
* );
|
|
155
|
+
* return {
|
|
156
|
+
* output: outputs,
|
|
157
|
+
* maxOutputLength: action.max_output_length,
|
|
158
|
+
* };
|
|
159
|
+
* },
|
|
160
|
+
* });
|
|
161
|
+
*
|
|
162
|
+
* const llmWithShell = model.bindTools([shellTool]);
|
|
163
|
+
* const response = await llmWithShell.invoke(
|
|
164
|
+
* "Find the largest PDF file in ~/Documents"
|
|
165
|
+
* );
|
|
166
|
+
* ```
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* // Full shell loop example
|
|
171
|
+
* async function shellLoop(model, task) {
|
|
172
|
+
* let response = await model.invoke(task, {
|
|
173
|
+
* tools: [tools.shell({ execute: myExecutor })],
|
|
174
|
+
* });
|
|
175
|
+
*
|
|
176
|
+
* while (true) {
|
|
177
|
+
* const shellCall = response.additional_kwargs.tool_outputs?.find(
|
|
178
|
+
* (output) => output.type === "shell_call"
|
|
179
|
+
* );
|
|
180
|
+
*
|
|
181
|
+
* if (!shellCall) break;
|
|
182
|
+
*
|
|
183
|
+
* // Execute commands (with proper sandboxing!)
|
|
184
|
+
* const result = await executeCommands(shellCall.action);
|
|
185
|
+
*
|
|
186
|
+
* // Send output back to model
|
|
187
|
+
* response = await model.invoke([
|
|
188
|
+
* response,
|
|
189
|
+
* {
|
|
190
|
+
* type: "shell_call_output",
|
|
191
|
+
* call_id: shellCall.call_id,
|
|
192
|
+
* output: result.output,
|
|
193
|
+
* max_output_length: result.maxOutputLength,
|
|
194
|
+
* },
|
|
195
|
+
* ], {
|
|
196
|
+
* tools: [tools.shell({ execute: myExecutor })],
|
|
197
|
+
* });
|
|
198
|
+
* }
|
|
199
|
+
*
|
|
200
|
+
* return response;
|
|
201
|
+
* }
|
|
202
|
+
* ```
|
|
203
|
+
*
|
|
204
|
+
* @remarks
|
|
205
|
+
* - Only available through the Responses API (not Chat Completions)
|
|
206
|
+
* - Designed for use with `gpt-5.1` model
|
|
207
|
+
* - Commands are provided as an array of strings that can be executed concurrently
|
|
208
|
+
* - Action includes: `commands`, `timeout_ms`, `max_output_length`
|
|
209
|
+
* - Always sandbox or validate commands before execution
|
|
210
|
+
* - The `timeout_ms` from the model is only a hint—enforce your own limits
|
|
211
|
+
* - If `max_output_length` exists in the action, always pass it back in the output
|
|
212
|
+
* - Many CLI tools return non-zero exit codes for warnings; still capture stdout/stderr
|
|
213
|
+
*/
|
|
214
|
+
declare function shell(options: ShellOptions): DynamicStructuredTool<z.ZodObject<{
|
|
215
|
+
commands: z.ZodArray<z.ZodString>;
|
|
216
|
+
timeout_ms: z.ZodOptional<z.ZodNumber>;
|
|
217
|
+
max_output_length: z.ZodOptional<z.ZodNumber>;
|
|
218
|
+
}, z.core.$strip>, OpenAI$1.Responses.ResponseFunctionShellToolCall.Action, unknown, string>;
|
|
219
|
+
//#endregion
|
|
220
|
+
export { ShellAction, ShellCallOutcome, ShellCommandOutput, ShellOptions, ShellResult, ShellTool, shell };
|
|
221
|
+
//# sourceMappingURL=shell.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell.d.cts","names":["z","OpenAI","OpenAIClient","DynamicStructuredTool","ShellAction","Responses","ResponseFunctionShellToolCall","Action","ShellActionSchema","ZodString","ZodArray","ZodNumber","ZodOptional","core","$strip","ZodObject","ShellCommandOutput","ResponseFunctionShellCallOutputContent","ShellCallOutcome","ShellResult","ShellOptions","Promise","ShellTool","FunctionShellTool","shell"],"sources":["../../src/tools/shell.d.ts"],"sourcesContent":["import { z } from \"zod/v4\";\nimport { OpenAI as OpenAIClient } from \"openai\";\nimport { type DynamicStructuredTool } from \"@langchain/core/tools\";\n/**\n * Re-export action type from OpenAI SDK for convenience.\n * The action contains command details like commands array, timeout, and max output length.\n */\nexport type ShellAction = OpenAIClient.Responses.ResponseFunctionShellToolCall.Action;\nexport declare const ShellActionSchema: z.ZodObject<{\n commands: z.ZodArray<z.ZodString>;\n timeout_ms: z.ZodOptional<z.ZodNumber>;\n max_output_length: z.ZodOptional<z.ZodNumber>;\n}, z.core.$strip>;\n/**\n * Result of a single shell command execution.\n * Contains stdout, stderr, and the outcome (exit code or timeout).\n */\nexport type ShellCommandOutput = OpenAIClient.Responses.ResponseFunctionShellCallOutputContent;\n/**\n * Outcome type for shell command execution - either exit with code or timeout.\n */\nexport type ShellCallOutcome = ShellCommandOutput[\"outcome\"];\n/**\n * Result of executing shell commands.\n * Contains an array of outputs (one per command) and the max_output_length parameter.\n */\nexport interface ShellResult {\n /**\n * Array of command outputs. Each entry corresponds to a command from the action.\n * The order should match the order of commands in the action.\n */\n output: ShellCommandOutput[];\n /**\n * The max_output_length from the action, which must be passed back to the API.\n * If not provided in the action, can be omitted.\n */\n maxOutputLength?: number | null;\n}\n/**\n * Options for the Shell tool.\n */\nexport interface ShellOptions {\n /**\n * Execute function that handles shell command execution.\n * This function receives the action input containing the commands and limits,\n * and should return a ShellResult with stdout, stderr, and outcome for each command.\n *\n * @example\n * ```typescript\n * execute: async (action) => {\n * const outputs = await Promise.all(\n * action.commands.map(async (cmd) => {\n * try {\n * const { stdout, stderr } = await exec(cmd, {\n * timeout: action.timeout_ms ?? undefined,\n * });\n * return {\n * stdout,\n * stderr,\n * outcome: { type: \"exit\" as const, exit_code: 0 },\n * };\n * } catch (error) {\n * const timedOut = error.killed && error.signal === \"SIGTERM\";\n * return {\n * stdout: error.stdout ?? \"\",\n * stderr: error.stderr ?? String(error),\n * outcome: timedOut\n * ? { type: \"timeout\" as const }\n * : { type: \"exit\" as const, exit_code: error.code ?? 1 },\n * };\n * }\n * })\n * );\n * return {\n * output: outputs,\n * maxOutputLength: action.max_output_length,\n * };\n * }\n * ```\n */\n execute: (action: ShellAction) => ShellResult | Promise<ShellResult>;\n}\n/**\n * OpenAI Shell tool type for the Responses API.\n */\nexport type ShellTool = OpenAIClient.Responses.FunctionShellTool;\n/**\n * Creates a Shell tool that allows models to run shell commands through your integration.\n *\n * The shell tool allows the model to interact with your local computer through a controlled\n * command-line interface. The model proposes shell commands; your integration executes them\n * and returns the outputs. This creates a simple plan-execute loop that lets models inspect\n * the system, run utilities, and gather data until they can finish the task.\n *\n * **Important**: The shell tool is available through the Responses API for use with `GPT-5.1`.\n * It is not available on other models, or via the Chat Completions API.\n *\n * **When to use**:\n * - **Automating filesystem or process diagnostics** – For example, \"find the largest PDF\n * under ~/Documents\" or \"show running gunicorn processes.\"\n * - **Extending the model's capabilities** – Using built-in UNIX utilities, python runtime\n * and other CLIs in your environment.\n * - **Running multi-step build and test flows** – Chaining commands like `pip install` and `pytest`.\n * - **Complex agentic coding workflows** – Using other tools like `apply_patch` to complete\n * workflows that involve complex file operations.\n *\n * **How it works**:\n * The tool operates in a continuous loop:\n * 1. Model sends shell commands (`shell_call` with `commands` array)\n * 2. Your code executes the commands (can be concurrent)\n * 3. You return stdout, stderr, and outcome for each command\n * 4. Repeat until the task is complete\n *\n * **Security Warning**: Running arbitrary shell commands can be dangerous.\n * Always sandbox execution or add strict allow/deny-lists before forwarding\n * a command to the system shell.\n *\n * @see {@link https://platform.openai.com/docs/guides/tools-shell | OpenAI Shell Documentation}\n * @see {@link https://github.com/openai/codex | Codex CLI} for reference implementation.\n *\n * @param options - Configuration for the Shell tool\n * @returns A Shell tool that can be passed to `bindTools`\n *\n * @example\n * ```typescript\n * import { ChatOpenAI, tools } from \"@langchain/openai\";\n * import { exec } from \"child_process/promises\";\n *\n * const model = new ChatOpenAI({ model: \"gpt-5.1\" });\n *\n * // With execute callback for automatic command handling\n * const shellTool = tools.shell({\n * execute: async (action) => {\n * const outputs = await Promise.all(\n * action.commands.map(async (cmd) => {\n * try {\n * const { stdout, stderr } = await exec(cmd, {\n * timeout: action.timeout_ms ?? undefined,\n * });\n * return {\n * stdout,\n * stderr,\n * outcome: { type: \"exit\" as const, exit_code: 0 },\n * };\n * } catch (error) {\n * const timedOut = error.killed && error.signal === \"SIGTERM\";\n * return {\n * stdout: error.stdout ?? \"\",\n * stderr: error.stderr ?? String(error),\n * outcome: timedOut\n * ? { type: \"timeout\" as const }\n * : { type: \"exit\" as const, exit_code: error.code ?? 1 },\n * };\n * }\n * })\n * );\n * return {\n * output: outputs,\n * maxOutputLength: action.max_output_length,\n * };\n * },\n * });\n *\n * const llmWithShell = model.bindTools([shellTool]);\n * const response = await llmWithShell.invoke(\n * \"Find the largest PDF file in ~/Documents\"\n * );\n * ```\n *\n * @example\n * ```typescript\n * // Full shell loop example\n * async function shellLoop(model, task) {\n * let response = await model.invoke(task, {\n * tools: [tools.shell({ execute: myExecutor })],\n * });\n *\n * while (true) {\n * const shellCall = response.additional_kwargs.tool_outputs?.find(\n * (output) => output.type === \"shell_call\"\n * );\n *\n * if (!shellCall) break;\n *\n * // Execute commands (with proper sandboxing!)\n * const result = await executeCommands(shellCall.action);\n *\n * // Send output back to model\n * response = await model.invoke([\n * response,\n * {\n * type: \"shell_call_output\",\n * call_id: shellCall.call_id,\n * output: result.output,\n * max_output_length: result.maxOutputLength,\n * },\n * ], {\n * tools: [tools.shell({ execute: myExecutor })],\n * });\n * }\n *\n * return response;\n * }\n * ```\n *\n * @remarks\n * - Only available through the Responses API (not Chat Completions)\n * - Designed for use with `gpt-5.1` model\n * - Commands are provided as an array of strings that can be executed concurrently\n * - Action includes: `commands`, `timeout_ms`, `max_output_length`\n * - Always sandbox or validate commands before execution\n * - The `timeout_ms` from the model is only a hint—enforce your own limits\n * - If `max_output_length` exists in the action, always pass it back in the output\n * - Many CLI tools return non-zero exit codes for warnings; still capture stdout/stderr\n */\nexport declare function shell(options: ShellOptions): DynamicStructuredTool<z.ZodObject<{\n commands: z.ZodArray<z.ZodString>;\n timeout_ms: z.ZodOptional<z.ZodNumber>;\n max_output_length: z.ZodOptional<z.ZodNumber>;\n}, z.core.$strip>, OpenAIClient.Responses.ResponseFunctionShellToolCall.Action, unknown, string>;\n//# sourceMappingURL=shell.d.ts.map"],"mappings":";;;;;;;;AAOA;AAUA;AAIYkB,KAdAd,WAAAA,GAAcF,QAAAA,CAAaG,SAAAA,CAAUC,6BAAAA,CAA8BC,MAc9B;;AA2DU;AAK3D;AAkIA;AAAuCa,KAtM3BJ,kBAAAA,GAAqBd,QAAAA,CAAaG,SAAAA,CAAUY,sCAsMjBG;;;;AAErBR,KApMNM,gBAAAA,GAAmBF,kBAoMbJ,CAAAA,SAAAA,CAAAA;;;;;AAECV,UAjMFiB,WAAAA,CAiMyBb;EAJYH;AAAqB;;;UAxL/Da;;;;;;;;;;UAUKI,YAAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;oBAuCKhB,gBAAgBe,cAAcE,QAAQF;;;;;KAKhDG,SAAAA,GAAYpB,QAAAA,CAAaG,SAAAA,CAAUkB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAkIvBC,KAAAA,UAAeJ,eAAejB,sBAAsBH,CAAAA,CAAEe;YAChEf,CAAAA,CAAEU,SAASV,CAAAA,CAAES;cACXT,CAAAA,CAAEY,YAAYZ,CAAAA,CAAEW;qBACTX,CAAAA,CAAEY,YAAYZ,CAAAA,CAAEW;GACpCX,CAAAA,CAAEa,IAAAA,CAAKC,SAASZ,QAAAA,CAAaG,SAAAA,CAAUC,6BAAAA,CAA8BC"}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { OpenAI as OpenAI$1 } from "openai";
|
|
2
|
+
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
3
|
+
import { z } from "zod/v4";
|
|
4
|
+
|
|
5
|
+
//#region src/tools/shell.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Re-export action type from OpenAI SDK for convenience.
|
|
9
|
+
* The action contains command details like commands array, timeout, and max output length.
|
|
10
|
+
*/
|
|
11
|
+
type ShellAction = OpenAI$1.Responses.ResponseFunctionShellToolCall.Action;
|
|
12
|
+
/**
|
|
13
|
+
* Result of a single shell command execution.
|
|
14
|
+
* Contains stdout, stderr, and the outcome (exit code or timeout).
|
|
15
|
+
*/
|
|
16
|
+
type ShellCommandOutput = OpenAI$1.Responses.ResponseFunctionShellCallOutputContent;
|
|
17
|
+
/**
|
|
18
|
+
* Outcome type for shell command execution - either exit with code or timeout.
|
|
19
|
+
*/
|
|
20
|
+
type ShellCallOutcome = ShellCommandOutput["outcome"];
|
|
21
|
+
/**
|
|
22
|
+
* Result of executing shell commands.
|
|
23
|
+
* Contains an array of outputs (one per command) and the max_output_length parameter.
|
|
24
|
+
*/
|
|
25
|
+
interface ShellResult {
|
|
26
|
+
/**
|
|
27
|
+
* Array of command outputs. Each entry corresponds to a command from the action.
|
|
28
|
+
* The order should match the order of commands in the action.
|
|
29
|
+
*/
|
|
30
|
+
output: ShellCommandOutput[];
|
|
31
|
+
/**
|
|
32
|
+
* The max_output_length from the action, which must be passed back to the API.
|
|
33
|
+
* If not provided in the action, can be omitted.
|
|
34
|
+
*/
|
|
35
|
+
maxOutputLength?: number | null;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Options for the Shell tool.
|
|
39
|
+
*/
|
|
40
|
+
interface ShellOptions {
|
|
41
|
+
/**
|
|
42
|
+
* Execute function that handles shell command execution.
|
|
43
|
+
* This function receives the action input containing the commands and limits,
|
|
44
|
+
* and should return a ShellResult with stdout, stderr, and outcome for each command.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* execute: async (action) => {
|
|
49
|
+
* const outputs = await Promise.all(
|
|
50
|
+
* action.commands.map(async (cmd) => {
|
|
51
|
+
* try {
|
|
52
|
+
* const { stdout, stderr } = await exec(cmd, {
|
|
53
|
+
* timeout: action.timeout_ms ?? undefined,
|
|
54
|
+
* });
|
|
55
|
+
* return {
|
|
56
|
+
* stdout,
|
|
57
|
+
* stderr,
|
|
58
|
+
* outcome: { type: "exit" as const, exit_code: 0 },
|
|
59
|
+
* };
|
|
60
|
+
* } catch (error) {
|
|
61
|
+
* const timedOut = error.killed && error.signal === "SIGTERM";
|
|
62
|
+
* return {
|
|
63
|
+
* stdout: error.stdout ?? "",
|
|
64
|
+
* stderr: error.stderr ?? String(error),
|
|
65
|
+
* outcome: timedOut
|
|
66
|
+
* ? { type: "timeout" as const }
|
|
67
|
+
* : { type: "exit" as const, exit_code: error.code ?? 1 },
|
|
68
|
+
* };
|
|
69
|
+
* }
|
|
70
|
+
* })
|
|
71
|
+
* );
|
|
72
|
+
* return {
|
|
73
|
+
* output: outputs,
|
|
74
|
+
* maxOutputLength: action.max_output_length,
|
|
75
|
+
* };
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
execute: (action: ShellAction) => ShellResult | Promise<ShellResult>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* OpenAI Shell tool type for the Responses API.
|
|
83
|
+
*/
|
|
84
|
+
type ShellTool = OpenAI$1.Responses.FunctionShellTool;
|
|
85
|
+
/**
|
|
86
|
+
* Creates a Shell tool that allows models to run shell commands through your integration.
|
|
87
|
+
*
|
|
88
|
+
* The shell tool allows the model to interact with your local computer through a controlled
|
|
89
|
+
* command-line interface. The model proposes shell commands; your integration executes them
|
|
90
|
+
* and returns the outputs. This creates a simple plan-execute loop that lets models inspect
|
|
91
|
+
* the system, run utilities, and gather data until they can finish the task.
|
|
92
|
+
*
|
|
93
|
+
* **Important**: The shell tool is available through the Responses API for use with `GPT-5.1`.
|
|
94
|
+
* It is not available on other models, or via the Chat Completions API.
|
|
95
|
+
*
|
|
96
|
+
* **When to use**:
|
|
97
|
+
* - **Automating filesystem or process diagnostics** – For example, "find the largest PDF
|
|
98
|
+
* under ~/Documents" or "show running gunicorn processes."
|
|
99
|
+
* - **Extending the model's capabilities** – Using built-in UNIX utilities, python runtime
|
|
100
|
+
* and other CLIs in your environment.
|
|
101
|
+
* - **Running multi-step build and test flows** – Chaining commands like `pip install` and `pytest`.
|
|
102
|
+
* - **Complex agentic coding workflows** – Using other tools like `apply_patch` to complete
|
|
103
|
+
* workflows that involve complex file operations.
|
|
104
|
+
*
|
|
105
|
+
* **How it works**:
|
|
106
|
+
* The tool operates in a continuous loop:
|
|
107
|
+
* 1. Model sends shell commands (`shell_call` with `commands` array)
|
|
108
|
+
* 2. Your code executes the commands (can be concurrent)
|
|
109
|
+
* 3. You return stdout, stderr, and outcome for each command
|
|
110
|
+
* 4. Repeat until the task is complete
|
|
111
|
+
*
|
|
112
|
+
* **Security Warning**: Running arbitrary shell commands can be dangerous.
|
|
113
|
+
* Always sandbox execution or add strict allow/deny-lists before forwarding
|
|
114
|
+
* a command to the system shell.
|
|
115
|
+
*
|
|
116
|
+
* @see {@link https://platform.openai.com/docs/guides/tools-shell | OpenAI Shell Documentation}
|
|
117
|
+
* @see {@link https://github.com/openai/codex | Codex CLI} for reference implementation.
|
|
118
|
+
*
|
|
119
|
+
* @param options - Configuration for the Shell tool
|
|
120
|
+
* @returns A Shell tool that can be passed to `bindTools`
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* import { ChatOpenAI, tools } from "@langchain/openai";
|
|
125
|
+
* import { exec } from "child_process/promises";
|
|
126
|
+
*
|
|
127
|
+
* const model = new ChatOpenAI({ model: "gpt-5.1" });
|
|
128
|
+
*
|
|
129
|
+
* // With execute callback for automatic command handling
|
|
130
|
+
* const shellTool = tools.shell({
|
|
131
|
+
* execute: async (action) => {
|
|
132
|
+
* const outputs = await Promise.all(
|
|
133
|
+
* action.commands.map(async (cmd) => {
|
|
134
|
+
* try {
|
|
135
|
+
* const { stdout, stderr } = await exec(cmd, {
|
|
136
|
+
* timeout: action.timeout_ms ?? undefined,
|
|
137
|
+
* });
|
|
138
|
+
* return {
|
|
139
|
+
* stdout,
|
|
140
|
+
* stderr,
|
|
141
|
+
* outcome: { type: "exit" as const, exit_code: 0 },
|
|
142
|
+
* };
|
|
143
|
+
* } catch (error) {
|
|
144
|
+
* const timedOut = error.killed && error.signal === "SIGTERM";
|
|
145
|
+
* return {
|
|
146
|
+
* stdout: error.stdout ?? "",
|
|
147
|
+
* stderr: error.stderr ?? String(error),
|
|
148
|
+
* outcome: timedOut
|
|
149
|
+
* ? { type: "timeout" as const }
|
|
150
|
+
* : { type: "exit" as const, exit_code: error.code ?? 1 },
|
|
151
|
+
* };
|
|
152
|
+
* }
|
|
153
|
+
* })
|
|
154
|
+
* );
|
|
155
|
+
* return {
|
|
156
|
+
* output: outputs,
|
|
157
|
+
* maxOutputLength: action.max_output_length,
|
|
158
|
+
* };
|
|
159
|
+
* },
|
|
160
|
+
* });
|
|
161
|
+
*
|
|
162
|
+
* const llmWithShell = model.bindTools([shellTool]);
|
|
163
|
+
* const response = await llmWithShell.invoke(
|
|
164
|
+
* "Find the largest PDF file in ~/Documents"
|
|
165
|
+
* );
|
|
166
|
+
* ```
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* // Full shell loop example
|
|
171
|
+
* async function shellLoop(model, task) {
|
|
172
|
+
* let response = await model.invoke(task, {
|
|
173
|
+
* tools: [tools.shell({ execute: myExecutor })],
|
|
174
|
+
* });
|
|
175
|
+
*
|
|
176
|
+
* while (true) {
|
|
177
|
+
* const shellCall = response.additional_kwargs.tool_outputs?.find(
|
|
178
|
+
* (output) => output.type === "shell_call"
|
|
179
|
+
* );
|
|
180
|
+
*
|
|
181
|
+
* if (!shellCall) break;
|
|
182
|
+
*
|
|
183
|
+
* // Execute commands (with proper sandboxing!)
|
|
184
|
+
* const result = await executeCommands(shellCall.action);
|
|
185
|
+
*
|
|
186
|
+
* // Send output back to model
|
|
187
|
+
* response = await model.invoke([
|
|
188
|
+
* response,
|
|
189
|
+
* {
|
|
190
|
+
* type: "shell_call_output",
|
|
191
|
+
* call_id: shellCall.call_id,
|
|
192
|
+
* output: result.output,
|
|
193
|
+
* max_output_length: result.maxOutputLength,
|
|
194
|
+
* },
|
|
195
|
+
* ], {
|
|
196
|
+
* tools: [tools.shell({ execute: myExecutor })],
|
|
197
|
+
* });
|
|
198
|
+
* }
|
|
199
|
+
*
|
|
200
|
+
* return response;
|
|
201
|
+
* }
|
|
202
|
+
* ```
|
|
203
|
+
*
|
|
204
|
+
* @remarks
|
|
205
|
+
* - Only available through the Responses API (not Chat Completions)
|
|
206
|
+
* - Designed for use with `gpt-5.1` model
|
|
207
|
+
* - Commands are provided as an array of strings that can be executed concurrently
|
|
208
|
+
* - Action includes: `commands`, `timeout_ms`, `max_output_length`
|
|
209
|
+
* - Always sandbox or validate commands before execution
|
|
210
|
+
* - The `timeout_ms` from the model is only a hint—enforce your own limits
|
|
211
|
+
* - If `max_output_length` exists in the action, always pass it back in the output
|
|
212
|
+
* - Many CLI tools return non-zero exit codes for warnings; still capture stdout/stderr
|
|
213
|
+
*/
|
|
214
|
+
declare function shell(options: ShellOptions): DynamicStructuredTool<z.ZodObject<{
|
|
215
|
+
commands: z.ZodArray<z.ZodString>;
|
|
216
|
+
timeout_ms: z.ZodOptional<z.ZodNumber>;
|
|
217
|
+
max_output_length: z.ZodOptional<z.ZodNumber>;
|
|
218
|
+
}, z.core.$strip>, OpenAI$1.Responses.ResponseFunctionShellToolCall.Action, unknown, string>;
|
|
219
|
+
//#endregion
|
|
220
|
+
export { ShellAction, ShellCallOutcome, ShellCommandOutput, ShellOptions, ShellResult, ShellTool, shell };
|
|
221
|
+
//# sourceMappingURL=shell.d.ts.map
|