latticesql 1.2.5 → 1.3.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 +139 -0
- package/dist/cli.js +265 -3
- package/dist/index.cjs +269 -3
- package/dist/index.d.cts +205 -1
- package/dist/index.d.ts +205 -1
- package/dist/index.js +267 -3
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -35,6 +35,7 @@ __export(index_exports, {
|
|
|
35
35
|
InMemoryStateStore: () => InMemoryStateStore,
|
|
36
36
|
Lattice: () => Lattice,
|
|
37
37
|
READ_ONLY_HEADER: () => READ_ONLY_HEADER,
|
|
38
|
+
applyTokenBudget: () => applyTokenBudget,
|
|
38
39
|
applyWriteEntry: () => applyWriteEntry,
|
|
39
40
|
autoUpdate: () => autoUpdate,
|
|
40
41
|
contentHash: () => contentHash,
|
|
@@ -44,6 +45,7 @@ __export(index_exports, {
|
|
|
44
45
|
deriveKey: () => deriveKey,
|
|
45
46
|
encrypt: () => encrypt,
|
|
46
47
|
entityFileNames: () => entityFileNames,
|
|
48
|
+
estimateTokens: () => estimateTokens,
|
|
47
49
|
fixSchemaConflicts: () => fixSchemaConflicts,
|
|
48
50
|
frontmatter: () => frontmatter,
|
|
49
51
|
generateEntryId: () => generateEntryId,
|
|
@@ -399,6 +401,55 @@ var Sanitizer = class {
|
|
|
399
401
|
var import_node_path4 = require("path");
|
|
400
402
|
var import_node_fs4 = require("fs");
|
|
401
403
|
|
|
404
|
+
// src/render/token-budget.ts
|
|
405
|
+
function estimateTokens(text) {
|
|
406
|
+
return Math.ceil(text.length / 4);
|
|
407
|
+
}
|
|
408
|
+
function applyTokenBudget(rows, renderFn, budget, prioritizeBy) {
|
|
409
|
+
const fullContent = renderFn(rows);
|
|
410
|
+
if (estimateTokens(fullContent) <= budget) return fullContent;
|
|
411
|
+
if (rows.length === 0) return fullContent;
|
|
412
|
+
const prioritized = [...rows];
|
|
413
|
+
if (typeof prioritizeBy === "function") {
|
|
414
|
+
prioritized.sort(prioritizeBy);
|
|
415
|
+
} else if (typeof prioritizeBy === "string") {
|
|
416
|
+
const col = prioritizeBy;
|
|
417
|
+
prioritized.sort((a, b) => {
|
|
418
|
+
const va = a[col];
|
|
419
|
+
const vb = b[col];
|
|
420
|
+
if (va == null && vb == null) return 0;
|
|
421
|
+
if (va == null) return 1;
|
|
422
|
+
if (vb == null) return -1;
|
|
423
|
+
if (va < vb) return 1;
|
|
424
|
+
if (va > vb) return -1;
|
|
425
|
+
return 0;
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
let lo = 0;
|
|
429
|
+
let hi = prioritized.length;
|
|
430
|
+
let bestContent = "";
|
|
431
|
+
let bestCount = 0;
|
|
432
|
+
while (lo < hi) {
|
|
433
|
+
const mid = Math.ceil((lo + hi) / 2);
|
|
434
|
+
const content = renderFn(prioritized.slice(0, mid));
|
|
435
|
+
if (estimateTokens(content) <= budget) {
|
|
436
|
+
bestContent = content;
|
|
437
|
+
bestCount = mid;
|
|
438
|
+
lo = mid;
|
|
439
|
+
if (lo === hi) break;
|
|
440
|
+
} else {
|
|
441
|
+
hi = mid - 1;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
if (bestCount === 0) {
|
|
445
|
+
bestContent = renderFn([]);
|
|
446
|
+
}
|
|
447
|
+
const tokens = estimateTokens(bestContent);
|
|
448
|
+
return bestContent + `
|
|
449
|
+
|
|
450
|
+
[truncated: ${bestCount} of ${rows.length} rows rendered, ~${tokens} tokens]`;
|
|
451
|
+
}
|
|
452
|
+
|
|
402
453
|
// src/render/entity-query.ts
|
|
403
454
|
var SAFE_COL_RE = /^[a-zA-Z0-9_]+$/;
|
|
404
455
|
function effectiveFilters(opts) {
|
|
@@ -833,9 +884,11 @@ function cleanupEntityContexts(outputDir, entityContexts, currentSlugsByTable, m
|
|
|
833
884
|
var RenderEngine = class {
|
|
834
885
|
_schema;
|
|
835
886
|
_adapter;
|
|
836
|
-
|
|
887
|
+
_getTaskContext;
|
|
888
|
+
constructor(schema, adapter, getTaskContext) {
|
|
837
889
|
this._schema = schema;
|
|
838
890
|
this._adapter = adapter;
|
|
891
|
+
this._getTaskContext = getTaskContext ?? (() => "");
|
|
839
892
|
}
|
|
840
893
|
async render(outputDir) {
|
|
841
894
|
const start = Date.now();
|
|
@@ -843,8 +896,38 @@ var RenderEngine = class {
|
|
|
843
896
|
const counters = { skipped: 0 };
|
|
844
897
|
for (const [name, def] of this._schema.getTables()) {
|
|
845
898
|
let rows = this._schema.queryTable(this._adapter, name);
|
|
899
|
+
if (def.relevanceFilter) {
|
|
900
|
+
const ctx = this._getTaskContext();
|
|
901
|
+
rows = rows.filter((row) => def.relevanceFilter(row, ctx));
|
|
902
|
+
}
|
|
846
903
|
if (def.filter) rows = def.filter(rows);
|
|
847
|
-
|
|
904
|
+
if (def.rewardTracking) {
|
|
905
|
+
if (def.pruneBelow !== void 0) {
|
|
906
|
+
const threshold = def.pruneBelow;
|
|
907
|
+
const toPrune = rows.filter(
|
|
908
|
+
(r) => r._reward_count > 0 && r._reward_total < threshold
|
|
909
|
+
);
|
|
910
|
+
if (toPrune.length > 0) {
|
|
911
|
+
for (const r of toPrune) {
|
|
912
|
+
const pkCol = this._schema.getPrimaryKey(name)[0] ?? "id";
|
|
913
|
+
this._adapter.run(
|
|
914
|
+
`UPDATE "${name}" SET deleted_at = datetime('now') WHERE "${pkCol}" = ?`,
|
|
915
|
+
[r[pkCol]]
|
|
916
|
+
);
|
|
917
|
+
}
|
|
918
|
+
rows = rows.filter(
|
|
919
|
+
(r) => r._reward_count === 0 || r._reward_total >= threshold
|
|
920
|
+
);
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
if (!def.prioritizeBy) {
|
|
924
|
+
rows.sort((a, b) => (b._reward_total ?? 0) - (a._reward_total ?? 0));
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
if (def.enrich) {
|
|
928
|
+
for (const fn of def.enrich) rows = fn(rows);
|
|
929
|
+
}
|
|
930
|
+
const content = def.tokenBudget ? applyTokenBudget(rows, def.render, def.tokenBudget, def.prioritizeBy) : def.render(rows);
|
|
848
931
|
const filePath = (0, import_node_path4.join)(outputDir, def.outputFile);
|
|
849
932
|
if (atomicWrite(filePath, content)) {
|
|
850
933
|
filesWritten.push(filePath);
|
|
@@ -1304,6 +1387,14 @@ var WritebackPipeline = class {
|
|
|
1304
1387
|
if (store.isSeen(filePath, key)) continue;
|
|
1305
1388
|
store.markSeen(filePath, key);
|
|
1306
1389
|
}
|
|
1390
|
+
if (def.validate) {
|
|
1391
|
+
const result = await def.validate(entry);
|
|
1392
|
+
const threshold = def.rejectBelow ?? 0;
|
|
1393
|
+
if (!result.pass || result.score < threshold) {
|
|
1394
|
+
def.onReject?.(entry, result);
|
|
1395
|
+
continue;
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1307
1398
|
await def.persist(entry, filePath);
|
|
1308
1399
|
processed++;
|
|
1309
1400
|
}
|
|
@@ -1710,6 +1801,73 @@ function resolveEncryptedColumns(encrypted, allColumns) {
|
|
|
1710
1801
|
return new Set(allColumns.filter((c) => !SKIP_COLUMNS.has(c)));
|
|
1711
1802
|
}
|
|
1712
1803
|
|
|
1804
|
+
// src/search/embeddings.ts
|
|
1805
|
+
var EMBEDDINGS_TABLE = "_lattice_embeddings";
|
|
1806
|
+
function ensureEmbeddingsTable(adapter) {
|
|
1807
|
+
adapter.run(`CREATE TABLE IF NOT EXISTS "${EMBEDDINGS_TABLE}" (
|
|
1808
|
+
"table_name" TEXT NOT NULL,
|
|
1809
|
+
"row_pk" TEXT NOT NULL,
|
|
1810
|
+
"embedding" TEXT NOT NULL,
|
|
1811
|
+
PRIMARY KEY ("table_name", "row_pk")
|
|
1812
|
+
)`);
|
|
1813
|
+
}
|
|
1814
|
+
async function storeEmbedding(adapter, table, pk, row, config) {
|
|
1815
|
+
const text = config.fields.map((f) => {
|
|
1816
|
+
const v = row[f];
|
|
1817
|
+
return v == null ? "" : String(v);
|
|
1818
|
+
}).filter((s) => s.length > 0).join(" ");
|
|
1819
|
+
if (text.length === 0) return;
|
|
1820
|
+
const vector = await config.embed(text);
|
|
1821
|
+
adapter.run(
|
|
1822
|
+
`INSERT OR REPLACE INTO "${EMBEDDINGS_TABLE}" ("table_name", "row_pk", "embedding") VALUES (?, ?, ?)`,
|
|
1823
|
+
[table, pk, JSON.stringify(vector)]
|
|
1824
|
+
);
|
|
1825
|
+
}
|
|
1826
|
+
function removeEmbedding(adapter, table, pk) {
|
|
1827
|
+
adapter.run(
|
|
1828
|
+
`DELETE FROM "${EMBEDDINGS_TABLE}" WHERE "table_name" = ? AND "row_pk" = ?`,
|
|
1829
|
+
[table, pk]
|
|
1830
|
+
);
|
|
1831
|
+
}
|
|
1832
|
+
function cosineSimilarity(a, b) {
|
|
1833
|
+
const len = Math.min(a.length, b.length);
|
|
1834
|
+
let dot = 0;
|
|
1835
|
+
let magA = 0;
|
|
1836
|
+
let magB = 0;
|
|
1837
|
+
for (let i = 0; i < len; i++) {
|
|
1838
|
+
dot += a[i] * b[i];
|
|
1839
|
+
magA += a[i] * a[i];
|
|
1840
|
+
magB += b[i] * b[i];
|
|
1841
|
+
}
|
|
1842
|
+
const denom = Math.sqrt(magA) * Math.sqrt(magB);
|
|
1843
|
+
return denom === 0 ? 0 : dot / denom;
|
|
1844
|
+
}
|
|
1845
|
+
async function searchByEmbedding(adapter, table, queryText, config, topK, minScore, pkColumn = "id") {
|
|
1846
|
+
const queryVector = await config.embed(queryText);
|
|
1847
|
+
const stored = adapter.all(
|
|
1848
|
+
`SELECT "row_pk", "embedding" FROM "${EMBEDDINGS_TABLE}" WHERE "table_name" = ?`,
|
|
1849
|
+
[table]
|
|
1850
|
+
);
|
|
1851
|
+
const scored = [];
|
|
1852
|
+
for (const entry of stored) {
|
|
1853
|
+
const vec = JSON.parse(entry.embedding);
|
|
1854
|
+
const score = cosineSimilarity(queryVector, vec);
|
|
1855
|
+
if (score >= minScore) {
|
|
1856
|
+
scored.push({ pk: entry.row_pk, score });
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
scored.sort((a, b) => b.score - a.score);
|
|
1860
|
+
const topResults = scored.slice(0, topK);
|
|
1861
|
+
const results = [];
|
|
1862
|
+
for (const { pk, score } of topResults) {
|
|
1863
|
+
const row = adapter.get(`SELECT * FROM "${table}" WHERE "${pkColumn}" = ?`, [pk]);
|
|
1864
|
+
if (row) {
|
|
1865
|
+
results.push({ row, score });
|
|
1866
|
+
}
|
|
1867
|
+
}
|
|
1868
|
+
return results;
|
|
1869
|
+
}
|
|
1870
|
+
|
|
1713
1871
|
// src/lattice.ts
|
|
1714
1872
|
var Lattice = class {
|
|
1715
1873
|
_adapter;
|
|
@@ -1728,6 +1886,8 @@ var Lattice = class {
|
|
|
1728
1886
|
_encryptedTableColumns = /* @__PURE__ */ new Map();
|
|
1729
1887
|
/** Raw encryption key passphrase from constructor options. */
|
|
1730
1888
|
_encryptionKeyRaw;
|
|
1889
|
+
/** Current task context string for relevance filtering. */
|
|
1890
|
+
_taskContext = "";
|
|
1731
1891
|
_auditHandlers = [];
|
|
1732
1892
|
_renderHandlers = [];
|
|
1733
1893
|
_writebackHandlers = [];
|
|
@@ -1754,7 +1914,7 @@ var Lattice = class {
|
|
|
1754
1914
|
this._adapter = new SQLiteAdapter(dbPath, adapterOpts);
|
|
1755
1915
|
this._schema = new SchemaManager();
|
|
1756
1916
|
this._sanitizer = new Sanitizer(options.security);
|
|
1757
|
-
this._render = new RenderEngine(this._schema, this._adapter);
|
|
1917
|
+
this._render = new RenderEngine(this._schema, this._adapter, () => this._taskContext);
|
|
1758
1918
|
this._reverseSync = new ReverseSyncEngine(this._schema, this._adapter);
|
|
1759
1919
|
this._loop = new SyncLoop(this._render);
|
|
1760
1920
|
this._writeback = new WritebackPipeline();
|
|
@@ -1778,8 +1938,10 @@ var Lattice = class {
|
|
|
1778
1938
|
// -------------------------------------------------------------------------
|
|
1779
1939
|
define(table, def) {
|
|
1780
1940
|
this._assertNotInit("define");
|
|
1941
|
+
const columns = def.rewardTracking ? { ...def.columns, _reward_total: "REAL DEFAULT 0", _reward_count: "INTEGER DEFAULT 0" } : def.columns;
|
|
1781
1942
|
const compiledDef = {
|
|
1782
1943
|
...def,
|
|
1944
|
+
columns,
|
|
1783
1945
|
render: def.render ? compileRender(
|
|
1784
1946
|
def,
|
|
1785
1947
|
table,
|
|
@@ -1825,6 +1987,10 @@ var Lattice = class {
|
|
|
1825
1987
|
const rows = this._adapter.all(`PRAGMA table_info("${tableName}")`);
|
|
1826
1988
|
this._columnCache.set(tableName, new Set(rows.map((r) => r.name)));
|
|
1827
1989
|
}
|
|
1990
|
+
const hasEmbeddings = [...this._schema.getTables().values()].some((d) => d.embeddings);
|
|
1991
|
+
if (hasEmbeddings) {
|
|
1992
|
+
ensureEmbeddingsTable(this._adapter);
|
|
1993
|
+
}
|
|
1828
1994
|
this._setupEncryption();
|
|
1829
1995
|
this._initialized = true;
|
|
1830
1996
|
return Promise.resolve();
|
|
@@ -1854,6 +2020,21 @@ var Lattice = class {
|
|
|
1854
2020
|
this._initialized = false;
|
|
1855
2021
|
}
|
|
1856
2022
|
// -------------------------------------------------------------------------
|
|
2023
|
+
// Task context (for relevance filtering)
|
|
2024
|
+
// -------------------------------------------------------------------------
|
|
2025
|
+
/**
|
|
2026
|
+
* Set the current task context string. Tables with a `relevanceFilter`
|
|
2027
|
+
* will use this value to filter rows before rendering.
|
|
2028
|
+
*/
|
|
2029
|
+
setTaskContext(context) {
|
|
2030
|
+
this._taskContext = context;
|
|
2031
|
+
return this;
|
|
2032
|
+
}
|
|
2033
|
+
/** Return the current task context string. */
|
|
2034
|
+
getTaskContext() {
|
|
2035
|
+
return this._taskContext;
|
|
2036
|
+
}
|
|
2037
|
+
// -------------------------------------------------------------------------
|
|
1857
2038
|
// Encryption helpers
|
|
1858
2039
|
// -------------------------------------------------------------------------
|
|
1859
2040
|
_setupEncryption() {
|
|
@@ -1928,6 +2109,7 @@ var Lattice = class {
|
|
|
1928
2109
|
const pkValue = rawPk != null ? String(rawPk) : "";
|
|
1929
2110
|
this._sanitizer.emitAudit(table, "insert", pkValue);
|
|
1930
2111
|
this._fireWriteHooks(table, "insert", rowWithPk, pkValue);
|
|
2112
|
+
this._syncEmbedding(table, "insert", rowWithPk, pkValue);
|
|
1931
2113
|
return Promise.resolve(pkValue);
|
|
1932
2114
|
}
|
|
1933
2115
|
/**
|
|
@@ -1995,6 +2177,11 @@ var Lattice = class {
|
|
|
1995
2177
|
const auditId = typeof id === "string" ? id : JSON.stringify(id);
|
|
1996
2178
|
this._sanitizer.emitAudit(table, "update", auditId);
|
|
1997
2179
|
this._fireWriteHooks(table, "update", sanitized, auditId, Object.keys(sanitized));
|
|
2180
|
+
const def = this._schema.getTables().get(table);
|
|
2181
|
+
if (def?.embeddings) {
|
|
2182
|
+
const fullRow = this._adapter.get(`SELECT * FROM "${table}" WHERE ${clause}`, pkParams);
|
|
2183
|
+
if (fullRow) this._syncEmbedding(table, "update", fullRow, auditId);
|
|
2184
|
+
}
|
|
1998
2185
|
return Promise.resolve();
|
|
1999
2186
|
}
|
|
2000
2187
|
/**
|
|
@@ -2016,6 +2203,7 @@ var Lattice = class {
|
|
|
2016
2203
|
const auditId = typeof id === "string" ? id : JSON.stringify(id);
|
|
2017
2204
|
this._sanitizer.emitAudit(table, "delete", auditId);
|
|
2018
2205
|
this._fireWriteHooks(table, "delete", { id: auditId }, auditId);
|
|
2206
|
+
this._syncEmbedding(table, "delete", {}, auditId);
|
|
2019
2207
|
return Promise.resolve();
|
|
2020
2208
|
}
|
|
2021
2209
|
get(table, id) {
|
|
@@ -2399,6 +2587,65 @@ var Lattice = class {
|
|
|
2399
2587
|
const ms = unit === "h" ? num * 36e5 : unit === "d" ? num * 864e5 : num * 6e4;
|
|
2400
2588
|
return new Date(Date.now() - ms).toISOString();
|
|
2401
2589
|
}
|
|
2590
|
+
// -------------------------------------------------------------------------
|
|
2591
|
+
// Reward tracking
|
|
2592
|
+
// -------------------------------------------------------------------------
|
|
2593
|
+
/**
|
|
2594
|
+
* Update reward scores for a row. The total reward is recalculated as
|
|
2595
|
+
* the running average across all reward calls. Requires `rewardTracking`
|
|
2596
|
+
* on the table definition.
|
|
2597
|
+
*/
|
|
2598
|
+
reward(table, id, scores) {
|
|
2599
|
+
const notInit = this._notInitError();
|
|
2600
|
+
if (notInit) return notInit;
|
|
2601
|
+
const def = this._schema.getTables().get(table);
|
|
2602
|
+
if (!def?.rewardTracking) {
|
|
2603
|
+
return Promise.reject(
|
|
2604
|
+
new Error(`Table "${table}" does not have rewardTracking enabled`)
|
|
2605
|
+
);
|
|
2606
|
+
}
|
|
2607
|
+
const vals = Object.values(scores);
|
|
2608
|
+
if (vals.length === 0) return Promise.resolve();
|
|
2609
|
+
const avg = vals.reduce((a, b) => a + b, 0) / vals.length;
|
|
2610
|
+
const { clause, params: pkParams } = this._pkWhere(table, id);
|
|
2611
|
+
this._adapter.run(
|
|
2612
|
+
`UPDATE "${table}" SET "_reward_total" = ("_reward_total" * "_reward_count" + ?) / ("_reward_count" + 1), "_reward_count" = "_reward_count" + 1 WHERE ${clause}`,
|
|
2613
|
+
[avg, ...pkParams]
|
|
2614
|
+
);
|
|
2615
|
+
return Promise.resolve();
|
|
2616
|
+
}
|
|
2617
|
+
// -------------------------------------------------------------------------
|
|
2618
|
+
// Semantic search
|
|
2619
|
+
// -------------------------------------------------------------------------
|
|
2620
|
+
/**
|
|
2621
|
+
* Search for rows by semantic similarity. Requires `embeddings` config
|
|
2622
|
+
* on the table definition.
|
|
2623
|
+
*
|
|
2624
|
+
* @param table - Table to search
|
|
2625
|
+
* @param query - Natural-language query text
|
|
2626
|
+
* @param opts - Search options (topK, minScore)
|
|
2627
|
+
* @returns Matching rows with similarity scores, sorted best-first.
|
|
2628
|
+
*/
|
|
2629
|
+
async search(table, query, opts = {}) {
|
|
2630
|
+
const notInit = this._notInitError();
|
|
2631
|
+
if (notInit) return notInit;
|
|
2632
|
+
const def = this._schema.getTables().get(table);
|
|
2633
|
+
if (!def?.embeddings) {
|
|
2634
|
+
return Promise.reject(
|
|
2635
|
+
new Error(`Table "${table}" does not have embeddings configured`)
|
|
2636
|
+
);
|
|
2637
|
+
}
|
|
2638
|
+
const pkCol = this._schema.getPrimaryKey(table)[0] ?? "id";
|
|
2639
|
+
return searchByEmbedding(
|
|
2640
|
+
this._adapter,
|
|
2641
|
+
table,
|
|
2642
|
+
query,
|
|
2643
|
+
def.embeddings,
|
|
2644
|
+
opts.topK ?? 10,
|
|
2645
|
+
opts.minScore ?? 0,
|
|
2646
|
+
pkCol
|
|
2647
|
+
);
|
|
2648
|
+
}
|
|
2402
2649
|
query(table, opts = {}) {
|
|
2403
2650
|
const notInit = this._notInitError();
|
|
2404
2651
|
if (notInit) return notInit;
|
|
@@ -2657,6 +2904,23 @@ var Lattice = class {
|
|
|
2657
2904
|
}
|
|
2658
2905
|
}
|
|
2659
2906
|
}
|
|
2907
|
+
/**
|
|
2908
|
+
* Update or remove the embedding for a row.
|
|
2909
|
+
* No-op if the table doesn't have `embeddings` configured.
|
|
2910
|
+
*/
|
|
2911
|
+
_syncEmbedding(table, op, row, pk) {
|
|
2912
|
+
const def = this._schema.getTables().get(table);
|
|
2913
|
+
if (!def?.embeddings) return;
|
|
2914
|
+
if (op === "delete") {
|
|
2915
|
+
removeEmbedding(this._adapter, table, pk);
|
|
2916
|
+
return;
|
|
2917
|
+
}
|
|
2918
|
+
storeEmbedding(this._adapter, table, pk, row, def.embeddings).catch((err) => {
|
|
2919
|
+
for (const h of this._errorHandlers) {
|
|
2920
|
+
h(err instanceof Error ? err : new Error(String(err)));
|
|
2921
|
+
}
|
|
2922
|
+
});
|
|
2923
|
+
}
|
|
2660
2924
|
_notInitError() {
|
|
2661
2925
|
if (!this._initialized) {
|
|
2662
2926
|
return Promise.reject(
|
|
@@ -3198,6 +3462,7 @@ async function autoUpdate(opts) {
|
|
|
3198
3462
|
InMemoryStateStore,
|
|
3199
3463
|
Lattice,
|
|
3200
3464
|
READ_ONLY_HEADER,
|
|
3465
|
+
applyTokenBudget,
|
|
3201
3466
|
applyWriteEntry,
|
|
3202
3467
|
autoUpdate,
|
|
3203
3468
|
contentHash,
|
|
@@ -3207,6 +3472,7 @@ async function autoUpdate(opts) {
|
|
|
3207
3472
|
deriveKey,
|
|
3208
3473
|
encrypt,
|
|
3209
3474
|
entityFileNames,
|
|
3475
|
+
estimateTokens,
|
|
3210
3476
|
fixSchemaConflicts,
|
|
3211
3477
|
frontmatter,
|
|
3212
3478
|
generateEntryId,
|
package/dist/index.d.cts
CHANGED
|
@@ -812,6 +812,90 @@ interface TableDefinition {
|
|
|
812
812
|
* ```
|
|
813
813
|
*/
|
|
814
814
|
relations?: Record<string, Relation>;
|
|
815
|
+
/**
|
|
816
|
+
* Enable semantic search for this table via embeddings.
|
|
817
|
+
*
|
|
818
|
+
* When configured, Lattice computes and stores vector embeddings for
|
|
819
|
+
* the specified text fields. Use `lattice.search(table, query, opts)`
|
|
820
|
+
* to retrieve rows by semantic similarity.
|
|
821
|
+
*
|
|
822
|
+
* The `embed` function is called to generate vectors — bring your own
|
|
823
|
+
* embedding model (OpenAI, local model, etc.).
|
|
824
|
+
*
|
|
825
|
+
* @example
|
|
826
|
+
* ```ts
|
|
827
|
+
* embeddings: {
|
|
828
|
+
* fields: ['title', 'body'],
|
|
829
|
+
* embed: async (text) => openai.embeddings.create({ input: text, model: 'text-embedding-3-small' }).then(r => r.data[0].embedding),
|
|
830
|
+
* }
|
|
831
|
+
* ```
|
|
832
|
+
*/
|
|
833
|
+
embeddings?: EmbeddingsConfig;
|
|
834
|
+
/**
|
|
835
|
+
* Enable reward tracking for this table. When `true`, Lattice
|
|
836
|
+
* auto-adds `_reward_total REAL DEFAULT 0` and `_reward_count INTEGER
|
|
837
|
+
* DEFAULT 0` columns. Rows are sorted by `_reward_total DESC` before
|
|
838
|
+
* rendering (unless overridden by `prioritizeBy`).
|
|
839
|
+
*
|
|
840
|
+
* Use `lattice.reward(table, id, scores)` to update reward values.
|
|
841
|
+
*/
|
|
842
|
+
rewardTracking?: boolean;
|
|
843
|
+
/**
|
|
844
|
+
* When `rewardTracking` is enabled, automatically soft-delete rows
|
|
845
|
+
* whose `_reward_total` falls below this threshold during rendering.
|
|
846
|
+
* Requires a `deleted_at` column on the table. Default: no pruning.
|
|
847
|
+
*/
|
|
848
|
+
pruneBelow?: number;
|
|
849
|
+
/**
|
|
850
|
+
* Pipeline of enrichment functions applied to rows after query and
|
|
851
|
+
* filtering but before rendering. Each function receives the row
|
|
852
|
+
* array and returns a (possibly transformed) row array.
|
|
853
|
+
*
|
|
854
|
+
* Use enrichment hooks to cluster, annotate, summarize, or
|
|
855
|
+
* cross-reference rows without modifying the underlying data.
|
|
856
|
+
*
|
|
857
|
+
* @example
|
|
858
|
+
* ```ts
|
|
859
|
+
* enrich: [
|
|
860
|
+
* (rows) => rows.map(r => ({ ...r, _age: Date.now() - new Date(r.created_at as string).getTime() })),
|
|
861
|
+
* (rows) => rows.length > 50 ? [{ summary: `${rows.length} items` }] : rows,
|
|
862
|
+
* ]
|
|
863
|
+
* ```
|
|
864
|
+
*/
|
|
865
|
+
enrich?: ((rows: Row[]) => Row[])[];
|
|
866
|
+
/**
|
|
867
|
+
* Dynamic filter that scores rows against the current task context
|
|
868
|
+
* (set via `lattice.setTaskContext()`). Called before `filter` and
|
|
869
|
+
* before rendering. Only rows for which the function returns `true`
|
|
870
|
+
* are included.
|
|
871
|
+
*
|
|
872
|
+
* The second argument is the current task-context string (empty string
|
|
873
|
+
* when none has been set).
|
|
874
|
+
*
|
|
875
|
+
* @example
|
|
876
|
+
* ```ts
|
|
877
|
+
* relevanceFilter: (row, ctx) =>
|
|
878
|
+
* ctx ? String(row.body).toLowerCase().includes(ctx.toLowerCase()) : true
|
|
879
|
+
* ```
|
|
880
|
+
*/
|
|
881
|
+
relevanceFilter?: (row: Row, taskContext: string) => boolean;
|
|
882
|
+
/**
|
|
883
|
+
* Maximum estimated token count for the rendered output.
|
|
884
|
+
* When the rendered content exceeds this budget, rows are pruned
|
|
885
|
+
* (lowest-priority first) and re-rendered with a truncation notice.
|
|
886
|
+
*
|
|
887
|
+
* Token count is estimated at ~4 characters per token.
|
|
888
|
+
*/
|
|
889
|
+
tokenBudget?: number;
|
|
890
|
+
/**
|
|
891
|
+
* Controls row priority when `tokenBudget` forces pruning.
|
|
892
|
+
*
|
|
893
|
+
* - `string` — column name to sort by descending (highest value = highest priority).
|
|
894
|
+
* - `(a, b) => number` — custom comparator (positive = a has higher priority).
|
|
895
|
+
*
|
|
896
|
+
* When omitted, rows at the end of the query result are pruned first.
|
|
897
|
+
*/
|
|
898
|
+
prioritizeBy?: string | ((a: Row, b: Row) => number);
|
|
815
899
|
}
|
|
816
900
|
interface MultiTableDefinition {
|
|
817
901
|
/** Returns the "anchor" entities — one output file is produced per anchor */
|
|
@@ -823,6 +907,58 @@ interface MultiTableDefinition {
|
|
|
823
907
|
/** Additional table names to query and pass into render */
|
|
824
908
|
tables?: string[];
|
|
825
909
|
}
|
|
910
|
+
/**
|
|
911
|
+
* Configuration for embedding-based semantic search on a table.
|
|
912
|
+
*/
|
|
913
|
+
interface EmbeddingsConfig {
|
|
914
|
+
/** Column names whose values are concatenated and embedded. */
|
|
915
|
+
fields: string[];
|
|
916
|
+
/**
|
|
917
|
+
* Function that converts text into a numeric vector.
|
|
918
|
+
* Bring your own model — Lattice does not bundle an embedding provider.
|
|
919
|
+
*/
|
|
920
|
+
embed: (text: string) => Promise<number[]>;
|
|
921
|
+
}
|
|
922
|
+
/**
|
|
923
|
+
* Options for `Lattice.search()`.
|
|
924
|
+
*/
|
|
925
|
+
interface SearchOptions {
|
|
926
|
+
/** Maximum number of results to return. Default: 10. */
|
|
927
|
+
topK?: number;
|
|
928
|
+
/**
|
|
929
|
+
* Minimum cosine similarity threshold (0–1). Results below this
|
|
930
|
+
* score are excluded. Default: 0.
|
|
931
|
+
*/
|
|
932
|
+
minScore?: number;
|
|
933
|
+
}
|
|
934
|
+
/**
|
|
935
|
+
* A single search result returned by `Lattice.search()`.
|
|
936
|
+
*/
|
|
937
|
+
interface SearchResult {
|
|
938
|
+
/** The matched row from the source table. */
|
|
939
|
+
row: Row;
|
|
940
|
+
/** Cosine similarity score (0–1). */
|
|
941
|
+
score: number;
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* Dimension scores passed to `Lattice.reward()`.
|
|
945
|
+
* Values should be between 0 and 1. The total reward is the average
|
|
946
|
+
* of all provided dimension scores, accumulated over multiple calls.
|
|
947
|
+
*/
|
|
948
|
+
interface RewardScores {
|
|
949
|
+
[dimension: string]: number;
|
|
950
|
+
}
|
|
951
|
+
/**
|
|
952
|
+
* Result of a writeback validation check.
|
|
953
|
+
*/
|
|
954
|
+
interface WritebackValidationResult {
|
|
955
|
+
/** Whether the entry passed validation. */
|
|
956
|
+
pass: boolean;
|
|
957
|
+
/** Overall quality score (0–1). */
|
|
958
|
+
score: number;
|
|
959
|
+
/** Human-readable reason when validation fails. */
|
|
960
|
+
reason?: string;
|
|
961
|
+
}
|
|
826
962
|
interface WritebackDefinition {
|
|
827
963
|
/** Path or glob to agent-written files */
|
|
828
964
|
file: string;
|
|
@@ -843,6 +979,25 @@ interface WritebackDefinition {
|
|
|
843
979
|
stateStore?: WritebackStateStore;
|
|
844
980
|
/** Called after entries are processed for a file. Useful for archival. */
|
|
845
981
|
onArchive?: (filePath: string) => void;
|
|
982
|
+
/**
|
|
983
|
+
* Optional validation hook. Called before `persist` for each entry.
|
|
984
|
+
* Return `{ pass: true, score }` to allow the write, or
|
|
985
|
+
* `{ pass: false, score, reason }` to reject it.
|
|
986
|
+
*
|
|
987
|
+
* When omitted, all parsed entries are persisted without validation.
|
|
988
|
+
*/
|
|
989
|
+
validate?: (entry: unknown) => WritebackValidationResult | Promise<WritebackValidationResult>;
|
|
990
|
+
/**
|
|
991
|
+
* Minimum score threshold. Entries with `score < rejectBelow` are
|
|
992
|
+
* automatically rejected even if `validate` returns `pass: true`.
|
|
993
|
+
* Only meaningful when `validate` is set. Default: 0 (no threshold).
|
|
994
|
+
*/
|
|
995
|
+
rejectBelow?: number;
|
|
996
|
+
/**
|
|
997
|
+
* Called for each entry that fails validation.
|
|
998
|
+
* Useful for logging or auditing rejected writes.
|
|
999
|
+
*/
|
|
1000
|
+
onReject?: (entry: unknown, result: WritebackValidationResult) => void;
|
|
846
1001
|
}
|
|
847
1002
|
interface QueryOptions {
|
|
848
1003
|
/**
|
|
@@ -1154,6 +1309,8 @@ declare class Lattice {
|
|
|
1154
1309
|
private readonly _encryptedTableColumns;
|
|
1155
1310
|
/** Raw encryption key passphrase from constructor options. */
|
|
1156
1311
|
private readonly _encryptionKeyRaw?;
|
|
1312
|
+
/** Current task context string for relevance filtering. */
|
|
1313
|
+
private _taskContext;
|
|
1157
1314
|
private readonly _auditHandlers;
|
|
1158
1315
|
private readonly _renderHandlers;
|
|
1159
1316
|
private readonly _writebackHandlers;
|
|
@@ -1178,6 +1335,13 @@ declare class Lattice {
|
|
|
1178
1335
|
*/
|
|
1179
1336
|
migrate(migrations: Migration[]): Promise<void>;
|
|
1180
1337
|
close(): void;
|
|
1338
|
+
/**
|
|
1339
|
+
* Set the current task context string. Tables with a `relevanceFilter`
|
|
1340
|
+
* will use this value to filter rows before rendering.
|
|
1341
|
+
*/
|
|
1342
|
+
setTaskContext(context: string): this;
|
|
1343
|
+
/** Return the current task context string. */
|
|
1344
|
+
getTaskContext(): string;
|
|
1181
1345
|
private _setupEncryption;
|
|
1182
1346
|
/** Encrypt applicable columns in a row before writing. Returns a new row. */
|
|
1183
1347
|
private _encryptRow;
|
|
@@ -1258,6 +1422,22 @@ declare class Lattice {
|
|
|
1258
1422
|
buildReport(config: ReportConfig): Promise<ReportResult>;
|
|
1259
1423
|
/** Parse duration shorthand ('8h', '24h', '7d') into ISO timestamp. */
|
|
1260
1424
|
private _resolveSince;
|
|
1425
|
+
/**
|
|
1426
|
+
* Update reward scores for a row. The total reward is recalculated as
|
|
1427
|
+
* the running average across all reward calls. Requires `rewardTracking`
|
|
1428
|
+
* on the table definition.
|
|
1429
|
+
*/
|
|
1430
|
+
reward(table: string, id: PkLookup, scores: RewardScores): Promise<void>;
|
|
1431
|
+
/**
|
|
1432
|
+
* Search for rows by semantic similarity. Requires `embeddings` config
|
|
1433
|
+
* on the table definition.
|
|
1434
|
+
*
|
|
1435
|
+
* @param table - Table to search
|
|
1436
|
+
* @param query - Natural-language query text
|
|
1437
|
+
* @param opts - Search options (topK, minScore)
|
|
1438
|
+
* @returns Matching rows with similarity scores, sorted best-first.
|
|
1439
|
+
*/
|
|
1440
|
+
search(table: string, query: string, opts?: SearchOptions): Promise<SearchResult[]>;
|
|
1261
1441
|
query(table: string, opts?: QueryOptions): Promise<Row[]>;
|
|
1262
1442
|
count(table: string, opts?: CountOptions): Promise<number>;
|
|
1263
1443
|
render(outputDir: string): Promise<RenderResult>;
|
|
@@ -1297,6 +1477,11 @@ declare class Lattice {
|
|
|
1297
1477
|
private _buildFilters;
|
|
1298
1478
|
/** Returns a rejected Promise if not initialized; null if ready. */
|
|
1299
1479
|
private _fireWriteHooks;
|
|
1480
|
+
/**
|
|
1481
|
+
* Update or remove the embedding for a row.
|
|
1482
|
+
* No-op if the table doesn't have `embeddings` configured.
|
|
1483
|
+
*/
|
|
1484
|
+
private _syncEmbedding;
|
|
1300
1485
|
private _notInitError;
|
|
1301
1486
|
/**
|
|
1302
1487
|
* Returns a rejected Promise if any of the given column names are not present
|
|
@@ -1512,6 +1697,25 @@ declare function parseConfigString(yamlContent: string, configDir: string): Pars
|
|
|
1512
1697
|
|
|
1513
1698
|
declare function contentHash(content: string): string;
|
|
1514
1699
|
|
|
1700
|
+
/**
|
|
1701
|
+
* Estimate the number of tokens in a string using a fast heuristic.
|
|
1702
|
+
* Uses ~4 characters per token, a reasonable approximation for English
|
|
1703
|
+
* text with typical LLM tokenizers (BPE/ByteBPE).
|
|
1704
|
+
*
|
|
1705
|
+
* Users who need exact counts should pre-process with their own tokenizer
|
|
1706
|
+
* and use the `filter` hook to manually limit rows.
|
|
1707
|
+
*/
|
|
1708
|
+
declare function estimateTokens(text: string): number;
|
|
1709
|
+
/**
|
|
1710
|
+
* Apply a token budget to rendered output.
|
|
1711
|
+
* If the full render exceeds the budget, rows are sorted by priority,
|
|
1712
|
+
* pruned via binary search, and re-rendered. A truncation footer is
|
|
1713
|
+
* appended so the consuming agent knows context was limited.
|
|
1714
|
+
*
|
|
1715
|
+
* @returns Final content string, possibly with truncation footer.
|
|
1716
|
+
*/
|
|
1717
|
+
declare function applyTokenBudget(rows: Row[], renderFn: (rows: Row[]) => string, budget: number, prioritizeBy?: string | ((a: Row, b: Row) => number)): string;
|
|
1718
|
+
|
|
1515
1719
|
/**
|
|
1516
1720
|
* Fix legacy schema conflicts before Lattice init().
|
|
1517
1721
|
*
|
|
@@ -1827,4 +2031,4 @@ declare function autoUpdate(opts?: {
|
|
|
1827
2031
|
quiet?: boolean;
|
|
1828
2032
|
}): Promise<AutoUpdateResult>;
|
|
1829
2033
|
|
|
1830
|
-
export { type ApplyWriteResult, type AuditEvent, type AutoUpdateResult, type BelongsToRelation, type BelongsToSource, type BuiltinTemplateName, type CleanupOptions, type CleanupResult, type CountOptions, type CustomSource, DEFAULT_ENTRY_TYPES, DEFAULT_TYPE_ALIASES, type EnrichedSource, type EnrichmentLookup, type EntityContextDefinition, type EntityContextManifestEntry, type EntityFileManifestInfo, type EntityFileSource, type EntityFileSpec, type EntityProfileField, type EntityProfileSection, type EntityProfileTemplate, type EntityRenderSpec, type EntityRenderTemplate, type EntitySectionPerRow, type EntitySectionsTemplate, type EntityTableColumn, type EntityTableTemplate, type Filter, type FilterOp, type HasManyRelation, type HasManySource, InMemoryStateStore, type InitOptions, Lattice, type LatticeConfig, type LatticeConfigInput, type LatticeEntityDef, type LatticeEntityRenderSpec, type LatticeFieldDef, type LatticeFieldType, type LatticeManifest, type LatticeOptions, type LinkOptions, type ManyToManySource, type MarkdownTableColumn, type Migration, type MultiTableDefinition, type OrderBySpec, type ParseError, type ParseResult, type ParsedConfig, type PkLookup, type PrimaryKey, type QueryOptions, READ_ONLY_HEADER, type ReadOnlyHeaderOptions, type ReconcileOptions, type ReconcileResult, type Relation, type RenderHooks, type RenderResult, type RenderSpec, type ReportConfig, type ReportResult, type ReportSection, type ReportSectionResult, type ReverseSyncError, type ReverseSyncResult, type ReverseSyncUpdate, type Row, type SecurityOptions, type SeedConfig, type SeedLinkSpec, type SeedResult, type SelfSource, type SessionEntry, type SessionParseOptions, type SessionWriteEntry, type SessionWriteOp, type SessionWriteParseResult, type SourceQueryOptions, type StopFn, type SyncResult, type TableDefinition, type TemplateRenderSpec, type UpsertByNaturalKeyOptions, type WatchOptions, type WriteHook, type WriteHookContext, type WritebackDefinition, type WritebackStateStore, applyWriteEntry, autoUpdate, contentHash, createReadOnlyHeader, createSQLiteStateStore, decrypt, deriveKey, encrypt, entityFileNames, fixSchemaConflicts, frontmatter, generateEntryId, generateWriteEntryId, isEncrypted, isV1EntityFiles, manifestPath, markdownTable, normalizeEntityFiles, parseConfigFile, parseConfigString, parseMarkdownEntries, parseSessionMD, parseSessionWrites, readManifest, slugify, truncate, validateEntryId, writeManifest };
|
|
2034
|
+
export { type ApplyWriteResult, type AuditEvent, type AutoUpdateResult, type BelongsToRelation, type BelongsToSource, type BuiltinTemplateName, type CleanupOptions, type CleanupResult, type CountOptions, type CustomSource, DEFAULT_ENTRY_TYPES, DEFAULT_TYPE_ALIASES, type EmbeddingsConfig, type EnrichedSource, type EnrichmentLookup, type EntityContextDefinition, type EntityContextManifestEntry, type EntityFileManifestInfo, type EntityFileSource, type EntityFileSpec, type EntityProfileField, type EntityProfileSection, type EntityProfileTemplate, type EntityRenderSpec, type EntityRenderTemplate, type EntitySectionPerRow, type EntitySectionsTemplate, type EntityTableColumn, type EntityTableTemplate, type Filter, type FilterOp, type HasManyRelation, type HasManySource, InMemoryStateStore, type InitOptions, Lattice, type LatticeConfig, type LatticeConfigInput, type LatticeEntityDef, type LatticeEntityRenderSpec, type LatticeFieldDef, type LatticeFieldType, type LatticeManifest, type LatticeOptions, type LinkOptions, type ManyToManySource, type MarkdownTableColumn, type Migration, type MultiTableDefinition, type OrderBySpec, type ParseError, type ParseResult, type ParsedConfig, type PkLookup, type PrimaryKey, type QueryOptions, READ_ONLY_HEADER, type ReadOnlyHeaderOptions, type ReconcileOptions, type ReconcileResult, type Relation, type RenderHooks, type RenderResult, type RenderSpec, type ReportConfig, type ReportResult, type ReportSection, type ReportSectionResult, type ReverseSyncError, type ReverseSyncResult, type ReverseSyncUpdate, type RewardScores, type Row, type SearchOptions, type SearchResult, type SecurityOptions, type SeedConfig, type SeedLinkSpec, type SeedResult, type SelfSource, type SessionEntry, type SessionParseOptions, type SessionWriteEntry, type SessionWriteOp, type SessionWriteParseResult, type SourceQueryOptions, type StopFn, type SyncResult, type TableDefinition, type TemplateRenderSpec, type UpsertByNaturalKeyOptions, type WatchOptions, type WriteHook, type WriteHookContext, type WritebackDefinition, type WritebackStateStore, type WritebackValidationResult, applyTokenBudget, applyWriteEntry, autoUpdate, contentHash, createReadOnlyHeader, createSQLiteStateStore, decrypt, deriveKey, encrypt, entityFileNames, estimateTokens, fixSchemaConflicts, frontmatter, generateEntryId, generateWriteEntryId, isEncrypted, isV1EntityFiles, manifestPath, markdownTable, normalizeEntityFiles, parseConfigFile, parseConfigString, parseMarkdownEntries, parseSessionMD, parseSessionWrites, readManifest, slugify, truncate, validateEntryId, writeManifest };
|