@kaana.ai/contracts 0.0.0-stage → 4.3.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 (79) hide show
  1. package/LICENSE +673 -0
  2. package/NOTICE +21 -0
  3. package/dist/index.d.ts +29 -0
  4. package/dist/index.js +29 -0
  5. package/dist/internal/attribution.d.ts +116 -0
  6. package/dist/internal/attribution.js +82 -0
  7. package/dist/internal/canonical.d.ts +31 -0
  8. package/dist/internal/canonical.js +57 -0
  9. package/dist/internal/catalogue.d.ts +1030 -0
  10. package/dist/internal/catalogue.js +349 -0
  11. package/dist/internal/credentialControl.d.ts +329 -0
  12. package/dist/internal/credentialControl.js +256 -0
  13. package/dist/internal/decisions.d.ts +245 -0
  14. package/dist/internal/decisions.js +69 -0
  15. package/dist/internal/economics.d.ts +27 -0
  16. package/dist/internal/economics.js +26 -0
  17. package/dist/internal/embeddings.d.ts +164 -0
  18. package/dist/internal/embeddings.js +59 -0
  19. package/dist/internal/errors.d.ts +185 -0
  20. package/dist/internal/errors.js +211 -0
  21. package/dist/internal/grant.d.ts +696 -0
  22. package/dist/internal/grant.js +437 -0
  23. package/dist/internal/identifiers.d.ts +63 -0
  24. package/dist/internal/identifiers.js +65 -0
  25. package/dist/internal/privateAutoExecution.d.ts +158 -0
  26. package/dist/internal/privateAutoExecution.js +188 -0
  27. package/dist/internal/realtime.d.ts +1005 -0
  28. package/dist/internal/realtime.js +187 -0
  29. package/dist/internal/request.d.ts +1079 -0
  30. package/dist/internal/request.js +404 -0
  31. package/dist/internal/routing.d.ts +90 -0
  32. package/dist/internal/routing.js +139 -0
  33. package/dist/internal/scopedExecution.d.ts +74 -0
  34. package/dist/internal/scopedExecution.js +61 -0
  35. package/dist/internal/settlement.d.ts +205 -0
  36. package/dist/internal/settlement.js +71 -0
  37. package/dist/internal/streamEvents.d.ts +422 -0
  38. package/dist/internal/streamEvents.js +171 -0
  39. package/dist/internal/usage.d.ts +127 -0
  40. package/dist/internal/usage.js +109 -0
  41. package/dist/internal.d.ts +30 -0
  42. package/dist/internal.js +30 -0
  43. package/dist/public/audio.d.ts +347 -0
  44. package/dist/public/audio.js +236 -0
  45. package/dist/public/audioFormats.d.ts +64 -0
  46. package/dist/public/audioFormats.js +60 -0
  47. package/dist/public/catalogue.d.ts +914 -0
  48. package/dist/public/catalogue.js +516 -0
  49. package/dist/public/chatCompletions.d.ts +381 -0
  50. package/dist/public/chatCompletions.js +321 -0
  51. package/dist/public/decisions.d.ts +182 -0
  52. package/dist/public/decisions.js +257 -0
  53. package/dist/public/embeddings.d.ts +34 -0
  54. package/dist/public/embeddings.js +66 -0
  55. package/dist/public/errors.d.ts +195 -0
  56. package/dist/public/errors.js +274 -0
  57. package/dist/public/generations.d.ts +330 -0
  58. package/dist/public/generations.js +121 -0
  59. package/dist/public/identifiers.d.ts +139 -0
  60. package/dist/public/identifiers.js +160 -0
  61. package/dist/public/images.d.ts +36 -0
  62. package/dist/public/images.js +36 -0
  63. package/dist/public/messages.d.ts +297 -0
  64. package/dist/public/messages.js +251 -0
  65. package/dist/public/money.d.ts +308 -0
  66. package/dist/public/money.js +237 -0
  67. package/dist/public/policy.d.ts +19 -0
  68. package/dist/public/policy.js +20 -0
  69. package/dist/public/realtime.d.ts +2006 -0
  70. package/dist/public/realtime.js +797 -0
  71. package/dist/public/responses.d.ts +253 -0
  72. package/dist/public/responses.js +95 -0
  73. package/dist/public/streamEvents.d.ts +372 -0
  74. package/dist/public/streamEvents.js +157 -0
  75. package/dist/public/version.d.ts +177 -0
  76. package/dist/public/version.js +177 -0
  77. package/openapi.json +6967 -0
  78. package/package.json +44 -4
  79. package/README.md +0 -3
@@ -0,0 +1,372 @@
1
+ /**
2
+ * Stream events a public client reads. The ones that carry nothing about who
3
+ * served a request are shared with the signed Oxy↔Kaana stream.
4
+ *
5
+ * Moved from `@oxy.so/contracts` 4.12.0 (`src/inference/streamEvents.ts`) by Kaana ADR 0002.
6
+ */
7
+ import { z } from 'zod';
8
+ /**
9
+ * A chunk of output.
10
+ *
11
+ * `channel` separates visible output from reasoning and refusals, because a
12
+ * client that renders reasoning as answer text is a product bug, not a display
13
+ * preference.
14
+ */
15
+ export declare const inferenceStreamDeltaEventSchema: z.ZodObject<{
16
+ schemaVersion: z.ZodLiteral<1>;
17
+ type: z.ZodLiteral<"delta">;
18
+ requestId: z.ZodString;
19
+ sequence: z.ZodNumber;
20
+ outputIndex: z.ZodNumber;
21
+ channel: z.ZodEnum<{
22
+ output_text: "output_text";
23
+ reasoning: "reasoning";
24
+ refusal: "refusal";
25
+ output_audio_transcript: "output_audio_transcript";
26
+ }>;
27
+ text: z.ZodString;
28
+ }, z.core.$strip>;
29
+ /** The encodings an audio output may carry. */
30
+ export declare const inferenceAudioMediaTypeSchema: z.ZodEnum<{
31
+ "audio/mpeg": "audio/mpeg";
32
+ "audio/wav": "audio/wav";
33
+ "audio/ogg": "audio/ogg";
34
+ "audio/aac": "audio/aac";
35
+ "audio/flac": "audio/flac";
36
+ "audio/pcm": "audio/pcm";
37
+ }>;
38
+ /** The most bytes one folded audio output may hold, at the edge and in the SDK. */
39
+ export declare const MAX_INFERENCE_AUDIO_BYTES: number;
40
+ /** A bounded, independently base64-encoded chunk of one audio output. */
41
+ export declare const inferenceStreamAudioEventSchema: z.ZodObject<{
42
+ schemaVersion: z.ZodLiteral<1>;
43
+ type: z.ZodLiteral<"audio">;
44
+ requestId: z.ZodString;
45
+ sequence: z.ZodNumber;
46
+ outputIndex: z.ZodNumber;
47
+ mediaType: z.ZodEnum<{
48
+ "audio/mpeg": "audio/mpeg";
49
+ "audio/wav": "audio/wav";
50
+ "audio/ogg": "audio/ogg";
51
+ "audio/aac": "audio/aac";
52
+ "audio/flac": "audio/flac";
53
+ "audio/pcm": "audio/pcm";
54
+ }>;
55
+ data: z.ZodString;
56
+ }, z.core.$strip>;
57
+ /**
58
+ * A tool call being streamed.
59
+ *
60
+ * `argumentsDelta` accumulates; `complete` marks the call finished so a client
61
+ * knows when the accumulated JSON text is worth parsing.
62
+ */
63
+ export declare const inferenceStreamToolCallEventSchema: z.ZodObject<{
64
+ schemaVersion: z.ZodLiteral<1>;
65
+ type: z.ZodLiteral<"tool_call">;
66
+ requestId: z.ZodString;
67
+ sequence: z.ZodNumber;
68
+ toolCallId: z.ZodString;
69
+ name: z.ZodOptional<z.ZodString>;
70
+ argumentsDelta: z.ZodOptional<z.ZodString>;
71
+ complete: z.ZodBoolean;
72
+ }, z.core.$strip>;
73
+ /**
74
+ * Why generation stopped.
75
+ *
76
+ * `refusal` and `content_filter` are separate members because they are separate
77
+ * events: the MODEL declining to answer is a property of the answer, while a
78
+ * filter is an upstream system removing one. The delta channels already carry
79
+ * that distinction (`channel: 'refusal'` beside the filter's own error code),
80
+ * so collapsing it here would have made the terminal event less specific than
81
+ * the stream that produced it.
82
+ */
83
+ export declare const inferenceFinishReasonSchema: z.ZodEnum<{
84
+ content_filter: "content_filter";
85
+ cancelled: "cancelled";
86
+ refusal: "refusal";
87
+ stop: "stop";
88
+ length: "length";
89
+ tool_calls: "tool_calls";
90
+ }>;
91
+ /**
92
+ * The successful terminal event.
93
+ *
94
+ * `receiptId` is present once settlement has produced one, giving a customer a
95
+ * direct handle on the exact amount charged rather than a telemetry estimate.
96
+ */
97
+ export declare const inferenceStreamDoneEventSchema: z.ZodObject<{
98
+ schemaVersion: z.ZodLiteral<1>;
99
+ type: z.ZodLiteral<"done">;
100
+ requestId: z.ZodString;
101
+ sequence: z.ZodNumber;
102
+ generationId: z.ZodOptional<z.ZodString>;
103
+ finishReason: z.ZodEnum<{
104
+ content_filter: "content_filter";
105
+ cancelled: "cancelled";
106
+ refusal: "refusal";
107
+ stop: "stop";
108
+ length: "length";
109
+ tool_calls: "tool_calls";
110
+ }>;
111
+ receiptId: z.ZodOptional<z.ZodString>;
112
+ completedAt: z.ZodString;
113
+ }, z.core.$strip>;
114
+ /**
115
+ * The first event of a public stream: the request id and the revision that is
116
+ * serving it.
117
+ *
118
+ * Kaana moves a request (retry, failover, the next grant step) only before any
119
+ * output is delivered, so the model named here is the one that produced every
120
+ * event after it; a public stream therefore needs no route-switch event, and it
121
+ * never names a provider or a deployment.
122
+ */
123
+ export declare const responseStreamStartEventSchema: z.ZodObject<{
124
+ schemaVersion: z.ZodLiteral<1>;
125
+ type: z.ZodLiteral<"start">;
126
+ requestId: z.ZodString;
127
+ sequence: z.ZodNumber;
128
+ generationId: z.ZodOptional<z.ZodString>;
129
+ model: z.ZodString;
130
+ startedAt: z.ZodString;
131
+ }, z.core.$strip>;
132
+ /** What the request consumed, as measured. Units only, never money. */
133
+ export declare const responseStreamUsageEventSchema: z.ZodObject<{
134
+ schemaVersion: z.ZodLiteral<1>;
135
+ type: z.ZodLiteral<"usage">;
136
+ requestId: z.ZodString;
137
+ sequence: z.ZodNumber;
138
+ units: z.ZodArray<z.ZodObject<{
139
+ unit: z.ZodEnum<{
140
+ input_tokens: "input_tokens";
141
+ cached_input_tokens: "cached_input_tokens";
142
+ output_tokens: "output_tokens";
143
+ reasoning_tokens: "reasoning_tokens";
144
+ requests: "requests";
145
+ images: "images";
146
+ audio_input_milliseconds: "audio_input_milliseconds";
147
+ audio_output_milliseconds: "audio_output_milliseconds";
148
+ video_milliseconds: "video_milliseconds";
149
+ characters: "characters";
150
+ embeddings: "embeddings";
151
+ audio_input_tokens: "audio_input_tokens";
152
+ cached_audio_input_tokens: "cached_audio_input_tokens";
153
+ audio_output_tokens: "audio_output_tokens";
154
+ session_milliseconds: "session_milliseconds";
155
+ input_transcription_milliseconds: "input_transcription_milliseconds";
156
+ }>;
157
+ quantity: z.ZodNumber;
158
+ }, z.core.$strict>>;
159
+ usageSource: z.ZodEnum<{
160
+ provider_reported: "provider_reported";
161
+ oxy_measured: "oxy_measured";
162
+ estimated: "estimated";
163
+ }>;
164
+ }, z.core.$strip>;
165
+ /** The terminal failure of a public stream, after which nothing follows. */
166
+ export declare const responseStreamErrorEventSchema: z.ZodObject<{
167
+ schemaVersion: z.ZodLiteral<1>;
168
+ type: z.ZodLiteral<"error">;
169
+ requestId: z.ZodString;
170
+ sequence: z.ZodNumber;
171
+ error: z.ZodObject<{
172
+ schemaVersion: z.ZodLiteral<1>;
173
+ code: z.ZodEnum<{
174
+ invalid_request: "invalid_request";
175
+ authentication_failed: "authentication_failed";
176
+ permission_denied: "permission_denied";
177
+ insufficient_scope: "insufficient_scope";
178
+ model_not_found: "model_not_found";
179
+ unsupported_modality: "unsupported_modality";
180
+ context_length_exceeded: "context_length_exceeded";
181
+ request_too_large: "request_too_large";
182
+ output_limit_exceeded: "output_limit_exceeded";
183
+ idempotency_conflict: "idempotency_conflict";
184
+ insufficient_balance: "insufficient_balance";
185
+ spending_limit_exceeded: "spending_limit_exceeded";
186
+ quota_exceeded: "quota_exceeded";
187
+ byok_credential_invalid: "byok_credential_invalid";
188
+ policy_violation: "policy_violation";
189
+ commercial_permission_denied: "commercial_permission_denied";
190
+ no_route_available: "no_route_available";
191
+ upstream_content_filtered: "upstream_content_filtered";
192
+ cancelled: "cancelled";
193
+ rate_limited: "rate_limited";
194
+ provider_error: "provider_error";
195
+ provider_timeout: "provider_timeout";
196
+ provider_overloaded: "provider_overloaded";
197
+ service_unavailable: "service_unavailable";
198
+ internal_error: "internal_error";
199
+ }>;
200
+ message: z.ZodString;
201
+ retryable: z.ZodBoolean;
202
+ requestId: z.ZodString;
203
+ retryAfterMs: z.ZodOptional<z.ZodNumber>;
204
+ param: z.ZodOptional<z.ZodString>;
205
+ upstreamCategory: z.ZodOptional<z.ZodEnum<{
206
+ rate_limit: "rate_limit";
207
+ quota: "quota";
208
+ timeout: "timeout";
209
+ overloaded: "overloaded";
210
+ server_error: "server_error";
211
+ content_filter: "content_filter";
212
+ invalid_request: "invalid_request";
213
+ authentication: "authentication";
214
+ unknown: "unknown";
215
+ }>>;
216
+ }, z.core.$strict>;
217
+ }, z.core.$strip>;
218
+ /** Every event of a public response stream. */
219
+ export declare const responseStreamEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
220
+ schemaVersion: z.ZodLiteral<1>;
221
+ type: z.ZodLiteral<"start">;
222
+ requestId: z.ZodString;
223
+ sequence: z.ZodNumber;
224
+ generationId: z.ZodOptional<z.ZodString>;
225
+ model: z.ZodString;
226
+ startedAt: z.ZodString;
227
+ }, z.core.$strip>, z.ZodObject<{
228
+ schemaVersion: z.ZodLiteral<1>;
229
+ type: z.ZodLiteral<"delta">;
230
+ requestId: z.ZodString;
231
+ sequence: z.ZodNumber;
232
+ outputIndex: z.ZodNumber;
233
+ channel: z.ZodEnum<{
234
+ output_text: "output_text";
235
+ reasoning: "reasoning";
236
+ refusal: "refusal";
237
+ output_audio_transcript: "output_audio_transcript";
238
+ }>;
239
+ text: z.ZodString;
240
+ }, z.core.$strip>, z.ZodObject<{
241
+ schemaVersion: z.ZodLiteral<1>;
242
+ type: z.ZodLiteral<"audio">;
243
+ requestId: z.ZodString;
244
+ sequence: z.ZodNumber;
245
+ outputIndex: z.ZodNumber;
246
+ mediaType: z.ZodEnum<{
247
+ "audio/mpeg": "audio/mpeg";
248
+ "audio/wav": "audio/wav";
249
+ "audio/ogg": "audio/ogg";
250
+ "audio/aac": "audio/aac";
251
+ "audio/flac": "audio/flac";
252
+ "audio/pcm": "audio/pcm";
253
+ }>;
254
+ data: z.ZodString;
255
+ }, z.core.$strip>, z.ZodObject<{
256
+ schemaVersion: z.ZodLiteral<1>;
257
+ type: z.ZodLiteral<"tool_call">;
258
+ requestId: z.ZodString;
259
+ sequence: z.ZodNumber;
260
+ toolCallId: z.ZodString;
261
+ name: z.ZodOptional<z.ZodString>;
262
+ argumentsDelta: z.ZodOptional<z.ZodString>;
263
+ complete: z.ZodBoolean;
264
+ }, z.core.$strip>, z.ZodObject<{
265
+ schemaVersion: z.ZodLiteral<1>;
266
+ type: z.ZodLiteral<"usage">;
267
+ requestId: z.ZodString;
268
+ sequence: z.ZodNumber;
269
+ units: z.ZodArray<z.ZodObject<{
270
+ unit: z.ZodEnum<{
271
+ input_tokens: "input_tokens";
272
+ cached_input_tokens: "cached_input_tokens";
273
+ output_tokens: "output_tokens";
274
+ reasoning_tokens: "reasoning_tokens";
275
+ requests: "requests";
276
+ images: "images";
277
+ audio_input_milliseconds: "audio_input_milliseconds";
278
+ audio_output_milliseconds: "audio_output_milliseconds";
279
+ video_milliseconds: "video_milliseconds";
280
+ characters: "characters";
281
+ embeddings: "embeddings";
282
+ audio_input_tokens: "audio_input_tokens";
283
+ cached_audio_input_tokens: "cached_audio_input_tokens";
284
+ audio_output_tokens: "audio_output_tokens";
285
+ session_milliseconds: "session_milliseconds";
286
+ input_transcription_milliseconds: "input_transcription_milliseconds";
287
+ }>;
288
+ quantity: z.ZodNumber;
289
+ }, z.core.$strict>>;
290
+ usageSource: z.ZodEnum<{
291
+ provider_reported: "provider_reported";
292
+ oxy_measured: "oxy_measured";
293
+ estimated: "estimated";
294
+ }>;
295
+ }, z.core.$strip>, z.ZodObject<{
296
+ schemaVersion: z.ZodLiteral<1>;
297
+ type: z.ZodLiteral<"error">;
298
+ requestId: z.ZodString;
299
+ sequence: z.ZodNumber;
300
+ error: z.ZodObject<{
301
+ schemaVersion: z.ZodLiteral<1>;
302
+ code: z.ZodEnum<{
303
+ invalid_request: "invalid_request";
304
+ authentication_failed: "authentication_failed";
305
+ permission_denied: "permission_denied";
306
+ insufficient_scope: "insufficient_scope";
307
+ model_not_found: "model_not_found";
308
+ unsupported_modality: "unsupported_modality";
309
+ context_length_exceeded: "context_length_exceeded";
310
+ request_too_large: "request_too_large";
311
+ output_limit_exceeded: "output_limit_exceeded";
312
+ idempotency_conflict: "idempotency_conflict";
313
+ insufficient_balance: "insufficient_balance";
314
+ spending_limit_exceeded: "spending_limit_exceeded";
315
+ quota_exceeded: "quota_exceeded";
316
+ byok_credential_invalid: "byok_credential_invalid";
317
+ policy_violation: "policy_violation";
318
+ commercial_permission_denied: "commercial_permission_denied";
319
+ no_route_available: "no_route_available";
320
+ upstream_content_filtered: "upstream_content_filtered";
321
+ cancelled: "cancelled";
322
+ rate_limited: "rate_limited";
323
+ provider_error: "provider_error";
324
+ provider_timeout: "provider_timeout";
325
+ provider_overloaded: "provider_overloaded";
326
+ service_unavailable: "service_unavailable";
327
+ internal_error: "internal_error";
328
+ }>;
329
+ message: z.ZodString;
330
+ retryable: z.ZodBoolean;
331
+ requestId: z.ZodString;
332
+ retryAfterMs: z.ZodOptional<z.ZodNumber>;
333
+ param: z.ZodOptional<z.ZodString>;
334
+ upstreamCategory: z.ZodOptional<z.ZodEnum<{
335
+ rate_limit: "rate_limit";
336
+ quota: "quota";
337
+ timeout: "timeout";
338
+ overloaded: "overloaded";
339
+ server_error: "server_error";
340
+ content_filter: "content_filter";
341
+ invalid_request: "invalid_request";
342
+ authentication: "authentication";
343
+ unknown: "unknown";
344
+ }>>;
345
+ }, z.core.$strict>;
346
+ }, z.core.$strip>, z.ZodObject<{
347
+ schemaVersion: z.ZodLiteral<1>;
348
+ type: z.ZodLiteral<"done">;
349
+ requestId: z.ZodString;
350
+ sequence: z.ZodNumber;
351
+ generationId: z.ZodOptional<z.ZodString>;
352
+ finishReason: z.ZodEnum<{
353
+ content_filter: "content_filter";
354
+ cancelled: "cancelled";
355
+ refusal: "refusal";
356
+ stop: "stop";
357
+ length: "length";
358
+ tool_calls: "tool_calls";
359
+ }>;
360
+ receiptId: z.ZodOptional<z.ZodString>;
361
+ completedAt: z.ZodString;
362
+ }, z.core.$strip>], "type">;
363
+ export type InferenceAudioMediaType = z.infer<typeof inferenceAudioMediaTypeSchema>;
364
+ export type InferenceStreamDeltaEvent = z.infer<typeof inferenceStreamDeltaEventSchema>;
365
+ export type InferenceStreamAudioEvent = z.infer<typeof inferenceStreamAudioEventSchema>;
366
+ export type InferenceStreamToolCallEvent = z.infer<typeof inferenceStreamToolCallEventSchema>;
367
+ export type InferenceFinishReason = z.infer<typeof inferenceFinishReasonSchema>;
368
+ export type InferenceStreamDoneEvent = z.infer<typeof inferenceStreamDoneEventSchema>;
369
+ export type ResponseStreamStartEvent = z.infer<typeof responseStreamStartEventSchema>;
370
+ export type ResponseStreamUsageEvent = z.infer<typeof responseStreamUsageEventSchema>;
371
+ export type ResponseStreamErrorEvent = z.infer<typeof responseStreamErrorEventSchema>;
372
+ export type ResponseStreamEvent = z.infer<typeof responseStreamEventSchema>;
@@ -0,0 +1,157 @@
1
+ /**
2
+ * Stream events a public client reads. The ones that carry nothing about who
3
+ * served a request are shared with the signed Oxy↔Kaana stream.
4
+ *
5
+ * Moved from `@oxy.so/contracts` 4.12.0 (`src/inference/streamEvents.ts`) by Kaana ADR 0002.
6
+ */
7
+ import { z } from 'zod';
8
+ import { generationIdSchema, inferenceTimestampSchema, modelReferenceSchema, PADDED_BASE64_PATTERN, requestIdSchema, } from './identifiers.js';
9
+ import { publicErrorSchema } from './errors.js';
10
+ import { usageQuantitySchema, usageSourceSchema } from './money.js';
11
+ /**
12
+ * A chunk of output.
13
+ *
14
+ * `channel` separates visible output from reasoning and refusals, because a
15
+ * client that renders reasoning as answer text is a product bug, not a display
16
+ * preference.
17
+ */
18
+ export const inferenceStreamDeltaEventSchema = z.object({
19
+ /** See `version.ts`: each stream event is a whole message on the wire. */
20
+ schemaVersion: z.literal(1),
21
+ type: z.literal('delta'),
22
+ requestId: requestIdSchema,
23
+ sequence: z.number().int().nonnegative().safe(),
24
+ /** Which output of a multi-output response this chunk belongs to. */
25
+ outputIndex: z.number().int().nonnegative().safe(),
26
+ /**
27
+ * `output_audio_transcript` (contract set 3.2.0) carries the words of spoken
28
+ * output requested with `audioOutput`. It is its own channel for the same
29
+ * reason `reasoning` is: a renderer that shows it as `output_text` presents
30
+ * the transcript of what the customer is hearing as a second, written answer.
31
+ */
32
+ channel: z.enum(['output_text', 'reasoning', 'refusal', 'output_audio_transcript']),
33
+ text: z.string(),
34
+ });
35
+ /** The encodings an audio output may carry. */
36
+ export const inferenceAudioMediaTypeSchema = z.enum([
37
+ 'audio/mpeg',
38
+ 'audio/wav',
39
+ 'audio/ogg',
40
+ 'audio/aac',
41
+ 'audio/flac',
42
+ 'audio/pcm',
43
+ ]);
44
+ /** The most bytes one folded audio output may hold, at the edge and in the SDK. */
45
+ export const MAX_INFERENCE_AUDIO_BYTES = 20 * 1024 * 1024;
46
+ /** A bounded, independently base64-encoded chunk of one audio output. */
47
+ export const inferenceStreamAudioEventSchema = z.object({
48
+ schemaVersion: z.literal(1),
49
+ type: z.literal('audio'),
50
+ requestId: requestIdSchema,
51
+ sequence: z.number().int().nonnegative().safe(),
52
+ outputIndex: z.number().int().nonnegative().safe(),
53
+ mediaType: inferenceAudioMediaTypeSchema,
54
+ data: z.string().min(4).max(65536).regex(PADDED_BASE64_PATTERN),
55
+ });
56
+ /**
57
+ * A tool call being streamed.
58
+ *
59
+ * `argumentsDelta` accumulates; `complete` marks the call finished so a client
60
+ * knows when the accumulated JSON text is worth parsing.
61
+ */
62
+ export const inferenceStreamToolCallEventSchema = z.object({
63
+ /** See `version.ts`: each stream event is a whole message on the wire. */
64
+ schemaVersion: z.literal(1),
65
+ type: z.literal('tool_call'),
66
+ requestId: requestIdSchema,
67
+ sequence: z.number().int().nonnegative().safe(),
68
+ toolCallId: z.string().min(1).max(128),
69
+ /** Present on the first event of a call. */
70
+ name: z.string().min(1).max(128).optional(),
71
+ argumentsDelta: z.string().optional(),
72
+ complete: z.boolean(),
73
+ });
74
+ /**
75
+ * Why generation stopped.
76
+ *
77
+ * `refusal` and `content_filter` are separate members because they are separate
78
+ * events: the MODEL declining to answer is a property of the answer, while a
79
+ * filter is an upstream system removing one. The delta channels already carry
80
+ * that distinction (`channel: 'refusal'` beside the filter's own error code),
81
+ * so collapsing it here would have made the terminal event less specific than
82
+ * the stream that produced it.
83
+ */
84
+ export const inferenceFinishReasonSchema = z.enum([
85
+ 'stop',
86
+ 'length',
87
+ 'tool_calls',
88
+ 'content_filter',
89
+ 'refusal',
90
+ 'cancelled',
91
+ ]);
92
+ /**
93
+ * The successful terminal event.
94
+ *
95
+ * `receiptId` is present once settlement has produced one, giving a customer a
96
+ * direct handle on the exact amount charged rather than a telemetry estimate.
97
+ */
98
+ export const inferenceStreamDoneEventSchema = z.object({
99
+ /** See `version.ts`: each stream event is a whole message on the wire. */
100
+ schemaVersion: z.literal(1),
101
+ type: z.literal('done'),
102
+ requestId: requestIdSchema,
103
+ sequence: z.number().int().nonnegative().safe(),
104
+ generationId: generationIdSchema.optional(),
105
+ finishReason: inferenceFinishReasonSchema,
106
+ receiptId: z.string().min(1).max(128).optional(),
107
+ completedAt: inferenceTimestampSchema,
108
+ });
109
+ /* -------------------------------------------------------------------------- */
110
+ /* The public response stream (`POST /v1/responses` with `stream: true`) */
111
+ /* -------------------------------------------------------------------------- */
112
+ /**
113
+ * The first event of a public stream: the request id and the revision that is
114
+ * serving it.
115
+ *
116
+ * Kaana moves a request (retry, failover, the next grant step) only before any
117
+ * output is delivered, so the model named here is the one that produced every
118
+ * event after it; a public stream therefore needs no route-switch event, and it
119
+ * never names a provider or a deployment.
120
+ */
121
+ export const responseStreamStartEventSchema = z.object({
122
+ schemaVersion: z.literal(1),
123
+ type: z.literal('start'),
124
+ requestId: requestIdSchema,
125
+ sequence: z.number().int().nonnegative().safe(),
126
+ generationId: generationIdSchema.optional(),
127
+ /** The revision that is serving the request. */
128
+ model: modelReferenceSchema,
129
+ startedAt: inferenceTimestampSchema,
130
+ });
131
+ /** What the request consumed, as measured. Units only, never money. */
132
+ export const responseStreamUsageEventSchema = z.object({
133
+ schemaVersion: z.literal(1),
134
+ type: z.literal('usage'),
135
+ requestId: requestIdSchema,
136
+ sequence: z.number().int().nonnegative().safe(),
137
+ units: z.array(usageQuantitySchema).min(1),
138
+ usageSource: usageSourceSchema,
139
+ });
140
+ /** The terminal failure of a public stream, after which nothing follows. */
141
+ export const responseStreamErrorEventSchema = z.object({
142
+ schemaVersion: z.literal(1),
143
+ type: z.literal('error'),
144
+ requestId: requestIdSchema,
145
+ sequence: z.number().int().nonnegative().safe(),
146
+ error: publicErrorSchema,
147
+ });
148
+ /** Every event of a public response stream. */
149
+ export const responseStreamEventSchema = z.discriminatedUnion('type', [
150
+ responseStreamStartEventSchema,
151
+ inferenceStreamDeltaEventSchema,
152
+ inferenceStreamAudioEventSchema,
153
+ inferenceStreamToolCallEventSchema,
154
+ responseStreamUsageEventSchema,
155
+ responseStreamErrorEventSchema,
156
+ inferenceStreamDoneEventSchema,
157
+ ]);