@restatedev/restate-sdk-gen 1.14.2 → 1.14.4

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/dist/index.cjs CHANGED
@@ -5,6 +5,14 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (all$1) => {
9
+ let target = {};
10
+ for (var name in all$1) __defProp(target, name, {
11
+ get: all$1[name],
12
+ enumerable: true
13
+ });
14
+ return target;
15
+ };
8
16
  var __copyProps = (to, from, except, desc) => {
9
17
  if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
18
  key = keys[i];
@@ -23,6 +31,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
31
  //#endregion
24
32
  let __restatedev_restate_sdk = require("@restatedev/restate-sdk");
25
33
  __restatedev_restate_sdk = __toESM(__restatedev_restate_sdk);
34
+ let __restatedev_restate_sdk_clients = require("@restatedev/restate-sdk-clients");
35
+ __restatedev_restate_sdk_clients = __toESM(__restatedev_restate_sdk_clients);
36
+ let __restatedev_restate_sdk_core = require("@restatedev/restate-sdk-core");
37
+ __restatedev_restate_sdk_core = __toESM(__restatedev_restate_sdk_core);
26
38
 
27
39
  //#region src/current.ts
28
40
  let CURRENT = null;
@@ -51,6 +63,24 @@ function getCurrent() {
51
63
  return CURRENT;
52
64
  }
53
65
 
66
+ //#endregion
67
+ //#region src/invocation-reference.ts
68
+ var InvocationReferenceImpl = class {
69
+ constructor(id, _outputSerde) {
70
+ this.id = id;
71
+ this._outputSerde = _outputSerde;
72
+ }
73
+ attach(serde$1) {
74
+ return getCurrent().attach(this.id, serde$1 ?? this._outputSerde);
75
+ }
76
+ signal(name, serde$1) {
77
+ return getCurrent().invocationSignal(this.id, name, serde$1);
78
+ }
79
+ cancel() {
80
+ getCurrent().cancel(this.id);
81
+ }
82
+ };
83
+
54
84
  //#endregion
55
85
  //#region src/free.ts
56
86
  /**
@@ -62,6 +92,15 @@ function getCurrent() {
62
92
  function currentOps() {
63
93
  return getCurrent();
64
94
  }
95
+ const rand = () => currentOps().rand;
96
+ const date = () => currentOps().date;
97
+ const logger = () => currentOps().console;
98
+ /**
99
+ * Returns the current invocation's request metadata plus the optional
100
+ * virtual-object / workflow key. The `key` field is only present when
101
+ * the handler belongs to an object or workflow.
102
+ */
103
+ const handlerRequest = () => currentOps().handlerRequest();
65
104
  /**
66
105
  * Run a side-effecting closure as a journal entry. See
67
106
  * `RestateOperations.run` for full semantics.
@@ -72,24 +111,31 @@ function currentOps() {
72
111
  */
73
112
  const run = (action, opts) => currentOps().run(action, opts);
74
113
  const sleep = (duration, name) => currentOps().sleep(duration, name);
75
- const awakeable = (serde) => currentOps().awakeable(serde);
76
- const resolveAwakeable = (id, payload, serde) => currentOps().resolveAwakeable(id, payload, serde);
114
+ const awakeable = (serde$1) => currentOps().awakeable(serde$1);
115
+ const resolveAwakeable = (id, payload, serde$1) => currentOps().resolveAwakeable(id, payload, serde$1);
77
116
  const rejectAwakeable = (id, reason) => currentOps().rejectAwakeable(id, reason);
78
- const signal = (name, serde) => currentOps().signal(name, serde);
79
- const attach = (invocationId, serde) => currentOps().attach(invocationId, serde);
80
- const serviceClient = (api) => currentOps().serviceClient(api);
81
- const objectClient = (api, key) => currentOps().objectClient(api, key);
82
- const workflowClient = (api, key) => currentOps().workflowClient(api, key);
83
- const serviceSendClient = (api) => currentOps().serviceSendClient(api);
84
- const objectSendClient = (api, key) => currentOps().objectSendClient(api, key);
85
- const workflowSendClient = (api, key) => currentOps().workflowSendClient(api, key);
86
- const genericCall = (call) => currentOps().genericCall(call);
87
- const genericSend = (call) => currentOps().genericSend(call);
117
+ const signal = (name, serde$1) => currentOps().signal(name, serde$1);
118
+ const attach = (invocationId, serde$1) => currentOps().attach(invocationId, serde$1);
119
+ function client(def, key) {
120
+ return currentOps().client(def, key);
121
+ }
122
+ function sendClient(def, key) {
123
+ return currentOps().sendClient(def, key);
124
+ }
125
+ const call = (c) => currentOps().call(c);
126
+ const send = (c, outputSerde) => currentOps().send(c, outputSerde);
127
+ /**
128
+ * Create an InvocationReference from a known invocation ID (e.g. retrieved from state).
129
+ * `attach()` and `cancel()` on the result use the current fiber slot like all free functions.
130
+ */
131
+ function invocation(id, opts) {
132
+ return new InvocationReferenceImpl(id, opts?.outputSerde);
133
+ }
88
134
  const cancel = (invocationId) => currentOps().cancel(invocationId);
89
135
  const channel = () => currentOps().channel();
90
136
  const state = () => currentOps().state();
91
137
  const sharedState = () => currentOps().sharedState();
92
- const workflowPromise = (name, serde) => currentOps().workflowPromise(name, serde);
138
+ const workflowPromise = (name, serde$1) => currentOps().workflowPromise(name, serde$1);
93
139
  /**
94
140
  * Wait for every future to settle; return their values in input order.
95
141
  * Heterogeneous-tuple typing — `all([fA, fB])` where `fA: Future<A>`
@@ -115,6 +161,12 @@ const any = (futures) => currentOps().any(futures);
115
161
  * Mirrors `Promise.allSettled`.
116
162
  */
117
163
  const allSettled = (futures) => currentOps().allSettled(futures);
164
+ /**
165
+ * Register `op` as a fresh routine and return a `Future<T>` for its
166
+ * eventual outcome. Eager — the child is already in flight by the time
167
+ * `spawn` returns. See `RestateOperations.spawn` for full semantics.
168
+ */
169
+ const spawn = (op) => currentOps().spawn(op);
118
170
 
119
171
  //#endregion
120
172
  //#region src/operation.ts
@@ -143,12 +195,6 @@ function awaitRace(futures) {
143
195
  function gen(body) {
144
196
  return { [Symbol.iterator]: body };
145
197
  }
146
- function spawn(op) {
147
- return makePrimitive({
148
- _tag: "Spawn",
149
- child: op
150
- });
151
- }
152
198
  function* select(branches) {
153
199
  const tags = Object.keys(branches);
154
200
  const tag = tags[(yield* awaitRace(tags.map((t) => branches[t]))).index];
@@ -158,88 +204,6 @@ function* select(branches) {
158
204
  };
159
205
  }
160
206
 
161
- //#endregion
162
- //#region src/state.ts
163
- /**
164
- * Build a `State<TState>` over the given context. The runtime delegates
165
- * straight to `ctx.get` / `ctx.set` / etc.; the TState generic is purely
166
- * a TS-level convenience and gets erased at runtime.
167
- *
168
- * For shared (read-only) contexts, the returned State has the same
169
- * runtime shape but the caller should use the `SharedState<TState>`
170
- * type to drop the write methods. The convenience method
171
- * `RestateOperations.sharedState()` does this cast.
172
- */
173
- function makeState(ctx, sched, adapt$1) {
174
- const writeCtx = ctx;
175
- return {
176
- get(name, serde) {
177
- return sched.makeJournalFuture(adapt$1(ctx.get(name, serde)));
178
- },
179
- keys() {
180
- return sched.makeJournalFuture(adapt$1(ctx.stateKeys()));
181
- },
182
- set(name, value, serde) {
183
- writeCtx.set(name, value, serde);
184
- },
185
- clear(name) {
186
- writeCtx.clear(name);
187
- },
188
- clearAll() {
189
- writeCtx.clearAll();
190
- }
191
- };
192
- }
193
-
194
- //#endregion
195
- //#region src/clients.ts
196
- /**
197
- * Wrap an SDK `Client<M>` so each handler-invocation returns
198
- * `Future<T>` (via the supplied `toFuture` adapter) instead of
199
- * `InvocationPromise<T>`.
200
- */
201
- function wrapClient(client, toFuture) {
202
- return new Proxy(client, { get(target, prop, receiver) {
203
- const orig = Reflect.get(target, prop, receiver);
204
- if (typeof orig !== "function") return orig;
205
- return (...args) => {
206
- return toFuture(orig.apply(target, args));
207
- };
208
- } });
209
- }
210
- /**
211
- * Wrap an SDK `DurablePromise<T>` so each method returns `Future<...>`
212
- * instead of `Promise<...>`/`RestatePromise<...>`.
213
- */
214
- function wrapDurablePromise(dp, toFuture) {
215
- return {
216
- peek: () => toFuture(dp.peek()),
217
- resolve: (value) => toFuture(dp.resolve(value)),
218
- reject: (errorMsg) => toFuture(dp.reject(errorMsg)),
219
- get: () => toFuture(dp.get())
220
- };
221
- }
222
-
223
- //#endregion
224
- //#region src/default-lib.ts
225
- const defaultLib = {
226
- all(items) {
227
- return __restatedev_restate_sdk.RestatePromise.all(items);
228
- },
229
- race(items) {
230
- return __restatedev_restate_sdk.RestatePromise.race(items);
231
- },
232
- any(items) {
233
- return __restatedev_restate_sdk.RestatePromise.any(items);
234
- },
235
- allSettled(items) {
236
- return __restatedev_restate_sdk.RestatePromise.allSettled(items);
237
- },
238
- isCancellation(e) {
239
- return e instanceof __restatedev_restate_sdk.CancelledError;
240
- }
241
- };
242
-
243
207
  //#endregion
244
208
  //#region src/future.ts
245
209
  const futureBacking = Symbol("restateFutureBacking");
@@ -365,12 +329,6 @@ var Fiber = class {
365
329
  case "Leaf":
366
330
  outcome = this.parkOnLeaf(node);
367
331
  break;
368
- case "Spawn":
369
- outcome = {
370
- ok: true,
371
- v: this.sched.spawnFuture(node.child)
372
- };
373
- break;
374
332
  case "AwaitRace":
375
333
  outcome = this.parkOnAwaitRace(node.futures);
376
334
  break;
@@ -548,16 +506,20 @@ var Scheduler = class {
548
506
  lib;
549
507
  abortController = new AbortController();
550
508
  /**
551
- * Slot for the RestateOperations bound to this scheduler. Set by
552
- * `execute()` after construction (we can't pass it into the ctor
553
- * because RestateOperations needs the scheduler to construct itself).
509
+ * Slot for the operations object bound to this scheduler. Defaults
510
+ * to the scheduler itself so tests (which don't construct a
511
+ * `RestateOperations`) still get a slot exposing `.spawn`. Production
512
+ * `execute()` overrides this with a `RestateOperations` instance
513
+ * after construction (we can't pass it into the ctor because
514
+ * `RestateOperations` needs the scheduler to construct itself).
554
515
  * `Fiber.advance` publishes this to the module-level current-fiber
555
516
  * slot read by free-standing API functions. Typed `unknown` here to
556
517
  * keep this module independent of `restate-operations.ts`.
557
518
  */
558
- contextSlot = null;
519
+ contextSlot;
559
520
  constructor(lib) {
560
521
  this.lib = lib;
522
+ this.contextSlot = this;
561
523
  }
562
524
  /**
563
525
  * The scheduler's current AbortSignal. Aborts when invocation
@@ -582,12 +544,6 @@ var Scheduler = class {
582
544
  markDone(f) {
583
545
  this.fibers.delete(f);
584
546
  }
585
- spawnFuture(op) {
586
- return makeFuture({
587
- kind: "local",
588
- target: this.createFiber(op)
589
- });
590
- }
591
547
  createFiber(op) {
592
548
  const f = new Fiber(op, this);
593
549
  this.fibers.add(f);
@@ -601,13 +557,19 @@ var Scheduler = class {
601
557
  });
602
558
  }
603
559
  /**
604
- * Spawn an operation as a fresh fiber and return a Future that
605
- * resolves with its eventual value. Same as the SchedulerOps method;
606
- * exposed publicly for external callers (combinator helpers, the
607
- * main run() entry point).
560
+ * Register an Operation as a fresh fiber and return a Future that
561
+ * resolves with its eventual value. Eager: by the time this returns,
562
+ * the fiber is queued ready and will be advanced on the next drain.
563
+ *
564
+ * Used by combinator fallbacks (race, allSettled, …), by
565
+ * `RestateOperations.spawn`, and via the slot interface by the free
566
+ * `spawn` function.
608
567
  */
609
- spawnDetached(op) {
610
- return this.spawnFuture(op);
568
+ spawn(op) {
569
+ return makeFuture({
570
+ kind: "local",
571
+ target: this.createFiber(op)
572
+ });
611
573
  }
612
574
  /**
613
575
  * Construct a single-shot in-memory channel. Send must be called from
@@ -634,7 +596,7 @@ var Scheduler = class {
634
596
  const promises = fs.map((f) => f[futureBacking].promise);
635
597
  return this.makeJournalFuture(this.lib.all(promises));
636
598
  }
637
- return this.spawnDetached(gen(function* () {
599
+ return this.spawn(gen(function* () {
638
600
  const out = new Array(fs.length);
639
601
  for (let i = 0; i < fs.length; i++) out[i] = yield* fs[i];
640
602
  return out;
@@ -642,7 +604,7 @@ var Scheduler = class {
642
604
  }
643
605
  race(futures) {
644
606
  const fs = futures;
645
- return this.spawnDetached(gen(function* () {
607
+ return this.spawn(gen(function* () {
646
608
  const result = yield* awaitRace(fs);
647
609
  if (result.settled.ok) return result.settled.v;
648
610
  throw result.settled.e;
@@ -669,7 +631,7 @@ var Scheduler = class {
669
631
  const promises = fs.map((f) => f[futureBacking].promise);
670
632
  return this.makeJournalFuture(this.lib.any(promises));
671
633
  }
672
- return this.spawnDetached(gen(function* () {
634
+ return this.spawn(gen(function* () {
673
635
  const errors = new Array(fs.length);
674
636
  const remaining = /* @__PURE__ */ new Set();
675
637
  for (let i = 0; i < fs.length; i++) remaining.add(i);
@@ -703,7 +665,7 @@ var Scheduler = class {
703
665
  const promises = fs.map((f) => f[futureBacking].promise);
704
666
  return this.makeJournalFuture(this.lib.allSettled(promises));
705
667
  }
706
- return this.spawnDetached(gen(function* () {
668
+ return this.spawn(gen(function* () {
707
669
  const out = new Array(fs.length);
708
670
  for (let i = 0; i < fs.length; i++) try {
709
671
  out[i] = {
@@ -778,6 +740,137 @@ var Scheduler = class {
778
740
  }
779
741
  };
780
742
 
743
+ //#endregion
744
+ //#region src/state.ts
745
+ /**
746
+ * Build a `State<TState>` over the given context. The runtime delegates
747
+ * straight to `ctx.get` / `ctx.set` / etc.; the TState generic is purely
748
+ * a TS-level convenience and gets erased at runtime.
749
+ *
750
+ * For shared (read-only) contexts, the returned State has the same
751
+ * runtime shape but the caller should use the `SharedState<TState>`
752
+ * type to drop the write methods. The convenience method
753
+ * `RestateOperations.sharedState()` does this cast.
754
+ */
755
+ function makeState(ctx, sched, adapt$1) {
756
+ const writeCtx = ctx;
757
+ return {
758
+ get(name, serde$1) {
759
+ return sched.makeJournalFuture(adapt$1(ctx.get(name, serde$1)));
760
+ },
761
+ keys() {
762
+ return sched.makeJournalFuture(adapt$1(ctx.stateKeys()));
763
+ },
764
+ set(name, value, serde$1) {
765
+ writeCtx.set(name, value, serde$1);
766
+ },
767
+ clear(name) {
768
+ writeCtx.clear(name);
769
+ },
770
+ clearAll() {
771
+ writeCtx.clearAll();
772
+ }
773
+ };
774
+ }
775
+
776
+ //#endregion
777
+ //#region src/clients.ts
778
+ function makeClient(def, key, call$1) {
779
+ return new Proxy({}, { get(_target, methodName) {
780
+ return (...args) => {
781
+ const { parameter, opts } = optsFromArgs$1(args);
782
+ const callOpts = opts;
783
+ const desc = def._handlers[methodName];
784
+ const outputSerde = callOpts?.output ?? desc?._outputSerde;
785
+ return call$1({
786
+ service: def.name,
787
+ key,
788
+ method: String(methodName),
789
+ parameter,
790
+ inputSerde: callOpts?.input ?? desc?._inputSerde ?? __restatedev_restate_sdk.serde.json,
791
+ outputSerde: outputSerde ?? __restatedev_restate_sdk.serde.json,
792
+ idempotencyKey: callOpts?.idempotencyKey,
793
+ headers: callOpts?.headers,
794
+ name: callOpts?.name
795
+ });
796
+ };
797
+ } });
798
+ }
799
+ function makeSendClient(def, key, send$1) {
800
+ return new Proxy({}, { get(_target, methodName) {
801
+ return (...args) => {
802
+ const { parameter, opts } = optsFromArgs$1(args);
803
+ const sendOpts = opts;
804
+ const desc = def._handlers[methodName];
805
+ return send$1({
806
+ service: def.name,
807
+ key,
808
+ method: String(methodName),
809
+ parameter,
810
+ inputSerde: sendOpts?.input ?? desc?._inputSerde ?? __restatedev_restate_sdk.serde.json,
811
+ delay: sendOpts?.delay,
812
+ idempotencyKey: sendOpts?.idempotencyKey,
813
+ headers: sendOpts?.headers,
814
+ name: sendOpts?.name
815
+ }, desc?._outputSerde);
816
+ };
817
+ } });
818
+ }
819
+ function optsFromArgs$1(args) {
820
+ let parameter;
821
+ let opts;
822
+ switch (args.length) {
823
+ case 0: break;
824
+ case 1:
825
+ if (args[0] instanceof __restatedev_restate_sdk.Opts) opts = args[0].getOpts();
826
+ else if (args[0] instanceof __restatedev_restate_sdk.SendOpts) opts = args[0].getOpts();
827
+ else parameter = args[0];
828
+ break;
829
+ case 2:
830
+ parameter = args[0];
831
+ if (args[1] instanceof __restatedev_restate_sdk.Opts) opts = args[1].getOpts();
832
+ else if (args[1] instanceof __restatedev_restate_sdk.SendOpts) opts = args[1].getOpts();
833
+ else throw new TypeError("The second argument must be either Opts or SendOpts");
834
+ break;
835
+ default: throw new TypeError("unexpected number of arguments");
836
+ }
837
+ return {
838
+ parameter,
839
+ opts
840
+ };
841
+ }
842
+
843
+ //#endregion
844
+ //#region src/durable-promise.ts
845
+ function wrapDurablePromise(dp, toFuture) {
846
+ return {
847
+ peek: () => toFuture(dp.peek()),
848
+ resolve: (value) => toFuture(dp.resolve(value)),
849
+ reject: (errorMsg) => toFuture(dp.reject(errorMsg)),
850
+ get: () => toFuture(dp.get())
851
+ };
852
+ }
853
+
854
+ //#endregion
855
+ //#region src/default-lib.ts
856
+ const defaultLib = {
857
+ all(items) {
858
+ return __restatedev_restate_sdk.RestatePromise.all(items);
859
+ },
860
+ race(items) {
861
+ return __restatedev_restate_sdk.RestatePromise.race(items);
862
+ },
863
+ any(items) {
864
+ return __restatedev_restate_sdk.RestatePromise.any(items);
865
+ },
866
+ allSettled(items) {
867
+ return __restatedev_restate_sdk.RestatePromise.allSettled(items);
868
+ },
869
+ isCancellation(e) {
870
+ return e instanceof __restatedev_restate_sdk.CancelledError;
871
+ }
872
+ };
873
+
781
874
  //#endregion
782
875
  //#region src/restate-operations.ts
783
876
  function adapt(p) {
@@ -853,6 +946,18 @@ var RestateOperations = class {
853
946
  toFuture(p) {
854
947
  return this.sched.makeJournalFuture(adapt(p));
855
948
  }
949
+ get rand() {
950
+ return this.ctx.rand;
951
+ }
952
+ get date() {
953
+ return {
954
+ now: () => this.toFuture(this.ctx.date.now()),
955
+ toJSON: () => this.toFuture(this.ctx.date.toJSON())
956
+ };
957
+ }
958
+ get console() {
959
+ return this.ctx.console;
960
+ }
856
961
  /**
857
962
  * Run a side-effecting closure as a journal entry.
858
963
  *
@@ -900,57 +1005,69 @@ var RestateOperations = class {
900
1005
  sleep(duration, name) {
901
1006
  return this.sched.makeJournalFuture(adapt(this.ctx.sleep(duration, name)));
902
1007
  }
903
- awakeable(serde) {
904
- const { id, promise } = this.ctx.awakeable(serde);
1008
+ awakeable(serde$1) {
1009
+ const { id, promise } = this.ctx.awakeable(serde$1);
905
1010
  return {
906
1011
  id,
907
1012
  promise: this.sched.makeJournalFuture(adapt(promise))
908
1013
  };
909
1014
  }
910
- resolveAwakeable(id, payload, serde) {
911
- this.ctx.resolveAwakeable(id, payload, serde);
1015
+ resolveAwakeable(id, payload, serde$1) {
1016
+ this.ctx.resolveAwakeable(id, payload, serde$1);
912
1017
  }
913
1018
  rejectAwakeable(id, reason) {
914
1019
  this.ctx.rejectAwakeable(id, reason);
915
1020
  }
916
- signal(name, serde) {
917
- return this.sched.makeJournalFuture(adapt(this.ctx.signal(name, serde)));
1021
+ signal(name, serde$1) {
1022
+ return this.sched.makeJournalFuture(adapt(this.ctx.signal(name, serde$1)));
918
1023
  }
919
- attach(invocationId, serde) {
920
- return this.sched.makeJournalFuture(adapt(this.ctx.attach(invocationId, serde)));
1024
+ attach(invocationId, serde$1) {
1025
+ return this.sched.makeJournalFuture(adapt(this.ctx.attach(invocationId, serde$1)));
921
1026
  }
922
1027
  /**
923
- * Typed RPC client for a service: `ops.serviceClient(api).foo(arg)`
924
- * yields a `Future<T>` whose value is the handler's return value.
925
- *
926
- * Same shape as `ctx.serviceClient(api)` from the SDK, but each
927
- * handler-method returns `Future<T>` rather than `InvocationPromise<T>`.
1028
+ * Returns the current invocation's request metadata plus the optional
1029
+ * virtual-object / workflow key. The `key` field is only present when
1030
+ * the handler belongs to an object or workflow.
928
1031
  */
929
- serviceClient(api) {
930
- return wrapClient(this.ctx.serviceClient(api), (p) => this.toFuture(p));
931
- }
932
- /** Typed RPC client for a virtual object. See {@link serviceClient}. */
933
- objectClient(api, key) {
934
- return wrapClient(this.ctx.objectClient(api, key), (p) => this.toFuture(p));
1032
+ handlerRequest() {
1033
+ const req = this.ctx.request();
1034
+ const ctx = this.ctx;
1035
+ return {
1036
+ attemptHeaders: req.attemptHeaders,
1037
+ body: req.body,
1038
+ extraArgs: req.extraArgs,
1039
+ headers: req.headers,
1040
+ id: req.id,
1041
+ target: req.target,
1042
+ get key() {
1043
+ return ctx.key;
1044
+ }
1045
+ };
935
1046
  }
936
- /** Typed RPC client for a workflow. See {@link serviceClient}. */
937
- workflowClient(api, key) {
938
- return wrapClient(this.ctx.workflowClient(api, key), (p) => this.toFuture(p));
1047
+ client(def, key) {
1048
+ return makeClient(def, key, (o) => this.call(o));
939
1049
  }
940
- serviceSendClient(api) {
941
- return this.ctx.serviceSendClient(api);
1050
+ sendClient(def, key) {
1051
+ return makeSendClient(def, key, (o, serde$1) => this.send(o, serde$1));
942
1052
  }
943
- objectSendClient(api, key) {
944
- return this.ctx.objectSendClient(api, key);
1053
+ call(opts) {
1054
+ const restatePromise = this.ctx.genericCall(opts);
1055
+ const resultFuture = this.toFuture(restatePromise);
1056
+ const invocation$1 = this.invocationReferenceFromHandle(restatePromise, opts.outputSerde);
1057
+ return Object.assign(resultFuture, { invocation: invocation$1 });
945
1058
  }
946
- workflowSendClient(api, key) {
947
- return this.ctx.workflowSendClient(api, key);
1059
+ send(opts, outputSerde) {
1060
+ const handle = this.ctx.genericSend(opts);
1061
+ return this.invocationReferenceFromHandle(handle, outputSerde);
948
1062
  }
949
- genericCall(call) {
950
- return this.toFuture(this.ctx.genericCall(call));
1063
+ invocationReferenceFromHandle(handle, outputSerde) {
1064
+ const idFuture = this.toFuture(handle.invocationId);
1065
+ return this.sched.spawn(gen(function* () {
1066
+ return new InvocationReferenceImpl(yield* idFuture, outputSerde);
1067
+ }));
951
1068
  }
952
- genericSend(call) {
953
- return this.ctx.genericSend(call);
1069
+ invocationSignal(invocationId, name, serde$1) {
1070
+ return this.ctx.invocation(invocationId).signal(name, serde$1);
954
1071
  }
955
1072
  /**
956
1073
  * Cancel another invocation by its id. To observe cancellation
@@ -967,12 +1084,19 @@ var RestateOperations = class {
967
1084
  * `WorkflowSharedContext`). Returns a wrapper whose `peek`/`get`/
968
1085
  * `resolve`/`reject` methods return Futures.
969
1086
  */
970
- workflowPromise(name, serde) {
1087
+ workflowPromise(name, serde$1) {
971
1088
  const wfCtx = this.ctx;
972
- return wrapDurablePromise(wfCtx.promise(name, serde), (p) => this.toFuture(p));
1089
+ return wrapDurablePromise(wfCtx.promise(name, serde$1), (p) => this.toFuture(p));
973
1090
  }
1091
+ /**
1092
+ * Register `op` as a fresh routine and return a `Future<T>` for its
1093
+ * eventual outcome. Eager: by the time `spawn` returns, the child is
1094
+ * already queued and will advance on the next scheduler tick — same
1095
+ * model as `run`/`sleep`/`awakeable`. The returned Future can be
1096
+ * yielded on, handed to combinators, or stored.
1097
+ */
974
1098
  spawn(op) {
975
- return spawn(op);
1099
+ return this.sched.spawn(op);
976
1100
  }
977
1101
  /**
978
1102
  * Create a single-shot in-memory channel. Returns a Channel<T> with
@@ -1097,33 +1221,338 @@ async function execute(context, op) {
1097
1221
  return sched.run(op);
1098
1222
  }
1099
1223
 
1224
+ //#endregion
1225
+ //#region src/define.ts
1226
+ function makeDescriptor(inputSerde, outputSerde) {
1227
+ return {
1228
+ _inputSerde: inputSerde,
1229
+ _outputSerde: outputSerde
1230
+ };
1231
+ }
1232
+ /** HandlerDef has _genFn as an object property; bare gen fns are plain functions */
1233
+ function isHandlerDef(entry) {
1234
+ return typeof entry === "object" && entry !== null && typeof entry._genFn === "function";
1235
+ }
1236
+ /** Convert a Standard Schema to a Serde via restate.serde.schema() */
1237
+ function toSerde(schema) {
1238
+ return __restatedev_restate_sdk.serde.schema(schema);
1239
+ }
1240
+ function extractEntry(entry) {
1241
+ if (isHandlerDef(entry)) return {
1242
+ genFn: entry._genFn,
1243
+ inputSerde: entry._inputSerde,
1244
+ outputSerde: entry._outputSerde
1245
+ };
1246
+ return {
1247
+ genFn: entry,
1248
+ inputSerde: void 0,
1249
+ outputSerde: void 0
1250
+ };
1251
+ }
1252
+ /** serdes(opts, fn) — explicit Serde per field */
1253
+ function serdes(opts, fn) {
1254
+ return {
1255
+ _genFn: fn,
1256
+ _inputSerde: opts.input,
1257
+ _outputSerde: opts.output
1258
+ };
1259
+ }
1260
+ /** schemas(opts, fn) — Standard Schema (Zod, TypeBox, Valibot, …) per field */
1261
+ function schemas(opts, fn) {
1262
+ return {
1263
+ _genFn: fn,
1264
+ _inputSerde: toSerde(opts.input),
1265
+ _outputSerde: toSerde(opts.output)
1266
+ };
1267
+ }
1268
+ function service(config) {
1269
+ const { name, description, metadata, handlers, options } = config;
1270
+ const { handlers: perHandlerOpts,...serviceOpts } = options ?? {};
1271
+ const coreHandlers = {};
1272
+ const descriptors = {};
1273
+ for (const [handlerName, entry] of Object.entries(handlers)) {
1274
+ const { genFn, inputSerde, outputSerde } = extractEntry(entry);
1275
+ const handlerOpts = perHandlerOpts?.[handlerName] ?? {};
1276
+ coreHandlers[handlerName] = __restatedev_restate_sdk.handlers.handler({
1277
+ input: inputSerde,
1278
+ output: outputSerde,
1279
+ ...handlerOpts
1280
+ }, async (ctx, input) => execute(ctx, genFn(input)));
1281
+ descriptors[handlerName] = makeDescriptor(inputSerde, outputSerde);
1282
+ }
1283
+ const coreDef = __restatedev_restate_sdk.service({
1284
+ name,
1285
+ handlers: coreHandlers,
1286
+ description,
1287
+ metadata,
1288
+ options: serviceOpts
1289
+ });
1290
+ return Object.assign(coreDef, {
1291
+ _kind: "service",
1292
+ _handlers: descriptors
1293
+ });
1294
+ }
1295
+ function object(config) {
1296
+ const { name, description, metadata, handlers, options } = config;
1297
+ const { handlers: perHandlerOpts,...objectOpts } = options ?? {};
1298
+ const coreHandlers = {};
1299
+ const descriptors = {};
1300
+ for (const [handlerName, entry] of Object.entries(handlers)) {
1301
+ const { genFn, inputSerde, outputSerde } = extractEntry(entry);
1302
+ const { shared,...restOpts } = perHandlerOpts?.[handlerName] ?? {};
1303
+ const sdkOpts = {
1304
+ input: inputSerde,
1305
+ output: outputSerde,
1306
+ ...restOpts
1307
+ };
1308
+ const fn = async (ctx, input) => execute(ctx, genFn(input));
1309
+ coreHandlers[handlerName] = shared ? __restatedev_restate_sdk.handlers.object.shared(sdkOpts, fn) : __restatedev_restate_sdk.handlers.object.exclusive(sdkOpts, fn);
1310
+ descriptors[handlerName] = makeDescriptor(inputSerde, outputSerde);
1311
+ }
1312
+ const coreDef = __restatedev_restate_sdk.object({
1313
+ name,
1314
+ handlers: coreHandlers,
1315
+ description,
1316
+ metadata,
1317
+ options: objectOpts
1318
+ });
1319
+ return Object.assign(coreDef, {
1320
+ _kind: "object",
1321
+ _handlers: descriptors
1322
+ });
1323
+ }
1324
+ function workflow(config) {
1325
+ const { name, description, metadata, handlers, options } = config;
1326
+ const { handlers: perHandlerOpts,...workflowOpts } = options ?? {};
1327
+ const coreHandlers = {};
1328
+ const descriptors = {};
1329
+ for (const [handlerName, entry] of Object.entries(handlers)) {
1330
+ const { genFn, inputSerde, outputSerde } = extractEntry(entry);
1331
+ const sdkOpts = {
1332
+ input: inputSerde,
1333
+ output: outputSerde,
1334
+ ...perHandlerOpts?.[handlerName] ?? {}
1335
+ };
1336
+ const fn = async (ctx, input) => execute(ctx, genFn(input));
1337
+ coreHandlers[handlerName] = handlerName === "run" ? __restatedev_restate_sdk.handlers.workflow.workflow(sdkOpts, fn) : __restatedev_restate_sdk.handlers.workflow.shared(sdkOpts, fn);
1338
+ descriptors[handlerName] = makeDescriptor(inputSerde, outputSerde);
1339
+ }
1340
+ const coreDef = __restatedev_restate_sdk.workflow({
1341
+ name,
1342
+ handlers: coreHandlers,
1343
+ description,
1344
+ metadata,
1345
+ options: workflowOpts
1346
+ });
1347
+ return Object.assign(coreDef, {
1348
+ _kind: "workflow",
1349
+ _handlers: descriptors
1350
+ });
1351
+ }
1352
+
1353
+ //#endregion
1354
+ //#region src/interface.ts
1355
+ var interface_exports = /* @__PURE__ */ __export({
1356
+ implement: () => implement,
1357
+ json: () => json,
1358
+ object: () => object$1,
1359
+ schemas: () => schemas$1,
1360
+ serdes: () => serdes$1,
1361
+ service: () => service$1,
1362
+ workflow: () => workflow$1
1363
+ });
1364
+ /** json<I, O>() — type params, default JSON serde */
1365
+ function json() {
1366
+ return makeDescriptor(void 0, void 0);
1367
+ }
1368
+ /** serdes(opts) — explicit Serde per field */
1369
+ function serdes$1(opts) {
1370
+ return makeDescriptor(opts.input, opts.output);
1371
+ }
1372
+ /** schemas(opts) — Standard Schema (Zod, TypeBox, Valibot, …) per field */
1373
+ function schemas$1(opts) {
1374
+ return makeDescriptor(opts.input ? toSerde(opts.input) : void 0, opts.output ? toSerde(opts.output) : void 0);
1375
+ }
1376
+ function service$1(name, handlers) {
1377
+ return {
1378
+ name,
1379
+ _kind: "service",
1380
+ _handlers: handlers
1381
+ };
1382
+ }
1383
+ function object$1(name, handlers) {
1384
+ return {
1385
+ name,
1386
+ _kind: "object",
1387
+ _handlers: handlers
1388
+ };
1389
+ }
1390
+ function workflow$1(name, handlers) {
1391
+ return {
1392
+ name,
1393
+ _kind: "workflow",
1394
+ _handlers: handlers
1395
+ };
1396
+ }
1397
+ function implement(iface, config) {
1398
+ const handlerEntries = {};
1399
+ for (const [name, desc] of Object.entries(iface._handlers)) {
1400
+ const genFn = config.handlers[name];
1401
+ if (!genFn) throw new Error(`implement(): missing handler "${name}"`);
1402
+ handlerEntries[name] = {
1403
+ _genFn: genFn,
1404
+ _inputSerde: desc._inputSerde,
1405
+ _outputSerde: desc._outputSerde
1406
+ };
1407
+ }
1408
+ if (iface._kind === "service") return service({
1409
+ name: iface.name,
1410
+ handlers: handlerEntries,
1411
+ options: config.options
1412
+ });
1413
+ else if (iface._kind === "object") return object({
1414
+ name: iface.name,
1415
+ handlers: handlerEntries,
1416
+ options: config.options
1417
+ });
1418
+ else return workflow({
1419
+ name: iface.name,
1420
+ handlers: handlerEntries,
1421
+ options: config.options
1422
+ });
1423
+ }
1424
+
1425
+ //#endregion
1426
+ //#region src/ingress.ts
1427
+ var ingress_exports = /* @__PURE__ */ __export({
1428
+ SendOpts: () => __restatedev_restate_sdk_clients.SendOpts,
1429
+ client: () => client$1,
1430
+ connect: () => connect,
1431
+ sendClient: () => sendClient$1
1432
+ });
1433
+ /**
1434
+ * Connect to the Restate Ingress.
1435
+ *
1436
+ * @param opts connection options
1437
+ * @returns a connection the the restate ingress
1438
+ */
1439
+ function connect(opts) {
1440
+ return (0, __restatedev_restate_sdk_clients.connect)(opts);
1441
+ }
1442
+ function client$1(ingress, def, key) {
1443
+ return new Proxy({}, { get(_target, methodName) {
1444
+ return (...args) => {
1445
+ const { parameter, opts } = optsFromArgs(args);
1446
+ const desc = def._handlers[methodName];
1447
+ const mergedOpts = __restatedev_restate_sdk_clients.Opts.from({
1448
+ ...opts?.opts,
1449
+ input: opts?.opts?.input ?? desc?._inputSerde,
1450
+ output: opts?.opts?.output ?? desc?._outputSerde
1451
+ });
1452
+ return ingress.call({
1453
+ service: def.name,
1454
+ handler: methodName,
1455
+ parameter,
1456
+ key,
1457
+ opts: mergedOpts
1458
+ });
1459
+ };
1460
+ } });
1461
+ }
1462
+ function sendClient$1(ingress, def, key) {
1463
+ return new Proxy({}, { get(_target, methodName) {
1464
+ return (...args) => {
1465
+ const { parameter, opts } = optsFromArgs(args);
1466
+ const desc = def._handlers[methodName];
1467
+ const mergedOpts = __restatedev_restate_sdk_clients.SendOpts.from({
1468
+ ...opts?.opts,
1469
+ input: opts?.opts?.input ?? desc?._inputSerde
1470
+ });
1471
+ return ingress.send({
1472
+ service: def.name,
1473
+ handler: methodName,
1474
+ parameter,
1475
+ key,
1476
+ opts: mergedOpts
1477
+ });
1478
+ };
1479
+ } });
1480
+ }
1481
+ function optsFromArgs(args) {
1482
+ let parameter;
1483
+ let opts;
1484
+ switch (args.length) {
1485
+ case 0: break;
1486
+ case 1:
1487
+ if (args[0] instanceof __restatedev_restate_sdk_clients.Opts) opts = args[0];
1488
+ else if (args[0] instanceof __restatedev_restate_sdk_clients.SendOpts) opts = args[0];
1489
+ else parameter = args[0];
1490
+ break;
1491
+ case 2:
1492
+ parameter = args[0];
1493
+ if (args[1] instanceof __restatedev_restate_sdk_clients.Opts) opts = args[1];
1494
+ else if (args[1] instanceof __restatedev_restate_sdk_clients.SendOpts) opts = args[1];
1495
+ else throw new TypeError("The second argument must be either Opts or SendOpts");
1496
+ break;
1497
+ default: throw new TypeError("unexpected number of arguments");
1498
+ }
1499
+ return {
1500
+ parameter,
1501
+ opts
1502
+ };
1503
+ }
1504
+
1100
1505
  //#endregion
1101
1506
  exports.all = all;
1102
1507
  exports.allSettled = allSettled;
1103
1508
  exports.any = any;
1104
1509
  exports.attach = attach;
1105
1510
  exports.awakeable = awakeable;
1511
+ exports.call = call;
1106
1512
  exports.cancel = cancel;
1107
1513
  exports.channel = channel;
1108
- exports.execute = execute;
1514
+ exports.client = client;
1515
+ Object.defineProperty(exports, 'clients', {
1516
+ enumerable: true,
1517
+ get: function () {
1518
+ return ingress_exports;
1519
+ }
1520
+ });
1521
+ exports.date = date;
1109
1522
  exports.gen = gen;
1110
- exports.genericCall = genericCall;
1111
- exports.genericSend = genericSend;
1112
- exports.objectClient = objectClient;
1113
- exports.objectSendClient = objectSendClient;
1523
+ exports.handlerRequest = handlerRequest;
1524
+ Object.defineProperty(exports, 'iface', {
1525
+ enumerable: true,
1526
+ get: function () {
1527
+ return interface_exports;
1528
+ }
1529
+ });
1530
+ exports.implement = implement;
1531
+ exports.invocation = invocation;
1532
+ exports.logger = logger;
1533
+ exports.object = object;
1114
1534
  exports.race = race;
1535
+ exports.rand = rand;
1115
1536
  exports.rejectAwakeable = rejectAwakeable;
1116
1537
  exports.resolveAwakeable = resolveAwakeable;
1117
1538
  exports.run = run;
1539
+ exports.schemas = schemas;
1118
1540
  exports.select = select;
1119
- exports.serviceClient = serviceClient;
1120
- exports.serviceSendClient = serviceSendClient;
1541
+ exports.send = send;
1542
+ exports.sendClient = sendClient;
1543
+ Object.defineProperty(exports, 'serde', {
1544
+ enumerable: true,
1545
+ get: function () {
1546
+ return __restatedev_restate_sdk_core.serde;
1547
+ }
1548
+ });
1549
+ exports.serdes = serdes;
1550
+ exports.service = service;
1121
1551
  exports.sharedState = sharedState;
1122
1552
  exports.signal = signal;
1123
1553
  exports.sleep = sleep;
1124
1554
  exports.spawn = spawn;
1125
1555
  exports.state = state;
1126
- exports.workflowClient = workflowClient;
1556
+ exports.workflow = workflow;
1127
1557
  exports.workflowPromise = workflowPromise;
1128
- exports.workflowSendClient = workflowSendClient;
1129
1558
  exports.wrapActionForCancellation = wrapActionForCancellation;