Files
moloapp/src/vendors/perplexity/sonar.h
T
retoorandClaude Sonnet 5.5 3404392887 feat(clients): perplexity: Sonar, Search, Agent, embeddings, decisions, batches, skills, orchestrator
Package molo::perplexity covering all 39 operations of the five published
OpenAPI documents, PerplexityChatModel, demo routes under /ai/perplexity,
check_spec.py, SPEC.md, 46 unit cases and 111 route checks.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 15:01:55 +01:00

66 lines
2.4 KiB
C++

#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 <functional>
#include <string>
#include <vector>
#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<AsyncSummary> 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<llm::ApiClient> client) : llm::Service(std::move(client)) {}
SonarResult create(const SonarParams& params);
using DeltaSink = std::function<bool(const SonarDelta&)>; // 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<void(int)>& sleeper = nullptr);
};
} // namespace molo::perplexity