File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -840,27 +840,11 @@ describe("LiteLLMHandler", () => {
840840 expect ( reasoningChunks ) . toHaveLength ( 0 )
841841 } )
842842
843- it ( "should preserve whitespace-only reasoning chunks so streamed boundaries survive concatenation " , async ( ) => {
843+ it ( "should not yield reasoning chunk for empty or whitespace-only reasoning " , async ( ) => {
844844 const mockStream = {
845845 async * [ Symbol . asyncIterator ] ( ) {
846846 yield {
847- choices : [ { delta : { reasoning_content : "Let's" } } ] ,
848- usage : null ,
849- }
850- yield {
851- choices : [ { delta : { reasoning_content : " " } } ] ,
852- usage : null ,
853- }
854- yield {
855- choices : [ { delta : { reasoning_content : "think" } } ] ,
856- usage : null ,
857- }
858- yield {
859- choices : [ { delta : { reasoning_content : "\n\n" } } ] ,
860- usage : null ,
861- }
862- yield {
863- choices : [ { delta : { reasoning_content : "next" } } ] ,
847+ choices : [ { delta : { reasoning_content : " " } } ] ,
864848 usage : null ,
865849 }
866850 yield {
@@ -881,7 +865,7 @@ describe("LiteLLMHandler", () => {
881865 }
882866
883867 const reasoningChunks = results . filter ( ( c ) => c . type === "reasoning" )
884- expect ( reasoningChunks . map ( ( c ) => ( c as { text : string } ) . text ) . join ( "" ) ) . toBe ( "Let's think\n\nnext" )
868+ expect ( reasoningChunks ) . toHaveLength ( 0 )
885869 } )
886870
887871 it ( "should fall back to reasoning when reasoning_content is null on the same delta" , async ( ) => {
Original file line number Diff line number Diff line change @@ -38,18 +38,9 @@ describe("extractReasoningFromDelta", () => {
3838 ) . toBe ( "fallback" )
3939 } )
4040
41- it ( "preserves whitespace-only payloads so streamed chunks keep word and paragraph boundaries" , ( ) => {
42- expect ( extractReasoningFromDelta ( { reasoning_content : " " } ) ) . toBe ( " " )
43- expect ( extractReasoningFromDelta ( { reasoning : "\n\n" } ) ) . toBe ( "\n\n" )
44- } )
45-
46- it ( "falls back to reasoning when reasoning_content is an empty string but does not skip whitespace" , ( ) => {
47- expect (
48- extractReasoningFromDelta ( {
49- reasoning_content : "" ,
50- reasoning : "\n\n" ,
51- } ) ,
52- ) . toBe ( "\n\n" )
41+ it ( "returns undefined for whitespace-only values" , ( ) => {
42+ expect ( extractReasoningFromDelta ( { reasoning_content : " " } ) ) . toBeUndefined ( )
43+ expect ( extractReasoningFromDelta ( { reasoning : "\n\t" } ) ) . toBeUndefined ( )
5344 } )
5445
5546 it ( "returns undefined when neither field is present" , ( ) => {
Original file line number Diff line number Diff line change 22 * Extracts reasoning text from a streaming delta object.
33 *
44 * Prefers `reasoning_content` (DeepSeek-R1 / QwQ style) and falls back to
5- * `reasoning` (OpenRouter style). Whitespace-only payloads (e.g. a lone " "
6- * or "\n\n" between paragraphs) are preserved so streamed reasoning keeps
7- * word and paragraph boundaries once chunks are concatenated downstream.
8- *
9- * The fallback only fires when the current field is missing, non-string,
10- * or an empty string — a delta with `reasoning_content: null` and a
11- * populated `reasoning` still resolves to the populated field.
5+ * `reasoning` (OpenRouter style). Returns `undefined` when neither field
6+ * carries non-blank text, so callers can decide whether to yield a chunk.
127 */
138export function extractReasoningFromDelta ( delta : unknown ) : string | undefined {
149 if ( ! delta ) return undefined
1510
1611 const d = delta as { reasoning_content ?: unknown ; reasoning ?: unknown }
12+ const candidate =
13+ ( typeof d . reasoning_content === "string" && d . reasoning_content ) ||
14+ ( typeof d . reasoning === "string" && d . reasoning ) ||
15+ ""
1716
18- if ( typeof d . reasoning_content === "string" && d . reasoning_content . length > 0 ) {
19- return d . reasoning_content
20- }
21- if ( typeof d . reasoning === "string" && d . reasoning . length > 0 ) {
22- return d . reasoning
23- }
24- return undefined
17+ return candidate . trim ( ) ? candidate : undefined
2518}
You can’t perform that action at this time.
0 commit comments