Skip to content

Commit c8e9d41

Browse files
committed
fix(line): surface partial delivery when rich or media send fails alongside text
1 parent bdc98f4 commit c8e9d41

3 files changed

Lines changed: 113 additions & 10 deletions

File tree

‎extensions/line/src/auto-reply-delivery.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,72 @@ describe("deliverLineAutoReply", () => {
248248
expect(pushOrder).toBeLessThan(replyOrder);
249249
});
250250

251+
it("surfaces a visible partial delivery when a rich bubble fails alongside quick-reply text", async () => {
252+
// Quick replies attach to the trailing text bubble, so the flex/media send
253+
// (pushMessagesLine) runs first. If it fails, the text still reaches the
254+
// user, but the loss must be reported instead of a silent full success.
255+
const createTextMessageWithQuickReplies = vi.fn((text: string) => ({
256+
type: "text" as const,
257+
text,
258+
quickReply: { items: ["A"] },
259+
}));
260+
const lineData = {
261+
flexMessage: { altText: "Card", contents: { type: "bubble" } },
262+
quickReplies: ["A"],
263+
};
264+
const failingPush = vi.fn(async () => {
265+
throw new Error("push failed");
266+
});
267+
const { deps, replyMessageLine } = createDeps({
268+
createTextMessageWithQuickReplies:
269+
createTextMessageWithQuickReplies as LineAutoReplyDeps["createTextMessageWithQuickReplies"],
270+
pushMessagesLine: failingPush as LineAutoReplyDeps["pushMessagesLine"],
271+
});
272+
273+
const result = await deliverLineAutoReply({
274+
...baseDeliveryParams,
275+
payload: { text: "hello", channelData: { line: lineData } },
276+
lineData,
277+
deps,
278+
});
279+
280+
// The partial failure is returned (not thrown) so the caller can adopt the
281+
// consumed reply-token state before surfacing it. visibleReplySent is the
282+
// signal dispatch uses to keep the sent text yet still report the failure.
283+
expect(result.visiblePartialDeliveryError).toMatchObject({ visibleReplySent: true });
284+
expect(result.replyTokenUsed).toBe(true);
285+
// Text still reached the user over the reply token despite the rich failure.
286+
expect(replyMessageLine).toHaveBeenCalledTimes(1);
287+
expect(failingPush).toHaveBeenCalledTimes(1);
288+
});
289+
290+
it("surfaces a visible partial delivery when a rich bubble fails after text without quick replies", async () => {
291+
// Without quick replies the text goes first and the rich bubble follows; a
292+
// failed rich push must surface the same visible partial delivery so the
293+
// sibling path stays consistent with the quick-reply branch.
294+
const lineData = {
295+
flexMessage: { altText: "Card", contents: { type: "bubble" } },
296+
};
297+
const failingPush = vi.fn(async () => {
298+
throw new Error("push failed");
299+
});
300+
const { deps, replyMessageLine } = createDeps({
301+
pushMessagesLine: failingPush as LineAutoReplyDeps["pushMessagesLine"],
302+
});
303+
304+
const result = await deliverLineAutoReply({
305+
...baseDeliveryParams,
306+
payload: { text: "hello", channelData: { line: lineData } },
307+
lineData,
308+
deps,
309+
});
310+
311+
expect(result.visiblePartialDeliveryError).toMatchObject({ visibleReplySent: true });
312+
expect(result.replyTokenUsed).toBe(true);
313+
expect(replyMessageLine).toHaveBeenCalledTimes(1);
314+
expect(failingPush).toHaveBeenCalledTimes(1);
315+
});
316+
251317
it("falls back to push when reply token delivery fails", async () => {
252318
const lineData = {
253319
flexMessage: { altText: "Card", contents: { type: "bubble" } },

‎extensions/line/src/auto-reply-delivery.ts‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ export async function deliverLineAutoReply(params: {
5353
cfg: OpenClawConfig;
5454
textLimit: number;
5555
deps: LineAutoReplyDeps;
56-
}): Promise<{ replyTokenUsed: boolean }> {
56+
}): Promise<{ replyTokenUsed: boolean; visiblePartialDeliveryError?: Error }> {
5757
const { payload, lineData, replyToken, accountId, to, textLimit, deps } = params;
5858
let replyTokenUsed = params.replyTokenUsed;
59+
// Set when the text reached the user but a rich/media bubble did not. Returned
60+
// (not thrown) so the caller adopts the consumed reply-token state before it
61+
// surfaces the failure.
62+
let visiblePartialDeliveryError: Error | undefined;
5963

6064
const pushLineMessages = async (messages: messagingApi.Message[]): Promise<void> => {
6165
if (messages.length === 0) {
@@ -141,11 +145,17 @@ export async function deliverLineAutoReply(params: {
141145

142146
if (chunks.length > 0) {
143147
const hasRichOrMedia = richMessages.length > 0 || mediaMessages.length > 0;
144-
if (hasQuickReplies && hasRichOrMedia) {
148+
// Quick replies attach to the trailing message, so when both are present the
149+
// rich/media bubbles must go out before the quick-reply text. Capture a
150+
// failure instead of swallowing it: the text still sends below, but a lost
151+
// rich/media bubble must surface as a partial delivery, not silent success.
152+
const sendRichBeforeText = hasQuickReplies && hasRichOrMedia;
153+
let richMediaError: unknown;
154+
if (sendRichBeforeText) {
145155
try {
146156
await sendLineMessages([...richMessages, ...mediaMessages], false);
147157
} catch (err) {
148-
deps.onReplyError?.(err);
158+
richMediaError = err;
149159
}
150160
}
151161
const { replyTokenUsed: nextReplyTokenUsed } = await deps.sendLineReplyChunks({
@@ -162,12 +172,27 @@ export async function deliverLineAutoReply(params: {
162172
createTextMessageWithQuickReplies: deps.createTextMessageWithQuickReplies,
163173
});
164174
replyTokenUsed = nextReplyTokenUsed;
165-
if (!hasQuickReplies || !hasRichOrMedia) {
166-
await sendLineMessages(richMessages, false);
167-
if (mediaMessages.length > 0) {
168-
await sendLineMessages(mediaMessages, false);
175+
if (!sendRichBeforeText) {
176+
try {
177+
await sendLineMessages(richMessages, false);
178+
if (mediaMessages.length > 0) {
179+
await sendLineMessages(mediaMessages, false);
180+
}
181+
} catch (err) {
182+
richMediaError = err;
169183
}
170184
}
185+
if (richMediaError !== undefined) {
186+
// Tag the failure as a visible partial delivery so the caller reports it
187+
// instead of silent success; visibleReplySent marks the text as visibly
188+
// delivered so the foreground fallback/freshness path does not re-send it
189+
// (see isVisiblePartialDeliveryError in src/auto-reply/dispatch.ts).
190+
const partialError =
191+
richMediaError instanceof Error
192+
? richMediaError
193+
: new Error("LINE rich or media message send failed", { cause: richMediaError });
194+
visiblePartialDeliveryError = Object.assign(partialError, { visibleReplySent: true });
195+
}
171196
} else {
172197
const combined = [...richMessages, ...mediaMessages];
173198
if (hasQuickReplies && combined.length === 0) {
@@ -200,5 +225,8 @@ export async function deliverLineAutoReply(params: {
200225
}
201226
}
202227

203-
return { replyTokenUsed };
228+
return {
229+
replyTokenUsed,
230+
...(visiblePartialDeliveryError ? { visiblePartialDeliveryError } : {}),
231+
};
204232
}

‎extensions/line/src/monitor.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export async function monitorLineProvider(
258258
}).catch(() => {});
259259
}
260260

261-
const { replyTokenUsed: nextReplyTokenUsed } = await deliverLineAutoReply({
261+
const deliveryResult = await deliverLineAutoReply({
262262
payload,
263263
lineData,
264264
to: ctxPayload.From,
@@ -288,7 +288,16 @@ export async function monitorLineProvider(
288288
},
289289
},
290290
});
291-
replyTokenUsed = nextReplyTokenUsed;
291+
replyTokenUsed = deliveryResult.replyTokenUsed;
292+
293+
if (deliveryResult.visiblePartialDeliveryError) {
294+
// Text reached the user but a rich/media bubble did not.
295+
// Surface the tagged partial failure after adopting the
296+
// consumed reply-token state so later blocks in this turn
297+
// route correctly; recordChannelRuntimeState is skipped
298+
// because this delivery was not a clean success.
299+
throw deliveryResult.visiblePartialDeliveryError;
300+
}
292301

293302
recordChannelRuntimeState({
294303
channel: "line",

0 commit comments

Comments
 (0)