Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(feishu): retain bot scope across streaming reply callbacks (#6573)
Chris Zhang8c8c0f025bd7
diff --git a/packages/lizi-im/src/feishu/__tests__/outboundLane.test.ts b/packages/lizi-im/src/feishu/__tests__/outboundLane.test.tsindex 1160c94b80..c065a84f10 100644--- a/packages/lizi-im/src/feishu/__tests__/outboundLane.test.ts+++ b/packages/lizi-im/src/feishu/__tests__/outboundLane.test.ts@@ -1,7 +1,7 @@/*** 群 lane 出站路由: open_id / chat_id / reply 三分支与回挂锚点领取语义。*/-import { beforeEach, describe, expect, it, vi } from 'vitest';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';const larkMocks = vi.hoisted(() => {const request = vi.fn(async () => ({ code: 0 }));@@ -106,6 +106,7 @@ vi.mock('../moduleScope.js', async (importOriginal) => ({import * as outbound from '../outbound.js';import { start } from '../streamingText.js';+import { createFeishuScope, runInScope } from '../moduleScope.js';const creds = { appId: 'cli_test', appSecret: 'secret', service: 'feishu' as const };const otherCreds = { appId: 'cli_other_account', appSecret: 'secret', service: 'feishu' as const };@@ -119,11 +120,63 @@ function rebindFresh(next = creds): void {}describe('feishu outbound lane routing', () => {+ afterEach(() => {+ vi.useRealTimers();+ });+beforeEach(() => {vi.clearAllMocks();rebindFresh();});+ // Regression: companion streaming handles escape FeishuIM's ALS boundary when+ // an already-running agent emits events. Existing lane cases stayed in the+ // default scope. Exercise real start/outbound routing, with only the SDK faked+ // (unit tier): an unbound or different caller must never receive the patches.+ it.each([false, true])(+ 'keeps streamed and final cards on their creating connection (caller connected=%s)',+ async (callerConnected) => {+ vi.useFakeTimers();+ if (!callerConnected) outbound.unbindClient();+ const companion = createFeishuScope({ host: null, secretKeyPrefix: 'companion' });+ const handle = await runInScope(companion, () => {+ outbound.bindClient(otherCreds);+ return start('ou_companion_owner');+ });+ const creator = larkMocks.create.mock.contexts.at(-1);+ handle.append('draft');+ await vi.advanceTimersByTimeAsync(1500);+ expect(larkMocks.patch).toHaveBeenCalledTimes(1);+ expect(larkMocks.patch.mock.contexts[0]).toBe(creator);++ handle.replace('revised');+ await vi.advanceTimersByTimeAsync(1500);+ await handle.finalize('finished');+ expect(larkMocks.patch).toHaveBeenCalledTimes(3);+ for (const client of larkMocks.patch.mock.contexts) expect(client).toBe(creator);+ expect(larkMocks.patch).toHaveBeenLastCalledWith({+ path: { message_id: handle.messageId },+ data: { content: expect.stringContaining('finished') },+ });+ },+ );++ it('recalls an empty companion reply in its own scope even from another connection', async () => {+ const companion = createFeishuScope({ host: null, secretKeyPrefix: 'companion' });+ const handle = await runInScope(companion, () => {+ outbound.bindClient(otherCreds);+ return start('ou_companion_owner');+ });+ await handle.finalize('');+ expect(larkMocks.request.mock.contexts.at(-1)).toBe(+ runInScope(companion, () => outbound.getBoundClient()),+ );+ expect(larkMocks.request).toHaveBeenLastCalledWith({+ method: 'DELETE',+ url: `open-apis/im/v1/messages/${handle.messageId}`,+ });+ });+it('sends p2p messages via open_id unchanged', async () => {await outbound.sendText('ou_someone', 'hi');expect(larkMocks.create).toHaveBeenCalledWith(diff --git a/packages/lizi-im/src/feishu/streamingText.ts b/packages/lizi-im/src/feishu/streamingText.tsindex df06b5605f..9f9f8cd302 100644--- a/packages/lizi-im/src/feishu/streamingText.ts+++ b/packages/lizi-im/src/feishu/streamingText.ts@@ -37,7 +37,7 @@ import {} from './outbound.js';import { buildMarkdownCardV2, buildMixedMarkdownCardV2 } from './cards.js';import { resolveFeishuMediaUrl } from './mediaCache.js';-import { getHost, getLog } from './moduleScope.js';+import { bindScope, getHost, getLog, getScope } from './moduleScope.js';import { messages as transportMessages } from './messages.js';import type { StreamingTextHandle } from '../types.js';// xdt-* 引用解析抽到渠道无关模块(slack streamingText 共用同一套语义)@@ -129,6 +129,13 @@ class FeishuStreamingTextHandle implements StreamingTextHandle {// to it (otherwise the placeholder permanently prefixes the message).this.buffer = '';this.flushed = initial;+ // The handle outlives FeishuIM.startStreamingText's ALS call. Agent events+ // may arrive from outside that scope (or another bot's), so bind both I/O+ // paths to the connection that created the card, including timer callbacks,+ // media uploads, empty-reply recalls and delivery-failure fallbacks.+ const scope = getScope();+ this.flushIntermediate = bindScope(scope, this.flushIntermediate.bind(this));+ this.doFinalize = bindScope(scope, this.doFinalize.bind(this));}append(delta: string): void {