(args: z.infer<typeof ArgsSchema>, ctx: ToolContext)
| 163 | argsSchema = ArgsSchema; |
| 164 | |
| 165 | async execute(args: z.infer<typeof ArgsSchema>, ctx: ToolContext): Promise<ToolResult> { |
| 166 | const abs = path.isAbsolute(args.path) ? args.path : path.resolve(ctx.cwd, args.path); |
| 167 | const rel = path.relative(ctx.cwd, abs); |
| 168 | |
| 169 | const lang = detectLanguage(abs); |
| 170 | if (!lang) { |
| 171 | return { |
| 172 | content: `[UNSUPPORTED_LANGUAGE] No AST grammar for ${path.extname(abs)}. Use edit_text instead for this file type.`, |
| 173 | isError: true, |
| 174 | }; |
| 175 | } |
| 176 | |
| 177 | const nodeTypes = NODE_TYPES[lang]?.[args.symbol_kind]; |
| 178 | if (!nodeTypes) { |
| 179 | return { |
| 180 | content: `[UNSUPPORTED] AST editing of ${args.symbol_kind} not supported for ${lang}. Use edit_text.`, |
| 181 | isError: true, |
| 182 | }; |
| 183 | } |
| 184 | |
| 185 | let parserResult; |
| 186 | try { |
| 187 | parserResult = await getParser(lang); |
| 188 | } catch (e: any) { |
| 189 | const msg = e?.message ?? String(e); |
| 190 | // Common failure: bundled .wasm grammar was built for a tree-sitter ABI |
| 191 | // version that doesn't match the installed `web-tree-sitter` runtime. |
| 192 | // Surface a clear, actionable message — and recommend edit_text as the |
| 193 | // immediate workaround instead of looping. |
| 194 | if (/Incompatible language version|abi/i.test(msg)) { |
| 195 | return { |
| 196 | content: `[AST_GRAMMAR_INCOMPATIBLE] The bundled tree-sitter grammar for ${lang} is incompatible with the installed runtime (${msg}). This is a packaging issue, not your code. Workaround: use \`edit_text\` for this edit — it's slightly less safe but works reliably. Don't retry edit_symbol on this file.`, |
| 197 | isError: true, |
| 198 | }; |
| 199 | } |
| 200 | return { |
| 201 | content: `[AST_GRAMMAR_LOAD_ERROR] Could not load grammar for ${lang}: ${msg}. Use \`edit_text\` instead.`, |
| 202 | isError: true, |
| 203 | }; |
| 204 | } |
| 205 | if (!parserResult) { |
| 206 | return { |
| 207 | content: `[AST_UNAVAILABLE] Tree-sitter grammar for ${lang} not loaded. Use edit_text instead. To enable AST editing, install the grammar wasm into grammars/tree-sitter-${lang}.wasm`, |
| 208 | isError: true, |
| 209 | }; |
| 210 | } |
| 211 | |
| 212 | let source: string; |
| 213 | try { |
| 214 | source = await fs.readFile(abs, 'utf-8'); |
| 215 | } catch (e: any) { |
| 216 | return { content: `[ERROR] Cannot read ${args.path}: ${e.message}`, isError: true }; |
| 217 | } |
| 218 | |
| 219 | let tree; |
| 220 | try { |
| 221 | tree = parserResult.parser.parse(source); |
| 222 | } catch (e: any) { |
nothing calls this directly
no test coverage detected