@andespindola/brainlink 0.1.0-beta.148 → 0.1.0-beta.149

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -59,6 +59,7 @@ const state = {
59
59
  searchToken: 0,
60
60
  fetchToken: 0,
61
61
  fetchTimer: null,
62
+ fetchAbortController: null,
62
63
  cameraSyncScheduled: false,
63
64
  lastDragFetchAt: 0,
64
65
  lastWheelAt: 0,
@@ -483,6 +484,11 @@ const fitFromChunk = () => {
483
484
 
484
485
  const fetchChunk = async ({ fit } = { fit: false }) => {
485
486
  const token = ++state.fetchToken
487
+ if (state.fetchAbortController) {
488
+ state.fetchAbortController.abort()
489
+ }
490
+ const controller = new AbortController()
491
+ state.fetchAbortController = controller
486
492
  const worldTopLeft = screenToWorld(0, 0)
487
493
  const worldBottomRight = screenToWorld(state.viewport.width, state.viewport.height)
488
494
  const x = Math.min(worldTopLeft.x, worldBottomRight.x)
@@ -504,12 +510,15 @@ const fetchChunk = async ({ fit } = { fit: false }) => {
504
510
  params.set('agent', state.agentId)
505
511
  }
506
512
 
507
- const response = await fetch('/api/graph-stream?' + params.toString())
513
+ const response = await fetch('/api/graph-stream?' + params.toString(), { signal: controller.signal })
508
514
  if (!response.ok) {
509
515
  throw new Error('Failed to fetch graph stream chunk')
510
516
  }
511
517
 
512
518
  const chunk = await response.json()
519
+ if (controller.signal.aborted) {
520
+ return
521
+ }
513
522
  if (token !== state.fetchToken) {
514
523
  return
515
524
  }
@@ -551,6 +560,9 @@ const scheduleChunkFetch = ({ fit } = { fit: false }) => {
551
560
  state.fetchTimer = setTimeout(() => {
552
561
  state.fetchTimer = null
553
562
  fetchChunk({ fit }).catch((error) => {
563
+ if (error && error.name === 'AbortError') {
564
+ return
565
+ }
554
566
  console.error(error)
555
567
  })
556
568
  }, delay)
@@ -55,6 +55,7 @@ const createLayoutCache = (signature, nodes, edges, groups) => {
55
55
  const groupById = new Map(groups.map((group) => [group.id, group]));
56
56
  const degrees = new Map();
57
57
  const adjacencyByNodeId = new Map();
58
+ const rankedAdjacencyByNodeId = new Map();
58
59
  for (let index = 0; index < edges.length; index += 1) {
59
60
  const edge = edges[index];
60
61
  degrees.set(edge.source, (degrees.get(edge.source) ?? 0) + edge.weight);
@@ -69,12 +70,16 @@ const createLayoutCache = (signature, nodes, edges, groups) => {
69
70
  targetAdjacency.push(index);
70
71
  adjacencyByNodeId.set(edge.target, targetAdjacency);
71
72
  }
73
+ for (const [nodeId, adjacency] of adjacencyByNodeId.entries()) {
74
+ rankedAdjacencyByNodeId.set(nodeId, [...adjacency].sort((leftIndex, rightIndex) => edgeRank(edges[rightIndex]) - edgeRank(edges[leftIndex])));
75
+ }
72
76
  return {
73
77
  signature,
74
78
  degrees,
75
79
  nodeById,
76
80
  groupById,
77
- adjacencyByNodeId
81
+ adjacencyByNodeId,
82
+ rankedAdjacencyByNodeId
78
83
  };
79
84
  };
80
85
  const getOrCreateLayoutCache = (signature, nodes, edges, groups) => {
@@ -198,17 +203,16 @@ const collectEdgesForNodes = (allEdges, cache, nodeRows, edgeBudget, maxEdgesPer
198
203
  const nodeIds = new Set(nodeRows.map((row) => row[0]));
199
204
  const collected = new Map();
200
205
  for (const nodeId of nodeIds) {
201
- const adjacency = cache.adjacencyByNodeId.get(nodeId);
206
+ const adjacency = cache.rankedAdjacencyByNodeId.get(nodeId);
202
207
  if (!adjacency || adjacency.length === 0) {
203
208
  continue;
204
209
  }
205
210
  let perNodeCount = 0;
206
- const rankedAdjacency = [...adjacency].sort((leftIndex, rightIndex) => edgeRank(allEdges[rightIndex]) - edgeRank(allEdges[leftIndex]));
207
- for (let index = 0; index < rankedAdjacency.length; index += 1) {
211
+ for (let index = 0; index < adjacency.length; index += 1) {
208
212
  if (perNodeCount >= maxEdgesPerNode) {
209
213
  break;
210
214
  }
211
- const edge = allEdges[rankedAdjacency[index]];
215
+ const edge = allEdges[adjacency[index]];
212
216
  if (!edge.target || !nodeIds.has(edge.source) || !nodeIds.has(edge.target)) {
213
217
  continue;
214
218
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andespindola/brainlink",
3
- "version": "0.1.0-beta.148",
3
+ "version": "0.1.0-beta.149",
4
4
  "description": "Local-first knowledge memory for agents with Markdown, backlinks, indexing and context retrieval.",
5
5
  "type": "module",
6
6
  "license": "MIT",