@@ -2154,24 +2154,34 @@ static void common_chat_parse_hermes_2_pro(common_chat_msg_parser & builder) {
21542154            }
21552155        } else  {
21562156            auto  prelude = res->prelude ;
2157-             bool  prelude_wrappers_only = true ;
2158-             auto  trimmed_prelude = string_strip (prelude);
2159-             while  (!trimmed_prelude.empty ()) {
2157+             size_t  suffix_to_remove = 0 ;
2158+             size_t  cursor = prelude.size ();
2159+             while  (cursor > 0 ) {
2160+                 size_t  ws = cursor;
2161+                 while  (ws > 0  && std::isspace (static_cast <unsigned  char >(prelude[ws - 1 ]))) {
2162+                     --ws;
2163+                 }
2164+                 size_t  trimmed_end = ws;
21602165                bool  matched_wrapper = false ;
21612166                for  (const  auto  & tag : wrapper_open_tags) {
2162-                     if  ( string_starts_with (trimmed_prelude,  tag)) { 
2163-                         trimmed_prelude =  string_strip (trimmed_prelude. substr ( tag. size ())); 
2167+                     const   size_t  tag_len =  tag. size (); 
2168+                     if  (trimmed_end >= tag_len && prelude. compare (trimmed_end - tag_len, tag_len,  tag) ==  0 ) { 
21642169                        matched_wrapper = true ;
2170+                         suffix_to_remove += cursor - (trimmed_end - tag_len);
2171+                         cursor = trimmed_end - tag_len;
21652172                        break ;
21662173                    }
21672174                }
21682175                if  (!matched_wrapper) {
2169-                     prelude_wrappers_only = false ;
21702176                    break ;
21712177                }
21722178            }
2173-             if  (!prelude.empty () && prelude_wrappers_only) {
2174-                 builder.remove_content_suffix (prelude.size ());
2179+             if  (suffix_to_remove > 0 ) {
2180+                 while  (cursor > 0  && std::isspace (static_cast <unsigned  char >(prelude[cursor - 1 ]))) {
2181+                     --cursor;
2182+                     ++suffix_to_remove;
2183+                 }
2184+                 builder.remove_content_suffix (suffix_to_remove);
21752185            }
21762186
21772187            auto  function_name = builder.str (res->groups [4 ]);
@@ -2181,7 +2191,13 @@ static void common_chat_parse_hermes_2_pro(common_chat_msg_parser & builder) {
21812191            GGML_ASSERT (!function_name.empty ());
21822192
21832193            close_tag = " </function>" 
2184-             const  bool  had_block_start = res->prelude .find (" ```" 
2194+             bool  had_block_start = false ;
2195+             {
2196+                 const  auto  backtick_pos = res->prelude .rfind (" ```" 
2197+                 if  (backtick_pos != std::string::npos) {
2198+                     had_block_start = true ;
2199+                 }
2200+             }
21852201
21862202            if  (auto  arguments = builder.try_consume_json_with_dumped_args ({{}})) {
21872203                if  (!builder.add_tool_call (function_name, " " value ) || arguments->is_partial ) {
0 commit comments