@dsh-mobile/mobile-remote 3.3.1 → 3.4.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 +9 -5
- package/docs/PRIVACY-POLICY.en.md +10 -3
- package/docs/PRIVACY-POLICY.md +10 -3
- package/docs/USER-GUIDE.zh-CN.md +1 -0
- package/lib/account-usage-query.js +60 -60
- package/lib/account-usage.js +10 -10
- package/lib/api-helpers.js +109 -109
- package/lib/assistant-prefs.js +82 -59
- package/lib/auto-config.js +65 -65
- package/lib/backup.js +117 -117
- package/lib/break-reminder.js +45 -45
- package/lib/client.js +93 -93
- package/lib/config.js +232 -218
- package/lib/git-read-bundled-provider.js +12 -12
- package/lib/git-read-service.js +5 -5
- package/lib/host-compat.js +84 -84
- package/lib/http-util.js +17 -17
- package/lib/index.js +1534 -1534
- package/lib/memory/context.js +44 -44
- package/lib/memory/decay.js +93 -93
- package/lib/memory/extract.js +77 -77
- package/lib/memory/routes.js +33 -33
- package/lib/memory/store.js +196 -196
- package/lib/notify-copy.js +93 -93
- package/lib/permission-guide.js +86 -86
- package/lib/persona-cards.js +73 -65
- package/lib/phone-action-args.js +175 -0
- package/lib/phone-actions.js +227 -0
- package/lib/phone-announce.js +58 -0
- package/lib/proactive-material.js +42 -42
- package/lib/proactive.js +121 -121
- package/lib/protected-paths.js +79 -79
- package/lib/protocol.js +61 -26
- package/lib/push.js +29 -29
- package/lib/rate-limit.js +16 -16
- package/lib/relay/connector.js +246 -102
- package/lib/relay/crypto.js +15 -15
- package/lib/relay/devices.js +36 -36
- package/lib/relay/fingerprint.js +34 -34
- package/lib/relay/identity.js +26 -26
- package/lib/relay/mux.js +60 -46
- package/lib/relay/noise.js +40 -40
- package/lib/relay/on-demand.js +33 -33
- package/lib/relay/p2p.js +239 -0
- package/lib/relay/pairing.js +24 -24
- package/lib/relay/phone.js +62 -40
- package/lib/relay/serve.js +21 -21
- package/lib/relay/signal.js +69 -0
- package/lib/relay/ws-server.js +32 -32
- package/lib/reminders.js +15 -15
- package/lib/rpc-adapter.js +43 -43
- package/lib/session-list.js +31 -31
- package/lib/session-rotation.js +95 -95
- package/lib/session-title-cache.js +35 -35
- package/lib/session-title-refresh.js +51 -51
- package/lib/timeline.js +140 -140
- package/lib/turn-outline.js +104 -104
- package/package.json +6 -13
package/lib/memory/store.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
23
|
import { appendFileSync, chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
24
24
|
import { createRequire } from "node:module";
|
|
25
25
|
import { dirname, join } from "node:path";
|
|
@@ -34,10 +34,10 @@ import {
|
|
|
34
34
|
retentionOfRow,
|
|
35
35
|
} from "./decay.js";
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
41
|
function loadDatabaseSync() {
|
|
42
42
|
let mod;
|
|
43
43
|
try {
|
|
@@ -56,11 +56,11 @@ function loadDatabaseSync() {
|
|
|
56
56
|
return mod.DatabaseSync;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
/** 记忆所属的上下文。`shared` = 两种模式都能用。 */
|
|
60
|
-
export const SCOPES = /** @type {const} */ (["work", "life", "shared"]);
|
|
61
59
|
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
export const SCOPES = (["work", "life", "shared"]);
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
export const SOURCES = (["conversation", "manual", "import"]);
|
|
64
64
|
|
|
65
65
|
const SCHEMA = `
|
|
66
66
|
CREATE TABLE IF NOT EXISTS memory (
|
|
@@ -87,73 +87,73 @@ CREATE INDEX IF NOT EXISTS idx_memory_scope ON memory(scope, deleted_at);
|
|
|
87
87
|
CREATE INDEX IF NOT EXISTS idx_memory_created ON memory(created_at DESC);
|
|
88
88
|
`;
|
|
89
89
|
|
|
90
|
-
|
|
90
|
+
|
|
91
91
|
const MIGRATIONS = [
|
|
92
92
|
{ column: "stability", ddl: "ALTER TABLE memory ADD COLUMN stability REAL NOT NULL DEFAULT 7" },
|
|
93
93
|
{ column: "difficulty", ddl: "ALTER TABLE memory ADD COLUMN difficulty REAL NOT NULL DEFAULT 5" },
|
|
94
94
|
{ column: "last_recalled_at", ddl: "ALTER TABLE memory ADD COLUMN last_recalled_at INTEGER" },
|
|
95
95
|
];
|
|
96
96
|
|
|
97
|
-
// ── 脱敏 ──────────────────────────────────────────────────────────────────
|
|
98
97
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
105
|
export function scrubMemoryText(text) {
|
|
106
106
|
if (typeof text !== "string") return "";
|
|
107
107
|
let out = text;
|
|
108
|
-
|
|
108
|
+
|
|
109
109
|
out = out.replace(
|
|
110
110
|
/\b(token|secret|password|passwd|api[-_]?key|authorization|ticket)\b\s*[:=]\s*["']?([^\s,;"']{4,})/gi,
|
|
111
111
|
"$1=***",
|
|
112
112
|
);
|
|
113
|
-
|
|
113
|
+
|
|
114
114
|
out = out.replace(/\bBearer\s+[A-Za-z0-9._-]{8,}/gi, "Bearer ***");
|
|
115
115
|
out = out.replace(/\bsk-[A-Za-z0-9_-]{12,}/g, "sk-***");
|
|
116
|
-
|
|
116
|
+
|
|
117
117
|
out = out.replace(
|
|
118
118
|
/\b[\w.+-]+@([\w-]+\.[\w.-]+)\b/g,
|
|
119
119
|
(_m, domain) => `***@${domain}`,
|
|
120
120
|
);
|
|
121
|
-
|
|
121
|
+
|
|
122
122
|
out = out.replace(/(?<!\d)1[3-9]\d{9}(?!\d)/g, "[手机号]");
|
|
123
|
-
|
|
123
|
+
|
|
124
124
|
out = out.replace(/(?<!\d)\d{17}[\dXx](?!\d)/g, "[身份证号]");
|
|
125
|
-
|
|
125
|
+
|
|
126
126
|
out = out.replace(/(?<!\d)\d{16,19}(?!\d)/g, "[长数字]");
|
|
127
127
|
return out.trim();
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
// ── 分数与长度 ────────────────────────────────────────────────────────────
|
|
131
130
|
|
|
132
|
-
|
|
131
|
+
|
|
132
|
+
|
|
133
133
|
export function normalizeScope(scope) {
|
|
134
134
|
return SCOPES.includes(scope) ? scope : null;
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
|
|
137
|
+
|
|
138
138
|
export function scopesVisibleTo(mode) {
|
|
139
139
|
return mode === "life" ? ["life", "shared"] : ["work", "shared"];
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
151
|
export function bm25Scores(docs, terms) {
|
|
152
152
|
const N = docs.length || 1;
|
|
153
153
|
const tokenized = docs.map((d) => tokenize(d));
|
|
154
154
|
const avgLen =
|
|
155
155
|
tokenized.reduce((sum, t) => sum + t.length, 0) / (tokenized.length || 1);
|
|
156
|
-
|
|
156
|
+
|
|
157
157
|
const df = new Map();
|
|
158
158
|
for (const t of tokenized) {
|
|
159
159
|
for (const w of new Set(t)) df.set(w, (df.get(w) ?? 0) + 1);
|
|
@@ -176,18 +176,18 @@ export function bm25Scores(docs, terms) {
|
|
|
176
176
|
});
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
185
|
export function tokenize(text) {
|
|
186
186
|
const out = [];
|
|
187
187
|
const lower = String(text ?? "").toLowerCase();
|
|
188
|
-
|
|
188
|
+
|
|
189
189
|
for (const m of lower.matchAll(/[a-z0-9_]{2,}/g)) out.push(m[0]);
|
|
190
|
-
|
|
190
|
+
|
|
191
191
|
for (const m of lower.matchAll(/[\u4e00-\u9fff]+/g)) {
|
|
192
192
|
const s = m[0];
|
|
193
193
|
if (s.length === 1) out.push(s);
|
|
@@ -200,13 +200,13 @@ export function queryTerms(query) {
|
|
|
200
200
|
return [...new Set(tokenize(query))];
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
-
// ── 删除(T-63 / E12)──────────────────────────────────────────────────────
|
|
204
|
-
//
|
|
205
|
-
// 删除就是删除:行从库里真删(`secure_delete` 把释放的页清零),WAL 里旧页的副本
|
|
206
|
-
// 随 `wal_checkpoint(TRUNCATE)` 一起截掉,全量存档里这条的每一行都被重写剔除。
|
|
207
|
-
// 之前的软删除把原文留在库、存档、导出、备份四个地方,用户点了删除却删不掉。
|
|
208
203
|
|
|
209
|
-
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
210
|
export function memoryFilesIn(dir) {
|
|
211
211
|
return [
|
|
212
212
|
join(dir, "memory.db"),
|
|
@@ -217,28 +217,28 @@ export function memoryFilesIn(dir) {
|
|
|
217
217
|
];
|
|
218
218
|
}
|
|
219
219
|
|
|
220
|
-
|
|
220
|
+
|
|
221
221
|
function restrictModes(dir) {
|
|
222
222
|
if (process.platform === "win32") return;
|
|
223
223
|
try {
|
|
224
224
|
chmodSync(dir, 0o700);
|
|
225
225
|
} catch {
|
|
226
|
-
|
|
226
|
+
|
|
227
227
|
}
|
|
228
228
|
for (const file of memoryFilesIn(dir)) {
|
|
229
229
|
try {
|
|
230
230
|
chmodSync(file, 0o600);
|
|
231
231
|
} catch {
|
|
232
|
-
|
|
232
|
+
|
|
233
233
|
}
|
|
234
234
|
}
|
|
235
235
|
}
|
|
236
236
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
242
|
export function wipeMemoryFiles(dir) {
|
|
243
243
|
let removed = 0;
|
|
244
244
|
for (const file of memoryFilesIn(dir)) {
|
|
@@ -249,10 +249,10 @@ export function wipeMemoryFiles(dir) {
|
|
|
249
249
|
return removed;
|
|
250
250
|
}
|
|
251
251
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
256
|
function purgeArchive(archiveFile, ids) {
|
|
257
257
|
if (!existsSync(archiveFile)) return;
|
|
258
258
|
const drop = new Set(ids.map(Number));
|
|
@@ -273,53 +273,53 @@ function purgeArchive(archiveFile, ids) {
|
|
|
273
273
|
renameSync(tmp, archiveFile);
|
|
274
274
|
}
|
|
275
275
|
|
|
276
|
-
// ── 记忆库 ────────────────────────────────────────────────────────────────
|
|
277
276
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
285
|
export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000, walRetries = 60 } = {}) {
|
|
286
286
|
if (!dir) throw new Error("openMemoryStore: dir is required");
|
|
287
|
-
const DatabaseSync = loadDatabaseSync();
|
|
287
|
+
const DatabaseSync = loadDatabaseSync();
|
|
288
288
|
const file = join(dir, "memory.db");
|
|
289
289
|
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
|
|
290
|
-
|
|
291
|
-
|
|
290
|
+
|
|
291
|
+
|
|
292
292
|
let recallError = null;
|
|
293
293
|
const db = new DatabaseSync(file);
|
|
294
294
|
db.exec("PRAGMA journal_mode = WAL;");
|
|
295
|
-
|
|
295
|
+
|
|
296
296
|
db.exec("PRAGMA secure_delete = ON;");
|
|
297
297
|
db.exec(SCHEMA);
|
|
298
|
-
restrictModes(dir);
|
|
299
|
-
|
|
298
|
+
restrictModes(dir);
|
|
299
|
+
|
|
300
300
|
const existing = new Set(
|
|
301
301
|
db.prepare(`PRAGMA table_info(memory)`).all().map((r) => r.name),
|
|
302
302
|
);
|
|
303
303
|
for (const m of MIGRATIONS) {
|
|
304
304
|
if (!existing.has(m.column)) db.exec(m.ddl);
|
|
305
305
|
}
|
|
306
|
-
|
|
306
|
+
|
|
307
307
|
db.exec(`UPDATE memory SET last_recalled_at = created_at WHERE last_recalled_at IS NULL`);
|
|
308
308
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
316
|
const archiveFile = join(dir, "memory-archive.jsonl");
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
323
|
let walTimer = null;
|
|
324
324
|
let walAttempts = 0;
|
|
325
325
|
let closed = false;
|
|
@@ -345,7 +345,7 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
345
345
|
walTimer = setTimeout(retry, walRetryMs);
|
|
346
346
|
walTimer.unref?.();
|
|
347
347
|
};
|
|
348
|
-
|
|
348
|
+
|
|
349
349
|
const hardDelete = (ids) => {
|
|
350
350
|
if (ids.length === 0) return 0;
|
|
351
351
|
const del = db.prepare(`DELETE FROM memory WHERE id = ?`);
|
|
@@ -363,20 +363,20 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
363
363
|
{ mode: 0o600 },
|
|
364
364
|
);
|
|
365
365
|
} catch {
|
|
366
|
-
|
|
366
|
+
|
|
367
367
|
}
|
|
368
368
|
};
|
|
369
369
|
|
|
370
|
-
|
|
370
|
+
|
|
371
371
|
const legacyDeleted = db
|
|
372
372
|
.prepare(`SELECT id FROM memory WHERE deleted_at IS NOT NULL`)
|
|
373
373
|
.all()
|
|
374
374
|
.map((r) => r.id);
|
|
375
375
|
if (legacyDeleted.length > 0) hardDelete(legacyDeleted);
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
380
|
const SECURE_SINCE = 1;
|
|
381
381
|
const version = Number(db.prepare(`PRAGMA user_version`).get()?.user_version ?? 0);
|
|
382
382
|
if (version < SECURE_SINCE || legacyDeleted.length > 0) {
|
|
@@ -385,7 +385,7 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
385
385
|
truncateWal();
|
|
386
386
|
db.exec(`PRAGMA user_version = ${SECURE_SINCE};`);
|
|
387
387
|
} catch {
|
|
388
|
-
|
|
388
|
+
|
|
389
389
|
}
|
|
390
390
|
}
|
|
391
391
|
|
|
@@ -399,12 +399,12 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
399
399
|
).join(",")}) ORDER BY pinned DESC, created_at DESC`,
|
|
400
400
|
);
|
|
401
401
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
408
|
function add({ scope, text, source = "conversation", sessionId = null, origin = null, pinned = false }) {
|
|
409
409
|
const s = normalizeScope(scope);
|
|
410
410
|
if (!s) throw new Error(`invalid scope: ${scope}`);
|
|
@@ -423,11 +423,11 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
423
423
|
pinned ? 1 : 0,
|
|
424
424
|
DEFAULT_STABILITY_DAYS,
|
|
425
425
|
DEFAULT_DIFFICULTY,
|
|
426
|
-
|
|
426
|
+
|
|
427
427
|
t,
|
|
428
428
|
);
|
|
429
429
|
const created = get(Number(info.lastInsertRowid));
|
|
430
|
-
|
|
430
|
+
|
|
431
431
|
appendArchive("add", { id: created?.id, scope: s, text: clean, source });
|
|
432
432
|
return created;
|
|
433
433
|
}
|
|
@@ -437,20 +437,20 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
437
437
|
return row ? toMemory(row) : null;
|
|
438
438
|
}
|
|
439
439
|
|
|
440
|
-
|
|
440
|
+
|
|
441
441
|
function listVisible(mode) {
|
|
442
442
|
const visible = scopesVisibleTo(mode);
|
|
443
443
|
return selectAll.all(...visible).map(toMemory);
|
|
444
444
|
}
|
|
445
445
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
454
454
|
function search(query, { mode = "work", limit = 10, thaw = true } = {}) {
|
|
455
455
|
const terms = queryTerms(query);
|
|
456
456
|
if (terms.length === 0) return [];
|
|
@@ -468,21 +468,21 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
468
468
|
.slice(0, limit);
|
|
469
469
|
if (thaw && hits.length > 0) {
|
|
470
470
|
try {
|
|
471
|
-
|
|
471
|
+
|
|
472
472
|
const at = new Date(now());
|
|
473
473
|
const cold = hits.filter((r) => retentionOfRow(r, at) < FORGET_THRESHOLD);
|
|
474
474
|
if (cold.length > 0) markRecalled(cold.map((r) => r.id));
|
|
475
475
|
} catch {
|
|
476
|
-
|
|
476
|
+
|
|
477
477
|
}
|
|
478
478
|
}
|
|
479
479
|
return hits;
|
|
480
480
|
}
|
|
481
481
|
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
486
|
function edit(id, text) {
|
|
487
487
|
const clean = scrubMemoryText(text ?? "");
|
|
488
488
|
if (clean === "") throw new Error("memory text is empty after scrubbing");
|
|
@@ -509,46 +509,46 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
509
509
|
return get(id);
|
|
510
510
|
}
|
|
511
511
|
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
517
|
function remove(id) {
|
|
518
518
|
return hardDelete([Number(id)]) > 0;
|
|
519
519
|
}
|
|
520
520
|
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
|
|
526
526
|
function removeAll() {
|
|
527
527
|
const n = Number(db.prepare(`DELETE FROM memory`).run().changes ?? 0);
|
|
528
528
|
try {
|
|
529
529
|
writeFileSync(archiveFile, "", { mode: 0o600 });
|
|
530
530
|
} catch {
|
|
531
|
-
|
|
531
|
+
|
|
532
532
|
}
|
|
533
|
-
|
|
533
|
+
|
|
534
534
|
rmSync(`${archiveFile}.tmp`, { force: true });
|
|
535
535
|
truncateWal();
|
|
536
536
|
return n;
|
|
537
537
|
}
|
|
538
538
|
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
552
|
function markRecalled(ids) {
|
|
553
553
|
if (!Array.isArray(ids) || ids.length === 0) return 0;
|
|
554
554
|
try {
|
|
@@ -571,20 +571,20 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
571
571
|
);
|
|
572
572
|
recalled += 1;
|
|
573
573
|
}
|
|
574
|
-
|
|
574
|
+
|
|
575
575
|
if (recalled > 0) recallError = null;
|
|
576
576
|
return recalled;
|
|
577
577
|
} catch (err) {
|
|
578
|
-
|
|
578
|
+
|
|
579
579
|
recallError = err?.message ?? String(err);
|
|
580
580
|
return 0;
|
|
581
581
|
}
|
|
582
582
|
}
|
|
583
583
|
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
588
|
function exportAll() {
|
|
589
589
|
return db
|
|
590
590
|
.prepare(`SELECT * FROM memory WHERE deleted_at IS NULL ORDER BY id ASC`)
|
|
@@ -596,20 +596,20 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
596
596
|
return listVisible(mode).length;
|
|
597
597
|
}
|
|
598
598
|
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
609
609
|
function contextFor(mode, { query = "", limit = 20, maxChars = 1500 } = {}) {
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
613
613
|
const at = new Date(now());
|
|
614
614
|
const { injectable } = partitionInjectable(listVisible(mode), {
|
|
615
615
|
now: at,
|
|
@@ -643,7 +643,7 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
643
643
|
let used = 0;
|
|
644
644
|
for (const r of rows) {
|
|
645
645
|
const line = `- ${r.text}`;
|
|
646
|
-
|
|
646
|
+
|
|
647
647
|
const clipped =
|
|
648
648
|
line.length > 300 ? `${line.slice(0, 299)}…` : line;
|
|
649
649
|
if (used + clipped.length + 1 > maxChars) break;
|
|
@@ -651,10 +651,10 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
651
651
|
used += clipped.length + 1;
|
|
652
652
|
}
|
|
653
653
|
if (lines.length === 0) return "";
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
|
|
658
658
|
markRecalled(rows.slice(0, lines.length).map((r) => r.id));
|
|
659
659
|
return lines.join("\n");
|
|
660
660
|
}
|
|
@@ -667,7 +667,7 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
667
667
|
try {
|
|
668
668
|
db.close();
|
|
669
669
|
} catch {
|
|
670
|
-
|
|
670
|
+
|
|
671
671
|
}
|
|
672
672
|
}
|
|
673
673
|
|
|
@@ -685,13 +685,13 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
685
685
|
exportAll,
|
|
686
686
|
countVisible,
|
|
687
687
|
contextFor,
|
|
688
|
-
|
|
688
|
+
|
|
689
689
|
recallErrorOf: () => recallError,
|
|
690
690
|
close,
|
|
691
691
|
};
|
|
692
692
|
}
|
|
693
693
|
|
|
694
|
-
|
|
694
|
+
|
|
695
695
|
function toMemory(row) {
|
|
696
696
|
return {
|
|
697
697
|
id: row.id,
|
|
@@ -704,8 +704,8 @@ function toMemory(row) {
|
|
|
704
704
|
updatedAt: row.updated_at,
|
|
705
705
|
recallCount: row.recall_count,
|
|
706
706
|
pinned: row.pinned === 1,
|
|
707
|
-
|
|
708
|
-
|
|
707
|
+
|
|
708
|
+
|
|
709
709
|
stability: row.stability ?? DEFAULT_STABILITY_DAYS,
|
|
710
710
|
difficulty: row.difficulty ?? DEFAULT_DIFFICULTY,
|
|
711
711
|
lastRecalledAt: row.last_recalled_at ?? row.created_at ?? null,
|