MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / loadGraphScope

Function loadGraphScope

graph-ui/src/galaxy/graph-scope.ts:126–248  ·  view source on GitHub ↗
(project: string, scope: GraphScope, depth: number, direction: TraceDirection,
    layout: GraphData | undefined, options: { fetch?: typeof globalThis.fetch; signal?: AbortSignal; client?: GraphQueryClient; previous?: ScopedGraph; edgeTypes?: readonly string[]; cache?: GraphNeighborhoodTransaction;
        /** Called after every relationship request with what is loaded so far. */
        onProgress?: (progress: ScopeProgress) => void;
        /** The render limits: a layer that grows past them stops and is marked partial. */
        limits?: { nodes: number; edges: number } } = {})

Source from the content-addressed store, hash-verified

124}
125
126export async function loadGraphScope(project: string, scope: GraphScope, depth: number, direction: TraceDirection,
127 layout: GraphData | undefined, options: { fetch?: typeof globalThis.fetch; signal?: AbortSignal; client?: GraphQueryClient; previous?: ScopedGraph; edgeTypes?: readonly string[]; cache?: GraphNeighborhoodTransaction;
128 /** Called after every relationship request with what is loaded so far. */
129 onProgress?: (progress: ScopeProgress) => void;
130 /** The render limits: a layer that grows past them stops and is marked partial. */
131 limits?: { nodes: number; edges: number } } = {}): Promise<ScopedGraph> {
132 options.signal?.throwIfAborted();
133 const client = options.client ?? new RpcIntelligenceClient({ fetch: options.fetch, signal: options.signal });
134 const edgeTypes = options.edgeTypes === undefined ? undefined : [...new Set(options.edgeTypes)].sort();
135 if (edgeTypes?.some(type => !/^[A-Za-z_][A-Za-z0-9_]*$/.test(type))) throw new Error('Invalid graph relationship type.');
136 const allowedTypes = edgeTypes === undefined ? undefined : new Set(edgeTypes);
137 const traversalKey = JSON.stringify([project, scope, direction, graphEdgeTypesKey(edgeTypes)]);
138 const relationship = edgeTypes?.length ? `[r:${edgeTypes.join('|')}]` : '[r]';
139 const known = new Map(layout?.nodes.map(node => [node.id, node]) ?? []);
140 if (scope.kind === 'node') {
141 const cachedNode = options.cache?.node(scope.id);
142 if (cachedNode && (!scope.qualifiedName || cachedNode.qualified_name === scope.qualifiedName)) known.set(scope.id, cachedNode);
143 }
144 if (scope.kind === 'node' && !(scope.qualifiedName ?? known.get(scope.id)?.qualified_name))
145 throw new Error('This node has no indexed qualified identity; its relationships cannot be retrieved reliably.');
146 const predicate = scope.kind === 'node' ? `n.qualified_name = "${escapeLiteral(scope.qualifiedName ?? known.get(scope.id)?.qualified_name ?? '')}"` : scope.kind === 'symbol' ? `n.qualified_name = "${escapeLiteral(scope.qualifiedName)}"` : scope.kind === 'file' ? `n.file_path = "${escapeLiteral(scope.path)}"`
147 : `n.file_path STARTS WITH "${escapeLiteral(scope.path.replace(/\/$/, '') + '/')}"`;
148 // A partial layer misses part of its frontier, so it is never the base of the next one.
149 const previous = options.previous?.traversalKey === traversalKey && options.previous.depth <= depth && !options.previous.partial ? options.previous : undefined;
150 let requests = 0;
151 const counted = () => { requests += 1; };
152 const cachedNode = scope.kind === 'node' ? options.cache?.node(scope.id) : undefined;
153 let rootNodes = cachedNode && cachedNode.qualified_name === (scope.kind === 'node' ? scope.qualifiedName ?? known.get(scope.id)?.qualified_name : '')
154 ? [cachedNode] : options.cache?.roots(predicate);
155 if (!previous && !rootNodes) {
156 const rootRows = await readGraphPages(client, project, `MATCH (n) WHERE ${predicate} RETURN ${nodeColumns('n', '')}`, 'id', options.signal, counted);
157 rootNodes = rootRows.map(row => readNode(row, '', known));
158 options.cache?.rememberRoots(predicate, rootNodes);
159 }
160 const candidates = previous ? previous.data.nodes.filter(node => previous.roots.has(node.id))
161 : rootNodes!.filter(node => scope.kind !== 'node' || node.id === scope.id);
162 const roots = scope.kind === 'file' && scope.range ? readerGraphFocus(candidates, scope.path, scope.range).ids : new Set(candidates.map(node => node.id));
163 const nodes = new Map((previous?.data.nodes ?? candidates.filter(node => roots.has(node.id))).map(node => [node.id, node]));
164 const levels = new Map<number, number>(previous?.levels ?? [...roots].map(identity => [identity, 0] as const));
165 const edges = new Map<number, GraphEdge>(previous?.data.edges.filter(edge => edge.id !== undefined).map(edge => [edge.id!, edge]) ?? []);
166 let frontier = edgeTypes?.length === 0 ? [] : previous?.frontier ?? [...roots], reachedDepth = previous?.depth ?? 0;
167 let partial: ScopePartial | undefined;
168 const overLimit = (layer: number): ScopePartial | undefined => !options.limits ? undefined
169 : nodes.size > options.limits.nodes ? { layer, nodes: nodes.size, edges: edges.size, limit: 'nodes' }
170 : edges.size > options.limits.edges ? { layer, nodes: nodes.size, edges: edges.size, limit: 'edges' } : undefined;
171 for (let hop = reachedDepth; hop < Math.max(depth, roots.size > 1 ? 1 : 0) && frontier.length && !partial; hop++) {
172 const next = new Set<number>();
173 const fileBoundary = hop === 0 && (scope.kind === 'folder' || (scope.kind === 'file' && !scope.range));
174 const accept = (evidenceNodes: readonly GraphNode[], evidenceEdges: readonly GraphEdge[]) => {
175 const byId = new Map(evidenceNodes.map(node => [node.id, node]));
176 for (const edge of evidenceEdges) {
177 for (const identity of [edge.source, edge.target]) {
178 const node = byId.get(identity)!;
179 if (!nodes.has(identity)) { next.add(identity); levels.set(identity, hop + 1); }
180 nodes.set(identity, node);
181 }
182 edges.set(edge.id!, edge);
183 }

Callers 3

useGraphScopeFunction · 0.90

Calls 15

escapeLiteralFunction · 0.90
readerGraphFocusFunction · 0.90
graphEdgeTypesKeyFunction · 0.85
readGraphPagesFunction · 0.85
nodeColumnsFunction · 0.85
anyNamesFunction · 0.85
idFunction · 0.85
overLimitFunction · 0.85
arrangeScopedGraphFunction · 0.85
nodeMethod · 0.80
rootsMethod · 0.80
rememberRootsMethod · 0.80

Tested by

no test coverage detected