@@ -65,11 +65,8 @@ def detect_and_parse(self, text: str, tools: List[Tool]) -> StreamingParseResult
6565 Returns:
6666 StreamingParseResult with parsed tool calls
6767 """
68- idx = text .find (self .bot_token )
69- normal_text = text [:idx ].strip () if idx != - 1 else text
70-
7168 if self .bot_token not in text :
72- return StreamingParseResult (normal_text = normal_text , calls = [])
69+ return StreamingParseResult (normal_text = text , calls = [])
7370
7471 # Try to use official parser if available
7572 if self .encoding_module and hasattr (self .encoding_module , "parse_tool_calls" ):
@@ -81,10 +78,25 @@ def detect_and_parse(self, text: str, tools: List[Tool]) -> StreamingParseResult
8178 # - index: position after <|DSML|function_calls> opening tag
8279 # - text: the FULL original text (not substring)
8380 # It will parse from index position until </|DSML|function_calls>
81+ # Returns: (final_index, stop_token, tool_calls)
82+ # - final_index: position after </|DSML|function_calls> closing tag
8483 parse_start_idx = start_idx + len (self .bot_token )
8584
8685 # Call official parser with correct parameters
87- _ , _ , tool_calls = self .encoding_module .parse_tool_calls (parse_start_idx , text )
86+ final_index , stop_token , tool_calls = self .encoding_module .parse_tool_calls (parse_start_idx , text )
87+
88+ # Construct normal_text from content before and after tool calls
89+ # This preserves any text after the tool calls section
90+ text_before = text [:start_idx ].strip ()
91+ text_after = text [final_index :].strip () if final_index < len (text ) else ""
92+
93+ # Combine before and after text
94+ normal_text_parts = []
95+ if text_before :
96+ normal_text_parts .append (text_before )
97+ if text_after :
98+ normal_text_parts .append (text_after )
99+ normal_text = "\n \n " .join (normal_text_parts )
88100
89101 # Convert to ToolCallItem format
90102 calls = []
@@ -100,6 +112,8 @@ def detect_and_parse(self, text: str, tools: List[Tool]) -> StreamingParseResult
100112 logger .warning (f"Official parser failed, falling back to regex: { e } " )
101113
102114 # Fallback to regex-based parsing
115+ idx = text .find (self .bot_token )
116+ normal_text = text [:idx ].strip () if idx != - 1 else text
103117 return self ._parse_with_regex (text , tools , normal_text )
104118
105119 def _parse_with_regex (self , text : str , tools : List [Tool ], normal_text : str ) -> StreamingParseResult :
0 commit comments