Buckets:
evalstate/sandbox-testing-2 / hf-mcp-server /packages /app /dist /server /utils /tool-selection-strategy.js
| import { logger } from './logger.js'; | |
| import { ALL_BUILTIN_TOOL_IDS, CREATE_REPO_TOOL_ID } from '@llmindset/hf-mcp'; | |
| import { extractAuthBouquetAndMix } from '../utils/auth-utils.js'; | |
| import { normalizeBuiltInTools } from '../../shared/tool-normalizer.js'; | |
| import { BOUQUETS } from '../../shared/bouquet-presets.js'; | |
| import { parseGradioSpaceIds } from './gradio-utils.js'; | |
| import { getProxyToolsConfig } from './proxy-tools-config.js'; | |
| export var ToolSelectionMode; | |
| (function (ToolSelectionMode) { | |
| ToolSelectionMode["BOUQUET_OVERRIDE"] = "bouquet_override"; | |
| ToolSelectionMode["MIX"] = "mix"; | |
| ToolSelectionMode["EXTERNAL_API"] = "external_api"; | |
| ToolSelectionMode["INTERNAL_API"] = "internal_api"; | |
| ToolSelectionMode["FALLBACK"] = "fallback"; | |
| })(ToolSelectionMode || (ToolSelectionMode = {})); | |
| export const AUTHENTICATED_BUILTIN_TOOL_IDS = [CREATE_REPO_TOOL_ID]; | |
| export class ToolSelectionStrategy { | |
| apiClient; | |
| constructor(apiClient) { | |
| this.apiClient = apiClient; | |
| } | |
| parseGradioEndpoints(gradioParam) { | |
| const parsedSpaces = parseGradioSpaceIds(gradioParam); | |
| return parsedSpaces.map((space) => { | |
| const placeholderSubdomain = space.name.replace(/[/]/g, '-'); | |
| return { | |
| _id: `gradio_metadata_${placeholderSubdomain}`, | |
| name: space.name, | |
| subdomain: placeholderSubdomain, | |
| emoji: '🔧', | |
| }; | |
| }); | |
| } | |
| applySearchEnablesFetch(enabledToolIds) { | |
| if (process.env.SEARCH_ENABLES_FETCH === 'true') { | |
| if (enabledToolIds.includes('hf_doc_search') && !enabledToolIds.includes('hf_doc_fetch')) { | |
| logger.debug('SEARCH_ENABLES_FETCH: Auto-enabling hf_doc_fetch because hf_doc_search is enabled'); | |
| return [...enabledToolIds, 'hf_doc_fetch']; | |
| } | |
| } | |
| return enabledToolIds; | |
| } | |
| getProxyToolNames() { | |
| return getProxyToolsConfig().map((tool) => tool.toolName); | |
| } | |
| appendProxyTools(enabledToolIds) { | |
| const proxyToolNames = this.getProxyToolNames(); | |
| if (proxyToolNames.length === 0) { | |
| return enabledToolIds; | |
| } | |
| return [...new Set([...normalizeBuiltInTools(enabledToolIds), ...proxyToolNames])]; | |
| } | |
| applyAuthVisibility(enabledToolIds, hfToken) { | |
| if (hfToken) { | |
| return enabledToolIds; | |
| } | |
| const authenticatedTools = new Set(AUTHENTICATED_BUILTIN_TOOL_IDS); | |
| return enabledToolIds.filter((toolId) => !authenticatedTools.has(toolId)); | |
| } | |
| async selectTools(context) { | |
| const { bouquet, mix, gradio } = extractAuthBouquetAndMix(context.headers); | |
| const mixList = mix ?? []; | |
| const gradioSpaceTools = gradio ? this.parseGradioEndpoints(gradio) : []; | |
| const proxyToolNames = this.getProxyToolNames(); | |
| const hasProxyBouquet = bouquet === 'proxy'; | |
| const includesProxyMix = mixList.includes('proxy'); | |
| if (bouquet && BOUQUETS[bouquet]) { | |
| let enabledToolIds = normalizeBuiltInTools(this.applySearchEnablesFetch(BOUQUETS[bouquet].builtInTools)); | |
| const wantsProxyTools = hasProxyBouquet || includesProxyMix; | |
| if (wantsProxyTools && proxyToolNames.length > 0) { | |
| enabledToolIds = this.appendProxyTools(enabledToolIds); | |
| } | |
| else if (wantsProxyTools && proxyToolNames.length === 0) { | |
| logger.warn('Proxy tools requested but no proxy tools are configured'); | |
| } | |
| enabledToolIds = this.applyAuthVisibility(enabledToolIds, context.hfToken); | |
| logger.debug({ bouquet, mix: includesProxyMix ? ['proxy'] : [], enabledToolIds, gradioCount: gradioSpaceTools.length }, 'Using bouquet override'); | |
| return { | |
| mode: ToolSelectionMode.BOUQUET_OVERRIDE, | |
| enabledToolIds, | |
| reason: `Bouquet override: ${bouquet}${includesProxyMix ? ' + proxy mix' : ''}${gradioSpaceTools.length > 0 ? ` + ${gradioSpaceTools.length} gradio endpoints` : ''}`, | |
| gradioSpaceTools: gradioSpaceTools.length > 0 ? gradioSpaceTools : undefined, | |
| }; | |
| } | |
| const baseSettings = await this.getUserSettings(context); | |
| if (mixList.length > 0 && baseSettings) { | |
| const validMixes = mixList.filter((mixName) => { | |
| const isValid = Boolean(BOUQUETS[mixName]); | |
| if (!isValid) { | |
| logger.warn({ mixName }, 'Ignoring invalid mix bouquet name'); | |
| } | |
| return isValid; | |
| }); | |
| if (validMixes.length > 0) { | |
| const includesProxyMix = validMixes.includes('proxy'); | |
| const mixedTools = validMixes.flatMap((mixName) => BOUQUETS[mixName]?.builtInTools ?? []); | |
| const combinedTools = [...new Set([...baseSettings.builtInTools, ...mixedTools])]; | |
| let enabledToolIds = normalizeBuiltInTools(this.applySearchEnablesFetch(combinedTools)); | |
| if (includesProxyMix && proxyToolNames.length > 0) { | |
| enabledToolIds = this.appendProxyTools(enabledToolIds); | |
| } | |
| else if (includesProxyMix && proxyToolNames.length === 0) { | |
| logger.warn('Proxy mix requested but no proxy tools are configured'); | |
| } | |
| enabledToolIds = this.applyAuthVisibility(enabledToolIds, context.hfToken); | |
| logger.debug({ | |
| mix: validMixes, | |
| baseToolCount: baseSettings.builtInTools.length, | |
| mixToolCount: mixedTools.length, | |
| finalToolCount: enabledToolIds.length, | |
| }, 'Applying mix to user settings'); | |
| return { | |
| mode: ToolSelectionMode.MIX, | |
| enabledToolIds, | |
| reason: `User settings + mix(${validMixes.join(',')})${gradioSpaceTools.length > 0 ? ` + ${gradioSpaceTools.length} gradio endpoints` : ''}`, | |
| baseSettings, | |
| mixedBouquet: validMixes, | |
| gradioSpaceTools: gradioSpaceTools.length > 0 ? gradioSpaceTools : undefined, | |
| }; | |
| } | |
| } | |
| if (baseSettings) { | |
| const mode = this.apiClient.getTransportInfo()?.externalApiMode | |
| ? ToolSelectionMode.EXTERNAL_API | |
| : ToolSelectionMode.INTERNAL_API; | |
| const enabledToolIds = normalizeBuiltInTools(this.applySearchEnablesFetch(baseSettings.builtInTools)); | |
| const visibleToolIds = this.applyAuthVisibility(enabledToolIds, context.hfToken); | |
| logger.debug({ | |
| mode, | |
| enabledToolIds: visibleToolIds, | |
| }, 'Using user settings'); | |
| return { | |
| mode, | |
| enabledToolIds: visibleToolIds, | |
| reason: mode === ToolSelectionMode.EXTERNAL_API | |
| ? `External API user settings${gradioSpaceTools.length > 0 ? ` + ${gradioSpaceTools.length} gradio endpoints` : ''}` | |
| : `Internal API user settings${gradioSpaceTools.length > 0 ? ` + ${gradioSpaceTools.length} gradio endpoints` : ''}`, | |
| baseSettings, | |
| gradioSpaceTools: gradioSpaceTools.length > 0 ? gradioSpaceTools : undefined, | |
| }; | |
| } | |
| logger.warn('No settings available, using fallback (all tools enabled)'); | |
| let enabledToolIds = normalizeBuiltInTools(this.applySearchEnablesFetch([...ALL_BUILTIN_TOOL_IDS])); | |
| if (includesProxyMix && proxyToolNames.length > 0) { | |
| enabledToolIds = this.appendProxyTools(enabledToolIds); | |
| } | |
| else if (includesProxyMix && proxyToolNames.length === 0) { | |
| logger.warn('Proxy mix requested but no proxy tools are configured'); | |
| } | |
| enabledToolIds = this.applyAuthVisibility(enabledToolIds, context.hfToken); | |
| return { | |
| mode: ToolSelectionMode.FALLBACK, | |
| enabledToolIds, | |
| reason: `Fallback - no settings available${includesProxyMix ? ' + proxy mix' : ''}${gradioSpaceTools.length > 0 ? ` + ${gradioSpaceTools.length} gradio endpoints` : ''}`, | |
| gradioSpaceTools: gradioSpaceTools.length > 0 ? gradioSpaceTools : undefined, | |
| }; | |
| } | |
| async getUserSettings(context) { | |
| if (context.userSettings) { | |
| logger.debug('Using provided user settings'); | |
| return context.userSettings; | |
| } | |
| if (process.env.NODE_ENV === 'test' || process.env.VITEST) { | |
| logger.debug('Skipping API client fetch in test environment'); | |
| return null; | |
| } | |
| try { | |
| const toolStates = await this.apiClient.getToolStates(context.hfToken); | |
| if (toolStates) { | |
| const builtInTools = Object.keys(toolStates).filter((id) => toolStates[id]); | |
| const spaceTools = this.apiClient.getGradioEndpoints().map((endpoint) => ({ | |
| name: endpoint.name, | |
| subdomain: endpoint.subdomain, | |
| _id: endpoint.id || endpoint.name, | |
| emoji: endpoint.emoji || '🛠️', | |
| })); | |
| return { builtInTools, spaceTools }; | |
| } | |
| } | |
| catch (error) { | |
| logger.warn({ error }, 'Failed to fetch user settings from API client'); | |
| } | |
| return null; | |
| } | |
| } | |
| //# sourceMappingURL=tool-selection-strategy.js.map |
Xet Storage Details
- Size:
- 9.52 kB
- Xet hash:
- 566fb5b8bed05c82bd4b42617be6ef70795afe167f43df36592aea9599fa5fd1
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.