@langchain/anthropic
Version:
Anthropic integrations for LangChain.js
1 lines • 4.24 kB
Source Map (JSON)
{"version":3,"file":"bash.cjs","names":["options?: Bash20250124Options","Bash20250124CommandSchema"],"sources":["../../src/tools/bash.ts"],"sourcesContent":["import Anthropic from \"@anthropic-ai/sdk\";\nimport { tool } from \"@langchain/core/tools\";\nimport type { DynamicStructuredTool, ToolRuntime } from \"@langchain/core/tools\";\n\nimport {\n Bash20250124CommandSchema,\n type Bash20250124Command,\n} from \"./types.js\";\n\n/**\n * Options for the bash tool.\n */\nexport interface Bash20250124Options {\n /**\n * Optional execute function that handles bash command execution.\n * This function receives the command input and should return the result\n * (stdout and stderr combined, or an error message).\n */\n execute?: (args: Bash20250124Command) => string | Promise<string>;\n}\n\n/**\n * Creates an Anthropic bash tool for Claude 4 models and Claude 3.7 that enables\n * shell command execution in a persistent bash session.\n *\n * The bash tool provides Claude with:\n * - **Persistent bash session**: Maintains state between commands\n * - **Shell command execution**: Run any shell command\n * - **Environment access**: Access to environment variables and working directory\n * - **Command chaining**: Support for pipes, redirects, and scripting\n *\n * Available commands:\n * - Execute a command: `{ command: \"ls -la\" }`\n * - Restart the session: `{ restart: true }`\n *\n * @warning The bash tool provides direct system access. Implement safety measures\n * such as running in isolated environments (Docker/VM), command filtering,\n * and resource limits.\n *\n * @example\n * ```typescript\n * import { ChatAnthropic, tools } from \"@langchain/anthropic\";\n * import { execSync } from \"child_process\";\n *\n * const llm = new ChatAnthropic({\n * model: \"claude-sonnet-4-5-20250929\",\n * });\n *\n * const bash = tools.bash_20250124({\n * execute: async (args) => {\n * if (args.restart) {\n * // Reset session state\n * return \"Bash session restarted\";\n * }\n * try {\n * const output = execSync(args.command!, {\n * encoding: \"utf-8\",\n * timeout: 30000,\n * });\n * return output;\n * } catch (error) {\n * return `Error: ${error.message}`;\n * }\n * },\n * });\n *\n * const llmWithBash = llm.bindTools([bash]);\n * const response = await llmWithBash.invoke(\n * \"List all Python files in the current directory\"\n * );\n *\n * // Outputs: \"bash\"\n * console.log(response.tool_calls[0].name);\n * // Outputs: \"ls -la *.py 2>/dev/null || echo \\\"No Python files found in the current directory\\\"\n * console.log(response.tool_calls[0].args.command);\n * ```\n *\n * @example Multi-step automation\n * ```typescript\n * // Claude can chain commands in a persistent session:\n * // 1. cd /tmp\n * // 2. echo \"Hello\" > test.txt\n * // 3. cat test.txt // Works because we're still in /tmp\n * ```\n *\n * @param options - Configuration options for the bash tool\n * @param options.execute - Function that handles bash command execution\n * @returns The bash tool object that can be passed to `bindTools`\n *\n * @see https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/bash-tool\n */\nexport function bash_20250124(options?: Bash20250124Options) {\n const name = \"bash\";\n const bashTool = tool(\n options?.execute as (\n input: unknown,\n runtime: ToolRuntime<unknown, unknown>\n ) => string | Promise<string>,\n {\n name,\n description: \"A tool for executing bash commands\",\n schema: Bash20250124CommandSchema,\n }\n );\n\n bashTool.extras = {\n ...(bashTool.extras ?? {}),\n providerToolDefinition: {\n type: \"bash_20250124\",\n name,\n } satisfies Anthropic.Beta.BetaToolBash20250124,\n };\n\n return bashTool as DynamicStructuredTool<\n typeof Bash20250124CommandSchema,\n Bash20250124Command,\n unknown,\n string\n >;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2FA,SAAgB,cAAcA,SAA+B;CAC3D,MAAM,OAAO;CACb,MAAM,4CACJ,SAAS,SAIT;EACE;EACA,aAAa;EACb,QAAQC;CACT,EACF;CAED,SAAS,SAAS;EAChB,GAAI,SAAS,UAAU,CAAE;EACzB,wBAAwB;GACtB,MAAM;GACN;EACD;CACF;AAED,QAAO;AAMR"}