* Check whether deferred tools exceed the auto-threshold for enabling TST. * Tries exact token count first; falls back to character-based heuristic.
( tools: Tools, getToolPermissionContext: () => Promise<ToolPermissionContext>, agents: AgentDefinition[], model: string, )
| 674 | * Tries exact token count first; falls back to character-based heuristic. |
| 675 | */ |
| 676 | async function checkAutoThreshold( |
| 677 | tools: Tools, |
| 678 | getToolPermissionContext: () => Promise<ToolPermissionContext>, |
| 679 | agents: AgentDefinition[], |
| 680 | model: string, |
| 681 | ): Promise<{ |
| 682 | enabled: boolean |
| 683 | debugDescription: string |
| 684 | metrics: Record<string, number> |
| 685 | }> { |
| 686 | // Try exact token count first (cached, one API call per toolset change) |
| 687 | const deferredToolTokens = await getDeferredToolTokenCount( |
| 688 | tools, |
| 689 | getToolPermissionContext, |
| 690 | agents, |
| 691 | model, |
| 692 | ) |
| 693 | |
| 694 | if (deferredToolTokens !== null) { |
| 695 | const threshold = getAutoSearchExtraToolsTokenThreshold(model) |
| 696 | return { |
| 697 | enabled: deferredToolTokens >= threshold, |
| 698 | debugDescription: |
| 699 | `${deferredToolTokens} tokens (threshold: ${threshold}, ` + |
| 700 | `${getAutoSearchExtraToolsPercentage()}% of context)`, |
| 701 | metrics: { deferredToolTokens, threshold }, |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | // Fallback: character-based heuristic when token API is unavailable |
| 706 | const deferredToolDescriptionChars = |
| 707 | await calculateDeferredToolDescriptionChars( |
| 708 | tools, |
| 709 | getToolPermissionContext, |
| 710 | agents, |
| 711 | ) |
| 712 | const charThreshold = getAutoSearchExtraToolsCharThreshold(model) |
| 713 | return { |
| 714 | enabled: deferredToolDescriptionChars >= charThreshold, |
| 715 | debugDescription: |
| 716 | `${deferredToolDescriptionChars} chars (threshold: ${charThreshold}, ` + |
| 717 | `${getAutoSearchExtraToolsPercentage()}% of context) (char fallback)`, |
| 718 | metrics: { deferredToolDescriptionChars, charThreshold }, |
| 719 | } |
| 720 | } |
no test coverage detected