@dsh-mobile/mobile-remote 3.4.1 → 3.5.0-beta.2
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 +1 -1
- package/docs/PRIVACY-POLICY.en.md +10 -2
- package/docs/PRIVACY-POLICY.md +10 -2
- package/docs/TERMS-OF-SERVICE.en.md +3 -2
- package/docs/TERMS-OF-SERVICE.md +3 -2
- package/lib/backup.js +30 -0
- package/lib/config.js +10 -0
- package/lib/host-plugins.js +290 -0
- package/lib/host-schedule.js +273 -0
- package/lib/host-team.js +89 -0
- package/lib/index.js +208 -3292
- package/lib/live-activity.js +351 -0
- package/lib/memory/context.js +17 -3
- package/lib/memory/decay.js +68 -4
- package/lib/memory/promises.js +116 -0
- package/lib/memory/routes.js +113 -1
- package/lib/memory/store.js +212 -13
- package/lib/memory/turn-hook.js +109 -0
- package/lib/phone-actions.js +17 -5
- package/lib/phone-announce.js +6 -5
- package/lib/phone-push.js +310 -0
- package/lib/protocol.js +16 -0
- package/lib/relay/connector.js +13 -1
- package/lib/routes/agents.js +405 -0
- package/lib/routes/app-state.js +179 -0
- package/lib/routes/backup.js +144 -0
- package/lib/routes/diagnostics.js +227 -0
- package/lib/routes/files.js +326 -0
- package/lib/routes/git.js +53 -0
- package/lib/routes/history.js +313 -0
- package/lib/routes/host-services.js +51 -0
- package/lib/routes/models.js +462 -0
- package/lib/routes/notifications.js +183 -0
- package/lib/routes/pairing.js +170 -0
- package/lib/routes/route-groups.js +20 -0
- package/lib/routes/send.js +341 -0
- package/lib/routes/sessions.js +462 -0
- package/lib/routes/toolbox.js +14 -0
- package/lib/routes/usage.js +211 -0
- package/lib/timeline.js +5 -1
- package/lib/toolbox.js +355 -0
- package/package.json +1 -1
package/lib/memory/routes.js
CHANGED
|
@@ -8,17 +8,41 @@
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
11
21
|
|
|
12
22
|
|
|
13
23
|
|
|
14
24
|
|
|
15
25
|
|
|
16
26
|
import { readBody, sendError, sendJson } from "../http-util.js";
|
|
17
|
-
import { normalizeScope } from "./store.js";
|
|
27
|
+
import { MAX_MUTE_TERM, normalizeScope } from "./store.js";
|
|
18
28
|
|
|
19
29
|
const MAX_TEXT = 4000;
|
|
20
30
|
|
|
21
31
|
|
|
32
|
+
const MAX_AHEAD_MS = 5 * 366 * 86_400_000;
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
function timeField(v) {
|
|
39
|
+
if (v === undefined) return { ok: true, value: undefined };
|
|
40
|
+
if (v === null) return { ok: true, value: null };
|
|
41
|
+
if (typeof v === "number" && Number.isFinite(v) && v > 0 && v < Date.now() + MAX_AHEAD_MS) return { ok: true, value: Math.round(v) };
|
|
42
|
+
return { ok: false };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
22
46
|
function normalizeMode(value) {
|
|
23
47
|
return value === "life" ? "life" : "work";
|
|
24
48
|
}
|
|
@@ -83,6 +107,49 @@ export function createMemoryRoutes({ store, wipe = () => 0, onDeleteAll = () =>
|
|
|
83
107
|
const s = lookup.store;
|
|
84
108
|
|
|
85
109
|
|
|
110
|
+
if (rest === "/memory/mutes") {
|
|
111
|
+
if (method === "GET" || method === "HEAD") {
|
|
112
|
+
sendJson(res, 200, { items: s.listMutes() });
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (method === "POST") {
|
|
116
|
+
let body;
|
|
117
|
+
try {
|
|
118
|
+
body = JSON.parse(await readBody(req));
|
|
119
|
+
} catch {
|
|
120
|
+
badRequest(res, "请求体不是合法 JSON");
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const term = typeof body?.term === "string" ? body.term.trim() : "";
|
|
124
|
+
if (term === "" || term.length > MAX_MUTE_TERM) {
|
|
125
|
+
badRequest(res, `term 必须是 1–${MAX_MUTE_TERM} 个字符 / term must be 1–${MAX_MUTE_TERM} characters`);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
sendJson(res, 200, { item: s.addMute(term) });
|
|
130
|
+
} catch (err) {
|
|
131
|
+
badRequest(res, err?.message ?? "写入失败");
|
|
132
|
+
}
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
sendError(res, 405, "method-not-allowed");
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const muteMatch = /^\/memory\/mutes\/(\d+)$/.exec(rest);
|
|
139
|
+
if (muteMatch) {
|
|
140
|
+
if (method !== "DELETE") {
|
|
141
|
+
sendError(res, 405, "method-not-allowed");
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (!s.removeMute(Number(muteMatch[1]))) {
|
|
145
|
+
sendError(res, 404, "mute-not-found");
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
sendJson(res, 200, { ok: true });
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
|
|
86
153
|
if (rest === "/memory/export") {
|
|
87
154
|
if (method !== "GET" && method !== "HEAD") {
|
|
88
155
|
sendError(res, 405, "method-not-allowed");
|
|
@@ -96,6 +163,11 @@ export function createMemoryRoutes({ store, wipe = () => 0, onDeleteAll = () =>
|
|
|
96
163
|
if (method === "GET" || method === "HEAD") {
|
|
97
164
|
const mode = normalizeMode(url.searchParams.get("mode"));
|
|
98
165
|
const q = (url.searchParams.get("q") ?? "").trim();
|
|
166
|
+
if (url.searchParams.get("kind") === "promise") {
|
|
167
|
+
const items = s.listPromises(mode);
|
|
168
|
+
sendJson(res, 200, { mode, kind: "promise", items, total: items.length });
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
99
171
|
const items = q ? s.search(q, { mode, limit: 50 }) : s.listVisible(mode);
|
|
100
172
|
if (q) s.markRecalled(items.map((m) => m.id));
|
|
101
173
|
sendJson(res, 200, { mode, items, total: s.countVisible(mode) });
|
|
@@ -124,11 +196,26 @@ export function createMemoryRoutes({ store, wipe = () => 0, onDeleteAll = () =>
|
|
|
124
196
|
return;
|
|
125
197
|
}
|
|
126
198
|
const source = body?.source === "manual" ? "manual" : "conversation";
|
|
199
|
+
const kind = body?.kind === undefined ? "fact" : body.kind;
|
|
200
|
+
if (kind !== "fact" && kind !== "promise") {
|
|
201
|
+
badRequest(res, "kind 必须是 fact / promise");
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const dueAt = timeField(body?.dueAt);
|
|
205
|
+
const expiresAt = timeField(body?.expiresAt);
|
|
206
|
+
if (!dueAt.ok || !expiresAt.ok) {
|
|
207
|
+
badRequest(res, "dueAt / expiresAt 必须是毫秒时间戳或 null / must be a time in ms or null");
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
127
210
|
try {
|
|
128
211
|
const item = s.add({
|
|
129
212
|
scope,
|
|
130
213
|
text,
|
|
131
214
|
source,
|
|
215
|
+
kind,
|
|
216
|
+
pinned: kind === "fact" && body?.pinned === true,
|
|
217
|
+
expiresAt: kind === "fact" && body?.pinned === true ? expiresAt.value ?? null : null,
|
|
218
|
+
dueAt: kind === "promise" ? dueAt.value ?? null : null,
|
|
132
219
|
sessionId: typeof body?.sessionId === "string" ? body.sessionId : null,
|
|
133
220
|
origin: typeof body?.origin === "string" ? body.origin : null,
|
|
134
221
|
});
|
|
@@ -186,6 +273,31 @@ export function createMemoryRoutes({ store, wipe = () => 0, onDeleteAll = () =>
|
|
|
186
273
|
}
|
|
187
274
|
s.setScope(id, scope);
|
|
188
275
|
}
|
|
276
|
+
|
|
277
|
+
const expiresAt = timeField(body?.expiresAt);
|
|
278
|
+
const dueAt = timeField(body?.dueAt);
|
|
279
|
+
if (!expiresAt.ok || !dueAt.ok) {
|
|
280
|
+
badRequest(res, "dueAt / expiresAt 必须是毫秒时间戳或 null / must be a time in ms or null");
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
const row = s.get(id);
|
|
284
|
+
if (body?.pinned !== undefined || expiresAt.value !== undefined) {
|
|
285
|
+
if (row.kind !== "fact") {
|
|
286
|
+
badRequest(res, "只有记忆条目能置顶 / only facts can be pinned");
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
const pinned = body?.pinned === undefined ? row.pinned : body.pinned === true;
|
|
290
|
+
s.setPinned(id, pinned, expiresAt.value === undefined ? row.expiresAt : expiresAt.value);
|
|
291
|
+
}
|
|
292
|
+
if (body?.confirm === true) s.confirm(id);
|
|
293
|
+
if (body?.done !== undefined || dueAt.value !== undefined) {
|
|
294
|
+
if (row.kind !== "promise") {
|
|
295
|
+
badRequest(res, "done / dueAt 只用于跟进事项 / done and dueAt are for promises");
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
if (body?.done !== undefined) s.setDone(id, body.done === true);
|
|
299
|
+
if (dueAt.value !== undefined) s.setDue(id, dueAt.value);
|
|
300
|
+
}
|
|
189
301
|
} catch (err) {
|
|
190
302
|
badRequest(res, err?.message ?? "更新失败");
|
|
191
303
|
return;
|
package/lib/memory/store.js
CHANGED
|
@@ -28,6 +28,8 @@ import {
|
|
|
28
28
|
DEFAULT_DIFFICULTY,
|
|
29
29
|
DEFAULT_STABILITY_DAYS,
|
|
30
30
|
FORGET_THRESHOLD,
|
|
31
|
+
evidenceStatusOf,
|
|
32
|
+
evidenceWeightOf,
|
|
31
33
|
nextDifficulty,
|
|
32
34
|
nextStability,
|
|
33
35
|
partitionInjectable,
|
|
@@ -62,6 +64,16 @@ export const SCOPES = (["work", "life", "shared"]);
|
|
|
62
64
|
|
|
63
65
|
export const SOURCES = (["conversation", "manual", "import"]);
|
|
64
66
|
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
export const KINDS = (["fact", "promise"]);
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
export const MAX_MUTE_TERM = 80;
|
|
75
|
+
export const MAX_MUTES = 200;
|
|
76
|
+
|
|
65
77
|
const SCHEMA = `
|
|
66
78
|
CREATE TABLE IF NOT EXISTS memory (
|
|
67
79
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
@@ -85,6 +97,13 @@ CREATE TABLE IF NOT EXISTS memory (
|
|
|
85
97
|
);
|
|
86
98
|
CREATE INDEX IF NOT EXISTS idx_memory_scope ON memory(scope, deleted_at);
|
|
87
99
|
CREATE INDEX IF NOT EXISTS idx_memory_created ON memory(created_at DESC);
|
|
100
|
+
-- T-50: "stop mentioning X". Matching facts are not injected, new ones are not recorded from chat, and the agent
|
|
101
|
+
-- is told not to bring the term up. Deleting a term leaves no text behind (secure_delete + WAL truncate).
|
|
102
|
+
CREATE TABLE IF NOT EXISTS memory_mute (
|
|
103
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
104
|
+
term TEXT NOT NULL,
|
|
105
|
+
created_at INTEGER NOT NULL
|
|
106
|
+
);
|
|
88
107
|
`;
|
|
89
108
|
|
|
90
109
|
|
|
@@ -92,9 +111,28 @@ const MIGRATIONS = [
|
|
|
92
111
|
{ column: "stability", ddl: "ALTER TABLE memory ADD COLUMN stability REAL NOT NULL DEFAULT 7" },
|
|
93
112
|
{ column: "difficulty", ddl: "ALTER TABLE memory ADD COLUMN difficulty REAL NOT NULL DEFAULT 5" },
|
|
94
113
|
{ column: "last_recalled_at", ddl: "ALTER TABLE memory ADD COLUMN last_recalled_at INTEGER" },
|
|
114
|
+
|
|
115
|
+
{ column: "kind", ddl: "ALTER TABLE memory ADD COLUMN kind TEXT NOT NULL DEFAULT 'fact'" },
|
|
116
|
+
{ column: "expires_at", ddl: "ALTER TABLE memory ADD COLUMN expires_at INTEGER" },
|
|
117
|
+
{ column: "due_at", ddl: "ALTER TABLE memory ADD COLUMN due_at INTEGER" },
|
|
118
|
+
{ column: "done_at", ddl: "ALTER TABLE memory ADD COLUMN done_at INTEGER" },
|
|
119
|
+
|
|
120
|
+
{ column: "evidence_at", ddl: "ALTER TABLE memory ADD COLUMN evidence_at INTEGER" },
|
|
121
|
+
|
|
122
|
+
{ column: "confirmed", ddl: "ALTER TABLE memory ADD COLUMN confirmed INTEGER NOT NULL DEFAULT 0" },
|
|
95
123
|
];
|
|
96
124
|
|
|
97
125
|
|
|
126
|
+
export function mutedBy(text, terms) {
|
|
127
|
+
const lower = String(text ?? "").toLowerCase();
|
|
128
|
+
for (const t of terms) {
|
|
129
|
+
const term = String(t?.term ?? t ?? "").trim().toLowerCase();
|
|
130
|
+
if (term !== "" && lower.includes(term)) return t?.term ?? t;
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
|
|
98
136
|
|
|
99
137
|
|
|
100
138
|
|
|
@@ -306,6 +344,9 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
306
344
|
|
|
307
345
|
db.exec(`UPDATE memory SET last_recalled_at = created_at WHERE last_recalled_at IS NULL`);
|
|
308
346
|
|
|
347
|
+
db.exec(`UPDATE memory SET evidence_at = updated_at WHERE evidence_at IS NULL`);
|
|
348
|
+
if (!existing.has("confirmed")) db.exec(`UPDATE memory SET confirmed = 1 WHERE source = 'manual'`);
|
|
349
|
+
|
|
309
350
|
|
|
310
351
|
|
|
311
352
|
|
|
@@ -390,14 +431,18 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
390
431
|
}
|
|
391
432
|
|
|
392
433
|
const insert = db.prepare(
|
|
393
|
-
`INSERT INTO memory (scope, text, source, session_id, origin, created_at, updated_at, pinned, stability, difficulty, last_recalled_at
|
|
394
|
-
|
|
434
|
+
`INSERT INTO memory (scope, text, source, session_id, origin, created_at, updated_at, pinned, stability, difficulty, last_recalled_at,
|
|
435
|
+
kind, expires_at, due_at, evidence_at, confirmed)
|
|
436
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
395
437
|
);
|
|
396
438
|
const selectAll = db.prepare(
|
|
397
|
-
`SELECT * FROM memory WHERE deleted_at IS NULL AND scope IN (${SCOPES.map(
|
|
439
|
+
`SELECT * FROM memory WHERE deleted_at IS NULL AND kind = ? AND scope IN (${SCOPES.map(
|
|
398
440
|
() => "?",
|
|
399
441
|
).join(",")}) ORDER BY pinned DESC, created_at DESC`,
|
|
400
442
|
);
|
|
443
|
+
const selectMutes = db.prepare(`SELECT * FROM memory_mute ORDER BY id ASC`);
|
|
444
|
+
const listMutes = () => selectMutes.all().map((r) => ({ id: r.id, term: r.term, createdAt: r.created_at }));
|
|
445
|
+
const facts = (visible) => selectAll.all("fact", ...visible).map(toMemory);
|
|
401
446
|
|
|
402
447
|
|
|
403
448
|
|
|
@@ -405,13 +450,30 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
405
450
|
|
|
406
451
|
|
|
407
452
|
|
|
408
|
-
function add({
|
|
453
|
+
function add({
|
|
454
|
+
scope, text, source = "conversation", sessionId = null, origin = null, pinned = false,
|
|
455
|
+
kind = "fact", expiresAt = null, dueAt = null, doneAt = null,
|
|
456
|
+
}) {
|
|
409
457
|
const s = normalizeScope(scope);
|
|
410
458
|
if (!s) throw new Error(`invalid scope: ${scope}`);
|
|
411
459
|
if (!SOURCES.includes(source)) throw new Error(`invalid source: ${source}`);
|
|
460
|
+
if (!KINDS.includes(kind)) throw new Error(`invalid kind: ${kind}`);
|
|
412
461
|
const clean = scrubMemoryText(text ?? "");
|
|
413
462
|
if (clean === "") throw new Error("memory text is empty after scrubbing");
|
|
463
|
+
|
|
464
|
+
if (source === "conversation" && mutedBy(clean, listMutes())) throw new Error("memory text mentions a muted term");
|
|
414
465
|
const t = now();
|
|
466
|
+
|
|
467
|
+
if (source === "conversation") {
|
|
468
|
+
const same = db
|
|
469
|
+
.prepare(`SELECT id FROM memory WHERE kind = ? AND scope = ? AND text = ? AND done_at IS NULL LIMIT 1`)
|
|
470
|
+
.get(kind, s, clean);
|
|
471
|
+
if (same) {
|
|
472
|
+
db.prepare(`UPDATE memory SET evidence_at = ? WHERE id = ?`).run(t, same.id);
|
|
473
|
+
if (kind === "fact") markRecalled([same.id]);
|
|
474
|
+
return { ...get(same.id), restated: true };
|
|
475
|
+
}
|
|
476
|
+
}
|
|
415
477
|
const info = insert.run(
|
|
416
478
|
s,
|
|
417
479
|
clean,
|
|
@@ -425,10 +487,17 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
425
487
|
DEFAULT_DIFFICULTY,
|
|
426
488
|
|
|
427
489
|
t,
|
|
490
|
+
kind,
|
|
491
|
+
validTime(expiresAt),
|
|
492
|
+
validTime(dueAt),
|
|
493
|
+
t,
|
|
494
|
+
source === "conversation" ? 0 : 1,
|
|
428
495
|
);
|
|
429
|
-
const
|
|
496
|
+
const id = Number(info.lastInsertRowid);
|
|
497
|
+
if (validTime(doneAt) !== null) db.prepare(`UPDATE memory SET done_at = ? WHERE id = ?`).run(validTime(doneAt), id);
|
|
498
|
+
const created = get(id);
|
|
430
499
|
|
|
431
|
-
appendArchive("add", { id: created?.id, scope: s, text: clean, source });
|
|
500
|
+
appendArchive("add", { id: created?.id, scope: s, kind, text: clean, source });
|
|
432
501
|
return created;
|
|
433
502
|
}
|
|
434
503
|
|
|
@@ -439,8 +508,77 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
439
508
|
|
|
440
509
|
|
|
441
510
|
function listVisible(mode) {
|
|
442
|
-
|
|
443
|
-
|
|
511
|
+
return withStatus(facts(scopesVisibleTo(mode)));
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
function withStatus(rows) {
|
|
516
|
+
const at = new Date(now());
|
|
517
|
+
const mutes = listMutes();
|
|
518
|
+
return rows.map((r) => ({
|
|
519
|
+
...r,
|
|
520
|
+
status: r.kind === "promise" ? promiseStatus(r, at) : evidenceStatusOf(r, at),
|
|
521
|
+
evidence: r.kind === "promise" ? 1 : Math.round(evidenceWeightOf(r, at) * 100) / 100,
|
|
522
|
+
muted: mutedBy(r.text, mutes) !== null,
|
|
523
|
+
}));
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
function listPromises(mode, { includeDone = true } = {}) {
|
|
528
|
+
const rows = selectAll.all("promise", ...scopesVisibleTo(mode)).map(toMemory);
|
|
529
|
+
const open = rows.filter((r) => r.doneAt == null);
|
|
530
|
+
const done = includeDone ? rows.filter((r) => r.doneAt != null) : [];
|
|
531
|
+
open.sort((a, b) => (a.dueAt ?? Infinity) - (b.dueAt ?? Infinity) || a.createdAt - b.createdAt);
|
|
532
|
+
done.sort((a, b) => b.doneAt - a.doneAt);
|
|
533
|
+
return withStatus([...open, ...done]);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
function setDone(id, done) {
|
|
537
|
+
db.prepare(`UPDATE memory SET done_at = ?, updated_at = ? WHERE id = ? AND kind = 'promise'`).run(done ? now() : null, now(), id);
|
|
538
|
+
return get(id);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
function setDue(id, dueAt) {
|
|
542
|
+
db.prepare(`UPDATE memory SET due_at = ?, updated_at = ? WHERE id = ? AND kind = 'promise'`).run(validTime(dueAt), now(), id);
|
|
543
|
+
return get(id);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
function setPinned(id, pinned, expiresAt = null) {
|
|
548
|
+
db.prepare(`UPDATE memory SET pinned = ?, expires_at = ?, updated_at = ?, evidence_at = ? WHERE id = ?`).run(
|
|
549
|
+
pinned ? 1 : 0,
|
|
550
|
+
pinned ? validTime(expiresAt) : null,
|
|
551
|
+
now(),
|
|
552
|
+
now(),
|
|
553
|
+
id,
|
|
554
|
+
);
|
|
555
|
+
return get(id);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
function confirm(id) {
|
|
560
|
+
const t = now();
|
|
561
|
+
db.prepare(`UPDATE memory SET confirmed = 1, evidence_at = ?, updated_at = ? WHERE id = ?`).run(t, t, id);
|
|
562
|
+
markRecalled([Number(id)]);
|
|
563
|
+
return get(id);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
function addMute(term) {
|
|
567
|
+
const clean = scrubMemoryText(String(term ?? "").replace(/\s+/g, " ")).slice(0, MAX_MUTE_TERM).trim();
|
|
568
|
+
if (clean === "") throw new Error("mute term is empty");
|
|
569
|
+
const existing = listMutes();
|
|
570
|
+
const same = existing.find((m) => m.term.toLowerCase() === clean.toLowerCase());
|
|
571
|
+
if (same) return same;
|
|
572
|
+
if (existing.length >= MAX_MUTES) throw new Error(`at most ${MAX_MUTES} muted terms`);
|
|
573
|
+
const info = db.prepare(`INSERT INTO memory_mute (term, created_at) VALUES (?, ?)`).run(clean, now());
|
|
574
|
+
return listMutes().find((m) => m.id === Number(info.lastInsertRowid));
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
function removeMute(id) {
|
|
579
|
+
const n = Number(db.prepare(`DELETE FROM memory_mute WHERE id = ?`).run(Number(id)).changes ?? 0);
|
|
580
|
+
if (n > 0) truncateWal();
|
|
581
|
+
return n > 0;
|
|
444
582
|
}
|
|
445
583
|
|
|
446
584
|
|
|
@@ -454,8 +592,7 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
454
592
|
function search(query, { mode = "work", limit = 10, thaw = true } = {}) {
|
|
455
593
|
const terms = queryTerms(query);
|
|
456
594
|
if (terms.length === 0) return [];
|
|
457
|
-
const
|
|
458
|
-
const rows = selectAll.all(...visible).map(toMemory);
|
|
595
|
+
const rows = listVisible(mode);
|
|
459
596
|
if (rows.length === 0) return [];
|
|
460
597
|
const scores = bm25Scores(
|
|
461
598
|
rows.map((r) => `${r.text} ${r.origin ?? ""}`),
|
|
@@ -487,12 +624,13 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
487
624
|
const clean = scrubMemoryText(text ?? "");
|
|
488
625
|
if (clean === "") throw new Error("memory text is empty after scrubbing");
|
|
489
626
|
const changed = Number(
|
|
490
|
-
|
|
627
|
+
|
|
628
|
+
db.prepare(`UPDATE memory SET text = ?, updated_at = ?, evidence_at = ?, confirmed = 1 WHERE id = ? AND deleted_at IS NULL`).run(clean, now(), now(), id).changes ?? 0,
|
|
491
629
|
);
|
|
492
630
|
const row = get(id);
|
|
493
631
|
if (changed > 0 && row) {
|
|
494
632
|
purgeArchive(archiveFile, [Number(id)]);
|
|
495
|
-
appendArchive("add", { id: row.id, scope: row.scope, text: row.text, source: row.source });
|
|
633
|
+
appendArchive("add", { id: row.id, scope: row.scope, kind: row.kind, text: row.text, source: row.source });
|
|
496
634
|
truncateWal();
|
|
497
635
|
}
|
|
498
636
|
return row;
|
|
@@ -525,6 +663,7 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
525
663
|
|
|
526
664
|
function removeAll() {
|
|
527
665
|
const n = Number(db.prepare(`DELETE FROM memory`).run().changes ?? 0);
|
|
666
|
+
db.prepare(`DELETE FROM memory_mute`).run();
|
|
528
667
|
try {
|
|
529
668
|
writeFileSync(archiveFile, "", { mode: 0o600 });
|
|
530
669
|
} catch {
|
|
@@ -611,7 +750,8 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
611
750
|
|
|
612
751
|
|
|
613
752
|
const at = new Date(now());
|
|
614
|
-
|
|
753
|
+
|
|
754
|
+
const { injectable } = partitionInjectable(listVisible(mode).filter((r) => !r.muted), {
|
|
615
755
|
now: at,
|
|
616
756
|
limit,
|
|
617
757
|
threshold: FORGET_THRESHOLD,
|
|
@@ -659,6 +799,28 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
659
799
|
return lines.join("\n");
|
|
660
800
|
}
|
|
661
801
|
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
function promisesContextFor(mode, { limit = 5 } = {}) {
|
|
807
|
+
const at = now();
|
|
808
|
+
const mutes = listMutes();
|
|
809
|
+
const open = listPromises(mode, { includeDone: false }).filter((p) => !mutedBy(p.text, mutes)).slice(0, limit);
|
|
810
|
+
return open
|
|
811
|
+
.map((p) => {
|
|
812
|
+
const due = p.dueAt == null ? "" : `(${p.dueAt <= at ? "已过期限 / overdue, " : ""}期限 / due ${localStamp(p.dueAt)})`;
|
|
813
|
+
const line = `- ${p.text}${due}`;
|
|
814
|
+
return line.length > 300 ? `${line.slice(0, 299)}…` : line;
|
|
815
|
+
})
|
|
816
|
+
.join("\n");
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
function mutedTermsFor() {
|
|
821
|
+
return listMutes().map((m) => m.term);
|
|
822
|
+
}
|
|
823
|
+
|
|
662
824
|
function close() {
|
|
663
825
|
checkpointOnce();
|
|
664
826
|
closed = true;
|
|
@@ -686,6 +848,17 @@ export function openMemoryStore({ dir, now = () => Date.now(), walRetryMs = 1000
|
|
|
686
848
|
countVisible,
|
|
687
849
|
contextFor,
|
|
688
850
|
|
|
851
|
+
listPromises,
|
|
852
|
+
setDone,
|
|
853
|
+
setDue,
|
|
854
|
+
setPinned,
|
|
855
|
+
confirm,
|
|
856
|
+
listMutes,
|
|
857
|
+
addMute,
|
|
858
|
+
removeMute,
|
|
859
|
+
promisesContextFor,
|
|
860
|
+
mutedTermsFor,
|
|
861
|
+
|
|
689
862
|
recallErrorOf: () => recallError,
|
|
690
863
|
close,
|
|
691
864
|
};
|
|
@@ -710,5 +883,31 @@ function toMemory(row) {
|
|
|
710
883
|
difficulty: row.difficulty ?? DEFAULT_DIFFICULTY,
|
|
711
884
|
lastRecalledAt: row.last_recalled_at ?? row.created_at ?? null,
|
|
712
885
|
deletedAt: row.deleted_at ?? null,
|
|
886
|
+
|
|
887
|
+
kind: row.kind ?? "fact",
|
|
888
|
+
expiresAt: row.expires_at ?? null,
|
|
889
|
+
dueAt: row.due_at ?? null,
|
|
890
|
+
doneAt: row.done_at ?? null,
|
|
891
|
+
evidenceAt: row.evidence_at ?? row.updated_at ?? null,
|
|
892
|
+
confirmed: row.confirmed === 1,
|
|
713
893
|
};
|
|
714
894
|
}
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
function validTime(v) {
|
|
898
|
+
const n = typeof v === "number" ? v : NaN;
|
|
899
|
+
return Number.isFinite(n) && n > 0 ? Math.round(n) : null;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
function promiseStatus(row, at) {
|
|
903
|
+
if (row.doneAt != null) return "done";
|
|
904
|
+
if (row.dueAt != null && row.dueAt <= at.getTime()) return "overdue";
|
|
905
|
+
return "open";
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
function localStamp(ms) {
|
|
910
|
+
const d = new Date(ms);
|
|
911
|
+
const p = (n) => String(n).padStart(2, "0");
|
|
912
|
+
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`;
|
|
913
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
import { extractMemories, recordMemories } from "./extract.js";
|
|
12
|
+
import { extractPromises } from "./promises.js";
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
export function createMemoryTurnHook({ enabled, getStore, getMode, blocksToText, broadcast, logger, shortId = (s) => s, now = () => new Date() }) {
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
const pendingUser = new Map();
|
|
29
|
+
const pendingReply = new Map();
|
|
30
|
+
|
|
31
|
+
const onEvent = (session, event) => {
|
|
32
|
+
const sid = session?.id;
|
|
33
|
+
if (!sid || !event) return;
|
|
34
|
+
if (event.type === "user/message") {
|
|
35
|
+
try {
|
|
36
|
+
pendingUser.set(sid, blocksToText(event.data?.message?.content));
|
|
37
|
+
} catch {
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (event.type === "assistant/message") {
|
|
43
|
+
try {
|
|
44
|
+
const text = blocksToText(event.data?.message?.content);
|
|
45
|
+
if (text && text.trim() !== "") pendingReply.set(sid, text);
|
|
46
|
+
} catch {
|
|
47
|
+
|
|
48
|
+
}
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (event.type !== "turn/end") return;
|
|
52
|
+
const said = pendingUser.get(sid);
|
|
53
|
+
const reply = pendingReply.get(sid);
|
|
54
|
+
pendingUser.delete(sid);
|
|
55
|
+
pendingReply.delete(sid);
|
|
56
|
+
|
|
57
|
+
if ((!said && !reply) || !enabled()) return;
|
|
58
|
+
try {
|
|
59
|
+
const store = getStore();
|
|
60
|
+
if (!store) return;
|
|
61
|
+
const mode = getMode();
|
|
62
|
+
const meta = { sessionId: sid, origin: session.header?.title ?? null };
|
|
63
|
+
let written = 0;
|
|
64
|
+
if (said) {
|
|
65
|
+
const candidates = extractMemories({ text: said, mode });
|
|
66
|
+
if (candidates.length > 0) written += recordMemories({ store, candidates, meta });
|
|
67
|
+
}
|
|
68
|
+
if (reply) {
|
|
69
|
+
for (const p of extractPromises(reply, { now: now() })) {
|
|
70
|
+
try {
|
|
71
|
+
const row = store.add({
|
|
72
|
+
scope: mode === "life" ? "life" : "work",
|
|
73
|
+
kind: "promise",
|
|
74
|
+
text: p.text,
|
|
75
|
+
dueAt: p.dueAt,
|
|
76
|
+
source: "conversation",
|
|
77
|
+
sessionId: meta.sessionId,
|
|
78
|
+
origin: meta.origin,
|
|
79
|
+
});
|
|
80
|
+
if (!row?.restated) written += 1;
|
|
81
|
+
} catch {
|
|
82
|
+
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (written > 0) {
|
|
87
|
+
logger.info?.(`mobile-remote: 记住了 ${written} 条(会话 ${shortId(sid)})`);
|
|
88
|
+
broadcast({ type: "memory/changed" });
|
|
89
|
+
}
|
|
90
|
+
} catch (err) {
|
|
91
|
+
|
|
92
|
+
logger.warn(`mobile-remote: 记忆写入失败:${err?.message ?? err}`);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
onEvent,
|
|
98
|
+
|
|
99
|
+
forget(sid) {
|
|
100
|
+
pendingUser.delete(sid);
|
|
101
|
+
pendingReply.delete(sid);
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
clear() {
|
|
105
|
+
pendingUser.clear();
|
|
106
|
+
pendingReply.clear();
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
package/lib/phone-actions.js
CHANGED
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
|
|
17
|
+
|
|
18
|
+
|
|
17
19
|
import { randomUUID } from "node:crypto";
|
|
18
20
|
import { hostname } from "node:os";
|
|
19
21
|
import { ACTIONS, TOOL_NAMES, TOOL_SPECS, validatePhoneAction } from "./phone-action-args.js";
|
|
@@ -49,7 +51,7 @@ const sessionIdOf = (agent) => agent?.session?.id ?? String(agent?.id ?? "").rep
|
|
|
49
51
|
|
|
50
52
|
|
|
51
53
|
|
|
52
|
-
export const createPhoneActions = ({ ctx, broadcast, phoneConnected, pcName, now = Date.now, setTimer = setTimeout, clearTimer = clearTimeout, graceMs = GRACE_MS }) => {
|
|
54
|
+
export const createPhoneActions = ({ ctx, broadcast, phoneConnected, pcName, canPush = () => false, onRequest = () => {}, now = Date.now, setTimer = setTimeout, clearTimer = clearTimeout, graceMs = GRACE_MS }) => {
|
|
53
55
|
const pending = new Map();
|
|
54
56
|
const settled = new Map();
|
|
55
57
|
const turns = new Map();
|
|
@@ -77,7 +79,8 @@ export const createPhoneActions = ({ ctx, broadcast, phoneConnected, pcName, now
|
|
|
77
79
|
};
|
|
78
80
|
|
|
79
81
|
const request = (action, args, exec) => {
|
|
80
|
-
|
|
82
|
+
const pushed = !phoneConnected() && canPush();
|
|
83
|
+
if (!phoneConnected() && !pushed) throw new Error("phone not connected: the DSH Mobile app is not connected to this computer right now, so this action cannot be done. Tell the user.");
|
|
81
84
|
if (pending.size >= MAX_PENDING) throw new Error("too many phone actions are waiting for the user");
|
|
82
85
|
const id = randomUUID();
|
|
83
86
|
const sessionId = sessionIdOf(exec?.agent);
|
|
@@ -96,13 +99,18 @@ export const createPhoneActions = ({ ctx, broadcast, phoneConnected, pcName, now
|
|
|
96
99
|
entry.offAbort = () => signal.removeEventListener("abort", onAbort);
|
|
97
100
|
}
|
|
98
101
|
}
|
|
99
|
-
|
|
100
|
-
broadcast({
|
|
102
|
+
const frame = {
|
|
101
103
|
type: "phone-action-request", id, pcName: name, action, args,
|
|
102
104
|
expiresAt: at + ASK_MS,
|
|
103
105
|
...(sessionId ? { sessionId } : {}),
|
|
104
106
|
...(turnId ? { turnId } : {}),
|
|
105
|
-
}
|
|
107
|
+
};
|
|
108
|
+
entry.frame = frame;
|
|
109
|
+
pending.set(id, entry);
|
|
110
|
+
broadcast(frame);
|
|
111
|
+
if (pushed) {
|
|
112
|
+
try { onRequest(frame); } catch { }
|
|
113
|
+
}
|
|
106
114
|
});
|
|
107
115
|
};
|
|
108
116
|
|
|
@@ -223,5 +231,9 @@ export const createPhoneActions = ({ ctx, broadcast, phoneConnected, pcName, now
|
|
|
223
231
|
|
|
224
232
|
features: () => (supported ? [FEATURE] : []),
|
|
225
233
|
pendingCount: () => pending.size,
|
|
234
|
+
|
|
235
|
+
pendingFrames: () => [...pending.values()].map((e) => e.frame).filter((f) => f && f.expiresAt > now()),
|
|
236
|
+
|
|
237
|
+
cancelFromPush: (id) => settle(id, { status: "cancelled", detail: "cancelled on the phone's lock screen" }, { byPhone: true }),
|
|
226
238
|
};
|
|
227
239
|
};
|