agent-loft 0.2.78 → 0.2.80

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.
@@ -106,12 +106,15 @@ export function helpText(agents: string[], channel?: string): string {
106
106
  return [
107
107
  "直接说话即可,会自动接着上次的会话。闲置太久会自动开新会话。",
108
108
  "",
109
- "Agent 给选项时会发一张提问卡,点按钮作答;都不合适就直接发消息,那条消息会被当成你的回答。",
109
+ channel === "weixin"
110
+ ? "Agent 要授权或提问时会发一条消息,回复里面的数字作答;提问的选项都不合适就直接发文字。"
111
+ : "Agent 给选项时会发一张提问卡,点按钮作答;都不合适就直接发消息,那条消息会被当成你的回答。",
110
112
  "",
111
113
  "**命令**(需整条消息只有命令;只发一个 `/` 可以列出命令;agent 自己的命令默认不在列表里,可以在设置 → 命令里勾选,没勾的照样可以直接发)",
112
114
  "",
113
115
  ...BUILTIN_COMMANDS.filter((c) => !c.multiAgentOnly || multi)
114
116
  .filter((c) => !c.feishuOnly || channel === "feishu")
117
+ .filter((c) => !c.chatOnly || channel === "feishu" || channel === "weixin")
115
118
  .filter((c) => !["claude", "codex"].includes(c.name))
116
119
  .map((c) =>
117
120
  c.name === "expert"
@@ -2,6 +2,7 @@
2
2
  import { deleteMemory, memoryPrompt, readMemory } from "./memory.js";
3
3
  import { purgeHistory } from "./chat-history.js";
4
4
  import { unlinkUser } from "./feishu-links.js";
5
+ import { removeWeixinAccount } from "./weixin-accounts.js";
5
6
  import { deleteUserTasks } from "./tasks.js";
6
7
  import { purgeUserSkills } from "./skills.js";
7
8
  import { purgeUserExperts } from "./experts.js";
@@ -42,11 +43,12 @@ export function sessionMemory(
42
43
  };
43
44
  }
44
45
 
45
- /** 删用户时一起删掉的个人数据:记忆、聊天记录索引、飞书绑定、定时任务、技能(他建的技能,装了的人也就没了)、专家和 MCP */
46
+ /** 删用户时一起删掉的个人数据:记忆、聊天记录索引、飞书和微信绑定、定时任务、技能(他建的技能,装了的人也就没了)、专家和 MCP */
46
47
  export function purgeUserData(userId: string): void {
47
48
  deleteMemory(userId);
48
49
  purgeHistory(userId);
49
50
  unlinkUser(userId);
51
+ removeWeixinAccount(userId);
50
52
  deleteUserTasks(userId);
51
53
  purgeUserSkills(userId);
52
54
  purgeUserExperts(userId);
@@ -0,0 +1,88 @@
1
+ // 微信 ClawBot 绑定:agent-loft 用户 ↔ 他扫码连上的那个微信。
2
+ //
3
+ // 和飞书不一样,这里没有「机器人」和「绑定码」:用户登录 Web 后自己扫码,扫码的人就是这个账号的主人,
4
+ // 这条微信对话也只认扫码的这个微信号(ClawBot 本身只能由账号所有者私聊使用)。
5
+ //
6
+ // 重新扫码后 iLink 的 bot id 会变,所以会话键跟着 agent-loft 用户走,不跟 bot 走。
7
+ //
8
+ // 这个文件里有 bot token,权限 0600,也不进 config.json(配置接口要给管理员看,token 是个人的)。
9
+ import { EventEmitter } from "node:events";
10
+ import path from "node:path";
11
+ import { dataDir, readJson, writeJsonAtomic } from "../home.js";
12
+
13
+ export type WeixinAccount = {
14
+ /** agent-loft 用户 */
15
+ userId: string;
16
+ botId: string;
17
+ botToken: string;
18
+ baseUrl: string;
19
+ /** 扫码的微信号。只有它发来的消息才处理 */
20
+ ilinkUserId: string;
21
+ boundAt: number;
22
+ /** 长轮询游标:重启后接着收,不重复也不丢 */
23
+ cursor: string;
24
+ /** 最近一条入站消息带的会话令牌,回复时要原样带回去 */
25
+ contextToken?: string;
26
+ lastInboundAt?: number;
27
+ /** 上一条入站消息之后发出去了多少条(微信限制每条入站之后能发的条数) */
28
+ sentSinceInbound: number;
29
+ };
30
+
31
+ const storePath = path.join(dataDir, "weixin-accounts.json");
32
+
33
+ let cache: Record<string, WeixinAccount> | undefined;
34
+
35
+ function load(): Record<string, WeixinAccount> {
36
+ cache ??= readJson<Record<string, WeixinAccount>>(storePath, {});
37
+ return cache;
38
+ }
39
+
40
+ function save(): void {
41
+ writeJsonAtomic(storePath, load());
42
+ }
43
+
44
+ /** changed:绑定多了、少了或者换了凭据。游标、令牌这些运行时字段变化不发 */
45
+ export const weixinAccountEvents = new EventEmitter<{ changed: [] }>();
46
+
47
+ /** 这个用户的微信对话的会话键 */
48
+ export const weixinKey = (userId: string): string => `weixin:${userId}`;
49
+
50
+ export function getWeixinAccount(userId: string): WeixinAccount | undefined {
51
+ return load()[userId];
52
+ }
53
+
54
+ export function listWeixinAccounts(): WeixinAccount[] {
55
+ return Object.values(load());
56
+ }
57
+
58
+ /**
59
+ * 记下一次扫码的结果。微信上一个号只连一个 ClawBot(再次绑定会自动解绑前一个),
60
+ * 所以别的 agent-loft 用户手里同一个微信号的旧绑定已经失效,一起清掉。
61
+ */
62
+ export function bindWeixinAccount(params: { userId: string; botId: string; botToken: string; baseUrl: string; ilinkUserId: string }): WeixinAccount {
63
+ const all = load();
64
+ for (const [userId, account] of Object.entries(all)) {
65
+ if (userId !== params.userId && account.ilinkUserId === params.ilinkUserId) delete all[userId];
66
+ }
67
+ const account: WeixinAccount = { ...params, boundAt: Date.now(), cursor: "", sentSinceInbound: 0 };
68
+ all[params.userId] = account;
69
+ save();
70
+ weixinAccountEvents.emit("changed");
71
+ return account;
72
+ }
73
+
74
+ export function patchWeixinAccount(userId: string, patch: Partial<Pick<WeixinAccount, "cursor" | "contextToken" | "lastInboundAt" | "sentSinceInbound">>): void {
75
+ const account = load()[userId];
76
+ if (!account) return;
77
+ Object.assign(account, patch);
78
+ save();
79
+ }
80
+
81
+ export function removeWeixinAccount(userId: string): boolean {
82
+ const all = load();
83
+ if (!all[userId]) return false;
84
+ delete all[userId];
85
+ save();
86
+ weixinAccountEvents.emit("changed");
87
+ return true;
88
+ }
package/src/main.ts CHANGED
@@ -7,6 +7,7 @@ import { ClaudeDriver } from "./agents/claude/driver.js";
7
7
  import { CodexDriver } from "./agents/codex/driver.js";
8
8
  import { agentHealth } from "./agents/health.js";
9
9
  import { startFeishu, stopFeishu } from "./channels/feishu/index.js";
10
+ import { startWeixin, stopWeixin } from "./channels/weixin/index.js";
10
11
  import { startWeb, stopWeb } from "./channels/web/server.js";
11
12
  import { home } from "./home.js";
12
13
  import { startMemoryCurator } from "./core/memory-curator.js";
@@ -51,6 +52,7 @@ async function main(): Promise<void> {
51
52
  startMemoryCurator();
52
53
 
53
54
  startFeishu();
55
+ startWeixin();
54
56
  if (config().web.enabled) await startWeb();
55
57
  // 定时发送:等 agent 和渠道都起来了再开始,服务停着时到点的这时补发
56
58
  startScheduler((m) => {
@@ -82,6 +84,7 @@ async function main(): Promise<void> {
82
84
  force.unref();
83
85
  stopScheduler();
84
86
  stopFeishu();
87
+ stopWeixin();
85
88
  await stopWeb();
86
89
  await hub.shutdown().catch(() => {});
87
90
  // hub 收尾时会把正在跑的定时任务记成中断,所以放在它后面
@@ -70,7 +70,7 @@ function search(query: string, limit: number) {
70
70
  });
71
71
  if (hits.length === 0) return text(`以前的聊天里没找到「${query}」相关的内容。可以换个说法再搜。`);
72
72
  const blocks = hits.map((h, i) => {
73
- const title = convs.get(h.key)?.title ?? (h.key.startsWith("feishu:") ? "飞书私聊" : "未命名对话");
73
+ const title = convs.get(h.key)?.title ?? (h.key.startsWith("feishu:") ? "飞书私聊" : h.key.startsWith("weixin:") ? "微信" : "未命名对话");
74
74
  return [
75
75
  `[${i + 1}] ${localDate(h.ts)} · 「${title}」`,
76
76
  ...(h.user ? [`用户:${h.user}`] : []),
@@ -1,7 +1,8 @@
1
1
  /* ---------- 输入框 ---------- */
2
2
 
3
3
  .composer-wrap {
4
- padding: 0 16px calc(12px + env(safe-area-inset-bottom));
4
+ padding: 0 16px calc(12px + max(0px, env(safe-area-inset-bottom) - var(--kb-inset, 0px)));
5
+ flex-shrink: 0;
5
6
  position: relative;
6
7
  z-index: 2;
7
8
  }
@@ -182,7 +183,7 @@ button.ctx-item:hover {
182
183
  background: transparent;
183
184
  resize: none;
184
185
  padding: 14px 16px 4px;
185
- max-height: 40vh;
186
+ max-height: min(40vh, var(--keyboard-input-max-height, 40vh));
186
187
  min-height: 50px;
187
188
  line-height: 1.5;
188
189
  font-size: 16px; /* iOS 小于 16px 会自动放大页面 */
@@ -145,7 +145,7 @@
145
145
  }
146
146
 
147
147
  .composer-wrap {
148
- padding: 0 10px calc(8px + env(safe-area-inset-bottom));
148
+ padding: 0 10px calc(8px + max(0px, env(safe-area-inset-bottom) - var(--kb-inset, 0px)));
149
149
  }
150
150
 
151
151
  .composer-hint {
@@ -36,12 +36,6 @@
36
36
  margin-top: calc(52px + env(safe-area-inset-top));
37
37
  }
38
38
 
39
- /* 软键盘盖住的高度(views/remote.js),iOS 上才不为 0 */
40
- .remote-pane,
41
- .main > .term-dock {
42
- padding-bottom: var(--kb-inset, 0px);
43
- }
44
-
45
39
  .remote-pane[hidden] {
46
40
  display: none;
47
41
  }
@@ -2,7 +2,8 @@
2
2
 
3
3
  .layout {
4
4
  display: flex;
5
- height: 100%;
5
+ /* 键盘只缩小可见视口时,整块界面一起避让;原生已缩小页面的浏览器补偿为 0。 */
6
+ height: calc(100% - var(--kb-inset, 0px));
6
7
  overflow: hidden;
7
8
  position: relative;
8
9
  }
@@ -255,7 +256,7 @@ button.conv-group:hover {
255
256
  display: grid;
256
257
  }
257
258
 
258
- /* 飞书分组的同一位置:平时显示飞书,悬停或键盘聚焦时显示新建加号。 */
259
+ /* IM 分组常驻渠道图标,桌面悬停或键盘聚焦时显示新建加号。 */
259
260
  .bot-row .conv-item.proj {
260
261
  padding-right: 36px;
261
262
  }
@@ -279,14 +280,16 @@ button.conv-group:hover {
279
280
  visibility: hidden;
280
281
  }
281
282
 
282
- .bot-row:hover .bot-new .feishu-mark,
283
- .bot-row:focus-within .bot-new .feishu-mark {
284
- visibility: hidden;
285
- }
283
+ @media (hover: hover) and (pointer: fine) {
284
+ .bot-row:hover .bot-new .feishu-mark,
285
+ .bot-row:focus-within .bot-new .feishu-mark {
286
+ visibility: hidden;
287
+ }
286
288
 
287
- .bot-row:hover .bot-new .bot-plus,
288
- .bot-row:focus-within .bot-new .bot-plus {
289
- visibility: visible;
289
+ .bot-row:hover .bot-new .bot-plus,
290
+ .bot-row:focus-within .bot-new .bot-plus {
291
+ visibility: visible;
292
+ }
290
293
  }
291
294
 
292
295
  /* 鼠标挪到右边的加号上时,整行的 hover 底色还留着 */
@@ -300,14 +303,6 @@ button.conv-group:hover {
300
303
  .proj-new {
301
304
  display: grid;
302
305
  }
303
-
304
- .bot-new .bot-plus {
305
- visibility: visible;
306
- }
307
-
308
- .bot-new .feishu-mark {
309
- visibility: hidden;
310
- }
311
306
  }
312
307
 
313
308
  .conv-item {
@@ -410,12 +405,6 @@ button.conv-group:hover {
410
405
  color: var(--fg-soft);
411
406
  }
412
407
 
413
- /* 有状态点时由状态点推到右侧,专家图标紧随其后 */
414
- .conv-item > .dot + .expert-mark,
415
- .conv-item > .conv-spin + .expert-mark {
416
- margin-left: 0;
417
- }
418
-
419
408
  .conv-item .task-mark,
420
409
  .conv-item .feishu-mark {
421
410
  flex-shrink: 0;
@@ -431,10 +420,9 @@ button.conv-group:hover {
431
420
  height: 14px;
432
421
  }
433
422
 
434
- .conv-item > .dot ~ .task-mark,
435
- .conv-item > .conv-spin ~ .task-mark,
436
- .conv-item > .dot ~ .feishu-mark,
437
- .conv-item > .conv-spin ~ .feishu-mark {
423
+ /* 尾部图标共用右侧空间,状态始终排在最后。 */
424
+ .conv-item > .task-mark ~ .dot,
425
+ .conv-item > .task-mark ~ .conv-spin {
438
426
  margin-left: 0;
439
427
  }
440
428
 
@@ -456,6 +444,11 @@ button.conv-group:hover {
456
444
  display: inline-grid;
457
445
  }
458
446
 
447
+ .conv-item:hover > .expert-mark ~ .dot,
448
+ .conv-item:hover > .expert-mark ~ .conv-spin {
449
+ margin-left: 0;
450
+ }
451
+
459
452
  .conv-item:hover .task-mark,
460
453
  .conv-item:hover .expert-mark ~ .feishu-mark {
461
454
  display: none;
@@ -190,6 +190,13 @@
190
190
  height: 18px;
191
191
  }
192
192
 
193
+ /* 上传图片包含背景,按头像尺寸显示,避免主体缩在引擎图标的 18px 方框里。 */
194
+ .topbar .conv-expert-icon .expert-avatar,
195
+ .topbar .conv-expert-icon .expert-avatar img {
196
+ width: 28px;
197
+ height: 28px;
198
+ }
199
+
193
200
  .topbar .conv-expert-icon .expert-emoji {
194
201
  font-size: 16px;
195
202
  }
package/web/lib/dom.js CHANGED
@@ -70,6 +70,8 @@ export const ICON = {
70
70
  restore: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 14h6v6M20 10h-6V4M14 10l7-7M3 21l7-7"/></svg>',
71
71
  laptop: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="5" width="16" height="11" rx="1.5"/><path d="M2 19h20"/></svg>',
72
72
  logo: '<svg viewBox="0 0 48 48" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M17.5 8.5a8 8 0 0 1 13.2 0 8 8 0 0 1 9 9.4 8 8 0 0 1 0 12.2 8 8 0 0 1-9 9.4 8 8 0 0 1-13.2 0 8 8 0 0 1-9-9.4 8 8 0 0 1 0-12.2 8 8 0 0 1 9-9.4Z"/><path d="m17 18 5 5-5 5M25 30h7"/></svg>',
73
+ weixin:
74
+ '<svg viewBox="0 0 24 24"><path fill="#07C160" d="M9.5 4C5.4 4 2 6.8 2 10.3c0 2 1.1 3.8 2.9 5l-.7 2.2 2.5-1.3c.9.3 1.8.4 2.8.4h.5a5.6 5.6 0 0 1-.2-1.5c0-3.3 3.1-6 7-6h.5C16.2 6.1 13.1 4 9.5 4zM7 8.1a.9.9 0 1 1 0 1.8.9.9 0 0 1 0-1.8zm5 0a.9.9 0 1 1 0 1.8.9.9 0 0 1 0-1.8z"/><path fill="#07C160" d="M22 14.6c0-2.8-2.7-5-6-5s-6 2.2-6 5 2.7 5 6 5c.7 0 1.4-.1 2-.3l1.9 1-.5-1.7c1.6-1 2.6-2.4 2.6-4zm-8-.9a.8.8 0 1 1 0-1.6.8.8 0 0 1 0 1.6zm4 0a.8.8 0 1 1 0-1.6.8.8 0 0 1 0 1.6z"/></svg>',
73
75
  feishu:
74
76
  '<svg viewBox="2 4 44 40"><path fill="#00D6B9" d="M11.6 7H27.6C30.9 9.9 32.8 14.7 33.6 17C30.9 18.3 29 20.6 27 24C22.6 16.4 15.6 11 11.6 7Z"/><path fill="#133C9A" d="M17 29C21 29 25 26.9 28 23.4C36 14 41.4 16.8 44 18C38.5 21 40.5 29.6 33 36C27.5 34.8 21.8 32.2 17 29Z"/><path fill="#3370FF" d="M4 16.2C7.2 20.8 12.4 26 17.4 29.4C23 33.2 28.6 35.7 34 35.2C28.9 39.8 23.8 41 19 41C12.5 41 6.9 37.8 4 35.4Z"/></svg>',
75
77
  };
@@ -0,0 +1,45 @@
1
+ import { isTouch } from "./dom.js";
2
+
3
+ // iOS 不按 interactive-widget=resizes-content 缩小布局视口,100dvh 也不会避开键盘。
4
+ // 从主界面加载时就跟踪可见视口,让聊天、侧栏和远程控制共用一次高度补偿。
5
+ const vv = window.visualViewport;
6
+ if (vv && isTouch) {
7
+ let last = 0;
8
+ let frame = 0;
9
+ let timers = [];
10
+ const sync = () => {
11
+ frame = 0;
12
+ // 双指缩放不是软键盘;后台恢复时还没拿到有效尺寸也先保留当前布局。
13
+ if (Math.abs(vv.scale - 1) > 0.01 || vv.height <= 0) return;
14
+ const covered = Math.max(0, Math.round(innerHeight - vv.height - vv.offsetTop));
15
+ const style = document.documentElement.style;
16
+ style.setProperty("--kb-inset", `${covered}px`);
17
+ style.setProperty("--keyboard-input-max-height", `${Math.floor(Math.min(innerHeight, vv.height) * 0.4)}px`);
18
+ // iOS 为露出光标推上去的外层容器,收键盘后有时不会复位。
19
+ // 消息列表、输入框和终端自己的滚动位置保留。
20
+ if (last > 0 && covered === 0) {
21
+ window.scrollTo(0, 0);
22
+ for (const el of [document.scrollingElement, ...document.querySelectorAll("#app, .layout, .main, .remote-pane, .remote-body")]) {
23
+ if (el?.scrollTop) el.scrollTop = 0;
24
+ }
25
+ }
26
+ last = covered;
27
+ };
28
+ const queue = () => {
29
+ if (!frame) frame = requestAnimationFrame(sync);
30
+ };
31
+ const settle = () => {
32
+ queue();
33
+ timers.forEach(clearTimeout);
34
+ // PWA 的 resize 可能先于 height / offsetTop 更新;动画结束后再读几次。
35
+ timers = [80, 300, 700].map((delay) => setTimeout(queue, delay));
36
+ };
37
+ vv.addEventListener("resize", settle);
38
+ vv.addEventListener("scroll", settle);
39
+ for (const event of ["resize", "orientationchange", "pageshow"]) window.addEventListener(event, settle);
40
+ for (const event of ["focusin", "focusout"]) document.addEventListener(event, settle);
41
+ document.addEventListener("visibilitychange", () => {
42
+ if (!document.hidden) settle();
43
+ });
44
+ sync();
45
+ }
@@ -5,6 +5,7 @@ import { copyText } from "../lib/markdown.js";
5
5
  import { isAdmin, S } from "../state.js";
6
6
  import { dropPushSub } from "../lib/push.js";
7
7
  import { field, saveButton } from "./common.js";
8
+ import { weixinLinkSection } from "./weixin.js";
8
9
 
9
10
  /** 设置 → 账号:我是谁、改密码、绑定飞书、我登录着的设备。别人的设备在 管理 →「用户」里 */
10
11
  export async function accountTab(page) {
@@ -64,6 +65,7 @@ export async function accountTab(page) {
64
65
  ),
65
66
  ),
66
67
  feishuLinkSection(),
68
+ weixinLinkSection(),
67
69
  devicesBox,
68
70
  );
69
71
  const loadDevices = async () => {
@@ -0,0 +1,168 @@
1
+ // 设置 → 账号 → 微信:用微信扫码,把这个账号和微信 ClawBot 连起来。
2
+ // 服务端在后台等扫码(见 channels/weixin/login.ts),这里每两秒取一次状态。
3
+ import { fill, h, toast } from "../lib/dom.js";
4
+ import { api } from "../lib/api.js";
5
+ import { confirmDialog, modal } from "../lib/overlay.js";
6
+ import { relTime } from "../lib/time.js";
7
+ import { field } from "./common.js";
8
+
9
+ const STATE_TEXT = {
10
+ connecting: "正在连接",
11
+ connected: "已连接",
12
+ reconnecting: "网络有问题,正在重连",
13
+ expired: "登录已失效,请重新扫码",
14
+ stopped: "没有运行",
15
+ };
16
+
17
+ const DELIVERY_TEXT = {
18
+ unconfirmed: "最近一次回复微信接口受理了,但手机可能没收到(微信可能在限制这个账号)",
19
+ failed: "最近一次回复没有发出去",
20
+ };
21
+
22
+ const NOTES = [
23
+ "每个人连自己的微信,这条对话用你的记忆和工具,只认扫码的这个微信号。",
24
+ "微信里只能发文字和语音转文字,授权和提问回复数字处理。网页上能看到同一条对话。",
25
+ "微信限制较多:约 24 小时没在微信里说话后,它不允许主动发消息,定时任务的结果可能发不过去;每条消息之后能发的条数也有限。",
26
+ "消息会经过腾讯服务器。这是腾讯的实验性功能,它可能限制或停止服务,建议先用小号试。不要反复重新扫码,可能触发风控。",
27
+ ];
28
+
29
+ export function weixinLinkSection() {
30
+ const box = h("div", { class: "section" });
31
+ const load = async () => {
32
+ let data;
33
+ try {
34
+ data = await api("/api/account/weixin");
35
+ } catch (err) {
36
+ fill(box, h("h4", {}, "微信"), h("div", { class: "error-text" }, err.message));
37
+ return;
38
+ }
39
+ const scan = h("button", { class: data.linked ? "btn" : "btn accent", onclick: () => startLogin(load) }, data.linked ? "重新扫码" : "扫码连接微信");
40
+ const rows = [];
41
+ if (data.linked) {
42
+ const rm = h("button", { class: "btn small danger" }, "断开");
43
+ rm.onclick = async () => {
44
+ const sure = await confirmDialog({
45
+ title: "断开微信?",
46
+ message: "断开后这里不再收发微信消息。微信里的 ClawBot 入口要在手机上自己移除。",
47
+ confirmText: "断开",
48
+ danger: true,
49
+ });
50
+ if (!sure) return;
51
+ rm.disabled = true;
52
+ try {
53
+ await api("/api/account/weixin", { method: "DELETE" });
54
+ toast("已断开");
55
+ void load();
56
+ } catch (err) {
57
+ toast(err.message);
58
+ rm.disabled = false;
59
+ }
60
+ };
61
+ const state = STATE_TEXT[data.status.state] ?? data.status.state;
62
+ const facts = [
63
+ data.wechatShort,
64
+ `绑定于 ${relTime(data.boundAt)}`,
65
+ data.lastInboundAt ? `上次收到消息 ${relTime(data.lastInboundAt)}` : "还没收到过消息,先在微信里发一条",
66
+ ].join(" · ");
67
+ rows.push(h("div", { class: "list-item" }, h("div", { class: "grow" }, h("div", { class: "t" }, state), h("div", { class: "s" }, facts)), rm));
68
+ if (DELIVERY_TEXT[data.status.delivery]) rows.push(h("div", { class: "error-text" }, DELIVERY_TEXT[data.status.delivery]));
69
+ }
70
+ fill(
71
+ box,
72
+ h("h4", {}, "微信"),
73
+ h("div", { class: "muted" }, "连接后可以在微信的 ClawBot 里和 agent 聊天。"),
74
+ h("ul", { class: "muted" }, NOTES.map((n) => h("li", {}, n))),
75
+ rows,
76
+ h("div", { class: "row" }, scan),
77
+ );
78
+ };
79
+ void load();
80
+ return box;
81
+ }
82
+
83
+ async function startLogin(onDone) {
84
+ let login;
85
+ try {
86
+ login = await api("/api/account/weixin/login", { method: "POST" });
87
+ } catch (err) {
88
+ toast(err.message);
89
+ return;
90
+ }
91
+ const qr = h("div", { class: "qr", html: login.qrSvg });
92
+ const state = h("div", { class: "muted", style: { textAlign: "center" } });
93
+ const verify = h("div", { hidden: true });
94
+ const hint = h(
95
+ "div",
96
+ { class: "muted" },
97
+ "用手机微信扫码,并在手机上确认。需要微信 8.0.70 以上,「设置 → 插件」里有 ClawBot 才能连。如果手机提示「运行环境存在风险」,是微信拒绝了这次连接,这边不会有提示,可以关掉这个窗口,过一段时间再试。",
98
+ );
99
+ const body = h("div", { class: "modal-body" }, hint, qr, state, verify);
100
+ let finished = false;
101
+ const close = modal({
102
+ title: "扫码连接微信",
103
+ body,
104
+ beforeClose: () => {
105
+ if (!finished) void api(`/api/account/weixin/login/${login.id}`, { method: "DELETE" }).catch(() => {});
106
+ finished = true;
107
+ },
108
+ });
109
+ let shownSvg = login.qrSvg;
110
+ let verifyShown = false;
111
+ const showVerify = (wrong) => {
112
+ state.textContent = wrong ? "数字不对,请重新输入。" : "请输入手机微信上显示的数字。";
113
+ if (verifyShown) return;
114
+ verifyShown = true;
115
+ const input = h("input", { inputmode: "numeric", autocomplete: "one-time-code", placeholder: "手机上显示的数字" });
116
+ const go = h("button", { class: "btn primary" }, "确定");
117
+ go.onclick = async () => {
118
+ go.disabled = true;
119
+ try {
120
+ await api(`/api/account/weixin/login/${login.id}/verify`, { method: "POST", body: { code: input.value } });
121
+ verify.hidden = true;
122
+ verifyShown = false;
123
+ state.textContent = "正在验证…";
124
+ } catch (err) {
125
+ toast(err.message);
126
+ }
127
+ go.disabled = false;
128
+ };
129
+ fill(verify, field("手机上显示的数字", input), h("div", { class: "row", style: { justifyContent: "center", marginTop: "12px" } }, go));
130
+ verify.hidden = false;
131
+ input.focus();
132
+ };
133
+ const poll = async () => {
134
+ if (finished) return;
135
+ let r;
136
+ try {
137
+ r = await api(`/api/account/weixin/login/${login.id}`);
138
+ } catch (err) {
139
+ state.textContent = err.message;
140
+ finished = true;
141
+ return;
142
+ }
143
+ if (r.qrSvg !== shownSvg) {
144
+ shownSvg = r.qrSvg;
145
+ qr.innerHTML = r.qrSvg;
146
+ verify.hidden = true;
147
+ verifyShown = false;
148
+ }
149
+ if (r.state === "confirmed" || r.state === "already") {
150
+ finished = true;
151
+ toast(r.state === "already" ? "这个微信已经连接" : "✅ 已连接。先在微信的 ClawBot 里发一条消息");
152
+ close();
153
+ onDone();
154
+ return;
155
+ }
156
+ if (r.state === "failed") {
157
+ finished = true;
158
+ state.textContent = r.message ?? "连接失败";
159
+ return;
160
+ }
161
+ if (r.state === "need_verifycode") showVerify(false);
162
+ else if (r.state === "verify_wrong") showVerify(true);
163
+ else if (!verifyShown) state.textContent = r.message ?? (r.state === "scaned" ? "已扫码,请在手机上确认…" : "等待扫码…");
164
+ setTimeout(poll, 2000);
165
+ };
166
+ state.textContent = "等待扫码…";
167
+ setTimeout(poll, 2000);
168
+ }
@@ -102,7 +102,7 @@ export function openSearch() {
102
102
  ...results.map(({ conv, snippet }, i) => {
103
103
  // 本地有就用本地的(标题是最新的)
104
104
  const c = S.convs.get(conv.key) ?? conv;
105
- const where = c.channel === "feishu" ? "飞书" : baseName(c.cwd);
105
+ const where = c.channel === "feishu" ? "飞书" : c.channel === "weixin" ? "微信" : baseName(c.cwd);
106
106
  return h(
107
107
  "button",
108
108
  {
@@ -33,6 +33,7 @@ import { clearCommandCache, preloadCommands } from "./slash.js";
33
33
  import { releaseForeignPushSub } from "../lib/push.js";
34
34
  import { createSidebarNav } from "./sidebar-nav.js";
35
35
  import { bindSidebarGestures } from "./sidebar-gestures.js";
36
+ import "../lib/keyboard.js";
36
37
 
37
38
  // ---------- 主界面 ----------
38
39
 
@@ -37,7 +37,7 @@ const downloadUrl = (f) => `${f.url}&download=1`;
37
37
 
38
38
  function convLabel(f) {
39
39
  if (f.conv.hidden) return "已删除的对话";
40
- return f.conv.title || (f.conv.key.startsWith("feishu:") ? "飞书对话" : "未命名对话");
40
+ return f.conv.title || (f.conv.key.startsWith("feishu:") ? "飞书对话" : f.conv.key.startsWith("weixin:") ? "微信" : "未命名对话");
41
41
  }
42
42
 
43
43
  /**
@@ -214,13 +214,17 @@ export async function openConversation(key) {
214
214
  // 历史一次性加进来,最后再统一排版,免得每加一条就强制排版一次
215
215
  bulk = true;
216
216
  try {
217
+ let todos = null;
217
218
  for (const m of data.messages) {
219
+ // 面板只属于最后一轮;提问卡切开的多段回复仍算同一轮。
220
+ if (m.role === "user") todos = null;
221
+ else if (m.todos) todos = m.todos;
218
222
  if (m.role === "user") addItem({ type: "user", text: m.text, files: m.attachments ?? [], origin: null, anchor: m.anchor });
219
223
  else if (m.role === "question") addItem({ type: "ix", id: m.question.id, ix: historyQuestion(key, m) });
220
224
  else addItem({ type: "ai", steps: m.steps ?? [], committed: m.text, partial: "", done: true, footer: m.footer, at: m.ts, anchor: m.anchor, segment: m.segment });
221
225
  }
222
- // 任务清单:最后改过的那一份(进行中这一轮又改了的话,下面重放 todos 事件时换成新的)
223
- setTodos(data.messages.findLast((m) => m.todos)?.todos ?? null);
226
+ // 进行中这一轮的清单由下面重放 todos 事件恢复。
227
+ setTodos(todos);
224
228
  S.seq.set(key, 0);
225
229
  // 进行中这一轮弹过的卡(含已处理的):重放到 ix 事件时放回原位
226
230
  replayCards = new Map((data.interactions ?? []).map((ix) => [ix.id, ix]));
@@ -799,7 +803,10 @@ export function applyEvent(env) {
799
803
  S.items = S.items.filter((i) => i !== S.turnAi);
800
804
  S.turnAi = null;
801
805
  }
802
- if (!queued && (!S.turnAi || S.turnAi.done)) turnStartAt = env.ts;
806
+ if (!queued && (!S.turnAi || S.turnAi.done)) {
807
+ setTodos(null);
808
+ turnStartAt = env.ts;
809
+ }
803
810
  const it = addItem({ type: "user", text: ev.text, files: ev.files ?? ev.attachments, origin: ev.origin, id: ev.id, queued });
804
811
  // 这个页面刚发的:普通消息顶到最上面,排队消息等轮到它时再定位。
805
812
  if (S.pinNext && ev.origin?.channel === "web") {
@@ -834,11 +841,16 @@ export function applyEvent(env) {
834
841
  }
835
842
  // 轮到它了(上一轮刚出结果):挂上「思考中」占位,用时从现在算。被当成提问的回答时那一轮还在跑,不用挂
836
843
  if (ev.state === "started" && (!S.turnAi || S.turnAi.done)) {
844
+ setTodos(null);
837
845
  turnStartAt = env.ts;
838
846
  currentAi(env.ts);
839
847
  }
840
848
  return;
841
849
  }
850
+ case "turn_start":
851
+ // 包括没有新用户消息、由后台任务触发的轮次。
852
+ setTodos(null);
853
+ return;
842
854
  case "step": {
843
855
  const ai = currentAi(env.ts);
844
856
  ai.steps.push(ev.text);