-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbasic_chat.cpp
More file actions
85 lines (70 loc) · 2.61 KB
/
basic_chat.cpp
File metadata and controls
85 lines (70 loc) · 2.61 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2025 Elias Bachaalany
// SPDX-License-Identifier: MIT
/// @file basic_chat.cpp
/// @brief Simple Q&A example demonstrating basic Copilot SDK usage
#include <copilot/copilot.hpp>
#include <iostream>
#include <string>
int main()
{
try
{
// Create client with default options (uses stdio transport)
copilot::ClientOptions options;
options.log_level = copilot::LogLevel::Info;
copilot::Client client(options);
std::cout << "Starting Copilot client...\n";
client.start().get();
std::cout << "Connected!\n";
// Create a session
copilot::SessionConfig session_config;
session_config.model = "gpt-4"; // Optional: specify model
auto session = client.create_session(session_config).get();
std::cout << "Session created: " << session->session_id() << "\n";
// Subscribe to events
auto subscription = session->on(
[](const copilot::SessionEvent& event)
{
// Handle different event types
if (auto* msg = event.try_as<copilot::AssistantMessageData>())
std::cout << "\nAssistant: " << msg->content << "\n";
else if (auto* delta = event.try_as<copilot::AssistantMessageDeltaData>())
std::cout << delta->delta_content << std::flush;
else if (auto* error = event.try_as<copilot::SessionErrorData>())
std::cerr << "Error: " << error->message << "\n";
else if (event.type == copilot::SessionEventType::SessionIdle)
std::cout << "\n> " << std::flush;
}
);
// Interactive chat loop
std::cout << "\nEnter your messages (type 'quit' to exit):\n> ";
std::string line;
while (std::getline(std::cin, line))
{
if (line == "quit" || line == "exit")
break;
if (line.empty())
{
std::cout << "> ";
continue;
}
// Send message
copilot::MessageOptions msg_opts;
msg_opts.prompt = line;
auto message_id = session->send(msg_opts).get();
std::cout << "Message sent (ID: " << message_id << ")\n";
}
// Cleanup
std::cout << "\nDestroying session...\n";
session->destroy().get();
std::cout << "Stopping client...\n";
client.stop().get();
std::cout << "Done!\n";
return 0;
}
catch (const std::exception& e)
{
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
}