@@ -724,16 +724,10 @@ inline void parse_msg_with_xml_tool_calls(common_chat_msg_parser & builder, cons
724724 if (reasoning_unclosed) {
725725 if (auto pos = content.find (end_think); pos == std::string::npos && builder.pos () != builder.input ().size ()) {
726726 unclosed_reasoning_content += content;
727- if (form.allow_toolcall_in_think ) {
728- builder.move_to (tc->groups [0 ].begin );
729- if (!builder.try_consume_xml_tool_calls (form)) {
730- unclosed_reasoning_content += tool_call_start;
731- builder.move_to (tc->groups [0 ].end );
732- }
733- } else {
727+ if (!(form.allow_toolcall_in_think && tc)) {
734728 unclosed_reasoning_content += tool_call_start;
729+ continue ;
735730 }
736- continue ;
737731 } else {
738732 reasoning_unclosed = false ;
739733 std::string reasoning_content;
@@ -781,8 +775,12 @@ inline void parse_msg_with_xml_tool_calls(common_chat_msg_parser & builder, cons
781775 }
782776 } else {
783777 // This <tool_call> start is in thinking block, skip this tool call
784- auto pos = think_start + start_think.size ();
785- unclosed_reasoning_content = content.substr (pos) + tool_call_start;
778+ // This <tool_call> start is in thinking block
779+ if (form.allow_toolcall_in_think ) {
780+ unclosed_reasoning_content = content.substr (think_start + start_think.size ());
781+ } else {
782+ unclosed_reasoning_content = content.substr (think_start + start_think.size ()) + tool_call_start;
783+ }
786784 reasoning_unclosed = true ;
787785 content.resize (think_start);
788786 toolcall_in_think = true ;
@@ -805,22 +803,43 @@ inline void parse_msg_with_xml_tool_calls(common_chat_msg_parser & builder, cons
805803 }
806804
807805 // remove potential partial suffix
808- if (content.size () > 0 && builder.pos () == builder.input ().size () && unclosed_reasoning_content.empty ()) {
809- rstrip (content);
810- trim_potential_partial_word (content);
811- rstrip (content);
806+ if (builder.pos () == builder.input ().size ()) {
807+ if (unclosed_reasoning_content.empty ()) {
808+ rstrip (content);
809+ trim_potential_partial_word (content);
810+ rstrip (content);
811+ } else {
812+ rstrip (unclosed_reasoning_content);
813+ trim_potential_partial_word (unclosed_reasoning_content);
814+ rstrip (unclosed_reasoning_content);
815+ }
816+ }
817+
818+ // consume unclosed_reasoning_content if allow_toolcall_in_think is set
819+ if (form.allow_toolcall_in_think && !unclosed_reasoning_content.empty ()) {
820+ if (builder.syntax ().reasoning_format != COMMON_REASONING_FORMAT_NONE && !builder.syntax ().reasoning_in_content ) {
821+ builder.add_reasoning_content (unclosed_reasoning_content);
822+ } else {
823+ if (content.empty ()) {
824+ content = start_think + unclosed_reasoning_content;
825+ } else {
826+ content += " \n\n " + start_think;
827+ content += unclosed_reasoning_content;
828+ }
829+ }
830+ unclosed_reasoning_content.clear ();
812831 }
813832
814833 // Add content
815- if (content.size () != 0 ) {
834+ if (! content.empty () ) {
816835 // If there are multiple content blocks
817836 if (builder.syntax ().reasoning_format != COMMON_REASONING_FORMAT_NONE && !builder.syntax ().reasoning_in_content && builder.result ().content .size () != 0 ) {
818837 builder.add_content (" \n\n " );
819838 }
820839 builder.add_content (content);
821840 }
822841
823- // This <tool_call> start is in thinking block, skip this tool call
842+ // This <tool_call> start is in thinking block and toolcall_in_think not set , skip this tool call
824843 if (toolcall_in_think && !form.allow_toolcall_in_think ) {
825844 continue ;
826845 }
@@ -829,7 +848,7 @@ inline void parse_msg_with_xml_tool_calls(common_chat_msg_parser & builder, cons
829848 if (!tc) {
830849 GGML_ASSERT (builder.pos () == builder.input ().size ());
831850 GGML_ASSERT (unclosed_reasoning_content.empty ());
832- GGML_ASSERT (!reasoning_unclosed);
851+ if (!form. allow_toolcall_in_think ) GGML_ASSERT (!reasoning_unclosed);
833852 break ;
834853 }
835854
@@ -854,7 +873,6 @@ inline void parse_msg_with_xml_tool_calls(common_chat_msg_parser & builder, cons
854873
855874/* *
856875 * Parse content uses reasoning and XML-Style tool call
857- * TODO: Note that form.allow_toolcall_in_think is not tested yet. If anyone confirms it works, this comment can be removed.
858876 */
859877void common_chat_msg_parser::consume_reasoning_with_xml_tool_calls (const struct xml_tool_call_format & form, const std::string & start_think, const std::string & end_think) {
860878 parse_msg_with_xml_tool_calls (*this , form, start_think, end_think);
0 commit comments