mixgram 1.3.7 → 2.0.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.
Files changed (39) hide show
  1. package/README.md +68 -345
  2. package/bin/mixgram.js +51 -160
  3. package/examples/embedding-demo.js +22 -142
  4. package/package.json +2 -3
  5. package/pnpm-workspace.yaml +2 -2
  6. package/spec/bench-indexing.js +23 -211
  7. package/spec/bench-search.js +26 -212
  8. package/spec/run-tests.js +1 -214
  9. package/spec/scenarios/scenarios.js +277 -607
  10. package/spec/sdd.md +14 -988
  11. package/spec/tdd.md +15 -1232
  12. package/spec/test-large-document.js +20 -91
  13. package/src/config.js +57 -48
  14. package/src/core/documents/documents.js +62 -224
  15. package/src/core/embeddings/embedder.js +14 -54
  16. package/src/core/embeddings/queue.js +33 -106
  17. package/src/core/embeddings/vectorStore.js +30 -111
  18. package/src/core/embeddings/worker-process.js +23 -74
  19. package/src/core/indexing/indexer.js +51 -237
  20. package/src/core/indexing/parser.js +13 -114
  21. package/src/core/indexing/reindex.js +28 -137
  22. package/src/core/search/search.js +64 -158
  23. package/src/db/migrations/001_initial.sql +14 -63
  24. package/src/db/sqlite.js +19 -29
  25. package/src/fs/paths.js +11 -101
  26. package/src/fs/watcher.js +15 -48
  27. package/src/mcp/cli-adapter.js +32 -110
  28. package/src/mcp/server.js +64 -141
  29. package/src/mcp/tool-registry.js +21 -132
  30. package/src/mcp/tools.js +27 -199
  31. package/src/utils/markdown.js +15 -82
  32. package/patches/sharp@0.34.5.patch +0 -13
  33. package/spec/fixtures/realistic-content.js +0 -106
  34. package/spec/reporter.js +0 -127
  35. package/spec/run-tests-runner.js +0 -40
  36. package/src/core/embeddings/worker.js +0 -27
  37. package/src/core/prompts/prompts.js +0 -15
  38. package/src/core/sessions/sessions.js +0 -27
  39. package/src/utils/ids.js +0 -27
package/src/mcp/tools.js CHANGED
@@ -1,206 +1,34 @@
1
- import { slugFromTopicKey } from '../fs/paths.js';
2
- import { saveDocument, updateDocument, deleteDocument, getObservation, getObservationsBySession } from '../core/documents/documents.js';
3
- import { search, getRecentContext } from '../core/search/search.js';
1
+ import { saveDocument, getDocument, deleteDocument } from '../core/documents/documents.js';
2
+ import { search } from '../core/search/search.js';
3
+ import { reindex } from '../core/indexing/reindex.js';
4
4
  import { getDb } from '../db/sqlite.js';
5
- import { startSession, endSession, getSession } from '../core/sessions/sessions.js';
6
- import { savePrompt } from '../core/prompts/prompts.js';
7
- import { fullReindex, incrementalReindex } from '../core/indexing/reindex.js';
8
-
9
- const DOC_TYPES = ['architecture', 'decision', 'bug', 'learning', 'discovery', 'pattern', 'reference', 'session_summary', 'prompt', 'generated_note'];
10
-
11
- function suggestTopicKey(title, content = '', type = 'generated_note') {
12
- const category = type && DOC_TYPES.includes(type) ? type : 'generated_note';
13
- const slug = slugFromTopicKey(title || content.slice(0, 80).trim());
14
- return `${category}/${slug}`;
15
- }
16
-
17
- function createToolHandlers(config) {
18
- return {
19
- mem_save: async (args) => {
20
- const topicKey = args.topic_key ?? suggestTopicKey(args.title, args.content ?? '', args.type || 'generated_note');
21
- const result = saveDocument(config, {
22
- title: args.title,
23
- type: args.type || 'generated_note',
24
- scope: args.scope || 'project',
25
- project: args.project ?? null,
26
- topic_key: topicKey,
27
- content: args.content ?? '',
28
- session_id: args.session_id ?? null,
29
- id: args.id ?? null,
30
- tags: args.tags ?? []
31
- });
32
- return { content: [{ type: 'text', text: JSON.stringify({ success: true, id: result.id, path: result.path, created: result.created }) }] };
33
- },
34
-
35
- mem_update: async (args) => {
36
- const result = updateDocument(config, {
37
- id: args.id,
38
- title: args.title,
39
- content: args.content,
40
- tags: args.tags
41
- });
42
- if (!result) return { content: [{ type: 'text', text: JSON.stringify({ success: false, error: 'Document not found' }) }], isError: true };
43
- return { content: [{ type: 'text', text: JSON.stringify({ success: true, id: result.id }) }] };
44
- },
45
-
46
- mem_delete: async (args) => {
47
- const ok = deleteDocument(config, args.id, { hardDelete: args.hardDelete === true });
48
- if (!ok) return { content: [{ type: 'text', text: JSON.stringify({ success: false, error: 'Document not found' }) }], isError: true };
49
- return { content: [{ type: 'text', text: JSON.stringify({ success: true }) }] };
50
- },
51
-
52
- mem_suggest_topic_key: async (args) => {
53
- const topicKey = suggestTopicKey(args.title, args.content, args.type);
54
- return { content: [{ type: 'text', text: JSON.stringify({ topic_key: topicKey }) }] };
55
- },
56
-
57
- mem_search: async (args) => {
58
- const scopeMode = args.scope_mode || config.search?.defaultScopeMode || 'merged';
59
- const project = args.project ?? null;
60
- const limit = args.limit ?? config.search?.defaultLimit ?? 10;
61
- const query = args.query?.trim();
62
- if (!query) {
63
- return { content: [{ type: 'text', text: JSON.stringify({ results: [] }) }] };
64
- }
65
-
66
- const ftsResults = await search(config, { query, scopeMode, project, limit });
67
- let results = [...ftsResults];
68
-
69
- if (config.embeddings?.enabled && config.getQueryEmbedding) {
70
- try {
71
- const { getVectorStore } = await import('../core/embeddings/vectorStore.js');
72
- const vectorStore = await getVectorStore(config);
73
- if (vectorStore) {
74
- const queryVec = await config.getQueryEmbedding(query);
75
- if (queryVec) {
76
- const semanticThreshold = config.embeddings?.similarityThreshold ?? 0.75;
77
- const vecResults = await vectorStore.search(config, queryVec, Math.max(limit * 2, 20), { similarityThreshold: semanticThreshold });
78
- const db = getDb(config);
79
- const seen = new Set(results.map((r) => r.documentId));
80
- const semanticList = [];
81
- for (const v of vecResults) {
82
- if (seen.has(v.document_id)) continue;
83
- const row = db.prepare(
84
- 'SELECT id, title, topic_key, type, scope, project, created_at, body FROM documents WHERE id = ? AND deleted_at IS NULL'
85
- ).get(v.document_id);
86
- if (!row) continue;
87
- if (scopeMode === 'project-only' && (row.scope !== 'project' || row.project !== project)) continue;
88
- if (scopeMode === 'home-only' && row.scope !== 'home') continue;
89
- if (scopeMode === 'merged' && project && row.scope === 'project' && row.project !== project) continue;
90
- seen.add(v.document_id);
91
- const snippet = (row.body || '').slice(0, 200).trim();
92
- semanticList.push({
93
- documentId: row.id,
94
- title: row.title,
95
- topicKey: row.topic_key,
96
- type: row.type,
97
- scope: row.scope,
98
- project: row.project,
99
- created: row.created_at || null,
100
- snippet: snippet || '',
101
- score: 1 - v.distance
102
- });
103
- }
104
- results = [...semanticList, ...results].slice(0, limit);
105
- }
106
- }
107
- } catch (_) {}
108
- }
109
-
110
- return { content: [{ type: 'text', text: JSON.stringify({ results }) }] };
111
- },
112
-
113
- mem_context: async (args) => {
114
- const q = (args.query || '').trim();
115
- const results = q && q !== '*'
116
- ? await search(config, {
117
- query: q,
118
- scopeMode: 'merged',
119
- project: args.project ?? null,
120
- limit: args.limit ?? config.search?.defaultLimit ?? 10
121
- })
122
- : getRecentContext(config, { project: args.project ?? null, limit: args.limit ?? config.search?.defaultLimit ?? 10 });
123
- const context = results.map((r) => `[${r.title}] ${r.snippet}`).join('\n\n');
124
- return { content: [{ type: 'text', text: context || '(no context)' }] };
125
- },
126
-
127
- mem_get_observation: async (args) => {
128
- const obs = getObservation(config, args.id);
129
- if (!obs) return { content: [{ type: 'text', text: JSON.stringify({ error: 'Observation not found' }) }], isError: true };
130
- return { content: [{ type: 'text', text: JSON.stringify(obs) }] };
131
- },
132
-
133
- mem_stats: async () => {
5
+ import { validateToolArgs } from './tool-registry.js';
6
+
7
+ export function createToolHandlers(config) {
8
+ const operations = {
9
+ mem_save: args => saveDocument(config, args),
10
+ mem_search: args => search(config, args),
11
+ mem_get: args => getDocument(config, args.id),
12
+ mem_delete: args => deleteDocument(config, args.id, args),
13
+ mem_reindex: args => reindex(config, args),
14
+ mem_stats: () => {
134
15
  const db = getDb(config);
135
- const docs = db.prepare("SELECT COUNT(*) AS c FROM documents WHERE deleted_at IS NULL").get();
136
- const sessions = db.prepare('SELECT COUNT(*) AS c FROM sessions').get();
137
- const prompts = db.prepare('SELECT COUNT(*) AS c FROM prompts').get();
138
16
  return {
139
- content: [{
140
- type: 'text',
141
- text: JSON.stringify({
142
- documents: docs?.c ?? 0,
143
- sessions: sessions?.c ?? 0,
144
- prompts: prompts?.c ?? 0,
145
- embeddings_enabled: config.embeddings?.enabled === true
146
- })
147
- }]
17
+ documents: db.prepare('SELECT COUNT(*) AS count FROM documents WHERE deleted_at IS NULL').get().count,
18
+ collections: db.prepare('SELECT collection,COUNT(*) AS count FROM documents WHERE deleted_at IS NULL GROUP BY collection').all(),
19
+ index: { path: config.sqlitePath, deleted: db.prepare('SELECT COUNT(*) AS count FROM documents WHERE deleted_at IS NOT NULL').get().count },
20
+ embeddings: { enabled: config.embeddings.enabled, workerAvailable: !!config.getQueryEmbedding,
21
+ jobs: db.prepare('SELECT status,COUNT(*) AS count FROM embedding_jobs GROUP BY status').all() }
148
22
  };
149
- },
150
-
151
- mem_timeline: async (args) => {
152
- const focusId = args.observation_id;
153
- const obs = getObservation(config, focusId);
154
- if (!obs) return { content: [{ type: 'text', text: JSON.stringify({ error: 'Observation not found' }) }], isError: true };
155
- const sessionId = obs.session_id;
156
- if (!sessionId) {
157
- return { content: [{ type: 'text', text: JSON.stringify({ before: [], focus: obs, after: [] }) }] };
158
- }
159
- const ids = getObservationsBySession(config, sessionId);
160
- const focusIndex = ids.indexOf(focusId);
161
- const before = focusIndex <= 0 ? [] : ids.slice(0, focusIndex).map((id) => getObservation(config, id)).filter(Boolean);
162
- const after = focusIndex < 0 || focusIndex >= ids.length - 1 ? [] : ids.slice(focusIndex + 1).map((id) => getObservation(config, id)).filter(Boolean);
163
- return { content: [{ type: 'text', text: JSON.stringify({ before, focus: obs, after }) }] };
164
- },
165
-
166
- mem_session_start: async (args) => {
167
- const result = startSession(config, { id: args.session_id ?? null, project: args.project ?? '', directory: args.directory ?? null });
168
- return { content: [{ type: 'text', text: JSON.stringify({ success: true, session_id: result.id, started_at: result.started_at }) }] };
169
- },
170
-
171
- mem_session_end: async (args) => {
172
- const result = endSession(config, args.session_id);
173
- return { content: [{ type: 'text', text: JSON.stringify({ success: result.success, ended_at: result.ended_at }) }] };
174
- },
175
-
176
- mem_session_summary: async (args) => {
177
- const session = getSession(config, args.session_id);
178
- if (!session) return { content: [{ type: 'text', text: JSON.stringify({ success: false, error: 'Session not found' }) }], isError: true };
179
- const summaryContent = args.content ?? args.summary ?? '';
180
- saveDocument(config, {
181
- title: args.title ?? `Session summary ${args.session_id}`,
182
- type: 'session_summary',
183
- scope: 'project',
184
- project: session.project || null,
185
- topic_key: `sessions/summary-${args.session_id}`,
186
- content: summaryContent,
187
- session_id: args.session_id
188
- });
189
- getDb(config).prepare('UPDATE sessions SET summary = ? WHERE id = ?').run(summaryContent.slice(0, 2000), args.session_id);
190
- return { content: [{ type: 'text', text: JSON.stringify({ success: true }) }] };
191
- },
192
-
193
- mem_save_prompt: async (args) => {
194
- const result = savePrompt(config, { session_id: args.session_id, project: args.project ?? null, content: args.content ?? '' });
195
- return { content: [{ type: 'text', text: JSON.stringify({ success: true, prompt_id: result.id }) }] };
196
- },
197
-
198
- mem_reindex: async (args) => {
199
- const full = args.full === true;
200
- const result = full ? fullReindex(config) : incrementalReindex(config);
201
- return { content: [{ type: 'text', text: JSON.stringify(result) }] };
202
23
  }
203
24
  };
25
+ return Object.fromEntries(Object.entries(operations).map(([name, operation]) => [name, async (args = {}) => {
26
+ try {
27
+ const value = await operation(validateToolArgs(name, args));
28
+ const isError = value.indexed === false || value.errors?.length > 0;
29
+ return { content: [{ type: 'text', text: JSON.stringify(value) }], ...(isError ? { isError: true } : {}) };
30
+ } catch (error) {
31
+ return { content: [{ type: 'text', text: JSON.stringify({ error: error.message }) }], isError: true };
32
+ }
33
+ }]));
204
34
  }
205
-
206
- export { suggestTopicKey, createToolHandlers };
@@ -1,90 +1,23 @@
1
1
  import matter from 'gray-matter';
2
-
3
- export const FRONTMATTER_KEYS = [
4
- 'id', 'title', 'type', 'scope', 'project', 'topic_key', 'session_id', 'tool_name',
5
- 'created_at', 'updated_at', 'revision_count', 'duplicate_count', 'deleted', 'tags',
6
- 'embedding_status', 'indexed_at', 'deleted_at'
7
- ];
8
-
9
- /** Keys written to .md: id, type, title, created (cosmetic). scope/project stay in DB only; reindex infers scope from path. */
10
- const DISPLAY_FRONTMATTER_KEYS = ['id', 'type', 'title', 'created'];
11
-
12
- function formatCreated(createdAt) {
13
- if (!createdAt) return null;
14
- const d = new Date(createdAt);
15
- if (Number.isNaN(d.getTime())) return createdAt;
16
- const y = d.getFullYear();
17
- const m = String(d.getMonth() + 1).padStart(2, '0');
18
- const day = String(d.getDate()).padStart(2, '0');
19
- const h = String(d.getHours()).padStart(2, '0');
20
- const min = String(d.getMinutes()).padStart(2, '0');
21
- const s = String(d.getSeconds()).padStart(2, '0');
22
- return `${y}-${m}-${day} ${h}:${min}:${s}`;
23
- }
24
-
25
- function parseCreated(value) {
26
- if (!value) return null;
27
- if (typeof value === 'string' && /^\d{4}-\d{2}-\d{2}T/.test(value)) return value;
28
- const d = new Date(value);
29
- return Number.isNaN(d.getTime()) ? null : d.toISOString();
30
- }
31
-
32
- function serializeFrontmatter(fm) {
33
- const obj = {};
34
- for (const k of FRONTMATTER_KEYS) {
35
- if (fm[k] !== undefined && fm[k] !== null && fm[k] !== '') {
36
- obj[k] = fm[k];
37
- }
38
- }
39
- return obj;
40
- }
41
-
42
- /** Minimal frontmatter for .md (cosmetic only; deleted flag for soft-delete visibility). */
43
- function serializeDisplayFrontmatter(fm) {
44
- const obj = {};
45
- for (const k of DISPLAY_FRONTMATTER_KEYS) {
46
- if (k === 'created') {
47
- const raw = fm.created_at ?? (fm.created ? parseCreated(fm.created) : null);
48
- const v = raw ? formatCreated(raw) : null;
49
- if (v) obj[k] = v;
50
- } else if (fm[k] !== undefined && fm[k] !== null && fm[k] !== '') {
51
- obj[k] = fm[k];
52
- }
53
- }
54
- if (fm.deleted_at) obj.deleted_at = fm.deleted_at;
55
- if (fm.deleted === true) obj.deleted = true;
56
- return obj;
57
- }
2
+ import { z } from 'zod';
3
+
4
+ const metadata = z.object({
5
+ id: z.string().min(1).optional(),
6
+ title: z.string().optional(),
7
+ type: z.string().min(1).optional(),
8
+ key: z.string().min(1).nullable().optional(),
9
+ tags: z.array(z.string().min(1)).optional(),
10
+ created_at: z.string().datetime().optional(),
11
+ updated_at: z.string().datetime().optional(),
12
+ deleted_at: z.string().datetime().nullable().optional()
13
+ }).strict();
58
14
 
59
15
  export function parse(content) {
60
16
  const parsed = matter(content);
61
- const data = parsed.data || {};
62
- const createdAt = data.created_at ?? parseCreated(data.created);
63
- return {
64
- frontmatter: {
65
- id: data.id,
66
- title: data.title,
67
- type: data.type || 'generated_note',
68
- scope: data.scope || 'project',
69
- project: data.project,
70
- topic_key: data.topic_key,
71
- session_id: data.session_id,
72
- tool_name: data.tool_name,
73
- created_at: createdAt,
74
- updated_at: data.updated_at,
75
- revision_count: data.revision_count ?? 1,
76
- duplicate_count: data.duplicate_count ?? 0,
77
- deleted: data.deleted === true,
78
- tags: Array.isArray(data.tags) ? data.tags : [],
79
- embedding_status: data.embedding_status || 'disabled',
80
- indexed_at: data.indexed_at,
81
- deleted_at: data.deleted_at
82
- },
83
- body: parsed.content ? parsed.content.trim() : ''
84
- };
17
+ return { frontmatter: metadata.parse(parsed.data), body: parsed.content };
85
18
  }
86
19
 
87
20
  export function toMarkdown(frontmatter, body) {
88
- const fm = serializeDisplayFrontmatter(frontmatter);
89
- return matter.stringify(body || '', fm, { lineWidth: -1 });
21
+ const header = matter.stringify('', metadata.parse(frontmatter), { lineWidth: -1 });
22
+ return header.slice(0, -1) + body;
90
23
  }
@@ -1,13 +0,0 @@
1
- diff --git a/package.json b/package.json
2
- index 0c0d009888ec50a444c930c31da0b57160739591..9dbf4c35e01e2855f2f30b573288e61bedf0ae07 100644
3
- --- a/package.json
4
- +++ b/package.json
5
- @@ -93,7 +93,7 @@
6
- ],
7
- "scripts": {
8
- "build": "node install/build.js",
9
- - "install": "node install/check.js || npm run build",
10
- + "install": "SHARP_IGNORE_GLOBAL_LIBVIPS=true node install/check.js || npm run build",
11
- "clean": "rm -rf src/build/ .nyc_output/ coverage/ test/fixtures/output.*",
12
- "test": "npm run lint && npm run test-unit",
13
- "lint": "npm run lint-cpp && npm run lint-js && npm run lint-types",
@@ -1,106 +0,0 @@
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
- };
98
-
99
- /** Used to test FTS5 OR query: save then search with "mcp OR \"model context protocol\"" */
100
- export const noteMcpArchitecture = {
101
- title: 'Other note',
102
- type: 'architecture',
103
- content: 'Model Context Protocol',
104
- scope: 'project',
105
- project: null
106
- };
package/spec/reporter.js DELETED
@@ -1,127 +0,0 @@
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
- };
@@ -1,40 +0,0 @@
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
- });
@@ -1,27 +0,0 @@
1
- /**
2
- * Background worker that processes embedding jobs when embeddings are enabled.
3
- */
4
- import { processNextJob } from './queue.js';
5
-
6
- const DEFAULT_POLL_MS = 2000;
7
- let intervalId = null;
8
-
9
- function startWorker(config) {
10
- if (!config?.embeddings?.enabled || intervalId != null) return;
11
- const pollMs = config.embeddings?.workerPollMs ?? DEFAULT_POLL_MS;
12
- intervalId = setInterval(async () => {
13
- try {
14
- const processed = await processNextJob(config);
15
- if (!processed) return;
16
- } catch (_) {}
17
- }, pollMs);
18
- }
19
-
20
- function stopWorker() {
21
- if (intervalId != null) {
22
- clearInterval(intervalId);
23
- intervalId = null;
24
- }
25
- }
26
-
27
- export { startWorker, stopWorker };
@@ -1,15 +0,0 @@
1
- import { getDb } from '../../db/sqlite.js';
2
- import { promptId } from '../../utils/ids.js';
3
-
4
- function savePrompt(config, options) {
5
- const { session_id, project = null, content } = options;
6
- const id = options.id || promptId();
7
- const db = getDb(config);
8
- const now = new Date().toISOString();
9
- db.prepare(
10
- 'INSERT INTO prompts (id, session_id, project, content, created_at) VALUES (?, ?, ?, ?, ?)'
11
- ).run(id, session_id, project, content ?? '', now);
12
- return { id, created_at: now };
13
- }
14
-
15
- export { savePrompt };