@andespindola/brainlink 0.1.0-alpha.8 → 0.1.0-beta.0
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.
- package/AGENTS.md +2 -0
- package/README.md +87 -7
- package/SECURITY.md +14 -2
- package/assets/brainlink-logo.svg +25 -0
- package/dist/application/frontend/client-js.js +80 -18
- package/dist/application/get-graph-layout.js +26 -1
- package/dist/application/index-vault.js +11 -3
- package/dist/application/server/host-security.js +3 -3
- package/dist/application/server/routes.js +45 -1
- package/dist/application/start-server.js +2 -2
- package/dist/application/watch-vault.js +4 -1
- package/dist/cli/commands/read-commands.js +10 -10
- package/dist/cli/commands/write-commands.js +4 -6
- package/dist/cli/runtime.js +2 -1
- package/dist/domain/agents.js +2 -1
- package/dist/domain/graph-layout.js +90 -29
- package/dist/domain/markdown.js +80 -3
- package/dist/infrastructure/bucket-vault.js +171 -0
- package/dist/infrastructure/config.js +5 -0
- package/dist/infrastructure/file-system-vault.js +21 -3
- package/dist/infrastructure/sqlite/document-writer.js +4 -3
- package/dist/infrastructure/sqlite/graph-reader.js +22 -10
- package/dist/infrastructure/sqlite/schema.js +12 -1
- package/dist/infrastructure/sqlite-index.js +6 -1
- package/dist/mcp/server.js +13 -3
- package/dist/mcp/tools.js +100 -42
- package/docs/AGENT_USAGE.md +64 -1
- package/docs/ARCHITECTURE.md +22 -1
- package/docs/RELEASE.md +1 -1
- package/docs/templates/agent-namespace-bootstrap.md +27 -0
- package/docs/templates/agent-note-template.md +31 -0
- package/package.json +6 -2
package/AGENTS.md
CHANGED
|
@@ -37,6 +37,8 @@ Use this loop when using Brainlink as memory:
|
|
|
37
37
|
|
|
38
38
|
When an agent adds durable memory, it should connect the new note to at least one existing concept unless the note is intentionally a root concept. Prefer exact note titles in links, for example `[[Architecture]]`, and run `broken-links`, `orphans` or `validate` when the graph looks disconnected.
|
|
39
39
|
|
|
40
|
+
Agents can mark important relationships by placing priority hints on the same line as a wiki link, for example `[[Architecture]] priority: high`, `[[Incident Runbook]] #important` or `[[Incident Runbook]] #critical`. Indexed graph edges expose `weight` and `priority` so agents can sort related notes by importance.
|
|
41
|
+
|
|
40
42
|
## Commands
|
|
41
43
|
|
|
42
44
|
```bash
|
package/README.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="./assets/brainlink-logo.svg" alt="Brainlink" width="720">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
1
5
|
# Brainlink
|
|
2
6
|
|
|
3
7
|
Local-first memory and knowledge graph for AI agents.
|
|
@@ -58,10 +62,12 @@ Markdown is the source of truth. `.brainlink/brainlink.db` is only a rebuildable
|
|
|
58
62
|
|
|
59
63
|
- Local-first Markdown vault.
|
|
60
64
|
- Obsidian-compatible `[[wiki links]]` and `#tags`.
|
|
65
|
+
- Weighted graph edges so agents can rank relationship importance and priority.
|
|
61
66
|
- Backlinks, broken-link reports, orphan detection and validation.
|
|
62
67
|
- Full-text, semantic and hybrid retrieval modes.
|
|
63
68
|
- SQLite-backed semantic candidate buckets for larger vaults.
|
|
64
69
|
- Agent namespaces under `agents/<agent-id>/`.
|
|
70
|
+
- S3-compatible bucket vaults through `s3://bucket/prefix` URIs.
|
|
65
71
|
- CLI with machine-readable `--json` output.
|
|
66
72
|
- Short CLI alias: `blink`.
|
|
67
73
|
- Built-in MCP stdio server for agent tool integration.
|
|
@@ -252,6 +258,36 @@ http://127.0.0.1:4321
|
|
|
252
258
|
|
|
253
259
|
When `--vault` is omitted, commands use the default vault at `$HOME/.brainlink/vault`. Pass `--vault` or configure `vault` in `brainlink.config.json` when you want a custom project-local vault.
|
|
254
260
|
|
|
261
|
+
## Bucket Vaults
|
|
262
|
+
|
|
263
|
+
Brainlink can use an S3-compatible bucket as the Markdown source of truth:
|
|
264
|
+
|
|
265
|
+
```bash
|
|
266
|
+
export AWS_REGION="us-east-1"
|
|
267
|
+
export AWS_ACCESS_KEY_ID="..."
|
|
268
|
+
export AWS_SECRET_ACCESS_KEY="..."
|
|
269
|
+
|
|
270
|
+
blink add "Architecture" \
|
|
271
|
+
--vault "s3://my-memory-bucket/brainlink" \
|
|
272
|
+
--content "Bucket Markdown is the source of truth. #architecture"
|
|
273
|
+
|
|
274
|
+
blink index --vault "s3://my-memory-bucket/brainlink"
|
|
275
|
+
blink context "architecture" --vault "s3://my-memory-bucket/brainlink"
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
For Cloudflare R2, MinIO or another S3-compatible endpoint:
|
|
279
|
+
|
|
280
|
+
```bash
|
|
281
|
+
export BRAINLINK_S3_ENDPOINT="https://<account-id>.r2.cloudflarestorage.com"
|
|
282
|
+
export BRAINLINK_S3_FORCE_PATH_STYLE=1
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
Bucket vaults mirror Markdown into a local cache under
|
|
286
|
+
`$BRAINLINK_HOME/bucket-cache`. The bucket remains canonical; the local
|
|
287
|
+
`.brainlink/brainlink.db` stays a disposable index. Run `index` after remote
|
|
288
|
+
bucket changes before relying on `search`, `context`, graph or validation
|
|
289
|
+
commands. Watch mode is only supported for local filesystem vaults.
|
|
290
|
+
|
|
255
291
|
## Core Model
|
|
256
292
|
|
|
257
293
|
```txt
|
|
@@ -355,12 +391,21 @@ Available tools:
|
|
|
355
391
|
- `brainlink_add_note`: write durable Markdown memory and reindex.
|
|
356
392
|
- `brainlink_index`: rebuild the vault index.
|
|
357
393
|
- `brainlink_validate`: validate broken links and orphan notes.
|
|
358
|
-
- `brainlink_graph`: read indexed graph nodes and links.
|
|
394
|
+
- `brainlink_graph`: read indexed graph nodes and weighted links.
|
|
359
395
|
- `brainlink_broken_links`: list unresolved wiki links.
|
|
360
396
|
- `brainlink_orphans`: list disconnected notes.
|
|
361
397
|
|
|
362
398
|
The same linking rule applies through MCP: `brainlink_context` is read-only, and real graph links require Markdown notes with explicit `[[wiki links]]` followed by indexing.
|
|
363
399
|
|
|
400
|
+
Agents can raise the importance of a relationship by putting priority markers on the same line as a wiki link:
|
|
401
|
+
|
|
402
|
+
```md
|
|
403
|
+
- [ ] Review [[Architecture]] priority: high
|
|
404
|
+
Related: [[Incident Runbook]] #critical
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
Indexed edges expose `weight` and `priority` (`low`, `normal`, `high`, `critical`) through CLI JSON, HTTP graph APIs and `brainlink_graph`.
|
|
408
|
+
|
|
364
409
|
## Graph UI
|
|
365
410
|
|
|
366
411
|
Start the local frontend:
|
|
@@ -374,7 +419,7 @@ By default, the server uses `$HOME/.brainlink/vault`. Pass `--vault ./vault` onl
|
|
|
374
419
|
The graph UI shows:
|
|
375
420
|
|
|
376
421
|
- notes as nodes
|
|
377
|
-
- `[[wiki links]]` as edges
|
|
422
|
+
- `[[wiki links]]` as weighted edges
|
|
378
423
|
- backlinks and outgoing links
|
|
379
424
|
- full Markdown content for the selected note
|
|
380
425
|
- neutral graph nodes with segment/group metadata
|
|
@@ -391,7 +436,7 @@ blink server --vault ./vault --no-index
|
|
|
391
436
|
|
|
392
437
|
The HTTP API is read-only and exists only to power the graph UI and local inspection workflows.
|
|
393
438
|
|
|
394
|
-
The server refuses non-loopback hosts
|
|
439
|
+
The server always refuses non-loopback hosts. Brainlink HTTP only runs on localhost.
|
|
395
440
|
|
|
396
441
|
Routes:
|
|
397
442
|
|
|
@@ -488,7 +533,7 @@ blink links --vault ./vault
|
|
|
488
533
|
blink links --vault ./vault --agent coding-agent
|
|
489
534
|
```
|
|
490
535
|
|
|
491
|
-
Lists indexed wiki links.
|
|
536
|
+
Lists indexed wiki links. JSON output includes `weight` and `priority` for each relationship.
|
|
492
537
|
|
|
493
538
|
### `backlinks`
|
|
494
539
|
|
|
@@ -497,7 +542,7 @@ blink backlinks "Architecture" --vault ./vault
|
|
|
497
542
|
blink backlinks "Architecture" --vault ./vault --agent coding-agent
|
|
498
543
|
```
|
|
499
544
|
|
|
500
|
-
Lists notes pointing to a target title.
|
|
545
|
+
Lists notes pointing to a target title, ordered by strongest relationship first. JSON output includes `weight` and `priority`.
|
|
501
546
|
|
|
502
547
|
### `graph`
|
|
503
548
|
|
|
@@ -506,7 +551,7 @@ blink graph --vault ./vault --json
|
|
|
506
551
|
blink graph --vault ./vault --agent coding-agent --json
|
|
507
552
|
```
|
|
508
553
|
|
|
509
|
-
Prints indexed graph data.
|
|
554
|
+
Prints indexed graph data. Edges include `weight` and `priority` so agents can categorize importance.
|
|
510
555
|
|
|
511
556
|
### `stats`
|
|
512
557
|
|
|
@@ -566,7 +611,7 @@ blink server --vault ./vault --watch
|
|
|
566
611
|
|
|
567
612
|
Starts the local read-only graph UI and HTTP API.
|
|
568
613
|
|
|
569
|
-
|
|
614
|
+
The HTTP server only binds to loopback hosts such as `127.0.0.1`, `localhost` or `::1`.
|
|
570
615
|
|
|
571
616
|
## Machine-Readable Output
|
|
572
617
|
|
|
@@ -592,22 +637,39 @@ Brainlink reads `brainlink.config.json` or `.brainlink.json` from the current wo
|
|
|
592
637
|
"host": "127.0.0.1",
|
|
593
638
|
"port": 4321,
|
|
594
639
|
"allowedVaults": [".brainlink-vault"],
|
|
640
|
+
"defaultAgent": "shared",
|
|
595
641
|
"defaultSearchLimit": 10,
|
|
596
642
|
"defaultContextTokens": 2000,
|
|
597
643
|
"embeddingProvider": "local",
|
|
598
644
|
"defaultSearchMode": "hybrid",
|
|
599
645
|
"chunkSize": 1200
|
|
600
646
|
}
|
|
647
|
+
|
|
648
|
+
`defaultAgent` is optional. When set, CLI and MCP calls that omit `--agent`/`agent` use this value automatically. If not set, behavior remains as before.
|
|
601
649
|
```
|
|
602
650
|
|
|
603
651
|
Use `"embeddingProvider": "none"` when you want FTS-only indexing.
|
|
604
652
|
|
|
653
|
+
For local security checks, set your Snyk token in the environment:
|
|
654
|
+
|
|
655
|
+
```bash
|
|
656
|
+
export SNYK_TOKEN="snyk_..."
|
|
657
|
+
```
|
|
658
|
+
|
|
659
|
+
For GitHub Actions, add a repository secret `SNYK_TOKEN` and the CI/publish workflows will consume it automatically during build/test.
|
|
660
|
+
|
|
605
661
|
Set `BRAINLINK_ALLOWED_VAULTS` for external wrappers, including MCP servers, so a tool cannot pass arbitrary `--vault` paths:
|
|
606
662
|
|
|
607
663
|
```bash
|
|
608
664
|
export BRAINLINK_ALLOWED_VAULTS="/absolute/path/to/project-vault,/absolute/path/to/team-vault"
|
|
609
665
|
```
|
|
610
666
|
|
|
667
|
+
Bucket vaults can be allowlisted with the same variable:
|
|
668
|
+
|
|
669
|
+
```bash
|
|
670
|
+
export BRAINLINK_ALLOWED_VAULTS="s3://my-memory-bucket/brainlink"
|
|
671
|
+
```
|
|
672
|
+
|
|
611
673
|
## Note Format
|
|
612
674
|
|
|
613
675
|
Brainlink supports Markdown with optional frontmatter:
|
|
@@ -714,6 +776,7 @@ The alpha includes local semantic retrieval. Remote embedding providers, remote
|
|
|
714
776
|
Brainlink is local-first by default.
|
|
715
777
|
|
|
716
778
|
- Do not expose the HTTP server publicly without authentication.
|
|
779
|
+
- Brainlink HTTP is localhost-only and refuses non-loopback hosts.
|
|
717
780
|
- Brainlink blocks common secret patterns by default when adding notes. Use `--allow-sensitive` only for intentional, protected vaults.
|
|
718
781
|
- Do not store secrets, credentials, API keys or regulated personal data unless the vault is protected by your own storage controls.
|
|
719
782
|
- Treat `.brainlink/brainlink.db` as disposable derived data.
|
|
@@ -727,3 +790,20 @@ See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
|
727
790
|
## License
|
|
728
791
|
|
|
729
792
|
MIT. See [LICENSE](LICENSE).
|
|
793
|
+
|
|
794
|
+
### Memory Optimization Loop (1-7)
|
|
795
|
+
|
|
796
|
+
Use this when your agent work needs consistent memory quality:
|
|
797
|
+
|
|
798
|
+
1. Start with `blink context "<task>" --agent "$BLINK_AGENT" --json`.
|
|
799
|
+
2. Keep notes focused with explicit `[[wiki links]]` and `#tags`.
|
|
800
|
+
3. Route agent-specific knowledge to dedicated namespaces under `agents/<agent-id>/`.
|
|
801
|
+
4. Keep `shared` as a curated global layer only.
|
|
802
|
+
5. Use targeted queries (`--limit`, explicit terms, `--mode hybrid`) before broad scans.
|
|
803
|
+
6. Run the sync command after writing notes:
|
|
804
|
+
|
|
805
|
+
```bash
|
|
806
|
+
npm run brainlink:sync -- --vault ./vault --agent "$BLINK_AGENT"
|
|
807
|
+
```
|
|
808
|
+
|
|
809
|
+
7. Before final response, keep the returned context sources as the grounding baseline.
|
package/SECURITY.md
CHANGED
|
@@ -5,7 +5,7 @@ Brainlink is local-first.
|
|
|
5
5
|
## Defaults
|
|
6
6
|
|
|
7
7
|
- The HTTP server binds to `127.0.0.1` by default.
|
|
8
|
-
- The HTTP server refuses non-loopback hosts
|
|
8
|
+
- The HTTP server always refuses non-loopback hosts.
|
|
9
9
|
- The HTTP server is read-only and does not expose note creation, indexing or update routes.
|
|
10
10
|
- The SQLite database is a derived local index.
|
|
11
11
|
- Markdown files are user-owned source data.
|
|
@@ -14,7 +14,7 @@ Brainlink is local-first.
|
|
|
14
14
|
|
|
15
15
|
## Remote Exposure
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
Brainlink HTTP is intentionally localhost-only. It does not support binding to a public interface.
|
|
18
18
|
|
|
19
19
|
## Sensitive Memory
|
|
20
20
|
|
|
@@ -32,4 +32,16 @@ External tool wrappers, including MCP servers, should set `BRAINLINK_ALLOWED_VAU
|
|
|
32
32
|
export BRAINLINK_ALLOWED_VAULTS="/absolute/path/to/project-vault"
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
+
For bucket vaults, allowlist the S3 URI prefix:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
export BRAINLINK_ALLOWED_VAULTS="s3://my-memory-bucket/brainlink"
|
|
39
|
+
```
|
|
40
|
+
|
|
35
41
|
When the allowlist is set, CLI commands fail if `--vault` points outside the allowed roots.
|
|
42
|
+
|
|
43
|
+
## Bucket Credentials
|
|
44
|
+
|
|
45
|
+
Bucket vaults use the standard AWS SDK credential chain. Prefer short-lived,
|
|
46
|
+
least-privilege credentials scoped to the specific bucket prefix used by
|
|
47
|
+
Brainlink. Do not store bucket credentials in Markdown notes.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<svg width="900" height="220" viewBox="0 0 900 220" fill="none" xmlns="http://www.w3.org/2000/svg" role="img" aria-labelledby="title desc">
|
|
2
|
+
<title id="title">Brainlink</title>
|
|
3
|
+
<desc id="desc">Brainlink logo with a linked memory graph mark and wordmark.</desc>
|
|
4
|
+
<rect width="900" height="220" fill="transparent"/>
|
|
5
|
+
<g transform="translate(42 24)">
|
|
6
|
+
<rect x="6" y="6" width="160" height="160" rx="36" fill="#F8FAFC"/>
|
|
7
|
+
<rect x="6" y="6" width="160" height="160" rx="36" stroke="#111827" stroke-width="8"/>
|
|
8
|
+
<path d="M47 84C47 65.225 62.225 50 81 50H92C110.775 50 126 65.225 126 84C126 102.775 110.775 118 92 118H81C62.225 118 47 102.775 47 84Z" stroke="#111827" stroke-width="12" stroke-linecap="round"/>
|
|
9
|
+
<path d="M67 84C67 76.268 73.268 70 81 70H92C99.732 70 106 76.268 106 84C106 91.732 99.732 98 92 98H81C73.268 98 67 91.732 67 84Z" fill="#FFFFFF"/>
|
|
10
|
+
<path d="M54 132L43 121L54 110" stroke="#2563EB" stroke-width="10" stroke-linecap="round" stroke-linejoin="round"/>
|
|
11
|
+
<path d="M118 110L129 121L118 132" stroke="#2563EB" stroke-width="10" stroke-linecap="round" stroke-linejoin="round"/>
|
|
12
|
+
<circle cx="55" cy="43" r="12" fill="#14B8A6"/>
|
|
13
|
+
<circle cx="121" cy="43" r="12" fill="#2563EB"/>
|
|
14
|
+
<circle cx="86" cy="136" r="12" fill="#111827"/>
|
|
15
|
+
<path d="M66.5 47.5L109.5 47.5" stroke="#94A3B8" stroke-width="7" stroke-linecap="round"/>
|
|
16
|
+
<path d="M62 54L79 125" stroke="#94A3B8" stroke-width="7" stroke-linecap="round"/>
|
|
17
|
+
<path d="M115 54L92 125" stroke="#94A3B8" stroke-width="7" stroke-linecap="round"/>
|
|
18
|
+
</g>
|
|
19
|
+
<g transform="translate(244 68)">
|
|
20
|
+
<text x="0" y="62" fill="#111827" font-family="Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif" font-size="72" font-weight="760">Brainlink</text>
|
|
21
|
+
<text x="4" y="102" fill="#475569" font-family="Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif" font-size="24" font-weight="500">Local-first memory for AI agents</text>
|
|
22
|
+
<path d="M0 124H268" stroke="#14B8A6" stroke-width="8" stroke-linecap="round"/>
|
|
23
|
+
<path d="M292 124H392" stroke="#2563EB" stroke-width="8" stroke-linecap="round"/>
|
|
24
|
+
</g>
|
|
25
|
+
</svg>
|
|
@@ -83,6 +83,8 @@ const visibleEdges = () => {
|
|
|
83
83
|
return state.edges.filter(edge => ids.has(edge.source) && edge.target && ids.has(edge.target))
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
const edgeWeight = edge => Number.isFinite(edge.weight) ? Math.max(1, edge.weight) : 1
|
|
87
|
+
|
|
86
88
|
const resetView = () => {
|
|
87
89
|
const rect = canvas.getBoundingClientRect()
|
|
88
90
|
state.transform = { x: Math.max(rect.width, 320) / 2, y: Math.max(rect.height, 320) / 2, scale: 1 }
|
|
@@ -101,9 +103,20 @@ const createLayout = graph => {
|
|
|
101
103
|
return { nodes, edges }
|
|
102
104
|
}
|
|
103
105
|
|
|
106
|
+
const encodeEntityTag = (value) => {
|
|
107
|
+
const utf8 = new TextEncoder().encode(value)
|
|
108
|
+
let binary = ''
|
|
109
|
+
|
|
110
|
+
for (let index = 0; index < utf8.length; index += 1) {
|
|
111
|
+
binary += String.fromCharCode(utf8[index])
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '')
|
|
115
|
+
}
|
|
116
|
+
|
|
104
117
|
const graphSignature = graph => JSON.stringify({
|
|
105
118
|
nodes: graph.nodes.map(node => [node.id, node.title, node.path, node.content, node.tags]),
|
|
106
|
-
edges: graph.edges.map(edge => [edge.source, edge.target, edge.targetTitle])
|
|
119
|
+
edges: graph.edges.map(edge => [edge.source, edge.target, edge.targetTitle, edge.weight, edge.priority])
|
|
107
120
|
})
|
|
108
121
|
|
|
109
122
|
const tick = delta => {
|
|
@@ -207,7 +220,7 @@ const render = now => {
|
|
|
207
220
|
ctx.moveTo(edge.sourceNode.x, edge.sourceNode.y)
|
|
208
221
|
ctx.lineTo(edge.targetNode.x, edge.targetNode.y)
|
|
209
222
|
ctx.strokeStyle = selectedEdge ? graphTheme.edgeActive : graphTheme.edge
|
|
210
|
-
ctx.lineWidth = selectedEdge ? 1.8 : 1
|
|
223
|
+
ctx.lineWidth = (selectedEdge ? 1.8 : 1) + Math.min(edgeWeight(edge) - 1, 8) * 0.22
|
|
211
224
|
ctx.stroke()
|
|
212
225
|
})
|
|
213
226
|
|
|
@@ -241,7 +254,7 @@ const render = now => {
|
|
|
241
254
|
}
|
|
242
255
|
|
|
243
256
|
const list = items => items.length
|
|
244
|
-
? items.map(item => '<li>' + (item.id ? '<button type="button" data-node-id="' + escapeHtml(item.id) + '">' + escapeHtml(item.title) + '</button>' : escapeHtml(item.title)) + '<small>' + escapeHtml(item.path) + '</small></li>').join('')
|
|
257
|
+
? items.map(item => '<li>' + (item.id ? '<button type="button" data-node-id="' + escapeHtml(item.id) + '">' + escapeHtml(item.title) + '</button>' : escapeHtml(item.title)) + '<small>' + escapeHtml(item.path) + (item.weight ? ' · weight ' + escapeHtml(item.weight) + ' · ' + escapeHtml(item.priority || 'normal') : '') + '</small></li>').join('')
|
|
245
258
|
: '<li><small>No links found.</small></li>'
|
|
246
259
|
|
|
247
260
|
const allNotesList = () => state.nodes.length
|
|
@@ -261,13 +274,18 @@ const selectNode = node => {
|
|
|
261
274
|
return
|
|
262
275
|
}
|
|
263
276
|
const nodeById = new Map(state.nodes.map(item => [item.id, item]))
|
|
277
|
+
const withEdgeMeta = (linkedNode, edge) => linkedNode ? {
|
|
278
|
+
...linkedNode,
|
|
279
|
+
weight: edge.weight,
|
|
280
|
+
priority: edge.priority
|
|
281
|
+
} : null
|
|
264
282
|
const outgoing = state.graph.edges
|
|
265
283
|
.filter(edge => edge.source === node.id)
|
|
266
|
-
.map(edge => edge.target ? nodeById.get(edge.target) : { title: edge.targetTitle + ' (unresolved)', path: 'Missing note' })
|
|
284
|
+
.map(edge => withEdgeMeta(edge.target ? nodeById.get(edge.target) : { title: edge.targetTitle + ' (unresolved)', path: 'Missing note' }, edge))
|
|
267
285
|
.filter(Boolean)
|
|
268
286
|
const incoming = state.graph.edges
|
|
269
287
|
.filter(edge => edge.target === node.id)
|
|
270
|
-
.map(edge => nodeById.get(edge.source))
|
|
288
|
+
.map(edge => withEdgeMeta(nodeById.get(edge.source), edge))
|
|
271
289
|
.filter(Boolean)
|
|
272
290
|
|
|
273
291
|
elements.title.textContent = node.title
|
|
@@ -377,9 +395,21 @@ const loadAgents = async () => {
|
|
|
377
395
|
}
|
|
378
396
|
|
|
379
397
|
const loadGraph = async (options = { reset: false }) => {
|
|
380
|
-
const response = await fetch('/api/graph-layout' + agentQuery()
|
|
381
|
-
|
|
382
|
-
|
|
398
|
+
const response = await fetch('/api/graph-layout' + agentQuery(), {
|
|
399
|
+
headers: state.graphSignature
|
|
400
|
+
? {
|
|
401
|
+
'if-none-match': encodeEntityTag(state.graphSignature)
|
|
402
|
+
}
|
|
403
|
+
: undefined
|
|
404
|
+
})
|
|
405
|
+
|
|
406
|
+
if (response.status === 304) {
|
|
407
|
+
return
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const payload = await response.json()
|
|
411
|
+
const graph = payload?.layout ?? payload
|
|
412
|
+
const signature = payload?.signature ?? graphSignature(graph)
|
|
383
413
|
if (!options.reset && signature === state.graphSignature) return
|
|
384
414
|
const selectedId = state.selected?.id
|
|
385
415
|
const layout = createLayout(graph)
|
|
@@ -402,15 +432,47 @@ requestAnimationFrame(() => {
|
|
|
402
432
|
resize()
|
|
403
433
|
resetView()
|
|
404
434
|
})
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
435
|
+
|
|
436
|
+
const pollIntervalMs = 5000
|
|
437
|
+
let tickCounter = 0
|
|
438
|
+
|
|
439
|
+
const refreshGraphLoop = () => {
|
|
440
|
+
if (document.hidden) {
|
|
441
|
+
return
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
loadGraph().catch((error) => {
|
|
445
|
+
elements.stats.textContent = 'Failed to refresh graph'
|
|
446
|
+
console.error(error)
|
|
447
|
+
})
|
|
448
|
+
|
|
449
|
+
tickCounter += 1
|
|
450
|
+
if (tickCounter % 3 === 0) {
|
|
451
|
+
loadAgents().catch((error) => {
|
|
410
452
|
console.error(error)
|
|
411
453
|
})
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
})
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
loadAgents()
|
|
458
|
+
.then(() => loadGraph({ reset: true }))
|
|
459
|
+
.then(() => {
|
|
460
|
+
requestAnimationFrame(render)
|
|
461
|
+
setInterval(refreshGraphLoop, pollIntervalMs)
|
|
462
|
+
})
|
|
463
|
+
.catch(error => {
|
|
464
|
+
elements.stats.textContent = 'Failed to load graph'
|
|
465
|
+
console.error(error)
|
|
466
|
+
})
|
|
467
|
+
|
|
468
|
+
document.addEventListener('visibilitychange', () => {
|
|
469
|
+
if (document.hidden) {
|
|
470
|
+
return
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
loadGraph({ reset: true }).catch(error => {
|
|
474
|
+
elements.stats.textContent = 'Failed to refresh graph'
|
|
475
|
+
console.error(error)
|
|
476
|
+
})
|
|
477
|
+
})
|
|
478
|
+
`;
|
|
@@ -1,3 +1,28 @@
|
|
|
1
1
|
import { createCauliflowerGraphLayout } from '../domain/graph-layout.js';
|
|
2
2
|
import { getGraph } from './get-graph.js';
|
|
3
|
-
|
|
3
|
+
const graphLayoutCache = new Map();
|
|
4
|
+
const createGraphSignature = (graph) => {
|
|
5
|
+
const nodesSignature = graph.nodes.map((node) => `${node.id}|${node.agentId}|${node.title}|${node.path}`).join('\n');
|
|
6
|
+
const edgesSignature = graph.edges
|
|
7
|
+
.map((edge) => `${edge.source}|${edge.target ?? ''}|${edge.targetTitle}|${edge.weight}|${edge.priority}`)
|
|
8
|
+
.join('\n');
|
|
9
|
+
return `${graph.nodes.length}:${nodesSignature}|${graph.edges.length}:${edgesSignature}`;
|
|
10
|
+
};
|
|
11
|
+
export const getGraphLayout = async (vaultPath, agentId) => {
|
|
12
|
+
const graph = await getGraph(vaultPath, agentId);
|
|
13
|
+
const signature = createGraphSignature(graph);
|
|
14
|
+
const cacheKey = `${vaultPath}:${agentId ?? ''}`;
|
|
15
|
+
const cached = graphLayoutCache.get(cacheKey);
|
|
16
|
+
if (cached?.signature === signature) {
|
|
17
|
+
return {
|
|
18
|
+
signature,
|
|
19
|
+
layout: cached.layout
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const layout = createCauliflowerGraphLayout(graph);
|
|
23
|
+
graphLayoutCache.set(cacheKey, { signature, layout });
|
|
24
|
+
return {
|
|
25
|
+
signature,
|
|
26
|
+
layout
|
|
27
|
+
};
|
|
28
|
+
};
|
|
@@ -6,10 +6,18 @@ import { ensureVault, readMarkdownFiles } from '../infrastructure/file-system-va
|
|
|
6
6
|
import { openSqliteIndex } from '../infrastructure/sqlite-index.js';
|
|
7
7
|
const toTitleKey = (title) => title.toLowerCase();
|
|
8
8
|
const appendTitleEntry = (map, document) => {
|
|
9
|
-
|
|
9
|
+
const key = toTitleKey(document.title);
|
|
10
|
+
if (!map.has(key)) {
|
|
11
|
+
map.set(key, {
|
|
12
|
+
id: document.id,
|
|
13
|
+
path: document.path
|
|
14
|
+
});
|
|
15
|
+
}
|
|
10
16
|
return map;
|
|
11
17
|
};
|
|
12
|
-
const createTitleMaps = (documents) => documents
|
|
18
|
+
const createTitleMaps = (documents) => [...documents]
|
|
19
|
+
.sort((left, right) => left.path.localeCompare(right.path))
|
|
20
|
+
.reduce((state, document) => {
|
|
13
21
|
const agentMap = state.byAgent.get(document.agentId) ?? new Map();
|
|
14
22
|
appendTitleEntry(agentMap, document);
|
|
15
23
|
state.byAgent.set(document.agentId, agentMap);
|
|
@@ -22,7 +30,7 @@ const createTitleMaps = (documents) => documents.reduce((state, document) => {
|
|
|
22
30
|
byAgent: new Map()
|
|
23
31
|
});
|
|
24
32
|
const createScopedTitleResolver = (document, titleMaps) => ({
|
|
25
|
-
get: (title) => titleMaps.byAgent.get(document.agentId)?.get(title) ?? titleMaps.shared.get(title)
|
|
33
|
+
get: (title) => titleMaps.byAgent.get(document.agentId)?.get(title)?.id ?? titleMaps.shared.get(title)?.id
|
|
26
34
|
});
|
|
27
35
|
const embedIndexedDocuments = async (documents, providerName) => {
|
|
28
36
|
const provider = createEmbeddingProvider(providerName);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export const isLoopbackHost = (host) => host === 'localhost' || host === '::1' || host === '[::1]' || host.startsWith('127.');
|
|
2
|
-
export const
|
|
3
|
-
if (!
|
|
4
|
-
throw new Error(`Refusing to bind Brainlink server to non-loopback host ${host}.
|
|
2
|
+
export const assertLoopbackHost = (host) => {
|
|
3
|
+
if (!isLoopbackHost(host)) {
|
|
4
|
+
throw new Error(`Refusing to bind Brainlink server to non-loopback host ${host}. Brainlink HTTP only runs on localhost.`);
|
|
5
5
|
}
|
|
6
6
|
};
|
|
@@ -26,6 +26,28 @@ const createResponse = (body, statusCode = 200, contentType = 'text/plain; chars
|
|
|
26
26
|
'cache-control': 'no-store'
|
|
27
27
|
}
|
|
28
28
|
});
|
|
29
|
+
const normalizeHeaderToken = (value) => value?.trim().replace(/^"|"$/g, '') ?? '';
|
|
30
|
+
const decodeEntityTag = (candidate) => {
|
|
31
|
+
const token = normalizeHeaderToken(candidate);
|
|
32
|
+
if (!/^[A-Za-z0-9_-]+$/.test(token))
|
|
33
|
+
return token;
|
|
34
|
+
try {
|
|
35
|
+
return Buffer.from(token, 'base64url').toString('utf8');
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return token;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const encodeEntityTag = (signature) => JSON.stringify(Buffer.from(signature, 'utf8').toString('base64url'));
|
|
42
|
+
const sameEntityTag = (candidate, signature) => {
|
|
43
|
+
if (Array.isArray(candidate)) {
|
|
44
|
+
return candidate.some((value) => sameEntityTag(value, signature));
|
|
45
|
+
}
|
|
46
|
+
if (candidate === undefined) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return decodeEntityTag(candidate) === signature;
|
|
50
|
+
};
|
|
29
51
|
const readAgentQuery = (url) => url.searchParams.get('agent') ?? undefined;
|
|
30
52
|
export const route = async (request, url, vaultPath) => {
|
|
31
53
|
if (isReadMethod(request) && (url.pathname === '/' || url.pathname === '/index.html')) {
|
|
@@ -41,7 +63,29 @@ export const route = async (request, url, vaultPath) => {
|
|
|
41
63
|
return createResponse(createJsonResponse(await getGraph(vaultPath, readAgentQuery(url))), 200, contentTypes['.json']);
|
|
42
64
|
}
|
|
43
65
|
if (isReadMethod(request) && url.pathname === '/api/graph-layout') {
|
|
44
|
-
|
|
66
|
+
const { signature, layout } = await getGraphLayout(vaultPath, readAgentQuery(url));
|
|
67
|
+
const requestEtags = request.headers['if-none-match'];
|
|
68
|
+
const notModified = sameEntityTag(requestEtags, signature);
|
|
69
|
+
const etag = encodeEntityTag(signature);
|
|
70
|
+
const body = createJsonResponse({ signature, layout });
|
|
71
|
+
const jsonResponse = createResponse(body, 200, contentTypes['.json']);
|
|
72
|
+
const notModifiedResponse = createResponse('', 304, contentTypes['.json']);
|
|
73
|
+
if (notModified) {
|
|
74
|
+
return {
|
|
75
|
+
...notModifiedResponse,
|
|
76
|
+
headers: {
|
|
77
|
+
...notModifiedResponse.headers,
|
|
78
|
+
etag
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
...jsonResponse,
|
|
84
|
+
headers: {
|
|
85
|
+
...jsonResponse.headers,
|
|
86
|
+
etag
|
|
87
|
+
}
|
|
88
|
+
};
|
|
45
89
|
}
|
|
46
90
|
if (isReadMethod(request) && url.pathname === '/api/agents') {
|
|
47
91
|
return createResponse(createJsonResponse({ agents: await listAgents(vaultPath) }), 200, contentTypes['.json']);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { createServer } from 'node:http';
|
|
2
2
|
import { indexVault } from './index-vault.js';
|
|
3
3
|
import { startVaultWatcher } from './watch-vault.js';
|
|
4
|
-
import {
|
|
4
|
+
import { assertLoopbackHost } from './server/host-security.js';
|
|
5
5
|
import { contentTypes, createJsonResponse, isHttpError } from './server/http.js';
|
|
6
6
|
import { route } from './server/routes.js';
|
|
7
7
|
export const startServer = async (input) => {
|
|
8
|
-
|
|
8
|
+
assertLoopbackHost(input.host);
|
|
9
9
|
if (input.shouldIndex) {
|
|
10
10
|
await indexVault(input.vaultPath);
|
|
11
11
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { watch } from 'node:fs';
|
|
2
2
|
import { indexVault } from './index-vault.js';
|
|
3
|
-
import { resolveVaultPath } from '../infrastructure/file-system-vault.js';
|
|
3
|
+
import { isBucketVaultPath, resolveVaultPath } from '../infrastructure/file-system-vault.js';
|
|
4
4
|
const shouldIgnore = (filename) => {
|
|
5
5
|
if (!filename) {
|
|
6
6
|
return false;
|
|
@@ -8,6 +8,9 @@ const shouldIgnore = (filename) => {
|
|
|
8
8
|
return filename.includes('.brainlink') || !filename.endsWith('.md');
|
|
9
9
|
};
|
|
10
10
|
export const startVaultWatcher = (input) => {
|
|
11
|
+
if (isBucketVaultPath(input.vaultPath)) {
|
|
12
|
+
throw new Error('Watch mode is only supported for local filesystem vaults.');
|
|
13
|
+
}
|
|
11
14
|
const absoluteVaultPath = resolveVaultPath(input.vaultPath);
|
|
12
15
|
const debounceMs = input.debounceMs ?? 350;
|
|
13
16
|
let timeout = null;
|