@mountaya/client 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/CHANGELOG.md +104 -0
- package/LICENCE.fr.txt +280 -0
- package/LICENSE.txt +262 -0
- package/README.md +1260 -2
- package/dist/admin/index.cjs +3521 -0
- package/dist/admin/index.cjs.map +1 -0
- package/dist/admin/index.d.cts +1070 -0
- package/dist/admin/index.d.ts +1070 -0
- package/dist/admin/index.js +301 -0
- package/dist/admin/index.js.map +1 -0
- package/dist/browser.cjs +8073 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.cts +106 -0
- package/dist/browser.d.ts +106 -0
- package/dist/browser.js +53 -0
- package/dist/browser.js.map +1 -0
- package/dist/chunk-BM526BHG.js +342 -0
- package/dist/chunk-BM526BHG.js.map +1 -0
- package/dist/chunk-D2YEVTG7.js +327 -0
- package/dist/chunk-D2YEVTG7.js.map +1 -0
- package/dist/chunk-OHKVUREN.js +3500 -0
- package/dist/chunk-OHKVUREN.js.map +1 -0
- package/dist/chunk-TDTK2L5H.js +3842 -0
- package/dist/chunk-TDTK2L5H.js.map +1 -0
- package/dist/coreOperations.gen-BMYPoBbz.d.ts +1760 -0
- package/dist/coreOperations.gen-BxX5xRxw.d.cts +1760 -0
- package/dist/errors-B-GqAtQv.d.cts +75960 -0
- package/dist/errors-B-GqAtQv.d.ts +75960 -0
- package/dist/events/index.cjs +1972 -0
- package/dist/events/index.cjs.map +1 -0
- package/dist/events/index.d.cts +781 -0
- package/dist/events/index.d.ts +781 -0
- package/dist/events/index.js +826 -0
- package/dist/events/index.js.map +1 -0
- package/dist/index.cjs +8072 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +76 -0
- package/dist/index.d.ts +76 -0
- package/dist/index.js +52 -0
- package/dist/index.js.map +1 -0
- package/dist/operations.gen-CvTyzKNF.d.cts +3993 -0
- package/dist/operations.gen-DUD406TV.d.ts +3993 -0
- package/package.json +127 -4
- package/src/admin/index.ts +47 -0
- package/src/admin/rules/mountayaAdmin.ts +102 -0
- package/src/browser.ts +232 -0
- package/src/core/engines/sessionToken.ts +870 -0
- package/src/core/rules/auth.ts +1017 -0
- package/src/core/rules/clientHeader.ts +28 -0
- package/src/core/rules/dates.ts +200 -0
- package/src/core/rules/decode.ts +154 -0
- package/src/core/rules/envelope.ts +41 -0
- package/src/core/rules/errors.ts +1486 -0
- package/src/core/rules/graphql.ts +576 -0
- package/src/core/rules/headerValue.ts +42 -0
- package/src/core/rules/idempotencyKey.ts +64 -0
- package/src/core/rules/nullMask.ts +127 -0
- package/src/core/rules/nullMasks.gen.ts +416 -0
- package/src/core/rules/paginate.ts +316 -0
- package/src/core/rules/rateLimit.ts +507 -0
- package/src/core/rules/redaction.ts +219 -0
- package/src/core/rules/resourceRef.ts +110 -0
- package/src/core/rules/responseBody.ts +492 -0
- package/src/core/rules/retry.ts +386 -0
- package/src/core/rules/selection.ts +416 -0
- package/src/core/rules/sessionAnswer.ts +90 -0
- package/src/core/rules/sessionSchedule.ts +131 -0
- package/src/core/rules/sessionWait.ts +112 -0
- package/src/core/rules/transport.ts +2386 -0
- package/src/core/rules/version.gen.ts +16 -0
- package/src/core/rules/webAddress.ts +165 -0
- package/src/core/rules/webAddresses.gen.ts +30 -0
- package/src/data/operations.gen.ts +603 -0
- package/src/data/schema.gen.ts +8078 -0
- package/src/data/selection.gen.ts +472 -0
- package/src/events/index.ts +67 -0
- package/src/events/rules/agent.ts +439 -0
- package/src/events/rules/collab.ts +554 -0
- package/src/events/rules/collabNulls.gen.ts +17 -0
- package/src/events/rules/eventStream.ts +94 -0
- package/src/events/rules/openSseStream.ts +594 -0
- package/src/events/rules/sse.ts +349 -0
- package/src/index.ts +231 -0
- package/src/rest/adminOperations.gen.ts +70 -0
- package/src/rest/adminResources.gen.ts +136 -0
- package/src/rest/coreOperations.gen.ts +74 -0
- package/src/rest/coreResources.gen.ts +244 -0
- package/src/rest/dates.gen.ts +217 -0
- package/src/rest/operations.gen.ts +4341 -0
- package/src/rest/resources.gen.ts +369 -0
- package/src/rest/schema.gen.ts +56840 -0
- package/src/tiles/operations.gen.ts +206 -0
- package/src/tiles/schema.gen.ts +8408 -0
- package/src/webhooks/rules/constructWebhookEvent.ts +482 -0
- package/src/webhooks/rules/webhookEvent.ts +38 -0
- package/src/webhooks/rules/webhookEvents.gen.ts +732 -0
|
@@ -0,0 +1,781 @@
|
|
|
1
|
+
import { MountayaError, AgentRunEvent, AgentRunInputParams, AgentRunCreateParams, CollabStreamEvent, CollabChangeAction, CollabSignalParams } from '../browser.cjs';
|
|
2
|
+
import { MountayaTransport } from '../index.cjs';
|
|
3
|
+
import 'geojson';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* SseFrame is one Server-Sent Events record that carried data: its `id`, its `event` name, and
|
|
7
|
+
* the raw `data` text, still a string the caller parses and validates.
|
|
8
|
+
*
|
|
9
|
+
* `id` is the record's own `id:` field, and the empty string where the record named none. It is
|
|
10
|
+
* the record's, not the stream's: the position a reconnect resumes from carries over from record
|
|
11
|
+
* to record, and {@link SseFrameParser.lastEventId} holds it. A record that carries no `data:` (a
|
|
12
|
+
* heartbeat comment, an `id:` alone) dispatches no frame.
|
|
13
|
+
*/
|
|
14
|
+
interface SseFrame {
|
|
15
|
+
/** The record's `data:` lines, joined with newlines, unparsed. */
|
|
16
|
+
data: string;
|
|
17
|
+
/** The record's last `event:` name, or the empty string where it names none. */
|
|
18
|
+
event: string;
|
|
19
|
+
/** The record's own `id:`, or the empty string where it names none. */
|
|
20
|
+
id: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* MAX_SSE_BUFFER_CHARS caps the text of one record a parser has not seen the end of: every
|
|
24
|
+
* character read since the record began, its line endings and comment lines included. A record
|
|
25
|
+
* ends only at an empty line, so a producer (or a proxy, or a hostile endpoint) that opens a
|
|
26
|
+
* `text/event-stream` and never sends one would grow the parser for as long as the connection
|
|
27
|
+
* lives. The bound turns that resource exhaustion into a fault the caller can act on.
|
|
28
|
+
*
|
|
29
|
+
* 8 MiB is measured against the largest legitimate record rather than the typical one: a
|
|
30
|
+
* collaboration frame is kilobytes (a presence or lock roster, one durable change), so even a
|
|
31
|
+
* megabyte-scale record keeps eightfold headroom while a runaway producer is stopped long before
|
|
32
|
+
* it threatens the tab. The unit is UTF-16 code units, a JavaScript string's own `length`, so a
|
|
33
|
+
* record at the cap costs at most twice this many bytes of heap.
|
|
34
|
+
*/
|
|
35
|
+
declare const MAX_SSE_BUFFER_CHARS: number;
|
|
36
|
+
/**
|
|
37
|
+
* SseBufferOverflowError is thrown by a parser whose unterminated record has passed its cap. It
|
|
38
|
+
* carries the `limit` it enforced, so a consumer can tell a broken producer apart from a network
|
|
39
|
+
* fault when it classifies the failure. It is a fault, not an outcome: the read it surfaces
|
|
40
|
+
* through is torn down rather than reported as a stream that stopped or ended.
|
|
41
|
+
*
|
|
42
|
+
* A parser knows no operation, so what it throws names none. `openSseStream` hands the overflow
|
|
43
|
+
* on as an error of this class that names the stream's operation, keeps the `limit`, and carries
|
|
44
|
+
* the parser's own error as its `cause`.
|
|
45
|
+
*
|
|
46
|
+
* It extends {@link MountayaError}, so the one catch clause this package documents for telling a
|
|
47
|
+
* platform failure from a bug in the calling program, `if (error instanceof MountayaError)`,
|
|
48
|
+
* covers it. A stream torn down by a malformed producer is a platform failure, and an error outside
|
|
49
|
+
* the family would read as the application's own crash.
|
|
50
|
+
*/
|
|
51
|
+
declare class SseBufferOverflowError extends MountayaError {
|
|
52
|
+
readonly name = "SseBufferOverflowError";
|
|
53
|
+
/** The cap the parser enforced, in UTF-16 code units. */
|
|
54
|
+
readonly limit: number;
|
|
55
|
+
/**
|
|
56
|
+
* Builds the error a parser throws once its unterminated record passes the cap.
|
|
57
|
+
*
|
|
58
|
+
* @param limit - the cap the parser enforced, in UTF-16 code units.
|
|
59
|
+
* @param operationId - the operation whose stream overflowed, where the thrower knows it: the
|
|
60
|
+
* message then opens with it and ends with what a reopen would meet.
|
|
61
|
+
* @param options - the standard error options, for the `cause` an opener hands on.
|
|
62
|
+
*/
|
|
63
|
+
constructor(limit: number, operationId?: string, options?: ErrorOptions);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* SseFrameParser frames the text of a `text/event-stream` body into records, by the HTML
|
|
67
|
+
* standard's event-stream rules:
|
|
68
|
+
*
|
|
69
|
+
* - A line ends at CRLF, LF or a lone CR. A CR ends its line at once, and an LF that follows it,
|
|
70
|
+
* in the same push or at the start of the next, completes that CRLF rather than ending a
|
|
71
|
+
* second, empty line.
|
|
72
|
+
* - A record ends at an empty line. Its `data:` lines join with newlines, its last `event:` names
|
|
73
|
+
* it, and a comment line (a leading `:`, such as a heartbeat) or a field the frame has no slot
|
|
74
|
+
* for (`retry:`) is read past.
|
|
75
|
+
* - One byte-order mark at the start of the stream is dropped.
|
|
76
|
+
* - A record that carries no `data:` dispatches no frame. Its `id:` still moves
|
|
77
|
+
* {@link SseFrameParser.lastEventId}, so a record naming a position and nothing else moves the
|
|
78
|
+
* position a reconnect resumes from.
|
|
79
|
+
* - Every record that carries data dispatches a frame, whatever the data says. No data is an end
|
|
80
|
+
* marker to the parser: what ends a stream is the caller's vocabulary to name.
|
|
81
|
+
*
|
|
82
|
+
* A push may end anywhere, inside a line or inside a record, so the parser holds what is
|
|
83
|
+
* unterminated for the next push. It searches a character for a line ending in the push that
|
|
84
|
+
* delivers it and never again, so framing a long record costs its length however many pushes it
|
|
85
|
+
* arrives in. A character split between two reads of the body arrives whole only if the bytes
|
|
86
|
+
* are decoded across reads, which `readEventStream` does.
|
|
87
|
+
*
|
|
88
|
+
* What it holds is bounded (see {@link MAX_SSE_BUFFER_CHARS}), and only the unterminated record
|
|
89
|
+
* counts: a push carrying many complete records is legitimate however large. Past the cap the
|
|
90
|
+
* parser throws {@link SseBufferOverflowError} and is definitively failed: it releases the text it
|
|
91
|
+
* was holding and refuses every later push, so a caller that swallowed the throw can never resume
|
|
92
|
+
* mid-record and frame the tail of a refused record as a record of its own.
|
|
93
|
+
*/
|
|
94
|
+
declare class SseFrameParser {
|
|
95
|
+
private lineParts;
|
|
96
|
+
private lineLength;
|
|
97
|
+
private dataLines;
|
|
98
|
+
private event;
|
|
99
|
+
private recordId;
|
|
100
|
+
private recordLength;
|
|
101
|
+
private lastId;
|
|
102
|
+
private atStart;
|
|
103
|
+
private afterCarriageReturn;
|
|
104
|
+
private overflowed;
|
|
105
|
+
private readonly maxBufferChars;
|
|
106
|
+
/**
|
|
107
|
+
* Builds a parser that holds at most `maxBufferChars` of a record it has not seen the end of.
|
|
108
|
+
*
|
|
109
|
+
* @param maxBufferChars - bounds the unterminated record this parser holds, in UTF-16 code
|
|
110
|
+
* units: a whole number of 1 or more. A consumer whose frames are known small may tighten
|
|
111
|
+
* it; the shared readers take the default. A cap that is not a number of characters would
|
|
112
|
+
* bound nothing (`NaN` and `Infinity` are exceeded by no length) or refuse every push (zero
|
|
113
|
+
* and below), so it is refused here.
|
|
114
|
+
* @default MAX_SSE_BUFFER_CHARS
|
|
115
|
+
* @throws `MountayaConfigurationError` for a cap that is not a whole number of 1 or more.
|
|
116
|
+
*/
|
|
117
|
+
constructor(maxBufferChars?: number);
|
|
118
|
+
/**
|
|
119
|
+
* lastEventId is the stream's last event ID, as the HTML standard defines it: the value of the
|
|
120
|
+
* latest `id:` in a record the parser has seen the end of, whatever else that record carried.
|
|
121
|
+
*
|
|
122
|
+
* It is the position a reconnect resumes from, sent as `Last-Event-ID`. It carries over a
|
|
123
|
+
* record that names no `id:`, an empty `id:` clears it to the empty string, and an `id:` holding
|
|
124
|
+
* U+0000 is ignored. An `id:` in a record still unterminated has not moved it: a reconnect
|
|
125
|
+
* discards that record, so resuming past it would skip it.
|
|
126
|
+
*
|
|
127
|
+
* It is `undefined` until the stream names a position, where the standard starts from the empty
|
|
128
|
+
* string, so a stream that names none reads apart from one that clears it. A caller resuming
|
|
129
|
+
* from a position an earlier connection named keeps that position through the first and drops
|
|
130
|
+
* it at the second.
|
|
131
|
+
*/
|
|
132
|
+
get lastEventId(): string | undefined;
|
|
133
|
+
/**
|
|
134
|
+
* push feeds the next decoded text chunk and returns every frame the records it completes
|
|
135
|
+
* dispatch, in stream order. What it leaves unterminated stays held for the next push, unless it
|
|
136
|
+
* passes the cap: then push throws {@link SseBufferOverflowError} instead of returning, dropping
|
|
137
|
+
* the frames the same chunk completed. The stream is being torn down with them, and a caller
|
|
138
|
+
* that resumes re-reads from the position it last recorded.
|
|
139
|
+
*/
|
|
140
|
+
push(chunk: string): SseFrame[];
|
|
141
|
+
/**
|
|
142
|
+
* completeLine joins the line held from earlier pushes with its last piece, `tail`, and starts
|
|
143
|
+
* the next line empty.
|
|
144
|
+
*/
|
|
145
|
+
private completeLine;
|
|
146
|
+
/**
|
|
147
|
+
* readLine applies one complete line to the current record, and returns the frame the record
|
|
148
|
+
* dispatches when the line is the empty one that ends it.
|
|
149
|
+
*
|
|
150
|
+
* A field line is split at its first colon, and one space after the colon is dropped: only the
|
|
151
|
+
* first space is the standard's separator, and a second one is part of the value. A line with no
|
|
152
|
+
* colon is its whole self as the field name, with an empty value.
|
|
153
|
+
*/
|
|
154
|
+
private readLine;
|
|
155
|
+
/**
|
|
156
|
+
* endRecord closes the current record: an `id:` it named becomes the stream's last event ID,
|
|
157
|
+
* and it returns the frame the record dispatches, or null for a record with no data.
|
|
158
|
+
*/
|
|
159
|
+
private endRecord;
|
|
160
|
+
/** startRecord empties the record being read, so the next line begins the next one. */
|
|
161
|
+
private startRecord;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** AgentEvent is one decoded frame off a run's event stream. */
|
|
165
|
+
type AgentEvent = AgentRunEvent;
|
|
166
|
+
/** AgentInput is one input signal sent into a running run. */
|
|
167
|
+
type AgentInput = AgentRunInputParams;
|
|
168
|
+
/**
|
|
169
|
+
* agentTerminalEventTypes are the event types that end a run: once one is seen the
|
|
170
|
+
* stream is finished and the read stops rather than waiting on the server to hang up.
|
|
171
|
+
* `done` is the sole terminal: an ended run always finishes with one.
|
|
172
|
+
*/
|
|
173
|
+
declare const agentTerminalEventTypes: ReadonlySet<AgentEvent["type"]>;
|
|
174
|
+
/**
|
|
175
|
+
* AgentStreamFrame is one decoded frame paired with its stream position id. A reader
|
|
176
|
+
* records the id of the last frame it consumed, so a reconnect passes it back as
|
|
177
|
+
* `Last-Event-ID` and resumes after it. `id` is the record's own `id:` field, and absent where
|
|
178
|
+
* the record names none: on the API's stream a `message-delta` carries none, since a delta
|
|
179
|
+
* is live and never replayed, and every other frame is numbered. A reader that holds no id
|
|
180
|
+
* resumes as a full replay.
|
|
181
|
+
*/
|
|
182
|
+
interface AgentStreamFrame {
|
|
183
|
+
/** The decoded event, discriminated on `type`. */
|
|
184
|
+
event: AgentEvent;
|
|
185
|
+
/** The record's own `id:`, the position a resume passes back; absent where it names none. */
|
|
186
|
+
id?: string | undefined;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* AgentStreamCloseReason distinguishes how a run's stream finished without an error:
|
|
190
|
+
* `terminal` means a terminal event was seen, so the run is over; `reconnect` means the API ended
|
|
191
|
+
* the stream and asks for a reopen at once. It says so with a `reconnect` event, when the
|
|
192
|
+
* credential the stream was opened with ends (the session token expires or the key is revoked)
|
|
193
|
+
* and when it cannot keep delivering the stream (`interrupted`: the reader fell behind or the
|
|
194
|
+
* subscription was lost), and a close at or after the token's expiry says the same. None is a
|
|
195
|
+
* failure: the reopen goes at once with the token the client holds then, a new one where the
|
|
196
|
+
* credential ended and the one already held where the stream was interrupted, and the last event
|
|
197
|
+
* id. `ended` means the body closed with no terminal, which mid-run is the server hanging up
|
|
198
|
+
* between turns: a candidate for a resume rather than the end of the run.
|
|
199
|
+
*/
|
|
200
|
+
type AgentStreamCloseReason = "terminal" | "reconnect" | "ended";
|
|
201
|
+
/**
|
|
202
|
+
* AgentStreamHandlers are the callbacks a subscriber wires into a run's event stream. The
|
|
203
|
+
* transport drives them as it reads: `onFrame` per decoded frame, `onSkippedFrame` per frame it
|
|
204
|
+
* skips, `onLastEventId` when the run's position moves without a frame to carry it, then exactly
|
|
205
|
+
* one of `onClose` (finished cleanly) or `onError` (it failed). An abort is the subscriber's own
|
|
206
|
+
* teardown and fires neither.
|
|
207
|
+
*/
|
|
208
|
+
interface AgentStreamHandlers {
|
|
209
|
+
/** onClose is told once the stream finished with no error, and why. */
|
|
210
|
+
onClose: (reason: AgentStreamCloseReason) => void;
|
|
211
|
+
/**
|
|
212
|
+
* onError receives the failure once. It is an error of the client's family, except a fault the
|
|
213
|
+
* subscriber's own `onFrame`, `onSkippedFrame` or `onLastEventId` threw, which arrives as it was
|
|
214
|
+
* thrown: an open the API refused raises the class a request answered the same way raises, and
|
|
215
|
+
* a connection that failed or broke off raises a `MountayaConnectionError` naming
|
|
216
|
+
* `getAgentRunEvents`.
|
|
217
|
+
*/
|
|
218
|
+
onError: (error: unknown) => void;
|
|
219
|
+
/**
|
|
220
|
+
* onFrame receives one decoded frame. It is awaited, so returning a promise backpressures
|
|
221
|
+
* the read: the next frame is pulled only once the current one is handled, which throttles
|
|
222
|
+
* the stream to tool dispatch rather than letting frames pile up ahead of their effects.
|
|
223
|
+
*/
|
|
224
|
+
onFrame: (frame: AgentStreamFrame) => void | Promise<void>;
|
|
225
|
+
/**
|
|
226
|
+
* onLastEventId receives the run's stream position when it moves to one no frame carries: a
|
|
227
|
+
* record that names an `id:` and no event, or an empty `id:`, which clears the position and
|
|
228
|
+
* arrives as `undefined`. A subscriber keeping a resume cursor records it as it records a
|
|
229
|
+
* frame's id, so a reopen resumes from the last position the stream named.
|
|
230
|
+
*
|
|
231
|
+
* A frame {@link decodeAgentFrame} skips (a kind this build does not know, or a payload that is
|
|
232
|
+
* not an object) is neither handed to onFrame nor reported here, whatever id it carries, so its
|
|
233
|
+
* position reaches a cursor only once a later frame or record names one. A reopen before then
|
|
234
|
+
* replays it, and it is skipped again.
|
|
235
|
+
*/
|
|
236
|
+
onLastEventId?: ((lastEventId: string | undefined) => void) | undefined;
|
|
237
|
+
/**
|
|
238
|
+
* onSkippedFrame is told of each frame the stream delivers that {@link decodeAgentFrame} skips:
|
|
239
|
+
* one naming an event this build does not know, or carrying data it cannot read. Nothing of it
|
|
240
|
+
* is handed over, and it moves no position, but it is a frame the stream delivered, so a reader
|
|
241
|
+
* counting what an open delivered, as a reconnect policy does to tell an open that made progress
|
|
242
|
+
* from one that did not, counts it. A `reconnect`, which is the transport's own, is not one.
|
|
243
|
+
*/
|
|
244
|
+
onSkippedFrame?: (() => void) | undefined;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* AgentWriteOptions tune one write into a run: starting it, or delivering input into it.
|
|
248
|
+
*/
|
|
249
|
+
interface AgentWriteOptions {
|
|
250
|
+
/**
|
|
251
|
+
* The `Idempotency-Key` the write sends on every attempt. Left out, the client draws one for
|
|
252
|
+
* the call. A caller that asks again itself after a failure passes the key it drew for the first
|
|
253
|
+
* ask, so a start whose answer was lost is answered with the run it started rather than a second
|
|
254
|
+
* run.
|
|
255
|
+
*/
|
|
256
|
+
readonly idempotencyKey?: string | undefined;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* AgentSubscribeOptions tune one open. `lastEventId` resumes the durable history after a
|
|
260
|
+
* position (the server replays past it before resuming the live feed); `signal` tears the
|
|
261
|
+
* open stream down from outside, exactly as the returned unsubscribe does.
|
|
262
|
+
*/
|
|
263
|
+
interface AgentSubscribeOptions {
|
|
264
|
+
/** The position to resume after, sent as `Last-Event-ID`; left out, the run replays whole. */
|
|
265
|
+
lastEventId?: string | undefined;
|
|
266
|
+
/** Closes the stream once it aborts, as the unsubscribe `subscribe` returns does. */
|
|
267
|
+
signal?: AbortSignal | undefined;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* AgentTransport is the seam an agent session drives: start a run, deliver input into it,
|
|
271
|
+
* and read its events frame by frame.
|
|
272
|
+
*
|
|
273
|
+
* A session takes this interface rather than a client, so an offline or scripted
|
|
274
|
+
* implementation is a plain object. {@link createAgentTransport} is the live one, which gives up
|
|
275
|
+
* an open that never answers and a stream gone silent as connection errors. A transport of your
|
|
276
|
+
* own bounds both waits the same way: the engine waits as long as `subscribe` says nothing. It
|
|
277
|
+
* reports `onClose("terminal")` only after it has handed over a `done`.
|
|
278
|
+
*/
|
|
279
|
+
interface AgentTransport {
|
|
280
|
+
/**
|
|
281
|
+
* sendInput delivers one input signal into a running run, resolving once the API took it.
|
|
282
|
+
*
|
|
283
|
+
* @throws the `MountayaApiError` class the API's refusal maps to, or a
|
|
284
|
+
* `MountayaConnectionError` when no answer arrived; in the live transport, a
|
|
285
|
+
* `MountayaConfigurationError` before anything is sent for a routing update naming a server
|
|
286
|
+
* tool under a publishable key.
|
|
287
|
+
*/
|
|
288
|
+
sendInput: (runId: string, input: AgentInput, options?: AgentWriteOptions) => Promise<void>;
|
|
289
|
+
/**
|
|
290
|
+
* startRun starts a run and answers its id, which `sendInput` and `subscribe` take.
|
|
291
|
+
*
|
|
292
|
+
* @throws the `MountayaApiError` class the API's refusal maps to, including a 202 that names no
|
|
293
|
+
* run, or a `MountayaConnectionError` when no answer arrived; in the live transport, a
|
|
294
|
+
* `MountayaConfigurationError` before anything is sent for a start naming a server tool under
|
|
295
|
+
* a publishable key.
|
|
296
|
+
*/
|
|
297
|
+
startRun: (params: AgentRunCreateParams, options?: AgentWriteOptions) => Promise<{
|
|
298
|
+
runId: string;
|
|
299
|
+
}>;
|
|
300
|
+
/**
|
|
301
|
+
* subscribe opens a run's event stream and returns the function that closes it. Every failure
|
|
302
|
+
* reaches `handlers.onError`; the call itself never throws.
|
|
303
|
+
*/
|
|
304
|
+
subscribe: (runId: string, handlers: AgentStreamHandlers, options?: AgentSubscribeOptions) => () => void;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* decodeAgentFrame turns one raw SSE frame into a typed event, or null to skip it.
|
|
308
|
+
*
|
|
309
|
+
* The `event:` line is the discriminant and the `data:` line is the payload without it, so
|
|
310
|
+
* the two are combined and the event line wins over any stray `type` in the body. A frame
|
|
311
|
+
* naming a type this build does not know is skipped rather than fatal, which is what makes
|
|
312
|
+
* a newer server emitting a new event a compatible change. The type is looked up among the
|
|
313
|
+
* table's own keys, so a name every object inherits (`constructor`, `toString`) is one this
|
|
314
|
+
* build does not know.
|
|
315
|
+
*
|
|
316
|
+
* No date mask here: nothing in this union carries a timestamp, so the contract generates no
|
|
317
|
+
* event masks for it, unlike the collaboration stream's.
|
|
318
|
+
*
|
|
319
|
+
* The cast is the decision this module is built on: the contract generates the union, so a
|
|
320
|
+
* frame is TYPED by it rather than re-validated against a hand-written transcription. What
|
|
321
|
+
* is checked at runtime is the one field the wire and the type disagree about, the
|
|
322
|
+
* discriminant, which arrives on its own line, and nothing below it. A `tool-call`'s input
|
|
323
|
+
* is `unknown` either way and is validated by the tool bus against the tool's own schema.
|
|
324
|
+
*/
|
|
325
|
+
declare const decodeAgentFrame: (frame: SseFrame) => AgentEvent | null;
|
|
326
|
+
/**
|
|
327
|
+
* createAgentTransport binds an agent run's endpoints to one client's transport.
|
|
328
|
+
*
|
|
329
|
+
* The two writes go through the generated operations, so the path, the method and the
|
|
330
|
+
* error taxonomy are the contract's. The read cannot: a run's event stream is open for as
|
|
331
|
+
* long as the run lasts, and every generated reader either buffers the whole body or arms a
|
|
332
|
+
* per-request timeout across it (see `MountayaTransport.streamSeam` for why). It uses `fetch`
|
|
333
|
+
* rather than `EventSource` because the stream must carry the transport's credential and set
|
|
334
|
+
* `Last-Event-ID` on a reconnect, and `EventSource` supports neither. An open the API refuses
|
|
335
|
+
* raises the class a request answered the same way raises, read off the refusal's body.
|
|
336
|
+
*
|
|
337
|
+
* A start or a routing update whose `server_tools` names anything is refused before it is sent
|
|
338
|
+
* under a publishable key, under which a run calls no server tool. An open waits for a session
|
|
339
|
+
* token the credential's source reports on its way, as a request does. The `reconnect` event the
|
|
340
|
+
* API ends a stream with, when its credential ends or when it cannot keep delivering it
|
|
341
|
+
* (`interrupted`), is never handed to `onFrame`: it ends the read and closes it as `reconnect`,
|
|
342
|
+
* whatever reason it names, and so does a close at or after the expiry of the token the open
|
|
343
|
+
* carried, where the credential's source reports one. A frame it skips is told to
|
|
344
|
+
* `onSkippedFrame`.
|
|
345
|
+
*/
|
|
346
|
+
declare const createAgentTransport: (transport: MountayaTransport) => AgentTransport;
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* CollabEvent is one decoded frame off a collaboration stream: the contract's union,
|
|
350
|
+
* re-exported under the name the reading code uses.
|
|
351
|
+
*/
|
|
352
|
+
type CollabEvent = CollabStreamEvent;
|
|
353
|
+
/**
|
|
354
|
+
* CollabSignal is one signal a member publishes upstream: a selection, a cursor move, a
|
|
355
|
+
* lock acquire or release, a draft update or clear.
|
|
356
|
+
*
|
|
357
|
+
* The inbound and outbound vocabularies are deliberately NOT one type. The server stamps
|
|
358
|
+
* `user_id` on every frame it republishes, and the lock pair disagrees on the discriminant
|
|
359
|
+
* itself (`lock.acquire` goes up, `lock.acquired` comes back), so a shared schema would
|
|
360
|
+
* have to be wrong about one of them.
|
|
361
|
+
*/
|
|
362
|
+
type CollabSignal = CollabSignalParams;
|
|
363
|
+
/**
|
|
364
|
+
* CollabDurableChange is the subset of frames reporting a COMMITTED change to a resource,
|
|
365
|
+
* as opposed to the live-only presence, cursor, lock and draft traffic. Its members are the
|
|
366
|
+
* frames carrying an `action`, which is what selects them here: a new change family joins by
|
|
367
|
+
* carrying one rather than by being listed.
|
|
368
|
+
*/
|
|
369
|
+
type CollabDurableChange = Extract<CollabEvent, {
|
|
370
|
+
action: CollabChangeAction;
|
|
371
|
+
}>;
|
|
372
|
+
/**
|
|
373
|
+
* CollabStreamResource is the resource whose editor stream is being read: the collection
|
|
374
|
+
* segment of its path and its id.
|
|
375
|
+
*
|
|
376
|
+
* It is the wire's own spelling rather than a domain handle (`itineraries` and `zones` are
|
|
377
|
+
* path segments here, not model names), which is why this package declares it instead of
|
|
378
|
+
* naming one from a package it must not depend on. `@mountaya/elements`' `CollabResource`
|
|
379
|
+
* is structurally identical and assignable to it, and a twin test pins the two.
|
|
380
|
+
*/
|
|
381
|
+
interface CollabStreamResource {
|
|
382
|
+
/** The resource's id, as its path carries it. */
|
|
383
|
+
readonly id: string;
|
|
384
|
+
/** The collection segment of the resource's path. */
|
|
385
|
+
readonly kind: "itineraries" | "zones";
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* CollabStreamFrame is one decoded frame paired with its stream position id. A reader
|
|
389
|
+
* records the id of the last frame it consumed so a reconnect passes it back as
|
|
390
|
+
* `Last-Event-ID` and resumes after it. The id is optional because not every producer
|
|
391
|
+
* numbers its frames, in which case a resume falls back to a full replay.
|
|
392
|
+
*/
|
|
393
|
+
interface CollabStreamFrame {
|
|
394
|
+
/** The decoded frame, discriminated on `type`. */
|
|
395
|
+
event: CollabEvent;
|
|
396
|
+
/** The record's own `id:`, the position a resume passes back; absent where it names none. */
|
|
397
|
+
id?: string | undefined;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* CollabStreamErrorOptions classify a stream failure for the reader that has to decide
|
|
401
|
+
* whether to reconnect.
|
|
402
|
+
*/
|
|
403
|
+
interface CollabStreamErrorOptions {
|
|
404
|
+
/**
|
|
405
|
+
* Whether reopening cannot help: a credential refused before the open, a runtime whose fetch
|
|
406
|
+
* streams no body, a 4xx other than 429 on the open, a producer that never ends a frame, and a
|
|
407
|
+
* fault the subscriber's own `onFrame` or `onLastEventId` threw, which a reopen would hand the
|
|
408
|
+
* same frame again. `false` for a failure a reopen can get past: a dropped connection, a
|
|
409
|
+
* stream gone silent, a 429 and a 5xx.
|
|
410
|
+
*/
|
|
411
|
+
terminal?: boolean | undefined;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* CollabStreamCloseReason says how a collaboration stream finished without an error:
|
|
415
|
+
* `reconnect` means the API ended it and asks for a reopen at once. It says so with a `reconnect`
|
|
416
|
+
* event, when the credential the stream was opened with ends (the session token expires or the
|
|
417
|
+
* key is revoked) and when it cannot keep delivering the stream (`interrupted`: the reader fell
|
|
418
|
+
* behind or the subscription was lost), and a close at or after the token's expiry says the same.
|
|
419
|
+
* None is a failure: the reopen goes at once with the token the client holds then, a new one
|
|
420
|
+
* where the credential ended and the one already held where the stream was interrupted, and the
|
|
421
|
+
* last event id. `ended` means the body closed on its own, a drop like any other.
|
|
422
|
+
*/
|
|
423
|
+
type CollabStreamCloseReason = "reconnect" | "ended";
|
|
424
|
+
/**
|
|
425
|
+
* CollabStreamHandlers are the callbacks a subscriber wires into a collaboration stream.
|
|
426
|
+
* The transport drives them as it reads: `onOpen` once the API has answered the open, `onFrame`
|
|
427
|
+
* per decoded frame, `onSkippedFrame` per frame it skips, `onLastEventId` when the stream's
|
|
428
|
+
* position moves without a frame to carry it, then exactly one of `onClose` (the body finished)
|
|
429
|
+
* or `onError` (it failed). An abort is the subscriber's own teardown and fires none of them
|
|
430
|
+
* after it.
|
|
431
|
+
*/
|
|
432
|
+
interface CollabStreamHandlers {
|
|
433
|
+
/** onClose is told once the stream's body finished with no error, and why. */
|
|
434
|
+
onClose: (reason: CollabStreamCloseReason) => void;
|
|
435
|
+
/**
|
|
436
|
+
* onError receives the failure once, classified. It is an error of the client's family, except
|
|
437
|
+
* a fault the subscriber's own `onOpen`, `onFrame`, `onSkippedFrame` or `onLastEventId` threw,
|
|
438
|
+
* which arrives as it was thrown and classified terminal: an open the API refused raises the
|
|
439
|
+
* class a request answered the same way raises, and a connection that failed or broke off raises
|
|
440
|
+
* a `MountayaConnectionError` naming the stream's operation.
|
|
441
|
+
*/
|
|
442
|
+
onError: (error: unknown, options?: CollabStreamErrorOptions) => void;
|
|
443
|
+
/** onFrame receives each decoded frame, in the order the stream wrote them. */
|
|
444
|
+
onFrame: (frame: CollabStreamFrame) => void;
|
|
445
|
+
/**
|
|
446
|
+
* onLastEventId receives the stream position when it moves to one no frame carries: a record
|
|
447
|
+
* that names an `id:` and no event, or an empty `id:`, which clears the position and arrives as
|
|
448
|
+
* `undefined`. A subscriber keeping a resume cursor records it, so a reopen resumes from the
|
|
449
|
+
* last position the stream named rather than replaying what it has read.
|
|
450
|
+
*
|
|
451
|
+
* A frame {@link decodeCollabFrame} skips (a kind this build does not know, or a payload that is
|
|
452
|
+
* not an object) is neither handed to onFrame nor reported here, whatever id it carries, so its
|
|
453
|
+
* position reaches a cursor only once a later frame or record names one. A reopen before then
|
|
454
|
+
* replays it, and it is skipped again.
|
|
455
|
+
*/
|
|
456
|
+
onLastEventId?: ((lastEventId: string | undefined) => void) | undefined;
|
|
457
|
+
/**
|
|
458
|
+
* onOpen is told once the API has answered the open, before any frame of it, with the user the
|
|
459
|
+
* stream is read as: the user the session token it was opened with names, `userId`, undefined
|
|
460
|
+
* where the credential's source does not say or the token is held as a string or a function,
|
|
461
|
+
* whose user the client is not told. A reader judging its own member's `presence.join` reads
|
|
462
|
+
* that user first, and a user it is told otherwise only where it is undefined.
|
|
463
|
+
*/
|
|
464
|
+
onOpen?: ((open: {
|
|
465
|
+
readonly userId: string | undefined;
|
|
466
|
+
}) => void) | undefined;
|
|
467
|
+
/**
|
|
468
|
+
* onSkippedFrame is told of each frame the stream delivers that {@link decodeCollabFrame} skips:
|
|
469
|
+
* one naming a kind this build does not know, or carrying data it cannot read. Nothing of it is
|
|
470
|
+
* handed over, and it moves no position, but it is a frame the stream delivered, so a reader
|
|
471
|
+
* telling an open that made progress from one that did not counts it as one past those every
|
|
472
|
+
* open begins with. A `reconnect`, which is the transport's own, is not one.
|
|
473
|
+
*/
|
|
474
|
+
onSkippedFrame?: (() => void) | undefined;
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* CollabSubscribeOptions tune one open. `lastEventId` resumes the durable history after a
|
|
478
|
+
* position; `signal` tears the open stream down from outside, exactly as the returned
|
|
479
|
+
* unsubscribe does.
|
|
480
|
+
*/
|
|
481
|
+
interface CollabSubscribeOptions {
|
|
482
|
+
/** The position to resume after, sent as `Last-Event-ID`; left out, the stream replays whole. */
|
|
483
|
+
lastEventId?: string | undefined;
|
|
484
|
+
/** Closes the stream once it aborts, as the unsubscribe a subscribe returns does. */
|
|
485
|
+
signal?: AbortSignal | undefined;
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* CollabSignalResult is how a published signal settled. A lock contention is a RESULT
|
|
489
|
+
* rather than a throw: the caller asked whether it may edit an entity and the answer is
|
|
490
|
+
* "no, that member is", which is an outcome of the question rather than a failure to ask
|
|
491
|
+
* it. Every other non-ok status throws from the client's own error family, as the generated
|
|
492
|
+
* operation raises it. A success throws a `MountayaConnectionError` when its body breaks off
|
|
493
|
+
* before it is complete, where the signal was answered and the answer lost, and the base
|
|
494
|
+
* `MountayaApiError` when its body is not JSON, where something on the network path may have
|
|
495
|
+
* answered in the API's place.
|
|
496
|
+
*/
|
|
497
|
+
type CollabSignalResult = {
|
|
498
|
+
ok: true;
|
|
499
|
+
} | {
|
|
500
|
+
holderId?: string | undefined;
|
|
501
|
+
ok: false;
|
|
502
|
+
};
|
|
503
|
+
/**
|
|
504
|
+
* CollabTransport is the seam a collaboration session drives: publish one signal, and open
|
|
505
|
+
* either the editor stream for one resource or the organization's event stream.
|
|
506
|
+
*
|
|
507
|
+
* A session takes this interface rather than a client, so an offline or scripted
|
|
508
|
+
* implementation is a plain object. {@link createCollabTransport} is the live one, which gives
|
|
509
|
+
* up an open that never answers and a stream gone silent as connection errors. A transport of
|
|
510
|
+
* your own bounds both waits the same way: the engine waits as long as a stream says nothing.
|
|
511
|
+
*/
|
|
512
|
+
interface CollabTransport {
|
|
513
|
+
/**
|
|
514
|
+
* sendSignal publishes one signal for a resource, and answers how it settled: a lock another
|
|
515
|
+
* member holds is a result naming them, never a throw.
|
|
516
|
+
*
|
|
517
|
+
* @throws the client's own error family for every other refusal: see {@link CollabSignalResult}.
|
|
518
|
+
*/
|
|
519
|
+
sendSignal: (resource: CollabStreamResource, signal: CollabSignal) => Promise<CollabSignalResult>;
|
|
520
|
+
/**
|
|
521
|
+
* subscribeEditor opens the editor stream of one resource and returns the function that closes
|
|
522
|
+
* it. Every failure reaches `handlers.onError`, classified; the call itself never throws.
|
|
523
|
+
*/
|
|
524
|
+
subscribeEditor: (resource: CollabStreamResource, handlers: CollabStreamHandlers, options?: CollabSubscribeOptions) => () => void;
|
|
525
|
+
/**
|
|
526
|
+
* subscribeList opens an organization's event stream, the changes to its itineraries, zones and
|
|
527
|
+
* collections, and returns the function that closes it. Every failure reaches `handlers.onError`, classified; the call itself never throws.
|
|
528
|
+
*/
|
|
529
|
+
subscribeList: (organizationId: string, handlers: CollabStreamHandlers, options?: CollabSubscribeOptions) => () => void;
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* decodeCollabFrame turns one raw SSE frame into a typed event, or null to skip it.
|
|
533
|
+
*
|
|
534
|
+
* The `event:` line is the discriminant and the `data:` line is the payload without it, so
|
|
535
|
+
* the two are combined and the event line wins over any stray `type` in the body. A frame
|
|
536
|
+
* naming a kind this build does not know is skipped rather than fatal: that is what makes
|
|
537
|
+
* a newer API release adding a frame a compatible change rather than a broken stream. The kind
|
|
538
|
+
* is looked up among the table's own keys, so a name every object inherits (`constructor`,
|
|
539
|
+
* `toString`) is one this build does not know.
|
|
540
|
+
*
|
|
541
|
+
* The payload decodes the instants its own event declares, by the mask the contract generates
|
|
542
|
+
* for that event, as every response decodes its operation's. Without it `connected_at` and
|
|
543
|
+
* `occurred_at` would be ISO strings behind a generated type that says `Date`, which is a
|
|
544
|
+
* `getTime is not a function` waiting for the first reader. A member its event types required and
|
|
545
|
+
* nullable, a cursor's `position` or a selection's `selection`, reads null where the frame leaves
|
|
546
|
+
* it out, as it does in an answer. Nothing else is decoded: a draft is an open object a peer is
|
|
547
|
+
* typing, so a timestamp in it stays the string the peer wrote.
|
|
548
|
+
*
|
|
549
|
+
* The cast is the decision this module is built on: the contract generates the union, so a
|
|
550
|
+
* frame is TYPED by it rather than re-validated against a hand-written transcription of it.
|
|
551
|
+
* What is checked at runtime is the one field the wire and the type disagree about (the
|
|
552
|
+
* discriminant, which arrives on a separate line) and nothing below it.
|
|
553
|
+
*/
|
|
554
|
+
declare const decodeCollabFrame: (frame: SseFrame) => CollabEvent | null;
|
|
555
|
+
/**
|
|
556
|
+
* createCollabTransport binds the collaboration streams to one client's transport.
|
|
557
|
+
*
|
|
558
|
+
* It reads the three feeds over `fetch` rather than `EventSource` because they must ride
|
|
559
|
+
* the transport's credential (a cookie session, a publishable key with a session token)
|
|
560
|
+
* and set `Last-Event-ID` on a reconnect, and `EventSource` supports neither. A
|
|
561
|
+
* collaboration stream has no terminal event, so a read finishes only when the body closes,
|
|
562
|
+
* a fault occurs, or the returned unsubscribe aborts it.
|
|
563
|
+
*
|
|
564
|
+
* An open waits for a session token the credential's source reports on its way, as a request
|
|
565
|
+
* does. The `reconnect` event the API ends a stream with, when its credential ends or when it
|
|
566
|
+
* cannot keep delivering it (`interrupted`), is never handed to `onFrame`: it ends the read and
|
|
567
|
+
* closes it as `reconnect`, whatever reason it names, and so does a close at or after the expiry
|
|
568
|
+
* of the token the open carried, where the credential's source reports one. An open the API
|
|
569
|
+
* answers tells `onOpen` the user that token names, and a frame it skips is told to
|
|
570
|
+
* `onSkippedFrame`.
|
|
571
|
+
*/
|
|
572
|
+
declare const createCollabTransport: (transport: MountayaTransport) => CollabTransport;
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* FrameHandler receives one framed record as readEventStream decodes it. It is
|
|
576
|
+
* awaited, so returning a promise backpressures the read: the next chunk is
|
|
577
|
+
* pulled only once the current frame is handled. Returning (or resolving to)
|
|
578
|
+
* `true` asks the reader to stop early: the caller has seen its own end-of-stream
|
|
579
|
+
* marker and need not wait for the body to close.
|
|
580
|
+
*/
|
|
581
|
+
type FrameHandler = (frame: SseFrame) => boolean | void | Promise<boolean> | Promise<void>;
|
|
582
|
+
/**
|
|
583
|
+
* EventStreamOutcome reports why readEventStream returned: `stopped` when a frame
|
|
584
|
+
* handler asked to stop early (it recognized the caller's terminator), `ended`
|
|
585
|
+
* when the body closed on its own with no such request.
|
|
586
|
+
*/
|
|
587
|
+
type EventStreamOutcome = "stopped" | "ended";
|
|
588
|
+
/**
|
|
589
|
+
* readEventStream reads a `text/event-stream` body to its end. It pulls each chunk from the body's
|
|
590
|
+
* reader, decodes it as UTF-8 across reads (so a character two reads split arrives whole), frames
|
|
591
|
+
* it with {@link SseFrameParser}, and hands every frame to `onFrame`. It knows nothing of the
|
|
592
|
+
* payload's shape or vocabulary: the caller decodes and validates each frame's data. Awaiting
|
|
593
|
+
* `onFrame` lets a slow handler backpressure the read, and a handler that returns `true` stops
|
|
594
|
+
* the read at once rather than waiting on the producer to hang up. It returns `stopped` when a
|
|
595
|
+
* handler stopped the read and `ended` when the body closed first.
|
|
596
|
+
*
|
|
597
|
+
* `onLastEventId`, where the caller passes one, receives the stream's last event ID (see
|
|
598
|
+
* {@link SseFrameParser.lastEventId}) whenever it moves to a value the frames handed to `onFrame`
|
|
599
|
+
* do not carry: after a record that names an `id:` and carries no `data:`, which dispatches no
|
|
600
|
+
* frame, and after an empty `id:`, which clears the position to the empty string while the frame's
|
|
601
|
+
* own id reads empty either way. An empty `id:` is reported even before the stream names any other
|
|
602
|
+
* position, so a cursor carried over from an earlier connection is dropped, while a stream that
|
|
603
|
+
* names no `id:` at all reports nothing and leaves it where it was. A frame carries its record's
|
|
604
|
+
* id itself, so a caller keeping a resume cursor records both, and its cursor is the position a
|
|
605
|
+
* reconnect resumes from. The callback runs once the frames a read completed are handled, so the
|
|
606
|
+
* position it reports never passes a frame `onFrame` has not seen, and a read that stops or fails
|
|
607
|
+
* before then reports none.
|
|
608
|
+
*
|
|
609
|
+
* A producer that never terminates a record is the one fault the reader raises on its own: past
|
|
610
|
+
* the parser's cap the read throws `SseBufferOverflowError` rather than buffer without bound.
|
|
611
|
+
* That is deliberately not an outcome: `stopped` and `ended` both describe a stream that framed
|
|
612
|
+
* what it was sent, and a caller reconnecting on a clean close must not mistake a broken producer
|
|
613
|
+
* for one.
|
|
614
|
+
*
|
|
615
|
+
* The reader's lock is released however the read finishes, and the body is left as it is: after
|
|
616
|
+
* `stopped` or a throw, the connection behind it stays open until the caller, whose body it is,
|
|
617
|
+
* cancels it or aborts the request that opened it.
|
|
618
|
+
*/
|
|
619
|
+
declare const readEventStream: (body: ReadableStream<Uint8Array>, onFrame: FrameHandler, onLastEventId?: (lastEventId: string) => void) => Promise<EventStreamOutcome>;
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* OpenSseStreamOptions configure one open of a resumable Server-Sent Events read
|
|
623
|
+
* over fetch. The caller supplies the network seam (doFetch + applyAuth), the URL and
|
|
624
|
+
* the operation it opens, an optional resume position and external abort, and the
|
|
625
|
+
* feature-specific policy: how to decode a raw frame and what to do with each decoded
|
|
626
|
+
* event. The vocabulary stays with the caller (decode returns the caller's own event
|
|
627
|
+
* type), so this reader knows no feature's schema.
|
|
628
|
+
*/
|
|
629
|
+
interface OpenSseStreamOptions<TEvent> {
|
|
630
|
+
/**
|
|
631
|
+
* applyAuth folds the host's credential into the request's RequestInit.
|
|
632
|
+
*
|
|
633
|
+
* `method` reaches it because the published seam takes one, for the wording of a refusal.
|
|
634
|
+
*
|
|
635
|
+
* What the stream's operation declares does not reach it, because this reader knows the
|
|
636
|
+
* operation by name alone. An opener states it in the closure it passes, as the collaboration
|
|
637
|
+
* and agent transports do over `MountayaTransport.streamSeam`: the session token every stream
|
|
638
|
+
* requires, that it acts as the member that token names, and, by saying nothing, that no
|
|
639
|
+
* stream accepts a share token. The seam refuses a credential that cannot meet them locally,
|
|
640
|
+
* which for a share token is the only place the rule is named: the server answers one sent where
|
|
641
|
+
* it is not accepted with a 401, as if no credential had been sent. A refusal thrown here
|
|
642
|
+
* surfaces through onError like any other failed open.
|
|
643
|
+
*/
|
|
644
|
+
applyAuth: (init: RequestInit, method: string) => RequestInit;
|
|
645
|
+
/** decode turns one raw SSE frame into a typed event, or null to skip it. */
|
|
646
|
+
decode: (frame: SseFrame) => TEvent | null;
|
|
647
|
+
/** doFetch is the fetch implementation: the real one, or a test double. */
|
|
648
|
+
doFetch: typeof fetch;
|
|
649
|
+
/** lastEventId resumes the stream after a position via the Last-Event-ID header. */
|
|
650
|
+
lastEventId?: string | undefined;
|
|
651
|
+
/** onClose runs once when the body finishes, told why the read returned. */
|
|
652
|
+
onClose?: ((outcome: EventStreamOutcome) => void) | undefined;
|
|
653
|
+
/**
|
|
654
|
+
* onResponse is told of the open's answer once it arrives, a refusal's as well as a stream's,
|
|
655
|
+
* before the stream is read or the refusal classified. The openers of this entry point pass
|
|
656
|
+
* `MountayaTransport.streamSeam`'s `report`, so the client's own `onResponse` hears of every
|
|
657
|
+
* open. A fault it throws fails the open, as a fault of `applyAuth` does.
|
|
658
|
+
*/
|
|
659
|
+
onResponse?: ((response: Response) => void) | undefined;
|
|
660
|
+
/**
|
|
661
|
+
* onError runs once on a failure that is not the caller's own abort (an abort is a silent
|
|
662
|
+
* teardown and fires neither onClose nor onError).
|
|
663
|
+
*
|
|
664
|
+
* What it receives is an error of this package's family, except a fault the caller's own
|
|
665
|
+
* `applyAuth`, `decode`, `onFrame` or `onLastEventId` threw, which arrives as it was thrown:
|
|
666
|
+
* - an open the API refused raises what a request the API answered the same way raises (the
|
|
667
|
+
* class its status and first entry decide, carrying the entries, the `code` and the metadata),
|
|
668
|
+
* so a `402` is a `MountayaBillingError` and a `409` naming a held lock a `MountayaConflictError`. Its body
|
|
669
|
+
* is read under a bound on time and one on size, and past either the status alone decides the
|
|
670
|
+
* class;
|
|
671
|
+
* - a request that failed before a response arrived and a body that broke off mid-read raise a
|
|
672
|
+
* `MountayaConnectionError` carrying the runtime's failure as its `cause`, and an ok answer
|
|
673
|
+
* with no body raises one with no `cause`, since no runtime failure lies behind it;
|
|
674
|
+
* - an open with no response within {@link OpenSseStreamOptions.timeoutMs}, and a stream that
|
|
675
|
+
* delivered nothing for the stall window, raise a `MountayaConnectionError` that says which
|
|
676
|
+
* wait ran out, with no `cause`: the request is aborted and its body let go first;
|
|
677
|
+
* - a `timeoutMs` out of range raises a `MountayaConfigurationError` naming it, and nothing is
|
|
678
|
+
* opened;
|
|
679
|
+
* - a runtime whose fetch exposes no response body to stream raises a
|
|
680
|
+
* `MountayaConfigurationError`, since every reopen meets the same runtime;
|
|
681
|
+
* - a producer past the frame parser's cap raises a `SseBufferOverflowError` naming the
|
|
682
|
+
* operation, with the parser's own error as its `cause`;
|
|
683
|
+
* - a credential the transports' stream seam refuses in applyAuth, and a failure the fetch raised
|
|
684
|
+
* from inside the family, are already of the family and arrive as they were thrown.
|
|
685
|
+
*/
|
|
686
|
+
onError?: ((error: unknown) => void) | undefined;
|
|
687
|
+
/**
|
|
688
|
+
* onFrame receives each decoded event with its stream position id and returns
|
|
689
|
+
* whether to stop the read (a terminal event). Returning a promise backpressures
|
|
690
|
+
* the read: the next frame is pulled only once it settles.
|
|
691
|
+
*/
|
|
692
|
+
onFrame: (event: TEvent, id: string | undefined) => boolean | Promise<boolean>;
|
|
693
|
+
/**
|
|
694
|
+
* onLastEventId receives the stream's position when it moves to one no frame handed to
|
|
695
|
+
* onFrame carries: a record that names an `id:` and no `data:`, which dispatches no frame, or
|
|
696
|
+
* an empty `id:`, even one before the stream names any other position, which clears the
|
|
697
|
+
* position and arrives as `undefined`. A caller keeping a resume cursor records it as it records
|
|
698
|
+
* a frame's id, so a reopen from the cursor resumes from the last position the stream named. It
|
|
699
|
+
* runs only once the frames before it are handled (see `readEventStream`).
|
|
700
|
+
*
|
|
701
|
+
* A frame decode skips is neither handed to onFrame nor reported, whatever id it carries, so its
|
|
702
|
+
* position reaches a cursor only once a later frame or record names one. A reopen before then
|
|
703
|
+
* replays it, and decode skips it again.
|
|
704
|
+
*/
|
|
705
|
+
onLastEventId?: ((lastEventId: string | undefined) => void) | undefined;
|
|
706
|
+
/**
|
|
707
|
+
* operationId names the operation the stream opens, and every failure this open builds names it
|
|
708
|
+
* too: a refusal, a connection failure, and an answer with no body to read
|
|
709
|
+
* (`getAgentRunEvents: 403 FORBIDDEN …`), and so does the frame parser's overflow, which is
|
|
710
|
+
* handed on as a `SseBufferOverflowError` naming it. A failure handed on as it was thrown keeps
|
|
711
|
+
* its own message.
|
|
712
|
+
*/
|
|
713
|
+
operationId: string;
|
|
714
|
+
/** signal is an external abort that tears the open stream down like the returned close. */
|
|
715
|
+
signal?: AbortSignal | undefined;
|
|
716
|
+
/**
|
|
717
|
+
* timeoutMs bounds the open's wait for its response, in milliseconds: past it the request is
|
|
718
|
+
* aborted and onError receives a `MountayaConnectionError` naming the wait. A whole number from
|
|
719
|
+
* 1 to 2,147,483,647; anything else fails the open with a `MountayaConfigurationError` before a
|
|
720
|
+
* request is made. The agent and collaboration transports pass the client's own per-attempt
|
|
721
|
+
* `timeoutMs`, which `MountayaTransport.streamSeam` carries, so their opens wait what a request
|
|
722
|
+
* of the same client waits.
|
|
723
|
+
*
|
|
724
|
+
* Once the response arrives the wait that follows is the stream's silence, which is bounded on
|
|
725
|
+
* its own: a stream that delivers nothing, not even the heartbeat comment the API writes to
|
|
726
|
+
* every live stream, for the stall window is given up the same way.
|
|
727
|
+
*
|
|
728
|
+
* @default the stall window, 45,000 ms, so no wait on the network is unbounded.
|
|
729
|
+
*/
|
|
730
|
+
timeoutMs?: number | undefined;
|
|
731
|
+
/**
|
|
732
|
+
* waitForSession is called before each open's credential is applied, with the open's own
|
|
733
|
+
* signal, and the promise it answers is awaited, so a session token on its way is the one the
|
|
734
|
+
* open carries; `undefined` is nothing to wait for, and the open is made at once. The openers of
|
|
735
|
+
* this entry point pass `MountayaTransport.streamSeam`'s, which waits for a token the
|
|
736
|
+
* credential's source reports on its way, as a request does. What it rejects with reaches
|
|
737
|
+
* onError like any other failed open, and a close during the wait ends it silently.
|
|
738
|
+
*/
|
|
739
|
+
waitForSession?: ((signal: AbortSignal) => Promise<void> | undefined) | undefined;
|
|
740
|
+
/**
|
|
741
|
+
* url is the fully-qualified stream endpoint, or a function answering it. A function is called
|
|
742
|
+
* once, when the open is made, and what it throws reaches onError as a failed open: an opener
|
|
743
|
+
* whose resolver refuses a path (`StreamSeam.url` does, for one that leaves the API) passes the
|
|
744
|
+
* call rather than its answer.
|
|
745
|
+
*/
|
|
746
|
+
url: string | (() => string);
|
|
747
|
+
}
|
|
748
|
+
/**
|
|
749
|
+
* openSseStream opens one resumable `text/event-stream` read over fetch and returns
|
|
750
|
+
* a close function that aborts it. It is the scaffolding every SSE consumer repeats:
|
|
751
|
+
* it forwards an external abort into its own controller, sets Accept and an optional
|
|
752
|
+
* Last-Event-ID, makes the request, decodes each frame through the caller's decode,
|
|
753
|
+
* hands each decoded event to onFrame (whose return stops the read on a terminal), and
|
|
754
|
+
* settles through onClose or, on a failure, onError. The reader stays vocabulary-free:
|
|
755
|
+
* every feature keeps its own decode and schema and shares only this transport shell.
|
|
756
|
+
*
|
|
757
|
+
* The call itself never throws and always returns the close. Whatever fails, a `timeoutMs` out
|
|
758
|
+
* of range, a `lastEventId` no request header carries, a credential or a path its seam refuses,
|
|
759
|
+
* reaches onError. `onFrame`, `onLastEventId`, `onClose` and `onError` run after the close has
|
|
760
|
+
* been returned; `url`, where it is a function, `applyAuth` and `doFetch` are each called once
|
|
761
|
+
* inside the call. An open under a `signal` that has already aborted reads no credential and
|
|
762
|
+
* makes no request.
|
|
763
|
+
*
|
|
764
|
+
* The connection lives exactly as long as the read. Once the read ends for any reason (a
|
|
765
|
+
* terminal frame, the body closing, a failure) the request is aborted and its body let go, so a
|
|
766
|
+
* stopped read leaves no socket open behind it, and then onClose or onError runs, once. A caller's
|
|
767
|
+
* abort, through the returned close or `signal`, is the caller's own teardown: it fires neither,
|
|
768
|
+
* and no frame is handed over after it. The returned close is inert once the stream has settled,
|
|
769
|
+
* and the listener on `signal` is removed then, so a signal that outlives many streams holds none
|
|
770
|
+
* of them.
|
|
771
|
+
*
|
|
772
|
+
* No wait on the network is unbounded. The wait for the response ends at `timeoutMs`, and each
|
|
773
|
+
* wait for the next chunk of the body ends once the stream has delivered nothing, not even a
|
|
774
|
+
* heartbeat comment, for the stall window. Either ends the read as a `MountayaConnectionError`,
|
|
775
|
+
* the failure a caller reopens on, rather than as a read that never settles. The time a frame
|
|
776
|
+
* handler holds the read is the caller's, and never counts as the stream's silence.
|
|
777
|
+
*/
|
|
778
|
+
declare const openSseStream: <TEvent>(options: OpenSseStreamOptions<TEvent>) => (() => void);
|
|
779
|
+
|
|
780
|
+
export { MAX_SSE_BUFFER_CHARS, SseBufferOverflowError, SseFrameParser, agentTerminalEventTypes, createAgentTransport, createCollabTransport, decodeAgentFrame, decodeCollabFrame, openSseStream, readEventStream };
|
|
781
|
+
export type { AgentEvent, AgentInput, AgentStreamCloseReason, AgentStreamFrame, AgentStreamHandlers, AgentSubscribeOptions, AgentTransport, AgentWriteOptions, CollabDurableChange, CollabEvent, CollabSignal, CollabSignalResult, CollabStreamCloseReason, CollabStreamErrorOptions, CollabStreamFrame, CollabStreamHandlers, CollabStreamResource, CollabSubscribeOptions, CollabTransport, EventStreamOutcome, FrameHandler, OpenSseStreamOptions, SseFrame };
|