@langchain/anthropic
Version:
Anthropic integrations for LangChain.js
1 lines • 9.87 kB
Source Map (JSON)
{"version":3,"file":"computer.cjs","names":["options: Computer20251124Options","Computer20251124ActionSchema","options: Computer20250124Options","Computer20250124ActionSchema"],"sources":["../../src/tools/computer.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport Anthropic from \"@anthropic-ai/sdk\";\nimport { tool } from \"@langchain/core/tools\";\nimport { ToolMessage } from \"@langchain/core/messages\";\nimport type { DynamicStructuredTool, ToolRuntime } from \"@langchain/core/tools\";\n\nimport type {\n Computer20251124Action,\n Computer20250124Action,\n} from \"./types.js\";\nimport {\n Computer20251124ActionSchema,\n Computer20250124ActionSchema,\n} from \"./types.js\";\n\nconst TOOL_NAME = \"computer\";\n\nexport type ComputerUseReturnType =\n | string\n | Promise<string>\n | ToolMessage<any>\n | Promise<ToolMessage<any>>;\n\n/**\n * Options for the computer use tool (Claude Opus 4.5 only version).\n *\n * @template TState - The type of the state schema (when used with `ReactAgent`)\n * @template TContext - The type of the context schema (when used with `ReactAgent`)\n */\nexport interface Computer20251124Options<TState = any, TContext = any> {\n /**\n * The width of the display in pixels.\n */\n displayWidthPx: number;\n /**\n * The height of the display in pixels.\n */\n displayHeightPx: number;\n /**\n * Optional display number for X11 environments.\n */\n displayNumber?: number;\n /**\n * Enable zoom action for detailed screen region inspection.\n * When enabled, Claude can zoom into specific screen regions.\n * @default false\n */\n enableZoom?: boolean;\n /**\n * Optional execute function that handles computer action execution.\n * This function receives the action input and should return the result\n * (typically a base64-encoded screenshot or action confirmation).\n */\n execute?: (\n args: Computer20251124Action,\n runtime: ToolRuntime<TState, TContext>\n ) => ComputerUseReturnType;\n}\n\n/**\n * Creates an Anthropic computer use tool for Claude Opus 4.5 that provides\n * screenshot capabilities and mouse/keyboard control for autonomous desktop interaction.\n *\n * The computer use tool enables Claude to interact with desktop environments through:\n * - **Screenshot capture**: See what's currently displayed on screen\n * - **Mouse control**: Click, drag, and move the cursor\n * - **Keyboard input**: Type text and use keyboard shortcuts\n * - **Zoom**: View specific screen regions at full resolution (when enabled)\n *\n * @warning Computer use is a beta feature with unique risks. Use a dedicated virtual machine\n * or container with minimal privileges. Avoid giving access to sensitive data.\n *\n * @see {@link https://platform.claude.com/docs/en/agents-and-tools/tool-use/computer-use-tool | Anthropic Computer Use Documentation}\n *\n * @example\n * ```typescript\n * import { ChatAnthropic, tools } from \"@langchain/anthropic\";\n *\n * const llm = new ChatAnthropic({\n * model: \"claude-opus-4-5-20251101\",\n * clientOptions: {\n * defaultHeaders: {\n * \"anthropic-beta\": \"computer-use-2025-11-24\",\n * },\n * },\n * });\n *\n * const computer = tools.computer_20251124({\n * displayWidthPx: 1024,\n * displayHeightPx: 768,\n * displayNumber: 1,\n * enableZoom: true,\n * execute: async (action) => {\n * if (action.action === \"screenshot\") {\n * // Capture and return base64-encoded screenshot\n * return captureScreenshot();\n * }\n * if (action.action === \"left_click\") {\n * // Click at the specified coordinates\n * await click(action.coordinate[0], action.coordinate[1]);\n * return captureScreenshot();\n * }\n * // Handle other actions...\n * },\n * });\n *\n * const llmWithComputer = llm.bindTools([computer]);\n * const response = await llmWithComputer.invoke(\n * \"Save a picture of a cat to my desktop.\"\n * );\n * ```\n *\n * @param options - Configuration options for the computer use tool\n * @returns The computer use tool object that can be passed to `bindTools`\n */\nexport function computer_20251124(options: Computer20251124Options) {\n const name = TOOL_NAME;\n const computerTool = tool(\n options.execute as (\n input: unknown,\n runtime: ToolRuntime<unknown, unknown>\n ) => ComputerUseReturnType,\n {\n name,\n schema: Computer20251124ActionSchema,\n }\n );\n\n computerTool.extras = {\n ...(computerTool.extras ?? {}),\n providerToolDefinition: {\n type: \"computer_20251124\",\n name,\n display_width_px: options.displayWidthPx,\n display_height_px: options.displayHeightPx,\n ...(options.displayNumber !== undefined && {\n display_number: options.displayNumber,\n }),\n ...(options.enableZoom !== undefined && {\n enable_zoom: options.enableZoom,\n }),\n } satisfies Anthropic.Beta.BetaToolComputerUse20251124,\n };\n\n return computerTool as DynamicStructuredTool<\n typeof Computer20251124ActionSchema,\n Computer20251124Action,\n any,\n ToolMessage<any>\n >;\n}\n\n/**\n * Options for the computer use tool.\n *\n * Supported models: Claude Sonnet 4.5, Haiku 4.5, Opus 4.1, Sonnet 4, Opus 4, and Sonnet 3.7 versions.\n */\nexport interface Computer20250124Options<TState = any, TContext = any> {\n /**\n * The width of the display in pixels.\n */\n displayWidthPx: number;\n /**\n * The height of the display in pixels.\n */\n displayHeightPx: number;\n /**\n * Optional display number for X11 environments.\n */\n displayNumber?: number;\n /**\n * Optional execute function that handles computer action execution.\n * This function receives the action input and should return the result\n * (typically a base64-encoded screenshot or action confirmation).\n */\n execute?: (\n args: Computer20250124Action,\n runtime: ToolRuntime<TState, TContext>\n ) => ComputerUseReturnType;\n}\n\n/**\n * Creates an Anthropic computer use tool that provides screenshot capabilities and mouse/keyboard control\n * for autonomous desktop interaction.\n *\n * Supported models: Claude Sonnet 4.5, Haiku 4.5, Opus 4.1, Sonnet 4, Opus 4, and Sonnet 3.7 versions.\n *\n * The computer use tool enables Claude to interact with desktop environments through:\n * - **Screenshot capture**: See what's currently displayed on screen\n * - **Mouse control**: Click, drag, and move the cursor\n * - **Keyboard input**: Type text and use keyboard shortcuts\n *\n * @warning Computer use is a beta feature with unique risks. Use a dedicated virtual machine\n * or container with minimal privileges. Avoid giving access to sensitive data.\n *\n * @see {@link https://platform.claude.com/docs/en/agents-and-tools/tool-use/computer-use-tool | Anthropic Computer Use Documentation}\n *\n * @example\n * ```typescript\n * import { ChatAnthropic, tools } from \"@langchain/anthropic\";\n *\n * const llm = new ChatAnthropic({\n * model: \"claude-sonnet-4-5-20250929\",\n * clientOptions: {\n * defaultHeaders: {\n * \"anthropic-beta\": \"computer-use-2025-01-24\",\n * },\n * },\n * });\n *\n * const computer = tools.computer_20250124({\n * displayWidthPx: 1024,\n * displayHeightPx: 768,\n * displayNumber: 1,\n * execute: async (action) => {\n * if (action.action === \"screenshot\") {\n * // Capture and return base64-encoded screenshot\n * return captureScreenshot();\n * }\n * if (action.action === \"left_click\") {\n * // Click at the specified coordinates\n * await click(action.coordinate[0], action.coordinate[1]);\n * return captureScreenshot();\n * }\n * // Handle other actions...\n * },\n * });\n *\n * const llmWithComputer = llm.bindTools([computer]);\n * const response = await llmWithComputer.invoke(\n * \"Save a picture of a cat to my desktop.\"\n * );\n * ```\n *\n * @param options - Configuration options for the computer use tool\n * @returns The computer use tool object that can be passed to `bindTools`\n */\nexport function computer_20250124(options: Computer20250124Options) {\n const name = TOOL_NAME;\n const computerTool = tool(\n options.execute as (\n input: unknown,\n runtime: ToolRuntime<unknown, unknown>\n ) => ComputerUseReturnType,\n {\n name,\n description: \"A tool for interacting with the computer\",\n schema: Computer20250124ActionSchema,\n }\n );\n\n computerTool.extras = {\n ...(computerTool.extras ?? {}),\n providerToolDefinition: {\n type: \"computer_20250124\",\n name,\n display_width_px: options.displayWidthPx,\n display_height_px: options.displayHeightPx,\n ...(options.displayNumber !== undefined && {\n display_number: options.displayNumber,\n }),\n } satisfies Anthropic.Beta.BetaToolComputerUse20250124,\n };\n\n return computerTool as DynamicStructuredTool<\n typeof Computer20250124ActionSchema,\n Computer20250124Action,\n any,\n ComputerUseReturnType\n >;\n}\n"],"mappings":";;;;;AAeA,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoGlB,SAAgB,kBAAkBA,SAAkC;CAClE,MAAM,OAAO;CACb,MAAM,gDACJ,QAAQ,SAIR;EACE;EACA,QAAQC;CACT,EACF;CAED,aAAa,SAAS;EACpB,GAAI,aAAa,UAAU,CAAE;EAC7B,wBAAwB;GACtB,MAAM;GACN;GACA,kBAAkB,QAAQ;GAC1B,mBAAmB,QAAQ;GAC3B,GAAI,QAAQ,kBAAkB,UAAa,EACzC,gBAAgB,QAAQ,cACzB;GACD,GAAI,QAAQ,eAAe,UAAa,EACtC,aAAa,QAAQ,WACtB;EACF;CACF;AAED,QAAO;AAMR;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuFD,SAAgB,kBAAkBC,SAAkC;CAClE,MAAM,OAAO;CACb,MAAM,gDACJ,QAAQ,SAIR;EACE;EACA,aAAa;EACb,QAAQC;CACT,EACF;CAED,aAAa,SAAS;EACpB,GAAI,aAAa,UAAU,CAAE;EAC7B,wBAAwB;GACtB,MAAM;GACN;GACA,kBAAkB,QAAQ;GAC1B,mBAAmB,QAAQ;GAC3B,GAAI,QAAQ,kBAAkB,UAAa,EACzC,gBAAgB,QAAQ,cACzB;EACF;CACF;AAED,QAAO;AAMR"}