{"version":3,"file":"state.mjs","names":[],"sources":["../../../../../../../ai/src/workflow/state.ts"],"sourcesContent":["import { WorkflowError } from \"../errors\";\n\n/**\n * Deep-clone workflow state. Uses `structuredClone` — handles Date,\n * Map, Set, ArrayBuffer, nested objects, arrays, primitives. Throws\n * a typed `WorkflowError` on older runtimes (pre-Node-17) rather\n * than silently falling back to a JSON round-trip that would drop\n * non-serializable values like Dates without warning.\n *\n * Workflow state should stay serializable anyway (it round-trips\n * through `KVStore` on every checkpoint). If `structuredClone` chokes\n * on a value, that's a bug in the user's state — surface it.\n */\nexport function cloneState<T>(value: T): T {\n  if (typeof structuredClone !== \"function\") {\n    throw new WorkflowError(\n      \"workflow state cloning requires `structuredClone` (Node 17+ or a modern browser)\",\n    );\n  }\n\n  return structuredClone(value);\n}\n\n/**\n * Recursively freeze `value` and every nested plain object / array so\n * consumers of `ctx.steps[x].state` or `report.state` can't mutate\n * historical snapshots. Already-frozen values are skipped.\n */\nexport function deepFreeze<T>(value: T): T {\n  if (value === null || typeof value !== \"object\") return value;\n  if (Object.isFrozen(value)) return value;\n\n  for (const key of Object.keys(value as Record<string, unknown>)) {\n    const child = (value as Record<string, unknown>)[key];\n    if (child && typeof child === \"object\") deepFreeze(child);\n  }\n\n  return Object.freeze(value);\n}\n"],"mappings":";;;;;;;;;;;;;;;AAaA,SAAgB,WAAc,OAAa;CACzC,IAAI,OAAO,oBAAoB,YAC7B,MAAM,IAAI,cACR,kFACF;CAGF,OAAO,gBAAgB,KAAK;AAC9B;;;;;;AAOA,SAAgB,WAAc,OAAa;CACzC,IAAI,UAAU,QAAQ,OAAO,UAAU,UAAU,OAAO;CACxD,IAAI,OAAO,SAAS,KAAK,GAAG,OAAO;CAEnC,KAAK,MAAM,OAAO,OAAO,KAAK,KAAgC,GAAG;EAC/D,MAAM,QAAS,MAAkC;EACjD,IAAI,SAAS,OAAO,UAAU,UAAU,WAAW,KAAK;CAC1D;CAEA,OAAO,OAAO,OAAO,KAAK;AAC5B"}