# AgentJava **Repository Path**: North_Fan/agent-java ## Basic Information - **Project Name**: AgentJava - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-07-12 - **Last Updated**: 2026-07-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # AgentTeams — 企业级多租户多用户 AgentTeams 调用平台 基于 K8S 的自托管 E2B 沙箱,支持多租户、多用户隔离的 AgentTeams 编排调用。 参考 [agentscope-java](https://github.com/agentscope-ai/agentscope-java) 的沙箱架构设计,扩展为完整的企业级 Web 服务。 ## 架构 ``` ┌─────────────────────────────────────────────────────────┐ │ AgentTeams Web Service │ │ (Spring Boot, port 8080) │ │ ┌──────────┐ ┌──────────┐ ┌──────────────────────┐ │ │ │JWT 认证 │ │租户隔离 │ │AgentTeams 编排器 │ │ │ │/api/auth │ │TenantCtx │ │TeamOrchestrator │ │ │ └──────────┘ └──────────┘ └─────────┬────────────┘ │ │ │ │ │ ┌──────────────────────────┘ │ │ ▼ │ │ ┌──────────────────────────────────────────────────┐ │ │ │ SandboxManager (隔离键 + 状态恢复) │ │ │ │ IsolationScope: SESSION / USER / TENANT / TEAM │ │ │ └──────────────────────┬───────────────────────────┘ │ │ │ │ └─────────────────────────┼────────────────────────────────┘ │ HTTP (E2B API) ▼ ┌─────────────────────────────────────────────────────────┐ │ 自托管 E2B 沙箱服务 (Docker/K8S) │ │ ┌─────────────────────────────────────────────────┐ │ │ │ e2b-sandbox-server (Flask, port 3000) │ │ │ │ POST /sandboxes → 创建沙箱 Pod/Container │ │ │ │ POST /sandboxes/{id}/exec → 执行命令 │ │ │ │ DELETE /sandboxes/{id} → 销毁沙箱 │ │ │ └─────────────────────┬───────────────────────────┘ │ │ │ │ │ ┌─────────────────────┘ │ │ ▼ │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ sandbox-pod │ │ sandbox-pod │ │ sandbox-pod │ │ │ │ (ubuntu) │ │ (ubuntu) │ │ (ubuntu) │ │ │ │ tenant-A │ │ tenant-B │ │ tenant-A │ │ │ │ user-alice │ │ user-bob │ │ user-carol │ │ │ └─────────────┘ └─────────────┘ └─────────────┘ │ └─────────────────────────────────────────────────────────┘ ``` ## 项目结构 ``` AgentJava/ ├── pom.xml # 父 POM (Java 17, 多模块 Maven) ├── agentteams-core/ # 核心模块 │ └── src/main/java/com/agentteams/core/ │ ├── tenant/ # Tenant, User, TenantContext │ ├── isolation/ # IsolationScope, SandboxIsolationKey │ ├── sandbox/ # Sandbox, SandboxClient, SandboxManager │ └── team/ # AgentTeam, AgentMember, TeamOrchestrator ├── agentteams-sandbox-e2b/ # E2B 沙箱模块 (自托管) │ └── src/main/java/com/agentteams/sandbox/e2b/ │ ├── E2bSandboxClient.java # 沙箱客户端工厂 │ ├── E2bSandbox.java # 沙箱实现 (HTTP exec) │ ├── E2bPlatformHttp.java # E2B API HTTP 客户端 │ └── E2bSandboxClientOptions.java ├── agentteams-web/ # Spring Boot Web 服务 │ └── src/main/java/com/agentteams/web/ │ ├── controller/ # REST 控制器 │ ├── service/ # 业务服务 │ ├── security/ # JWT 认证 │ └── config/ # 沙箱配置 ├── docker/ # Docker 部署 │ ├── docker-compose.yml # K3s + E2B 沙箱服务 │ ├── e2b-sandbox-server/ # E2B 兼容 API 服务 │ └── k8s/ # K8S 部署清单 └── Dockerfile # Web 应用镜像 ``` ## 快速开始 ### 前置条件 - Java 17+ (开发环境) - Docker (运行沙箱) ### 1. 启动 E2B 沙箱服务 ```bash cd docker docker-compose up -d # 验证服务 curl http://localhost:3010/health # {"status":"healthy",...} ``` ### 2. 启动 AgentTeams Web 服务 **本地开发 (InMemory 沙箱, 无需 Docker):** ```bash .\mvnw.cmd spring-boot:run -pl agentteams-web ``` **使用 E2B 沙箱:** ```bash # 设置环境变量 $env:SANDBOX_TYPE="e2b" $env:E2B_SANDBOX_URL="http://localhost:3010" .\mvnw.cmd spring-boot:run -pl agentteams-web ``` ### 3. 使用 API ```bash # 登录获取 JWT (默认管理员: default/admin/admin123) TOKEN=$(curl -s http://localhost:8080/api/auth/login \ -H "Content-Type: application/json" \ -d '{"tenantId":"default","userId":"admin","password":"admin123"}' \ | jq -r .token) # 创建 AgentTeam curl -s http://localhost:8080/api/teams \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{ "teamId": "dev-team", "name": "开发团队", "isolationScope": "USER", "members": [ {"memberId":"coder","name":"编码Agent","role":"coder","executeCommand":"echo coding ${input}"}, {"memberId":"tester","name":"测试Agent","role":"tester","executeCommand":"echo testing ${input}"} ] }' # 调用团队 curl -s http://localhost:8080/api/teams/dev-team/invoke \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{"input":"hello world"}' ``` ## 多租户隔离 沙箱隔离由 `IsolationScope` 枚举控制: | 作用域 | 隔离维度 | 说明 | |--------|---------|------| | `SESSION` | tenant + session | 每个会话独立沙箱 | | `USER` | tenant + user | 同一用户跨会话复用沙箱 (默认) | | `TENANT` | tenant | 同一租户所有用户共享沙箱 | | `TEAM` | tenant + teamId | 同一团队复用沙箱 | | `GLOBAL` | - | 全局共享 (谨慎使用) | 隔离键 = `(scope, tenantId, value)`,确保不同租户的沙箱状态永不冲突。 ## 技术栈 - **Java 17** + Maven 多模块 - **Spring Boot 3.2** + Spring Security (JWT) - **E2B 沙箱** (自托管, HTTP API) - **K3s** (轻量级 K8S, Docker-in-Docker) - **OkHttp** (E2B API 调用) - **Jackson** (JSON 序列化) ## 参考 - [agentscope-java](https://github.com/agentscope-ai/agentscope-java) — Sandbox/IsolationScope 架构设计 - [E2B](https://e2b.dev) — 云沙箱服务 - [K3s](https://k3s.io) — 轻量级 Kubernetes