AI 舊程式碼智能重構系統 - REST API 完整規格
Base URL: http://localhost:8000 (開發環境)
API Version: v1
最後更新: 2026-02-08
所有需要認證的端點(標註 🔒)需要在 HTTP Header 中提供 JWT Token:
Authorization: Bearer <access_token>- 使用 POST /auth/register 註冊帳號
- 使用 POST /auth/login 登入取得
access_token - 在後續請求中帶上 Token
- 預設過期時間:24 小時
- Token 過期後需重新登入
所有錯誤回應遵循統一格式:
{
"detail": "錯誤訊息描述"
}| 狀態碼 | 說明 |
|---|---|
| 200 | 成功 |
| 201 | 建立成功 |
| 204 | 成功(無回應內容) |
| 400 | 請求錯誤(參數不正確) |
| 401 | 未認證(Token 無效或過期) |
| 403 | 無權限(非專案擁有者) |
| 404 | 資源不存在 |
| 500 | 伺服器錯誤 |
| 503 | 服務不可用(容器或 AI Server 錯誤) |
註冊新用戶帳號。
認證: 無需認證
{
"email": "user@example.com",
"username": "testuser",
"password": "password123"
}欄位說明:
| 欄位 | 類型 | 必填 | 限制 | 說明 |
|---|---|---|---|---|
| string | ✅ | Email 格式 | 用戶 Email(必須唯一) | |
| username | string | ✅ | 3-50 字元 | 用戶名稱 |
| password | string | ✅ | 8-100 字元 | 密碼 |
{
"id": "507f1f77bcf86cd799439011",
"email": "user@example.com",
"username": "testuser",
"is_active": true,
"created_at": "2026-02-02T12:00:00Z"
}400 Bad Request - Email 已存在
{
"detail": "Email already exists"
}用戶登入,取得 JWT Token。
認證: 無需認證
{
"username": "testuser",
"password": "password123"
}欄位說明:
| 欄位 | 類型 | 必填 | 說明 |
|---|---|---|---|
| username | string | ✅ | 用戶名稱 |
| password | string | ✅ | 密碼 |
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MDdmMWY3N2JjZjg2Y2Q3OTk0MzkwMTEiLCJlbWFpbCI6InVzZXJAZXhhbXBsZS5jb20iLCJleHAiOjE3MDcyMjU2MDB9.xxxxx",
"token_type": "bearer",
"expires_in": 86400
}欄位說明:
| 欄位 | 類型 | 說明 |
|---|---|---|
| access_token | string | JWT Token(用於後續認證) |
| token_type | string | Token 類型(固定為 "bearer") |
| expires_in | integer | Token 有效期限(秒數) |
401 Unauthorized - Username 或密碼錯誤
{
"detail": "Incorrect username or password"
}取得當前已登入用戶的資訊。
認證: 🔒 需要 Bearer Token
無需 Request Body
{
"id": "507f1f77bcf86cd799439011",
"email": "user@example.com",
"username": "testuser",
"is_active": true,
"created_at": "2026-02-02T12:00:00Z"
}401 Unauthorized - Token 無效或過期
{
"detail": "Invalid authentication"
}建立新專案。
認證: 🔒 需要 Bearer Token
{
"title": "My Refactor Project",
"project_type": "REFACTOR",
"repo_url": "https://github.com/user/repo.git",
"branch": "main",
"spec": "請先掃描專案並提出可量化的重構計劃"
}欄位說明:
| 欄位 | 類型 | 必填 | 預設值 | 說明 |
|---|---|---|---|---|
| title | string | ❌ | - | 專案標題(未填可由後端推導) |
| description | string | ❌ | - | 專案描述 |
| project_type | string | ❌ | "REFACTOR" | 專案類型:REFACTOR / SANDBOX |
| repo_url | string | 條件式 | - | Git Repository URL(REFACTOR 必填) |
| branch | string | ❌ | "main" | Git 分支名稱 |
| spec | string | ❌ | "" | 重構規格說明(Agent 主要輸入) |
{
"id": "507f1f77bcf86cd799439011",
"title": "My Refactor Project",
"description": null,
"project_type": "REFACTOR",
"repo_url": "https://github.com/user/repo.git",
"branch": "main",
"spec": "請先掃描專案並提出可量化的重構計劃",
"refactor_thread_id": null,
"status": "CREATED",
"container_id": null,
"created_at": "2026-02-02T12:00:00Z",
"updated_at": "2026-02-02T12:00:00Z",
"last_error": null,
"docker_status": null
}專案狀態說明:
| 狀態 | 說明 |
|---|---|
| CREATED | 已建立(尚未 Provision) |
| PROVISIONING | 正在建立容器 |
| READY | 容器就緒(可執行 Agent) |
| RUNNING | Agent 執行中 |
| STOPPED | 已停止 |
| FAILED | 失敗(查看 last_error) |
列出當前用戶的所有專案。
認證: 🔒 需要 Bearer Token
| 參數 | 類型 | 必填 | 預設值 | 說明 |
|---|---|---|---|---|
| skip | integer | ❌ | 0 | 跳過前 N 筆 |
| limit | integer | ❌ | 100 | 限制回傳筆數 |
GET /api/v1/projects?skip=0&limit=10{
"total": 5,
"projects": [
{
"id": "507f1f77bcf86cd799439011",
"repo_url": "https://github.com/user/repo.git",
"branch": "main",
"spec": "重構建議",
"status": "READY",
"container_id": "abc123def456",
"created_at": "2026-02-02T12:00:00Z",
"updated_at": "2026-02-02T12:05:00Z",
"last_error": null
}
]
}查詢專案詳細資訊(包含 Docker 容器即時狀態)。
認證: 🔒 需要 Bearer Token
| 參數 | 類型 | 說明 |
|---|---|---|
| id | string | 專案 ID |
| 參數 | 類型 | 必填 | 預設值 | 說明 |
|---|---|---|---|---|
| include_docker_status | boolean | ❌ | true | 是否查詢 Docker 容器即時狀態 |
GET /api/v1/projects/507f1f77bcf86cd799439011?include_docker_status=true{
"id": "507f1f77bcf86cd799439011",
"repo_url": "https://github.com/user/repo.git",
"branch": "main",
"spec": "重構建議",
"status": "READY",
"container_id": "abc123def456",
"created_at": "2026-02-02T12:00:00Z",
"updated_at": "2026-02-02T12:05:00Z",
"last_error": null,
"docker_status": {
"id": "abc123def456",
"name": "refactor-project-507f1f77bcf86cd799439011",
"status": "running",
"image": "refactor-base:latest"
}
}docker_status 欄位 (when include_docker_status=true):
| 欄位 | 類型 | 說明 |
|---|---|---|
| id | string | 容器 ID(短格式) |
| name | string | 容器名稱 |
| status | string | Docker 狀態(running, exited, etc.) |
| image | string | 使用的 Docker Image |
| inconsistent | boolean | 狀態不一致標記(容器在 Docker 中不存在但 DB 有記錄) |
403 Forbidden - 非專案擁有者
{
"detail": "無權限訪問此專案"
}404 Not Found - 專案不存在
{
"detail": "專案不存在"
}更新專案資訊。
認證: 🔒 需要 Bearer Token
重要限制: Provision 後(狀態 != CREATED)無法修改 repo_url
| 參數 | 類型 | 說明 |
|---|---|---|
| id | string | 專案 ID |
所有欄位皆為可選,僅更新提供的欄位。
{
"branch": "develop",
"spec": "新的任務描述"
}欄位說明:
| 欄位 | 類型 | 限制 | 說明 |
|---|---|---|---|
| repo_url | string | Git Repository URL | |
| title | string | - | 專案標題 |
| description | string | - | 專案描述 |
| branch | string | - | Git 分支名稱 |
| spec | string | - | 重構規格說明 |
| status | string | - | 專案狀態(通常由系統流程更新,不建議手動改) |
{
"id": "507f1f77bcf86cd799439011",
"repo_url": "https://github.com/user/repo.git",
"branch": "develop",
"spec": "新的任務描述",
"status": "CREATED",
"container_id": null,
"created_at": "2026-02-02T12:00:00Z",
"updated_at": "2026-02-02T12:10:00Z",
"last_error": null
}400 Bad Request - 嘗試修改已 Provision 專案的 repo_url
{
"detail": "專案已經 Provision,無法修改 Repository URL"
}Provision 專案:建立 Docker 容器並 clone repository。
認證: 🔒 需要 Bearer Token
前置條件: 專案狀態必須為 CREATED
| 參數 | 類型 | 說明 |
|---|---|---|
| id | string | 專案 ID |
| 參數 | 類型 | 必填 | 預設值 | 說明 |
|---|---|---|---|---|
| dev_mode | boolean | ❌ | null | 覆蓋全域 DEV_MODE 設定 • true: 強制啟用開發模式(掛載本機 agent) • false: 強制停用開發模式(使用 image 內建 agent) • null: 使用 .env 中的 DEV_MODE 設定 |
POST /api/v1/projects/507f1f77bcf86cd799439011/provision?dev_mode=false{
"message": "專案 provision 成功",
"project_id": "507f1f77bcf86cd799439011",
"container_id": "abc123def456",
"status": "READY"
}400 Bad Request - 專案狀態不正確
{
"detail": "專案狀態必須為 CREATED,目前為 READY"
}500 Internal Server Error - Provision 失敗
{
"detail": "Provision 失敗: Clone repository 失敗"
}說明:
- Provision 失敗時會自動清理已建立的容器
- 專案狀態會設為
FAILED,錯誤訊息記錄在last_error欄位
在專案容器中執行指令。
認證: 🔒 需要 Bearer Token
前置條件: 專案必須已 Provision(有 container_id)
| 參數 | 類型 | 說明 |
|---|---|---|
| id | string | 專案 ID |
{
"command": "ls -la /workspace/repo",
"workdir": "/workspace/repo"
}欄位說明:
| 欄位 | 類型 | 必填 | 預設值 | 說明 |
|---|---|---|---|---|
| command | string | ✅ | - | 要執行的指令 |
| workdir | string | ❌ | "/workspace/repo" | 工作目錄 |
{
"exit_code": 0,
"stdout": "total 12\ndrwxr-xr-x 3 root root 96 Feb 2 12:00 .\ndrwxr-xr-x 4 root root 128 Feb 2 12:00 ..\n-rw-r--r-- 1 root root 1234 Feb 2 12:00 README.md\n",
"stderr": ""
}欄位說明:
| 欄位 | 類型 | 說明 |
|---|---|---|
| exit_code | integer | 指令退出代碼(0 表示成功) |
| stdout | string | 標準輸出 |
| stderr | string | 標準錯誤 |
400 Bad Request - 專案尚未 Provision
{
"detail": "專案尚未 provision,請先執行 provision"
}SSE 串流容器日誌。
認證: 🔒 需要 Bearer Token
前置條件: 專案必須已 Provision
| 參數 | 類型 | 說明 |
|---|---|---|
| id | string | 專案 ID |
| 參數 | 類型 | 必填 | 預設值 | 說明 |
|---|---|---|---|---|
| follow | boolean | ❌ | true | 是否持續串流(類似 tail -f) |
| tail | integer | ❌ | 100 | 顯示最後 N 行日誌 |
GET /api/v1/projects/507f1f77bcf86cd799439011/logs/stream?follow=true&tail=100
Accept: text/event-streamContent-Type: text/event-stream
event: log
data: [2026-02-02 12:00:00] Container started
event: log
data: [2026-02-02 12:00:05] Cloning repository...
event: log
data: [2026-02-02 12:00:10] Clone complete
event: ping
data: keep-alive
SSE Event 類型:
| Event | 說明 |
|---|---|
| log | 日誌訊息 |
| ping | Keep-alive 心跳(每 30 秒) |
停止專案容器。
認證: 🔒 需要 Bearer Token
前置條件: 專案必須已 Provision
| 參數 | 類型 | 說明 |
|---|---|---|
| id | string | 專案 ID |
無需 Request Body
{
"id": "507f1f77bcf86cd799439011",
"repo_url": "https://github.com/user/repo.git",
"branch": "main",
"spec": "重構建議",
"status": "STOPPED",
"container_id": "abc123def456",
"created_at": "2026-02-02T12:00:00Z",
"updated_at": "2026-02-02T12:15:00Z",
"last_error": null
}400 Bad Request - 專案尚未 Provision
{
"detail": "專案尚未 provision,沒有容器可停止"
}刪除專案和容器。
認證: 🔒 需要 Bearer Token
| 參數 | 類型 | 說明 |
|---|---|---|
| id | string | 專案 ID |
無需 Request Body
無回應內容
說明:
- 會同時刪除 MongoDB 專案記錄和 Docker 容器
- 容器刪除失敗不會影響資料庫記錄的刪除
啟動 AI Agent 執行(異步模式)。
認證: 🔒 需要 Bearer Token
前置條件: 專案狀態必須為 READY
| 參數 | 類型 | 說明 |
|---|---|---|
| id | string | 專案 ID |
無需 Request Body
{
"run_id": "task_abc123xyz",
"project_id": "507f1f77bcf86cd799439011",
"status": "RUNNING",
"iteration_index": 0,
"phase": "plan",
"created_at": "2026-02-02T12:00:00Z",
"message": "Agent 任務已啟動,正在背景執行"
}欄位說明:
| 欄位 | 類型 | 說明 |
|---|---|---|
| run_id | string | Agent Run ID(用於後續查詢) |
| project_id | string | 專案 ID |
| status | string | 執行狀態(RUNNING, DONE, FAILED) |
| iteration_index | integer | 迭代索引(目前固定為 0) |
| phase | string | 執行階段(plan, test, exec) |
| created_at | string | 建立時間(ISO 8601) |
| message | string | 提示訊息 |
執行流程:
- Backend 驗證專案狀態
- 呼叫容器內 AI Server
POST /run - 立即返回
run_id - Agent 在容器內背景執行
400 Bad Request - 專案狀態不正確
{
"detail": "專案狀態必須為 READY,目前為 CREATED"
}503 Service Unavailable - AI Server 錯誤
{
"detail": "AI Server 錯誤: Connection refused"
}列出專案的所有 Agent Runs。
認證: 🔒 需要 Bearer Token
| 參數 | 類型 | 說明 |
|---|---|---|
| id | string | 專案 ID |
GET /api/v1/projects/507f1f77bcf86cd799439011/agent/runs{
"total": 3,
"runs": [
{
"id": "task_abc123xyz",
"project_id": "507f1f77bcf86cd799439011",
"iteration_index": 0,
"phase": "plan",
"status": "DONE",
"created_at": "2026-02-02T12:00:00Z",
"updated_at": "2026-02-02T12:05:00Z",
"finished_at": "2026-02-02T12:05:30Z",
"error_message": null
},
{
"id": "task_def456uvw",
"project_id": "507f1f77bcf86cd799439011",
"iteration_index": 0,
"phase": "plan",
"status": "FAILED",
"created_at": "2026-02-02T11:00:00Z",
"updated_at": "2026-02-02T11:02:00Z",
"finished_at": "2026-02-02T11:02:15Z",
"error_message": "Code analysis failed: timeout"
}
]
}狀態映射(AI Server → Backend):
| AI Server Status | Backend Status |
|---|---|
| pending | RUNNING |
| running | RUNNING |
| success | DONE |
| failed | FAILED |
查詢 Agent Run 詳細狀態。
認證: 🔒 需要 Bearer Token
| 參數 | 類型 | 說明 |
|---|---|---|
| id | string | 專案 ID |
| run_id | string | Agent Run ID |
GET /api/v1/projects/507f1f77bcf86cd799439011/agent/runs/task_abc123xyz{
"id": "task_abc123xyz",
"project_id": "507f1f77bcf86cd799439011",
"iteration_index": 0,
"phase": "plan",
"status": "DONE",
"created_at": "2026-02-02T12:00:00Z",
"updated_at": "2026-02-02T12:05:00Z",
"finished_at": "2026-02-02T12:05:30Z",
"error_message": null
}欄位說明:
| 欄位 | 類型 | 說明 |
|---|---|---|
| id | string | Agent Run ID |
| project_id | string | 專案 ID |
| iteration_index | integer | 迭代索引 |
| phase | string | 執行階段(plan, test, exec) |
| status | string | 執行狀態(RUNNING, DONE, FAILED) |
| created_at | string | 建立時間 |
| updated_at | string | 更新時間 |
| finished_at | string | 完成時間(null 表示尚未完成) |
| error_message | string | 錯誤訊息(僅 status=FAILED 時) |
503 Service Unavailable - 無法查詢任務狀態
{
"detail": "無法查詢任務狀態"
}SSE 串流 Agent 執行日誌(轉發容器的 stream)。
認證: 🔒 需要 Bearer Token
| 參數 | 類型 | 說明 |
|---|---|---|
| id | string | 專案 ID |
| run_id | string | Agent Run ID |
GET /api/v1/projects/507f1f77bcf86cd799439011/agent/runs/task_abc123xyz/stream
Accept: text/event-streamContent-Type: text/event-stream
event: log
data: {"level": "info", "message": "開始分析程式碼庫...", "timestamp": "2026-02-02T12:00:00Z"}
event: log
data: {"level": "info", "message": "讀取 Python 檔案: src/main.py", "timestamp": "2026-02-02T12:00:02Z"}
event: progress
data: {"step": 1, "total": 5, "message": "程式碼掃描中", "timestamp": "2026-02-02T12:00:05Z"}
event: log
data: {"level": "info", "message": "分析完成,生成重構計劃...", "timestamp": "2026-02-02T12:05:00Z"}
event: complete
data: {"status": "success", "message": "分析完成", "artifacts": ["plan.json", "plan.md"], "timestamp": "2026-02-02T12:05:30Z"}
SSE Event 類型:
| Event | 說明 | Data 格式 |
|---|---|---|
| log | 日誌訊息 | {"level": "info/warn/error", "message": "...", "timestamp": "..."} |
| progress | 進度更新 | {"step": 1, "total": 5, "message": "...", "timestamp": "..."} |
| complete | 執行完成 | {"status": "success/failed", "message": "...", "timestamp": "..."} |
| error | 錯誤訊息 | "錯誤描述" |
說明:
- 此端點轉發容器內 AI Server 的 SSE stream
- 連線會持續到 Agent 執行完成或發生錯誤
- 建議使用 EventSource API 或支援 SSE 的 HTTP Client
聊天模式會呼叫 Project Container 內的 AI Server /chat,並以 thread_id 維持多輪對話上下文。
發送聊天訊息,啟動聊天任務(背景執行)。
認證: 🔒 需要 Bearer Token
前置條件: 專案狀態必須為 READY
{
"message": "Hello! Please list the files in /workspace",
"thread_id": null,
"verbose": true,
"model": null
}| 欄位 | 類型 | 必填 | 說明 |
|---|---|---|---|
| message | string | ✅ | 使用者訊息 |
| thread_id | string | ❌ | 對話 ID(不提供則自動生成) |
| verbose | boolean | ❌ | 是否輸出較多日誌(預設 true) |
| model | string | ❌ | 模型 ID(選用) |
{
"task_id": "task_abc123xyz",
"thread_id": "chat-507f1f77bcf86cd799439011-uuid",
"project_id": "507f1f77bcf86cd799439011",
"status": "RUNNING",
"message": "聊天任務已啟動,正在背景執行"
}400 Bad Request - 專案狀態不正確
{
"detail": "專案狀態必須為 READY,目前為 CREATED"
}列出專案的聊天會話(依最後訊息時間排序)。
認證: 🔒 需要 Bearer Token
{
"total": 1,
"sessions": [
{
"thread_id": "chat-507f1f77bcf86cd799439011-uuid",
"project_id": "507f1f77bcf86cd799439011",
"title": "Hello! Please list the files in /workspace",
"created_at": "2026-02-02T12:00:00Z",
"last_message_at": "2026-02-02T12:01:00Z"
}
]
}取得聊天歷史(轉發 Project Container AI Server)。
認證: 🔒 需要 Bearer Token
{
"thread_id": "chat-507f1f77bcf86cd799439011-uuid",
"messages": [
{
"id": "msg_1",
"role": "user",
"content": "Hello",
"timestamp": "2026-02-02T12:00:00Z"
}
]
}SSE 串流聊天回應(直接轉發容器的 task stream)。
認證: 🔒 需要 Bearer Token
GET /api/v1/projects/507f1f77bcf86cd799439011/chat/task_abc123xyz/stream
Accept: text/event-streamContent-Type: text/event-stream
event: text_delta
data: {"delta": "Hello"}
# 1. 註冊用戶
curl -X POST http://localhost:8000/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "dev@example.com",
"username": "developer",
"password": "secure123"
}'
# 2. 登入取得 Token
TOKEN=$(curl -X POST http://localhost:8000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{
"username": "developer",
"password": "secure123"
}' | jq -r '.access_token')
# 3. 建立專案
PROJECT_ID=$(curl -X POST http://localhost:8000/api/v1/projects \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"project_type": "REFACTOR",
"repo_url": "https://github.com/user/legacy-code.git",
"branch": "main",
"spec": "分析專案並提出重構建議"
}' | jq -r '.id')
# 4. Provision 專案
curl -X POST http://localhost:8000/api/v1/projects/$PROJECT_ID/provision \
-H "Authorization: Bearer $TOKEN"
# 5. 啟動 Agent
RUN_ID=$(curl -X POST http://localhost:8000/api/v1/projects/$PROJECT_ID/agent/run \
-H "Authorization: Bearer $TOKEN" | jq -r '.run_id')
# 6. 串流 Agent 日誌(使用 curl)
curl -N -H "Authorization: Bearer $TOKEN" \
http://localhost:8000/api/v1/projects/$PROJECT_ID/agent/runs/$RUN_ID/stream
# 7. 查詢 Agent 執行狀態
curl -H "Authorization: Bearer $TOKEN" \
http://localhost:8000/api/v1/projects/$PROJECT_ID/agent/runs/$RUN_ID// 使用 axios + eventsource
import axios from 'axios';
import { EventSourcePolyfill } from 'event-source-polyfill';
const API_BASE = 'http://localhost:8000';
const token = 'your-access-token';
// 建立 axios instance
const api = axios.create({
baseURL: API_BASE,
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
// 建立專案
const { data: project } = await api.post('/api/v1/projects', {
project_type: 'REFACTOR',
repo_url: 'https://github.com/user/repo.git',
branch: 'main',
spec: '重構建議'
});
// Provision 專案
await api.post(`/api/v1/projects/${project.id}/provision`);
// 啟動 Agent
const { data: run } = await api.post(`/api/v1/projects/${project.id}/agent/run`);
// 串流 Agent 日誌
const eventSource = new EventSourcePolyfill(
`${API_BASE}/api/v1/projects/${project.id}/agent/runs/${run.run_id}/stream`,
{
headers: {
'Authorization': `Bearer ${token}`
}
}
);
eventSource.addEventListener('log', (event) => {
const log = JSON.parse(event.data);
console.log(`[${log.level}] ${log.message}`);
});
eventSource.addEventListener('complete', (event) => {
const result = JSON.parse(event.data);
console.log('Agent 執行完成:', result);
eventSource.close();
});
eventSource.addEventListener('error', (event) => {
console.error('SSE 錯誤:', event);
eventSource.close();
});文件版本: v1.0.0 最後更新: 2026-02-02 維護者: Backend Team