@enlitt/protocol 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/CHANGELOG.md +21 -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 +1078 -0
  20. package/dist/dashboard.d.ts +1079 -0
  21. package/dist/dashboard.d.ts.map +1 -0
  22. package/dist/errors.d.cts +113 -0
  23. package/dist/errors.d.ts +114 -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 +5050 -0
  29. package/dist/index.cjs.map +1 -0
  30. package/dist/index.d.cts +33 -0
  31. package/dist/index.d.ts +34 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +4857 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/outreach.d.cts +73 -0
  36. package/dist/outreach.d.ts +74 -0
  37. package/dist/outreach.d.ts.map +1 -0
  38. package/dist/premium-operations.d.cts +84 -0
  39. package/dist/premium-operations.d.ts +85 -0
  40. package/dist/premium-operations.d.ts.map +1 -0
  41. package/dist/response-stream.d.cts +246 -0
  42. package/dist/response-stream.d.ts +247 -0
  43. package/dist/response-stream.d.ts.map +1 -0
  44. package/dist/responses.d.cts +233 -0
  45. package/dist/responses.d.ts +234 -0
  46. package/dist/responses.d.ts.map +1 -0
  47. package/dist/usage.d.cts +11 -0
  48. package/dist/usage.d.ts +12 -0
  49. package/dist/usage.d.ts.map +1 -0
  50. package/generated/contract.json +491 -0
  51. package/package.json +60 -3
@@ -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
@@ -0,0 +1 @@
1
+ {"version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AAC,IAAI,EAAE,uBAAuB,CAAA;CAAE,GAC1C;IAAE,QAAQ,CAAC,IAAI,EAAE,wBAAwB,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACnE;IAAE,QAAQ,CAAC,IAAI,EAAE,sBAAsB,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACjE;IAAE,QAAQ,CAAC,IAAI,EAAE,kBAAkB,CAAA;CAAE,CAAC;AAE1C,MAAM,MAAM,mBAAmB,GAC3B;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAA;CAAE,GACrB;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,SAAS,EAAE,uBAAuB,CAAA;CAAE,CAAC;AAExE;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,SAAS,mBAAmB,EAAE,GACrC,mBAAmB,CA0BrB"}
@@ -0,0 +1,233 @@
1
+ /**
2
+ * The stateless `POST /v1/responses` wire contract: request body, response
3
+ * body, and the hand-written runtime validators for both.
4
+ *
5
+ * `specs/PROTOCOLS.md` "Create model response" names an exact accepted field
6
+ * list, an exact rejected field list, and three failure classes a validator
7
+ * must distinguish so the backend can raise the right stable code and the SDK
8
+ * can classify it the same way from the same table. This module owns that
9
+ * classification once, the same way `errors.ts` owns the code table: the
10
+ * backend and the SDK both import it rather than each re-deriving which
11
+ * field belongs to which class.
12
+ *
13
+ * This package has zero runtime dependencies, so validation here is
14
+ * hand-written structural checking rather than a schema library. A validator
15
+ * never throws on ordinary bad input — it returns a result, because an
16
+ * `invalid_value` on request field 40 of 40 is routine traffic, not a bug in
17
+ * this process.
18
+ *
19
+ * Two shapes in `specs/PROTOCOLS.md` are gestured at but not enumerated in the
20
+ * "Create model response" section: `JsonSchema` is used as a type ("bounded
21
+ * ... schema depth, and encoded size") without the bound values being stated,
22
+ * and `ResponseOutputItem` is named without its member shapes being spelled
23
+ * out the way input items are. This module deliberately does not invent
24
+ * numeric bounds or output-item field shapes the spec does not give; see the
25
+ * doc comments on `JsonSchema`, `FunctionTool`, and `ResponseOutputItem` for
26
+ * exactly what is and is not enforced, and the implementation report for the
27
+ * ambiguity this leaves open.
28
+ */
29
+ import { type EnlittErrorCode } from "./errors.cjs";
30
+ import { type ParseOptions, type Readable, type UnknownVariant } from "./compatibility.cjs";
31
+ /**
32
+ * The only accepted value of the request's `model` field. A single accepted
33
+ * string is deliberate: the caller asks for the standard route and Enlitt
34
+ * alone decides which model actually serves it, so there is no override to
35
+ * validate the shape of.
36
+ */
37
+ export declare const ENLITT_STANDARD_MODEL = "enlitt-standard";
38
+ export type EnlittStandardModel = typeof ENLITT_STANDARD_MODEL;
39
+ /**
40
+ * The three failure classes `specs/PROTOCOLS.md` names for this surface,
41
+ * narrowed from the full stable-code union so a caller cannot construct a
42
+ * validation failure carrying a code this surface never raises. Codes are
43
+ * referenced through `ENLITT_ERROR_CODE`'s own keys rather than written as
44
+ * string literals, so a rename in `errors.ts` fails this file to compile
45
+ * instead of silently drifting.
46
+ */
47
+ export type ResponseRequestErrorCode = Extract<EnlittErrorCode, "invalid_value" | "unsupported_parameter" | "unsupported_value">;
48
+ /**
49
+ * A single validation failure. `param` names the offending field using the
50
+ * same dotted/bracketed path an OpenAI-shaped error already uses, so the
51
+ * value here can be copied directly into `EnlittErrorInput.param` at the call
52
+ * site without renaming anything.
53
+ */
54
+ export interface ResponseValidationFailure {
55
+ readonly code: ResponseRequestErrorCode;
56
+ readonly param: string;
57
+ readonly message: string;
58
+ }
59
+ /**
60
+ * The result shape every validator in this module returns. Ordinary
61
+ * validation failure is data, not an exception: a request body is untrusted
62
+ * input on the hottest path this package serves, and throwing for the
63
+ * expected case would force every caller to wrap every call in `try/catch`.
64
+ */
65
+ export type ResponseValidationResult<T> = {
66
+ readonly ok: true;
67
+ readonly value: T;
68
+ } | {
69
+ readonly ok: false;
70
+ readonly error: ResponseValidationFailure;
71
+ };
72
+ /** A JSON Schema object supplied to a function tool. */
73
+ export type JsonSchema = Readonly<Record<string, unknown>>;
74
+ export interface EnlittTextPart {
75
+ readonly type: "text";
76
+ readonly text: string;
77
+ }
78
+ export interface EnlittImageUrlSource {
79
+ readonly type: "url";
80
+ readonly url: string;
81
+ }
82
+ export interface EnlittImageBase64Source {
83
+ readonly type: "base64";
84
+ readonly media_type: "image/png" | "image/jpeg";
85
+ readonly data: string;
86
+ }
87
+ export interface EnlittImagePart {
88
+ readonly type: "image";
89
+ readonly source: EnlittImageUrlSource | EnlittImageBase64Source;
90
+ }
91
+ export type EnlittUserPart = EnlittTextPart | EnlittImagePart;
92
+ export interface EnlittSystemMessage {
93
+ readonly role: "system";
94
+ readonly content: string;
95
+ }
96
+ export interface EnlittUserMessage {
97
+ readonly role: "user";
98
+ readonly content: readonly EnlittUserPart[];
99
+ }
100
+ export interface EnlittToolCall {
101
+ readonly id: string;
102
+ readonly name: string;
103
+ readonly arguments: string;
104
+ }
105
+ export interface EnlittAssistantMessage {
106
+ readonly role: "assistant";
107
+ readonly content?: string;
108
+ readonly tool_calls?: readonly EnlittToolCall[];
109
+ }
110
+ export interface EnlittToolMessage {
111
+ readonly role: "tool";
112
+ readonly tool_call_id: string;
113
+ readonly content: string;
114
+ }
115
+ export type EnlittStandardMessage = EnlittSystemMessage | EnlittUserMessage | EnlittAssistantMessage | EnlittToolMessage;
116
+ export interface EnlittFunctionTool {
117
+ readonly name: string;
118
+ readonly description?: string;
119
+ readonly input_schema: JsonSchema;
120
+ }
121
+ /**
122
+ * The identical caller-metadata bound `specs/PROTOCOLS.md` states once and
123
+ * both the SDK and the premium path must apply without restating: at most 16
124
+ * entries, keys at most 64 characters, values at most 512, and no caller key
125
+ * beginning with `enlitt_` (that prefix is reserved for facts Enlitt adds to
126
+ * a response, such as `enlitt_operation_id`).
127
+ */
128
+ export declare const CALLER_METADATA_LIMITS: Readonly<{
129
+ maxEntries: 16;
130
+ maxKeyLength: 64;
131
+ maxValueLength: 512;
132
+ reservedKeyPrefix: "enlitt_";
133
+ }>;
134
+ export type ResponseMetadata = Readonly<Record<string, string>>;
135
+ /**
136
+ * Validate caller-supplied metadata against the one bound this surface, the
137
+ * premium path, and the SDK all share. Exported standalone — not folded into
138
+ * `validateEnlittStandardRequest` — precisely so a second copy of this
139
+ * rule never needs to be written next to a different endpoint's field list.
140
+ */
141
+ export declare function validateCallerMetadata(value: unknown, param?: string): ResponseValidationResult<ResponseMetadata>;
142
+ export interface EnlittStandardRequest {
143
+ readonly model: EnlittStandardModel;
144
+ readonly messages: readonly EnlittStandardMessage[];
145
+ readonly tools?: readonly EnlittFunctionTool[];
146
+ readonly max_output_tokens?: number;
147
+ readonly stream?: boolean;
148
+ readonly metadata?: ResponseMetadata;
149
+ }
150
+ export declare function validateEnlittStandardRequest(value: unknown): ResponseValidationResult<EnlittStandardRequest>;
151
+ export declare const RESPONSE_STATUSES: readonly ["completed", "failed", "in_progress", "cancelled", "queued", "incomplete"];
152
+ export declare const RESPONSE_INCOMPLETE_REASONS: readonly ["max_output_tokens"];
153
+ export declare const RESPONSE_ITEM_STATUSES: readonly ["in_progress", "completed", "incomplete"];
154
+ export type ResponseStatusMember = "completed" | "failed" | "in_progress" | "cancelled" | "queued" | "incomplete";
155
+ export type ResponseStatus = Readable<ResponseStatusMember>;
156
+ export type ResponseIncompleteReason = Readable<(typeof RESPONSE_INCOMPLETE_REASONS)[number]>;
157
+ /**
158
+ * A member of `OpenAIResponse.output`.
159
+ *
160
+ * `specs/PROTOCOLS.md` names `ResponseOutputItem` but, in the section this
161
+ * module implements, does not enumerate its member shapes the way it
162
+ * enumerates input items. The two variants below are modeled on the pinned
163
+ * OpenAI Responses API subset this endpoint implements (the same subset the
164
+ * differential test harness drives) and on the streaming event names the
165
+ * spec does give — `response.output_item.*`, `response.content_part.*`,
166
+ * `response.output_text.*`, and `response.function_call_arguments.*` imply
167
+ * exactly a text-bearing message item and a function-call item and nothing
168
+ * else. This is the closest reading available from the read scope for this
169
+ * task; it is not a spec quotation, and it should be confirmed against
170
+ * wherever `ResponseOutputItem` is actually pinned down before another
171
+ * workspace depends on these exact field names.
172
+ */
173
+ export interface ResponseOutputTextPart {
174
+ readonly type: "output_text";
175
+ readonly text: string;
176
+ }
177
+ export type ResponseOutputContentPart = ResponseOutputTextPart | UnknownVariant;
178
+ export interface MessageOutputItem {
179
+ readonly type: "message";
180
+ readonly id: string;
181
+ readonly role: "assistant";
182
+ readonly status: Readable<(typeof RESPONSE_ITEM_STATUSES)[number]>;
183
+ readonly content: readonly ResponseOutputContentPart[];
184
+ }
185
+ export interface FunctionCallOutputItem {
186
+ readonly type: "function_call";
187
+ readonly id: string;
188
+ readonly call_id: string;
189
+ readonly name: string;
190
+ readonly arguments: string;
191
+ readonly status: Readable<(typeof RESPONSE_ITEM_STATUSES)[number]>;
192
+ }
193
+ export type ResponseOutputItem = MessageOutputItem | FunctionCallOutputItem | UnknownVariant;
194
+ export declare function validateOutputTextPart(value: unknown, param: string, options?: ParseOptions): ResponseValidationResult<ResponseOutputContentPart>;
195
+ export declare function validateResponseOutputItem(value: unknown, param: string, options?: ParseOptions): ResponseValidationResult<ResponseOutputItem>;
196
+ export interface ResponseUsage {
197
+ readonly input_tokens: number;
198
+ readonly input_tokens_details?: {
199
+ readonly cached_tokens?: number;
200
+ };
201
+ readonly output_tokens: number;
202
+ readonly output_tokens_details?: {
203
+ readonly reasoning_tokens?: number;
204
+ };
205
+ readonly total_tokens: number;
206
+ }
207
+ export interface OpenAIResponse {
208
+ readonly id: string;
209
+ readonly object: "response";
210
+ readonly created_at: number;
211
+ readonly completed_at?: number;
212
+ readonly status: ResponseStatus;
213
+ readonly error: {
214
+ readonly code: string;
215
+ readonly message: string;
216
+ } | null;
217
+ readonly incomplete_details: {
218
+ readonly reason: ResponseIncompleteReason;
219
+ } | null;
220
+ readonly model: EnlittStandardModel;
221
+ readonly output: readonly ResponseOutputItem[];
222
+ readonly output_text?: string;
223
+ readonly usage?: ResponseUsage;
224
+ readonly metadata: Readonly<Record<string, string>>;
225
+ }
226
+ /**
227
+ * Validate an `OpenAIResponse`. Unlike the request validator this is not on
228
+ * the untrusted-input boundary of a hostile caller — it validates what this
229
+ * system itself produced, or what a test fixture claims this system
230
+ * produced — so failures are reported with the same three-class shape for
231
+ * consistency but are expected to be rare in production.
232
+ */
233
+ export declare function validateOpenAIResponse(value: unknown, options?: ParseOptions): ResponseValidationResult<OpenAIResponse>;