agent-loft 0.2.90 → 0.2.94

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.
@@ -4,10 +4,10 @@
4
4
  // (手机切回前台、刷新)读的是 agent 的记录,脚注就丢了。所以在这里按会话记下来,
5
5
  // 读历史时再按回复正文的结尾对回去。存在数据库的 footers 表里(见 db.ts),一轮一行。
6
6
  import { sql, transaction } from "./db.js";
7
- import type { HistoryMessage } from "./types.js";
7
+ import type { HistoryMessage, TurnChanges } from "./types.js";
8
8
 
9
- /** t:回复正文(去掉空白后)的结尾;f:脚注 */
10
- type Entry = { t: string; f: string };
9
+ /** t:回复正文(去掉空白后)的结尾;f:脚注;c:这一轮改了什么(JSON) */
10
+ type Entry = { t: string; f: string; c: string | null };
11
11
 
12
12
  const MAX_PER_SESSION = 1000;
13
13
  /** 存下来比对用的正文结尾长度 */
@@ -36,21 +36,36 @@ function endsWithTail(text: string, tail: string): boolean {
36
36
  return squash(text).endsWith(tail);
37
37
  }
38
38
 
39
- export function recordFooter(sessionId: string | undefined, text: string, footer: string): void {
39
+ /** 记一轮的脚注,返回这一行的编号(这一轮改了什么要等快照做完才知道,到时候用 setFooterChanges 补上)。没记就是 undefined */
40
+ export function recordFooter(sessionId: string | undefined, text: string, footer: string, changes?: TurnChanges): number | undefined {
40
41
  const t = squash(text).slice(-TAIL);
41
- if (!sessionId || !t || !footer) return;
42
- transaction(() => {
43
- sql("INSERT INTO footers (session_id, tail, footer, at) VALUES (?, ?, ?, ?)").run(sessionId, t, footer, Date.now());
42
+ if (!sessionId || !t || !footer) return undefined;
43
+ return transaction(() => {
44
+ const row = sql("INSERT INTO footers (session_id, tail, footer, at, changes) VALUES (?, ?, ?, ?, ?)").run(sessionId, t, footer, Date.now(), changes ? JSON.stringify(changes) : null);
44
45
  // 一个会话只留最近这么多轮
45
46
  sql(
46
47
  `DELETE FROM footers WHERE session_id = ? AND id <= (
47
48
  SELECT id FROM footers WHERE session_id = ? ORDER BY id DESC LIMIT 1 OFFSET ?)`,
48
49
  ).run(sessionId, sessionId, MAX_PER_SESSION);
50
+ return Number(row.lastInsertRowid);
49
51
  });
50
52
  }
51
53
 
54
+ export function setFooterChanges(id: number, changes: TurnChanges): void {
55
+ sql("UPDATE footers SET changes = ? WHERE id = ?").run(JSON.stringify(changes), id);
56
+ }
57
+
52
58
  function entriesOf(sessionId: string): Entry[] {
53
- return sql("SELECT tail AS t, footer AS f FROM footers WHERE session_id = ? ORDER BY id").all(sessionId) as Entry[];
59
+ return sql("SELECT tail AS t, footer AS f, changes AS c FROM footers WHERE session_id = ? ORDER BY id").all(sessionId) as Entry[];
60
+ }
61
+
62
+ function changesOf(entry: Entry): TurnChanges | undefined {
63
+ if (!entry.c) return undefined;
64
+ try {
65
+ return JSON.parse(entry.c) as TurnChanges;
66
+ } catch {
67
+ return undefined;
68
+ }
54
69
  }
55
70
 
56
71
  /** 给历史里的助手消息按顺序配上脚注。同样的正文(比如两次都回「好的」)按先后一一对应 */
@@ -64,7 +79,8 @@ export function attachFooters(sessionId: string, messages: HistoryMessage[]): Hi
64
79
  const found = entries.findIndex((e, i) => i >= next && endsWithTail(m.text, e.t));
65
80
  if (found < 0) return m;
66
81
  next = found + 1;
67
- return { ...m, footer: entries[found]!.f };
82
+ const changes = changesOf(entries[found]!);
83
+ return { ...m, footer: entries[found]!.f, ...(changes ? { changes } : {}) };
68
84
  });
69
85
  }
70
86
 
@@ -73,6 +89,6 @@ export function copyFooters(from: string, to: string): void {
73
89
  if (from === to || !sql("SELECT 1 FROM footers WHERE session_id = ? LIMIT 1").get(from)) return;
74
90
  transaction(() => {
75
91
  sql("DELETE FROM footers WHERE session_id = ?").run(to);
76
- sql("INSERT INTO footers (session_id, tail, footer, at) SELECT ?, tail, footer, ? FROM footers WHERE session_id = ? ORDER BY id").run(to, Date.now(), from);
92
+ sql("INSERT INTO footers (session_id, tail, footer, at, changes) SELECT ?, tail, footer, ?, changes FROM footers WHERE session_id = ? ORDER BY id").run(to, Date.now(), from);
77
93
  });
78
94
  }