@mastra/core
Version:
60 lines (59 loc) • 2.6 kB
JavaScript
import { n as Tool, t as MASTRA_TOOL_MARKER } from "./tool-qGw4ZhYO.js";
//#region src/tools/toolchecks.ts
/**
* Checks if a tool is a Mastra Tool, using both instanceof and marker.
* The marker fallback handles environments like Vite SSR where the same
* module may be loaded multiple times, causing instanceof to fail.
*/
function isMastraTool(tool) {
return tool instanceof Tool || typeof tool === "object" && tool !== null && MASTRA_TOOL_MARKER in tool;
}
/**
* Checks if a tool is a Vercel Tool (AI SDK tool)
* @param tool - The tool to check
* @returns True if the tool is a Vercel Tool, false otherwise
*/
function isVercelTool(tool) {
return !!(tool && !isMastraTool(tool) && ("parameters" in tool || "execute" in tool && typeof tool.execute === "function" && "inputSchema" in tool));
}
/**
* Checks if a tool is a provider-defined tool from the AI SDK.
* Provider tools (like google.tools.googleSearch(), openai.tools.webSearch()) have:
* - type: "provider-defined" (AI SDK v5) or "provider" (AI SDK v6)
* - id: in format 'provider.tool_name' (e.g., 'google.google_search')
*
* These tools have a lazy `inputSchema` function that returns an AI SDK Schema
* (not a Zod schema), so they require special handling during serialization.
*/
function isProviderDefinedTool(tool) {
if (typeof tool !== "object" || tool === null) return false;
const t = tool;
return (t.type === "provider-defined" || t.type === "provider") && typeof t.id === "string";
}
/**
* Alias for callers that prefer the shorter provider-tool terminology.
*/
const isProviderTool = isProviderDefinedTool;
/**
* Extracts the model-facing tool name from a provider tool id.
* e.g. 'openai.web_search' -> 'web_search'
*/
function getProviderToolName(providerId) {
return providerId.split(".").slice(1).join(".");
}
/**
* Reads a per-tool {@link NeedsApprovalFn} attached to a tool instance, if present.
*
* Runtime tool maps are loosely typed (AI SDK `ToolSet` shape plus Mastra extras), so the
* `needsApprovalFn` decided by {@link CoreToolBuilder} / the MCP client is read off an
* arbitrary tool-like value. This helper centralizes that access with a typed result so the
* runtime never has to reach through `any` for it.
*/
function getNeedsApprovalFn(tool) {
if (typeof tool !== "object" || tool === null) return void 0;
const fn = tool.needsApprovalFn;
return typeof fn === "function" ? fn : void 0;
}
//#endregion
export { isProviderTool as a, isProviderDefinedTool as i, getProviderToolName as n, isVercelTool as o, isMastraTool as r, getNeedsApprovalFn as t };
//# sourceMappingURL=toolchecks-BWgiThPN.js.map