(cwd: string, ranked: RankedFile[], maxFiles: number)
| 346 | * embeddings). We seed it with the top 3 semantic hits and expand 1 hop. |
| 347 | */ |
| 348 | export async function expandRankedViaGraph(cwd: string, ranked: RankedFile[], maxFiles: number): Promise<RankedFile[]> { |
| 349 | const { buildImportGraph, expandViaGraph } = await import('./import-graph.js'); |
| 350 | // Walk project source for the graph (cap to keep it fast). |
| 351 | const files = await walkSource(cwd, 2000); |
| 352 | const graph = await buildImportGraph( |
| 353 | cwd, |
| 354 | files.map(f => ({ rel: f.rel, content: f.content })), |
| 355 | ); |
| 356 | |
| 357 | const seeds = ranked.slice(0, 3).map(r => r.file); |
| 358 | const neighbors = expandViaGraph(graph, seeds, { hops: 1, maxFiles: maxFiles + 6, hubDamping: true }); |
| 359 | |
| 360 | const seen = new Set(ranked.map(r => r.file)); |
| 361 | const out: RankedFile[] = [...ranked]; |
| 362 | const lowestSemantic = ranked.length ? ranked[ranked.length - 1]!.score : 0.3; |
| 363 | for (const n of neighbors) { |
| 364 | if (n.distance === 0 || seen.has(n.file)) continue; // already a semantic hit |
| 365 | seen.add(n.file); |
| 366 | // Discounted score: below the lowest semantic hit, scaled by the graph |
| 367 | // weight (which already accounts for distance, direction, and hub damping). |
| 368 | out.push({ |
| 369 | file: n.file, |
| 370 | score: Math.max(0.04, lowestSemantic * 0.5 * n.weight), |
| 371 | bestLines: 'import-neighbor', |
| 372 | }); |
| 373 | } |
| 374 | return out.sort((a, b) => b.score - a.score).slice(0, maxFiles); |
| 375 | } |
| 376 | |
| 377 | /** Render the ranked files as a system-prompt context block (empty string if none). */ |
| 378 | export function formatRetrievalBlock(files: RankedFile[]): string { |
no test coverage detected