@@ -462,6 +462,126 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
462462 ) ;
463463 } ) ;
464464
465+ it ( "streams reasoning content as blockquote before answer" , async ( ) => {
466+ const { result, options } = createDispatcherHarness ( {
467+ runtime : createRuntimeLogger ( ) ,
468+ } ) ;
469+
470+ await options . onReplyStart ?.( ) ;
471+ // Core agent sends pre-formatted text from formatReasoningMessage
472+ result . replyOptions . onReasoningStream ?.( { text : "Reasoning:\n_thinking step 1_" } ) ;
473+ result . replyOptions . onReasoningStream ?.( {
474+ text : "Reasoning:\n_thinking step 1_\n_step 2_" ,
475+ } ) ;
476+ result . replyOptions . onPartialReply ?.( { text : "answer part" } ) ;
477+ result . replyOptions . onReasoningEnd ?.( ) ;
478+ await options . deliver ( { text : "answer part final" } , { kind : "final" } ) ;
479+
480+ expect ( streamingInstances ) . toHaveLength ( 1 ) ;
481+ const updateCalls = streamingInstances [ 0 ] . update . mock . calls . map ( ( c : unknown [ ] ) => c [ 0 ] ) ;
482+ const reasoningUpdate = updateCalls . find ( ( c : string ) => c . includes ( "Thinking" ) ) ;
483+ expect ( reasoningUpdate ) . toContain ( "> 💭 **Thinking**" ) ;
484+ // formatReasoningPrefix strips "Reasoning:" prefix and italic markers
485+ expect ( reasoningUpdate ) . toContain ( "> thinking step" ) ;
486+ expect ( reasoningUpdate ) . not . toContain ( "Reasoning:" ) ;
487+ expect ( reasoningUpdate ) . not . toMatch ( / > _ .* _ / ) ;
488+
489+ const combinedUpdate = updateCalls . find (
490+ ( c : string ) => c . includes ( "Thinking" ) && c . includes ( "---" ) ,
491+ ) ;
492+ expect ( combinedUpdate ) . toBeDefined ( ) ;
493+
494+ expect ( streamingInstances [ 0 ] . close ) . toHaveBeenCalledTimes ( 1 ) ;
495+ const closeArg = streamingInstances [ 0 ] . close . mock . calls [ 0 ] [ 0 ] as string ;
496+ expect ( closeArg ) . toContain ( "> 💭 **Thinking**" ) ;
497+ expect ( closeArg ) . toContain ( "---" ) ;
498+ expect ( closeArg ) . toContain ( "answer part final" ) ;
499+ } ) ;
500+
501+ it ( "provides onReasoningStream and onReasoningEnd when streaming is enabled" , ( ) => {
502+ const { result } = createDispatcherHarness ( {
503+ runtime : createRuntimeLogger ( ) ,
504+ } ) ;
505+
506+ expect ( result . replyOptions . onReasoningStream ) . toBeTypeOf ( "function" ) ;
507+ expect ( result . replyOptions . onReasoningEnd ) . toBeTypeOf ( "function" ) ;
508+ } ) ;
509+
510+ it ( "omits reasoning callbacks when streaming is disabled" , ( ) => {
511+ resolveFeishuAccountMock . mockReturnValue ( {
512+ accountId : "main" ,
513+ appId : "app_id" ,
514+ appSecret : "app_secret" ,
515+ domain : "feishu" ,
516+ config : {
517+ renderMode : "auto" ,
518+ streaming : false ,
519+ } ,
520+ } ) ;
521+
522+ const { result } = createDispatcherHarness ( {
523+ runtime : createRuntimeLogger ( ) ,
524+ } ) ;
525+
526+ expect ( result . replyOptions . onReasoningStream ) . toBeUndefined ( ) ;
527+ expect ( result . replyOptions . onReasoningEnd ) . toBeUndefined ( ) ;
528+ } ) ;
529+
530+ it ( "renders reasoning-only card when no answer text arrives" , async ( ) => {
531+ const { result, options } = createDispatcherHarness ( {
532+ runtime : createRuntimeLogger ( ) ,
533+ } ) ;
534+
535+ await options . onReplyStart ?.( ) ;
536+ result . replyOptions . onReasoningStream ?.( { text : "Reasoning:\n_deep thought_" } ) ;
537+ result . replyOptions . onReasoningEnd ?.( ) ;
538+ await options . onIdle ?.( ) ;
539+
540+ expect ( streamingInstances ) . toHaveLength ( 1 ) ;
541+ expect ( streamingInstances [ 0 ] . close ) . toHaveBeenCalledTimes ( 1 ) ;
542+ const closeArg = streamingInstances [ 0 ] . close . mock . calls [ 0 ] [ 0 ] as string ;
543+ expect ( closeArg ) . toContain ( "> 💭 **Thinking**" ) ;
544+ expect ( closeArg ) . toContain ( "> deep thought" ) ;
545+ expect ( closeArg ) . not . toContain ( "Reasoning:" ) ;
546+ expect ( closeArg ) . not . toContain ( "---" ) ;
547+ } ) ;
548+
549+ it ( "ignores empty reasoning payloads" , async ( ) => {
550+ const { result, options } = createDispatcherHarness ( {
551+ runtime : createRuntimeLogger ( ) ,
552+ } ) ;
553+
554+ await options . onReplyStart ?.( ) ;
555+ result . replyOptions . onReasoningStream ?.( { text : "" } ) ;
556+ result . replyOptions . onPartialReply ?.( { text : "```ts\ncode\n```" } ) ;
557+ await options . deliver ( { text : "```ts\ncode\n```" } , { kind : "final" } ) ;
558+
559+ expect ( streamingInstances ) . toHaveLength ( 1 ) ;
560+ const closeArg = streamingInstances [ 0 ] . close . mock . calls [ 0 ] [ 0 ] as string ;
561+ expect ( closeArg ) . not . toContain ( "Thinking" ) ;
562+ expect ( closeArg ) . toBe ( "```ts\ncode\n```" ) ;
563+ } ) ;
564+
565+ it ( "deduplicates final text by raw answer payload, not combined card text" , async ( ) => {
566+ const { result, options } = createDispatcherHarness ( {
567+ runtime : createRuntimeLogger ( ) ,
568+ } ) ;
569+
570+ await options . onReplyStart ?.( ) ;
571+ result . replyOptions . onReasoningStream ?.( { text : "Reasoning:\n_thought_" } ) ;
572+ result . replyOptions . onReasoningEnd ?.( ) ;
573+ await options . deliver ( { text : "```ts\nfinal answer\n```" } , { kind : "final" } ) ;
574+
575+ expect ( streamingInstances ) . toHaveLength ( 1 ) ;
576+ expect ( streamingInstances [ 0 ] . close ) . toHaveBeenCalledTimes ( 1 ) ;
577+
578+ // Deliver the same raw answer text again — should be deduped
579+ await options . deliver ( { text : "```ts\nfinal answer\n```" } , { kind : "final" } ) ;
580+
581+ // No second streaming session since the raw answer text matches
582+ expect ( streamingInstances ) . toHaveLength ( 1 ) ;
583+ } ) ;
584+
465585 it ( "passes replyToMessageId and replyInThread to streaming.start()" , async ( ) => {
466586 const { options } = createDispatcherHarness ( {
467587 runtime : createRuntimeLogger ( ) ,
0 commit comments