forked from 0xeb/copilot-sdk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_agents.cpp
More file actions
205 lines (180 loc) · 8.49 KB
/
Copy pathcustom_agents.cpp
File metadata and controls
205 lines (180 loc) · 8.49 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
// Copyright (c) 2025 Elias Bachaalany
// SPDX-License-Identifier: MIT
/// @file custom_agents.cpp
/// @brief Example demonstrating custom agent configuration
///
/// This example shows how to:
/// 1. Define custom agents with specific roles and capabilities
/// 2. Restrict agents to specific tools
/// 3. Configure multiple agents in a single session
/// 4. Use agent-specific MCP servers
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <copilot/copilot.hpp>
#include <iostream>
#include <mutex>
#include <string>
int main()
{
try
{
copilot::ClientOptions options;
options.log_level = copilot::LogLevel::Info;
copilot::Client client(options);
std::cout << "=== Custom Agents Example ===\n\n";
std::cout << "This example demonstrates defining custom agents with specific roles.\n";
std::cout << "Custom agents can have restricted tools, specific prompts, and MCP servers.\n\n";
client.start().get();
// ---------------------------------------------------------------------
// Define Custom Agents
// ---------------------------------------------------------------------
// Agent 1: Code Reviewer - focused on reviewing code quality
copilot::CustomAgentConfig code_reviewer;
code_reviewer.name = "code-reviewer";
code_reviewer.display_name = "Code Reviewer";
code_reviewer.description = "Reviews code for bugs, security issues, and best practices";
code_reviewer.prompt =
"You are a senior code reviewer with expertise in security and performance. "
"When reviewing code:\n"
"1. Look for potential bugs and edge cases\n"
"2. Check for security vulnerabilities (SQL injection, XSS, buffer overflows)\n"
"3. Evaluate code readability and maintainability\n"
"4. Suggest performance improvements where applicable\n"
"5. Verify proper error handling\n"
"Always be constructive and explain the 'why' behind your suggestions.";
code_reviewer.tools = std::vector<std::string>{"Read", "Glob", "Grep"}; // Read-only tools
code_reviewer.infer = true; // Allow automatic agent selection
// Agent 2: Documentation Writer - focused on writing docs
copilot::CustomAgentConfig doc_writer;
doc_writer.name = "doc-writer";
doc_writer.display_name = "Documentation Writer";
doc_writer.description = "Writes and improves documentation for code and APIs";
doc_writer.prompt =
"You are a technical documentation specialist. Your role is to:\n"
"1. Write clear, concise documentation\n"
"2. Create helpful code examples\n"
"3. Document APIs with proper parameter descriptions\n"
"4. Write README files and getting started guides\n"
"5. Use proper markdown formatting\n"
"Focus on making documentation accessible to developers of all skill levels.";
doc_writer.tools = std::vector<std::string>{"Read", "Write", "Glob"};
doc_writer.infer = true;
// Agent 3: Test Writer - focused on writing tests
copilot::CustomAgentConfig test_writer;
test_writer.name = "test-writer";
test_writer.display_name = "Test Writer";
test_writer.description = "Writes unit tests and integration tests";
test_writer.prompt =
"You are a QA engineer specializing in test automation. Your role is to:\n"
"1. Write comprehensive unit tests with good coverage\n"
"2. Create meaningful test cases that cover edge cases\n"
"3. Follow testing best practices (AAA pattern, isolation, etc.)\n"
"4. Write clear test descriptions\n"
"5. Ensure tests are maintainable and not flaky\n"
"Use the appropriate testing framework for the project.";
test_writer.tools = std::vector<std::string>{"Read", "Write", "Glob", "Grep", "Bash"};
test_writer.infer = true;
// Agent 4: Security Auditor - restricted, no inference
copilot::CustomAgentConfig security_auditor;
security_auditor.name = "security-auditor";
security_auditor.display_name = "Security Auditor";
security_auditor.description = "Audits code for security vulnerabilities";
security_auditor.prompt =
"You are a security expert conducting a code audit. Focus on:\n"
"1. OWASP Top 10 vulnerabilities\n"
"2. Input validation and sanitization\n"
"3. Authentication and authorization flaws\n"
"4. Cryptographic issues\n"
"5. Sensitive data exposure\n"
"Report findings with severity levels (Critical, High, Medium, Low).";
security_auditor.tools = std::vector<std::string>{"Read", "Glob", "Grep"}; // Read-only
security_auditor.infer = false; // Must be explicitly invoked
// ---------------------------------------------------------------------
// Create Session with Custom Agents
// ---------------------------------------------------------------------
copilot::SessionConfig config;
config.custom_agents = std::vector<copilot::CustomAgentConfig>{
code_reviewer,
doc_writer,
test_writer,
security_auditor
};
auto session = client.create_session(config).get();
std::cout << "Session created with 4 custom agents:\n";
std::cout << " - code-reviewer: Reviews code quality (read-only tools)\n";
std::cout << " - doc-writer: Writes documentation (can write files)\n";
std::cout << " - test-writer: Writes tests (full tool access)\n";
std::cout << " - security-auditor: Security audits (read-only, no auto-inference)\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[Agent using 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();
}
}
);
// Interactive loop
std::cout << "You can now interact with the session. The system will automatically\n";
std::cout << "route requests to the appropriate agent based on your query.\n\n";
std::cout << "Examples:\n";
std::cout << " - 'Review this code for bugs' -> code-reviewer\n";
std::cout << " - 'Write documentation for...' -> doc-writer\n";
std::cout << " - 'Write tests for...' -> test-writer\n";
std::cout << " - '@security-auditor check for vulnerabilities' -> explicit agent\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;
}
}