@kaana.ai/contracts 0.0.0-stage → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +673 -0
- package/NOTICE +21 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +29 -0
- package/dist/internal/attribution.d.ts +116 -0
- package/dist/internal/attribution.js +82 -0
- package/dist/internal/canonical.d.ts +31 -0
- package/dist/internal/canonical.js +57 -0
- package/dist/internal/catalogue.d.ts +1059 -0
- package/dist/internal/catalogue.js +389 -0
- package/dist/internal/credentialControl.d.ts +329 -0
- package/dist/internal/credentialControl.js +256 -0
- package/dist/internal/decisions.d.ts +245 -0
- package/dist/internal/decisions.js +69 -0
- package/dist/internal/economics.d.ts +27 -0
- package/dist/internal/economics.js +26 -0
- package/dist/internal/embeddings.d.ts +164 -0
- package/dist/internal/embeddings.js +59 -0
- package/dist/internal/errors.d.ts +185 -0
- package/dist/internal/errors.js +211 -0
- package/dist/internal/grant.d.ts +699 -0
- package/dist/internal/grant.js +445 -0
- package/dist/internal/identifiers.d.ts +63 -0
- package/dist/internal/identifiers.js +65 -0
- package/dist/internal/privateAutoExecution.d.ts +158 -0
- package/dist/internal/privateAutoExecution.js +188 -0
- package/dist/internal/realtime.d.ts +1005 -0
- package/dist/internal/realtime.js +187 -0
- package/dist/internal/request.d.ts +1079 -0
- package/dist/internal/request.js +404 -0
- package/dist/internal/routing.d.ts +90 -0
- package/dist/internal/routing.js +139 -0
- package/dist/internal/scopedExecution.d.ts +74 -0
- package/dist/internal/scopedExecution.js +61 -0
- package/dist/internal/settlement.d.ts +205 -0
- package/dist/internal/settlement.js +71 -0
- package/dist/internal/streamEvents.d.ts +422 -0
- package/dist/internal/streamEvents.js +171 -0
- package/dist/internal/usage.d.ts +127 -0
- package/dist/internal/usage.js +109 -0
- package/dist/internal.d.ts +30 -0
- package/dist/internal.js +30 -0
- package/dist/public/audio.d.ts +347 -0
- package/dist/public/audio.js +236 -0
- package/dist/public/audioFormats.d.ts +64 -0
- package/dist/public/audioFormats.js +60 -0
- package/dist/public/catalogue.d.ts +914 -0
- package/dist/public/catalogue.js +516 -0
- package/dist/public/chatCompletions.d.ts +381 -0
- package/dist/public/chatCompletions.js +321 -0
- package/dist/public/decisions.d.ts +182 -0
- package/dist/public/decisions.js +257 -0
- package/dist/public/embeddings.d.ts +34 -0
- package/dist/public/embeddings.js +66 -0
- package/dist/public/errors.d.ts +195 -0
- package/dist/public/errors.js +274 -0
- package/dist/public/generations.d.ts +330 -0
- package/dist/public/generations.js +121 -0
- package/dist/public/identifiers.d.ts +139 -0
- package/dist/public/identifiers.js +160 -0
- package/dist/public/images.d.ts +36 -0
- package/dist/public/images.js +36 -0
- package/dist/public/messages.d.ts +297 -0
- package/dist/public/messages.js +251 -0
- package/dist/public/money.d.ts +308 -0
- package/dist/public/money.js +237 -0
- package/dist/public/policy.d.ts +19 -0
- package/dist/public/policy.js +20 -0
- package/dist/public/realtime.d.ts +2006 -0
- package/dist/public/realtime.js +797 -0
- package/dist/public/responses.d.ts +253 -0
- package/dist/public/responses.js +95 -0
- package/dist/public/streamEvents.d.ts +372 -0
- package/dist/public/streamEvents.js +157 -0
- package/dist/public/version.d.ts +184 -0
- package/dist/public/version.js +184 -0
- package/openapi.json +6967 -0
- package/package.json +44 -4
- package/README.md +0 -3
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The signed realtime session request and the server events that name the
|
|
3
|
+
* serving provider and deployment.
|
|
4
|
+
*
|
|
5
|
+
* Moved from `@oxy.so/contracts` 4.12.0 (`src/inference/realtime.ts`) by Kaana ADR 0002.
|
|
6
|
+
*/
|
|
7
|
+
import { z } from 'zod';
|
|
8
|
+
import { realtimeSessionKindSchema, realtimeSessionTransportSchema } from '../public/catalogue.js';
|
|
9
|
+
import { inferenceTimestampSchema, modelReferenceSchema, requestIdSchema, } from '../public/identifiers.js';
|
|
10
|
+
import { usageQuantitySchema, usageSourceSchema } from '../public/money.js';
|
|
11
|
+
import { routingPolicyReferenceSchema } from '../public/policy.js';
|
|
12
|
+
import { inferenceFinishReasonSchema } from '../public/streamEvents.js';
|
|
13
|
+
import { MAX_REALTIME_RESUME_WINDOW_MS, realtimeCommandAcceptedEventSchema, realtimeCommandIdSchema, realtimeConfigIssues, realtimeInputAudioClearedEventSchema, realtimeInputAudioCommittedEventSchema, realtimeItemAddedEventSchema, realtimeItemDeletedEventSchema, realtimeItemDoneEventSchema, realtimeItemTruncatedEventSchema, realtimeOutputAudioDeltaEventSchema, realtimeOutputAudioDoneEventSchema, realtimeResponseCreatedEventSchema, realtimeResponseIdSchema, realtimeResponseStatusSchema, realtimeSessionCloseReasonSchema, realtimeSessionConfigSchema, realtimeSessionLimitsSchema, realtimeSessionResumedEventSchema, realtimeSessionUpdatedEventSchema, realtimeSpeechStartedEventSchema, realtimeSpeechStoppedEventSchema, realtimeTextDeltaEventSchema, realtimeToolCallEventSchema, realtimeTranscriptDeltaEventSchema, realtimeTranscriptDoneEventSchema, } from '../public/realtime.js';
|
|
14
|
+
import { inferenceAttributionSchema } from './attribution.js';
|
|
15
|
+
import { inferenceErrorSchema } from './errors.js';
|
|
16
|
+
import { deploymentIdSchema, inferenceProviderSlugSchema } from './identifiers.js';
|
|
17
|
+
import { authorizedRouteSchema } from './routing.js';
|
|
18
|
+
const sequenceSchema = z.number().int().nonnegative().safe();
|
|
19
|
+
/**
|
|
20
|
+
* What the edge records about the session CALL. Strict for the reason
|
|
21
|
+
* `clientRequestMetadataSchema` is: this object is where an IP would be added.
|
|
22
|
+
*/
|
|
23
|
+
export const realtimeClientMetadataSchema = z
|
|
24
|
+
.object({
|
|
25
|
+
/** The public path, e.g. `/v1/realtime`. */
|
|
26
|
+
endpoint: z.string().min(1).max(256),
|
|
27
|
+
clientSessionId: z.string().min(1).max(128).optional(),
|
|
28
|
+
receivedAt: inferenceTimestampSchema,
|
|
29
|
+
labels: z.record(z.string(), z.string().max(256)).optional(),
|
|
30
|
+
})
|
|
31
|
+
.strict();
|
|
32
|
+
const modelLineOf = (reference) => {
|
|
33
|
+
const at = reference.indexOf('@');
|
|
34
|
+
return at === -1 ? reference : reference.slice(0, at);
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* The signed request that opens a session: the first frame of its connection.
|
|
38
|
+
*/
|
|
39
|
+
export const realtimeSessionRequestSchema = z
|
|
40
|
+
.object({
|
|
41
|
+
schemaVersion: z.literal(1),
|
|
42
|
+
attribution: inferenceAttributionSchema,
|
|
43
|
+
/** The model the caller named, pinned or not. Never a routing profile. */
|
|
44
|
+
modelReference: modelReferenceSchema,
|
|
45
|
+
kind: realtimeSessionKindSchema,
|
|
46
|
+
transport: realtimeSessionTransportSchema,
|
|
47
|
+
config: realtimeSessionConfigSchema,
|
|
48
|
+
limits: realtimeSessionLimitsSchema,
|
|
49
|
+
client: realtimeClientMetadataSchema,
|
|
50
|
+
routingPolicy: routingPolicyReferenceSchema,
|
|
51
|
+
/** In preference order, tried only until one opens. All `same_model`. */
|
|
52
|
+
authorizedRoutes: z.array(authorizedRouteSchema).min(1),
|
|
53
|
+
})
|
|
54
|
+
.superRefine((request, ctx) => {
|
|
55
|
+
const { config } = request;
|
|
56
|
+
const issue = (path, message) => ctx.addIssue({ code: 'custom', path, message });
|
|
57
|
+
if (request.limits.idleTimeoutMs > request.limits.maxDurationMs) {
|
|
58
|
+
issue(['limits', 'idleTimeoutMs'], 'an idle timeout cannot outlast the session');
|
|
59
|
+
}
|
|
60
|
+
for (const found of realtimeConfigIssues(request.kind, config)) {
|
|
61
|
+
issue(['config', ...found.path], found.message);
|
|
62
|
+
}
|
|
63
|
+
// A session is never substituted: the conversation it holds belongs to one
|
|
64
|
+
// model, so every route serves the line the caller named, and a pinned
|
|
65
|
+
// request is served on exactly the revision it pinned.
|
|
66
|
+
const line = modelLineOf(request.modelReference);
|
|
67
|
+
const pinned = request.modelReference.includes('@');
|
|
68
|
+
for (const [index, route] of request.authorizedRoutes.entries()) {
|
|
69
|
+
if (route.substitution !== 'same_model' || modelLineOf(route.modelReference) !== line) {
|
|
70
|
+
issue(['authorizedRoutes', index], 'every route of a session serves the model it named');
|
|
71
|
+
}
|
|
72
|
+
if (pinned && route.modelReference !== request.modelReference) {
|
|
73
|
+
issue(['authorizedRoutes', index, 'modelReference'], 'a pinned session is served on exactly the revision it pinned');
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const deployments = request.authorizedRoutes.map((route) => route.deploymentId);
|
|
77
|
+
if (new Set(deployments).size !== deployments.length) {
|
|
78
|
+
issue(['authorizedRoutes'], 'each deployment appears at most once in the authorized route list');
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
const eventBase = {
|
|
82
|
+
/** See `version.ts`: each event is a whole message on the wire. */
|
|
83
|
+
schemaVersion: z.literal(1),
|
|
84
|
+
requestId: requestIdSchema,
|
|
85
|
+
/** One monotonic sequence per session, from 0; what makes a replay detectable. */
|
|
86
|
+
sequence: sequenceSchema,
|
|
87
|
+
/** The command this event answers, when it answers one. */
|
|
88
|
+
commandId: realtimeCommandIdSchema.optional(),
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* The session opened. Names the revision-pinned model, the serving provider and
|
|
92
|
+
* the exact deployment — fixed for the rest of the session — and the effective
|
|
93
|
+
* configuration and limits. `deploymentId` is for settlement: like the usage
|
|
94
|
+
* event's, it is not rendered to the customer.
|
|
95
|
+
*/
|
|
96
|
+
export const realtimeSessionCreatedEventSchema = z.object({
|
|
97
|
+
...eventBase,
|
|
98
|
+
type: z.literal('session.created'),
|
|
99
|
+
resolvedModelReference: modelReferenceSchema,
|
|
100
|
+
servingProvider: inferenceProviderSlugSchema,
|
|
101
|
+
deploymentId: deploymentIdSchema,
|
|
102
|
+
kind: realtimeSessionKindSchema,
|
|
103
|
+
config: realtimeSessionConfigSchema,
|
|
104
|
+
limits: realtimeSessionLimitsSchema,
|
|
105
|
+
resumeWindowMs: z.number().int().nonnegative().max(MAX_REALTIME_RESUME_WINDOW_MS),
|
|
106
|
+
startedAt: inferenceTimestampSchema,
|
|
107
|
+
expiresAt: inferenceTimestampSchema,
|
|
108
|
+
});
|
|
109
|
+
/**
|
|
110
|
+
* A response ended, with the units it consumed — measurement evidence, as on
|
|
111
|
+
* the one-shot usage event. A cancelled or failed response still consumed what
|
|
112
|
+
* it consumed, so its units are reported too.
|
|
113
|
+
*/
|
|
114
|
+
export const realtimeResponseDoneEventSchema = z.object({
|
|
115
|
+
...eventBase,
|
|
116
|
+
type: z.literal('response.done'),
|
|
117
|
+
responseId: realtimeResponseIdSchema,
|
|
118
|
+
status: realtimeResponseStatusSchema,
|
|
119
|
+
finishReason: inferenceFinishReasonSchema.optional(),
|
|
120
|
+
deploymentId: deploymentIdSchema,
|
|
121
|
+
units: z.array(usageQuantitySchema),
|
|
122
|
+
usageSource: usageSourceSchema,
|
|
123
|
+
});
|
|
124
|
+
/**
|
|
125
|
+
* An error. `fatal: false` refuses one command and the session continues;
|
|
126
|
+
* `fatal: true` is followed by `session.closed` and nothing else.
|
|
127
|
+
*/
|
|
128
|
+
export const realtimeErrorEventSchema = z.object({
|
|
129
|
+
...eventBase,
|
|
130
|
+
type: z.literal('error'),
|
|
131
|
+
fatal: z.boolean(),
|
|
132
|
+
error: inferenceErrorSchema,
|
|
133
|
+
});
|
|
134
|
+
/**
|
|
135
|
+
* The terminal event. Carries the session's total units; `deploymentId` is
|
|
136
|
+
* absent only when no route ever opened, in which case nothing was consumed.
|
|
137
|
+
*/
|
|
138
|
+
export const realtimeSessionClosedEventSchema = z.object({
|
|
139
|
+
...eventBase,
|
|
140
|
+
type: z.literal('session.closed'),
|
|
141
|
+
reason: realtimeSessionCloseReasonSchema,
|
|
142
|
+
deploymentId: deploymentIdSchema.optional(),
|
|
143
|
+
units: z.array(usageQuantitySchema),
|
|
144
|
+
usageSource: usageSourceSchema,
|
|
145
|
+
closedAt: inferenceTimestampSchema,
|
|
146
|
+
});
|
|
147
|
+
/** Every event a session can emit. */
|
|
148
|
+
export const realtimeServerEventSchema = z
|
|
149
|
+
.discriminatedUnion('type', [
|
|
150
|
+
realtimeSessionCreatedEventSchema,
|
|
151
|
+
realtimeSessionUpdatedEventSchema,
|
|
152
|
+
realtimeSessionResumedEventSchema,
|
|
153
|
+
realtimeCommandAcceptedEventSchema,
|
|
154
|
+
realtimeItemAddedEventSchema,
|
|
155
|
+
realtimeItemDoneEventSchema,
|
|
156
|
+
realtimeItemDeletedEventSchema,
|
|
157
|
+
realtimeItemTruncatedEventSchema,
|
|
158
|
+
realtimeSpeechStartedEventSchema,
|
|
159
|
+
realtimeSpeechStoppedEventSchema,
|
|
160
|
+
realtimeInputAudioCommittedEventSchema,
|
|
161
|
+
realtimeInputAudioClearedEventSchema,
|
|
162
|
+
realtimeResponseCreatedEventSchema,
|
|
163
|
+
realtimeOutputAudioDeltaEventSchema,
|
|
164
|
+
realtimeOutputAudioDoneEventSchema,
|
|
165
|
+
realtimeTranscriptDeltaEventSchema,
|
|
166
|
+
realtimeTranscriptDoneEventSchema,
|
|
167
|
+
realtimeTextDeltaEventSchema,
|
|
168
|
+
realtimeToolCallEventSchema,
|
|
169
|
+
realtimeResponseDoneEventSchema,
|
|
170
|
+
realtimeErrorEventSchema,
|
|
171
|
+
realtimeSessionClosedEventSchema,
|
|
172
|
+
])
|
|
173
|
+
.superRefine((event, ctx) => {
|
|
174
|
+
// Each unit is reported once, as a total — the rule every usage record in
|
|
175
|
+
// this contract holds. Checked on the union because a discriminated union's
|
|
176
|
+
// options must stay plain objects.
|
|
177
|
+
if (event.type !== 'response.done' && event.type !== 'session.closed')
|
|
178
|
+
return;
|
|
179
|
+
const units = event.units.map((quantity) => quantity.unit);
|
|
180
|
+
if (new Set(units).size !== units.length) {
|
|
181
|
+
ctx.addIssue({
|
|
182
|
+
code: 'custom',
|
|
183
|
+
path: ['units'],
|
|
184
|
+
message: 'each unit is reported at most once',
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
});
|