rivetkit 2.0.2 → 2.0.4-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -5
- package/dist/browser/client.d.ts +2485 -0
- package/dist/browser/client.js +5182 -0
- package/dist/browser/client.js.map +1 -0
- package/dist/browser/inspector/client.d.ts +130 -0
- package/dist/browser/inspector/client.js +2854 -0
- package/dist/browser/inspector/client.js.map +1 -0
- package/dist/browser/v3-DnYObHH3.d.ts +279 -0
- package/dist/inspector.tar.gz +0 -0
- package/dist/schemas/actor-inspector/v1.ts +784 -0
- package/dist/schemas/actor-inspector/v2.ts +796 -0
- package/dist/schemas/actor-inspector/v3.ts +899 -0
- package/dist/schemas/actor-persist/v1.ts +225 -0
- package/dist/schemas/actor-persist/v2.ts +268 -0
- package/dist/schemas/actor-persist/v3.ts +280 -0
- package/dist/schemas/actor-persist/v4.ts +406 -0
- package/dist/schemas/client-protocol/v1.ts +441 -0
- package/dist/schemas/client-protocol/v2.ts +438 -0
- package/dist/schemas/client-protocol/v3.ts +554 -0
- package/dist/schemas/file-system-driver/v1.ts +108 -0
- package/dist/schemas/file-system-driver/v2.ts +142 -0
- package/dist/schemas/file-system-driver/v3.ts +167 -0
- package/dist/schemas/persist/v1.ts +781 -0
- package/dist/schemas/transport/v1.ts +697 -0
- package/dist/tsup/actor/errors.cjs +106 -0
- package/dist/tsup/actor/errors.cjs.map +1 -0
- package/dist/tsup/actor/errors.d.cts +188 -0
- package/dist/tsup/actor/errors.d.ts +188 -0
- package/dist/tsup/actor/errors.js +106 -0
- package/dist/tsup/actor/errors.js.map +1 -0
- package/dist/tsup/actor-router-consts-D29T1Z-K.d.cts +24 -0
- package/dist/tsup/actor-router-consts-D29T1Z-K.d.ts +24 -0
- package/dist/tsup/chunk-325TLXJT.js +1060 -0
- package/dist/tsup/chunk-325TLXJT.js.map +1 -0
- package/dist/tsup/chunk-424PT5DM.js +23 -0
- package/dist/tsup/chunk-424PT5DM.js.map +1 -0
- package/dist/tsup/chunk-4JVIG3SS.cjs +6289 -0
- package/dist/tsup/chunk-4JVIG3SS.cjs.map +1 -0
- package/dist/tsup/chunk-6LJAZ5R4.cjs +96 -0
- package/dist/tsup/chunk-6LJAZ5R4.cjs.map +1 -0
- package/dist/tsup/chunk-6XU3FMCB.cjs +534 -0
- package/dist/tsup/chunk-6XU3FMCB.cjs.map +1 -0
- package/dist/tsup/chunk-7HTNH26M.js +509 -0
- package/dist/tsup/chunk-7HTNH26M.js.map +1 -0
- package/dist/tsup/chunk-AUVH72RE.cjs +5977 -0
- package/dist/tsup/chunk-AUVH72RE.cjs.map +1 -0
- package/dist/tsup/chunk-D4BYUPNQ.js +645 -0
- package/dist/tsup/chunk-D4BYUPNQ.js.map +1 -0
- package/dist/tsup/chunk-HDQ2JUQT.cjs +23 -0
- package/dist/tsup/chunk-HDQ2JUQT.cjs.map +1 -0
- package/dist/tsup/chunk-HHXX2VRM.js +6289 -0
- package/dist/tsup/chunk-HHXX2VRM.js.map +1 -0
- package/dist/tsup/chunk-JEAEA2PB.js +49 -0
- package/dist/tsup/chunk-JEAEA2PB.js.map +1 -0
- package/dist/tsup/chunk-JYSEG3VF.cjs +642 -0
- package/dist/tsup/chunk-JYSEG3VF.cjs.map +1 -0
- package/dist/tsup/chunk-K6DGYILQ.js +2657 -0
- package/dist/tsup/chunk-K6DGYILQ.js.map +1 -0
- package/dist/tsup/chunk-KJSYAUOM.js +96 -0
- package/dist/tsup/chunk-KJSYAUOM.js.map +1 -0
- package/dist/tsup/chunk-L47L3ZWJ.cjs +509 -0
- package/dist/tsup/chunk-L47L3ZWJ.cjs.map +1 -0
- package/dist/tsup/chunk-LXUQ667X.js +2006 -0
- package/dist/tsup/chunk-LXUQ667X.js.map +1 -0
- package/dist/tsup/chunk-MXNPAB5W.js +5977 -0
- package/dist/tsup/chunk-MXNPAB5W.js.map +1 -0
- package/dist/tsup/chunk-N4KRDJ56.js +72 -0
- package/dist/tsup/chunk-N4KRDJ56.js.map +1 -0
- package/dist/tsup/chunk-NIYZDWMW.cjs +2006 -0
- package/dist/tsup/chunk-NIYZDWMW.cjs.map +1 -0
- package/dist/tsup/chunk-PQZHDKRW.cjs +1060 -0
- package/dist/tsup/chunk-PQZHDKRW.cjs.map +1 -0
- package/dist/tsup/chunk-PVOE6BU7.cjs +1050 -0
- package/dist/tsup/chunk-PVOE6BU7.cjs.map +1 -0
- package/dist/tsup/chunk-Q4UD2GA4.cjs +1810 -0
- package/dist/tsup/chunk-Q4UD2GA4.cjs.map +1 -0
- package/dist/tsup/chunk-QUD664YZ.js +1810 -0
- package/dist/tsup/chunk-QUD664YZ.js.map +1 -0
- package/dist/tsup/chunk-RTOCTWME.js +1050 -0
- package/dist/tsup/chunk-RTOCTWME.js.map +1 -0
- package/dist/tsup/chunk-SAZZ4SB2.cjs +2657 -0
- package/dist/tsup/chunk-SAZZ4SB2.cjs.map +1 -0
- package/dist/tsup/chunk-SR3KQE7Q.cjs +72 -0
- package/dist/tsup/chunk-SR3KQE7Q.cjs.map +1 -0
- package/dist/tsup/chunk-V2GHLYC6.cjs +49 -0
- package/dist/tsup/chunk-V2GHLYC6.cjs.map +1 -0
- package/dist/tsup/chunk-V3WG7XTW.cjs +645 -0
- package/dist/tsup/chunk-V3WG7XTW.cjs.map +1 -0
- package/dist/tsup/chunk-VKVNIQRQ.js +257 -0
- package/dist/tsup/chunk-VKVNIQRQ.js.map +1 -0
- package/dist/tsup/chunk-WMPW7JYC.js +642 -0
- package/dist/tsup/chunk-WMPW7JYC.js.map +1 -0
- package/dist/tsup/chunk-Z7HNQ2WF.js +534 -0
- package/dist/tsup/chunk-Z7HNQ2WF.js.map +1 -0
- package/dist/tsup/chunk-ZFY5J2EP.cjs +257 -0
- package/dist/tsup/chunk-ZFY5J2EP.cjs.map +1 -0
- package/dist/tsup/client/mod.cjs +33 -0
- package/dist/tsup/client/mod.cjs.map +1 -0
- package/dist/tsup/client/mod.d.cts +64 -0
- package/dist/tsup/client/mod.d.ts +64 -0
- package/dist/tsup/client/mod.js +33 -0
- package/dist/tsup/client/mod.js.map +1 -0
- package/dist/tsup/common/log.cjs +21 -0
- package/dist/tsup/common/log.cjs.map +1 -0
- package/dist/tsup/common/log.d.cts +34 -0
- package/dist/tsup/common/log.d.ts +34 -0
- package/dist/tsup/common/log.js +21 -0
- package/dist/tsup/common/log.js.map +1 -0
- package/dist/tsup/common/websocket.cjs +10 -0
- package/dist/tsup/common/websocket.cjs.map +1 -0
- package/dist/tsup/common/websocket.d.cts +3 -0
- package/dist/tsup/common/websocket.d.ts +3 -0
- package/dist/tsup/common/websocket.js +10 -0
- package/dist/tsup/common/websocket.js.map +1 -0
- package/dist/tsup/config-BiNoIHRs.d.cts +80 -0
- package/dist/tsup/config-BiNoIHRs.d.ts +80 -0
- package/dist/tsup/config-P3XujgRr.d.ts +2594 -0
- package/dist/tsup/config-_gfywqqI.d.cts +2594 -0
- package/dist/tsup/context-Bxd8Cx4H.d.cts +75 -0
- package/dist/tsup/context-uNA4TRn3.d.ts +75 -0
- package/dist/tsup/db/drizzle/mod.cjs +49 -0
- package/dist/tsup/db/drizzle/mod.cjs.map +1 -0
- package/dist/tsup/db/drizzle/mod.d.cts +17 -0
- package/dist/tsup/db/drizzle/mod.d.ts +17 -0
- package/dist/tsup/db/drizzle/mod.js +49 -0
- package/dist/tsup/db/drizzle/mod.js.map +1 -0
- package/dist/tsup/db/mod.cjs +9 -0
- package/dist/tsup/db/mod.cjs.map +1 -0
- package/dist/tsup/db/mod.d.cts +9 -0
- package/dist/tsup/db/mod.d.ts +9 -0
- package/dist/tsup/db/mod.js +9 -0
- package/dist/tsup/db/mod.js.map +1 -0
- package/dist/tsup/driver-BcLvZcKl.d.cts +13 -0
- package/dist/tsup/driver-CPGHKXyh.d.ts +13 -0
- package/dist/tsup/driver-helpers/mod.cjs +53 -0
- package/dist/tsup/driver-helpers/mod.cjs.map +1 -0
- package/dist/tsup/driver-helpers/mod.d.cts +47 -0
- package/dist/tsup/driver-helpers/mod.d.ts +47 -0
- package/dist/tsup/driver-helpers/mod.js +53 -0
- package/dist/tsup/driver-helpers/mod.js.map +1 -0
- package/dist/tsup/driver-test-suite/mod.cjs +4974 -0
- package/dist/tsup/driver-test-suite/mod.cjs.map +1 -0
- package/dist/tsup/driver-test-suite/mod.d.cts +73 -0
- package/dist/tsup/driver-test-suite/mod.d.ts +73 -0
- package/dist/tsup/driver-test-suite/mod.js +4974 -0
- package/dist/tsup/driver-test-suite/mod.js.map +1 -0
- package/dist/tsup/inspector/mod.cjs +164 -0
- package/dist/tsup/inspector/mod.cjs.map +1 -0
- package/dist/tsup/inspector/mod.d.cts +130 -0
- package/dist/tsup/inspector/mod.d.ts +130 -0
- package/dist/tsup/inspector/mod.js +164 -0
- package/dist/tsup/inspector/mod.js.map +1 -0
- package/dist/tsup/keys-CydblqMh.d.cts +13 -0
- package/dist/tsup/keys-CydblqMh.d.ts +13 -0
- package/dist/tsup/mod.cjs +82 -0
- package/dist/tsup/mod.cjs.map +1 -0
- package/dist/tsup/mod.d.cts +126 -0
- package/dist/tsup/mod.d.ts +126 -0
- package/dist/tsup/mod.js +82 -0
- package/dist/tsup/mod.js.map +1 -0
- package/dist/tsup/serve-test-suite/mod.cjs +2601 -0
- package/dist/tsup/serve-test-suite/mod.cjs.map +1 -0
- package/dist/tsup/serve-test-suite/mod.d.cts +9 -0
- package/dist/tsup/serve-test-suite/mod.d.ts +9 -0
- package/dist/tsup/serve-test-suite/mod.js +2601 -0
- package/dist/tsup/serve-test-suite/mod.js.map +1 -0
- package/dist/tsup/test/mod.cjs +90 -0
- package/dist/tsup/test/mod.cjs.map +1 -0
- package/dist/tsup/test/mod.d.cts +26 -0
- package/dist/tsup/test/mod.d.ts +26 -0
- package/dist/tsup/test/mod.js +90 -0
- package/dist/tsup/test/mod.js.map +1 -0
- package/dist/tsup/utils-fwx3o3K9.d.cts +18 -0
- package/dist/tsup/utils-fwx3o3K9.d.ts +18 -0
- package/dist/tsup/utils.cjs +43 -0
- package/dist/tsup/utils.cjs.map +1 -0
- package/dist/tsup/utils.d.cts +148 -0
- package/dist/tsup/utils.d.ts +148 -0
- package/dist/tsup/utils.js +43 -0
- package/dist/tsup/utils.js.map +1 -0
- package/dist/tsup/v3-DnYObHH3.d.cts +279 -0
- package/dist/tsup/v3-DnYObHH3.d.ts +279 -0
- package/dist/tsup/workflow/mod.cjs +16 -0
- package/dist/tsup/workflow/mod.cjs.map +1 -0
- package/dist/tsup/workflow/mod.d.cts +25 -0
- package/dist/tsup/workflow/mod.d.ts +25 -0
- package/dist/tsup/workflow/mod.js +16 -0
- package/dist/tsup/workflow/mod.js.map +1 -0
- package/package.json +293 -5
- package/src/actor/config.ts +1221 -0
- package/src/actor/conn/driver.ts +61 -0
- package/src/actor/conn/drivers/http.ts +17 -0
- package/src/actor/conn/drivers/raw-request.ts +24 -0
- package/src/actor/conn/drivers/raw-websocket.ts +65 -0
- package/src/actor/conn/drivers/websocket.ts +144 -0
- package/src/actor/conn/mod.ts +288 -0
- package/src/actor/conn/persisted.ts +81 -0
- package/src/actor/conn/state-manager.ts +196 -0
- package/src/actor/contexts/action.ts +47 -0
- package/src/actor/contexts/base/actor.ts +347 -0
- package/src/actor/contexts/base/conn-init.ts +68 -0
- package/src/actor/contexts/base/conn.ts +73 -0
- package/src/actor/contexts/before-action-response.ts +42 -0
- package/src/actor/contexts/before-connect.ts +31 -0
- package/src/actor/contexts/connect.ts +42 -0
- package/src/actor/contexts/create-conn-state.ts +32 -0
- package/src/actor/contexts/create-vars.ts +39 -0
- package/src/actor/contexts/create.ts +39 -0
- package/src/actor/contexts/destroy.ts +42 -0
- package/src/actor/contexts/disconnect.ts +43 -0
- package/src/actor/contexts/index.ts +33 -0
- package/src/actor/contexts/request.ts +80 -0
- package/src/actor/contexts/run.ts +47 -0
- package/src/actor/contexts/sleep.ts +42 -0
- package/src/actor/contexts/state-change.ts +42 -0
- package/src/actor/contexts/wake.ts +42 -0
- package/src/actor/contexts/websocket.ts +80 -0
- package/src/actor/database.ts +13 -0
- package/src/actor/definition.ts +64 -0
- package/src/actor/driver.ts +114 -0
- package/src/actor/errors.ts +556 -0
- package/src/actor/instance/connection-manager.ts +574 -0
- package/src/actor/instance/event-manager.ts +314 -0
- package/src/actor/instance/keys.ts +146 -0
- package/src/actor/instance/kv.ts +241 -0
- package/src/actor/instance/mod.ts +1658 -0
- package/src/actor/instance/persisted.ts +67 -0
- package/src/actor/instance/queue-manager.ts +603 -0
- package/src/actor/instance/queue.ts +345 -0
- package/src/actor/instance/schedule-manager.ts +392 -0
- package/src/actor/instance/state-manager.ts +542 -0
- package/src/actor/instance/traces-driver.ts +128 -0
- package/src/actor/keys.test.ts +275 -0
- package/src/actor/keys.ts +89 -0
- package/src/actor/log.ts +6 -0
- package/src/actor/mod.ts +110 -0
- package/src/actor/protocol/old.ts +416 -0
- package/src/actor/protocol/serde.ts +222 -0
- package/src/actor/router-endpoints.ts +400 -0
- package/src/actor/router-websocket-endpoints.test.ts +54 -0
- package/src/actor/router-websocket-endpoints.ts +405 -0
- package/src/actor/router.ts +380 -0
- package/src/actor/schedule.ts +17 -0
- package/src/actor/schema.ts +291 -0
- package/src/actor/utils.test.ts +48 -0
- package/src/actor/utils.ts +158 -0
- package/src/client/actor-common.ts +32 -0
- package/src/client/actor-conn.ts +1262 -0
- package/src/client/actor-handle.ts +344 -0
- package/src/client/actor-query.ts +112 -0
- package/src/client/client.ts +558 -0
- package/src/client/config.ts +151 -0
- package/src/client/errors.ts +76 -0
- package/src/client/log.ts +5 -0
- package/src/client/mod.browser.ts +2 -0
- package/src/client/mod.ts +70 -0
- package/src/client/queue.ts +146 -0
- package/src/client/raw-utils.ts +149 -0
- package/src/client/test.ts +44 -0
- package/src/client/utils.ts +252 -0
- package/src/common/actor-router-consts.ts +59 -0
- package/src/common/cors.ts +57 -0
- package/src/common/eventsource-interface.ts +47 -0
- package/src/common/eventsource.ts +44 -0
- package/src/common/inline-websocket-adapter.ts +154 -0
- package/src/common/log-levels.ts +27 -0
- package/src/common/log.ts +229 -0
- package/src/common/logfmt.ts +221 -0
- package/src/common/network.ts +2 -0
- package/src/common/router.ts +174 -0
- package/src/common/utils.ts +339 -0
- package/src/common/websocket-interface.ts +7 -0
- package/src/common/websocket.ts +43 -0
- package/src/db/config.ts +100 -0
- package/src/db/drizzle/mod.ts +226 -0
- package/src/db/drizzle/sqlite-core.ts +22 -0
- package/src/db/mod.ts +125 -0
- package/src/db/shared.ts +92 -0
- package/src/db/sqlite-vfs.ts +12 -0
- package/src/devtools-loader/index.ts +33 -0
- package/src/devtools-loader/log.ts +5 -0
- package/src/driver-helpers/mod.ts +33 -0
- package/src/driver-helpers/utils.ts +54 -0
- package/src/driver-test-suite/log.ts +5 -0
- package/src/driver-test-suite/mod.ts +293 -0
- package/src/driver-test-suite/test-inline-client-driver.ts +307 -0
- package/src/driver-test-suite/tests/access-control.ts +218 -0
- package/src/driver-test-suite/tests/action-features.ts +203 -0
- package/src/driver-test-suite/tests/actor-conn-hibernation.ts +152 -0
- package/src/driver-test-suite/tests/actor-conn-state.ts +300 -0
- package/src/driver-test-suite/tests/actor-conn.ts +596 -0
- package/src/driver-test-suite/tests/actor-db-raw.ts +73 -0
- package/src/driver-test-suite/tests/actor-db.ts +477 -0
- package/src/driver-test-suite/tests/actor-destroy.ts +294 -0
- package/src/driver-test-suite/tests/actor-driver.ts +18 -0
- package/src/driver-test-suite/tests/actor-error-handling.ts +150 -0
- package/src/driver-test-suite/tests/actor-handle.ts +312 -0
- package/src/driver-test-suite/tests/actor-inline-client.ts +163 -0
- package/src/driver-test-suite/tests/actor-inspector.ts +264 -0
- package/src/driver-test-suite/tests/actor-kv.ts +65 -0
- package/src/driver-test-suite/tests/actor-metadata.ts +116 -0
- package/src/driver-test-suite/tests/actor-onstatechange.ts +95 -0
- package/src/driver-test-suite/tests/actor-queue.ts +325 -0
- package/src/driver-test-suite/tests/actor-run.ts +181 -0
- package/src/driver-test-suite/tests/actor-schedule.ts +97 -0
- package/src/driver-test-suite/tests/actor-sleep.ts +415 -0
- package/src/driver-test-suite/tests/actor-state.ts +54 -0
- package/src/driver-test-suite/tests/actor-stateless.ts +70 -0
- package/src/driver-test-suite/tests/actor-vars.ts +97 -0
- package/src/driver-test-suite/tests/actor-workflow.ts +118 -0
- package/src/driver-test-suite/tests/manager-driver.ts +388 -0
- package/src/driver-test-suite/tests/raw-http-direct-registry.ts +227 -0
- package/src/driver-test-suite/tests/raw-http-request-properties.ts +454 -0
- package/src/driver-test-suite/tests/raw-http.ts +359 -0
- package/src/driver-test-suite/tests/raw-websocket-direct-registry.ts +393 -0
- package/src/driver-test-suite/tests/raw-websocket.ts +513 -0
- package/src/driver-test-suite/tests/request-access.ts +240 -0
- package/src/driver-test-suite/utils.ts +80 -0
- package/src/drivers/default.ts +38 -0
- package/src/drivers/engine/actor-driver.ts +1027 -0
- package/src/drivers/engine/config.ts +43 -0
- package/src/drivers/engine/log.ts +5 -0
- package/src/drivers/engine/mod.ts +36 -0
- package/src/drivers/file-system/actor.ts +102 -0
- package/src/drivers/file-system/global-state.ts +1445 -0
- package/src/drivers/file-system/kv-limits.ts +70 -0
- package/src/drivers/file-system/log.ts +5 -0
- package/src/drivers/file-system/manager.ts +300 -0
- package/src/drivers/file-system/mod.ts +78 -0
- package/src/drivers/file-system/sqlite-runtime.ts +210 -0
- package/src/drivers/file-system/utils.ts +125 -0
- package/src/engine-process/constants.ts +2 -0
- package/src/engine-process/log.ts +5 -0
- package/src/engine-process/mod.ts +464 -0
- package/src/globals.d.ts +35 -0
- package/src/inspector/actor-inspector.ts +352 -0
- package/src/inspector/config.ts +49 -0
- package/src/inspector/handler.ts +273 -0
- package/src/inspector/log.ts +5 -0
- package/src/inspector/mod.browser.ts +8 -0
- package/src/inspector/mod.ts +4 -0
- package/src/inspector/serve-ui.ts +40 -0
- package/src/inspector/transport.ts +18 -0
- package/src/inspector/utils.ts +32 -0
- package/src/manager/driver.ts +106 -0
- package/src/manager/gateway.ts +668 -0
- package/src/manager/log.ts +5 -0
- package/src/manager/mod.ts +2 -0
- package/src/manager/protocol/mod.ts +22 -0
- package/src/manager/protocol/query.ts +85 -0
- package/src/manager/router-schema.ts +22 -0
- package/src/manager/router.ts +660 -0
- package/src/manager-api/actors.ts +83 -0
- package/src/manager-api/common.ts +4 -0
- package/src/mod.ts +24 -0
- package/src/registry/config/driver.ts +21 -0
- package/src/registry/config/index.ts +510 -0
- package/src/registry/config/legacy-runner.ts +157 -0
- package/src/registry/config/runner.ts +21 -0
- package/src/registry/config/serverless.ts +94 -0
- package/src/registry/index.ts +194 -0
- package/src/registry/log.ts +5 -0
- package/src/remote-manager-driver/actor-http-client.ts +84 -0
- package/src/remote-manager-driver/actor-websocket-client.ts +81 -0
- package/src/remote-manager-driver/api-endpoints.ts +159 -0
- package/src/remote-manager-driver/api-utils.ts +69 -0
- package/src/remote-manager-driver/log.ts +5 -0
- package/src/remote-manager-driver/metadata.ts +64 -0
- package/src/remote-manager-driver/mod.ts +414 -0
- package/src/remote-manager-driver/ws-proxy.ts +189 -0
- package/src/schemas/actor-inspector/mod.ts +1 -0
- package/src/schemas/actor-inspector/versioned.ts +233 -0
- package/src/schemas/actor-persist/mod.ts +1 -0
- package/src/schemas/actor-persist/versioned.ts +217 -0
- package/src/schemas/client-protocol/mod.ts +1 -0
- package/src/schemas/client-protocol/versioned.ts +330 -0
- package/src/schemas/client-protocol-zod/mod.ts +118 -0
- package/src/schemas/file-system-driver/mod.ts +1 -0
- package/src/schemas/file-system-driver/versioned.ts +135 -0
- package/src/schemas/persist/mod.ts +1 -0
- package/src/schemas/transport/mod.ts +1 -0
- package/src/serde.ts +138 -0
- package/src/serve-test-suite/mod.ts +148 -0
- package/src/serverless/configure.ts +82 -0
- package/src/serverless/log.ts +5 -0
- package/src/serverless/router.test.ts +299 -0
- package/src/serverless/router.ts +215 -0
- package/src/test/log.ts +5 -0
- package/src/test/mod.ts +99 -0
- package/src/utils/crypto.ts +24 -0
- package/src/utils/endpoint-parser.test.ts +202 -0
- package/src/utils/endpoint-parser.ts +124 -0
- package/src/utils/env-vars.ts +78 -0
- package/src/utils/node.ts +178 -0
- package/src/utils/router.ts +83 -0
- package/src/utils/serve.ts +212 -0
- package/src/utils.test.ts +34 -0
- package/src/utils.ts +437 -0
- package/src/workflow/constants.ts +2 -0
- package/src/workflow/context.ts +597 -0
- package/src/workflow/driver.ts +194 -0
- package/src/workflow/inspector.ts +268 -0
- package/src/workflow/mod.ts +128 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import type { RunContext } from "@/actor/contexts/run";
|
|
2
|
+
import type { AnyActorInstance } from "@/actor/instance/mod";
|
|
3
|
+
import { makeWorkflowKey, workflowStoragePrefix } from "@/actor/instance/keys";
|
|
4
|
+
import type {
|
|
5
|
+
EngineDriver,
|
|
6
|
+
KVEntry,
|
|
7
|
+
KVWrite,
|
|
8
|
+
Message,
|
|
9
|
+
WorkflowMessageDriver,
|
|
10
|
+
} from "@rivetkit/workflow-engine";
|
|
11
|
+
|
|
12
|
+
const WORKFLOW_STORAGE_PREFIX = workflowStoragePrefix();
|
|
13
|
+
|
|
14
|
+
function stripWorkflowKey(prefixed: Uint8Array): Uint8Array {
|
|
15
|
+
return prefixed.slice(WORKFLOW_STORAGE_PREFIX.length);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
class ActorWorkflowMessageDriver implements WorkflowMessageDriver {
|
|
19
|
+
#actor: AnyActorInstance;
|
|
20
|
+
#runCtx: RunContext<any, any, any, any, any, any, any, any>;
|
|
21
|
+
|
|
22
|
+
constructor(
|
|
23
|
+
actor: AnyActorInstance,
|
|
24
|
+
runCtx: RunContext<any, any, any, any, any, any, any, any>,
|
|
25
|
+
) {
|
|
26
|
+
this.#actor = actor;
|
|
27
|
+
this.#runCtx = runCtx;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async addMessage(message: Message): Promise<void> {
|
|
31
|
+
await this.#runCtx.keepAwake(
|
|
32
|
+
this.#actor.queueManager.enqueue(message.name, message.data),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async receiveMessages(opts: {
|
|
37
|
+
names?: readonly string[];
|
|
38
|
+
count: number;
|
|
39
|
+
completable: boolean;
|
|
40
|
+
}): Promise<Message[]> {
|
|
41
|
+
const messages = await this.#runCtx.keepAwake(
|
|
42
|
+
this.#actor.queueManager.receive(
|
|
43
|
+
opts.names && opts.names.length > 0 ? [...opts.names] : undefined,
|
|
44
|
+
opts.count,
|
|
45
|
+
0,
|
|
46
|
+
undefined,
|
|
47
|
+
opts.completable,
|
|
48
|
+
),
|
|
49
|
+
);
|
|
50
|
+
return messages.map((message) => ({
|
|
51
|
+
id: message.id.toString(),
|
|
52
|
+
name: message.name,
|
|
53
|
+
data: message.body,
|
|
54
|
+
sentAt: message.createdAt,
|
|
55
|
+
...(opts.completable
|
|
56
|
+
? {
|
|
57
|
+
complete: async (response?: unknown) => {
|
|
58
|
+
await this.#runCtx.keepAwake(
|
|
59
|
+
this.#actor.queueManager.completeMessage(
|
|
60
|
+
message,
|
|
61
|
+
response,
|
|
62
|
+
),
|
|
63
|
+
);
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
: {}),
|
|
67
|
+
}));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async completeMessage(messageId: string, response?: unknown): Promise<void> {
|
|
71
|
+
let parsedId: bigint;
|
|
72
|
+
try {
|
|
73
|
+
parsedId = BigInt(messageId);
|
|
74
|
+
} catch {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
await this.#runCtx.keepAwake(
|
|
79
|
+
this.#actor.queueManager.completeMessageById(parsedId, response),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export class ActorWorkflowDriver implements EngineDriver {
|
|
85
|
+
readonly workerPollInterval = 100;
|
|
86
|
+
readonly messageDriver: WorkflowMessageDriver;
|
|
87
|
+
#actor: AnyActorInstance;
|
|
88
|
+
#runCtx: RunContext<any, any, any, any, any, any, any, any>;
|
|
89
|
+
|
|
90
|
+
constructor(
|
|
91
|
+
actor: AnyActorInstance,
|
|
92
|
+
runCtx: RunContext<any, any, any, any, any, any, any, any>,
|
|
93
|
+
) {
|
|
94
|
+
this.#actor = actor;
|
|
95
|
+
this.#runCtx = runCtx;
|
|
96
|
+
this.messageDriver = new ActorWorkflowMessageDriver(actor, runCtx);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async get(key: Uint8Array): Promise<Uint8Array | null> {
|
|
100
|
+
const [value] = await this.#runCtx.keepAwake(
|
|
101
|
+
this.#actor.driver.kvBatchGet(this.#actor.id, [
|
|
102
|
+
makeWorkflowKey(key),
|
|
103
|
+
]),
|
|
104
|
+
);
|
|
105
|
+
return value ?? null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async set(key: Uint8Array, value: Uint8Array): Promise<void> {
|
|
109
|
+
await this.#runCtx.keepAwake(
|
|
110
|
+
this.#actor.driver.kvBatchPut(this.#actor.id, [
|
|
111
|
+
[makeWorkflowKey(key), value],
|
|
112
|
+
]),
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async delete(key: Uint8Array): Promise<void> {
|
|
117
|
+
await this.#runCtx.keepAwake(
|
|
118
|
+
this.#actor.driver.kvBatchDelete(this.#actor.id, [
|
|
119
|
+
makeWorkflowKey(key),
|
|
120
|
+
]),
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async deletePrefix(prefix: Uint8Array): Promise<void> {
|
|
125
|
+
const entries = await this.#runCtx.keepAwake(
|
|
126
|
+
this.#actor.driver.kvListPrefix(
|
|
127
|
+
this.#actor.id,
|
|
128
|
+
makeWorkflowKey(prefix),
|
|
129
|
+
),
|
|
130
|
+
);
|
|
131
|
+
if (entries.length === 0) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
await this.#runCtx.keepAwake(
|
|
135
|
+
this.#actor.driver.kvBatchDelete(
|
|
136
|
+
this.#actor.id,
|
|
137
|
+
entries.map(([key]) => key),
|
|
138
|
+
),
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async list(prefix: Uint8Array): Promise<KVEntry[]> {
|
|
143
|
+
const entries = await this.#runCtx.keepAwake(
|
|
144
|
+
this.#actor.driver.kvListPrefix(
|
|
145
|
+
this.#actor.id,
|
|
146
|
+
makeWorkflowKey(prefix),
|
|
147
|
+
),
|
|
148
|
+
);
|
|
149
|
+
return entries.map(([key, value]) => ({
|
|
150
|
+
key: stripWorkflowKey(key),
|
|
151
|
+
value,
|
|
152
|
+
}));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async batch(writes: KVWrite[]): Promise<void> {
|
|
156
|
+
if (writes.length === 0) return;
|
|
157
|
+
|
|
158
|
+
// Flush actor state together with workflow state to ensure atomicity.
|
|
159
|
+
// If the server crashes after workflow flush, actor state must also be persisted.
|
|
160
|
+
await this.#runCtx.keepAwake(
|
|
161
|
+
Promise.all([
|
|
162
|
+
this.#actor.driver.kvBatchPut(
|
|
163
|
+
this.#actor.id,
|
|
164
|
+
writes.map(({ key, value }) => [makeWorkflowKey(key), value]),
|
|
165
|
+
),
|
|
166
|
+
this.#actor.stateManager.saveState({
|
|
167
|
+
immediate: true,
|
|
168
|
+
allowStoppingState: true,
|
|
169
|
+
}),
|
|
170
|
+
]),
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async setAlarm(_workflowId: string, wakeAt: number): Promise<void> {
|
|
175
|
+
await this.#runCtx.keepAwake(
|
|
176
|
+
this.#actor.driver.setAlarm(this.#actor, wakeAt),
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async clearAlarm(_workflowId: string): Promise<void> {
|
|
181
|
+
// No dedicated clear alarm support in actor drivers.
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
waitForMessages(
|
|
186
|
+
messageNames: string[],
|
|
187
|
+
abortSignal: AbortSignal,
|
|
188
|
+
): Promise<void> {
|
|
189
|
+
return this.#actor.queueManager.waitForNames(
|
|
190
|
+
messageNames.length > 0 ? messageNames : undefined,
|
|
191
|
+
abortSignal,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import * as cbor from "cbor-x";
|
|
2
|
+
import { createNanoEvents } from "nanoevents";
|
|
3
|
+
import type {
|
|
4
|
+
BranchStatus,
|
|
5
|
+
BranchStatusType,
|
|
6
|
+
EntryKind,
|
|
7
|
+
EntryStatus,
|
|
8
|
+
Location,
|
|
9
|
+
SleepState,
|
|
10
|
+
WorkflowHistoryEntry,
|
|
11
|
+
WorkflowHistorySnapshot,
|
|
12
|
+
WorkflowEntryMetadataSnapshot,
|
|
13
|
+
} from "@rivetkit/workflow-engine";
|
|
14
|
+
import { encodeWorkflowHistoryTransport } from "@/inspector/transport";
|
|
15
|
+
import type * as inspectorSchema from "@/schemas/actor-inspector/mod";
|
|
16
|
+
import * as transport from "@/schemas/transport/mod";
|
|
17
|
+
import { assertUnreachable, bufferToArrayBuffer } from "@/utils";
|
|
18
|
+
|
|
19
|
+
export interface WorkflowInspectorAdapter {
|
|
20
|
+
getHistory: () => inspectorSchema.WorkflowHistory | null;
|
|
21
|
+
onHistoryUpdated: (
|
|
22
|
+
listener: (history: inspectorSchema.WorkflowHistory) => void,
|
|
23
|
+
) => () => void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function createWorkflowInspectorAdapter(): {
|
|
27
|
+
adapter: WorkflowInspectorAdapter;
|
|
28
|
+
update: (snapshot: WorkflowHistorySnapshot) => void;
|
|
29
|
+
} {
|
|
30
|
+
const emitter = createNanoEvents<{
|
|
31
|
+
updated: (history: inspectorSchema.WorkflowHistory) => void;
|
|
32
|
+
}>();
|
|
33
|
+
let history: inspectorSchema.WorkflowHistory | null = null;
|
|
34
|
+
|
|
35
|
+
const adapter: WorkflowInspectorAdapter = {
|
|
36
|
+
getHistory: () => history,
|
|
37
|
+
onHistoryUpdated: (listener) => emitter.on("updated", listener),
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const update = (snapshot: WorkflowHistorySnapshot) => {
|
|
41
|
+
const transportHistory = toWorkflowHistory(snapshot);
|
|
42
|
+
const next = encodeWorkflowHistoryTransport(transportHistory);
|
|
43
|
+
history = next;
|
|
44
|
+
emitter.emit("updated", next);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
return { adapter, update };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function encodeCbor(value: unknown): ArrayBuffer {
|
|
51
|
+
return bufferToArrayBuffer(cbor.encode(value));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function encodeOptionalCbor(value: unknown): ArrayBuffer | null {
|
|
55
|
+
if (value === undefined) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
return encodeCbor(value);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function toU64(value: number): bigint {
|
|
62
|
+
return BigInt(Math.max(0, Math.floor(value)));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function toWorkflowLocation(
|
|
66
|
+
location: Location,
|
|
67
|
+
): transport.WorkflowLocation {
|
|
68
|
+
return location.map((segment) => {
|
|
69
|
+
if (typeof segment === "number") {
|
|
70
|
+
return { tag: "WorkflowNameIndex", val: segment };
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
tag: "WorkflowLoopIterationMarker",
|
|
74
|
+
val: {
|
|
75
|
+
loop: segment.loop,
|
|
76
|
+
iteration: segment.iteration,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function toWorkflowEntryKind(
|
|
83
|
+
kind: EntryKind,
|
|
84
|
+
): transport.WorkflowEntryKind {
|
|
85
|
+
switch (kind.type) {
|
|
86
|
+
case "step":
|
|
87
|
+
return {
|
|
88
|
+
tag: "WorkflowStepEntry",
|
|
89
|
+
val: {
|
|
90
|
+
output: encodeOptionalCbor(kind.data.output),
|
|
91
|
+
error: kind.data.error ?? null,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
case "loop":
|
|
95
|
+
return {
|
|
96
|
+
tag: "WorkflowLoopEntry",
|
|
97
|
+
val: {
|
|
98
|
+
state: encodeCbor(kind.data.state),
|
|
99
|
+
iteration: kind.data.iteration,
|
|
100
|
+
output: encodeOptionalCbor(kind.data.output),
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
case "sleep":
|
|
104
|
+
return {
|
|
105
|
+
tag: "WorkflowSleepEntry",
|
|
106
|
+
val: {
|
|
107
|
+
deadline: toU64(kind.data.deadline),
|
|
108
|
+
state: toWorkflowSleepState(kind.data.state),
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
case "message":
|
|
112
|
+
return {
|
|
113
|
+
tag: "WorkflowMessageEntry",
|
|
114
|
+
val: {
|
|
115
|
+
name: kind.data.name,
|
|
116
|
+
messageData: encodeCbor(kind.data.data),
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
case "rollback_checkpoint":
|
|
120
|
+
return {
|
|
121
|
+
tag: "WorkflowRollbackCheckpointEntry",
|
|
122
|
+
val: { name: kind.data.name },
|
|
123
|
+
};
|
|
124
|
+
case "join":
|
|
125
|
+
return {
|
|
126
|
+
tag: "WorkflowJoinEntry",
|
|
127
|
+
val: { branches: toWorkflowBranchStatusMap(kind.data.branches) },
|
|
128
|
+
};
|
|
129
|
+
case "race":
|
|
130
|
+
return {
|
|
131
|
+
tag: "WorkflowRaceEntry",
|
|
132
|
+
val: {
|
|
133
|
+
winner: kind.data.winner ?? null,
|
|
134
|
+
branches: toWorkflowBranchStatusMap(kind.data.branches),
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
case "removed":
|
|
138
|
+
return {
|
|
139
|
+
tag: "WorkflowRemovedEntry",
|
|
140
|
+
val: {
|
|
141
|
+
originalType: kind.data.originalType,
|
|
142
|
+
originalName: kind.data.originalName ?? null,
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
default:
|
|
146
|
+
assertUnreachable(kind as never);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function toWorkflowEntry(
|
|
151
|
+
entry: WorkflowHistoryEntry,
|
|
152
|
+
): transport.WorkflowEntry {
|
|
153
|
+
return {
|
|
154
|
+
id: entry.id,
|
|
155
|
+
location: toWorkflowLocation(entry.location),
|
|
156
|
+
kind: toWorkflowEntryKind(entry.kind),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function toWorkflowEntryStatus(
|
|
161
|
+
status: EntryStatus,
|
|
162
|
+
): transport.WorkflowEntryStatus {
|
|
163
|
+
switch (status) {
|
|
164
|
+
case "pending":
|
|
165
|
+
return transport.WorkflowEntryStatus.PENDING;
|
|
166
|
+
case "running":
|
|
167
|
+
return transport.WorkflowEntryStatus.RUNNING;
|
|
168
|
+
case "completed":
|
|
169
|
+
return transport.WorkflowEntryStatus.COMPLETED;
|
|
170
|
+
case "failed":
|
|
171
|
+
return transport.WorkflowEntryStatus.FAILED;
|
|
172
|
+
case "exhausted":
|
|
173
|
+
return transport.WorkflowEntryStatus.EXHAUSTED;
|
|
174
|
+
default:
|
|
175
|
+
assertUnreachable(status as never);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function toWorkflowSleepState(
|
|
180
|
+
state: SleepState,
|
|
181
|
+
): transport.WorkflowSleepState {
|
|
182
|
+
switch (state) {
|
|
183
|
+
case "pending":
|
|
184
|
+
return transport.WorkflowSleepState.PENDING;
|
|
185
|
+
case "completed":
|
|
186
|
+
return transport.WorkflowSleepState.COMPLETED;
|
|
187
|
+
case "interrupted":
|
|
188
|
+
return transport.WorkflowSleepState.INTERRUPTED;
|
|
189
|
+
default:
|
|
190
|
+
assertUnreachable(state as never);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function toWorkflowBranchStatusType(
|
|
195
|
+
status: BranchStatusType,
|
|
196
|
+
): transport.WorkflowBranchStatusType {
|
|
197
|
+
switch (status) {
|
|
198
|
+
case "pending":
|
|
199
|
+
return transport.WorkflowBranchStatusType.PENDING;
|
|
200
|
+
case "running":
|
|
201
|
+
return transport.WorkflowBranchStatusType.RUNNING;
|
|
202
|
+
case "completed":
|
|
203
|
+
return transport.WorkflowBranchStatusType.COMPLETED;
|
|
204
|
+
case "failed":
|
|
205
|
+
return transport.WorkflowBranchStatusType.FAILED;
|
|
206
|
+
case "cancelled":
|
|
207
|
+
return transport.WorkflowBranchStatusType.CANCELLED;
|
|
208
|
+
default:
|
|
209
|
+
assertUnreachable(status as never);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function toWorkflowBranchStatus(
|
|
214
|
+
status: BranchStatus,
|
|
215
|
+
): transport.WorkflowBranchStatus {
|
|
216
|
+
return {
|
|
217
|
+
status: toWorkflowBranchStatusType(status.status),
|
|
218
|
+
output: encodeOptionalCbor(status.output),
|
|
219
|
+
error: status.error ?? null,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function toWorkflowBranchStatusMap(
|
|
224
|
+
branches: Record<string, BranchStatus>,
|
|
225
|
+
): ReadonlyMap<string, transport.WorkflowBranchStatus> {
|
|
226
|
+
return new Map(
|
|
227
|
+
Object.entries(branches).map(([name, status]) => [
|
|
228
|
+
name,
|
|
229
|
+
toWorkflowBranchStatus(status),
|
|
230
|
+
]),
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function toWorkflowEntryMetadata(
|
|
235
|
+
metadata: WorkflowEntryMetadataSnapshot,
|
|
236
|
+
): transport.WorkflowEntryMetadata {
|
|
237
|
+
return {
|
|
238
|
+
status: toWorkflowEntryStatus(metadata.status),
|
|
239
|
+
error: metadata.error ?? null,
|
|
240
|
+
attempts: metadata.attempts,
|
|
241
|
+
lastAttemptAt: toU64(metadata.lastAttemptAt),
|
|
242
|
+
createdAt: toU64(metadata.createdAt),
|
|
243
|
+
completedAt:
|
|
244
|
+
metadata.completedAt === undefined
|
|
245
|
+
? null
|
|
246
|
+
: toU64(metadata.completedAt),
|
|
247
|
+
rollbackCompletedAt:
|
|
248
|
+
metadata.rollbackCompletedAt === undefined
|
|
249
|
+
? null
|
|
250
|
+
: toU64(metadata.rollbackCompletedAt),
|
|
251
|
+
rollbackError: metadata.rollbackError ?? null,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function toWorkflowHistory(
|
|
256
|
+
snapshot: WorkflowHistorySnapshot,
|
|
257
|
+
): transport.WorkflowHistory {
|
|
258
|
+
const entryMetadata = new Map<string, transport.WorkflowEntryMetadata>();
|
|
259
|
+
for (const [id, metadata] of snapshot.entryMetadata) {
|
|
260
|
+
entryMetadata.set(id, toWorkflowEntryMetadata(metadata));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return {
|
|
264
|
+
nameRegistry: snapshot.nameRegistry,
|
|
265
|
+
entries: snapshot.entries.map((entry) => toWorkflowEntry(entry)),
|
|
266
|
+
entryMetadata,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { ACTOR_CONTEXT_INTERNAL_SYMBOL } from "@/actor/contexts/base/actor";
|
|
2
|
+
import type { RunContext } from "@/actor/contexts/run";
|
|
3
|
+
import type { AnyDatabaseProvider } from "@/actor/database";
|
|
4
|
+
import type { AnyActorInstance } from "@/actor/instance/mod";
|
|
5
|
+
import type { EventSchemaConfig, QueueSchemaConfig } from "@/actor/schema";
|
|
6
|
+
import { RUN_FUNCTION_CONFIG_SYMBOL } from "@/actor/config";
|
|
7
|
+
import { stringifyError } from "@/utils";
|
|
8
|
+
import { runWorkflow } from "@rivetkit/workflow-engine";
|
|
9
|
+
import invariant from "invariant";
|
|
10
|
+
import { ActorWorkflowContext } from "./context";
|
|
11
|
+
import { ActorWorkflowDriver } from "./driver";
|
|
12
|
+
import { createWorkflowInspectorAdapter } from "./inspector";
|
|
13
|
+
|
|
14
|
+
export { Loop } from "@rivetkit/workflow-engine";
|
|
15
|
+
export {
|
|
16
|
+
ActorWorkflowContext,
|
|
17
|
+
type WorkflowBranchContextOf,
|
|
18
|
+
type WorkflowContextOf,
|
|
19
|
+
type WorkflowLoopContextOf,
|
|
20
|
+
type WorkflowStepContextOf,
|
|
21
|
+
} from "./context";
|
|
22
|
+
|
|
23
|
+
export function workflow<
|
|
24
|
+
TState,
|
|
25
|
+
TConnParams,
|
|
26
|
+
TConnState,
|
|
27
|
+
TVars,
|
|
28
|
+
TInput,
|
|
29
|
+
TDatabase extends AnyDatabaseProvider,
|
|
30
|
+
TEvents extends EventSchemaConfig = Record<never, never>,
|
|
31
|
+
TQueues extends QueueSchemaConfig = Record<never, never>,
|
|
32
|
+
>(
|
|
33
|
+
fn: (
|
|
34
|
+
ctx: ActorWorkflowContext<
|
|
35
|
+
TState,
|
|
36
|
+
TConnParams,
|
|
37
|
+
TConnState,
|
|
38
|
+
TVars,
|
|
39
|
+
TInput,
|
|
40
|
+
TDatabase,
|
|
41
|
+
TEvents,
|
|
42
|
+
TQueues
|
|
43
|
+
>,
|
|
44
|
+
) => Promise<unknown>,
|
|
45
|
+
): (
|
|
46
|
+
c: RunContext<
|
|
47
|
+
TState,
|
|
48
|
+
TConnParams,
|
|
49
|
+
TConnState,
|
|
50
|
+
TVars,
|
|
51
|
+
TInput,
|
|
52
|
+
TDatabase,
|
|
53
|
+
TEvents,
|
|
54
|
+
TQueues
|
|
55
|
+
>,
|
|
56
|
+
) => Promise<void> {
|
|
57
|
+
const workflowInspector = createWorkflowInspectorAdapter();
|
|
58
|
+
|
|
59
|
+
async function run(
|
|
60
|
+
runCtx: RunContext<
|
|
61
|
+
TState,
|
|
62
|
+
TConnParams,
|
|
63
|
+
TConnState,
|
|
64
|
+
TVars,
|
|
65
|
+
TInput,
|
|
66
|
+
TDatabase,
|
|
67
|
+
TEvents,
|
|
68
|
+
TQueues
|
|
69
|
+
>,
|
|
70
|
+
): Promise<void> {
|
|
71
|
+
const actor = (
|
|
72
|
+
runCtx as unknown as {
|
|
73
|
+
[ACTOR_CONTEXT_INTERNAL_SYMBOL]?: AnyActorInstance;
|
|
74
|
+
}
|
|
75
|
+
)[ACTOR_CONTEXT_INTERNAL_SYMBOL];
|
|
76
|
+
invariant(actor, "workflow() requires an actor instance");
|
|
77
|
+
|
|
78
|
+
const driver = new ActorWorkflowDriver(actor, runCtx);
|
|
79
|
+
|
|
80
|
+
const handle = runWorkflow(
|
|
81
|
+
actor.id,
|
|
82
|
+
async (ctx) => await fn(new ActorWorkflowContext(ctx, runCtx)),
|
|
83
|
+
undefined,
|
|
84
|
+
driver,
|
|
85
|
+
{
|
|
86
|
+
mode: "live",
|
|
87
|
+
logger: runCtx.log,
|
|
88
|
+
onHistoryUpdated: workflowInspector.update,
|
|
89
|
+
},
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const onAbort = () => {
|
|
93
|
+
handle.evict();
|
|
94
|
+
};
|
|
95
|
+
if (runCtx.abortSignal.aborted) {
|
|
96
|
+
onAbort();
|
|
97
|
+
} else {
|
|
98
|
+
runCtx.abortSignal.addEventListener("abort", onAbort, {
|
|
99
|
+
once: true,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
await handle.result;
|
|
105
|
+
} catch (error) {
|
|
106
|
+
runCtx.log.error({
|
|
107
|
+
msg: "workflow run failed",
|
|
108
|
+
error: stringifyError(error),
|
|
109
|
+
});
|
|
110
|
+
throw error;
|
|
111
|
+
} finally {
|
|
112
|
+
runCtx.abortSignal.removeEventListener("abort", onAbort);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const runWithConfig = run as typeof run & {
|
|
117
|
+
[RUN_FUNCTION_CONFIG_SYMBOL]?: {
|
|
118
|
+
icon?: string;
|
|
119
|
+
inspector?: { workflow: typeof workflowInspector.adapter };
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
runWithConfig[RUN_FUNCTION_CONFIG_SYMBOL] = {
|
|
123
|
+
icon: "diagram-project",
|
|
124
|
+
inspector: { workflow: workflowInspector.adapter },
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
return runWithConfig;
|
|
128
|
+
}
|