{"version":3,"file":"runtime-BmfsHLrk.mjs","names":[],"sources":["../src/runtime.ts"],"sourcesContent":["export type ProjectRuntimeDatabase = {\n  host: string;\n  port: number;\n  database: string;\n  user: string;\n  password: string;\n  searchPath: string;\n  organizationId: string;\n  /** @deprecated Per-project workers only — org workers omit this. */\n  projectId?: string;\n};\n\nexport type OrgProjectDeploy = {\n  projectId: string;\n  name: string;\n  artifactId: string;\n  artifactStorageKey: string;\n  artifactVersion: number;\n  /** Require an affirmative artifact-specific health record before promotion. */\n  requiresArtifactHealth?: boolean;\n};\n\nexport type OrgArtifactSpec = {\n  projectId: string;\n  artifactStorageKey: string;\n  artifactId: string;\n  /** Monotonic per-project artifact version from the platform database. */\n  artifactVersion: number;\n};\n\nexport type ProjectRuntimeHosting = {\n  appName: string;\n};\n\nexport type ProjectRuntimeInput = {\n  organizationId: string;\n  projects: OrgProjectDeploy[];\n  database: ProjectRuntimeDatabase;\n  hosting?: ProjectRuntimeHosting;\n  /** Extra env merged into the project server launch spec (e.g. from an llmProxy plugin). */\n  env?: Record<string, string>;\n  /** Previous runtime to retire after a successful cutover. */\n  previousRuntimeId?: string | null;\n};\n\n/** Builds base64 `KEYSTROKE_ORG_ARTIFACTS` payload for org worker containers. */\nexport function encodeOrgArtifacts(artifacts: OrgArtifactSpec[]): string {\n  return Buffer.from(JSON.stringify(artifacts)).toString(\"base64\");\n}\n\nexport function parseOrgArtifactsFromEnv(\n  env: NodeJS.ProcessEnv = process.env,\n): OrgArtifactSpec[] | undefined {\n  const raw = env.KEYSTROKE_ORG_ARTIFACTS?.trim();\n  if (!raw) {\n    return undefined;\n  }\n\n  const parsed = JSON.parse(Buffer.from(raw, \"base64\").toString(\"utf8\")) as unknown;\n  if (!Array.isArray(parsed)) {\n    throw new Error(\"KEYSTROKE_ORG_ARTIFACTS must decode to a JSON array\");\n  }\n\n  return parsed.map((entry) => {\n    if (\n      !entry ||\n      typeof entry !== \"object\" ||\n      typeof (entry as OrgArtifactSpec).projectId !== \"string\" ||\n      typeof (entry as OrgArtifactSpec).artifactStorageKey !== \"string\" ||\n      typeof (entry as OrgArtifactSpec).artifactId !== \"string\" ||\n      typeof (entry as OrgArtifactSpec).artifactVersion !== \"number\"\n    ) {\n      throw new Error(\n        \"KEYSTROKE_ORG_ARTIFACTS entries require projectId, artifactStorageKey, artifactId, and artifactVersion\",\n      );\n    }\n\n    return entry as OrgArtifactSpec;\n  });\n}\n\nexport type ProjectRuntimeDestroyInput = {\n  runtimeId: string;\n  hosting?: ProjectRuntimeHosting;\n};\n\nexport type ProjectRuntimeReconcileInput = {\n  organizationId: string;\n  /** The runtime to keep — every other runtime belonging to this org should be destroyed. */\n  keepRuntimeId: string;\n  hosting?: ProjectRuntimeHosting;\n};\n\nexport type ProjectRuntimeResult = {\n  runtimeId: string;\n  baseUrl: string;\n};\n\nexport type ProjectRuntimeSleepInput = {\n  runtimeId: string;\n  hosting?: ProjectRuntimeHosting;\n};\n\nexport type ProjectRuntimeWakeInput = {\n  runtimeId: string;\n  hosting?: ProjectRuntimeHosting;\n};\n\nexport type ProjectRuntime = {\n  start(input: ProjectRuntimeInput): Promise<ProjectRuntimeResult>;\n  destroy?(input: ProjectRuntimeDestroyInput): Promise<void>;\n  /**\n   * Sweep away every runtime for this org except `keepRuntimeId`, retrying\n   * transient failures instead of giving up after one attempt. This catches\n   * orphans that a single {@link destroy} call missed (e.g. a previous deploy's\n   * destroy failed silently), not just the one runtime recorded as \"previous.\"\n   */\n  reconcile?(input: ProjectRuntimeReconcileInput): Promise<void>;\n  /**\n   * Put an existing runtime to sleep without destroying it (e.g. Docker stop,\n   * Fly suspend). Provider-specific; omit when unsupported.\n   */\n  sleep?(input: ProjectRuntimeSleepInput): Promise<void>;\n  /**\n   * Resume a sleeping runtime. May return a fresh {@link ProjectRuntimeResult}\n   * when the base URL can change (e.g. Docker host port remap).\n   */\n  wake?(input: ProjectRuntimeWakeInput): Promise<ProjectRuntimeResult | void>;\n};\n\nexport type ProjectPingTarget = {\n  baseUrl: string | null;\n  runtimeId: string | null;\n};\n"],"mappings":";;AA8CA,SAAgB,mBAAmB,WAAsC;CACvE,OAAO,OAAO,KAAK,KAAK,UAAU,SAAS,CAAC,EAAE,SAAS,QAAQ;AACjE;AAEA,SAAgB,yBACd,MAAyB,QAAQ,KACF;CAC/B,MAAM,MAAM,IAAI,yBAAyB,KAAK;CAC9C,IAAI,CAAC,KACH;CAGF,MAAM,SAAS,KAAK,MAAM,OAAO,KAAK,KAAK,QAAQ,EAAE,SAAS,MAAM,CAAC;CACrE,IAAI,CAAC,MAAM,QAAQ,MAAM,GACvB,MAAM,IAAI,MAAM,qDAAqD;CAGvE,OAAO,OAAO,KAAK,UAAU;EAC3B,IACE,CAAC,SACD,OAAO,UAAU,YACjB,OAAQ,MAA0B,cAAc,YAChD,OAAQ,MAA0B,uBAAuB,YACzD,OAAQ,MAA0B,eAAe,YACjD,OAAQ,MAA0B,oBAAoB,UAEtD,MAAM,IAAI,MACR,wGACF;EAGF,OAAO;CACT,CAAC;AACH"}