This repository was archived by the owner on Aug 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinteraction.cc
More file actions
55 lines (46 loc) · 1.53 KB
/
interaction.cc
File metadata and controls
55 lines (46 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "interaction.h"
namespace convai_sdk {
static constexpr char CONVAI_API_ENDPOINT[] = "stream.convai.com";
void Interaction::Start(std::function<void(GetResponseResponse)> res_func) {
rpc_channel_ = grpc::CreateChannel(
CONVAI_API_ENDPOINT, grpc::SslCredentials(grpc::SslCredentialsOptions()));
stub_ = ConvaiService::NewStub(rpc_channel_);
stream_ = std::shared_ptr<
ClientReaderWriter<GetResponseRequest, GetResponseResponse>>(
stub_->GetResponse(&context_));
GetResponseRequest first_request;
*first_request.mutable_get_response_config() = get_response_config_;
stream_->Write(first_request);
response_thread_ =
std::unique_ptr<std::thread>(new std::thread([this, res_func] {
GetResponseResponse resp;
while (stream_->Read(&resp)) {
res_func(resp);
}
}));
}
grpc::Status Interaction::Stop() {
stream_->WritesDone();
grpc::Status status;
if (stream_ != nullptr) {
status = stream_->Finish();
if (!status.ok()) {
std::cout << "Convai Service Error: " << status.error_message();
}
}
if (response_thread_ != nullptr) {
response_thread_->join();
}
return status;
}
void Interaction::SendText(const std::string &text) {
GetResponseRequest req;
req.mutable_get_response_data()->set_text_data(text);
stream_->Write(req);
}
void Interaction::SendAudio(char *audio_data, int64_t length) {
GetResponseRequest req;
req.mutable_get_response_data()->set_audio_data(audio_data, length);
stream_->Write(req);
};
} // namespace convai_sdk