| 151 | } |
| 152 | |
| 153 | std::vector<common_chat_msg_diff> common_chat_msg_diff::compute_diffs(const common_chat_msg & msg_prv, |
| 154 | const common_chat_msg & msg_new) { |
| 155 | std::vector<common_chat_msg_diff> diffs; |
| 156 | if (msg_new.tool_calls.size() > msg_prv.tool_calls.size()) { |
| 157 | diffs.reserve(msg_new.tool_calls.size() - msg_prv.tool_calls.size() + 3); |
| 158 | } else { |
| 159 | diffs.reserve(3); |
| 160 | } |
| 161 | |
| 162 | // TODO: these can become expensive for long messages - how to optimize? |
| 163 | if (msg_prv.reasoning_content != msg_new.reasoning_content) { |
| 164 | auto & diff = diffs.emplace_back(); |
| 165 | diff.reasoning_content_delta = string_diff(msg_prv.reasoning_content, msg_new.reasoning_content); |
| 166 | } |
| 167 | if (msg_prv.content != msg_new.content) { |
| 168 | auto & diff = diffs.emplace_back(); |
| 169 | diff.content_delta = string_diff(msg_prv.content, msg_new.content); |
| 170 | } |
| 171 | |
| 172 | if (msg_new.tool_calls.size() < msg_prv.tool_calls.size()) { |
| 173 | std::string err = "Invalid diff: now finding less tool calls!\n"; |
| 174 | err += " Previous (" + std::to_string(msg_prv.tool_calls.size()) + "):\n"; |
| 175 | for (const auto & tc : msg_prv.tool_calls) { |
| 176 | err += " - name: '" + tc.name + "', args: '" + tc.arguments + "'\n"; |
| 177 | } |
| 178 | err += " Current (" + std::to_string(msg_new.tool_calls.size()) + "):\n"; |
| 179 | for (const auto & tc : msg_new.tool_calls) { |
| 180 | err += " - name: '" + tc.name + "', args: '" + tc.arguments + "'\n"; |
| 181 | } |
| 182 | err += " Current msg text content:\n" + msg_new.content + "\n"; |
| 183 | throw std::runtime_error(err); |
| 184 | } |
| 185 | |
| 186 | if (!msg_prv.tool_calls.empty()) { |
| 187 | const auto idx = msg_prv.tool_calls.size() - 1; |
| 188 | const auto & pref = msg_prv.tool_calls[idx]; |
| 189 | const auto & newf = msg_new.tool_calls[idx]; |
| 190 | // Allow tool name to change during incremental parsing: |
| 191 | // - empty -> non-empty (initial discovery) |
| 192 | // - prefix -> longer string (name grows as more input is parsed) |
| 193 | if (pref.name != newf.name && !pref.name.empty() && !newf.name.empty()) { |
| 194 | // Check if one is a prefix of the other (for incremental parsing where names grow or shrink) |
| 195 | bool is_prefix = (newf.name.rfind(pref.name, 0) == 0); |
| 196 | if (!is_prefix) { |
| 197 | LOG_ERR("Tool call mismatch: prev='%s' new='%s'\n", pref.name.c_str(), newf.name.c_str()); |
| 198 | throw std::runtime_error("Invalid diff: tool call mismatch!"); |
| 199 | } |
| 200 | } |
| 201 | const auto args_diff = string_diff(pref.arguments, newf.arguments); |
| 202 | if (!args_diff.empty() || pref.id != newf.id || pref.name != newf.name) { |
| 203 | auto & diff = diffs.emplace_back(); |
| 204 | diff.tool_call_index = idx; |
| 205 | if (pref.id != newf.id || pref.name != newf.name) { |
| 206 | diff.tool_call_delta.id = newf.id; |
| 207 | diff.tool_call_delta.name = newf.name; |
| 208 | } |
| 209 | diff.tool_call_delta.arguments = args_diff; |
| 210 | } |
nothing calls this directly
no test coverage detected