@andespindola/brainlink 0.1.0-alpha.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.
Files changed (52) hide show
  1. package/AGENTS.md +142 -0
  2. package/CHANGELOG.md +13 -0
  3. package/CONTRIBUTING.md +28 -0
  4. package/LICENSE +23 -0
  5. package/README.md +715 -0
  6. package/SECURITY.md +35 -0
  7. package/dist/application/add-note.js +30 -0
  8. package/dist/application/analyze-vault.js +28 -0
  9. package/dist/application/build-context.js +15 -0
  10. package/dist/application/frontend/client-css.js +294 -0
  11. package/dist/application/frontend/client-html.js +66 -0
  12. package/dist/application/frontend/client-js.js +416 -0
  13. package/dist/application/get-graph-layout.js +3 -0
  14. package/dist/application/get-graph.js +12 -0
  15. package/dist/application/index-vault.js +67 -0
  16. package/dist/application/list-agents.js +12 -0
  17. package/dist/application/list-links.js +22 -0
  18. package/dist/application/search-knowledge.js +19 -0
  19. package/dist/application/server/host-security.js +6 -0
  20. package/dist/application/server/http.js +13 -0
  21. package/dist/application/server/routes.js +88 -0
  22. package/dist/application/server/types.js +1 -0
  23. package/dist/application/start-server.js +54 -0
  24. package/dist/application/watch-vault.js +36 -0
  25. package/dist/benchmarks/large-vault.js +88 -0
  26. package/dist/cli/commands/read-commands.js +149 -0
  27. package/dist/cli/commands/write-commands.js +107 -0
  28. package/dist/cli/main.js +21 -0
  29. package/dist/cli/runtime.js +18 -0
  30. package/dist/cli/types.js +1 -0
  31. package/dist/domain/agents.js +11 -0
  32. package/dist/domain/context.js +44 -0
  33. package/dist/domain/embeddings.js +117 -0
  34. package/dist/domain/graph-analysis.js +48 -0
  35. package/dist/domain/graph-layout.js +187 -0
  36. package/dist/domain/ids.js +2 -0
  37. package/dist/domain/markdown.js +100 -0
  38. package/dist/domain/note-safety.js +54 -0
  39. package/dist/domain/tokens.js +1 -0
  40. package/dist/domain/types.js +1 -0
  41. package/dist/infrastructure/config.js +60 -0
  42. package/dist/infrastructure/file-system-vault.js +62 -0
  43. package/dist/infrastructure/sqlite/document-writer.js +50 -0
  44. package/dist/infrastructure/sqlite/graph-reader.js +108 -0
  45. package/dist/infrastructure/sqlite/schema.js +87 -0
  46. package/dist/infrastructure/sqlite/search-reader.js +156 -0
  47. package/dist/infrastructure/sqlite/types.js +1 -0
  48. package/dist/infrastructure/sqlite-index.js +20 -0
  49. package/docs/AGENT_USAGE.md +477 -0
  50. package/docs/ARCHITECTURE.md +286 -0
  51. package/docs/RELEASE.md +67 -0
  52. package/package.json +67 -0
@@ -0,0 +1,477 @@
1
+ # Agent Usage
2
+
3
+ Brainlink is designed to be used by agents as an external memory and retrieval layer.
4
+
5
+ It is not a replacement for the model context window. It is a way to retrieve the most relevant historical knowledge before the model answers.
6
+
7
+ ## Mental Model
8
+
9
+ ```txt
10
+ Markdown vault durable knowledge
11
+ Brainlink index rebuildable retrieval cache
12
+ context command compact memory package for agents
13
+ ```
14
+
15
+ The correct dependency direction is:
16
+
17
+ ```txt
18
+ agent -> Brainlink CLI -> Markdown vault + derived index
19
+ ```
20
+
21
+ Agents should never depend on the internal SQLite schema as a public API.
22
+
23
+ The installed CLI exposes two equivalent binaries:
24
+
25
+ ```bash
26
+ brainlink --help
27
+ blink --help
28
+ ```
29
+
30
+ Use `blink` as the short terminal alias and `brainlink` in documentation when explicit naming is more important.
31
+
32
+ ## Agent Namespaces
33
+
34
+ Each agent writes into a dedicated namespace under `agents/<agent-id>/`:
35
+
36
+ ```txt
37
+ vault/
38
+ agents/
39
+ shared/
40
+ project-decisions.md
41
+ coding-agent/
42
+ implementation-policy.md
43
+ research-agent/
44
+ source-review-policy.md
45
+ ```
46
+
47
+ Rules:
48
+
49
+ - Use `shared` only for knowledge that should be visible to every workflow.
50
+ - Use a stable `--agent <agent-id>` for private agent memory.
51
+ - Do not write one agent's private context into another agent's folder.
52
+ - `[[links]]` resolve first inside the current agent namespace, then in `shared`.
53
+ - Run `agents` to inspect available namespaces.
54
+
55
+ ## When To Use Brainlink
56
+
57
+ Use Brainlink when a task needs memory about:
58
+
59
+ - prior decisions
60
+ - project conventions
61
+ - architecture notes
62
+ - long-running conversations
63
+ - operational runbooks
64
+ - user preferences
65
+ - domain concepts
66
+ - implementation rationale
67
+
68
+ Do not use Brainlink as a dumping ground for every transient message. Store durable knowledge only.
69
+
70
+ ## Write Policy
71
+
72
+ Good memory is curated.
73
+
74
+ Before adding a note, an agent should ask:
75
+
76
+ - Is this knowledge likely to be useful later?
77
+ - Does it have a clear title?
78
+ - Can it be linked to an existing concept?
79
+ - Does it need tags?
80
+ - Is it a fact, decision, preference, runbook, or open question?
81
+
82
+ Recommended note categories:
83
+
84
+ - `Decision`
85
+ - `Architecture`
86
+ - `Runbook`
87
+ - `Concept`
88
+ - `Preference`
89
+ - `Conversation Summary`
90
+ - `Open Question`
91
+
92
+ ## Note Format
93
+
94
+ Preferred note:
95
+
96
+ ```md
97
+ ---
98
+ title: "Auth Decision"
99
+ type: "Decision"
100
+ ---
101
+
102
+ # Auth Decision
103
+
104
+ We chose JWT for API clients.
105
+
106
+ Reason:
107
+
108
+ - Stateless API authentication.
109
+ - Simple integration with external clients.
110
+ - Existing infrastructure supports token validation.
111
+
112
+ Related:
113
+
114
+ - [[Architecture]]
115
+ - [[API Gateway]]
116
+
117
+ #auth #jwt #decision
118
+ ```
119
+
120
+ Rules:
121
+
122
+ - Use a clear title.
123
+ - Use `[[Note Title]]` for relationships.
124
+ - Use tags for retrieval.
125
+ - Keep each note focused.
126
+ - Prefer summaries over raw transcripts.
127
+ - Preserve dates when the timing matters.
128
+
129
+ ## Read Policy
130
+
131
+ Before answering a memory-dependent question, run:
132
+
133
+ ```bash
134
+ blink context "<question>" --vault ./vault --agent coding-agent
135
+ ```
136
+
137
+ Use the returned context as source-grounded memory.
138
+
139
+ For machine-readable output, use:
140
+
141
+ ```bash
142
+ blink context "<question>" --vault ./vault --agent coding-agent --json
143
+ ```
144
+
145
+ If the context is empty or weak:
146
+
147
+ 1. Try a more explicit search query.
148
+ 2. Run `search` to inspect raw matches.
149
+ 3. Inspect links and backlinks.
150
+ 4. Only then answer from general reasoning.
151
+
152
+ ## Examples For Common Coding Agents
153
+
154
+ These examples assume the agent can run shell commands in the user workspace.
155
+
156
+ ### Codex-Style Terminal Agent
157
+
158
+ Run this at the start of a task:
159
+
160
+ ```bash
161
+ export BLINK_VAULT=".brainlink-vault"
162
+ export BLINK_AGENT="codex"
163
+ blink init "$BLINK_VAULT"
164
+ blink context "$USER_TASK" --vault "$BLINK_VAULT" --agent "$BLINK_AGENT" --mode hybrid --json
165
+ ```
166
+
167
+ After discovering durable project knowledge:
168
+
169
+ ```bash
170
+ blink add "Implementation Boundary" \
171
+ --vault "$BLINK_VAULT" \
172
+ --agent "$BLINK_AGENT" \
173
+ --content "Keep use cases in application and pure transformations in domain. [[Architecture]] #architecture #typescript"
174
+ blink index --vault "$BLINK_VAULT"
175
+ ```
176
+
177
+ ### Claude Code-Style Agent
178
+
179
+ Use Brainlink as a preflight memory read before editing files:
180
+
181
+ ```bash
182
+ blink context "task: $USER_TASK repo: $(basename "$PWD")" \
183
+ --vault .brainlink-vault \
184
+ --agent claude-code \
185
+ --tokens 2500 \
186
+ --json
187
+ ```
188
+
189
+ Store only stable outcomes:
190
+
191
+ ```bash
192
+ blink add "Test Command" \
193
+ --vault .brainlink-vault \
194
+ --agent claude-code \
195
+ --content "For this repository, run npm run check before final delivery. #testing #process"
196
+ blink index --vault .brainlink-vault
197
+ ```
198
+
199
+ ### Cursor Or IDE Agent
200
+
201
+ Use a project-local vault and a namespace per assistant profile:
202
+
203
+ ```bash
204
+ blink search "frontend graph layout conventions" \
205
+ --vault .brainlink-vault \
206
+ --agent ide-agent \
207
+ --mode hybrid \
208
+ --limit 8 \
209
+ --json
210
+ ```
211
+
212
+ When the IDE agent changes architecture, persist the rationale:
213
+
214
+ ```bash
215
+ blink add "Frontend Asset Boundary" \
216
+ --vault .brainlink-vault \
217
+ --agent ide-agent \
218
+ --content "Frontend graph assets live outside server routing modules so UI changes do not affect HTTP bootstrap. #frontend #architecture"
219
+ blink index --vault .brainlink-vault
220
+ ```
221
+
222
+ ## Command Reference
223
+
224
+ ### Initialize A Vault
225
+
226
+ ```bash
227
+ blink init ./vault
228
+ ```
229
+
230
+ Creates:
231
+
232
+ ```txt
233
+ vault/
234
+ .brainlink/
235
+ ```
236
+
237
+ ### Add A Note
238
+
239
+ ```bash
240
+ blink add "Note Title" --vault ./vault --content "Markdown content"
241
+ ```
242
+
243
+ This creates a slugged Markdown file with frontmatter and a heading.
244
+
245
+ The CLI blocks common secret patterns by default. Do not use `--allow-sensitive` unless the vault is intentionally protected.
246
+
247
+ For agent-private memory:
248
+
249
+ ```bash
250
+ blink add "Implementation Policy" \
251
+ --vault ./vault \
252
+ --agent coding-agent \
253
+ --content "Prefer functional TypeScript modules. [[Architecture]] #typescript"
254
+ ```
255
+
256
+ This writes to:
257
+
258
+ ```txt
259
+ vault/agents/coding-agent/implementation-policy.md
260
+ ```
261
+
262
+ ### Rebuild The Index
263
+
264
+ ```bash
265
+ blink index --vault ./vault
266
+ ```
267
+
268
+ This scans Markdown files and rebuilds:
269
+
270
+ - documents
271
+ - chunks
272
+ - links
273
+ - full-text search records
274
+
275
+ ### Search Knowledge
276
+
277
+ ```bash
278
+ blink search "jwt auth" --vault ./vault --limit 10
279
+ blink search "jwt auth" --vault ./vault --json
280
+ blink search "jwt auth" --vault ./vault --agent coding-agent --json
281
+ blink search "authentication token policy" --vault ./vault --mode semantic --json
282
+ ```
283
+
284
+ This returns matching chunks with title, source path, score, `textScore`, `semanticScore`, `searchMode`, and content.
285
+
286
+ Search modes:
287
+
288
+ - `hybrid`: default; combines SQLite FTS and local embedding similarity.
289
+ - `fts`: lexical SQLite full-text search only.
290
+ - `semantic`: local deterministic embedding similarity with SQLite bucket candidate narrowing.
291
+
292
+ ### Build Agent Context
293
+
294
+ ```bash
295
+ blink context "how does authentication work?" --vault ./vault --limit 12 --tokens 2000
296
+ blink context "how does authentication work?" --vault ./vault --json
297
+ blink context "how does authentication work?" --vault ./vault --agent coding-agent --json
298
+ blink context "how does authentication work?" --vault ./vault --agent coding-agent --mode hybrid --json
299
+ ```
300
+
301
+ This returns a Markdown context package optimized for prompt injection.
302
+
303
+ ### Inspect Links
304
+
305
+ ```bash
306
+ blink links --vault ./vault
307
+ blink links --vault ./vault --agent coding-agent
308
+ ```
309
+
310
+ Example output:
311
+
312
+ ```txt
313
+ Auth Decision (auth-decision.md) -> Architecture (architecture.md)
314
+ ```
315
+
316
+ ### Inspect Backlinks
317
+
318
+ ```bash
319
+ blink backlinks "Architecture" --vault ./vault
320
+ blink backlinks "Architecture" --vault ./vault --agent coding-agent
321
+ ```
322
+
323
+ Example output:
324
+
325
+ ```txt
326
+ Auth Decision (auth-decision.md) -> Architecture
327
+ ```
328
+
329
+ ### List Agents
330
+
331
+ ```bash
332
+ blink agents --vault ./vault
333
+ blink agents --vault ./vault --json
334
+ ```
335
+
336
+ Example output:
337
+
338
+ ```txt
339
+ coding-agent: 12 documents
340
+ research-agent: 7 documents
341
+ shared: 30 documents
342
+ ```
343
+
344
+ ### Start Graph UI
345
+
346
+ ```bash
347
+ blink server --vault ./vault --host 127.0.0.1 --port 4321
348
+ ```
349
+
350
+ This starts a local frontend for inspecting the knowledge graph.
351
+
352
+ The frontend includes an agent selector. Selecting an agent calls the same read APIs with `agent=<agent-id>` and renders that namespace instead of merging every agent into one graph.
353
+
354
+ The command reindexes by default, then serves:
355
+
356
+ ```txt
357
+ http://127.0.0.1:4321/
358
+ http://127.0.0.1:4321/api/graph
359
+ http://127.0.0.1:4321/api/graph?agent=coding-agent
360
+ http://127.0.0.1:4321/api/agents
361
+ ```
362
+
363
+ Use `--no-index` when you need to inspect the current index without rebuilding it:
364
+
365
+ ```bash
366
+ blink server --vault ./vault --no-index
367
+ ```
368
+
369
+ Use `--watch` to keep the graph updated after Markdown edits:
370
+
371
+ ```bash
372
+ blink server --vault ./vault --watch
373
+ ```
374
+
375
+ ### Watch A Vault
376
+
377
+ ```bash
378
+ blink watch --vault ./vault
379
+ ```
380
+
381
+ This process watches Markdown files and rebuilds the index after changes.
382
+
383
+ ### Use From An External MCP Server
384
+
385
+ Brainlink does not ship an MCP server. An MCP server can use Brainlink by executing the CLI and parsing `--json`.
386
+
387
+ Recommended wrapper mapping:
388
+
389
+ - `brainlink_context`: run `blink context "<query>" --vault <vault> --agent <agent> --mode hybrid --json`.
390
+ - `brainlink_search`: run `blink search "<query>" --vault <vault> --agent <agent> --mode hybrid --json`.
391
+ - `brainlink_add_note`: run `blink add "<title>" --vault <vault> --agent <agent> --content "<content>" --json`, then `blink index`.
392
+ - `brainlink_graph`: run `blink graph --vault <vault> --agent <agent> --json`.
393
+ - `brainlink_validate`: run `blink validate --vault <vault> --agent <agent> --json`.
394
+
395
+ External wrappers should set `BRAINLINK_ALLOWED_VAULTS` before invoking the CLI:
396
+
397
+ ```bash
398
+ export BRAINLINK_ALLOWED_VAULTS="/absolute/path/to/project-vault"
399
+ ```
400
+
401
+ ### HTTP API
402
+
403
+ ```txt
404
+ GET /api/graph
405
+ GET /api/graph-layout
406
+ GET /api/search?q=<query>&limit=10&mode=hybrid
407
+ GET /api/context?q=<query>&limit=12&tokens=2000&mode=hybrid
408
+ GET /api/links
409
+ GET /api/backlinks?title=<title>
410
+ GET /api/stats
411
+ GET /api/broken-links
412
+ GET /api/orphans
413
+ GET /api/validate
414
+ ```
415
+
416
+ The HTTP API is read-only. Use the CLI for writes and indexing.
417
+
418
+ ## Agent Integration Contract
419
+
420
+ Input:
421
+
422
+ - A Markdown vault path.
423
+ - A user question or task.
424
+ - Optional limits for result count and token budget.
425
+
426
+ Processing:
427
+
428
+ 1. Ensure the vault has been indexed.
429
+ 2. Search relevant chunks.
430
+ 3. Select context sections within token budget.
431
+ 4. Return Markdown with sources.
432
+
433
+ Output:
434
+
435
+ - Markdown context package.
436
+ - Each section includes title, source path, tags, score, and content.
437
+
438
+ Agents should include source paths in their reasoning or final answer when the user needs traceability.
439
+
440
+ ## Operational Rules
441
+
442
+ - Re-run `index` after modifying notes.
443
+ - Treat `.brainlink/brainlink.db` as disposable.
444
+ - Commit Markdown notes, not local database files.
445
+ - Do not manually edit the database.
446
+ - Keep generated context short enough for the target model.
447
+ - Prefer specific queries over broad queries.
448
+
449
+ ## Failure Modes
450
+
451
+ Empty context usually means:
452
+
453
+ - The vault was not indexed.
454
+ - The query terms do not match existing notes.
455
+ - The notes are missing useful tags.
456
+ - The knowledge was never written into the vault.
457
+
458
+ Unresolved links usually mean:
459
+
460
+ - A note title does not match the link text.
461
+ - The target note does not exist yet.
462
+ - The note uses a different first heading than expected.
463
+
464
+ Weak retrieval usually means:
465
+
466
+ - Notes are too large or unfocused.
467
+ - Tags are missing.
468
+ - The query is too generic.
469
+ - The wrong retrieval mode was selected for the question.
470
+
471
+ ## Current Limits
472
+
473
+ - Search supports FTS, local semantic embeddings, SQLite semantic buckets and hybrid ranking.
474
+ - Local embeddings are deterministic and provider-free; remote embedding providers are not implemented yet.
475
+ - MCP integration is external: wrap the CLI from your own MCP server.
476
+ - HTTP API is local and unauthenticated.
477
+ - Watch mode depends on platform filesystem watcher behavior.