'use strict';

var chunkN54Q3AQR_cjs = require('./chunk-N54Q3AQR.cjs');
var chunk64ITUOXI_cjs = require('./chunk-64ITUOXI.cjs');
var zod = require('zod');

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

// src/server/handlers/background-tasks.ts
var BACKGROUND_TASK_STREAM_ROUTE = chunkN54Q3AQR_cjs.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 = chunkN54Q3AQR_cjs.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 = chunkN54Q3AQR_cjs.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 chunk64ITUOXI_cjs.HTTPException(404, { message: "Background task not found" });
    }
    const task = await bgManager.getTask(backgroundTaskId);
    if (!task) {
      throw new chunk64ITUOXI_cjs.HTTPException(404, { message: "Background task not found" });
    }
    return task;
  }
});

exports.BACKGROUND_TASK_STREAM_ROUTE = BACKGROUND_TASK_STREAM_ROUTE;
exports.GET_BACKGROUND_TASK_ROUTE = GET_BACKGROUND_TASK_ROUTE;
exports.LIST_BACKGROUND_TASKS_ROUTE = LIST_BACKGROUND_TASKS_ROUTE;
//# sourceMappingURL=chunk-UIAQYIDW.cjs.map
//# sourceMappingURL=chunk-UIAQYIDW.cjs.map