@dsh-mobile/mobile-remote 3.3.0 → 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/FAQ.md +2 -2
- package/LICENSE +0 -0
- package/README.md +13 -8
- package/docs/PRIVACY-POLICY.en.md +26 -18
- package/docs/PRIVACY-POLICY.md +26 -18
- package/docs/SUPPORT.md +1 -1
- package/docs/TERMS-OF-SERVICE.en.md +5 -3
- package/docs/TERMS-OF-SERVICE.md +5 -3
- package/docs/USER-GUIDE.zh-CN.md +7 -6
- package/lib/account-usage-query.js +60 -60
- package/lib/account-usage.js +10 -10
- package/lib/api-helpers.js +114 -110
- package/lib/assistant-prefs.js +119 -76
- package/lib/auto-config.js +65 -65
- package/lib/backup.js +159 -123
- package/lib/break-reminder.js +45 -45
- package/lib/client.js +336 -138
- package/lib/config.js +234 -218
- package/lib/git-read-bundled-provider.js +27 -7
- 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 +1670 -1511
- package/lib/memory/context.js +45 -40
- 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 +239 -192
- package/lib/notify-copy.js +93 -93
- package/lib/permission-guide.js +86 -86
- package/lib/persona-cards.js +73 -61
- 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 +122 -119
- package/lib/protected-paths.js +205 -45
- package/lib/protocol.js +62 -23
- package/lib/push.js +29 -29
- package/lib/rate-limit.js +16 -16
- package/lib/relay/connector.js +334 -81
- package/lib/relay/crypto.js +15 -15
- package/lib/relay/devices.js +64 -32
- 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 +36 -31
- package/lib/relay/p2p.js +239 -0
- package/lib/relay/pairing.js +54 -15
- package/lib/relay/phone.js +63 -39
- package/lib/relay/serve.js +21 -21
- package/lib/relay/signal.js +69 -0
- package/lib/relay/ws-server.js +245 -0
- 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 +11 -18
package/lib/memory/store.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
import { appendFileSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
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";
|
|
26
26
|
|
|
@@ -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,27 +200,45 @@ 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"),
|
|
213
213
|
join(dir, "memory.db-wal"),
|
|
214
214
|
join(dir, "memory.db-shm"),
|
|
215
215
|
join(dir, "memory-archive.jsonl"),
|
|
216
|
+
join(dir, "memory-archive.jsonl.tmp"),
|
|
216
217
|
];
|
|
217
218
|
}
|
|
218
219
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
220
|
+
|
|
221
|
+
function restrictModes(dir) {
|
|
222
|
+
if (process.platform === "win32") return;
|
|
223
|
+
try {
|
|
224
|
+
chmodSync(dir, 0o700);
|
|
225
|
+
} catch {
|
|
226
|
+
|
|
227
|
+
}
|
|
228
|
+
for (const file of memoryFilesIn(dir)) {
|
|
229
|
+
try {
|
|
230
|
+
chmodSync(file, 0o600);
|
|
231
|
+
} catch {
|
|
232
|
+
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
224
242
|
export function wipeMemoryFiles(dir) {
|
|
225
243
|
let removed = 0;
|
|
226
244
|
for (const file of memoryFilesIn(dir)) {
|
|
@@ -231,10 +249,10 @@ export function wipeMemoryFiles(dir) {
|
|
|
231
249
|
return removed;
|
|
232
250
|
}
|
|
233
251
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
238
256
|
function purgeArchive(archiveFile, ids) {
|
|
239
257
|
if (!existsSync(archiveFile)) return;
|
|
240
258
|
const drop = new Set(ids.map(Number));
|
|
@@ -255,55 +273,79 @@ function purgeArchive(archiveFile, ids) {
|
|
|
255
273
|
renameSync(tmp, archiveFile);
|
|
256
274
|
}
|
|
257
275
|
|
|
258
|
-
// ── 记忆库 ────────────────────────────────────────────────────────────────
|
|
259
276
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000, walRetries = 60 } = {}) {
|
|
268
286
|
if (!dir) throw new Error("openMemoryStore: dir is required");
|
|
269
|
-
const DatabaseSync = loadDatabaseSync();
|
|
287
|
+
const DatabaseSync = loadDatabaseSync();
|
|
270
288
|
const file = join(dir, "memory.db");
|
|
271
289
|
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
|
|
272
|
-
|
|
273
|
-
|
|
290
|
+
|
|
291
|
+
|
|
274
292
|
let recallError = null;
|
|
275
293
|
const db = new DatabaseSync(file);
|
|
276
294
|
db.exec("PRAGMA journal_mode = WAL;");
|
|
277
|
-
|
|
295
|
+
|
|
278
296
|
db.exec("PRAGMA secure_delete = ON;");
|
|
279
297
|
db.exec(SCHEMA);
|
|
280
|
-
|
|
298
|
+
restrictModes(dir);
|
|
299
|
+
|
|
281
300
|
const existing = new Set(
|
|
282
301
|
db.prepare(`PRAGMA table_info(memory)`).all().map((r) => r.name),
|
|
283
302
|
);
|
|
284
303
|
for (const m of MIGRATIONS) {
|
|
285
304
|
if (!existing.has(m.column)) db.exec(m.ddl);
|
|
286
305
|
}
|
|
287
|
-
|
|
306
|
+
|
|
288
307
|
db.exec(`UPDATE memory SET last_recalled_at = created_at WHERE last_recalled_at IS NULL`);
|
|
289
308
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
297
316
|
const archiveFile = join(dir, "memory-archive.jsonl");
|
|
298
|
-
|
|
299
|
-
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
let walTimer = null;
|
|
324
|
+
let walAttempts = 0;
|
|
325
|
+
let closed = false;
|
|
326
|
+
const checkpointOnce = () => {
|
|
300
327
|
try {
|
|
301
|
-
db.
|
|
328
|
+
const row = db.prepare("PRAGMA wal_checkpoint(TRUNCATE)").get();
|
|
329
|
+
return Number(row?.busy ?? 0) === 0;
|
|
302
330
|
} catch {
|
|
303
|
-
|
|
331
|
+
return false;
|
|
304
332
|
}
|
|
305
333
|
};
|
|
306
|
-
|
|
334
|
+
const truncateWal = () => {
|
|
335
|
+
if (closed) return;
|
|
336
|
+
if (checkpointOnce()) return;
|
|
337
|
+
walAttempts = 0;
|
|
338
|
+
if (walTimer) return;
|
|
339
|
+
const retry = () => {
|
|
340
|
+
walTimer = null;
|
|
341
|
+
if (closed || checkpointOnce() || ++walAttempts >= walRetries) return;
|
|
342
|
+
walTimer = setTimeout(retry, walRetryMs);
|
|
343
|
+
walTimer.unref?.();
|
|
344
|
+
};
|
|
345
|
+
walTimer = setTimeout(retry, walRetryMs);
|
|
346
|
+
walTimer.unref?.();
|
|
347
|
+
};
|
|
348
|
+
|
|
307
349
|
const hardDelete = (ids) => {
|
|
308
350
|
if (ids.length === 0) return 0;
|
|
309
351
|
const del = db.prepare(`DELETE FROM memory WHERE id = ?`);
|
|
@@ -321,20 +363,20 @@ export function openMemoryStore({ dir, now = () => Date.now() } = {}) {
|
|
|
321
363
|
{ mode: 0o600 },
|
|
322
364
|
);
|
|
323
365
|
} catch {
|
|
324
|
-
|
|
366
|
+
|
|
325
367
|
}
|
|
326
368
|
};
|
|
327
369
|
|
|
328
|
-
|
|
370
|
+
|
|
329
371
|
const legacyDeleted = db
|
|
330
372
|
.prepare(`SELECT id FROM memory WHERE deleted_at IS NOT NULL`)
|
|
331
373
|
.all()
|
|
332
374
|
.map((r) => r.id);
|
|
333
375
|
if (legacyDeleted.length > 0) hardDelete(legacyDeleted);
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
338
380
|
const SECURE_SINCE = 1;
|
|
339
381
|
const version = Number(db.prepare(`PRAGMA user_version`).get()?.user_version ?? 0);
|
|
340
382
|
if (version < SECURE_SINCE || legacyDeleted.length > 0) {
|
|
@@ -343,7 +385,7 @@ export function openMemoryStore({ dir, now = () => Date.now() } = {}) {
|
|
|
343
385
|
truncateWal();
|
|
344
386
|
db.exec(`PRAGMA user_version = ${SECURE_SINCE};`);
|
|
345
387
|
} catch {
|
|
346
|
-
|
|
388
|
+
|
|
347
389
|
}
|
|
348
390
|
}
|
|
349
391
|
|
|
@@ -357,12 +399,12 @@ export function openMemoryStore({ dir, now = () => Date.now() } = {}) {
|
|
|
357
399
|
).join(",")}) ORDER BY pinned DESC, created_at DESC`,
|
|
358
400
|
);
|
|
359
401
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
366
408
|
function add({ scope, text, source = "conversation", sessionId = null, origin = null, pinned = false }) {
|
|
367
409
|
const s = normalizeScope(scope);
|
|
368
410
|
if (!s) throw new Error(`invalid scope: ${scope}`);
|
|
@@ -381,11 +423,11 @@ export function openMemoryStore({ dir, now = () => Date.now() } = {}) {
|
|
|
381
423
|
pinned ? 1 : 0,
|
|
382
424
|
DEFAULT_STABILITY_DAYS,
|
|
383
425
|
DEFAULT_DIFFICULTY,
|
|
384
|
-
|
|
426
|
+
|
|
385
427
|
t,
|
|
386
428
|
);
|
|
387
429
|
const created = get(Number(info.lastInsertRowid));
|
|
388
|
-
|
|
430
|
+
|
|
389
431
|
appendArchive("add", { id: created?.id, scope: s, text: clean, source });
|
|
390
432
|
return created;
|
|
391
433
|
}
|
|
@@ -395,20 +437,20 @@ export function openMemoryStore({ dir, now = () => Date.now() } = {}) {
|
|
|
395
437
|
return row ? toMemory(row) : null;
|
|
396
438
|
}
|
|
397
439
|
|
|
398
|
-
|
|
440
|
+
|
|
399
441
|
function listVisible(mode) {
|
|
400
442
|
const visible = scopesVisibleTo(mode);
|
|
401
443
|
return selectAll.all(...visible).map(toMemory);
|
|
402
444
|
}
|
|
403
445
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
|
|
412
454
|
function search(query, { mode = "work", limit = 10, thaw = true } = {}) {
|
|
413
455
|
const terms = queryTerms(query);
|
|
414
456
|
if (terms.length === 0) return [];
|
|
@@ -426,21 +468,21 @@ export function openMemoryStore({ dir, now = () => Date.now() } = {}) {
|
|
|
426
468
|
.slice(0, limit);
|
|
427
469
|
if (thaw && hits.length > 0) {
|
|
428
470
|
try {
|
|
429
|
-
|
|
471
|
+
|
|
430
472
|
const at = new Date(now());
|
|
431
473
|
const cold = hits.filter((r) => retentionOfRow(r, at) < FORGET_THRESHOLD);
|
|
432
474
|
if (cold.length > 0) markRecalled(cold.map((r) => r.id));
|
|
433
475
|
} catch {
|
|
434
|
-
|
|
476
|
+
|
|
435
477
|
}
|
|
436
478
|
}
|
|
437
479
|
return hits;
|
|
438
480
|
}
|
|
439
481
|
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
444
486
|
function edit(id, text) {
|
|
445
487
|
const clean = scrubMemoryText(text ?? "");
|
|
446
488
|
if (clean === "") throw new Error("memory text is empty after scrubbing");
|
|
@@ -467,44 +509,46 @@ export function openMemoryStore({ dir, now = () => Date.now() } = {}) {
|
|
|
467
509
|
return get(id);
|
|
468
510
|
}
|
|
469
511
|
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
475
517
|
function remove(id) {
|
|
476
518
|
return hardDelete([Number(id)]) > 0;
|
|
477
519
|
}
|
|
478
520
|
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
|
|
484
526
|
function removeAll() {
|
|
485
527
|
const n = Number(db.prepare(`DELETE FROM memory`).run().changes ?? 0);
|
|
486
528
|
try {
|
|
487
529
|
writeFileSync(archiveFile, "", { mode: 0o600 });
|
|
488
530
|
} catch {
|
|
489
|
-
|
|
531
|
+
|
|
490
532
|
}
|
|
533
|
+
|
|
534
|
+
rmSync(`${archiveFile}.tmp`, { force: true });
|
|
491
535
|
truncateWal();
|
|
492
536
|
return n;
|
|
493
537
|
}
|
|
494
538
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
508
552
|
function markRecalled(ids) {
|
|
509
553
|
if (!Array.isArray(ids) || ids.length === 0) return 0;
|
|
510
554
|
try {
|
|
@@ -527,20 +571,20 @@ export function openMemoryStore({ dir, now = () => Date.now() } = {}) {
|
|
|
527
571
|
);
|
|
528
572
|
recalled += 1;
|
|
529
573
|
}
|
|
530
|
-
|
|
574
|
+
|
|
531
575
|
if (recalled > 0) recallError = null;
|
|
532
576
|
return recalled;
|
|
533
577
|
} catch (err) {
|
|
534
|
-
|
|
578
|
+
|
|
535
579
|
recallError = err?.message ?? String(err);
|
|
536
580
|
return 0;
|
|
537
581
|
}
|
|
538
582
|
}
|
|
539
583
|
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
544
588
|
function exportAll() {
|
|
545
589
|
return db
|
|
546
590
|
.prepare(`SELECT * FROM memory WHERE deleted_at IS NULL ORDER BY id ASC`)
|
|
@@ -552,20 +596,20 @@ export function openMemoryStore({ dir, now = () => Date.now() } = {}) {
|
|
|
552
596
|
return listVisible(mode).length;
|
|
553
597
|
}
|
|
554
598
|
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
|
|
565
609
|
function contextFor(mode, { query = "", limit = 20, maxChars = 1500 } = {}) {
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
|
|
569
613
|
const at = new Date(now());
|
|
570
614
|
const { injectable } = partitionInjectable(listVisible(mode), {
|
|
571
615
|
now: at,
|
|
@@ -599,7 +643,7 @@ export function openMemoryStore({ dir, now = () => Date.now() } = {}) {
|
|
|
599
643
|
let used = 0;
|
|
600
644
|
for (const r of rows) {
|
|
601
645
|
const line = `- ${r.text}`;
|
|
602
|
-
|
|
646
|
+
|
|
603
647
|
const clipped =
|
|
604
648
|
line.length > 300 ? `${line.slice(0, 299)}…` : line;
|
|
605
649
|
if (used + clipped.length + 1 > maxChars) break;
|
|
@@ -607,20 +651,23 @@ export function openMemoryStore({ dir, now = () => Date.now() } = {}) {
|
|
|
607
651
|
used += clipped.length + 1;
|
|
608
652
|
}
|
|
609
653
|
if (lines.length === 0) return "";
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
|
|
614
658
|
markRecalled(rows.slice(0, lines.length).map((r) => r.id));
|
|
615
659
|
return lines.join("\n");
|
|
616
660
|
}
|
|
617
661
|
|
|
618
662
|
function close() {
|
|
619
|
-
|
|
663
|
+
checkpointOnce();
|
|
664
|
+
closed = true;
|
|
665
|
+
if (walTimer) clearTimeout(walTimer);
|
|
666
|
+
walTimer = null;
|
|
620
667
|
try {
|
|
621
668
|
db.close();
|
|
622
669
|
} catch {
|
|
623
|
-
|
|
670
|
+
|
|
624
671
|
}
|
|
625
672
|
}
|
|
626
673
|
|
|
@@ -638,13 +685,13 @@ export function openMemoryStore({ dir, now = () => Date.now() } = {}) {
|
|
|
638
685
|
exportAll,
|
|
639
686
|
countVisible,
|
|
640
687
|
contextFor,
|
|
641
|
-
|
|
688
|
+
|
|
642
689
|
recallErrorOf: () => recallError,
|
|
643
690
|
close,
|
|
644
691
|
};
|
|
645
692
|
}
|
|
646
693
|
|
|
647
|
-
|
|
694
|
+
|
|
648
695
|
function toMemory(row) {
|
|
649
696
|
return {
|
|
650
697
|
id: row.id,
|
|
@@ -657,8 +704,8 @@ function toMemory(row) {
|
|
|
657
704
|
updatedAt: row.updated_at,
|
|
658
705
|
recallCount: row.recall_count,
|
|
659
706
|
pinned: row.pinned === 1,
|
|
660
|
-
|
|
661
|
-
|
|
707
|
+
|
|
708
|
+
|
|
662
709
|
stability: row.stability ?? DEFAULT_STABILITY_DAYS,
|
|
663
710
|
difficulty: row.difficulty ?? DEFAULT_DIFFICULTY,
|
|
664
711
|
lastRecalledAt: row.last_recalled_at ?? row.created_at ?? null,
|