UNPKG

@mastra/core

Version:
1 lines 4.2 kB
{"version":3,"file":"toolchecks-Rfz17G5p.cjs","names":["Tool","MASTRA_TOOL_MARKER"],"sources":["../src/tools/toolchecks.ts"],"sourcesContent":["import { Tool, MASTRA_TOOL_MARKER } from './tool';\nimport type { ToolToConvert } from './tool-builder/builder';\nimport type { NeedsApprovalFn, VercelTool } from './types';\n\n/**\n * Checks if a tool is a Mastra Tool, using both instanceof and marker.\n * The marker fallback handles environments like Vite SSR where the same\n * module may be loaded multiple times, causing instanceof to fail.\n */\nexport function isMastraTool(tool: unknown): boolean {\n return tool instanceof Tool || (typeof tool === 'object' && tool !== null && MASTRA_TOOL_MARKER in tool);\n}\n\n/**\n * Checks if a tool is a Vercel Tool (AI SDK tool)\n * @param tool - The tool to check\n * @returns True if the tool is a Vercel Tool, false otherwise\n */\nexport function isVercelTool(tool?: ToolToConvert): tool is VercelTool {\n // Checks if this tool is not an instance of Mastra's Tool class\n // AI SDK tools must have an execute function and either:\n // - 'parameters' (v4) or 'inputSchema' (v5/v6)\n // This prevents plain objects with inputSchema (like client tools) from being treated as VercelTools\n return !!(\n tool &&\n !isMastraTool(tool) &&\n ('parameters' in tool || ('execute' in tool && typeof tool.execute === 'function' && 'inputSchema' in tool))\n );\n}\n\ntype ProviderTool = {\n type: 'provider-defined' | 'provider';\n id: string;\n args?: Record<string, unknown>;\n inputSchema?: unknown;\n outputSchema?: unknown;\n requestContextSchema?: unknown;\n};\n\n/**\n * Checks if a tool is a provider-defined tool from the AI SDK.\n * Provider tools (like google.tools.googleSearch(), openai.tools.webSearch()) have:\n * - type: \"provider-defined\" (AI SDK v5) or \"provider\" (AI SDK v6)\n * - id: in format 'provider.tool_name' (e.g., 'google.google_search')\n *\n * These tools have a lazy `inputSchema` function that returns an AI SDK Schema\n * (not a Zod schema), so they require special handling during serialization.\n */\nexport function isProviderDefinedTool(tool: unknown): tool is ProviderTool {\n if (typeof tool !== 'object' || tool === null) return false;\n const t = tool as Record<string, unknown>;\n const isProviderType = t.type === 'provider-defined' || t.type === 'provider';\n return isProviderType && typeof t.id === 'string';\n}\n\n/**\n * Alias for callers that prefer the shorter provider-tool terminology.\n */\nexport const isProviderTool = isProviderDefinedTool;\n\n/**\n * Extracts the model-facing tool name from a provider tool id.\n * e.g. 'openai.web_search' -> 'web_search'\n */\nexport function getProviderToolName(providerId: string): string {\n return providerId.split('.').slice(1).join('.');\n}\n\n/**\n * Reads a per-tool {@link NeedsApprovalFn} attached to a tool instance, if present.\n *\n * Runtime tool maps are loosely typed (AI SDK `ToolSet` shape plus Mastra extras), so the\n * `needsApprovalFn` decided by {@link CoreToolBuilder} / the MCP client is read off an\n * arbitrary tool-like value. This helper centralizes that access with a typed result so the\n * runtime never has to reach through `any` for it.\n */\nexport function getNeedsApprovalFn(tool: unknown): NeedsApprovalFn | undefined {\n if (typeof tool !== 'object' || tool === null) return undefined;\n const fn = (tool as { needsApprovalFn?: unknown }).needsApprovalFn;\n return typeof fn === 'function' ? (fn as NeedsApprovalFn) : undefined;\n}\n"],"mappings":";;;;;;;AASA,SAAgB,aAAa,MAAwB;CACnD,OAAO,gBAAgBA,aAAAA,QAAS,OAAO,SAAS,YAAY,SAAS,QAAQC,aAAAA,sBAAsB;AACrG;;;;;;AAOA,SAAgB,aAAa,MAA0C;CAKrE,OAAO,CAAC,EACN,QACA,CAAC,aAAa,IAAI,MACjB,gBAAgB,QAAS,aAAa,QAAQ,OAAO,KAAK,YAAY,cAAc,iBAAiB;AAE1G;;;;;;;;;;AAoBA,SAAgB,sBAAsB,MAAqC;CACzE,IAAI,OAAO,SAAS,YAAY,SAAS,MAAM,OAAO;CACtD,MAAM,IAAI;CAEV,QADuB,EAAE,SAAS,sBAAsB,EAAE,SAAS,eAC1C,OAAO,EAAE,OAAO;AAC3C;;;;AAKA,MAAa,iBAAiB;;;;;AAM9B,SAAgB,oBAAoB,YAA4B;CAC9D,OAAO,WAAW,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG;AAChD;;;;;;;;;AAUA,SAAgB,mBAAmB,MAA4C;CAC7E,IAAI,OAAO,SAAS,YAAY,SAAS,MAAM,OAAO,KAAA;CACtD,MAAM,KAAM,KAAuC;CACnD,OAAO,OAAO,OAAO,aAAc,KAAyB,KAAA;AAC9D"}