MCPcopy Create free account
hub / github.com/Noumena-Network/code / checkAutoThreshold

Function checkAutoThreshold

src/utils/toolSearch.ts:714–758  ·  view source on GitHub ↗

* 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,
)

Source from the content-addressed store, hash-verified

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

Callers 1

isToolSearchEnabledFunction · 0.85

Tested by

no test coverage detected