Spaces:
Sleeping
Sleeping
| import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; | |
| import * as taskService from '../services/task.service.js'; | |
| interface TaskParams { | |
| id: string; | |
| } | |
| interface ListQuery { | |
| score_id?: string; | |
| status?: taskService.TaskStatus; | |
| } | |
| export default async function tasksRoutes(fastify: FastifyInstance) { | |
| // List tasks | |
| fastify.get<{ Querystring: ListQuery }>('/tasks', async (request, reply) => { | |
| const { score_id, status } = request.query; | |
| const tasks = await taskService.listTasks(score_id, status); | |
| return { tasks }; | |
| }); | |
| // Get task status and progress | |
| fastify.get<{ Params: TaskParams }>('/tasks/:id', async (request, reply) => { | |
| const task = await taskService.getTask(request.params.id); | |
| if (!task) { | |
| reply.code(404); | |
| return { error: 'Task not found' }; | |
| } | |
| return { task }; | |
| }); | |
| // Poll task (same as get, but intended for polling) | |
| fastify.get<{ Params: TaskParams }>('/tasks/:id/poll', async (request, reply) => { | |
| const task = await taskService.getTask(request.params.id); | |
| if (!task) { | |
| reply.code(404); | |
| return { error: 'Task not found' }; | |
| } | |
| // Return a simplified response for polling | |
| return { | |
| id: task.id, | |
| status: task.status, | |
| progress: task.progress, | |
| current_step: task.current_step, | |
| completed: task.status === 'completed' || task.status === 'failed', | |
| result: task.status === 'completed' ? task.result : null, | |
| error: task.status === 'failed' ? task.error : null, | |
| }; | |
| }); | |
| } | |