{"version":3,"file":"request-input.cjs","sources":["@gensx/core/../../../../src/request-input.ts"],"sourcesContent":["import { Component } from \"./component.js\";\nimport { getCurrentContext } from \"./context.js\";\nimport { InferZodType, toJsonSchema, ZodTypeAny } from \"./zod.js\";\n\nfunction getCallbackUrl(nodeId: string) {\n  return `${process.env.GENSX_API_BASE_URL}/org/${process.env.GENSX_ORG}/workflowExecutions/${process.env.GENSX_EXECUTION_ID}/fulfill/${nodeId}`;\n}\n\nexport async function requestInput<T extends ZodTypeAny>(\n  trigger: (callbackUrl: string) => Promise<void>,\n  resultSchema: T,\n  {\n    timeoutAt,\n    timeoutMs,\n  }:\n    | {\n        timeoutAt?: Date;\n        timeoutMs?: never;\n      }\n    | {\n        timeoutMs?: number;\n        timeoutAt?: never;\n      } = {},\n): Promise<InferZodType<T> | { error: string; type: \"timeout\" | \"error\" }> {\n  const resultJsonSchema = toJsonSchema(resultSchema);\n\n  // TODO: We should do some locking here to prevent multiple simultaneous requestInput calls.\n  const TriggerComponent = Component(\n    \"RequestInputTrigger\",\n    async ({ nodeId }: { nodeId: string }) => {\n      await trigger(getCallbackUrl(nodeId));\n    },\n  );\n\n  // This is a magical component that, upon resume, will have the expected output in the checkpoint, filled in by the cloud runtime when the /fulfill endpoint is called.\n  // We define this inside the requestInput function so that it can reference the trigger function _without_ it being passed in as an argument.\n  const RequestInputComponent = Component(\"RequestInput\", async () => {\n    const context = getCurrentContext();\n    const workflowContext = context.getWorkflowContext();\n    const currentNodeId = context.getCurrentNode()?.id;\n    if (!currentNodeId) {\n      throw new Error(\"No current node ID found\");\n    }\n    await TriggerComponent({ nodeId: currentNodeId });\n\n    // Ensure that the we have flushed all pending updates to the server.\n    await workflowContext.checkpointManager.waitForPendingUpdates();\n\n    if (timeoutMs !== undefined) {\n      timeoutAt = new Date(Date.now() + timeoutMs);\n    }\n\n    // This is where the magic happens 🪄\n    return await workflowContext.onRequestInput({\n      type: \"input-request\",\n      nodeId: currentNodeId,\n      resultSchema: resultJsonSchema,\n      timeoutAt: timeoutAt?.toISOString() ?? null,\n    });\n  });\n\n  // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n  return (await RequestInputComponent()) as\n    | InferZodType<T>\n    | { error: string; type: \"timeout\" | \"error\" };\n}\n"],"names":["toJsonSchema","Component","context","getCurrentContext"],"mappings":";;;;;;;;;;;;AAIA,SAAS,cAAc,CAAC,MAAc,EAAA;IACpC,OAAO,CAAA,EAAG,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAA,KAAA,EAAQ,OAAO,CAAC,GAAG,CAAC,SAAS,CAAA,oBAAA,EAAuB,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAA,SAAA,EAAY,MAAM,CAAA,CAAE;AAChJ;AAEO,eAAe,YAAY,CAChC,OAA+C,EAC/C,YAAe,EACf,EACE,SAAS,EACT,SAAS,MASH,EAAE,EAAA;AAEV,IAAA,MAAM,gBAAgB,GAAGA,gBAAY,CAAC,YAAY,CAAC;;AAGnD,IAAA,MAAM,gBAAgB,GAAGC,mBAAS,CAChC,qBAAqB,EACrB,OAAO,EAAE,MAAM,EAAsB,KAAI;AACvC,QAAA,MAAM,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;AACvC,KAAC,CACF;;;IAID,MAAM,qBAAqB,GAAGA,mBAAS,CAAC,cAAc,EAAE,YAAW;AACjE,QAAA,MAAMC,SAAO,GAAGC,yBAAiB,EAAE;AACnC,QAAA,MAAM,eAAe,GAAGD,SAAO,CAAC,kBAAkB,EAAE;QACpD,MAAM,aAAa,GAAGA,SAAO,CAAC,cAAc,EAAE,EAAE,EAAE;QAClD,IAAI,CAAC,aAAa,EAAE;AAClB,YAAA,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC;;QAE7C,MAAM,gBAAgB,CAAC,EAAE,MAAM,EAAE,aAAa,EAAE,CAAC;;AAGjD,QAAA,MAAM,eAAe,CAAC,iBAAiB,CAAC,qBAAqB,EAAE;AAE/D,QAAA,IAAI,SAAS,KAAK,SA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