(tools: ToolDefinition[])
| 14 | import { log } from '../utils/logging/index.ts'; |
| 15 | |
| 16 | export function createToolCatalog(tools: ToolDefinition[]): ToolCatalog { |
| 17 | // Build lookup maps for fast resolution, deduplicating by mcpName so that |
| 18 | // tools shared across multiple workflows don't cause ambiguous resolution. |
| 19 | const byCliName = new Map<string, ToolDefinition>(); |
| 20 | const byMcpName = new Map<string, ToolDefinition>(); |
| 21 | const byToolId = new Map<string, ToolDefinition>(); |
| 22 | const byMcpKebab = new Map<string, ToolDefinition[]>(); |
| 23 | const seenMcpNames = new Set<string>(); |
| 24 | |
| 25 | for (const tool of tools) { |
| 26 | const mcpKey = tool.mcpName.toLowerCase(); |
| 27 | if (seenMcpNames.has(mcpKey)) continue; |
| 28 | seenMcpNames.add(mcpKey); |
| 29 | |
| 30 | byCliName.set(tool.cliName, tool); |
| 31 | byMcpName.set(mcpKey, tool); |
| 32 | if (tool.id) { |
| 33 | byToolId.set(tool.id, tool); |
| 34 | } |
| 35 | |
| 36 | const mcpKebab = toKebabCase(tool.mcpName); |
| 37 | let kebabGroup = byMcpKebab.get(mcpKebab); |
| 38 | if (!kebabGroup) { |
| 39 | kebabGroup = []; |
| 40 | byMcpKebab.set(mcpKebab, kebabGroup); |
| 41 | } |
| 42 | kebabGroup.push(tool); |
| 43 | } |
| 44 | |
| 45 | return { |
| 46 | tools, |
| 47 | |
| 48 | getByCliName(name: string): ToolDefinition | null { |
| 49 | return byCliName.get(name) ?? null; |
| 50 | }, |
| 51 | |
| 52 | getByMcpName(name: string): ToolDefinition | null { |
| 53 | return byMcpName.get(name.toLowerCase().trim()) ?? null; |
| 54 | }, |
| 55 | |
| 56 | getByToolId(toolId: string): ToolDefinition | null { |
| 57 | return byToolId.get(toolId) ?? null; |
| 58 | }, |
| 59 | |
| 60 | resolve(input: string): ToolResolution { |
| 61 | const normalized = input.toLowerCase().trim(); |
| 62 | |
| 63 | // Try exact CLI name match first |
| 64 | const exact = byCliName.get(normalized); |
| 65 | if (exact) { |
| 66 | return { tool: exact }; |
| 67 | } |
| 68 | |
| 69 | // Try kebab-case of MCP name (alias) |
| 70 | const mcpKebab = toKebabCase(normalized); |
| 71 | const aliasMatches = byMcpKebab.get(mcpKebab); |
| 72 | if (aliasMatches && aliasMatches.length === 1) { |
| 73 | return { tool: aliasMatches[0] }; |
no test coverage detected