import{SUPPORTED_AUTHORED_MODULE_FILE_EXTENSIONS}from"../update/module-files.js";import{pathExists,writeTextFile}from"../files.js";import{PNPM_WORKSPACE_CONTENT,PNPM_WORKSPACE_PATH}from"../update/pnpm-workspace.js";import{WEB_APP_TEMPLATE_FILES}from"./web-template.js";import{basename,join,resolve}from"node:path";import{mkdir,readdir,stat}from"node:fs/promises";const CURRENT_DIRECTORY_PROJECT_NAME=`.`,ALLOWED_CREATE_IN_PLACE_ENTRIES=new Set([`.DS_Store`,`.git`,`.gitkeep`,`.hg`]);function modelProviderSlug(e){let t=(e.split(`/`)[0]??``).replaceAll(/[^A-Za-z0-9._-]/gu,``);return t.length>0?t:`anthropic`}function byokProviderEnvVar(e){let t=modelProviderSlug(e).toUpperCase().replaceAll(/[^A-Z0-9]/gu,`_`);return`${/^[0-9]/.test(t)?`_`:``}${t}_API_KEY`}function renderTemplate(e,t){return e.replaceAll(`__EVE_INIT_APP_NAME__`,t.appName).replaceAll(`__EVE_INIT_MODEL__`,t.model).replaceAll(`__EVE_INIT_BYOK_PROVIDER__`,modelProviderSlug(t.model)).replaceAll(`__EVE_INIT_BYOK_ENV_VAR__`,byokProviderEnvVar(t.model)).replaceAll(`__EVE_INIT_PACKAGE_VERSION__`,formatEveDependencySpecifier(t.evePackageVersion)).replaceAll(`__EVE_INIT_AI_SDK_VERSION__`,t.aiPackageVersion).replaceAll(`__EVE_INIT_ZOD_VERSION__`,t.zodPackageVersion).replaceAll(`__EVE_INIT_TSGO_VERSION__`,t.tsgoPackageVersion).replaceAll(`__EVE_INIT_TYPES_NODE_VERSION__`,t.typesNodePackageVersion)}function formatEveDependencySpecifier(e){return/^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z-.]+)?$/.test(e)?`^${e}`:e}function packageJsonTemplate(e){return`{
  "name": "__EVE_INIT_APP_NAME__",
  "version": "0.0.0",
  "type": "module",
  "imports": {
    "#*": "./agent/*",
    "#evals/*": "./evals/*"
  },
  "scripts": {
    "build": "eve build",
    "dev": "eve dev",
    "start": "eve start",
    "typecheck": "tsgo"
  },
  "dependencies": {
    "ai": "__EVE_INIT_AI_SDK_VERSION__",
    "eve": "__EVE_INIT_PACKAGE_VERSION__",
    "zod": "__EVE_INIT_ZOD_VERSION__"
  },
  "devDependencies": {
${e?`    "@types/node": "__EVE_INIT_TYPES_NODE_VERSION__",
    "@typescript/native-preview": "__EVE_INIT_TSGO_VERSION__"`:`    "@typescript/native-preview": "__EVE_INIT_TSGO_VERSION__"`}
  }
}
`}const SHARED_TEMPLATE_FILES={"agent/channels/eve.ts":WEB_APP_TEMPLATE_FILES[`agent/channels/eve.ts`],"agent/instructions.md":`# Identity

You are a helpful assistant.
`,"tsconfig.json":`{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "noEmit": true
  },
  "include": ["agent/**/*.ts", "evals/**/*.ts", ".eve/**/*.d.ts"]
}
`,".gitignore":`node_modules
.env*
.eve
.vercel
.workflow-data
.next
.output
.nitro
dist
.DS_Store
*.tsbuildinfo
`,".vercelignore":`node_modules
.eve
.next
.output
.nitro
dist
`,[PNPM_WORKSPACE_PATH]:PNPM_WORKSPACE_CONTENT,"AGENTS.md":`# Eve Agent App

This project uses the Eve framework. Before writing code, always read the relevant guide in \`node_modules/eve/dist/docs/public/\`.
`,"CLAUDE.md":`@AGENTS.md
`};function templateFiles(e){return{"agent/agent.ts":e?`import { defineAgent } from "eve";

export default defineAgent({
  model: "__EVE_INIT_MODEL__",
  modelOptions: {
    providerOptions: {
      gateway: {
        byok: {
          "__EVE_INIT_BYOK_PROVIDER__": [{ apiKey: process.env.__EVE_INIT_BYOK_ENV_VAR__! }],
        },
      },
    },
  },
});
`:`import { defineAgent } from "eve";

export default defineAgent({
  model: "__EVE_INIT_MODEL__",
});
`,...SHARED_TEMPLATE_FILES,"package.json":packageJsonTemplate(e)}}function assertStampedVersion(e,t){if(t.startsWith(`__`))throw Error(`Scaffold received unstamped version token (${e}=${t}). Build eve before using its dist entrypoint.`)}async function assertCanCreateInPlace(e,n){if(!await pathExists(e))return;let r=(await readdir(e)).filter(e=>!ALLOWED_CREATE_IN_PLACE_ENTRIES.has(e));if(r.length>0&&!n){let e=r.slice(0,5).join(`, `),t=r.length>5?`, and ${r.length-5} more`:``;throw Error(`Cannot create project in current directory because it is not empty. Found: ${e}${t}. Pass --yes with --in-place to overwrite Eve scaffold files.`)}}async function scaffoldBaseProject(e){let r=resolve(e.targetDirectory??process.cwd(),e.projectName),i=e.projectName===`.`,a=e.overwriteExisting??!1,s=e.byokProvider??!1;if(i)await assertCanCreateInPlace(r,a);else if(await pathExists(r))throw Error(`Cannot create project because "${r}" already exists.`);let u={appName:basename(r),model:e.model,evePackageVersion:e.evePackageVersion??`0.6.0-beta.13`,aiPackageVersion:e.aiPackageVersion??`7.0.0-canary.171`,zodPackageVersion:e.zodPackageVersion??`4.4.3`,tsgoPackageVersion:e.tsgoPackageVersion??`7.0.0-dev.20260523.1`,typesNodePackageVersion:e.typesNodePackageVersion??`25.9.1`};assertStampedVersion(`evePackageVersion`,u.evePackageVersion),assertStampedVersion(`aiPackageVersion`,u.aiPackageVersion),assertStampedVersion(`zodPackageVersion`,u.zodPackageVersion),assertStampedVersion(`tsgoPackageVersion`,u.tsgoPackageVersion),s&&assertStampedVersion(`typesNodePackageVersion`,u.typesNodePackageVersion),await mkdir(r,{recursive:!0});for(let[o,c]of Object.entries(templateFiles(s))){let s=`${r}/${o}`,l=await pathExists(s);await writeTextFile(s,renderTemplate(c,u),{force:i&&a}),l&&await e.onOverwriteFile?.(s)}return r}async function isEveProject(t){for(let n of SUPPORTED_AUTHORED_MODULE_FILE_EXTENSIONS)try{return await stat(join(t,`agent`,`agent${n}`)),!0}catch{}return!1}export{CURRENT_DIRECTORY_PROJECT_NAME,byokProviderEnvVar,isEveProject,modelProviderSlug,scaffoldBaseProject};