#pragma once // Sonar chat completions: POST /v1/sonar (create, stream) and the async // variant (POST/GET /v1/async/sonar). The SDKs call the same operations at // /chat/completions and /async/chat/completions (aliases, see SPEC). #include #include #include #include "llm/api_client.h" #include "vendors/perplexity/sonar_params.h" #include "vendors/perplexity/sonar_result.h" namespace molo::perplexity { enum class AsyncStatus { kCreated, kInProgress, kCompleted, kFailed, kUnknown }; const char* async_status_name(AsyncStatus s); struct AsyncSummary { std::string id, model, status; long created_at = 0, started_at = 0, completed_at = 0, failed_at = 0; static AsyncSummary parse(const Json& doc); AsyncStatus state() const; }; struct AsyncJob : AsyncSummary { std::string error_message; bool has_response = false; SonarResult response; Json raw; static AsyncJob parse(const Json& doc); bool finished() const { return state() == AsyncStatus::kCompleted || state() == AsyncStatus::kFailed; } }; struct AsyncList { std::vector requests; std::string next_token; // the spec documents no request parameter to send it back bool truncated = false; Json raw; }; class Sonar : public llm::Service { public: explicit Sonar(std::shared_ptr client) : llm::Service(std::move(client)) {} SonarResult create(const SonarParams& params); using DeltaSink = std::function; // false: stop early // Streams chunks; returns the folded result. A stream that ends without a // finish_reason / end_of_stream (and was not stopped by the sink) throws 502 // STREAM_TRUNCATED. SonarResult stream(const SonarParams& params, const DeltaSink& on_delta); // Async chat completions. idempotency_key: "" omits it (1..200 chars otherwise). AsyncJob create_async(const SonarParams& params, const std::string& idempotency_key = ""); AsyncList list_async(); AsyncJob get_async(const std::string& request_id); // Polls get_async until COMPLETED or FAILED. interval floor 1 s, wait ceiling 600 s; // throws 504 WAIT_TIMEOUT when the ceiling passes. `sleeper` is injectable for tests. AsyncJob wait_async(const std::string& request_id, int interval_seconds = 5, int max_wait_seconds = 300, const std::function& sleeper = nullptr); }; } // namespace molo::perplexity