'use strict';

var chunkJ5IJWZEA_cjs = require('./chunk-J5IJWZEA.cjs');
var chunkRWLJZWDL_cjs = require('./chunk-RWLJZWDL.cjs');
var chunkSUKFO7UM_cjs = require('./chunk-SUKFO7UM.cjs');
var chunk4LVJBXI2_cjs = require('./chunk-4LVJBXI2.cjs');
var chunk64ITUOXI_cjs = require('./chunk-64ITUOXI.cjs');
var z = require('zod');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var z__default = /*#__PURE__*/_interopDefault(z);

var storedWorkspaceIdPathParams = z__default.default.object({
  storedWorkspaceId: z__default.default.string().describe("Unique identifier for the stored workspace")
});
var storageOrderBySchema = z__default.default.object({
  field: z__default.default.enum(["createdAt", "updatedAt"]).optional(),
  direction: z__default.default.enum(["ASC", "DESC"]).optional()
});
var listStoredWorkspacesQuerySchema = chunkRWLJZWDL_cjs.createPagePaginationSchema(100).extend({
  orderBy: storageOrderBySchema.optional(),
  authorId: z__default.default.string().optional().describe("Filter workspaces by author identifier"),
  metadata: z__default.default.record(z__default.default.string(), z__default.default.unknown()).optional().describe("Filter workspaces by metadata key-value pairs")
});
var filesystemConfigSchema = z__default.default.object({
  provider: z__default.default.string().describe("Filesystem provider name"),
  config: z__default.default.record(z__default.default.string(), z__default.default.unknown()).describe("Filesystem provider configuration")
});
var sandboxConfigSchema = z__default.default.object({
  provider: z__default.default.string().describe("Sandbox provider name"),
  config: z__default.default.record(z__default.default.string(), z__default.default.unknown()).describe("Sandbox provider configuration")
});
var searchConfigSchema = z__default.default.object({
  vectorProvider: z__default.default.string().optional().describe("Vector store provider identifier"),
  vectorConfig: z__default.default.record(z__default.default.string(), z__default.default.unknown()).optional().describe("Vector store provider-specific configuration"),
  embedderProvider: z__default.default.string().optional().describe("Embedder provider identifier"),
  embedderModel: z__default.default.string().optional().describe("Embedder model name"),
  embedderConfig: z__default.default.record(z__default.default.string(), z__default.default.unknown()).optional().describe("Embedder provider-specific configuration"),
  bm25: z__default.default.union([z__default.default.boolean(), z__default.default.object({ k1: z__default.default.number().optional(), b: z__default.default.number().optional() })]).optional().describe("BM25 keyword search config"),
  searchIndexName: z__default.default.string().optional().describe("Custom index name for the vector store"),
  autoIndexPaths: z__default.default.array(z__default.default.string()).optional().describe("Paths to auto-index on init")
});
var workspaceToolConfigSchema = z__default.default.object({
  enabled: z__default.default.boolean().optional().describe("Whether the tool is enabled"),
  requireApproval: z__default.default.boolean().optional().describe("Whether the tool requires user approval before execution"),
  requireReadBeforeWrite: z__default.default.boolean().optional().describe("For write tools: require reading a file before writing to it")
});
var workspaceToolsConfigSchema = z__default.default.object({
  enabled: z__default.default.boolean().optional().describe("Default: whether all tools are enabled"),
  requireApproval: z__default.default.boolean().optional().describe("Default: whether all tools require user approval"),
  tools: z__default.default.record(z__default.default.string(), workspaceToolConfigSchema).optional().describe("Per-tool overrides keyed by workspace tool name")
});
var snapshotConfigSchema = z__default.default.object({
  name: z__default.default.string().describe("Name of the workspace"),
  description: z__default.default.string().optional().describe("Description of the workspace"),
  filesystem: filesystemConfigSchema.optional().describe("Filesystem configuration"),
  sandbox: sandboxConfigSchema.optional().describe("Sandbox configuration"),
  mounts: z__default.default.record(z__default.default.string(), filesystemConfigSchema).optional().describe("Mounted filesystems keyed by mount path"),
  search: searchConfigSchema.optional().describe("Search configuration"),
  skills: z__default.default.array(z__default.default.string()).optional().describe("Array of skill IDs"),
  tools: workspaceToolsConfigSchema.optional().describe("Workspace tool configuration"),
  autoSync: z__default.default.boolean().optional().describe("Whether to automatically sync the workspace"),
  operationTimeout: z__default.default.number().optional().describe("Operation timeout in milliseconds")
});
var createStoredWorkspaceBodySchema = z__default.default.object({
  id: z__default.default.string().optional().describe("Unique identifier. If not provided, derived from name."),
  authorId: z__default.default.string().optional().describe("Author identifier for multi-tenant filtering"),
  metadata: z__default.default.record(z__default.default.string(), z__default.default.unknown()).optional().describe("Additional metadata for the workspace")
}).merge(snapshotConfigSchema);
var updateStoredWorkspaceBodySchema = z__default.default.object({
  authorId: z__default.default.string().optional(),
  metadata: z__default.default.record(z__default.default.string(), z__default.default.unknown()).optional()
}).partial().merge(snapshotConfigSchema.partial());
var storedWorkspaceSchema = z__default.default.object({
  id: z__default.default.string(),
  status: z__default.default.string().describe("Workspace status: draft, published, or archived"),
  activeVersionId: z__default.default.string().optional(),
  authorId: z__default.default.string().optional(),
  metadata: z__default.default.record(z__default.default.string(), z__default.default.unknown()).optional(),
  createdAt: z__default.default.coerce.date(),
  updatedAt: z__default.default.coerce.date(),
  name: z__default.default.string().describe("Name of the workspace"),
  description: z__default.default.string().optional().describe("Description of the workspace"),
  filesystem: filesystemConfigSchema.optional().describe("Filesystem configuration"),
  sandbox: sandboxConfigSchema.optional().describe("Sandbox configuration"),
  mounts: z__default.default.record(z__default.default.string(), filesystemConfigSchema).optional().describe("Mounted filesystems keyed by mount path"),
  search: searchConfigSchema.optional().describe("Search configuration"),
  skills: z__default.default.array(z__default.default.string()).optional().describe("Array of skill IDs"),
  tools: workspaceToolsConfigSchema.optional().describe("Workspace tool configuration"),
  autoSync: z__default.default.boolean().optional().describe("Whether to automatically sync the workspace"),
  operationTimeout: z__default.default.number().optional().describe("Operation timeout in milliseconds")
});
var listStoredWorkspacesResponseSchema = chunkRWLJZWDL_cjs.paginationInfoSchema.extend({
  workspaces: z__default.default.array(storedWorkspaceSchema)
});
var getStoredWorkspaceResponseSchema = storedWorkspaceSchema;
var createStoredWorkspaceResponseSchema = storedWorkspaceSchema;
var updateStoredWorkspaceResponseSchema = z__default.default.union([
  z__default.default.object({
    id: z__default.default.string(),
    status: z__default.default.string(),
    activeVersionId: z__default.default.string().optional(),
    authorId: z__default.default.string().optional(),
    metadata: z__default.default.record(z__default.default.string(), z__default.default.unknown()).optional(),
    createdAt: z__default.default.coerce.date(),
    updatedAt: z__default.default.coerce.date()
  }),
  storedWorkspaceSchema
]);
var deleteStoredWorkspaceResponseSchema = z__default.default.object({
  success: z__default.default.boolean(),
  message: z__default.default.string()
});

// src/server/handlers/stored-workspaces.ts
var LIST_STORED_WORKSPACES_ROUTE = chunk4LVJBXI2_cjs.createRoute({
  method: "GET",
  path: "/stored/workspaces",
  responseType: "json",
  queryParamSchema: listStoredWorkspacesQuerySchema,
  responseSchema: listStoredWorkspacesResponseSchema,
  summary: "List stored workspaces",
  description: "Returns a paginated list of all workspace configurations stored in the database",
  tags: ["Stored Workspaces"],
  requiresAuth: true,
  handler: async ({ mastra, page, perPage, orderBy, authorId, metadata }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const workspaceStore = await storage.getStore("workspaces");
      if (!workspaceStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Workspaces storage domain is not available" });
      }
      const result = await workspaceStore.listResolved({
        page,
        perPage,
        orderBy,
        authorId,
        metadata
      });
      return result;
    } catch (error) {
      return chunkSUKFO7UM_cjs.handleError(error, "Error listing stored workspaces");
    }
  }
});
var GET_STORED_WORKSPACE_ROUTE = chunk4LVJBXI2_cjs.createRoute({
  method: "GET",
  path: "/stored/workspaces/:storedWorkspaceId",
  responseType: "json",
  pathParamSchema: storedWorkspaceIdPathParams,
  responseSchema: getStoredWorkspaceResponseSchema,
  summary: "Get stored workspace by ID",
  description: "Returns a specific workspace from storage by its unique identifier (resolved with active version config)",
  tags: ["Stored Workspaces"],
  requiresAuth: true,
  handler: async ({ mastra, storedWorkspaceId }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const workspaceStore = await storage.getStore("workspaces");
      if (!workspaceStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Workspaces storage domain is not available" });
      }
      const workspace = await workspaceStore.getByIdResolved(storedWorkspaceId);
      if (!workspace) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored workspace with id ${storedWorkspaceId} not found` });
      }
      return workspace;
    } catch (error) {
      return chunkSUKFO7UM_cjs.handleError(error, "Error getting stored workspace");
    }
  }
});
var CREATE_STORED_WORKSPACE_ROUTE = chunk4LVJBXI2_cjs.createRoute({
  method: "POST",
  path: "/stored/workspaces",
  responseType: "json",
  bodySchema: createStoredWorkspaceBodySchema,
  responseSchema: createStoredWorkspaceResponseSchema,
  summary: "Create stored workspace",
  description: "Creates a new workspace configuration in storage with the provided settings",
  tags: ["Stored Workspaces"],
  requiresAuth: true,
  handler: async ({
    mastra,
    id: providedId,
    authorId,
    metadata,
    name,
    description,
    filesystem,
    sandbox,
    mounts,
    search,
    skills,
    tools,
    autoSync,
    operationTimeout
  }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const workspaceStore = await storage.getStore("workspaces");
      if (!workspaceStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Workspaces storage domain is not available" });
      }
      const id = providedId || chunkJ5IJWZEA_cjs.toSlug(name);
      if (!id) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, {
          message: "Could not derive workspace ID from name. Please provide an explicit id."
        });
      }
      const existing = await workspaceStore.getById(id);
      if (existing) {
        throw new chunk64ITUOXI_cjs.HTTPException(409, { message: `Workspace with id ${id} already exists` });
      }
      await workspaceStore.create({
        workspace: {
          id,
          authorId,
          metadata,
          name,
          description,
          filesystem,
          sandbox,
          mounts,
          search,
          skills,
          tools,
          autoSync,
          operationTimeout
        }
      });
      const resolved = await workspaceStore.getByIdResolved(id);
      if (!resolved) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Failed to resolve created workspace" });
      }
      return resolved;
    } catch (error) {
      return chunkSUKFO7UM_cjs.handleError(error, "Error creating stored workspace");
    }
  }
});
var UPDATE_STORED_WORKSPACE_ROUTE = chunk4LVJBXI2_cjs.createRoute({
  method: "PATCH",
  path: "/stored/workspaces/:storedWorkspaceId",
  responseType: "json",
  pathParamSchema: storedWorkspaceIdPathParams,
  bodySchema: updateStoredWorkspaceBodySchema,
  responseSchema: updateStoredWorkspaceResponseSchema,
  summary: "Update stored workspace",
  description: "Updates an existing workspace in storage with the provided fields",
  tags: ["Stored Workspaces"],
  requiresAuth: true,
  handler: async ({
    mastra,
    storedWorkspaceId,
    // Metadata-level fields
    authorId,
    metadata,
    // Config fields (snapshot-level)
    name,
    description,
    filesystem,
    sandbox,
    mounts,
    search,
    skills,
    tools,
    autoSync,
    operationTimeout
  }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const workspaceStore = await storage.getStore("workspaces");
      if (!workspaceStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Workspaces storage domain is not available" });
      }
      const existing = await workspaceStore.getById(storedWorkspaceId);
      if (!existing) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored workspace with id ${storedWorkspaceId} not found` });
      }
      await workspaceStore.update({
        id: storedWorkspaceId,
        authorId,
        metadata,
        name,
        description,
        filesystem,
        sandbox,
        mounts,
        search,
        skills,
        tools,
        autoSync,
        operationTimeout
      });
      const resolved = await workspaceStore.getByIdResolved(storedWorkspaceId);
      if (!resolved) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Failed to resolve updated workspace" });
      }
      return resolved;
    } catch (error) {
      return chunkSUKFO7UM_cjs.handleError(error, "Error updating stored workspace");
    }
  }
});
var DELETE_STORED_WORKSPACE_ROUTE = chunk4LVJBXI2_cjs.createRoute({
  method: "DELETE",
  path: "/stored/workspaces/:storedWorkspaceId",
  responseType: "json",
  pathParamSchema: storedWorkspaceIdPathParams,
  responseSchema: deleteStoredWorkspaceResponseSchema,
  summary: "Delete stored workspace",
  description: "Deletes a workspace from storage by its unique identifier",
  tags: ["Stored Workspaces"],
  requiresAuth: true,
  handler: async ({ mastra, storedWorkspaceId }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const workspaceStore = await storage.getStore("workspaces");
      if (!workspaceStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Workspaces storage domain is not available" });
      }
      const existing = await workspaceStore.getById(storedWorkspaceId);
      if (!existing) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored workspace with id ${storedWorkspaceId} not found` });
      }
      await workspaceStore.delete(storedWorkspaceId);
      return {
        success: true,
        message: `Workspace ${storedWorkspaceId} deleted successfully`
      };
    } catch (error) {
      return chunkSUKFO7UM_cjs.handleError(error, "Error deleting stored workspace");
    }
  }
});

exports.CREATE_STORED_WORKSPACE_ROUTE = CREATE_STORED_WORKSPACE_ROUTE;
exports.DELETE_STORED_WORKSPACE_ROUTE = DELETE_STORED_WORKSPACE_ROUTE;
exports.GET_STORED_WORKSPACE_ROUTE = GET_STORED_WORKSPACE_ROUTE;
exports.LIST_STORED_WORKSPACES_ROUTE = LIST_STORED_WORKSPACES_ROUTE;
exports.UPDATE_STORED_WORKSPACE_ROUTE = UPDATE_STORED_WORKSPACE_ROUTE;
//# sourceMappingURL=chunk-Y4B2NZI4.cjs.map
//# sourceMappingURL=chunk-Y4B2NZI4.cjs.map