| 257 | } |
| 258 | |
| 259 | std::optional<common_chat_msg_parser::consume_json_result> common_chat_msg_parser::try_consume_json_with_dumped_args( |
| 260 | const std::vector<std::vector<std::string>> & args_paths, |
| 261 | const std::vector<std::vector<std::string>> & content_paths |
| 262 | ) { |
| 263 | auto partial = try_consume_json(); |
| 264 | if (!partial) { |
| 265 | return std::nullopt; |
| 266 | } |
| 267 | auto is_arguments_path = [&](const std::vector<std::string> & path) { |
| 268 | return std::find(args_paths.begin(), args_paths.end(), path) != args_paths.end(); |
| 269 | }; |
| 270 | auto is_content_path = [&](const std::vector<std::string> & path) { |
| 271 | return std::find(content_paths.begin(), content_paths.end(), path) != content_paths.end(); |
| 272 | }; |
| 273 | |
| 274 | if (partial->healing_marker.marker.empty()) { |
| 275 | if (args_paths.empty()) { |
| 276 | // No arguments to dump, and JSON was parsed fully. |
| 277 | return consume_json_result { |
| 278 | partial->json, |
| 279 | /* .is_partial = */ false, |
| 280 | }; |
| 281 | } |
| 282 | if (is_arguments_path({})) { |
| 283 | // Entire JSON is the arguments and was parsed fully. |
| 284 | return consume_json_result { |
| 285 | partial->json.dump(), |
| 286 | /* .is_partial = */ false, |
| 287 | }; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | LOG_DBG("Parsed partial JSON: %s (json_healing_marker: %s)\n", partial->json.dump().c_str(), partial->healing_marker.json_dump_marker.c_str()); |
| 292 | |
| 293 | auto found_healing_marker = false; |
| 294 | std::vector<std::string> path; |
| 295 | std::function<json(const json &)> remove_unsupported_healings_and_dump_args = [&](const json & j) -> json { |
| 296 | if (is_arguments_path(path)) { |
| 297 | auto arguments = j.dump(); |
| 298 | if (is_partial() && !partial->healing_marker.marker.empty()) { |
| 299 | auto idx = arguments.find(partial->healing_marker.json_dump_marker); |
| 300 | if (idx != std::string::npos) { |
| 301 | arguments.resize(idx); |
| 302 | found_healing_marker = true; |
| 303 | } |
| 304 | if (arguments == "\"") { |
| 305 | // This happens because of completing `:"$magic` after `"arguments"` |
| 306 | arguments = ""; |
| 307 | } |
| 308 | } |
| 309 | return arguments; |
| 310 | } |
| 311 | if (is_content_path(path)) { |
| 312 | if (!j.is_string()) { |
| 313 | throw std::runtime_error("Content path must be a string"); |
| 314 | } |
| 315 | std::string str = j; |
| 316 | auto idx = str.find(partial->healing_marker.marker); // not using json_dump_marker as we're inside a string |