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): 撤回空飞书开场卡后恢复话题回复锚点

Dashc3dd2293a0cd
diff --git a/packages/lizi-im/src/feishu/__tests__/outboundLane.test.ts b/packages/lizi-im/src/feishu/__tests__/outboundLane.test.ts
index 6b2cfaa676..885ee144fa 100644
--- a/packages/lizi-im/src/feishu/__tests__/outboundLane.test.ts
+++ b/packages/lizi-im/src/feishu/__tests__/outboundLane.test.ts
@@ -4,6 +4,7 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
const larkMocks = vi.hoisted(() => {
+ const request = vi.fn(async () => ({ code: 0 }));
const list = vi.fn<() => Promise<{ code: number; data: { items: Array<{
message_id: string; thread_id: string; chat_id: string; deleted?: boolean;
}> } }>>(async () => ({ code: 0, data: { items: [] } }));
@@ -63,11 +64,12 @@ const larkMocks = vi.hoisted(() => {
return Promise.resolve({ data: { items: [] } });
},
);
- return { list, create, reply, deleteMessage, getMessage, patch, replyOwners, getOwners, deleteOwners };
+ return { request, list, create, reply, deleteMessage, getMessage, patch, replyOwners, getOwners, deleteOwners };
});
vi.mock('@larksuiteoapi/node-sdk', () => ({
Client: class {
+ request = larkMocks.request;
im = {
v1: {
message: {
@@ -103,6 +105,7 @@ vi.mock('../moduleScope.js', async (importOriginal) => ({
}));
import * as outbound from '../outbound.js';
+import { start } from '../streamingText.js';
const creds = { appId: 'cli_test', appSecret: 'secret', service: 'feishu' as const };
const otherCreds = { appId: 'cli_other_account', appSecret: 'secret', service: 'feishu' as const };
@@ -163,6 +166,49 @@ describe('feishu outbound lane routing', () => {
expect(larkMocks.create).not.toHaveBeenCalled();
});
+ // PR #6330: real streaming final + outbound reply, rather than a mocked
+ // rearm call. Existing tests covered pending openers but not claimed cards.
+ // Unit tier: empty finalize followed by background text, no real I/O/waits.
+ it.each(['same', 'changed-before', 'changed-during', 'new-round', 'delete-failed'])(
+ 'empty claimed opener preserves the next reply target (%s)', async (transition) => {
+ const lane = 'g/oc_group1/omt_t1';
+ outbound.pushReplyAnchor(lane, 'om_opener');
+ outbound.pushPatchableOpener(lane, 'om_opener', 'om_trigger');
+ const handle = await start(lane);
+ const replaceAccount = () => {
+ rebindFresh(otherCreds);
+ outbound.pushReplyAnchor(lane, 'om_new');
+ outbound.pushPatchableOpener(lane, 'om_new', 'om_new_trigger');
+ outbound.claimPatchableOpener(lane);
+ };
+ if (transition === 'changed-before') replaceAccount();
+ if (transition === 'changed-during') larkMocks.request.mockImplementationOnce(async () => {
+ replaceAccount();
+ return { code: 0 };
+ });
+ if (transition === 'new-round') larkMocks.request.mockImplementationOnce(async () => {
+ outbound.pushReplyAnchor(lane, 'om_new');
+ outbound.pushPatchableOpener(lane, 'om_new', 'om_new_trigger');
+ outbound.claimPatchableOpener(lane);
+ return { code: 0 };
+ });
+ if (transition === 'delete-failed') {
+ larkMocks.request.mockRejectedValueOnce(new Error('delete failed'));
+ await expect(handle.finalize('')).rejects.toThrow('delete failed');
+ } else {
+ await handle.finalize('');
+ }
+ if (transition === 'changed-before') expect(larkMocks.request).not.toHaveBeenCalled();
+ await outbound.sendText(lane, 'background result');
+ const expected = transition === 'same' ? 'om_trigger'
+ : transition === 'delete-failed' ? 'om_opener' : 'om_new';
+ expect(larkMocks.reply).toHaveBeenLastCalledWith(expect.objectContaining({
+ path: { message_id: expected },
+ data: expect.objectContaining(transition === 'same' ? { reply_in_thread: true } : {}),
+ }));
+ },
+ );
+
it('restores a cold topic anchor before streaming a background result', async () => {
larkMocks.list.mockResolvedValueOnce({ code: 0, data: { items: [
{ message_id: 'om_deleted', thread_id: 'omt_t1', chat_id: 'oc_group1', deleted: true },
diff --git a/packages/lizi-im/src/feishu/outbound.ts b/packages/lizi-im/src/feishu/outbound.ts
index d6ade5f556..b1e4d2428d 100644
--- a/packages/lizi-im/src/feishu/outbound.ts
+++ b/packages/lizi-im/src/feishu/outbound.ts
@@ -592,7 +592,11 @@ export function getOpenerTrigger(laneUserId: string): string | undefined {
* reply_in_thread 标记)。兜底发送向触发消息 reply 且 reply_in_thread=true,
* 仍落回话题 — 而不是向已删除的开场白卡 reply 失败。无触发记录返回 false。
*/
-export function rearmAnchorToTrigger(laneUserId: string): boolean {
+export function rearmAnchorToTrigger(laneUserId: string, expectedOpenerId?: string): boolean {
+ // A delayed recall must not replace the anchor claimed by a newer round.
+ if (expectedOpenerId !== undefined && S().laneAnchors.get(laneUserId)?.held !== expectedOpenerId) {
+ return false;
+ }
const triggerId = S().openerTriggers.get(laneUserId);
S().openerTriggers.delete(laneUserId);
if (!triggerId) return false;
diff --git a/packages/lizi-im/src/feishu/streamingText.ts b/packages/lizi-im/src/feishu/streamingText.ts
index 2857a392b4..df06b5605f 100644
--- a/packages/lizi-im/src/feishu/streamingText.ts
+++ b/packages/lizi-im/src/feishu/streamingText.ts
@@ -32,6 +32,8 @@ import {
sendText,
claimPatchableOpener,
recallMessage,
+ getAccountEpoch,
+ rearmAnchorToTrigger,
} from './outbound.js';
import { buildMarkdownCardV2, buildMixedMarkdownCardV2 } from './cards.js';
import { resolveFeishuMediaUrl } from './mediaCache.js';
@@ -118,7 +120,7 @@ class FeishuStreamingTextHandle implements StreamingTextHandle {
*/
private extraImageAbsPaths: string[] = [];
- constructor(messageId: string, openId: string, initial: string, private readonly opts?: { threadTs?: string }) {
+ constructor(messageId: string, openId: string, initial: string, private readonly opts?: { threadTs?: string }, private readonly openerEpoch?: number) {
this.messageId = messageId;
this.openId = openId;
// `initial` is what's currently DISPLAYED in feishu (e.g. "🧠 思考中...").
@@ -225,7 +227,13 @@ class FeishuStreamingTextHandle implements StreamingTextHandle {
const log = getLog();
const text = this.buffer;
if (!text.trim() && this.extraImageAbsPaths.length === 0) {
+ // A claimed opener is also the topic's reply anchor. Only its owning
+ // account/round may restore that anchor after a successful recall.
+ if (this.openerEpoch !== undefined && getAccountEpoch() !== this.openerEpoch) return;
await recallMessage(this.messageId);
+ if (this.openerEpoch !== undefined && getAccountEpoch() === this.openerEpoch) {
+ rearmAnchorToTrigger(this.openId, this.messageId);
+ }
return;
}
@@ -364,7 +372,7 @@ export async function start(
// 认领后直接 patch, 不再新建一条「开个话题」占位回复。
const claimed = opts?.threadTs ? null : claimPatchableOpener(openId);
if (claimed) {
- return new FeishuStreamingTextHandle(claimed, openId, initial, opts);
+ return new FeishuStreamingTextHandle(claimed, openId, initial, opts, getAccountEpoch());
}
const { messageId } = await sendCardRaw(openId, buildMarkdownCardV2(initial), opts);
return new FeishuStreamingTextHandle(messageId, openId, initial, opts);