Browse Source

fix: correctly skip thinking tags in stream handler

CassiopeiaCode 2 tháng trước cách đây
mục cha
commit
8a6c52af28
1 tập tin đã thay đổi với 7 bổ sung4 xóa
  1. 7 4
      claude_stream.py

+ 7 - 4
claude_stream.py

@@ -17,6 +17,9 @@ try:
 except Exception:
     ENCODING = None
 
+THINKING_START_TAG = "<thinking>"
+THINKING_END_TAG = "</thinking>"
+
 def count_tokens(text: str) -> int:
     """Counts tokens with tiktoken."""
     if not text or not ENCODING:
@@ -111,7 +114,7 @@ class ClaudeStreamHandler:
                 while pos < len(self.think_buffer):
                     if not self.in_think_block:
                         # Look for <thinking> tag
-                        think_start = self.think_buffer.find("<thinking>", pos)
+                        think_start = self.think_buffer.find(THINKING_START_TAG, pos)
                         if think_start != -1:
                             # Send text before <thinking>
                             before_text = self.think_buffer[pos:think_start]
@@ -136,7 +139,7 @@ class ClaudeStreamHandler:
                             self.content_block_started = True
                             self.content_block_stop_sent = False
                             self.in_think_block = True
-                            pos = think_start + 7  # Skip <thinking>
+                            pos = think_start + len(THINKING_START_TAG)
                         else:
                             # No <thinking> found, send remaining as text
                             remaining = self.think_buffer[pos:]
@@ -151,7 +154,7 @@ class ClaudeStreamHandler:
                             break
                     else:
                         # Look for </thinking> tag
-                        think_end = self.think_buffer.find("</thinking>", pos)
+                        think_end = self.think_buffer.find(THINKING_END_TAG, pos)
                         if think_end != -1:
                             # Send thinking content
                             thinking_text = self.think_buffer[pos:think_end]
@@ -163,7 +166,7 @@ class ClaudeStreamHandler:
                             self.content_block_stop_sent = True
                             self.content_block_start_sent = False
                             self.in_think_block = False
-                            pos = think_end + 8  # Skip </thinking>
+                            pos = think_end + len(THINKING_END_TAG)
                         else:
                             # No </thinking> yet, send as thinking
                             remaining = self.think_buffer[pos:]