This document describes all Next.js API routes in the Code-Automata application.
Routes for managing AI agent lifecycle and execution.
Starts an AI agent on a task.
Request Body:
{
"taskId": "task-1234567890-abc12",
"prompt": "Implement the feature according to the plan"
}Response (200):
{
"success": true,
"threadId": "thread-uuid-here",
"message": "Agent started successfully"
}Error Responses:
400- MissingtaskIdorprompt404- Task not found409- Task already has an agent running500- Server error
Starts an AI agent to plan a task. Behavior depends on requiresHumanReview flag:
- If
true: Generates clarifying questions first - If
false: Generates plan directly and auto-starts development
Request Body:
{
"taskId": "task-1234567890-abc12"
}Response (200):
{
"success": true,
"threadId": "thread-uuid-here",
"planningStatus": "generating_questions" | "generating_plan",
"message": "Planning agent started successfully"
}Error Responses:
400- MissingtaskId404- Task not found409- Task already has an agent running500- Server error (includes retry count on failure)
Starts the development phase after plan approval. Task remains in planning until subtasks (dev + qa) are generated and validated. Generates subtasks from the approved plan, then executes them sequentially. Uses fix-agent retry (up to 2 attempts) on subtask JSON parse/validation failure.
Request Body:
{
"taskId": "task-1234567890-abc12"
}Response (200):
{
"success": true,
"threadId": "thread-uuid-here",
"message": "Generating subtasks - task remains in planning until subtasks are ready"
}Error Responses:
400- MissingtaskIdor task doesn't have approved plan404- Task not found500- Server error
Starts the AI review phase. Executes QA subtasks sequentially.
Request Body:
{
"taskId": "task-1234567890-abc12"
}Response (200):
{
"success": true,
"message": "AI Review started - executing QA subtasks"
}Error Responses:
400- MissingtaskId, task not inai_reviewphase, or no QA subtasks404- Task not found500- Server error
Stops a running AI agent.
Request Body:
{
"threadId": "thread-uuid-here"
}Response (200):
{
"success": true,
"message": "Agent stopped successfully"
}Error Responses:
400- MissingthreadId404- Agent not found500- Server error
Server-Sent Events (SSE) endpoint for streaming agent logs in real-time.
Query Parameters:
threadId(required) - The agent thread ID
SSE Message Types:
// Connection established
{ "type": "connected" }
// Log message
{ "type": "log", "content": "...", "timestamp": 1234567890 }
// Status update (sent when agent completes/errors/stops)
{ "type": "status", "status": "completed" | "error" | "stopped", "error": "..." }Error Responses:
400- MissingthreadId404- Agent not found
Approves a plan and optionally starts development.
Request Body:
{
"taskId": "task-1234567890-abc12",
"startDevelopment": true // optional, default: false
}Response (200):
{
"success": true,
"message": "Plan approved and development started" | "Plan approved",
"task": { /* updated task object */ }
}Error Responses:
400- MissingtaskId404- Task not found500- Server error
Modifies an existing plan via inline edit or AI feedback regeneration.
Request Body (Inline Edit):
{
"taskId": "task-1234567890-abc12",
"method": "inline",
"newPlan": "# Updated Implementation Plan\n..."
}Request Body (Feedback Regeneration):
{
"taskId": "task-1234567890-abc12",
"method": "feedback",
"feedback": "Please add more error handling and consider edge cases"
}Response (200 - Inline):
{
"success": true,
"message": "Plan updated successfully"
}Response (200 - Feedback):
{
"success": true,
"message": "Plan regeneration started",
"threadId": "thread-uuid-here"
}Error Responses:
400- Missing required fields or invalid method404- Task not found500- Server error
Submits answers to planning questions and triggers plan generation. Uses fix-agent retry (up to 2 attempts) on plan JSON parse failure.
Request Body:
{
"taskId": "task-1234567890-abc12",
"answers": {
"q1": {
"selectedOption": "JWT tokens",
"additionalText": "Prefer short-lived tokens"
},
"q2": {
"selectedOption": "Combination approach",
"additionalText": ""
}
}
}Response (200):
{
"success": true,
"message": "Answers submitted, plan generation started",
"threadId": "thread-uuid-here"
}Error Responses:
400- MissingtaskIdoranswers404- Task not found500- Server error
Re-parses the plan from planning logs for tasks blocked during plan generation (JSON parse failure). Resumes the task if extraction succeeds.
Request Body:
{
"taskId": "task-1234567890-abc12"
}Response (200):
{
"success": true,
"message": "Plan extracted successfully. Task resumed."
}Error Responses:
400- MissingtaskId, task not blocked, orplanningStatusnotgenerating_plan404- Task not found or planning logs not found500- Server error
Routes for CLI adapter management.
Returns available CLI adapters and their configuration schemas.
Response (200):
[
{
"name": "amp",
"displayName": "Amp CLI",
"configSchema": {
"apiKey": { "type": "string", "required": true },
"model": { "type": "string", "default": "claude-3-5-sonnet" }
}
},
{
"name": "mock",
"displayName": "Mock Adapter",
"configSchema": {
/* ... */
}
}
]Error Responses:
500- Failed to load CLI adapters
Routes for git operations and worktree management.
Check git status for a task's worktree.
Query Parameters:
taskId(required) - The task ID
Response (200):
{
"hasChanges": true,
"status": "Changes pending" | "Clean" | "No worktree",
"clean": false,
"exists": true
}Error Responses:
400- MissingtaskId404- Task not found500- Server error
Manages git worktrees (create, delete, cleanup).
Request Body (Create):
{
"action": "create",
"taskId": "task-1234567890-abc12"
}Response (200 - Create):
{
"success": true,
"worktreeInfo": {
"path": "/path/to/.code-automata/worktrees/task-xxx",
"branchName": "code-automata/task-xxx"
},
"message": "Worktree created at /path/to/..."
}Request Body (Delete):
{
"action": "delete",
"taskId": "task-1234567890-abc12",
"force": false // optional
}Response (200 - Delete):
{
"success": true,
"message": "Worktree deleted for task task-xxx"
}Request Body (Cleanup All):
{
"action": "cleanup-all",
"force": false // optional
}Response (200 - Cleanup):
{
"success": true,
"message": "All worktrees cleaned up"
}Error Responses:
400- MissingtaskIdor unknown action500- Server error503- Git not available or not in a git repository
Query worktree status and information.
Query Parameters:
taskId- Required forstatusandinfoactionsaction- One of:status(default),list,info
Response (200 - Status):
{
"status": {
"exists": true,
"hasChanges": false
}
}Response (200 - List):
{
"worktrees": [
{ "path": "/path/to/worktree1", "branch": "code-automata/task-1" },
{ "path": "/path/to/worktree2", "branch": "code-automata/task-2" }
],
"count": 2
}Response (200 - Info):
{
"taskId": "task-xxx",
"mainRepo": "/path/to/main/repo",
"mainBranch": "main",
"worktreePath": "/path/to/.code-automata/worktrees/task-xxx",
"branchName": "code-automata/task-xxx"
}Error Responses:
400- Missing required parameters or unknown action500- Server error503- Git not available
Routes for task CRUD operations and subtask management.
Creates a new task with automatic git worktree setup.
Request Body:
{
"title": "Implement authentication",
"description": "Add JWT-based authentication system",
"phase": "planning", // optional, default: "planning"
"status": "pending", // optional, default: "pending"
"cliTool": "amp", // optional
"cliConfig": {}, // optional
"requiresHumanReview": true, // optional, default: false
"metadata": {} // optional
}Response (200):
{
"id": "task-1234567890-abc12",
"title": "Implement authentication",
"description": "Add JWT-based authentication system",
"phase": "planning",
"status": "pending",
"subtasks": [],
"worktreePath": "/path/to/.code-automata/worktrees/task-xxx",
"branchName": "code-automata/task-xxx",
"createdAt": 1234567890,
"updatedAt": 1234567890
/* ... other fields */
}Error Responses:
500- Server error
Lists all tasks.
Response (200):
[
{
"id": "task-xxx",
"title": "Task 1",
"phase": "planning"
/* ... */
},
{
"id": "task-yyy",
"title": "Task 2",
"phase": "in_progress"
/* ... */
}
]Error Responses:
500- Server error
Updates a task's properties.
Request Body:
{
"taskId": "task-1234567890-abc12",
"phase": "in_progress",
"status": "in_progress",
"title": "Updated title"
// Any task fields can be updated
}Response (200):
{
"id": "task-xxx",
"title": "Updated title",
"phase": "in_progress",
"updatedAt": 1234567890
/* ... updated task object */
}Error Responses:
400- MissingtaskId404- Task not found500- Server error
Reorders subtasks within a task.
Request Body:
{
"taskId": "task-1234567890-abc12",
"subtaskIds": ["subtask-3", "subtask-1", "subtask-2"]
}Response (200):
{
"success": true
}Error Responses:
400- Missing/invalidtaskIdorsubtaskIds, or IDs don't match existing subtasks404- Task not found500- Server error
Marks a subtask as completed (skips it). Stops any running agent on the subtask.
Request Body:
{
"taskId": "task-1234567890-abc12",
"subtaskId": "subtask-1"
}Response (200):
{
"success": true,
"message": "Subtask skipped successfully"
}Notes:
- Automatically transitions task to
ai_reviewphase if all dev subtasks are completed
Error Responses:
400- Missing required fields or subtask already completed404- Task or subtask not found500- Server error
Removes a subtask from the task. Stops any running agent on the subtask.
Request Body:
{
"taskId": "task-1234567890-abc12",
"subtaskId": "subtask-1"
}Response (200):
{
"success": true,
"message": "Subtask deleted successfully"
}Notes:
- Cannot delete completed subtasks
- Automatically transitions task to
ai_reviewphase if all remaining dev subtasks are completed
Error Responses:
400- Missing required fields or subtask already completed404- Task or subtask not found500- Server error
Creates sample tasks for testing the Kanban board.
Request Body: None required
Response (200):
{
"success": true,
"message": "Created 6 sample tasks",
"tasks": [
/* array of created tasks */
]
}Error Responses:
500- Server error
Creates sample tasks for E2E testing (marked with isTestData flag in metadata).
Request Body: None required
Response (200):
{
"success": true,
"message": "Created 6 test tasks",
"tasks": [
/* array of created tasks with isTestData: true */
]
}Error Responses:
500- Server error
Routes for testing and development.
Tests the Mock CLI adapter without using any API credits.
Response (200):
{
"success": true,
"adapter": "mock",
"capabilities": {
"streaming": true,
"tools": true
},
"messages": [
/* array of mock messages */
],
"totalMessages": 5
}Error Responses:
500- Server error with details
All API routes follow a consistent error response format:
{
"error": "Error message describing what went wrong"
}| Code | Meaning |
|---|---|
200 |
Success |
400 |
Bad Request - Missing or invalid parameters |
404 |
Not Found - Resource doesn't exist |
409 |
Conflict - Resource state conflict (e.g., agent already running) |
500 |
Internal Server Error |
503 |
Service Unavailable - External dependency not available (e.g., Git) |
All routes use try-catch blocks with consistent error extraction:
try {
// Route logic
} catch (error) {
return NextResponse.json(
{
error: error instanceof Error ? error.message : 'Unknown error',
},
{ status: 500 }
);
}Currently, the API routes do not implement authentication. All endpoints are accessible without authorization.
Future Considerations:
- Add API key authentication for external access
- Implement session-based auth for web UI
- Add rate limiting for agent operations
Tasks move through the following phases:
| Phase | Description |
|---|---|
planning |
Task is being planned (questions/plan generation) |
in_progress |
Development subtasks being executed |
ai_review |
QA subtasks being executed |
human_review |
Awaiting human review |
done |
Task completed |
| Status | Description |
|---|---|
not_started |
Planning not yet initiated |
generating_questions |
AI generating clarifying questions |
waiting_for_answers |
Waiting for user to answer questions |
generating_plan |
AI generating implementation plan |
plan_ready |
Plan generated, awaiting approval |
plan_approved |
Plan approved, ready for development |
| Status | Description |
|---|---|
pending |
Waiting for action |
planning |
Currently in planning phase |
in_progress |
Actively being worked on |
blocked |
Blocked due to error |
completed |
Successfully completed |
interface Subtask {
id: string; // Unique identifier
content: string; // Detailed description
label: string; // Short label for UI
type: 'dev' | 'qa'; // Development or QA verification
status: 'pending' | 'in_progress' | 'completed';
activeForm?: string; // Present continuous form for progress display
completedAt?: number; // Timestamp when completed
}