forked from 0xeb/copilot-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool_progress.cpp
More file actions
226 lines (196 loc) · 7.74 KB
/
Copy pathtool_progress.cpp
File metadata and controls
226 lines (196 loc) · 7.74 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (c) 2025 Elias Bachaalany
// SPDX-License-Identifier: MIT
/// @file tool_progress.cpp
/// @brief Example demonstrating tool execution progress monitoring
///
/// This example shows how to:
/// 1. Register a custom tool and subscribe to tool lifecycle events
/// 2. Monitor ToolExecutionStart, ToolExecutionProgress, and ToolExecutionComplete
/// 3. Display real-time progress updates during tool execution
#include <atomic>
#include <condition_variable>
#include <copilot/copilot.hpp>
#include <iostream>
#include <mutex>
#include <sstream>
#include <string>
#include <thread>
// A simulated long-running tool that counts words in text
copilot::ToolResultObject word_count_handler(const copilot::ToolInvocation& invocation)
{
copilot::ToolResultObject result;
try
{
std::string text = "Hello World";
if (invocation.arguments.has_value() && invocation.arguments->contains("text"))
text = (*invocation.arguments)["text"].get<std::string>();
// Count words
std::istringstream iss(text);
std::string word;
int count = 0;
while (iss >> word)
count++;
std::ostringstream oss;
oss << "The text contains " << count << " word(s).";
result.text_result_for_llm = oss.str();
}
catch (const std::exception& e)
{
result.result_type = copilot::ToolResultType::Failure;
result.error = e.what();
result.text_result_for_llm = std::string("Error: ") + e.what();
}
return result;
}
// A simulated file search tool
copilot::ToolResultObject search_handler(const copilot::ToolInvocation& invocation)
{
copilot::ToolResultObject result;
try
{
std::string query = "example";
if (invocation.arguments.has_value() && invocation.arguments->contains("query"))
query = (*invocation.arguments)["query"].get<std::string>();
std::ostringstream oss;
oss << "Search results for '" << query << "':\n"
<< " 1. README.md - line 42: contains '" << query << "'\n"
<< " 2. main.cpp - line 7: references '" << query << "'\n"
<< " 3. docs/guide.md - line 15: explains '" << query << "'";
result.text_result_for_llm = oss.str();
}
catch (const std::exception& e)
{
result.result_type = copilot::ToolResultType::Failure;
result.error = e.what();
result.text_result_for_llm = std::string("Error: ") + e.what();
}
return result;
}
int main()
{
try
{
copilot::ClientOptions options;
options.log_level = copilot::LogLevel::Info;
copilot::Client client(options);
std::cout << "=== Tool Execution Progress Example ===\n\n";
std::cout << "This example shows the full tool lifecycle:\n";
std::cout << " ToolExecutionStart -> ToolExecutionProgress -> ToolExecutionComplete\n\n";
client.start().get();
// Define custom tools
copilot::Tool word_count_tool;
word_count_tool.name = "word_count";
word_count_tool.description = "Count the number of words in text";
word_count_tool.parameters_schema = copilot::json{
{"type", "object"},
{"properties",
{{"text", {{"type", "string"}, {"description", "The text to count words in"}}}}},
{"required", {"text"}}
};
word_count_tool.handler = word_count_handler;
copilot::Tool search_tool;
search_tool.name = "search_files";
search_tool.description = "Search for files containing a query string";
search_tool.parameters_schema = copilot::json{
{"type", "object"},
{"properties",
{{"query", {{"type", "string"}, {"description", "The search query"}}}}},
{"required", {"query"}}
};
search_tool.handler = search_handler;
// Create session with tools
copilot::SessionConfig config;
config.tools = {word_count_tool, search_tool};
auto session = client.create_session(config).get();
std::cout << "Session created with 2 tools: word_count, search_files\n\n";
// Synchronization
std::mutex mtx;
std::condition_variable cv;
std::atomic<bool> idle{false};
// Subscribe to events — including tool progress
auto subscription = session->on(
[&](const copilot::SessionEvent& event)
{
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* start = event.try_as<copilot::ToolExecutionStartData>())
{
// Tool is starting execution
std::cout << "\n[Tool Start] " << start->tool_name;
if (start->arguments)
std::cout << " | Args: " << start->arguments->dump();
std::cout << "\n";
}
else if (auto* progress = event.try_as<copilot::ToolExecutionProgressData>())
{
// Progress update during tool execution
std::cout << "[Tool Progress] " << progress->tool_call_id
<< ": " << progress->progress_message << "\n";
}
else if (auto* complete = event.try_as<copilot::ToolExecutionCompleteData>())
{
// Tool finished execution
std::cout << "[Tool Complete] " << complete->tool_call_id
<< " | " << (complete->success ? "Success" : "Failed");
if (complete->result)
std::cout << " | " << complete->result->content;
if (complete->error)
std::cout << " | Error: " << complete->error->message;
std::cout << "\n";
}
else if (auto* error = event.try_as<copilot::SessionErrorData>())
{
std::cerr << "\nError: " << error->message << "\n";
}
else if (event.type == copilot::SessionEventType::SessionIdle)
{
std::lock_guard<std::mutex> lock(mtx);
idle = true;
cv.notify_one();
}
}
);
// Interactive loop
std::cout << "Try asking questions that use the tools!\n";
std::cout << "Examples:\n";
std::cout << " - How many words are in 'The quick brown fox jumps over the lazy dog'?\n";
std::cout << " - Search for files containing 'main'\n";
std::cout << "\nType 'quit' to exit.\n\n> ";
std::string line;
while (std::getline(std::cin, line))
{
if (line == "quit" || line == "exit")
break;
if (line.empty())
{
std::cout << "> ";
continue;
}
idle = false;
copilot::MessageOptions msg_opts;
msg_opts.prompt = line;
session->send(msg_opts).get();
{
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [&idle]() { return idle.load(); });
}
std::cout << "\n> ";
}
// Cleanup
std::cout << "\nCleaning up...\n";
session->destroy().get();
client.stop().get();
return 0;
}
catch (const std::exception& e)
{
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
}