import { createRoute } from './chunk-PIKPS6Q6.js';
import { HTTPException } from './chunk-6QWQZI4Q.js';
import { z } from 'zod';

var backgroundTaskStatusSchema = z.enum([
  "pending",
  "running",
  "suspended",
  "completed",
  "failed",
  "cancelled",
  "timed_out"
]);
var backgroundTaskStreamQuerySchema = z.object({
  agentId: z.string().optional(),
  runId: z.string().optional(),
  threadId: z.string().optional(),
  resourceId: z.string().optional(),
  taskId: z.string().optional()
});
var backgroundTaskDateColumnSchema = z.enum(["createdAt", "startedAt", "suspendedAt", "completedAt"]);
var listBackgroundTasksQuerySchema = z.object({
  agentId: z.string().optional(),
  status: backgroundTaskStatusSchema.optional(),
  runId: z.string().optional(),
  threadId: z.string().optional(),
  resourceId: z.string().optional(),
  toolName: z.string().optional(),
  toolCallId: z.string().optional(),
  fromDate: z.coerce.date().optional(),
  toDate: z.coerce.date().optional(),
  dateFilterBy: backgroundTaskDateColumnSchema.optional(),
  orderBy: backgroundTaskDateColumnSchema.optional(),
  orderDirection: z.enum(["asc", "desc"]).optional(),
  page: z.coerce.number().optional(),
  perPage: z.coerce.number().optional()
});
var backgroundTaskIdPathParams = z.object({
  backgroundTaskId: z.string()
});
var backgroundTaskResponseSchema = z.object({
  id: z.string(),
  status: backgroundTaskStatusSchema,
  toolName: z.string(),
  toolCallId: z.string(),
  args: z.record(z.string(), z.unknown()),
  agentId: z.string(),
  threadId: z.string().optional(),
  resourceId: z.string().optional(),
  runId: z.string(),
  result: z.unknown().optional(),
  error: z.object({ message: z.string(), stack: z.string().optional() }).optional(),
  createdAt: z.date(),
  startedAt: z.date().optional(),
  completedAt: z.date().optional(),
  retryCount: z.number(),
  maxRetries: z.number(),
  timeoutMs: z.number(),
  suspendPayload: z.unknown().optional()
});
var listBackgroundTaskResponseSchema = z.object({
  tasks: z.array(backgroundTaskResponseSchema),
  total: z.number()
});
var backgroundTaskStreamResponseSchema = z.any();

// src/server/handlers/background-tasks.ts
var BACKGROUND_TASK_STREAM_ROUTE = createRoute({
  method: "GET",
  path: "/background-tasks/stream",
  responseType: "stream",
  streamFormat: "sse",
  queryParamSchema: backgroundTaskStreamQuerySchema,
  responseSchema: backgroundTaskStreamResponseSchema,
  summary: "Stream background task events via SSE",
  description: "Real-time Server-Sent Events stream of background task completion/failure events.",
  tags: ["Background Tasks"],
  requiresAuth: true,
  handler: async ({ mastra, agentId, runId, threadId, resourceId, taskId, abortSignal }) => {
    const bgManager = mastra.backgroundTaskManager;
    if (!bgManager) {
      return new ReadableStream({
        start(controller) {
          abortSignal?.addEventListener("abort", () => {
            try {
              controller.close();
            } catch {
            }
          });
        }
      });
    }
    return bgManager.stream({ agentId, runId, threadId, resourceId, taskId, abortSignal });
  }
});
var LIST_BACKGROUND_TASKS_ROUTE = createRoute({
  method: "GET",
  path: "/background-tasks",
  responseType: "json",
  queryParamSchema: listBackgroundTasksQuerySchema,
  responseSchema: listBackgroundTaskResponseSchema,
  summary: "List background tasks",
  description: "Returns background tasks filtered by status, agent, run, etc.",
  tags: ["Background Tasks"],
  requiresAuth: true,
  handler: async ({ mastra, ...params }) => {
    const bgManager = mastra.backgroundTaskManager;
    if (!bgManager) {
      return { tasks: [], total: 0 };
    }
    return bgManager.listTasks(params);
  }
});
var GET_BACKGROUND_TASK_ROUTE = createRoute({
  method: "GET",
  path: "/background-tasks/:backgroundTaskId",
  responseType: "json",
  pathParamSchema: backgroundTaskIdPathParams,
  responseSchema: backgroundTaskResponseSchema,
  summary: "Get a background task by ID",
  description: "Returns a background task by ID.",
  tags: ["Background Tasks"],
  requiresAuth: true,
  handler: async ({ mastra, backgroundTaskId }) => {
    const bgManager = mastra.backgroundTaskManager;
    if (!bgManager) {
      throw new HTTPException(404, { message: "Background task not found" });
    }
    const task = await bgManager.getTask(backgroundTaskId);
    if (!task) {
      throw new HTTPException(404, { message: "Background task not found" });
    }
    return task;
  }
});

export { BACKGROUND_TASK_STREAM_ROUTE, GET_BACKGROUND_TASK_ROUTE, LIST_BACKGROUND_TASKS_ROUTE };
//# sourceMappingURL=chunk-S2JGT2GI.js.map
//# sourceMappingURL=chunk-S2JGT2GI.js.map