Files
moloapp/src/vendors/azure/realtime.h
T

86 lines
3.4 KiB
C++

#pragma once
// Realtime (gpt-realtime, gpt-4o-realtime-preview): the REST creators of
// sessions and ephemeral client secrets, the WebSocket URL builder, and a
// small WebSocket session over the RFC 6455 client of src/google/live
// (WsClient, reused, not copied).
//
// v1 wss://<host>/openai/v1/realtime?model=<deployment>
// dated wss://<host>/openai/realtime?api-version=<v>&deployment=<deployment>
// The credential travels in a header (api-key, or Authorization: Bearer for
// Entra), never in the URL.
#include <functional>
#include <memory>
#include <string>
#include "google/live/ws_client.h"
#include "google/live/ws_stream.h"
#include "vendors/azure/service.h"
namespace molo::azure {
class RealtimeService : public AzureService {
public:
explicit RealtimeService(std::shared_ptr<AzureClient> azure)
: AzureService(std::move(azure), Surface::kOpenAi) {}
// Ephemeral client secret for browsers and devices (v1 API). The answer
// holds the secret in `value`: hand it to the client, never log it.
// body: session, expires_after ({"anchor":"created_at","seconds":10..7200}).
clients::body::Json create_client_secret(const clients::body::Json& body = clients::body::Json::object());
// Session creator: /realtime/sessions (v1) or /realtimeapi/sessions (dated).
clients::body::Json create_session(const clients::body::Json& body);
clients::body::Json create_transcription_session(
const clients::body::Json& body = clients::body::Json::object());
// SIP / WebRTC call control (v1 API). Destructive ones have no defaults.
clients::body::Json accept_call(const std::string& call_id, const clients::body::Json& body);
clients::body::Json refer_call(const std::string& call_id, const std::string& target_uri);
clients::body::Json reject_call(const std::string& call_id, int status_code);
clients::body::Json hangup_call(const std::string& call_id);
};
class RealtimeUrl {
public:
// The WebSocket URL without credentials. Throws on a bad deployment.
static std::string build(const Settings& settings, const std::string& deployment);
};
struct RealtimeStats {
std::size_t events = 0;
std::size_t bytes = 0;
std::string stop_reason; // sink_stopped, closed, timeout, event_cap, byte_cap
int close_code = 0;
};
class RealtimeSocket {
public:
static constexpr std::size_t kMaxEventBytes = 1024 * 1024;
static constexpr std::size_t kMaxSessionEvents = 100000;
static constexpr std::size_t kMaxSessionBytes = 64u * 1024u * 1024u;
static constexpr int kMaxSessionSeconds = 3600;
using Sink = std::function<bool(const clients::body::Json& event)>; // false: stop
explicit RealtimeSocket(std::shared_ptr<AzureClient> azure, google::live::TlsFactory tls = nullptr);
~RealtimeSocket();
bool connected() const { return ws_ != nullptr; }
// Connects and upgrades. Throws NotConfiguredError (503) before any traffic
// when there is no credential or endpoint.
void connect(const std::string& deployment);
// Sends one client event (a JSON object with a string `type`).
void send(const clients::body::Json& event);
// Receives until the sink returns false, the server closes, `seconds` pass
// (1..3600) or a cap is hit.
RealtimeStats run(const Sink& sink, int seconds);
void close();
private:
std::shared_ptr<AzureClient> azure_;
google::live::TlsFactory tls_;
std::unique_ptr<google::live::WsClient> ws_;
};
} // namespace molo::azure