本文档帮助开发者快速上手 AgentHub 项目开发,涵盖环境搭建、编码规范、测试流程等。
| 工具 | 版本 | 用途 |
|---|---|---|
| JDK | 21+ | 编译运行 |
| Gradle | 8.0+ | 构建工具(可使用 wrapper) |
| Node.js | 18+ | 前端开发 |
| npm | 9+ | 前端包管理 |
| Docker | 最新 | 运行基础设施(推荐) |
| IntelliJ IDEA | 2024+ | 推荐 IDE |
# 1. 克隆项目
git clone https://github.com/HuangDayu/AgentHub.git
cd AgentHub
# 2. 启动基础设施(Docker)
docker run -d --name postgres -e POSTGRES_USER=things -e POSTGRES_PASSWORD=things123 -e POSTGRES_DB=agenthub -p 5432:5432 postgres:16
docker run -d --name redis -p 6379:6379 redis:7
docker run -d --name qdrant -p 6333:6333 -p 6334:6334 qdrant/qdrant
# 3. 构建项目
./gradlew clean build
# 4. 运行测试
./gradlew test
# 5. 启动应用
./gradlew bootRun
# 进入前端目录
cd src/main/web
# 安装依赖
npm install
# 启动开发服务器(热重载)
npm run dev
src/main/java/com/agenthub/
├── api/ # 接口适配层
│ ├── controller/ # REST 控制器
│ ├── dto/ # 请求/响应 DTO
│ ├── mapper/ # DTO 映射器
│ └── exception/ # 异常处理
├── application/ # 应用层
│ ├── usecase/ # 用例实现
│ ├── command/ # 命令对象
│ ├── dto/ # 应用层 DTO
│ ├── executor/ # 执行器
│ ├── service/ # 应用服务
│ └── port/ # 端口接口
│ ├── in/ # 输入端口
│ └── out/ # 输出端口
├── domain/ # 领域层
│ ├── model/ # 领域模型
│ ├── event/ # 领域事件
│ └── exception/ # 领域异常
├── infrastructure/ # 基础设施层
│ ├── agents/ # Agent 运行时
│ ├── auth/ # 认证授权
│ ├── context/ # 上下文管理
│ ├── etl/ # ETL 管道
│ ├── rag/ # RAG 流水线
│ ├── store/ # 数据存储
│ ├── tools/ # 工具实现
│ ├── vector/ # 向量存储
│ ├── web/ # Web 配置
│ └── wiki/ # Wiki 集成
└── common/ # 公共组件
├── annotations/ # 自定义注解
├── constants/ # 常量
└── utils/ # 工具类
每个功能模块(如 Agent、知识库、工作流等)遵循如下结构:
模块/
├── api/
│ ├── controller/ModuleController.java # REST 接口
│ ├── dto/ModuleRequest.java # 请求 DTO
│ ├── dto/ModuleResponse.java # 响应 DTO
│ └── mapper/ModuleResponseMapper.java # DTO 映射
├── application/
│ ├── usecase/ModuleUseCase.java # 用例实现
│ ├── command/ModuleCommand.java # 命令对象
│ ├── dto/ModuleOutput.java # 应用 DTO
│ └── port/out/repositories/ # 仓储接口
├── domain/
│ └── model/Module.java # 领域模型
└── infrastructure/
└── store/db/
├── entity/ModuleEntity.java # 数据库实体
├── mapper/ModuleMapper.java # MyBatis Mapper
└── repository/MybatisModuleRepository.java # 仓储实现
| 规范项 | 要求 |
|---|---|
| 架构模式 | 整洁架构(Clean Architecture) |
| 设计原则 | SOLID |
| 开发流程 | TDD(Red → Green → Refactor) |
| 编码风格 | Alibaba P3C 规范 |
| 注解工具 | 提倡 Lombok,禁止使用 record 类 |
| 方法长度 | 不超过 10 行 |
| 方法注释 | 必须有 Javadoc 注释 |
| 参数数量 | 不超过 3 个 |
| 禁止项 | 禁止写伪代码、Thread.sleep() 等 |
| 类型 | 命名规则 | 示例 |
|---|---|---|
| Controller | *Controller |
AgentController |
| UseCase | *UseCase |
AgentUseCase |
| Repository 接口 | *Repository |
AgentRepository |
| Repository 实现 | Mybatis*Repository |
MybatisAgentRepository |
| Entity | *Entity |
AgentEntity |
| Mapper | *Mapper |
AgentMapper |
| Request DTO | *Request |
CreateAgentRequest |
| Response DTO | *Response |
AgentResponse |
| Command | *Command |
CreateAgentCommand |
| Port | *Port |
AgentPort |
ArchUnit 规则在测试阶段自动校验以下约束:
// API 层只能依赖 Application 层
layeredArchitecture()
.layer("API").definedBy("..api..")
.layer("APPLICATION").definedBy("..application..")
.whereLayer("API").mayOnlyBeAccessedByLayers("API")
// Domain 层不能依赖外部框架
noClasses()
.that().resideInAPackage("..domain..")
.should().dependOnClassesThat().resideInAnyPackage(
"..spring..", "..jakarta..", "..kafka.."
)
// Application 层不能依赖 Infrastructure 层
noClasses()
.that().resideInAPackage("..application..")
.should().dependOnClassesThat().resideInAPackage("..infrastructure..")
| 测试类型 | 说明 | 框架 |
|---|---|---|
| 单元测试 | 测试领域模型和工具类 | JUnit 6 |
| 集成测试 | 测试 Controller 完整请求链路 | Spring Boot Test |
| 架构测试 | 校验分层依赖和命名约定 | ArchUnit |
# 运行所有测试
./gradlew test
# 运行架构测试
./gradlew test --tests "*ArchTest*"
# 运行特定集成测试
./gradlew test --tests "*AgentConfigControllerIntegrationTest*"
# 运行测试并生成报告
./gradlew test jacocoTestReport
@SpringBootTest
@AutoConfigureMockMvc
class AgentConfigControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Test
void shouldCreateConfig() throws Exception {
mockMvc.perform(post("/api/v1/workspaces/{wsId}/agents/{id}/configs")
.contentType(MediaType.APPLICATION_JSON)
.content("{}"))
.andExpect(status().isCreated());
}
}
1. Red: 编写失败的测试用例
2. Green: 编写最小实现代码使测试通过
3. Refactor: 重构代码,确保测试依然通过
domain/model/ 中创建或修改领域模型application/port/ 中定义输入/输出端口application/usecase/ 中创建 UseCaseinfrastructure/store/ 中实现仓储接口api/controller/ 中添加 REST 接口src/main/web/ 中添加视图推荐插件:
代码风格:
推荐扩展:
domain/model/ 中创建模型类application/command/ 中创建对应 Commandapplication/dto/ 中创建应用层 DTOinfrastructure/store/db/entity/ 中创建 Entityinfrastructure/store/db/mapper/ 中创建 Mapperapplication/port/out/repositories/ 中创建 Repository 接口infrastructure/store/db/repository/ 中创建 MyBatis 实现api/controller/ 中添加(或修改)Controllerapi/dto/ 中创建请求/响应 DTOapi/mapper/ 中添加 DTO 映射domain/model/ 中定义策略模型application/port/out/repositories/ 中定义仓储接口api/controller/ 中创建管理端点