| 24 | } |
| 25 | |
| 26 | export function scopeFetch({ nodes, edges, gate, degrees = {} }: ScopeFetchOptions) { |
| 27 | const calls: { tool: string; args: Record<string, unknown> }[] = []; |
| 28 | const fetch = vi.fn(async (url: RequestInfo | URL, init?: RequestInit) => { |
| 29 | if (String(url).includes('/api/layout')) return new Response(JSON.stringify({ nodes, edges, total_nodes: nodes.length })); |
| 30 | const request = JSON.parse(String(init?.body)) as { params: { name: string; arguments: Record<string, unknown> } }; |
| 31 | calls.push({ tool: request.params.name, args: request.params.arguments }); |
| 32 | if (request.params.name === 'index_status') { |
| 33 | return new Response(JSON.stringify({ jsonrpc: '2.0', id: 1, result: { content: [{ type: 'text', text: JSON.stringify({ indexed_at: 'generation-1' }) }] } })); |
| 34 | } |
| 35 | const query = String(request.params.arguments.query); |
| 36 | const columns = (prefix: string) => ['id', 'label', 'name', 'qn', 'file', 'start_line', 'end_line'].map(key => prefix + key); |
| 37 | const values = (entry: GraphNode) => [entry.id, entry.label, entry.name, entry.qualified_name ?? '', entry.file_path ?? '', entry.start_line ?? '', entry.end_line ?? ''].map(String); |
| 38 | const names = [...query.matchAll(/qualified_name = "([^"]+)"/g)].map(match => match[1]); |
| 39 | let cols: string[], rows: string[][]; |
| 40 | if (query.includes('n.in_degree')) { |
| 41 | cols = ['id', 'calls_in', 'calls_out']; |
| 42 | rows = nodes.filter(entry => names.includes(entry.qualified_name ?? '')) |
| 43 | .map(entry => [String(entry.id), String(degrees[entry.id]?.in ?? 0), String(degrees[entry.id]?.out ?? 0)]); |
| 44 | } else if (query.startsWith('MATCH (n)')) { |
| 45 | // A file scope finds its roots by path. |
| 46 | const files = [...query.matchAll(/n\.file_path = "([^"]+)"/g)].map(match => match[1]); |
| 47 | cols = columns(''); rows = nodes.filter(entry => names.includes(entry.qualified_name ?? '') || files.includes(entry.file_path ?? '')).map(values); |
| 48 | } else { |
| 49 | if (gate) await gate(); |
| 50 | const inbound = query.startsWith('MATCH (b)<-'); |
| 51 | cols = ['edge_id', 'edge_type', 'edge_line', ...columns('a_'), ...columns('b_')]; |
| 52 | rows = edges.flatMap((edge, i) => { |
| 53 | const a = nodes.find(entry => entry.id === edge.source)!, b = nodes.find(entry => entry.id === edge.target)!; |
| 54 | const seed = inbound ? b : a; |
| 55 | return names.includes(seed.qualified_name ?? '') |
| 56 | ? [[String(edge.id ?? i + 1), edge.type, String(edge.line ?? ''), ...values(a), ...values(b)]] : []; |
| 57 | }); |
| 58 | } |
| 59 | const text = `rows: ${rows.length} (cols: ${cols.join(' ')})\n${rows.map(row => ' ' + row.map(value => JSON.stringify(value || '-')).join(' ')).join('\n')}\ntotal: ${rows.length}\nhas_more: false\ntruncated: false`; |
| 60 | return new Response(JSON.stringify({ jsonrpc: '2.0', id: 1, result: { content: [{ type: 'text', text }] } })); |
| 61 | }); |
| 62 | return { fetch, calls }; |
| 63 | } |