import { f as defineAdminEventHandler, h as requireAdminRole, q as readBody, c as createError, z as withAIGuardrails, B as approxBodyBytes, L as getPluginRuntimeManager, M as generateScaffoldingResponse } from '../../../../nitro/nitro.mjs';
import 'node:crypto';
import 'jsonwebtoken';
import 'node:fs';
import 'node:module';
import 'node:path';
import 'node:url';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'pino';
import 'drizzle-orm/better-sqlite3';
import 'drizzle-orm/node-postgres';
import 'drizzle-orm/mysql2';
import 'better-sqlite3';
import 'pg';
import 'mysql2/promise';
import 'drizzle-orm';
import 'fs';
import 'path';
import 'prom-client';
import 'bcryptjs';
import '@node-rs/argon2';
import 'node:async_hooks';
import 'crypto';
import 'node:dns';
import 'node:util';
import 'node:net';
import 'fs/promises';
import 'url';
import 'semver';

const PLATFORM_VERSION_FALLBACK = "0.16.0";
const PLUGIN_API_VERSION_FALLBACK = "0.14.0";
const THEME_API_VERSION_FALLBACK = "0.14.0";
const scaffoldingAssistant_post = defineAdminEventHandler("admin:ai:scaffolding-assistant", async (event) => {
  var _a;
  await requireAdminRole(event, ["super-admin"]);
  const body = (_a = await readBody(event)) != null ? _a : {};
  if (body.kind !== "plugin" && body.kind !== "theme") {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: 'Body must include "kind": either "plugin" or "theme".'
    });
  }
  const request = {
    kind: body.kind,
    intent: typeof body.intent === "string" ? body.intent : void 0,
    name: typeof body.name === "string" ? body.name : void 0,
    displayName: typeof body.displayName === "string" ? body.displayName : void 0,
    description: typeof body.description === "string" ? body.description : void 0
  };
  const data = await withAIGuardrails({
    advisor: "scaffolding",
    event,
    bodyBytes: approxBodyBytes(body),
    inputDigest: {
      kind: request.kind,
      hasName: Boolean(request.name),
      hasIntent: Boolean(request.intent)
    },
    run: () => {
      const manager = getPluginRuntimeManager();
      const registry = manager == null ? void 0 : manager.getRegistry();
      const installedPluginNames = registry ? registry.getAll().map((entry) => entry.definition.manifest.name) : [];
      const context = {
        installedPluginNames,
        platformVersion: resolvePlatformVersion(),
        pluginApiVersion: resolveSdkVersion("@notty/plugin-api", PLUGIN_API_VERSION_FALLBACK),
        themeApiVersion: resolveSdkVersion("@notty/theme-api", THEME_API_VERSION_FALLBACK)
      };
      return generateScaffoldingResponse(request, context);
    },
    digestOutput: (result) => ({
      suggestions: result.files.length
    })
  });
  return { success: true, data };
});
function resolvePlatformVersion() {
  const fromEnv = process.env.NOTTY_VERSION;
  if (typeof fromEnv === "string" && fromEnv.length > 0) {
    return fromEnv;
  }
  return PLATFORM_VERSION_FALLBACK;
}
function resolveSdkVersion(packageName, fallback) {
  const envKey = packageName === "@notty/plugin-api" ? "NOTTY_PLUGIN_API_VERSION" : "NOTTY_THEME_API_VERSION";
  const fromEnv = process.env[envKey];
  if (typeof fromEnv === "string" && fromEnv.length > 0) {
    return fromEnv;
  }
  return fallback;
}

export { scaffoldingAssistant_post as default };
//# sourceMappingURL=scaffolding-assistant.post.mjs.map
