@demlik/tea 0.1.2 → 0.2.0

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 (124) hide show
  1. package/dist/agent/index.d.ts +2 -2
  2. package/dist/agent/index.js +8 -8
  3. package/dist/authed-call/index.d.ts +2 -2
  4. package/dist/authed-call/index.js +5 -5
  5. package/dist/await-terminal/index.d.ts +1 -1
  6. package/dist/await-terminal/index.js +2 -2
  7. package/dist/batch-window/index.d.ts +2 -2
  8. package/dist/batch-window/index.js +3 -3
  9. package/dist/cache/index.d.ts +2 -2
  10. package/dist/{chunk-EHIX7NYZ.js → chunk-5KVHID6A.js} +4 -4
  11. package/dist/{chunk-EHIX7NYZ.js.map → chunk-5KVHID6A.js.map} +1 -1
  12. package/dist/{chunk-27N2SPTN.js → chunk-6GIGQT6C.js} +4 -4
  13. package/dist/{chunk-27N2SPTN.js.map → chunk-6GIGQT6C.js.map} +1 -1
  14. package/dist/{chunk-3XJWC7VW.js → chunk-6OUDPSSG.js} +3 -3
  15. package/dist/{chunk-3XJWC7VW.js.map → chunk-6OUDPSSG.js.map} +1 -1
  16. package/dist/{chunk-MMH7CW5K.js → chunk-6VDCTR7N.js} +24 -27
  17. package/dist/chunk-6VDCTR7N.js.map +1 -0
  18. package/dist/{chunk-QLA76GIT.js → chunk-DYRKDCJI.js} +30 -5
  19. package/dist/chunk-DYRKDCJI.js.map +1 -0
  20. package/dist/{chunk-LP7VQBRG.js → chunk-EGWTT5XJ.js} +3 -3
  21. package/dist/{chunk-LP7VQBRG.js.map → chunk-EGWTT5XJ.js.map} +1 -1
  22. package/dist/{chunk-DILMKOZ7.js → chunk-EMJTPHI7.js} +4 -4
  23. package/dist/{chunk-DILMKOZ7.js.map → chunk-EMJTPHI7.js.map} +1 -1
  24. package/dist/{chunk-5DEQ5I3U.js → chunk-FOGL3TC7.js} +3 -3
  25. package/dist/{chunk-5DEQ5I3U.js.map → chunk-FOGL3TC7.js.map} +1 -1
  26. package/dist/{chunk-SQTCS44W.js → chunk-G2U7DJHL.js} +3 -3
  27. package/dist/{chunk-SQTCS44W.js.map → chunk-G2U7DJHL.js.map} +1 -1
  28. package/dist/{chunk-ON24RKSE.js → chunk-IK4S5DJN.js} +3 -3
  29. package/dist/{chunk-ON24RKSE.js.map → chunk-IK4S5DJN.js.map} +1 -1
  30. package/dist/{chunk-PS23H6KQ.js → chunk-M6UZT7HS.js} +3 -3
  31. package/dist/{chunk-PS23H6KQ.js.map → chunk-M6UZT7HS.js.map} +1 -1
  32. package/dist/{chunk-K7Q23EQ4.js → chunk-SKAMZKTE.js} +3 -3
  33. package/dist/{chunk-K7Q23EQ4.js.map → chunk-SKAMZKTE.js.map} +1 -1
  34. package/dist/{chunk-S3FQN4SL.js → chunk-VFGKTGGC.js} +7 -7
  35. package/dist/{chunk-S3FQN4SL.js.map → chunk-VFGKTGGC.js.map} +1 -1
  36. package/dist/{chunk-7W4LMPUW.js → chunk-ZDRN3YCE.js} +4 -4
  37. package/dist/{chunk-7W4LMPUW.js.map → chunk-ZDRN3YCE.js.map} +1 -1
  38. package/dist/{core-BoB6t7dB.d.ts → core-BmlJOvxM.d.ts} +10 -1
  39. package/dist/deadline/index.d.ts +2 -2
  40. package/dist/deadline/index.js +3 -3
  41. package/dist/do/index.d.ts +451 -335
  42. package/dist/do/index.js +172 -144
  43. package/dist/do/index.js.map +1 -1
  44. package/dist/{durable-effects-CO9Uh971.d.ts → durable-effects-9LxFpmh3.d.ts} +1 -1
  45. package/dist/extension/index.d.ts +1 -1
  46. package/dist/extension/index.js +3 -3
  47. package/dist/extension/react.js +3 -3
  48. package/dist/extension/subs/index.d.ts +2 -2
  49. package/dist/fan-out/index.d.ts +1 -1
  50. package/dist/idempotent-intake/index.d.ts +1 -1
  51. package/dist/index.d.ts +20 -3
  52. package/dist/index.js +2 -2
  53. package/dist/llm-call/index.d.ts +2 -2
  54. package/dist/llm-call/index.js +5 -5
  55. package/dist/machine-viz/index.d.ts +1 -1
  56. package/dist/machine-viz/index.js +2 -2
  57. package/dist/mem/index.d.ts +1 -1
  58. package/dist/monitored-run/index.d.ts +2 -2
  59. package/dist/monitored-run/index.js +5 -5
  60. package/dist/node/index.d.ts +1 -1
  61. package/dist/paginated-walk/index.d.ts +2 -2
  62. package/dist/paginated-walk/index.js +5 -5
  63. package/dist/parity/index.d.ts +1 -1
  64. package/dist/parity/index.js +3 -3
  65. package/dist/pbt/arbitraries/index.d.ts +1 -1
  66. package/dist/pbt/index.d.ts +1 -1
  67. package/dist/pbt/index.js +3 -3
  68. package/dist/pbt/runners/index.d.ts +1 -1
  69. package/dist/pbt/runners/index.js +3 -3
  70. package/dist/poller/index.d.ts +2 -2
  71. package/dist/poller/index.js +3 -3
  72. package/dist/prediction/index.d.ts +1 -1
  73. package/dist/prediction/index.js +2 -2
  74. package/dist/pure/index.d.ts +1 -1
  75. package/dist/pure/index.js +2 -2
  76. package/dist/react/index.d.ts +1 -1
  77. package/dist/react/index.js +2 -2
  78. package/dist/reconciler/index.d.ts +2 -2
  79. package/dist/reconciler/index.js +5 -5
  80. package/dist/recorder/index.d.ts +1 -1
  81. package/dist/resilient-call/index.d.ts +2 -2
  82. package/dist/resilient-call/index.js +4 -4
  83. package/dist/saga/index.d.ts +1 -1
  84. package/dist/snapshot/index.d.ts +1 -1
  85. package/dist/snapshot/index.js +3 -3
  86. package/dist/subs/index.d.ts +2 -2
  87. package/dist/testing/index.d.ts +1 -1
  88. package/dist/testing/index.js +2 -2
  89. package/dist/throttled-input/index.d.ts +2 -2
  90. package/dist/throttled-input/index.js +4 -4
  91. package/dist/token-refresh/index.d.ts +1 -1
  92. package/dist/token-refresh/index.js +3 -3
  93. package/dist/trace-replay/index.d.ts +1 -1
  94. package/dist/trace-replay/index.js +3 -3
  95. package/dist/{types-B-8bcAp8.d.ts → types-BoUWgwSm.d.ts} +1 -1
  96. package/dist/with-deadline/index.d.ts +1 -1
  97. package/dist/with-deadline/index.js +2 -2
  98. package/dist/with-resilience/index.d.ts +2 -2
  99. package/dist/with-resilience/index.js +4 -4
  100. package/dist/with-telemetry/index.d.ts +1 -1
  101. package/dist/with-telemetry/index.js +2 -2
  102. package/dist/work-queue/adapter.d.ts +1 -1
  103. package/dist/work-queue/index.d.ts +1 -1
  104. package/dist/work-queue/ops.d.ts +1 -1
  105. package/dist/workflow/index.d.ts +3 -3
  106. package/package.json +1 -19
  107. package/dist/chunk-6RGBKODV.js +0 -268
  108. package/dist/chunk-6RGBKODV.js.map +0 -1
  109. package/dist/chunk-MMH7CW5K.js.map +0 -1
  110. package/dist/chunk-QLA76GIT.js.map +0 -1
  111. package/dist/chunk-VLGARN6H.js +0 -202
  112. package/dist/chunk-VLGARN6H.js.map +0 -1
  113. package/dist/raft/demo.d.ts +0 -105
  114. package/dist/raft/demo.js +0 -172
  115. package/dist/raft/demo.js.map +0 -1
  116. package/dist/raft/index.d.ts +0 -548
  117. package/dist/raft/index.js +0 -7
  118. package/dist/raft/index.js.map +0 -1
  119. package/dist/raft/sim.d.ts +0 -222
  120. package/dist/raft/sim.js +0 -8
  121. package/dist/raft/sim.js.map +0 -1
  122. package/dist/raft/timeline.d.ts +0 -46
  123. package/dist/raft/timeline.js +0 -62
  124. package/dist/raft/timeline.js.map +0 -1
package/dist/do/index.js CHANGED
@@ -1,13 +1,13 @@
1
- import { status, agentBootMsg, agentEvents } from '../chunk-S3FQN4SL.js';
2
- import '../chunk-7W4LMPUW.js';
3
- import '../chunk-PS23H6KQ.js';
1
+ import { status, agentBootMsg, agentEvents } from '../chunk-VFGKTGGC.js';
4
2
  export { applyEffectEvent, emptyLedger, foldLedger, isOwed, pendingEffectsLedger, survivingEffects } from '../chunk-3545BOYL.js';
5
- import '../chunk-EHIX7NYZ.js';
6
- import '../chunk-27N2SPTN.js';
3
+ import '../chunk-ZDRN3YCE.js';
4
+ import '../chunk-M6UZT7HS.js';
5
+ import '../chunk-5KVHID6A.js';
6
+ import '../chunk-6GIGQT6C.js';
7
7
  import '../chunk-JELSZN25.js';
8
8
  import '../chunk-UV6UWGEO.js';
9
9
  import { createIntake } from '../chunk-LEC4U6CX.js';
10
- import '../chunk-K7Q23EQ4.js';
10
+ import '../chunk-SKAMZKTE.js';
11
11
  import { defaultRetryPolicy, initRetry, recordFailure, shouldRetry, nextDelayMs } from '../chunk-5VNWZADP.js';
12
12
  import '../chunk-2YOE5PIS.js';
13
13
  import '../chunk-BYQUGDD3.js';
@@ -18,8 +18,8 @@ import '../chunk-HJO6VNQI.js';
18
18
  import '../chunk-TGG26XMK.js';
19
19
  import '../chunk-NSDFA6M6.js';
20
20
  import '../chunk-2YFLOCWX.js';
21
- import { run, replay } from '../chunk-MMH7CW5K.js';
22
- import '../chunk-QLA76GIT.js';
21
+ import { run, replay } from '../chunk-6VDCTR7N.js';
22
+ import '../chunk-DYRKDCJI.js';
23
23
 
24
24
  // src/do/durable-timer.ts
25
25
  function durableTimer(config) {
@@ -147,87 +147,7 @@ function doEventSourcedStore(storage, machine, ctx, opts = {}) {
147
147
  return { store, append, snapshotNow, readEvents };
148
148
  }
149
149
 
150
- // src/do/host.ts
151
- function deferredGateway() {
152
- const pending = /* @__PURE__ */ new Map();
153
- return {
154
- await(callId, send, deadlineMs) {
155
- const existing = pending.get(callId);
156
- if (existing !== void 0) return existing.promise;
157
- let resolve;
158
- let reject;
159
- const promise = new Promise((res, rej) => {
160
- resolve = res;
161
- reject = rej;
162
- });
163
- const timer = setTimeout(() => {
164
- if (pending.delete(callId)) {
165
- reject(new Error(`deadline_exceeded after ${deadlineMs}ms`));
166
- }
167
- }, deadlineMs);
168
- pending.set(callId, {
169
- resolve: (v) => {
170
- clearTimeout(timer);
171
- resolve(v);
172
- },
173
- reject: (e) => {
174
- clearTimeout(timer);
175
- reject(e);
176
- },
177
- promise
178
- });
179
- send();
180
- return promise;
181
- },
182
- settle(callId, result) {
183
- const entry = pending.get(callId);
184
- if (entry === void 0) return;
185
- pending.delete(callId);
186
- entry.resolve(result);
187
- },
188
- fail(callId, reason) {
189
- const entry = pending.get(callId);
190
- if (entry === void 0) return;
191
- pending.delete(callId);
192
- entry.reject(new Error(reason));
193
- },
194
- inFlight() {
195
- return [...pending.keys()];
196
- }
197
- };
198
- }
199
- function durableDeferredGateway(inner, recorder, hooks) {
200
- const idByCallId = /* @__PURE__ */ new Map();
201
- return {
202
- recorder,
203
- await(callId, send, deadlineMs) {
204
- if (!idByCallId.has(callId)) {
205
- const { id, event } = recorder.owe({ callId });
206
- idByCallId.set(callId, id);
207
- hooks.recordOwed(callId, event);
208
- }
209
- return inner.await(callId, send, deadlineMs);
210
- },
211
- settle(callId, result) {
212
- confirm(callId);
213
- inner.settle(callId, result);
214
- },
215
- fail(callId, reason) {
216
- confirm(callId);
217
- inner.fail(callId, reason);
218
- },
219
- inFlight() {
220
- return inner.inFlight();
221
- }
222
- };
223
- function confirm(callId) {
224
- const id = idByCallId.get(callId);
225
- if (id === void 0) return;
226
- idByCallId.delete(callId);
227
- const { event } = recorder.confirm(id);
228
- hooks.recordConfirmed(callId, event);
229
- }
230
- }
150
+ // src/do/resume.ts
231
151
  async function bootResume(booting, port, now = Date.now) {
232
152
  const runtime = await booting.ready;
233
153
  if (port.isResumable(runtime.getState())) {
@@ -244,44 +164,6 @@ async function autoBoot(booting, now = Date.now) {
244
164
  now
245
165
  );
246
166
  }
247
- function acceptCommandSocket(clients, onFrame) {
248
- const pair = new WebSocketPair();
249
- const [client, server] = [pair[0], pair[1]];
250
- server.accept();
251
- clients.add(server);
252
- server.addEventListener("message", (ev) => {
253
- if (typeof ev.data === "string") onFrame(ev.data);
254
- });
255
- server.addEventListener("close", () => clients.delete(server));
256
- return new Response(null, { status: 101, webSocket: client });
257
- }
258
- function broadcast(clients, frame) {
259
- const json = JSON.stringify(frame);
260
- for (const ws of clients) {
261
- try {
262
- ws.send(json);
263
- } catch {
264
- }
265
- }
266
- }
267
- function acceptDurableCommandSocket(ctx) {
268
- const pair = new WebSocketPair();
269
- const [client, server] = [pair[0], pair[1]];
270
- ctx.acceptWebSocket(server);
271
- return new Response(null, { status: 101, webSocket: client });
272
- }
273
- function broadcastHibernatable(ctx, frame) {
274
- const json = JSON.stringify(frame);
275
- for (const ws of ctx.getWebSockets()) {
276
- try {
277
- ws.send(json);
278
- } catch {
279
- }
280
- }
281
- }
282
- function durableCommandCarrier(inner, recorder, hooks) {
283
- return durableDeferredGateway(inner, recorder, hooks);
284
- }
285
167
  function reissueSurvivingEffects(carrier, reissue) {
286
168
  const reemitted = [];
287
169
  for (const { effect } of carrier.recorder.surviving()) {
@@ -290,6 +172,8 @@ function reissueSurvivingEffects(carrier, reissue) {
290
172
  }
291
173
  return reemitted;
292
174
  }
175
+
176
+ // src/do/sse.ts
293
177
  function sseHub() {
294
178
  const sinks = /* @__PURE__ */ new Set();
295
179
  const encoder = new TextEncoder();
@@ -361,6 +245,8 @@ function sseFromAgentEvents(runtime, hub, toFrame) {
361
245
  for (const off of offs) off();
362
246
  };
363
247
  }
248
+
249
+ // src/do/agent-host.ts
364
250
  function createAgentHost(config) {
365
251
  const sse = sseHub();
366
252
  const now = config.now ?? Date.now;
@@ -396,6 +282,128 @@ function createAgentHost(config) {
396
282
  };
397
283
  }
398
284
 
285
+ // src/do/command-socket.ts
286
+ function acceptCommandSocket(clients, onFrame) {
287
+ const pair = new WebSocketPair();
288
+ const [client, server] = [pair[0], pair[1]];
289
+ server.accept();
290
+ clients.add(server);
291
+ server.addEventListener("message", (ev) => {
292
+ if (typeof ev.data === "string") onFrame(ev.data);
293
+ });
294
+ server.addEventListener("close", () => clients.delete(server));
295
+ return new Response(null, { status: 101, webSocket: client });
296
+ }
297
+ function broadcast(clients, frame) {
298
+ const json = JSON.stringify(frame);
299
+ for (const ws of clients) {
300
+ try {
301
+ ws.send(json);
302
+ } catch {
303
+ }
304
+ }
305
+ }
306
+ function acceptDurableCommandSocket(ctx) {
307
+ const pair = new WebSocketPair();
308
+ const [client, server] = [pair[0], pair[1]];
309
+ ctx.acceptWebSocket(server);
310
+ return new Response(null, { status: 101, webSocket: client });
311
+ }
312
+ function broadcastHibernatable(ctx, frame) {
313
+ const json = JSON.stringify(frame);
314
+ for (const ws of ctx.getWebSockets()) {
315
+ try {
316
+ ws.send(json);
317
+ } catch {
318
+ }
319
+ }
320
+ }
321
+
322
+ // src/do/deferred-gateway.ts
323
+ function deferredGateway() {
324
+ const pending = /* @__PURE__ */ new Map();
325
+ return {
326
+ await(callId, send, deadlineMs) {
327
+ const existing = pending.get(callId);
328
+ if (existing !== void 0) return existing.promise;
329
+ let resolve;
330
+ let reject;
331
+ const promise = new Promise((res, rej) => {
332
+ resolve = res;
333
+ reject = rej;
334
+ });
335
+ const timer = setTimeout(() => {
336
+ if (pending.delete(callId)) {
337
+ reject(new Error(`deadline_exceeded after ${deadlineMs}ms`));
338
+ }
339
+ }, deadlineMs);
340
+ pending.set(callId, {
341
+ resolve: (v) => {
342
+ clearTimeout(timer);
343
+ resolve(v);
344
+ },
345
+ reject: (e) => {
346
+ clearTimeout(timer);
347
+ reject(e);
348
+ },
349
+ promise
350
+ });
351
+ send();
352
+ return promise;
353
+ },
354
+ settle(callId, result) {
355
+ const entry = pending.get(callId);
356
+ if (entry === void 0) return;
357
+ pending.delete(callId);
358
+ entry.resolve(result);
359
+ },
360
+ fail(callId, reason) {
361
+ const entry = pending.get(callId);
362
+ if (entry === void 0) return;
363
+ pending.delete(callId);
364
+ entry.reject(new Error(reason));
365
+ },
366
+ inFlight() {
367
+ return [...pending.keys()];
368
+ }
369
+ };
370
+ }
371
+ function durableDeferredGateway(inner, recorder, hooks) {
372
+ const idByCallId = /* @__PURE__ */ new Map();
373
+ return {
374
+ recorder,
375
+ await(callId, send, deadlineMs) {
376
+ if (!idByCallId.has(callId)) {
377
+ const { id, event } = recorder.owe({ callId });
378
+ idByCallId.set(callId, id);
379
+ hooks.recordOwed(callId, event);
380
+ }
381
+ return inner.await(callId, send, deadlineMs);
382
+ },
383
+ settle(callId, result) {
384
+ confirm(callId);
385
+ inner.settle(callId, result);
386
+ },
387
+ fail(callId, reason) {
388
+ confirm(callId);
389
+ inner.fail(callId, reason);
390
+ },
391
+ inFlight() {
392
+ return inner.inFlight();
393
+ }
394
+ };
395
+ function confirm(callId) {
396
+ const id = idByCallId.get(callId);
397
+ if (id === void 0) return;
398
+ idByCallId.delete(callId);
399
+ const { event } = recorder.confirm(id);
400
+ hooks.recordConfirmed(callId, event);
401
+ }
402
+ }
403
+ function durableCommandCarrier(inner, recorder, hooks) {
404
+ return durableDeferredGateway(inner, recorder, hooks);
405
+ }
406
+
399
407
  // src/do/idempotent-effect.ts
400
408
  function emptyApplied() {
401
409
  return /* @__PURE__ */ new Set();
@@ -593,8 +601,12 @@ function constantTimeEqual(a, b) {
593
601
  return diff === 0 && lengthMatches;
594
602
  }
595
603
  var DEFAULT_GIVE_UP_MS = 5 * 6e4;
604
+ function workingBody(retryAfterMs) {
605
+ return retryAfterMs === void 0 ? { done: false, working: true } : { done: false, working: true, retryAfterMs };
606
+ }
596
607
  function stepHost(ctx, engine, config = {}) {
597
608
  const giveUpAfterMs = config.giveUpAfterMs ?? DEFAULT_GIVE_UP_MS;
609
+ const deferResume = "deferResume" in config ? config.deferResume : null;
598
610
  const intake = createIntake({
599
611
  keyOf: (posted) => posted.callId
600
612
  });
@@ -610,23 +622,31 @@ function stepHost(ctx, engine, config = {}) {
610
622
  await ctx.storage.setAlarm(now + giveUpAfterMs);
611
623
  const posted = request.posted;
612
624
  if (posted !== null) {
613
- const [nextIntake, cmds] = intake.receive(
614
- intakeState,
615
- posted,
616
- now,
617
- posted.callId
618
- );
619
- intakeState = nextIntake;
620
- for (const cmd of cmds) {
621
- if (cmd.type === "intake:process") {
622
- await engine.resume(request.runId, posted);
623
- const [completed] = intake.complete(
624
- intakeState,
625
- cmd.key,
626
- "settled",
627
- now
628
- );
629
- intakeState = completed;
625
+ if (deferResume === null) {
626
+ const [nextIntake, cmds] = intake.receive(
627
+ intakeState,
628
+ posted,
629
+ now,
630
+ posted.callId
631
+ );
632
+ intakeState = nextIntake;
633
+ for (const cmd of cmds) {
634
+ if (cmd.type === "intake:process") {
635
+ await engine.resume(request.runId, posted);
636
+ const [completed] = intake.complete(
637
+ intakeState,
638
+ cmd.key,
639
+ "settled",
640
+ now
641
+ );
642
+ intakeState = completed;
643
+ }
644
+ }
645
+ } else {
646
+ const ready = await deferResume.settled(request.runId, posted.callId);
647
+ if (!ready) {
648
+ await deferResume.enqueue(request.runId, posted);
649
+ return { status: 200, body: workingBody(deferResume.retryAfterMs) };
630
650
  }
631
651
  }
632
652
  }
@@ -646,6 +666,7 @@ async function runStepLoop(token, runId, transport, execute, config) {
646
666
  const rng = config.rng;
647
667
  const executed = /* @__PURE__ */ new Map();
648
668
  let posted = null;
669
+ let pollRetry = initRetry();
649
670
  for (; ; ) {
650
671
  if (now() >= config.deadlineMs) return { kind: "deadline_exceeded" };
651
672
  let response;
@@ -662,6 +683,13 @@ async function runStepLoop(token, runId, transport, execute, config) {
662
683
  }
663
684
  }
664
685
  if (response.done) return { kind: "done", output: response.output };
686
+ if ("working" in response) {
687
+ pollRetry = recordFailure(pollRetry, "working");
688
+ if (now() >= config.deadlineMs) return { kind: "deadline_exceeded" };
689
+ await sleep(response.retryAfterMs ?? nextDelayMs(pollRetry, policy, rng));
690
+ continue;
691
+ }
692
+ pollRetry = initRetry();
665
693
  const step = response.step;
666
694
  const cached = executed.get(step.callId);
667
695
  const result = cached !== void 0 ? cached : await execute(step);