agent-loft 0.2.125 → 0.2.126

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.
@@ -19,7 +19,7 @@ import {
19
19
  updateMemoryItem,
20
20
  } from "../core/memory.js";
21
21
  import { parseDay } from "../core/memory-dates.js";
22
- import { readConversationRecords, recordSearch, searchHistory } from "../core/chat-history.js";
22
+ import { incompleteExcerpt, readConversationRecords, recordSearch, searchHistory } from "../core/chat-history.js";
23
23
  import { listSummaries, searchSummaries, type MemorySummary } from "../core/memory-summaries.js";
24
24
  import { conversationLabels } from "../core/store.js";
25
25
 
@@ -128,6 +128,7 @@ const TOOLS = [
128
128
  properties: {
129
129
  conversation: { type: "string", description: "对话 id" },
130
130
  around: { type: "number", description: "句子编号(搜索结果里的 #数字)" },
131
+ char_offset: { type: "integer", minimum: 0, description: "从 offset 指定的那一句的第几个字符继续,按上次返回值填写" },
131
132
  offset: { type: "number", description: "从第几句开始,默认 0" },
132
133
  limit: { type: "number", description: "读几句,默认 20,最多 100" },
133
134
  },
@@ -168,8 +169,13 @@ function dateRange(args: Record<string, unknown>): { after?: number; before?: nu
168
169
  return out;
169
170
  }
170
171
 
172
+ function sourceLabel(key: string): string {
173
+ const device = /^sync\/([a-f0-9]{8})[a-f0-9]{24}\//.exec(key);
174
+ return device ? ` · 来源实例 ${device[1]}` : "";
175
+ }
176
+
171
177
  function summaryLine(s: MemorySummary): string {
172
- const label = s.kind === "month" ? `${s.scope} 月度摘要` : `${localDate(s.toTs)} 对话摘要 · 对话 id: ${s.scope}`;
178
+ const label = s.kind === "month" ? `${s.scope.split("/").at(-1)} 月度摘要${sourceLabel(s.scope)}` : `${localDate(s.toTs)} 对话摘要 · 对话 id: ${s.scope}`;
173
179
  return `【${label}】「${s.title}」${s.project ? `(${s.project})` : ""}\n${s.text}`;
174
180
  }
175
181
 
@@ -189,7 +195,7 @@ function search(args: Record<string, unknown>) {
189
195
  const blocks = hits.map((h, i) => {
190
196
  const title = convs.get(h.key)?.title ?? (h.key.startsWith("feishu:") ? "飞书私聊" : h.key.startsWith("weixin:") ? "微信" : "未命名对话");
191
197
  return [
192
- `[${i + 1}] ${localDate(h.ts)} · 「${title}」 · 对话 id: ${h.key} · #${h.rowId}`,
198
+ `[${i + 1}] ${localDate(h.ts)} · 「${title}」${sourceLabel(h.key)} · 对话 id: ${h.key} · #${h.rowId}`,
193
199
  ...(h.user ? [`用户:${h.user}`] : []),
194
200
  ...(h.assistant ? [`助手:${h.assistant}`] : []),
195
201
  ].join("\n");
@@ -213,17 +219,24 @@ function readChat(args: Record<string, unknown>) {
213
219
  ...(args.limit !== undefined ? { limit: Number(args.limit) } : {}),
214
220
  });
215
221
  if (total === 0) return text(`没有 id 为 ${key} 的对话记录。`, true);
216
- // 一次给太多会把上下文撑爆(一句最长 8000 字):放不下的告诉它接着从哪读
222
+ // Paginate within a single long reply as well as between messages.
217
223
  const lines: string[] = [];
218
224
  let size = 0;
219
225
  for (const r of rows) {
220
- const line = `#${r.rowId} ${localDate(r.ts)} ${r.r === "u" ? "用户" : "助手"}:${r.t}`;
226
+ const start = lines.length ? 0 : Math.max(0, Math.floor(Number(args.char_offset) || 0));
227
+ const prefix = `#${r.rowId} ${localDate(r.ts)} ${r.r === "u" ? "用户" : "助手"}${incompleteExcerpt(r.t) ? "(历史正文不完整)" : ""}:`;
228
+ if (!lines.length && r.t.length - start > READ_MAX - prefix.length) {
229
+ let end = start + READ_MAX - prefix.length;
230
+ if (/[\uD800-\uDBFF]/.test(r.t[end-1]!)) end--;
231
+ return text(`${prefix}${r.t.slice(start,end)}\n\n本句未完,接着用 offset: ${offset},char_offset: ${end}。`);
232
+ }
233
+ const line = prefix + r.t.slice(start);
221
234
  if (lines.length > 0 && size + line.length > READ_MAX) break;
222
235
  lines.push(line);
223
236
  size += line.length;
224
237
  }
225
238
  const next = offset + lines.length;
226
- return text(`这段对话一共 ${total} 句,下面是第 ${offset + 1}–${next} 句${next < total ? `(后面还有,要接着看就用 offset: ${next})` : ""}:\n\n${lines.join("\n\n")}`);
239
+ return text(`这段对话一共 ${total} 句${sourceLabel(key)},下面是第 ${offset + 1}–${next} 句${next < total ? `(后面还有,要接着看就用 offset: ${next})` : ""}:\n\n${lines.join("\n\n")}`);
227
240
  }
228
241
 
229
242
  function timeline(args: Record<string, unknown>) {
@@ -0,0 +1,119 @@
1
+ import { fill, h, toast } from "../lib/dom.js";
2
+ import { api } from "../lib/api.js";
3
+ import { relTime } from "../lib/time.js";
4
+
5
+ const path = "/api/account/memory/sync";
6
+
7
+ export async function showMemorySync(body, inviteCode = "") {
8
+ const button = (label, task) => {
9
+ const el = h("button", { class: "btn small" }, label);
10
+ el.onclick = async () => {
11
+ el.disabled = true;
12
+ try {
13
+ await task();
14
+ } catch (err) {
15
+ toast(err.message);
16
+ } finally {
17
+ el.disabled = false;
18
+ }
19
+ };
20
+ return el;
21
+ };
22
+ const post = (action, data = {}) => api(path + "/" + action, { method: "POST", body: data });
23
+ try {
24
+ const state = await api(path);
25
+ const parts = [];
26
+ if (state.mode !== "off") {
27
+ parts.push(
28
+ h("div", {}, state.url ? `中转地址 ${state.url}` : "本实例作为中转"),
29
+ h(
30
+ "div",
31
+ { class: "muted" },
32
+ `最近同步 ${state.lastSync ? relTime(state.lastSync) : "尚未完成"} · 待发送 ${state.pending} · 冲突 ${state.conflicts.length}`,
33
+ ),
34
+ state.error ? h("div", { class: "error-text" }, state.error) : null,
35
+ button("立即同步", async () => {
36
+ try {
37
+ await post("now");
38
+ } finally {
39
+ await showMemorySync(body);
40
+ }
41
+ }),
42
+ );
43
+ }
44
+ if (state.mode !== "client") {
45
+ const code = h("input", { readOnly: true, hidden: !inviteCode, value: inviteCode, "aria-label": "一次性配对码" });
46
+ parts.push(
47
+ h(
48
+ "div",
49
+ { class: "row" },
50
+ button("生成配对码", async () => {
51
+ const invite = await post("invite");
52
+ await showMemorySync(body, invite.code);
53
+ }),
54
+ code,
55
+ ),
56
+ h(
57
+ "div",
58
+ { class: "muted" },
59
+ "在另一台自己的实例上输入中转地址和配对码,10 分钟内有效。配对后双方将交换该账号已有和新增的记忆、摘要及问答正文。",
60
+ ),
61
+ );
62
+ }
63
+ if (state.mode === "off" || (state.mode === "client" && state.error)) {
64
+ const url = h("input", { type: "url", placeholder: "https://…", value: state.url ?? "", "aria-label": "中转地址" });
65
+ const code = h("input", { placeholder: "一次性配对码", autocomplete: "off", "aria-label": "中转实例配对码" });
66
+ parts.push(
67
+ h(
68
+ "div",
69
+ { class: "row" },
70
+ url,
71
+ code,
72
+ button("连接中转", async () => {
73
+ await post("join", { url: url.value, code: code.value });
74
+ await showMemorySync(body);
75
+ }),
76
+ ),
77
+ );
78
+ }
79
+ for (const peer of state.peers) {
80
+ parts.push(
81
+ h(
82
+ "div",
83
+ { class: "row" },
84
+ h("span", {}, `实例 ${peer.device.slice(0, 8)} · ${peer.last_sync ? relTime(peer.last_sync) : "等待首次同步"}`),
85
+ button("撤销凭据", async () => {
86
+ await post("revoke", { device: peer.device });
87
+ await showMemorySync(body);
88
+ }),
89
+ ),
90
+ );
91
+ }
92
+ for (const conflict of state.conflicts) {
93
+ const draft = h("textarea", { "aria-label": "合并后的正文" });
94
+ draft.value = conflict.text;
95
+ parts.push(
96
+ h("h4", {}, conflict.kind === "about" ? "「关于我」有并发修改" : "记忆有并发修改"),
97
+ ...conflict.choices.map((choice) =>
98
+ h(
99
+ "div",
100
+ { class: "row" },
101
+ h("span", {}, choice),
102
+ button("选用", () => {
103
+ draft.value = choice;
104
+ }),
105
+ ),
106
+ ),
107
+ draft,
108
+ button("保存合并结果", async () => {
109
+ await post("resolve", { gid: conflict.gid, choices: conflict.choices, text: draft.value });
110
+ await showMemorySync(body);
111
+ }),
112
+ );
113
+ }
114
+ fill(body, ...parts);
115
+ if (inviteCode) body.querySelector('input[aria-label="一次性配对码"]')?.select();
116
+ } catch (err) {
117
+ fill(body, h("div", { class: "error-text" }, err.message));
118
+ }
119
+ }
@@ -1,3 +1,4 @@
1
+ import { showMemorySync } from "./memory-sync.js";
1
2
  import { fill, h, toast } from "../lib/dom.js";
2
3
  import { api } from "../lib/api.js";
3
4
  import { relTime } from "../lib/time.js";
@@ -435,6 +436,7 @@ export async function memoryTab(panel, refresh = false) {
435
436
  ),
436
437
  otherItemsSection("归档", "archive", mem.counts?.archive ?? 0, "过时了的事。不放进系统提示,agent 问到以前的情况时能搜到。", reload, run),
437
438
  otherItemsSection("回收站", "trash", mem.counts?.trash ?? 0, "删掉的放在这里 30 天,之后彻底删除。", reload, run),
439
+ lazySection("sync", "跨实例同步", showMemorySync),
438
440
  logSection(reload),
439
441
  summariesSection(mem),
440
442
  dataSection(mem, reload),