Skip to content

Commit 62edfdf

Browse files
committed
refactor: deduplicate reply payload handling
1 parent 152d179 commit 62edfdf

58 files changed

Lines changed: 704 additions & 450 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎extensions/discord/src/monitor/message-handler.process.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { resolveDiscordPreviewStreamMode } from "openclaw/plugin-sdk/config-runt
1616
import { resolveMarkdownTableMode } from "openclaw/plugin-sdk/config-runtime";
1717
import { readSessionUpdatedAt, resolveStorePath } from "openclaw/plugin-sdk/config-runtime";
1818
import { getAgentScopedMediaLocalRoots } from "openclaw/plugin-sdk/media-runtime";
19+
import { resolveSendableOutboundReplyParts } from "openclaw/plugin-sdk/reply-payload";
1920
import { resolveChunkMode } from "openclaw/plugin-sdk/reply-runtime";
2021
import { dispatchInboundMessage } from "openclaw/plugin-sdk/reply-runtime";
2122
import {
@@ -610,7 +611,8 @@ export async function processDiscordMessage(ctx: DiscordMessagePreflightContext)
610611
}
611612
if (draftStream && isFinal) {
612613
await flushDraft();
613-
const hasMedia = Boolean(payload.mediaUrl) || (payload.mediaUrls?.length ?? 0) > 0;
614+
const reply = resolveSendableOutboundReplyParts(payload);
615+
const hasMedia = reply.hasMedia;
614616
const finalText = payload.text;
615617
const previewFinalText = resolvePreviewFinalText(finalText);
616618
const previewMessageId = draftStream.messageId();

‎extensions/discord/src/monitor/native-command.ts‎

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { buildPairingReply } from "openclaw/plugin-sdk/conversation-runtime";
2626
import { getAgentScopedMediaLocalRoots } from "openclaw/plugin-sdk/media-runtime";
2727
import { executePluginCommand, matchPluginCommand } from "openclaw/plugin-sdk/plugin-runtime";
2828
import {
29-
resolveOutboundMediaUrls,
29+
resolveSendableOutboundReplyParts,
3030
resolveTextChunksWithFallback,
3131
} from "openclaw/plugin-sdk/reply-payload";
3232
import { resolveChunkMode, resolveTextChunkLimit } from "openclaw/plugin-sdk/reply-runtime";
@@ -236,13 +236,7 @@ function isDiscordUnknownInteraction(error: unknown): boolean {
236236
}
237237

238238
function hasRenderableReplyPayload(payload: ReplyPayload): boolean {
239-
if ((payload.text ?? "").trim()) {
240-
return true;
241-
}
242-
if ((payload.mediaUrl ?? "").trim()) {
243-
return true;
244-
}
245-
if (payload.mediaUrls?.some((entry) => entry.trim())) {
239+
if (resolveSendableOutboundReplyParts(payload).hasContent) {
246240
return true;
247241
}
248242
const discordData = payload.channelData?.discord as
@@ -891,8 +885,7 @@ async function deliverDiscordInteractionReply(params: {
891885
chunkMode: "length" | "newline";
892886
}) {
893887
const { interaction, payload, textLimit, maxLinesPerMessage, preferFollowUp, chunkMode } = params;
894-
const mediaList = resolveOutboundMediaUrls(payload);
895-
const text = payload.text ?? "";
888+
const reply = resolveSendableOutboundReplyParts(payload);
896889
const discordData = payload.channelData?.discord as
897890
| { components?: TopLevelComponents[] }
898891
| undefined;
@@ -937,9 +930,9 @@ async function deliverDiscordInteractionReply(params: {
937930
});
938931
};
939932

940-
if (mediaList.length > 0) {
933+
if (reply.hasMedia) {
941934
const media = await Promise.all(
942-
mediaList.map(async (url) => {
935+
reply.mediaUrls.map(async (url) => {
943936
const loaded = await loadWebMedia(url, {
944937
localRoots: params.mediaLocalRoots,
945938
});
@@ -950,8 +943,8 @@ async function deliverDiscordInteractionReply(params: {
950943
}),
951944
);
952945
const chunks = resolveTextChunksWithFallback(
953-
text,
954-
chunkDiscordTextWithMode(text, {
946+
reply.text,
947+
chunkDiscordTextWithMode(reply.text, {
955948
maxChars: textLimit,
956949
maxLines: maxLinesPerMessage,
957950
chunkMode,
@@ -968,14 +961,14 @@ async function deliverDiscordInteractionReply(params: {
968961
return;
969962
}
970963

971-
if (!text.trim() && !firstMessageComponents) {
964+
if (!reply.hasText && !firstMessageComponents) {
972965
return;
973966
}
974967
const chunks =
975-
text || firstMessageComponents
968+
reply.text || firstMessageComponents
976969
? resolveTextChunksWithFallback(
977-
text,
978-
chunkDiscordTextWithMode(text, {
970+
reply.text,
971+
chunkDiscordTextWithMode(reply.text, {
979972
maxChars: textLimit,
980973
maxLines: maxLinesPerMessage,
981974
chunkMode,

‎extensions/discord/src/monitor/reply-delivery.ts‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
type RetryConfig,
1010
} from "openclaw/plugin-sdk/infra-runtime";
1111
import {
12-
resolveOutboundMediaUrls,
12+
resolveSendableOutboundReplyParts,
1313
resolveTextChunksWithFallback,
1414
sendMediaWithLeadingCaption,
1515
} from "openclaw/plugin-sdk/reply-payload";
@@ -268,18 +268,18 @@ export async function deliverDiscordReply(params: {
268268
: undefined;
269269
let deliveredAny = false;
270270
for (const payload of params.replies) {
271-
const mediaList = resolveOutboundMediaUrls(payload);
272-
const rawText = payload.text ?? "";
273271
const tableMode = params.tableMode ?? "code";
274-
const text = convertMarkdownTables(rawText, tableMode);
275-
if (!text && mediaList.length === 0) {
272+
const reply = resolveSendableOutboundReplyParts(payload, {
273+
text: convertMarkdownTables(payload.text ?? "", tableMode),
274+
});
275+
if (!reply.hasContent) {
276276
continue;
277277
}
278-
if (mediaList.length === 0) {
278+
if (!reply.hasMedia) {
279279
const mode = params.chunkMode ?? "length";
280280
const chunks = resolveTextChunksWithFallback(
281-
text,
282-
chunkDiscordTextWithMode(text, {
281+
reply.text,
282+
chunkDiscordTextWithMode(reply.text, {
283283
maxChars: chunkLimit,
284284
maxLines: params.maxLinesPerMessage,
285285
chunkMode: mode,
@@ -312,7 +312,7 @@ export async function deliverDiscordReply(params: {
312312
continue;
313313
}
314314

315-
const firstMedia = mediaList[0];
315+
const firstMedia = reply.mediaUrls[0];
316316
if (!firstMedia) {
317317
continue;
318318
}
@@ -331,7 +331,7 @@ export async function deliverDiscordReply(params: {
331331
await sendDiscordChunkWithFallback({
332332
cfg: params.cfg,
333333
target: params.target,
334-
text,
334+
text: reply.text,
335335
token: params.token,
336336
rest: params.rest,
337337
accountId: params.accountId,
@@ -347,7 +347,7 @@ export async function deliverDiscordReply(params: {
347347
});
348348
// Additional media items are sent as regular attachments (voice is single-file only).
349349
await sendMediaWithLeadingCaption({
350-
mediaUrls: mediaList.slice(1),
350+
mediaUrls: reply.mediaUrls.slice(1),
351351
caption: "",
352352
send: async ({ mediaUrl }) => {
353353
const replyTo = resolveReplyTo();
@@ -370,8 +370,8 @@ export async function deliverDiscordReply(params: {
370370
}
371371

372372
await sendMediaWithLeadingCaption({
373-
mediaUrls: mediaList,
374-
caption: text,
373+
mediaUrls: reply.mediaUrls,
374+
caption: reply.text,
375375
send: async ({ mediaUrl, caption }) => {
376376
const replyTo = resolveReplyTo();
377377
await sendWithRetry(

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

Lines changed: 71 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import {
2+
resolveSendableOutboundReplyParts,
3+
resolveTextChunksWithFallback,
4+
sendMediaWithLeadingCaption,
5+
} from "openclaw/plugin-sdk/reply-payload";
16
import {
27
createReplyPrefixContext,
38
createTypingCallbacks,
@@ -13,12 +18,7 @@ import { sendMediaFeishu } from "./media.js";
1318
import type { MentionTarget } from "./mention.js";
1419
import { buildMentionedCardContent } from "./mention.js";
1520
import { getFeishuRuntime } from "./runtime.js";
16-
import {
17-
sendMarkdownCardFeishu,
18-
sendMessageFeishu,
19-
sendStructuredCardFeishu,
20-
type CardHeaderConfig,
21-
} from "./send.js";
21+
import { sendMessageFeishu, sendStructuredCardFeishu, type CardHeaderConfig } from "./send.js";
2222
import { FeishuStreamingSession, mergeStreamingText } from "./streaming-card.js";
2323
import { resolveReceiveIdType } from "./targets.js";
2424
import { addTypingIndicator, removeTypingIndicator, type TypingIndicatorState } from "./typing.js";
@@ -300,37 +300,43 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
300300
text: string;
301301
useCard: boolean;
302302
infoKind?: string;
303+
sendChunk: (params: { chunk: string; isFirst: boolean }) => Promise<void>;
303304
}) => {
304-
let first = true;
305305
const chunkSource = params.useCard
306306
? params.text
307307
: core.channel.text.convertMarkdownTables(params.text, tableMode);
308-
for (const chunk of core.channel.text.chunkTextWithMode(
308+
const chunks = resolveTextChunksWithFallback(
309309
chunkSource,
310-
textChunkLimit,
311-
chunkMode,
312-
)) {
313-
const message = {
314-
cfg,
315-
to: chatId,
316-
text: chunk,
317-
replyToMessageId: sendReplyToMessageId,
318-
replyInThread: effectiveReplyInThread,
319-
mentions: first ? mentionTargets : undefined,
320-
accountId,
321-
};
322-
if (params.useCard) {
323-
await sendMarkdownCardFeishu(message);
324-
} else {
325-
await sendMessageFeishu(message);
326-
}
327-
first = false;
310+
core.channel.text.chunkTextWithMode(chunkSource, textChunkLimit, chunkMode),
311+
);
312+
for (const [index, chunk] of chunks.entries()) {
313+
await params.sendChunk({
314+
chunk,
315+
isFirst: index === 0,
316+
});
328317
}
329318
if (params.infoKind === "final") {
330319
deliveredFinalTexts.add(params.text);
331320
}
332321
};
333322

323+
const sendMediaReplies = async (payload: ReplyPayload) => {
324+
await sendMediaWithLeadingCaption({
325+
mediaUrls: resolveSendableOutboundReplyParts(payload).mediaUrls,
326+
caption: "",
327+
send: async ({ mediaUrl }) => {
328+
await sendMediaFeishu({
329+
cfg,
330+
to: chatId,
331+
mediaUrl,
332+
replyToMessageId: sendReplyToMessageId,
333+
replyInThread: effectiveReplyInThread,
334+
accountId,
335+
});
336+
},
337+
});
338+
};
339+
334340
const { dispatcher, replyOptions, markDispatchIdle } =
335341
core.channel.reply.createReplyDispatcherWithTyping({
336342
responsePrefix: prefixContext.responsePrefix,
@@ -344,15 +350,10 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
344350
void typingCallbacks.onReplyStart?.();
345351
},
346352
deliver: async (payload: ReplyPayload, info) => {
347-
const text = payload.text ?? "";
348-
const mediaList =
349-
payload.mediaUrls && payload.mediaUrls.length > 0
350-
? payload.mediaUrls
351-
: payload.mediaUrl
352-
? [payload.mediaUrl]
353-
: [];
354-
const hasText = Boolean(text.trim());
355-
const hasMedia = mediaList.length > 0;
353+
const reply = resolveSendableOutboundReplyParts(payload);
354+
const text = reply.text;
355+
const hasText = reply.hasText;
356+
const hasMedia = reply.hasMedia;
356357
const skipTextForDuplicateFinal =
357358
info?.kind === "final" && hasText && deliveredFinalTexts.has(text);
358359
const shouldDeliverText = hasText && !skipTextForDuplicateFinal;
@@ -363,7 +364,6 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
363364

364365
if (shouldDeliverText) {
365366
const useCard = renderMode === "card" || (renderMode === "auto" && shouldUseCard(text));
366-
let first = true;
367367

368368
if (info?.kind === "block") {
369369
// Drop internal block chunks unless we can safely consume them as
@@ -397,60 +397,54 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
397397
}
398398
// Send media even when streaming handled the text
399399
if (hasMedia) {
400-
for (const mediaUrl of mediaList) {
401-
await sendMediaFeishu({
402-
cfg,
403-
to: chatId,
404-
mediaUrl,
405-
replyToMessageId: sendReplyToMessageId,
406-
replyInThread: effectiveReplyInThread,
407-
accountId,
408-
});
409-
}
400+
await sendMediaReplies(payload);
410401
}
411402
return;
412403
}
413404

414405
if (useCard) {
415406
const cardHeader = resolveCardHeader(agentId, identity);
416407
const cardNote = resolveCardNote(agentId, identity, prefixContext.prefixContext);
417-
for (const chunk of core.channel.text.chunkTextWithMode(
408+
await sendChunkedTextReply({
418409
text,
419-
textChunkLimit,
420-
chunkMode,
421-
)) {
422-
await sendStructuredCardFeishu({
423-
cfg,
424-
to: chatId,
425-
text: chunk,
426-
replyToMessageId: sendReplyToMessageId,
427-
replyInThread: effectiveReplyInThread,
428-
mentions: first ? mentionTargets : undefined,
429-
accountId,
430-
header: cardHeader,
431-
note: cardNote,
432-
});
433-
first = false;
434-
}
435-
if (info?.kind === "final") {
436-
deliveredFinalTexts.add(text);
437-
}
410+
useCard: true,
411+
infoKind: info?.kind,
412+
sendChunk: async ({ chunk, isFirst }) => {
413+
await sendStructuredCardFeishu({
414+
cfg,
415+
to: chatId,
416+
text: chunk,
417+
replyToMessageId: sendReplyToMessageId,
418+
replyInThread: effectiveReplyInThread,
419+
mentions: isFirst ? mentionTargets : undefined,
420+
accountId,
421+
header: cardHeader,
422+
note: cardNote,
423+
});
424+
},
425+
});
438426
} else {
439-
await sendChunkedTextReply({ text, useCard: false, infoKind: info?.kind });
427+
await sendChunkedTextReply({
428+
text,
429+
useCard: false,
430+
infoKind: info?.kind,
431+
sendChunk: async ({ chunk, isFirst }) => {
432+
await sendMessageFeishu({
433+
cfg,
434+
to: chatId,
435+
text: chunk,
436+
replyToMessageId: sendReplyToMessageId,
437+
replyInThread: effectiveReplyInThread,
438+
mentions: isFirst ? mentionTargets : undefined,
439+
accountId,
440+
});
441+
},
442+
});
440443
}
441444
}
442445

443446
if (hasMedia) {
444-
for (const mediaUrl of mediaList) {
445-
await sendMediaFeishu({
446-
cfg,
447-
to: chatId,
448-
mediaUrl,
449-
replyToMessageId: sendReplyToMessageId,
450-
replyInThread: effectiveReplyInThread,
451-
accountId,
452-
});
453-
}
447+
await sendMediaReplies(payload);
454448
}
455449
},
456450
onError: async (error, info) => {

0 commit comments

Comments
 (0)