* Handle codegraph_node
(args: Record<string, unknown>)
| 8214 | * Handle codegraph_node |
| 8215 | */ |
| 8216 | private async handleNode(args: Record<string, unknown>): Promise<ToolResult> { |
| 8217 | const cg = this.getCodeGraph(args.projectPath as string | undefined); |
| 8218 | // Default to false to minimize context usage |
| 8219 | const includeCode = args.includeCode === true; |
| 8220 | const fileHint = typeof args.file === 'string' && args.file.trim() ? args.file.trim() : undefined; |
| 8221 | const lineHint = typeof args.line === 'number' && args.line > 0 ? args.line : undefined; |
| 8222 | const offset = typeof args.offset === 'number' && args.offset > 0 ? Math.floor(args.offset) : undefined; |
| 8223 | const limit = typeof args.limit === 'number' && args.limit > 0 ? Math.floor(args.limit) : undefined; |
| 8224 | const symbolsOnly = args.symbolsOnly === true; |
| 8225 | const symbolRaw = typeof args.symbol === 'string' ? args.symbol.trim() : ''; |
| 8226 | |
| 8227 | // FILE READ MODE: a `file` with no `symbol` reads that file like the Read |
| 8228 | // tool — its current on-disk source with line numbers, narrowable with |
| 8229 | // `offset`/`limit` exactly as Read does — PLUS a one-line blast-radius |
| 8230 | // header (which files depend on it). `symbolsOnly` returns just the |
| 8231 | // structural map instead. Backed by the index: same bytes Read gives you. |
| 8232 | if (!symbolRaw && fileHint) { |
| 8233 | return this.handleFileView(cg, fileHint, { offset, limit, symbolsOnly }); |
| 8234 | } |
| 8235 | |
| 8236 | const symbol = this.validateString(args.symbol, 'symbol'); |
| 8237 | if (typeof symbol !== 'string') return symbol; |
| 8238 | |
| 8239 | let matches = this.findSymbolMatches(cg, symbol); |
| 8240 | if (matches.length === 0) { |
| 8241 | return this.textResult(`Symbol "${symbol}" not found in the codebase`); |
| 8242 | } |
| 8243 | |
| 8244 | // Disambiguate a heavily-overloaded name to a specific definition the caller |
| 8245 | // pinned by file/line (the `file:line` a trail or another tool showed it) — |
| 8246 | // so it can fetch e.g. `Harness::poll` at harness.rs:153 out of 50+ `poll`s |
| 8247 | // instead of Reading. file matches by path suffix/substring; line prefers the |
| 8248 | // def whose body contains it, else the nearest start. Only narrows (never |
| 8249 | // empties — if a hint matches nothing it's ignored). |
| 8250 | if (matches.length > 1 && (fileHint || lineHint !== undefined)) { |
| 8251 | const norm = (p: string) => p.replace(/\\/g, '/').toLowerCase(); |
| 8252 | let narrowed = matches; |
| 8253 | if (fileHint) { |
| 8254 | const fh = norm(fileHint); |
| 8255 | const byFile = narrowed.filter((n) => norm(n.filePath).endsWith(fh) || norm(n.filePath).includes(fh)); |
| 8256 | if (byFile.length > 0) narrowed = byFile; |
| 8257 | } |
| 8258 | if (lineHint !== undefined && narrowed.length > 1) { |
| 8259 | const containing = narrowed.filter((n) => n.startLine <= lineHint && (n.endLine ?? n.startLine) >= lineHint); |
| 8260 | narrowed = containing.length > 0 |
| 8261 | ? containing |
| 8262 | : [...narrowed].sort((a, b) => Math.abs(a.startLine - lineHint) - Math.abs(b.startLine - lineHint)).slice(0, 1); |
| 8263 | } |
| 8264 | if (narrowed.length > 0) matches = narrowed; |
| 8265 | } |
| 8266 | |
| 8267 | // Single definition — the common case. |
| 8268 | if (matches.length === 1) { |
| 8269 | return this.textResult(this.truncateOutput(await this.renderNodeSection(cg, matches[0]!, includeCode))); |
| 8270 | } |
| 8271 | |
| 8272 | // Multiple definitions share this name — overloads, or same-named methods on |
| 8273 | // different types (Alamofire `didCompleteTask`/`task`/`validate`, gin |
no test coverage detected