@jarenjs/linq 0.49.2 → 0.66.1
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/ARCHITECTURE.md +227 -0
- package/README.md +650 -17
- package/docs/APP-PEN.md +1143 -0
- package/docs/CONTRACT-PEN.md +1221 -0
- package/docs/DB-CLIENT.md +882 -0
- package/docs/FLOW-PEN.md +1033 -0
- package/docs/FORMS-PEN.md +940 -0
- package/docs/JSLT-PEN.md +955 -0
- package/docs/LINQ-FORMAT.md +778 -383
- package/docs/MIGRATION-PEN.md +781 -0
- package/docs/MODEL-PEN.md +1092 -0
- package/docs/QUERY-PEN.md +1724 -0
- package/docs/SCHEMA-PEN.md +1218 -0
- package/package.json +57 -4
- package/src/app/action.js +251 -0
- package/src/app/capture.js +63 -0
- package/src/app/define.js +255 -0
- package/src/app/index.js +20 -0
- package/src/app/patch.js +277 -0
- package/src/app/sub.js +106 -0
- package/src/async.js +377 -75
- package/src/capture-root.js +82 -0
- package/src/concurrency.js +48 -11
- package/src/contract/define.js +282 -0
- package/src/contract/http.js +247 -0
- package/src/contract/index.js +23 -0
- package/src/contract/operation.js +338 -0
- package/src/db/handle.js +89 -0
- package/src/db/include.js +351 -0
- package/src/db/index.js +24 -0
- package/src/db/ledger.js +195 -0
- package/src/db/live.js +43 -0
- package/src/db/membership.js +37 -0
- package/src/db/open.js +130 -0
- package/src/document.js +143 -13
- package/src/effect.js +65 -0
- package/src/errors.js +78 -6
- package/src/expression.js +463 -36
- package/src/federate.js +531 -0
- package/src/flow/capture.js +33 -0
- package/src/flow/dag.js +316 -0
- package/src/flow/fsm.js +323 -0
- package/src/flow/index.js +22 -0
- package/src/forms/index.js +43 -0
- package/src/forms/rules.js +170 -0
- package/src/forms/submit.js +177 -0
- package/src/index.js +5 -2
- package/src/jslt/body.js +226 -0
- package/src/jslt/index.js +18 -0
- package/src/jslt/rules.js +202 -0
- package/src/json-boundary.js +90 -0
- package/src/migration/define.js +318 -0
- package/src/migration/index.js +15 -0
- package/src/migration/steps.js +244 -0
- package/src/model/collection.js +273 -0
- package/src/model/define.js +125 -0
- package/src/model/entity.js +307 -0
- package/src/model/index.js +47 -0
- package/src/model/relation.js +85 -0
- package/src/provider.js +137 -20
- package/src/schema/brand.js +31 -0
- package/src/schema/builders.js +526 -0
- package/src/schema/check.js +29 -0
- package/src/schema/emit.js +394 -0
- package/src/schema/factories.js +239 -0
- package/src/schema/index.js +37 -0
- package/src/schema-of.js +24 -0
- package/src/sequence.js +233 -103
- package/src/sources.js +10 -3
- package/types/app.d.ts +293 -0
- package/types/contract.d.ts +468 -0
- package/types/db.d.ts +359 -0
- package/types/flow.d.ts +285 -0
- package/types/forms.d.ts +253 -0
- package/types/index.d.ts +296 -26
- package/types/jslt.d.ts +193 -0
- package/types/migration.d.ts +201 -0
- package/types/model.d.ts +526 -0
- package/types/schema.d.ts +494 -0
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@jarenjs/linq/contract` — the contract pen's declarations.
|
|
3
|
+
*
|
|
4
|
+
* `defineContract()` returns a `Contract<Ops>` whose phantom `Ops` is
|
|
5
|
+
* read by `ContractOf<>`: one entry per declared operation carrying its
|
|
6
|
+
* `kind`, the `Infer<>` of its input (or `null` when it declares none),
|
|
7
|
+
* the `Infer<>` of its output, the union of its declared error codes,
|
|
8
|
+
* and whether its media makes it opaque. Every one of those is a
|
|
9
|
+
* compile-time reading of the SAME builders the emitted document was
|
|
10
|
+
* written from (D2), so the wrappers below can type a client (an HTTP
|
|
11
|
+
* one with its byte method), a handler table and an AI toolbox with no
|
|
12
|
+
* `generate` step.
|
|
13
|
+
*
|
|
14
|
+
* The agreement is a gate: `test/consumer/linq-contract.ts` proves
|
|
15
|
+
* `ContractOf<>`'s members EQUAL to what
|
|
16
|
+
* `@jarenjs/contract/project`'s `toTypeScript` declares for the document
|
|
17
|
+
* the pen emitted (`test/consumer/linq-contract-generated.ts`,
|
|
18
|
+
* regenerated byte-identically by `test/linq/contract-pen.test.js`),
|
|
19
|
+
* for every worked example. Where the projection widens — an opaque or
|
|
20
|
+
* boolean `output` is `unknown`, an error's `details` is `unknown` — the
|
|
21
|
+
* pen widens identically. The shapes `Meta`, `WireError`, `Outcome<T>`,
|
|
22
|
+
* `InvokeContext`, `Failure`, `HandlerContext` restate
|
|
23
|
+
* CONTRACT-FORMAT §10.1's fixed D6 members and §12.3's rendering of
|
|
24
|
+
* them, and the same file pins them equal.
|
|
25
|
+
*
|
|
26
|
+
* The runtime is in src/contract/*; CONTRACT-PEN.md is the normative
|
|
27
|
+
* mapping table.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import type { BuilderLike, Infer, Input, Json, JsonSchema, Simplify } from './schema.js';
|
|
31
|
+
|
|
32
|
+
/** A `$contract` 0.1 document as the pen writes it: members, verbatim. */
|
|
33
|
+
export interface ContractDocument {
|
|
34
|
+
readonly [member: string]: unknown;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// ——— the declaration surface ———
|
|
38
|
+
|
|
39
|
+
/** The head members §2.1 declares beside `operations`. */
|
|
40
|
+
export interface ContractMeta {
|
|
41
|
+
/** An identifier for the contract (`[A-Za-z_][A-Za-z0-9_-]*`). */
|
|
42
|
+
readonly id?: string;
|
|
43
|
+
/** The consumer's version string — a compatibility claim. */
|
|
44
|
+
readonly version?: string;
|
|
45
|
+
/** Peer `version` strings this contract accepts. */
|
|
46
|
+
readonly compat?: readonly string[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** An uppercase method token §4's table lists. */
|
|
50
|
+
export type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
|
|
51
|
+
|
|
52
|
+
/** Where one input member travels (§4.1). */
|
|
53
|
+
export type Location = 'path' | 'query' | 'header' | 'body';
|
|
54
|
+
|
|
55
|
+
/** The REST binding of one operation (§4). */
|
|
56
|
+
export interface HttpSpec {
|
|
57
|
+
readonly method: HttpMethod;
|
|
58
|
+
/** A path template (§4.2): `/api/products/{id}` or `/docs/:id`. */
|
|
59
|
+
readonly path: string;
|
|
60
|
+
/** Input member → location; the ones the default does not place. */
|
|
61
|
+
readonly in?: { readonly [member: string]: Location };
|
|
62
|
+
/** The input member whose value IS the request body. */
|
|
63
|
+
readonly body?: string;
|
|
64
|
+
/** The success status, 200–299. Default `200`, never written. */
|
|
65
|
+
readonly status?: number;
|
|
66
|
+
/** The response media type. Default `application/json`, never written. */
|
|
67
|
+
readonly media?: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A checked binding: the spec it was written from, carried as a phantom. */
|
|
71
|
+
export interface HttpBinding<H extends HttpSpec = HttpSpec> {
|
|
72
|
+
readonly __http: H;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The stream knobs of a subscribe operation (§3.1). */
|
|
76
|
+
export interface StreamPolicy {
|
|
77
|
+
readonly resume?: 'snapshot' | 'replay';
|
|
78
|
+
readonly heartbeatMs?: number;
|
|
79
|
+
readonly maxPatchBytes?: number;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The declared behavior of one operation (§3.1). Every member has a
|
|
83
|
+
* default the COMPILER materializes; the pen writes only what is here. */
|
|
84
|
+
export interface Policy {
|
|
85
|
+
readonly task?: 'switch' | 'exhaust' | 'concat' | 'parallel';
|
|
86
|
+
readonly idempotency?: 'none' | 'optional' | 'required';
|
|
87
|
+
/** `"input:<json-pointer>"` — where the revision a command asserts lives. */
|
|
88
|
+
readonly revision?: string;
|
|
89
|
+
readonly cache?: 'none' | 'revision';
|
|
90
|
+
readonly limits?: { readonly maxBodyBytes: number };
|
|
91
|
+
readonly errors?: { readonly details: 'none' | 'paths' | 'full' };
|
|
92
|
+
readonly retry?: { readonly max: number; readonly on: readonly string[] };
|
|
93
|
+
readonly stream?: StreamPolicy;
|
|
94
|
+
readonly audience?: 'public' | 'server';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** A schema position: a schema-pen builder, or a JSON Schema by hand. */
|
|
98
|
+
export type SchemaSpec = BuilderLike<any, any, any> | JsonSchema | boolean;
|
|
99
|
+
|
|
100
|
+
/** One entry of an operation's `errors` map (§3). */
|
|
101
|
+
export interface ErrorSpec {
|
|
102
|
+
/** 100–599. Default `400`, never written. */
|
|
103
|
+
readonly status?: number;
|
|
104
|
+
/** A JSON Schema for the error's details. */
|
|
105
|
+
readonly schema?: SchemaSpec;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** A checked error declaration: the spec it was written from. */
|
|
109
|
+
export interface ErrorDeclaration<E extends ErrorSpec = ErrorSpec> {
|
|
110
|
+
readonly __error: E;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** What `read()`/`command()`/`subscribe()` take (§3). */
|
|
114
|
+
export interface OperationSpec {
|
|
115
|
+
/** A schema whose effective type is `object`; absent takes no input. */
|
|
116
|
+
readonly input?: SchemaSpec;
|
|
117
|
+
readonly output: SchemaSpec;
|
|
118
|
+
readonly errors?: { readonly [code: string]: ErrorDeclaration<any> | ErrorSpec };
|
|
119
|
+
readonly policy?: Policy;
|
|
120
|
+
readonly http?: HttpBinding<any> | HttpSpec;
|
|
121
|
+
readonly doc?: string;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** A declared operation: its kind and the spec it was written from. */
|
|
125
|
+
export interface OperationDeclaration<K extends OperationKind, S extends OperationSpec> {
|
|
126
|
+
readonly __kind: K;
|
|
127
|
+
readonly __spec: S;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The three kinds §3 declares. */
|
|
131
|
+
export type OperationKind = 'read' | 'command' | 'subscribe';
|
|
132
|
+
|
|
133
|
+
/** An operation of any kind. */
|
|
134
|
+
export type AnyOperation = OperationDeclaration<OperationKind, any>;
|
|
135
|
+
|
|
136
|
+
// ——— the phantom reading ———
|
|
137
|
+
|
|
138
|
+
/** The output shape of a schema position; `unknown` for JSON by hand. */
|
|
139
|
+
type SchemaOut<S> = S extends BuilderLike<any, any, any> ? Infer<S> : unknown;
|
|
140
|
+
/** The accepted shape of a schema position; `unknown` for JSON by hand. */
|
|
141
|
+
type SchemaIn<S> = S extends BuilderLike<any, any, any> ? Input<S> : unknown;
|
|
142
|
+
|
|
143
|
+
/** The media a binding declares; the default when it declares none. A
|
|
144
|
+
* spec is matched by PATTERN, never indexed: an absent optional member
|
|
145
|
+
* of the constraint is not the same as one the author declared. */
|
|
146
|
+
type MediaOf<S> =
|
|
147
|
+
S extends { http: HttpBinding<infer H> }
|
|
148
|
+
? (H extends { media: infer M extends string } ? M : 'application/json')
|
|
149
|
+
: S extends { http: { media: infer M extends string } } ? M : 'application/json';
|
|
150
|
+
|
|
151
|
+
/** A non-JSON media marks the operation opaque (§4.5); a subscribe never is. */
|
|
152
|
+
type IsOpaque<K extends OperationKind, S> =
|
|
153
|
+
K extends 'subscribe' ? false
|
|
154
|
+
: MediaOf<S> extends 'application/json' ? false
|
|
155
|
+
: MediaOf<S> extends `${string}+json` ? false : true;
|
|
156
|
+
|
|
157
|
+
/** The codes an operation declares, as a literal union. */
|
|
158
|
+
type CodesOf<S> = S extends { errors: infer E } ? Extract<keyof E, string> : never;
|
|
159
|
+
|
|
160
|
+
/** One operation, as the type system reads it. */
|
|
161
|
+
export interface OperationType<
|
|
162
|
+
K extends OperationKind, I, In, O, E extends string, Opaque extends boolean,
|
|
163
|
+
> {
|
|
164
|
+
readonly kind: K;
|
|
165
|
+
/** The output shape of the input schema; `null` when none is declared. */
|
|
166
|
+
readonly input: I;
|
|
167
|
+
/** The accepted (pre-normalization) shape of the input schema. */
|
|
168
|
+
readonly accepts: In;
|
|
169
|
+
readonly output: O;
|
|
170
|
+
/** The declared error codes; `never` when none is declared. */
|
|
171
|
+
readonly errors: E;
|
|
172
|
+
readonly opaque: Opaque;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The reading of one declared operation. */
|
|
176
|
+
type Read<D> = D extends OperationDeclaration<infer K, infer S>
|
|
177
|
+
? OperationType<
|
|
178
|
+
K,
|
|
179
|
+
S extends { input: infer I } ? SchemaOut<I> : null,
|
|
180
|
+
S extends { input: infer I } ? SchemaIn<I> : null,
|
|
181
|
+
S extends { output: infer O } ? SchemaOut<O> : unknown,
|
|
182
|
+
CodesOf<S>,
|
|
183
|
+
IsOpaque<K, S>>
|
|
184
|
+
: never;
|
|
185
|
+
|
|
186
|
+
/** The reading of a whole operation map. */
|
|
187
|
+
export type OperationsOf<O> = Simplify<{ readonly [K in keyof O]: Read<O[K]> }>;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* The contract a pen wrote: `document` and `toJSON()` are the same
|
|
191
|
+
* deep-frozen `$contract` 0.1 JSON, and `Ops` is the phantom
|
|
192
|
+
* `ContractOf<>` reads.
|
|
193
|
+
*/
|
|
194
|
+
export class Contract<Ops = Record<string, never>> {
|
|
195
|
+
private constructor();
|
|
196
|
+
/** Declared, never present at runtime. */
|
|
197
|
+
readonly __ops: Ops;
|
|
198
|
+
/** The deep-frozen `$contract` 0.1 document. */
|
|
199
|
+
readonly document: ContractDocument;
|
|
200
|
+
toJSON(): ContractDocument;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Every operation a contract declares, opaque ones included. */
|
|
204
|
+
export type ContractOf<C> = C extends Contract<infer Ops> ? Ops : never;
|
|
205
|
+
|
|
206
|
+
/** The invokable operations: everything the AI tools and `invoke` reach —
|
|
207
|
+
* the opaque ones are excluded, exactly as §12.3's `Operations` is. */
|
|
208
|
+
export type InvokableOf<C> = Simplify<{
|
|
209
|
+
[K in keyof ContractOf<C> as ContractOf<C>[K] extends { opaque: true } ? never : K]:
|
|
210
|
+
ContractOf<C>[K]
|
|
211
|
+
}>;
|
|
212
|
+
|
|
213
|
+
/** The subscribe operations of a contract. */
|
|
214
|
+
export type SubscribableOf<C> = Simplify<{
|
|
215
|
+
[K in keyof ContractOf<C> as ContractOf<C>[K] extends { kind: 'subscribe' } ? K : never]:
|
|
216
|
+
ContractOf<C>[K]
|
|
217
|
+
}>;
|
|
218
|
+
|
|
219
|
+
/** The opaque operations of a contract: what an HTTP client's `bytes`
|
|
220
|
+
* reaches — exactly the set §12.3's `ByteOperations` declares. */
|
|
221
|
+
export type OpaqueOf<C> = Simplify<{
|
|
222
|
+
[K in keyof ContractOf<C> as ContractOf<C>[K] extends { opaque: true } ? K : never]:
|
|
223
|
+
ContractOf<C>[K]
|
|
224
|
+
}>;
|
|
225
|
+
|
|
226
|
+
// ——— the fixed outcome shapes (§10.1, rendered by §12.3) ———
|
|
227
|
+
|
|
228
|
+
/** The correlation members of every outcome. A member a binding cannot
|
|
229
|
+
* carry is null (or false for `notModified`), never omitted. */
|
|
230
|
+
export type Meta = {
|
|
231
|
+
op: string; attempt: unknown; trace: string | null; revision: string | null;
|
|
232
|
+
etag: string | null; notModified: boolean;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
/** The error member of a failed outcome. */
|
|
236
|
+
export type WireError = {
|
|
237
|
+
code: string; message: string; status: number | null; details: unknown; retryable: boolean;
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
/** What every invoke resolves to — JSON, never a thrown error. */
|
|
241
|
+
export type Outcome<T> =
|
|
242
|
+
| { ok: true; value: T; meta: Meta }
|
|
243
|
+
| {
|
|
244
|
+
ok: false; kind: 'failure' | 'network' | 'contract' | 'cancelled';
|
|
245
|
+
error: WireError; meta: Meta;
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
/** Per-call options of `invoke`. */
|
|
249
|
+
export interface InvokeContext {
|
|
250
|
+
signal?: AbortSignal; attempt?: unknown; idempotencyKey?: string;
|
|
251
|
+
headers?: Record<string, string>; ifNoneMatch?: string; ifMatch?: string;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** A declared failure a handler returns (`ctx.fail`). */
|
|
255
|
+
export type Failure = {
|
|
256
|
+
code: string; params: Readonly<Record<string, unknown>>; details: unknown;
|
|
257
|
+
retryable: boolean | null;
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
/** The binding a handler context comes from. */
|
|
261
|
+
export type CarrierName = 'http' | 'port' | 'local';
|
|
262
|
+
|
|
263
|
+
/** The members every carrier's context shares; `host` is the host
|
|
264
|
+
* lifecycle's acquired value (§7.7), `null` by default. */
|
|
265
|
+
export interface HandlerContextBase<Host = null> {
|
|
266
|
+
op: unknown;
|
|
267
|
+
trace: string;
|
|
268
|
+
host: Host;
|
|
269
|
+
headers: Readonly<Record<string, string>>;
|
|
270
|
+
signal: AbortSignal | null;
|
|
271
|
+
fail(code: string, params?: Record<string, unknown>, details?: unknown,
|
|
272
|
+
options?: { retryable?: boolean }): Failure;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** The HTTP binding's context: the request line, the raw body of an
|
|
276
|
+
* opaque operation, the idempotency key, and the entity-tag and status
|
|
277
|
+
* arms. */
|
|
278
|
+
export interface HttpHandlerContext<Host = null> extends HandlerContextBase<Host> {
|
|
279
|
+
carrier: 'http';
|
|
280
|
+
method: string;
|
|
281
|
+
path: string;
|
|
282
|
+
params: Readonly<Record<string, string>>;
|
|
283
|
+
body: string | Uint8Array | AsyncIterable<Uint8Array> | null;
|
|
284
|
+
idempotency: Readonly<{ key: string; scope: string }> | null;
|
|
285
|
+
etag(tag: string, options?: { strong?: boolean }): void;
|
|
286
|
+
status(status: number): void;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** The port and local bindings' context: no request line, no body, no
|
|
290
|
+
* key, and no callable `etag` or `status` — spelled `null`, never
|
|
291
|
+
* omitted (§15, §16). */
|
|
292
|
+
export interface ChannelHandlerContext<Host = null, Carrier extends 'port' | 'local' = 'port' | 'local'>
|
|
293
|
+
extends HandlerContextBase<Host> {
|
|
294
|
+
carrier: Carrier;
|
|
295
|
+
method: null;
|
|
296
|
+
path: null;
|
|
297
|
+
params: null;
|
|
298
|
+
body: null;
|
|
299
|
+
idempotency: null;
|
|
300
|
+
etag: null;
|
|
301
|
+
status: null;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** The per-request context a server binding hands a handler, selected
|
|
305
|
+
* by carrier: the HTTP context by default (`HandlerContext` is the
|
|
306
|
+
* shape it always was, plus `carrier` and `host`); a carrier union is a
|
|
307
|
+
* discriminated union — narrow on `carrier` before an HTTP-only
|
|
308
|
+
* member. */
|
|
309
|
+
export type HandlerContext<Host = null, Carrier extends CarrierName = 'http'> =
|
|
310
|
+
Extract<HttpHandlerContext<Host> | ChannelHandlerContext<Host, 'port'> | ChannelHandlerContext<Host, 'local'>, { carrier: Carrier }>;
|
|
311
|
+
|
|
312
|
+
/** Per-call options of `bytes` (§10.6): the members of `InvokeContext`
|
|
313
|
+
* that apply to an opaque call, plus the request body to send. */
|
|
314
|
+
export interface ByteContext {
|
|
315
|
+
signal?: AbortSignal; attempt?: unknown; headers?: Record<string, string>;
|
|
316
|
+
ifNoneMatch?: string; ifMatch?: string;
|
|
317
|
+
body?: string | Uint8Array | ReadableStream<Uint8Array> | AsyncIterable<Uint8Array> | null;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** The success value of `bytes`: the status, the lowercase response
|
|
321
|
+
* headers, the response media and the LIVE body — a stream the caller
|
|
322
|
+
* reads; `null` when the response carries none. */
|
|
323
|
+
export type ByteResponse = {
|
|
324
|
+
status: number; headers: Record<string, string>; media: string | null;
|
|
325
|
+
body: ReadableStream<Uint8Array> | null;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
/** The info beside every snapshot (§19): the event's seq, whether the
|
|
329
|
+
* stream resumed, whether the snapshot re-seeds a consumer whose cursor
|
|
330
|
+
* fell behind the server's retention (`reset`), and the server log's
|
|
331
|
+
* watermarks when it reported them. */
|
|
332
|
+
export interface SnapshotInfo {
|
|
333
|
+
seq: number; resumed: boolean; reset: boolean;
|
|
334
|
+
earliestAvailable: number | null; highWatermark: number | null;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** What `client.subscribe` takes (§19); every callback is optional.
|
|
338
|
+
* `reconnect` opts the HTTP client into re-establishing the stream
|
|
339
|
+
* after a network loss, up to `max` further attempts from the last
|
|
340
|
+
* delivered seq; the budget spent, `onError` gets one `JC2097`. The
|
|
341
|
+
* port client has no network loss to reconnect from and ignores it. */
|
|
342
|
+
export interface SubscribeHandlers<T> {
|
|
343
|
+
onSnapshot?(value: T, info: SnapshotInfo): void;
|
|
344
|
+
onPatch?(emission: { patch: readonly Json[]; seq: number }): void;
|
|
345
|
+
onError?(outcome: Outcome<never>): void;
|
|
346
|
+
onEnd?(info: { reason: string }): void;
|
|
347
|
+
signal?: AbortSignal;
|
|
348
|
+
lastSeq?: number;
|
|
349
|
+
reconnect?: { max: number };
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** A live subscription: `stop()` releases it; `lastSeq` is the last
|
|
353
|
+
* delivered seq (`null` before the first, the passed `lastSeq` until an
|
|
354
|
+
* event moves it) — what a re-entered `subscribe` passes (§19). */
|
|
355
|
+
export interface Subscription {
|
|
356
|
+
stop(): void;
|
|
357
|
+
readonly lastSeq: number | null;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// ——— the three identity wrappers ———
|
|
361
|
+
|
|
362
|
+
/** A contract client typed by one contract's operations. */
|
|
363
|
+
export interface TypedClient<C> {
|
|
364
|
+
invoke<K extends keyof InvokableOf<C>>(
|
|
365
|
+
op: K,
|
|
366
|
+
input: InvokableOf<C>[K] extends { input: infer I } ? I : never,
|
|
367
|
+
ctx?: InvokeContext,
|
|
368
|
+
): Promise<Outcome<InvokableOf<C>[K] extends { output: infer O } ? O : never>>;
|
|
369
|
+
subscribe<K extends keyof SubscribableOf<C>>(
|
|
370
|
+
op: K,
|
|
371
|
+
input: SubscribableOf<C>[K] extends { input: infer I } ? I : never,
|
|
372
|
+
handlers: SubscribeHandlers<
|
|
373
|
+
SubscribableOf<C>[K] extends { output: infer O } ? O : never>,
|
|
374
|
+
): Subscription;
|
|
375
|
+
url<K extends keyof ContractOf<C>>(
|
|
376
|
+
op: K,
|
|
377
|
+
input: ContractOf<C>[K] extends { input: infer I } ? I : never,
|
|
378
|
+
): string;
|
|
379
|
+
close(): void;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** An HTTP client typed by one contract's operations: `TypedClient` plus
|
|
383
|
+
* `bytes` over the opaque ones, whose success owns a live stream. */
|
|
384
|
+
export interface TypedHttpClient<C> extends TypedClient<C> {
|
|
385
|
+
bytes<K extends keyof OpaqueOf<C>>(
|
|
386
|
+
op: K,
|
|
387
|
+
input: OpaqueOf<C>[K] extends { input: infer I } ? I : never,
|
|
388
|
+
ctx?: ByteContext,
|
|
389
|
+
): Promise<Outcome<ByteResponse>>;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** The handler table of a server binding, one handler per invokable
|
|
393
|
+
* operation. `Host` is what the host lifecycle's `acquire` hands the
|
|
394
|
+
* handler as `ctx.host`; `Carrier` selects the context — `'http'` by
|
|
395
|
+
* default, a union for a table several bindings share (narrow on
|
|
396
|
+
* `ctx.carrier` before an HTTP-only member). */
|
|
397
|
+
export type TypedHandlerTable<C, Host = null, Carrier extends CarrierName = 'http'> = {
|
|
398
|
+
[K in keyof InvokableOf<C>]: (
|
|
399
|
+
input: InvokableOf<C>[K] extends { input: infer I } ? I : never,
|
|
400
|
+
ctx: HandlerContext<Host, Carrier>,
|
|
401
|
+
) => (InvokableOf<C>[K] extends { output: infer O } ? O : never)
|
|
402
|
+
| Failure
|
|
403
|
+
| Promise<(InvokableOf<C>[K] extends { output: infer O } ? O : never) | Failure>;
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
/** An operation id as `contractTools` names it: `.` → `_`. */
|
|
407
|
+
export type ToolName<S extends string> =
|
|
408
|
+
S extends `${infer A}.${infer B}` ? `${A}_${ToolName<B>}` : S;
|
|
409
|
+
|
|
410
|
+
/** One tool definition, typed by the operation it invokes — the
|
|
411
|
+
* `ToolDef` shape `@jarenjs/ai`'s `createToolbox().add` takes. */
|
|
412
|
+
export type TypedTool<C> = {
|
|
413
|
+
[K in keyof InvokableOf<C>]: {
|
|
414
|
+
name: ToolName<Extract<K, string>>;
|
|
415
|
+
description: string;
|
|
416
|
+
inputSchema: JsonSchema;
|
|
417
|
+
execute: InvokableOf<C>[K] extends { accepts: infer In; output: infer O }
|
|
418
|
+
? (In extends null ? null : (args: In) => Promise<Outcome<O>>)
|
|
419
|
+
: never;
|
|
420
|
+
};
|
|
421
|
+
}[keyof InvokableOf<C>];
|
|
422
|
+
|
|
423
|
+
// ——— the functions ———
|
|
424
|
+
|
|
425
|
+
/** A `read` operation: a query whose input members default to the query
|
|
426
|
+
* string and whose result may be cached by revision. */
|
|
427
|
+
export function read<const S extends OperationSpec>(spec: S): OperationDeclaration<'read', S>;
|
|
428
|
+
|
|
429
|
+
/** A `command` operation: a state change whose input members default to
|
|
430
|
+
* the request body. */
|
|
431
|
+
export function command<const S extends OperationSpec>(spec: S): OperationDeclaration<'command', S>;
|
|
432
|
+
|
|
433
|
+
/** A `subscribe` operation (§17): `output` is the snapshot schema and
|
|
434
|
+
* the emissions travel the stream binding. */
|
|
435
|
+
export function subscribe<const S extends OperationSpec>(spec: S):
|
|
436
|
+
OperationDeclaration<'subscribe', S>;
|
|
437
|
+
|
|
438
|
+
/** One entry of an operation's `errors` map. */
|
|
439
|
+
export function error<const E extends ErrorSpec>(spec?: E): ErrorDeclaration<E>;
|
|
440
|
+
|
|
441
|
+
/** The REST binding of one operation; the path template is checked here
|
|
442
|
+
* (`JL0102` naming the reserved form), earlier than `JC0008`. */
|
|
443
|
+
export function http<const H extends HttpSpec>(spec: H): HttpBinding<H>;
|
|
444
|
+
|
|
445
|
+
/** Write a `$contract` 0.1 document. */
|
|
446
|
+
export function defineContract<O extends Record<string, AnyOperation | ({
|
|
447
|
+
kind: OperationKind } & OperationSpec)>>(
|
|
448
|
+
meta: ContractMeta, operations: O): Contract<OperationsOf<O>>;
|
|
449
|
+
|
|
450
|
+
/** Bind a contract client to the contract that types it. Identity at runtime. */
|
|
451
|
+
export function typedClient<C extends Contract<any>>(client: unknown, contract: C): TypedClient<C>;
|
|
452
|
+
|
|
453
|
+
/** Bind an HTTP client (`openHttpClient`) to the contract that types it:
|
|
454
|
+
* `typedClient` plus `bytes` over the opaque operations. Identity at
|
|
455
|
+
* runtime; a local or port client has no `bytes` and takes `typedClient`. */
|
|
456
|
+
export function typedHttpClient<C extends Contract<any>>(client: unknown, contract: C): TypedHttpClient<C>;
|
|
457
|
+
|
|
458
|
+
/** Bind a handler table to the contract it serves. Identity at runtime;
|
|
459
|
+
* a missing or misspelled operation is a type error. Name `Host` and
|
|
460
|
+
* `Carrier` explicitly for a table whose context is not the HTTP default:
|
|
461
|
+
* `typedHandlers<typeof Shop, { db: Client }, 'http' | 'port'>(Shop, …)`. */
|
|
462
|
+
export function typedHandlers<C extends Contract<any>, Host = null, Carrier extends CarrierName = 'http'>(
|
|
463
|
+
contract: C, handlers: TypedHandlerTable<C, Host, Carrier>): TypedHandlerTable<C, Host, Carrier>;
|
|
464
|
+
|
|
465
|
+
/** Bind `contractTools`' output to the contract that types it. Identity
|
|
466
|
+
* at runtime. */
|
|
467
|
+
export function typedTools<C extends Contract<any>>(
|
|
468
|
+
tools: readonly unknown[], contract: C): TypedTool<C>[];
|