@restatedev/sessions 0.2.0 → 0.3.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.
package/src/schema.ts CHANGED
@@ -21,6 +21,7 @@
21
21
  // in a schema of their own name, which `schema.ensure(ctx.db)` applies where
22
22
  // they use them, in any object: internal, from @restatedev/sessions/internal.
23
23
  import type { Connection, DatabaseReader, DatabaseWriter, ConnectionTransaction } from './database.ts';
24
+ import { Link } from './link.ts';
24
25
 
25
26
  /**
26
27
  * The database's record of its schemas: how many steps of each it has, by
@@ -56,22 +57,22 @@ export class Schema {
56
57
  * handler's are its own, vanishing with its session.
57
58
  */
58
59
  ensure(db: DatabaseReader | Connection): Promise<void> {
59
- const session = sessions.get(db);
60
- return session ? session([this]) : apply(db as Connection, [this]);
60
+ const ensure = ensureOf(db);
61
+ return ensure ? ensure([this]) : apply(db as Connection, [this]);
61
62
  }
62
63
  }
63
64
 
64
- /** The handlers' sessions (`ctx.db`), each with how it applies schemas. */
65
- export const sessions = new WeakMap<object, (schemas: readonly Schema[]) => Promise<void>>();
65
+ /** How a database applies schemas: a handler's (`ctx.db`, its reader), or one of `asDatabase`. */
66
+ type Ensure = (schemas: readonly Schema[]) => Promise<void>;
66
67
 
67
- // The schema of each session object, by its name.
68
- const objects = new Map<string, Schema>();
68
+ /** A database's {@link Ensure}. */
69
+ const ENSURE = new Link<Ensure>('Schema.ensure');
69
70
 
70
- /** The session object `name`'s own schema: its steps, which each of its databases is made by. */
71
- export const register = (name: string, steps: readonly Step[]) => void objects.set(name, new Schema(name, steps));
71
+ /** `db`, which applies schemas with `ensure` (see {@link Schema.ensure}). */
72
+ export const ensuring = <D extends object>(db: D, ensure: Ensure): D => ENSURE.on(db, ensure);
72
73
 
73
- /** The session object `name`'s own schema, if it has one. */
74
- export const ofObject = (name: string) => objects.get(name);
74
+ /** How `db` applies schemas, if it is a database of this package's. */
75
+ export const ensureOf = (db: object): Ensure | undefined => ENSURE.of(db);
75
76
 
76
77
  /** Apply the steps of `schemas` that `db` lacks, in one transaction, with how far each got. */
77
78
  export async function apply(db: Connection, schemas: readonly Schema[], { reader = false } = {}) {
package/src/scope.ts ADDED
@@ -0,0 +1,78 @@
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
+ }