Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(im): 伙伴主任务出站改走自己的飞书连接

Leng Jingxue0f7543d69a7
diff --git a/apps/desktop/src/main/im/shared/botImConnectionRuntime.ts b/apps/desktop/src/main/im/shared/botImConnectionRuntime.ts
index 459f274900..f68e5c53e8 100644
--- a/apps/desktop/src/main/im/shared/botImConnectionRuntime.ts
+++ b/apps/desktop/src/main/im/shared/botImConnectionRuntime.ts
@@ -275,6 +275,23 @@ export function getBotImConnectionOrchestrator(id: string): ImOrchestrator | und
return instances.get(id)?.transport.orchestrator;
}
+/**
+ * 某位伙伴当前在线的飞书连接(伙伴主任务出站用)。
+ *
+ * 伙伴主任务(role='canonical')与桌面共用、不带专线的 `feishuChatId`, 所以非飞书入站轮次
+ * (后台任务回执 / 心跳 / scheduler)拿不到逐轮作用域。这些轮次必须仍走伙伴自己的连接,
+ * 不能回落到设置页那份可能已被清空的个人 bot —— 否则 `getOwnerOpenId()` 为 null 时
+ * `send_message_to_user` / `send_file_to_user` 会误报 NO_CHAT_CONTEXT。
+ */
+export function getBotFeishuTransport(botId: string): FeishuIM | null {
+ for (const record of listBotImConnectionsForBot(botId)) {
+ if (record.channel !== 'feishu') continue;
+ const live = instances.get(record.id);
+ if (live?.transport.kind === 'feishu') return live.transport.im;
+ }
+ return null;
+}
+
async function readBotProfileStatus(botId: string): Promise<string | null> {
const [row] = await getDbClient()
.drizzle.select({ status: botProfiles.status })
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/feishuTransportRouting.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/feishuTransportRouting.test.ts
new file mode 100644
index 0000000000..a54738f20f
--- /dev/null
+++ b/apps/desktop/src/main/mcp-integrations/__tests__/feishuTransportRouting.test.ts
@@ -0,0 +1,35 @@
+import { describe, expect, it } from 'vitest';
+
+import { selectFeishuTransport } from '../feishuTransportRouting';
+
+const personal = { id: 'personal' };
+const companion = { id: 'companion' };
+const lane = { id: 'lane' };
+
+interface Transport { id: string }
+
+function pick(input: Partial<Parameters<typeof selectFeishuTransport<Transport>>[0]>) {
+ return selectFeishuTransport<Transport>({
+ turnScopeIm: null,
+ companionBotId: null,
+ companionIm: null,
+ personalIm: personal,
+ ...input,
+ }).id;
+}
+
+describe('cindy_feishu_bot transport routing', () => {
+ it('uses the connection that triggered this Feishu turn', () => {
+ expect(pick({ turnScopeIm: lane, companionBotId: 'bot', companionIm: companion })).toBe('lane');
+ });
+
+ it('keeps a companion session on its own connection without a turn scope', () => {
+ // 后台任务回执 / 心跳 / 桌面输入触发的轮次没有逐轮作用域; 不能回落到设置页那份。
+ expect(pick({ companionBotId: 'bot', companionIm: companion })).toBe('companion');
+ });
+
+ it('falls back to the personal bot for non-companion sessions', () => {
+ expect(pick({})).toBe('personal');
+ expect(pick({ companionBotId: 'bot', companionIm: null })).toBe('personal');
+ });
+});
diff --git a/apps/desktop/src/main/mcp-integrations/feishuTransportRouting.ts b/apps/desktop/src/main/mcp-integrations/feishuTransportRouting.ts
new file mode 100644
index 0000000000..b854a635a2
--- /dev/null
+++ b/apps/desktop/src/main/mcp-integrations/feishuTransportRouting.ts
@@ -0,0 +1,23 @@
+/**
+ * cindy_feishu_bot 出站选哪条飞书连接。
+ *
+ * 顺序:
+ * 1. 本轮飞书逐轮作用域(由飞书入站消息触发的轮次);
+ * 2. 会话所属伙伴自己的飞书连接(伙伴主任务的后台任务回执 / 心跳 / 桌面输入轮次);
+ * 3. 设置页那份个人 bot(hook 会话、桌面任务的「飞书通知我」)。
+ *
+ * 第 2 步不能省: 伙伴主任务不带专线的 `feishuChatId`, 个人 bot 移交给伙伴后那份又被清空,
+ * 少了这一步就会拿设置页的空 owner 去出站, 误报 NO_CHAT_CONTEXT。
+ */
+export function selectFeishuTransport<T>(
+ input: {
+ turnScopeIm: T | null;
+ companionBotId: string | null;
+ companionIm: T | null;
+ personalIm: T;
+ },
+): T {
+ if (input.turnScopeIm) return input.turnScopeIm;
+ if (input.companionBotId && input.companionIm) return input.companionIm;
+ return input.personalIm;
+}
diff --git a/apps/desktop/src/main/mcp-integrations/mcp-providers.ts b/apps/desktop/src/main/mcp-integrations/mcp-providers.ts
index 72ab105802..37fd1402af 100644
--- a/apps/desktop/src/main/mcp-integrations/mcp-providers.ts
+++ b/apps/desktop/src/main/mcp-integrations/mcp-providers.ts
@@ -43,6 +43,8 @@ import { getAndroidMcpDeps } from './android.js';
import { getBrowserMcpDeps } from './browser.js';
import { getComputerMcpDeps } from './computer.js';
import { feishuIm, wechatIm } from '../im';
+import { getBotFeishuTransport } from '../im/shared/botImConnectionRuntime';
+import { selectFeishuTransport } from './feishuTransportRouting';
import { readChannelToolScope, type FeishuToolTransport } from '../im/shared/channelToolScope';
import { hasImChannelVendorOptions } from '../../shared/botImConnection.js';
import { sendFeishuSessionNotification } from '../im/feishu/notificationOrigin';
@@ -168,6 +170,9 @@ export interface DesktopMcpProvidersDeps {
* 当前 tool-call 所属的飞书逐轮作用域(个人 bot 或伙伴自有连接), 没有就是 null。
* 用 sessionId + sessionInstanceId 查租约: 旧实例借不到新实例的权限。
*/
+/** 会话 → 伙伴主任务归属(tool-call 高频路径); 换账号 / 换连接时整表清空。 */
+const companionBotIdBySession = new Map<string, { dbClient: object; botId: string | null }>();
+
function readFeishuTurnScope(
session: { sessionId?: string; sessionInstanceId?: string } | undefined,
) {
@@ -179,14 +184,57 @@ function readFeishuTurnScope(
}
/**
- * cindy_feishu_bot 出站走哪个 bot: 轮次由伙伴自有连接触发时走那条连接自己的 FeishuIM,
- * 否则(个人 bot 轮次 / hook 会话 / 桌面任务的「飞书通知我」)走设置页那份。2026-10-17 之前
- * 这里永远是设置页那份 —— 伙伴绑定的飞书连接里 send_message_to_user 会从别的 bot 发出或直接失败。
+ * 这个 tool-call 所属的会话是哪位伙伴(没有 ≡ 不是伙伴会话)。
+ *
+ * 伙伴主任务(role='canonical')与桌面共用、不带专线的 `feishuChatId`, 所以后台任务回执 /
+ * 心跳 / scheduler / 对话输入触发的轮次没有逐轮作用域。出站仍必须走伙伴自己的连接。
+ */
+function companionBotIdForSession(sessionId: string | undefined): string | null {
+ if (!sessionId) return null;
+ const dbClient = tryGetDbClient();
+ if (!dbClient) return null;
+ const cached = companionBotIdBySession.get(sessionId);
+ if (cached && cached.dbClient === dbClient) return cached.botId;
+ let botId: string | null = null;
+ try {
+ const row = dbClient.drizzle
+ .select({ botId: botSessionLinks.botId })
+ .from(botSessionLinks)
+ .innerJoin(sessions, eq(sessions.id, botSessionLinks.sessionId))
+ .where(and(
+ eq(botSessionLinks.sessionId, sessionId),
+ eq(sessions.source, 'bot'),
+ eq(botSessionLinks.role, 'canonical'),
+ ))
+ .limit(1)
+ .get();
+ botId = row?.botId ?? null;
+ } catch {
+ return null;
+ }
+ if (companionBotIdBySession.size > 512) companionBotIdBySession.clear();
+ companionBotIdBySession.set(sessionId, { dbClient, botId });
+ return botId;
+}
+
+/**
+ * cindy_feishu_bot 出站走哪个 bot: 飞书入站轮次走触发本轮的那条连接; 伙伴主任务
+ * (后台任务回执 / 心跳 / 桌面输入轮次)走该伙伴自己的连接; 其余(hook 会话 / 桌面
+ * 任务的「飞书通知我」)才走设置页那份。
+ *
+ * 不能把伙伴会话回落到设置页那份: 个人 bot 移交给伙伴后那份已被清空, owner 为空,
+ * `getChatId()` 会直接给 NO_CHAT_CONTEXT —— 用户明明已经私聊过机器人。
*/
function feishuTransportFor(
session: { sessionId?: string; sessionInstanceId?: string } | undefined,
): FeishuToolTransport {
- return readFeishuTurnScope(session)?.im ?? feishuIm;
+ const companionBotId = companionBotIdForSession(session?.sessionId);
+ return selectFeishuTransport({
+ turnScopeIm: readFeishuTurnScope(session)?.im ?? null,
+ companionBotId,
+ companionIm: companionBotId ? getBotFeishuTransport(companionBotId) : null,
+ personalIm: feishuIm,
+ });
}
export function createDesktopMcpProviders(deps: DesktopMcpProvidersDeps): LiziMcpProvider[] {