@modootoday/pmcp 0.9.2 → 0.11.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.
@@ -1,134 +0,0 @@
1
- import {
2
- normaliseIntent
3
- } from "./chunk-A5NOHVHN.js";
4
-
5
- // src/cache.ts
6
- async function openDatabase(path) {
7
- try {
8
- const { DatabaseSync } = await import(`node:${"sqlite"}`);
9
- return { db: new DatabaseSync(path), provider: "node:sqlite" };
10
- } catch {
11
- }
12
- try {
13
- const { Database } = await import(`bun:${"sqlite"}`);
14
- const bun = new Database(path, { create: true });
15
- return {
16
- db: {
17
- exec: (sql) => bun.run(sql),
18
- prepare: (sql) => bun.query(sql),
19
- close: () => bun.close()
20
- },
21
- provider: "bun:sqlite"
22
- };
23
- } catch {
24
- return null;
25
- }
26
- }
27
- var DEFAULT_INTENT_CACHE_LIMIT = 4096;
28
- var SCHEMA = `
29
- CREATE TABLE IF NOT EXISTS intent_vector (
30
- intent TEXT NOT NULL PRIMARY KEY,
31
- model_id TEXT NOT NULL,
32
- dims INTEGER NOT NULL,
33
- vector BLOB NOT NULL,
34
- used_at INTEGER NOT NULL
35
- );
36
- CREATE INDEX IF NOT EXISTS intent_vector_used_at ON intent_vector (used_at);
37
- `;
38
- async function openIntentCache(options) {
39
- const opened = await openDatabase(options.path);
40
- if (!opened) return null;
41
- const { db, provider } = opened;
42
- try {
43
- db.exec("PRAGMA journal_mode = WAL");
44
- db.exec(SCHEMA);
45
- } catch {
46
- db.close();
47
- return null;
48
- }
49
- const limit = options.limit ?? DEFAULT_INTENT_CACHE_LIMIT;
50
- const now = options.now ?? (() => Date.now());
51
- let hits = 0;
52
- let misses = 0;
53
- let mismatched = 0;
54
- const selectRow = db.prepare("SELECT model_id, dims, vector FROM intent_vector WHERE intent = ?");
55
- const touch = db.prepare(
56
- "UPDATE intent_vector SET used_at = ? WHERE intent = ?"
57
- );
58
- const upsert = db.prepare(
59
- `INSERT INTO intent_vector (intent, model_id, dims, vector, used_at)
60
- VALUES (?, ?, ?, ?, ?)
61
- ON CONFLICT(intent) DO UPDATE SET
62
- model_id = excluded.model_id,
63
- dims = excluded.dims,
64
- vector = excluded.vector,
65
- used_at = excluded.used_at`
66
- );
67
- const countRows = db.prepare(
68
- "SELECT count(*) AS n FROM intent_vector"
69
- );
70
- const evict = db.prepare(
71
- `DELETE FROM intent_vector WHERE intent IN (
72
- SELECT intent FROM intent_vector ORDER BY used_at ASC LIMIT ?
73
- )`
74
- );
75
- return {
76
- get(intent) {
77
- const key = normaliseIntent(intent);
78
- const row = selectRow.get(key) ?? null;
79
- if (!row) {
80
- misses += 1;
81
- return null;
82
- }
83
- if (row.model_id !== options.modelId || row.dims !== options.dims) {
84
- mismatched += 1;
85
- return null;
86
- }
87
- hits += 1;
88
- touch.run(now(), key);
89
- const bytes = row.vector;
90
- return new Float32Array(
91
- bytes.buffer.slice(
92
- bytes.byteOffset,
93
- bytes.byteOffset + bytes.byteLength
94
- )
95
- );
96
- },
97
- put(intent, vector) {
98
- if (vector.length !== options.dims) {
99
- throw new Error(
100
- `intent vector is ${vector.length} wide, cache holds ${options.dims}`
101
- );
102
- }
103
- const key = normaliseIntent(intent);
104
- upsert.run(
105
- key,
106
- options.modelId,
107
- options.dims,
108
- new Uint8Array(vector.buffer, vector.byteOffset, vector.byteLength),
109
- now()
110
- );
111
- const rows = countRows.get()?.n ?? 0;
112
- if (rows > limit) evict.run(rows - limit);
113
- },
114
- stats() {
115
- return {
116
- hits,
117
- misses,
118
- mismatched,
119
- rows: countRows.get()?.n ?? 0,
120
- provider
121
- };
122
- },
123
- close() {
124
- db.close();
125
- }
126
- };
127
- }
128
-
129
- export {
130
- openDatabase,
131
- DEFAULT_INTENT_CACHE_LIMIT,
132
- openIntentCache
133
- };
134
- //# sourceMappingURL=chunk-CMZ7OTQD.js.map