生成 C++ 代码工程
This commit is contained in:
parent
b9213c18ba
commit
4c9251a62f
|
|
@ -0,0 +1,37 @@
|
|||
cmake_minimum_required(VERSION 3.15)
|
||||
project(MultiAgentSystem VERSION 1.0.0 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
# 输出目录
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
|
||||
# 主程序
|
||||
add_executable(multi_agent_system
|
||||
src/main.cpp
|
||||
src/app.cpp
|
||||
)
|
||||
|
||||
target_include_directories(multi_agent_system
|
||||
PUBLIC
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
)
|
||||
|
||||
# 测试程序
|
||||
add_executable(basic_test
|
||||
tests/basic_test.cpp
|
||||
src/app.cpp
|
||||
)
|
||||
|
||||
target_include_directories(basic_test
|
||||
PUBLIC
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
)
|
||||
|
||||
# 启用测试
|
||||
enable_testing()
|
||||
add_test(NAME basic_test COMMAND basic_test)
|
||||
82
README.md
82
README.md
|
|
@ -1,3 +1,81 @@
|
|||
# 图书管理系统
|
||||
# 多智能体协同系统 C++ 工程
|
||||
|
||||
图书管理系统
|
||||
这是一个基于 C++17 和 CMake 的多智能体协同系统骨架工程,实现了核心的调度引擎和智能体管理功能。
|
||||
|
||||
## 项目结构
|
||||
|
||||
```
|
||||
├── CMakeLists.txt # CMake 构建配置文件
|
||||
├── README.md # 项目说明文档
|
||||
├── include/ # 头文件目录
|
||||
│ └── app.hpp # 应用程序主要头文件
|
||||
├── src/ # 源文件目录
|
||||
│ ├── main.cpp # 程序入口点
|
||||
│ └── app.cpp # 应用程序实现
|
||||
└── tests/ # 测试文件目录
|
||||
└── basic_test.cpp # 基础测试
|
||||
```
|
||||
|
||||
## 核心功能模块
|
||||
|
||||
### 1. 智能体管理
|
||||
- Agent 类的定义和管理
|
||||
- 智能体注册、注销和发现
|
||||
- 智能体元数据管理
|
||||
|
||||
### 2. 执行流引擎
|
||||
- 执行流节点定义
|
||||
- DAG(有向无环图)解析和执行
|
||||
- 串行和并行执行支持
|
||||
|
||||
### 3. 上下文管理
|
||||
- 全局上下文和节点局部上下文
|
||||
- 上下文数据传递和持久化
|
||||
|
||||
### 4. 任务调度
|
||||
- 任务队列管理
|
||||
- 任务状态跟踪
|
||||
- 异常重试机制
|
||||
|
||||
## 编译与运行
|
||||
|
||||
### 编译项目
|
||||
```bash
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
cmake --build .
|
||||
```
|
||||
|
||||
### 运行主程序
|
||||
```bash
|
||||
./bin/multi_agent_system
|
||||
```
|
||||
|
||||
### 运行测试
|
||||
```bash
|
||||
./bin/basic_test
|
||||
# 或使用 CMake 测试
|
||||
ctest
|
||||
```
|
||||
|
||||
## 依赖项
|
||||
|
||||
- C++17 或更高版本
|
||||
- CMake 3.15 或更高版本
|
||||
- 标准 C++ 库
|
||||
|
||||
## 后续扩展方向
|
||||
|
||||
1. 添加 HTTP 客户端支持智能体通信
|
||||
2. 实现 JSON Schema 验证
|
||||
3. 添加数据库连接支持(MySQL、MongoDB)
|
||||
4. 实现 JWT 认证和 RBAC 权限控制
|
||||
5. 添加 SSE(Server-Sent Events)流式输出支持
|
||||
6. 容器化部署配置
|
||||
|
||||
## 注意事项
|
||||
|
||||
- 当前为骨架实现,包含核心数据结构和基础功能
|
||||
- 实际生产环境需要添加网络通信、数据库持久化等模块
|
||||
- 建议结合前端图形化编排器使用
|
||||
|
|
@ -0,0 +1,183 @@
|
|||
#ifndef MULTI_AGENT_SYSTEM_APP_HPP
|
||||
#define MULTI_AGENT_SYSTEM_APP_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
|
||||
namespace multi_agent {
|
||||
|
||||
// 智能体状态枚举
|
||||
enum class AgentStatus {
|
||||
IDLE, // 空闲
|
||||
BUSY, // 忙碌
|
||||
OFFLINE, // 离线
|
||||
ERROR // 错误
|
||||
};
|
||||
|
||||
// 任务执行状态枚举
|
||||
enum class ExecutionStatus {
|
||||
PENDING, // 等待中
|
||||
RUNNING, // 执行中
|
||||
COMPLETED, // 已完成
|
||||
FAILED, // 失败
|
||||
CANCELLED // 已取消
|
||||
};
|
||||
|
||||
// 智能体元数据结构
|
||||
struct AgentMetadata {
|
||||
std::string id; // 智能体唯一标识
|
||||
std::string name; // 智能体名称
|
||||
std::string version; // 版本号
|
||||
std::string service_url; // 服务地址
|
||||
std::vector<std::string> capabilities; // 能力标签
|
||||
std::string input_schema; // 输入JSON Schema
|
||||
std::string output_schema; // 输出JSON Schema
|
||||
AgentStatus status; // 当前状态
|
||||
std::chrono::system_clock::time_point last_heartbeat; // 最后心跳时间
|
||||
};
|
||||
|
||||
// 智能体类
|
||||
class Agent {
|
||||
public:
|
||||
Agent(const AgentMetadata& metadata);
|
||||
virtual ~Agent() = default;
|
||||
|
||||
// 执行任务
|
||||
virtual std::string execute(const std::string& input_data);
|
||||
|
||||
// 获取元数据
|
||||
const AgentMetadata& get_metadata() const { return metadata_; }
|
||||
|
||||
// 更新状态
|
||||
void set_status(AgentStatus status) { metadata_.status = status; }
|
||||
|
||||
// 更新心跳时间
|
||||
void update_heartbeat();
|
||||
|
||||
private:
|
||||
AgentMetadata metadata_;
|
||||
};
|
||||
|
||||
// 执行流节点
|
||||
class ExecutionNode {
|
||||
public:
|
||||
ExecutionNode(const std::string& id, const std::string& agent_id);
|
||||
|
||||
std::string get_id() const { return id_; }
|
||||
std::string get_agent_id() const { return agent_id_; }
|
||||
ExecutionStatus get_status() const { return status_; }
|
||||
|
||||
void set_input_data(const std::string& data) { input_data_ = data; }
|
||||
void set_output_data(const std::string& data) { output_data_ = data; }
|
||||
|
||||
void set_status(ExecutionStatus status) { status_ = status; }
|
||||
|
||||
// 添加依赖节点
|
||||
void add_dependency(const std::string& node_id);
|
||||
const std::vector<std::string>& get_dependencies() const { return dependencies_; }
|
||||
|
||||
// 执行节点任务
|
||||
bool execute(const std::map<std::string, std::shared_ptr<Agent>>& agents);
|
||||
|
||||
private:
|
||||
std::string id_;
|
||||
std::string agent_id_;
|
||||
std::string input_data_;
|
||||
std::string output_data_;
|
||||
ExecutionStatus status_;
|
||||
std::vector<std::string> dependencies_;
|
||||
std::chrono::system_clock::time_point start_time_;
|
||||
std::chrono::system_clock::time_point end_time_;
|
||||
};
|
||||
|
||||
// 执行上下文
|
||||
class ExecutionContext {
|
||||
public:
|
||||
ExecutionContext();
|
||||
|
||||
// 全局上下文操作
|
||||
void set_global_value(const std::string& key, const std::string& value);
|
||||
std::string get_global_value(const std::string& key) const;
|
||||
|
||||
// 节点上下文操作
|
||||
void set_node_output(const std::string& node_id, const std::string& output);
|
||||
std::string get_node_output(const std::string& node_id) const;
|
||||
|
||||
// 上下文快照
|
||||
std::string serialize() const;
|
||||
void deserialize(const std::string& snapshot);
|
||||
|
||||
// 清空上下文
|
||||
void clear();
|
||||
|
||||
private:
|
||||
std::map<std::string, std::string> global_context_;
|
||||
std::map<std::string, std::string> node_outputs_;
|
||||
};
|
||||
|
||||
// 执行流引擎
|
||||
class FlowEngine {
|
||||
public:
|
||||
FlowEngine();
|
||||
|
||||
// 智能体管理
|
||||
void register_agent(const AgentMetadata& metadata);
|
||||
void unregister_agent(const std::string& agent_id);
|
||||
std::shared_ptr<Agent> get_agent(const std::string& agent_id);
|
||||
|
||||
// 执行流管理
|
||||
void add_node(const std::shared_ptr<ExecutionNode>& node);
|
||||
void remove_node(const std::string& node_id);
|
||||
|
||||
// 执行流执行
|
||||
bool execute_sequential();
|
||||
bool execute_parallel();
|
||||
|
||||
// 获取执行状态
|
||||
std::map<std::string, ExecutionStatus> get_execution_status() const;
|
||||
|
||||
// 清空引擎
|
||||
void clear();
|
||||
|
||||
private:
|
||||
std::map<std::string, std::shared_ptr<Agent>> agents_;
|
||||
std::map<std::string, std::shared_ptr<ExecutionNode>> nodes_;
|
||||
std::shared_ptr<ExecutionContext> context_;
|
||||
|
||||
// 拓扑排序(用于依赖分析)
|
||||
std::vector<std::string> topological_sort() const;
|
||||
|
||||
// 检查是否有环
|
||||
bool has_cycle() const;
|
||||
bool dfs_cycle_detection(const std::string& node_id,
|
||||
std::map<std::string, bool>& visited,
|
||||
std::map<std::string, bool>& rec_stack) const;
|
||||
};
|
||||
|
||||
// 应用程序类
|
||||
class MultiAgentSystem {
|
||||
public:
|
||||
MultiAgentSystem();
|
||||
|
||||
// 初始化系统
|
||||
bool initialize();
|
||||
|
||||
// 演示功能
|
||||
void demo_basic_flow();
|
||||
void demo_parallel_execution();
|
||||
|
||||
// 运行系统
|
||||
void run();
|
||||
|
||||
private:
|
||||
std::unique_ptr<FlowEngine> flow_engine_;
|
||||
};
|
||||
|
||||
} // namespace multi_agent
|
||||
|
||||
#endif // MULTI_AGENT_SYSTEM_APP_HPP
|
||||
|
|
@ -0,0 +1,454 @@
|
|||
#include "app.hpp"
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
|
||||
namespace multi_agent {
|
||||
|
||||
// Agent 类实现
|
||||
Agent::Agent(const AgentMetadata& metadata) : metadata_(metadata) {
|
||||
metadata_.status = AgentStatus::IDLE;
|
||||
metadata_.last_heartbeat = std::chrono::system_clock::now();
|
||||
}
|
||||
|
||||
std::string Agent::execute(const std::string& input_data) {
|
||||
// 模拟智能体执行任务
|
||||
metadata_.status = AgentStatus::BUSY;
|
||||
|
||||
// 这里只是简单返回,实际应该调用智能体的实际服务
|
||||
std::string result = "Agent " + metadata_.id + " processed: " + input_data;
|
||||
|
||||
metadata_.status = AgentStatus::IDLE;
|
||||
update_heartbeat();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Agent::update_heartbeat() {
|
||||
metadata_.last_heartbeat = std::chrono::system_clock::now();
|
||||
}
|
||||
|
||||
// ExecutionNode 类实现
|
||||
ExecutionNode::ExecutionNode(const std::string& id, const std::string& agent_id)
|
||||
: id_(id), agent_id_(agent_id), status_(ExecutionStatus::PENDING) {
|
||||
}
|
||||
|
||||
void ExecutionNode::add_dependency(const std::string& node_id) {
|
||||
dependencies_.push_back(node_id);
|
||||
}
|
||||
|
||||
bool ExecutionNode::execute(const std::map<std::string, std::shared_ptr<Agent>>& agents) {
|
||||
// 检查智能体是否存在
|
||||
auto agent_it = agents.find(agent_id_);
|
||||
if (agent_it == agents.end()) {
|
||||
status_ = ExecutionStatus::FAILED;
|
||||
output_data_ = "Agent not found: " + agent_id_;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 执行任务
|
||||
status_ = ExecutionStatus::RUNNING;
|
||||
start_time_ = std::chrono::system_clock::now();
|
||||
|
||||
try {
|
||||
output_data_ = agent_it->second->execute(input_data_);
|
||||
status_ = ExecutionStatus::COMPLETED;
|
||||
} catch (const std::exception& e) {
|
||||
status_ = ExecutionStatus::FAILED;
|
||||
output_data_ = std::string("Execution failed: ") + e.what();
|
||||
}
|
||||
|
||||
end_time_ = std::chrono::system_clock::now();
|
||||
return status_ == ExecutionStatus::COMPLETED;
|
||||
}
|
||||
|
||||
// ExecutionContext 类实现
|
||||
ExecutionContext::ExecutionContext() {
|
||||
clear();
|
||||
}
|
||||
|
||||
void ExecutionContext::set_global_value(const std::string& key, const std::string& value) {
|
||||
global_context_[key] = value;
|
||||
}
|
||||
|
||||
std::string ExecutionContext::get_global_value(const std::string& key) const {
|
||||
auto it = global_context_.find(key);
|
||||
if (it != global_context_.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
void ExecutionContext::set_node_output(const std::string& node_id, const std::string& output) {
|
||||
node_outputs_[node_id] = output;
|
||||
}
|
||||
|
||||
std::string ExecutionContext::get_node_output(const std::string& node_id) const {
|
||||
auto it = node_outputs_.find(node_id);
|
||||
if (it != node_outputs_.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string ExecutionContext::serialize() const {
|
||||
// 简化实现:实际应该序列化为JSON
|
||||
std::stringstream ss;
|
||||
ss << "Global Context: ";
|
||||
for (const auto& [key, value] : global_context_) {
|
||||
ss << key << "=" << value << "; ";
|
||||
}
|
||||
ss << "Node Outputs: ";
|
||||
for (const auto& [node_id, output] : node_outputs_) {
|
||||
ss << node_id << "=" << output << "; ";
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void ExecutionContext::deserialize(const std::string& snapshot) {
|
||||
// 简化实现:实际应该从JSON反序列化
|
||||
// 这里只清空并设置一个示例值
|
||||
clear();
|
||||
set_global_value("deserialized_from", snapshot);
|
||||
}
|
||||
|
||||
void ExecutionContext::clear() {
|
||||
global_context_.clear();
|
||||
node_outputs_.clear();
|
||||
}
|
||||
|
||||
// FlowEngine 类实现
|
||||
FlowEngine::FlowEngine() : context_(std::make_shared<ExecutionContext>()) {
|
||||
}
|
||||
|
||||
void FlowEngine::register_agent(const AgentMetadata& metadata) {
|
||||
auto agent = std::make_shared<Agent>(metadata);
|
||||
agents_[metadata.id] = agent;
|
||||
}
|
||||
|
||||
void FlowEngine::unregister_agent(const std::string& agent_id) {
|
||||
agents_.erase(agent_id);
|
||||
}
|
||||
|
||||
std::shared_ptr<Agent> FlowEngine::get_agent(const std::string& agent_id) {
|
||||
auto it = agents_.find(agent_id);
|
||||
if (it != agents_.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void FlowEngine::add_node(const std::shared_ptr<ExecutionNode>& node) {
|
||||
nodes_[node->get_id()] = node;
|
||||
}
|
||||
|
||||
void FlowEngine::remove_node(const std::string& node_id) {
|
||||
nodes_.erase(node_id);
|
||||
}
|
||||
|
||||
bool FlowEngine::execute_sequential() {
|
||||
// 获取拓扑排序
|
||||
auto execution_order = topological_sort();
|
||||
if (execution_order.empty() || has_cycle()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 按顺序执行节点
|
||||
for (const auto& node_id : execution_order) {
|
||||
auto node = nodes_[node_id];
|
||||
|
||||
// 设置节点的输入数据(从上下文获取)
|
||||
for (const auto& dep_id : node->get_dependencies()) {
|
||||
std::string dep_output = context_->get_node_output(dep_id);
|
||||
if (!dep_output.empty()) {
|
||||
node->set_input_data(dep_output);
|
||||
}
|
||||
}
|
||||
|
||||
// 执行节点
|
||||
bool success = node->execute(agents_);
|
||||
|
||||
// 保存输出到上下文
|
||||
if (success) {
|
||||
context_->set_node_output(node_id, "Executed successfully");
|
||||
} else {
|
||||
context_->set_node_output(node_id, "Execution failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FlowEngine::execute_parallel() {
|
||||
// 简化实现:实际应该使用线程池
|
||||
// 这里先按顺序执行,但标记为并行模式
|
||||
return execute_sequential();
|
||||
}
|
||||
|
||||
std::map<std::string, ExecutionStatus> FlowEngine::get_execution_status() const {
|
||||
std::map<std::string, ExecutionStatus> status_map;
|
||||
for (const auto& [node_id, node] : nodes_) {
|
||||
status_map[node_id] = node->get_status();
|
||||
}
|
||||
return status_map;
|
||||
}
|
||||
|
||||
void FlowEngine::clear() {
|
||||
agents_.clear();
|
||||
nodes_.clear();
|
||||
context_->clear();
|
||||
}
|
||||
|
||||
std::vector<std::string> FlowEngine::topological_sort() const {
|
||||
std::vector<std::string> result;
|
||||
|
||||
// 计算入度
|
||||
std::map<std::string, int> in_degree;
|
||||
for (const auto& [node_id, node] : nodes_) {
|
||||
in_degree[node_id] = 0;
|
||||
}
|
||||
|
||||
for (const auto& [node_id, node] : nodes_) {
|
||||
for (const auto& dep_id : node->get_dependencies()) {
|
||||
if (nodes_.find(dep_id) != nodes_.end()) {
|
||||
in_degree[node_id]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 使用队列进行拓扑排序
|
||||
std::queue<std::string> zero_in_degree_queue;
|
||||
for (const auto& [node_id, degree] : in_degree) {
|
||||
if (degree == 0) {
|
||||
zero_in_degree_queue.push(node_id);
|
||||
}
|
||||
}
|
||||
|
||||
while (!zero_in_degree_queue.empty()) {
|
||||
std::string current = zero_in_degree_queue.front();
|
||||
zero_in_degree_queue.pop();
|
||||
result.push_back(current);
|
||||
|
||||
// 减少依赖当前节点的节点的入度
|
||||
for (const auto& [node_id, node] : nodes_) {
|
||||
auto& deps = node->get_dependencies();
|
||||
if (std::find(deps.begin(), deps.end(), current) != deps.end()) {
|
||||
in_degree[node_id]--;
|
||||
if (in_degree[node_id] == 0) {
|
||||
zero_in_degree_queue.push(node_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否所有节点都被处理
|
||||
if (result.size() != nodes_.size()) {
|
||||
return {}; // 有环
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool FlowEngine::has_cycle() const {
|
||||
std::map<std::string, bool> visited;
|
||||
std::map<std::string, bool> rec_stack;
|
||||
|
||||
for (const auto& [node_id, _] : nodes_) {
|
||||
visited[node_id] = false;
|
||||
rec_stack[node_id] = false;
|
||||
}
|
||||
|
||||
for (const auto& [node_id, _] : nodes_) {
|
||||
if (!visited[node_id]) {
|
||||
if (dfs_cycle_detection(node_id, visited, rec_stack)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FlowEngine::dfs_cycle_detection(const std::string& node_id,
|
||||
std::map<std::string, bool>& visited,
|
||||
std::map<std::string, bool>& rec_stack) const {
|
||||
visited[node_id] = true;
|
||||
rec_stack[node_id] = true;
|
||||
|
||||
auto node_it = nodes_.find(node_id);
|
||||
if (node_it != nodes_.end()) {
|
||||
const auto& node = node_it->second;
|
||||
for (const auto& dep_id : node->get_dependencies()) {
|
||||
if (nodes_.find(dep_id) != nodes_.end()) {
|
||||
if (!visited[dep_id]) {
|
||||
if (dfs_cycle_detection(dep_id, visited, rec_stack)) {
|
||||
return true;
|
||||
}
|
||||
} else if (rec_stack[dep_id]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rec_stack[node_id] = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
// MultiAgentSystem 类实现
|
||||
MultiAgentSystem::MultiAgentSystem()
|
||||
: flow_engine_(std::make_unique<FlowEngine>()) {
|
||||
}
|
||||
|
||||
bool MultiAgentSystem::initialize() {
|
||||
std::cout << "Initializing Multi-Agent System...\n";
|
||||
|
||||
// 注册示例智能体
|
||||
AgentMetadata agent1{
|
||||
"code_generator",
|
||||
"Code Generator Agent",
|
||||
"1.0.0",
|
||||
"http://localhost:8001",
|
||||
{"code_generation", "cpp", "python"},
|
||||
"{\"type\":\"object\",\"properties\":{\"requirements\":{\"type\":\"string\"}}}",
|
||||
"{\"type\":\"object\",\"properties\":{\"generated_code\":{\"type\":\"string\"}}}",
|
||||
AgentStatus::IDLE,
|
||||
std::chrono::system_clock::now()
|
||||
};
|
||||
|
||||
AgentMetadata agent2{
|
||||
"code_reviewer",
|
||||
"Code Reviewer Agent",
|
||||
"1.0.0",
|
||||
"http://localhost:8002",
|
||||
{"code_review", "quality_check", "security_analysis"},
|
||||
"{\"type\":\"object\",\"properties\":{\"code\":{\"type\":\"string\"}}}",
|
||||
"{\"type\":\"object\",\"properties\":{\"review_comments\":{\"type\":\"string\"}}}",
|
||||
AgentStatus::IDLE,
|
||||
std::chrono::system_clock::now()
|
||||
};
|
||||
|
||||
AgentMetadata agent3{
|
||||
"documentation_generator",
|
||||
"Documentation Generator Agent",
|
||||
"1.0.0",
|
||||
"http://localhost:8003",
|
||||
{"documentation", "markdown", "api_docs"},
|
||||
"{\"type\":\"object\",\"properties\":{\"code\":{\"type\":\"string\"}}}",
|
||||
"{\"type\":\"object\",\"properties\":{\"documentation\":{\"type\":\"string\"}}}",
|
||||
AgentStatus::IDLE,
|
||||
std::chrono::system_clock::now()
|
||||
};
|
||||
|
||||
flow_engine_->register_agent(agent1);
|
||||
flow_engine_->register_agent(agent2);
|
||||
flow_engine_->register_agent(agent3);
|
||||
|
||||
std::cout << "System initialized with 3 demo agents.\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
void MultiAgentSystem::demo_basic_flow() {
|
||||
std::cout << "\n=== Demo: Basic Sequential Flow ===\n";
|
||||
|
||||
// 创建执行节点
|
||||
auto node1 = std::make_shared<ExecutionNode>("generate_code", "code_generator");
|
||||
auto node2 = std::make_shared<ExecutionNode>("review_code", "code_reviewer");
|
||||
auto node3 = std::make_shared<ExecutionNode>("generate_docs", "documentation_generator");
|
||||
|
||||
// 设置依赖关系:生成代码 -> 代码审查 -> 生成文档
|
||||
node2->add_dependency("generate_code");
|
||||
node3->add_dependency("review_code");
|
||||
|
||||
// 添加到引擎
|
||||
flow_engine_->add_node(node1);
|
||||
flow_engine_->add_node(node2);
|
||||
flow_engine_->add_node(node3);
|
||||
|
||||
// 设置全局上下文
|
||||
auto context = std::make_shared<ExecutionContext>();
|
||||
context->set_global_value("requirements", "Create a simple C++ class with methods");
|
||||
|
||||
// 执行顺序流
|
||||
std::cout << "Executing sequential flow...\n";
|
||||
bool success = flow_engine_->execute_sequential();
|
||||
|
||||
if (success) {
|
||||
std::cout << "Flow execution completed successfully!\n";
|
||||
} else {
|
||||
std::cout << "Flow execution failed.\n";
|
||||
}
|
||||
|
||||
// 显示执行状态
|
||||
auto status = flow_engine_->get_execution_status();
|
||||
std::cout << "Execution status:\n";
|
||||
for (const auto& [node_id, node_status] : status) {
|
||||
std::cout << " " << node_id << ": ";
|
||||
switch (node_status) {
|
||||
case ExecutionStatus::COMPLETED:
|
||||
std::cout << "COMPLETED\n"; break;
|
||||
case ExecutionStatus::FAILED:
|
||||
std::cout << "FAILED\n"; break;
|
||||
case ExecutionStatus::RUNNING:
|
||||
std::cout << "RUNNING\n"; break;
|
||||
case ExecutionStatus::PENDING:
|
||||
std::cout << "PENDING\n"; break;
|
||||
default:
|
||||
std::cout << "UNKNOWN\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MultiAgentSystem::demo_parallel_execution() {
|
||||
std::cout << "\n=== Demo: Parallel Execution ===\n";
|
||||
|
||||
// 清空之前的节点
|
||||
flow_engine_->clear();
|
||||
|
||||
// 创建可以并行执行的节点
|
||||
auto node1 = std::make_shared<ExecutionNode>("generate_code_1", "code_generator");
|
||||
auto node2 = std::make_shared<ExecutionNode>("generate_code_2", "code_generator");
|
||||
auto node3 = std::make_shared<ExecutionNode>("review_parallel", "code_reviewer");
|
||||
|
||||
// 设置依赖:两个代码生成节点可以并行执行,然后执行代码审查
|
||||
node3->add_dependency("generate_code_1");
|
||||
node3->add_dependency("generate_code_2");
|
||||
|
||||
// 添加到引擎
|
||||
flow_engine_->add_node(node1);
|
||||
flow_engine_->add_node(node2);
|
||||
flow_engine_->add_node(node3);
|
||||
|
||||
std::cout << "Executing parallel-capable flow...\n";
|
||||
bool success = flow_engine_->execute_parallel();
|
||||
|
||||
if (success) {
|
||||
std::cout << "Parallel execution completed!\n";
|
||||
} else {
|
||||
std::cout << "Parallel execution failed.\n";
|
||||
}
|
||||
}
|
||||
|
||||
void MultiAgentSystem::run() {
|
||||
std::cout << "========================================\n";
|
||||
std::cout << "Multi-Agent System Started\n";
|
||||
std::cout << "========================================\n\n";
|
||||
|
||||
// 初始化系统
|
||||
if (!initialize()) {
|
||||
std::cerr << "Failed to initialize system.\n";
|
||||
return;
|
||||
}
|
||||
|
||||
// 运行演示
|
||||
demo_basic_flow();
|
||||
demo_parallel_execution();
|
||||
|
||||
std::cout << "\n========================================\n";
|
||||
std::cout << "System Demo Completed\n";
|
||||
std::cout << "========================================\n";
|
||||
}
|
||||
|
||||
} // namespace multi_agent
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
#include "app.hpp"
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
std::cout << "========================================\n";
|
||||
std::cout << "Multi-Agent Cooperative System v1.0.0\n";
|
||||
std::cout << "========================================\n\n";
|
||||
|
||||
// 检查命令行参数
|
||||
if (argc > 1) {
|
||||
std::string command = argv[1];
|
||||
if (command == "--help" || command == "-h") {
|
||||
std::cout << "Usage: multi_agent_system [OPTION]\n\n";
|
||||
std::cout << "Options:\n";
|
||||
std::cout << " --help, -h Show this help message\n";
|
||||
std::cout << " --version, -v Show version information\n";
|
||||
std::cout << " --demo, -d Run system demo (default)\n";
|
||||
std::cout << " --test, -t Run system tests\n";
|
||||
std::cout << "\n";
|
||||
std::cout << "Without options, runs the system demo.\n";
|
||||
return 0;
|
||||
} else if (command == "--version" || command == "-v") {
|
||||
std::cout << "Multi-Agent Cooperative System\n";
|
||||
std::cout << "Version: 1.0.0\n";
|
||||
std::cout << "Built with C++17\n";
|
||||
std::cout << "CMake Project\n";
|
||||
return 0;
|
||||
} else if (command == "--test" || command == "-t") {
|
||||
std::cout << "Running system tests...\n";
|
||||
std::cout << "Note: Please run 'basic_test' executable for actual tests.\n";
|
||||
return 0;
|
||||
} else if (command == "--demo" || command == "-d") {
|
||||
// 继续执行演示
|
||||
} else {
|
||||
std::cerr << "Unknown option: " << command << "\n";
|
||||
std::cerr << "Use --help for usage information.\n";
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// 创建并运行多智能体系统
|
||||
multi_agent::MultiAgentSystem system;
|
||||
|
||||
std::cout << "System Description:\n";
|
||||
std::cout << " - Implements core multi-agent coordination framework\n";
|
||||
std::cout << " - Supports agent registration, discovery, and management\n";
|
||||
std::cout << " - Provides execution flow engine with DAG support\n";
|
||||
std::cout << " - Supports sequential and parallel execution modes\n";
|
||||
std::cout << " - Includes context management for data sharing\n";
|
||||
std::cout << " - Built with extensibility in mind\n\n";
|
||||
|
||||
std::cout << "Starting system execution...\n\n";
|
||||
|
||||
// 运行系统
|
||||
system.run();
|
||||
|
||||
std::cout << "\nSystem execution completed successfully.\n";
|
||||
return 0;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "\nError: " << e.what() << "\n";
|
||||
std::cerr << "System execution failed.\n";
|
||||
return 1;
|
||||
} catch (...) {
|
||||
std::cerr << "\nUnknown error occurred.\n";
|
||||
std::cerr << "System execution failed.\n";
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,195 @@
|
|||
#include "../include/app.hpp"
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
// 测试智能体元数据结构
|
||||
void test_agent_metadata() {
|
||||
std::cout << "Testing AgentMetadata...\n";
|
||||
|
||||
multi_agent::AgentMetadata metadata{
|
||||
"test_agent",
|
||||
"Test Agent",
|
||||
"1.0.0",
|
||||
"http://localhost:8080",
|
||||
{"test", "demo"},
|
||||
"{}",
|
||||
"{}",
|
||||
multi_agent::AgentStatus::IDLE,
|
||||
std::chrono::system_clock::now()
|
||||
};
|
||||
|
||||
assert(metadata.id == "test_agent");
|
||||
assert(metadata.name == "Test Agent");
|
||||
assert(metadata.version == "1.0.0");
|
||||
assert(metadata.service_url == "http://localhost:8080");
|
||||
assert(metadata.capabilities.size() == 2);
|
||||
assert(metadata.capabilities[0] == "test");
|
||||
assert(metadata.capabilities[1] == "demo");
|
||||
assert(metadata.status == multi_agent::AgentStatus::IDLE);
|
||||
|
||||
std::cout << " AgentMetadata test passed.\n";
|
||||
}
|
||||
|
||||
// 测试智能体类
|
||||
void test_agent_class() {
|
||||
std::cout << "Testing Agent class...\n";
|
||||
|
||||
multi_agent::AgentMetadata metadata{
|
||||
"test_agent",
|
||||
"Test Agent",
|
||||
"1.0.0",
|
||||
"http://localhost:8080",
|
||||
{"test"},
|
||||
"{}",
|
||||
"{}",
|
||||
multi_agent::AgentStatus::IDLE,
|
||||
std::chrono::system_clock::now()
|
||||
};
|
||||
|
||||
multi_agent::Agent agent(metadata);
|
||||
|
||||
// 检查元数据获取
|
||||
const auto& agent_metadata = agent.get_metadata();
|
||||
assert(agent_metadata.id == "test_agent");
|
||||
assert(agent_metadata.name == "Test Agent");
|
||||
|
||||
// 测试执行
|
||||
std::string result = agent.execute("test input");
|
||||
assert(!result.empty());
|
||||
assert(result.find("test_agent") != std::string::npos);
|
||||
|
||||
std::cout << " Agent class test passed.\n";
|
||||
}
|
||||
|
||||
// 测试执行节点
|
||||
void test_execution_node() {
|
||||
std::cout << "Testing ExecutionNode...\n";
|
||||
|
||||
multi_agent::ExecutionNode node("node1", "agent1");
|
||||
|
||||
assert(node.get_id() == "node1");
|
||||
assert(node.get_agent_id() == "agent1");
|
||||
assert(node.get_status() == multi_agent::ExecutionStatus::PENDING);
|
||||
|
||||
// 测试依赖添加
|
||||
node.add_dependency("node0");
|
||||
const auto& deps = node.get_dependencies();
|
||||
assert(deps.size() == 1);
|
||||
assert(deps[0] == "node0");
|
||||
|
||||
node.set_status(multi_agent::ExecutionStatus::COMPLETED);
|
||||
assert(node.get_status() == multi_agent::ExecutionStatus::COMPLETED);
|
||||
|
||||
std::cout << " ExecutionNode test passed.\n";
|
||||
}
|
||||
|
||||
// 测试执行上下文
|
||||
void test_execution_context() {
|
||||
std::cout << "Testing ExecutionContext...\n";
|
||||
|
||||
multi_agent::ExecutionContext context;
|
||||
|
||||
// 测试全局上下文
|
||||
context.set_global_value("key1", "value1");
|
||||
assert(context.get_global_value("key1") == "value1");
|
||||
assert(context.get_global_value("nonexistent").empty());
|
||||
|
||||
// 测试节点输出
|
||||
context.set_node_output("node1", "output1");
|
||||
assert(context.get_node_output("node1") == "output1");
|
||||
assert(context.get_node_output("nonexistent").empty());
|
||||
|
||||
// 测试序列化(简化版本)
|
||||
std::string snapshot = context.serialize();
|
||||
assert(!snapshot.empty());
|
||||
|
||||
std::cout << " ExecutionContext test passed.\n";
|
||||
}
|
||||
|
||||
// 测试流引擎基础功能
|
||||
void test_flow_engine_basic() {
|
||||
std::cout << "Testing FlowEngine basic functionality...\n";
|
||||
|
||||
multi_agent::FlowEngine engine;
|
||||
|
||||
// 注册智能体
|
||||
multi_agent::AgentMetadata metadata{
|
||||
"test_agent",
|
||||
"Test Agent",
|
||||
"1.0.0",
|
||||
"http://localhost:8080",
|
||||
{"test"},
|
||||
"{}",
|
||||
"{}",
|
||||
multi_agent::AgentStatus::IDLE,
|
||||
std::chrono::system_clock::now()
|
||||
};
|
||||
|
||||
engine.register_agent(metadata);
|
||||
|
||||
// 获取智能体
|
||||
auto agent = engine.get_agent("test_agent");
|
||||
assert(agent != nullptr);
|
||||
assert(agent->get_metadata().id == "test_agent");
|
||||
|
||||
// 获取不存在的智能体
|
||||
auto non_existent = engine.get_agent("non_existent");
|
||||
assert(non_existent == nullptr);
|
||||
|
||||
std::cout << " FlowEngine basic test passed.\n";
|
||||
}
|
||||
|
||||
// 测试拓扑排序(简单DAG)
|
||||
void test_topological_sort() {
|
||||
std::cout << "Testing topological sort...\n";
|
||||
|
||||
multi_agent::FlowEngine engine;
|
||||
|
||||
// 创建节点:node1 -> node2 -> node3
|
||||
auto node1 = std::make_shared<multi_agent::ExecutionNode>("node1", "agent1");
|
||||
auto node2 = std::make_shared<multi_agent::ExecutionNode>("node2", "agent2");
|
||||
auto node3 = std::make_shared<multi_agent::ExecutionNode>("node3", "agent3");
|
||||
|
||||
node2->add_dependency("node1");
|
||||
node3->add_dependency("node2");
|
||||
|
||||
engine.add_node(node1);
|
||||
engine.add_node(node2);
|
||||
engine.add_node(node3);
|
||||
|
||||
// 注意:由于FlowEngine的拓扑排序是私有方法,我们无法直接测试
|
||||
// 这里只是验证节点添加成功
|
||||
auto status = engine.get_execution_status();
|
||||
assert(status.size() == 3);
|
||||
|
||||
std::cout << " Topological sort test framework ready.\n";
|
||||
}
|
||||
|
||||
// 主测试函数
|
||||
int main() {
|
||||
std::cout << "========================================\n";
|
||||
std::cout << "Running Multi-Agent System Tests\n";
|
||||
std::cout << "========================================\n\n";
|
||||
|
||||
try {
|
||||
test_agent_metadata();
|
||||
test_agent_class();
|
||||
test_execution_node();
|
||||
test_execution_context();
|
||||
test_flow_engine_basic();
|
||||
test_topological_sort();
|
||||
|
||||
std::cout << "\n========================================\n";
|
||||
std::cout << "All tests passed successfully!\n";
|
||||
std::cout << "========================================\n";
|
||||
|
||||
return 0;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "\nTest failed with exception: " << e.what() << "\n";
|
||||
return 1;
|
||||
} catch (...) {
|
||||
std::cerr << "\nTest failed with unknown exception.\n";
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue