#include "openrouter/chat_result.h" #include "llm/error.h" #include "openrouter/call_plan.h" #include "openrouter/caps.h" #include "openrouter/errors.h" namespace molo::openrouter { using clients::body::Json; namespace { std::string str(const Json& o, const char* k) { const Json* v = o.is_object() ? o.find(k) : nullptr; return v && v->is_string() ? v->as_string() : ""; } int integer(const Json* o, const char* k) { const Json* v = o && o->is_object() ? o->find(k) : nullptr; return v && v->is_number() ? static_cast(v->as_int()) : 0; } [[noreturn]] void bad_response(const std::string& why) { throw llm::ApiError(kVendor, 502, "BAD_RESPONSE", why); } } // namespace llm::Usage parse_usage(const Json& u) { llm::Usage out; if (!u.is_object()) return out; out.input_tokens = integer(&u, "prompt_tokens"); out.output_tokens = integer(&u, "completion_tokens"); out.total_tokens = integer(&u, "total_tokens"); if (out.total_tokens == 0) out.total_tokens = out.input_tokens + out.output_tokens; out.cached_tokens = integer(u.find("prompt_tokens_details"), "cached_tokens"); out.reasoning_tokens = integer(u.find("completion_tokens_details"), "reasoning_tokens"); return out; } Json parse_tool_arguments(const std::string& text) { if (text.find_first_not_of(" \t\r\n") == std::string::npos) return Json::object(); try { Json j = Json::parse(text); if (j.is_object()) return j; } catch (const std::exception&) { } Json bad = Json::object(); bad.set("_invalid_arguments", Json(text)); return bad; } ChatResult ChatResult::parse(const Json& doc) { if (!doc.is_object()) bad_response("chat response is not a JSON object"); throw_if_error_body(doc); ChatResult r; r.raw = doc; r.id = str(doc, "id"); r.model = str(doc, "model"); r.provider = str(doc, "provider"); r.service_tier = str(doc, "service_tier"); if (const Json* c = doc.find("created"); c && c->is_number()) r.created = c->as_int(); if (const Json* m = doc.find("openrouter_metadata"); m && m->is_object()) r.openrouter_metadata = *m; if (const Json* u = doc.find("usage"); u && u->is_object()) { r.usage = parse_usage(*u); if (const Json* c = u->find("cost"); c && c->is_number()) r.cost = c->as_number(); if (const Json* b = u->find("is_byok"); b && b->is_bool()) r.is_byok = b->as_bool(); if (const Json* d = u->find("cost_details"); d && d->is_object()) r.cost_details = *d; } const Json* choices = doc.find("choices"); if (!choices || !choices->is_array()) bad_response("chat response has no choices array"); if (choices->as_array().size() > Caps::kMaxChoices) bad_response("too many choices"); for (const Json& c : choices->as_array()) { if (!c.is_object()) bad_response("choice is not an object"); ChatChoiceResult cr; cr.index = integer(&c, "index"); cr.finish_reason = str(c, "finish_reason"); cr.native_finish_reason = str(c, "native_finish_reason"); if (const Json* e = c.find("error"); e && e->is_object()) throw ErrorBody::parse(*e).to_api_error(); if (cr.finish_reason == "error") throw llm::ApiError(kVendor, 502, "provider_unavailable", "generation ended with finish_reason error"); if (const Json* m = c.find("message"); m && m->is_object()) { cr.message = *m; if (const Json* content = m->find("content")) { if (content->is_string()) { cr.content = content->as_string(); } else if (content->is_array()) { for (const Json& part : content->as_array()) if (part.is_object() && str(part, "type") == "text") cr.content += str(part, "text"); } } cr.reasoning = str(*m, "reasoning"); cr.refusal = str(*m, "refusal"); if (const Json* rd = m->find("reasoning_details"); rd && rd->is_array()) { if (rd->as_array().size() > Caps::kMaxReasoningDetails) bad_response("too many reasoning details"); cr.reasoning_details = *rd; } if (const Json* tcs = m->find("tool_calls"); tcs && tcs->is_array()) { if (tcs->as_array().size() > Caps::kMaxToolCalls) bad_response("too many tool calls"); for (const Json& tc : tcs->as_array()) { const Json* fn = tc.is_object() ? tc.find("function") : nullptr; if (!fn || !fn->is_object()) bad_response("tool call without function"); llm::ToolCall call; call.id = str(tc, "id"); call.name = str(*fn, "name"); call.arguments = parse_tool_arguments(str(*fn, "arguments")); cr.tool_calls.push_back(std::move(call)); } } } r.choices.push_back(std::move(cr)); } return r; } const ChatChoiceResult& ChatResult::first() const { if (choices.empty()) bad_response("chat response has no choice"); return choices.front(); } std::string ChatResult::text() const { return choices.empty() ? "" : choices.front().content; } std::string ChatResult::finish_reason() const { return choices.empty() ? "" : choices.front().finish_reason; } } // namespace molo::openrouter