feat(clients): openai platform: 188-call table, 217 events, WebSocket sessions, checker, demo
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
#include "openai/platform/realtime_api.h"
|
||||
|
||||
#include "llm/error.h"
|
||||
#include "openai/platform/caps.h"
|
||||
|
||||
namespace molo::openai::platform {
|
||||
|
||||
namespace {
|
||||
[[noreturn]] void bad(const std::string& why) { throw llm::ApiError::invalid("openai", why); }
|
||||
|
||||
void check_sdp(const std::string& sdp) {
|
||||
if (sdp.empty() || sdp.size() > 256 * 1024) bad("sdp offer must be 1 byte to 256 KiB");
|
||||
if (sdp.compare(0, 2, "v=") != 0) bad("sdp offer must start with 'v='");
|
||||
}
|
||||
|
||||
CallAnswer answer_of(const llm::RawResponse& r) {
|
||||
CallAnswer a;
|
||||
a.sdp = r.body;
|
||||
if (auto loc = r.headers.get("Location")) {
|
||||
std::size_t slash = loc->rfind('/');
|
||||
if (slash != std::string::npos) a.call_id = loc->substr(slash + 1);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Json RealtimeApi::create_client_secret(const Json& body) {
|
||||
return invoke(Op::kCreateRealtimeClientSecret, {}, &body);
|
||||
}
|
||||
|
||||
Json RealtimeApi::create_translation_client_secret(const Json& body) {
|
||||
return invoke(Op::kCreateRealtimeTranslationClientSecret, {}, &body);
|
||||
}
|
||||
|
||||
Json RealtimeApi::create_session(const Json& body) { return invoke(Op::kCreateRealtimeSession, {}, &body); }
|
||||
|
||||
Json RealtimeApi::create_transcription_session(const Json& body) {
|
||||
return invoke(Op::kCreateRealtimeTranscriptionSession, {}, &body);
|
||||
}
|
||||
|
||||
CallAnswer RealtimeApi::create_call(const std::string& sdp_offer) {
|
||||
check_sdp(sdp_offer);
|
||||
return answer_of(invoke_raw(Op::kCreateRealtimeCall, {}, Json::object(), &sdp_offer, "application/sdp"));
|
||||
}
|
||||
|
||||
CallAnswer RealtimeApi::create_call(const std::string& sdp_offer, const Json& session) {
|
||||
check_sdp(sdp_offer);
|
||||
Form f;
|
||||
f.text("sdp", sdp_offer);
|
||||
f.text("session", session.dump());
|
||||
Calls::check_form(Op::kCreateRealtimeCall, f.names());
|
||||
std::string encoded = f.body().encode();
|
||||
return answer_of(invoke_raw(Op::kCreateRealtimeCall, {}, Json::object(), &encoded, f.body().content_type()));
|
||||
}
|
||||
|
||||
Json RealtimeApi::accept_call(const std::string& call_id, const Json& body) {
|
||||
return invoke(Op::kAcceptRealtimeCall, {{"call_id", call_id}}, &body);
|
||||
}
|
||||
|
||||
Json RealtimeApi::refer_call(const std::string& call_id, const std::string& target_uri) {
|
||||
if (target_uri.empty() || target_uri.size() > 512) bad("target_uri must be 1 to 512 characters");
|
||||
if (target_uri.compare(0, 4, "tel:") != 0 && target_uri.compare(0, 4, "sip:") != 0 &&
|
||||
target_uri.compare(0, 5, "sips:") != 0)
|
||||
bad("target_uri must start with tel:, sip: or sips:");
|
||||
Json body = Json::object();
|
||||
body.set("target_uri", Json(target_uri));
|
||||
return invoke(Op::kReferRealtimeCall, {{"call_id", call_id}}, &body);
|
||||
}
|
||||
|
||||
Json RealtimeApi::reject_call(const std::string& call_id, int status_code) {
|
||||
Json body = Json::object();
|
||||
if (status_code != 0) {
|
||||
if (status_code < 400 || status_code > 699) bad("status_code must be a SIP failure code, 400 to 699");
|
||||
body.set("status_code", Json(status_code));
|
||||
}
|
||||
return invoke(Op::kRejectRealtimeCall, {{"call_id", call_id}}, &body);
|
||||
}
|
||||
|
||||
Json RealtimeApi::hangup_call(const std::string& call_id) {
|
||||
return invoke(Op::kHangupRealtimeCall, {{"call_id", call_id}});
|
||||
}
|
||||
|
||||
} // namespace molo::openai::platform
|
||||
Reference in New Issue
Block a user