Skip to content

Commit 4df0e10

Browse files
authored
fix(feishu): back off failed streaming card starts
1 parent d37f165 commit 4df0e10

4 files changed

Lines changed: 157 additions & 10 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Docs: https://docs.openclaw.ai
8989

9090
### Fixes
9191

92+
- Feishu: back off streaming-card creation after HTTP 400 startup failures, so unsupported card setups fall back without delaying every message. Fixes #56981. Thanks @JinnanDuan.
9293
- Feishu: suppress duplicate final card delivery when idle closes a streaming card before the final payload arrives. (#68491) Thanks @MoerAI.
9394
- Signal: preserve sender attachment filenames and resolve missing MIME types from those filenames, so Linux `signal-cli` voice notes without `contentType` still enter audio transcription. Fixes #48614. Thanks @mindfury.
9495
- Telegram/agents: suppress the phantom "Agent couldn't generate a response" fallback after a reply was already committed through the messaging tool. (#70623) Thanks @chinar-amrutkar.

‎extensions/feishu/openclaw.plugin.json‎

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,102 @@
1212
"skills": ["./skills"],
1313
"configSchema": {
1414
"type": "object",
15-
"additionalProperties": false,
16-
"properties": {}
15+
"additionalProperties": true,
16+
"$defs": {
17+
"secretRef": {
18+
"type": "object",
19+
"additionalProperties": false,
20+
"properties": {
21+
"source": {
22+
"type": "string",
23+
"enum": ["env", "file", "exec"]
24+
},
25+
"provider": { "type": "string" },
26+
"id": { "type": "string" }
27+
},
28+
"required": ["source", "provider", "id"]
29+
},
30+
"secretInput": {
31+
"anyOf": [{ "type": "string", "minLength": 1 }, { "$ref": "#/$defs/secretRef" }]
32+
},
33+
"account": {
34+
"type": "object",
35+
"additionalProperties": true,
36+
"properties": {
37+
"enabled": { "type": "boolean" },
38+
"name": { "type": "string" },
39+
"appId": { "type": "string" },
40+
"appSecret": { "$ref": "#/$defs/secretInput" },
41+
"encryptKey": { "$ref": "#/$defs/secretInput" },
42+
"verificationToken": { "$ref": "#/$defs/secretInput" },
43+
"domain": {
44+
"anyOf": [
45+
{
46+
"type": "string",
47+
"enum": ["feishu", "lark"]
48+
},
49+
{
50+
"type": "string",
51+
"format": "uri"
52+
}
53+
]
54+
},
55+
"connectionMode": {
56+
"type": "string",
57+
"enum": ["websocket", "webhook"]
58+
},
59+
"renderMode": {
60+
"type": "string",
61+
"enum": ["auto", "raw", "card"]
62+
},
63+
"streaming": { "type": "boolean" },
64+
"replyInThread": {
65+
"type": "string",
66+
"enum": ["disabled", "enabled"]
67+
},
68+
"typingIndicator": { "type": "boolean" }
69+
}
70+
}
71+
},
72+
"properties": {
73+
"enabled": { "type": "boolean" },
74+
"defaultAccount": { "type": "string" },
75+
"appId": { "type": "string" },
76+
"appSecret": { "$ref": "#/$defs/secretInput" },
77+
"encryptKey": { "$ref": "#/$defs/secretInput" },
78+
"verificationToken": { "$ref": "#/$defs/secretInput" },
79+
"domain": {
80+
"anyOf": [
81+
{
82+
"type": "string",
83+
"enum": ["feishu", "lark"]
84+
},
85+
{
86+
"type": "string",
87+
"format": "uri"
88+
}
89+
]
90+
},
91+
"connectionMode": {
92+
"type": "string",
93+
"enum": ["websocket", "webhook"]
94+
},
95+
"renderMode": {
96+
"type": "string",
97+
"enum": ["auto", "raw", "card"]
98+
},
99+
"streaming": { "type": "boolean" },
100+
"replyInThread": {
101+
"type": "string",
102+
"enum": ["disabled", "enabled"]
103+
},
104+
"typingIndicator": { "type": "boolean" },
105+
"accounts": {
106+
"type": "object",
107+
"additionalProperties": {
108+
"$ref": "#/$defs/account"
109+
}
110+
}
111+
}
17112
}
18113
}

‎extensions/feishu/src/reply-dispatcher.test.ts‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,17 @@ vi.mock("./streaming-card.js", () => {
8686
};
8787
});
8888

89-
import { createFeishuReplyDispatcher } from "./reply-dispatcher.js";
89+
import {
90+
clearFeishuStreamingStartBackoffForTests,
91+
createFeishuReplyDispatcher,
92+
} from "./reply-dispatcher.js";
9093

9194
describe("createFeishuReplyDispatcher streaming behavior", () => {
9295
type ReplyDispatcherArgs = Parameters<typeof createFeishuReplyDispatcher>[0];
9396

9497
beforeEach(() => {
9598
vi.clearAllMocks();
99+
clearFeishuStreamingStartBackoffForTests();
96100
streamingInstances.length = 0;
97101
sendMediaFeishuMock.mockResolvedValue(undefined);
98102
sendStructuredCardFeishuMock.mockResolvedValue(undefined);
@@ -731,9 +735,10 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
731735
);
732736
});
733737

734-
it("recovers streaming after start() throws (HTTP 400)", async () => {
738+
it("backs off streaming retries after start() throws (HTTP 400)", async () => {
735739
const errorMock = vi.fn();
736740
let shouldFailStart = true;
741+
const nowSpy = vi.spyOn(Date, "now").mockReturnValue(1_000);
737742

738743
// Intercept streaming instance creation to make first start() reject
739744
const origPush = streamingInstances.push.bind(streamingInstances);
@@ -758,22 +763,33 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
758763
const options = createReplyDispatcherWithTypingMock.mock.calls[0]?.[0];
759764

760765
// First deliver with markdown triggers startStreaming - which will fail
761-
await options.deliver({ text: "```ts\nconst x = 1\n```" }, { kind: "block" });
766+
await options.deliver({ text: "```ts\nconst x = 1\n```" }, { kind: "final" });
762767

763768
// Wait for the async error to propagate
764769
await vi.waitFor(() => {
765770
expect(errorMock).toHaveBeenCalledWith(expect.stringContaining("streaming start failed"));
766771
});
772+
expect(streamingInstances).toHaveLength(1);
773+
expect(sendStructuredCardFeishuMock).toHaveBeenCalledTimes(1);
767774

768-
// Second deliver should create a NEW streaming session (not stuck)
775+
// Immediate next markdown reply should skip a new streaming start and
776+
// fall back directly to a normal card instead of paying the 400 latency.
769777
await options.deliver({ text: "```ts\nconst y = 2\n```" }, { kind: "final" });
770778

771-
// Two instances created: first failed, second succeeded and closed
779+
expect(streamingInstances).toHaveLength(1);
780+
expect(sendStructuredCardFeishuMock).toHaveBeenCalledTimes(2);
781+
782+
// After the short backoff expires, retry streaming so fixed permissions
783+
// or transient Feishu failures recover without a process restart.
784+
nowSpy.mockReturnValue(62_000);
785+
await options.deliver({ text: "```ts\nconst z = 3\n```" }, { kind: "final" });
786+
772787
expect(streamingInstances).toHaveLength(2);
773788
expect(streamingInstances[1].start).toHaveBeenCalled();
774789
expect(streamingInstances[1].close).toHaveBeenCalled();
775790
} finally {
776791
streamingInstances.push = origPush;
792+
nowSpy.mockRestore();
777793
}
778794
});
779795
});

‎extensions/feishu/src/reply-dispatcher.ts‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@ function shouldUseCard(text: string): boolean {
3232
* Messages older than this are likely replays after context compaction (#30418). */
3333
const TYPING_INDICATOR_MAX_AGE_MS = 2 * 60_000;
3434
const MS_EPOCH_MIN = 1_000_000_000_000;
35+
const STREAMING_START_FAILURE_BACKOFF_MS = 60_000;
36+
const streamingStartBackoffUntilByAccount = new Map<string, number>();
37+
38+
function isStreamingStartBackedOff(accountId: string, now = Date.now()): boolean {
39+
const backoffUntil = streamingStartBackoffUntilByAccount.get(accountId);
40+
if (backoffUntil === undefined) {
41+
return false;
42+
}
43+
if (backoffUntil <= now) {
44+
streamingStartBackoffUntilByAccount.delete(accountId);
45+
return false;
46+
}
47+
return true;
48+
}
49+
50+
function rememberStreamingStartFailure(accountId: string, now = Date.now()): number {
51+
const backoffUntil = now + STREAMING_START_FAILURE_BACKOFF_MS;
52+
streamingStartBackoffUntilByAccount.set(accountId, backoffUntil);
53+
return backoffUntil;
54+
}
55+
56+
export function clearFeishuStreamingStartBackoffForTests() {
57+
streamingStartBackoffUntilByAccount.clear();
58+
}
3559

3660
function normalizeEpochMs(timestamp: number | undefined): number | undefined {
3761
if (!Number.isFinite(timestamp) || timestamp === undefined || timestamp <= 0) {
@@ -266,7 +290,12 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
266290
};
267291

268292
const startStreaming = () => {
269-
if (!streamingEnabled || streamingStartPromise || streaming) {
293+
if (
294+
!streamingEnabled ||
295+
streamingStartPromise ||
296+
streaming ||
297+
isStreamingStartBackedOff(account.accountId)
298+
) {
270299
return;
271300
}
272301
streamingStartPromise = (async () => {
@@ -291,10 +320,16 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
291320
header: cardHeader,
292321
note: cardNote,
293322
});
323+
streamingStartBackoffUntilByAccount.delete(account.accountId);
294324
} catch (error) {
295-
params.runtime.error?.(`feishu: streaming start failed: ${String(error)}`);
325+
rememberStreamingStartFailure(account.accountId);
326+
params.runtime.error?.(
327+
`feishu[${account.accountId}]: streaming start failed; using non-streaming card fallback for ${
328+
STREAMING_START_FAILURE_BACKOFF_MS / 1000
329+
}s: ${String(error)}`,
330+
);
296331
streaming = null;
297-
streamingStartPromise = null; // allow retry on next deliver
332+
streamingStartPromise = null;
298333
}
299334
})();
300335
};

0 commit comments

Comments
 (0)