'use strict';

var chunkUY5FTPBZ_cjs = require('./chunk-UY5FTPBZ.cjs');
var chunkDIG2K5CV_cjs = require('./chunk-DIG2K5CV.cjs');
var chunkZ7LCIYK7_cjs = require('./chunk-Z7LCIYK7.cjs');
var chunkN54Q3AQR_cjs = require('./chunk-N54Q3AQR.cjs');
var chunk64ITUOXI_cjs = require('./chunk-64ITUOXI.cjs');
var agent = require('@mastra/core/agent');
var error = require('@mastra/core/error');
var features = require('@mastra/core/features');
var llm = require('@mastra/core/llm');
var requestContext = require('@mastra/core/request-context');
var zod = require('zod');

function assertDatasetsAvailable() {
  if (!features.coreFeatures.has("datasets")) {
    throw new chunk64ITUOXI_cjs.HTTPException(501, { message: "Datasets require @mastra/core >= 1.4.0" });
  }
}
function isSchemaValidationError(error) {
  return error instanceof Error && error.name === "SchemaValidationError";
}
function isSchemaUpdateValidationError(error) {
  return error instanceof Error && error.name === "SchemaUpdateValidationError";
}
function getHttpStatusForMastraError(errorId) {
  switch (errorId) {
    case "DATASET_NOT_FOUND":
    case "EXPERIMENT_NOT_FOUND":
      return 404;
    case "EXPERIMENT_NO_ITEMS":
      return 400;
    default:
      return 500;
  }
}
var LIST_DATASETS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/datasets",
  responseType: "json",
  queryParamSchema: chunkUY5FTPBZ_cjs.paginationQuerySchema,
  responseSchema: chunkUY5FTPBZ_cjs.listDatasetsResponseSchema,
  summary: "List all datasets",
  description: "Returns a paginated list of all datasets",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, ...params }) => {
    assertDatasetsAvailable();
    try {
      const { page, perPage } = params;
      const result = await mastra.datasets.list({ page: page ?? 0, perPage: perPage ?? 10 });
      return {
        datasets: result.datasets,
        pagination: result.pagination
      };
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error listing datasets");
    }
  }
});
var CREATE_DATASET_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/datasets",
  responseType: "json",
  bodySchema: chunkUY5FTPBZ_cjs.createDatasetBodySchema,
  responseSchema: chunkUY5FTPBZ_cjs.datasetResponseSchema,
  summary: "Create a new dataset",
  description: "Creates a new dataset with the specified name and optional metadata",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, ...params }) => {
    assertDatasetsAvailable();
    try {
      const {
        name,
        description,
        metadata,
        inputSchema,
        groundTruthSchema,
        requestContextSchema,
        targetType,
        targetIds,
        scorerIds
      } = params;
      const ds = await mastra.datasets.create({
        name,
        description,
        metadata,
        inputSchema,
        groundTruthSchema,
        requestContextSchema,
        targetType,
        targetIds,
        scorerIds
      });
      const details = await ds.getDetails();
      return details;
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error creating dataset");
    }
  }
});
var GET_DATASET_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/datasets/:datasetId",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetIdPathParams,
  queryParamSchema: chunkUY5FTPBZ_cjs.tenancyQuerySchema,
  responseSchema: chunkUY5FTPBZ_cjs.datasetResponseSchema.nullable(),
  summary: "Get dataset by ID",
  description: "Returns details for a specific dataset",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, ...params }) => {
    assertDatasetsAvailable();
    try {
      const { organizationId, projectId } = params;
      const ds = await mastra.datasets.get({ id: datasetId, organizationId, projectId });
      return await ds.getDetails();
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error getting dataset");
    }
  }
});
var UPDATE_DATASET_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "PATCH",
  path: "/datasets/:datasetId",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetIdPathParams,
  queryParamSchema: chunkUY5FTPBZ_cjs.tenancyQuerySchema,
  bodySchema: chunkUY5FTPBZ_cjs.updateDatasetBodySchema,
  responseSchema: chunkUY5FTPBZ_cjs.datasetResponseSchema,
  summary: "Update dataset",
  description: "Updates a dataset with the specified fields",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, ...params }) => {
    assertDatasetsAvailable();
    try {
      const {
        name,
        description,
        metadata,
        inputSchema,
        groundTruthSchema,
        requestContextSchema,
        tags,
        targetType,
        targetIds,
        scorerIds,
        organizationId,
        projectId
      } = params;
      const ds = await mastra.datasets.get({ id: datasetId, organizationId, projectId });
      const result = await ds.update({
        name,
        description,
        metadata,
        inputSchema,
        groundTruthSchema,
        requestContextSchema,
        tags,
        targetType,
        targetIds,
        scorerIds
      });
      return result;
    } catch (error$1) {
      if (isSchemaUpdateValidationError(error$1)) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, {
          message: error$1.message,
          cause: { failingItems: error$1.failingItems }
        });
      }
      if (isSchemaValidationError(error$1)) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, {
          message: error$1.message,
          cause: { field: error$1.field, errors: error$1.errors }
        });
      }
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error updating dataset");
    }
  }
});
var DELETE_DATASET_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "DELETE",
  path: "/datasets/:datasetId",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetIdPathParams,
  queryParamSchema: chunkUY5FTPBZ_cjs.tenancyQuerySchema,
  responseSchema: chunkDIG2K5CV_cjs.successResponseSchema,
  summary: "Delete dataset",
  description: "Deletes a dataset and all its items",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, ...params }) => {
    assertDatasetsAvailable();
    try {
      const { organizationId, projectId } = params;
      if (organizationId === void 0 && projectId === void 0) {
        await mastra.datasets.get({ id: datasetId });
      }
      await mastra.datasets.delete({ id: datasetId, organizationId, projectId });
      return { success: true };
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error deleting dataset");
    }
  }
});
var LIST_ITEMS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/datasets/:datasetId/items",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetIdPathParams,
  queryParamSchema: chunkUY5FTPBZ_cjs.listItemsQuerySchema,
  responseSchema: chunkUY5FTPBZ_cjs.listItemsResponseSchema,
  summary: "List dataset items",
  description: "Returns a paginated list of items in the dataset",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, ...params }) => {
    assertDatasetsAvailable();
    try {
      const { page, perPage, version, search } = params;
      const ds = await mastra.datasets.get({ id: datasetId });
      const result = await ds.listItems({
        page: page ?? 0,
        perPage: perPage ?? 10,
        version,
        search
      });
      if (Array.isArray(result)) {
        return { items: result, pagination: { total: result.length, page: 0, perPage: result.length, hasMore: false } };
      }
      return { items: result.items, pagination: result.pagination };
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error listing dataset items");
    }
  }
});
var ADD_ITEM_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/datasets/:datasetId/items",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetIdPathParams,
  bodySchema: chunkUY5FTPBZ_cjs.addItemBodySchema,
  responseSchema: chunkUY5FTPBZ_cjs.datasetItemResponseSchema,
  summary: "Add item to dataset",
  description: "Adds a new item to the dataset (auto-increments dataset version)",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, ...params }) => {
    assertDatasetsAvailable();
    try {
      const { input, groundTruth, requestContext, metadata, source, expectedTrajectory, toolMocks } = params;
      const ds = await mastra.datasets.get({ id: datasetId });
      return await ds.addItem({ input, groundTruth, requestContext, metadata, source, expectedTrajectory, toolMocks });
    } catch (error$1) {
      if (isSchemaValidationError(error$1)) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, {
          message: error$1.message,
          cause: { field: error$1.field, errors: error$1.errors }
        });
      }
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error adding item to dataset");
    }
  }
});
var GET_ITEM_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/datasets/:datasetId/items/:itemId",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetAndItemIdPathParams,
  responseSchema: chunkUY5FTPBZ_cjs.datasetItemResponseSchema.nullable(),
  summary: "Get dataset item by ID",
  description: "Returns details for a specific dataset item",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, itemId }) => {
    assertDatasetsAvailable();
    try {
      const ds = await mastra.datasets.get({ id: datasetId });
      const item = await ds.getItem({ itemId });
      if (!item || item.datasetId !== datasetId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Item not found: ${itemId}` });
      }
      return item;
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error getting dataset item");
    }
  }
});
var UPDATE_ITEM_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "PATCH",
  path: "/datasets/:datasetId/items/:itemId",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetAndItemIdPathParams,
  bodySchema: chunkUY5FTPBZ_cjs.updateItemBodySchema,
  responseSchema: chunkUY5FTPBZ_cjs.datasetItemResponseSchema,
  summary: "Update dataset item",
  description: "Updates a dataset item (auto-increments dataset version)",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, itemId, ...params }) => {
    assertDatasetsAvailable();
    try {
      const { input, groundTruth, requestContext, metadata, expectedTrajectory, toolMocks } = params;
      const ds = await mastra.datasets.get({ id: datasetId });
      const existing = await ds.getItem({ itemId });
      if (!existing || existing.datasetId !== datasetId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Item not found: ${itemId}` });
      }
      return await ds.updateItem({
        itemId,
        input,
        groundTruth,
        requestContext,
        metadata,
        expectedTrajectory,
        toolMocks
      });
    } catch (error$1) {
      if (isSchemaValidationError(error$1)) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, {
          message: error$1.message,
          cause: { field: error$1.field, errors: error$1.errors }
        });
      }
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error updating dataset item");
    }
  }
});
var DELETE_ITEM_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "DELETE",
  path: "/datasets/:datasetId/items/:itemId",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetAndItemIdPathParams,
  responseSchema: chunkDIG2K5CV_cjs.successResponseSchema,
  summary: "Delete dataset item",
  description: "Deletes a dataset item",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, itemId }) => {
    assertDatasetsAvailable();
    try {
      const ds = await mastra.datasets.get({ id: datasetId });
      const existing = await ds.getItem({ itemId });
      if (!existing || existing.datasetId !== datasetId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Item not found: ${itemId}` });
      }
      await ds.deleteItem({ itemId });
      return { success: true };
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error deleting dataset item");
    }
  }
});
var LIST_ALL_EXPERIMENTS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/experiments",
  responseType: "json",
  queryParamSchema: chunkUY5FTPBZ_cjs.paginationQuerySchema,
  responseSchema: chunkUY5FTPBZ_cjs.listExperimentsResponseSchema,
  summary: "List all experiments",
  description: "Returns a paginated list of all experiments across all datasets",
  tags: ["Experiments"],
  requiresAuth: true,
  handler: async ({ mastra, ...params }) => {
    assertDatasetsAvailable();
    try {
      const { page, perPage } = params;
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage not configured" });
      }
      const experimentsStore = await storage.getStore("experiments");
      if (!experimentsStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Experiments storage not available" });
      }
      const result = await experimentsStore.listExperiments({
        pagination: { page: page ?? 0, perPage: perPage ?? 20 }
      });
      return { experiments: result.experiments, pagination: result.pagination };
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error listing experiments");
    }
  }
});
var EXPERIMENT_REVIEW_SUMMARY_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/experiments/review-summary",
  responseType: "json",
  responseSchema: chunkUY5FTPBZ_cjs.reviewSummaryResponseSchema,
  summary: "Get review summary for all experiments",
  description: "Returns review status counts (needs-review, reviewed, complete) aggregated per experiment",
  tags: ["Experiments"],
  requiresAuth: true,
  handler: async ({ mastra }) => {
    assertDatasetsAvailable();
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage not configured" });
      }
      const experimentsStore = await storage.getStore("experiments");
      if (!experimentsStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Experiments storage not available" });
      }
      const counts = await experimentsStore.getReviewSummary();
      return { counts };
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error getting review summary");
    }
  }
});
var LIST_EXPERIMENTS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/datasets/:datasetId/experiments",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetIdPathParams,
  queryParamSchema: chunkUY5FTPBZ_cjs.paginationQuerySchema,
  responseSchema: chunkUY5FTPBZ_cjs.listExperimentsResponseSchema,
  summary: "List experiments for dataset",
  description: "Returns a paginated list of experiments for the dataset",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, ...params }) => {
    assertDatasetsAvailable();
    try {
      const { page, perPage } = params;
      const ds = await mastra.datasets.get({ id: datasetId });
      const result = await ds.listExperiments({ page: page ?? 0, perPage: perPage ?? 10 });
      return { experiments: result.experiments, pagination: result.pagination };
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error listing experiments");
    }
  }
});
var TRIGGER_EXPERIMENT_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/datasets/:datasetId/experiments",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetIdPathParams,
  bodySchema: chunkUY5FTPBZ_cjs.triggerExperimentBodySchema,
  responseSchema: chunkUY5FTPBZ_cjs.experimentSummaryResponseSchema,
  summary: "Trigger a new experiment",
  description: "Triggers a new experiment on the dataset against the specified target. Returns immediately with pending status; execution happens in background.",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, ...params }) => {
    assertDatasetsAvailable();
    try {
      const {
        targetType,
        targetId,
        scorerIds,
        version,
        agentVersion,
        maxConcurrency,
        requestContext: rawRequestContext,
        versions
      } = params;
      const requestContext$1 = rawRequestContext instanceof requestContext.RequestContext ? rawRequestContext.all : rawRequestContext;
      const ds = await mastra.datasets.get({ id: datasetId });
      const result = await ds.startExperimentAsync({
        targetType,
        targetId,
        scorers: scorerIds,
        version,
        agentVersion,
        maxConcurrency,
        requestContext: requestContext$1,
        versions
      });
      return {
        experimentId: result.experimentId,
        status: result.status,
        totalItems: result.totalItems ?? 0,
        succeededCount: 0,
        failedCount: 0,
        startedAt: /* @__PURE__ */ new Date(),
        completedAt: null,
        results: []
      };
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error triggering experiment");
    }
  }
});
var GET_EXPERIMENT_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/datasets/:datasetId/experiments/:experimentId",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetAndExperimentIdPathParams,
  responseSchema: chunkUY5FTPBZ_cjs.experimentResponseSchema.nullable(),
  summary: "Get experiment by ID",
  description: "Returns details for a specific experiment",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, experimentId }) => {
    assertDatasetsAvailable();
    try {
      const ds = await mastra.datasets.get({ id: datasetId });
      const run = await ds.getExperiment({ experimentId });
      if (!run || run.datasetId !== datasetId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Experiment not found: ${experimentId}` });
      }
      return run;
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error getting experiment");
    }
  }
});
var LIST_EXPERIMENT_RESULTS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/datasets/:datasetId/experiments/:experimentId/results",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetAndExperimentIdPathParams,
  queryParamSchema: chunkUY5FTPBZ_cjs.paginationQuerySchema,
  responseSchema: chunkUY5FTPBZ_cjs.listExperimentResultsResponseSchema,
  summary: "List experiment results",
  description: "Returns a paginated list of results for the experiment",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, experimentId, ...params }) => {
    assertDatasetsAvailable();
    try {
      const { page, perPage } = params;
      const ds = await mastra.datasets.get({ id: datasetId });
      const run = await ds.getExperiment({ experimentId });
      if (!run || run.datasetId !== datasetId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Experiment not found: ${experimentId}` });
      }
      const result = await ds.listExperimentResults({ experimentId, page: page ?? 0, perPage: perPage ?? 10 });
      return {
        results: result.results.map(({ experimentId: _eid, ...rest }) => ({ experimentId, ...rest })),
        pagination: result.pagination
      };
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error listing experiment results");
    }
  }
});
var UPDATE_EXPERIMENT_RESULT_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "PATCH",
  path: "/datasets/:datasetId/experiments/:experimentId/results/:resultId",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.experimentResultIdPathParams,
  bodySchema: chunkUY5FTPBZ_cjs.updateExperimentResultBodySchema,
  responseSchema: chunkUY5FTPBZ_cjs.experimentResultResponseSchema,
  summary: "Update an experiment result",
  description: "Updates the status and/or tags on an experiment result",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, resultId, experimentId, ...params }) => {
    assertDatasetsAvailable();
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage not configured" });
      }
      const experimentsStore = await storage.getStore("experiments");
      if (!experimentsStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Experiments storage not available" });
      }
      const result = await experimentsStore.updateExperimentResult({
        id: resultId,
        experimentId,
        status: params.status,
        tags: params.tags
      });
      return result;
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error updating experiment result");
    }
  }
});
var COMPARE_EXPERIMENTS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/datasets/:datasetId/compare",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetIdPathParams,
  bodySchema: chunkUY5FTPBZ_cjs.compareExperimentsBodySchema,
  responseSchema: chunkUY5FTPBZ_cjs.comparisonResponseSchema,
  summary: "Compare two experiments",
  description: "Compares two experiments to detect score regressions",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, ...params }) => {
    assertDatasetsAvailable();
    try {
      const { experimentIdA, experimentIdB } = params;
      await mastra.datasets.get({ id: datasetId });
      const result = await mastra.datasets.compareExperiments({
        experimentIds: [experimentIdA, experimentIdB],
        baselineId: experimentIdA
      });
      return result;
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error comparing experiments");
    }
  }
});
var LIST_DATASET_VERSIONS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/datasets/:datasetId/versions",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetIdPathParams,
  queryParamSchema: chunkUY5FTPBZ_cjs.paginationQuerySchema,
  responseSchema: chunkUY5FTPBZ_cjs.listDatasetVersionsResponseSchema,
  summary: "List dataset versions",
  description: "Returns a paginated list of all versions for the dataset",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, ...params }) => {
    assertDatasetsAvailable();
    try {
      const { page, perPage } = params;
      const ds = await mastra.datasets.get({ id: datasetId });
      const result = await ds.listVersions({ page: page ?? 0, perPage: perPage ?? 10 });
      return { versions: result.versions, pagination: result.pagination };
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error listing dataset versions");
    }
  }
});
var LIST_ITEM_VERSIONS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/datasets/:datasetId/items/:itemId/history",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetAndItemIdPathParams,
  responseSchema: chunkUY5FTPBZ_cjs.listItemVersionsResponseSchema,
  summary: "Get item history",
  description: "Returns the full SCD-2 history of the item across all dataset versions",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, itemId }) => {
    assertDatasetsAvailable();
    try {
      const ds = await mastra.datasets.get({ id: datasetId });
      const rows = await ds.getItemHistory({ itemId });
      if (rows.length > 0 && rows[0]?.datasetId !== datasetId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Item not found in dataset: ${itemId}` });
      }
      return { history: rows };
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error listing item history");
    }
  }
});
var GET_ITEM_VERSION_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/datasets/:datasetId/items/:itemId/versions/:datasetVersion",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetItemVersionPathParams,
  responseSchema: chunkUY5FTPBZ_cjs.datasetItemResponseSchema.nullable(),
  summary: "Get item at specific dataset version",
  description: "Returns the item as it existed at a specific dataset version",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, itemId, datasetVersion }) => {
    assertDatasetsAvailable();
    try {
      const ds = await mastra.datasets.get({ id: datasetId });
      const item = await ds.getItem({ itemId, version: datasetVersion });
      if (!item) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Item ${itemId} not found at version ${datasetVersion}` });
      }
      if (item.datasetId !== datasetId) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Item not found in dataset: ${itemId}` });
      }
      return item;
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error getting item version");
    }
  }
});
var BATCH_INSERT_ITEMS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/datasets/:datasetId/items/batch",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetIdPathParams,
  bodySchema: chunkUY5FTPBZ_cjs.batchInsertItemsBodySchema,
  responseSchema: chunkUY5FTPBZ_cjs.batchInsertItemsResponseSchema,
  summary: "Batch insert items to dataset",
  description: "Adds multiple items to the dataset in a single operation (single version entry)",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, ...params }) => {
    assertDatasetsAvailable();
    try {
      const { items } = params;
      const ds = await mastra.datasets.get({ id: datasetId });
      const addedItems = await ds.addItems({ items });
      return { items: addedItems, count: addedItems.length };
    } catch (error$1) {
      if (isSchemaValidationError(error$1)) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, {
          message: error$1.message,
          cause: { field: error$1.field, errors: error$1.errors }
        });
      }
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error batch inserting items");
    }
  }
});
var BATCH_DELETE_ITEMS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "DELETE",
  path: "/datasets/:datasetId/items/batch",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetIdPathParams,
  bodySchema: chunkUY5FTPBZ_cjs.batchDeleteItemsBodySchema,
  responseSchema: chunkUY5FTPBZ_cjs.batchDeleteItemsResponseSchema,
  summary: "Batch delete items from dataset",
  description: "Deletes multiple items from the dataset in a single operation (single version entry)",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, ...params }) => {
    assertDatasetsAvailable();
    try {
      const { itemIds } = params;
      const ds = await mastra.datasets.get({ id: datasetId });
      await ds.deleteItems({ itemIds });
      return { success: true, deletedCount: itemIds.length };
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error bulk deleting items");
    }
  }
});
var GENERATE_ITEMS_SYSTEM_PROMPT = `You are a test data generation expert. Your job is to generate realistic, diverse test data items for an AI agent evaluation dataset.

You will be given context about the agent being tested \u2014 its purpose, system prompt, and available tools. Use this to generate inputs that thoroughly exercise the agent's capabilities.

Generate test items that:
1. Are realistic and diverse \u2014 cover edge cases, different complexities, and various scenarios
2. Match the provided schemas exactly
3. Include ground truth values when a ground truth schema is provided
4. Vary in difficulty (easy, medium, hard cases)
5. Include potential edge cases and tricky inputs
6. Test different aspects of the agent's capabilities based on its tools and instructions

Return the items as a JSON array.`;
var GENERATE_ITEMS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/datasets/:datasetId/generate-items",
  responseType: "json",
  pathParamSchema: chunkUY5FTPBZ_cjs.datasetIdPathParams,
  bodySchema: chunkUY5FTPBZ_cjs.generateItemsBodySchema,
  responseSchema: chunkUY5FTPBZ_cjs.generateItemsResponseSchema,
  summary: "Generate dataset items using AI",
  description: "Uses an LLM to generate synthetic dataset items based on the dataset schema and a user prompt. Returns generated items for review \u2014 they are NOT automatically added to the dataset.",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, datasetId, modelId, prompt, count, agentContext }) => {
    assertDatasetsAvailable();
    try {
      const ds = await mastra.datasets.get({ id: datasetId });
      const dataset = await ds.getDetails();
      const model = await llm.resolveModelConfig(modelId, void 0, mastra);
      const schemaContext = [
        dataset.inputSchema ? `Input schema:
${JSON.stringify(dataset.inputSchema, null, 2)}` : null,
        dataset.groundTruthSchema ? `Ground truth schema:
${JSON.stringify(dataset.groundTruthSchema, null, 2)}` : null
      ].filter(Boolean).join("\n\n");
      const generatorAgent = new agent.Agent({
        id: "dataset-item-generator",
        name: "dataset-item-generator",
        instructions: GENERATE_ITEMS_SYSTEM_PROMPT,
        model
      });
      const itemSchema = zod.z.object({
        input: zod.z.string().describe("The input data as a JSON string matching the input schema, or a plain text string if no schema"),
        groundTruth: zod.z.string().optional().describe("The expected output as a JSON string matching the ground truth schema")
      });
      const outputSchema = zod.z.object({
        items: zod.z.array(itemSchema).min(1).max(count)
      });
      const agentContextParts = [];
      if (agentContext?.description) {
        agentContextParts.push(`Agent description: ${agentContext.description}`);
      }
      if (agentContext?.instructions) {
        agentContextParts.push(`Agent system prompt:
${agentContext.instructions}`);
      }
      if (agentContext?.tools?.length) {
        agentContextParts.push(`Agent tools: ${agentContext.tools.join(", ")}`);
      }
      const agentContextSection = agentContextParts.length > 0 ? agentContextParts.join("\n\n") : null;
      const userMessage = [
        `Generate exactly ${count} test items for a dataset named "${dataset.name}".`,
        dataset.description ? `Dataset description: ${dataset.description}` : null,
        agentContextSection ? `--- AGENT CONTEXT ---
${agentContextSection}` : null,
        schemaContext || null,
        `User's request: ${prompt}`,
        `Return exactly ${count} items.`
      ].filter(Boolean).join("\n\n");
      const result = await generatorAgent.generate(userMessage, {
        structuredOutput: { schema: outputSchema }
      });
      const generated = await result.object;
      const items = generated.items.map((item) => {
        let input = item.input;
        try {
          input = JSON.parse(item.input);
        } catch {
        }
        let groundTruth = item.groundTruth;
        if (item.groundTruth) {
          try {
            groundTruth = JSON.parse(item.groundTruth);
          } catch {
          }
        }
        return { input, groundTruth };
      });
      return { items };
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error generating dataset items");
    }
  }
});
var CLUSTER_FAILURES_SYSTEM_PROMPT = `You are an AI evaluation expert specializing in failure analysis. Given a set of failure items from an AI agent experiment, identify common failure patterns and assign descriptive tags to each item.

For each cluster you identify, provide:
- A short, descriptive tag label (2-5 words, lowercase, hyphenated, e.g., "no-tool-usage", "hallucination")
- A description explaining the common failure pattern
- The IDs of items that belong to this cluster

Also return a "proposedTags" array mapping each item ID to the tags you recommend, along with a brief "reason" explaining WHY those tags apply to that specific item. The reason should reference concrete evidence from the item's input/output/error.

Guidelines:
- Create between 1 and 8 clusters depending on the diversity of failures
- Every item must be assigned to at least one cluster unless there is no clear pattern of failure
- Focus on the root cause of failures, not surface-level symptoms
- If items have scores, use low scores as signals for the failure type
- Be specific about what went wrong
- IMPORTANT: If existing tags are provided, PREFER reusing them over creating new ones. Only create new tags when no existing tag fits.
- Items may already have tags \u2014 consider those when assigning new ones and avoid duplicating existing tags on an item.
- The "reason" field should be 1-2 sentences explaining the specific evidence for each tag assignment.`;
var CLUSTER_FAILURES_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/datasets/cluster-failures",
  responseType: "json",
  bodySchema: chunkUY5FTPBZ_cjs.clusterFailuresBodySchema,
  responseSchema: chunkUY5FTPBZ_cjs.clusterFailuresResponseSchema,
  summary: "Cluster experiment failures using AI",
  description: "Uses an LLM to analyze failure items from an experiment and group them into meaningful failure pattern clusters.",
  tags: ["Datasets"],
  requiresAuth: true,
  handler: async ({ mastra, modelId, items, availableTags, prompt }) => {
    assertDatasetsAvailable();
    try {
      const model = await llm.resolveModelConfig(modelId, void 0, mastra);
      const clusterAgent = new agent.Agent({
        id: "failure-cluster-analyzer",
        name: "failure-cluster-analyzer",
        instructions: CLUSTER_FAILURES_SYSTEM_PROMPT,
        model
      });
      const outputSchema = zod.z.object({
        clusters: zod.z.array(
          zod.z.object({
            id: zod.z.string(),
            label: zod.z.string(),
            description: zod.z.string(),
            itemIds: zod.z.array(zod.z.string())
          })
        ),
        proposedTags: zod.z.array(
          zod.z.object({
            itemId: zod.z.string(),
            tags: zod.z.array(zod.z.string()),
            reason: zod.z.string().describe("Brief explanation of why these tags were assigned")
          })
        )
      });
      const itemSummaries = items.map((item, i) => {
        const parts = [`Item ${i + 1} (id: ${item.id}):`];
        if (item.input !== void 0 && item.input !== null) parts.push(`  Input: ${JSON.stringify(item.input)}`);
        if (item.output !== void 0 && item.output !== null) parts.push(`  Output: ${JSON.stringify(item.output)}`);
        if (item.error !== void 0 && item.error !== null) {
          parts.push(`  Error: ${typeof item.error === "string" ? item.error : JSON.stringify(item.error)}`);
        }
        if (item.scores !== void 0 && item.scores !== null) {
          parts.push(`  Scores: ${JSON.stringify(item.scores)}`);
        }
        if (item.existingTags && item.existingTags.length > 0) {
          parts.push(`  Existing tags: ${item.existingTags.join(", ")}`);
        }
        return parts.join("\n");
      });
      let userMessage = `Analyze these ${items.length} failure items and group them into clusters of common failure patterns:

${itemSummaries.join("\n\n")}`;
      if (availableTags && availableTags.length > 0) {
        userMessage += `

Existing tag vocabulary (prefer reusing these): ${availableTags.join(", ")}`;
      }
      if (prompt) {
        userMessage += `

Additional instructions from the reviewer: ${prompt}`;
      }
      userMessage += `

Return both "clusters" (grouping items by pattern) and "proposedTags" (a list mapping each item ID to the tag labels you recommend, with a "reason" explaining why). For proposedTags, only include NEW tags to add \u2014 do not repeat tags the item already has.`;
      const result = await clusterAgent.generate(userMessage, {
        structuredOutput: { schema: outputSchema }
      });
      const generated = await result.object;
      return { clusters: generated.clusters, proposedTags: generated.proposedTags ?? [] };
    } catch (error$1) {
      if (error$1 instanceof error.MastraError) {
        throw new chunk64ITUOXI_cjs.HTTPException(getHttpStatusForMastraError(error$1.id), { message: error$1.message });
      }
      return chunkZ7LCIYK7_cjs.handleError(error$1, "Error clustering failures");
    }
  }
});

exports.ADD_ITEM_ROUTE = ADD_ITEM_ROUTE;
exports.BATCH_DELETE_ITEMS_ROUTE = BATCH_DELETE_ITEMS_ROUTE;
exports.BATCH_INSERT_ITEMS_ROUTE = BATCH_INSERT_ITEMS_ROUTE;
exports.CLUSTER_FAILURES_ROUTE = CLUSTER_FAILURES_ROUTE;
exports.COMPARE_EXPERIMENTS_ROUTE = COMPARE_EXPERIMENTS_ROUTE;
exports.CREATE_DATASET_ROUTE = CREATE_DATASET_ROUTE;
exports.DELETE_DATASET_ROUTE = DELETE_DATASET_ROUTE;
exports.DELETE_ITEM_ROUTE = DELETE_ITEM_ROUTE;
exports.EXPERIMENT_REVIEW_SUMMARY_ROUTE = EXPERIMENT_REVIEW_SUMMARY_ROUTE;
exports.GENERATE_ITEMS_ROUTE = GENERATE_ITEMS_ROUTE;
exports.GET_DATASET_ROUTE = GET_DATASET_ROUTE;
exports.GET_EXPERIMENT_ROUTE = GET_EXPERIMENT_ROUTE;
exports.GET_ITEM_ROUTE = GET_ITEM_ROUTE;
exports.GET_ITEM_VERSION_ROUTE = GET_ITEM_VERSION_ROUTE;
exports.LIST_ALL_EXPERIMENTS_ROUTE = LIST_ALL_EXPERIMENTS_ROUTE;
exports.LIST_DATASETS_ROUTE = LIST_DATASETS_ROUTE;
exports.LIST_DATASET_VERSIONS_ROUTE = LIST_DATASET_VERSIONS_ROUTE;
exports.LIST_EXPERIMENTS_ROUTE = LIST_EXPERIMENTS_ROUTE;
exports.LIST_EXPERIMENT_RESULTS_ROUTE = LIST_EXPERIMENT_RESULTS_ROUTE;
exports.LIST_ITEMS_ROUTE = LIST_ITEMS_ROUTE;
exports.LIST_ITEM_VERSIONS_ROUTE = LIST_ITEM_VERSIONS_ROUTE;
exports.TRIGGER_EXPERIMENT_ROUTE = TRIGGER_EXPERIMENT_ROUTE;
exports.UPDATE_DATASET_ROUTE = UPDATE_DATASET_ROUTE;
exports.UPDATE_EXPERIMENT_RESULT_ROUTE = UPDATE_EXPERIMENT_RESULT_ROUTE;
exports.UPDATE_ITEM_ROUTE = UPDATE_ITEM_ROUTE;
//# sourceMappingURL=chunk-VCCMOFQR.cjs.map
//# sourceMappingURL=chunk-VCCMOFQR.cjs.map