* Handle codegraph_impact
(args: Record<string, unknown>)
| 3576 | * Handle codegraph_impact |
| 3577 | */ |
| 3578 | private async handleImpact(args: Record<string, unknown>): Promise<ToolResult> { |
| 3579 | const symbol = this.validateString(args.symbol, 'symbol'); |
| 3580 | if (typeof symbol !== 'string') return symbol; |
| 3581 | |
| 3582 | const cg = this.getCodeGraph(args.projectPath as string | undefined); |
| 3583 | const depth = clamp((args.depth as number) || 2, 1, 10); |
| 3584 | const fileFilter = typeof args.file === 'string' ? args.file : undefined; |
| 3585 | |
| 3586 | const allMatches = this.findAllSymbols(cg, symbol); |
| 3587 | if (allMatches.nodes.length === 0) { |
| 3588 | return this.textResult(`Symbol "${symbol}" not found in the codebase${allMatches.note}`); |
| 3589 | } |
| 3590 | |
| 3591 | const { groups, filteredOut } = this.groupDefinitions(allMatches.nodes, fileFilter); |
| 3592 | const answerPaths = new Set(groups.flat().map(node => node.filePath)); |
| 3593 | const filterNote = filteredOut |
| 3594 | ? `\n\n> **Note:** no definition of "${symbol}" matches file "${fileFilter}" — showing all definitions instead.` |
| 3595 | : ''; |
| 3596 | |
| 3597 | const impactOf = (defNodes: Node[]) => { |
| 3598 | const mergedNodes = new Map<string, Node>(); |
| 3599 | const mergedEdges: Edge[] = []; |
| 3600 | const seenEdges = new Set<string>(); |
| 3601 | let truncated = false; |
| 3602 | for (const node of defNodes) { |
| 3603 | const impact = cg.getImpactRadius(node.id, depth); |
| 3604 | truncated ||= impact.truncated === true; |
| 3605 | for (const [id, n] of impact.nodes) { |
| 3606 | if (!mergedNodes.has(id) && mergedNodes.size >= DEFAULT_IMPACT_MAX_NODES) { |
| 3607 | truncated = true; |
| 3608 | continue; |
| 3609 | } |
| 3610 | mergedNodes.set(id, n); |
| 3611 | answerPaths.add(n.filePath); |
| 3612 | } |
| 3613 | for (const e of impact.edges) { |
| 3614 | if (!mergedNodes.has(e.source) || !mergedNodes.has(e.target)) { |
| 3615 | truncated = true; |
| 3616 | continue; |
| 3617 | } |
| 3618 | const key = `${e.source}->${e.target}:${e.kind}`; |
| 3619 | if (!seenEdges.has(key)) { |
| 3620 | if (mergedEdges.length >= DEFAULT_IMPACT_MAX_EDGES) { |
| 3621 | truncated = true; |
| 3622 | continue; |
| 3623 | } |
| 3624 | seenEdges.add(key); |
| 3625 | mergedEdges.push(e); |
| 3626 | } |
| 3627 | } |
| 3628 | } |
| 3629 | return { nodes: mergedNodes, edges: mergedEdges, roots: defNodes.map((n) => n.id), truncated }; |
| 3630 | }; |
| 3631 | |
| 3632 | // Single definition (or same-file overloads): the familiar merged report. |
| 3633 | if (groups.length === 1) { |
| 3634 | const formatted = this.formatImpact(symbol, impactOf(groups[0]!)) + (fileFilter && !filteredOut ? "" : allMatches.note) + filterNote; |
| 3635 | return this.answerResult(cg, this.truncateOutput(formatted), answerPaths); |
no test coverage detected