{"version":3,"file":"registry-HlEaGvl5.mjs","names":[],"sources":["../src/configure/services/workflow/registry.ts"],"sourcesContent":["import type { TailorEnv, TailorPrincipal } from \"#/runtime/types\";\nimport type { ExecJobFunctionOptions, StartWorkflowOptions } from \"#/runtime/workflow\";\n\n/**\n * Body signature shared by workflow jobs at registry-write time.\n * The user's `createWorkflowJob`/`createWorkflow` body uses concrete types,\n * but the registry erases them for storage.\n */\nexport type RegisteredJobBody = (\n  args: unknown,\n  context: { env: TailorEnv; invoker: TailorPrincipal | null },\n) => unknown | Promise<unknown>;\n\nconst JOB_REGISTRY_KEY: unique symbol = Symbol.for(\"tailor-platform/sdk:job-registry\");\n\ntype PlatformWorkflow = {\n  startWorkflow: (name: string, args?: unknown, options?: StartWorkflowOptions) => Promise<string>;\n  execJobFunction: (name: string, args?: unknown, options?: ExecJobFunctionOptions) => unknown;\n};\n\ntype GlobalWithRegistry = typeof globalThis & {\n  [JOB_REGISTRY_KEY]?: Map<string, RegisteredJobBody>;\n  tailor?: { workflow?: PlatformWorkflow };\n};\n\nfunction jobs(): Map<string, RegisteredJobBody> {\n  const g = globalThis as GlobalWithRegistry;\n  let map = g[JOB_REGISTRY_KEY];\n  if (!map) {\n    map = new Map();\n    g[JOB_REGISTRY_KEY] = map;\n  }\n  return map;\n}\n\n/**\n * Register a job body keyed by job name. Called as a side effect by\n * `createWorkflowJob` so `runWorkflowLocally()` can execute dependent job\n * bodies when `globalThis.tailor.workflow.execJobFunction(name, args)` is invoked.\n *\n * In production builds the bundler rewrites `.start()` calls so this registry\n * is never read; the gated write is dropped as dead code.\n * @param name - Job name\n * @param body - Job body function\n */\nexport function registerJob(name: string, body: RegisteredJobBody): void {\n  jobs().set(name, body);\n}\n\n/**\n * Look up a registered job body by name.\n * @param name - Job name\n * @returns The registered body, or undefined when no job is registered\n */\nexport function getRegisteredJob(name: string): RegisteredJobBody | undefined {\n  return jobs().get(name);\n}\n\nfunction currentPlatformWorkflow(): PlatformWorkflow | undefined {\n  // globalThis may not have the tailor property at runtime\n  // oxlint-disable-next-line typescript/no-unnecessary-condition\n  return (globalThis as GlobalWithRegistry).tailor?.workflow;\n}\n\nfunction requirePlatformWorkflow(): PlatformWorkflow {\n  const workflow = currentPlatformWorkflow();\n  if (!workflow) {\n    throw new Error(\n      \"tailor.workflow is not available. Run tests in the `tailor-runtime` Vitest environment and use mockWorkflow(), or use runWorkflowLocally() from @tailor-platform/sdk/vitest for local workflow execution.\",\n    );\n  }\n  return workflow;\n}\n\n// A valid placeholder UUID, so callers that validate the execution id behave the\n// same locally as against the platform.\nexport const START_DEFAULT = \"00000000-0000-4000-8000-000000000000\";\n\n// `.start()` routes through the installed `tailor.workflow` shim. Local body\n// execution is intentionally available only through `runWorkflowLocally()`.\n// Preserve arity: the shim sees a 2-argument call when the caller supplied no\n// options, and a 3-argument call otherwise, mirroring the bundler rewrite so\n// mocks observe the same shape in local execution and in bundled workflows.\nexport function dispatchStartJob(\n  name: string,\n  args?: unknown,\n  options?: ExecJobFunctionOptions,\n): unknown {\n  const workflow = requirePlatformWorkflow();\n  // oxlint-disable-next-line prefer-rest-params\n  return arguments.length >= 3\n    ? workflow.execJobFunction(name, args, options)\n    : workflow.execJobFunction(name, args);\n}\n\n// Accepts `unknown` because the SDK-side `.start()` accepts a wider options\n// shape than the platform surface (e.g. `authInvoker` may be a machine-user\n// name string that the bundler normalizes at build time). Local execution\n// forwards the value verbatim; only the bundled path enforces the platform\n// contract.\nexport function dispatchStartWorkflow(\n  name: string,\n  args?: unknown,\n  options?: { invoker?: unknown },\n): Promise<string> {\n  const workflow = requirePlatformWorkflow();\n  // oxlint-disable-next-line prefer-rest-params\n  if (arguments.length < 3) {\n    return workflow.startWorkflow(name, args);\n  }\n  return workflow.startWorkflow(\n    name,\n    args,\n    options?.invoker === undefined\n      ? (options as StartWorkflowOptions | undefined)\n      : ({ authInvoker: options.invoker } as StartWorkflowOptions),\n  );\n}\n"],"mappings":"AAaA,MAAM,EAAkC,OAAO,IAAI,kCAAkC,EAYrF,SAAS,MAAuC,CAC9C,IAAM,EAAI,WACN,EAAM,EAAE,GAKZ,OAJK,IACH,EAAM,IAAI,IACV,EAAE,GAAoB,GAEjB,CACT,CAYA,SAAgB,YAAY,EAAc,EAA+B,CACvE,KAAK,CAAC,CAAC,IAAI,EAAM,CAAI,CACvB,CAOA,SAAgB,iBAAiB,EAA6C,CAC5E,OAAO,KAAK,CAAC,CAAC,IAAI,CAAI,CACxB,CAEA,SAAS,yBAAwD,CAG/D,OAAQ,WAAkC,QAAQ,QACpD,CAEA,SAAS,yBAA4C,CACnD,IAAM,EAAW,wBAAwB,EACzC,GAAI,CAAC,EACH,MAAU,MACR,2MACF,EAEF,OAAO,CACT,CAIA,MAAa,EAAgB,uCAO7B,SAAgB,iBACd,EACA,EACA,EACS,CACT,IAAM,EAAW,wBAAwB,EAEzC,OAAO,UAAU,QAAU,EACvB,EAAS,gBAAgB,EAAM,EAAM,CAAO,EAC5C,EAAS,gBAAgB,EAAM,CAAI,CACzC,CAOA,SAAgB,sBACd,EACA,EACA,EACiB,CACjB,IAAM,EAAW,wBAAwB,EAKzC,OAHI,UAAU,OAAS,EACd,EAAS,cAAc,EAAM,CAAI,EAEnC,EAAS,cACd,EACA,EACA,GAAS,UAAY,IAAA,GAChB,EACA,CAAE,YAAa,EAAQ,OAAQ,CACtC,CACF"}