|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Common abbreviations for tool name segments. |
| 9 | + * Used to produce human-readable short aliases. |
| 10 | + */ |
| 11 | +const ABBREVIATIONS: Record<string, string> = { |
| 12 | + action: 'act', |
| 13 | + analyze: 'anlz', |
| 14 | + console: 'cons', |
| 15 | + evaluate: 'eval', |
| 16 | + experimental: 'exp', |
| 17 | + extension: 'ext', |
| 18 | + extensions: 'exts', |
| 19 | + insight: 'ins', |
| 20 | + install: 'inst', |
| 21 | + lighthouse: 'lh', |
| 22 | + memory: 'mem', |
| 23 | + message: 'msg', |
| 24 | + messages: 'msgs', |
| 25 | + navigate: 'nav', |
| 26 | + network: 'net', |
| 27 | + performance: 'perf', |
| 28 | + request: 'req', |
| 29 | + requests: 'reqs', |
| 30 | + screencast: 'scrcast', |
| 31 | + screenshot: 'scrn', |
| 32 | + snapshot: 'snap', |
| 33 | + trigger: 'trig', |
| 34 | + uninstall: 'uninst', |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * Tool name aliaser for provider compatibility. |
| 39 | + * |
| 40 | + * Some LLM providers (e.g., AWS Bedrock) enforce character limits on tool |
| 41 | + * names. When MCP clients add prefixes like |
| 42 | + * `mcp__plugin_<pkg>_<server>__`, the full tool name can exceed these limits. |
| 43 | + * |
| 44 | + * This class provides deterministic, collision-safe shortening of tool names |
| 45 | + * while maintaining bidirectional mappings for dispatch. |
| 46 | + * |
| 47 | + * Example: with a Bedrock 64-char limit and a 49-char client prefix, |
| 48 | + * `maxLength` should be set to 15 (64 - 49). Tool names longer than 15 |
| 49 | + * characters are automatically shortened using human-readable abbreviations. |
| 50 | + */ |
| 51 | +export class ToolNameAliaser { |
| 52 | + readonly #aliasToOriginal = new Map<string, string>(); |
| 53 | + readonly #originalToAlias = new Map<string, string>(); |
| 54 | + readonly #maxLength: number; |
| 55 | + |
| 56 | + constructor(maxLength: number) { |
| 57 | + if (maxLength < 1) { |
| 58 | + throw new Error('maxLength must be at least 1'); |
| 59 | + } |
| 60 | + this.#maxLength = maxLength; |
| 61 | + } |
| 62 | + |
| 63 | + get maxLength(): number { |
| 64 | + return this.#maxLength; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Register a tool name. Returns the alias (which equals the original name |
| 69 | + * if it already fits within the max length). |
| 70 | + * |
| 71 | + * Tool names should be registered in a deterministic order (e.g., |
| 72 | + * alphabetical) to ensure stable alias generation across runs. |
| 73 | + */ |
| 74 | + register(originalName: string): string { |
| 75 | + if (this.#originalToAlias.has(originalName)) { |
| 76 | + return this.#originalToAlias.get(originalName)!; |
| 77 | + } |
| 78 | + |
| 79 | + if (originalName.length <= this.#maxLength) { |
| 80 | + this.#aliasToOriginal.set(originalName, originalName); |
| 81 | + this.#originalToAlias.set(originalName, originalName); |
| 82 | + return originalName; |
| 83 | + } |
| 84 | + |
| 85 | + const alias = this.#shorten(originalName); |
| 86 | + this.#aliasToOriginal.set(alias, originalName); |
| 87 | + this.#originalToAlias.set(originalName, alias); |
| 88 | + return alias; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Resolve an alias back to its original tool name. |
| 93 | + * Returns `undefined` if the alias is not registered. |
| 94 | + */ |
| 95 | + resolve(alias: string): string | undefined { |
| 96 | + return this.#aliasToOriginal.get(alias); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Get the alias for an original tool name. |
| 101 | + * Returns `undefined` if the name is not registered. |
| 102 | + */ |
| 103 | + getAlias(originalName: string): string | undefined { |
| 104 | + return this.#originalToAlias.get(originalName); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Get all registered (alias, original) pairs. |
| 109 | + */ |
| 110 | + entries(): Array<[alias: string, original: string]> { |
| 111 | + return [...this.#aliasToOriginal.entries()]; |
| 112 | + } |
| 113 | + |
| 114 | + #shorten(name: string): string { |
| 115 | + const segments = name.split('_'); |
| 116 | + |
| 117 | + // Step 1: Apply known abbreviations to each segment. |
| 118 | + const abbreviated = segments.map(seg => ABBREVIATIONS[seg] ?? seg); |
| 119 | + |
| 120 | + let candidate = abbreviated.join('_'); |
| 121 | + if (candidate.length <= this.#maxLength) { |
| 122 | + return this.#ensureUnique(candidate); |
| 123 | + } |
| 124 | + |
| 125 | + // Step 2: Progressively truncate the longest segment by one character |
| 126 | + // until the name fits. |
| 127 | + const working = [...abbreviated]; |
| 128 | + while (working.join('_').length > this.#maxLength && working.length > 0) { |
| 129 | + let longestIdx = 0; |
| 130 | + for (let i = 1; i < working.length; i++) { |
| 131 | + if (working[i].length > working[longestIdx].length) { |
| 132 | + longestIdx = i; |
| 133 | + } |
| 134 | + } |
| 135 | + if (working[longestIdx].length <= 1) { |
| 136 | + // Cannot shorten further; drop the last segment. |
| 137 | + working.pop(); |
| 138 | + continue; |
| 139 | + } |
| 140 | + working[longestIdx] = working[longestIdx].slice(0, -1); |
| 141 | + } |
| 142 | + |
| 143 | + candidate = working.join('_'); |
| 144 | + |
| 145 | + // Step 3: Hard truncate as a safety net (shouldn't be reached by the |
| 146 | + // loop above for reasonable maxLength values). |
| 147 | + if (candidate.length > this.#maxLength) { |
| 148 | + candidate = candidate.slice(0, this.#maxLength); |
| 149 | + } |
| 150 | + |
| 151 | + return this.#ensureUnique(candidate); |
| 152 | + } |
| 153 | + |
| 154 | + #ensureUnique(candidate: string): string { |
| 155 | + if (!this.#aliasToOriginal.has(candidate)) { |
| 156 | + return candidate; |
| 157 | + } |
| 158 | + |
| 159 | + // Collision: append a numeric suffix while staying within maxLength. |
| 160 | + for (let i = 1; i < 1000; i++) { |
| 161 | + const suffix = `_${i}`; |
| 162 | + const maxBase = this.#maxLength - suffix.length; |
| 163 | + const base = |
| 164 | + candidate.length > maxBase ? candidate.slice(0, maxBase) : candidate; |
| 165 | + const withSuffix = base + suffix; |
| 166 | + if (!this.#aliasToOriginal.has(withSuffix)) { |
| 167 | + return withSuffix; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + throw new Error( |
| 172 | + `Cannot generate unique alias for "${candidate}" after 1000 attempts`, |
| 173 | + ); |
| 174 | + } |
| 175 | +} |
0 commit comments