agent-loft 0.2.87 → 0.2.89
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 +7 -5
- package/package.json +1 -1
- package/src/agents/claude/session.ts +1 -1
- package/src/channels/web/api.ts +158 -6
- package/src/core/backup.ts +228 -0
- package/src/core/chat-history.ts +224 -56
- package/src/core/db.ts +72 -1
- package/src/core/fts.ts +53 -0
- package/src/core/hub.ts +20 -5
- package/src/core/memory-curator.ts +418 -76
- package/src/core/memory-dates.ts +16 -0
- package/src/core/memory-mcp.ts +1 -1
- package/src/core/memory-summaries.ts +225 -0
- package/src/core/memory.ts +698 -73
- package/src/core/personal.ts +33 -2
- package/src/core/tasks.ts +8 -7
- package/src/main.ts +27 -1
- package/src/mcp/memory-server.ts +237 -21
- package/web/css/settings.css +27 -0
- package/web/settings/memory.js +291 -13
- package/web/views/layout.js +2 -2
- package/web/views/messages.js +2 -2
- package/web/views/sidebar.js +1 -1
- package/web/views/todos.js +3 -3
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// 模型写的日期(工具参数、整理结果):YYYY-MM 或 YYYY-MM-DD,当成服务器所在时区的时间。
|
|
2
|
+
// 单独成模块:记忆工具子进程要用,不能因此把 hub 整个加载进去。
|
|
3
|
+
|
|
4
|
+
/** 写错了就当没有。不合法的日子(2 月 31 日)也算写错,不让它滚到下个月 */
|
|
5
|
+
export function parseDay(v: unknown): number | undefined {
|
|
6
|
+
if (typeof v !== "string") return undefined;
|
|
7
|
+
const m = /^(\d{4})-(\d{1,2})(?:-(\d{1,2}))?$/.exec(v.trim());
|
|
8
|
+
if (!m) return undefined;
|
|
9
|
+
const year = Number(m[1]);
|
|
10
|
+
const month = Number(m[2]);
|
|
11
|
+
const day = Number(m[3] ?? 1);
|
|
12
|
+
if (month < 1 || month > 12 || day < 1) return undefined;
|
|
13
|
+
const d = new Date(year, month - 1, day);
|
|
14
|
+
if (d.getMonth() !== month - 1 || d.getDate() !== day) return undefined;
|
|
15
|
+
return d.getTime();
|
|
16
|
+
}
|
package/src/core/memory-mcp.ts
CHANGED
|
@@ -20,7 +20,7 @@ export type StdioSpawn = { command: string; args: string[]; env: Record<string,
|
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* via:日志里标一下是哪个 agent 在写(方便排查),不影响写给谁。
|
|
23
|
-
* search
|
|
23
|
+
* search:给不给搜聊天记录的工具(search_past_chats、read_past_chat、memory_timeline);convKey:当前这条对话,搜索时跳过它(它本来就在上下文里)。
|
|
24
24
|
*/
|
|
25
25
|
export function memoryMcpSpawn(userId: string, via: string, opts: { search?: boolean; convKey?: string } = {}): StdioSpawn {
|
|
26
26
|
return {
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// 摘要层:夹在「一句话的记忆条目」和「原始聊天」之间,回答「那件事的来龙去脉」这类问题。
|
|
2
|
+
//
|
|
3
|
+
// - conv:一段对话的摘要。整理记忆时顺带写(同一次调用,不多占额度),对话继续聊就更新;
|
|
4
|
+
// - month:一个月的摘要。月份过去之后,由这个月的对话摘要合成(见 memory-curator.ts 的 rollupMonths)。
|
|
5
|
+
//
|
|
6
|
+
// 只有在整理范围内的对话才有摘要。删对话时它的摘要一起删,它跨过的每个月的月度摘要也立刻删掉
|
|
7
|
+
// (不是标记过期等重新合成:关着整理的人可能永远等不到,被删的内容不能一直留着、还能被搜到)。
|
|
8
|
+
// 还剩对话摘要的月份,下次合成时会重新生成。
|
|
9
|
+
import { sql, transaction } from "./db.js";
|
|
10
|
+
import { ftsIndexText, ftsMatch } from "./fts.js";
|
|
11
|
+
|
|
12
|
+
export type MemorySummary = {
|
|
13
|
+
id: number;
|
|
14
|
+
kind: "conv" | "month";
|
|
15
|
+
/** conv:对话 key;month:YYYY-MM */
|
|
16
|
+
scope: string;
|
|
17
|
+
title: string;
|
|
18
|
+
text: string;
|
|
19
|
+
/** 对话的工作目录,同一个项目的对话放在一起看 */
|
|
20
|
+
project?: string;
|
|
21
|
+
fromTs: number;
|
|
22
|
+
toTs: number;
|
|
23
|
+
updatedAt: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const SUMMARY_LIMITS = { title: 80, conv: 600, month: 1500 };
|
|
27
|
+
|
|
28
|
+
type Row = {
|
|
29
|
+
id: number;
|
|
30
|
+
kind: "conv" | "month";
|
|
31
|
+
scope: string;
|
|
32
|
+
title: string;
|
|
33
|
+
text: string;
|
|
34
|
+
project: string | null;
|
|
35
|
+
from_ts: number;
|
|
36
|
+
to_ts: number;
|
|
37
|
+
updated_at: number;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const COLUMNS = "id, kind, scope, title, text, project, from_ts, to_ts, updated_at";
|
|
41
|
+
|
|
42
|
+
function toSummary(r: Row): MemorySummary {
|
|
43
|
+
return {
|
|
44
|
+
id: r.id,
|
|
45
|
+
kind: r.kind,
|
|
46
|
+
scope: r.scope,
|
|
47
|
+
title: r.title,
|
|
48
|
+
text: r.text,
|
|
49
|
+
...(r.project ? { project: r.project } : {}),
|
|
50
|
+
fromTs: r.from_ts,
|
|
51
|
+
toTs: r.to_ts,
|
|
52
|
+
updatedAt: r.updated_at,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** 本地月份 YYYY-MM */
|
|
57
|
+
export function localMonth(ts: number): string {
|
|
58
|
+
const d = new Date(ts);
|
|
59
|
+
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function reindex(id: number, title: string, text: string): void {
|
|
63
|
+
sql("DELETE FROM memory_summaries_fts WHERE rowid = ?").run(id);
|
|
64
|
+
sql("INSERT INTO memory_summaries_fts (rowid, text) VALUES (?, ?)").run(id, ftsIndexText(`${title}\n${text}`));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function readSummary(userId: string, kind: MemorySummary["kind"], scope: string): MemorySummary | undefined {
|
|
68
|
+
const row = sql(`SELECT ${COLUMNS} FROM memory_summaries WHERE user_id = ? AND kind = ? AND scope = ?`).get(userId, kind, scope) as Row | undefined;
|
|
69
|
+
return row ? toSummary(row) : undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 写一份摘要,同一范围的覆盖。fromTs 取旧的和新的里更早的那个 */
|
|
73
|
+
export function upsertSummary(
|
|
74
|
+
userId: string,
|
|
75
|
+
s: { kind: MemorySummary["kind"]; scope: string; title: string; text: string; project?: string; fromTs: number; toTs: number },
|
|
76
|
+
now = Date.now(),
|
|
77
|
+
): MemorySummary {
|
|
78
|
+
const title = s.title.trim().slice(0, SUMMARY_LIMITS.title);
|
|
79
|
+
const text = s.text.trim().slice(0, s.kind === "conv" ? SUMMARY_LIMITS.conv : SUMMARY_LIMITS.month);
|
|
80
|
+
return transaction(() => {
|
|
81
|
+
const old = readSummary(userId, s.kind, s.scope);
|
|
82
|
+
const from = old ? Math.min(old.fromTs, s.fromTs) : s.fromTs;
|
|
83
|
+
const to = old ? Math.max(old.toTs, s.toTs) : s.toTs;
|
|
84
|
+
sql(
|
|
85
|
+
`INSERT INTO memory_summaries (user_id, kind, scope, title, text, project, from_ts, to_ts, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
86
|
+
ON CONFLICT (user_id, kind, scope) DO UPDATE SET title = excluded.title, text = excluded.text, project = excluded.project,
|
|
87
|
+
from_ts = excluded.from_ts, to_ts = excluded.to_ts, updated_at = excluded.updated_at`,
|
|
88
|
+
).run(userId, s.kind, s.scope, title, text, s.project ?? null, from, to, now);
|
|
89
|
+
const saved = readSummary(userId, s.kind, s.scope)!;
|
|
90
|
+
reindex(saved.id, title, text);
|
|
91
|
+
return saved;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function listSummaries(
|
|
96
|
+
userId: string,
|
|
97
|
+
opts: { kind?: MemorySummary["kind"]; after?: number; before?: number; project?: string; limit?: number; offset?: number } = {},
|
|
98
|
+
): MemorySummary[] {
|
|
99
|
+
const where = ["user_id = ?"];
|
|
100
|
+
const args: (string | number)[] = [userId];
|
|
101
|
+
if (opts.kind) {
|
|
102
|
+
where.push("kind = ?");
|
|
103
|
+
args.push(opts.kind);
|
|
104
|
+
}
|
|
105
|
+
if (opts.after !== undefined) {
|
|
106
|
+
where.push("to_ts >= ?");
|
|
107
|
+
args.push(opts.after);
|
|
108
|
+
}
|
|
109
|
+
if (opts.before !== undefined) {
|
|
110
|
+
where.push("from_ts < ?");
|
|
111
|
+
args.push(opts.before);
|
|
112
|
+
}
|
|
113
|
+
if (opts.project) {
|
|
114
|
+
where.push("instr(lower(project), ?) > 0");
|
|
115
|
+
args.push(opts.project.toLowerCase());
|
|
116
|
+
}
|
|
117
|
+
const rows = sql(`SELECT ${COLUMNS} FROM memory_summaries WHERE ${where.join(" AND ")} ORDER BY to_ts DESC, id DESC LIMIT ? OFFSET ?`).all(
|
|
118
|
+
...args,
|
|
119
|
+
Math.max(1, Math.min(500, opts.limit ?? 50)),
|
|
120
|
+
Math.max(0, opts.offset ?? 0),
|
|
121
|
+
) as Row[];
|
|
122
|
+
return rows.map(toSummary);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** 全部摘要(导出用) */
|
|
126
|
+
export function exportSummaries(userId: string): MemorySummary[] {
|
|
127
|
+
const rows = sql(`SELECT ${COLUMNS} FROM memory_summaries WHERE user_id = ? ORDER BY id`).all(userId) as Row[];
|
|
128
|
+
return rows.map(toSummary);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function countSummaries(userId: string): { conv: number; month: number } {
|
|
132
|
+
const rows = sql("SELECT kind, count(*) AS n FROM memory_summaries WHERE user_id = ? GROUP BY kind").all(userId) as { kind: string; n: number }[];
|
|
133
|
+
return { conv: rows.find((r) => r.kind === "conv")?.n ?? 0, month: rows.find((r) => r.kind === "month")?.n ?? 0 };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function searchSummaries(
|
|
137
|
+
userId: string,
|
|
138
|
+
query: string,
|
|
139
|
+
opts: { after?: number; before?: number; limit?: number; skip?: (s: MemorySummary) => boolean } = {},
|
|
140
|
+
): MemorySummary[] {
|
|
141
|
+
const match = ftsMatch(query);
|
|
142
|
+
if (!match) return [];
|
|
143
|
+
const where: string[] = [];
|
|
144
|
+
const args: number[] = [];
|
|
145
|
+
if (opts.after !== undefined) {
|
|
146
|
+
where.push(" AND s.to_ts >= ?");
|
|
147
|
+
args.push(opts.after);
|
|
148
|
+
}
|
|
149
|
+
if (opts.before !== undefined) {
|
|
150
|
+
where.push(" AND s.from_ts < ?");
|
|
151
|
+
args.push(opts.before);
|
|
152
|
+
}
|
|
153
|
+
const rows = sql(
|
|
154
|
+
`SELECT s.id, s.kind, s.scope, s.title, s.text, s.project, s.from_ts, s.to_ts, s.updated_at
|
|
155
|
+
FROM memory_summaries_fts CROSS JOIN memory_summaries s ON s.id = memory_summaries_fts.rowid
|
|
156
|
+
WHERE memory_summaries_fts MATCH ? AND s.user_id = ?${where.join("")} ORDER BY memory_summaries_fts.rank LIMIT 50`,
|
|
157
|
+
).all(match, userId, ...args) as Row[];
|
|
158
|
+
const out = rows.map(toSummary).filter((s) => !opts.skip?.(s));
|
|
159
|
+
return out.slice(0, opts.limit ?? 3);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function removeRow(userId: string, s: MemorySummary): void {
|
|
163
|
+
sql("DELETE FROM memory_summaries_fts WHERE rowid = ?").run(s.id);
|
|
164
|
+
sql("DELETE FROM memory_summaries WHERE id = ? AND user_id = ?").run(s.id, userId);
|
|
165
|
+
if (s.kind !== "conv") return;
|
|
166
|
+
// 这段对话跨过的每个月:月度摘要都可能含有它的内容,删掉,还有别的对话的月份下次重新合成
|
|
167
|
+
const months = sql("SELECT id FROM memory_summaries WHERE user_id = ? AND kind = 'month' AND scope >= ? AND scope <= ?").all(userId, localMonth(s.fromTs), localMonth(s.toTs)) as { id: number }[];
|
|
168
|
+
for (const m of months) {
|
|
169
|
+
sql("DELETE FROM memory_summaries_fts WHERE rowid = ?").run(m.id);
|
|
170
|
+
sql("DELETE FROM memory_summaries WHERE id = ?").run(m.id);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** 删一段对话的摘要(对话被删时) */
|
|
175
|
+
export function deleteConvSummary(userId: string, key: string): void {
|
|
176
|
+
transaction(() => {
|
|
177
|
+
const s = readSummary(userId, "conv", key);
|
|
178
|
+
if (s) removeRow(userId, s);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** 用户在设置里删一条摘要 */
|
|
183
|
+
export function deleteSummary(userId: string, id: number): boolean {
|
|
184
|
+
return transaction(() => {
|
|
185
|
+
const row = sql(`SELECT ${COLUMNS} FROM memory_summaries WHERE id = ? AND user_id = ?`).get(id, userId) as Row | undefined;
|
|
186
|
+
if (!row) return false;
|
|
187
|
+
removeRow(userId, toSummary(row));
|
|
188
|
+
return true;
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function purgeSummaries(userId: string): void {
|
|
193
|
+
transaction(() => {
|
|
194
|
+
sql("DELETE FROM memory_summaries_fts WHERE rowid IN (SELECT id FROM memory_summaries WHERE user_id = ?)").run(userId);
|
|
195
|
+
sql("DELETE FROM memory_summaries WHERE user_id = ?").run(userId);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* 该(重新)合成月度摘要的月份,从早到晚。当前月还没过完不算。
|
|
201
|
+
* 要合成的:这个月有对话摘要,且月度摘要还没有、或者比对话摘要旧。
|
|
202
|
+
*/
|
|
203
|
+
export function monthsToRollup(userId: string, now = Date.now()): string[] {
|
|
204
|
+
const current = localMonth(now);
|
|
205
|
+
const rows = sql("SELECT to_ts, updated_at FROM memory_summaries WHERE user_id = ? AND kind = 'conv'").all(userId) as { to_ts: number; updated_at: number }[];
|
|
206
|
+
const newest = new Map<string, number>();
|
|
207
|
+
for (const r of rows) {
|
|
208
|
+
const month = localMonth(r.to_ts);
|
|
209
|
+
if (month < current) newest.set(month, Math.max(newest.get(month) ?? 0, r.updated_at));
|
|
210
|
+
}
|
|
211
|
+
const byScope = new Map(listSummaries(userId, { kind: "month", limit: 500 }).map((m) => [m.scope, m]));
|
|
212
|
+
return [...newest.entries()]
|
|
213
|
+
.filter(([month, at]) => {
|
|
214
|
+
const m = byScope.get(month);
|
|
215
|
+
return !m || m.updatedAt < at;
|
|
216
|
+
})
|
|
217
|
+
.map(([month]) => month)
|
|
218
|
+
.sort();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** 某个月的全部对话摘要,从早到晚 */
|
|
222
|
+
export function convSummariesOfMonth(userId: string, month: string): MemorySummary[] {
|
|
223
|
+
const rows = sql(`SELECT ${COLUMNS} FROM memory_summaries WHERE user_id = ? AND kind = 'conv' ORDER BY to_ts`).all(userId) as Row[];
|
|
224
|
+
return rows.map(toSummary).filter((s) => localMonth(s.toTs) === month);
|
|
225
|
+
}
|