( model: string, tools: Tools, getToolPermissionContext: () => Promise<ToolPermissionContext>, agents: AgentDefinition[], source?: string, )
| 294 | * @returns true if tool search should be enabled for this request |
| 295 | */ |
| 296 | export async function isSearchExtraToolsEnabled( |
| 297 | model: string, |
| 298 | tools: Tools, |
| 299 | getToolPermissionContext: () => Promise<ToolPermissionContext>, |
| 300 | agents: AgentDefinition[], |
| 301 | source?: string, |
| 302 | ): Promise<boolean> { |
| 303 | const mcpToolCount = count(tools, t => t.isMcp) |
| 304 | |
| 305 | // Helper to log the mode decision event |
| 306 | function logModeDecision( |
| 307 | enabled: boolean, |
| 308 | mode: SearchExtraToolsMode, |
| 309 | reason: string, |
| 310 | extraProps?: Record<string, number>, |
| 311 | ): void { |
| 312 | logEvent('tengu_search_extra_tools_mode_decision', { |
| 313 | enabled, |
| 314 | mode: mode as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 315 | reason: |
| 316 | reason as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 317 | // Log the actual model being checked, not the session's main model. |
| 318 | // This is important for debugging subagent tool search decisions where |
| 319 | // the subagent model (e.g., haiku) differs from the session model (e.g., opus). |
| 320 | checkedModel: |
| 321 | model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 322 | mcpToolCount, |
| 323 | userType: (process.env.USER_TYPE ?? |
| 324 | 'external') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 325 | ...extraProps, |
| 326 | }) |
| 327 | } |
| 328 | |
| 329 | // Tool search is enabled uniformly regardless of provider or model. |
| 330 | // All providers use self-built TF-IDF + keyword search via SearchExtraToolsTool + ExecuteExtraTool. |
| 331 | |
| 332 | // Check if SearchExtraToolsTool is available (respects disallowedTools) |
| 333 | if (!isSearchExtraToolsToolAvailable(tools)) { |
| 334 | logForDebugging( |
| 335 | `Tool search disabled: SearchExtraToolsTool is not available (may have been disallowed via disallowedTools).`, |
| 336 | ) |
| 337 | logModeDecision(false, 'standard', 'mcp_search_unavailable') |
| 338 | return false |
| 339 | } |
| 340 | |
| 341 | const mode = getSearchExtraToolsMode() |
| 342 | |
| 343 | switch (mode) { |
| 344 | case 'tst': |
| 345 | logModeDecision(true, mode, 'tst_enabled') |
| 346 | return true |
| 347 | |
| 348 | case 'tst-auto': { |
| 349 | const { enabled, debugDescription, metrics } = await checkAutoThreshold( |
| 350 | tools, |
| 351 | getToolPermissionContext, |
| 352 | agents, |
| 353 | model, |
no test coverage detected