'use strict';

var chunkO272KKXN_cjs = require('./chunk-O272KKXN.cjs');
var chunkEFX5WYRT_cjs = require('./chunk-EFX5WYRT.cjs');
var chunkY2BKW5ND_cjs = require('./chunk-Y2BKW5ND.cjs');
var chunk6P7P7Y5U_cjs = require('./chunk-6P7P7Y5U.cjs');
var chunk64ITUOXI_cjs = require('./chunk-64ITUOXI.cjs');
var workspace = require('@mastra/core/workspace');

var LIST_STORED_SKILLS_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "GET",
  path: "/stored/skills",
  responseType: "json",
  queryParamSchema: chunkO272KKXN_cjs.listStoredSkillsQuerySchema,
  responseSchema: chunkO272KKXN_cjs.listStoredSkillsResponseSchema,
  summary: "List stored skills",
  description: "Returns a paginated list of all skill configurations stored in the database",
  tags: ["Stored Skills"],
  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 skillStore = await storage.getStore("skills");
      if (!skillStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Skills storage domain is not available" });
      }
      const result = await skillStore.listResolved({
        page,
        perPage,
        orderBy,
        authorId,
        metadata
      });
      return result;
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error listing stored skills");
    }
  }
});
var GET_STORED_SKILL_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "GET",
  path: "/stored/skills/:storedSkillId",
  responseType: "json",
  pathParamSchema: chunkO272KKXN_cjs.storedSkillIdPathParams,
  responseSchema: chunkO272KKXN_cjs.getStoredSkillResponseSchema,
  summary: "Get stored skill by ID",
  description: "Returns a specific skill from storage by its unique identifier (resolved with active version config)",
  tags: ["Stored Skills"],
  requiresAuth: true,
  handler: async ({ mastra, storedSkillId }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const skillStore = await storage.getStore("skills");
      if (!skillStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Skills storage domain is not available" });
      }
      const skill = await skillStore.getByIdResolved(storedSkillId);
      if (!skill) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored skill with id ${storedSkillId} not found` });
      }
      return skill;
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error getting stored skill");
    }
  }
});
var CREATE_STORED_SKILL_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "POST",
  path: "/stored/skills",
  responseType: "json",
  bodySchema: chunkO272KKXN_cjs.createStoredSkillBodySchema,
  responseSchema: chunkO272KKXN_cjs.createStoredSkillResponseSchema,
  summary: "Create stored skill",
  description: "Creates a new skill configuration in storage with the provided details",
  tags: ["Stored Skills"],
  requiresAuth: true,
  handler: async ({
    mastra,
    id: providedId,
    authorId,
    name,
    description,
    instructions,
    license,
    compatibility,
    source,
    references,
    scripts,
    assets,
    metadata
  }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const skillStore = await storage.getStore("skills");
      if (!skillStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Skills storage domain is not available" });
      }
      const id = providedId || chunkEFX5WYRT_cjs.toSlug(name);
      if (!id) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, {
          message: "Could not derive skill ID from name. Please provide an explicit id."
        });
      }
      const existing = await skillStore.getById(id);
      if (existing) {
        throw new chunk64ITUOXI_cjs.HTTPException(409, { message: `Skill with id ${id} already exists` });
      }
      await skillStore.create({
        skill: {
          id,
          authorId,
          name,
          description,
          instructions,
          license,
          compatibility,
          source,
          references,
          scripts,
          assets,
          metadata
        }
      });
      const resolved = await skillStore.getByIdResolved(id);
      if (!resolved) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Failed to resolve created skill" });
      }
      return resolved;
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error creating stored skill");
    }
  }
});
var UPDATE_STORED_SKILL_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "PATCH",
  path: "/stored/skills/:storedSkillId",
  responseType: "json",
  pathParamSchema: chunkO272KKXN_cjs.storedSkillIdPathParams,
  bodySchema: chunkO272KKXN_cjs.updateStoredSkillBodySchema,
  responseSchema: chunkO272KKXN_cjs.updateStoredSkillResponseSchema,
  summary: "Update stored skill",
  description: "Updates an existing skill in storage with the provided fields",
  tags: ["Stored Skills"],
  requiresAuth: true,
  handler: async ({
    mastra,
    storedSkillId,
    // Entity-level fields
    authorId,
    // Config fields (snapshot-level)
    name,
    description,
    instructions,
    license,
    compatibility,
    source,
    references,
    scripts,
    assets,
    metadata
  }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const skillStore = await storage.getStore("skills");
      if (!skillStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Skills storage domain is not available" });
      }
      const existing = await skillStore.getById(storedSkillId);
      if (!existing) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored skill with id ${storedSkillId} not found` });
      }
      await skillStore.update({
        id: storedSkillId,
        authorId,
        name,
        description,
        instructions,
        license,
        compatibility,
        source,
        references,
        scripts,
        assets,
        metadata
      });
      const resolved = await skillStore.getByIdResolved(storedSkillId);
      if (!resolved) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Failed to resolve updated skill" });
      }
      return resolved;
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error updating stored skill");
    }
  }
});
var DELETE_STORED_SKILL_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "DELETE",
  path: "/stored/skills/:storedSkillId",
  responseType: "json",
  pathParamSchema: chunkO272KKXN_cjs.storedSkillIdPathParams,
  responseSchema: chunkO272KKXN_cjs.deleteStoredSkillResponseSchema,
  summary: "Delete stored skill",
  description: "Deletes a skill from storage by its unique identifier",
  tags: ["Stored Skills"],
  requiresAuth: true,
  handler: async ({ mastra, storedSkillId }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const skillStore = await storage.getStore("skills");
      if (!skillStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Skills storage domain is not available" });
      }
      const existing = await skillStore.getById(storedSkillId);
      if (!existing) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored skill with id ${storedSkillId} not found` });
      }
      await skillStore.delete(storedSkillId);
      return {
        success: true,
        message: `Skill ${storedSkillId} deleted successfully`
      };
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error deleting stored skill");
    }
  }
});
var PUBLISH_STORED_SKILL_ROUTE = chunk6P7P7Y5U_cjs.createRoute({
  method: "POST",
  path: "/stored/skills/:storedSkillId/publish",
  responseType: "json",
  pathParamSchema: chunkO272KKXN_cjs.storedSkillIdPathParams,
  bodySchema: chunkO272KKXN_cjs.publishStoredSkillBodySchema,
  responseSchema: chunkO272KKXN_cjs.publishStoredSkillResponseSchema,
  summary: "Publish stored skill",
  description: "Snapshots the skill directory from the filesystem into content-addressable blob storage, creates a new version with a tree manifest, and marks the skill as published",
  tags: ["Stored Skills"],
  requiresAuth: true,
  handler: async ({ mastra, storedSkillId, skillPath }) => {
    try {
      const storage = mastra.getStorage();
      if (!storage) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Storage is not configured" });
      }
      const skillStore = await storage.getStore("skills");
      if (!skillStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Skills storage domain is not available" });
      }
      const blobStore = await storage.getStore("blobs");
      if (!blobStore) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Blob storage domain is not available" });
      }
      const existing = await skillStore.getById(storedSkillId);
      if (!existing) {
        throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Stored skill with id ${storedSkillId} not found` });
      }
      const path = await import('path');
      const resolvedPath = path.default.resolve(skillPath);
      const allowedBase = path.default.resolve(process.env.SKILLS_BASE_DIR || process.cwd());
      if (!resolvedPath.startsWith(allowedBase + path.default.sep) && resolvedPath !== allowedBase) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, {
          message: `skillPath must be within the allowed directory: ${allowedBase}`
        });
      }
      const source = new workspace.LocalSkillSource();
      const { publishSkillFromSource } = await import('@mastra/core/workspace');
      const { snapshot, tree } = await publishSkillFromSource(source, resolvedPath, blobStore);
      await skillStore.update({
        id: storedSkillId,
        ...snapshot,
        tree,
        status: "published"
      });
      const latestVersion = await skillStore.getLatestVersion(storedSkillId);
      if (latestVersion) {
        await skillStore.update({
          id: storedSkillId,
          activeVersionId: latestVersion.id
        });
      }
      const resolved = await skillStore.getByIdResolved(storedSkillId);
      if (!resolved) {
        throw new chunk64ITUOXI_cjs.HTTPException(500, { message: "Failed to resolve skill after publish" });
      }
      return resolved;
    } catch (error) {
      return chunkY2BKW5ND_cjs.handleError(error, "Error publishing stored skill");
    }
  }
});

exports.CREATE_STORED_SKILL_ROUTE = CREATE_STORED_SKILL_ROUTE;
exports.DELETE_STORED_SKILL_ROUTE = DELETE_STORED_SKILL_ROUTE;
exports.GET_STORED_SKILL_ROUTE = GET_STORED_SKILL_ROUTE;
exports.LIST_STORED_SKILLS_ROUTE = LIST_STORED_SKILLS_ROUTE;
exports.PUBLISH_STORED_SKILL_ROUTE = PUBLISH_STORED_SKILL_ROUTE;
exports.UPDATE_STORED_SKILL_ROUTE = UPDATE_STORED_SKILL_ROUTE;
//# sourceMappingURL=chunk-R2DKW2EK.cjs.map
//# sourceMappingURL=chunk-R2DKW2EK.cjs.map