@oxyhq/contracts 0.26.0 → 0.28.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/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/accountGraph.js +4 -3
- package/dist/cjs/index.js +186 -1
- package/dist/cjs/inference/accountBilling.js +334 -0
- package/dist/cjs/inference/attribution.js +106 -0
- package/dist/cjs/inference/catalogue.js +482 -0
- package/dist/cjs/inference/entitlement.js +217 -0
- package/dist/cjs/inference/errors.js +210 -0
- package/dist/cjs/inference/identifiers.js +197 -0
- package/dist/cjs/inference/money.js +188 -0
- package/dist/cjs/inference/priceVersion.js +110 -0
- package/dist/cjs/inference/providerConnection.js +142 -0
- package/dist/cjs/inference/request.js +288 -0
- package/dist/cjs/inference/routingPolicy.js +213 -0
- package/dist/cjs/inference/streamEvents.js +219 -0
- package/dist/cjs/inference/usage.js +297 -0
- package/dist/cjs/inference/version.js +85 -0
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/accountGraph.js +4 -3
- package/dist/esm/index.js +54 -0
- package/dist/esm/inference/accountBilling.js +331 -0
- package/dist/esm/inference/attribution.js +103 -0
- package/dist/esm/inference/catalogue.js +479 -0
- package/dist/esm/inference/entitlement.js +214 -0
- package/dist/esm/inference/errors.js +207 -0
- package/dist/esm/inference/identifiers.js +194 -0
- package/dist/esm/inference/money.js +185 -0
- package/dist/esm/inference/priceVersion.js +107 -0
- package/dist/esm/inference/providerConnection.js +139 -0
- package/dist/esm/inference/request.js +285 -0
- package/dist/esm/inference/routingPolicy.js +210 -0
- package/dist/esm/inference/streamEvents.js +216 -0
- package/dist/esm/inference/usage.js +294 -0
- package/dist/esm/inference/version.js +82 -0
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/accountGraph.d.ts +6 -5
- package/dist/types/index.d.ts +27 -0
- package/dist/types/inference/accountBilling.d.ts +738 -0
- package/dist/types/inference/attribution.d.ts +176 -0
- package/dist/types/inference/catalogue.d.ts +1612 -0
- package/dist/types/inference/entitlement.d.ts +519 -0
- package/dist/types/inference/errors.d.ts +206 -0
- package/dist/types/inference/identifiers.d.ts +157 -0
- package/dist/types/inference/money.d.ts +185 -0
- package/dist/types/inference/priceVersion.d.ts +182 -0
- package/dist/types/inference/providerConnection.d.ts +297 -0
- package/dist/types/inference/request.d.ts +2364 -0
- package/dist/types/inference/routingPolicy.d.ts +426 -0
- package/dist/types/inference/streamEvents.d.ts +906 -0
- package/dist/types/inference/usage.d.ts +1139 -0
- package/dist/types/inference/version.d.ts +82 -0
- package/package.json +1 -1
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inference errors and retryability.
|
|
3
|
+
*
|
|
4
|
+
* One closed set of codes, shared by the Oxy public edge, the data plane and
|
|
5
|
+
* every SDK.
|
|
6
|
+
* Closed because the alternative — a free-form `code` string — makes a client's
|
|
7
|
+
* error handling a guess about the producer's spelling, and makes "is this
|
|
8
|
+
* worth retrying" a decision every consumer re-derives from prose.
|
|
9
|
+
*
|
|
10
|
+
* Retryability is carried explicitly and is CONSTRAINED by the code: a code
|
|
11
|
+
* that can never succeed on a bare retry (an invalid request, a denied
|
|
12
|
+
* permission, an insufficient balance) cannot claim `retryable: true`. Without
|
|
13
|
+
* that constraint the field is advisory, and one producer setting it optimistically
|
|
14
|
+
* turns every client into a retry storm against a request that will never pass.
|
|
15
|
+
*
|
|
16
|
+
* The provider passthrough exists so a customer can see what the upstream said
|
|
17
|
+
* without Oxy having to interpret every provider's error vocabulary — but it is
|
|
18
|
+
* the single most likely place for an upstream credential to escape, because
|
|
19
|
+
* provider errors routinely echo the request that caused them. It is therefore
|
|
20
|
+
* a `.strict()` object of four fields with no room for headers or a request
|
|
21
|
+
* body, and its free text is refused if it looks like it contains a credential.
|
|
22
|
+
*
|
|
23
|
+
* Decided in: docs/adr/0010-public-api-compatibility.md.
|
|
24
|
+
*/
|
|
25
|
+
import { z } from 'zod';
|
|
26
|
+
/**
|
|
27
|
+
* The closed set of inference error codes.
|
|
28
|
+
*
|
|
29
|
+
* Grouped by who must act: the caller (`invalid_request` … `idempotency_conflict`),
|
|
30
|
+
* the account owner (`insufficient_balance`, `spending_limit_exceeded`,
|
|
31
|
+
* `quota_exceeded`, `byok_credential_invalid`), routing/permission policy
|
|
32
|
+
* (`policy_violation`, `commercial_permission_denied`, `no_route_available`),
|
|
33
|
+
* and the platform or its upstreams (everything from `deployment_unavailable`).
|
|
34
|
+
*
|
|
35
|
+
* The platform group is NOT uniformly retryable, and that is the point of
|
|
36
|
+
* `provider_credential_invalid` sitting in it: an upstream that refuses the
|
|
37
|
+
* PLATFORM's own credential fails every identical retry until an operator
|
|
38
|
+
* rotates a key, so classifying it as `provider_error` would send every client
|
|
39
|
+
* into a retry loop against a request that cannot succeed.
|
|
40
|
+
*/
|
|
41
|
+
export declare const INFERENCE_ERROR_CODES: readonly ["invalid_request", "authentication_failed", "permission_denied", "insufficient_scope", "model_not_found", "unsupported_modality", "context_length_exceeded", "request_too_large", "output_limit_exceeded", "idempotency_conflict", "insufficient_balance", "spending_limit_exceeded", "quota_exceeded", "byok_credential_invalid", "policy_violation", "commercial_permission_denied", "no_route_available", "upstream_content_filtered", "cancelled", "rate_limited", "deployment_unavailable", "provider_error", "provider_timeout", "provider_overloaded", "provider_credential_invalid", "service_unavailable", "internal_error"];
|
|
42
|
+
export declare const inferenceErrorCodeSchema: z.ZodEnum<["invalid_request", "authentication_failed", "permission_denied", "insufficient_scope", "model_not_found", "unsupported_modality", "context_length_exceeded", "request_too_large", "output_limit_exceeded", "idempotency_conflict", "insufficient_balance", "spending_limit_exceeded", "quota_exceeded", "byok_credential_invalid", "policy_violation", "commercial_permission_denied", "no_route_available", "upstream_content_filtered", "cancelled", "rate_limited", "deployment_unavailable", "provider_error", "provider_timeout", "provider_overloaded", "provider_credential_invalid", "service_unavailable", "internal_error"]>;
|
|
43
|
+
/**
|
|
44
|
+
* Codes for which an identical retried request cannot succeed.
|
|
45
|
+
*
|
|
46
|
+
* `rate_limited` and `quota_exceeded` sit on opposite sides of this line
|
|
47
|
+
* deliberately: a rate limit clears on its own within the window the response
|
|
48
|
+
* names, while a quota is an account-level ceiling that only a human raises.
|
|
49
|
+
* `cancelled` is here because the caller already withdrew the request; a client
|
|
50
|
+
* that retries it is contradicting its own cancellation.
|
|
51
|
+
*
|
|
52
|
+
* `byok_credential_invalid` and `provider_credential_invalid` are the same
|
|
53
|
+
* failure seen from the two sides of the BYOK boundary — the customer's own
|
|
54
|
+
* upstream credential and the platform's — and they are two codes rather than
|
|
55
|
+
* one because only the first names an action the customer can take. Both are
|
|
56
|
+
* non-retryable for the same reason: a credential an upstream has refused keeps
|
|
57
|
+
* being refused until somebody replaces it.
|
|
58
|
+
*/
|
|
59
|
+
export declare const NON_RETRYABLE_INFERENCE_ERROR_CODES: readonly ["invalid_request", "authentication_failed", "permission_denied", "insufficient_scope", "model_not_found", "unsupported_modality", "context_length_exceeded", "request_too_large", "output_limit_exceeded", "idempotency_conflict", "insufficient_balance", "spending_limit_exceeded", "quota_exceeded", "byok_credential_invalid", "policy_violation", "commercial_permission_denied", "no_route_available", "upstream_content_filtered", "cancelled", "provider_credential_invalid"];
|
|
60
|
+
/**
|
|
61
|
+
* Free text that is safe to hand a customer: bounded, and refused outright if a
|
|
62
|
+
* credential marker appears in it. Applied to BOTH the Oxy message and the
|
|
63
|
+
* upstream one — a leak is no less a leak for having been written by a provider.
|
|
64
|
+
*/
|
|
65
|
+
export declare const safeErrorTextSchema: z.ZodEffects<z.ZodString, string, string>;
|
|
66
|
+
/**
|
|
67
|
+
* A coarse classification of an upstream failure (ADR 0010's `upstreamCategory`).
|
|
68
|
+
*
|
|
69
|
+
* Distinct from {@link providerErrorPassthroughSchema}, which carries the
|
|
70
|
+
* upstream's OWN code and text: this is Oxy's reading of what kind of failure it
|
|
71
|
+
* was, in a vocabulary that is the same across every provider, so a client can
|
|
72
|
+
* branch on it without knowing who served the request.
|
|
73
|
+
*/
|
|
74
|
+
export declare const upstreamErrorCategorySchema: z.ZodEnum<["rate_limit", "quota", "timeout", "overloaded", "server_error", "content_filter", "invalid_request", "authentication", "unknown"]>;
|
|
75
|
+
/**
|
|
76
|
+
* What the upstream provider said, reduced to the four fields a customer can
|
|
77
|
+
* act on.
|
|
78
|
+
*
|
|
79
|
+
* `.strict()` is the security control here, not a tidiness preference: it means
|
|
80
|
+
* a producer cannot widen this by attaching `requestHeaders`, `curl`, `body` or
|
|
81
|
+
* `raw` and have it silently pass. Adding a field is a contract change with a
|
|
82
|
+
* version bump and a review, which is the point.
|
|
83
|
+
*/
|
|
84
|
+
export declare const providerErrorPassthroughSchema: z.ZodObject<{
|
|
85
|
+
provider: z.ZodString;
|
|
86
|
+
/** The upstream HTTP status, when the upstream spoke HTTP. */
|
|
87
|
+
status: z.ZodOptional<z.ZodNumber>;
|
|
88
|
+
/** The upstream's own error code, verbatim and uninterpreted. */
|
|
89
|
+
code: z.ZodOptional<z.ZodString>;
|
|
90
|
+
/** The upstream's message, subject to the same credential refusal. */
|
|
91
|
+
message: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
|
|
92
|
+
}, "strict", z.ZodTypeAny, {
|
|
93
|
+
provider: string;
|
|
94
|
+
code?: string | undefined;
|
|
95
|
+
message?: string | undefined;
|
|
96
|
+
status?: number | undefined;
|
|
97
|
+
}, {
|
|
98
|
+
provider: string;
|
|
99
|
+
code?: string | undefined;
|
|
100
|
+
message?: string | undefined;
|
|
101
|
+
status?: number | undefined;
|
|
102
|
+
}>;
|
|
103
|
+
/**
|
|
104
|
+
* The error body every inference surface returns and every stream error event
|
|
105
|
+
* carries.
|
|
106
|
+
*
|
|
107
|
+
* `requestId` is always present — an error a customer cannot correlate with a
|
|
108
|
+
* log line is an error they have to reproduce to report.
|
|
109
|
+
*/
|
|
110
|
+
export declare const inferenceErrorSchema: z.ZodEffects<z.ZodObject<{
|
|
111
|
+
/** See `version.ts`: this shape appears alone on the wire, so it is versioned. */
|
|
112
|
+
schemaVersion: z.ZodLiteral<1>;
|
|
113
|
+
code: z.ZodEnum<["invalid_request", "authentication_failed", "permission_denied", "insufficient_scope", "model_not_found", "unsupported_modality", "context_length_exceeded", "request_too_large", "output_limit_exceeded", "idempotency_conflict", "insufficient_balance", "spending_limit_exceeded", "quota_exceeded", "byok_credential_invalid", "policy_violation", "commercial_permission_denied", "no_route_available", "upstream_content_filtered", "cancelled", "rate_limited", "deployment_unavailable", "provider_error", "provider_timeout", "provider_overloaded", "provider_credential_invalid", "service_unavailable", "internal_error"]>;
|
|
114
|
+
message: z.ZodEffects<z.ZodString, string, string>;
|
|
115
|
+
retryable: z.ZodBoolean;
|
|
116
|
+
requestId: z.ZodString;
|
|
117
|
+
/** How long to wait before retrying. Only meaningful when `retryable`. */
|
|
118
|
+
retryAfterMs: z.ZodOptional<z.ZodNumber>;
|
|
119
|
+
/** The request field at fault, for `invalid_request`. */
|
|
120
|
+
param: z.ZodOptional<z.ZodString>;
|
|
121
|
+
/** Present only when an upstream provider was reached and failed. */
|
|
122
|
+
upstreamCategory: z.ZodOptional<z.ZodEnum<["rate_limit", "quota", "timeout", "overloaded", "server_error", "content_filter", "invalid_request", "authentication", "unknown"]>>;
|
|
123
|
+
providerError: z.ZodOptional<z.ZodObject<{
|
|
124
|
+
provider: z.ZodString;
|
|
125
|
+
/** The upstream HTTP status, when the upstream spoke HTTP. */
|
|
126
|
+
status: z.ZodOptional<z.ZodNumber>;
|
|
127
|
+
/** The upstream's own error code, verbatim and uninterpreted. */
|
|
128
|
+
code: z.ZodOptional<z.ZodString>;
|
|
129
|
+
/** The upstream's message, subject to the same credential refusal. */
|
|
130
|
+
message: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
|
|
131
|
+
}, "strict", z.ZodTypeAny, {
|
|
132
|
+
provider: string;
|
|
133
|
+
code?: string | undefined;
|
|
134
|
+
message?: string | undefined;
|
|
135
|
+
status?: number | undefined;
|
|
136
|
+
}, {
|
|
137
|
+
provider: string;
|
|
138
|
+
code?: string | undefined;
|
|
139
|
+
message?: string | undefined;
|
|
140
|
+
status?: number | undefined;
|
|
141
|
+
}>>;
|
|
142
|
+
}, "strip", z.ZodTypeAny, {
|
|
143
|
+
code: "invalid_request" | "authentication_failed" | "permission_denied" | "insufficient_scope" | "model_not_found" | "unsupported_modality" | "context_length_exceeded" | "request_too_large" | "output_limit_exceeded" | "idempotency_conflict" | "insufficient_balance" | "spending_limit_exceeded" | "quota_exceeded" | "byok_credential_invalid" | "policy_violation" | "commercial_permission_denied" | "no_route_available" | "upstream_content_filtered" | "cancelled" | "rate_limited" | "deployment_unavailable" | "provider_error" | "provider_timeout" | "provider_overloaded" | "provider_credential_invalid" | "service_unavailable" | "internal_error";
|
|
144
|
+
message: string;
|
|
145
|
+
requestId: string;
|
|
146
|
+
schemaVersion: 1;
|
|
147
|
+
retryable: boolean;
|
|
148
|
+
retryAfterMs?: number | undefined;
|
|
149
|
+
param?: string | undefined;
|
|
150
|
+
upstreamCategory?: "unknown" | "authentication" | "invalid_request" | "rate_limit" | "quota" | "timeout" | "overloaded" | "server_error" | "content_filter" | undefined;
|
|
151
|
+
providerError?: {
|
|
152
|
+
provider: string;
|
|
153
|
+
code?: string | undefined;
|
|
154
|
+
message?: string | undefined;
|
|
155
|
+
status?: number | undefined;
|
|
156
|
+
} | undefined;
|
|
157
|
+
}, {
|
|
158
|
+
code: "invalid_request" | "authentication_failed" | "permission_denied" | "insufficient_scope" | "model_not_found" | "unsupported_modality" | "context_length_exceeded" | "request_too_large" | "output_limit_exceeded" | "idempotency_conflict" | "insufficient_balance" | "spending_limit_exceeded" | "quota_exceeded" | "byok_credential_invalid" | "policy_violation" | "commercial_permission_denied" | "no_route_available" | "upstream_content_filtered" | "cancelled" | "rate_limited" | "deployment_unavailable" | "provider_error" | "provider_timeout" | "provider_overloaded" | "provider_credential_invalid" | "service_unavailable" | "internal_error";
|
|
159
|
+
message: string;
|
|
160
|
+
requestId: string;
|
|
161
|
+
schemaVersion: 1;
|
|
162
|
+
retryable: boolean;
|
|
163
|
+
retryAfterMs?: number | undefined;
|
|
164
|
+
param?: string | undefined;
|
|
165
|
+
upstreamCategory?: "unknown" | "authentication" | "invalid_request" | "rate_limit" | "quota" | "timeout" | "overloaded" | "server_error" | "content_filter" | undefined;
|
|
166
|
+
providerError?: {
|
|
167
|
+
provider: string;
|
|
168
|
+
code?: string | undefined;
|
|
169
|
+
message?: string | undefined;
|
|
170
|
+
status?: number | undefined;
|
|
171
|
+
} | undefined;
|
|
172
|
+
}>, {
|
|
173
|
+
code: "invalid_request" | "authentication_failed" | "permission_denied" | "insufficient_scope" | "model_not_found" | "unsupported_modality" | "context_length_exceeded" | "request_too_large" | "output_limit_exceeded" | "idempotency_conflict" | "insufficient_balance" | "spending_limit_exceeded" | "quota_exceeded" | "byok_credential_invalid" | "policy_violation" | "commercial_permission_denied" | "no_route_available" | "upstream_content_filtered" | "cancelled" | "rate_limited" | "deployment_unavailable" | "provider_error" | "provider_timeout" | "provider_overloaded" | "provider_credential_invalid" | "service_unavailable" | "internal_error";
|
|
174
|
+
message: string;
|
|
175
|
+
requestId: string;
|
|
176
|
+
schemaVersion: 1;
|
|
177
|
+
retryable: boolean;
|
|
178
|
+
retryAfterMs?: number | undefined;
|
|
179
|
+
param?: string | undefined;
|
|
180
|
+
upstreamCategory?: "unknown" | "authentication" | "invalid_request" | "rate_limit" | "quota" | "timeout" | "overloaded" | "server_error" | "content_filter" | undefined;
|
|
181
|
+
providerError?: {
|
|
182
|
+
provider: string;
|
|
183
|
+
code?: string | undefined;
|
|
184
|
+
message?: string | undefined;
|
|
185
|
+
status?: number | undefined;
|
|
186
|
+
} | undefined;
|
|
187
|
+
}, {
|
|
188
|
+
code: "invalid_request" | "authentication_failed" | "permission_denied" | "insufficient_scope" | "model_not_found" | "unsupported_modality" | "context_length_exceeded" | "request_too_large" | "output_limit_exceeded" | "idempotency_conflict" | "insufficient_balance" | "spending_limit_exceeded" | "quota_exceeded" | "byok_credential_invalid" | "policy_violation" | "commercial_permission_denied" | "no_route_available" | "upstream_content_filtered" | "cancelled" | "rate_limited" | "deployment_unavailable" | "provider_error" | "provider_timeout" | "provider_overloaded" | "provider_credential_invalid" | "service_unavailable" | "internal_error";
|
|
189
|
+
message: string;
|
|
190
|
+
requestId: string;
|
|
191
|
+
schemaVersion: 1;
|
|
192
|
+
retryable: boolean;
|
|
193
|
+
retryAfterMs?: number | undefined;
|
|
194
|
+
param?: string | undefined;
|
|
195
|
+
upstreamCategory?: "unknown" | "authentication" | "invalid_request" | "rate_limit" | "quota" | "timeout" | "overloaded" | "server_error" | "content_filter" | undefined;
|
|
196
|
+
providerError?: {
|
|
197
|
+
provider: string;
|
|
198
|
+
code?: string | undefined;
|
|
199
|
+
message?: string | undefined;
|
|
200
|
+
status?: number | undefined;
|
|
201
|
+
} | undefined;
|
|
202
|
+
}>;
|
|
203
|
+
export type InferenceErrorCode = z.infer<typeof inferenceErrorCodeSchema>;
|
|
204
|
+
export type UpstreamErrorCategory = z.infer<typeof upstreamErrorCategorySchema>;
|
|
205
|
+
export type ProviderErrorPassthrough = z.infer<typeof providerErrorPassthroughSchema>;
|
|
206
|
+
export type InferenceError = z.infer<typeof inferenceErrorSchema>;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Identifiers, references and wire primitives shared by every Oxy↔data-plane
|
|
3
|
+
* inference contract.
|
|
4
|
+
*
|
|
5
|
+
* Two kinds of identifier live here, and the difference matters:
|
|
6
|
+
*
|
|
7
|
+
* - **Principal identifiers** owned by Oxy (`accountId`, `applicationId`,
|
|
8
|
+
* `credentialId`, the optional delegated `userId`). The data plane may store
|
|
9
|
+
* them as immutable references; it never owns, mints or mutates them.
|
|
10
|
+
* - **Catalogue references** (`<publisher>/<model>`, `<publisher>/<model>@<revision>`,
|
|
11
|
+
* a routing-profile slug). These are the strings a customer types, so their
|
|
12
|
+
* grammar is part of the public contract, not an implementation detail.
|
|
13
|
+
*
|
|
14
|
+
* Platform-agnostic — zod only. Every regex here is plain ASCII: this package
|
|
15
|
+
* is imported by React Native apps running on Hermes, which rejects Unicode
|
|
16
|
+
* property escapes (`\p{…}`) at runtime.
|
|
17
|
+
*
|
|
18
|
+
* Decided in: docs/adr/0007-canonical-request-attribution.md, docs/adr/0008-catalogue-concept-separation.md.
|
|
19
|
+
*/
|
|
20
|
+
import { z } from 'zod';
|
|
21
|
+
/**
|
|
22
|
+
* An Oxy account id — the account that owns the workload and is financially
|
|
23
|
+
* responsible for it.
|
|
24
|
+
*
|
|
25
|
+
* Branded, and that brand is load-bearing rather than decorative: it is the
|
|
26
|
+
* type-level half of the rule that a delegated end-user identity can never
|
|
27
|
+
* become the billing identity (see {@link delegatedUserIdSchema} and
|
|
28
|
+
* `billingPrincipalSchema`). A plain `string` — and therefore any id read out
|
|
29
|
+
* of a header, a JWT claim or a request body — is not assignable to it; the
|
|
30
|
+
* only way to obtain one is to parse a value through this schema.
|
|
31
|
+
*/
|
|
32
|
+
export declare const oxyAccountIdSchema: z.ZodBranded<z.ZodString, "OxyAccountId">;
|
|
33
|
+
/**
|
|
34
|
+
* A delegated end-user identity (Alia's `X-Oxy-User-Id`), branded with a
|
|
35
|
+
* DIFFERENT brand from {@link oxyAccountIdSchema} so the two cannot be
|
|
36
|
+
* substituted for one another in either direction, in any consumer, without a
|
|
37
|
+
* cast that review would catch.
|
|
38
|
+
*
|
|
39
|
+
* It exists for attribution and product-side personalisation only. It is never
|
|
40
|
+
* a billing principal, never an access-control principal, and its presence
|
|
41
|
+
* never changes which account is charged.
|
|
42
|
+
*/
|
|
43
|
+
export declare const delegatedUserIdSchema: z.ZodBranded<z.ZodString, "DelegatedUserId">;
|
|
44
|
+
/**
|
|
45
|
+
* An Oxy `Application._id`. Not branded: no invariant in this contract turns on
|
|
46
|
+
* confusing it with another id, and brand inflation costs every producer a
|
|
47
|
+
* parse call for no safety. The two ids above are branded because ADR 0007's
|
|
48
|
+
* rule is exactly that they must not be interchangeable.
|
|
49
|
+
*/
|
|
50
|
+
export declare const oxyApplicationIdSchema: z.ZodString;
|
|
51
|
+
/** An Oxy `ApplicationCredential._id` — the credential used for this request. */
|
|
52
|
+
export declare const oxyCredentialIdSchema: z.ZodString;
|
|
53
|
+
/**
|
|
54
|
+
* A request id allocated by the Oxy EDGE, at admission and BEFORE
|
|
55
|
+
* authentication, so that a request rejected for a bad credential is as
|
|
56
|
+
* traceable as one that was served (ADR 0007, and step 1 of ADR 0010's edge
|
|
57
|
+
* order). It is required on the inbound envelope, which is what makes the data
|
|
58
|
+
* plane a consumer of this id rather than its source: the data plane echoes it
|
|
59
|
+
* on every stream event, on the usage report and on anything it can be asked
|
|
60
|
+
* about later.
|
|
61
|
+
*
|
|
62
|
+
* Correlates the Oxy edge, the data plane, the financial ledger and the
|
|
63
|
+
* customer-visible receipt, so it appears on every stream event and every
|
|
64
|
+
* ledger record.
|
|
65
|
+
*/
|
|
66
|
+
export declare const requestIdSchema: z.ZodString;
|
|
67
|
+
/**
|
|
68
|
+
* A generation id generated by the data plane, present when a request produced
|
|
69
|
+
* a generation that can be looked up later (`GET /v1/generations/:id`).
|
|
70
|
+
*/
|
|
71
|
+
export declare const generationIdSchema: z.ZodString;
|
|
72
|
+
/**
|
|
73
|
+
* A caller-supplied idempotency key. Every reserve/settle/refund call is keyed
|
|
74
|
+
* on one so a retry, a redelivered event or a duplicated webhook can never
|
|
75
|
+
* charge twice.
|
|
76
|
+
*/
|
|
77
|
+
export declare const idempotencyKeySchema: z.ZodString;
|
|
78
|
+
/** Credential environments. A credential is issued into exactly one of them. */
|
|
79
|
+
export declare const inferenceEnvironmentSchema: z.ZodEnum<["development", "staging", "production"]>;
|
|
80
|
+
/**
|
|
81
|
+
* An instant, as an ISO 8601 string in UTC (`2026-08-15T09:41:00.000Z`).
|
|
82
|
+
*
|
|
83
|
+
* Validated rather than left free-form — unlike the session contracts, which
|
|
84
|
+
* carry legacy expiry strings no consumer interprets, every instant here is
|
|
85
|
+
* read: a reservation expires, a price version starts applying, a receipt is
|
|
86
|
+
* settled. One canonical spelling (UTC, `Z`) so two records that describe the
|
|
87
|
+
* same moment compare and sort as equal, which a mix of offsets would not.
|
|
88
|
+
*/
|
|
89
|
+
export declare const inferenceTimestampSchema: z.ZodString;
|
|
90
|
+
/**
|
|
91
|
+
* A calendar DATE with no instant attached (`2026-05-01`) — a knowledge cutoff
|
|
92
|
+
* or a release date, which are published as days and become wrong when a
|
|
93
|
+
* timezone is invented for them.
|
|
94
|
+
*/
|
|
95
|
+
export declare const inferenceDateSchema: z.ZodString;
|
|
96
|
+
/** An absolute https URL, for model cards, licenses and provider documentation. */
|
|
97
|
+
export declare const inferenceHttpsUrlSchema: z.ZodString;
|
|
98
|
+
/** A publisher slug, e.g. `openai`, `anthropic`, `meta`, `alia`. */
|
|
99
|
+
export declare const publisherSlugSchema: z.ZodString;
|
|
100
|
+
/** A model slug within its publisher's namespace, e.g. `gpt-5`, `llama-3.1-70b`. */
|
|
101
|
+
export declare const modelSlugSchema: z.ZodString;
|
|
102
|
+
/**
|
|
103
|
+
* A canonical model id, `<publisher>/<model>`. This names a MODEL — a
|
|
104
|
+
* long-lived product identity whose behaviour changes as revisions ship. It
|
|
105
|
+
* does not name a revision, a deployment or a provider.
|
|
106
|
+
*/
|
|
107
|
+
export declare const modelIdSchema: z.ZodString;
|
|
108
|
+
/** An immutable revision label, unique within its model, e.g. `2026-05-01`. */
|
|
109
|
+
export declare const modelRevisionLabelSchema: z.ZodString;
|
|
110
|
+
/**
|
|
111
|
+
* A model reference as a customer writes it: `<publisher>/<model>` (the model's
|
|
112
|
+
* current revision, chosen by Oxy) or `<publisher>/<model>@<revision>` (an
|
|
113
|
+
* immutable revision the customer pinned).
|
|
114
|
+
*
|
|
115
|
+
* Both forms name a CONCRETE MODEL. Neither can name a routing profile — see
|
|
116
|
+
* {@link routingProfileSlugSchema} — which is what makes "a request for a
|
|
117
|
+
* concrete model is never silently replaced with a different model" a
|
|
118
|
+
* distinction the type system can carry rather than a convention.
|
|
119
|
+
*/
|
|
120
|
+
export declare const modelReferenceSchema: z.ZodString;
|
|
121
|
+
/**
|
|
122
|
+
* A routing-profile slug, e.g. `auto`, `fast`, `quality`.
|
|
123
|
+
*
|
|
124
|
+
* Deliberately refuses a `/`, so a profile can never be written in the shape of
|
|
125
|
+
* a model id and no caller can be confused about whether they asked for a
|
|
126
|
+
* concrete model or for Oxy to choose one. Modes like `auto`/`fast`/`quality`
|
|
127
|
+
* are profiles or product presets; they are never model objects.
|
|
128
|
+
*/
|
|
129
|
+
export declare const routingProfileSlugSchema: z.ZodString;
|
|
130
|
+
/** An inference provider slug, e.g. `openai`, `bedrock`, `oxy-hosted`. */
|
|
131
|
+
export declare const inferenceProviderSlugSchema: z.ZodString;
|
|
132
|
+
/**
|
|
133
|
+
* A deployment/endpoint id. Opaque to customers: which concrete endpoint served
|
|
134
|
+
* a request is the data plane's operational detail, and only the customer-safe
|
|
135
|
+
* subset of it is ever attributed back (see the catalogue's serving-boundary
|
|
136
|
+
* rules).
|
|
137
|
+
*/
|
|
138
|
+
export declare const deploymentIdSchema: z.ZodString;
|
|
139
|
+
/**
|
|
140
|
+
* A region identifier, e.g. `us-west-2`, `eu-central-1`. Free-form rather than
|
|
141
|
+
* a closed enum because the set is provider-defined and grows without any
|
|
142
|
+
* contract change; residency policies match on exact strings.
|
|
143
|
+
*/
|
|
144
|
+
export declare const inferenceRegionSchema: z.ZodString;
|
|
145
|
+
/**
|
|
146
|
+
* The publisher namespace reserved for models Alia actually owns or derives.
|
|
147
|
+
*
|
|
148
|
+
* `alia/*` is never a re-badged third-party route and never a prompt preset —
|
|
149
|
+
* enforcing that is the job of `modelSchema`'s provenance refinement, which
|
|
150
|
+
* this constant exists to be checked against.
|
|
151
|
+
*/
|
|
152
|
+
export declare const RESERVED_ALIA_PUBLISHER = "alia";
|
|
153
|
+
export type OxyAccountId = z.infer<typeof oxyAccountIdSchema>;
|
|
154
|
+
export type DelegatedUserId = z.infer<typeof delegatedUserIdSchema>;
|
|
155
|
+
export type InferenceEnvironment = z.infer<typeof inferenceEnvironmentSchema>;
|
|
156
|
+
export type ModelReference = z.infer<typeof modelReferenceSchema>;
|
|
157
|
+
export type ModelId = z.infer<typeof modelIdSchema>;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Money and usage units for the inference contracts.
|
|
3
|
+
*
|
|
4
|
+
* The non-negotiable invariant this file exists to make structural: **customer
|
|
5
|
+
* charges never use floating-point values as the financial source of truth.**
|
|
6
|
+
* A JS `number` cannot represent `0.1 + 0.2` exactly, and an inference ledger
|
|
7
|
+
* adds millions of small amounts, so a float total is wrong by construction
|
|
8
|
+
* rather than by accident.
|
|
9
|
+
*
|
|
10
|
+
* Every amount and every price is one representation: {@link exactDecimalSchema},
|
|
11
|
+
* an exact decimal STRING at the scale ADR 0009 declares. Amounts are not
|
|
12
|
+
* integer minor units, and that is the ADR's decision rather than an oversight:
|
|
13
|
+
* one token costs several orders of magnitude less than one cent, so rounding
|
|
14
|
+
* per request would make a customer's bill depend on how their client chunked
|
|
15
|
+
* its work. Rounding happens ONCE, at the invoice boundary, and is itself a
|
|
16
|
+
* ledger entry.
|
|
17
|
+
*
|
|
18
|
+
* A string is also what the driver hands back — `postgres.js` decodes `NUMERIC`
|
|
19
|
+
* as a string — so keeping it a string on the wire means accidental JS
|
|
20
|
+
* arithmetic fails loudly instead of silently losing precision. Money
|
|
21
|
+
* arithmetic happens in SQL or in a decimal type, never in a JS `number`.
|
|
22
|
+
*
|
|
23
|
+
* Units are carried separately from money in every shape: a receipt says both
|
|
24
|
+
* "204 output tokens" and "0.003060000000 USD", and neither is derived from the
|
|
25
|
+
* other at read time. That separation is what lets a price version change
|
|
26
|
+
* without rewriting settled history.
|
|
27
|
+
*
|
|
28
|
+
* Decided in: docs/adr/0009-usage-reservation-and-settlement.md.
|
|
29
|
+
*/
|
|
30
|
+
import { z } from 'zod';
|
|
31
|
+
/** ISO 4217 alpha-3 currency code, e.g. `USD`. */
|
|
32
|
+
export declare const currencyCodeSchema: z.ZodString;
|
|
33
|
+
/**
|
|
34
|
+
* The declared fractional scale of every amount and price in this contract, and
|
|
35
|
+
* of the `NUMERIC` columns the ledger stores them in.
|
|
36
|
+
*
|
|
37
|
+
* Twelve digits is sub-minor-unit precision by a wide margin: at $3 per million
|
|
38
|
+
* input tokens, one token costs `0.000003000000`, which this scale represents
|
|
39
|
+
* exactly. Amounts are compared and summed NUMERICALLY, never as text — `3.0`
|
|
40
|
+
* and `3.000000000000` are one amount written two ways.
|
|
41
|
+
*/
|
|
42
|
+
export declare const INFERENCE_MONEY_SCALE = 12;
|
|
43
|
+
/**
|
|
44
|
+
* An exact non-negative decimal, carried as a STRING so no parse step can turn
|
|
45
|
+
* it into a float on the way past. Up to 18 integer digits and
|
|
46
|
+
* {@link INFERENCE_MONEY_SCALE} fractional digits.
|
|
47
|
+
*
|
|
48
|
+
* Non-negative: direction is carried by the SHAPE — a receipt debits, a refund
|
|
49
|
+
* credits — so a stray sign can never silently invert an entry.
|
|
50
|
+
*
|
|
51
|
+
* No exponent form: `1e-6` and `0.000001` are the same number, but only one of
|
|
52
|
+
* them survives a naive string comparison, a cache key or a log grep intact.
|
|
53
|
+
*
|
|
54
|
+
* Branded, so a bare `string` is not assignable and an amount cannot arrive
|
|
55
|
+
* from string concatenation that was never checked. Producers construct one
|
|
56
|
+
* with `exactDecimalSchema.parse(value)`.
|
|
57
|
+
*/
|
|
58
|
+
export declare const exactDecimalSchema: z.ZodBranded<z.ZodString, "ExactDecimal">;
|
|
59
|
+
/**
|
|
60
|
+
* An amount of money: the exact decimal plus the currency it is in.
|
|
61
|
+
*
|
|
62
|
+
* `.strict()` so a payload carrying a convenience float beside the exact value
|
|
63
|
+
* (`{ amount: '18.06', amountFloat: 18.06 }`) is REJECTED rather than stripped.
|
|
64
|
+
* A stripped float is the more dangerous outcome: it disappears silently here
|
|
65
|
+
* and survives in the producer, where it is the value somebody eventually
|
|
66
|
+
* displays.
|
|
67
|
+
*/
|
|
68
|
+
export declare const moneySchema: z.ZodObject<{
|
|
69
|
+
amount: z.ZodBranded<z.ZodString, "ExactDecimal">;
|
|
70
|
+
currency: z.ZodString;
|
|
71
|
+
}, "strict", z.ZodTypeAny, {
|
|
72
|
+
amount: string & z.BRAND<"ExactDecimal">;
|
|
73
|
+
currency: string;
|
|
74
|
+
}, {
|
|
75
|
+
amount: string;
|
|
76
|
+
currency: string;
|
|
77
|
+
}>;
|
|
78
|
+
/**
|
|
79
|
+
* The closed set of units inference is metered in.
|
|
80
|
+
*
|
|
81
|
+
* **The units PARTITION a request: every unit counts material no other unit
|
|
82
|
+
* counts.** `cached_input_tokens` is not part of `input_tokens`, and
|
|
83
|
+
* `reasoning_tokens` is not part of `output_tokens` — they are siblings, not
|
|
84
|
+
* subsets. A request whose 10 000-token prompt was served 9 000 tokens from
|
|
85
|
+
* cache is reported as `input_tokens: 1000` beside `cached_input_tokens: 9000`,
|
|
86
|
+
* never as `input_tokens: 10000` beside it.
|
|
87
|
+
*
|
|
88
|
+
* That belongs to the definition rather than to a convention somewhere else,
|
|
89
|
+
* because settlement applies a price to EVERY reported unit and sums them
|
|
90
|
+
* (`inferenceLedger.service.ts`'s `computeCharge`). Under the partition rule
|
|
91
|
+
* that sum IS the request's cost, and a cached token can carry its own — lower
|
|
92
|
+
* — price. Under the nested reading the same sum charges the cached and
|
|
93
|
+
* reasoning tokens twice: once inside their parent and once on their own line.
|
|
94
|
+
* It fails silently, because every total still looks plausible and the receipt
|
|
95
|
+
* is still internally consistent, and on a reasoning model the reasoning tokens
|
|
96
|
+
* can dominate the completion, so the error is not marginal.
|
|
97
|
+
*
|
|
98
|
+
* **Every OpenAI-compatible provider reports the other way round**:
|
|
99
|
+
* `prompt_tokens` INCLUDES `prompt_tokens_details.cached_tokens`, and
|
|
100
|
+
* `completion_tokens` INCLUDES `completion_tokens_details.reasoning_tokens`.
|
|
101
|
+
* Normalising is the data plane's job and it is subtraction:
|
|
102
|
+
*
|
|
103
|
+
* ```text
|
|
104
|
+
* input_tokens = prompt_tokens - prompt_tokens_details.cached_tokens
|
|
105
|
+
* output_tokens = completion_tokens - completion_tokens_details.reasoning_tokens
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* No refinement in this package can enforce it, and saying so is part of the
|
|
109
|
+
* rule: a nested report and a disjoint one are the same four non-negative
|
|
110
|
+
* integers, so no predicate over a single report can tell them apart. The two
|
|
111
|
+
* structural guards that DO exist — refining `cached <= input` and
|
|
112
|
+
* `reasoning <= output`, or deriving the parents instead of reporting them —
|
|
113
|
+
* both encode the nested reading, which is the one this rule rejects. What IS
|
|
114
|
+
* enforceable is the arithmetic that depends on the rule, and that is where the
|
|
115
|
+
* enforcement lives — `inferenceLedger.service.test.ts` prices a report in
|
|
116
|
+
* which cached and reasoning tokens are both non-zero and asserts the exact
|
|
117
|
+
* total, which the nested reading cannot produce.
|
|
118
|
+
*
|
|
119
|
+
* Where the public surface has to speak a nested dialect, the sum is put back
|
|
120
|
+
* at the boundary rather than the internal reading being bent to it
|
|
121
|
+
* (`routes/inferenceEdge.ts` renders `prompt_tokens` as
|
|
122
|
+
* `input_tokens + cached_input_tokens`).
|
|
123
|
+
*
|
|
124
|
+
* Time is carried in integer MILLISECONDS rather than seconds so that no unit
|
|
125
|
+
* quantity is ever fractional: a 12.5-second transcription is `12500`, exactly,
|
|
126
|
+
* and the "units are integers" rule holds for every modality instead of holding
|
|
127
|
+
* for tokens and being quietly broken by audio.
|
|
128
|
+
*/
|
|
129
|
+
export declare const USAGE_UNITS: readonly ["input_tokens", "cached_input_tokens", "output_tokens", "reasoning_tokens", "requests", "images", "audio_input_milliseconds", "audio_output_milliseconds", "video_milliseconds", "characters", "embeddings"];
|
|
130
|
+
export declare const usageUnitSchema: z.ZodEnum<["input_tokens", "cached_input_tokens", "output_tokens", "reasoning_tokens", "requests", "images", "audio_input_milliseconds", "audio_output_milliseconds", "video_milliseconds", "characters", "embeddings"]>;
|
|
131
|
+
/**
|
|
132
|
+
* A metered quantity of ONE unit. Never money — a quantity carries no price and
|
|
133
|
+
* no currency, so a consumer cannot mistake a token count for an amount owed.
|
|
134
|
+
*/
|
|
135
|
+
export declare const usageQuantitySchema: z.ZodObject<{
|
|
136
|
+
unit: z.ZodEnum<["input_tokens", "cached_input_tokens", "output_tokens", "reasoning_tokens", "requests", "images", "audio_input_milliseconds", "audio_output_milliseconds", "video_milliseconds", "characters", "embeddings"]>;
|
|
137
|
+
quantity: z.ZodNumber;
|
|
138
|
+
}, "strict", z.ZodTypeAny, {
|
|
139
|
+
unit: "input_tokens" | "cached_input_tokens" | "output_tokens" | "reasoning_tokens" | "requests" | "images" | "audio_input_milliseconds" | "audio_output_milliseconds" | "video_milliseconds" | "characters" | "embeddings";
|
|
140
|
+
quantity: number;
|
|
141
|
+
}, {
|
|
142
|
+
unit: "input_tokens" | "cached_input_tokens" | "output_tokens" | "reasoning_tokens" | "requests" | "images" | "audio_input_milliseconds" | "audio_output_milliseconds" | "video_milliseconds" | "characters" | "embeddings";
|
|
143
|
+
quantity: number;
|
|
144
|
+
}>;
|
|
145
|
+
/**
|
|
146
|
+
* Where a metered quantity came from.
|
|
147
|
+
*
|
|
148
|
+
* Kept explicit because the three are not interchangeable when a charge is
|
|
149
|
+
* disputed: `provider_reported` is the upstream's own count, `oxy_measured` is
|
|
150
|
+
* counted by the platform (streamed bytes, wall-clock milliseconds), and
|
|
151
|
+
* `estimated` is a reconstruction used when a provider returned no usage at
|
|
152
|
+
* all. An estimate that is indistinguishable from a reported number is an
|
|
153
|
+
* estimate nobody can later reconcile or refund against.
|
|
154
|
+
*/
|
|
155
|
+
export declare const USAGE_SOURCES: readonly ["provider_reported", "oxy_measured", "estimated"];
|
|
156
|
+
export declare const usageSourceSchema: z.ZodEnum<["provider_reported", "oxy_measured", "estimated"]>;
|
|
157
|
+
/**
|
|
158
|
+
* A price for one unit, as `amount` per `per` units — `per` because a price
|
|
159
|
+
* quoted per single token would need more fractional digits than it is worth
|
|
160
|
+
* ("$3.00 per 1000000 input_tokens" is how every provider quotes it, and how
|
|
161
|
+
* every customer reads it).
|
|
162
|
+
*/
|
|
163
|
+
export declare const unitPriceSchema: z.ZodObject<{
|
|
164
|
+
unit: z.ZodEnum<["input_tokens", "cached_input_tokens", "output_tokens", "reasoning_tokens", "requests", "images", "audio_input_milliseconds", "audio_output_milliseconds", "video_milliseconds", "characters", "embeddings"]>;
|
|
165
|
+
amount: z.ZodBranded<z.ZodString, "ExactDecimal">;
|
|
166
|
+
per: z.ZodNumber;
|
|
167
|
+
currency: z.ZodString;
|
|
168
|
+
}, "strict", z.ZodTypeAny, {
|
|
169
|
+
amount: string & z.BRAND<"ExactDecimal">;
|
|
170
|
+
currency: string;
|
|
171
|
+
unit: "input_tokens" | "cached_input_tokens" | "output_tokens" | "reasoning_tokens" | "requests" | "images" | "audio_input_milliseconds" | "audio_output_milliseconds" | "video_milliseconds" | "characters" | "embeddings";
|
|
172
|
+
per: number;
|
|
173
|
+
}, {
|
|
174
|
+
amount: string;
|
|
175
|
+
currency: string;
|
|
176
|
+
unit: "input_tokens" | "cached_input_tokens" | "output_tokens" | "reasoning_tokens" | "requests" | "images" | "audio_input_milliseconds" | "audio_output_milliseconds" | "video_milliseconds" | "characters" | "embeddings";
|
|
177
|
+
per: number;
|
|
178
|
+
}>;
|
|
179
|
+
export type CurrencyCode = z.infer<typeof currencyCodeSchema>;
|
|
180
|
+
export type ExactDecimal = z.infer<typeof exactDecimalSchema>;
|
|
181
|
+
export type Money = z.infer<typeof moneySchema>;
|
|
182
|
+
export type UsageUnit = z.infer<typeof usageUnitSchema>;
|
|
183
|
+
export type UsageSource = z.infer<typeof usageSourceSchema>;
|
|
184
|
+
export type UsageQuantity = z.infer<typeof usageQuantitySchema>;
|
|
185
|
+
export type UnitPrice = z.infer<typeof unitPriceSchema>;
|