{"version":3,"file":"artifacts.cjs","names":["Schema","formatUnknownError","CORE_UI_PLUGIN_KEY","existsSync","join","Effect","writeResolvedConfig","openResolution","syncApiContractBridge","emitCoreUiStubs","generateUiManifest","generateRuntimeMapEffect"],"sources":["../../../src/services/codegen/artifacts.ts"],"sourcesContent":["import { existsSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { Effect, Schema } from \"effect\";\nimport { emitCoreUiStubs } from \"every-plugin/build/ui\";\nimport { CORE_UI_PLUGIN_KEY } from \"every-plugin/ui/manifest\";\nimport { generateUiManifest } from \"every-plugin/ui/manifest-generator\";\nimport { writeResolvedConfig } from \"../../config\";\nimport type { BosConfig, RuntimeConfig } from \"../../types\";\nimport { formatUnknownError } from \"../../utils/errors\";\nimport type { BosEnv } from \"../resolution/merge\";\nimport { openResolution } from \"../resolution/session\";\nimport { type ContractBridgeStatus, syncApiContractBridge } from \"./api-contract\";\nimport { generateRuntimeMapEffect } from \"./runtime-map\";\n\nexport class ArtifactGenError extends Schema.TaggedError<ArtifactGenError>()(\"ArtifactGenError\", {\n  phase: Schema.String,\n  cause: Schema.optional(Schema.Unknown),\n}) {\n  // TaggedError carries no message field, so `error.message` serializes as \"\"\n  // down every `error instanceof Error ? error.message` wire (CI output\n  // included). Render the phase + cause instead — a generation failure must\n  // always name what failed.\n  override get message(): string {\n    let detail: string;\n    try {\n      detail =\n        this.cause instanceof Error\n          ? this.cause.message\n          : this.cause === undefined\n            ? \"\"\n            : formatUnknownError(this.cause);\n    } catch {\n      // some Error subclasses (Effect's YieldableError) delete toString —\n      // a non-serializable cause must not break rendering the message\n      detail = \"non-serializable cause\";\n    }\n    return `artifact generation failed (${this.phase})${detail ? `: ${detail}` : \"\"}`;\n  }\n}\n\ninterface GeneratedArtifacts {\n  resolvedConfigPath?: string;\n  contractBridgePath: string;\n}\n\ninterface UiManifestTarget {\n  workspaceRoot: string;\n  pluginName: string;\n  emitRouteTree: boolean;\n}\n\nexport function uiManifestTargets(\n  runtimeConfig: RuntimeConfig,\n  forceRouteTree?: boolean,\n): UiManifestTarget[] {\n  const targets: UiManifestTarget[] = [];\n  const ui = runtimeConfig.ui;\n  // The core ui is always a manifest target when local — with no authored\n  // rsbuild.config.ts the every-plugin build synthesizes one, so the\n  // config's existence no longer gates manifest generation.\n  if (ui?.source === \"local\" && ui.localPath) {\n    targets.push({\n      workspaceRoot: ui.localPath,\n      pluginName: CORE_UI_PLUGIN_KEY,\n      emitRouteTree: forceRouteTree\n        ? true\n        : !existsSync(join(ui.localPath, \"src/routeTree.gen.ts\")),\n    });\n  }\n  // A local auth attachment's ui (`auth.ui`) is its own manifest target:\n  // the plugins bucket only covers `plugins.<id>` entries, so an\n  // `auth.ui` override against a published auth server would otherwise\n  // never get a manifest (it is composed under the \"auth\" key).\n  const authUi = runtimeConfig.auth?.ui;\n  if (\n    authUi?.source === \"local\" &&\n    authUi.localPath &&\n    !targets.some((t) => t.workspaceRoot === authUi.localPath)\n  ) {\n    targets.push({\n      workspaceRoot: authUi.localPath,\n      pluginName: \"auth\",\n      emitRouteTree: forceRouteTree\n        ? true\n        : !existsSync(join(authUi.localPath, \"src/routeTree.gen.ts\")),\n    });\n  }\n  for (const [key, plugin] of Object.entries(runtimeConfig.plugins ?? {})) {\n    const uiPath = plugin.ui?.localPath;\n    // Folder-form plugin uis (no committed rsbuild.config.ts) are still\n    // manifest targets: generateUiManifest needs only src/routes, and on a\n    // fresh checkout no manifest.gen.json exists until something generates\n    // it — gating on the rsbuild config left those uis ungenerated, so a\n    // strict types gen failed the run on a source typegen itself should\n    // have produced (the #558 class; same reason auth-ui is unconditional).\n    if (plugin.ui?.source === \"local\" && uiPath && existsSync(join(uiPath, \"src/routes\"))) {\n      targets.push({ workspaceRoot: uiPath, pluginName: key, emitRouteTree: false });\n    }\n  }\n  return targets;\n}\n\nexport interface GenerateCodeArtifactsOptions {\n  env?: BosEnv;\n  extendsChain?: string[];\n  runtimeConfig?: RuntimeConfig;\n  forceRouteTree?: boolean;\n  /** Strict runtime-map degradation (#594): an unresolvable remote manifest\n   * fails the run instead of shrinking the map. `bos types gen` sets this —\n   * dev/build/train paths degrade to a warning (a flaky remote must not\n   * block a boot). */\n  runtimeMapStrict?: boolean;\n}\n\nexport const generateCodeArtifactsEffect = Effect.fn(\"generateCodeArtifacts\")(function* (\n  configDir: string,\n  config: BosConfig,\n  opts?: GenerateCodeArtifactsOptions,\n): Effect.fn.Return<\n  (GeneratedArtifacts & { contractStatus: ContractBridgeStatus[] }) | null,\n  ArtifactGenError\n> {\n  const env = opts?.env;\n  if (env) {\n    yield* Effect.try({\n      try: () => writeResolvedConfig(configDir, config, env, opts?.extendsChain),\n      catch: (cause) => new ArtifactGenError({ phase: \"write resolved config\", cause }),\n    });\n  }\n\n  const runtimeConfig =\n    opts?.runtimeConfig ??\n    (yield* Effect.tryPromise({\n      try: () => openResolution({ cwd: configDir }),\n      catch: (cause) => new ArtifactGenError({ phase: \"load resolved config\", cause }),\n    }))?.runtime;\n  if (!runtimeConfig) return null;\n\n  const bridge = yield* Effect.tryPromise({\n    try: () =>\n      syncApiContractBridge({\n        configDir,\n        runtimeConfig,\n        apiBaseUrl: runtimeConfig.api.url,\n      }),\n    catch: (cause) => new ArtifactGenError({ phase: \"sync api contract bridge\", cause }),\n  });\n\n  for (const target of uiManifestTargets(runtimeConfig, opts?.forceRouteTree)) {\n    if (target.pluginName === CORE_UI_PLUGIN_KEY) {\n      yield* Effect.try({\n        try: () => emitCoreUiStubs(target.workspaceRoot),\n        catch: (cause) =>\n          new ArtifactGenError({ phase: `emit ui stubs (${target.pluginName})`, cause }),\n      });\n    }\n    yield* Effect.tryPromise({\n      try: () => generateUiManifest(target),\n      catch: (cause) =>\n        new ArtifactGenError({ phase: `generate ui manifest (${target.pluginName})`, cause }),\n    });\n  }\n\n  // The composition inspector (#591/#594): the generated effective route\n  // tree — local manifests were just refreshed above; remote ones fetch from\n  // the published `<ui>/manifest.gen.json`. Strict mode (`bos types gen`)\n  // fails the run on an unresolvable map so typecheck catches an upstream\n  // rename; dev/build/train paths warn and continue (a flaky remote must\n  // not block a boot — the runtime load fails loudly on its own).\n  yield* generateRuntimeMapEffect(configDir, runtimeConfig, {\n    strict: opts?.runtimeMapStrict === true,\n    env,\n    platform: config.platform ?? null,\n    ...(opts?.extendsChain ? { extendsChain: opts.extendsChain } : {}),\n  }).pipe(\n    Effect.catch((error) => {\n      if (opts?.runtimeMapStrict === true) {\n        return Effect.fail(new ArtifactGenError({ phase: \"generate runtime map\", cause: error }));\n      }\n      console.warn(\n        `[runtime-map] generation failed — continuing: ${error instanceof Error ? error.message : String(error)}`,\n      );\n      return Effect.succeed(null);\n    }),\n  );\n\n  return {\n    resolvedConfigPath: env ? join(configDir, \".bos/bos.resolved-config.json\") : undefined,\n    contractBridgePath: bridge.bridgePath,\n    contractStatus: bridge.status,\n  };\n});\n\nexport async function generateCodeArtifacts(\n  configDir: string,\n  config: BosConfig,\n  opts?: GenerateCodeArtifactsOptions,\n): Promise<(GeneratedArtifacts & { contractStatus: ContractBridgeStatus[] }) | null> {\n  return Effect.runPromise(generateCodeArtifactsEffect(configDir, config, opts));\n}\n"],"mappings":";;;;;;;;;;;;;AAcA,IAAa,mBAAb,cAAsCA,cAAO,YAA8B,CAAC,CAAC,oBAAoB;CAC/F,OAAOA,cAAO;CACd,OAAOA,cAAO,SAASA,cAAO,OAAO;AACvC,CAAC,CAAC,CAAC;CAKD,IAAa,UAAkB;EAC7B,IAAI;EACJ,IAAI;GACF,SACE,KAAK,iBAAiB,QAClB,KAAK,MAAM,UACX,KAAK,UAAU,SACb,KACAC,kCAAmB,KAAK,KAAK;EACvC,QAAQ;GAGN,SAAS;EACX;EACA,OAAO,+BAA+B,KAAK,MAAM,GAAG,SAAS,KAAK,WAAW;CAC/E;AACF;AAaA,SAAgB,kBACd,eACA,gBACoB;CACpB,MAAM,UAA8B,CAAC;CACrC,MAAM,KAAK,cAAc;CAIzB,IAAI,IAAI,WAAW,WAAW,GAAG,WAC/B,QAAQ,KAAK;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