@@ -8,11 +8,57 @@ import {
88 extractAssistantText ,
99 extractAssistantThinking ,
1010 extractAssistantVisibleText ,
11+ createThinkingTagStreamState ,
12+ extractThinkingFromTaggedStream ,
13+ extractThinkingFromTaggedText ,
1114 formatReasoningMessage ,
1215 promoteThinkingTagsToBlocks ,
1316 stripDowngradedToolCallText ,
1417} from "./embedded-agent-utils.js" ;
1518
19+ const REFERENCE_THINKING_TAG_NAME_PATTERN = String . raw `(?:(?:antml:|mm:)?(?:think(?:ing)?|thought)|antthinking)` ;
20+ const REFERENCE_THINKING_TAG_OPEN_RE = new RegExp (
21+ String . raw `<\s*${ REFERENCE_THINKING_TAG_NAME_PATTERN } \s*>` ,
22+ "gi" ,
23+ ) ;
24+ const REFERENCE_THINKING_TAG_CLOSE_RE = new RegExp (
25+ String . raw `<\s*\/\s*${ REFERENCE_THINKING_TAG_NAME_PATTERN } \s*>` ,
26+ "gi" ,
27+ ) ;
28+
29+ function extractThinkingFromTaggedStreamReference ( text : string ) : string {
30+ if ( ! text ) {
31+ return "" ;
32+ }
33+ const closed = extractThinkingFromTaggedText ( text ) ;
34+ if ( closed ) {
35+ return closed ;
36+ }
37+ const openMatches = [ ...text . matchAll ( REFERENCE_THINKING_TAG_OPEN_RE ) ] ;
38+ const lastOpen = openMatches . at ( - 1 ) ;
39+ if ( ! lastOpen ) {
40+ return "" ;
41+ }
42+ const lastClose = [ ...text . matchAll ( REFERENCE_THINKING_TAG_CLOSE_RE ) ] . at ( - 1 ) ;
43+ if ( lastClose && ( lastClose . index ?? - 1 ) > ( lastOpen . index ?? - 1 ) ) {
44+ return closed ;
45+ }
46+ return text . slice ( ( lastOpen . index ?? 0 ) + lastOpen [ 0 ] . length ) . trim ( ) ;
47+ }
48+
49+ function randomChunks ( text : string , seed : number ) : string [ ] {
50+ const chunks : string [ ] = [ ] ;
51+ let offset = 0 ;
52+ let value = seed ;
53+ while ( offset < text . length ) {
54+ value = ( value * 1664525 + 1013904223 ) >>> 0 ;
55+ const length = Math . min ( 1 + ( value % 7 ) , text . length - offset ) ;
56+ chunks . push ( text . slice ( offset , offset + length ) ) ;
57+ offset += length ;
58+ }
59+ return chunks ;
60+ }
61+
1662function makeAssistantMessage (
1763 message : Omit <
1864 AssistantMessage ,
@@ -40,6 +86,32 @@ function makeAssistantMessage(
4086 } as unknown as AssistantMessage ;
4187}
4288
89+ describe ( "extractThinkingFromTaggedStream" , ( ) => {
90+ it ( "matches full-buffer extraction at every randomized chunk boundary" , ( ) => {
91+ const cases = [
92+ "plain text with <not-a-thinking-tag> and no reasoning" ,
93+ "Before <think>first line\nsecond line</think> after" ,
94+ "prefix<thought>one</thought>middle<mm:thinking>two</mm:thinking>suffix" ,
95+ "surrounding text <antml:think>unfinished reasoning tail" ,
96+ "< \nAnTThinking \t>spaced tag content< / antthinking > visible" ,
97+ "<think>closed</think><think>unclosed trailing reasoning" ,
98+ ] ;
99+
100+ for ( const text of cases ) {
101+ for ( let seed = 1 ; seed <= 24 ; seed += 1 ) {
102+ const state = createThinkingTagStreamState ( ) ;
103+ let prefix = "" ;
104+ for ( const chunk of randomChunks ( text , seed ) ) {
105+ prefix += chunk ;
106+ expect ( extractThinkingFromTaggedStream ( prefix , state ) , `${ text } (seed ${ seed } )` ) . toBe (
107+ extractThinkingFromTaggedStreamReference ( prefix ) ,
108+ ) ;
109+ }
110+ }
111+ }
112+ } ) ;
113+ } ) ;
114+
43115describe ( "extractAssistantText" , ( ) => {
44116 it ( "strips tool-only Minimax invocation XML from text" , ( ) => {
45117 const cases = [
0 commit comments