task_result_state
| 157 | // task_result_state |
| 158 | // |
| 159 | common_chat_msg task_result_state::update_chat_msg( |
| 160 | const std::string & text_added, |
| 161 | bool is_partial, |
| 162 | std::vector<common_chat_msg_diff> & diffs, |
| 163 | bool filter_tool_calls) { |
| 164 | generated_text += text_added; |
| 165 | auto msg_prv_copy = chat_msg; |
| 166 | //SRV_DBG("Parsing chat message: %s\n", generated_text.c_str()); |
| 167 | auto new_msg = common_chat_parse( |
| 168 | generated_text, |
| 169 | is_partial, |
| 170 | chat_parser_params); |
| 171 | if (!new_msg.empty()) { |
| 172 | new_msg.set_tool_call_ids(generated_tool_call_ids, gen_tool_call_id); |
| 173 | chat_msg = new_msg; |
| 174 | auto all_diffs = common_chat_msg_diff::compute_diffs(msg_prv_copy, chat_msg); |
| 175 | |
| 176 | if (!filter_tool_calls) { |
| 177 | diffs = std::move(all_diffs); |
| 178 | } else { |
| 179 | for (auto & d : all_diffs) { |
| 180 | // If this is a new type of delta, flush all currently pending tool call names |
| 181 | for (size_t i = 0; i < chat_msg.tool_calls.size(); ++i) { |
| 182 | if (sent_tool_call_names.count(i) || chat_msg.tool_calls[i].name.empty()) { |
| 183 | continue; |
| 184 | } |
| 185 | if (d.tool_call_index != i || !d.tool_call_delta.arguments.empty()) { |
| 186 | common_chat_msg_diff header; |
| 187 | header.tool_call_index = i; |
| 188 | header.tool_call_delta.id = chat_msg.tool_calls[i].id; |
| 189 | header.tool_call_delta.name = chat_msg.tool_calls[i].name; |
| 190 | diffs.push_back(std::move(header)); |
| 191 | sent_tool_call_names.insert(i); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if (d.tool_call_index == std::string::npos) { |
| 196 | diffs.push_back(std::move(d)); |
| 197 | } else { |
| 198 | size_t i = d.tool_call_index; |
| 199 | if (sent_tool_call_names.count(i)) { |
| 200 | if (!d.tool_call_delta.arguments.empty()) { |
| 201 | d.tool_call_delta.name = ""; |
| 202 | d.tool_call_delta.id = ""; |
| 203 | diffs.push_back(std::move(d)); |
| 204 | } |
| 205 | } else { |
| 206 | // Not sent yet. |
| 207 | if (!d.tool_call_delta.arguments.empty() || !is_partial) { |
| 208 | d.tool_call_delta.name = chat_msg.tool_calls[i].name; |
| 209 | d.tool_call_delta.id = chat_msg.tool_calls[i].id; |
| 210 | diffs.push_back(std::move(d)); |
| 211 | sent_tool_call_names.insert(i); |
| 212 | } else { |
| 213 | // Suppress |
| 214 | } |
| 215 | } |
| 216 | } |
no test coverage detected