@mastra/core
Version:
185 lines (184 loc) • 5.91 kB
JavaScript
const require_tool = require("./tool-BMl4Lb5_.cjs");
// 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 require_tool.Tool || typeof tool === "object" && tool !== null && require_tool.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(".");
}
const APPROVAL_EXEMPT = Symbol.for("mastra.tool.approvalExempt");
/**
* Marks a built tool as never requiring approval, overriding a global `requireToolApproval`
* (e.g. skill tools). Only the approval resolvers read this, so it does not affect
* tool-call concurrency or eager execution the way a `requireApproval` function would.
*/
function markApprovalExempt(tool) {
Object.defineProperty(tool, APPROVAL_EXEMPT, {
value: true,
enumerable: false
});
}
function isApprovalExempt(tool) {
return typeof tool === "object" && tool !== null && tool[APPROVAL_EXEMPT] === true;
}
const AGENT_WORKFLOW_TOOL = Symbol.for("mastra.tool.agentWorkflow");
/**
* Marks a tool an agent built from one of its `workflows`. Enumerable so that
* wrappers which spread the tool (e.g. tool hooks) keep the marker.
*/
function markAgentWorkflowTool(tool) {
Object.defineProperty(tool, AGENT_WORKFLOW_TOOL, {
value: true,
enumerable: true,
configurable: true
});
}
function isAgentWorkflowTool(tool) {
return typeof tool === "object" && tool !== null && tool[AGENT_WORKFLOW_TOOL] === true;
}
/**
* 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;
}
/**
* Reads a per-tool {@link BeforeApprovalFn} attached to a tool instance, if present.
*/
function getBeforeApprovalFn(tool) {
if (typeof tool !== "object" || tool === null) return void 0;
const fn = tool.beforeApproval;
return typeof fn === "function" ? fn : void 0;
}
const BUILT_BEFORE_APPROVAL = Symbol.for("mastra.builtBeforeApproval");
/**
* Marks a {@link CoreToolBuilder} `beforeApproval` wrapper that expects trusted runtime values
* (request context and actor) as a third argument for its authorization check.
*/
function markBuiltBeforeApproval(fn) {
return Object.assign(fn, { [BUILT_BEFORE_APPROVAL]: true });
}
function isBuiltBeforeApproval(fn) {
return fn[BUILT_BEFORE_APPROVAL] === true;
}
Object.defineProperty(exports, "getBeforeApprovalFn", {
enumerable: true,
get: function() {
return getBeforeApprovalFn;
}
});
Object.defineProperty(exports, "getNeedsApprovalFn", {
enumerable: true,
get: function() {
return getNeedsApprovalFn;
}
});
Object.defineProperty(exports, "getProviderToolName", {
enumerable: true,
get: function() {
return getProviderToolName;
}
});
Object.defineProperty(exports, "isAgentWorkflowTool", {
enumerable: true,
get: function() {
return isAgentWorkflowTool;
}
});
Object.defineProperty(exports, "isApprovalExempt", {
enumerable: true,
get: function() {
return isApprovalExempt;
}
});
Object.defineProperty(exports, "isBuiltBeforeApproval", {
enumerable: true,
get: function() {
return isBuiltBeforeApproval;
}
});
Object.defineProperty(exports, "isMastraTool", {
enumerable: true,
get: function() {
return isMastraTool;
}
});
Object.defineProperty(exports, "isProviderDefinedTool", {
enumerable: true,
get: function() {
return isProviderDefinedTool;
}
});
Object.defineProperty(exports, "isProviderTool", {
enumerable: true,
get: function() {
return isProviderTool;
}
});
Object.defineProperty(exports, "isVercelTool", {
enumerable: true,
get: function() {
return isVercelTool;
}
});
Object.defineProperty(exports, "markAgentWorkflowTool", {
enumerable: true,
get: function() {
return markAgentWorkflowTool;
}
});
Object.defineProperty(exports, "markApprovalExempt", {
enumerable: true,
get: function() {
return markApprovalExempt;
}
});
Object.defineProperty(exports, "markBuiltBeforeApproval", {
enumerable: true,
get: function() {
return markBuiltBeforeApproval;
}
});
//# sourceMappingURL=toolchecks-BSANSUXW.cjs.map