@@ -6,32 +6,21 @@ type StreamCallback = (chunk: string) => void
66
77const DATA_PREFIX = 'data: '
88const DONE_TOKEN = '[DONE]'
9- const THINK_OPEN = '<think>'
10- const THINK_CLOSE = '</think>'
119
1210async function process_stream_chunk (
1311 chunk : string ,
1412 buffer : string ,
1513 accumulated_content : string ,
1614 last_log_time : number ,
17- think_buffer : string ,
18- in_think_block : boolean ,
19- think_block_ended : boolean ,
2015 on_chunk ?: StreamCallback
2116) : Promise < {
2217 updated_buffer : string
2318 updated_accumulated_content : string
2419 updated_last_log_time : number
25- updated_think_buffer : string
26- updated_in_think_block : boolean
27- updated_think_block_ended : boolean
2820} > {
2921 let updated_buffer = buffer
3022 let updated_accumulated_content = accumulated_content
3123 let updated_last_log_time = last_log_time
32- let updated_think_buffer = think_buffer
33- let updated_in_think_block = in_think_block
34- let updated_think_block_ended = think_block_ended
3524
3625 try {
3726 updated_buffer += chunk
@@ -52,64 +41,9 @@ async function process_stream_chunk(
5241 const new_content = json_data . choices [ 0 ] . delta . content
5342 updated_accumulated_content += new_content
5443
55- // --- Think block handling logic ---
56- if ( updated_think_block_ended ) {
57- // If the first think block has already ended, stream all new content
58- if ( on_chunk ) {
59- on_chunk ( new_content )
60- }
61- } else {
62- // We are either before, or inside, the first think block
63- updated_think_buffer += new_content
64-
65- if ( ! updated_in_think_block ) {
66- // We are currently *not* in a think block (haven't seen <think> yet)
67- const think_open_index =
68- updated_think_buffer . indexOf ( THINK_OPEN )
69- if ( think_open_index !== - 1 ) {
70- // <think> tag found!
71- updated_in_think_block = true
72- // Stream content *before* <think>
73- const pre_think_content = updated_think_buffer . substring (
74- 0 ,
75- think_open_index
76- )
77- if ( pre_think_content && on_chunk ) {
78- on_chunk ( pre_think_content )
79- }
80- // The remaining part of updated_think_buffer now starts with <think>
81- updated_think_buffer =
82- updated_think_buffer . substring ( think_open_index )
83- } else {
84- // No <think> tag found yet, stream this content
85- if ( on_chunk ) {
86- on_chunk ( new_content )
87- }
88- }
89- }
90-
91- // Now, if we are in a think block (either just entered or already were)
92- if ( updated_in_think_block ) {
93- const think_close_index =
94- updated_think_buffer . indexOf ( THINK_CLOSE )
95- if ( think_close_index != - 1 ) {
96- // </think> tag found!
97- updated_in_think_block = false
98- updated_think_block_ended = true
99- // Stream content *after* </think>
100- const post_think_content = updated_think_buffer . substring (
101- think_close_index + THINK_CLOSE . length
102- )
103- if ( post_think_content && on_chunk ) {
104- on_chunk ( post_think_content )
105- }
106- // Clear the think buffer as the first think block is fully processed
107- updated_think_buffer = ''
108- }
109- // If </think> not found, we remain in_think_block and do not stream.
110- }
44+ if ( on_chunk ) {
45+ on_chunk ( new_content )
11146 }
112- // --- End think block handling logic ---
11347
11448 const current_time = Date . now ( )
11549 if ( current_time - updated_last_log_time >= 1000 ) {
@@ -141,10 +75,7 @@ async function process_stream_chunk(
14175 return {
14276 updated_buffer,
14377 updated_accumulated_content,
144- updated_last_log_time,
145- updated_think_buffer,
146- updated_in_think_block,
147- updated_think_block_ended
78+ updated_last_log_time
14879 }
14980}
15081
@@ -161,9 +92,6 @@ export async function make_api_request(
16192 let accumulated_content = ''
16293 let last_log_time = Date . now ( )
16394 let buffer = ''
164- let think_buffer = ''
165- let in_think_block = false
166- let think_block_ended = false
16795
16896 const response : AxiosResponse < NodeJS . ReadableStream > = await axios . post (
16997 endpoint_url + '/chat/completions' ,
@@ -184,7 +112,7 @@ export async function make_api_request(
184112 }
185113 )
186114
187- response . data . setEncoding ( 'utf8' ) ;
115+ response . data . setEncoding ( 'utf8' )
188116
189117 return new Promise ( ( resolve , reject ) => {
190118 response . data . on ( 'data' , async ( chunk : string ) => {
@@ -193,17 +121,11 @@ export async function make_api_request(
193121 buffer ,
194122 accumulated_content ,
195123 last_log_time ,
196- think_buffer ,
197- in_think_block ,
198- think_block_ended ,
199124 on_chunk
200125 )
201126 buffer = processing_result . updated_buffer
202127 accumulated_content = processing_result . updated_accumulated_content
203128 last_log_time = processing_result . updated_last_log_time
204- think_buffer = processing_result . updated_think_buffer
205- in_think_block = processing_result . updated_in_think_block
206- think_block_ended = processing_result . updated_think_block_ended
207129 } )
208130
209131 response . data . on ( 'end' , ( ) => {
@@ -270,4 +192,4 @@ export async function make_api_request(
270192 } )
271193 return null
272194 }
273- }
195+ }
0 commit comments