@restatedev/sessions 0.11.2 → 0.12.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 (96) hide show
  1. package/README.md +330 -178
  2. package/dist/addon.d.ts +1 -0
  3. package/dist/addon.d.ts.map +1 -1
  4. package/dist/addon.js.map +1 -1
  5. package/dist/calls.d.ts +28 -28
  6. package/dist/calls.d.ts.map +1 -1
  7. package/dist/calls.js +25 -25
  8. package/dist/calls.js.map +1 -1
  9. package/dist/database.d.ts +90 -166
  10. package/dist/database.d.ts.map +1 -1
  11. package/dist/database.js +137 -249
  12. package/dist/database.js.map +1 -1
  13. package/dist/driver.d.ts +129 -126
  14. package/dist/driver.d.ts.map +1 -1
  15. package/dist/driver.js +337 -276
  16. package/dist/driver.js.map +1 -1
  17. package/dist/endpoint.d.ts +47 -5
  18. package/dist/endpoint.d.ts.map +1 -1
  19. package/dist/endpoint.js +175 -60
  20. package/dist/endpoint.js.map +1 -1
  21. package/dist/handler.d.ts +20 -17
  22. package/dist/handler.d.ts.map +1 -1
  23. package/dist/handler.js +161 -127
  24. package/dist/handler.js.map +1 -1
  25. package/dist/host.d.ts +29 -0
  26. package/dist/host.d.ts.map +1 -0
  27. package/dist/host.js +2 -0
  28. package/dist/host.js.map +1 -0
  29. package/dist/index.d.ts +1 -1
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +5 -26
  32. package/dist/index.js.map +1 -1
  33. package/dist/internal.d.ts +11 -11
  34. package/dist/internal.d.ts.map +1 -1
  35. package/dist/internal.js +20 -16
  36. package/dist/internal.js.map +1 -1
  37. package/dist/link.d.ts +1 -2
  38. package/dist/link.d.ts.map +1 -1
  39. package/dist/link.js +4 -5
  40. package/dist/link.js.map +1 -1
  41. package/dist/log.d.ts +14 -0
  42. package/dist/log.d.ts.map +1 -0
  43. package/dist/log.js +21 -0
  44. package/dist/log.js.map +1 -0
  45. package/dist/object.d.ts +217 -142
  46. package/dist/object.d.ts.map +1 -1
  47. package/dist/object.js +195 -241
  48. package/dist/object.js.map +1 -1
  49. package/dist/platform.d.ts +0 -1
  50. package/dist/platform.d.ts.map +1 -1
  51. package/dist/platform.js +4 -6
  52. package/dist/platform.js.map +1 -1
  53. package/dist/schema.d.ts +14 -22
  54. package/dist/schema.d.ts.map +1 -1
  55. package/dist/schema.js +31 -69
  56. package/dist/schema.js.map +1 -1
  57. package/dist/serve.d.ts +11 -17
  58. package/dist/serve.d.ts.map +1 -1
  59. package/dist/serve.js +21 -39
  60. package/dist/serve.js.map +1 -1
  61. package/dist/session.d.ts +58 -240
  62. package/dist/session.d.ts.map +1 -1
  63. package/dist/session.js +206 -532
  64. package/dist/session.js.map +1 -1
  65. package/dist/stop.d.ts +59 -0
  66. package/dist/stop.d.ts.map +1 -0
  67. package/dist/stop.js +220 -0
  68. package/dist/stop.js.map +1 -0
  69. package/dist/vm.d.ts +108 -29
  70. package/dist/vm.d.ts.map +1 -1
  71. package/dist/vm.js +43 -10
  72. package/dist/vm.js.map +1 -1
  73. package/package.json +4 -4
  74. package/src/addon.ts +1 -0
  75. package/src/calls.ts +51 -47
  76. package/src/database.ts +195 -339
  77. package/src/driver.ts +379 -315
  78. package/src/endpoint.ts +217 -62
  79. package/src/handler.ts +183 -164
  80. package/src/host.ts +28 -0
  81. package/src/index.ts +6 -26
  82. package/src/internal.ts +28 -39
  83. package/src/link.ts +4 -5
  84. package/src/log.ts +42 -0
  85. package/src/object.ts +379 -335
  86. package/src/platform.ts +4 -6
  87. package/src/schema.ts +37 -103
  88. package/src/serve.ts +20 -41
  89. package/src/session.ts +236 -629
  90. package/src/stop.ts +225 -0
  91. package/src/vm.ts +151 -55
  92. package/dist/scope.d.ts +0 -38
  93. package/dist/scope.d.ts.map +0 -1
  94. package/dist/scope.js +0 -55
  95. package/dist/scope.js.map +0 -1
  96. package/src/scope.ts +0 -78
package/src/scope.ts DELETED
@@ -1,78 +0,0 @@
1
- // An attempt's scope: what its code keeps for the attempt (an agent's turn,
2
- // the calls of its tools...), which its context holds and which ends with it.
3
- // A retry gets a context, and so a scope, of its own. A module holds only the
4
- // key of what it keeps (`ScopeKey`), never the state.
5
- import type * as restate from '@restatedev/restate-sdk';
6
- import { Link } from './link.ts';
7
- import type { SessionContext, ViewContext } from './object.ts';
8
-
9
- /** A context of an attempt: a handler's or a view's. */
10
- type Context = SessionContext | ViewContext;
11
-
12
- /** The scope's values, which only {@link ScopeKey} reaches. */
13
- const VALUES = Symbol('values');
14
-
15
- /** An attempt's own, as @restatedev/sessions made it, and what its code keeps for it ({@link ScopeKey}). */
16
- export class Scope {
17
- /** The SDK's context of the attempt. */
18
- readonly sdk: restate.ObjectContext | restate.ObjectSharedContext;
19
- /** Whether its handler has `explicitCancellation: true`. */
20
- readonly explicitCancellation: boolean;
21
- /** The definition of the session object whose handler or view it runs, if any. */
22
- readonly definition: object | undefined;
23
- readonly [VALUES] = new Map<ScopeKey<unknown>, unknown>();
24
-
25
- constructor(
26
- sdk: restate.ObjectContext | restate.ObjectSharedContext,
27
- { explicitCancellation = false, definition = undefined as object | undefined } = {},
28
- ) {
29
- this.sdk = sdk;
30
- this.explicitCancellation = explicitCancellation;
31
- this.definition = definition;
32
- }
33
- }
34
-
35
- /** The attempt's scope, on its context. */
36
- const SCOPE = new Link<Scope>('scope');
37
-
38
- /** `context`, holding `scope`: as it is made, before it is frozen. */
39
- export const holding = <C extends Context>(context: C, scope: Scope): C => SCOPE.on(context, scope);
40
-
41
- /** The scope of the attempt whose context `ctx` is, if it is one of @restatedev/sessions. */
42
- export const scopeOf = (ctx: Context): Scope | undefined => SCOPE.of(ctx);
43
-
44
- /** A kind of state an attempt's code keeps, by its context: its key in the attempt's scope. */
45
- export class ScopeKey<T> {
46
- /** What it is, for an error. */
47
- private readonly what: string;
48
-
49
- constructor(what: string) {
50
- this.what = what;
51
- }
52
-
53
- get(ctx: Context): T | undefined {
54
- return this.values(ctx).get(this) as T | undefined;
55
- }
56
-
57
- set(ctx: Context, value: T): void {
58
- this.values(ctx).set(this, value);
59
- }
60
-
61
- delete(ctx: Context): void {
62
- this.values(ctx).delete(this);
63
- }
64
-
65
- /** The attempt's, made by `make` the first time. */
66
- of(ctx: Context, make: () => T): T {
67
- const values = this.values(ctx);
68
- if (!values.has(this)) values.set(this, make());
69
- return values.get(this) as T;
70
- }
71
-
72
- private values(ctx: Context): Map<ScopeKey<unknown>, unknown> {
73
- const scope = scopeOf(ctx);
74
- if (scope === undefined)
75
- throw new TypeError(`${this.what} takes the context of a session object's handler or view`);
76
- return scope[VALUES];
77
- }
78
- }