nano-brain 2.0.0-beta.1

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/README.md ADDED
@@ -0,0 +1,256 @@
1
+ # nano-brain
2
+
3
+ **Persistent memory and code intelligence for AI coding agents.**
4
+
5
+ [![Go 1.23](https://img.shields.io/badge/Go-1.23-00ADD8?logo=go)](https://go.dev/)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
7
+ [![GitHub](https://img.shields.io/badge/GitHub-nano--step%2Fnano--brain-181717?logo=github)](https://github.com/nano-step/nano-brain)
8
+
9
+ ## What It Does
10
+
11
+ nano-brain is a persistent memory server for AI coding agents that solves session amnesia. It automatically ingests AI sessions, notes, and codebase files, indexes everything with hybrid search (BM25 + pgvector), and serves memories via MCP tools and REST API. Built in Go with PostgreSQL — single static binary, zero CGO dependencies.
12
+
13
+ ## Key Features
14
+
15
+ - **Hybrid search** — BM25 full-text + pgvector HNSW cosine similarity + RRF fusion + recency decay
16
+ - **9 MCP tools** — query, search, vsearch, get, write, tags, status, update, wake_up
17
+ - **Session harvesting** — auto-ingest OpenCode and Claude Code sessions
18
+ - **File watcher** — fsnotify-based directory monitoring with debounce
19
+ - **Content-addressed storage** — SHA-256 deduplication
20
+ - **Heading-aware markdown chunking**
21
+ - **Multi-workspace isolation** with per-workspace data
22
+ - **Config hot-reload** — `POST /api/reload-config`
23
+ - **V1 migration** — import from SQLite (pure Go, no CGO)
24
+ - **Benchmarking suite** — generate, run, compare, stress
25
+ - **Search telemetry** — local-only, 90-day retention, non-blocking
26
+
27
+ ## Prerequisites
28
+
29
+ - **Go 1.23+** (building from source) OR pre-built binary
30
+ - **PostgreSQL 17** with **pgvector 0.8.2** extension
31
+ - **Embedding provider:** Ollama (default, local) or Voyage AI
32
+
33
+ ## Quick Start
34
+
35
+ ```bash
36
+ # Build
37
+ CGO_ENABLED=0 go build -o nano-brain ./cmd/nano-brain
38
+
39
+ # Start PostgreSQL + pgvector (example with Docker)
40
+ docker run -d --name nanobrain-pg -p 5432:5432 \
41
+ -e POSTGRES_USER=nanobrain -e POSTGRES_PASSWORD=nanobrain -e POSTGRES_DB=nanobrain_dev \
42
+ pgvector/pgvector:pg17
43
+
44
+ # Start server
45
+ DATABASE_URL="postgres://nanobrain:nanobrain@localhost:5432/nanobrain_dev" ./nano-brain
46
+
47
+ # Register workspace
48
+ curl -X POST http://localhost:3100/api/v1/init \
49
+ -H "Content-Type: application/json" \
50
+ -d '{"root_path":"/path/to/project","name":"my-project"}'
51
+
52
+ # Write a document
53
+ curl -X POST http://localhost:3100/api/v1/write \
54
+ -H "Content-Type: application/json" \
55
+ -d '{"workspace":"<hash>","source_path":"notes/decision.md","content":"# Decision\nUse PostgreSQL.","tags":["decision"]}'
56
+
57
+ # Search
58
+ curl -X POST http://localhost:3100/api/v1/query \
59
+ -H "Content-Type: application/json" \
60
+ -d '{"workspace":"<hash>","query":"database decision"}'
61
+ ```
62
+
63
+ ## Configuration
64
+
65
+ Config file: `~/.nano-brain/config.yml`
66
+
67
+ ```yaml
68
+ server:
69
+ host: localhost
70
+ port: 3100
71
+
72
+ database:
73
+ url: postgres://nanobrain:nanobrain@localhost:5432/nanobrain_dev
74
+
75
+ embedding:
76
+ provider: ollama # ollama or voyage
77
+ url: http://localhost:11434
78
+ model: nomic-embed-text
79
+ dimension: 0 # auto-detect from provider
80
+ concurrency: 3
81
+
82
+ search:
83
+ rrf_k: 60
84
+ recency_weight: 0.3
85
+ recency_half_life_days: 180
86
+ limit: 20
87
+
88
+ harvester:
89
+ opencode:
90
+ session_dir: "" # e.g., ~/.local/share/opencode/storage
91
+ claudecode:
92
+ enabled: false
93
+ session_dir: ""
94
+
95
+ watcher:
96
+ debounce_ms: 2000
97
+ reindex_interval: 300
98
+
99
+ storage:
100
+ max_file_size: 314572800 # 300MB
101
+ max_size: 10737418240 # 10GB
102
+
103
+ telemetry:
104
+ retention_days: 90
105
+
106
+ logging:
107
+ level: info
108
+ file: "" # empty = stdout only
109
+ ```
110
+
111
+ ### Environment Variables
112
+
113
+ | Variable | Description |
114
+ |----------|-------------|
115
+ | `DATABASE_URL` | PostgreSQL connection string |
116
+ | `VOYAGE_API_KEY` | Voyage AI API key |
117
+ | `OPENCODE_STORAGE_DIR` | OpenCode session directory |
118
+ | `NANO_BRAIN_*` | Override any config (e.g., `NANO_BRAIN_SERVER_PORT=3100`) |
119
+
120
+ ## REST API
121
+
122
+ ### Public Endpoints
123
+
124
+ | Method | Path | Description |
125
+ |--------|------|-------------|
126
+ | GET | `/health` | Health check |
127
+ | GET | `/api/status` | Server status with version, uptime, workspace stats |
128
+ | POST | `/api/v1/init` | Register workspace |
129
+ | GET | `/api/v1/workspaces` | List all workspaces (with doc counts) |
130
+ | GET | `/api/v1/wake-up` | Workspace briefing |
131
+ | POST | `/api/harvest` | Trigger session harvesting |
132
+ | POST | `/api/reload-config` | Hot-reload configuration |
133
+
134
+ ### Workspace-Scoped Endpoints
135
+
136
+ Workspace is passed in the JSON body for POST, query param for GET.
137
+
138
+ | Method | Path | Description |
139
+ |--------|------|-------------|
140
+ | POST | `/api/v1/write` | Write/update document |
141
+ | POST | `/api/v1/embed` | Trigger embedding |
142
+ | POST | `/api/v1/search` | BM25 keyword search |
143
+ | POST | `/api/v1/vsearch` | Vector similarity search |
144
+ | POST | `/api/v1/query` | Hybrid search (BM25 + vector + RRF + recency) |
145
+ | POST | `/api/v1/collections` | Add collection |
146
+ | GET | `/api/v1/collections` | List collections |
147
+ | PUT | `/api/v1/collections/:name` | Rename collection |
148
+ | DELETE | `/api/v1/collections/:name` | Remove collection |
149
+ | GET | `/api/v1/tags` | List tags with counts |
150
+ | POST | `/api/v1/reindex` | Queue reindex (202) |
151
+ | POST | `/api/v1/update` | Queue update (202) |
152
+ | POST | `/api/v1/wake-up` | Workspace briefing with session_dir |
153
+
154
+ ### MCP Endpoints
155
+
156
+ | Method | Path | Description |
157
+ |--------|------|-------------|
158
+ | GET/POST | `/mcp` | Streamable HTTP (MCP 2025-03-26) |
159
+ | GET/POST | `/sse` | SSE transport (legacy) |
160
+
161
+ ## CLI Commands
162
+
163
+ | Command | Description |
164
+ |---------|-------------|
165
+ | `nano-brain` (no args) | Start HTTP server (default: port 3100) |
166
+ | `nano-brain init --root=<path>` | Register workspace |
167
+ | `nano-brain write` | Write document via CLI |
168
+ | `nano-brain query` | Hybrid search |
169
+ | `nano-brain search` | BM25 keyword search |
170
+ | `nano-brain vsearch` | Vector similarity search |
171
+ | `nano-brain collection add\|remove\|list` | Manage collections |
172
+ | `nano-brain harvest` | Trigger session harvesting |
173
+ | `nano-brain bench generate\|run\|compare\|stress` | Benchmarking suite |
174
+ | `nano-brain db:migrate` | Run pending goose migrations |
175
+ | `nano-brain db:migrate --from-v1 <path>` | Import V1 SQLite data |
176
+ | `nano-brain logs [-n 50] [-f]` | Tail log file |
177
+ | `nano-brain docker start\|stop\|status` | Docker compose management |
178
+ | `nano-brain status [--json]` | Server status |
179
+
180
+ ## MCP Tools
181
+
182
+ nano-brain exposes 9 tools via MCP (Model Context Protocol):
183
+
184
+ | Tool | Description |
185
+ |------|-------------|
186
+ | `memory_query` | Hybrid search (BM25 + vector + RRF + recency) |
187
+ | `memory_search` | BM25 keyword search |
188
+ | `memory_vsearch` | Vector similarity search |
189
+ | `memory_get` | Get document by path |
190
+ | `memory_write` | Write/update document |
191
+ | `memory_tags` | List tags with counts |
192
+ | `memory_status` | Server and embedding status |
193
+ | `memory_update` | Trigger re-embedding |
194
+ | `memory_wake_up` | Workspace briefing |
195
+
196
+ ### MCP Configuration
197
+
198
+ ```json
199
+ {
200
+ "mcp": {
201
+ "nano-brain": {
202
+ "type": "remote",
203
+ "url": "http://localhost:3100/mcp"
204
+ }
205
+ }
206
+ }
207
+ ```
208
+
209
+ ## Search Pipeline
210
+
211
+ ```
212
+ Query --> BM25 (ts_rank_cd) ---+
213
+ +--> RRF Fusion (k=60) --> Recency Decay --> Results
214
+ Query --> Vector (HNSW cos) ---+
215
+ ```
216
+
217
+ - **BM25:** `websearch_to_tsquery` + `ts_rank_cd` on PostgreSQL tsvector
218
+ - **Vector:** pgvector HNSW index with cosine distance
219
+ - **RRF:** Reciprocal Rank Fusion (k=60), scores normalized to [0,1]
220
+ - **Recency:** exponential half-life decay (default 180 days, weight 0.3)
221
+
222
+ ## Architecture
223
+
224
+ - 15 internal packages: config, server, handlers, storage, sqlc, embed, search, watcher, harvest, mcp, migrate, telemetry, health, bench
225
+ - 7 goose SQL migrations (embedded)
226
+ - Constructor injection (no DI framework)
227
+ - errgroup + context for goroutine lifecycle
228
+ - Echo v4 middleware: workspace extraction, content-type enforcement, version header
229
+
230
+ ## Migration from V1
231
+
232
+ ```bash
233
+ # Import V1 SQLite data to PostgreSQL
234
+ nano-brain db:migrate --from-v1 /path/to/old/index.db
235
+
236
+ # Idempotent — safe to run multiple times
237
+ # Uses content-addressed SHA-256 hashing
238
+ # Pure Go SQLite reader (modernc.org/sqlite, no CGO)
239
+ ```
240
+
241
+ ## Tech Stack
242
+
243
+ - **Go 1.23** — compiled to single static binary (`CGO_ENABLED=0`)
244
+ - **PostgreSQL 17** — relational storage + full-text search (tsvector/tsquery)
245
+ - **pgvector 0.8.2** — HNSW vector indexing
246
+ - **Echo v4** — HTTP framework
247
+ - **sqlc** — type-safe SQL code generation
248
+ - **goose v3** — database migrations
249
+ - **zerolog** — structured JSON logging
250
+ - **koanf** — YAML + env configuration
251
+ - **fsnotify** — file system watching
252
+ - **modernc.org/sqlite** — V1 migration reader (pure Go)
253
+
254
+ ## License
255
+
256
+ MIT
@@ -0,0 +1,89 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ const https = require("https");
5
+ const fs = require("fs");
6
+ const path = require("path");
7
+ const os = require("os");
8
+ const { execSync } = require("child_process");
9
+
10
+ const VERSION = require("../package.json").version;
11
+ const REPO = "nano-step/nano-brain";
12
+
13
+ const PLATFORM_MAP = {
14
+ darwin: "darwin",
15
+ linux: "linux",
16
+ };
17
+ const ARCH_MAP = {
18
+ arm64: "arm64",
19
+ x64: "amd64",
20
+ };
21
+
22
+ function getPlatformKey() {
23
+ const platform = PLATFORM_MAP[os.platform()];
24
+ const arch = ARCH_MAP[os.arch()];
25
+ if (!platform || !arch) {
26
+ console.error(`Unsupported platform: ${os.platform()}-${os.arch()}`);
27
+ console.error("Build from source: CGO_ENABLED=0 go build -o nano-brain ./cmd/nano-brain");
28
+ process.exit(0);
29
+ }
30
+ return `${platform}-${arch}`;
31
+ }
32
+
33
+ function download(url, dest) {
34
+ return new Promise((resolve, reject) => {
35
+ const file = fs.createWriteStream(dest);
36
+ https.get(url, (res) => {
37
+ if (res.statusCode === 301 || res.statusCode === 302) {
38
+ file.close();
39
+ fs.unlinkSync(dest);
40
+ return download(res.headers.location, dest).then(resolve).catch(reject);
41
+ }
42
+ if (res.statusCode !== 200) {
43
+ file.close();
44
+ fs.unlinkSync(dest);
45
+ return reject(new Error(`Download failed: HTTP ${res.statusCode}`));
46
+ }
47
+ res.pipe(file);
48
+ file.on("finish", () => {
49
+ file.close(resolve);
50
+ });
51
+ }).on("error", (err) => {
52
+ fs.unlinkSync(dest);
53
+ reject(err);
54
+ });
55
+ });
56
+ }
57
+
58
+ async function main() {
59
+ const platformKey = getPlatformKey();
60
+ const binName = os.platform() === "win32" ? "nano-brain.exe" : "nano-brain";
61
+ const binPath = path.join(__dirname, binName);
62
+
63
+ if (fs.existsSync(binPath)) {
64
+ try {
65
+ const output = execSync(`"${binPath}" status --json`, { timeout: 5000 }).toString();
66
+ if (output.includes(VERSION)) {
67
+ console.log(`nano-brain v${VERSION} already installed.`);
68
+ return;
69
+ }
70
+ } catch {
71
+ // Wrong version or can't run — redownload
72
+ }
73
+ }
74
+
75
+ const url = `https://github.com/${REPO}/releases/download/v${VERSION}/nano-brain-${platformKey}`;
76
+ console.log(`Downloading nano-brain v${VERSION} for ${platformKey}...`);
77
+
78
+ try {
79
+ await download(url, binPath);
80
+ fs.chmodSync(binPath, 0o755);
81
+ console.log(`nano-brain v${VERSION} installed successfully.`);
82
+ } catch (err) {
83
+ console.error(`Failed to download binary: ${err.message}`);
84
+ console.error("Build from source: CGO_ENABLED=0 go build -o npm/nano-brain ./cmd/nano-brain");
85
+ process.exit(0);
86
+ }
87
+ }
88
+
89
+ main();
package/npm/run.js ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ const { execFileSync } = require("child_process");
5
+ const path = require("path");
6
+ const fs = require("fs");
7
+ const os = require("os");
8
+
9
+ const binName = os.platform() === "win32" ? "nano-brain.exe" : "nano-brain";
10
+ const binPath = path.join(__dirname, binName);
11
+
12
+ if (!fs.existsSync(binPath)) {
13
+ console.error("nano-brain binary not found. Run: npx nano-brain (it will download automatically)");
14
+ console.error("Or build from source: CGO_ENABLED=0 go build -o npm/nano-brain ./cmd/nano-brain");
15
+ process.exit(1);
16
+ }
17
+
18
+ try {
19
+ execFileSync(binPath, process.argv.slice(2), { stdio: "inherit" });
20
+ } catch (e) {
21
+ process.exit(e.status || 1);
22
+ }
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "nano-brain",
3
+ "version": "2.0.0-beta.1",
4
+ "description": "Persistent memory and code intelligence for AI coding agents",
5
+ "bin": {
6
+ "nano-brain": "npm/run.js"
7
+ },
8
+ "scripts": {
9
+ "postinstall": "node npm/postinstall.js"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/nano-step/nano-brain.git"
14
+ },
15
+ "keywords": [
16
+ "ai",
17
+ "memory",
18
+ "mcp",
19
+ "coding-agent",
20
+ "search",
21
+ "pgvector"
22
+ ],
23
+ "author": "nano-step",
24
+ "license": "MIT",
25
+ "files": [
26
+ "npm/",
27
+ "README.md",
28
+ "LICENSE"
29
+ ]
30
+ }