{"version":3,"file":"restore-checkpoint.cjs","sources":["@gensx/core/../../../../src/restore-checkpoint.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-unnecessary-type-parameters */\nimport { ExecutionNode } from \"./checkpoint-types.js\";\nimport { Component } from \"./component.js\";\nimport { getCurrentContext } from \"./context.js\";\n\n// This is a magical component used to store a marker in the checkpoint. When restoring from a checkpoint, the execution layer will update this marker in the checkpoint with the given feedback,\n// remove all subsequent nodes, and resume execution from this point.\nconst CheckpointMarkerComponent = Component(\n  `CheckpointMarker`,\n  ({ maxRestores }: { maxRestores: number }) => {\n    const context = getCurrentContext();\n    const currentNode = context.getCurrentNode();\n    if (!currentNode) {\n      throw new Error(`[GenSX] No current node id found.`);\n    }\n\n    return {\n      feedback: null,\n      restoreCount: 0,\n      maxRestores,\n      node: currentNode,\n    };\n  },\n);\n\nexport function createCheckpoint<T = unknown>(\n  {\n    label,\n    // schema,\n  }: {\n    label?: string;\n    // schema: z.ZodSchema<T> // TODO: Validate the given feedback against the schema\n  } = {},\n  { maxRestores = 3 }: { maxRestores?: number } = {},\n): {\n  feedback: T | null;\n  restore: (feedback: T) => Promise<void>;\n  label: string;\n} {\n  const context = getCurrentContext();\n  const workflowContext = context.getWorkflowContext();\n\n  label ??= `checkpoint-marker-${Date.now()}`;\n\n  if (workflowContext.checkpointLabelMap.has(label)) {\n    throw new Error(`[GenSX] Checkpoint ${label} has already been created.`);\n  }\n  // Do not pass the label as a prop, as that would affect that ability to deterministically calculate the nodeId.\n  const result = CheckpointMarkerComponent(\n    { maxRestores },\n    {\n      metadata: { label, maxRestores },\n    },\n  );\n  workflowContext.checkpointLabelMap.set(label, result.node);\n\n  if (result.restoreCount >= result.maxRestores) {\n    throw new Error(\n      `[GenSX] Checkpoint ${label} has been restored more than ${result.maxRestores} times.`,\n    );\n  }\n\n  return {\n    feedback: result.feedback as T | null,\n    restore: (feedback: T) => restoreCheckpointByNode(result.node, feedback),\n    label,\n  };\n}\n\nasync function restoreCheckpointByNode(node: ExecutionNode, feedback: unknown) {\n  const context = getCurrentContext();\n  const workflowContext = context.getWorkflowContext();\n\n  await workflowContext.checkpointManager.waitForPendingUpdates();\n\n  // This is where the magic happens. The execution layer will halt execution and update the checkpoint with the given feedback.\n  await workflowContext.onRestoreCheckpoint(node, feedback);\n\n  console.error(\n    `[GenSX] Restoring checkpoints is not supported in this environment.`,\n  );\n}\n\nexport async function restoreCheckpoint<T = unknown>(\n  label: string,\n  feedback: T,\n) {\n  // TODO: Add some locking mechanism to prevent multiple simultaneous restores, or for other workflow work to happen while we're restoring.\n  const context = getCurrentContext();\n  const workflowContext = context.getWorkflowContext();\n  const node = workflowContext.checkpointLabelMap.get(label);\n  if (!node) {\n    throw new Error(`[GenSX] Checkpoint ${label} has not been created.`);\n  }\n\n  await restoreCheckpointByNode(node, feedback);\n}\n"],"names":["Component","context","getCurrentContext"],"mappings":";;;;;;;;;;;AAKA;AACA;AACA,MAAM,yBAAyB,GAAGA,mBAAS,CACzC,CAAA,gBAAA,CAAkB,EAClB,CAAC,EAAE,WAAW,EAA2B,KAAI;AAC3C,IAAA,MAAMC,SAAO,GAAGC,yBAAiB,EAAE;AACnC,IAAA,MAAM,WAAW,GAAGD,SAAO,CAAC,cAAc,EAAE;IAC5C,IAAI,CAAC,WAAW,EAAE;AAChB,QAAA,MAAM,IAAI,KAAK,CAAC,CAAA,iCAAA,CAAmC,CAAC;;IAGtD,OAAO;AACL,QAAA,QAAQ,EAAE,IAAI;AACd,QAAA,YAAY,EAAE,CAAC;QACf,WAAW;AACX,QAAA,IAAI,EAAE,WAAW;KAClB;AACH,CAAC,CACF;AAEe,SAAA,gBAAgB,CAC9B,EACE,KAAK;AACL;AAIE,EAAA,GAAA,EAAE,EACN,EAAE,WAAW,GAAG,CAAC,KAA+B,EAAE,EAAA;AAMlD,IAAA,MAAMA,SAAO,GAAGC,yBAAiB,EAAE;AACnC,IAAA,MAAM,eAAe,GAAGD,SAAO,CAAC,kBAAkB,EAAE;AAEpD,IAA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