@howaboua/pi-durable-context 0.0.0-stage → 0.10.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/LICENSE +21 -0
- package/NOTICE +63 -0
- package/README.md +168 -2
- package/dist/activation.js +2953 -0
- package/dist/history-search-worker.js +85 -0
- package/dist/index.js +2703 -0
- package/dist/internal/context-visibility.d.ts +6 -0
- package/dist/node.js +124 -0
- package/dist/packages/context/src/activation.d.ts +20 -0
- package/dist/packages/context/src/admission.d.ts +23 -0
- package/dist/packages/context/src/history-archive.d.ts +24 -0
- package/dist/packages/context/src/history-items.d.ts +17 -0
- package/dist/packages/context/src/history-ranker.d.ts +27 -0
- package/dist/packages/context/src/history-search-worker.d.ts +15 -0
- package/dist/packages/context/src/history.d.ts +80 -0
- package/dist/packages/context/src/index.d.ts +76 -0
- package/dist/packages/context/src/input-placement.d.ts +6 -0
- package/dist/packages/context/src/instruction-run.d.ts +13 -0
- package/dist/packages/context/src/lifecycle.d.ts +40 -0
- package/dist/packages/context/src/model-roles.d.ts +26 -0
- package/dist/packages/context/src/node.d.ts +10 -0
- package/dist/packages/context/src/note-path.d.ts +2 -0
- package/dist/packages/context/src/note-store.d.ts +53 -0
- package/dist/packages/context/src/notes.d.ts +76 -0
- package/dist/packages/context/src/policy.d.ts +24 -0
- package/dist/packages/context/src/presentation.d.ts +52 -0
- package/dist/packages/context/src/query-input.d.ts +3 -0
- package/dist/packages/context/src/remote.d.ts +34 -0
- package/dist/packages/context/src/rollover-preparation.d.ts +16 -0
- package/dist/packages/context/src/rollover.d.ts +32 -0
- package/dist/packages/context/src/tool-contract.d.ts +85 -0
- package/dist/packages/context/src/tool-rollover.d.ts +41 -0
- package/dist/packages/context/src/usage.d.ts +24 -0
- package/dist/packages/context/src/window-state.d.ts +37 -0
- package/dist/presentation.js +93 -0
- package/package.json +70 -5
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// packages/context/src/history-search-worker.ts
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { mkdirSync } from "node:fs";
|
|
4
|
+
import { dirname } from "node:path";
|
|
5
|
+
import { DatabaseSync } from "node:sqlite";
|
|
6
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
7
|
+
var port = parentPort;
|
|
8
|
+
if (!port)
|
|
9
|
+
throw new Error("History index requires a worker thread");
|
|
10
|
+
var options = workerData;
|
|
11
|
+
var database;
|
|
12
|
+
try {
|
|
13
|
+
mkdirSync(dirname(options.path), { recursive: true, mode: 448 });
|
|
14
|
+
const db = new DatabaseSync(options.path);
|
|
15
|
+
database = db;
|
|
16
|
+
const prepare = (sql) => {
|
|
17
|
+
const statement = db.prepare(sql);
|
|
18
|
+
statement.setReadBigInts(true);
|
|
19
|
+
return statement;
|
|
20
|
+
};
|
|
21
|
+
db.exec("PRAGMA busy_timeout=5000; BEGIN IMMEDIATE");
|
|
22
|
+
db.exec("CREATE TABLE IF NOT EXISTS ranked_history_documents (id INTEGER PRIMARY KEY, scope TEXT NOT NULL, item TEXT NOT NULL, fingerprint TEXT NOT NULL, UNIQUE(scope,item)); CREATE VIRTUAL TABLE IF NOT EXISTS ranked_history_terms USING fts5(content, content='', contentless_delete=1, tokenize='unicode61 remove_diacritics 2')");
|
|
23
|
+
const existing = prepare("SELECT id, fingerprint FROM ranked_history_documents WHERE scope=? AND item=?");
|
|
24
|
+
const insert = prepare("INSERT INTO ranked_history_documents(scope,item,fingerprint) VALUES (?,?,?)");
|
|
25
|
+
const removeTerms = prepare("DELETE FROM ranked_history_terms WHERE rowid=?");
|
|
26
|
+
const addTerms = prepare("INSERT INTO ranked_history_terms(rowid,content) VALUES (?,?)");
|
|
27
|
+
const update = prepare("UPDATE ranked_history_documents SET fingerprint=? WHERE id=?");
|
|
28
|
+
db.exec("CREATE TEMP TABLE search_scope (id INTEGER PRIMARY KEY, position INTEGER NOT NULL)");
|
|
29
|
+
const addScope = prepare("INSERT INTO search_scope VALUES (?,CAST(? AS INTEGER))");
|
|
30
|
+
port.on("message", (message) => {
|
|
31
|
+
try {
|
|
32
|
+
if ("items" in message) {
|
|
33
|
+
for (const item of message.items) {
|
|
34
|
+
if (!/^[1-9][0-9]*$/.test(item.id))
|
|
35
|
+
throw new Error("History index requires canonical Durable entry IDs");
|
|
36
|
+
const previous = existing.get(options.scope, item.id);
|
|
37
|
+
if (previous && (typeof previous["id"] !== "bigint" || typeof previous["fingerprint"] !== "string"))
|
|
38
|
+
throw new Error("Invalid history index metadata");
|
|
39
|
+
const fingerprint = createHash("sha256").update(item.content).digest("hex");
|
|
40
|
+
const id = previous?.["id"] ?? insert.run(options.scope, item.id, fingerprint).lastInsertRowid;
|
|
41
|
+
if (!previous || previous["fingerprint"] !== fingerprint) {
|
|
42
|
+
if (previous) {
|
|
43
|
+
removeTerms.run(id);
|
|
44
|
+
update.run(fingerprint, id);
|
|
45
|
+
}
|
|
46
|
+
addTerms.run(id, item.content);
|
|
47
|
+
}
|
|
48
|
+
addScope.run(id, item.id);
|
|
49
|
+
}
|
|
50
|
+
port.postMessage({ ready: true });
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (options.match === undefined) {
|
|
54
|
+
for (const previous of prepare("SELECT id FROM ranked_history_documents WHERE scope=? AND id NOT IN (SELECT id FROM search_scope)").iterate(options.scope)) {
|
|
55
|
+
if (typeof previous["id"] !== "bigint")
|
|
56
|
+
throw new Error("Invalid history index ID");
|
|
57
|
+
removeTerms.run(previous["id"]);
|
|
58
|
+
}
|
|
59
|
+
prepare("DELETE FROM ranked_history_documents WHERE scope=? AND id NOT IN (SELECT id FROM search_scope)").run(options.scope);
|
|
60
|
+
}
|
|
61
|
+
const ids = [];
|
|
62
|
+
if (options.match !== undefined) {
|
|
63
|
+
for (const row of prepare("SELECT d.item FROM ranked_history_terms JOIN search_scope s ON s.id=ranked_history_terms.rowid JOIN ranked_history_documents d ON d.id=s.id WHERE ranked_history_terms MATCH ? ORDER BY " + (options.recentFirst ? "s.position DESC" : "rank, s.position") + " LIMIT ?").all(options.match, options.limit ?? 25)) {
|
|
64
|
+
if (typeof row["item"] !== "string")
|
|
65
|
+
throw new Error("Invalid history index item ID");
|
|
66
|
+
ids.push(row["item"]);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
db.exec("COMMIT");
|
|
70
|
+
db.close();
|
|
71
|
+
database = undefined;
|
|
72
|
+
port.postMessage({ ids });
|
|
73
|
+
} catch (error) {
|
|
74
|
+
fail(error);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
port.postMessage({ ready: true });
|
|
78
|
+
} catch (error) {
|
|
79
|
+
fail(error);
|
|
80
|
+
}
|
|
81
|
+
function fail(error) {
|
|
82
|
+
database?.close();
|
|
83
|
+
database = undefined;
|
|
84
|
+
port?.postMessage({ error: String(error) });
|
|
85
|
+
}
|