@@ -352,43 +352,48 @@ def _update_think_state(self, state: dict[str, Any]):
352
352
if not state ["in_end" ]:
353
353
if recent_start_pos >= 0 and recent_end_pos >= 0 :
354
354
if recent_start_pos > recent_end_pos :
355
- # Case: ...<end>...<start>...
355
+ # Case: ...<end>...<start>... - entering think mode
356
356
absolute_start_pos = check_start_idx + recent_start_pos
357
+ new_think_count = current_length - (absolute_start_pos +
358
+ start_len )
357
359
state ["in_think" ] = True
358
- state ["think_count" ] = current_length - (
359
- absolute_start_pos + start_len )
360
- state ["check_count_down" ] = state [
361
- "thinking_token_budget" ] - state ["think_count" ]
360
+ state ["think_count" ] = new_think_count
362
361
else :
363
- # Case: ...<start>...<end>...
362
+ # Case: ...<start>...<end>... - exiting think mode
364
363
state ["in_think" ] = False
365
364
state ["think_count" ] = 0
366
365
elif recent_start_pos >= 0 :
367
- # Found think start in recent tokens
366
+ # Found think start - entering think mode
368
367
absolute_start_pos = check_start_idx + recent_start_pos
368
+ new_think_count = current_length - (absolute_start_pos +
369
+ start_len )
369
370
state ["in_think" ] = True
370
- state ["think_count" ] = current_length - (absolute_start_pos +
371
- start_len )
372
- state ["check_count_down" ] = state [
373
- "thinking_token_budget" ] - state ["think_count" ]
371
+ state ["think_count" ] = new_think_count
374
372
elif recent_end_pos >= 0 :
375
- # Found think end in recent tokens
373
+ # Found think end - exiting think mode
376
374
state ["in_think" ] = False
377
375
state ["think_count" ] = 0
378
- state ["check_count_down" ] = state ["thinking_token_budget" ]
379
376
elif state ["in_think" ]:
380
377
# Continue thinking mode, increment count by new tokens
381
378
state ["think_count" ] += len (new_tokens )
382
379
383
- if state ["in_think" ] and state ["think_count" ] \
384
- >= state ["thinking_token_budget" ]:
385
- state .update ({
386
- "in_think" : False ,
387
- "in_end" : True ,
388
- "end_count" : 0 ,
389
- "check_count_down" : state ["thinking_token_budget" ]
390
- })
380
+ # Set countdown based on current state
381
+ if state ["in_think" ]:
382
+ remaining_budget = max (
383
+ 0 , state ["thinking_token_budget" ] - state ["think_count" ])
384
+ state ["check_count_down" ] = remaining_budget
385
+ else :
386
+ state ["check_count_down" ] = state ["thinking_token_budget" ]
387
+
388
+ # Check if need to transition to end mode
389
+ if state ["in_think" ] and state ["think_count" ] >= state [
390
+ "thinking_token_budget" ]:
391
+ state ["in_think" ] = False
392
+ state ["in_end" ] = True
393
+ state ["end_count" ] = 0
394
+ state ["check_count_down" ] = state ["thinking_token_budget" ]
391
395
else :
396
+ # In end mode
392
397
state ["end_count" ] += 1
393
398
if state ["end_count" ] >= len (self .think_end_token_ids ):
394
399
state .update ({
@@ -503,4 +508,3 @@ def process_dict_updates(
503
508
req_entries [a_index ] = b_entry
504
509
505
510
return updated
506
-
0 commit comments