mixgram 1.0.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/.mixgram/index.db +0 -0
- package/.mixgram/test-esm.db +0 -0
- package/.mixgram/test-index.db +0 -0
- package/.mixgram/test-stage1.db +0 -0
- package/README.md +335 -0
- package/bin/mixgram.js +309 -0
- package/index.js +9 -0
- package/package.json +51 -0
- package/projects/castlebravo/memory/decisions/decision--architecture/sqlite-derived-index.md +17 -0
- package/projects/test/memory/decisions/decision--architecture/sqlite-derived-index.md +17 -0
- package/spec/fixtures/realistic-content.js +97 -0
- package/spec/reporter.js +127 -0
- package/spec/run-tests-runner.js +40 -0
- package/spec/run-tests.js +160 -0
- package/spec/scenarios/scenarios.js +520 -0
- package/spec/sdd.md +1002 -0
- package/spec/tdd.md +1237 -0
- package/src/config.js +56 -0
- package/src/core/documents/documents.js +207 -0
- package/src/core/embeddings/embedder.js +58 -0
- package/src/core/embeddings/queue.js +118 -0
- package/src/core/embeddings/vectorStore.js +127 -0
- package/src/core/embeddings/worker.js +27 -0
- package/src/core/indexing/indexer.js +131 -0
- package/src/core/indexing/parser.js +78 -0
- package/src/core/indexing/reindex.js +101 -0
- package/src/core/prompts/prompts.js +15 -0
- package/src/core/search/search.js +223 -0
- package/src/core/sessions/sessions.js +27 -0
- package/src/db/migrations/001_initial.sql +86 -0
- package/src/db/sqlite.js +39 -0
- package/src/fs/paths.js +79 -0
- package/src/fs/watcher.js +49 -0
- package/src/mcp/cli-adapter.js +118 -0
- package/src/mcp/server.js +47 -0
- package/src/mcp/tool-registry.js +138 -0
- package/src/mcp/tools.js +159 -0
- package/src/utils/hash.js +6 -0
- package/src/utils/ids.js +22 -0
- package/src/utils/markdown.js +49 -0
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mixgram",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "MCP memory server with Markdown source of truth and SQLite FTS5.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"mcp",
|
|
7
|
+
"model-context-protocol",
|
|
8
|
+
"memory",
|
|
9
|
+
"engram",
|
|
10
|
+
"markdown",
|
|
11
|
+
"sqlite",
|
|
12
|
+
"fts5",
|
|
13
|
+
"full-text-search",
|
|
14
|
+
"embeddings",
|
|
15
|
+
"semantic-search",
|
|
16
|
+
"cursor",
|
|
17
|
+
"ai-memory",
|
|
18
|
+
"antigravity",
|
|
19
|
+
"claude",
|
|
20
|
+
"code",
|
|
21
|
+
"codex",
|
|
22
|
+
"sdd",
|
|
23
|
+
"tdd",
|
|
24
|
+
"clasen"
|
|
25
|
+
],
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"author": "Martin Clasen",
|
|
28
|
+
"type": "module",
|
|
29
|
+
"main": "index.js",
|
|
30
|
+
"bin": {
|
|
31
|
+
"mixgram": "./bin/mixgram.js"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"start": "node index.js",
|
|
35
|
+
"test": "node spec/run-tests.js",
|
|
36
|
+
"test:visual": "node spec/run-tests.js --visual"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@huggingface/transformers": "^3.4.2",
|
|
40
|
+
"@modelcontextprotocol/sdk": "^1.0.0",
|
|
41
|
+
"better-sqlite3": "^12.6.2",
|
|
42
|
+
"chokidar": "^4.0.1",
|
|
43
|
+
"gray-matter": "^4.0.3",
|
|
44
|
+
"glob": "^10.4.5",
|
|
45
|
+
"remark-parse": "^11.0.0",
|
|
46
|
+
"sqlite-vec": "^0.1.6",
|
|
47
|
+
"unified": "^11.0.5",
|
|
48
|
+
"unist-util-visit": "^5.0.0",
|
|
49
|
+
"zod": "^3.23.0"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: obs_6ebf7e3f75daa71933d79c52
|
|
3
|
+
title: Use SQLite as derived index
|
|
4
|
+
type: decision
|
|
5
|
+
scope: project
|
|
6
|
+
project: castlebravo
|
|
7
|
+
topic_key: architecture/sqlite-derived-index
|
|
8
|
+
tool_name: mem_save
|
|
9
|
+
created_at: '2026-03-09T02:51:10.990Z'
|
|
10
|
+
updated_at: '2026-03-09T02:51:10.990Z'
|
|
11
|
+
revision_count: 1
|
|
12
|
+
duplicate_count: 0
|
|
13
|
+
deleted: false
|
|
14
|
+
tags: []
|
|
15
|
+
embedding_status: disabled
|
|
16
|
+
---
|
|
17
|
+
SQLite will be used as a derived index over Markdown documents.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: obs_8a9cd5a608500ed1a05a6326
|
|
3
|
+
title: Use SQLite as derived index
|
|
4
|
+
type: decision
|
|
5
|
+
scope: project
|
|
6
|
+
project: test
|
|
7
|
+
topic_key: architecture/sqlite-derived-index
|
|
8
|
+
tool_name: mem_save
|
|
9
|
+
created_at: '2026-03-09T02:40:42.952Z'
|
|
10
|
+
updated_at: '2026-03-09T02:40:59.029Z'
|
|
11
|
+
revision_count: 2
|
|
12
|
+
duplicate_count: 0
|
|
13
|
+
deleted: false
|
|
14
|
+
tags: []
|
|
15
|
+
embedding_status: pending
|
|
16
|
+
---
|
|
17
|
+
We use a derived SQLite index over Markdown.
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Realistic corpus content for visual/scenario tests.
|
|
3
|
+
* Used so humans can judge relevance, snippets, and ranking.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export const PROJECT_NAME = 'castlebravo';
|
|
7
|
+
|
|
8
|
+
/** Architecture decision: SQLite as derived index */
|
|
9
|
+
export const decisionSqliteIndex = {
|
|
10
|
+
title: 'Use SQLite as derived index',
|
|
11
|
+
type: 'decision',
|
|
12
|
+
scope: 'project',
|
|
13
|
+
project: PROJECT_NAME,
|
|
14
|
+
topic_key: 'architecture/sqlite-derived-index',
|
|
15
|
+
content: `We use SQLite as a derived index over Markdown documents. The Markdown files under \`memory/\` are the source of truth; the SQLite database is rebuilt from them on reindex.
|
|
16
|
+
|
|
17
|
+
Benefits:
|
|
18
|
+
- Git-friendly: only Markdown is committed; the index can be recreated.
|
|
19
|
+
- Human-editable: memory can be fixed or extended by editing .md files.
|
|
20
|
+
- Fast full-text search via FTS5 and optional semantic search via sqlite-vec.`
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** Updated version of the same decision (revision two) */
|
|
24
|
+
export const decisionSqliteIndexRevision2 = {
|
|
25
|
+
...decisionSqliteIndex,
|
|
26
|
+
content: `We use SQLite as the derived index over Markdown documents. Revision two: the index is rebuilt from disk on startup and on demand. Markdown remains the single source of truth.`
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Home-scoped pattern: incremental reindexing */
|
|
30
|
+
export const patternIncrementalReindex = {
|
|
31
|
+
scope: 'home',
|
|
32
|
+
type: 'pattern',
|
|
33
|
+
topic_key: 'pattern/incremental-reindexing',
|
|
34
|
+
title: 'Incremental reindex',
|
|
35
|
+
content: `Reindex only changed files. Use file mtime and content hash to skip unchanged documents. This keeps index updates fast when the corpus grows.`
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** Project-local pattern with same topic_key (must not overwrite home) */
|
|
39
|
+
export const patternIncrementalReindexProject = {
|
|
40
|
+
title: 'Project local reindex',
|
|
41
|
+
type: 'pattern',
|
|
42
|
+
scope: 'project',
|
|
43
|
+
project: PROJECT_NAME,
|
|
44
|
+
topic_key: 'pattern/incremental-reindexing',
|
|
45
|
+
content: 'Project-specific reindex note: we run incremental reindex after each batch of edits.'
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** Session summary content */
|
|
49
|
+
export const sessionSummaryContent = 'We decided to use SQLite for the index and to keep Markdown as source of truth. Next: add watch mode for live reindex.';
|
|
50
|
+
|
|
51
|
+
/** Prompt stored in session */
|
|
52
|
+
export const promptContent = 'User asked about indexing strategy and whether to use FTS5 or external search.';
|
|
53
|
+
|
|
54
|
+
/** Timeline: first and second note in same session */
|
|
55
|
+
export const timelineFirst = {
|
|
56
|
+
title: 'First',
|
|
57
|
+
type: 'decision',
|
|
58
|
+
scope: 'project',
|
|
59
|
+
project: PROJECT_NAME,
|
|
60
|
+
topic_key: 'timeline/first',
|
|
61
|
+
content: 'First note in this session: we agreed on SQLite.'
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export const timelineSecond = {
|
|
65
|
+
title: 'Second',
|
|
66
|
+
type: 'decision',
|
|
67
|
+
scope: 'project',
|
|
68
|
+
project: PROJECT_NAME,
|
|
69
|
+
topic_key: 'timeline/second',
|
|
70
|
+
content: 'Second note: FTS5 will be used for full-text search; optional embeddings later.'
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/** Content to inject by manual edit (reindex scenario) */
|
|
74
|
+
export const manualEditSnippet = 'Index is rebuilt from Markdown on reindex. Manual edits are reflected after reindex.';
|
|
75
|
+
|
|
76
|
+
/** Semantic test: Spanish doc about persistence */
|
|
77
|
+
export const semanticPersistenceDoc = {
|
|
78
|
+
title: 'Persistencia y búsqueda',
|
|
79
|
+
type: 'reference',
|
|
80
|
+
scope: 'project',
|
|
81
|
+
project: PROJECT_NAME,
|
|
82
|
+
topic_key: 'test/persistencia-sqlite',
|
|
83
|
+
content: 'Los datos se guardan en SQLite y la búsqueda es una forma de persistencia.'
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** Semantic query that should find the above (paraphrase) */
|
|
87
|
+
export const semanticPersistenceQuery = 'dónde se persisten los datos';
|
|
88
|
+
|
|
89
|
+
/** Semantic fallback test (FTS when embeddings enabled) */
|
|
90
|
+
export const semanticFallbackDoc = {
|
|
91
|
+
title: 'Semantic test doc',
|
|
92
|
+
type: 'generated_note',
|
|
93
|
+
scope: 'project',
|
|
94
|
+
project: PROJECT_NAME,
|
|
95
|
+
topic_key: 'test/semantic-fallback',
|
|
96
|
+
content: 'Text for semantic fallback test.'
|
|
97
|
+
};
|
package/spec/reporter.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Visual console reporter for scenario tests.
|
|
3
|
+
* Prints scenario name, goal, inputs, outputs, and PASS/FAIL so humans can evaluate behavior.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const VISUAL = process.argv.includes('--visual');
|
|
7
|
+
|
|
8
|
+
function out(...args) {
|
|
9
|
+
if (VISUAL) console.log(...args);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function dim(msg) {
|
|
13
|
+
if (!VISUAL) return;
|
|
14
|
+
console.log('\x1b[2m%s\x1b[0m', msg);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function green(msg) {
|
|
18
|
+
if (!VISUAL) return;
|
|
19
|
+
console.log('\x1b[32m%s\x1b[0m', msg);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function red(msg) {
|
|
23
|
+
if (!VISUAL) return;
|
|
24
|
+
console.log('\x1b[31m%s\x1b[0m', msg);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Pretty-print a small object for console (one line or few lines).
|
|
29
|
+
*/
|
|
30
|
+
function preview(obj, maxLen = 200) {
|
|
31
|
+
if (obj === null || obj === undefined) return String(obj);
|
|
32
|
+
const s = typeof obj === 'string' ? obj : JSON.stringify(obj);
|
|
33
|
+
if (s.length <= maxLen) return s;
|
|
34
|
+
return s.slice(0, maxLen) + '...';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Start a scenario block.
|
|
39
|
+
* @param {string} name
|
|
40
|
+
* @param {string} goal
|
|
41
|
+
*/
|
|
42
|
+
function startScenario(name, goal) {
|
|
43
|
+
if (!VISUAL) return;
|
|
44
|
+
console.log('');
|
|
45
|
+
console.log('━━━ %s ━━━', name);
|
|
46
|
+
dim('Goal: ' + goal);
|
|
47
|
+
console.log('');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Print a step: label + optional input/output.
|
|
52
|
+
* @param {string} label - e.g. "mem_save", "mem_search"
|
|
53
|
+
* @param {object} [input] - args sent to the tool
|
|
54
|
+
* @param {object} [output] - parsed result (or key fields)
|
|
55
|
+
* @param {{ highlight?: string[] }} [opts] - keys to highlight from output (e.g. ['snippet', 'score', 'path'])
|
|
56
|
+
*/
|
|
57
|
+
function step(label, input, output, opts = {}) {
|
|
58
|
+
if (!VISUAL) return;
|
|
59
|
+
dim(' → ' + label);
|
|
60
|
+
if (input != null) dim(' in: ' + preview(input));
|
|
61
|
+
if (output != null) {
|
|
62
|
+
if (opts.highlight && Array.isArray(output)) {
|
|
63
|
+
output.slice(0, 3).forEach((r, i) => {
|
|
64
|
+
const parts = opts.highlight.map((k) => (r[k] != null ? `${k}=${preview(String(r[k]), 80)}` : '')).filter(Boolean);
|
|
65
|
+
dim(' out[' + i + ']: ' + parts.join(' | '));
|
|
66
|
+
});
|
|
67
|
+
if (output.length > 3) dim(' ... and ' + (output.length - 3) + ' more');
|
|
68
|
+
} else if (typeof output === 'object' && output.results) {
|
|
69
|
+
output.results.slice(0, 3).forEach((r, i) => {
|
|
70
|
+
const parts = ['title', 'snippet', 'score', 'scope'].filter((k) => r[k] != null).map((k) => `${k}=${preview(String(r[k]), 60)}`);
|
|
71
|
+
dim(' result[' + i + ']: ' + parts.join(' | '));
|
|
72
|
+
});
|
|
73
|
+
if (output.results.length > 3) dim(' ... and ' + (output.results.length - 3) + ' more');
|
|
74
|
+
} else {
|
|
75
|
+
dim(' out: ' + preview(output));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Print a single check result.
|
|
82
|
+
* @param {string} label
|
|
83
|
+
* @param {boolean} passed
|
|
84
|
+
* @param {string} [detail] - optional extra info
|
|
85
|
+
*/
|
|
86
|
+
function check(label, passed, detail) {
|
|
87
|
+
if (!VISUAL) return;
|
|
88
|
+
if (passed) green(' ✓ ' + label);
|
|
89
|
+
else red(' ✗ ' + label + (detail ? ': ' + detail : ''));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* End scenario with passed/failed counts.
|
|
94
|
+
*/
|
|
95
|
+
function endScenario(passedCount, failedCount) {
|
|
96
|
+
if (!VISUAL) return;
|
|
97
|
+
if (failedCount === 0) green(' Scenario: ' + passedCount + ' passed');
|
|
98
|
+
else red(' Scenario: ' + passedCount + ' passed, ' + failedCount + ' failed');
|
|
99
|
+
console.log('');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Print final summary (always).
|
|
104
|
+
*/
|
|
105
|
+
function summary(totalPassed, totalFailed) {
|
|
106
|
+
const msg = 'Total: ' + totalPassed + ' passed, ' + totalFailed + ' failed';
|
|
107
|
+
console.log('');
|
|
108
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
109
|
+
if (totalFailed === 0) {
|
|
110
|
+
if (VISUAL) green(msg);
|
|
111
|
+
else console.log(msg);
|
|
112
|
+
} else {
|
|
113
|
+
if (VISUAL) red(msg);
|
|
114
|
+
else console.error(msg);
|
|
115
|
+
}
|
|
116
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export {
|
|
120
|
+
startScenario,
|
|
121
|
+
step,
|
|
122
|
+
check,
|
|
123
|
+
endScenario,
|
|
124
|
+
summary,
|
|
125
|
+
preview,
|
|
126
|
+
VISUAL
|
|
127
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Runs spec/run-tests.js in a subprocess and reports success from stdout.
|
|
4
|
+
* Use this so that a native crash on exit (e.g. from embedding libs) does not fail the test run
|
|
5
|
+
* when all tests have already passed.
|
|
6
|
+
*/
|
|
7
|
+
import { spawn } from 'child_process';
|
|
8
|
+
import { fileURLToPath } from 'url';
|
|
9
|
+
import path from 'path';
|
|
10
|
+
|
|
11
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
12
|
+
const testScript = path.join(__dirname, 'run-tests.js');
|
|
13
|
+
|
|
14
|
+
const child = spawn(process.execPath, [testScript], {
|
|
15
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
16
|
+
cwd: path.dirname(__dirname)
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
let stdout = '';
|
|
20
|
+
let stderr = '';
|
|
21
|
+
child.stdout.setEncoding('utf8');
|
|
22
|
+
child.stderr.setEncoding('utf8');
|
|
23
|
+
child.stdout.on('data', (chunk) => {
|
|
24
|
+
stdout += chunk;
|
|
25
|
+
process.stdout.write(chunk);
|
|
26
|
+
});
|
|
27
|
+
child.stderr.on('data', (chunk) => {
|
|
28
|
+
stderr += chunk;
|
|
29
|
+
process.stderr.write(chunk);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
child.on('close', (code, signal) => {
|
|
33
|
+
const output = stdout + stderr;
|
|
34
|
+
const match = output.match(/(\d+)\s+passed,\s*(\d+)\s+failed/);
|
|
35
|
+
const allPassed = match && parseInt(match[1], 10) > 0 && parseInt(match[2], 10) === 0;
|
|
36
|
+
if (allPassed) {
|
|
37
|
+
process.exit(0);
|
|
38
|
+
}
|
|
39
|
+
process.exit(code !== null && code !== 0 ? code : 1);
|
|
40
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Black-box scenario tests for Mixgram.
|
|
4
|
+
* Run: node spec/run-tests.js
|
|
5
|
+
* Visual mode (narrative + inputs/outputs in console): node spec/run-tests.js --visual
|
|
6
|
+
*/
|
|
7
|
+
import fs from 'fs';
|
|
8
|
+
import path from 'path';
|
|
9
|
+
import os from 'os';
|
|
10
|
+
import { loadConfig } from '../src/config.js';
|
|
11
|
+
import { createToolHandlers } from '../src/mcp/tools.js';
|
|
12
|
+
import { getToolDefinitions } from '../src/mcp/tool-registry.js';
|
|
13
|
+
import { closeDb, getDb } from '../src/db/sqlite.js';
|
|
14
|
+
import { scenarios } from './scenarios/scenarios.js';
|
|
15
|
+
import * as reporter from './reporter.js';
|
|
16
|
+
|
|
17
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mixgram-test-'));
|
|
18
|
+
const config = loadConfig({
|
|
19
|
+
homeMemoryRoot: path.join(tmpDir, 'home', 'memory'),
|
|
20
|
+
projectsRoot: path.join(tmpDir, 'projects'),
|
|
21
|
+
sqlitePath: path.join(tmpDir, '.mixgram', 'index.db')
|
|
22
|
+
});
|
|
23
|
+
fs.mkdirSync(path.join(tmpDir, 'projects', 'castlebravo', 'memory'), { recursive: true });
|
|
24
|
+
fs.mkdirSync(path.dirname(config.sqlitePath), { recursive: true });
|
|
25
|
+
|
|
26
|
+
const h = createToolHandlers(config);
|
|
27
|
+
|
|
28
|
+
// Ensure every tool in the registry has a handler (CLI and MCP both use the same registry)
|
|
29
|
+
const definitions = getToolDefinitions();
|
|
30
|
+
for (const def of definitions) {
|
|
31
|
+
if (!h[def.name]) throw new Error(`Tool "${def.name}" in registry has no handler in createToolHandlers`);
|
|
32
|
+
}
|
|
33
|
+
if (definitions.length === 0) throw new Error('Tool registry is empty');
|
|
34
|
+
|
|
35
|
+
function parse(res) {
|
|
36
|
+
return JSON.parse(res.content[0].text);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let totalPassed = 0;
|
|
40
|
+
let totalFailed = 0;
|
|
41
|
+
|
|
42
|
+
const ctx = {
|
|
43
|
+
h,
|
|
44
|
+
parse,
|
|
45
|
+
config,
|
|
46
|
+
fs: fs,
|
|
47
|
+
reporter,
|
|
48
|
+
shared: {}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
async function runScenarios() {
|
|
52
|
+
for (const scenario of scenarios) {
|
|
53
|
+
if (scenario.name === 'Semantic / embeddings (save and search)' && !ctx.hSemantic) {
|
|
54
|
+
const configSemantic = loadConfig({
|
|
55
|
+
homeMemoryRoot: config.homeMemoryRoot,
|
|
56
|
+
projectsRoot: config.projectsRoot,
|
|
57
|
+
sqlitePath: config.sqlitePath,
|
|
58
|
+
embeddings: { enabled: true }
|
|
59
|
+
});
|
|
60
|
+
ctx.hSemantic = createToolHandlers(configSemantic);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
const { passed, failed } = await scenario.run(ctx);
|
|
65
|
+
totalPassed += passed;
|
|
66
|
+
totalFailed += failed;
|
|
67
|
+
} catch (err) {
|
|
68
|
+
if (reporter.VISUAL) {
|
|
69
|
+
reporter.check(scenario.name + ' run', false, err.message);
|
|
70
|
+
}
|
|
71
|
+
console.error('Scenario "' + scenario.name + '" threw:', err.message);
|
|
72
|
+
totalFailed += 1;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function runLegacySemanticKnn() {
|
|
78
|
+
const configSemantic = ctx.hSemantic
|
|
79
|
+
? loadConfig({
|
|
80
|
+
homeMemoryRoot: config.homeMemoryRoot,
|
|
81
|
+
projectsRoot: config.projectsRoot,
|
|
82
|
+
sqlitePath: config.sqlitePath,
|
|
83
|
+
embeddings: { enabled: true }
|
|
84
|
+
})
|
|
85
|
+
: null;
|
|
86
|
+
if (!configSemantic) return;
|
|
87
|
+
|
|
88
|
+
const { getEmbedder } = await import('../src/core/embeddings/embedder.js');
|
|
89
|
+
const { getVectorStore } = await import('../src/core/embeddings/vectorStore.js');
|
|
90
|
+
const embedder = await getEmbedder(configSemantic);
|
|
91
|
+
const vectorStore = await getVectorStore(configSemantic);
|
|
92
|
+
|
|
93
|
+
if (reporter.VISUAL) {
|
|
94
|
+
reporter.startScenario('Embedder and vector store (KNN)', 'Verify embedder and vectorStore APIs and semantic search for persistence doc.');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
let p = 0, f = 0;
|
|
98
|
+
const ok = (cond, msg) => { if (cond) p++; else f++; if (reporter.VISUAL) reporter.check(msg, cond); };
|
|
99
|
+
|
|
100
|
+
if (embedder) {
|
|
101
|
+
const vec = await embedder.embed('Embedding test phrase.');
|
|
102
|
+
ok(vec instanceof Float32Array && vec.length === embedder.dimensions, 'embedder.embed returns Float32Array of correct dimensions');
|
|
103
|
+
}
|
|
104
|
+
if (vectorStore) {
|
|
105
|
+
ok(typeof vectorStore.insert === 'function' && typeof vectorStore.search === 'function', 'vectorStore has insert and search');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const hSemantic = ctx.hSemantic || createToolHandlers(configSemantic);
|
|
109
|
+
const docPersistence = 'Los datos se guardan en SQLite y la búsqueda es una forma de persistencia.';
|
|
110
|
+
const savePersist = await hSemantic.mem_save({
|
|
111
|
+
title: 'Persistencia y búsqueda',
|
|
112
|
+
type: 'reference',
|
|
113
|
+
scope: 'project',
|
|
114
|
+
project: 'castlebravo',
|
|
115
|
+
topic_key: 'test/persistencia-sqlite',
|
|
116
|
+
content: docPersistence
|
|
117
|
+
});
|
|
118
|
+
const persistId = parse(savePersist).id;
|
|
119
|
+
ok(!!persistId, 'saved persistence doc');
|
|
120
|
+
const db = getDb(configSemantic);
|
|
121
|
+
const chunkRow = db.prepare('SELECT id, document_id, content_hash, content FROM document_chunks WHERE document_id = ? LIMIT 1').get(persistId);
|
|
122
|
+
ok(!!chunkRow, 'chunk exists for persistence doc');
|
|
123
|
+
if (embedder && vectorStore && chunkRow) {
|
|
124
|
+
const embedding = await embedder.embed(chunkRow.content);
|
|
125
|
+
await vectorStore.ensureTables(configSemantic);
|
|
126
|
+
await vectorStore.insert(configSemantic, {
|
|
127
|
+
chunk_id: chunkRow.id,
|
|
128
|
+
document_id: chunkRow.document_id,
|
|
129
|
+
content_hash: chunkRow.content_hash,
|
|
130
|
+
embedding
|
|
131
|
+
});
|
|
132
|
+
const queryEmb = await embedder.embed('dónde se persisten los datos');
|
|
133
|
+
const knnResults = await vectorStore.search(configSemantic, queryEmb, 5, { similarityThreshold: 0.5 });
|
|
134
|
+
ok(knnResults.length >= 1, 'KNN search returns at least one result');
|
|
135
|
+
const found = knnResults.some((r) => r.document_id === persistId);
|
|
136
|
+
ok(found, 'persistence doc found by semantic search');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (reporter.VISUAL) reporter.endScenario(p, f);
|
|
140
|
+
totalPassed += p;
|
|
141
|
+
totalFailed += f;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function run() {
|
|
145
|
+
await runScenarios();
|
|
146
|
+
await runLegacySemanticKnn();
|
|
147
|
+
|
|
148
|
+
closeDb();
|
|
149
|
+
try { fs.rmSync(tmpDir, { recursive: true }); } catch (_) {}
|
|
150
|
+
|
|
151
|
+
reporter.summary(totalPassed, totalFailed);
|
|
152
|
+
process.exitCode = totalFailed > 0 ? 1 : 0;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
run().catch((err) => {
|
|
156
|
+
console.error(err);
|
|
157
|
+
closeDb();
|
|
158
|
+
try { fs.rmSync(tmpDir, { recursive: true }); } catch (_) {}
|
|
159
|
+
process.exitCode = 1;
|
|
160
|
+
});
|