soulbridge 0.0.22 → 0.0.98
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 +202 -202
- package/dist/packages/cli/src/cli.js +0 -0
- package/dist/packages/core/src/config/dimensions.d.ts +18 -0
- package/dist/packages/core/src/config/dimensions.js +33 -0
- package/dist/packages/core/src/config/domains.d.ts +70 -0
- package/dist/packages/core/src/config/domains.js +70 -0
- package/dist/packages/core/src/config/labels.d.ts +22 -0
- package/dist/packages/core/src/config/labels.js +21 -0
- package/dist/packages/core/src/config/status.d.ts +51 -0
- package/dist/packages/core/src/config/status.js +47 -0
- package/dist/packages/core/src/consolidate.d.ts +170 -0
- package/dist/packages/core/src/consolidate.js +311 -0
- package/dist/packages/core/src/context.d.ts +2 -0
- package/dist/packages/core/src/context.js +48 -17
- package/dist/packages/core/src/dimension-text.d.ts +6 -0
- package/dist/packages/core/src/dimension-text.js +24 -0
- package/dist/packages/core/src/dimensions.d.ts +28 -0
- package/dist/packages/core/src/dimensions.js +53 -0
- package/dist/packages/core/src/extract.d.ts +140 -0
- package/dist/packages/core/src/extract.js +611 -0
- package/dist/packages/core/src/index.d.ts +380 -11
- package/dist/packages/core/src/index.js +1015 -127
- package/dist/packages/core/src/inject-text.d.ts +16 -0
- package/dist/packages/core/src/inject-text.js +24 -0
- package/dist/packages/core/src/profile-prompt.d.ts +18 -0
- package/dist/packages/core/src/profile-prompt.js +103 -0
- package/dist/packages/core/src/prompts.d.ts +122 -0
- package/dist/packages/core/src/prompts.generated.d.ts +9 -0
- package/dist/packages/core/src/prompts.generated.js +9 -0
- package/dist/packages/core/src/prompts.js +293 -0
- package/dist/packages/core/src/reflect.d.ts +27 -0
- package/dist/packages/core/src/reflect.js +87 -27
- package/dist/packages/core/src/retrieval.d.ts +39 -0
- package/dist/packages/core/src/retrieval.js +105 -0
- package/dist/packages/core/src/roles.d.ts +18 -0
- package/dist/packages/core/src/roles.js +17 -0
- package/dist/packages/core/src/rules.d.ts +69 -0
- package/dist/packages/core/src/rules.js +152 -0
- package/dist/packages/core/src/scoring.d.ts +26 -1
- package/dist/packages/core/src/scoring.js +65 -5
- package/dist/packages/core/src/sqlite.d.ts +26 -5
- package/dist/packages/core/src/sqlite.js +1013 -17
- package/dist/packages/core/src/store.d.ts +98 -1
- package/dist/packages/core/src/types.d.ts +472 -1
- package/dist/packages/core/src/types.js +1 -0
- package/package.json +5 -3
|
@@ -7,6 +7,9 @@ export const WEIGHTS = {
|
|
|
7
7
|
vividness: 0.15,
|
|
8
8
|
recall: 0.05,
|
|
9
9
|
};
|
|
10
|
+
/** BM25 constants. k1 = term-frequency saturation, b = length normalization. */
|
|
11
|
+
export const BM25_K1 = 1.5;
|
|
12
|
+
export const BM25_B = 0.75;
|
|
10
13
|
/**
|
|
11
14
|
* CJK text has no spaces, so single characters are too coarse and the whole
|
|
12
15
|
* sentence is too fine: use character bigrams. Latin runs become words.
|
|
@@ -26,6 +29,10 @@ export function tokenize(input) {
|
|
|
26
29
|
}
|
|
27
30
|
return [...out];
|
|
28
31
|
}
|
|
32
|
+
/** Token list of one memory, duplicates kept (BM25 needs term frequency). */
|
|
33
|
+
function memoryTokens(memory) {
|
|
34
|
+
return [...tokenize(memory.content), ...memory.keywords.map((k) => k.toLowerCase())];
|
|
35
|
+
}
|
|
29
36
|
export function relevance(queryTokens, memory) {
|
|
30
37
|
if (queryTokens.length === 0)
|
|
31
38
|
return 0;
|
|
@@ -39,13 +46,55 @@ export function relevance(queryTokens, memory) {
|
|
|
39
46
|
hit += 1;
|
|
40
47
|
return hit / queryTokens.length;
|
|
41
48
|
}
|
|
49
|
+
/** Build idf / average length over a batch of tokenized documents. Call once per query. */
|
|
50
|
+
export function buildCorpusStats(docs) {
|
|
51
|
+
const df = new Map();
|
|
52
|
+
let totalLen = 0;
|
|
53
|
+
for (const tokens of docs) {
|
|
54
|
+
totalLen += tokens.length;
|
|
55
|
+
for (const token of new Set(tokens))
|
|
56
|
+
df.set(token, (df.get(token) ?? 0) + 1);
|
|
57
|
+
}
|
|
58
|
+
const n = Math.max(1, docs.length);
|
|
59
|
+
const idf = new Map();
|
|
60
|
+
for (const [token, count] of df) {
|
|
61
|
+
// +1 keeps idf positive so a shared rare token never becomes a penalty.
|
|
62
|
+
idf.set(token, Math.log((n + 1) / (count + 1)) + 1);
|
|
63
|
+
}
|
|
64
|
+
return { idf, avgdl: totalLen / n };
|
|
65
|
+
}
|
|
66
|
+
/** BM25 over pre-tokenized document tokens. Length normalization is why this replaced plain recall. */
|
|
67
|
+
export function bm25(tokens, docTokens, stats) {
|
|
68
|
+
if (tokens.length === 0)
|
|
69
|
+
return 0;
|
|
70
|
+
if (docTokens.length === 0)
|
|
71
|
+
return 0;
|
|
72
|
+
const tf = new Map();
|
|
73
|
+
for (const token of docTokens)
|
|
74
|
+
tf.set(token, (tf.get(token) ?? 0) + 1);
|
|
75
|
+
const dl = docTokens.length;
|
|
76
|
+
const norm = 1 - BM25_B + (BM25_B * dl) / Math.max(1, stats.avgdl);
|
|
77
|
+
let score = 0;
|
|
78
|
+
for (const token of new Set(tokens)) {
|
|
79
|
+
const f = tf.get(token) ?? 0;
|
|
80
|
+
if (f === 0)
|
|
81
|
+
continue;
|
|
82
|
+
const idf = stats.idf.get(token) ?? 1;
|
|
83
|
+
score += (idf * (f * (BM25_K1 + 1))) / (f + BM25_K1 * norm);
|
|
84
|
+
}
|
|
85
|
+
return score;
|
|
86
|
+
}
|
|
42
87
|
export function recency(reference, now) {
|
|
43
88
|
const days = Math.max(0, (now - reference) / 86_400_000);
|
|
44
89
|
return Math.pow(0.5, days / RECENCY_HALF_LIFE_DAYS);
|
|
45
90
|
}
|
|
46
91
|
const clamp01 = (n) => Math.min(1, Math.max(0, n));
|
|
47
|
-
|
|
48
|
-
|
|
92
|
+
/**
|
|
93
|
+
* @param relOverride 归一化到 0–1 的相关性。rank() 传 BM25 的归一化值;
|
|
94
|
+
* 省略时退回旧的命中比例,保持单独调用的行为不变。
|
|
95
|
+
*/
|
|
96
|
+
export function scoreMemory(memory, queryTokens, now, relOverride) {
|
|
97
|
+
const rel = relOverride ?? relevance(queryTokens, memory);
|
|
49
98
|
const imp = clamp01(memory.importance / 10);
|
|
50
99
|
const rec = recency(memory.lastRecalledAt ?? memory.createdAt, now);
|
|
51
100
|
const viv = clamp01(memory.vividness);
|
|
@@ -60,8 +109,19 @@ export function rank(memories, query, now) {
|
|
|
60
109
|
const tokens = tokenize(query);
|
|
61
110
|
// A query is a selection: with one, a memory that shares no token is noise,
|
|
62
111
|
// no matter how important it is. Without one (session start), rank by weight alone.
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
112
|
+
if (tokens.length === 0) {
|
|
113
|
+
return memories
|
|
114
|
+
.map((memory) => ({ memory, score: scoreMemory(memory, tokens, now) }))
|
|
115
|
+
.sort((a, b) => b.score - a.score);
|
|
116
|
+
}
|
|
117
|
+
const stats = buildCorpusStats(memories.map(memoryTokens));
|
|
118
|
+
const scored = memories.map((memory) => ({ memory, raw: bm25(tokens, memoryTokens(memory), stats) }));
|
|
119
|
+
const best = scored.reduce((max, s) => Math.max(max, s.raw), 0);
|
|
120
|
+
if (best <= 0)
|
|
121
|
+
return [];
|
|
122
|
+
// Normalize to 0–1 so the weighted sum below keeps its original scale.
|
|
123
|
+
return scored
|
|
124
|
+
.filter((s) => s.raw > 0)
|
|
125
|
+
.map(({ memory, raw }) => ({ memory, score: scoreMemory(memory, tokens, now, raw / best) }))
|
|
66
126
|
.sort((a, b) => b.score - a.score);
|
|
67
127
|
}
|
|
@@ -1,19 +1,40 @@
|
|
|
1
1
|
import { DatabaseSync } from 'node:sqlite';
|
|
2
2
|
import type { SoulStore } from './store.ts';
|
|
3
|
-
export declare const SCHEMA_VERSION =
|
|
3
|
+
export declare const SCHEMA_VERSION = 9;
|
|
4
4
|
export declare const AFFECT_RETENTION_MS: number;
|
|
5
5
|
export declare class DbNewerThanBinaryError extends Error {
|
|
6
6
|
readonly code = "DB_NEWER_THAN_BINARY";
|
|
7
7
|
readonly dbVersion: number;
|
|
8
8
|
constructor(dbVersion: number);
|
|
9
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* v3/v4 → v5: turns become the unit of memory (briefs/0014). Existing exchanges
|
|
12
|
+
* are copied across as turns with a single text step, so nothing is lost; the
|
|
13
|
+
* old table stays as a read-only legacy copy.
|
|
14
|
+
*/
|
|
15
|
+
/** v6 → v7: turn threads (a topic's turns need not be adjacent) plus turns.thread_id. */
|
|
16
|
+
export declare function upgradeToV9(db: DatabaseSync): void;
|
|
17
|
+
export declare function upgradeToV8(db: DatabaseSync): void;
|
|
18
|
+
export declare function upgradeToV7(db: DatabaseSync): void;
|
|
19
|
+
export declare function upgradeToV5(db: DatabaseSync): void;
|
|
10
20
|
/**
|
|
11
21
|
* Copy the database aside before a schema upgrade.
|
|
12
22
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
23
|
+
* The backup is the only way back from a bad migration, so it has to be *valid*,
|
|
24
|
+
* not merely present. Two failures were possible before: a plain file copy after
|
|
25
|
+
* `wal_checkpoint(TRUNCATE)` dropped the newest commits when another connection
|
|
26
|
+
* pinned an old snapshot (the checkpoint returns busy and only partially flushes,
|
|
27
|
+
* and that return value was ignored), and an existing backup file was trusted
|
|
28
|
+
* forever even if it was the broken one from an earlier attempt.
|
|
29
|
+
*
|
|
30
|
+
* `VACUUM INTO` reads through the WAL and writes a consistent snapshot regardless
|
|
31
|
+
* of other readers, so it is the primary path; a checkpoint+copy remains as the
|
|
32
|
+
* fallback, and whatever it produces is opened and integrity-checked. A copy that
|
|
33
|
+
* fails its own check is moved aside (kept for forensics) and the upgrade refuses
|
|
34
|
+
* to run — a corrupt backup is worse than none, because it is the one thing we
|
|
35
|
+
* would trust when everything else failed.
|
|
36
|
+
*
|
|
37
|
+
* Exported for the upgrade test.
|
|
17
38
|
*/
|
|
18
39
|
export declare function backupBeforeUpgrade(db: DatabaseSync, file: string, from: number): void;
|
|
19
40
|
export declare function openSqlite(file: string): SoulStore;
|