-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathattachments.cpp
More file actions
314 lines (267 loc) · 10.7 KB
/
attachments.cpp
File metadata and controls
314 lines (267 loc) · 10.7 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (c) 2025 Elias Bachaalany
// SPDX-License-Identifier: MIT
/// @file attachments.cpp
/// @brief Example demonstrating file and directory attachments
///
/// This example shows how to:
/// 1. Attach files to messages for analysis
/// 2. Attach directories for broader context
/// 3. Use attachments for code review workflows
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <copilot/copilot.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <mutex>
#include <string>
namespace fs = std::filesystem;
/// Create a sample file for demonstration
void create_sample_file(const std::string& path, const std::string& content)
{
std::ofstream file(path, std::ios::binary);
if (!file)
throw std::runtime_error("Failed to create sample file: " + path);
file << content;
if (!file)
throw std::runtime_error("Failed to write sample file: " + path);
std::cout << "Created sample file: " << path << "\n";
}
int main()
{
try
{
copilot::ClientOptions options;
options.log_level = copilot::LogLevel::Info;
copilot::Client client(options);
std::cout << "=== File Attachments Example ===\n\n";
std::cout << "This example demonstrates attaching files and directories to messages.\n";
std::cout << "Attachments provide context to Copilot for code review, analysis, etc.\n\n";
// Create sample files for demonstration
fs::path temp_dir = fs::temp_directory_path() / "copilot-sdk-cpp-attachments-example";
fs::create_directories(temp_dir);
fs::path sample_cpp = temp_dir / "sample_code.cpp";
fs::path sample_header = temp_dir / "sample_code.h";
create_sample_file(sample_cpp.string(), R"(
#include "sample_code.h"
#include <iostream>
#include <vector>
// TODO: Add error handling
void processData(std::vector<int>& data) {
for (int i = 0; i <= data.size(); i++) { // Bug: off-by-one error
data[i] = data[i] * 2;
}
}
int divide(int a, int b) {
return a / b; // Bug: no check for division by zero
}
char* allocateBuffer(size_t size) {
char* buf = new char[size];
// Bug: potential memory leak - no delete
return buf;
}
int main() {
std::vector<int> nums = {1, 2, 3, 4, 5};
processData(nums);
int result = divide(10, 0); // Will crash
std::cout << "Result: " << result << std::endl;
return 0;
}
)");
create_sample_file(sample_header.string(), R"(
#ifndef SAMPLE_CODE_H
#define SAMPLE_CODE_H
#include <vector>
void processData(std::vector<int>& data);
int divide(int a, int b);
char* allocateBuffer(size_t size);
#endif // SAMPLE_CODE_H
)");
client.start().get();
copilot::SessionConfig config;
auto session = client.create_session(config).get();
std::cout << "\nSession created: " << session->session_id() << "\n\n";
// Synchronization
std::mutex mtx;
std::condition_variable cv;
std::atomic<bool> idle{false};
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* tool_start = event.try_as<copilot::ToolExecutionStartData>())
{
std::cout << "\n[Tool: " << tool_start->tool_name << "]\n";
}
else if (auto* error = event.try_as<copilot::SessionErrorData>())
{
std::cerr << "Error: " << error->message << "\n";
}
else if (event.type == copilot::SessionEventType::SessionIdle)
{
std::lock_guard<std::mutex> lock(mtx);
idle = true;
cv.notify_one();
}
}
);
// ---------------------------------------------------------------------
// Demo 1: Single file attachment
// ---------------------------------------------------------------------
std::cout << "=== Demo 1: Single File Attachment ===\n";
{
copilot::MessageOptions opts;
opts.prompt = "Review this C++ code and identify all bugs and issues.";
opts.attachments = std::vector<copilot::UserMessageAttachment>{
{copilot::AttachmentType::File, sample_cpp.string(), "sample_code.cpp"}
};
std::cout << "Sending message with attachment: " << sample_cpp.string() << "\n";
idle = false;
session->send(opts).get();
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [&idle]() { return idle.load(); });
}
// ---------------------------------------------------------------------
// Demo 2: Multiple file attachments
// ---------------------------------------------------------------------
std::cout << "\n=== Demo 2: Multiple File Attachments ===\n";
{
copilot::MessageOptions opts;
opts.prompt = "Review both files. Does the header match the implementation?";
opts.attachments = std::vector<copilot::UserMessageAttachment>{
{copilot::AttachmentType::File, sample_cpp.string(), "sample_code.cpp"},
{copilot::AttachmentType::File, sample_header.string(), "sample_code.h"}
};
std::cout << "Sending message with 2 attachments\n";
idle = false;
session->send(opts).get();
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [&idle]() { return idle.load(); });
}
// ---------------------------------------------------------------------
// Demo 3: Directory attachment
// ---------------------------------------------------------------------
std::cout << "\n=== Demo 3: Directory Attachment ===\n";
{
copilot::MessageOptions opts;
opts.prompt = "What files are in this directory and what do they contain?";
opts.attachments = std::vector<copilot::UserMessageAttachment>{
{copilot::AttachmentType::Directory, temp_dir.string(), "temp_directory"}
};
std::cout << "Sending message with directory attachment: " << temp_dir.string() << "\n";
idle = false;
session->send(opts).get();
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [&idle]() { return idle.load(); });
}
// ---------------------------------------------------------------------
// Interactive mode
// ---------------------------------------------------------------------
std::cout << "\n=== Interactive Mode ===\n";
std::cout << "Commands:\n";
std::cout << " attach <path> - Attach a file to your next message\n";
std::cout << " attachdir <path> - Attach a directory to your next message\n";
std::cout << " clear - Clear attachments\n";
std::cout << " quit - Exit\n";
std::cout << "\nOr just type a message (with any pending attachments).\n\n";
std::vector<copilot::UserMessageAttachment> pending_attachments;
std::cout << "> ";
std::string line;
while (std::getline(std::cin, line))
{
if (line == "quit" || line == "exit")
break;
if (line.empty())
{
std::cout << "> ";
continue;
}
// Handle attach command
if (line.substr(0, 7) == "attach ")
{
std::string path = line.substr(7);
if (fs::exists(path))
{
std::string name = fs::path(path).filename().string();
pending_attachments.push_back(
{copilot::AttachmentType::File, path, name}
);
std::cout << "Added attachment: " << path << " (as '" << name << "')\n";
std::cout << "Pending attachments: " << pending_attachments.size() << "\n";
}
else
{
std::cout << "File not found: " << path << "\n";
}
std::cout << "> ";
continue;
}
// Handle attachdir command
if (line.substr(0, 10) == "attachdir ")
{
std::string path = line.substr(10);
if (fs::exists(path) && fs::is_directory(path))
{
std::string name = fs::path(path).filename().string();
pending_attachments.push_back(
{copilot::AttachmentType::Directory, path, name}
);
std::cout << "Added directory attachment: " << path << "\n";
std::cout << "Pending attachments: " << pending_attachments.size() << "\n";
}
else
{
std::cout << "Directory not found: " << path << "\n";
}
std::cout << "> ";
continue;
}
// Handle clear command
if (line == "clear")
{
pending_attachments.clear();
std::cout << "Attachments cleared.\n> ";
continue;
}
// Send message with any pending attachments
idle = false;
copilot::MessageOptions opts;
opts.prompt = line;
if (!pending_attachments.empty())
{
opts.attachments = pending_attachments;
std::cout << "Sending with " << pending_attachments.size() << " attachment(s)...\n";
pending_attachments.clear();
}
session->send(opts).get();
{
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [&idle]() { return idle.load(); });
}
std::cout << "\n> ";
}
// Cleanup sample files
std::error_code ec;
fs::remove(sample_cpp, ec);
fs::remove(sample_header, ec);
fs::remove(temp_dir, ec);
// Cleanup session
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;
}
}