(
tool: Tool,
options: {
getToolPermissionContext: () => Promise<ToolPermissionContext>
tools: Tools
agents: AgentDefinition[]
allowedAgentTypes?: string[]
model?: string
/** When true, mark this tool with defer_loading for tool search */
deferLoading?: boolean
cacheControl?: {
type: 'ephemeral'
scope?: 'global' | 'org'
ttl?: '5m' | '1h'
}
},
)
| 119 | } |
| 120 | |
| 121 | export async function toolToAPISchema( |
| 122 | tool: Tool, |
| 123 | options: { |
| 124 | getToolPermissionContext: () => Promise<ToolPermissionContext> |
| 125 | tools: Tools |
| 126 | agents: AgentDefinition[] |
| 127 | allowedAgentTypes?: string[] |
| 128 | model?: string |
| 129 | /** When true, mark this tool with defer_loading for tool search */ |
| 130 | deferLoading?: boolean |
| 131 | cacheControl?: { |
| 132 | type: 'ephemeral' |
| 133 | scope?: 'global' | 'org' |
| 134 | ttl?: '5m' | '1h' |
| 135 | } |
| 136 | }, |
| 137 | ): Promise<BetaToolUnion> { |
| 138 | // Session-stable base schema: name, description, input_schema, strict, |
| 139 | // eager_input_streaming. These are computed once per session and cached to |
| 140 | // prevent mid-session GrowthBook flips (tengu_tool_pear, tengu_fgts) or |
| 141 | // tool.prompt() drift from churning the serialized tool array bytes. |
| 142 | // See toolSchemaCache.ts for rationale. |
| 143 | // |
| 144 | // Cache key includes inputJSONSchema when present. StructuredOutput instances |
| 145 | // share the name 'StructuredOutput' but carry different schemas per workflow |
| 146 | // call — name-only keying returned a stale schema (5.4% → 51% err rate, see |
| 147 | // PR#25424). MCP tools also set inputJSONSchema but each has a stable schema, |
| 148 | // so including it preserves their GB-flip cache stability. |
| 149 | const cacheKey = |
| 150 | 'inputJSONSchema' in tool && tool.inputJSONSchema |
| 151 | ? `${tool.name}:${jsonStringify(tool.inputJSONSchema)}` |
| 152 | : tool.name |
| 153 | const cache = getToolSchemaCache() |
| 154 | let base = cache.get(cacheKey) |
| 155 | if (!base) { |
| 156 | const strictToolsEnabled = |
| 157 | checkStatsigFeatureGate_CACHED_MAY_BE_STALE('tengu_tool_pear') |
| 158 | // Use tool's JSON schema directly if provided, otherwise convert Zod schema |
| 159 | let input_schema = ( |
| 160 | 'inputJSONSchema' in tool && tool.inputJSONSchema |
| 161 | ? tool.inputJSONSchema |
| 162 | : zodToJsonSchema(tool.inputSchema) |
| 163 | ) as Anthropic.Tool.InputSchema |
| 164 | |
| 165 | // Filter out swarm-related fields when swarms are not enabled |
| 166 | // This ensures external non-EAP users don't see swarm features in the schema |
| 167 | if (!isAgentSwarmsEnabled()) { |
| 168 | input_schema = filterSwarmFieldsFromSchema(tool.name, input_schema) |
| 169 | } |
| 170 | |
| 171 | base = { |
| 172 | name: tool.name, |
| 173 | description: await tool.prompt({ |
| 174 | getToolPermissionContext: options.getToolPermissionContext, |
| 175 | tools: options.tools, |
| 176 | agents: options.agents, |
| 177 | allowedAgentTypes: options.allowedAgentTypes, |
| 178 | }), |
no test coverage detected