rivetkit 0.0.0-sqlite-profiling-compat.87d61c0 → 0.0.0-sqlite-sync-3x-release.a00b07f

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.
Files changed (277) hide show
  1. package/dist/browser/client.d.ts +1317 -765
  2. package/dist/browser/client.js +1457 -737
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.d.ts +252 -129
  5. package/dist/browser/inspector/client.js +2042 -228
  6. package/dist/browser/inspector/client.js.map +1 -1
  7. package/dist/tsup/actor/errors.cjs +6 -2
  8. package/dist/tsup/actor/errors.cjs.map +1 -1
  9. package/dist/tsup/actor/errors.d.cts +1 -1
  10. package/dist/tsup/actor/errors.d.ts +1 -1
  11. package/dist/tsup/actor/errors.js +5 -1
  12. package/dist/tsup/agent-os/index.cjs +3 -2367
  13. package/dist/tsup/agent-os/index.cjs.map +1 -1
  14. package/dist/tsup/agent-os/index.d.cts +1 -2277
  15. package/dist/tsup/agent-os/index.d.ts +1 -2277
  16. package/dist/tsup/agent-os/index.js +3 -2367
  17. package/dist/tsup/agent-os/index.js.map +1 -1
  18. package/dist/tsup/{chunk-E4LNLI3W.cjs → chunk-2MOLY6AM.cjs} +444 -29
  19. package/dist/tsup/chunk-2MOLY6AM.cjs.map +1 -0
  20. package/dist/tsup/{chunk-OAUFXOG7.js → chunk-3DONDAMG.js} +2 -2
  21. package/dist/tsup/{chunk-XIX5DOZN.js → chunk-6FEYCVZC.js} +196 -20
  22. package/dist/tsup/chunk-6FEYCVZC.js.map +1 -0
  23. package/dist/tsup/chunk-6KXVLIXL.cjs +14 -0
  24. package/dist/tsup/chunk-6KXVLIXL.cjs.map +1 -0
  25. package/dist/tsup/chunk-73RNTXHB.cjs +243 -0
  26. package/dist/tsup/chunk-73RNTXHB.cjs.map +1 -0
  27. package/dist/tsup/{chunk-HXUEHHJF.js → chunk-A2CF6PFH.js} +19 -11
  28. package/dist/tsup/chunk-A2CF6PFH.js.map +1 -0
  29. package/dist/tsup/{chunk-EFU4JW3T.js → chunk-BRH4PQS5.js} +436 -21
  30. package/dist/tsup/chunk-BRH4PQS5.js.map +1 -0
  31. package/dist/tsup/{chunk-GM45LG7L.cjs → chunk-BTSDFFDC.cjs} +18 -11
  32. package/dist/tsup/chunk-BTSDFFDC.cjs.map +1 -0
  33. package/dist/tsup/{chunk-B3GLD4IN.js → chunk-D5WUPE47.js} +50 -3
  34. package/dist/tsup/chunk-D5WUPE47.js.map +1 -0
  35. package/dist/tsup/{chunk-O3ZUMAN7.js → chunk-DJUBJQEQ.js} +3 -3
  36. package/dist/tsup/chunk-DJUBJQEQ.js.map +1 -0
  37. package/dist/tsup/{chunk-3MHDOUD7.cjs → chunk-HDVZ6YZG.cjs} +197 -21
  38. package/dist/tsup/chunk-HDVZ6YZG.cjs.map +1 -0
  39. package/dist/tsup/{chunk-JZ7TWV65.js → chunk-JTHHCZCZ.js} +24 -1
  40. package/dist/tsup/chunk-JTHHCZCZ.js.map +1 -0
  41. package/dist/tsup/{chunk-D363DMNH.cjs → chunk-JUHXV3HF.cjs} +3 -3
  42. package/dist/tsup/chunk-JUHXV3HF.cjs.map +1 -0
  43. package/dist/tsup/chunk-JUWMP5O3.js +14 -0
  44. package/dist/tsup/chunk-JUWMP5O3.js.map +1 -0
  45. package/dist/tsup/{chunk-ZA7FLHKH.cjs → chunk-KVFIONKQ.cjs} +25 -2
  46. package/dist/tsup/chunk-KVFIONKQ.cjs.map +1 -0
  47. package/dist/tsup/{chunk-4FC7TVS6.cjs → chunk-MGI2VVF6.cjs} +39 -7
  48. package/dist/tsup/chunk-MGI2VVF6.cjs.map +1 -0
  49. package/dist/tsup/{chunk-BWERJMV6.cjs → chunk-OTVJYZ2L.cjs} +1139 -470
  50. package/dist/tsup/chunk-OTVJYZ2L.cjs.map +1 -0
  51. package/dist/tsup/{chunk-VE2X4KMG.cjs → chunk-RTWH3AX3.cjs} +18 -10
  52. package/dist/tsup/chunk-RTWH3AX3.cjs.map +1 -0
  53. package/dist/tsup/{chunk-DTRDGPHU.cjs → chunk-TM76Z7XT.cjs} +7 -7
  54. package/dist/tsup/chunk-TM76Z7XT.cjs.map +1 -0
  55. package/dist/tsup/{chunk-3GVMLSND.js → chunk-TRUHDXOU.js} +1023 -354
  56. package/dist/tsup/chunk-TRUHDXOU.js.map +1 -0
  57. package/dist/tsup/{chunk-PT7AC5PC.js → chunk-TSQTI6H6.js} +16 -9
  58. package/dist/tsup/chunk-TSQTI6H6.js.map +1 -0
  59. package/dist/tsup/{chunk-FNAXD5BQ.cjs → chunk-Z7E2G3JZ.cjs} +52 -5
  60. package/dist/tsup/chunk-Z7E2G3JZ.cjs.map +1 -0
  61. package/dist/tsup/{chunk-ZZ3WBRPD.js → chunk-ZOVJDAOW.js} +38 -6
  62. package/dist/tsup/chunk-ZOVJDAOW.js.map +1 -0
  63. package/dist/tsup/chunk-ZYK4B5WV.js +243 -0
  64. package/dist/tsup/chunk-ZYK4B5WV.js.map +1 -0
  65. package/dist/tsup/client/mod.cjs +8 -9
  66. package/dist/tsup/client/mod.cjs.map +1 -1
  67. package/dist/tsup/client/mod.d.cts +32 -6
  68. package/dist/tsup/client/mod.d.ts +32 -6
  69. package/dist/tsup/client/mod.js +7 -8
  70. package/dist/tsup/common/log.cjs +5 -3
  71. package/dist/tsup/common/log.cjs.map +1 -1
  72. package/dist/tsup/common/log.d.cts +5 -1
  73. package/dist/tsup/common/log.d.ts +5 -1
  74. package/dist/tsup/common/log.js +6 -4
  75. package/dist/tsup/common/websocket.cjs +4 -4
  76. package/dist/tsup/common/websocket.cjs.map +1 -1
  77. package/dist/tsup/common/websocket.js +3 -3
  78. package/dist/tsup/{config-BR1Nn0QQ.d.ts → config-B4erdavw.d.ts} +571 -65
  79. package/dist/tsup/{config-B0LNDrLY.d.cts → config-BfG1qpYz.d.cts} +80 -14
  80. package/dist/tsup/{config-B0LNDrLY.d.ts → config-BfG1qpYz.d.ts} +80 -14
  81. package/dist/tsup/{config-tCvkwzyN.d.cts → config-DK144OAw.d.cts} +571 -65
  82. package/dist/tsup/{context-D4WDwf9m.d.cts → context-Cvnf3BRe.d.cts} +3 -2
  83. package/dist/tsup/{context-oc9zhA3x.d.ts → context-Oxi6YWcG.d.ts} +3 -2
  84. package/dist/tsup/db/drizzle.cjs +127 -67
  85. package/dist/tsup/db/drizzle.cjs.map +1 -1
  86. package/dist/tsup/db/drizzle.d.cts +8 -4
  87. package/dist/tsup/db/drizzle.d.ts +8 -4
  88. package/dist/tsup/db/drizzle.js +127 -67
  89. package/dist/tsup/db/drizzle.js.map +1 -1
  90. package/dist/tsup/db/mod.cjs +6 -126
  91. package/dist/tsup/db/mod.cjs.map +1 -1
  92. package/dist/tsup/db/mod.d.cts +8 -4
  93. package/dist/tsup/db/mod.d.ts +8 -4
  94. package/dist/tsup/db/mod.js +5 -125
  95. package/dist/tsup/db/mod.js.map +1 -1
  96. package/dist/tsup/dynamic/mod.cjs +2 -2
  97. package/dist/tsup/dynamic/mod.cjs.map +1 -1
  98. package/dist/tsup/dynamic/mod.d.cts +6 -4
  99. package/dist/tsup/dynamic/mod.d.ts +6 -4
  100. package/dist/tsup/dynamic/mod.js +1 -1
  101. package/dist/tsup/inspector/mod.cjs +47 -12
  102. package/dist/tsup/inspector/mod.cjs.map +1 -1
  103. package/dist/tsup/inspector/mod.d.cts +26 -136
  104. package/dist/tsup/inspector/mod.d.ts +26 -136
  105. package/dist/tsup/inspector/mod.js +46 -11
  106. package/dist/tsup/inspector/mod.js.map +1 -1
  107. package/dist/tsup/inspector/workflow.cjs +21 -0
  108. package/dist/tsup/inspector/workflow.cjs.map +1 -0
  109. package/dist/tsup/inspector/workflow.d.cts +144 -0
  110. package/dist/tsup/inspector/workflow.d.ts +144 -0
  111. package/dist/tsup/inspector/workflow.js +21 -0
  112. package/dist/tsup/inspector/workflow.js.map +1 -0
  113. package/dist/tsup/inspector-tab/mod.cjs +2 -13
  114. package/dist/tsup/inspector-tab/mod.cjs.map +1 -1
  115. package/dist/tsup/inspector-tab/mod.d.cts +9 -17
  116. package/dist/tsup/inspector-tab/mod.d.ts +9 -17
  117. package/dist/tsup/inspector-tab/mod.js +1 -12
  118. package/dist/tsup/inspector-tab/mod.js.map +1 -1
  119. package/dist/tsup/mod.cjs +2724 -611
  120. package/dist/tsup/mod.cjs.map +1 -1
  121. package/dist/tsup/mod.d.cts +9 -7
  122. package/dist/tsup/mod.d.ts +9 -7
  123. package/dist/tsup/mod.js +2641 -528
  124. package/dist/tsup/mod.js.map +1 -1
  125. package/dist/tsup/test/mod.cjs +19 -17
  126. package/dist/tsup/test/mod.cjs.map +1 -1
  127. package/dist/tsup/test/mod.d.cts +6 -4
  128. package/dist/tsup/test/mod.d.ts +6 -4
  129. package/dist/tsup/test/mod.js +18 -16
  130. package/dist/tsup/test/mod.js.map +1 -1
  131. package/dist/tsup/unstable/migrations.cjs +101 -0
  132. package/dist/tsup/unstable/migrations.cjs.map +1 -0
  133. package/dist/tsup/unstable/migrations.d.cts +29 -0
  134. package/dist/tsup/unstable/migrations.d.ts +29 -0
  135. package/dist/tsup/unstable/migrations.js +101 -0
  136. package/dist/tsup/unstable/migrations.js.map +1 -0
  137. package/dist/tsup/{utils-CqDnC_PS.d.cts → utils-3rsE0JvI.d.cts} +25 -2
  138. package/dist/tsup/{utils-CqDnC_PS.d.ts → utils-3rsE0JvI.d.ts} +25 -2
  139. package/dist/tsup/utils-C1O-SklO.d.ts +162 -0
  140. package/dist/tsup/utils-C6b0aYUM.d.cts +162 -0
  141. package/dist/tsup/utils.cjs +7 -3
  142. package/dist/tsup/utils.cjs.map +1 -1
  143. package/dist/tsup/utils.d.cts +4 -147
  144. package/dist/tsup/utils.d.ts +4 -147
  145. package/dist/tsup/utils.js +6 -2
  146. package/dist/tsup/workflow/mod.cjs +260 -200
  147. package/dist/tsup/workflow/mod.cjs.map +1 -1
  148. package/dist/tsup/workflow/mod.d.cts +8 -6
  149. package/dist/tsup/workflow/mod.d.ts +8 -6
  150. package/dist/tsup/workflow/mod.js +249 -189
  151. package/dist/tsup/workflow/mod.js.map +1 -1
  152. package/package.json +34 -14
  153. package/src/actor/actions.ts +155 -0
  154. package/src/actor/config.ts +403 -43
  155. package/src/actor/definition.ts +26 -7
  156. package/src/actor/errors.ts +87 -5
  157. package/src/actor/keys.ts +86 -0
  158. package/src/actor/mod.ts +15 -1
  159. package/src/actor/tracing.ts +28 -0
  160. package/src/agent-os/index.ts +5 -65
  161. package/src/client/actor-common.ts +8 -15
  162. package/src/client/actor-conn.ts +135 -8
  163. package/src/client/actor-handle.ts +180 -42
  164. package/src/client/actor-query.ts +8 -1
  165. package/src/client/auth.ts +37 -0
  166. package/src/client/client.ts +89 -21
  167. package/src/client/config.ts +27 -2
  168. package/src/client/errors.ts +2 -1
  169. package/src/client/lifecycle-errors.ts +16 -1
  170. package/src/client/mod.ts +9 -0
  171. package/src/client/outbound-telemetry.ts +66 -0
  172. package/src/client/query.ts +2 -0
  173. package/src/client/queue.ts +3 -0
  174. package/src/client/raw-utils.ts +37 -8
  175. package/src/client/resolve-gateway-target.ts +2 -0
  176. package/src/client/retry-budget.ts +78 -0
  177. package/src/client/token-provider.ts +93 -0
  178. package/src/client/utils.ts +16 -2
  179. package/src/common/actor-router-consts.ts +5 -0
  180. package/src/common/actor-telemetry-context.ts +19 -0
  181. package/src/common/bare/generated/actor-runtime-socket-protocol/v1.ts +848 -0
  182. package/src/common/bare/generated/inspector/v5.ts +1016 -0
  183. package/src/common/bare/generated/inspector/v6.ts +1367 -0
  184. package/src/common/bare/transport/v1.ts +36 -0
  185. package/src/common/database/config.ts +108 -15
  186. package/src/common/database/mod.ts +247 -76
  187. package/src/common/database/native-database.ts +433 -23
  188. package/src/common/database/shared.ts +24 -0
  189. package/src/common/encoding.ts +31 -21
  190. package/src/common/fetch-like.ts +63 -0
  191. package/src/common/inspector-transport.ts +12 -2
  192. package/src/common/log.ts +14 -8
  193. package/src/common/otel-context.ts +99 -0
  194. package/src/common/router.ts +1 -1
  195. package/src/common/utils.ts +7 -19
  196. package/src/db/drizzle.ts +182 -80
  197. package/src/db/mod.ts +2 -0
  198. package/src/dynamic/isolate-runtime.ts +0 -2
  199. package/src/engine-api/actors.ts +3 -3
  200. package/src/engine-client/actor-http-client.ts +29 -20
  201. package/src/engine-client/actor-websocket-client.ts +6 -3
  202. package/src/engine-client/api-endpoints.ts +72 -14
  203. package/src/engine-client/driver.ts +8 -15
  204. package/src/engine-client/mod.ts +248 -112
  205. package/src/inspector/client.browser.ts +134 -16
  206. package/src/inspector/mod.ts +1 -1
  207. package/src/inspector/workflow.ts +61 -0
  208. package/src/inspector-tab/mod.ts +1 -15
  209. package/src/registry/config/envoy.ts +2 -18
  210. package/src/registry/config/index.ts +53 -61
  211. package/src/registry/index.ts +311 -21
  212. package/src/registry/napi-runtime.ts +583 -39
  213. package/src/registry/native-http.ts +225 -0
  214. package/src/registry/native.ts +1365 -414
  215. package/src/registry/run-handler-coordinator.ts +219 -0
  216. package/src/registry/runtime.ts +372 -7
  217. package/src/registry/wasm-runtime.ts +490 -21
  218. package/src/registry/write-through-proxy.ts +101 -0
  219. package/src/test/mod.ts +1 -19
  220. package/src/test/ready.ts +22 -0
  221. package/src/unstable/migrations.ts +141 -0
  222. package/src/utils/env-vars.ts +15 -6
  223. package/src/utils/node.ts +44 -28
  224. package/src/utils.ts +3 -0
  225. package/src/workflow/context.ts +9 -27
  226. package/src/workflow/driver.ts +253 -116
  227. package/src/workflow/inspector.ts +23 -24
  228. package/src/workflow/mod.ts +50 -72
  229. package/dist/tsup/chunk-3GVMLSND.js.map +0 -1
  230. package/dist/tsup/chunk-3MHDOUD7.cjs.map +0 -1
  231. package/dist/tsup/chunk-4FC7TVS6.cjs.map +0 -1
  232. package/dist/tsup/chunk-B3GLD4IN.js.map +0 -1
  233. package/dist/tsup/chunk-BWERJMV6.cjs.map +0 -1
  234. package/dist/tsup/chunk-D363DMNH.cjs.map +0 -1
  235. package/dist/tsup/chunk-DTRDGPHU.cjs.map +0 -1
  236. package/dist/tsup/chunk-E4LNLI3W.cjs.map +0 -1
  237. package/dist/tsup/chunk-EFU4JW3T.js.map +0 -1
  238. package/dist/tsup/chunk-FNAXD5BQ.cjs.map +0 -1
  239. package/dist/tsup/chunk-GM45LG7L.cjs.map +0 -1
  240. package/dist/tsup/chunk-HXUEHHJF.js.map +0 -1
  241. package/dist/tsup/chunk-JZ7TWV65.js.map +0 -1
  242. package/dist/tsup/chunk-O3ZUMAN7.js.map +0 -1
  243. package/dist/tsup/chunk-P6F5MEWX.cjs +0 -382
  244. package/dist/tsup/chunk-P6F5MEWX.cjs.map +0 -1
  245. package/dist/tsup/chunk-PT7AC5PC.js.map +0 -1
  246. package/dist/tsup/chunk-UV7INX3N.js +0 -382
  247. package/dist/tsup/chunk-UV7INX3N.js.map +0 -1
  248. package/dist/tsup/chunk-VE2X4KMG.cjs.map +0 -1
  249. package/dist/tsup/chunk-XIX5DOZN.js.map +0 -1
  250. package/dist/tsup/chunk-ZA7FLHKH.cjs.map +0 -1
  251. package/dist/tsup/chunk-ZZ3WBRPD.js.map +0 -1
  252. package/src/actor/driver.ts +0 -141
  253. package/src/actor/instance/mod.ts +0 -2484
  254. package/src/actor/keys.test.ts +0 -275
  255. package/src/actor/utils.test.ts +0 -48
  256. package/src/agent-os/actor/cron.ts +0 -84
  257. package/src/agent-os/actor/db.ts +0 -54
  258. package/src/agent-os/actor/filesystem.ts +0 -131
  259. package/src/agent-os/actor/index.ts +0 -285
  260. package/src/agent-os/actor/network.ts +0 -58
  261. package/src/agent-os/actor/preview.ts +0 -190
  262. package/src/agent-os/actor/process.ts +0 -177
  263. package/src/agent-os/actor/session.ts +0 -518
  264. package/src/agent-os/actor/shell.ts +0 -60
  265. package/src/agent-os/config.ts +0 -79
  266. package/src/agent-os/fs/database-vfs.ts +0 -624
  267. package/src/agent-os/types.ts +0 -148
  268. package/src/client/test.ts +0 -44
  269. package/src/common/database/mod.test.ts +0 -137
  270. package/src/common/database/native-database.test.ts +0 -211
  271. package/src/common/inline-websocket-adapter.test.ts +0 -51
  272. package/src/drivers/engine/actor-driver.ts +0 -2473
  273. package/src/registry/process-metrics.ts +0 -250
  274. package/src/registry/runtime.test.ts +0 -50
  275. package/src/utils/endpoint-parser.test.ts +0 -206
  276. package/src/utils.test.ts +0 -34
  277. /package/dist/tsup/{chunk-OAUFXOG7.js.map → chunk-3DONDAMG.js.map} +0 -0
@@ -2,6 +2,12 @@ import type { Context as HonoContext } from "hono";
2
2
  import invariant from "invariant";
3
3
  import { deserializeActorKey, serializeActorKey } from "@/actor/keys";
4
4
  import type { ClientConfig } from "@/client/client";
5
+ import type { IssuedToken, IssueTokenOptions } from "@/client/auth";
6
+ import {
7
+ isInvalidToken,
8
+ isInvalidTokenResponse,
9
+ TokenProvider,
10
+ } from "@/client/token-provider";
5
11
  import {
6
12
  PATH_CONNECT,
7
13
  PATH_WEBSOCKET_BASE,
@@ -40,7 +46,7 @@ import {
40
46
  getActor,
41
47
  getActorByKey,
42
48
  getOrCreateActor,
43
- kvGet,
49
+ issueToken as issueEngineToken,
44
50
  listActorsByName,
45
51
  } from "./api-endpoints";
46
52
  import { EngineApiError, getEndpoint } from "./api-utils";
@@ -51,6 +57,13 @@ import { createWebSocketProxy } from "./ws-proxy";
51
57
  export class RemoteEngineControlClient implements EngineControlClient {
52
58
  #config: ClientConfig;
53
59
  #metadataPromise: Promise<void> | undefined;
60
+ #tokenProvider?: TokenProvider;
61
+ #webSocketTokens = new WeakMap<
62
+ UniversalWebSocket,
63
+ {
64
+ refresh?: Promise<string>;
65
+ }
66
+ >();
54
67
 
55
68
  constructor(runConfig: ClientConfig) {
56
69
  // Disable health check if in Next.js build phase since there is no `/metadata` endpoint
@@ -66,6 +79,8 @@ export class RemoteEngineControlClient implements EngineControlClient {
66
79
  // Clone config so we can mutate the endpoint in #metadataPromise
67
80
  // NOTE: This is a shallow clone, so mutating nested properties will not do anything
68
81
  this.#config = { ...runConfig };
82
+ if (runConfig.getToken)
83
+ this.#tokenProvider = new TokenProvider(runConfig.getToken);
69
84
 
70
85
  // Perform metadata check if enabled
71
86
  if (!runConfig.disableMetadataLookup) {
@@ -81,7 +96,7 @@ export class RemoteEngineControlClient implements EngineControlClient {
81
96
  this.#config.namespace =
82
97
  metadataData.clientNamespace;
83
98
  }
84
- if (metadataData.clientToken) {
99
+ if (metadataData.clientToken && !this.#tokenProvider) {
85
100
  this.#config.token = metadataData.clientToken;
86
101
  }
87
102
 
@@ -89,7 +104,6 @@ export class RemoteEngineControlClient implements EngineControlClient {
89
104
  msg: "overriding client endpoint",
90
105
  endpoint: metadataData.clientEndpoint,
91
106
  namespace: metadataData.clientNamespace,
92
- token: metadataData.clientToken,
93
107
  });
94
108
  }
95
109
 
@@ -104,6 +118,51 @@ export class RemoteEngineControlClient implements EngineControlClient {
104
118
  }
105
119
  }
106
120
 
121
+ async #authorizedConfig(): Promise<ClientConfig> {
122
+ if (!this.#tokenProvider) return this.#config;
123
+ return { ...this.#config, token: await this.#tokenProvider.current() };
124
+ }
125
+
126
+ async #withCredential<T>(
127
+ request: (config: ClientConfig) => Promise<T>,
128
+ readOnly = false,
129
+ ): Promise<T> {
130
+ const config = await this.#authorizedConfig();
131
+ try {
132
+ return await request(config);
133
+ } catch (error) {
134
+ if (
135
+ this.#tokenProvider &&
136
+ config.token &&
137
+ error instanceof EngineApiError &&
138
+ error.statusCode === 401 &&
139
+ isInvalidToken(error.group, error.code)
140
+ ) {
141
+ if (readOnly) {
142
+ const token = await this.#tokenProvider.refreshIfCurrent(
143
+ config.token,
144
+ );
145
+ return request({ ...this.#config, token });
146
+ }
147
+ // Mutations may have executed; refresh the next call, not this one.
148
+ await this.#tokenProvider.refreshIfCurrent(config.token);
149
+ }
150
+ throw error;
151
+ }
152
+ }
153
+
154
+ async issueToken(options: IssueTokenOptions): Promise<IssuedToken> {
155
+ await this.#metadataPromise;
156
+ const response = await this.#withCredential((config) =>
157
+ issueEngineToken(config, options),
158
+ );
159
+ return {
160
+ token: response.token,
161
+ issuedAt: response.issued_ts,
162
+ expiresAt: response.expires_ts,
163
+ };
164
+ }
165
+
107
166
  async getForId({
108
167
  name,
109
168
  actorId,
@@ -111,7 +170,10 @@ export class RemoteEngineControlClient implements EngineControlClient {
111
170
  await this.#metadataPromise;
112
171
 
113
172
  // Fetch from API if not in cache
114
- const response = await getActor(this.#config, name, actorId);
173
+ const response = await this.#withCredential(
174
+ (config) => getActor(config, name, actorId),
175
+ true,
176
+ );
115
177
  const actor = response.actors[0];
116
178
  if (!actor) return undefined;
117
179
 
@@ -139,7 +201,10 @@ export class RemoteEngineControlClient implements EngineControlClient {
139
201
 
140
202
  // If not in local cache, fetch by key from API
141
203
  try {
142
- const response = await getActorByKey(this.#config, name, key);
204
+ const response = await this.#withCredential(
205
+ (config) => getActorByKey(config, name, key),
206
+ true,
207
+ );
143
208
  const actor = response.actors[0];
144
209
  if (!actor) return undefined;
145
210
 
@@ -168,7 +233,14 @@ export class RemoteEngineControlClient implements EngineControlClient {
168
233
  ): Promise<ActorOutput> {
169
234
  await this.#metadataPromise;
170
235
 
171
- const { name, key, input: actorInput, region, crashPolicy } = input;
236
+ const {
237
+ name,
238
+ key,
239
+ input: actorInput,
240
+ region,
241
+ crashPolicy,
242
+ poolName,
243
+ } = input;
172
244
 
173
245
  logger().info({
174
246
  msg: "getOrCreateWithKey: getting or creating actor via engine api",
@@ -176,28 +248,64 @@ export class RemoteEngineControlClient implements EngineControlClient {
176
248
  key,
177
249
  });
178
250
 
179
- const { actor, created } = await getOrCreateActor(this.#config, {
180
- datacenter: region,
181
- name,
182
- key: serializeActorKey(key),
183
- runner_name_selector: this.#config.poolName,
184
- input: actorInput
185
- ? uint8ArrayToBase64(
186
- encodeCborCompat(actorInput as JsonCompatValue),
187
- )
188
- : undefined,
189
- crash_policy: crashPolicy ?? "sleep",
190
- });
251
+ try {
252
+ const { actor, created } = await this.#withCredential((config) =>
253
+ getOrCreateActor(config, {
254
+ datacenter: region,
255
+ name,
256
+ key: serializeActorKey(key),
257
+ pool: poolName ?? this.#config.poolName,
258
+ input: actorInput
259
+ ? uint8ArrayToBase64(
260
+ encodeCborCompat(actorInput as JsonCompatValue),
261
+ )
262
+ : undefined,
263
+ crash_policy: crashPolicy ?? "sleep",
264
+ }),
265
+ );
191
266
 
192
- logger().info({
193
- msg: "getOrCreateWithKey: actor ready",
194
- actorId: actor.actor_id,
195
- name,
196
- key,
197
- created,
198
- });
267
+ logger().info({
268
+ msg: "getOrCreateWithKey: actor ready",
269
+ actorId: actor.actor_id,
270
+ name,
271
+ key,
272
+ created,
273
+ });
199
274
 
200
- return apiActorToOutput(actor);
275
+ return apiActorToOutput(actor);
276
+ } catch (error) {
277
+ // The key is reserved in a different datacenter, which means the
278
+ // actor already exists there. get-by-key forwards to the reserved
279
+ // datacenter, so retry as a get to resolve the existing actor.
280
+ if (
281
+ error instanceof EngineApiError &&
282
+ error.group === "actor" &&
283
+ error.code === "key_reserved_in_different_datacenter"
284
+ ) {
285
+ logger().warn({
286
+ msg: "getOrCreateWithKey: key reserved in different datacenter, retrying as get",
287
+ name,
288
+ key,
289
+ });
290
+
291
+ const response = await this.#withCredential(
292
+ (config) => getActorByKey(config, name, key),
293
+ true,
294
+ );
295
+ const existing = response.actors[0];
296
+ if (!existing) throw error;
297
+
298
+ logger().info({
299
+ msg: "getOrCreateWithKey: resolved existing actor via get",
300
+ actorId: existing.actor_id,
301
+ name,
302
+ key,
303
+ });
304
+
305
+ return apiActorToOutput(existing);
306
+ }
307
+ throw error;
308
+ }
201
309
  }
202
310
 
203
311
  async createActor({
@@ -206,22 +314,27 @@ export class RemoteEngineControlClient implements EngineControlClient {
206
314
  input,
207
315
  region,
208
316
  crashPolicy,
317
+ poolName,
209
318
  }: CreateInput): Promise<ActorOutput> {
210
319
  await this.#metadataPromise;
211
320
 
212
321
  logger().info({ msg: "creating actor via engine api", name, key });
213
322
 
214
323
  // Create actor via engine API
215
- const result = await createActor(this.#config, {
216
- datacenter: region,
217
- name,
218
- runner_name_selector: this.#config.poolName,
219
- key: serializeActorKey(key),
220
- input: input
221
- ? uint8ArrayToBase64(encodeCborCompat(input as JsonCompatValue))
222
- : undefined,
223
- crash_policy: crashPolicy ?? "sleep",
224
- });
324
+ const result = await this.#withCredential((config) =>
325
+ createActor(config, {
326
+ datacenter: region,
327
+ name,
328
+ pool: poolName ?? this.#config.poolName,
329
+ key: serializeActorKey(key),
330
+ input: input
331
+ ? uint8ArrayToBase64(
332
+ encodeCborCompat(input as JsonCompatValue),
333
+ )
334
+ : undefined,
335
+ crash_policy: crashPolicy ?? "sleep",
336
+ }),
337
+ );
225
338
 
226
339
  logger().info({
227
340
  msg: "actor created",
@@ -238,7 +351,10 @@ export class RemoteEngineControlClient implements EngineControlClient {
238
351
 
239
352
  logger().debug({ msg: "listing actors via engine api", name });
240
353
 
241
- const response = await listActorsByName(this.#config, name);
354
+ const response = await this.#withCredential(
355
+ (config) => listActorsByName(config, name),
356
+ true,
357
+ );
242
358
 
243
359
  return response.actors.map(apiActorToOutput);
244
360
  }
@@ -248,7 +364,7 @@ export class RemoteEngineControlClient implements EngineControlClient {
248
364
 
249
365
  logger().info({ msg: "destroying actor via engine api", actorId });
250
366
 
251
- await destroyActor(this.#config, actorId);
367
+ await this.#withCredential((config) => destroyActor(config, actorId));
252
368
 
253
369
  logger().info({ msg: "actor destroyed", actorId });
254
370
  }
@@ -259,13 +375,7 @@ export class RemoteEngineControlClient implements EngineControlClient {
259
375
  options: GatewayRequestOptions = {},
260
376
  ): Promise<Response> {
261
377
  await this.#metadataPromise;
262
-
263
- const path = requestPath(actorRequest);
264
- const gatewayUrl = this.#buildGatewayUrlForTarget(
265
- target,
266
- path,
267
- options,
268
- );
378
+ const config = await this.#authorizedConfig();
269
379
  const httpOptions = {
270
380
  ...options,
271
381
  directActorId: shouldSkipReadyWait(options)
@@ -273,12 +383,37 @@ export class RemoteEngineControlClient implements EngineControlClient {
273
383
  : undefined,
274
384
  };
275
385
 
276
- return sendHttpRequestToGateway(
277
- this.#config,
278
- gatewayUrl,
279
- actorRequest,
280
- httpOptions,
281
- );
386
+ const send = (current: ClientConfig) =>
387
+ sendHttpRequestToGateway(
388
+ current,
389
+ this.#buildGatewayUrlForTarget(
390
+ current,
391
+ target,
392
+ requestPath(actorRequest),
393
+ options,
394
+ ),
395
+ actorRequest,
396
+ httpOptions,
397
+ );
398
+ const response = await send(config);
399
+ if (
400
+ !this.#tokenProvider ||
401
+ !config.token ||
402
+ !isInvalidTokenResponse(response)
403
+ )
404
+ return response;
405
+ if (
406
+ (actorRequest.method === "GET" || actorRequest.method === "HEAD") &&
407
+ !("getOrCreateForKey" in target)
408
+ ) {
409
+ const token = await this.#tokenProvider.refreshIfCurrent(
410
+ config.token,
411
+ );
412
+ await response.body?.cancel();
413
+ return send({ ...this.#config, token });
414
+ }
415
+ await this.#tokenProvider.refreshIfCurrent(config.token);
416
+ return response;
282
417
  }
283
418
 
284
419
  async openWebSocket(
@@ -289,15 +424,17 @@ export class RemoteEngineControlClient implements EngineControlClient {
289
424
  options: GatewayRequestOptions = {},
290
425
  ): Promise<UniversalWebSocket> {
291
426
  await this.#metadataPromise;
427
+ const config = await this.#authorizedConfig();
292
428
 
293
429
  const gatewayUrl = this.#buildGatewayUrlForTarget(
430
+ config,
294
431
  target,
295
432
  path,
296
433
  options,
297
434
  );
298
435
 
299
- return openWebSocketToGateway(
300
- this.#config,
436
+ const ws = await openWebSocketToGateway(
437
+ config,
301
438
  gatewayUrl,
302
439
  encoding,
303
440
  params,
@@ -308,6 +445,45 @@ export class RemoteEngineControlClient implements EngineControlClient {
308
445
  : undefined,
309
446
  },
310
447
  );
448
+ this.#watchWebSocketAuth(ws, config.token);
449
+ return ws;
450
+ }
451
+
452
+ async refreshAuthToken(ws: UniversalWebSocket): Promise<boolean> {
453
+ const context = this.#webSocketTokens.get(ws);
454
+ if (!context?.refresh) return false;
455
+ await context.refresh;
456
+ return true;
457
+ }
458
+
459
+ #watchWebSocketAuth(ws: UniversalWebSocket, token?: string): void {
460
+ const tokenProvider = this.#tokenProvider;
461
+ if (!tokenProvider || !token) return;
462
+ const context: { refresh?: Promise<string> } = {};
463
+ this.#webSocketTokens.set(ws, context);
464
+ ws.addEventListener(
465
+ "close",
466
+ (event: { code?: number; reason?: string }) => {
467
+ if (event.code !== 1008 || !event.reason) return;
468
+ const separator = event.reason.indexOf(".");
469
+ if (separator < 0) return;
470
+ const code = event.reason
471
+ .slice(separator + 1)
472
+ .split(/[\s:#]/, 1)[0];
473
+ if (isInvalidToken(event.reason.slice(0, separator), code)) {
474
+ context.refresh = tokenProvider
475
+ .refreshIfCurrent(token)
476
+ .catch((error) => {
477
+ logger().warn(
478
+ "failed to renew authentication token",
479
+ );
480
+ throw error;
481
+ });
482
+ // Raw websockets have no built-in reconnection; preempt unhandled rejection.
483
+ context.refresh.catch(() => {});
484
+ }
485
+ },
486
+ );
311
487
  }
312
488
 
313
489
  async buildGatewayUrl(
@@ -315,7 +491,12 @@ export class RemoteEngineControlClient implements EngineControlClient {
315
491
  options: GatewayRequestOptions = {},
316
492
  ): Promise<string> {
317
493
  await this.#metadataPromise;
318
- return this.#buildGatewayUrlForTarget(target, "", options);
494
+ return this.#buildGatewayUrlForTarget(
495
+ await this.#authorizedConfig(),
496
+ target,
497
+ "",
498
+ options,
499
+ );
319
500
  }
320
501
 
321
502
  async proxyRequest(
@@ -323,14 +504,7 @@ export class RemoteEngineControlClient implements EngineControlClient {
323
504
  actorRequest: Request,
324
505
  actorId: string,
325
506
  ): Promise<Response> {
326
- await this.#metadataPromise;
327
-
328
- const gatewayUrl = this.#buildGatewayUrlForTarget(
329
- { directId: actorId },
330
- requestPath(actorRequest),
331
- );
332
-
333
- return sendHttpRequestToGateway(this.#config, gatewayUrl, actorRequest);
507
+ return this.sendRequest({ directId: actorId }, actorRequest);
334
508
  }
335
509
 
336
510
  async proxyWebSocket(
@@ -341,11 +515,12 @@ export class RemoteEngineControlClient implements EngineControlClient {
341
515
  params: unknown,
342
516
  ): Promise<Response> {
343
517
  await this.#metadataPromise;
518
+ const config = await this.#authorizedConfig();
344
519
 
345
- const upgradeWebSocket = this.#config.getUpgradeWebSocket?.();
520
+ const upgradeWebSocket = config.getUpgradeWebSocket?.();
346
521
  invariant(upgradeWebSocket, "missing getUpgradeWebSocket");
347
522
 
348
- const endpoint = getEndpoint(this.#config);
523
+ const endpoint = getEndpoint(config);
349
524
  const guardUrl = combineUrlPath(endpoint, path);
350
525
  const wsGuardUrl = guardUrl.replace("http://", "ws://");
351
526
 
@@ -358,7 +533,7 @@ export class RemoteEngineControlClient implements EngineControlClient {
358
533
 
359
534
  // Build protocols
360
535
  const protocols = buildWebSocketProtocols(
361
- this.#config,
536
+ config,
362
537
  encoding,
363
538
  params,
364
539
  undefined,
@@ -372,46 +547,6 @@ export class RemoteEngineControlClient implements EngineControlClient {
372
547
  return await upgradeWebSocket(() => args)(c, noopNext());
373
548
  }
374
549
 
375
- async kvGet(actorId: string, key: Uint8Array): Promise<string | null> {
376
- await this.#metadataPromise;
377
-
378
- logger().debug({ msg: "getting kv value via engine api", key });
379
-
380
- const response = await kvGet(
381
- this.#config,
382
- actorId,
383
- new TextDecoder("utf8").decode(key),
384
- );
385
-
386
- return response.value;
387
- }
388
-
389
- async kvBatchGet(
390
- _actorId: string,
391
- _keys: Uint8Array[],
392
- ): Promise<(Uint8Array | null)[]> {
393
- throw new Error("kvBatchGet not supported on remote engine client");
394
- }
395
-
396
- async kvBatchPut(
397
- _actorId: string,
398
- _entries: [Uint8Array, Uint8Array][],
399
- ): Promise<void> {
400
- throw new Error("kvBatchPut not supported on remote engine client");
401
- }
402
-
403
- async kvBatchDelete(_actorId: string, _keys: Uint8Array[]): Promise<void> {
404
- throw new Error("kvBatchDelete not supported on remote engine client");
405
- }
406
-
407
- async kvDeleteRange(
408
- _actorId: string,
409
- _start: Uint8Array,
410
- _end: Uint8Array,
411
- ): Promise<void> {
412
- throw new Error("kvDeleteRange not supported on remote engine client");
413
- }
414
-
415
550
  displayInformation(): RuntimeDisplayInformation {
416
551
  return { properties: {} };
417
552
  }
@@ -421,11 +556,12 @@ export class RemoteEngineControlClient implements EngineControlClient {
421
556
  }
422
557
 
423
558
  #buildGatewayUrlForTarget(
559
+ config: ClientConfig,
424
560
  target: GatewayTarget,
425
561
  path: string,
426
562
  options: GatewayRequestOptions = {},
427
563
  ): string {
428
- const endpoint = getEndpoint(this.#config);
564
+ const endpoint = getEndpoint(config);
429
565
 
430
566
  if (
431
567
  shouldSkipReadyWait(options) &&
@@ -439,7 +575,7 @@ export class RemoteEngineControlClient implements EngineControlClient {
439
575
  return buildActorGatewayUrl(
440
576
  endpoint,
441
577
  target.directId,
442
- this.#config.token,
578
+ config.token,
443
579
  path,
444
580
  );
445
581
  }
@@ -448,7 +584,7 @@ export class RemoteEngineControlClient implements EngineControlClient {
448
584
  return buildActorGatewayUrl(
449
585
  endpoint,
450
586
  target.getForId.actorId,
451
- this.#config.token,
587
+ config.token,
452
588
  path,
453
589
  );
454
590
  }
@@ -456,14 +592,14 @@ export class RemoteEngineControlClient implements EngineControlClient {
456
592
  if ("getForKey" in target || "getOrCreateForKey" in target) {
457
593
  return buildActorQueryGatewayUrl(
458
594
  endpoint,
459
- this.#config.namespace,
595
+ config.namespace,
460
596
  target,
461
- this.#config.token,
597
+ config.token,
462
598
  path,
463
- this.#config.maxInputSize,
599
+ config.maxInputSize,
464
600
  undefined,
465
601
  "getOrCreateForKey" in target
466
- ? this.#config.poolName
602
+ ? (target.getOrCreateForKey.poolName ?? config.poolName)
467
603
  : undefined,
468
604
  options,
469
605
  );