sensemaking 0.11.4 → 0.12.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/README.md +4 -3
- package/bin/cli.js +5 -2
- package/dist/cjs/cli/index.d.cts +3 -3
- package/dist/cjs/cli/index.d.ts +3 -3
- package/dist/cjs/cli/index.js +3 -3
- package/dist/cjs/cli/index.js.map +1 -1
- package/dist/cjs/cli/named.js +2 -2
- package/dist/cjs/cli/named.js.map +1 -1
- package/dist/cjs/cli/related.js +1 -1
- package/dist/cjs/cli/related.js.map +1 -1
- package/dist/cjs/cli/search.js +1 -1
- package/dist/cjs/cli/search.js.map +1 -1
- package/dist/cjs/cli/shared.d.cts +3 -1
- package/dist/cjs/cli/shared.d.ts +3 -1
- package/dist/cjs/cli/shared.js +35 -5
- package/dist/cjs/cli/shared.js.map +1 -1
- package/dist/cjs/cli/sql.js +1 -1
- package/dist/cjs/cli/sql.js.map +1 -1
- package/dist/cjs/cli.js +14 -1
- package/dist/cjs/cli.js.map +1 -1
- package/dist/cjs/commands.js +19 -5
- package/dist/cjs/commands.js.map +1 -1
- package/dist/cjs/config.d.cts +4 -0
- package/dist/cjs/config.d.ts +4 -0
- package/dist/cjs/config.js +39 -4
- package/dist/cjs/config.js.map +1 -1
- package/dist/cjs/db.d.cts +1 -1
- package/dist/cjs/db.d.ts +1 -1
- package/dist/cjs/db.js +158 -14
- package/dist/cjs/db.js.map +1 -1
- package/dist/cjs/output.d.cts +4 -1
- package/dist/cjs/output.d.ts +4 -1
- package/dist/cjs/output.js +158 -6
- package/dist/cjs/output.js.map +1 -1
- package/dist/cjs/segment.d.cts +2 -0
- package/dist/cjs/segment.d.ts +2 -0
- package/dist/cjs/segment.js +98 -0
- package/dist/cjs/segment.js.map +1 -0
- package/dist/esm/cli/index.d.ts +3 -3
- package/dist/esm/cli/index.js +3 -3
- package/dist/esm/cli/index.js.map +1 -1
- package/dist/esm/cli/named.js +3 -3
- package/dist/esm/cli/named.js.map +1 -1
- package/dist/esm/cli/related.js +2 -2
- package/dist/esm/cli/related.js.map +1 -1
- package/dist/esm/cli/search.js +2 -2
- package/dist/esm/cli/search.js.map +1 -1
- package/dist/esm/cli/shared.d.ts +3 -1
- package/dist/esm/cli/shared.js +32 -5
- package/dist/esm/cli/shared.js.map +1 -1
- package/dist/esm/cli/sql.js +2 -2
- package/dist/esm/cli/sql.js.map +1 -1
- package/dist/esm/cli.js +14 -1
- package/dist/esm/cli.js.map +1 -1
- package/dist/esm/commands.js +19 -5
- package/dist/esm/commands.js.map +1 -1
- package/dist/esm/config.d.ts +4 -0
- package/dist/esm/config.js +37 -4
- package/dist/esm/config.js.map +1 -1
- package/dist/esm/db.d.ts +1 -1
- package/dist/esm/db.js +140 -15
- package/dist/esm/db.js.map +1 -1
- package/dist/esm/output.d.ts +4 -1
- package/dist/esm/output.js +104 -4
- package/dist/esm/output.js.map +1 -1
- package/dist/esm/segment.d.ts +2 -0
- package/dist/esm/segment.js +65 -0
- package/dist/esm/segment.js.map +1 -0
- package/package.json +4 -1
- package/schema.json +12 -1
- package/skills/sense/SKILL.md +7 -6
- package/skills/sense-setup/SKILL.md +1 -0
package/dist/esm/db.js
CHANGED
|
@@ -3,11 +3,12 @@
|
|
|
3
3
|
import { mkdirSync, rmSync } from 'node:fs';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
5
|
import { DatabaseSync } from 'node:sqlite';
|
|
6
|
-
import { featureSignature, STATE_DIR } from './config.js';
|
|
6
|
+
import { contentTokenize, featureSignature, STATE_DIR } from './config.js';
|
|
7
7
|
import { SenseError } from './errors.js';
|
|
8
8
|
import { activeFeatures } from './features/index.js';
|
|
9
9
|
import { progress } from './progress.js';
|
|
10
10
|
import { listFiles, parseFile, RESERVED_COLUMNS } from './scan.js';
|
|
11
|
+
import { segmentField, segmentMatch } from './segment.js';
|
|
11
12
|
// Feature-owned columns (`_rank`) must stay out of the upsert: a reparse would null the last
|
|
12
13
|
// computed value on every touch, not just the reconciles that recompute it.
|
|
13
14
|
const CORE_FRONTMATTER_COLUMNS = new Set([
|
|
@@ -21,14 +22,14 @@ const CORE_FRONTMATTER_COLUMNS = new Set([
|
|
|
21
22
|
export const DB_FILENAME = 'cache.db';
|
|
22
23
|
// Cache shape version, independent of the config's own `version`. Bumping it rebuilds
|
|
23
24
|
// existing trees on first query.
|
|
24
|
-
export const SCHEMA_VERSION = '
|
|
25
|
+
export const SCHEMA_VERSION = '12';
|
|
25
26
|
// SQLite's compile-time SQLITE_MAX_COLUMN, default 2000 (https://www.sqlite.org/limits.html).
|
|
26
27
|
const MAX_FRONTMATTER_COLUMNS = 2000;
|
|
27
28
|
function quoteIdent(name) {
|
|
28
29
|
return `"${name.split('"').join('""')}"`;
|
|
29
30
|
}
|
|
30
31
|
// has(field, value): JSON-array field -> membership, string field -> substring, NULL -> false.
|
|
31
|
-
function registerFunctions(db) {
|
|
32
|
+
function registerFunctions(db, segmenting) {
|
|
32
33
|
db.function('has', {
|
|
33
34
|
deterministic: true,
|
|
34
35
|
varargs: false
|
|
@@ -48,16 +49,64 @@ function registerFunctions(db) {
|
|
|
48
49
|
}
|
|
49
50
|
return String(field).includes(needle) ? 1 : 0;
|
|
50
51
|
});
|
|
52
|
+
// Raw `content MATCH '<unspaced text>'` cannot be rewritten behind the author's back, and
|
|
53
|
+
// matches nothing, so the same transform is available to hand-written SQL by name. `segmenting`
|
|
54
|
+
// is the same config predicate commands.ts's search() gates on (contentTokenize(cfg) ===
|
|
55
|
+
// undefined), passed in by open() rather than read back from persisted state.
|
|
56
|
+
db.function('segment', {
|
|
57
|
+
deterministic: true,
|
|
58
|
+
varargs: false
|
|
59
|
+
}, (terms)=>{
|
|
60
|
+
const text = terms === null || terms === undefined ? '' : String(terms);
|
|
61
|
+
return segmenting ? segmentMatch(text) : text;
|
|
62
|
+
});
|
|
51
63
|
}
|
|
52
64
|
function getColumns(db) {
|
|
53
65
|
const rows = db.prepare('PRAGMA table_info(frontmatter)').all();
|
|
54
66
|
return new Set(rows.map((r)=>r.name));
|
|
55
67
|
}
|
|
68
|
+
// Stemming is English-only, but the segmentation underneath it is what decides coverage:
|
|
69
|
+
// unicode61 splits on spaces, so a language written without them (Chinese, Japanese, Thai)
|
|
70
|
+
// indexes a whole run as one token and word search finds nothing. `content.tokenize` is how
|
|
71
|
+
// such a tree picks trigram instead.
|
|
72
|
+
const DEFAULT_TOKENIZE = 'porter unicode61';
|
|
73
|
+
// FTS5 takes its tokenizer as a string literal inside DDL, where nothing can bind, so a
|
|
74
|
+
// configured value has to be concatenated. Probing a throwaway table is what makes that safe
|
|
75
|
+
// and is also the whole validation: anything the linked SQLite accepts passes, anything else
|
|
76
|
+
// fails here with SQLite's own message rather than against the real table. It means no table
|
|
77
|
+
// of which version added which tokenizer has to be maintained.
|
|
78
|
+
function resolveTokenize(db, cfg) {
|
|
79
|
+
const configured = contentTokenize(cfg);
|
|
80
|
+
if (configured === undefined) return DEFAULT_TOKENIZE;
|
|
81
|
+
const literal = configured.replace(/'/g, "''");
|
|
82
|
+
try {
|
|
83
|
+
db.exec('DROP TABLE IF EXISTS temp.sense_tokenize_probe');
|
|
84
|
+
db.exec(`CREATE VIRTUAL TABLE temp.sense_tokenize_probe USING fts5(x, tokenize = '${literal}')`);
|
|
85
|
+
db.exec('DROP TABLE IF EXISTS temp.sense_tokenize_probe');
|
|
86
|
+
} catch (err) {
|
|
87
|
+
throw new SenseError('CONFIG_INVALID', `content.tokenize "${configured}" is not a tokenizer this SQLite accepts (${err.message}); the built-in choices are unicode61, ascii, porter, and trigram, each with their own options`);
|
|
88
|
+
}
|
|
89
|
+
return literal;
|
|
90
|
+
}
|
|
91
|
+
// The tokenizer the content table was actually built with, from its own DDL -- the one
|
|
92
|
+
// record that cannot desynchronize from the table. NULL when the table does not exist yet.
|
|
93
|
+
function storedTokenize(db) {
|
|
94
|
+
const row = db.prepare(`SELECT sql FROM sqlite_master WHERE name = 'content'`).get();
|
|
95
|
+
if (!row) return null;
|
|
96
|
+
const m = row.sql.match(/tokenize = '((?:[^']|'')*)'/);
|
|
97
|
+
return m ? m[1] : null;
|
|
98
|
+
}
|
|
56
99
|
// Content is a separate table (not a column on frontmatter) so `SELECT * FROM frontmatter`
|
|
57
100
|
// can't dump file text into context. Features add their own tables after the core ones.
|
|
58
|
-
function ensureSchema(db, cfg) {
|
|
101
|
+
function ensureSchema(db, cfg, tokenize) {
|
|
59
102
|
db.exec(`CREATE TABLE IF NOT EXISTS frontmatter ("path" TEXT PRIMARY KEY, "_mtime" REAL, "_size" INTEGER, "_parse_error" TEXT)`);
|
|
60
|
-
|
|
103
|
+
// IF NOT EXISTS is safe against a tokenizer change: open() compares the table's own DDL
|
|
104
|
+
// against the resolved tokenizer before this runs, so a stale table is already gone by now.
|
|
105
|
+
// The three `_seg` sidecars are appended after path, never inserted: bm25(content, ...) and
|
|
106
|
+
// snippet(content, 2, ...) are documented against the first three columns and keep working
|
|
107
|
+
// (FTS5 defaults the weights it was not given). Each carries its field's exploded unspaced
|
|
108
|
+
// runs for text that needs it, and an empty string for text that does not.
|
|
109
|
+
db.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS content USING fts5(title, summary, text, path UNINDEXED, title_seg, summary_seg, text_seg, tokenize = '${tokenize}')`);
|
|
61
110
|
// Coverage, not ownership: a path can appear under several presets. path leads the PK so the
|
|
62
111
|
// per-doc delete is an index hit -- keyed the other way, cold builds went quadratic.
|
|
63
112
|
db.exec(`CREATE TABLE IF NOT EXISTS preset_files ("path" TEXT, preset TEXT, PRIMARY KEY ("path", preset))`);
|
|
@@ -77,6 +126,14 @@ export function setMeta(db, key, value) {
|
|
|
77
126
|
}
|
|
78
127
|
db.prepare('INSERT INTO meta (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value').run(key, value);
|
|
79
128
|
}
|
|
129
|
+
// Shared by reconcile() and the tokenize-only rebuild in open(), so both prepare the same
|
|
130
|
+
// literal instead of two copies drifting apart.
|
|
131
|
+
const INSERT_CONTENT_SQL = `INSERT INTO content (rowid, title, summary, text, "path", title_seg, summary_seg, text_seg) VALUES ((SELECT rowid FROM frontmatter WHERE "path" = ?), ?, ?, ?, ?, ?, ?, ?)`;
|
|
132
|
+
// Shared by reconcile() and the tokenize-only rebuild in open(), so content population is
|
|
133
|
+
// defined once. Assumes the frontmatter row for doc.relPath already exists (rowid lookup).
|
|
134
|
+
function insertContentRow(insertBody, doc, segmenting) {
|
|
135
|
+
insertBody.run(doc.relPath, doc.search.title, doc.search.summary, doc.search.text, doc.relPath, segmenting ? segmentField(doc.search.title) : '', segmenting ? segmentField(doc.search.summary) : '', segmenting ? segmentField(doc.search.text) : '');
|
|
136
|
+
}
|
|
80
137
|
export function docCount(db) {
|
|
81
138
|
const row = db.prepare('SELECT COUNT(*) AS n FROM frontmatter').get();
|
|
82
139
|
return row.n;
|
|
@@ -169,7 +226,10 @@ export function reconcile(db, cfg, baseDir) {
|
|
|
169
226
|
}
|
|
170
227
|
if (parsedDocs.length > 0) {
|
|
171
228
|
const insert = db.prepare(insertSql);
|
|
172
|
-
const insertBody = db.prepare(
|
|
229
|
+
const insertBody = db.prepare(INSERT_CONTENT_SQL);
|
|
230
|
+
// A non-default tokenizer means the tree has chosen its own scheme; a phrase query over
|
|
231
|
+
// grapheme runs would be nonsense against trigram, so the sidecars stay empty.
|
|
232
|
+
const segmenting = contentTokenize(cfg) === undefined;
|
|
173
233
|
for (const doc of parsedDocs){
|
|
174
234
|
var _feature_store;
|
|
175
235
|
const values = writableColumns.map((col)=>{
|
|
@@ -189,7 +249,7 @@ export function reconcile(db, cfg, baseDir) {
|
|
|
189
249
|
delBody.run(doc.relPath);
|
|
190
250
|
for (const feature of features)(_feature_remove1 = feature.remove) === null || _feature_remove1 === void 0 ? void 0 : _feature_remove1.call(feature, db, doc.relPath, delta);
|
|
191
251
|
}
|
|
192
|
-
insertBody
|
|
252
|
+
insertContentRow(insertBody, doc, segmenting);
|
|
193
253
|
// A preset edit forces a full rebuild, so an unchanged doc's coverage is already
|
|
194
254
|
// correct; new docs have nothing to clear, which keeps cold builds linear.
|
|
195
255
|
if (existing.has(doc.relPath)) delPresetFiles.run(doc.relPath);
|
|
@@ -216,10 +276,9 @@ export function reconcile(db, cfg, baseDir) {
|
|
|
216
276
|
warnings
|
|
217
277
|
};
|
|
218
278
|
}
|
|
219
|
-
//
|
|
220
|
-
// global features, embed provider, then one segment per preset)
|
|
221
|
-
function
|
|
222
|
-
// Segment keys: `features`, `embed`, `preset:<name>` (config.featureSignature's format).
|
|
279
|
+
// Segment keys that moved between two feature signatures (see config.featureSignature's
|
|
280
|
+
// format: global features, embed provider, tokenize, then one segment per preset).
|
|
281
|
+
function changedSignatureKeys(before, after) {
|
|
223
282
|
const keyOf = (part)=>part.startsWith('preset:') ? part.split(':').slice(0, 2).join(':') : part.split(':')[0];
|
|
224
283
|
const parse = (sig)=>new Map(sig.split('|').map((part)=>[
|
|
225
284
|
keyOf(part),
|
|
@@ -230,11 +289,42 @@ function signatureDiff(before, after) {
|
|
|
230
289
|
const changed = new Set();
|
|
231
290
|
for (const [key, val] of b)if (a.get(key) !== val) changed.add(key);
|
|
232
291
|
for (const key of a.keys())if (!b.has(key)) changed.add(key);
|
|
233
|
-
|
|
292
|
+
return changed;
|
|
293
|
+
}
|
|
294
|
+
// Names what moved, for the rebuild notice.
|
|
295
|
+
function signatureDiff(before, after) {
|
|
296
|
+
const changed = changedSignatureKeys(before, after);
|
|
297
|
+
const label = (key)=>key === 'embed' ? 'embed settings' : key === 'tokenize' ? 'content tokenizer' : key.startsWith('preset:') ? `preset "${key.slice(7)}"` : 'features';
|
|
234
298
|
return changed.size === 0 ? 'features' : [
|
|
235
299
|
...changed
|
|
236
300
|
].map(label).join(', ');
|
|
237
301
|
}
|
|
302
|
+
// The tokenize-only rebuild in open(): content is dropped and repopulated from files already
|
|
303
|
+
// listed in frontmatter, which itself is untouched. Frontmatter, links, sections, and
|
|
304
|
+
// embeddings are file-derived and tokenizer-independent, so they survive. No feature extractors
|
|
305
|
+
// run here -- doc.search (title/summary/text) is all content population needs. Returns
|
|
306
|
+
// parseFile's per-file warnings (e.g. a bad date) so open() can surface them -- mtimes are
|
|
307
|
+
// untouched, so reconcile() never reparses these files and would otherwise never emit them again.
|
|
308
|
+
function rebuildContentTable(db, cfg, baseDir) {
|
|
309
|
+
const known = new Set(db.prepare('SELECT "path" FROM frontmatter').all().map((r)=>r.path));
|
|
310
|
+
const files = listFiles(cfg, baseDir).filter((f)=>known.has(f.relPath));
|
|
311
|
+
const segmenting = contentTokenize(cfg) === undefined;
|
|
312
|
+
const insertBody = db.prepare(INSERT_CONTENT_SQL);
|
|
313
|
+
const warnings = [];
|
|
314
|
+
db.exec('BEGIN');
|
|
315
|
+
try {
|
|
316
|
+
for (const file of files){
|
|
317
|
+
const { doc, warnings: fileWarnings } = parseFile(file);
|
|
318
|
+
warnings.push(...fileWarnings);
|
|
319
|
+
insertContentRow(insertBody, doc, segmenting);
|
|
320
|
+
}
|
|
321
|
+
db.exec('COMMIT');
|
|
322
|
+
} catch (err) {
|
|
323
|
+
db.exec('ROLLBACK');
|
|
324
|
+
throw err;
|
|
325
|
+
}
|
|
326
|
+
return warnings;
|
|
327
|
+
}
|
|
238
328
|
export function open(cfg) {
|
|
239
329
|
var _getMeta;
|
|
240
330
|
const stateDir = join(cfg.baseDir, STATE_DIR);
|
|
@@ -247,27 +337,59 @@ export function open(cfg) {
|
|
|
247
337
|
// Covers a concurrent watcher's bulk reconcile (~5s for 500 files at 26k notes). A query
|
|
248
338
|
// that outwaits it still fails loudly.
|
|
249
339
|
db.exec('PRAGMA busy_timeout = 30000');
|
|
250
|
-
registerFunctions(db);
|
|
340
|
+
registerFunctions(db, contentTokenize(cfg) === undefined);
|
|
251
341
|
db.exec('CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT)');
|
|
342
|
+
// Before the rebuild branch below, never after: that branch deletes the cache, so a typo'd
|
|
343
|
+
// tokenizer validated later would cost a full re-index (and re-embed) to reach its own error.
|
|
344
|
+
const tokenize = resolveTokenize(db, cfg);
|
|
252
345
|
// Schema-version or feature-set mismatch: reconcile only reparses changed files, so an
|
|
253
346
|
// old cache can't be patched incrementally -- rebuild instead (cheap: nothing expensive lives here).
|
|
254
347
|
const version = getMeta(db, 'schema_version');
|
|
255
348
|
const features = getMeta(db, 'features');
|
|
256
349
|
const wantFeatures = featureSignature(cfg);
|
|
350
|
+
let tokenizeOnlyRebuild = false;
|
|
257
351
|
if (version !== null && version !== SCHEMA_VERSION || features !== null && features !== wantFeatures) {
|
|
258
352
|
// Indexing derives from presets, so a config edit rebuilding the cache must say so and
|
|
259
353
|
// name what changed -- silent rebuilds make derived indexing look like a hang or a bug.
|
|
260
354
|
if (version !== null && version !== SCHEMA_VERSION) {
|
|
261
355
|
console.error('sense: cache format changed (new sensemaking version); rebuilding the index');
|
|
356
|
+
db.close();
|
|
357
|
+
clearCache(cfg);
|
|
358
|
+
return open(cfg);
|
|
359
|
+
}
|
|
360
|
+
const changedKeys = changedSignatureKeys(features !== null && features !== void 0 ? features : '', wantFeatures);
|
|
361
|
+
// Only the tokenizer moved: frontmatter, links, sections, and embeddings are file-derived
|
|
362
|
+
// and tokenizer-independent, so they don't need re-deriving. Everything else -- a preset
|
|
363
|
+
// edit, an embed model change -- still takes the full clear/reopen below.
|
|
364
|
+
if (changedKeys.size === 1 && changedKeys.has('tokenize')) {
|
|
365
|
+
console.error('sense: config change (content tokenizer) rebuilds the text index; vectors, links, and sections are kept');
|
|
366
|
+
db.exec('DROP TABLE content');
|
|
367
|
+
tokenizeOnlyRebuild = true;
|
|
262
368
|
} else {
|
|
263
369
|
const changed = signatureDiff(features !== null && features !== void 0 ? features : '', wantFeatures);
|
|
264
370
|
console.error(`sense: config change (${changed}) rebuilds the index`);
|
|
371
|
+
db.close();
|
|
372
|
+
clearCache(cfg);
|
|
373
|
+
return open(cfg);
|
|
265
374
|
}
|
|
375
|
+
}
|
|
376
|
+
// Meta can lie after a crash between table creation and the signature write; the table's
|
|
377
|
+
// own DDL cannot. A mismatch here rebuilds no matter what meta says. A tokenize-only rebuild
|
|
378
|
+
// just dropped content above, so storedTokenize sees no table and this guard is skipped
|
|
379
|
+
// naturally rather than needing its own case.
|
|
380
|
+
const stored = storedTokenize(db);
|
|
381
|
+
if (stored !== null && stored !== tokenize) {
|
|
382
|
+
console.error('sense: cache was built with a different content tokenizer; rebuilding the index');
|
|
266
383
|
db.close();
|
|
267
384
|
clearCache(cfg);
|
|
268
385
|
return open(cfg);
|
|
269
386
|
}
|
|
270
|
-
ensureSchema(db, cfg);
|
|
387
|
+
ensureSchema(db, cfg, tokenize);
|
|
388
|
+
let rebuildWarnings = [];
|
|
389
|
+
if (tokenizeOnlyRebuild) {
|
|
390
|
+
rebuildWarnings = rebuildContentTable(db, cfg, cfg.baseDir);
|
|
391
|
+
setMeta(db, 'features', wantFeatures);
|
|
392
|
+
}
|
|
271
393
|
// 3x the largest reconcile this cache has recorded, floored at 30s and capped at 10min.
|
|
272
394
|
// Installed before reconcile() -- that call is the one that races a watcher's transaction.
|
|
273
395
|
const recordedMaxMs = Number((_getMeta = getMeta(db, 'reconcile_max_ms')) !== null && _getMeta !== void 0 ? _getMeta : '0');
|
|
@@ -278,7 +400,10 @@ export function open(cfg) {
|
|
|
278
400
|
cfg,
|
|
279
401
|
dbPath,
|
|
280
402
|
parsed,
|
|
281
|
-
warnings
|
|
403
|
+
warnings: [
|
|
404
|
+
...rebuildWarnings,
|
|
405
|
+
...warnings
|
|
406
|
+
]
|
|
282
407
|
};
|
|
283
408
|
}
|
|
284
409
|
// Deletes the cache directory, and only that. The rebuild is not this function's job: the next
|
package/dist/esm/db.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/ai/sensemaking/src/db.ts"],"sourcesContent":["// The Node floor (>=22.20) is explained here and nowhere else: 22.20 is the first release with\n// both FTS5 and row-returning INSERT ... RETURNING. Raise it only for a load-bearing capability.\nimport { mkdirSync, rmSync } from 'node:fs';\nimport { join } from 'node:path';\nimport { DatabaseSync } from 'node:sqlite';\nimport type { Config, ResolvedConfig } from './config.ts';\nimport { featureSignature, STATE_DIR } from './config.ts';\nimport { SenseError } from './errors.ts';\nimport { activeFeatures } from './features/index.ts';\nimport type { ReconcileDelta } from './features/types.ts';\nimport { progress } from './progress.ts';\nimport type { ParsedDoc } from './scan.ts';\nimport { listFiles, parseFile, RESERVED_COLUMNS } from './scan.ts';\n\n// Feature-owned columns (`_rank`) must stay out of the upsert: a reparse would null the last\n// computed value on every touch, not just the reconciles that recompute it.\nconst CORE_FRONTMATTER_COLUMNS = new Set(['path', '_mtime', '_size', '_parse_error']);\n\n// Rows -> SQLite: core schema, reconcile loop, has(). Parsing lives in scan.ts;\n// everything beyond frontmatter + content lives in src/features/.\n\nexport const DB_FILENAME = 'cache.db';\n// Cache shape version, independent of the config's own `version`. Bumping it rebuilds\n// existing trees on first query.\nexport const SCHEMA_VERSION = '10';\n\n// SQLite's compile-time SQLITE_MAX_COLUMN, default 2000 (https://www.sqlite.org/limits.html).\nconst MAX_FRONTMATTER_COLUMNS = 2000;\n\nexport interface OpenResult {\n db: DatabaseSync;\n cfg: ResolvedConfig;\n dbPath: string;\n parsed: number;\n warnings: string[];\n}\n\nfunction quoteIdent(name: string): string {\n return `\"${name.split('\"').join('\"\"')}\"`;\n}\n\n// has(field, value): JSON-array field -> membership, string field -> substring, NULL -> false.\nfunction registerFunctions(db: DatabaseSync): void {\n db.function('has', { deterministic: true, varargs: false }, (field: unknown, value: unknown): number => {\n if (field === null || field === undefined) return 0;\n\n const needle = String(value);\n\n if (typeof field === 'string') {\n if (field.startsWith('[')) {\n try {\n const parsed = JSON.parse(field);\n if (Array.isArray(parsed)) {\n return parsed.some((item) => String(item) === needle) ? 1 : 0;\n }\n } catch {}\n }\n return field.includes(needle) ? 1 : 0;\n }\n\n return String(field).includes(needle) ? 1 : 0;\n });\n}\n\nfunction getColumns(db: DatabaseSync): Set<string> {\n const rows = db.prepare('PRAGMA table_info(frontmatter)').all() as Array<{ name: string }>;\n return new Set(rows.map((r) => r.name));\n}\n\n// Content is a separate table (not a column on frontmatter) so `SELECT * FROM frontmatter`\n// can't dump file text into context. Features add their own tables after the core ones.\nfunction ensureSchema(db: DatabaseSync, cfg: Config): void {\n db.exec(`CREATE TABLE IF NOT EXISTS frontmatter (\"path\" TEXT PRIMARY KEY, \"_mtime\" REAL, \"_size\" INTEGER, \"_parse_error\" TEXT)`);\n db.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS content USING fts5(title, summary, text, path UNINDEXED, tokenize = 'porter unicode61')`);\n // Coverage, not ownership: a path can appear under several presets. path leads the PK so the\n // per-doc delete is an index hit -- keyed the other way, cold builds went quadratic.\n db.exec(`CREATE TABLE IF NOT EXISTS preset_files (\"path\" TEXT, preset TEXT, PRIMARY KEY (\"path\", preset))`);\n db.exec('CREATE INDEX IF NOT EXISTS preset_files_preset ON preset_files(preset)');\n for (const feature of activeFeatures(cfg)) feature.schema(db);\n if (getMeta(db, 'schema_version') === null) setMeta(db, 'schema_version', SCHEMA_VERSION);\n if (getMeta(db, 'features') === null) setMeta(db, 'features', featureSignature(cfg));\n}\n\nexport function getMeta(db: DatabaseSync, key: string): string | null {\n const row = db.prepare('SELECT value FROM meta WHERE key = ?').get(key) as { value: string } | undefined;\n return row ? row.value : null;\n}\n\nexport function setMeta(db: DatabaseSync, key: string, value: string | null): void {\n if (value === null) {\n db.prepare('DELETE FROM meta WHERE key = ?').run(key);\n return;\n }\n db.prepare('INSERT INTO meta (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value').run(key, value);\n}\n\nexport function docCount(db: DatabaseSync): number {\n const row = db.prepare('SELECT COUNT(*) AS n FROM frontmatter').get() as { n: number };\n return row.n;\n}\n\nexport function reconcile(db: DatabaseSync, cfg: Config, baseDir: string): { parsed: number; warnings: string[] } {\n const files = listFiles(cfg, baseDir);\n const currentSet = new Set(files.map((f) => f.relPath));\n\n const existingRows = db.prepare(`SELECT \"path\", \"_mtime\", \"_size\" FROM frontmatter`).all() as Array<{\n path: string;\n _mtime: number;\n _size: number;\n }>;\n const existing = new Map(existingRows.map((r) => [r.path, r]));\n const vanished = existingRows.filter((r) => !currentSet.has(r.path)).map((r) => r.path);\n\n const toReparse = files.filter((f) => {\n const row = existing.get(f.relPath);\n return !row || row._mtime !== f.mtimeMs || row._size !== f.size;\n });\n\n if (vanished.length === 0 && toReparse.length === 0) return { parsed: 0, warnings: [] };\n\n const features = activeFeatures(cfg);\n const seenColumns = getColumns(db);\n const newColumns: string[] = [];\n const parsedDocs: ParsedDoc[] = [];\n const warnings: string[] = [];\n\n // Bulk reparses (a sync, a cold build) are the long silences a query can hit; short\n // reconciles stay silent (progress() has a threshold).\n const report = progress('reparsing files', toReparse.length);\n let parsedCount = 0;\n for (const file of toReparse) {\n // A doc only gets extract/store from features that apply to it (currently: embed, via\n // FileStat.embed -- true iff the config names an embedding model).\n const fileFeatures = features.filter((feature) => !feature.enabledForFile || feature.enabledForFile(cfg, file));\n const { doc, warnings: fileWarnings } = parseFile(file, fileFeatures);\n report.tick(++parsedCount);\n warnings.push(...fileWarnings);\n for (const key of Object.keys(doc.data)) {\n if (!seenColumns.has(key)) {\n seenColumns.add(key);\n newColumns.push(key);\n }\n }\n parsedDocs.push(doc);\n }\n report.finish();\n\n const allColumns = [...seenColumns];\n // Fence before ALTERing: SQLite's own failure past this point is a raw\n // \"too many columns on sqlite_altertab_frontmatter\" with no indication of the boundary or the levers.\n if (allColumns.length > MAX_FRONTMATTER_COLUMNS) {\n throw new SenseError(\n 'COLUMN_LIMIT',\n `frontmatter would need ${allColumns.length} columns, crossing SQLite's compile-time SQLITE_MAX_COLUMN limit (default ${MAX_FRONTMATTER_COLUMNS}; see https://www.sqlite.org/limits.html). Narrow the presets' include globs so fewer/other files are indexed, or fix whatever is generating unbounded frontmatter keys.`\n );\n }\n // Columns the frontmatter upsert actually writes: core + parsed frontmatter keys, never a\n // feature-owned reserved column (see CORE_FRONTMATTER_COLUMNS above).\n const writableColumns = allColumns.filter((c) => CORE_FRONTMATTER_COLUMNS.has(c) || !RESERVED_COLUMNS.has(c));\n // ON CONFLICT UPDATE (not OR REPLACE) keeps the row's rowid stable across reparses --\n // content rows are coupled to that rowid below.\n const insertSql = `INSERT INTO frontmatter (${writableColumns.map(quoteIdent).join(', ')}) VALUES (${writableColumns.map(() => '?').join(', ')}) ON CONFLICT(\"path\") DO UPDATE SET ${writableColumns\n .filter((c) => c !== 'path')\n .map((c) => `${quoteIdent(c)} = excluded.${quoteIdent(c)}`)\n .join(', ')}`;\n\n const added = toReparse.filter((f) => !existing.has(f.relPath)).map((f) => f.relPath);\n const delta: ReconcileDelta = { files, reparsed: parsedDocs.map((d) => d.relPath), added, vanished };\n\n const txStart = Date.now();\n db.exec('BEGIN');\n try {\n for (const col of newColumns) db.exec(`ALTER TABLE frontmatter ADD COLUMN ${quoteIdent(col)}`);\n // FTS5 has no upsert, so delete-before-insert into `content`; coupled to the\n // frontmatter rowid (indexed via its PRIMARY KEY) instead of the UNINDEXED `path`\n // column, which a per-row DELETE would otherwise scan the whole table to find.\n const delBody = db.prepare(`DELETE FROM content WHERE rowid = (SELECT rowid FROM frontmatter WHERE \"path\" = ?)`);\n const delPresetFiles = db.prepare(`DELETE FROM preset_files WHERE \"path\" = ?`);\n const insertPresetFile = db.prepare(`INSERT INTO preset_files (\"path\", preset) VALUES (?, ?)`);\n if (vanished.length > 0) {\n const del = db.prepare(`DELETE FROM frontmatter WHERE \"path\" = ?`);\n for (const path of vanished) {\n // content delete must run first: it looks up the frontmatter rowid by path, which\n // the frontmatter delete below would otherwise have already removed.\n delBody.run(path);\n del.run(path);\n delPresetFiles.run(path);\n for (const feature of features) feature.remove?.(db, path, delta);\n }\n }\n if (parsedDocs.length > 0) {\n const insert = db.prepare(insertSql);\n const insertBody = db.prepare(`INSERT INTO content (rowid, title, summary, text, \"path\") VALUES ((SELECT rowid FROM frontmatter WHERE \"path\" = ?), ?, ?, ?, ?)`);\n for (const doc of parsedDocs) {\n const values = writableColumns.map((col) => {\n if (col === 'path') return doc.relPath;\n if (col === '_mtime') return doc.mtimeMs;\n if (col === '_size') return doc.size;\n // Written per parse, unlike _rank, which a feature pass owns and the upsert skips.\n if (col === '_parse_error') return doc.parseError;\n return doc.data[col] ?? null;\n });\n // Frontmatter upsert first: content's rowid lookup below depends on this row existing.\n insert.run(...values);\n // Only for docs that have rows: an unconditional delete made cold crawls quadratic.\n if (existing.has(doc.relPath)) {\n delBody.run(doc.relPath);\n for (const feature of features) feature.remove?.(db, doc.relPath, delta);\n }\n insertBody.run(doc.relPath, doc.search.title, doc.search.summary, doc.search.text, doc.relPath);\n // A preset edit forces a full rebuild, so an unchanged doc's coverage is already\n // correct; new docs have nothing to clear, which keeps cold builds linear.\n if (existing.has(doc.relPath)) delPresetFiles.run(doc.relPath);\n for (const presetName of doc.presets) insertPresetFile.run(doc.relPath, presetName);\n for (const feature of features) feature.store?.(db, doc.relPath, doc.extracted[feature.name], delta);\n }\n }\n for (const feature of features) feature.afterReconcile?.(db, delta);\n db.exec('COMMIT');\n } catch (err) {\n db.exec('ROLLBACK');\n throw err;\n }\n\n // Reconcile's own write-transaction duration, for open()'s derived busy_timeout (F):\n // keep the observed max so a big watcher reconcile's lock hold is what the next open bounds its wait against.\n const durationMs = Date.now() - txStart;\n const prevRaw = getMeta(db, 'reconcile_max_ms');\n // -1, not 0, so a genuinely 0ms first reconcile (sub-millisecond, common on a tiny tree)\n // still gets recorded instead of losing to the \"nothing recorded yet\" default.\n const prevMax = prevRaw === null ? -1 : Number(prevRaw);\n if (durationMs > prevMax) setMeta(db, 'reconcile_max_ms', String(durationMs));\n\n return { parsed: parsedDocs.length, warnings };\n}\n\n// Names what moved between two feature signatures (see config.featureSignature's format:\n// global features, embed provider, then one segment per preset) for the rebuild notice.\nfunction signatureDiff(before: string, after: string): string {\n // Segment keys: `features`, `embed`, `preset:<name>` (config.featureSignature's format).\n const keyOf = (part: string) => (part.startsWith('preset:') ? part.split(':').slice(0, 2).join(':') : part.split(':')[0]);\n const parse = (sig: string) => new Map(sig.split('|').map((part) => [keyOf(part), part]));\n const a = parse(before);\n const b = parse(after);\n const changed = new Set<string>();\n for (const [key, val] of b) if (a.get(key) !== val) changed.add(key);\n for (const key of a.keys()) if (!b.has(key)) changed.add(key);\n const label = (key: string) => (key === 'embed' ? 'embed settings' : key.startsWith('preset:') ? `preset \"${key.slice(7)}\"` : 'features');\n return changed.size === 0 ? 'features' : [...changed].map(label).join(', ');\n}\n\nexport function open(cfg: ResolvedConfig): OpenResult {\n const stateDir = join(cfg.baseDir, STATE_DIR);\n mkdirSync(stateDir, { recursive: true });\n const dbPath = join(stateDir, DB_FILENAME);\n\n const db = new DatabaseSync(dbPath);\n db.exec('PRAGMA journal_mode = WAL');\n // Covers a concurrent watcher's bulk reconcile (~5s for 500 files at 26k notes). A query\n // that outwaits it still fails loudly.\n db.exec('PRAGMA busy_timeout = 30000');\n registerFunctions(db);\n\n db.exec('CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT)');\n\n // Schema-version or feature-set mismatch: reconcile only reparses changed files, so an\n // old cache can't be patched incrementally -- rebuild instead (cheap: nothing expensive lives here).\n const version = getMeta(db, 'schema_version');\n const features = getMeta(db, 'features');\n const wantFeatures = featureSignature(cfg);\n if ((version !== null && version !== SCHEMA_VERSION) || (features !== null && features !== wantFeatures)) {\n // Indexing derives from presets, so a config edit rebuilding the cache must say so and\n // name what changed -- silent rebuilds make derived indexing look like a hang or a bug.\n if (version !== null && version !== SCHEMA_VERSION) {\n console.error('sense: cache format changed (new sensemaking version); rebuilding the index');\n } else {\n const changed = signatureDiff(features ?? '', wantFeatures);\n console.error(`sense: config change (${changed}) rebuilds the index`);\n }\n db.close();\n clearCache(cfg);\n return open(cfg);\n }\n\n ensureSchema(db, cfg);\n\n // 3x the largest reconcile this cache has recorded, floored at 30s and capped at 10min.\n // Installed before reconcile() -- that call is the one that races a watcher's transaction.\n const recordedMaxMs = Number(getMeta(db, 'reconcile_max_ms') ?? '0');\n db.exec(`PRAGMA busy_timeout = ${Math.min(Math.max(30000, 3 * recordedMaxMs), 600_000)}`);\n\n const { parsed, warnings } = reconcile(db, cfg, cfg.baseDir);\n\n return { db, cfg, dbPath, parsed, warnings };\n}\n\n// Deletes the cache directory, and only that. The rebuild is not this function's job: the next\n// open() reconciles, which is what running any command already does, so a verb that bundled the\n// two described the half that was not its own. Manual reset for a doubted cache; the schema and\n// config-signature mismatches below reset themselves.\nexport function clearCache(cfg: ResolvedConfig): void {\n rmSync(join(cfg.baseDir, STATE_DIR), { recursive: true, force: true });\n}\n"],"names":["mkdirSync","rmSync","join","DatabaseSync","featureSignature","STATE_DIR","SenseError","activeFeatures","progress","listFiles","parseFile","RESERVED_COLUMNS","CORE_FRONTMATTER_COLUMNS","Set","DB_FILENAME","SCHEMA_VERSION","MAX_FRONTMATTER_COLUMNS","quoteIdent","name","split","registerFunctions","db","function","deterministic","varargs","field","value","undefined","needle","String","startsWith","parsed","JSON","parse","Array","isArray","some","item","includes","getColumns","rows","prepare","all","map","r","ensureSchema","cfg","exec","feature","schema","getMeta","setMeta","key","row","get","run","docCount","n","reconcile","baseDir","files","currentSet","f","relPath","existingRows","existing","Map","path","vanished","filter","has","toReparse","_mtime","mtimeMs","_size","size","length","warnings","features","seenColumns","newColumns","parsedDocs","report","parsedCount","file","fileFeatures","enabledForFile","doc","fileWarnings","tick","push","Object","keys","data","add","finish","allColumns","writableColumns","c","insertSql","added","delta","reparsed","d","txStart","Date","now","col","delBody","delPresetFiles","insertPresetFile","del","remove","insert","insertBody","values","parseError","search","title","summary","text","presetName","presets","store","extracted","afterReconcile","err","durationMs","prevRaw","prevMax","Number","signatureDiff","before","after","keyOf","part","slice","sig","a","b","changed","val","label","open","stateDir","recursive","dbPath","version","wantFeatures","console","error","close","clearCache","recordedMaxMs","Math","min","max","force"],"mappings":"AAAA,+FAA+F;AAC/F,iGAAiG;AACjG,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A,QAAQ7B,IAAI,iCAAZ6B,sBAAAA,WAAmC;IAChE7B,GAAG0B,IAAI,CAAC,CAAC,sBAAsB,EAAE2G,KAAKC,GAAG,CAACD,KAAKE,GAAG,CAAC,OAAO,IAAIH,gBAAgB,SAAU;IAExF,MAAM,EAAE1H,MAAM,EAAE8C,QAAQ,EAAE,GAAGnB,UAAUrC,IAAIyB,KAAKA,IAAIa,OAAO;IAE3D,OAAO;QAAEtC;QAAIyB;QAAKoG;QAAQnH;QAAQ8C;IAAS;AAC7C;AAEA,+FAA+F;AAC/F,gGAAgG;AAChG,gGAAgG;AAChG,sDAAsD;AACtD,OAAO,SAAS2E,WAAW1G,GAAmB;IAC5C7C,OAAOC,KAAK4C,IAAIa,OAAO,EAAEtD,YAAY;QAAE4I,WAAW;QAAMY,OAAO;IAAK;AACtE"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/ai/sensemaking/src/db.ts"],"sourcesContent":["// The Node floor (>=22.20) is explained here and nowhere else: 22.20 is the first release with\n// both FTS5 and row-returning INSERT ... RETURNING. Raise it only for a load-bearing capability.\nimport { mkdirSync, rmSync } from 'node:fs';\nimport { join } from 'node:path';\nimport { DatabaseSync, type StatementSync } from 'node:sqlite';\nimport type { Config, ResolvedConfig } from './config.ts';\nimport { contentTokenize, featureSignature, STATE_DIR } from './config.ts';\nimport { SenseError } from './errors.ts';\nimport { activeFeatures } from './features/index.ts';\nimport type { ReconcileDelta } from './features/types.ts';\nimport { progress } from './progress.ts';\nimport type { ParsedDoc } from './scan.ts';\nimport { listFiles, parseFile, RESERVED_COLUMNS } from './scan.ts';\nimport { segmentField, segmentMatch } from './segment.ts';\n\n// Feature-owned columns (`_rank`) must stay out of the upsert: a reparse would null the last\n// computed value on every touch, not just the reconciles that recompute it.\nconst CORE_FRONTMATTER_COLUMNS = new Set(['path', '_mtime', '_size', '_parse_error']);\n\n// Rows -> SQLite: core schema, reconcile loop, has(). Parsing lives in scan.ts;\n// everything beyond frontmatter + content lives in src/features/.\n\nexport const DB_FILENAME = 'cache.db';\n// Cache shape version, independent of the config's own `version`. Bumping it rebuilds\n// existing trees on first query.\nexport const SCHEMA_VERSION = '12';\n\n// SQLite's compile-time SQLITE_MAX_COLUMN, default 2000 (https://www.sqlite.org/limits.html).\nconst MAX_FRONTMATTER_COLUMNS = 2000;\n\nexport interface OpenResult {\n db: DatabaseSync;\n cfg: ResolvedConfig;\n dbPath: string;\n parsed: number;\n warnings: string[];\n}\n\nfunction quoteIdent(name: string): string {\n return `\"${name.split('\"').join('\"\"')}\"`;\n}\n\n// has(field, value): JSON-array field -> membership, string field -> substring, NULL -> false.\nfunction registerFunctions(db: DatabaseSync, segmenting: boolean): void {\n db.function('has', { deterministic: true, varargs: false }, (field: unknown, value: unknown): number => {\n if (field === null || field === undefined) return 0;\n\n const needle = String(value);\n\n if (typeof field === 'string') {\n if (field.startsWith('[')) {\n try {\n const parsed = JSON.parse(field);\n if (Array.isArray(parsed)) {\n return parsed.some((item) => String(item) === needle) ? 1 : 0;\n }\n } catch {}\n }\n return field.includes(needle) ? 1 : 0;\n }\n\n return String(field).includes(needle) ? 1 : 0;\n });\n\n // Raw `content MATCH '<unspaced text>'` cannot be rewritten behind the author's back, and\n // matches nothing, so the same transform is available to hand-written SQL by name. `segmenting`\n // is the same config predicate commands.ts's search() gates on (contentTokenize(cfg) ===\n // undefined), passed in by open() rather than read back from persisted state.\n db.function('segment', { deterministic: true, varargs: false }, (terms: unknown): string => {\n const text = terms === null || terms === undefined ? '' : String(terms);\n return segmenting ? segmentMatch(text) : text;\n });\n}\n\nfunction getColumns(db: DatabaseSync): Set<string> {\n const rows = db.prepare('PRAGMA table_info(frontmatter)').all() as Array<{ name: string }>;\n return new Set(rows.map((r) => r.name));\n}\n\n// Stemming is English-only, but the segmentation underneath it is what decides coverage:\n// unicode61 splits on spaces, so a language written without them (Chinese, Japanese, Thai)\n// indexes a whole run as one token and word search finds nothing. `content.tokenize` is how\n// such a tree picks trigram instead.\nconst DEFAULT_TOKENIZE = 'porter unicode61';\n\n// FTS5 takes its tokenizer as a string literal inside DDL, where nothing can bind, so a\n// configured value has to be concatenated. Probing a throwaway table is what makes that safe\n// and is also the whole validation: anything the linked SQLite accepts passes, anything else\n// fails here with SQLite's own message rather than against the real table. It means no table\n// of which version added which tokenizer has to be maintained.\nfunction resolveTokenize(db: DatabaseSync, cfg: Config): string {\n const configured = contentTokenize(cfg);\n if (configured === undefined) return DEFAULT_TOKENIZE;\n const literal = configured.replace(/'/g, \"''\");\n try {\n db.exec('DROP TABLE IF EXISTS temp.sense_tokenize_probe');\n db.exec(`CREATE VIRTUAL TABLE temp.sense_tokenize_probe USING fts5(x, tokenize = '${literal}')`);\n db.exec('DROP TABLE IF EXISTS temp.sense_tokenize_probe');\n } catch (err) {\n throw new SenseError('CONFIG_INVALID', `content.tokenize \"${configured}\" is not a tokenizer this SQLite accepts (${(err as Error).message}); the built-in choices are unicode61, ascii, porter, and trigram, each with their own options`);\n }\n return literal;\n}\n\n// The tokenizer the content table was actually built with, from its own DDL -- the one\n// record that cannot desynchronize from the table. NULL when the table does not exist yet.\nfunction storedTokenize(db: DatabaseSync): string | null {\n const row = db.prepare(`SELECT sql FROM sqlite_master WHERE name = 'content'`).get() as { sql: string } | undefined;\n if (!row) return null;\n const m = row.sql.match(/tokenize = '((?:[^']|'')*)'/);\n return m ? m[1] : null;\n}\n\n// Content is a separate table (not a column on frontmatter) so `SELECT * FROM frontmatter`\n// can't dump file text into context. Features add their own tables after the core ones.\nfunction ensureSchema(db: DatabaseSync, cfg: Config, tokenize: string): void {\n db.exec(`CREATE TABLE IF NOT EXISTS frontmatter (\"path\" TEXT PRIMARY KEY, \"_mtime\" REAL, \"_size\" INTEGER, \"_parse_error\" TEXT)`);\n // IF NOT EXISTS is safe against a tokenizer change: open() compares the table's own DDL\n // against the resolved tokenizer before this runs, so a stale table is already gone by now.\n // The three `_seg` sidecars are appended after path, never inserted: bm25(content, ...) and\n // snippet(content, 2, ...) are documented against the first three columns and keep working\n // (FTS5 defaults the weights it was not given). Each carries its field's exploded unspaced\n // runs for text that needs it, and an empty string for text that does not.\n db.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS content USING fts5(title, summary, text, path UNINDEXED, title_seg, summary_seg, text_seg, tokenize = '${tokenize}')`);\n // Coverage, not ownership: a path can appear under several presets. path leads the PK so the\n // per-doc delete is an index hit -- keyed the other way, cold builds went quadratic.\n db.exec(`CREATE TABLE IF NOT EXISTS preset_files (\"path\" TEXT, preset TEXT, PRIMARY KEY (\"path\", preset))`);\n db.exec('CREATE INDEX IF NOT EXISTS preset_files_preset ON preset_files(preset)');\n for (const feature of activeFeatures(cfg)) feature.schema(db);\n if (getMeta(db, 'schema_version') === null) setMeta(db, 'schema_version', SCHEMA_VERSION);\n if (getMeta(db, 'features') === null) setMeta(db, 'features', featureSignature(cfg));\n}\n\nexport function getMeta(db: DatabaseSync, key: string): string | null {\n const row = db.prepare('SELECT value FROM meta WHERE key = ?').get(key) as { value: string } | undefined;\n return row ? row.value : null;\n}\n\nexport function setMeta(db: DatabaseSync, key: string, value: string | null): void {\n if (value === null) {\n db.prepare('DELETE FROM meta WHERE key = ?').run(key);\n return;\n }\n db.prepare('INSERT INTO meta (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value').run(key, value);\n}\n\n// Shared by reconcile() and the tokenize-only rebuild in open(), so both prepare the same\n// literal instead of two copies drifting apart.\nconst INSERT_CONTENT_SQL = `INSERT INTO content (rowid, title, summary, text, \"path\", title_seg, summary_seg, text_seg) VALUES ((SELECT rowid FROM frontmatter WHERE \"path\" = ?), ?, ?, ?, ?, ?, ?, ?)`;\n\n// Shared by reconcile() and the tokenize-only rebuild in open(), so content population is\n// defined once. Assumes the frontmatter row for doc.relPath already exists (rowid lookup).\nfunction insertContentRow(insertBody: StatementSync, doc: ParsedDoc, segmenting: boolean): void {\n insertBody.run(doc.relPath, doc.search.title, doc.search.summary, doc.search.text, doc.relPath, segmenting ? segmentField(doc.search.title) : '', segmenting ? segmentField(doc.search.summary) : '', segmenting ? segmentField(doc.search.text) : '');\n}\n\nexport function docCount(db: DatabaseSync): number {\n const row = db.prepare('SELECT COUNT(*) AS n FROM frontmatter').get() as { n: number };\n return row.n;\n}\n\nexport function reconcile(db: DatabaseSync, cfg: Config, baseDir: string): { parsed: number; warnings: string[] } {\n const files = listFiles(cfg, baseDir);\n const currentSet = new Set(files.map((f) => f.relPath));\n\n const existingRows = db.prepare(`SELECT \"path\", \"_mtime\", \"_size\" FROM frontmatter`).all() as Array<{\n path: string;\n _mtime: number;\n _size: number;\n }>;\n const existing = new Map(existingRows.map((r) => [r.path, r]));\n const vanished = existingRows.filter((r) => !currentSet.has(r.path)).map((r) => r.path);\n\n const toReparse = files.filter((f) => {\n const row = existing.get(f.relPath);\n return !row || row._mtime !== f.mtimeMs || row._size !== f.size;\n });\n\n if (vanished.length === 0 && toReparse.length === 0) return { parsed: 0, warnings: [] };\n\n const features = activeFeatures(cfg);\n const seenColumns = getColumns(db);\n const newColumns: string[] = [];\n const parsedDocs: ParsedDoc[] = [];\n const warnings: string[] = [];\n\n // Bulk reparses (a sync, a cold build) are the long silences a query can hit; short\n // reconciles stay silent (progress() has a threshold).\n const report = progress('reparsing files', toReparse.length);\n let parsedCount = 0;\n for (const file of toReparse) {\n // A doc only gets extract/store from features that apply to it (currently: embed, via\n // FileStat.embed -- true iff the config names an embedding model).\n const fileFeatures = features.filter((feature) => !feature.enabledForFile || feature.enabledForFile(cfg, file));\n const { doc, warnings: fileWarnings } = parseFile(file, fileFeatures);\n report.tick(++parsedCount);\n warnings.push(...fileWarnings);\n for (const key of Object.keys(doc.data)) {\n if (!seenColumns.has(key)) {\n seenColumns.add(key);\n newColumns.push(key);\n }\n }\n parsedDocs.push(doc);\n }\n report.finish();\n\n const allColumns = [...seenColumns];\n // Fence before ALTERing: SQLite's own failure past this point is a raw\n // \"too many columns on sqlite_altertab_frontmatter\" with no indication of the boundary or the levers.\n if (allColumns.length > MAX_FRONTMATTER_COLUMNS) {\n throw new SenseError(\n 'COLUMN_LIMIT',\n `frontmatter would need ${allColumns.length} columns, crossing SQLite's compile-time SQLITE_MAX_COLUMN limit (default ${MAX_FRONTMATTER_COLUMNS}; see https://www.sqlite.org/limits.html). Narrow the presets' include globs so fewer/other files are indexed, or fix whatever is generating unbounded frontmatter keys.`\n );\n }\n // Columns the frontmatter upsert actually writes: core + parsed frontmatter keys, never a\n // feature-owned reserved column (see CORE_FRONTMATTER_COLUMNS above).\n const writableColumns = allColumns.filter((c) => CORE_FRONTMATTER_COLUMNS.has(c) || !RESERVED_COLUMNS.has(c));\n // ON CONFLICT UPDATE (not OR REPLACE) keeps the row's rowid stable across reparses --\n // content rows are coupled to that rowid below.\n const insertSql = `INSERT INTO frontmatter (${writableColumns.map(quoteIdent).join(', ')}) VALUES (${writableColumns.map(() => '?').join(', ')}) ON CONFLICT(\"path\") DO UPDATE SET ${writableColumns\n .filter((c) => c !== 'path')\n .map((c) => `${quoteIdent(c)} = excluded.${quoteIdent(c)}`)\n .join(', ')}`;\n\n const added = toReparse.filter((f) => !existing.has(f.relPath)).map((f) => f.relPath);\n const delta: ReconcileDelta = { files, reparsed: parsedDocs.map((d) => d.relPath), added, vanished };\n\n const txStart = Date.now();\n db.exec('BEGIN');\n try {\n for (const col of newColumns) db.exec(`ALTER TABLE frontmatter ADD COLUMN ${quoteIdent(col)}`);\n // FTS5 has no upsert, so delete-before-insert into `content`; coupled to the\n // frontmatter rowid (indexed via its PRIMARY KEY) instead of the UNINDEXED `path`\n // column, which a per-row DELETE would otherwise scan the whole table to find.\n const delBody = db.prepare(`DELETE FROM content WHERE rowid = (SELECT rowid FROM frontmatter WHERE \"path\" = ?)`);\n const delPresetFiles = db.prepare(`DELETE FROM preset_files WHERE \"path\" = ?`);\n const insertPresetFile = db.prepare(`INSERT INTO preset_files (\"path\", preset) VALUES (?, ?)`);\n if (vanished.length > 0) {\n const del = db.prepare(`DELETE FROM frontmatter WHERE \"path\" = ?`);\n for (const path of vanished) {\n // content delete must run first: it looks up the frontmatter rowid by path, which\n // the frontmatter delete below would otherwise have already removed.\n delBody.run(path);\n del.run(path);\n delPresetFiles.run(path);\n for (const feature of features) feature.remove?.(db, path, delta);\n }\n }\n if (parsedDocs.length > 0) {\n const insert = db.prepare(insertSql);\n const insertBody = db.prepare(INSERT_CONTENT_SQL);\n // A non-default tokenizer means the tree has chosen its own scheme; a phrase query over\n // grapheme runs would be nonsense against trigram, so the sidecars stay empty.\n const segmenting = contentTokenize(cfg) === undefined;\n for (const doc of parsedDocs) {\n const values = writableColumns.map((col) => {\n if (col === 'path') return doc.relPath;\n if (col === '_mtime') return doc.mtimeMs;\n if (col === '_size') return doc.size;\n // Written per parse, unlike _rank, which a feature pass owns and the upsert skips.\n if (col === '_parse_error') return doc.parseError;\n return doc.data[col] ?? null;\n });\n // Frontmatter upsert first: content's rowid lookup below depends on this row existing.\n insert.run(...values);\n // Only for docs that have rows: an unconditional delete made cold crawls quadratic.\n if (existing.has(doc.relPath)) {\n delBody.run(doc.relPath);\n for (const feature of features) feature.remove?.(db, doc.relPath, delta);\n }\n insertContentRow(insertBody, doc, segmenting);\n // A preset edit forces a full rebuild, so an unchanged doc's coverage is already\n // correct; new docs have nothing to clear, which keeps cold builds linear.\n if (existing.has(doc.relPath)) delPresetFiles.run(doc.relPath);\n for (const presetName of doc.presets) insertPresetFile.run(doc.relPath, presetName);\n for (const feature of features) feature.store?.(db, doc.relPath, doc.extracted[feature.name], delta);\n }\n }\n for (const feature of features) feature.afterReconcile?.(db, delta);\n db.exec('COMMIT');\n } catch (err) {\n db.exec('ROLLBACK');\n throw err;\n }\n\n // Reconcile's own write-transaction duration, for open()'s derived busy_timeout (F):\n // keep the observed max so a big watcher reconcile's lock hold is what the next open bounds its wait against.\n const durationMs = Date.now() - txStart;\n const prevRaw = getMeta(db, 'reconcile_max_ms');\n // -1, not 0, so a genuinely 0ms first reconcile (sub-millisecond, common on a tiny tree)\n // still gets recorded instead of losing to the \"nothing recorded yet\" default.\n const prevMax = prevRaw === null ? -1 : Number(prevRaw);\n if (durationMs > prevMax) setMeta(db, 'reconcile_max_ms', String(durationMs));\n\n return { parsed: parsedDocs.length, warnings };\n}\n\n// Segment keys that moved between two feature signatures (see config.featureSignature's\n// format: global features, embed provider, tokenize, then one segment per preset).\nfunction changedSignatureKeys(before: string, after: string): Set<string> {\n const keyOf = (part: string) => (part.startsWith('preset:') ? part.split(':').slice(0, 2).join(':') : part.split(':')[0]);\n const parse = (sig: string) => new Map(sig.split('|').map((part) => [keyOf(part), part]));\n const a = parse(before);\n const b = parse(after);\n const changed = new Set<string>();\n for (const [key, val] of b) if (a.get(key) !== val) changed.add(key);\n for (const key of a.keys()) if (!b.has(key)) changed.add(key);\n return changed;\n}\n\n// Names what moved, for the rebuild notice.\nfunction signatureDiff(before: string, after: string): string {\n const changed = changedSignatureKeys(before, after);\n const label = (key: string) => (key === 'embed' ? 'embed settings' : key === 'tokenize' ? 'content tokenizer' : key.startsWith('preset:') ? `preset \"${key.slice(7)}\"` : 'features');\n return changed.size === 0 ? 'features' : [...changed].map(label).join(', ');\n}\n\n// The tokenize-only rebuild in open(): content is dropped and repopulated from files already\n// listed in frontmatter, which itself is untouched. Frontmatter, links, sections, and\n// embeddings are file-derived and tokenizer-independent, so they survive. No feature extractors\n// run here -- doc.search (title/summary/text) is all content population needs. Returns\n// parseFile's per-file warnings (e.g. a bad date) so open() can surface them -- mtimes are\n// untouched, so reconcile() never reparses these files and would otherwise never emit them again.\nfunction rebuildContentTable(db: DatabaseSync, cfg: Config, baseDir: string): string[] {\n const known = new Set((db.prepare('SELECT \"path\" FROM frontmatter').all() as Array<{ path: string }>).map((r) => r.path));\n const files = listFiles(cfg, baseDir).filter((f) => known.has(f.relPath));\n const segmenting = contentTokenize(cfg) === undefined;\n const insertBody = db.prepare(INSERT_CONTENT_SQL);\n const warnings: string[] = [];\n db.exec('BEGIN');\n try {\n for (const file of files) {\n const { doc, warnings: fileWarnings } = parseFile(file);\n warnings.push(...fileWarnings);\n insertContentRow(insertBody, doc, segmenting);\n }\n db.exec('COMMIT');\n } catch (err) {\n db.exec('ROLLBACK');\n throw err;\n }\n return warnings;\n}\n\nexport function open(cfg: ResolvedConfig): OpenResult {\n const stateDir = join(cfg.baseDir, STATE_DIR);\n mkdirSync(stateDir, { recursive: true });\n const dbPath = join(stateDir, DB_FILENAME);\n\n const db = new DatabaseSync(dbPath);\n db.exec('PRAGMA journal_mode = WAL');\n // Covers a concurrent watcher's bulk reconcile (~5s for 500 files at 26k notes). A query\n // that outwaits it still fails loudly.\n db.exec('PRAGMA busy_timeout = 30000');\n registerFunctions(db, contentTokenize(cfg) === undefined);\n\n db.exec('CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT)');\n\n // Before the rebuild branch below, never after: that branch deletes the cache, so a typo'd\n // tokenizer validated later would cost a full re-index (and re-embed) to reach its own error.\n const tokenize = resolveTokenize(db, cfg);\n\n // Schema-version or feature-set mismatch: reconcile only reparses changed files, so an\n // old cache can't be patched incrementally -- rebuild instead (cheap: nothing expensive lives here).\n const version = getMeta(db, 'schema_version');\n const features = getMeta(db, 'features');\n const wantFeatures = featureSignature(cfg);\n let tokenizeOnlyRebuild = false;\n if ((version !== null && version !== SCHEMA_VERSION) || (features !== null && features !== wantFeatures)) {\n // Indexing derives from presets, so a config edit rebuilding the cache must say so and\n // name what changed -- silent rebuilds make derived indexing look like a hang or a bug.\n if (version !== null && version !== SCHEMA_VERSION) {\n console.error('sense: cache format changed (new sensemaking version); rebuilding the index');\n db.close();\n clearCache(cfg);\n return open(cfg);\n }\n const changedKeys = changedSignatureKeys(features ?? '', wantFeatures);\n // Only the tokenizer moved: frontmatter, links, sections, and embeddings are file-derived\n // and tokenizer-independent, so they don't need re-deriving. Everything else -- a preset\n // edit, an embed model change -- still takes the full clear/reopen below.\n if (changedKeys.size === 1 && changedKeys.has('tokenize')) {\n console.error('sense: config change (content tokenizer) rebuilds the text index; vectors, links, and sections are kept');\n db.exec('DROP TABLE content');\n tokenizeOnlyRebuild = true;\n } else {\n const changed = signatureDiff(features ?? '', wantFeatures);\n console.error(`sense: config change (${changed}) rebuilds the index`);\n db.close();\n clearCache(cfg);\n return open(cfg);\n }\n }\n\n // Meta can lie after a crash between table creation and the signature write; the table's\n // own DDL cannot. A mismatch here rebuilds no matter what meta says. A tokenize-only rebuild\n // just dropped content above, so storedTokenize sees no table and this guard is skipped\n // naturally rather than needing its own case.\n const stored = storedTokenize(db);\n if (stored !== null && stored !== tokenize) {\n console.error('sense: cache was built with a different content tokenizer; rebuilding the index');\n db.close();\n clearCache(cfg);\n return open(cfg);\n }\n\n ensureSchema(db, cfg, tokenize);\n\n let rebuildWarnings: string[] = [];\n if (tokenizeOnlyRebuild) {\n rebuildWarnings = rebuildContentTable(db, cfg, cfg.baseDir);\n setMeta(db, 'features', wantFeatures);\n }\n\n // 3x the largest reconcile this cache has recorded, floored at 30s and capped at 10min.\n // Installed before reconcile() -- that call is the one that races a watcher's transaction.\n const recordedMaxMs = Number(getMeta(db, 'reconcile_max_ms') ?? '0');\n db.exec(`PRAGMA busy_timeout = ${Math.min(Math.max(30000, 3 * recordedMaxMs), 600_000)}`);\n\n const { parsed, warnings } = reconcile(db, cfg, cfg.baseDir);\n\n return { db, cfg, dbPath, parsed, warnings: [...rebuildWarnings, ...warnings] };\n}\n\n// Deletes the cache directory, and only that. The rebuild is not this function's job: the next\n// open() reconciles, which is what running any command already does, so a verb that bundled the\n// two described the half that was not its own. Manual reset for a doubted cache; the schema and\n// config-signature mismatches below reset themselves.\nexport function clearCache(cfg: ResolvedConfig): void {\n rmSync(join(cfg.baseDir, STATE_DIR), { recursive: true, force: true });\n}\n"],"names":["mkdirSync","rmSync","join","DatabaseSync","contentTokenize","featureSignature","STATE_DIR","SenseError","activeFeatures","progress","listFiles","parseFile","RESERVED_COLUMNS","segmentField","segmentMatch","CORE_FRONTMATTER_COLUMNS","Set","DB_FILENAME","SCHEMA_VERSION","MAX_FRONTMATTER_COLUMNS","quoteIdent","name","split","registerFunctions","db","segmenting","function","deterministic","varargs","field","value","undefined","needle","String","startsWith","parsed","JSON","parse","Array","isArray","some","item","includes","terms","text","getColumns","rows","prepare","all","map","r","DEFAULT_TOKENIZE","resolveTokenize","cfg","configured","literal","replace","exec","err","message","storedTokenize","row","get","m","sql","match","ensureSchema","tokenize","feature","schema","getMeta","setMeta","key","run","INSERT_CONTENT_SQL","insertContentRow","insertBody","doc","relPath","search","title","summary","docCount","n","reconcile","baseDir","files","currentSet","f","existingRows","existing","Map","path","vanished","filter","has","toReparse","_mtime","mtimeMs","_size","size","length","warnings","features","seenColumns","newColumns","parsedDocs","report","parsedCount","file","fileFeatures","enabledForFile","fileWarnings","tick","push","Object","keys","data","add","finish","allColumns","writableColumns","c","insertSql","added","delta","reparsed","d","txStart","Date","now","col","delBody","delPresetFiles","insertPresetFile","del","remove","insert","values","parseError","presetName","presets","store","extracted","afterReconcile","durationMs","prevRaw","prevMax","Number","changedSignatureKeys","before","after","keyOf","part","slice","sig","a","b","changed","val","signatureDiff","label","rebuildContentTable","known","open","stateDir","recursive","dbPath","version","wantFeatures","tokenizeOnlyRebuild","console","error","close","clearCache","changedKeys","stored","rebuildWarnings","recordedMaxMs","Math","min","max","force"],"mappings":"AAAA,+FAA+F;AAC/F,iGAAiG;AACjG,SAASA,SAAS,EAAEC,MAAM,QAAQ,UAAU;AAC5C,SAASC,IAAI,QAAQ,YAAY;AACjC,SAASC,YAAY,QAA4B,cAAc;AAE/D,SAASC,eAAe,EAAEC,gBAAgB,EAAEC,SAAS,QAAQ,cAAc;AAC3E,SAASC,UAAU,QAAQ,cAAc;AACzC,SAASC,cAAc,QAAQ,sBAAsB;AAErD,SAASC,QAAQ,QAAQ,gBAAgB;AAEzC,SAASC,SAAS,EAAEC,SAAS,EAAEC,gBAAgB,QAAQ,YAAY;AACnE,SAASC,YAAY,EAAEC,YAAY,QAAQ,eAAe;AAE1D,6FAA6F;AAC7F,4EAA4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ist/esm/output.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export type Row = Record<string, unknown>;
|
|
2
|
-
export
|
|
2
|
+
export type Format = 'table' | 'json';
|
|
3
|
+
export type RowFormat = Format | 'csv';
|
|
4
|
+
export declare function printRows(rows: Row[], format: RowFormat, columns?: string[]): void;
|
|
5
|
+
export declare function printRowStream(rows: Iterable<Row>, format: RowFormat, columns: string[]): void;
|
|
3
6
|
export declare function featureOffHint(name: string): string;
|
|
4
7
|
export declare function featuresLine(features: {
|
|
5
8
|
on: string[];
|
package/dist/esm/output.js
CHANGED
|
@@ -1,16 +1,116 @@
|
|
|
1
|
-
// rows -> table (default) | json
|
|
1
|
+
// rows -> table (default) | json | csv
|
|
2
2
|
// Warned about on stderr (keeps --format json stdout machine-readable), never truncated.
|
|
3
3
|
const OVERSIZE_BYTES = 50000;
|
|
4
4
|
// Flatten embedded newlines so they can't break table column alignment; JSON output is left alone.
|
|
5
5
|
function cell(value) {
|
|
6
6
|
return String(value !== null && value !== void 0 ? value : '').replace(/\s*\n\s*/g, ' ');
|
|
7
7
|
}
|
|
8
|
-
|
|
8
|
+
// RFC 4180, and deliberately not cell(): csv is the format for redirecting a result to a
|
|
9
|
+
// file, so it keeps every character, embedded newlines included. What it cannot carry is
|
|
10
|
+
// NULL vs empty string, or a value's type -- that is what json is for.
|
|
11
|
+
function csvField(value) {
|
|
12
|
+
const text = value === null || value === undefined ? '' : String(value);
|
|
13
|
+
return /["\r\n,]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text;
|
|
14
|
+
}
|
|
15
|
+
function csvLine(values) {
|
|
16
|
+
return values.map(csvField).join(',');
|
|
17
|
+
}
|
|
18
|
+
function warnOversize(bytes, rowCount) {
|
|
19
|
+
if (bytes <= OVERSIZE_BYTES) return;
|
|
20
|
+
console.warn(`warning: result is ${Math.round(bytes / 1000)} KB across ${rowCount} row(s); consider a LIMIT, fewer columns, snippet() instead of whole values, or --format csv redirected to a file`);
|
|
21
|
+
}
|
|
22
|
+
// Header names come from a row wherever there is one: a statement's column list keeps
|
|
23
|
+
// duplicates that the row object has already collapsed, so `SELECT a AS x, b AS x` would print
|
|
24
|
+
// b's value under both headers. `columns` is the fallback that labels a 0-row result, and with
|
|
25
|
+
// no rows and no statement (a bounded command that found nothing) csv writes nothing at all --
|
|
26
|
+
// a bare newline would read to a csv parser as one empty record.
|
|
27
|
+
export function printRows(rows, format, columns) {
|
|
28
|
+
if (format === 'csv') {
|
|
29
|
+
// Collapsed the same way a row object collapses them, so a statement naming a column
|
|
30
|
+
// twice describes itself identically whether it matched anything or not.
|
|
31
|
+
const names = rows.length > 0 ? Object.keys(rows[0]) : [
|
|
32
|
+
...new Set(columns !== null && columns !== void 0 ? columns : [])
|
|
33
|
+
];
|
|
34
|
+
if (names.length === 0) return;
|
|
35
|
+
const rendered = [
|
|
36
|
+
csvLine(names),
|
|
37
|
+
...rows.map((row)=>csvLine(names.map((name)=>row[name])))
|
|
38
|
+
].join('\n');
|
|
39
|
+
console.log(rendered);
|
|
40
|
+
// Bytes, not code units -- CJK is 3 bytes per character.
|
|
41
|
+
warnOversize(Buffer.byteLength(rendered), rows.length);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
9
44
|
const rendered = renderRows(rows, format);
|
|
10
45
|
console.log(rendered);
|
|
11
|
-
|
|
12
|
-
|
|
46
|
+
warnOversize(Buffer.byteLength(rendered), rows.length);
|
|
47
|
+
}
|
|
48
|
+
// Streaming counterpart for the one unbounded caller, `sense sql`: rows arrive from an
|
|
49
|
+
// iterator, so a large result is never held here as rows and again as a rendered string.
|
|
50
|
+
// Table buffers by necessity (a column is as wide as its widest cell, which the last row can
|
|
51
|
+
// change); json and csv write row by row. The writes are synchronous, so when stdout is a pipe
|
|
52
|
+
// Node buffers what the reader has not taken yet -- the saving is this module's copies, not
|
|
53
|
+
// the operating system's.
|
|
54
|
+
export function printRowStream(rows, format, columns) {
|
|
55
|
+
const iterator = rows[Symbol.iterator]();
|
|
56
|
+
// One step before any write. FTS5 parses a MATCH expression at step time rather than at
|
|
57
|
+
// prepare time, so a bad search string throws here, with stdout still clean for the error.
|
|
58
|
+
const first = iterator.next();
|
|
59
|
+
const rest = {
|
|
60
|
+
[Symbol.iterator]: ()=>iterator
|
|
61
|
+
};
|
|
62
|
+
if (format === 'table') {
|
|
63
|
+
printRows(first.done ? [] : [
|
|
64
|
+
first.value,
|
|
65
|
+
...rest
|
|
66
|
+
], format, columns);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
let bytes = 0;
|
|
70
|
+
let rowCount = 0;
|
|
71
|
+
const write = (text)=>{
|
|
72
|
+
// Bytes, not code units -- CJK is 3 bytes per character.
|
|
73
|
+
bytes += Buffer.byteLength(text);
|
|
74
|
+
process.stdout.write(text);
|
|
75
|
+
};
|
|
76
|
+
if (format === 'json') {
|
|
77
|
+
// Byte-identical to JSON.stringify(rows, null, 2) + newline: each row re-indented one
|
|
78
|
+
// level, comma-joined, inside the array brackets written here.
|
|
79
|
+
// JSON.stringify throws on BigInt, so a row with an int64 past 2^53 (setReadBigInts) is
|
|
80
|
+
// converted via the replacer: a value that fits a safe integer stays a number, a larger one
|
|
81
|
+
// becomes its decimal string, since a JSON number cannot carry it losslessly.
|
|
82
|
+
const MAX_SAFE = BigInt(Number.MAX_SAFE_INTEGER);
|
|
83
|
+
const bigintSafe = (_key, value)=>typeof value === 'bigint' ? value >= -MAX_SAFE && value <= MAX_SAFE ? Number(value) : value.toString() : value;
|
|
84
|
+
const indent = (row)=>JSON.stringify(row, bigintSafe, 2).split('\n').map((line)=>` ${line}`).join('\n');
|
|
85
|
+
if (first.done) {
|
|
86
|
+
write('[]\n');
|
|
87
|
+
} else {
|
|
88
|
+
write('[\n');
|
|
89
|
+
write(indent(first.value));
|
|
90
|
+
rowCount = 1;
|
|
91
|
+
for (const row of rest){
|
|
92
|
+
write(',\n');
|
|
93
|
+
write(indent(row));
|
|
94
|
+
rowCount++;
|
|
95
|
+
}
|
|
96
|
+
write('\n]\n');
|
|
97
|
+
}
|
|
98
|
+
} else if (first.done) {
|
|
99
|
+
const names = [
|
|
100
|
+
...new Set(columns)
|
|
101
|
+
];
|
|
102
|
+
if (names.length > 0) write(`${csvLine(names)}\n`);
|
|
103
|
+
} else {
|
|
104
|
+
const names = Object.keys(first.value);
|
|
105
|
+
write(`${csvLine(names)}\n`);
|
|
106
|
+
write(`${csvLine(names.map((name)=>first.value[name]))}\n`);
|
|
107
|
+
rowCount = 1;
|
|
108
|
+
for (const row of rest){
|
|
109
|
+
write(`${csvLine(names.map((name)=>row[name]))}\n`);
|
|
110
|
+
rowCount++;
|
|
111
|
+
}
|
|
13
112
|
}
|
|
113
|
+
warnOversize(bytes, rowCount);
|
|
14
114
|
}
|
|
15
115
|
// Tables are for reading, and a row wider than the terminal wraps unreadably: widest columns
|
|
16
116
|
// give up space first, down to a floor. --format json is never truncated.
|