mixgram 1.1.2 → 1.1.6
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 +14 -16
- package/bin/mixgram.js +18 -11
- package/package.json +1 -1
- package/spec/run-tests.js +4 -4
- package/spec/scenarios/scenarios.js +1 -1
- package/src/config.js +1 -1
- package/src/core/embeddings/worker-process.js +79 -0
- package/src/core/search/search.js +28 -8
- package/src/mcp/server.js +67 -2
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ MCP memory server compatible with [Engram](https://github.com/gentleman-programm
|
|
|
9
9
|
### Advantages over Engram
|
|
10
10
|
|
|
11
11
|
- **Visible, editable docs** — As you advance with agentic development you see the actual documentation files on disk. You can open them, read them, and edit them manually; the agent’s memory is not hidden in a black box.
|
|
12
|
-
- **Versioned** — Memory lives in Markdown under your repo (e.g. `
|
|
12
|
+
- **Versioned** — Memory lives in Markdown under your repo (e.g. `docs/<type>/` by default for project scope) or in a shared home (e.g. `home/memory/` for cross-project). Commit, branch, and diff as with any other docs.
|
|
13
13
|
- **No semantic deps required** — Core experience is text-only (FTS5). No need to install embeddings or vector libs unless you opt in.
|
|
14
14
|
|
|
15
15
|
---
|
|
@@ -57,37 +57,35 @@ Arguments are passed as `--key value`. Booleans use `--flag` or `--no-flag`. Arr
|
|
|
57
57
|
| `--embeddings` | `MIXGRAM_EMBEDDINGS_ENABLED` | Enable optional semantic (hybrid) search. |
|
|
58
58
|
| `--watch` | `MIXGRAM_WATCH` | Watch files and reindex on change. |
|
|
59
59
|
| `--home <path>` | `MIXGRAM_HOME` | Home memory root (cross-project). |
|
|
60
|
-
| `--project-memory <path>` | `MIXGRAM_PROJECT_MEMORY` | Project memory root (default: `./
|
|
60
|
+
| `--project-memory <path>` | `MIXGRAM_PROJECT_MEMORY` | Project memory root (default: `./docs`, relative to repo). |
|
|
61
61
|
| `--projects <path>` | `MIXGRAM_PROJECTS` | Projects root (legacy). |
|
|
62
62
|
| `--sqlite-path <path>` | `MIXGRAM_SQLITE_PATH` | SQLite index path. |
|
|
63
63
|
|
|
64
64
|
**Example — Cursor**
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
Create `.cursor/mcp.json` in your repo (recommended, and required for Cursor Cloud Agents) or add the same config to `~/.cursor/mcp.json` (or run `mixgram setup cursor`):
|
|
67
67
|
|
|
68
68
|
```json
|
|
69
69
|
{
|
|
70
70
|
"mcpServers": {
|
|
71
71
|
"mixgram": {
|
|
72
72
|
"command": "mixgram",
|
|
73
|
-
"args": ["mcp"]
|
|
74
|
-
"cwd": "${workspaceFolder}"
|
|
73
|
+
"args": ["mcp", "--project-memory", "${workspaceFolder}/docs"]
|
|
75
74
|
}
|
|
76
75
|
}
|
|
77
76
|
}
|
|
78
77
|
```
|
|
79
78
|
|
|
80
|
-
With embeddings and custom paths
|
|
79
|
+
With embeddings and custom paths:
|
|
81
80
|
|
|
82
81
|
```json
|
|
83
82
|
"mixgram": {
|
|
84
83
|
"command": "mixgram",
|
|
85
|
-
"args": ["mcp", "--
|
|
86
|
-
"cwd": "${workspaceFolder}"
|
|
84
|
+
"args": ["mcp", "--project-memory", "${workspaceFolder}/docs", "--embeddings", "--home", "/data/memory"]
|
|
87
85
|
}
|
|
88
86
|
```
|
|
89
87
|
|
|
90
|
-
|
|
88
|
+
Restart Cursor after changing config.
|
|
91
89
|
|
|
92
90
|
---
|
|
93
91
|
|
|
@@ -117,7 +115,7 @@ Example **`.mixgram/config.json`** (project or home):
|
|
|
117
115
|
```json
|
|
118
116
|
{
|
|
119
117
|
"homeMemoryRoot": "~/.mixgram/docs",
|
|
120
|
-
"projectMemoryRoot": "./
|
|
118
|
+
"projectMemoryRoot": "./docs",
|
|
121
119
|
"sqlitePath": "~/.mixgram/index.db",
|
|
122
120
|
"embeddings": {
|
|
123
121
|
"enabled": true
|
|
@@ -130,7 +128,7 @@ Paths in the config file are relative to the config file’s directory (project)
|
|
|
130
128
|
| Option | Default | Description |
|
|
131
129
|
|--------|---------|-------------|
|
|
132
130
|
| `homeMemoryRoot` | `~/.mixgram/docs` | Cross-project memory (Markdown files). |
|
|
133
|
-
| `projectMemoryRoot` | `./
|
|
131
|
+
| `projectMemoryRoot` | `./docs` | Project memory in the current repo (e.g. `docs/architecture/`, `docs/decisions/`). You can use another folder name, e.g. `./specs` for `specs/architecture/`. |
|
|
134
132
|
| `sqlitePath` | `~/.mixgram/index.db` | SQLite index (FTS5 + optional vectors). |
|
|
135
133
|
| `watch` | `true` | Watch files and reindex on change. |
|
|
136
134
|
| `indexing.reindexOnStartup` | `true` | Full reindex when server starts. |
|
|
@@ -145,7 +143,7 @@ import { run } from 'mixgram/src/mcp/server.js';
|
|
|
145
143
|
|
|
146
144
|
await run({
|
|
147
145
|
homeMemoryRoot: '/data/memory',
|
|
148
|
-
projectMemoryRoot: '/path/to/repo/
|
|
146
|
+
projectMemoryRoot: '/path/to/repo/docs',
|
|
149
147
|
sqlitePath: '/data/.mixgram/index.db',
|
|
150
148
|
watch: true
|
|
151
149
|
});
|
|
@@ -171,7 +169,7 @@ Core flow: save Markdown-backed documents, search with FTS5, get context.
|
|
|
171
169
|
}
|
|
172
170
|
```
|
|
173
171
|
|
|
174
|
-
- **scope** `project` → file under `<repo>/
|
|
172
|
+
- **scope** `project` → file under `<repo>/docs/<type>/...` (e.g. `docs/architecture/`, `docs/decisions/`).
|
|
175
173
|
- **scope** `home` → file under `home/memory/...` (cross-project).
|
|
176
174
|
- Same `topic_key` + scope + project → **update** existing doc; otherwise create.
|
|
177
175
|
|
|
@@ -240,12 +238,12 @@ Useful after cloning a repo or editing Markdown by hand:
|
|
|
240
238
|
{ "full": true }
|
|
241
239
|
```
|
|
242
240
|
|
|
243
|
-
- `full: true` — rebuild entire index from `home/**/*.md` and `
|
|
241
|
+
- `full: true` — rebuild entire index from `home/**/*.md` and `docs/**/*.md` (project memory in repo).
|
|
244
242
|
- `full: false` (default) — incremental (by mtime).
|
|
245
243
|
|
|
246
244
|
Manual edit example:
|
|
247
245
|
|
|
248
|
-
1. Edit a `.md` file under `home/memory` or `
|
|
246
|
+
1. Edit a `.md` file under `home/memory` or `docs/<type>/` in your repo.
|
|
249
247
|
2. Call `mem_reindex({ full: true })` (or run with `watch: true` so changes are picked up).
|
|
250
248
|
3. `mem_search` and `mem_get_observation` reflect the new content.
|
|
251
249
|
|
|
@@ -282,7 +280,7 @@ When `embeddings.enabled` is `true` and the optional embedding stack is availabl
|
|
|
282
280
|
|
|
283
281
|
- **Saves** are durable and searchable by text immediately; embeddings are queued and processed in the background.
|
|
284
282
|
- **Search** becomes **hybrid**: FTS + vector similarity, blended with `search.ftsWeight` and `search.semanticWeight` (e.g. 0.7 and 0.3).
|
|
285
|
-
- A **worker** runs in
|
|
283
|
+
- A **worker** runs in a separate process and processes the embedding queue (e.g. every 2 seconds). Query embeddings for hybrid search are also computed in that process via IPC, so the main MCP server never loads `@huggingface/transformers` and is not affected by native/ONNX crashes in the embedding stack.
|
|
286
284
|
|
|
287
285
|
### Enable embeddings
|
|
288
286
|
|
package/bin/mixgram.js
CHANGED
|
@@ -12,6 +12,10 @@ import { getToolByName, listToolNames, parseToolArgs, formatToolHelp } from '../
|
|
|
12
12
|
import fs from 'fs';
|
|
13
13
|
import path from 'path';
|
|
14
14
|
import os from 'os';
|
|
15
|
+
import { fileURLToPath } from 'url';
|
|
16
|
+
|
|
17
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
18
|
+
const PKG = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf8'));
|
|
15
19
|
|
|
16
20
|
const SUBCOMMAND = process.argv[2];
|
|
17
21
|
const ARG = process.argv[3];
|
|
@@ -101,10 +105,8 @@ const MIXGRAM_ENTRY = {
|
|
|
101
105
|
};
|
|
102
106
|
|
|
103
107
|
const CURSOR_MIXGRAM_ENTRY = {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
// stays under <workspace>/mixgram instead of the parent process cwd.
|
|
107
|
-
cwd: '${workspaceFolder}'
|
|
108
|
+
command: 'mixgram',
|
|
109
|
+
args: ['mcp', '--project-memory', '${workspaceFolder}/docs']
|
|
108
110
|
};
|
|
109
111
|
|
|
110
112
|
function cursorMcpPath() {
|
|
@@ -216,6 +218,7 @@ Commands:
|
|
|
216
218
|
mcp [options] Run the MCP server (stdio). Use this in your client config.
|
|
217
219
|
setup <client> Register Mixgram with an MCP client.
|
|
218
220
|
help [tool] Show help; with optional tool name, show options for that tool.
|
|
221
|
+
-v, --version Print version from package and exit.
|
|
219
222
|
<tool> [options] Run an MCP tool by name. Tools:
|
|
220
223
|
${toolLines}
|
|
221
224
|
|
|
@@ -223,21 +226,20 @@ mcp options (and env / config file):
|
|
|
223
226
|
--config <path> Config file (default: ./.mixgram/config.json or ~/.mixgram/config.json)
|
|
224
227
|
--embeddings Enable semantic search (or embeddings.enabled in config)
|
|
225
228
|
--watch Watch files and reindex on change
|
|
226
|
-
--home <path> Home memory root (default:
|
|
227
|
-
--project-memory <path> Project memory root (default: ./
|
|
228
|
-
--
|
|
229
|
-
--sqlite-path <path> SQLite index path (default: ./.mixgram/index.db)
|
|
229
|
+
--home <path> Home memory root (default: ~/.mixgram/docs)
|
|
230
|
+
--project-memory <path> Project memory root (default: ./docs, relative to repo)
|
|
231
|
+
--sqlite-path <path> SQLite index path (default: ~/.mixgram/index.db)
|
|
230
232
|
|
|
231
233
|
Env: MIXGRAM_CONFIG, MIXGRAM_EMBEDDINGS_ENABLED, MIXGRAM_HOME, MIXGRAM_PROJECT_MEMORY,
|
|
232
|
-
|
|
234
|
+
MIXGRAM_SQLITE_PATH, MIXGRAM_WATCH
|
|
233
235
|
|
|
234
236
|
Setup targets:
|
|
235
237
|
cursor Cursor IDE
|
|
236
238
|
gemini-cli Gemini CLI
|
|
237
239
|
codex Codex
|
|
238
240
|
|
|
239
|
-
Example (Cursor): "mixgram": { "command": "mixgram", "args": ["mcp"] }
|
|
240
|
-
With embeddings: "args": ["mcp", "--embeddings"]
|
|
241
|
+
Example (Cursor): "mixgram": { "command": "mixgram", "args": ["mcp", "--project-memory", "${workspaceFolder}/docs"] }
|
|
242
|
+
With embeddings: "args": ["mcp", "--project-memory", "${workspaceFolder}/docs", "--embeddings"]
|
|
241
243
|
Config file: .mixgram/config.json or ~/.mixgram/config.json
|
|
242
244
|
|
|
243
245
|
npm install -g mixgram
|
|
@@ -245,6 +247,11 @@ Config file: .mixgram/config.json or ~/.mixgram/config.json
|
|
|
245
247
|
}
|
|
246
248
|
|
|
247
249
|
async function main() {
|
|
250
|
+
if (SUBCOMMAND === '-v' || SUBCOMMAND === '--version') {
|
|
251
|
+
console.log(PKG.version);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
248
255
|
if (SUBCOMMAND === 'mcp') {
|
|
249
256
|
const { overrides, baseDir, projectBaseDir } = loadCliConfig();
|
|
250
257
|
await run(overrides, baseDir, projectBaseDir)
|
package/package.json
CHANGED
package/spec/run-tests.js
CHANGED
|
@@ -22,7 +22,7 @@ const __dirname = path.dirname(__filename);
|
|
|
22
22
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mixgram-test-'));
|
|
23
23
|
const config = loadConfig({
|
|
24
24
|
homeMemoryRoot: path.join(tmpDir, 'home', 'memory'),
|
|
25
|
-
projectMemoryRoot: path.join(tmpDir, '
|
|
25
|
+
projectMemoryRoot: path.join(tmpDir, 'docs'),
|
|
26
26
|
sqlitePath: path.join(tmpDir, '.mixgram', 'index.db')
|
|
27
27
|
});
|
|
28
28
|
fs.mkdirSync(path.dirname(config.sqlitePath), { recursive: true });
|
|
@@ -57,7 +57,7 @@ function runCliSetupChecks() {
|
|
|
57
57
|
const repoRoot = path.dirname(__dirname);
|
|
58
58
|
|
|
59
59
|
if (reporter.VISUAL) {
|
|
60
|
-
reporter.startScenario('CLI setup cursor config', 'Verify Cursor setup writes
|
|
60
|
+
reporter.startScenario('CLI setup cursor config', 'Verify Cursor setup writes project-memory arg for project-scoped memory.');
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
let p = 0;
|
|
@@ -83,8 +83,8 @@ function runCliSetupChecks() {
|
|
|
83
83
|
const cursorConfig = JSON.parse(fs.readFileSync(cursorConfigPath, 'utf8'));
|
|
84
84
|
const entry = cursorConfig?.mcpServers?.mixgram;
|
|
85
85
|
ok(entry?.command === 'mixgram', 'cursor entry command preserved');
|
|
86
|
-
|
|
87
|
-
ok(
|
|
86
|
+
const hasProjectMemory = Array.isArray(entry?.args) && entry.args.includes('--project-memory') && entry.args.includes('${workspaceFolder}/docs');
|
|
87
|
+
ok(hasProjectMemory, 'cursor entry uses --project-memory ${workspaceFolder}/docs');
|
|
88
88
|
} catch (err) {
|
|
89
89
|
if (reporter.VISUAL) reporter.check('CLI setup cursor config threw', false, err.message);
|
|
90
90
|
f += 1;
|
|
@@ -45,7 +45,7 @@ export const scenarios = [
|
|
|
45
45
|
reporter.step('mem_save', { title: decisionSqliteIndex.title, topic_key: decisionSqliteIndex.topic_key }, { id: s1.id, path: s1.path, created: s1.created });
|
|
46
46
|
ok(s1.success && s1.id && s1.created === true, 'save success');
|
|
47
47
|
ok(fs.existsSync(s1.path), 'file created');
|
|
48
|
-
ok(s1.path.includes('
|
|
48
|
+
ok(s1.path.includes('docs') || s1.path.replace(/\\/g, '/').includes('docs'), 'project doc under project memory root');
|
|
49
49
|
ok(path.basename(s1.path) === 'architecture-sqlite-derived-index.md', 'project doc filename omits redundant prefixes');
|
|
50
50
|
|
|
51
51
|
const search1 = await h.mem_search({ query: 'derived SQLite index', project: PROJECT_NAME });
|
package/src/config.js
CHANGED
|
@@ -55,7 +55,7 @@ function resolvePaths(config, baseDir, projectBaseDir = null) {
|
|
|
55
55
|
return {
|
|
56
56
|
...config,
|
|
57
57
|
homeMemoryRoot: path.isAbsolute(homeRoot) ? homeRoot : path.resolve(base, homeRoot),
|
|
58
|
-
projectMemoryRoot: path.resolve(projectBase, config.projectMemoryRoot ?? './
|
|
58
|
+
projectMemoryRoot: path.resolve(projectBase, config.projectMemoryRoot ?? './docs'),
|
|
59
59
|
sqlitePath: path.isAbsolute(sqlite) ? sqlite : path.resolve(base, sqlite)
|
|
60
60
|
};
|
|
61
61
|
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standalone process for embedding jobs and query embeddings. Isolates
|
|
3
|
+
* @huggingface/transformers (and any native/ONNX crashes) from the MCP server.
|
|
4
|
+
* Invoked via child_process.fork() with env MIXGRAM_WORKER_CONFIG pointing to
|
|
5
|
+
* a JSON file with the resolved config. Listens for IPC 'embed' to embed query
|
|
6
|
+
* text for hybrid search so the main process never loads transformers.
|
|
7
|
+
*/
|
|
8
|
+
import fs from 'fs';
|
|
9
|
+
import path from 'path';
|
|
10
|
+
import { fileURLToPath } from 'url';
|
|
11
|
+
|
|
12
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
const DEFAULT_POLL_MS = 2000;
|
|
14
|
+
|
|
15
|
+
function loadWorkerConfig() {
|
|
16
|
+
const configPath = process.env.MIXGRAM_WORKER_CONFIG;
|
|
17
|
+
if (!configPath || !fs.existsSync(configPath)) return null;
|
|
18
|
+
const raw = fs.readFileSync(configPath, 'utf8');
|
|
19
|
+
return JSON.parse(raw);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let embedderInstance = null;
|
|
23
|
+
|
|
24
|
+
async function getEmbedder(config) {
|
|
25
|
+
if (embedderInstance) return embedderInstance;
|
|
26
|
+
const { getEmbedder: getEmbedderFn } = await import('./embedder.js');
|
|
27
|
+
embedderInstance = await getEmbedderFn(config);
|
|
28
|
+
return embedderInstance;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function setupIpc(config) {
|
|
32
|
+
process.on('message', async (msg) => {
|
|
33
|
+
if (msg?.type !== 'embed' || typeof msg.text !== 'string') return;
|
|
34
|
+
try {
|
|
35
|
+
const embedder = await getEmbedder(config);
|
|
36
|
+
if (!embedder) {
|
|
37
|
+
process.send?.({ type: 'embedResult', id: msg.id, err: 'embeddings not available' });
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const vector = await embedder.embed(msg.text);
|
|
41
|
+
process.send?.({ type: 'embedResult', id: msg.id, vector: Array.from(vector) });
|
|
42
|
+
} catch (err) {
|
|
43
|
+
process.send?.({ type: 'embedResult', id: msg.id, err: String(err?.message ?? err) });
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function runLoop(config) {
|
|
49
|
+
const { processNextJob } = await import('./queue.js');
|
|
50
|
+
const pollMs = config?.embeddings?.workerPollMs ?? DEFAULT_POLL_MS;
|
|
51
|
+
const run = async () => {
|
|
52
|
+
try {
|
|
53
|
+
await processNextJob(config);
|
|
54
|
+
} catch (err) {
|
|
55
|
+
if (process.stderr) {
|
|
56
|
+
process.stderr.write(`[mixgram-worker] job error: ${err?.message ?? err}\n`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
await run();
|
|
61
|
+
setInterval(run, pollMs);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function main() {
|
|
65
|
+
const config = loadWorkerConfig();
|
|
66
|
+
if (!config?.embeddings?.enabled) {
|
|
67
|
+
process.exit(0);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (process.send) setupIpc(config);
|
|
71
|
+
runLoop(config).catch((err) => {
|
|
72
|
+
if (process.stderr) {
|
|
73
|
+
process.stderr.write(`[mixgram-worker] fatal: ${err?.message ?? err}\n`);
|
|
74
|
+
}
|
|
75
|
+
process.exit(1);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
main();
|
|
@@ -118,24 +118,44 @@ function search(config, options = {}) {
|
|
|
118
118
|
async function hybridSearch(config, { query, scopeMode, project, limit, ftsRows }) {
|
|
119
119
|
const ftsWeight = config.search?.ftsWeight ?? 0.7;
|
|
120
120
|
const semanticWeight = config.search?.semanticWeight ?? 0.3;
|
|
121
|
-
let
|
|
121
|
+
let queryEmbedding;
|
|
122
|
+
if (typeof config.getQueryEmbedding === 'function') {
|
|
123
|
+
try {
|
|
124
|
+
queryEmbedding = await config.getQueryEmbedding(query);
|
|
125
|
+
} catch {
|
|
126
|
+
return ftsRows.slice(0, limit).map(({ _rank, ...r }) => r);
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
let embedder;
|
|
130
|
+
try {
|
|
131
|
+
const [{ getEmbedder }, { getVectorStore }] = await Promise.all([
|
|
132
|
+
import('../embeddings/embedder.js'),
|
|
133
|
+
import('../embeddings/vectorStore.js')
|
|
134
|
+
]);
|
|
135
|
+
embedder = await getEmbedder(config);
|
|
136
|
+
if (!embedder) {
|
|
137
|
+
return ftsRows.slice(0, limit).map(({ _rank, ...r }) => r);
|
|
138
|
+
}
|
|
139
|
+
queryEmbedding = await embedder.embed(query);
|
|
140
|
+
} catch {
|
|
141
|
+
return ftsRows.slice(0, limit).map(({ _rank, ...r }) => r);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (!queryEmbedding) {
|
|
145
|
+
return ftsRows.slice(0, limit).map(({ _rank, ...r }) => r);
|
|
146
|
+
}
|
|
122
147
|
let vectorStore;
|
|
123
148
|
try {
|
|
124
|
-
const
|
|
125
|
-
import('../embeddings/embedder.js'),
|
|
126
|
-
import('../embeddings/vectorStore.js')
|
|
127
|
-
]);
|
|
128
|
-
embedder = await getEmbedder(config);
|
|
149
|
+
const { getVectorStore } = await import('../embeddings/vectorStore.js');
|
|
129
150
|
vectorStore = await getVectorStore(config);
|
|
130
151
|
} catch {
|
|
131
152
|
return ftsRows.slice(0, limit).map(({ _rank, ...r }) => r);
|
|
132
153
|
}
|
|
133
|
-
if (!
|
|
154
|
+
if (!vectorStore) {
|
|
134
155
|
return ftsRows.slice(0, limit).map(({ _rank, ...r }) => r);
|
|
135
156
|
}
|
|
136
157
|
let semanticHits;
|
|
137
158
|
try {
|
|
138
|
-
const queryEmbedding = await embedder.embed(query);
|
|
139
159
|
semanticHits = await vectorStore.search(config, queryEmbedding, Math.max(limit * 2, 30));
|
|
140
160
|
} catch {
|
|
141
161
|
return ftsRows.slice(0, limit).map(({ _rank, ...r }) => r);
|
package/src/mcp/server.js
CHANGED
|
@@ -1,9 +1,49 @@
|
|
|
1
|
+
import { fork } from 'child_process';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import os from 'os';
|
|
5
|
+
import { randomUUID } from 'crypto';
|
|
6
|
+
import { fileURLToPath } from 'url';
|
|
1
7
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
2
8
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
3
9
|
import { loadConfig } from '../config.js';
|
|
4
10
|
import { createToolHandlers } from './tools.js';
|
|
5
11
|
import { getToolDefinitions } from './tool-registry.js';
|
|
6
12
|
|
|
13
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
14
|
+
let embeddingWorkerChild = null;
|
|
15
|
+
let embeddingWorkerConfigPath = null;
|
|
16
|
+
|
|
17
|
+
const EMBED_TIMEOUT_MS = 60000;
|
|
18
|
+
|
|
19
|
+
function makeQueryEmbedder(child) {
|
|
20
|
+
return function getQueryEmbedding(text) {
|
|
21
|
+
if (!child?.connected) return Promise.resolve(null);
|
|
22
|
+
const id = randomUUID();
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
const timeout = setTimeout(() => {
|
|
25
|
+
cleanup();
|
|
26
|
+
reject(new Error('embedding timeout'));
|
|
27
|
+
}, EMBED_TIMEOUT_MS);
|
|
28
|
+
const onMessage = (msg) => {
|
|
29
|
+
if (msg?.type !== 'embedResult' || msg.id !== id) return;
|
|
30
|
+
cleanup();
|
|
31
|
+
if (msg.err) {
|
|
32
|
+
reject(new Error(msg.err));
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
resolve(msg.vector ? new Float32Array(msg.vector) : null);
|
|
36
|
+
};
|
|
37
|
+
const cleanup = () => {
|
|
38
|
+
clearTimeout(timeout);
|
|
39
|
+
child.off('message', onMessage);
|
|
40
|
+
};
|
|
41
|
+
child.on('message', onMessage);
|
|
42
|
+
child.send({ type: 'embed', id, text });
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
7
47
|
function createServer(configOverrides = {}, baseDir = null, projectBaseDir = null) {
|
|
8
48
|
const config = loadConfig(configOverrides, baseDir, projectBaseDir);
|
|
9
49
|
const mcpServer = new McpServer(
|
|
@@ -59,8 +99,33 @@ async function run(configOverrides = {}, baseDir = null, projectBaseDir = null)
|
|
|
59
99
|
}
|
|
60
100
|
if (config.embeddings?.enabled) {
|
|
61
101
|
try {
|
|
62
|
-
|
|
63
|
-
|
|
102
|
+
embeddingWorkerConfigPath = path.join(
|
|
103
|
+
os.tmpdir(),
|
|
104
|
+
`mixgram-worker-config-${process.pid}-${Date.now()}.json`
|
|
105
|
+
);
|
|
106
|
+
fs.writeFileSync(embeddingWorkerConfigPath, JSON.stringify(config), 'utf8');
|
|
107
|
+
const workerPath = path.join(__dirname, '..', 'core', 'embeddings', 'worker-process.js');
|
|
108
|
+
embeddingWorkerChild = fork(workerPath, [], {
|
|
109
|
+
stdio: ['ignore', 'ignore', 'ignore', 'ipc'],
|
|
110
|
+
env: { ...process.env, MIXGRAM_WORKER_CONFIG: embeddingWorkerConfigPath }
|
|
111
|
+
});
|
|
112
|
+
config.getQueryEmbedding = makeQueryEmbedder(embeddingWorkerChild);
|
|
113
|
+
embeddingWorkerChild.on('exit', (code, signal) => {
|
|
114
|
+
if (code !== 0 && code != null && process.stderr) {
|
|
115
|
+
process.stderr.write(`[mixgram] embeddings worker exited: code=${code} signal=${signal}\n`);
|
|
116
|
+
}
|
|
117
|
+
config.getQueryEmbedding = null;
|
|
118
|
+
if (embeddingWorkerConfigPath && fs.existsSync(embeddingWorkerConfigPath)) {
|
|
119
|
+
try { fs.unlinkSync(embeddingWorkerConfigPath); } catch (_) {}
|
|
120
|
+
embeddingWorkerConfigPath = null;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
process.on('exit', () => {
|
|
124
|
+
if (embeddingWorkerChild) embeddingWorkerChild.kill();
|
|
125
|
+
if (embeddingWorkerConfigPath && fs.existsSync(embeddingWorkerConfigPath)) {
|
|
126
|
+
try { fs.unlinkSync(embeddingWorkerConfigPath); } catch (_) {}
|
|
127
|
+
}
|
|
128
|
+
});
|
|
64
129
|
} catch (err) {
|
|
65
130
|
if (typeof process !== 'undefined' && process.stderr) {
|
|
66
131
|
process.stderr.write(`[mixgram] embeddings worker start failed: ${err?.message ?? err}\n`);
|