72 lines
2.3 KiB
C++
72 lines
2.3 KiB
C++
#pragma once
|
|
|
|
#include "AudioInterface.hpp"
|
|
#include <boost/beast/core.hpp>
|
|
#include <boost/beast/websocket.hpp>
|
|
#include <boost/beast/ssl.hpp>
|
|
#include <boost/asio.hpp>
|
|
#include <boost/asio/ssl/stream.hpp>
|
|
#include <boost/asio/ip/tcp.hpp>
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <thread>
|
|
#include <atomic>
|
|
#include <functional>
|
|
|
|
class Conversation {
|
|
public:
|
|
using CallbackAgentResponse = std::function<void(const std::string&)>;
|
|
using CallbackAgentResponseCorrection = std::function<void(const std::string&, const std::string&)>;
|
|
using CallbackUserTranscript = std::function<void(const std::string&)>;
|
|
using CallbackLatencyMeasurement = std::function<void(int)>;
|
|
|
|
Conversation(
|
|
const std::string& agentId,
|
|
bool requiresAuth,
|
|
std::shared_ptr<AudioInterface> audioInterface,
|
|
CallbackAgentResponse callbackAgentResponse = nullptr,
|
|
CallbackAgentResponseCorrection callbackAgentResponseCorrection = nullptr,
|
|
CallbackUserTranscript callbackUserTranscript = nullptr,
|
|
CallbackLatencyMeasurement callbackLatencyMeasurement = nullptr
|
|
);
|
|
|
|
~Conversation();
|
|
|
|
void startSession();
|
|
void endSession();
|
|
std::string waitForSessionEnd();
|
|
|
|
void sendUserMessage(const std::string& text);
|
|
void registerUserActivity();
|
|
void sendContextualUpdate(const std::string& content);
|
|
|
|
private:
|
|
void run();
|
|
void handleMessage(const nlohmann::json& message);
|
|
std::string getWssUrl() const;
|
|
|
|
// networking members
|
|
boost::asio::io_context ioc_;
|
|
boost::asio::ssl::context sslCtx_{boost::asio::ssl::context::tlsv12_client};
|
|
|
|
using tcp = boost::asio::ip::tcp;
|
|
using websocket_t = boost::beast::websocket::stream<
|
|
boost::beast::ssl_stream<tcp::socket>>;
|
|
std::unique_ptr<websocket_t> ws_;
|
|
|
|
// general state
|
|
std::string agentId_;
|
|
bool requiresAuth_;
|
|
std::shared_ptr<AudioInterface> audioInterface_;
|
|
|
|
CallbackAgentResponse callbackAgentResponse_;
|
|
CallbackAgentResponseCorrection callbackAgentResponseCorrection_;
|
|
CallbackUserTranscript callbackUserTranscript_;
|
|
CallbackLatencyMeasurement callbackLatencyMeasurement_;
|
|
|
|
std::thread workerThread_;
|
|
std::atomic<bool> shouldStop_{false};
|
|
std::string conversationId_;
|
|
|
|
std::atomic<int> lastInterruptId_{0};
|
|
};
|