@@ -417,43 +417,48 @@ def _update_think_state(self, state: dict[str, Any]):
417
417
if not state ["in_end" ]:
418
418
if recent_start_pos >= 0 and recent_end_pos >= 0 :
419
419
if recent_start_pos > recent_end_pos :
420
- # Case: ...<end>...<start>...
420
+ # Case: ...<end>...<start>... - entering think mode
421
421
absolute_start_pos = check_start_idx + recent_start_pos
422
+ new_think_count = current_length - (absolute_start_pos +
423
+ start_len )
422
424
state ["in_think" ] = True
423
- state ["think_count" ] = current_length - (
424
- absolute_start_pos + start_len )
425
- state ["check_count_down" ] = state [
426
- "thinking_token_budget" ] - state ["think_count" ]
425
+ state ["think_count" ] = new_think_count
427
426
else :
428
- # Case: ...<start>...<end>...
427
+ # Case: ...<start>...<end>... - exiting think mode
429
428
state ["in_think" ] = False
430
429
state ["think_count" ] = 0
431
430
elif recent_start_pos >= 0 :
432
- # Found think start in recent tokens
431
+ # Found think start - entering think mode
433
432
absolute_start_pos = check_start_idx + recent_start_pos
433
+ new_think_count = current_length - (absolute_start_pos +
434
+ start_len )
434
435
state ["in_think" ] = True
435
- state ["think_count" ] = current_length - (absolute_start_pos +
436
- start_len )
437
- state ["check_count_down" ] = state [
438
- "thinking_token_budget" ] - state ["think_count" ]
436
+ state ["think_count" ] = new_think_count
439
437
elif recent_end_pos >= 0 :
440
- # Found think end in recent tokens
438
+ # Found think end - exiting think mode
441
439
state ["in_think" ] = False
442
440
state ["think_count" ] = 0
443
- state ["check_count_down" ] = state ["thinking_token_budget" ]
444
441
elif state ["in_think" ]:
445
442
# Continue thinking mode, increment count by new tokens
446
443
state ["think_count" ] += len (new_tokens )
447
444
448
- if state ["in_think" ] and state ["think_count" ] \
449
- >= state ["thinking_token_budget" ]:
450
- state .update ({
451
- "in_think" : False ,
452
- "in_end" : True ,
453
- "end_count" : 0 ,
454
- "check_count_down" : state ["thinking_token_budget" ]
455
- })
445
+ # Set countdown based on current state
446
+ if state ["in_think" ]:
447
+ remaining_budget = max (
448
+ 0 , state ["thinking_token_budget" ] - state ["think_count" ])
449
+ state ["check_count_down" ] = remaining_budget
450
+ else :
451
+ state ["check_count_down" ] = state ["thinking_token_budget" ]
452
+
453
+ # Check if need to transition to end mode
454
+ if state ["in_think" ] and state ["think_count" ] >= state [
455
+ "thinking_token_budget" ]:
456
+ state ["in_think" ] = False
457
+ state ["in_end" ] = True
458
+ state ["end_count" ] = 0
459
+ state ["check_count_down" ] = state ["thinking_token_budget" ]
456
460
else :
461
+ # In end mode
457
462
state ["end_count" ] += 1
458
463
if state ["end_count" ] >= len (self .think_end_token_ids ):
459
464
state .update ({
0 commit comments