@lostgradient/backchannel 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +62 -2
- package/dist/access-policy-metadata.d.ts +92 -0
- package/dist/access-policy-metadata.js +87 -0
- package/dist/asyncapi-channels.d.ts +46 -0
- package/dist/asyncapi-channels.js +399 -0
- package/dist/asyncapi.d.ts +40 -0
- package/dist/asyncapi.js +165 -0
- package/dist/authentication/api-key.d.ts +4 -0
- package/dist/authentication/api-key.js +114 -0
- package/dist/authentication/audit.d.ts +115 -0
- package/dist/authentication/audit.js +75 -0
- package/dist/authentication/credential-verifiers.d.ts +60 -0
- package/dist/authentication/credential-verifiers.js +102 -0
- package/dist/authentication/crypto.d.ts +7 -0
- package/dist/authentication/crypto.js +140 -0
- package/dist/authentication/index.d.ts +80 -0
- package/dist/authentication/index.js +212 -0
- package/dist/authentication/rate-limiter.d.ts +133 -0
- package/dist/authentication/rate-limiter.js +124 -0
- package/dist/authentication/redaction.d.ts +72 -0
- package/dist/authentication/redaction.js +118 -0
- package/dist/authentication/rotating-api-key-store.d.ts +127 -0
- package/dist/authentication/rotating-api-key-store.js +126 -0
- package/dist/authentication/types.d.ts +337 -0
- package/dist/authentication/types.js +26 -0
- package/dist/authentication.d.ts +1 -0
- package/dist/authentication.js +1 -0
- package/dist/authorization-scope.d.ts +109 -0
- package/dist/authorization-scope.js +144 -0
- package/dist/authorization.d.ts +55 -0
- package/dist/authorization.js +134 -0
- package/dist/bind-feed-lifetime.d.ts +45 -0
- package/dist/bind-feed-lifetime.js +55 -0
- package/dist/byte-arrays.d.ts +5 -0
- package/dist/byte-arrays.js +9 -0
- package/dist/client/catalog-client.d.ts +285 -0
- package/dist/client/catalog-client.js +74 -0
- package/dist/client/in-process-transport.d.ts +19 -0
- package/dist/client/in-process-transport.js +136 -0
- package/dist/client/index.d.ts +14 -0
- package/dist/client/index.js +14 -0
- package/dist/client/json-rpc-http-transport.d.ts +118 -0
- package/dist/client/json-rpc-http-transport.js +127 -0
- package/dist/client/server-sent-event-parser.d.ts +55 -0
- package/dist/client/server-sent-event-parser.js +78 -0
- package/dist/client/sse-subscription-transport.d.ts +72 -0
- package/dist/client/sse-subscription-transport.js +224 -0
- package/dist/client/subscription-client.d.ts +122 -0
- package/dist/client/subscription-client.js +28 -0
- package/dist/connection-lease.d.ts +10 -0
- package/dist/connection-lease.js +8 -0
- package/dist/discovery-filter.d.ts +13 -0
- package/dist/discovery-filter.js +16 -0
- package/dist/discovery-info.d.ts +94 -0
- package/dist/discovery-info.js +38 -0
- package/dist/event-selector.d.ts +7 -0
- package/dist/event-selector.js +7 -0
- package/dist/fault-code.d.ts +84 -0
- package/dist/fault-code.js +114 -0
- package/dist/fault-remediation.d.ts +30 -0
- package/dist/fault-remediation.js +52 -0
- package/dist/fault-to-http.d.ts +11 -0
- package/dist/fault-to-http.js +13 -0
- package/dist/fault-to-json-rpc.d.ts +53 -0
- package/dist/fault-to-json-rpc.js +143 -0
- package/dist/fault-wire-safety.d.ts +45 -0
- package/dist/fault-wire-safety.js +72 -0
- package/dist/generator.d.ts +9 -0
- package/dist/generator.js +9 -0
- package/dist/handler/auth-context-principal.d.ts +37 -0
- package/dist/handler/auth-context-principal.js +67 -0
- package/dist/handler/authentication-responses.d.ts +23 -0
- package/dist/handler/authentication-responses.js +34 -0
- package/dist/handler/json-responses.d.ts +15 -0
- package/dist/handler/json-responses.js +32 -0
- package/dist/handler/operation-handler-authentication.d.ts +64 -0
- package/dist/handler/operation-handler-authentication.js +58 -0
- package/dist/handler/operation-handler-discovery.d.ts +29 -0
- package/dist/handler/operation-handler-discovery.js +55 -0
- package/dist/handler/operation-handler-mounts.d.ts +100 -0
- package/dist/handler/operation-handler-mounts.js +134 -0
- package/dist/handler/operation-handler-subscription.d.ts +34 -0
- package/dist/handler/operation-handler-subscription.js +112 -0
- package/dist/handler/operation-handler.d.ts +162 -0
- package/dist/handler/operation-handler.js +236 -0
- package/dist/handler/workspace-release.d.ts +6 -0
- package/dist/handler/workspace-release.js +75 -0
- package/dist/in-memory-replay-live-backend.d.ts +47 -0
- package/dist/in-memory-replay-live-backend.js +129 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +30 -0
- package/dist/internal/cryptography/encryption.d.ts +108 -0
- package/dist/internal/cryptography/encryption.js +241 -0
- package/dist/internal/cryptography/hash.d.ts +72 -0
- package/dist/internal/cryptography/hash.js +233 -0
- package/dist/internal/cryptography/index.d.ts +4 -0
- package/dist/internal/cryptography/index.js +2 -0
- package/dist/internals.d.ts +58 -0
- package/dist/internals.js +54 -0
- package/dist/json-rpc-dispatch.d.ts +88 -0
- package/dist/json-rpc-dispatch.js +154 -0
- package/dist/json-rpc-framing.d.ts +42 -0
- package/dist/json-rpc-framing.js +50 -0
- package/dist/json-rpc-http.d.ts +36 -0
- package/dist/json-rpc-http.js +191 -0
- package/dist/json-rpc-parse.d.ts +69 -0
- package/dist/json-rpc-parse.js +206 -0
- package/dist/json-rpc-protocol.d.ts +76 -0
- package/dist/json-rpc-protocol.js +54 -0
- package/dist/json-rpc-session-runtime.d.ts +55 -0
- package/dist/json-rpc-session-runtime.js +111 -0
- package/dist/json-rpc-websocket-subscription-pump.d.ts +9 -0
- package/dist/json-rpc-websocket-subscription-pump.js +46 -0
- package/dist/json-rpc-websocket-subscriptions.d.ts +10 -0
- package/dist/json-rpc-websocket-subscriptions.js +33 -0
- package/dist/json-rpc-websocket-types.d.ts +34 -0
- package/dist/json-rpc-websocket-types.js +1 -0
- package/dist/json-rpc-websocket-validation.d.ts +45 -0
- package/dist/json-rpc-websocket-validation.js +154 -0
- package/dist/json-rpc-websocket.d.ts +20 -0
- package/dist/json-rpc-websocket.js +363 -0
- package/dist/json-schema-utilities.d.ts +5 -0
- package/dist/json-schema-utilities.js +17 -0
- package/dist/mcp-discovery-names.d.ts +11 -0
- package/dist/mcp-discovery-names.js +11 -0
- package/dist/openapi-canonical-json.d.ts +10 -0
- package/dist/openapi-canonical-json.js +23 -0
- package/dist/openapi-error-responses.d.ts +15 -0
- package/dist/openapi-error-responses.js +104 -0
- package/dist/openapi-schemas.d.ts +17 -0
- package/dist/openapi-schemas.js +94 -0
- package/dist/openapi.d.ts +60 -0
- package/dist/openapi.js +351 -0
- package/dist/openrpc-document-schema.d.ts +255 -0
- package/dist/openrpc-document-schema.js +84 -0
- package/dist/openrpc-errors.d.ts +93 -0
- package/dist/openrpc-errors.js +134 -0
- package/dist/openrpc.d.ts +66 -0
- package/dist/openrpc.js +260 -0
- package/dist/operation-catalog/behavior-types.d.ts +34 -0
- package/dist/operation-catalog/behavior-types.js +1 -0
- package/dist/operation-catalog/behavior-validation.d.ts +3 -0
- package/dist/operation-catalog/behavior-validation.js +298 -0
- package/dist/operation-catalog/dispatch-allowlist.d.ts +4 -0
- package/dist/operation-catalog/dispatch-allowlist.js +7 -0
- package/dist/operation-catalog/dispatch-preparation.d.ts +24 -0
- package/dist/operation-catalog/dispatch-preparation.js +49 -0
- package/dist/operation-catalog/index.d.ts +19 -0
- package/dist/operation-catalog/index.js +17 -0
- package/dist/operation-catalog/operation-dispatch.d.ts +14 -0
- package/dist/operation-catalog/operation-dispatch.js +115 -0
- package/dist/operation-catalog/operation-interceptors.d.ts +15 -0
- package/dist/operation-catalog/operation-interceptors.js +1 -0
- package/dist/operation-catalog/operation-metadata.d.ts +5 -0
- package/dist/operation-catalog/operation-metadata.js +104 -0
- package/dist/operation-catalog/pipeline-helpers.d.ts +70 -0
- package/dist/operation-catalog/pipeline-helpers.js +242 -0
- package/dist/operation-catalog/pipeline-stages.d.ts +23 -0
- package/dist/operation-catalog/pipeline-stages.js +226 -0
- package/dist/operation-catalog/pipeline.d.ts +7 -0
- package/dist/operation-catalog/pipeline.js +33 -0
- package/dist/operation-catalog/registry.d.ts +17 -0
- package/dist/operation-catalog/registry.js +132 -0
- package/dist/operation-catalog/stream-pipeline.d.ts +37 -0
- package/dist/operation-catalog/stream-pipeline.js +129 -0
- package/dist/operation-catalog/subscription-lifecycle.d.ts +6 -0
- package/dist/operation-catalog/subscription-lifecycle.js +27 -0
- package/dist/operation-catalog/types.d.ts +392 -0
- package/dist/operation-catalog/types.js +40 -0
- package/dist/operation-catalog/universal-faults.d.ts +11 -0
- package/dist/operation-catalog/universal-faults.js +15 -0
- package/dist/operation-catalog-snapshot.d.ts +215 -0
- package/dist/operation-catalog-snapshot.js +128 -0
- package/dist/operation-catalog.d.ts +1 -0
- package/dist/operation-catalog.js +1 -0
- package/dist/operation-description-audit.d.ts +44 -0
- package/dist/operation-description-audit.js +90 -0
- package/dist/operation-fault-helpers.d.ts +21 -0
- package/dist/operation-fault-helpers.js +32 -0
- package/dist/operation-fault.d.ts +245 -0
- package/dist/operation-fault.js +235 -0
- package/dist/operation-registry.d.ts +46 -0
- package/dist/operation-registry.js +46 -0
- package/dist/operations/closable-iterable.d.ts +23 -0
- package/dist/operations/closable-iterable.js +51 -0
- package/dist/operations/get-principal.d.ts +106 -0
- package/dist/operations/get-principal.js +107 -0
- package/dist/operations/sse-stream.d.ts +67 -0
- package/dist/operations/sse-stream.js +264 -0
- package/dist/principal.d.ts +253 -0
- package/dist/principal.js +254 -0
- package/dist/replay-live-feed-internals.d.ts +47 -0
- package/dist/replay-live-feed-internals.js +215 -0
- package/dist/replay-live-feed-types.d.ts +66 -0
- package/dist/replay-live-feed-types.js +7 -0
- package/dist/replay-live-feed.d.ts +49 -0
- package/dist/replay-live-feed.js +126 -0
- package/dist/rest-binding.d.ts +215 -0
- package/dist/rest-binding.js +95 -0
- package/dist/rest-body.d.ts +17 -0
- package/dist/rest-body.js +95 -0
- package/dist/route-model.d.ts +305 -0
- package/dist/route-model.js +320 -0
- package/dist/runtime/cors.d.ts +161 -0
- package/dist/runtime/cors.js +265 -0
- package/dist/scripts/generate-operation-client.d.ts +186 -0
- package/dist/scripts/generate-operation-client.js +561 -0
- package/dist/scripts/operation-behaviors.d.ts +3 -0
- package/dist/scripts/operation-behaviors.js +16 -0
- package/dist/scripts/operation-client-aliases.d.ts +32 -0
- package/dist/scripts/operation-client-aliases.js +255 -0
- package/dist/scripts/operation-client-domains.d.ts +21 -0
- package/dist/scripts/operation-client-domains.js +74 -0
- package/dist/scripts/operation-client-schema.d.ts +46 -0
- package/dist/scripts/operation-client-schema.js +310 -0
- package/dist/scripts/operation-client-svelte.d.ts +3 -0
- package/dist/scripts/operation-client-svelte.js +61 -0
- package/dist/sequence-cursor.d.ts +46 -0
- package/dist/sequence-cursor.js +62 -0
- package/dist/sse-error-payload-schema.d.ts +12 -0
- package/dist/sse-error-payload-schema.js +33 -0
- package/dist/stdio-session.d.ts +71 -0
- package/dist/stdio-session.js +347 -0
- package/dist/zod-json-schema.d.ts +22 -0
- package/dist/zod-json-schema.js +49 -0
- package/package.json +38 -3
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { OperationBehavior } from "./behavior-types.js";
|
|
3
|
+
import type { OperationHostFactory, OperationInterceptor } from "./operation-interceptors.js";
|
|
4
|
+
import type { AccessPolicy } from "../authorization.js";
|
|
5
|
+
import type { FaultCode } from "../fault-code.js";
|
|
6
|
+
import type { OperationFault, TransportKind } from "../operation-fault.js";
|
|
7
|
+
import type { Principal } from "../principal.js";
|
|
8
|
+
export type { OperationBehavior } from "./behavior-types.js";
|
|
9
|
+
/**
|
|
10
|
+
* Regex for the canonical `<namespace>.<segment>(.<segment>)*` operation-name
|
|
11
|
+
* form: at least two dot-separated segments, each starting with a lowercase
|
|
12
|
+
* ASCII letter and continuing with lowercase ASCII letters or digits.
|
|
13
|
+
*
|
|
14
|
+
* THE NAMESPACE IS NO LONGER FIXED TO `weft`. It was, and every operation this
|
|
15
|
+
* package defines still begins with `weft.` — the change is backward
|
|
16
|
+
* compatible, because the old pattern's language is a strict subset of this
|
|
17
|
+
* one. What it admits is a catalog shared with other packages: a gateway that
|
|
18
|
+
* serves `@lostgradient/operative` and `@lostgradient/bureau` alongside Weft needs
|
|
19
|
+
* `operative.*` and `bureau.*` to be legal names, and those packages already
|
|
20
|
+
* depend on this one, so registering through this catalog is the path that
|
|
21
|
+
* keeps one dispatch pipeline, one transport matrix, and one AsyncAPI document
|
|
22
|
+
* rather than a second RPC dialect beside them.
|
|
23
|
+
*
|
|
24
|
+
* The two-segment minimum is retained deliberately: a bare `start` carries no
|
|
25
|
+
* owner, and an unnamespaced catalog is one collision away from ambiguity
|
|
26
|
+
* about which package answers a method.
|
|
27
|
+
*
|
|
28
|
+
* `rpc.` IS RESERVED, and the negative lookahead is what keeps a guarantee the
|
|
29
|
+
* mandatory `weft.` prefix used to provide for free. JSON-RPC 2.0 reserves
|
|
30
|
+
* every method name beginning with `rpc.` for rpc-internal methods and
|
|
31
|
+
* extensions, and this server answers `rpc.discover` itself. Under a pattern
|
|
32
|
+
* that admitted any namespace, a registered `rpc.discover` would shadow
|
|
33
|
+
* discovery, and the only thing standing between that and a served catalog
|
|
34
|
+
* would be the OpenRPC generator's deduplication guard — a backstop rather
|
|
35
|
+
* than an impossibility. `openrpc-regressions.test.ts` pins this.
|
|
36
|
+
*/
|
|
37
|
+
export declare const OPERATION_NAME_PATTERN: RegExp;
|
|
38
|
+
/** Throws if `name` does not match the canonical operation-name pattern. */
|
|
39
|
+
export declare function validateOperationName(name: string): void;
|
|
40
|
+
/** Non-throwing variant of `validateOperationName`. */
|
|
41
|
+
export declare function isValidOperationName(name: string): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Per-operation transport availability flags. A `false` entry means callers
|
|
44
|
+
* on that transport receive `UnsupportedTransport`, not `MethodNotFound`.
|
|
45
|
+
*/
|
|
46
|
+
export type TransportAvailability = {
|
|
47
|
+
/** Whether REST and Server-Sent Events serve the operation. */
|
|
48
|
+
http: boolean;
|
|
49
|
+
/** Whether JSON-RPC over HTTP serves the operation. */
|
|
50
|
+
jsonRpcHttp: boolean;
|
|
51
|
+
/** Whether JSON-RPC over WebSocket serves the operation. */
|
|
52
|
+
jsonRpcWebSocket: boolean;
|
|
53
|
+
/** Whether JSON-RPC over stdio serves the operation. */
|
|
54
|
+
jsonRpcStdio: boolean;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Per-transport unknown-key disposition. Top-level only; nested object
|
|
58
|
+
* behavior is controlled by the zod schema's own object mode.
|
|
59
|
+
*/
|
|
60
|
+
export type UnknownKeyDisposition = 'reject' | 'strip' | 'passthrough';
|
|
61
|
+
/** How each transport treats input keys the operation's schema does not declare. */
|
|
62
|
+
export type UnknownKeyPolicy = {
|
|
63
|
+
/** What REST does with an undeclared top-level key. */
|
|
64
|
+
http: UnknownKeyDisposition;
|
|
65
|
+
/** What JSON-RPC does with an undeclared top-level key. */
|
|
66
|
+
jsonRpc: UnknownKeyDisposition;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Runtime shape of an operation. Unary operations return one validated
|
|
70
|
+
* `outputSchema` value. Stream and subscription operations return long-lived
|
|
71
|
+
* iterables whose elements are validated against `eventSchema`.
|
|
72
|
+
*/
|
|
73
|
+
export type OperationKind = 'unary' | 'stream' | 'subscription';
|
|
74
|
+
/** Invocation result for `kind: 'stream'` operations. */
|
|
75
|
+
export type StreamOperationInvocation<Element> = AsyncIterable<Element>;
|
|
76
|
+
/** Invocation result for `kind: 'subscription'` operations. */
|
|
77
|
+
export type SubscriptionOperationInvocation<Element, Envelope> = {
|
|
78
|
+
/** Internal lifecycle key used by transports; independent from the public envelope. */
|
|
79
|
+
readonly subscriptionId: string;
|
|
80
|
+
/** What the operation answers with before its events. */
|
|
81
|
+
readonly envelope: Envelope;
|
|
82
|
+
/** The events, in order. */
|
|
83
|
+
readonly iterable: AsyncIterable<Element>;
|
|
84
|
+
/** Ends the subscription and releases what it holds. */
|
|
85
|
+
readonly close: () => Promise<void>;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Envelope acknowledging that a JSON-RPC subscription has started. This is
|
|
89
|
+
* the protocol-level shape the built-in JSON-RPC WebSocket subscriptions
|
|
90
|
+
* (`weft.workflows.events` and the fleet-events subscription in
|
|
91
|
+
* `json-rpc-websocket.ts`) use for their own `outputSchema`/envelope — it is
|
|
92
|
+
* NOT the general contract for every `kind: 'subscription'` operation.
|
|
93
|
+
* `executeSubscription` in `stream-pipeline.ts` returns an erased envelope
|
|
94
|
+
* precisely because a catalog subscription can declare any envelope shape its
|
|
95
|
+
* own `outputSchema` validates; other subscription operations are free to use a different shape (see the
|
|
96
|
+
* `subscriptionId`-only regression case in
|
|
97
|
+
* `operation-catalog/dispatch-audit.test.ts`).
|
|
98
|
+
*/
|
|
99
|
+
export type SubscriptionStartEnvelope = {
|
|
100
|
+
readonly subscriptionId: string;
|
|
101
|
+
readonly cursor: string;
|
|
102
|
+
};
|
|
103
|
+
export type OperationInvocationResult<Output, Element = unknown> = Output | StreamOperationInvocation<Element> | SubscriptionOperationInvocation<Element, Output>;
|
|
104
|
+
/**
|
|
105
|
+
* Metadata that connects an operation-catalog entry to a live, engine-served
|
|
106
|
+
* MCP workflow tool. The exact MCP tool name is resolved from the live engine
|
|
107
|
+
* registry during discovery because `tools/list` owns collision handling.
|
|
108
|
+
*
|
|
109
|
+
* An `mcpExposable` operation without it is served as an MCP tool of its own
|
|
110
|
+
* name by dispatch through the catalog pipeline, by `createOperationMcpMount`.
|
|
111
|
+
*/
|
|
112
|
+
export type McpToolMetadata = {
|
|
113
|
+
readonly workflowType: string;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Result of the parameter-aware `authorize` hook.
|
|
117
|
+
*
|
|
118
|
+
* **`reason` is wire-visible.** Hook authors must not embed secrets or
|
|
119
|
+
* sensitive context in a denial reason.
|
|
120
|
+
*/
|
|
121
|
+
export type AuthorizationDecision = {
|
|
122
|
+
allowed: true;
|
|
123
|
+
} | {
|
|
124
|
+
allowed: false;
|
|
125
|
+
classification?: 'unauthorized' | 'forbidden';
|
|
126
|
+
reason: string;
|
|
127
|
+
};
|
|
128
|
+
/** A hint that an operation's required scope depends on one of its input fields. */
|
|
129
|
+
export type ParameterizedAccessHint = {
|
|
130
|
+
/** The input field that selects the variant. */
|
|
131
|
+
readonly discriminator: string;
|
|
132
|
+
/** The variant used when the input omits the field. */
|
|
133
|
+
readonly defaultValue?: string;
|
|
134
|
+
/** The values the field can take, each with its own access. */
|
|
135
|
+
readonly variants: ReadonlyArray<{
|
|
136
|
+
readonly value: string;
|
|
137
|
+
readonly access: AccessPolicy;
|
|
138
|
+
}>;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Stable audit markers emitted after each successful operation pipeline stage.
|
|
142
|
+
*/
|
|
143
|
+
export type PipelineTraceMarker = 'looked-up' | 'transport-checked' | 'access-checked' | 'parsed' | 'unknown-key-policy-applied' | 'authorized' | 'invoked' | 'output-validated';
|
|
144
|
+
/**
|
|
145
|
+
* Optional observer hook used by audit tests to prove a transport used the
|
|
146
|
+
* full `executeOperation` pipeline.
|
|
147
|
+
*/
|
|
148
|
+
export type PipelineTrace = (marker: PipelineTraceMarker) => void;
|
|
149
|
+
/**
|
|
150
|
+
* Context passed to both the `authorize` hook and `invoke`.
|
|
151
|
+
*/
|
|
152
|
+
export type OperationContext<Input, Host = unknown> = {
|
|
153
|
+
/** The parsed, validated input. */
|
|
154
|
+
readonly input: Input;
|
|
155
|
+
/** The authenticated caller. */
|
|
156
|
+
readonly principal: Principal;
|
|
157
|
+
/**
|
|
158
|
+
* Whatever the transport host registered these operations against. Weft's
|
|
159
|
+
* own handlers deliver the `Engine`; another package's handler delivers its
|
|
160
|
+
* own host object. An operation declares the shape it needs through `Host`,
|
|
161
|
+
* so a host that cannot supply it is a compile error at registration.
|
|
162
|
+
*/
|
|
163
|
+
readonly host: Host;
|
|
164
|
+
/** The transport the call arrived on. */
|
|
165
|
+
readonly transport: TransportKind;
|
|
166
|
+
/**
|
|
167
|
+
* The abort signal of the request that carried this call, when its transport has one: an HTTP
|
|
168
|
+
* transport (JSON-RPC over HTTP and the REST routes) passes the `Request`'s own signal, which
|
|
169
|
+
* aborts when the client goes away. Absent on a transport with no request of its own (the
|
|
170
|
+
* WebSocket and stdio JSON-RPC transports, MCP). The operation decides what an abort means for
|
|
171
|
+
* it; the pipeline neither checks it nor changes how a result or a fault is answered.
|
|
172
|
+
*/
|
|
173
|
+
readonly signal?: AbortSignal;
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* Common operation fields shared by unary, stream, and subscription kinds.
|
|
177
|
+
* The discriminated union below adds `kind` and `eventSchema` per kind.
|
|
178
|
+
*/
|
|
179
|
+
export type OperationDefinitionBase<Input, InvokeOutput, SchemaOutput = InvokeOutput, Host = unknown> = {
|
|
180
|
+
readonly name: string;
|
|
181
|
+
readonly mcpExposable: boolean;
|
|
182
|
+
readonly behavior?: OperationBehavior;
|
|
183
|
+
readonly conflictReasonTokens?: readonly string[];
|
|
184
|
+
readonly mcpTool?: McpToolMetadata;
|
|
185
|
+
readonly summary: string;
|
|
186
|
+
/**
|
|
187
|
+
* Optional longer-form prose describing what the operation does, its key
|
|
188
|
+
* inputs, and notable faults or preconditions. `summary` stays the short
|
|
189
|
+
* one-liner; `description` is the paragraph that surfaces in OpenAPI /
|
|
190
|
+
* OpenRPC operation descriptions, the CLI `weft api --describe` view, and
|
|
191
|
+
* MCP-facing discovery metadata. Optional: only the interactively-used
|
|
192
|
+
* subset of operations populate it. `summary` remains mandatory for all.
|
|
193
|
+
*/
|
|
194
|
+
readonly description?: string;
|
|
195
|
+
readonly tags: ReadonlyArray<string>;
|
|
196
|
+
/**
|
|
197
|
+
* Whether invoking this operation irreversibly mutates state or is
|
|
198
|
+
* otherwise hard to undo (cancel, purge, bulk-delete, raw storage writes,
|
|
199
|
+
* recover-all, worker drain). Consumers read this single source of truth
|
|
200
|
+
* instead of maintaining their own allowlists: the CLI `weft api` escape
|
|
201
|
+
* hatch refuses destructive operations without explicit confirmation,
|
|
202
|
+
* dashboard bulk actions gate their confirmations on it, and MCP exposure
|
|
203
|
+
* uses it to decide what to surface. Read-only operations (`get-*`,
|
|
204
|
+
* `list-*`, `aggregate`, `registry`, `metrics`) are `false`.
|
|
205
|
+
*
|
|
206
|
+
* Required with no implicit default so every new operation is forced to
|
|
207
|
+
* make the call explicitly — the same quality-floor pattern as the
|
|
208
|
+
* lint-disable rationale rule.
|
|
209
|
+
*/
|
|
210
|
+
readonly destructive: boolean;
|
|
211
|
+
readonly inputSchema: z.ZodType<Input>;
|
|
212
|
+
/**
|
|
213
|
+
* For unary operations, validates the returned value. For subscriptions,
|
|
214
|
+
* validates the subscribe envelope. For streams, describes the start/SSE
|
|
215
|
+
* metadata while each yielded element is validated by `eventSchema`.
|
|
216
|
+
*/
|
|
217
|
+
readonly outputSchema: z.ZodType<SchemaOutput>;
|
|
218
|
+
readonly access: AccessPolicy;
|
|
219
|
+
readonly parameterizedAccess?: ParameterizedAccessHint;
|
|
220
|
+
/** Fault codes this operation can raise in addition to universal pipeline defaults. */
|
|
221
|
+
readonly producibleFaults?: ReadonlyArray<FaultCode>;
|
|
222
|
+
/** Whether non-public operations should appear in generated discovery documents. */
|
|
223
|
+
readonly discoverable?: boolean;
|
|
224
|
+
/**
|
|
225
|
+
* Whether the operation takes a boolean `dryRun` input: with it `true`, the operation performs
|
|
226
|
+
* its authorization and validation, writes nothing, and answers what the call would do instead
|
|
227
|
+
* of doing it. Absent or `false` means the input has no such field. A destructive operation a
|
|
228
|
+
* caller may want to preview declares this, and its input schema carries the `dryRun` field.
|
|
229
|
+
*/
|
|
230
|
+
readonly supportsDryRun?: boolean;
|
|
231
|
+
readonly transports: TransportAvailability;
|
|
232
|
+
readonly unknownKeyPolicy: UnknownKeyPolicy;
|
|
233
|
+
readonly authorize?: (context: OperationContext<Input, Host>) => Promise<AuthorizationDecision>;
|
|
234
|
+
};
|
|
235
|
+
/** Metadata and schema hooks shared by typed authoring and dispatch stages. */
|
|
236
|
+
export type OperationMetadata<Input, SchemaOutput = unknown, Host = unknown> = OperationDefinitionBase<Input, unknown, SchemaOutput, Host>;
|
|
237
|
+
/**
|
|
238
|
+
* Unary (request/response) operation. `kind` may be omitted (defaults to
|
|
239
|
+
* `'unary'`). `eventSchema` MUST be absent — a unary operation has no
|
|
240
|
+
* per-element shape to validate. The `eventSchema?: never` constraint
|
|
241
|
+
* enforces this at the type level: passing `eventSchema` to a unary
|
|
242
|
+
* operation is a TypeScript error, not a silent runtime mismatch.
|
|
243
|
+
*/
|
|
244
|
+
type UnaryOperationDefinition<Input, InvokeOutput, SchemaOutput = InvokeOutput, Host = unknown> = OperationDefinitionBase<Input, InvokeOutput, SchemaOutput, Host> & {
|
|
245
|
+
readonly kind?: 'unary';
|
|
246
|
+
readonly eventSchema?: never;
|
|
247
|
+
readonly invoke: (context: OperationContext<Input, Host>) => Promise<InvokeOutput>;
|
|
248
|
+
};
|
|
249
|
+
/**
|
|
250
|
+
* Streaming operation (e.g. SSE). `kind: 'stream'` is required and
|
|
251
|
+
* `eventSchema` MUST be present — the dispatcher validates each yielded
|
|
252
|
+
* element against this schema. Without it the streaming pipeline would
|
|
253
|
+
* have no contract to validate per-element output against, which would
|
|
254
|
+
* silently leak un-validated data to consumers.
|
|
255
|
+
*/
|
|
256
|
+
type StreamOperationDefinition<Input, InvokeOutput, SchemaOutput, InvokeElement, Element, Host = unknown> = OperationDefinitionBase<Input, InvokeOutput, SchemaOutput, Host> & {
|
|
257
|
+
readonly kind: 'stream';
|
|
258
|
+
readonly eventSchema: z.ZodType<Element>;
|
|
259
|
+
readonly invoke: (context: OperationContext<Input, Host>) => Promise<InvokeOutput | StreamOperationInvocation<InvokeElement>>;
|
|
260
|
+
};
|
|
261
|
+
/**
|
|
262
|
+
* WebSocket subscription operation. `kind: 'subscription'` is required and
|
|
263
|
+
* `eventSchema` MUST be present (validates each delivered envelope). Same
|
|
264
|
+
* rationale as `StreamOperationDefinition` — the type forbids declaring a
|
|
265
|
+
* subscription without its element schema.
|
|
266
|
+
*/
|
|
267
|
+
type SubscriptionOperationDefinition<Input, InvokeOutput, SchemaOutput, InvokeElement, Element, Host = unknown> = OperationDefinitionBase<Input, InvokeOutput, SchemaOutput, Host> & {
|
|
268
|
+
readonly kind: 'subscription';
|
|
269
|
+
readonly eventSchema: z.ZodType<Element>;
|
|
270
|
+
readonly invoke: (context: OperationContext<Input, Host>) => Promise<SubscriptionOperationInvocation<InvokeElement, InvokeOutput>>;
|
|
271
|
+
};
|
|
272
|
+
/**
|
|
273
|
+
* Discriminated union over the three operation kinds. The discriminator
|
|
274
|
+
* (`kind`) determines whether `eventSchema` is required (`'stream'` /
|
|
275
|
+
* `'subscription'`) or forbidden (`'unary'` or absent). Streaming
|
|
276
|
+
* operations declared without `eventSchema` are now a TypeScript error
|
|
277
|
+
* rather than a runtime EngineFailure; unary operations cannot
|
|
278
|
+
* accidentally carry an `eventSchema` that the pipeline would never read.
|
|
279
|
+
*/
|
|
280
|
+
export type OperationDefinition<Input, InvokeOutput, SchemaOutput = InvokeOutput, InvokeElement = unknown, Element = InvokeElement, Host = unknown> = UnaryOperationDefinition<Input, InvokeOutput, SchemaOutput, Host> | StreamOperationDefinition<Input, InvokeOutput, SchemaOutput, InvokeElement, Element, Host> | SubscriptionOperationDefinition<Input, InvokeOutput, SchemaOutput, InvokeElement, Element, Host>;
|
|
281
|
+
/**
|
|
282
|
+
* An operation with its Input/Output type parameters erased. The dispatcher
|
|
283
|
+
* only feeds values that have been validated by the operation's own schema.
|
|
284
|
+
*/
|
|
285
|
+
/**
|
|
286
|
+
* Registry-facing operation metadata. Invocation stays behind the schema-owned
|
|
287
|
+
* dispatch function so an erased registry entry can never be called with an
|
|
288
|
+
* invented input type.
|
|
289
|
+
*/
|
|
290
|
+
export type ErasedOperation = RegistrableOperation;
|
|
291
|
+
/** Read-only registry of operations keyed by name. */
|
|
292
|
+
export type OperationRegistry = {
|
|
293
|
+
/** The operation with the given name, if any. */
|
|
294
|
+
get(name: string): ErasedOperation | undefined;
|
|
295
|
+
/** Every registered operation. */
|
|
296
|
+
list(): ReadonlyArray<ErasedOperation>;
|
|
297
|
+
};
|
|
298
|
+
/**
|
|
299
|
+
* Erased operation shape accepted by `createOperationRegistry`. Mirrors
|
|
300
|
+
* the discriminated union on `OperationDefinition` so registry callers
|
|
301
|
+
* declaring a stream/subscription without `eventSchema` get a compile-time
|
|
302
|
+
* error rather than a runtime `EngineFailure` when the pipeline first
|
|
303
|
+
* attempts per-element validation.
|
|
304
|
+
*/
|
|
305
|
+
type RegistrableOperationBase<Host> = {
|
|
306
|
+
readonly name: string;
|
|
307
|
+
readonly mcpExposable: boolean;
|
|
308
|
+
readonly behavior?: OperationBehavior;
|
|
309
|
+
readonly conflictReasonTokens?: readonly string[];
|
|
310
|
+
readonly mcpTool?: McpToolMetadata;
|
|
311
|
+
readonly summary: string;
|
|
312
|
+
/** See {@link OperationDefinitionBase.description}. Optional longer-form prose. */
|
|
313
|
+
readonly description?: string;
|
|
314
|
+
readonly tags: ReadonlyArray<string>;
|
|
315
|
+
/** See {@link OperationDefinitionBase.destructive}. Required, no default. */
|
|
316
|
+
readonly destructive: boolean;
|
|
317
|
+
readonly inputSchema: z.ZodType;
|
|
318
|
+
readonly outputSchema: z.ZodType;
|
|
319
|
+
readonly access: AccessPolicy;
|
|
320
|
+
readonly parameterizedAccess?: ParameterizedAccessHint;
|
|
321
|
+
/** Fault codes this operation can raise in addition to universal pipeline defaults. */
|
|
322
|
+
readonly producibleFaults?: ReadonlyArray<FaultCode>;
|
|
323
|
+
/** Whether non-public operations should appear in generated discovery documents. */
|
|
324
|
+
readonly discoverable?: boolean;
|
|
325
|
+
/** See {@link OperationDefinitionBase.supportsDryRun}. */
|
|
326
|
+
readonly supportsDryRun?: boolean;
|
|
327
|
+
readonly transports: TransportAvailability;
|
|
328
|
+
readonly unknownKeyPolicy: UnknownKeyPolicy;
|
|
329
|
+
readonly dispatch: (rawInput: unknown, context: DispatchContext<Host>) => Promise<DispatchResult<unknown>>;
|
|
330
|
+
/** Check policy without invoking the operation, when the operation supports it. */
|
|
331
|
+
readonly checkPolicy?: (rawInput: unknown, context: DispatchContext<Host>) => Promise<DispatchResult<void>>;
|
|
332
|
+
};
|
|
333
|
+
type UnaryRegistrableOperation<Host> = RegistrableOperationBase<Host> & {
|
|
334
|
+
readonly kind?: 'unary';
|
|
335
|
+
readonly eventSchema?: never;
|
|
336
|
+
};
|
|
337
|
+
type StreamRegistrableOperation<Host> = RegistrableOperationBase<Host> & {
|
|
338
|
+
readonly kind: 'stream';
|
|
339
|
+
readonly eventSchema: z.ZodType;
|
|
340
|
+
};
|
|
341
|
+
type SubscriptionRegistrableOperation<Host> = RegistrableOperationBase<Host> & {
|
|
342
|
+
readonly kind: 'subscription';
|
|
343
|
+
readonly eventSchema: z.ZodType;
|
|
344
|
+
};
|
|
345
|
+
/** An operation the registry can hold, of any kind. */
|
|
346
|
+
export type RegistrableOperation<Host = unknown> = UnaryRegistrableOperation<Host> | StreamRegistrableOperation<Host> | SubscriptionRegistrableOperation<Host>;
|
|
347
|
+
/**
|
|
348
|
+
* What the host that owns a registry decides about the faults its operations throw. The generic
|
|
349
|
+
* dispatch code reads no environment variable and names no engine error class: it asks this.
|
|
350
|
+
*/
|
|
351
|
+
export type FaultPolicy = {
|
|
352
|
+
/**
|
|
353
|
+
* Whether a thrown `OperationFault` whose code is neither declared in the operation's
|
|
354
|
+
* `producibleFaults` nor a universal default answers `EngineFailure` (strict) or passes
|
|
355
|
+
* through with a warning. A function is read on every undeclared fault, so a host can follow
|
|
356
|
+
* a setting that changes while the registry lives. Omitted, strict: a host that supplies
|
|
357
|
+
* nothing never lets an undeclared fault through silently.
|
|
358
|
+
*/
|
|
359
|
+
readonly strict?: boolean | (() => boolean);
|
|
360
|
+
/**
|
|
361
|
+
* Classifies a thrown value the dispatch code does not recognise (anything that is not an
|
|
362
|
+
* `OperationFault`), such as the host's own error classes. Return `undefined` to leave it to
|
|
363
|
+
* the generic message heuristics.
|
|
364
|
+
*/
|
|
365
|
+
readonly classifyError?: (error: unknown) => OperationFault | undefined;
|
|
366
|
+
/**
|
|
367
|
+
* Whether a string is one of the host's fine-grained error codes. A fault's `data.weftCode`
|
|
368
|
+
* reaches a REST body only when this accepts it; omitted, every string passes.
|
|
369
|
+
*/
|
|
370
|
+
readonly isKnownErrorCode?: (value: string) => boolean;
|
|
371
|
+
};
|
|
372
|
+
export type DispatchContext<Host = unknown> = {
|
|
373
|
+
readonly principal: Principal;
|
|
374
|
+
/** The registry's {@link FaultPolicy}, which `createOperationRegistry` puts on every dispatch. */
|
|
375
|
+
readonly faultPolicy?: FaultPolicy;
|
|
376
|
+
/** See {@link OperationContext.host}. */
|
|
377
|
+
readonly host: Host | OperationHostFactory<Host>;
|
|
378
|
+
readonly interceptors?: readonly OperationInterceptor[];
|
|
379
|
+
readonly transport: TransportKind;
|
|
380
|
+
readonly registry: OperationRegistry;
|
|
381
|
+
readonly pipelineTrace?: PipelineTrace;
|
|
382
|
+
/** See {@link OperationContext.signal}: copied into the context `authorize` and `invoke` read. */
|
|
383
|
+
readonly signal?: AbortSignal;
|
|
384
|
+
};
|
|
385
|
+
/** The outcome of dispatching one operation: its value, or the {@link OperationFault} that stopped it. */
|
|
386
|
+
export type DispatchResult<Output> = {
|
|
387
|
+
readonly ok: true;
|
|
388
|
+
readonly value: Output;
|
|
389
|
+
} | {
|
|
390
|
+
readonly ok: false;
|
|
391
|
+
readonly fault: OperationFault;
|
|
392
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Regex for the canonical `<namespace>.<segment>(.<segment>)*` operation-name
|
|
4
|
+
* form: at least two dot-separated segments, each starting with a lowercase
|
|
5
|
+
* ASCII letter and continuing with lowercase ASCII letters or digits.
|
|
6
|
+
*
|
|
7
|
+
* THE NAMESPACE IS NO LONGER FIXED TO `weft`. It was, and every operation this
|
|
8
|
+
* package defines still begins with `weft.` — the change is backward
|
|
9
|
+
* compatible, because the old pattern's language is a strict subset of this
|
|
10
|
+
* one. What it admits is a catalog shared with other packages: a gateway that
|
|
11
|
+
* serves `@lostgradient/operative` and `@lostgradient/bureau` alongside Weft needs
|
|
12
|
+
* `operative.*` and `bureau.*` to be legal names, and those packages already
|
|
13
|
+
* depend on this one, so registering through this catalog is the path that
|
|
14
|
+
* keeps one dispatch pipeline, one transport matrix, and one AsyncAPI document
|
|
15
|
+
* rather than a second RPC dialect beside them.
|
|
16
|
+
*
|
|
17
|
+
* The two-segment minimum is retained deliberately: a bare `start` carries no
|
|
18
|
+
* owner, and an unnamespaced catalog is one collision away from ambiguity
|
|
19
|
+
* about which package answers a method.
|
|
20
|
+
*
|
|
21
|
+
* `rpc.` IS RESERVED, and the negative lookahead is what keeps a guarantee the
|
|
22
|
+
* mandatory `weft.` prefix used to provide for free. JSON-RPC 2.0 reserves
|
|
23
|
+
* every method name beginning with `rpc.` for rpc-internal methods and
|
|
24
|
+
* extensions, and this server answers `rpc.discover` itself. Under a pattern
|
|
25
|
+
* that admitted any namespace, a registered `rpc.discover` would shadow
|
|
26
|
+
* discovery, and the only thing standing between that and a served catalog
|
|
27
|
+
* would be the OpenRPC generator's deduplication guard — a backstop rather
|
|
28
|
+
* than an impossibility. `openrpc-regressions.test.ts` pins this.
|
|
29
|
+
*/
|
|
30
|
+
export const OPERATION_NAME_PATTERN = /^(?!rpc\.)[a-z][a-z0-9]*(?:\.[a-z][a-z0-9]*)+$/;
|
|
31
|
+
/** Throws if `name` does not match the canonical operation-name pattern. */
|
|
32
|
+
export function validateOperationName(name) {
|
|
33
|
+
if (!OPERATION_NAME_PATTERN.test(name)) {
|
|
34
|
+
throw new Error(`invalid operation name "${name}" — must match <namespace>.<segment>(.<segment>)* where each segment starts with a lowercase ASCII letter and may contain lowercase ASCII letters or digits (e.g., "weft.workflows.start", "operative.runs.list2")`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** Non-throwing variant of `validateOperationName`. */
|
|
38
|
+
export function isValidOperationName(name) {
|
|
39
|
+
return OPERATION_NAME_PATTERN.test(name);
|
|
40
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The fault codes every operation can raise without declaring them.
|
|
3
|
+
*
|
|
4
|
+
* @module server/operation-catalog/universal-faults
|
|
5
|
+
*/
|
|
6
|
+
import type { OperationFault } from "../operation-fault.js";
|
|
7
|
+
/**
|
|
8
|
+
* Fault codes that every operation can raise through the shared pipeline
|
|
9
|
+
* without listing them in `producibleFaults`.
|
|
10
|
+
*/
|
|
11
|
+
export declare const UNIVERSAL_FAULT_DEFAULTS: ReadonlySet<OperationFault['code']>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The fault codes every operation can raise without declaring them.
|
|
3
|
+
*
|
|
4
|
+
* @module server/operation-catalog/universal-faults
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Fault codes that every operation can raise through the shared pipeline
|
|
8
|
+
* without listing them in `producibleFaults`.
|
|
9
|
+
*/
|
|
10
|
+
export const UNIVERSAL_FAULT_DEFAULTS = new Set([
|
|
11
|
+
'Unauthorized',
|
|
12
|
+
'Forbidden',
|
|
13
|
+
'InvalidParams',
|
|
14
|
+
'EngineFailure',
|
|
15
|
+
]);
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import type { FaultCode } from "./fault-code.js";
|
|
2
|
+
import type { ErasedOperation, OperationBehavior, OperationKind, TransportAvailability } from "./operation-catalog.js";
|
|
3
|
+
import type { ParamSource, ResponseShape, RestBindingDescription } from "./rest-binding.js";
|
|
4
|
+
/**
|
|
5
|
+
* One operation's REST route as the catalog records it: the HTTP method and
|
|
6
|
+
* server-side path, where each input field travels, and the shape of a
|
|
7
|
+
* successful response.
|
|
8
|
+
*
|
|
9
|
+
* This is the snapshot's own record of the route. The generated client carries
|
|
10
|
+
* the same facts in `CLIENT_REST_OPERATION_BINDINGS`, with the `/v1` prefix
|
|
11
|
+
* already stripped — use that one to call Weft, and this one to describe it.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { type CatalogRestBindingSnapshot } from '@lostgradient/backchannel';
|
|
16
|
+
* import { createCatalogSnapshot } from '@lostgradient/weft';
|
|
17
|
+
*
|
|
18
|
+
* const routes = createCatalogSnapshot()
|
|
19
|
+
* .operations.map((operation) => operation.rest)
|
|
20
|
+
* .filter((rest): rest is CatalogRestBindingSnapshot => rest !== undefined);
|
|
21
|
+
*
|
|
22
|
+
* console.log(routes.length, 'operations are reachable over REST');
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export type CatalogRestBindingSnapshot = {
|
|
26
|
+
/** The HTTP method. */
|
|
27
|
+
readonly method: string;
|
|
28
|
+
/** The path template, with `:name` parameters. */
|
|
29
|
+
readonly path: string;
|
|
30
|
+
/** Where each input field travels: path, query, header or body. */
|
|
31
|
+
readonly inputSources: Readonly<Record<string, ParamSource>>;
|
|
32
|
+
/** What a successful response looks like. */
|
|
33
|
+
readonly success: ResponseShape;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* What an operation demands of its caller, as a discriminated union on `kind`:
|
|
37
|
+
* nothing (`public`), any valid token (`authenticated`), specific scopes
|
|
38
|
+
* (`scoped`), scopes only when a token is present (`optionalAuth`), or any one
|
|
39
|
+
* of several scope sets (`scopedAlternatives`).
|
|
40
|
+
*
|
|
41
|
+
* Narrow on `kind` before reading `scopes` or `alternatives` — only some
|
|
42
|
+
* variants carry them. `scopedAlternatives` is a disjunction: holding every
|
|
43
|
+
* scope in any single inner array is enough.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* import type { CatalogAccessSnapshot } from '@lostgradient/backchannel';
|
|
48
|
+
*
|
|
49
|
+
* declare const access: CatalogAccessSnapshot;
|
|
50
|
+
*
|
|
51
|
+
* switch (access.kind) {
|
|
52
|
+
* case 'public':
|
|
53
|
+
* console.log('no credentials needed');
|
|
54
|
+
* break;
|
|
55
|
+
* case 'scoped':
|
|
56
|
+
* console.log('requires all of:', access.scopes.join(', '));
|
|
57
|
+
* break;
|
|
58
|
+
* case 'scopedAlternatives':
|
|
59
|
+
* console.log('requires any one set of:', access.alternatives.length);
|
|
60
|
+
* break;
|
|
61
|
+
* default:
|
|
62
|
+
* console.log('requires a token');
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export type CatalogAccessSnapshot = {
|
|
67
|
+
readonly kind: 'public';
|
|
68
|
+
} | {
|
|
69
|
+
readonly kind: 'authenticated';
|
|
70
|
+
} | {
|
|
71
|
+
readonly kind: 'optionalAuth';
|
|
72
|
+
readonly scopes: ReadonlyArray<string>;
|
|
73
|
+
} | {
|
|
74
|
+
readonly kind: 'scoped';
|
|
75
|
+
readonly scopes: ReadonlyArray<string>;
|
|
76
|
+
} | {
|
|
77
|
+
readonly kind: 'scopedAlternatives';
|
|
78
|
+
readonly alternatives: ReadonlyArray<ReadonlyArray<string>>;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Everything the catalog records about one operation: its name and summary,
|
|
82
|
+
* the access it demands, which transports serve it, the faults it can raise,
|
|
83
|
+
* and JSON Schema for its input and output.
|
|
84
|
+
*
|
|
85
|
+
* This is the unit the code generators consume — `weft codegen` and the
|
|
86
|
+
* generated operation client both read these records rather than reaching into
|
|
87
|
+
* the live registry. Read one when you need to answer a question about an
|
|
88
|
+
* operation without calling it: whether it is destructive, whether it is
|
|
89
|
+
* reachable over JSON-RPC, what shape its input takes.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* import { type CatalogOperationSnapshot } from '@lostgradient/backchannel';
|
|
94
|
+
* import { createCatalogSnapshot } from '@lostgradient/weft';
|
|
95
|
+
*
|
|
96
|
+
* const byName = new Map<string, CatalogOperationSnapshot>(
|
|
97
|
+
* createCatalogSnapshot().operations.map((operation) => [operation.name, operation]),
|
|
98
|
+
* );
|
|
99
|
+
*
|
|
100
|
+
* const alerts = byName.get('weft.alerts.list');
|
|
101
|
+
* console.log(alerts?.summary, alerts?.transports.jsonRpcHttp);
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export type CatalogOperationSnapshot = {
|
|
105
|
+
/** The operation's name. */
|
|
106
|
+
readonly name: string;
|
|
107
|
+
/** Whether the operation may be exposed as an MCP tool. */
|
|
108
|
+
readonly mcpExposable?: boolean;
|
|
109
|
+
/** Producer-declared read or command semantics for generated consumers. */
|
|
110
|
+
readonly behavior?: OperationBehavior;
|
|
111
|
+
/** Conflict reason tokens the operation declares for its command behavior. */
|
|
112
|
+
readonly conflictReasonTokens?: readonly string[];
|
|
113
|
+
/** Whether the operation is unary, a stream or a subscription. */
|
|
114
|
+
readonly kind: OperationKind;
|
|
115
|
+
/** A one-line description of the operation. */
|
|
116
|
+
readonly summary: string;
|
|
117
|
+
/** Optional longer-form prose; present only for the interactive subset. */
|
|
118
|
+
readonly description?: string;
|
|
119
|
+
/** The groups the operation belongs to; the first names its domain. */
|
|
120
|
+
readonly tags: ReadonlyArray<string>;
|
|
121
|
+
/** Whether the operation changes or deletes state. */
|
|
122
|
+
readonly destructive: boolean;
|
|
123
|
+
/**
|
|
124
|
+
* `true` when the operation's input takes a boolean `dryRun` that performs its authorization
|
|
125
|
+
* and validation, writes nothing, and answers what the call would do; absent otherwise.
|
|
126
|
+
*/
|
|
127
|
+
readonly supportsDryRun?: true;
|
|
128
|
+
/** The operation's access policy. */
|
|
129
|
+
readonly access: CatalogAccessSnapshot;
|
|
130
|
+
/** The input field the required scope depends on, with its variants. */
|
|
131
|
+
readonly parameterizedAccess?: {
|
|
132
|
+
readonly discriminator: string;
|
|
133
|
+
readonly defaultValue?: string;
|
|
134
|
+
readonly variants: ReadonlyArray<{
|
|
135
|
+
readonly value: string;
|
|
136
|
+
readonly access: CatalogAccessSnapshot;
|
|
137
|
+
}>;
|
|
138
|
+
};
|
|
139
|
+
/** Which transports serve the operation. */
|
|
140
|
+
readonly transports: TransportAvailability;
|
|
141
|
+
/** The fault codes the operation can produce. */
|
|
142
|
+
readonly producibleFaults: ReadonlyArray<FaultCode>;
|
|
143
|
+
/** The JSON Schema of the operation's input. */
|
|
144
|
+
readonly inputSchema: Record<string, unknown>;
|
|
145
|
+
/** The JSON Schema of the operation's output. */
|
|
146
|
+
readonly outputSchema: Record<string, unknown>;
|
|
147
|
+
/** The JSON Schema of each event, for a stream or subscription operation. */
|
|
148
|
+
readonly eventSchema?: Record<string, unknown>;
|
|
149
|
+
/** Declarative REST route metadata for generated client transports. */
|
|
150
|
+
readonly rest?: CatalogRestBindingSnapshot;
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* A complete, serialisable description of a server's operation surface: every
|
|
154
|
+
* {@link CatalogOperationSnapshot}, sorted by name, under a version stamp.
|
|
155
|
+
*
|
|
156
|
+
* Think of it as the API's contract in data form. Vendor one into a repository
|
|
157
|
+
* and `weft codegen --from` will generate a client against exactly that
|
|
158
|
+
* surface, so builds stay reproducible and a server upgrade becomes a visible
|
|
159
|
+
* diff instead of a silent change. `version` is the snapshot format's own
|
|
160
|
+
* version, not the server's.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* import { type CatalogSnapshot } from '@lostgradient/backchannel';
|
|
165
|
+
* import { createCatalogSnapshot } from '@lostgradient/weft';
|
|
166
|
+
*
|
|
167
|
+
* const snapshot: CatalogSnapshot = createCatalogSnapshot();
|
|
168
|
+
* console.log(snapshot.version, snapshot.operations.length);
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
export type CatalogSnapshot = {
|
|
172
|
+
/** The tool that wrote the snapshot. */
|
|
173
|
+
readonly generatedBy: 'weft catalog snapshot';
|
|
174
|
+
/** The snapshot format version. */
|
|
175
|
+
readonly version: 1;
|
|
176
|
+
/** Every operation in the catalog. */
|
|
177
|
+
readonly operations: ReadonlyArray<CatalogOperationSnapshot>;
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* Capture a set of operations, and the REST routes that serve them, as a
|
|
181
|
+
* {@link CatalogSnapshot}.
|
|
182
|
+
*
|
|
183
|
+
* The result is deterministic: operations sorted by name, schemas normalised,
|
|
184
|
+
* so two snapshots of the same surface are byte-identical and a real surface
|
|
185
|
+
* change is the only thing that shows up in a diff. That is what makes it safe
|
|
186
|
+
* to commit one and regenerate clients from it.
|
|
187
|
+
*
|
|
188
|
+
* A server supplies its own `operations` and `restBindings`. Weft's
|
|
189
|
+
* `createCatalogSnapshot` supplies every operation and route this build
|
|
190
|
+
* registers.
|
|
191
|
+
*/
|
|
192
|
+
export declare function snapshotOperationCatalog(options: {
|
|
193
|
+
readonly operations: ReadonlyArray<ErasedOperation>;
|
|
194
|
+
readonly restBindings: ReadonlyArray<RestBindingDescription>;
|
|
195
|
+
}): CatalogSnapshot;
|
|
196
|
+
/**
|
|
197
|
+
* Serialise a {@link CatalogSnapshot} the way Weft writes it to disk:
|
|
198
|
+
* two-space indentation and a trailing newline.
|
|
199
|
+
*
|
|
200
|
+
* Use this rather than `JSON.stringify` when the output is going into a file.
|
|
201
|
+
* The formatting is part of the contract — a vendored snapshot written any
|
|
202
|
+
* other way produces a whole-file diff on the next regeneration, which buries
|
|
203
|
+
* the surface change you actually wanted to review.
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```ts
|
|
207
|
+
* import { stringifyCatalogSnapshot } from '@lostgradient/backchannel';
|
|
208
|
+
* import { createCatalogSnapshot } from '@lostgradient/weft';
|
|
209
|
+
*
|
|
210
|
+
* const serialized = stringifyCatalogSnapshot(createCatalogSnapshot());
|
|
211
|
+
*
|
|
212
|
+
* console.log(serialized.endsWith('\n')); // true
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
export declare function stringifyCatalogSnapshot(snapshot: CatalogSnapshot): string;
|