{"version":3,"file":"router.mjs","names":[],"sources":["../../../../../../../ai/src/workflow/router.ts"],"sourcesContent":["import type { NextStepResult } from \"../contracts/workflow/next-step-result.type\";\nimport type { StepDefinition } from \"../contracts/workflow/step.contract\";\nimport type { WorkflowContext } from \"../contracts/workflow/workflow-context.type\";\nimport type { WorkflowDefinition } from \"../contracts/workflow/workflow.contract\";\nimport { RoutingError } from \"../errors\";\n\n/**\n * Resolve the next step to run after `step` completes. Tries\n * step-level `nextStep` first, then workflow-level, then falls\n * through (returns `undefined`, engine picks the next declared step).\n *\n * Throws `RoutingError` when either callback throws — routing is\n * authoritative, so a broken router terminates the workflow instead\n * of being retried.\n */\nexport async function resolveNextStep<T>(params: {\n  step: StepDefinition;\n  definition: WorkflowDefinition<any, T, any, any>;\n  ctx: WorkflowContext;\n}): Promise<\"end\" | string | undefined> {\n  const { step, definition, ctx } = params;\n\n  if (step.nextStep) {\n    let outcome: NextStepResult;\n    try {\n      outcome = await step.nextStep(ctx);\n    } catch (err) {\n      throw new RoutingError(\n        `workflow \"${definition.name}\": step \"${step.name}\" nextStep threw`,\n        { stepName: step.name, cause: err },\n      );\n    }\n    const mapped = mapNextStep(outcome);\n    if (mapped !== undefined) return mapped;\n  }\n\n  if (definition.nextStep) {\n    let outcome: NextStepResult;\n    try {\n      outcome = await definition.nextStep(step.name, ctx);\n    } catch (err) {\n      throw new RoutingError(\n        `workflow \"${definition.name}\": workflow-level nextStep threw after \"${step.name}\"`,\n        { stepName: step.name, cause: err },\n      );\n    }\n    const mapped = mapNextStep(outcome);\n    if (mapped !== undefined) return mapped;\n  }\n\n  return undefined;\n}\n\nexport function mapNextStep(\n  outcome: NextStepResult,\n): \"end\" | string | undefined {\n  if (!outcome) return undefined;\n  if (\"end\" in outcome && outcome.end === true) return \"end\";\n  if (\"goto\" in outcome && typeof outcome.goto === \"string\")\n    return outcome.goto;\n  return undefined;\n}\n\nexport function nextDeclaredStep<T>(\n  definition: WorkflowDefinition<any, T, any, any>,\n  currentName: string,\n): string | null {\n  const idx = definition.steps.findIndex(s => s.name === currentName);\n  if (idx === -1) return null;\n  return definition.steps[idx + 1]?.name ?? null;\n}\n"],"mappings":";;;;;;;;;;;;;AAeA,eAAsB,gBAAmB,QAID;CACtC,MAAM,EAAE,MAAM,YAAY,QAAQ;CAElC,IAAI,KAAK,UAAU;EACjB,IAAI;EACJ,IAAI;GACF,UAAU,MAAM,KAAK,SAAS,GAAG;EACnC,SAAS,KAAK;GACZ,MAAM,IAAI,aACR,aAAa,WAAW,KAAK,WAAW,KAAK,KAAK,mBAClD;IAAE,UAAU,KAAK;IAAM,OAAO;GAAI,CACpC;EACF;EACA,MAAM,SAAS,YAAY,OAAO;EAClC,IAAI,WAAW,QAAW,OAAO;CACnC;CAEA,IAAI,WAAW,UAAU;EACvB,IAAI;EACJ,IAAI;GACF,UAAU,MAAM,WAAW,SAAS,KAAK,MAAM,GAAG;EACpD,SAAS,KAAK;GACZ,MAAM,IAAI,aACR,aAAa,WAAW,KAAK,0CAA0C,KAAK,KAAK,IACjF;IAAE,UAAU,KAAK;IAAM,OAAO;GAAI,CACpC;EACF;EACA,MAAM,SAAS,YAAY,OAAO;EAClC,IAAI,WAAW,QAAW,OAAO;CACnC;AAGF;AAEA,SAAgB,YACd,SAC4B;CAC5B,IAAI,CAAC,SAAS,OAAO;CACrB,IAAI,SAAS,WAAW,QAAQ,QAAQ,MAAM,OAAO;CACrD,IAAI,UAAU,WAAW,OAAO,QAAQ,SAAS,UAC/C,OAAO,QAAQ;AAEnB;AAEA,SAAgB,iBACd,YACA,aACe;CACf,MAAM,MAAM,WAAW,MAAM,WAAU,MAAK,EAAE,SAAS,WAAW;CAClE,IAAI,QAAQ,IAAI,OAAO;CACvB,OAAO,WAAW,MAAM,MAAM,EAAE,EAAE,QAAQ;AAC5C"}