agenthub/src/server/routes.ts

70 lines
2.8 KiB
TypeScript

import { FastifyInstance } from 'fastify';
import { createTask, listTasks, getTask, claimTask, doneTask } from '../core/services/taskService.js';
import { createHandoff, listHandoffs, getHandoff } from '../core/services/handoffService.js';
import { createDecision, listDecisions } from '../core/services/decisionService.js';
import { addMemory, searchMemory, listMemory } from '../core/services/memoryService.js';
import { getStatus, updateStatus } from '../core/services/statusService.js';
import { autoDelegate, suggestDelegation } from '../core/services/delegateService.js';
import type { Task, Handoff, Decision, Memory } from '../core/schema.js';
export async function registerRoutes(app: FastifyInstance, cwd: string): Promise<void> {
app.get('/status', async () => ({ body: getStatus(cwd) }));
app.post('/status/update', async () => ({ body: updateStatus(cwd) }));
app.get('/tasks', async (request) => {
const { status, role } = request.query as { status?: string; role?: string };
return listTasks(cwd, { status, role });
});
app.post('/tasks', async (request) => {
return createTask(cwd, request.body as Partial<Task>);
});
app.get('/tasks/:id', async (request) => {
const { id } = request.params as { id: string };
const { task, body } = getTask(cwd, id);
return { task, body };
});
app.patch('/tasks/:id', async (request) => {
const { id } = request.params as { id: string };
const patch = request.body as Partial<Task>;
if (patch.status === 'in_progress' && patch.assignedTo) {
return claimTask(cwd, id, patch.assignedTo);
}
if (patch.status === 'done') {
return doneTask(cwd, id);
}
return { error: 'Unsupported patch' };
});
app.get('/handoffs', async () => listHandoffs(cwd));
app.post('/handoffs', async (request) => createHandoff(cwd, request.body as Partial<Handoff>));
app.get('/handoffs/:id', async (request) => {
const { id } = request.params as { id: string };
const { handoff, body } = getHandoff(cwd, id);
return { handoff, body };
});
app.get('/decisions', async () => listDecisions(cwd));
app.post('/decisions', async (request) => createDecision(cwd, request.body as Partial<Decision>));
app.get('/memory', async () => listMemory(cwd));
app.post('/memory', async (request) => addMemory(cwd, request.body as Partial<Memory>));
app.get('/memory/search', async (request) => {
const { q } = request.query as { q: string };
return searchMemory(cwd, q ?? '');
});
app.post('/delegate', async (request) => {
const { auto } = request.query as { auto?: string };
const suggestion = suggestDelegation(cwd);
if (!suggestion) return { suggestion: null };
if (auto === 'true') {
const handoff = autoDelegate(cwd);
return { suggestion, handoff };
}
return { suggestion };
});
}