|
| 1 | +import { stripVersionSuffix } from '@sim/utils/string' |
| 2 | +import rawToolIds from '@/tools/generated/tool-ids' |
| 3 | + |
| 4 | +/** |
| 5 | + * Tool id resolution, without importing the executable registry. |
| 6 | + * |
| 7 | + * Resolving a tool name and checking whether one exists need only the registry's |
| 8 | + * key set — never a `ToolConfig` — so this module carries just the ids (~100 KB |
| 9 | + * versus ~4 MB for params or outputs). `@/tools/metadata` and |
| 10 | + * `@/tools/metadata-outputs` both resolve through here, which is what keeps them |
| 11 | + * independent of each other. |
| 12 | + * |
| 13 | + * Semantics mirror `resolveToolId`/`getTool` in `@/tools/utils` exactly, |
| 14 | + * including returning the input unchanged when nothing matches. See |
| 15 | + * `.agents/skills/tool-registry-boundary/SKILL.md`. |
| 16 | + */ |
| 17 | +const toolIds: string[] = rawToolIds |
| 18 | + |
| 19 | +const toolIdSet = new Set(toolIds) |
| 20 | + |
| 21 | +/** |
| 22 | + * Base name -> newest versioned id, built once. |
| 23 | + * |
| 24 | + * `@/tools/utils` rebuilds this on every unresolved lookup; the id set is static |
| 25 | + * at runtime, so caching it is behaviour-identical and drops the repeated O(n) |
| 26 | + * scan. |
| 27 | + */ |
| 28 | +let latestByBaseName: Map<string, string> | null = null |
| 29 | + |
| 30 | +function getLatestByBaseName(): Map<string, string> { |
| 31 | + if (latestByBaseName) return latestByBaseName |
| 32 | + |
| 33 | + const versions = new Map<string, { toolId: string; version: number }>() |
| 34 | + for (const toolId of toolIds) { |
| 35 | + const baseName = stripVersionSuffix(toolId) |
| 36 | + const versionMatch = toolId.match(/_v(\d+)$/) |
| 37 | + const version = versionMatch ? Number.parseInt(versionMatch[1], 10) : 1 |
| 38 | + const previous = versions.get(baseName) |
| 39 | + if (!previous || version > previous.version) { |
| 40 | + versions.set(baseName, { toolId, version }) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + latestByBaseName = new Map() |
| 45 | + for (const [baseName, { toolId }] of versions) { |
| 46 | + latestByBaseName.set(baseName, toolId) |
| 47 | + } |
| 48 | + return latestByBaseName |
| 49 | +} |
| 50 | + |
| 51 | +/** Every registered tool id, including versioned variants. */ |
| 52 | +export function getToolIds(): string[] { |
| 53 | + return toolIds |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Resolves a tool name to its registered id, mapping an unversioned name onto |
| 58 | + * the newest version (`notion_search` -> `notion_search_v2`). Returns the input |
| 59 | + * unchanged when nothing matches, matching `@/tools/utils`. |
| 60 | + */ |
| 61 | +export function resolveToolId(toolName: string): string { |
| 62 | + if (toolIdSet.has(toolName)) return toolName |
| 63 | + return getLatestByBaseName().get(toolName) ?? toolName |
| 64 | +} |
| 65 | + |
| 66 | +/** Whether `toolId` names a built-in tool, resolving unversioned names. */ |
| 67 | +export function hasToolId(toolId: string): boolean { |
| 68 | + return toolIdSet.has(resolveToolId(toolId)) |
| 69 | +} |
0 commit comments