Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(bots): topics on colon message ids, link switching and read on return
zqchrisb0d9bb0ab11a
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.tsindex fd23c8ce7f..d60ceaf62d 100644--- a/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts+++ b/apps/desktop/src/main/localDb/ipc/__tests__/botCanonicalSession.test.ts@@ -1782,8 +1782,9 @@ describe('Bot canonical Session lifecycle', () => {});const chatId = created.session.id;const insert = h.sqlite!.prepare('INSERT INTO messages (id, client_id, session_id, role, content, created_at) VALUES (?, ?, ?, ?, ?, ?)');- insert.run('m-root', 'root-1', chatId, 'user', JSON.stringify({ text: 'Plan the launch post' }), 1_000);- const root = { botId: 'bot-1', rootSessionId: chatId, rootClientId: 'root-1' };+ // Host-written messages such as the opening hello carry colons in their clientId.+ insert.run('m-root', 'bot-opening:inv-1', chatId, 'user', JSON.stringify({ text: 'Plan the launch post' }), 1_000);+ const root = { botId: 'bot-1', rootSessionId: chatId, rootClientId: 'bot-opening:inv-1' };// A root outside the companion's main Chat cannot carry a topic.await expect(invoke('local-db:bots:dm-thread:ensure', { ...root, rootClientId: 'missing' }))@@ -1796,10 +1797,10 @@ describe('Bot canonical Session lifecycle', () => {.get(chatId);expect(h.sqlite!.prepare(`SELECT s.source, s.working_dir AS workingDir, s.permission_mode AS permissionMode,l.role, l.route_key AS routeKey FROM sessions s JOIN bot_session_links l ON l.session_id = s.id WHERE s.id = ?`)- .get(first.sessionId)).toEqual({ source: 'bot', role: 'thread', routeKey: `dm-thread:${chatId}:root-1`, ...canonical as object });+ .get(first.sessionId)).toEqual({ source: 'bot', role: 'thread', routeKey: `dm-thread:${chatId}:bot-opening:inv-1`, ...canonical as object });const [profile] = await invoke('local-db:bots:list', undefined);expect(profile.sessions).toEqual(expect.arrayContaining([expect.objectContaining({- id: first.sessionId, kind: 'thread', threadRoot: { rootSessionId: chatId, rootClientId: 'root-1' },+ id: first.sessionId, kind: 'thread', threadRoot: { rootSessionId: chatId, rootClientId: 'bot-opening:inv-1' },})]));// Opened and left empty: no entry under the root yet.@@ -1807,7 +1808,7 @@ describe('Bot canonical Session lifecycle', () => {insert.run('t-1', 'topic-1', first.sessionId, 'user', JSON.stringify({ text: 'Shorter, please' }), 2_000);insert.run('t-2', 'topic-2', first.sessionId, 'assistant', JSON.stringify('Here is a shorter draft.'), 3_000);await expect(invoke('local-db:bots:dm-thread:list', { botId: 'bot-1', rootSessionId: chatId })).resolves.toEqual([{- sessionId: first.sessionId, rootSessionId: chatId, rootClientId: 'root-1',+ sessionId: first.sessionId, rootSessionId: chatId, rootClientId: 'bot-opening:inv-1',replyCount: 2, lastReplyAt: 3_000, lastAssistantAt: 3_000, createdAt: expect.any(Number),}]);diff --git a/apps/desktop/src/main/localDb/ipc/bots.ts b/apps/desktop/src/main/localDb/ipc/bots.tsindex 1e716f4986..45dc9b12df 100644--- a/apps/desktop/src/main/localDb/ipc/bots.ts+++ b/apps/desktop/src/main/localDb/ipc/bots.ts@@ -2654,7 +2654,7 @@ export function registerBotIpc(): void {const body = requireObject(raw, 'body');const botId = readText(body.botId, 'botId', 128, true);const rootSessionId = readText(body.rootSessionId, 'rootSessionId', 128, true);- const rootClientId = readText(body.rootClientId, 'rootClientId', 128, true);+ const rootClientId = readText(body.rootClientId, 'rootClientId', 256, true);if (!parseBotDmThreadRouteKey(`dm-thread:${rootSessionId}:${rootClientId}`)) {throwIpcError('INVALID_PARAMS', '话题的原消息无效');}diff --git a/apps/desktop/src/renderer/features/bots/BotDmThreadHost.tsx b/apps/desktop/src/renderer/features/bots/BotDmThreadHost.tsxindex 9c5d2f391a..029b136268 100644--- a/apps/desktop/src/renderer/features/bots/BotDmThreadHost.tsx+++ b/apps/desktop/src/renderer/features/bots/BotDmThreadHost.tsx@@ -84,17 +84,20 @@ export function BotDmThreadHost({ enabled, bot, rootSessionId, mentions, childrereload();}, [reload, setSearchParams]);- // A link or notification names the topic's Session; open it once its root is known. Each link- // opens at most once, so closing the panel (which drops the parameter a render later) sticks.+ // A link or notification names the topic's Session: open it, or switch to it from another open+ // topic, once its root is known. Each link value is followed once, so closing the panel (which+ // drops the parameter a render later) sticks; once the parameter is gone the same link works again.const linked = searchParams.get(BOT_DM_THREAD_PARAM);const handledLink = useRef<string | null>(null);+ const openSessionId = open?.state.kind === 'ready' ? open.state.sessionId : null;useEffect(() => {- if (!enabled || !linked || open || handledLink.current === linked) return;+ if (!linked) { handledLink.current = null; return; }+ if (!enabled || handledLink.current === linked) return;const projection = topicProjections.find((session) => session.id === linked);if (!projection?.threadRoot) return;handledLink.current = linked;- ensure(projection.threadRoot.rootClientId);- }, [enabled, ensure, linked, open, topicProjections]);+ if (openSessionId !== linked) ensure(projection.threadRoot.rootClientId);+ }, [enabled, ensure, linked, openSessionId, topicProjections]);const value = useMemo<BotDmThreadsValue | null>(() => enabled ? {botId: bot.id,diff --git a/apps/desktop/src/renderer/features/bots/BotDmThreadViews.tsx b/apps/desktop/src/renderer/features/bots/BotDmThreadViews.tsxindex 7a1454d022..7f508019f2 100644--- a/apps/desktop/src/renderer/features/bots/BotDmThreadViews.tsx+++ b/apps/desktop/src/renderer/features/bots/BotDmThreadViews.tsx@@ -56,9 +56,20 @@ export function BotDmThreadPanel({ bot, rootSessionId, rootClientId, state, mentconst panelRef = useRef<HTMLElement>(null);const threadSessionId = state.kind === 'ready' ? state.sessionId : null;+ // Seen only while the window shows it: a reply that arrived while hidden is read on return.useEffect(() => {- if (!threadSessionId || document.visibilityState !== 'visible') return;- markBotRead(botDmThreadReadKey(threadSessionId), Math.max(Date.now(), lastAssistantAt ?? 0));+ if (!threadSessionId) return;+ const markRead = () => {+ if (document.visibilityState !== 'visible') return;+ markBotRead(botDmThreadReadKey(threadSessionId), Math.max(Date.now(), lastAssistantAt ?? 0));+ };+ markRead();+ document.addEventListener('visibilitychange', markRead);+ window.addEventListener('focus', markRead);+ return () => {+ document.removeEventListener('visibilitychange', markRead);+ window.removeEventListener('focus', markRead);+ };}, [lastAssistantAt, threadSessionId]);useEffect(() => {diff --git a/apps/desktop/src/shared/botDmThread.ts b/apps/desktop/src/shared/botDmThread.tsindex b8bef66f70..9f6482a395 100644--- a/apps/desktop/src/shared/botDmThread.ts+++ b/apps/desktop/src/shared/botDmThread.ts@@ -8,11 +8,15 @@export const BOT_DM_THREAD_ROUTE_PREFIX = 'dm-thread:';-/** clientId 与 Session id 都不含冒号;含冒号的输入不构成合法话题。 */-const ROUTE_PART = /^[^:\s]{1,128}$/;+/**+ * Session id 不含冒号,所以第一个冒号之后都是 clientId;clientId 本身可以带冒号+ * (例如伙伴开场白 `bot-opening:<id>`、定时任务结果 `schedule-result:<id>`)。+ */+const SESSION_PART = /^[^:\s]{1,128}$/;+const CLIENT_PART = /^\S{1,256}$/;export function botDmThreadRouteKey(rootSessionId: string, rootClientId: string): string {- if (!ROUTE_PART.test(rootSessionId) || !ROUTE_PART.test(rootClientId)) {+ if (!SESSION_PART.test(rootSessionId) || !CLIENT_PART.test(rootClientId)) {throw new Error('Invalid private-chat topic root');}return `${BOT_DM_THREAD_ROUTE_PREFIX}${rootSessionId}:${rootClientId}`;@@ -22,9 +26,12 @@ export function parseBotDmThreadRouteKey(routeKey: string | null | undefined,): { rootSessionId: string; rootClientId: string } | null {if (!routeKey?.startsWith(BOT_DM_THREAD_ROUTE_PREFIX)) return null;- const [rootSessionId, rootClientId, ...rest] = routeKey.slice(BOT_DM_THREAD_ROUTE_PREFIX.length).split(':');- if (rest.length > 0 || !rootSessionId || !rootClientId) return null;- if (!ROUTE_PART.test(rootSessionId) || !ROUTE_PART.test(rootClientId)) return null;+ const rest = routeKey.slice(BOT_DM_THREAD_ROUTE_PREFIX.length);+ const split = rest.indexOf(':');+ if (split <= 0) return null;+ const rootSessionId = rest.slice(0, split);+ const rootClientId = rest.slice(split + 1);+ if (!SESSION_PART.test(rootSessionId) || !CLIENT_PART.test(rootClientId)) return null;return { rootSessionId, rootClientId };}