1+ import {
2+ resolveSendableOutboundReplyParts ,
3+ resolveTextChunksWithFallback ,
4+ sendMediaWithLeadingCaption ,
5+ } from "openclaw/plugin-sdk/reply-payload" ;
16import {
27 createReplyPrefixContext ,
38 createTypingCallbacks ,
@@ -13,12 +18,7 @@ import { sendMediaFeishu } from "./media.js";
1318import type { MentionTarget } from "./mention.js" ;
1419import { buildMentionedCardContent } from "./mention.js" ;
1520import { 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" ;
2222import { FeishuStreamingSession , mergeStreamingText } from "./streaming-card.js" ;
2323import { resolveReceiveIdType } from "./targets.js" ;
2424import { 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