@@ -2992,6 +2992,34 @@ int main() {
29922992 assert (extracted.find (" <|tool▁calls▁begin|>" ) == std::string::npos);
29932993 std::cout << " ✅ PASS: DeepSeek R1 content extraction works" << std::endl;
29942994
2995+ // Test streaming finish_reason logic (core of the fix)
2996+ std::cout << " \n 🎯 Testing Streaming finish_reason Logic:" << std::endl;
2997+
2998+ // Test Case 1: Content with tool calls should lead to finish_reason="tool_calls"
2999+ std::string tool_call_content = " functions.get_weather:0{\" location\" : \" Tokyo\" }" ;
3000+ ik_chat_msg msg_with_tools = parse_chat_message_incremental (tool_call_content, false , " kimi-k2" );
3001+ bool should_be_tool_calls = !msg_with_tools.tool_calls .empty ();
3002+ std::string finish_reason_with_tools = should_be_tool_calls ? " tool_calls" : " stop" ;
3003+ assert (finish_reason_with_tools == " tool_calls" );
3004+ std::cout << " ✅ PASS: Content with tool calls -> finish_reason='tool_calls'" << std::endl;
3005+
3006+ // Test Case 2: Content without tool calls should lead to finish_reason="stop"
3007+ std::string regular_content = " This is just regular text without any tool calls." ;
3008+ ik_chat_msg msg_without_tools = parse_chat_message_incremental (regular_content, false , " kimi-k2" );
3009+ bool should_be_stop = msg_without_tools.tool_calls .empty ();
3010+ std::string finish_reason_without_tools = should_be_stop ? " stop" : " tool_calls" ;
3011+ assert (finish_reason_without_tools == " stop" );
3012+ std::cout << " ✅ PASS: Content without tool calls -> finish_reason='stop'" << std::endl;
3013+
3014+ // Test Case 3: Qwen3 XML format tool calls
3015+ std::string qwen3_content = " <tool_call>\n {\" name\" : \" get_weather\" , \" arguments\" : {\" location\" : \" Tokyo\" }}\n </tool_call>" ;
3016+ ik_chat_msg qwen3_msg = parse_chat_message_incremental (qwen3_content, false , " qwen3-7b" );
3017+ bool qwen3_should_be_tool_calls = !qwen3_msg.tool_calls .empty ();
3018+ std::string qwen3_finish_reason = qwen3_should_be_tool_calls ? " tool_calls" : " stop" ;
3019+ assert (qwen3_finish_reason == " tool_calls" );
3020+ std::cout << " ✅ PASS: Qwen3 XML tool calls -> finish_reason='tool_calls'" << std::endl;
3021+
3022+ std::cout << " 🎯 All streaming finish_reason tests passed!" << std::endl;
29953023 } catch (const std::exception& e) {
29963024 std::cout << std::endl;
29973025 std::cout << " ❌ Test failed with exception: " << e.what () << std::endl;
0 commit comments