* 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, )
| 710 | * Tries exact token count first; falls back to character-based heuristic. |
| 711 | */ |
| 712 | async function checkAutoThreshold( |
| 713 | tools: Tools, |
| 714 | getToolPermissionContext: () => Promise<ToolPermissionContext>, |
| 715 | agents: AgentDefinition[], |
| 716 | model: string, |
| 717 | ): Promise<{ |
| 718 | enabled: boolean |
| 719 | debugDescription: string |
| 720 | metrics: Record<string, number> |
| 721 | }> { |
| 722 | // Try exact token count first (cached, one API call per toolset change) |
| 723 | const deferredToolTokens = await getDeferredToolTokenCount( |
| 724 | tools, |
| 725 | getToolPermissionContext, |
| 726 | agents, |
| 727 | model, |
| 728 | ) |
| 729 | |
| 730 | if (deferredToolTokens !== null) { |
| 731 | const threshold = getAutoToolSearchTokenThreshold(model) |
| 732 | return { |
| 733 | enabled: deferredToolTokens >= threshold, |
| 734 | debugDescription: |
| 735 | `${deferredToolTokens} tokens (threshold: ${threshold}, ` + |
| 736 | `${getAutoToolSearchPercentage()}% of context)`, |
| 737 | metrics: { deferredToolTokens, threshold }, |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | // Fallback: character-based heuristic when token API is unavailable |
| 742 | const deferredToolDescriptionChars = |
| 743 | await calculateDeferredToolDescriptionChars( |
| 744 | tools, |
| 745 | getToolPermissionContext, |
| 746 | agents, |
| 747 | ) |
| 748 | const charThreshold = getAutoToolSearchCharThreshold(model) |
| 749 | return { |
| 750 | enabled: deferredToolDescriptionChars >= charThreshold, |
| 751 | debugDescription: |
| 752 | `${deferredToolDescriptionChars} chars (threshold: ${charThreshold}, ` + |
| 753 | `${getAutoToolSearchPercentage()}% of context) (char fallback)`, |
| 754 | metrics: { deferredToolDescriptionChars, charThreshold }, |
| 755 | } |
| 756 | } |
| 757 |
no test coverage detected