# CC Cloud API 文档 本文档详细描述 CC Cloud 后端 API 接口。 ## 基础信息 - **Base URL**: `http://localhost:3000/api` - **认证方式**: JWT Bearer Token - **Content-Type**: `application/json` ## 认证 ### 验证密码 验证访问密码并获取令牌。 **请求** ```http POST /auth/verify-password Content-Type: application/json { "password": "string" } ``` **响应** ```json { "success": true, "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." } ``` ### 验证 Token 验证当前 Token 是否有效。 **请求** ```http GET /auth/verify Authorization: Bearer ``` **响应** ```json { "valid": true, "authenticated": true } ``` ## 实例管理 ### 创建实例 创建新的 Claude Code 实例。 **请求** ```http POST /instances/start Authorization: Bearer Content-Type: application/json { "name": "string", // 实例名称 "config": { "model": "string", // Claude 模型名称 "apiKey": "string", // API Key(可选) "workingDir": "string", // 工作目录路径 "port": 4000, // 端口(可选,自动分配) "maxMemory": 512, // 最大内存(MB,可选) "env": { // 环境变量(可选) "KEY": "value" } } } ``` **响应** ```json { "instance": { "id": "string", "name": "string", "status": "stopped", "config": { ... }, "stats": { "memory": 0, "cpu": 0, "restarts": 0, "status": "offline" }, "createdAt": "2024-01-01T00:00:00.000Z" } } ``` ### 列出所有实例 获取所有实例列表。 **请求** ```http GET /instances Authorization: Bearer ``` **响应** ```json { "instances": [ { "id": "string", "name": "string", "status": "running", "config": { ... }, "stats": { ... }, "createdAt": "2024-01-01T00:00:00.000Z", "lastStarted": "2024-01-01T01:00:00.000Z" } ] } ``` ### 获取实例详情 获取指定实例的详细信息。 **请求** ```http GET /instances/:instanceId Authorization: Bearer ``` **响应** ```json { "instance": { ... } } ``` ### 启动实例 启动已停止的实例。 **请求** ```http POST /instances/:instanceId/start Authorization: Bearer ``` **响应** ```json { "success": true, "message": "实例启动成功" } ``` ### 停止实例 停止正在运行的实例。 **请求** ```http POST /instances/:instanceId/stop Authorization: Bearer ``` **响应** ```json { "success": true, "message": "实例停止成功" } ``` ### 删除实例 删除指定实例(会先停止实例)。 **请求** ```http DELETE /instances/:instanceId Authorization: Bearer ``` **响应** ```json { "success": true, "message": "实例删除成功" } ``` ### 获取实例状态 获取实例的实时状态信息。 **请求** ```http GET /instances/:instanceId/status Authorization: Bearer ``` **响应** ```json { "status": "running", "stats": { "pid": 12345, "port": 4000, "uptime": 3600, "memory": 134217728, "cpu": 5.2, "restarts": 0, "status": "online" } } ``` ## 数据模型 ### Instance 实例对象结构: ```typescript interface Instance { id: string; // 实例 ID (UUID) name: string; // 实例名称 status: 'running' | 'stopped' | 'errored' | 'offline'; config: InstanceConfig; // 实例配置 stats: InstanceStats; // 实时统计 createdAt: Date; // 创建时间 lastStarted?: Date; // 最后启动时间 } ``` ### InstanceConfig 实例配置: ```typescript interface InstanceConfig { model: string; // Claude 模型名称 apiKey?: string; // Anthropic API Key(可选) workingDir: string; // 工作目录路径 port?: number; // HTTP 服务端口 maxMemory?: number; // 最大内存限制(MB) env?: Record; // 环境变量 } ``` ### InstanceStats 实例统计信息: ```typescript interface InstanceStats { pid?: number; // 进程 ID port?: number; // HTTP 服务端口 uptime?: number; // 运行时长(秒) memory: number; // 内存使用(字节) cpu: number; // CPU 使用率(%) restarts: number; // 重启次数 status: 'online' | 'offline'; // 进程状态 } ``` ## 错误响应 所有错误响应遵循统一格式: ```json { "error": "错误描述", "message": "详细错误信息(可选)" } ``` ### HTTP 状态码 - `200` - 成功 - `201` - 创建成功 - `400` - 请求错误 - `401` - 未授权 - `404` - 资源不存在 - `500` - 服务器错误 ## WebSocket 事件 ### 连接 ```javascript import { io } from 'socket.io-client'; const socket = io('http://localhost:3000', { auth: { token: 'your-jwt-token' } }); ``` ### 加入实例房间 ```javascript socket.emit('join-instance', 'instance-id'); ``` ### 离开实例房间 ```javascript socket.emit('leave-instance', 'instance-id'); ``` ### 监听事件(待实现) ```javascript // 实例状态变化 socket.on('instance-status-changed', (data) => { console.log('Status:', data.status); }); // 日志更新 socket.on('log-update', (data) => { console.log('Log:', data.log); }); // 监控数据更新 socket.on('monitor-update', (data) => { console.log('Stats:', data.stats); }); ``` ## 使用示例 ### 创建并启动实例 ```bash # 1. 登录获取 token TOKEN=$(curl -X POST http://localhost:3000/api/auth/login \ -H "Content-Type: application/json" \ -d '{"username":"admin","password":"admin123"}' \ | jq -r '.token') # 2. 创建实例 INSTANCE=$(curl -X POST http://localhost:3000/api/instances/start \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{ "name": "My Instance", "config": { "model": "claude-sonnet-4-5", "workingDir": "/path/to/project" } }' | jq -r '.instance.id') # 3. 启动实例 curl -X POST http://localhost:3000/api/instances/$INSTANCE/start \ -H "Authorization: Bearer $TOKEN" # 4. 查看状态 curl http://localhost:3000/api/instances/$INSTANCE/status \ -H "Authorization: Bearer $TOKEN" ``` ### JavaScript 示例 ```javascript import axios from 'axios'; const API = 'http://localhost:3000/api'; let token = ''; // 登录 async function login(username, password) { const response = await axios.post(`${API}/auth/login`, { username, password }); token = response.data.token; return response.data; } // 创建实例 async function createInstance(name, config) { const response = await axios.post(`${API}/instances/start`, { name, config }, { headers: { Authorization: `Bearer ${token}` } }); return response.data.instance; } // 启动实例 async function startInstance(instanceId) { const response = await axios.post(`${API}/instances/${instanceId}/start`, {}, { headers: { Authorization: `Bearer ${token}` } }); return response.data; } // 使用示例 await login('admin', 'admin123'); const instance = await createInstance('My Instance', { model: 'claude-sonnet-4-5', workingDir: '/path/to/project' }); await startInstance(instance.id); ```