agent-loft 0.2.133 → 0.2.135

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.
Files changed (50) hide show
  1. package/README.md +31 -31
  2. package/package.json +1 -1
  3. package/src/agents/dsh/errors.ts +2 -2
  4. package/src/agents/pi/constants.ts +1 -1
  5. package/src/agents/pi/errors.ts +2 -2
  6. package/src/agents/workbuddy/errors.ts +2 -2
  7. package/src/agents/workbuddy/sdk.ts +1 -1
  8. package/src/channels/feishu/bot.ts +3 -3
  9. package/src/channels/feishu/cards.ts +2 -2
  10. package/src/channels/web/api.ts +3 -1
  11. package/src/channels/web/desktop.ts +1 -1
  12. package/src/channels/web/terminal.ts +1 -1
  13. package/src/channels/web/workspace-api.ts +2 -2
  14. package/src/channels/weixin/bot.ts +1 -1
  15. package/src/core/command-catalog.ts +1 -1
  16. package/src/core/commands.ts +1 -1
  17. package/src/core/feishu-links.ts +1 -1
  18. package/src/core/hub.ts +3 -3
  19. package/src/core/memory-curator.ts +1 -1
  20. package/src/core/ui-prefs.ts +10 -4
  21. package/src/mcp/memory-server.ts +1 -1
  22. package/web/css/mobile.css +19 -31
  23. package/web/css/settings.css +66 -37
  24. package/web/lib/overlay.js +15 -2
  25. package/web/lib/push.js +1 -1
  26. package/web/lib/shell.js +1 -1
  27. package/web/settings/agent-defaults.js +3 -16
  28. package/web/settings/agents.js +118 -95
  29. package/web/settings/commands.js +32 -29
  30. package/web/settings/experts.js +14 -12
  31. package/web/settings/feishu.js +6 -6
  32. package/web/settings/general.js +22 -17
  33. package/web/settings/index.js +193 -112
  34. package/web/settings/memory-sync.js +1 -1
  35. package/web/settings/memory.js +63 -73
  36. package/web/settings/notify.js +2 -2
  37. package/web/settings/security.js +14 -6
  38. package/web/settings/servers.js +6 -5
  39. package/web/settings/skills.js +9 -4
  40. package/web/settings/tabs.js +67 -51
  41. package/web/settings/usage.js +3 -3
  42. package/web/settings/users.js +25 -8
  43. package/web/settings/weixin.js +1 -1
  44. package/web/state.js +2 -2
  45. package/web/views/layout.js +2 -2
  46. package/web/views/memo-dialogs.js +1 -1
  47. package/web/views/sidebar-gestures.js +1 -1
  48. package/web/views/sidebar-nav.js +2 -2
  49. package/web/views/tasks.js +1 -1
  50. package/web/views/topbar.js +1 -1
@@ -2,140 +2,221 @@ import { fill, h, icon } from "../lib/dom.js";
2
2
  import { api } from "../lib/api.js";
3
3
  import { contentHash, isAdmin, ui } from "../state.js";
4
4
  import { closeMenu, confirmDialog } from "../lib/overlay.js";
5
- import { closeSide, openSide, renderSidebar } from "../views/sidebar.js";
5
+ import { closeSide, renderSidebar } from "../views/sidebar.js";
6
6
  import { syncDesktopMenu } from "../views/topbar.js";
7
7
  import { WS } from "../lib/ws.js";
8
8
  import { hasDirty, markDirty } from "./common.js";
9
+ import { directory, resolvePage, searchDirectory, sections } from "./tabs.js";
9
10
 
10
- // ---------- 设置 ----------
11
- // 按「谁的、管什么」分组:我的(每个人自己的)、Agent、连接(飞书、远程控制)、管理。
12
- // 普通成员可管理自己的资料和内置 Agent 默认项(在「专家」里);管理员另有运行环境和服务器设置。
11
+ const settingsRoute = /^#(?:assistants|settings)(?:\/(.*))?$/;
13
12
 
14
- const LAST_TAB = "aim.settingsTab";
15
-
16
- /** 以前的标签名:外面(地址、别处的按钮)传进来的旧名字照样能打开对应的页 */
17
- const RENAMED = { 通用: "对话", 授权: "对话", 飞书机器人: "飞书", 我的账号: "账号", 通知: "系统" };
18
- /** 成员没有 Agent 页了:以前记住的 / 外面传进来的「Agent」打开专家页 */
19
- const MEMBER_RENAMED = { Agent: "专家" };
20
-
21
- function warnBeforeUnload(event) {
22
- if (!ui.settings?.isDirty()) return;
23
- event.preventDefault();
24
- event.returnValue = true;
13
+ // 旧入口仍可直接进入专家设置。
14
+ export function openAssistants() {
15
+ return openSettings(undefined, "assistants");
25
16
  }
26
17
 
27
- /** tab:直接打开哪一页(标签名,如「技能」),不给就是上次看的那页 */
28
- export async function openSettings(tab) {
29
- closeSide();
18
+ export async function openSettings(requested, area = "settings") {
30
19
  closeMenu();
31
- if (ui.settings) {
32
- return tab && ui.settings.selectTab(tab);
33
- }
34
- const nav = h("nav", { class: "settings-nav", "aria-label": "设置分类" });
35
- const panel = h("div", { class: "settings-body" }, h("span", { class: "spinner", role: "status", "aria-label": "加载设置" }));
36
- let page = null;
37
- const isDirty = () => page?.isConnected && hasDirty(page);
38
- let confirming = false;
39
- const leaveOk = async () => {
20
+ closeSide();
21
+ if (!requested && area === "assistants") requested = localStorage.getItem("aim.assistantTab") || "experts";
22
+ if (ui.settings) return ui.settings.selectTab(requested);
23
+ const pages = directory.filter(item => !item.admin || isAdmin());
24
+ const mainPages = pages.filter(item => !item.parent);
25
+ const groups = [...new Set(mainPages.map(item => item.group))];
26
+ const previousFocus = document.activeElement;
27
+ const previousHash = contentHash();
28
+ const previousScroll = ui.messages?.scrollTop;
29
+ const shelf = new Map();
30
+ let active, page, pendingConfig, confirming, closing;
31
+ let parentFromPage;
32
+ let lastField;
33
+ let lastUrl = location.href;
34
+ let returnHash = previousHash;
35
+ const isDirty = () => Boolean((page && hasDirty(page)) || document.querySelector(".settings-editor[data-edited], .settings-editor [data-dirty]"));
36
+ async function leaveOk() {
40
37
  if (!isDirty()) return true;
41
38
  if (confirming) return false;
42
39
  confirming = true;
43
40
  try {
44
- return await confirmDialog({ title: "有修改还没保存", message: "现在离开,这一页刚才改的内容不会保存。", confirmText: "不保存", danger: true });
45
- } finally {
46
- confirming = false;
41
+ return await confirmDialog({ title: "有修改还没保存", message: "离开后将丢弃当前表单的修改。", confirmText: "不保存", danger: true });
42
+ } finally { confirming = false; }
43
+ }
44
+ function beforeUnload(event) {
45
+ if (isDirty()) { event.preventDefault(); event.returnValue = true; }
46
+ }
47
+
48
+ const panel = h("div", { class: "settings-body" });
49
+ const search = h("input", { type: "search", "aria-label": "搜索设置和工具", placeholder: "搜索设置和工具", oninput: drawNav });
50
+ const status = h("span", { class: "settings-sr", role: "status" });
51
+ const list = h("div", { class: "settings-links" });
52
+ const nav = h("nav", { class: "settings-nav", "aria-label": "设置目录" }, h("label", { class: "settings-search" }, icon("search"), search), status, list);
53
+ const primary = h("nav", { class: "settings-primary", "aria-label": "设置分类" });
54
+ const secondary = h("nav", { class: "settings-secondary", "aria-label": "组内项目" });
55
+ const toggle = h("button", { class: "icon-btn settings-find", "aria-label": "搜索设置", "aria-expanded": "false", onclick: () => setSearch(!pane.classList.contains("searching")) }, icon("search"));
56
+ const pane = h("main", { class: "settings-view", "aria-label": "设置" },
57
+ h("header", { class: "settings-header" },
58
+ h("button", { class: "icon-btn", "aria-label": "返回原页面", onclick: () => parentFromPage ? select(parentFromPage) : close() }, icon("back")),
59
+ h("h1", {}, "设置"), h("span", { class: "grow" }), toggle),
60
+ primary, secondary, h("div", { class: "settings" }, nav, panel));
61
+ function setSearch(on) {
62
+ pane.classList.toggle("searching", on);
63
+ toggle.setAttribute("aria-expanded", String(on));
64
+ if (on) search.focus();
65
+ else if (nav.contains(document.activeElement)) toggle.focus({ preventScroll: true });
66
+ }
67
+ search.onkeydown = event => {
68
+ if (event.isComposing) return;
69
+ if (event.key === "Escape") {
70
+ search.value = "";
71
+ drawNav();
72
+ setSearch(false);
47
73
  }
74
+ const hits = list.querySelectorAll("button");
75
+ if (event.key === "ArrowDown") { event.preventDefault(); hits[0]?.focus(); }
76
+ if (event.key === "Enter" && hits.length === 1) hits[0].click();
48
77
  };
49
- const previousHash = contentHash();
50
- const previousFocus = document.activeElement;
51
- const scrollTop = ui.messages?.scrollTop;
52
- const close = async () => {
53
- if (!(await leaveOk()) || ui.settings !== settings) return false;
54
- window.removeEventListener("beforeunload", warnBeforeUnload);
78
+ function drawNav() {
79
+ const query = search.value.trim();
80
+ const hits = query ? searchDirectory(pages, query) : mainPages.map(page => ({ page, label: page.label }));
81
+ let group;
82
+ const current = active?.parent || active?.id;
83
+ const nodes = [];
84
+ for (const { page: item, section, label } of hits) {
85
+ if (!query && item.group !== group) nodes.push(h("h2", { class: "settings-group" }, item.group));
86
+ group = item.group;
87
+ nodes.push(h("button", { class: "settings-nav-link", "data-page": item.id, "aria-current": item.id === current && !query ? "page" : null,
88
+ onclick: () => select(section ? item.id + "/" + section : item.id) }, icon(item.icon),
89
+ h("span", {}, label, query ? h("small", {}, item.group + (item.label !== label ? " › " + item.label : "")) : null)));
90
+ }
91
+ fill(list, ...nodes, hits.length ? null : h("p", { class: "muted" }, "没有匹配项,试试其他关键词。"));
92
+ status.textContent = query ? "找到 " + hits.length + " 项" : "";
93
+ }
94
+ function drawTabs() {
95
+ const current = active.parent || active.id;
96
+ fill(primary, groups.map(group => h("button", { "aria-pressed": String(group === active.group), onclick: () => {
97
+ const saved = localStorage.getItem("aim.settingsGroup." + group);
98
+ const items = mainPages.filter(item => item.group === group);
99
+ select((items.find(item => item.id === saved) || items[0]).id);
100
+ } }, group)));
101
+ const items = mainPages.filter(item => item.group === active.group);
102
+ secondary.hidden = items.length < 2;
103
+ fill(secondary, items.map(item => h("button", { "data-page": item.id, "aria-current": item.id === current ? "page" : null, onclick: () => select(item.id) }, item.label)));
104
+ secondary.querySelector('[aria-current]')?.scrollIntoView({ block: "nearest", inline: "nearest" });
105
+ drawNav();
106
+ }
107
+ function reveal(key) {
108
+ const target = key && page.querySelector('[data-section="' + key + '"]');
109
+ if (!target) return false;
110
+ for (let el = target; el; el = el.parentElement) if (el.tagName === "DETAILS") el.open = true;
111
+ if (!target.matches("input,select,button,textarea")) target.tabIndex = -1;
112
+ target.focus({ preventScroll: true });
113
+ target.scrollIntoView({ block: "nearest" });
114
+ return true;
115
+ }
116
+ function route(id, section) {
117
+ returnHash = contentHash();
118
+ history.replaceState({ ...history.state, settingsReturnHash: returnHash }, "", "#settings/" + [id, section].filter(Boolean).join("/"));
119
+ lastUrl = location.href;
120
+ }
121
+ async function select(name) {
122
+ const [id, anchor] = (resolvePage(name || localStorage.getItem("aim.settingsTab"), isAdmin()) || "account").split("/");
123
+ const item = pages.find(p => p.id === id) || mainPages[0];
124
+ const section = sections[item.id]?.find(([key]) => key === anchor)?.[0];
125
+ if (item === active && page && (!section || page.querySelector('[data-section="' + section + '"]'))) {
126
+ setSearch(false);
127
+ route(item.id, section);
128
+ reveal(section);
129
+ return;
130
+ }
131
+ if (!(await leaveOk()) || !pane.isConnected) {
132
+ setSearch(false);
133
+ if (lastField?.isConnected) lastField.focus({ preventScroll: true });
134
+ return;
135
+ }
136
+ if (active) shelf.set(active.id, panel.scrollTop);
137
+ parentFromPage = active?.id === item.parent ? item.parent : null;
138
+ active = item;
139
+ setSearch(false);
140
+ route(item.id, section);
141
+ localStorage.setItem("aim.settingsTab", item.id);
142
+ localStorage.setItem("aim.settingsGroup." + item.group, item.parent || item.id);
143
+ drawTabs();
144
+ const heading = h("h2", { class: "settings-page-title", tabindex: "-1" }, item.label);
145
+ const content = h("div", { class: "settings-page", "aria-busy": "true" }, h("span", { class: "spinner", role: "status", "aria-label": "加载设置" }));
146
+ page = content;
147
+ fill(panel, heading, content);
148
+ heading.focus({ preventScroll: true });
149
+ panel.scrollTop = 0;
150
+ try {
151
+ if (item.config) pendingConfig ??= api("/api/config").catch(err => { pendingConfig = null; throw err; });
152
+ const [module, cfg] = await Promise.all([item.load(), item.config ? pendingConfig : null]);
153
+ if (!content.isConnected) return;
154
+ fill(content);
155
+ if (item.config) await module[item.method](cfg, content, section);
156
+ else await module[item.method](content, section);
157
+ if (content.isConnected && !reveal(section)) panel.scrollTop = shelf.get(item.id) || 0;
158
+ } catch (err) {
159
+ if (content.isConnected) fill(content, h("p", { class: "error-text", role: "alert" }, err.message),
160
+ h("button", { class: "btn", onclick: () => { active = null; select(name); } }, "重试"));
161
+ } finally { content.removeAttribute("aria-busy"); }
162
+ }
163
+
164
+ function finish() {
165
+ window.removeEventListener("beforeunload", beforeUnload);
166
+ window.removeEventListener("popstate", onBack);
55
167
  pane.remove();
56
168
  ui.settings = null;
57
- const returnHash = contentHash();
58
- const { settingsReturnHash, ...state } = history.state ?? {};
59
- history.replaceState(state, "", location.pathname + location.search + returnHash);
60
169
  syncDesktopMenu();
61
170
  renderSidebar();
62
171
  WS.presence();
63
- if (ui.messages && previousHash === returnHash) ui.messages.scrollTop = scrollTop;
172
+ if (previousHash === returnHash && ui.messages) ui.messages.scrollTop = previousScroll;
64
173
  if (previousFocus?.isConnected) previousFocus.focus({ preventScroll: true });
174
+ closing?.(true);
65
175
  return true;
66
- };
67
- const title = h("h1", { tabindex: "-1" }, "设置");
68
- const pane = h("main", { class: "settings-view", "aria-label": "设置" },
69
- h("header", { class: "settings-header" },
70
- h("button", { class: "icon-btn menu-btn", onclick: openSide, title: "会话列表" }, icon("menu")),
71
- title,
72
- ),
73
- h("div", { class: "settings" }, nav, panel),
74
- );
75
- const settings = { close, isDirty, selectTab: (name) => { tab = name; } };
76
- ui.settings = settings;
77
- window.addEventListener("beforeunload", warnBeforeUnload);
78
- ui.layout.append(pane);
79
- history.replaceState({ ...history.state, settingsReturnHash: previousHash }, "", "#settings");
80
- syncDesktopMenu();
81
- renderSidebar();
82
- WS.presence();
83
- title.focus({ preventScroll: true });
84
-
85
- // 带 data-form 的表单里改了东西:标成没保存(common.js 的 saveButton)
86
- const onEdit = (e) => {
87
- if (e.target.closest("[data-nodirty]")) return;
88
- const form = e.target.closest("[data-form]");
89
- if (form && !form.dataset.dirty) markDirty(form);
90
- };
91
- panel.addEventListener("input", onEdit);
92
- panel.addEventListener("change", onEdit);
93
-
94
- let cfg, groups;
95
- try {
96
- const [module, config] = await Promise.all([import("./tabs.js"), isAdmin() ? api("/api/config") : null]);
97
- if (!pane.isConnected) return;
98
- cfg = config;
99
- groups = module.groupsFor(cfg);
100
- } catch (err) {
101
- fill(panel, h("div", { class: "error-text" }, err.message));
102
- return;
103
176
  }
104
- const tabs = groups.flatMap((g) => g.tabs);
105
- const buttons = [];
106
- let current = -1;
107
-
108
- const select = async (i) => {
109
- if (i === current || !(await leaveOk()) || !pane.isConnected) return;
110
- current = i;
111
- buttons.forEach((b, j) => b.classList.toggle("on", i === j));
112
- buttons[i].scrollIntoView({ block: "nearest", inline: "nearest" });
113
- localStorage.setItem(LAST_TAB, tabs[i][0]);
114
- // 每页一个新容器:慢的页还在等接口时切走了,它回来后写进的是已经不在页面上的旧容器,不会串到新页里
115
- const content = h("div", { class: "settings-page" });
116
- page = content;
117
- fill(panel, page);
118
- panel.scrollTop = 0;
119
- try {
120
- await tabs[i][1](content);
121
- } catch (err) {
122
- if (content.isConnected) fill(content, h("div", { class: "error-text" }, err.message));
123
- }
124
- };
125
-
126
- for (const g of groups) {
127
- // 只有一组时不用标题
128
- if (groups.length > 1) nav.append(h("div", { class: "settings-group" }, g.name));
129
- for (const [name] of g.tabs) {
130
- const i = buttons.length;
131
- const b = h("button", { onclick: () => void select(i) }, name);
132
- buttons.push(b);
133
- nav.append(b);
177
+ async function onBack() {
178
+ if (settingsRoute.test(location.hash)) return;
179
+ if (!closing) {
180
+ const overlay = [...document.querySelectorAll("body > .overlay:not(.leaving)")].at(-1);
181
+ if (overlay || parentFromPage || isDirty()) {
182
+ // 浏览器已回到来源页;先恢复当前地址,再处理最上层的编辑/确认。
183
+ history.pushState({ ...history.state, settingsEntry: true, settingsReturnHash: returnHash }, "", lastUrl);
184
+ if (overlay) {
185
+ if (!overlay.classList.contains("dialog-overlay")) overlay.dispatchEvent(new Event("settings-back"));
186
+ } else if (parentFromPage) await select(parentFromPage);
187
+ else await close();
188
+ return;
189
+ }
134
190
  }
191
+ history.replaceState(history.state, "", returnHash || location.pathname + location.search);
192
+ finish();
135
193
  }
136
- settings.selectTab = (requested) => {
137
- const want = (cfg ? undefined : MEMBER_RENAMED[requested]) ?? RENAMED[requested] ?? requested;
138
- return select(Math.max(0, tabs.findIndex(([name]) => name === want)));
139
- };
140
- await settings.selectTab(tab || localStorage.getItem(LAST_TAB));
194
+ async function close() {
195
+ if (closing || !(await leaveOk()) || !pane.isConnected) return false;
196
+ returnHash = contentHash();
197
+ return new Promise(resolve => { closing = resolve; history.back(); });
198
+ }
199
+ ui.settings = { area: "settings", close, selectTab: select, isDirty };
200
+ ui.layout.append(pane);
201
+ if (!history.state?.settingsEntry) {
202
+ const base = { ...history.state };
203
+ delete base.settingsReturnHash;
204
+ history.replaceState(base, "", previousHash || location.pathname + location.search);
205
+ history.pushState({ ...base, settingsEntry: true, settingsReturnHash: previousHash }, "", "#settings");
206
+ }
207
+ window.addEventListener("beforeunload", beforeUnload);
208
+ window.addEventListener("popstate", onBack);
209
+ panel.addEventListener("focusin", e => { if (e.target.matches("input,select,textarea")) lastField = e.target; });
210
+ for (const type of ["input", "change"]) panel.addEventListener(type, e => {
211
+ if (!e.target.closest("[data-nodirty]")) markDirty(e.target.closest("[data-form]"));
212
+ });
213
+ renderSidebar();
214
+ syncDesktopMenu();
215
+ WS.presence();
216
+ await select(requested);
141
217
  }
218
+
219
+ window.addEventListener("popstate", () => {
220
+ const match = settingsRoute.exec(location.hash);
221
+ if (match && !ui.settings && ui.layout) void openSettings(match[1], location.hash.startsWith("#assistants") ? "assistants" : "settings");
222
+ });
@@ -104,7 +104,7 @@ export async function showMemorySync(body, inviteCode = "") {
104
104
  }),
105
105
  ),
106
106
  ),
107
- draft,
107
+ h("div", { "data-form": "" }, draft),
108
108
  button("保存合并结果", async () => {
109
109
  await post("resolve", { gid: conflict.gid, choices: conflict.choices, text: draft.value });
110
110
  await showMemorySync(body);
@@ -1,10 +1,11 @@
1
1
  import { showMemorySync } from "./memory-sync.js";
2
- import { fill, h, toast } from "../lib/dom.js";
2
+ import { fill, h, icon, toast } from "../lib/dom.js";
3
+ import { ui } from "../state.js";
3
4
  import { api } from "../lib/api.js";
4
5
  import { relTime } from "../lib/time.js";
5
6
  import { agentCatalog } from "../views/topbar.js";
6
7
  import { expertChoices } from "../views/experts.js";
7
- import { confirmDialog } from "../lib/overlay.js";
8
+ import { confirmDialog, showMenu } from "../lib/overlay.js";
8
9
  import { field, saveButton } from "./common.js";
9
10
 
10
11
  // ---------- 记忆(每个人自己的) ----------
@@ -26,7 +27,7 @@ const openSections = new Set();
26
27
  /** 一个点开才读的区块 */
27
28
  function lazySection(key, title, load) {
28
29
  const body = h("div", { class: "memory-more-body" });
29
- const details = h("details", { open: openSections.has(key) }, h("summary", { class: "memory-more" }, title), body);
30
+ const details = h("details", { "data-section": key, open: openSections.has(key) }, h("summary", { class: "memory-more" }, title), body);
30
31
  let loaded = false;
31
32
  details.ontoggle = () => {
32
33
  if (details.open) openSections.add(key);
@@ -201,34 +202,10 @@ function dataSection(mem, reload) {
201
202
  }
202
203
  };
203
204
  const stats = mem.searchStats;
204
- const b = mem.backup;
205
- let backupRow = null;
206
- if (b) {
207
- const btn = h("button", { class: "btn small" }, "现在备份");
208
- btn.onclick = async () => {
209
- btn.disabled = true;
210
- btn.textContent = "备份中…";
211
- try {
212
- await api("/api/account/memory/backup", { method: "POST" });
213
- toast("已备份");
214
- await reload();
215
- } catch (err) {
216
- toast(err.message);
217
- btn.disabled = false;
218
- btn.textContent = "现在备份";
219
- }
220
- };
221
- const size = b.last ? `,${(b.last.size / 1048576).toFixed(1)} MB` : "";
222
- backupRow = [
223
- h("div", { class: "row", style: { alignItems: "center", gap: "10px" } }, btn, h("span", { class: "muted" }, b.last ? `上次备份:${relTime(b.last.at)}${size},共 ${b.count} 份` : "还没有备份")),
224
- h("div", { class: "muted" }, `整个数据库每天自动备份一份到 ${b.dir}(留最近 7 天、4 周、12 个月)。这个目录和数据在同一块盘上,防不了硬盘损坏;可以设环境变量 AGENT_LOFT_BACKUP_DIR 放到别的盘。`),
225
- b.error ? h("div", { class: "error-text" }, `上次备份失败(${relTime(b.error.at)}):${b.error.message}`) : null,
226
- ];
227
- }
228
205
  return h(
229
206
  "div",
230
- { class: "section" },
231
- h("h4", {}, "导出和备份"),
207
+ { class: "section", "data-section": "data" },
208
+ h("h4", {}, "个人数据导入导出"),
232
209
  h(
233
210
  "div",
234
211
  { class: "row" },
@@ -238,12 +215,14 @@ function dataSection(mem, reload) {
238
215
  file,
239
216
  ),
240
217
  h("div", { class: "muted" }, "记忆导出成 JSON(条目、摘要和改动记录),可以导入到别的账号或服务器,内容一样的会跳过。聊天记录导出成一行一句的 JSONL。"),
241
- backupRow,
242
218
  stats?.searches ? h("div", { class: "muted" }, `近 30 天 agent 搜了 ${stats.searches} 次聊天记录,${stats.empty} 次没搜到。`) : null,
243
219
  );
244
220
  }
245
221
 
246
- export async function memoryTab(panel, refresh = false) {
222
+ export const memoryTab = panel => loadMemory(panel, false, renderItems);
223
+ export const memoryPrefsTab = panel => loadMemory(panel, false, renderPreferences);
224
+
225
+ async function loadMemory(panel, refresh, render) {
247
226
  const box = h("div", { class: "section" }, h("h4", {}, "记忆"), h("span", { class: "spinner" }));
248
227
  // 更新时保留旧内容,避免加载占位让页面变短、滚动位置被浏览器压回顶部。
249
228
  if (!refresh) fill(panel, box);
@@ -255,7 +234,7 @@ export async function memoryTab(panel, refresh = false) {
255
234
  else fill(box, h("h4", {}, "记忆"), h("div", { class: "error-text" }, err.message));
256
235
  return;
257
236
  }
258
- const reload = () => memoryTab(panel, true);
237
+ const reload = () => loadMemory(panel, true, render);
259
238
  const run = (btn, fn) => async () => {
260
239
  btn.disabled = true;
261
240
  try {
@@ -267,6 +246,10 @@ export async function memoryTab(panel, refresh = false) {
267
246
  }
268
247
  };
269
248
 
249
+ await render(panel, mem, reload, run);
250
+ }
251
+
252
+ async function renderPreferences(panel, mem, reload, run) {
270
253
  const enabled = h("input", { type: "checkbox", checked: mem.enabled });
271
254
  enabled.onchange = async () => {
272
255
  try {
@@ -311,8 +294,44 @@ export async function memoryTab(panel, refresh = false) {
311
294
  const saveAbout = saveButton(async () => {
312
295
  await api("/api/account/memory", { method: "PUT", body: { about: about.value } });
313
296
  toast("已保存,之后的对话会用上");
314
- });
297
+ }, "保存自我介绍");
298
+
299
+ const draft = panel.querySelector('[data-section="about"][data-dirty]');
300
+ fill(
301
+ panel,
302
+ h(
303
+ "div",
304
+ { class: "section" },
305
+ h("h4", {}, "使用记忆", h("span", { class: "grow" }), h("label", { class: "check" }, enabled, "开启")),
306
+ h(
307
+ "div",
308
+ { class: "muted" },
309
+ "像 ChatGPT 的记忆:对话时 agent 会参考你的「关于我」和记住的事,聊天里遇到长期有用的信息也会主动记一条(会在回复里告诉你)。Claude 和 Codex、Web 和飞书共用这一份。",
310
+ ),
311
+ h(
312
+ "div",
313
+ { class: "muted" },
314
+ "用在哪些对话:你在 Web 上的对话;在「账号」里绑定了飞书后,和机器人的私聊也会用。飞书群聊不用。别人(包括管理员)看不到你的记忆,管理员只能清空它。",
315
+ ),
316
+ ),
317
+ h(
318
+ "div",
319
+ { class: "section", "data-section": "automatic" },
320
+ h("h4", {}, "自动整理与聊天历史"),
321
+ curateSection(mem, reload),
322
+ toggle("chatHistory", "参考以前的聊天", "已开启:agent 能搜你以前的聊天了", "已关闭:agent 不再搜以前的聊天"),
323
+ h(
324
+ "div",
325
+ { class: "muted" },
326
+ `agent 遇到「上次说的那个……」时,可以搜你以前的聊天(只有你自己的对话,被删掉的对话不算)。侧栏的「搜索」也用这份聊天记录索引,不开记忆也会记。${history ? `现在收录了 ${history.conversations} 段对话、${history.count} 句话。` : ""}`,
327
+ ),
328
+ h("div", { class: "row" }, clearHistory),
329
+ ),
330
+ draft || h("div", { class: "section", "data-section": "about", "data-form": "" }, h("h4", {}, "关于我"), field("写给 agent 看的自我介绍:你是谁、在做什么、希望它怎么回答", about, `每次对话都会带上,最多 ${mem.limits.about} 字`), h("div", { class: "row" }, saveAbout)),
331
+ );
332
+ }
315
333
 
334
+ function renderItems(panel, mem, reload, run) {
316
335
  const newItem = h("input", { class: "input", maxlength: mem.limits.item, style: { flex: "1", minWidth: "0" }, placeholder: "手动加一条,比如:我的软路由是 iStoreOS,地址 192.168.8.2" });
317
336
  const addBtn = h("button", { class: "btn" }, "添加");
318
337
  addBtn.onclick = run(addBtn, async () => {
@@ -338,13 +357,15 @@ export async function memoryTab(panel, refresh = false) {
338
357
  .join(" · ");
339
358
  };
340
359
  const itemRow = (it) => {
341
- const body = h("div", { class: "grow" }, h("div", { class: "t", style: { whiteSpace: "pre-wrap" } }, it.text), h("div", { class: "s" }, itemMeta(it)));
342
- const edit = h("button", { class: "btn small" }, "编辑");
343
- const pin = h("button", { class: "btn small", title: "置顶的一直放在系统提示里,自动整理也不会动它" }, it.tier === "core" ? "取消置顶" : "置顶");
344
- const archive = h("button", { class: "btn small", title: "归档:不再放进系统提示,agent 需要时还能搜到" }, "归档");
345
- const del = h("button", { class: "btn small danger" }, "删除");
360
+ const edit = h("button", { class: "memory-text", "aria-label": "编辑:" + it.text }, it.text);
361
+ const body = h("div", { class: "grow", "data-form": "" }, edit, h("div", { class: "s" }, itemMeta(it)));
362
+ const more = h("button", { class: "icon-btn", "aria-label": "更多操作", onclick: () => showMenu(more, [
363
+ { label: it.tier === "core" ? "取消置顶" : "置顶", onclick: run(more, () => patchItem(it, { tier: it.tier === "core" ? "active" : "core" })) },
364
+ { label: "归档", onclick: run(more, () => patchItem(it, { tier: "archive" })) },
365
+ { label: "删除", danger: true, onclick: run(more, () => api("/api/account/memory/items/" + it.id, { method: "DELETE" })) },
366
+ ]) }, icon("more"));
346
367
  edit.onclick = () => {
347
- edit.hidden = del.hidden = pin.hidden = archive.hidden = true;
368
+ more.hidden = true;
348
369
  const input = h("textarea", { class: "input", rows: 2, maxlength: mem.limits.item });
349
370
  input.value = it.text;
350
371
  const ok = h("button", { class: "btn small primary" }, "保存");
@@ -352,10 +373,7 @@ export async function memoryTab(panel, refresh = false) {
352
373
  fill(body, input, h("div", { class: "row" }, ok, h("button", { class: "btn small", onclick: reload }, "取消")));
353
374
  input.focus();
354
375
  };
355
- pin.onclick = run(pin, () => patchItem(it, { tier: it.tier === "core" ? "active" : "core" }));
356
- archive.onclick = run(archive, () => patchItem(it, { tier: "archive" }));
357
- del.onclick = run(del, () => api(`/api/account/memory/items/${it.id}`, { method: "DELETE" }));
358
- return h("div", { class: "list-item memory-item" }, body, edit, pin, archive, del);
376
+ return h("div", { class: "list-item memory-item" }, body, more);
359
377
  };
360
378
 
361
379
  // 条目多了要能找:输入就筛,一次先显示前 PAGE 条
@@ -399,36 +417,8 @@ export async function memoryTab(panel, refresh = false) {
399
417
  h(
400
418
  "div",
401
419
  { class: "section" },
402
- h("h4", {}, "记忆", h("span", { class: "grow" }), h("label", { class: "check" }, enabled, "开启")),
403
- h(
404
- "div",
405
- { class: "muted" },
406
- "像 ChatGPT 的记忆:对话时 agent 会参考你的「关于我」和记住的事,聊天里遇到长期有用的信息也会主动记一条(会在回复里告诉你)。Claude 和 Codex、Web 和飞书共用这一份。",
407
- ),
408
- h(
409
- "div",
410
- { class: "muted" },
411
- "用在哪些对话:你在 Web 上的对话;在「账号」里绑定了飞书后,和机器人的私聊也会用。飞书群聊不用。别人(包括管理员)看不到你的记忆,管理员只能清空它。",
412
- ),
413
- ),
414
- h(
415
- "div",
416
- { class: "section" },
417
- h("h4", {}, "自动"),
418
- curateSection(mem, reload),
419
- toggle("chatHistory", "参考以前的聊天", "已开启:agent 能搜你以前的聊天了", "已关闭:agent 不再搜以前的聊天"),
420
- h(
421
- "div",
422
- { class: "muted" },
423
- `agent 遇到「上次说的那个……」时,可以搜你以前的聊天(只有你自己的对话,被删掉的对话不算)。侧栏的「搜索」也用这份聊天记录索引,不开记忆也会记。${history ? `现在收录了 ${history.conversations} 段对话、${history.count} 句话。` : ""}`,
424
- ),
425
- h("div", { class: "row" }, clearHistory),
426
- ),
427
- h("div", { class: "section", "data-form": "" }, h("h4", {}, "关于我"), field("写给 agent 看的自我介绍:你是谁、在做什么、希望它怎么回答", about, `每次对话都会带上,最多 ${mem.limits.about} 字`), h("div", { class: "row" }, saveAbout)),
428
- h(
429
- "div",
430
- { class: "section" },
431
- h("h4", {}, `记住的事(${items.length})`),
420
+ h("div", { class: "row" }, h("h4", {}, `记住的事(${items.length})`), h("span", { class: "grow" }),
421
+ h("button", { class: "btn small", onclick: () => ui.settings.selectTab("memory-prefs") }, "记忆偏好")),
432
422
  h("div", { class: "muted" }, "「长期」和置顶的一直放在系统提示里;其余的按最近动过的时间放,放不下的 agent 需要时能搜到。满了会把最久没动过的挪到归档,不会删掉。你加的、你让记的、置顶的,自动整理都不会动。"),
433
423
  h("div", { class: "row" }, newItem, addBtn),
434
424
  items.length > 8 ? h("div", { class: "row" }, filter) : null,
@@ -17,7 +17,7 @@ export async function notifyTab(panel) {
17
17
  fill(box, h("h4", {}, "系统通知"), h("div", { class: "error-text" }, err.message));
18
18
  return;
19
19
  }
20
- const mine = await currentPushSub();
20
+ const mine = data.subscriptions.length ? await currentPushSub() : null;
21
21
  const here = mine && data.subscriptions.some((d) => d.endpoint === mine.endpoint);
22
22
  const busy = (btn, fn) => async () => {
23
23
  btn.disabled = true;
@@ -43,7 +43,7 @@ export async function notifyTab(panel) {
43
43
  await mine.unsubscribe().catch(() => {});
44
44
  toast("已关闭");
45
45
  });
46
- const testBtn = h("button", { class: "btn" }, "发送测试通知");
46
+ const testBtn = h("button", { class: "btn" }, "向已订阅设备发送测试");
47
47
  testBtn.onclick = busy(testBtn, async () => {
48
48
  const r = await api("/api/push/test", { method: "POST" });
49
49
  toast(`已发送到 ${r.sent} 台设备`);
@@ -2,7 +2,7 @@ import { fill, h, toast } from "../lib/dom.js";
2
2
  import { api } from "../lib/api.js";
3
3
  import { relTime } from "../lib/time.js";
4
4
  import { copyText } from "../lib/markdown.js";
5
- import { isAdmin, S } from "../state.js";
5
+ import { isAdmin, S, ui } from "../state.js";
6
6
  import { dropPushSub } from "../lib/push.js";
7
7
  import { field, markClean, saveButton } from "./common.js";
8
8
  import { weixinLinkSection } from "./weixin.js";
@@ -26,7 +26,7 @@ export async function accountTab(page) {
26
26
  const pw = h("input", { type: "password", autocomplete: "new-password" });
27
27
  const name = h("input", { value: S.user?.name ?? "", autocomplete: "username", autocapitalize: "off", spellcheck: "false", maxlength: "32" });
28
28
  const currentName = h("span", {}, S.user?.name ?? "");
29
- const devicesBox = h("div", { class: "section" }, h("h4", {}, "已登录的设备"), h("span", { class: "spinner" }));
29
+ const devicesBox = h("div", { class: "section", "data-section": "devices" }, h("h4", {}, "已登录的设备"), h("span", { class: "spinner" }));
30
30
  const nameForm = h("div", { class: "account-edit", "data-form": "", hidden: true });
31
31
  const passwordForm = h("div", { class: "account-edit", "data-form": "", hidden: true });
32
32
  const hideForms = () => {
@@ -74,8 +74,6 @@ export async function accountTab(page) {
74
74
  nameForm,
75
75
  passwordForm,
76
76
  ),
77
- feishuLinkSection(),
78
- weixinLinkSection(),
79
77
  devicesBox,
80
78
  );
81
79
  const loadDevices = async () => {
@@ -93,6 +91,17 @@ export async function accountTab(page) {
93
91
  await loadDevices();
94
92
  }
95
93
 
94
+ export function bindingsTab(page) {
95
+ const feishu = feishuLinkSection();
96
+ feishu.dataset.section = "feishu";
97
+ if (isAdmin()) page.append(h("button", { class: "btn", onclick: () => ui.settings.selectTab("feishu") }, "机器人管理"));
98
+ page.append(feishu);
99
+ }
100
+
101
+ export function weixinTab(page) {
102
+ page.append(weixinLinkSection());
103
+ }
104
+
96
105
  /** 登录设备列表,每台一个「下线」。showOwner:标上是谁的(管理员看所有人时) */
97
106
  export function deviceList(sessions, { showOwner = false, onChange }) {
98
107
  if (!sessions.length) return h("div", { class: "muted" }, "没有");
@@ -138,8 +147,7 @@ function feishuLinkSection() {
138
147
  return;
139
148
  }
140
149
  // 没有飞书机器人、也没绑过:整节不显示
141
- box.hidden = !data.bots.length && !data.links.length;
142
- if (box.hidden) return fill(box);
150
+ if (!data.bots.length && !data.links.length) return fill(box, h("p", { class: "muted" }, "当前实例还没有可绑定的飞书机器人。"));
143
151
  const codeBox = h("div");
144
152
  const gen = h("button", { class: "btn" }, data.links.length ? "再绑一个" : "生成绑定码");
145
153
  gen.onclick = async () => {