Files
moloapp/src/openrouter/stream_summary.h
T

55 lines
1.5 KiB
C++

#pragma once
// Stream parsers for the two non-chat inference skins: the Responses API
// (POST /responses, "beta") and the Anthropic-compatible Messages API
// (POST /messages). Event type names come from the spec schemas
// StreamEvents and MessagesStreamEvents. Each parser folds the events into a
// StreamSummary (text, reasoning, usage, terminal status or error) without
// touching the network.
#include <map>
#include <optional>
#include <string>
#include "clients/body/json.h"
#include "llm/chat_types.h"
#include "llm/sse.h"
#include "openrouter/errors.h"
namespace molo::openrouter {
struct StreamSummary {
std::string id;
std::string model;
std::string text;
std::string reasoning;
std::string status; // responses: completed incomplete failed; messages: stop_reason
std::optional<llm::Usage> usage;
std::optional<ErrorBody> error;
std::map<std::string, int> event_counts; // by event type
std::size_t events = 0;
};
class ResponsesStreamParser {
public:
// Returns the text delta this event carried ("" when none).
std::string feed(const llm::SseEvent& event);
const StreamSummary& summary() const { return summary_; }
void throw_if_errored() const;
private:
StreamSummary summary_;
};
class MessagesStreamParser {
public:
std::string feed(const llm::SseEvent& event);
const StreamSummary& summary() const { return summary_; }
void throw_if_errored() const;
private:
StreamSummary summary_;
};
} // namespace molo::openrouter