'use strict';

var chunkAKJAQVZ3_cjs = require('./chunk-AKJAQVZ3.cjs');
var chunkZ7LCIYK7_cjs = require('./chunk-Z7LCIYK7.cjs');
var chunkN54Q3AQR_cjs = require('./chunk-N54Q3AQR.cjs');
var fs = require('fs');

var SOURCE_PROVIDER_CAPABILITIES_TIMEOUT_MS = 3e3;
async function getSourceProviderCapabilities(getCapabilities) {
  let timeout;
  try {
    return await Promise.race([
      getCapabilities(),
      new Promise((_, reject) => {
        timeout = setTimeout(
          () => reject(new Error("Source provider capabilities timed out")),
          SOURCE_PROVIDER_CAPABILITIES_TIMEOUT_MS
        );
      })
    ]);
  } finally {
    if (timeout) clearTimeout(timeout);
  }
}
async function getEditorSourceCapabilities(editor) {
  const editorSource = editor.getSource?.();
  if (!editorSource) return void 0;
  if (editorSource === "db") {
    return {
      source: editorSource,
      storage: "database",
      canSave: true,
      canOpenChangeRequest: false
    };
  }
  const configuredProvider = editor.getSourceControlProvider?.();
  if (configuredProvider) {
    const provider = {
      id: configuredProvider.id,
      displayName: configuredProvider.displayName
    };
    try {
      const capabilities = await getSourceProviderCapabilities(() => configuredProvider.getCapabilities());
      return {
        source: editorSource,
        storage: "source-provider",
        provider,
        canSave: capabilities.canWrite,
        canOpenChangeRequest: capabilities.canOpenChangeRequest,
        unavailableReason: capabilities.canWrite ? void 0 : capabilities.reason
      };
    } catch {
      return {
        source: editorSource,
        storage: "source-provider",
        provider,
        canSave: false,
        canOpenChangeRequest: false,
        unavailableReason: "Unable to load source provider capabilities."
      };
    }
  }
  const sourceProvider = process.env.MASTRA_SOURCE_PROVIDER;
  if (sourceProvider) {
    return {
      source: editorSource,
      storage: "source-provider",
      provider: {
        id: sourceProvider,
        displayName: process.env.MASTRA_SOURCE_PROVIDER_NAME || sourceProvider
      },
      canSave: process.env.MASTRA_SOURCE_STORAGE_CAN_WRITE !== "false",
      canOpenChangeRequest: process.env.MASTRA_SOURCE_STORAGE_CAN_OPEN_CHANGE_REQUEST === "true"
    };
  }
  const isHosted = process.env.MASTRA_DEPLOYMENT_ID || process.env.MASTRA_CLOUD_API_ENDPOINT || process.env.MASTRA_PLATFORM_PROJECT_ID;
  if (isHosted) {
    return {
      source: editorSource,
      storage: "unavailable",
      canSave: false,
      canOpenChangeRequest: false,
      unavailableReason: "Code-source editing requires a source provider in hosted Studio."
    };
  }
  return {
    source: editorSource,
    storage: "filesystem",
    canSave: true,
    canOpenChangeRequest: false
  };
}
var GET_API_SCHEMA_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/system/api-schema",
  responseType: "json",
  responseSchema: chunkAKJAQVZ3_cjs.apiSchemaManifestResponseSchema,
  summary: "Get API schema manifest",
  description: "Returns the route-contract-derived API schema manifest for the machine-readable CLI",
  tags: ["System"],
  requiresAuth: true,
  handler: async () => {
    const { buildApiSchemaManifest } = await import('./api-schema-manifest-TMUB7DUR.cjs');
    return buildApiSchemaManifest();
  }
});
var GET_SYSTEM_PACKAGES_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/system/packages",
  responseType: "json",
  responseSchema: chunkAKJAQVZ3_cjs.systemPackagesResponseSchema,
  summary: "Get installed Mastra packages",
  description: "Returns a list of all installed Mastra packages and their versions from the project",
  tags: ["System"],
  requiresAuth: true,
  handler: async ({ mastra }) => {
    try {
      const packagesFilePath = process.env.MASTRA_PACKAGES_FILE;
      let packages = [];
      if (packagesFilePath) {
        try {
          const fileContent = fs.readFileSync(packagesFilePath, "utf-8");
          packages = JSON.parse(fileContent);
        } catch {
          packages = [];
        }
      }
      const storage = mastra.getStorage();
      const storageType = storage?.name;
      const observabilityStorage = storage?.stores?.observability;
      const observabilityStorageType = observabilityStorage?.constructor.name;
      const observabilityRuntimeStrategy = observabilityStorage?.runtimeTracingStrategy;
      const observabilityEnabled = !!mastra.observability.getDefaultInstance();
      const editor = mastra.getEditor();
      const editorSource = editor?.getSource?.();
      const editorSourceCapabilities = editor ? await getEditorSourceCapabilities(editor) : void 0;
      return {
        packages,
        isDev: process.env.MASTRA_DEV === "true",
        cmsEnabled: !!editor,
        editorSource,
        editorSourceCapabilities,
        observabilityEnabled,
        storageType,
        observabilityStorageType,
        observabilityRuntimeStrategy
      };
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error getting system packages");
    }
  }
});

exports.GET_API_SCHEMA_ROUTE = GET_API_SCHEMA_ROUTE;
exports.GET_SYSTEM_PACKAGES_ROUTE = GET_SYSTEM_PACKAGES_ROUTE;
//# sourceMappingURL=chunk-KR6SLS2Y.cjs.map
//# sourceMappingURL=chunk-KR6SLS2Y.cjs.map