UNPKG

naisys

Version:

NAISYS - Autonomous AI agent runner with built-in context management and cost tracking

67 lines 2.74 kB
/** * Anthropic-specific computer use helpers. * Builds the tool definition and extracts tool_use blocks from responses. * Coordinates stay in scaled-pixel (API) space throughout — ComputerService * handles translation to absolute screen coords at execute time. */ import { KNOWN_DESKTOP_ACTION_TAGS } from "../computerService.js"; /** Determine the computer use tool type and beta flag for an Anthropic model */ function getVersionConfig(versionName) { if (versionName.includes("4-6") || versionName.includes("4-5")) { return { toolType: "computer_20251124", betaFlag: "computer-use-2025-11-24", }; } return { toolType: "computer_20250124", betaFlag: "computer-use-2025-01-24", }; } /** * Prepare the computer use tool definition. * Returns the tool to add to the request and the beta flag for the API * request. Screenshots are already scaled at capture time by ComputerService. */ export function prepareComputerUse(desktopConfig, versionName) { const { toolType, betaFlag } = getVersionConfig(versionName); const computerTool = { type: toolType, name: "computer", display_width_px: desktopConfig.scaledWidth, display_height_px: desktopConfig.scaledHeight, }; return { computerTool, betaFlag }; } /** * Extract desktop actions from the response content. Coordinates are passed * through as-is in the API's scaled-pixel space. The action discriminator * is checked against the closed `DesktopSubAction` set; an unrecognized * action name is surfaced as a `validationError` so confirmAndExecuteActions * can reply to the model with a tool_result error instead of silently * no-op'ing later. Field shapes (coordinate types, etc.) are trusted to * the API contract — if a known action carries malformed fields, the * existing try/catch around `executeAction` contains the explosion. */ export function extractDesktopActions(content) { const actions = []; for (const block of content) { if (block.type === "tool_use" && block.name === "computer") { const tag = block.input?.action; const isKnown = typeof tag === "string" && KNOWN_DESKTOP_ACTION_TAGS.has(tag); actions.push({ id: block.id, name: block.name, input: { actions: [block.input] }, ...(isKnown ? {} : { validationError: `Unsupported computer-use action: ${typeof tag === "string" ? tag : "(no action field)"}`, }), }); } } return actions; } //# sourceMappingURL=anthropic-computer-use.js.map