Skip to content

Commit 0a146d5

Browse files
steipetePeter Steinberger
andauthored
fix(line): centralize Flex alternative-text bounds (#117217)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 4c2d2d3 commit 0a146d5

7 files changed

Lines changed: 81 additions & 33 deletions

File tree

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

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
type LineAutoReplyDeps,
1111
} from "./auto-reply-delivery.test-helpers.js";
1212
import { buildLineMediaMessage } from "./outbound-media.js";
13+
import { createFlexMessage as createProviderFlexMessage } from "./send.js";
1314

1415
describe("deliverLineAutoReply", () => {
1516
it("sends text and rich messages on one reply token instead of pushing the rich bubble", async () => {
@@ -249,13 +250,47 @@ describe("deliverLineAutoReply", () => {
249250
});
250251
});
251252

252-
it("truncates flex altText on a surrogate boundary", async () => {
253-
// The emoji's surrogate pair straddles LINE's 400-char altText cap; a raw
254-
// slice used to send a lone high surrogate to the LINE API.
253+
it.each([
254+
{ label: "explicit card", extracted: false },
255+
{ label: "extracted Markdown card", extracted: true },
256+
])("preserves provider-valid $label alternative text in auto-replies", async ({ extracted }) => {
257+
const altText = "a".repeat(1200);
258+
const lineData = extracted ? {} : { flexMessage: { altText, contents: { type: "bubble" } } };
259+
const { deps, replyMessageLine } = createDeps({
260+
...(extracted
261+
? {
262+
processLineMessage: (text) => ({
263+
text,
264+
flexMessages: [{ type: "flex", altText, contents: { type: "bubble" } }],
265+
}),
266+
}
267+
: {}),
268+
createFlexMessage: createProviderFlexMessage,
269+
});
270+
271+
await deliverLineAutoReply({
272+
...baseDeliveryParams,
273+
payload: { text: "hello", channelData: { line: lineData } },
274+
lineData,
275+
deps,
276+
});
277+
278+
expect(replyMessageLine).toHaveBeenCalledExactlyOnceWith(
279+
"token",
280+
[
281+
{ type: "text", text: "hello" },
282+
{ type: "flex", altText, contents: { type: "bubble" } },
283+
],
284+
{ cfg: LINE_TEST_CFG, accountId: "acc" },
285+
);
286+
});
287+
288+
it("bounds Flex alternative text without splitting a Unicode surrogate pair", async () => {
289+
// The emoji crosses the real provider's 1500-character alternative-text boundary.
255290
const lineData = {
256-
flexMessage: { altText: `${"a".repeat(399)}😀 overflow`, contents: { type: "bubble" } },
291+
flexMessage: { altText: `${"a".repeat(1499)}😀 overflow`, contents: { type: "bubble" } },
257292
};
258-
const createFlexMessageSpy = vi.fn(createFlexMessage);
293+
const createFlexMessageSpy = vi.fn(createProviderFlexMessage);
259294
const { deps } = createDeps({
260295
createFlexMessage: createFlexMessageSpy as LineAutoReplyDeps["createFlexMessage"],
261296
});
@@ -267,8 +302,8 @@ describe("deliverLineAutoReply", () => {
267302
deps,
268303
});
269304

270-
const sentAltText = createFlexMessageSpy.mock.calls[0]?.[0] ?? "";
271-
expect(sentAltText.length).toBeLessThanOrEqual(400);
305+
const sentAltText = createFlexMessageSpy.mock.results[0]?.value.altText ?? "";
306+
expect(sentAltText).toBe("a".repeat(1499));
272307
expect(
273308
/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/.test(sentAltText),
274309
).toBe(false);

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { resolveSendableOutboundReplyParts } from "openclaw/plugin-sdk/reply-pay
1212
import type { ReplyPayload } from "openclaw/plugin-sdk/reply-runtime";
1313
import { classifyTransientNetworkErrorCode } from "openclaw/plugin-sdk/retry-runtime";
1414
import { sanitizeAssistantVisibleText } from "openclaw/plugin-sdk/text-chunking";
15-
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
1615
import type { FlexContainer } from "./flex-templates.js";
1716
import type { ProcessedLineMessage } from "./markdown-to-line.js";
1817
import { hasLineSpecificMediaOptions } from "./outbound-media.js";
@@ -228,7 +227,7 @@ export async function deliverLineAutoReply(params: {
228227
if (lineData.flexMessage) {
229228
richMessages.push(
230229
deps.createFlexMessage(
231-
truncateUtf16Safe(lineData.flexMessage.altText, 400),
230+
lineData.flexMessage.altText,
232231
lineData.flexMessage.contents as FlexContainer,
233232
),
234233
);
@@ -253,9 +252,7 @@ export async function deliverLineAutoReply(params: {
253252
: { text: "", flexMessages: [] };
254253

255254
for (const flexMsg of processed.flexMessages) {
256-
richMessages.push(
257-
deps.createFlexMessage(truncateUtf16Safe(flexMsg.altText, 400), flexMsg.contents),
258-
);
255+
richMessages.push(deps.createFlexMessage(flexMsg.altText, flexMsg.contents));
259256
}
260257

261258
const chunks = processed.text ? deps.chunkMarkdownText(processed.text, textLimit) : [];

‎extensions/line/src/channel.sendPayload.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ describe("line outbound sendPayload", () => {
417417
});
418418
});
419419

420-
it("attaches quick replies when no text chunks are present", async () => {
420+
it("attaches quick replies while preserving the provider's full Flex alternative-text limit", async () => {
421421
const { runtime, mocks } = createRuntime();
422422
setLineRuntime(runtime);
423423
const cfg = { channels: { line: {} } } as OpenClawConfig;

‎extensions/line/src/outbound.runtime.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Line plugin module implements outbound behavior.
22
export { processLineMessage } from "./markdown-to-line.js";
33
export {
4+
createFlexMessage,
45
createQuickReplyItems,
56
pushFlexMessage,
67
pushLocationMessage,

‎extensions/line/src/outbound.ts‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import { createLineSendReceipt } from "./send-receipt.js";
2626
import type { LineChannelData, LineSendResult } from "./types.js";
2727

2828
const loadLineOutboundRuntime = createLazyRuntimeModule(() => import("./outbound.runtime.js"));
29-
const LINE_FLEX_ALT_TEXT_LIMIT = 1500;
3029

3130
export const lineOutboundAdapter: NonNullable<ChannelPlugin<ResolvedLineAccount>["outbound"]> = {
3231
deliveryMode: "direct",
@@ -221,11 +220,14 @@ export const lineOutboundAdapter: NonNullable<ChannelPlugin<ResolvedLineAccount>
221220
} else if (shouldSendQuickRepliesInline) {
222221
const quickReplyMessages: Array<Record<string, unknown>> = [];
223222
if (lineData.flexMessage) {
224-
quickReplyMessages.push({
225-
type: "flex",
226-
altText: truncateUtf16Safe(lineData.flexMessage.altText, LINE_FLEX_ALT_TEXT_LIMIT),
227-
contents: lineData.flexMessage.contents,
228-
});
223+
quickReplyMessages.push(
224+
outboundRuntime.createFlexMessage(
225+
lineData.flexMessage.altText,
226+
lineData.flexMessage.contents as Parameters<
227+
typeof outboundRuntime.createFlexMessage
228+
>[1],
229+
),
230+
);
229231
}
230232
if (lineData.templateMessage) {
231233
const template = buildTemplate(lineData.templateMessage);
@@ -243,11 +245,9 @@ export const lineOutboundAdapter: NonNullable<ChannelPlugin<ResolvedLineAccount>
243245
});
244246
}
245247
for (const flexMsg of processed.flexMessages) {
246-
quickReplyMessages.push({
247-
type: "flex",
248-
altText: truncateUtf16Safe(flexMsg.altText, LINE_FLEX_ALT_TEXT_LIMIT),
249-
contents: flexMsg.contents,
250-
});
248+
quickReplyMessages.push(
249+
outboundRuntime.createFlexMessage(flexMsg.altText, flexMsg.contents),
250+
);
251251
}
252252
for (const url of mediaUrls) {
253253
const trimmed = url?.trim();

‎extensions/line/src/send.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,27 @@ describe("LINE send helpers", () => {
200200
expect(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])/.test(item?.action.label ?? "")).toBe(false);
201201
});
202202

203+
it("keeps provider-valid Flex alternative text and bounds oversized Unicode safely", () => {
204+
const contents = { type: "bubble" as const };
205+
206+
expect(sendModule.createFlexMessage("a".repeat(1200), contents).altText).toBe("a".repeat(1200));
207+
208+
const oversized = sendModule.createFlexMessage(`${"a".repeat(1499)}😀 overflow`, contents);
209+
expect(oversized.altText).toBe("a".repeat(1499));
210+
expect(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])/.test(oversized.altText)).toBe(false);
211+
});
212+
213+
it("sends the same provider-valid Flex alternative text through direct pushes", async () => {
214+
const altText = "a".repeat(1200);
215+
216+
await sendModule.pushFlexMessage("U123", altText, { type: "bubble" }, { cfg: LINE_TEST_CFG });
217+
218+
expect(pushMessageMock).toHaveBeenCalledWith({
219+
to: "U123",
220+
messages: [{ type: "flex", altText, contents: { type: "bubble" } }],
221+
});
222+
});
223+
203224
it("normalizes raw Flex actions at both outbound API boundaries", async () => {
204225
const oversizedPostback = { type: "postback", label: "Open", data: "x".repeat(301) };
205226
const oversizedUri = {

‎extensions/line/src/send.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ type ImageMessage = messagingApi.ImageMessage;
2222
type VideoMessage = messagingApi.VideoMessage & { trackingId?: string };
2323
type AudioMessage = messagingApi.AudioMessage;
2424
type LocationMessage = messagingApi.LocationMessage;
25-
type FlexMessage = messagingApi.FlexMessage;
2625
type FlexContainer = messagingApi.FlexContainer;
2726
type TemplateMessage = messagingApi.TemplateMessage;
2827
type QuickReply = messagingApi.QuickReply;
@@ -34,6 +33,7 @@ const userProfileCache = new Map<
3433
>();
3534
const PROFILE_CACHE_TTL_MS = 5 * 60 * 1000;
3635
const PROFILE_CACHE_MAX_ENTRIES = 1000;
36+
const LINE_FLEX_ALT_TEXT_LIMIT = 1500;
3737

3838
function cacheUserProfile(
3939
userId: string,
@@ -481,7 +481,7 @@ export function createFlexMessage(
481481
): messagingApi.FlexMessage {
482482
return {
483483
type: "flex",
484-
altText,
484+
altText: truncateUtf16Safe(altText, LINE_FLEX_ALT_TEXT_LIMIT),
485485
contents,
486486
};
487487
}
@@ -522,13 +522,7 @@ export async function pushFlexMessage(
522522
contents: FlexContainer,
523523
opts: LinePushOpts,
524524
): Promise<LineSendResult> {
525-
const flexMessage: FlexMessage = {
526-
type: "flex",
527-
altText: truncateUtf16Safe(altText, 400),
528-
contents,
529-
};
530-
531-
return pushLineMessages(to, [flexMessage], opts, {
525+
return pushLineMessages(to, [createFlexMessage(altText, contents)], opts, {
532526
errorContext: "push flex message",
533527
verboseMessage: (chatId) => `line: pushed flex message to ${chatId}`,
534528
});

0 commit comments

Comments
 (0)