@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/README.md +47 -8
- package/dist/database.d.ts +41 -2
- package/dist/database.d.ts.map +1 -1
- package/dist/database.js +99 -20
- package/dist/database.js.map +1 -1
- package/dist/internal.d.ts +5 -3
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +4 -2
- package/dist/internal.js.map +1 -1
- package/dist/link.d.ts +10 -0
- package/dist/link.d.ts.map +1 -0
- package/dist/link.js +19 -0
- package/dist/link.js.map +1 -0
- package/dist/object.d.ts +17 -4
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +61 -46
- package/dist/object.js.map +1 -1
- package/dist/schema.d.ts +7 -6
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +9 -10
- package/dist/schema.js.map +1 -1
- package/dist/scope.d.ts +38 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scope.js +55 -0
- package/dist/scope.js.map +1 -0
- package/dist/session.d.ts +70 -34
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +273 -221
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/src/database.ts +111 -22
- package/src/internal.ts +15 -3
- package/src/link.ts +22 -0
- package/src/object.ts +94 -63
- package/src/schema.ts +11 -10
- package/src/scope.ts +78 -0
- package/src/session.ts +273 -230
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
|
|
60
|
-
return
|
|
60
|
+
const ensure = ensureOf(db);
|
|
61
|
+
return ensure ? ensure([this]) : apply(db as Connection, [this]);
|
|
61
62
|
}
|
|
62
63
|
}
|
|
63
64
|
|
|
64
|
-
/**
|
|
65
|
-
|
|
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
|
-
|
|
68
|
-
const
|
|
68
|
+
/** A database's {@link Ensure}. */
|
|
69
|
+
const ENSURE = new Link<Ensure>('Schema.ensure');
|
|
69
70
|
|
|
70
|
-
/**
|
|
71
|
-
export const
|
|
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
|
-
/**
|
|
74
|
-
export const
|
|
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
|
+
}
|