@enlitt/protocol 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.
Files changed (48) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/LICENSE +21 -0
  3. package/README.md +15 -2
  4. package/dist/admin.d.cts +353 -0
  5. package/dist/admin.d.ts +354 -0
  6. package/dist/admin.d.ts.map +1 -0
  7. package/dist/authentication.d.cts +8 -0
  8. package/dist/authentication.d.ts +9 -0
  9. package/dist/authentication.d.ts.map +1 -0
  10. package/dist/canonical-integer.d.cts +48 -0
  11. package/dist/canonical-integer.d.ts +49 -0
  12. package/dist/canonical-integer.d.ts.map +1 -0
  13. package/dist/compatibility.d.cts +23 -0
  14. package/dist/compatibility.d.ts +24 -0
  15. package/dist/compatibility.d.ts.map +1 -0
  16. package/dist/contract.d.cts +67 -0
  17. package/dist/contract.d.ts +68 -0
  18. package/dist/contract.d.ts.map +1 -0
  19. package/dist/dashboard.d.cts +1030 -0
  20. package/dist/dashboard.d.ts +1031 -0
  21. package/dist/dashboard.d.ts.map +1 -0
  22. package/dist/errors.d.cts +112 -0
  23. package/dist/errors.d.ts +113 -0
  24. package/dist/errors.d.ts.map +1 -0
  25. package/dist/idempotency.d.cts +79 -0
  26. package/dist/idempotency.d.ts +80 -0
  27. package/dist/idempotency.d.ts.map +1 -0
  28. package/dist/index.cjs +4806 -0
  29. package/dist/index.cjs.map +1 -0
  30. package/dist/index.d.cts +31 -0
  31. package/dist/index.d.ts +32 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +4624 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/premium-operations.d.cts +84 -0
  36. package/dist/premium-operations.d.ts +85 -0
  37. package/dist/premium-operations.d.ts.map +1 -0
  38. package/dist/response-stream.d.cts +246 -0
  39. package/dist/response-stream.d.ts +247 -0
  40. package/dist/response-stream.d.ts.map +1 -0
  41. package/dist/responses.d.cts +233 -0
  42. package/dist/responses.d.ts +234 -0
  43. package/dist/responses.d.ts.map +1 -0
  44. package/dist/usage.d.cts +11 -0
  45. package/dist/usage.d.ts +12 -0
  46. package/dist/usage.d.ts.map +1 -0
  47. package/generated/contract.json +482 -0
  48. package/package.json +51 -3
@@ -0,0 +1,84 @@
1
+ /**
2
+ * The content-free managed-premium wire contracts.
3
+ *
4
+ * These validators deliberately know nothing about admission, a user's
5
+ * capacity, or settlement arithmetic. They validate the only facts a
6
+ * business may send or receive at the two premium endpoints, leaving the
7
+ * captured maximum and the status-specific lowering rule to Finance.
8
+ */
9
+ import { type ParseOptions, type Readable } from "./compatibility.cjs";
10
+ import { type ResponseMetadata, type ResponseValidationResult } from "./responses.cjs";
11
+ type Result<T> = ResponseValidationResult<T>;
12
+ /** The deployed maximum in `FINANCE_POLICY.maxChargeNanoUsd`: $100 in NANOUSD. */
13
+ export declare const PREMIUM_OPERATION_MAX_CHARGE_NANO_USD = "100000000000";
14
+ /** The terminal report amount is bounded independently of the current policy. */
15
+ export declare const PREMIUM_TERMINAL_CHARGE_MAX_DIGITS = 30;
16
+ export declare const PREMIUM_OPERATION_BILLING_VALUES: readonly ["managed"];
17
+ export declare const PREMIUM_TERMINAL_STATUSES: readonly ["completed", "failed", "canceled"];
18
+ export interface PremiumProviderLabel {
19
+ readonly id: string;
20
+ readonly model: string;
21
+ }
22
+ export interface BeginPremiumOperationRequest {
23
+ readonly mode: "premium";
24
+ readonly max_charge_nano_usd: string;
25
+ readonly provider: PremiumProviderLabel;
26
+ readonly reporting_deadline_ms?: number;
27
+ readonly metadata?: ResponseMetadata;
28
+ }
29
+ export interface BeginPremiumOperationResponse {
30
+ readonly operation_id: string;
31
+ readonly billing: Readable<PremiumOperationBilling>;
32
+ readonly report_terminal: true;
33
+ readonly effective_max_charge_nano_usd: string;
34
+ readonly reserved_nano_usd: string;
35
+ readonly terminal_deadline: string;
36
+ }
37
+ export type PremiumOperationBilling = (typeof PREMIUM_OPERATION_BILLING_VALUES)[number];
38
+ export type PremiumTerminalStatus = (typeof PREMIUM_TERMINAL_STATUSES)[number];
39
+ export type PremiumErrorCategory = "provider" | "timeout" | "network" | "invalid_request" | "canceled" | "unknown";
40
+ export interface PremiumTerminalUsage {
41
+ readonly input_tokens?: number;
42
+ readonly output_tokens?: number;
43
+ readonly cached_input_tokens?: number;
44
+ readonly reasoning_tokens?: number;
45
+ }
46
+ export interface PremiumTerminalDelivery {
47
+ readonly output_started: boolean;
48
+ readonly output_completed: boolean;
49
+ }
50
+ export interface PremiumTerminalTiming {
51
+ readonly started_at: string;
52
+ readonly ended_at: string;
53
+ }
54
+ export interface ReportPremiumTerminalRequest {
55
+ readonly report_id: string;
56
+ readonly status: PremiumTerminalStatus;
57
+ readonly charge_nano_usd: string;
58
+ readonly usage?: PremiumTerminalUsage;
59
+ readonly finish_reason?: string;
60
+ readonly provider_response_id?: string;
61
+ readonly delivery?: PremiumTerminalDelivery;
62
+ readonly timing?: PremiumTerminalTiming;
63
+ readonly error_category?: PremiumErrorCategory;
64
+ }
65
+ export interface PremiumTerminalSettlement {
66
+ readonly captured_nano_usd: string;
67
+ readonly released_nano_usd: string;
68
+ readonly business_earnings_nano_usd: string;
69
+ }
70
+ export interface ReportPremiumTerminalResponse {
71
+ readonly operation_id: string;
72
+ readonly status: Readable<PremiumTerminalStatus>;
73
+ readonly idempotent_replay: boolean;
74
+ readonly settlement: PremiumTerminalSettlement;
75
+ }
76
+ /** Validate `POST /v1/enlitt/operations` before any reservation attempt. */
77
+ export declare function validateBeginPremiumOperationRequest(value: unknown): Result<BeginPremiumOperationRequest>;
78
+ /** Validate the exact admitted-operation response shape. */
79
+ export declare function validateBeginPremiumOperationResponse(value: unknown, options?: ParseOptions): Result<BeginPremiumOperationResponse>;
80
+ /** Validate `POST /v1/enlitt/operations/{operation_id}/terminal`. */
81
+ export declare function validateReportPremiumTerminalRequest(value: unknown): Result<ReportPremiumTerminalRequest>;
82
+ /** Validate the exact terminal settlement response shape, including replays. */
83
+ export declare function validateReportPremiumTerminalResponse(value: unknown, options?: ParseOptions): Result<ReportPremiumTerminalResponse>;
84
+ export {};
@@ -0,0 +1,85 @@
1
+ /**
2
+ * The content-free managed-premium wire contracts.
3
+ *
4
+ * These validators deliberately know nothing about admission, a user's
5
+ * capacity, or settlement arithmetic. They validate the only facts a
6
+ * business may send or receive at the two premium endpoints, leaving the
7
+ * captured maximum and the status-specific lowering rule to Finance.
8
+ */
9
+ import { type ParseOptions, type Readable } from "./compatibility.js";
10
+ import { type ResponseMetadata, type ResponseValidationResult } from "./responses.js";
11
+ type Result<T> = ResponseValidationResult<T>;
12
+ /** The deployed maximum in `FINANCE_POLICY.maxChargeNanoUsd`: $100 in NANOUSD. */
13
+ export declare const PREMIUM_OPERATION_MAX_CHARGE_NANO_USD = "100000000000";
14
+ /** The terminal report amount is bounded independently of the current policy. */
15
+ export declare const PREMIUM_TERMINAL_CHARGE_MAX_DIGITS = 30;
16
+ export declare const PREMIUM_OPERATION_BILLING_VALUES: readonly ["managed"];
17
+ export declare const PREMIUM_TERMINAL_STATUSES: readonly ["completed", "failed", "canceled"];
18
+ export interface PremiumProviderLabel {
19
+ readonly id: string;
20
+ readonly model: string;
21
+ }
22
+ export interface BeginPremiumOperationRequest {
23
+ readonly mode: "premium";
24
+ readonly max_charge_nano_usd: string;
25
+ readonly provider: PremiumProviderLabel;
26
+ readonly reporting_deadline_ms?: number;
27
+ readonly metadata?: ResponseMetadata;
28
+ }
29
+ export interface BeginPremiumOperationResponse {
30
+ readonly operation_id: string;
31
+ readonly billing: Readable<PremiumOperationBilling>;
32
+ readonly report_terminal: true;
33
+ readonly effective_max_charge_nano_usd: string;
34
+ readonly reserved_nano_usd: string;
35
+ readonly terminal_deadline: string;
36
+ }
37
+ export type PremiumOperationBilling = (typeof PREMIUM_OPERATION_BILLING_VALUES)[number];
38
+ export type PremiumTerminalStatus = (typeof PREMIUM_TERMINAL_STATUSES)[number];
39
+ export type PremiumErrorCategory = "provider" | "timeout" | "network" | "invalid_request" | "canceled" | "unknown";
40
+ export interface PremiumTerminalUsage {
41
+ readonly input_tokens?: number;
42
+ readonly output_tokens?: number;
43
+ readonly cached_input_tokens?: number;
44
+ readonly reasoning_tokens?: number;
45
+ }
46
+ export interface PremiumTerminalDelivery {
47
+ readonly output_started: boolean;
48
+ readonly output_completed: boolean;
49
+ }
50
+ export interface PremiumTerminalTiming {
51
+ readonly started_at: string;
52
+ readonly ended_at: string;
53
+ }
54
+ export interface ReportPremiumTerminalRequest {
55
+ readonly report_id: string;
56
+ readonly status: PremiumTerminalStatus;
57
+ readonly charge_nano_usd: string;
58
+ readonly usage?: PremiumTerminalUsage;
59
+ readonly finish_reason?: string;
60
+ readonly provider_response_id?: string;
61
+ readonly delivery?: PremiumTerminalDelivery;
62
+ readonly timing?: PremiumTerminalTiming;
63
+ readonly error_category?: PremiumErrorCategory;
64
+ }
65
+ export interface PremiumTerminalSettlement {
66
+ readonly captured_nano_usd: string;
67
+ readonly released_nano_usd: string;
68
+ readonly business_earnings_nano_usd: string;
69
+ }
70
+ export interface ReportPremiumTerminalResponse {
71
+ readonly operation_id: string;
72
+ readonly status: Readable<PremiumTerminalStatus>;
73
+ readonly idempotent_replay: boolean;
74
+ readonly settlement: PremiumTerminalSettlement;
75
+ }
76
+ /** Validate `POST /v1/enlitt/operations` before any reservation attempt. */
77
+ export declare function validateBeginPremiumOperationRequest(value: unknown): Result<BeginPremiumOperationRequest>;
78
+ /** Validate the exact admitted-operation response shape. */
79
+ export declare function validateBeginPremiumOperationResponse(value: unknown, options?: ParseOptions): Result<BeginPremiumOperationResponse>;
80
+ /** Validate `POST /v1/enlitt/operations/{operation_id}/terminal`. */
81
+ export declare function validateReportPremiumTerminalRequest(value: unknown): Result<ReportPremiumTerminalRequest>;
82
+ /** Validate the exact terminal settlement response shape, including replays. */
83
+ export declare function validateReportPremiumTerminalResponse(value: unknown, options?: ParseOptions): Result<ReportPremiumTerminalResponse>;
84
+ export {};
85
+ //# sourceMappingURL=premium-operations.d.ts.map
@@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CAChD;AAED,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAC;IACnC,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAC;IACnC,QAAQ,CAAC,0BAA0B,EAAE,MAAM,CAAC;CAC7C;AAED,MAAM,WAAW,6BAA6B;IAC5C,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,qBAAqB,CAAC,CAAC;IACjD,QAAQ,CAAC,iBAAiB,EAAE,OAAO,CAAC;IACpC,QAAQ,CAAC,UAAU,EAAE,yBAAyB,CAAC;CAChD;AAoBD,4EAA4E;AAC5E,wBAAgB,oCAAoC,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAAC,4BAA4B,CAAC,CA+CzG;AAED,4DAA4D;AAC5D,wBAAgB,qCAAqC,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,MAAM,CAAC,6BAA6B,CAAC,CA8BnI;AAyCD,qEAAqE;AACrE,wBAAgB,oCAAoC,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAAC,4BAA4B,CAAC,CA+EzG;AAED,gFAAgF;AAChF,wBAAgB,qCAAqC,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,MAAM,CAAC,6BAA6B,CAAC,CAqCnI"}
@@ -0,0 +1,246 @@
1
+ /**
2
+ * The `text/event-stream` wire contract for `POST /v1/responses` streaming,
3
+ * shared by the backend emitter and the SDK reader.
4
+ *
5
+ * `specs/PROTOCOLS.md` "Model response stream" fixes three things this module
6
+ * exists to check once instead of twice: the closed set of event names, the
7
+ * shape every event carries (`type` plus a monotonic `sequence_number`), and
8
+ * an ordering rule (`response.created` first, at most one terminal event,
9
+ * nothing after it, sequence numbers monotonic). The ordering rule is pure —
10
+ * it takes a list of already-parsed events and returns a verdict — so both
11
+ * the backend, which must never emit a violation, and the SDK, which must
12
+ * never trust a violation even from a well-behaved backend, run the identical
13
+ * check instead of each inventing their own notion of "in order".
14
+ *
15
+ * As with `responses.ts`, the spec text this module reads names the event
16
+ * types and the ordering rule but does not enumerate every per-event payload
17
+ * field. The per-event payload shapes below are inferred from the event
18
+ * names themselves (an `*.added`/`*.done` pair evidently carries the item or
19
+ * part it announces; a `*.delta` event evidently carries the incremental
20
+ * text) and from the pinned OpenAI Responses API subset this endpoint
21
+ * implements. Where a field is not confirmed by the read spec text, this is
22
+ * called out below rather than asserted as settled.
23
+ */
24
+ import { type EnlittErrorCode } from "./errors.cjs";
25
+ import type { ParseOptions, UnknownVariant } from "./compatibility.cjs";
26
+ import { type OpenAIResponse, type ResponseOutputContentPart, type ResponseOutputItem } from "./responses.cjs";
27
+ /**
28
+ * The closed set of stream event types, as a frozen record mapping each name
29
+ * to whether it is one of the three terminal events. A record rather than
30
+ * two separate arrays keeps "is this name known" and "is this name terminal"
31
+ * as one lookup instead of two lists that could silently drift out of sync,
32
+ * the same shape `ENLITT_ERROR_CODES` uses for the same reason.
33
+ */
34
+ export declare const RESPONSE_STREAM_EVENT_TYPES: Readonly<{
35
+ readonly "response.created": Readonly<{
36
+ terminal: false;
37
+ }>;
38
+ readonly "response.in_progress": Readonly<{
39
+ terminal: false;
40
+ }>;
41
+ readonly "response.output_item.added": Readonly<{
42
+ terminal: false;
43
+ }>;
44
+ readonly "response.output_item.done": Readonly<{
45
+ terminal: false;
46
+ }>;
47
+ readonly "response.content_part.added": Readonly<{
48
+ terminal: false;
49
+ }>;
50
+ readonly "response.content_part.done": Readonly<{
51
+ terminal: false;
52
+ }>;
53
+ readonly "response.output_text.delta": Readonly<{
54
+ terminal: false;
55
+ }>;
56
+ readonly "response.output_text.done": Readonly<{
57
+ terminal: false;
58
+ }>;
59
+ readonly "response.function_call_arguments.delta": Readonly<{
60
+ terminal: false;
61
+ }>;
62
+ readonly "response.function_call_arguments.done": Readonly<{
63
+ terminal: false;
64
+ }>;
65
+ readonly "response.completed": Readonly<{
66
+ terminal: true;
67
+ }>;
68
+ readonly "response.incomplete": Readonly<{
69
+ terminal: true;
70
+ }>;
71
+ readonly "response.failed": Readonly<{
72
+ terminal: true;
73
+ }>;
74
+ }>;
75
+ export type ResponseStreamEventType = keyof typeof RESPONSE_STREAM_EVENT_TYPES;
76
+ export declare const RESPONSE_STREAM_EVENT_TYPE_NAMES: readonly ("response.completed" | "response.content_part.added" | "response.content_part.done" | "response.created" | "response.failed" | "response.function_call_arguments.delta" | "response.function_call_arguments.done" | "response.in_progress" | "response.incomplete" | "response.output_item.added" | "response.output_item.done" | "response.output_text.delta" | "response.output_text.done")[];
77
+ export declare function isResponseStreamEventType(value: unknown): value is ResponseStreamEventType;
78
+ export declare function isTerminalResponseStreamEventType(type: ResponseStreamEventType): boolean;
79
+ /** The three event types that legally end a stream. */
80
+ export type TerminalResponseStreamEventType = "response.completed" | "response.incomplete" | "response.failed";
81
+ export type StreamValidationErrorCode = Extract<EnlittErrorCode, "invalid_value" | "unsupported_parameter" | "unsupported_value">;
82
+ export interface StreamValidationFailure {
83
+ readonly code: StreamValidationErrorCode;
84
+ readonly param: string;
85
+ readonly message: string;
86
+ }
87
+ export type StreamValidationResult<T> = {
88
+ readonly ok: true;
89
+ readonly value: T;
90
+ } | {
91
+ readonly ok: false;
92
+ readonly error: StreamValidationFailure;
93
+ };
94
+ /**
95
+ * The per-event payload variants. Every event carries `type` and
96
+ * `sequence_number`; the fields beyond that are inferred as described in the
97
+ * module doc comment and are the least-committal reading of the event names
98
+ * the spec text gives, not a spec quotation.
99
+ */
100
+ export interface ResponseCreatedEvent {
101
+ readonly type: "response.created";
102
+ readonly sequence_number: number;
103
+ readonly response: OpenAIResponse;
104
+ }
105
+ export interface ResponseInProgressEvent {
106
+ readonly type: "response.in_progress";
107
+ readonly sequence_number: number;
108
+ readonly response: OpenAIResponse;
109
+ }
110
+ export interface OutputItemAddedEvent {
111
+ readonly type: "response.output_item.added";
112
+ readonly sequence_number: number;
113
+ readonly output_index: number;
114
+ readonly item: ResponseOutputItem;
115
+ }
116
+ export interface OutputItemDoneEvent {
117
+ readonly type: "response.output_item.done";
118
+ readonly sequence_number: number;
119
+ readonly output_index: number;
120
+ readonly item: ResponseOutputItem;
121
+ }
122
+ export interface ContentPartAddedEvent {
123
+ readonly type: "response.content_part.added";
124
+ readonly sequence_number: number;
125
+ readonly item_id: string;
126
+ readonly output_index: number;
127
+ readonly content_index: number;
128
+ readonly part: ResponseOutputContentPart;
129
+ }
130
+ export interface ContentPartDoneEvent {
131
+ readonly type: "response.content_part.done";
132
+ readonly sequence_number: number;
133
+ readonly item_id: string;
134
+ readonly output_index: number;
135
+ readonly content_index: number;
136
+ readonly part: ResponseOutputContentPart;
137
+ }
138
+ export interface OutputTextDeltaEvent {
139
+ readonly type: "response.output_text.delta";
140
+ readonly sequence_number: number;
141
+ readonly item_id: string;
142
+ readonly output_index: number;
143
+ readonly content_index: number;
144
+ readonly delta: string;
145
+ }
146
+ export interface OutputTextDoneEvent {
147
+ readonly type: "response.output_text.done";
148
+ readonly sequence_number: number;
149
+ readonly item_id: string;
150
+ readonly output_index: number;
151
+ readonly content_index: number;
152
+ readonly text: string;
153
+ }
154
+ export interface FunctionCallArgumentsDeltaEvent {
155
+ readonly type: "response.function_call_arguments.delta";
156
+ readonly sequence_number: number;
157
+ readonly item_id: string;
158
+ readonly output_index: number;
159
+ readonly delta: string;
160
+ }
161
+ export interface FunctionCallArgumentsDoneEvent {
162
+ readonly type: "response.function_call_arguments.done";
163
+ readonly sequence_number: number;
164
+ readonly item_id: string;
165
+ readonly output_index: number;
166
+ readonly arguments: string;
167
+ }
168
+ export interface ResponseCompletedEvent {
169
+ readonly type: "response.completed";
170
+ readonly sequence_number: number;
171
+ readonly response: OpenAIResponse;
172
+ }
173
+ export interface ResponseIncompleteEvent {
174
+ readonly type: "response.incomplete";
175
+ readonly sequence_number: number;
176
+ readonly response: OpenAIResponse;
177
+ }
178
+ export interface ResponseFailedEvent {
179
+ readonly type: "response.failed";
180
+ readonly sequence_number: number;
181
+ readonly response: OpenAIResponse;
182
+ }
183
+ /**
184
+ * A stream event introduced after this protocol package was released.
185
+ *
186
+ * Tolerant readers retain its type and sequence number so they can skip the
187
+ * payload without weakening stream-order validation. The payload is not
188
+ * copied: no older reader can safely interpret fields belonging to a variant
189
+ * it does not know.
190
+ */
191
+ export interface UnknownResponseStreamEvent extends UnknownVariant {
192
+ readonly sequence_number: number;
193
+ }
194
+ export type ResponseStreamEvent = ResponseCreatedEvent | ResponseInProgressEvent | OutputItemAddedEvent | OutputItemDoneEvent | ContentPartAddedEvent | ContentPartDoneEvent | OutputTextDeltaEvent | OutputTextDoneEvent | FunctionCallArgumentsDeltaEvent | FunctionCallArgumentsDoneEvent | ResponseCompletedEvent | ResponseIncompleteEvent | ResponseFailedEvent;
195
+ /**
196
+ * Validate one already-JSON-parsed SSE `data` payload.
197
+ *
198
+ * Response-bearing events delegate to `validateOpenAIResponse`, including its
199
+ * strict/tolerant nested parsing. Item and part events remain outer-shape
200
+ * checks: those partial objects are not complete `OpenAIResponse` values and
201
+ * their stream-specific fields are validated by their own event variants.
202
+ */
203
+ export declare function validateResponseStreamEvent(value: unknown, options?: ParseOptions): StreamValidationResult<ResponseStreamEvent | UnknownResponseStreamEvent>;
204
+ /** The minimal shape the ordering check needs from an event; deliberately not the full validated union so a caller can check ordering before or independent of full payload validation. */
205
+ export interface StreamOrderingEvent {
206
+ /** Unknown stream events are kept in the sequence rather than dropped. */
207
+ readonly type: string;
208
+ readonly sequence_number: number;
209
+ }
210
+ export type StreamOrderingViolation = {
211
+ readonly kind: "missing_first_created";
212
+ } | {
213
+ readonly kind: "non_monotonic_sequence";
214
+ readonly index: number;
215
+ } | {
216
+ readonly kind: "event_after_terminal";
217
+ readonly index: number;
218
+ } | {
219
+ readonly kind: "missing_terminal";
220
+ };
221
+ export type StreamOrderingCheck = {
222
+ readonly ok: true;
223
+ } | {
224
+ readonly ok: false;
225
+ readonly violation: StreamOrderingViolation;
226
+ };
227
+ /**
228
+ * Check a full (or full-so-far) list of stream events against
229
+ * `specs/PROTOCOLS.md`'s ordering rule: `response.created` first, sequence
230
+ * numbers strictly increasing, at most one terminal event, and nothing after
231
+ * it. No I/O and no dependency on wall-clock time — this runs identically
232
+ * whether the backend calls it before emitting an event or the SDK calls it
233
+ * after receiving one.
234
+ *
235
+ * A second terminal event is reported as `event_after_terminal` rather than
236
+ * as its own violation kind: by the time a second terminal event arrives, an
237
+ * earlier one has already closed the stream, and "an event arrived after the
238
+ * terminal event" is the true description of what happened regardless of
239
+ * what kind of event it is.
240
+ *
241
+ * "Monotonic" is read as strictly increasing. `specs/PROTOCOLS.md` does not
242
+ * state whether a repeated sequence number would be tolerated; this module
243
+ * takes the stricter reading because a repeated number cannot tell two
244
+ * distinct events apart, which is the ordering guarantee's entire purpose.
245
+ */
246
+ export declare function checkResponseStreamOrdering(events: readonly StreamOrderingEvent[]): StreamOrderingCheck;
@@ -0,0 +1,247 @@
1
+ /**
2
+ * The `text/event-stream` wire contract for `POST /v1/responses` streaming,
3
+ * shared by the backend emitter and the SDK reader.
4
+ *
5
+ * `specs/PROTOCOLS.md` "Model response stream" fixes three things this module
6
+ * exists to check once instead of twice: the closed set of event names, the
7
+ * shape every event carries (`type` plus a monotonic `sequence_number`), and
8
+ * an ordering rule (`response.created` first, at most one terminal event,
9
+ * nothing after it, sequence numbers monotonic). The ordering rule is pure —
10
+ * it takes a list of already-parsed events and returns a verdict — so both
11
+ * the backend, which must never emit a violation, and the SDK, which must
12
+ * never trust a violation even from a well-behaved backend, run the identical
13
+ * check instead of each inventing their own notion of "in order".
14
+ *
15
+ * As with `responses.ts`, the spec text this module reads names the event
16
+ * types and the ordering rule but does not enumerate every per-event payload
17
+ * field. The per-event payload shapes below are inferred from the event
18
+ * names themselves (an `*.added`/`*.done` pair evidently carries the item or
19
+ * part it announces; a `*.delta` event evidently carries the incremental
20
+ * text) and from the pinned OpenAI Responses API subset this endpoint
21
+ * implements. Where a field is not confirmed by the read spec text, this is
22
+ * called out below rather than asserted as settled.
23
+ */
24
+ import { type EnlittErrorCode } from "./errors.js";
25
+ import type { ParseOptions, UnknownVariant } from "./compatibility.js";
26
+ import { type OpenAIResponse, type ResponseOutputContentPart, type ResponseOutputItem } from "./responses.js";
27
+ /**
28
+ * The closed set of stream event types, as a frozen record mapping each name
29
+ * to whether it is one of the three terminal events. A record rather than
30
+ * two separate arrays keeps "is this name known" and "is this name terminal"
31
+ * as one lookup instead of two lists that could silently drift out of sync,
32
+ * the same shape `ENLITT_ERROR_CODES` uses for the same reason.
33
+ */
34
+ export declare const RESPONSE_STREAM_EVENT_TYPES: Readonly<{
35
+ readonly "response.created": Readonly<{
36
+ terminal: false;
37
+ }>;
38
+ readonly "response.in_progress": Readonly<{
39
+ terminal: false;
40
+ }>;
41
+ readonly "response.output_item.added": Readonly<{
42
+ terminal: false;
43
+ }>;
44
+ readonly "response.output_item.done": Readonly<{
45
+ terminal: false;
46
+ }>;
47
+ readonly "response.content_part.added": Readonly<{
48
+ terminal: false;
49
+ }>;
50
+ readonly "response.content_part.done": Readonly<{
51
+ terminal: false;
52
+ }>;
53
+ readonly "response.output_text.delta": Readonly<{
54
+ terminal: false;
55
+ }>;
56
+ readonly "response.output_text.done": Readonly<{
57
+ terminal: false;
58
+ }>;
59
+ readonly "response.function_call_arguments.delta": Readonly<{
60
+ terminal: false;
61
+ }>;
62
+ readonly "response.function_call_arguments.done": Readonly<{
63
+ terminal: false;
64
+ }>;
65
+ readonly "response.completed": Readonly<{
66
+ terminal: true;
67
+ }>;
68
+ readonly "response.incomplete": Readonly<{
69
+ terminal: true;
70
+ }>;
71
+ readonly "response.failed": Readonly<{
72
+ terminal: true;
73
+ }>;
74
+ }>;
75
+ export type ResponseStreamEventType = keyof typeof RESPONSE_STREAM_EVENT_TYPES;
76
+ export declare const RESPONSE_STREAM_EVENT_TYPE_NAMES: readonly ("response.completed" | "response.content_part.added" | "response.content_part.done" | "response.created" | "response.failed" | "response.function_call_arguments.delta" | "response.function_call_arguments.done" | "response.in_progress" | "response.incomplete" | "response.output_item.added" | "response.output_item.done" | "response.output_text.delta" | "response.output_text.done")[];
77
+ export declare function isResponseStreamEventType(value: unknown): value is ResponseStreamEventType;
78
+ export declare function isTerminalResponseStreamEventType(type: ResponseStreamEventType): boolean;
79
+ /** The three event types that legally end a stream. */
80
+ export type TerminalResponseStreamEventType = "response.completed" | "response.incomplete" | "response.failed";
81
+ export type StreamValidationErrorCode = Extract<EnlittErrorCode, "invalid_value" | "unsupported_parameter" | "unsupported_value">;
82
+ export interface StreamValidationFailure {
83
+ readonly code: StreamValidationErrorCode;
84
+ readonly param: string;
85
+ readonly message: string;
86
+ }
87
+ export type StreamValidationResult<T> = {
88
+ readonly ok: true;
89
+ readonly value: T;
90
+ } | {
91
+ readonly ok: false;
92
+ readonly error: StreamValidationFailure;
93
+ };
94
+ /**
95
+ * The per-event payload variants. Every event carries `type` and
96
+ * `sequence_number`; the fields beyond that are inferred as described in the
97
+ * module doc comment and are the least-committal reading of the event names
98
+ * the spec text gives, not a spec quotation.
99
+ */
100
+ export interface ResponseCreatedEvent {
101
+ readonly type: "response.created";
102
+ readonly sequence_number: number;
103
+ readonly response: OpenAIResponse;
104
+ }
105
+ export interface ResponseInProgressEvent {
106
+ readonly type: "response.in_progress";
107
+ readonly sequence_number: number;
108
+ readonly response: OpenAIResponse;
109
+ }
110
+ export interface OutputItemAddedEvent {
111
+ readonly type: "response.output_item.added";
112
+ readonly sequence_number: number;
113
+ readonly output_index: number;
114
+ readonly item: ResponseOutputItem;
115
+ }
116
+ export interface OutputItemDoneEvent {
117
+ readonly type: "response.output_item.done";
118
+ readonly sequence_number: number;
119
+ readonly output_index: number;
120
+ readonly item: ResponseOutputItem;
121
+ }
122
+ export interface ContentPartAddedEvent {
123
+ readonly type: "response.content_part.added";
124
+ readonly sequence_number: number;
125
+ readonly item_id: string;
126
+ readonly output_index: number;
127
+ readonly content_index: number;
128
+ readonly part: ResponseOutputContentPart;
129
+ }
130
+ export interface ContentPartDoneEvent {
131
+ readonly type: "response.content_part.done";
132
+ readonly sequence_number: number;
133
+ readonly item_id: string;
134
+ readonly output_index: number;
135
+ readonly content_index: number;
136
+ readonly part: ResponseOutputContentPart;
137
+ }
138
+ export interface OutputTextDeltaEvent {
139
+ readonly type: "response.output_text.delta";
140
+ readonly sequence_number: number;
141
+ readonly item_id: string;
142
+ readonly output_index: number;
143
+ readonly content_index: number;
144
+ readonly delta: string;
145
+ }
146
+ export interface OutputTextDoneEvent {
147
+ readonly type: "response.output_text.done";
148
+ readonly sequence_number: number;
149
+ readonly item_id: string;
150
+ readonly output_index: number;
151
+ readonly content_index: number;
152
+ readonly text: string;
153
+ }
154
+ export interface FunctionCallArgumentsDeltaEvent {
155
+ readonly type: "response.function_call_arguments.delta";
156
+ readonly sequence_number: number;
157
+ readonly item_id: string;
158
+ readonly output_index: number;
159
+ readonly delta: string;
160
+ }
161
+ export interface FunctionCallArgumentsDoneEvent {
162
+ readonly type: "response.function_call_arguments.done";
163
+ readonly sequence_number: number;
164
+ readonly item_id: string;
165
+ readonly output_index: number;
166
+ readonly arguments: string;
167
+ }
168
+ export interface ResponseCompletedEvent {
169
+ readonly type: "response.completed";
170
+ readonly sequence_number: number;
171
+ readonly response: OpenAIResponse;
172
+ }
173
+ export interface ResponseIncompleteEvent {
174
+ readonly type: "response.incomplete";
175
+ readonly sequence_number: number;
176
+ readonly response: OpenAIResponse;
177
+ }
178
+ export interface ResponseFailedEvent {
179
+ readonly type: "response.failed";
180
+ readonly sequence_number: number;
181
+ readonly response: OpenAIResponse;
182
+ }
183
+ /**
184
+ * A stream event introduced after this protocol package was released.
185
+ *
186
+ * Tolerant readers retain its type and sequence number so they can skip the
187
+ * payload without weakening stream-order validation. The payload is not
188
+ * copied: no older reader can safely interpret fields belonging to a variant
189
+ * it does not know.
190
+ */
191
+ export interface UnknownResponseStreamEvent extends UnknownVariant {
192
+ readonly sequence_number: number;
193
+ }
194
+ export type ResponseStreamEvent = ResponseCreatedEvent | ResponseInProgressEvent | OutputItemAddedEvent | OutputItemDoneEvent | ContentPartAddedEvent | ContentPartDoneEvent | OutputTextDeltaEvent | OutputTextDoneEvent | FunctionCallArgumentsDeltaEvent | FunctionCallArgumentsDoneEvent | ResponseCompletedEvent | ResponseIncompleteEvent | ResponseFailedEvent;
195
+ /**
196
+ * Validate one already-JSON-parsed SSE `data` payload.
197
+ *
198
+ * Response-bearing events delegate to `validateOpenAIResponse`, including its
199
+ * strict/tolerant nested parsing. Item and part events remain outer-shape
200
+ * checks: those partial objects are not complete `OpenAIResponse` values and
201
+ * their stream-specific fields are validated by their own event variants.
202
+ */
203
+ export declare function validateResponseStreamEvent(value: unknown, options?: ParseOptions): StreamValidationResult<ResponseStreamEvent | UnknownResponseStreamEvent>;
204
+ /** The minimal shape the ordering check needs from an event; deliberately not the full validated union so a caller can check ordering before or independent of full payload validation. */
205
+ export interface StreamOrderingEvent {
206
+ /** Unknown stream events are kept in the sequence rather than dropped. */
207
+ readonly type: string;
208
+ readonly sequence_number: number;
209
+ }
210
+ export type StreamOrderingViolation = {
211
+ readonly kind: "missing_first_created";
212
+ } | {
213
+ readonly kind: "non_monotonic_sequence";
214
+ readonly index: number;
215
+ } | {
216
+ readonly kind: "event_after_terminal";
217
+ readonly index: number;
218
+ } | {
219
+ readonly kind: "missing_terminal";
220
+ };
221
+ export type StreamOrderingCheck = {
222
+ readonly ok: true;
223
+ } | {
224
+ readonly ok: false;
225
+ readonly violation: StreamOrderingViolation;
226
+ };
227
+ /**
228
+ * Check a full (or full-so-far) list of stream events against
229
+ * `specs/PROTOCOLS.md`'s ordering rule: `response.created` first, sequence
230
+ * numbers strictly increasing, at most one terminal event, and nothing after
231
+ * it. No I/O and no dependency on wall-clock time — this runs identically
232
+ * whether the backend calls it before emitting an event or the SDK calls it
233
+ * after receiving one.
234
+ *
235
+ * A second terminal event is reported as `event_after_terminal` rather than
236
+ * as its own violation kind: by the time a second terminal event arrives, an
237
+ * earlier one has already closed the stream, and "an event arrived after the
238
+ * terminal event" is the true description of what happened regardless of
239
+ * what kind of event it is.
240
+ *
241
+ * "Monotonic" is read as strictly increasing. `specs/PROTOCOLS.md` does not
242
+ * state whether a repeated sequence number would be tolerated; this module
243
+ * takes the stricter reading because a repeated number cannot tell two
244
+ * distinct events apart, which is the ordering guarantee's entire purpose.
245
+ */
246
+ export declare function checkResponseStreamOrdering(events: readonly StreamOrderingEvent[]): StreamOrderingCheck;
247
+ //# sourceMappingURL=response-stream.d.ts.map