/** * Service for interacting with the Task Management API. */ const API_BASE = '/api/tasks'; // [DEF:getTasks:Function] /* @PURPOSE: Fetch a list of tasks with pagination and optional status filter. @PRE: limit and offset are numbers. @POST: Returns a promise resolving to a list of tasks. */ /** * Fetch a list of tasks with pagination and optional status filter. * @param {number} limit - Maximum number of tasks to return. * @param {number} offset - Number of tasks to skip. * @param {string|null} status - Filter by task status (optional). * @returns {Promise} List of tasks. */ export async function getTasks(limit = 10, offset = 0, status = null) { const params = new URLSearchParams({ limit: limit.toString(), offset: offset.toString() }); if (status) { params.append('status', status); } const response = await fetch(`${API_BASE}?${params.toString()}`); if (!response.ok) { throw new Error(`Failed to fetch tasks: ${response.statusText}`); } return await response.json(); } // [/DEF:getTasks:Function] // [DEF:getTask:Function] /* @PURPOSE: Fetch details for a specific task. @PRE: taskId must be provided. @POST: Returns a promise resolving to task details. */ /** * Fetch details for a specific task. * @param {string} taskId - The ID of the task. * @returns {Promise} Task details. */ export async function getTask(taskId) { const response = await fetch(`${API_BASE}/${taskId}`); if (!response.ok) { throw new Error(`Failed to fetch task ${taskId}: ${response.statusText}`); } return await response.json(); } // [/DEF:getTask:Function] // [DEF:getTaskLogs:Function] /* @PURPOSE: Fetch logs for a specific task. @PRE: taskId must be provided. @POST: Returns a promise resolving to a list of log entries. */ /** * Fetch logs for a specific task. * @param {string} taskId - The ID of the task. * @returns {Promise} List of log entries. */ export async function getTaskLogs(taskId) { // Currently, logs are included in the task object, but we might have a separate endpoint later. // For now, we fetch the task and return its logs. // Or if we implement T017 (GET /api/tasks/{task_id}/logs), we would use that. // The current backend implementation in tasks.py does NOT have a separate /logs endpoint yet. // T017 is in Phase 3. // So for now, we'll fetch the task. const task = await getTask(taskId); return task.logs || []; } // [/DEF:getTaskLogs:Function] // [DEF:resumeTask:Function] /* @PURPOSE: Resume a task that is awaiting input (e.g., passwords). @PRE: taskId and passwords must be provided. @POST: Returns a promise resolving to the updated task object. */ /** * Resume a task that is awaiting input (e.g., passwords). * @param {string} taskId - The ID of the task. * @param {Object} passwords - Map of database names to passwords. * @returns {Promise} Updated task object. */ export async function resumeTask(taskId, passwords) { const response = await fetch(`${API_BASE}/${taskId}/resume`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ passwords }) }); if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new Error(errorData.detail || `Failed to resume task: ${response.statusText}`); } return await response.json(); } // [/DEF:resumeTask:Function] // [DEF:resolveTask:Function] /* @PURPOSE: Resolve a task that is awaiting mapping. @PRE: taskId and resolutionParams must be provided. @POST: Returns a promise resolving to the updated task object. */ /** * Resolve a task that is awaiting mapping. * @param {string} taskId - The ID of the task. * @param {Object} resolutionParams - Resolution parameters. * @returns {Promise} Updated task object. */ export async function resolveTask(taskId, resolutionParams) { const response = await fetch(`${API_BASE}/${taskId}/resolve`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ resolution_params: resolutionParams }) }); if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new Error(errorData.detail || `Failed to resolve task: ${response.statusText}`); } return await response.json(); } // [/DEF:resolveTask:Function] // [DEF:clearTasks:Function] /* @PURPOSE: Clear tasks based on status. @PRE: status is a string or null. @POST: Returns a promise that resolves when tasks are cleared. */ /** * Clear tasks based on status. * @param {string|null} status - Filter by task status (optional). */ export async function clearTasks(status = null) { const params = new URLSearchParams(); if (status) { params.append('status', status); } const response = await fetch(`${API_BASE}?${params.toString()}`, { method: 'DELETE' }); if (!response.ok) { throw new Error(`Failed to clear tasks: ${response.statusText}`); } } // [/DEF:clearTasks:Function]