agent-loft 0.2.59 → 0.2.61

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.
@@ -1,19 +1,17 @@
1
1
  // 记忆:agent 对「这个人」的长期了解,像 ChatGPT 的记忆那样。
2
2
  //
3
- // 每个用户一份,存在 data/memory/<用户 id>.json:
3
+ // 每个用户一份,存在数据库的 memory 表里(见 db.ts),一人一行:
4
4
  // - about:用户自己写的「关于我」;
5
5
  // - items:一条条记住的事,用户在设置里加的,或者 agent 在对话里用 remember 工具记下的。
6
6
  // 开会话时拼成一段文字放进系统提示(见 memoryPrompt),Claude / Codex、Web / 飞书共用同一份。
7
7
  //
8
8
  // 归属跟着对话的主人走(见 memoryOwnerOf):别人的对话里绝不注入你的记忆,也不往你的记忆里写。
9
- // 记忆隔离和多用户一样是「信任型」的:agent 都以同一个系统用户运行,工作模式下能读到这些文件。
9
+ // 记忆隔离和多用户一样是「信任型」的:agent 都以同一个系统用户运行,工作模式下能读到数据库文件。
10
10
  //
11
- // 不做内存缓存,每次都读文件:agent 的记忆工具跑在单独的进程里(见 mcp/memory-server.ts),
12
- // 两边都会写,读文件最简单也不会读到过期的内容。文件很小。
11
+ // 不做内存缓存,每次都查库:agent 的记忆工具跑在单独的进程里(见 mcp/memory-server.ts),
12
+ // 两边都会写。读—改—写放在一个事务里,两边同时改也不会互相冲掉。
13
13
  import crypto from "node:crypto";
14
- import fs from "node:fs";
15
- import path from "node:path";
16
- import { dataDir, readJson, writeJsonAtomic } from "../home.js";
14
+ import { MEMORY_DEFAULT_OFF, sql, transaction } from "./db.js";
17
15
 
18
16
  export type MemoryItem = {
19
17
  id: string;
@@ -94,12 +92,10 @@ export const MEMORY_LIMITS = {
94
92
  promptItems: 4000,
95
93
  };
96
94
 
97
- const memoryDir = path.join(dataDir, "memory");
98
-
99
- /** 用户 id 只会是 users.ts 生成的那种;校验一下,免得拼出目录外的路径 */
100
- function fileOf(userId: string): string {
95
+ /** 用户 id 只会是 users.ts 生成的那种 */
96
+ function checkUser(userId: string): string {
101
97
  if (!/^[A-Za-z0-9_]{1,64}$/.test(userId)) throw new Error("用户 id 不合法");
102
- return path.join(memoryDir, `${userId}.json`);
98
+ return userId;
103
99
  }
104
100
 
105
101
  function empty(): UserMemory {
@@ -107,30 +103,37 @@ function empty(): UserMemory {
107
103
  }
108
104
 
109
105
  /**
110
- * 记忆从默认开改成默认关(0.2.46):升级前就有、但还没写过记忆文件的用户,补一份开着的,
106
+ * 记忆从默认开改成默认关(0.2.46):升级前就有、但还没存过记忆的用户,补一份开着的,
111
107
  * 让老实例里的人保持原样。只跑一次,跑过留个标记;新装的第一次启动时还没有用户,什么都不补。
112
108
  */
113
109
  export function migrateMemoryDefault(userIds: string[]): void {
114
- const marker = path.join(memoryDir, ".default-off-migrated");
115
- if (fs.existsSync(marker)) return;
116
- fs.mkdirSync(memoryDir, { recursive: true, mode: 0o700 });
117
- let n = 0;
118
- for (const id of userIds) {
119
- if (fs.existsSync(fileOf(id))) continue;
120
- write(id, { ...empty(), enabled: true });
121
- n++;
122
- }
123
- fs.writeFileSync(marker, String(Date.now()), { mode: 0o600 });
110
+ const n = transaction(() => {
111
+ if (sql("SELECT 1 FROM migrations WHERE name = ?").get(MEMORY_DEFAULT_OFF)) return 0;
112
+ let added = 0;
113
+ for (const id of userIds) {
114
+ if (sql("SELECT 1 FROM memory WHERE user_id = ?").get(checkUser(id))) continue;
115
+ write(id, { ...empty(), enabled: true });
116
+ added++;
117
+ }
118
+ sql("INSERT INTO migrations (name, at) VALUES (?, ?)").run(MEMORY_DEFAULT_OFF, Date.now());
119
+ return added;
120
+ });
124
121
  if (n) console.log(`[记忆] 默认改成关闭:升级前的 ${n} 个用户保持开启`);
125
122
  }
126
123
 
127
124
  export function readMemory(userId: string): UserMemory {
128
- const raw = readJson<Partial<UserMemory> & { autoExtract?: boolean }>(fileOf(userId), {});
125
+ const row = sql("SELECT data FROM memory WHERE user_id = ?").get(checkUser(userId)) as { data: string } | undefined;
126
+ let raw: Partial<UserMemory> & { autoExtract?: boolean } = {};
127
+ try {
128
+ raw = row ? JSON.parse(row.data) : {};
129
+ } catch {
130
+ // 坏了就当没有
131
+ }
129
132
  const items = Array.isArray(raw.items)
130
133
  ? raw.items.filter((i): i is MemoryItem => !!i && typeof i.id === "string" && typeof i.text === "string")
131
134
  : [];
132
135
  return {
133
- // 默认关:后台整理会占订阅额度,夜里还会单独开一个 5 小时窗口。写过的文件都存着 enabled,老用户不受影响
136
+ // 默认关:后台整理会占订阅额度,夜里还会单独开一个 5 小时窗口。存过的都带着 enabled,老用户不受影响
134
137
  enabled: raw.enabled === true,
135
138
  // 早先的版本只有一个「自动整理」开关:关着的就是不整理
136
139
  curate: cleanCurate(raw.curate, raw.autoExtract === false ? { ...DEFAULT_CURATE, when: "off" } : DEFAULT_CURATE),
@@ -147,44 +150,15 @@ export function readMemory(userId: string): UserMemory {
147
150
  }
148
151
 
149
152
  function write(userId: string, memory: UserMemory): void {
150
- fs.mkdirSync(memoryDir, { recursive: true, mode: 0o700 });
151
- writeJsonAtomic(fileOf(userId), memory);
153
+ sql("INSERT OR REPLACE INTO memory (user_id, data) VALUES (?, ?)").run(checkUser(userId), JSON.stringify(memory));
152
154
  }
153
155
 
154
- const sleepSync = (ms: number) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
155
-
156
156
  /**
157
- * 读—改—写之间加一把跨进程的锁:主进程(设置页、自动整理)和每个会话的记忆工具子进程都会写同一个文件,
158
- * 不锁的话两边同时改,后写的那个会把先写的冲掉(新记的一条、刚保存的「关于我」悄悄没了)。
159
- * 用 O_EXCL 建锁文件;持锁的进程被杀了留下的锁,5 秒后当作废。改动都是毫秒级的,等不到 3 秒就报错。
157
+ * 读—改—写放进一个事务:主进程(设置页、自动整理)和每个会话的记忆工具子进程都会写同一个人的记忆,
158
+ * 不这样的话两边同时改,后写的那个会把先写的冲掉(新记的一条、刚保存的「关于我」悄悄没了)。
160
159
  */
161
160
  function locked<T>(userId: string, fn: (memory: UserMemory) => T): T {
162
- const lock = `${fileOf(userId)}.lock`;
163
- fs.mkdirSync(memoryDir, { recursive: true, mode: 0o700 });
164
- const deadline = Date.now() + 3000;
165
- for (;;) {
166
- try {
167
- fs.writeFileSync(lock, String(process.pid), { flag: "wx", mode: 0o600 });
168
- break;
169
- } catch (err) {
170
- if ((err as NodeJS.ErrnoException).code !== "EEXIST") throw err;
171
- try {
172
- if (Date.now() - fs.statSync(lock).mtimeMs > 5000) {
173
- fs.rmSync(lock, { force: true });
174
- continue;
175
- }
176
- } catch {
177
- continue; // 刚好被释放了
178
- }
179
- if (Date.now() > deadline) throw new Error("记忆文件正被别处修改,稍后再试");
180
- sleepSync(15);
181
- }
182
- }
183
- try {
184
- return fn(readMemory(userId));
185
- } finally {
186
- fs.rmSync(lock, { force: true });
187
- }
161
+ return transaction(() => fn(readMemory(userId)));
188
162
  }
189
163
 
190
164
  function cleanText(text: unknown, max: number, what: string): string {
@@ -283,9 +257,9 @@ export function clearMemory(userId: string): void {
283
257
  locked(userId, (memory) => write(userId, { ...memory, about: "", items: [] }));
284
258
  }
285
259
 
286
- /** 删用户时:整个文件删掉 */
260
+ /** 删用户时:整个删掉 */
287
261
  export function deleteMemory(userId: string): void {
288
- fs.rmSync(fileOf(userId), { force: true });
262
+ sql("DELETE FROM memory WHERE user_id = ?").run(checkUser(userId));
289
263
  }
290
264
 
291
265
  // ---------- 注入系统提示 ----------
package/src/core/store.ts CHANGED
@@ -4,8 +4,8 @@
4
4
  // 的一条对话。它记着用哪个专家(core/experts.ts)和它的引擎(agent)、在哪个目录,以及 agent 的会话 id
5
5
  // 和模型。专家定了就不换:飞书里换专家等于开新会话(见 hub.setExpert)。
6
6
  import path from "node:path";
7
- import { dataDir, readJson, writeJsonAtomic } from "../home.js";
8
7
  import { config } from "./config.js";
8
+ import { sql, transaction } from "./db.js";
9
9
 
10
10
  export type AgentBinding = {
11
11
  /** agent 侧的会话 id(Claude 的 sessionId / Codex 的 threadId) */
@@ -61,13 +61,24 @@ export type Conversation = {
61
61
 
62
62
  type StoreShape = Record<string, Conversation>;
63
63
 
64
- const storePath = path.join(dataDir, "conversations.json");
64
+ // 存在数据库的 conversations 表里(见 db.ts),一条对话一行。读进内存后都改内存里的对象,
65
+ // 落盘时和上次写下去的比,只写变了的、删掉没了的。
65
66
  let cache: StoreShape | null = null;
67
+ /** 每条对话上次写进数据库的内容 */
68
+ const written = new Map<string, string>();
66
69
  let persistTimer: NodeJS.Timeout | null = null;
67
70
 
68
71
  function load(): StoreShape {
69
72
  if (cache) return cache;
70
- cache = readJson<StoreShape>(storePath, {});
73
+ cache = {};
74
+ for (const row of sql("SELECT key, data FROM conversations").all() as { key: string; data: string }[]) {
75
+ try {
76
+ cache[row.key] = JSON.parse(row.data) as Conversation;
77
+ written.set(row.key, row.data);
78
+ } catch {
79
+ console.warn(`[store] 会话 ${row.key} 的数据坏了,跳过`);
80
+ }
81
+ }
71
82
  // 聊天模式取消了(换成了专家):以前聊天模式的对话直接丢掉,不迁移
72
83
  let dropped = 0;
73
84
  for (const [key, conv] of Object.entries(cache)) {
@@ -79,11 +90,28 @@ function load(): StoreShape {
79
90
  }
80
91
  if (dropped) {
81
92
  console.log(`[store] 聊天模式已取消,丢掉了 ${dropped} 条聊天模式的对话`);
82
- writeJsonAtomic(storePath, cache);
93
+ writeChanges();
83
94
  }
84
95
  return cache;
85
96
  }
86
97
 
98
+ function writeChanges(): void {
99
+ const all = cache ?? {};
100
+ const changed: [string, string][] = [];
101
+ for (const [key, conv] of Object.entries(all)) {
102
+ const data = JSON.stringify(conv);
103
+ if (written.get(key) !== data) changed.push([key, data]);
104
+ }
105
+ const removed = [...written.keys()].filter((key) => !(key in all));
106
+ if (changed.length === 0 && removed.length === 0) return;
107
+ transaction(() => {
108
+ for (const [key, data] of changed) sql("INSERT OR REPLACE INTO conversations (key, data) VALUES (?, ?)").run(key, data);
109
+ for (const key of removed) sql("DELETE FROM conversations WHERE key = ?").run(key);
110
+ });
111
+ for (const [key, data] of changed) written.set(key, data);
112
+ for (const key of removed) written.delete(key);
113
+ }
114
+
87
115
  /** 这条对话的专家 id:自定义专家的 id,或者内置专家(就是 agent 本身) */
88
116
  export function expertIdOf(conv: Pick<Conversation, "agent" | "expert">): string {
89
117
  return conv.expert ?? conv.agent;
@@ -94,12 +122,12 @@ function persist(immediate = false): void {
94
122
  if (immediate) {
95
123
  if (persistTimer) clearTimeout(persistTimer);
96
124
  persistTimer = null;
97
- writeJsonAtomic(storePath, cache ?? {});
125
+ writeChanges();
98
126
  return;
99
127
  }
100
128
  persistTimer ??= setTimeout(() => {
101
129
  persistTimer = null;
102
- writeJsonAtomic(storePath, cache ?? {});
130
+ writeChanges();
103
131
  }, 500);
104
132
  }
105
133
 
@@ -222,23 +250,23 @@ export function putConversation(conv: Conversation): void {
222
250
  // /sessions、/resume 列的是 agent 自己存的全部历史(磁盘上谁开的都有),多用户时要按人过滤:
223
251
  // 记下每个 agent 会话 id 是哪个用户的对话里开出来的。没记过的(飞书、终端里开的、升级前的)归管理员。
224
252
 
225
- const sessionOwnersPath = path.join(dataDir, "session-owners.json");
226
- let sessionOwners: Record<string, string> | null = null;
227
-
228
- function owners(): Record<string, string> {
229
- sessionOwners ??= readJson<Record<string, string>>(sessionOwnersPath, {});
230
- return sessionOwners;
231
- }
232
-
233
253
  /** 第一次见到这个会话 id 时记下主人;已经有主了不改 */
234
254
  export function recordSessionOwner(sessionId: string | undefined, userId: string | undefined): void {
235
255
  if (!sessionId || !userId) return;
236
- const all = owners();
237
- if (all[sessionId]) return;
238
- all[sessionId] = userId;
239
- writeJsonAtomic(sessionOwnersPath, all);
256
+ sql("INSERT OR IGNORE INTO session_owners (session_id, user_id) VALUES (?, ?)").run(sessionId, userId);
240
257
  }
241
258
 
242
259
  export function sessionOwner(sessionId: string): string | undefined {
243
- return owners()[sessionId];
260
+ const row = sql("SELECT user_id FROM session_owners WHERE session_id = ?").get(sessionId) as { user_id: string } | undefined;
261
+ return row?.user_id;
262
+ }
263
+
264
+ /** 别的进程(记忆工具)要对话的标题和是否隐藏:直接查库,不走缓存,Web 上刚删的立刻生效 */
265
+ export function conversationLabels(): Map<string, { title?: string; hidden?: boolean }> {
266
+ const rows = sql("SELECT key, json_extract(data, '$.title') AS title, json_extract(data, '$.hidden') AS hidden FROM conversations").all() as {
267
+ key: string;
268
+ title: string | null;
269
+ hidden: number | null;
270
+ }[];
271
+ return new Map(rows.map((r) => [r.key, { ...(r.title ? { title: r.title } : {}), ...(r.hidden ? { hidden: true } : {}) }]));
244
272
  }
@@ -1,4 +1,5 @@
1
- // 附件落盘:每条会话一个上传目录,开新会话就整个删掉。
1
+ // 附件落盘:每条会话一个上传目录。发出去的附件收进资料库(见 library.ts),开新会话也不删;
2
+ // 对话没了、或者上传了一直没发的,启动时清掉(library.ts 的 tidyLibrary)。
2
3
  //
3
4
  // 放在家目录下而不是项目目录里,免得弄脏用户的仓库。agent 需要能直接读这些文件:
4
5
  // Claude 通过 additionalDirectories 放行,Codex 的沙箱本来就允许读。
@@ -19,50 +20,6 @@ export function ensureUploadsDir(key: string): string {
19
20
  return dir;
20
21
  }
21
22
 
22
- /**
23
- * keep:要保留的文件(绝对路径)。
24
- * olderThan:只删修改时间早于它的(毫秒时间戳)—— 切换会话时用,保住切换途中刚收到的附件。
25
- */
26
- export function clearUploads(key: string, keep: string[] = [], olderThan?: number): void {
27
- try {
28
- const dir = uploadsDirFor(key);
29
- if (keep.length === 0 && olderThan === undefined) {
30
- fs.rmSync(dir, { recursive: true, force: true });
31
- return;
32
- }
33
- const kept = new Set(keep.map((p) => path.resolve(p)));
34
- for (const entry of fs.readdirSync(dir)) {
35
- const full = path.join(dir, entry);
36
- if (kept.has(full)) continue;
37
- if (olderThan !== undefined && fs.statSync(full).mtimeMs >= olderThan) continue;
38
- fs.rmSync(full, { recursive: true, force: true });
39
- }
40
- } catch (err) {
41
- if ((err as NodeJS.ErrnoException).code === "ENOENT") return;
42
- console.warn(`[附件] 清理 ${key} 的上传目录失败:`, err);
43
- }
44
- }
45
-
46
- /** 启动时清理:会话没了、或闲置过期的,目录一并删掉。 */
47
- export function sweepUploads(isAlive: (key: string) => boolean): void {
48
- let entries: string[];
49
- try {
50
- entries = fs.readdirSync(uploadsRoot);
51
- } catch {
52
- return;
53
- }
54
- for (const entry of entries) {
55
- let key: string;
56
- try {
57
- key = decodeURIComponent(entry);
58
- } catch {
59
- continue;
60
- }
61
- if (isAlive(key)) continue;
62
- fs.rmSync(path.join(uploadsRoot, entry), { recursive: true, force: true });
63
- }
64
- }
65
-
66
23
  /** 去掉路径分隔符和控制字符,防止文件名把文件写到目录外面去。 */
67
24
  export function safeName(name: string): string {
68
25
  const cleaned = path
@@ -96,7 +53,7 @@ export function uniquePath(dir: string, name: string): string {
96
53
  return candidate;
97
54
  }
98
55
 
99
- function kindOf(mediaType: string | undefined): AttachmentKind {
56
+ export function kindOf(mediaType: string | undefined): AttachmentKind {
100
57
  if (mediaType?.startsWith("image/")) return "image";
101
58
  if (mediaType?.startsWith("audio/")) return "audio";
102
59
  if (mediaType?.startsWith("video/")) return "video";
package/src/core/usage.ts CHANGED
@@ -5,52 +5,35 @@
5
5
  // 按 agent 会话算,不按对话:/new、恢复旧会话、切 agent 之后显示的都是现在这个会话的。
6
6
  // Codex 升级前跑过的轮次没有记录,从升级后开始算。
7
7
  //
8
- // 另外按天记一本账(usage-daily.json,设置 →「用量」按日 / 月 / 年统计):每次报上来的增量记到当天、
8
+ // 另外按天记一本账(设置 →「用量」按日 / 月 / 年统计):每次报上来的增量记到当天、
9
9
  // 按「谁的对话 + 哪个 agent + 什么模型」分开。只从有这本账开始算,之前的用量没有日期,不补。
10
10
  // 没报费用的(Codex)查的时候按模型单价估一个(见 pricing.ts)。
11
- import path from "node:path";
12
- import { dataDir, readJson, writeJsonAtomic } from "../home.js";
11
+ //
12
+ // 两本账都在数据库里(见 db.ts):usage_sessions 一个会话一行,usage_daily 一天一人一 agent 一模型一行。
13
+ import { sql, transaction } from "./db.js";
13
14
  import { estimateCost } from "./pricing.js";
14
15
  import type { TokenUsage } from "./types.js";
15
16
 
16
17
  export type UsageTotals = Required<Omit<TokenUsage, "costUsd">> & { costUsd?: number; at: number };
17
- type Store = Record<string, UsageTotals>;
18
18
 
19
19
  /** 一天里某个人用某个 agent 的合计 */
20
20
  export type DailyTotals = Required<Omit<TokenUsage, "costUsd">> & { costUsd?: number };
21
- /** 按天的账:日期(服务器本地时间 YYYY-MM-DD)→「owner\tagent\tmodel」→ 合计。没有主人、不知道模型的是空串 */
22
- type Ledger = Record<string, Record<string, DailyTotals>>;
23
21
  /** 查询结果的一行。estimated:costUsd 是按单价估的,不是 agent 报的 */
24
- export type DailyRow = DailyTotals & { day: string; owner?: string; agent: string; model?: string; estimated?: true };
25
-
26
- const FILE = () => path.join(dataDir, "usage.json");
27
- const DAILY_FILE = () => path.join(dataDir, "usage-daily.json");
28
- const MAX_SESSIONS = 1000;
29
- /** 按天的账留多久(天) */
30
- const MAX_DAYS = 3 * 366;
31
-
32
- let store: Store | null = null;
33
- let ledger: Ledger | null = null;
34
- let timer: NodeJS.Timeout | null = null;
22
+ export type DailyRow = DailyTotals & { day: string; owner?: string; agent: string; model?: string; estimated?: true; source?: "desktop" | "cli" | "other" };
35
23
 
36
- function load(): Store {
37
- return (store ??= readJson<Store>(FILE(), {}));
38
- }
24
+ type TotalsRow = { input: number; cache_read: number; cache_write: number; output: number; reasoning: number; cost_usd: number | null };
39
25
 
40
- function loadLedger(): Ledger {
41
- return (ledger ??= readJson<Ledger>(DAILY_FILE(), {}));
42
- }
26
+ const TOTALS = "input, cache_read, cache_write, output, reasoning, cost_usd";
43
27
 
44
- function write(): void {
45
- const s = load();
46
- // 只留最近用过的会话
47
- const keys = Object.keys(s).sort((a, b) => s[b]!.at - s[a]!.at);
48
- for (const k of keys.slice(MAX_SESSIONS)) delete s[k];
49
- writeJsonAtomic(FILE(), s);
50
- const l = loadLedger();
51
- const days = Object.keys(l).sort();
52
- for (const d of days.slice(0, Math.max(0, days.length - MAX_DAYS))) delete l[d];
53
- writeJsonAtomic(DAILY_FILE(), l);
28
+ function fromRow<T extends TotalsRow>(r: T): DailyTotals {
29
+ return {
30
+ input: r.input,
31
+ cacheRead: r.cache_read,
32
+ cacheWrite: r.cache_write,
33
+ output: r.output,
34
+ reasoning: r.reasoning,
35
+ ...(r.cost_usd ? { costUsd: r.cost_usd } : {}),
36
+ };
54
37
  }
55
38
 
56
39
  /** 本地时间的 YYYY-MM-DD */
@@ -59,23 +42,6 @@ export function dayOf(ms: number): string {
59
42
  return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
60
43
  }
61
44
 
62
- function persistSoon(): void {
63
- if (timer) return;
64
- timer = setTimeout(() => {
65
- timer = null;
66
- write();
67
- }, 1000);
68
- timer.unref();
69
- }
70
-
71
- /** 退出前把还没写的落盘 */
72
- export function flushUsage(): void {
73
- if (!timer) return;
74
- clearTimeout(timer);
75
- timer = null;
76
- write();
77
- }
78
-
79
45
  const num = (v: unknown): number => (typeof v === "number" && Number.isFinite(v) && v > 0 ? v : 0);
80
46
 
81
47
  const FIELDS = ["input", "cacheRead", "cacheWrite", "output", "reasoning"] as const;
@@ -92,8 +58,26 @@ export function recordUsage(
92
58
  by?: { owner?: string; agent: string; model?: string },
93
59
  ): void {
94
60
  if (!sessionId) return;
95
- const s = load();
96
- const t = (s[sessionId] ??= { input: 0, cacheRead: 0, cacheWrite: 0, output: 0, reasoning: 0, at: 0 });
61
+ transaction(() => {
62
+ const t = usageOf(sessionId) ?? { input: 0, cacheRead: 0, cacheWrite: 0, output: 0, reasoning: 0, at: 0 };
63
+ const delta = accumulate(t, usage, cumulative);
64
+ t.at = Date.now();
65
+ sql(`INSERT OR REPLACE INTO usage_sessions (session_id, ${TOTALS}, at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`).run(
66
+ sessionId,
67
+ t.input,
68
+ t.cacheRead,
69
+ t.cacheWrite,
70
+ t.output,
71
+ t.reasoning,
72
+ t.costUsd ?? null,
73
+ t.at,
74
+ );
75
+ if (by) addDaily(t.at, by, delta);
76
+ });
77
+ }
78
+
79
+ /** 把这次报上来的加进会话的总数(就地改 t),返回比原来多了多少 */
80
+ function accumulate(t: UsageTotals, usage: TokenUsage, cumulative: boolean): DailyTotals {
97
81
  const delta: DailyTotals = { input: 0, cacheRead: 0, cacheWrite: 0, output: 0, reasoning: 0 };
98
82
  for (const f of FIELDS) {
99
83
  const next = cumulative ? Math.max(t[f], num(usage[f])) : t[f] + num(usage[f]);
@@ -106,39 +90,45 @@ export function recordUsage(
106
90
  if (next > (t.costUsd ?? 0)) delta.costUsd = next - (t.costUsd ?? 0);
107
91
  t.costUsd = next;
108
92
  }
109
- t.at = Date.now();
110
- if (by) addDaily(t.at, by, delta);
111
- persistSoon();
93
+ return delta;
112
94
  }
113
95
 
114
96
  function addDaily(at: number, by: { owner?: string; agent: string; model?: string }, delta: DailyTotals): void {
115
97
  if (!FIELDS.some((f) => delta[f] > 0) && !delta.costUsd) return;
116
- const day = (loadLedger()[dayOf(at)] ??= {});
117
- const d = (day[`${by.owner ?? ""}\t${by.agent}\t${by.model ?? ""}`] ??= { input: 0, cacheRead: 0, cacheWrite: 0, output: 0, reasoning: 0 });
118
- for (const f of FIELDS) d[f] += delta[f];
119
- if (delta.costUsd) d.costUsd = (d.costUsd ?? 0) + delta.costUsd;
98
+ sql(
99
+ `INSERT INTO usage_daily (day, owner, agent, model, ${TOTALS}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
100
+ ON CONFLICT (day, owner, agent, model) DO UPDATE SET
101
+ input = input + excluded.input, cache_read = cache_read + excluded.cache_read,
102
+ cache_write = cache_write + excluded.cache_write, output = output + excluded.output,
103
+ reasoning = reasoning + excluded.reasoning,
104
+ cost_usd = CASE WHEN excluded.cost_usd IS NULL THEN cost_usd ELSE coalesce(cost_usd, 0) + excluded.cost_usd END`,
105
+ ).run(dayOf(at), by.owner ?? "", by.agent, by.model ?? "", delta.input, delta.cacheRead, delta.cacheWrite, delta.output, delta.reasoning, delta.costUsd ?? null);
120
106
  }
121
107
 
122
108
  /** 按天的账里 from ~ to(含两头,YYYY-MM-DD)的行;visible 按主人过滤 */
123
109
  export function dailyUsage(from: string, to: string, visible?: (owner: string | undefined) => boolean): DailyRow[] {
110
+ const found = sql(`SELECT day, owner, agent, model, ${TOTALS} FROM usage_daily WHERE day >= ? AND day <= ? ORDER BY day`).all(from, to) as (TotalsRow & {
111
+ day: string;
112
+ owner: string;
113
+ agent: string;
114
+ model: string;
115
+ })[];
124
116
  const rows: DailyRow[] = [];
125
- for (const [day, buckets] of Object.entries(loadLedger())) {
126
- if (day < from || day > to) continue;
127
- for (const [k, v] of Object.entries(buckets)) {
128
- const [o = "", agent = "", model = ""] = k.split("\t");
129
- const owner = o || undefined;
130
- if (visible && !visible(owner)) continue;
131
- const row: DailyRow = { day, ...(owner ? { owner } : {}), agent, ...(model ? { model } : {}), ...v };
132
- if (!row.costUsd) {
133
- const est = estimateCost(model, row);
134
- if (est) Object.assign(row, { costUsd: est, estimated: true });
135
- }
136
- rows.push(row);
117
+ for (const r of found) {
118
+ const owner = r.owner || undefined;
119
+ if (visible && !visible(owner)) continue;
120
+ const row: DailyRow = { day: r.day, ...(owner ? { owner } : {}), agent: r.agent, ...(r.model ? { model: r.model } : {}), ...fromRow(r) };
121
+ if (!row.costUsd) {
122
+ const est = estimateCost(r.model, row);
123
+ if (est) Object.assign(row, { costUsd: est, estimated: true });
137
124
  }
125
+ rows.push(row);
138
126
  }
139
- return rows.sort((a, b) => a.day.localeCompare(b.day));
127
+ return rows;
140
128
  }
141
129
 
142
130
  export function usageOf(sessionId: string | undefined): UsageTotals | undefined {
143
- return sessionId ? load()[sessionId] : undefined;
131
+ if (!sessionId) return undefined;
132
+ const r = sql(`SELECT ${TOTALS}, at FROM usage_sessions WHERE session_id = ?`).get(sessionId) as (TotalsRow & { at: number }) | undefined;
133
+ return r ? { ...fromRow(r), at: r.at } : undefined;
144
134
  }
package/src/main.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  // 入口:拉起 agent 驱动、飞书机器人和 Web 服务。
2
2
  import { config, loadConfig, onConfigChange } from "./core/config.js";
3
3
  import { hub } from "./core/hub.js";
4
- import { flushStore, getConversation, isExpired } from "./core/store.js";
5
- import { sweepUploads } from "./core/uploads.js";
4
+ import { flushStore, getConversation } from "./core/store.js";
5
+ import { backfillLibrary, startLibraryCapture, tidyLibrary } from "./core/library.js";
6
6
  import { ClaudeDriver } from "./agents/claude/driver.js";
7
7
  import { CodexDriver } from "./agents/codex/driver.js";
8
8
  import { agentHealth } from "./agents/health.js";
@@ -40,10 +40,11 @@ async function main(): Promise<void> {
40
40
  if (next.agents.codex.enabled && !prev.agents.codex.enabled) void startDriver("codex");
41
41
  });
42
42
 
43
- sweepUploads((key) => {
44
- const conv = getConversation(key);
45
- return !!conv && !isExpired(conv);
46
- });
43
+ // 资料库:升级时把已有的附件收进来(只一次),再清掉对话没了的、上传了一直没发的
44
+ const backfilled = backfillLibrary();
45
+ if (backfilled) console.log(`[资料库] 已收录上传目录里原有的 ${backfilled} 个文件`);
46
+ tidyLibrary();
47
+ startLibraryCapture();
47
48
 
48
49
  // 聊天记录索引和聊完后的记忆整理
49
50
  migrateMemoryDefault(listUsers().map((u) => u.id));
@@ -5,10 +5,9 @@
5
5
  //
6
6
  // 只实现 MCP 里用得到的那一小块:initialize、tools/list、tools/call、ping。
7
7
  // 传输是一行一条 JSON(MCP stdio 的规定),stdout 只能写协议消息,日志一律走 stderr。
8
- import path from "node:path";
9
8
  import { addMemoryEntry, localDate, MEMORY_LIMITS, removeMemoryItem } from "../core/memory.js";
10
9
  import { searchHistory } from "../core/chat-history.js";
11
- import { dataDir, readJson } from "../home.js";
10
+ import { conversationLabels } from "../core/store.js";
12
11
 
13
12
  const userId = process.env.AGENT_LOFT_MEMORY_USER ?? "";
14
13
  const via = process.env.AGENT_LOFT_MEMORY_VIA ?? "";
@@ -60,20 +59,18 @@ const TOOLS = [
60
59
  },
61
60
  ].filter((t) => t.name !== "search_past_chats" || searchEnabled);
62
61
 
63
- type ConvMeta = { title?: string; hidden?: boolean; agent?: string };
64
-
65
62
  function search(query: string, limit: number) {
66
63
  if (!searchEnabled) return text("没有开启「参考以前的聊天」。", true);
67
64
  if (!query.trim()) return text("要给关键词。", true);
68
65
  // 每次现读:用户在 Web 上删掉的对话(隐藏)立刻搜不到
69
- const convs = readJson<Record<string, ConvMeta>>(path.join(dataDir, "conversations.json"), {});
66
+ const convs = conversationLabels();
70
67
  const hits = searchHistory(userId, query, {
71
68
  limit: Math.max(1, Math.min(15, Math.round(limit) || 6)),
72
- skip: (k) => k === currentConv || !!convs[k]?.hidden,
69
+ skip: (k) => k === currentConv || !!convs.get(k)?.hidden,
73
70
  });
74
71
  if (hits.length === 0) return text(`以前的聊天里没找到「${query}」相关的内容。可以换个说法再搜。`);
75
72
  const blocks = hits.map((h, i) => {
76
- const title = convs[h.key]?.title ?? (h.key.startsWith("feishu:") ? "飞书私聊" : "未命名对话");
73
+ const title = convs.get(h.key)?.title ?? (h.key.startsWith("feishu:") ? "飞书私聊" : "未命名对话");
77
74
  return [
78
75
  `[${i + 1}] ${localDate(h.ts)} · 「${title}」`,
79
76
  ...(h.user ? [`用户:${h.user}`] : []),