@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.
- 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 +1030 -0
- package/dist/internal/catalogue.js +349 -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 +696 -0
- package/dist/internal/grant.js +437 -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 +177 -0
- package/dist/public/version.js +177 -0
- package/openapi.json +6967 -0
- package/package.json +44 -4
- package/README.md +0 -3
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scoped execution (envelope v3).
|
|
3
|
+
*
|
|
4
|
+
* Moved from `@oxy.so/contracts` 4.12.0 (`src/inference/scopedExecution.ts`) by Kaana ADR 0002.
|
|
5
|
+
*/
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
/** Explicit extension negotiation; never replaces the legacy 3.5 handshake. */
|
|
8
|
+
export declare const SCOPED_EXECUTION_CONTRACT_VERSION: "3.6.0";
|
|
9
|
+
/** Scoped only; normal envelope v2 and metadata 3.5 remain unchanged. */
|
|
10
|
+
export declare const SCOPED_REQUEST_ENVELOPE_VERSION: 3;
|
|
11
|
+
/** Restriction only: none of these identities grants catalogue eligibility. */
|
|
12
|
+
export declare const scopedExecutionAudienceSchema: z.ZodObject<{
|
|
13
|
+
permitId: z.ZodString;
|
|
14
|
+
idempotencyKey: z.ZodString;
|
|
15
|
+
fixtureSha256: z.ZodString;
|
|
16
|
+
expiresAt: z.ZodString;
|
|
17
|
+
principal: z.ZodObject<{
|
|
18
|
+
accountId: z.ZodString;
|
|
19
|
+
applicationId: z.ZodString;
|
|
20
|
+
credentialId: z.ZodString;
|
|
21
|
+
environment: z.ZodEnum<{
|
|
22
|
+
development: "development";
|
|
23
|
+
staging: "staging";
|
|
24
|
+
production: "production";
|
|
25
|
+
}>;
|
|
26
|
+
}, z.core.$strict>;
|
|
27
|
+
policy: z.ZodObject<{
|
|
28
|
+
routingPolicyId: z.ZodString;
|
|
29
|
+
policyVersion: z.ZodNumber;
|
|
30
|
+
}, z.core.$strict>;
|
|
31
|
+
deploymentId: z.ZodString;
|
|
32
|
+
provider: z.ZodString;
|
|
33
|
+
keyId: z.ZodString;
|
|
34
|
+
modelReference: z.ZodString;
|
|
35
|
+
upstreamModelId: z.ZodString;
|
|
36
|
+
priceVersionId: z.ZodString;
|
|
37
|
+
providerRateCardVersionId: z.ZodString;
|
|
38
|
+
providerSourceVersion: z.ZodString;
|
|
39
|
+
maxCostUsd: z.core.$ZodBranded<z.ZodString, "ExactDecimal", "out">;
|
|
40
|
+
}, z.core.$strict>;
|
|
41
|
+
export declare const scopedExecutionSchema: z.ZodObject<{
|
|
42
|
+
permitId: z.ZodString;
|
|
43
|
+
idempotencyKey: z.ZodString;
|
|
44
|
+
fixtureSha256: z.ZodString;
|
|
45
|
+
expiresAt: z.ZodString;
|
|
46
|
+
principal: z.ZodObject<{
|
|
47
|
+
accountId: z.ZodString;
|
|
48
|
+
applicationId: z.ZodString;
|
|
49
|
+
credentialId: z.ZodString;
|
|
50
|
+
environment: z.ZodEnum<{
|
|
51
|
+
development: "development";
|
|
52
|
+
staging: "staging";
|
|
53
|
+
production: "production";
|
|
54
|
+
}>;
|
|
55
|
+
}, z.core.$strict>;
|
|
56
|
+
policy: z.ZodObject<{
|
|
57
|
+
routingPolicyId: z.ZodString;
|
|
58
|
+
policyVersion: z.ZodNumber;
|
|
59
|
+
}, z.core.$strict>;
|
|
60
|
+
deploymentId: z.ZodString;
|
|
61
|
+
provider: z.ZodString;
|
|
62
|
+
keyId: z.ZodString;
|
|
63
|
+
modelReference: z.ZodString;
|
|
64
|
+
upstreamModelId: z.ZodString;
|
|
65
|
+
priceVersionId: z.ZodString;
|
|
66
|
+
providerRateCardVersionId: z.ZodString;
|
|
67
|
+
providerSourceVersion: z.ZodString;
|
|
68
|
+
maxCostUsd: z.core.$ZodBranded<z.ZodString, "ExactDecimal", "out">;
|
|
69
|
+
requestId: z.ZodString;
|
|
70
|
+
snapshotId: z.ZodString;
|
|
71
|
+
catalogueEvidenceHash: z.ZodString;
|
|
72
|
+
}, z.core.$strict>;
|
|
73
|
+
export type ScopedExecutionAudience = z.infer<typeof scopedExecutionAudienceSchema>;
|
|
74
|
+
export type ScopedExecution = z.infer<typeof scopedExecutionSchema>;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scoped execution (envelope v3).
|
|
3
|
+
*
|
|
4
|
+
* Moved from `@oxy.so/contracts` 4.12.0 (`src/inference/scopedExecution.ts`) by Kaana ADR 0002.
|
|
5
|
+
*/
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
import { idempotencyKeySchema, inferenceEnvironmentSchema, modelReferenceSchema, requestIdSchema, } from '../public/identifiers.js';
|
|
8
|
+
import { exactDecimalSchema } from '../public/money.js';
|
|
9
|
+
import { deploymentIdSchema, inferenceProviderSlugSchema } from './identifiers.js';
|
|
10
|
+
/** Explicit extension negotiation; never replaces the legacy 3.5 handshake. */
|
|
11
|
+
export const SCOPED_EXECUTION_CONTRACT_VERSION = '3.6.0';
|
|
12
|
+
/** Scoped only; normal envelope v2 and metadata 3.5 remain unchanged. */
|
|
13
|
+
export const SCOPED_REQUEST_ENVELOPE_VERSION = 3;
|
|
14
|
+
const identity = z.string().min(1).max(256);
|
|
15
|
+
const sha256 = z.string().regex(/^[0-9a-f]{64}$/);
|
|
16
|
+
const maxCost = exactDecimalSchema.refine((value) => {
|
|
17
|
+
if (!exactDecimalSchema.safeParse(value).success)
|
|
18
|
+
return false;
|
|
19
|
+
const [whole, fraction = ''] = value.split('.');
|
|
20
|
+
const units = BigInt(whole) * 1000000000000n + BigInt(fraction.padEnd(12, '0'));
|
|
21
|
+
return units > 0n && units <= 10000000000n;
|
|
22
|
+
}, 'scoped cost must be positive and at most 0.01 USD');
|
|
23
|
+
/** Restriction only: none of these identities grants catalogue eligibility. */
|
|
24
|
+
export const scopedExecutionAudienceSchema = z
|
|
25
|
+
.object({
|
|
26
|
+
permitId: identity,
|
|
27
|
+
idempotencyKey: idempotencyKeySchema,
|
|
28
|
+
fixtureSha256: sha256,
|
|
29
|
+
expiresAt: z.string().datetime(),
|
|
30
|
+
principal: z
|
|
31
|
+
.object({
|
|
32
|
+
accountId: identity,
|
|
33
|
+
applicationId: identity,
|
|
34
|
+
credentialId: identity,
|
|
35
|
+
environment: inferenceEnvironmentSchema,
|
|
36
|
+
})
|
|
37
|
+
.strict(),
|
|
38
|
+
policy: z
|
|
39
|
+
.object({
|
|
40
|
+
routingPolicyId: z.string().min(1).max(128),
|
|
41
|
+
policyVersion: z.number().int().positive().safe(),
|
|
42
|
+
})
|
|
43
|
+
.strict(),
|
|
44
|
+
deploymentId: deploymentIdSchema,
|
|
45
|
+
provider: inferenceProviderSlugSchema,
|
|
46
|
+
keyId: identity,
|
|
47
|
+
modelReference: modelReferenceSchema.refine((value) => value.includes('@'), 'must pin a revision'),
|
|
48
|
+
upstreamModelId: identity,
|
|
49
|
+
priceVersionId: identity,
|
|
50
|
+
providerRateCardVersionId: identity,
|
|
51
|
+
providerSourceVersion: identity,
|
|
52
|
+
maxCostUsd: maxCost,
|
|
53
|
+
})
|
|
54
|
+
.strict();
|
|
55
|
+
export const scopedExecutionSchema = scopedExecutionAudienceSchema
|
|
56
|
+
.extend({
|
|
57
|
+
requestId: requestIdSchema,
|
|
58
|
+
snapshotId: identity,
|
|
59
|
+
catalogueEvidenceHash: sha256,
|
|
60
|
+
})
|
|
61
|
+
.strict();
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Settlement: exactly once, from Kaana's measurement (Kaana ADR 0002,
|
|
3
|
+
* decision 3).
|
|
4
|
+
*
|
|
5
|
+
* When a granted request ends — success, error, cancellation or the close of a
|
|
6
|
+
* realtime session — Kaana writes the settlement into a durable outbox in its
|
|
7
|
+
* PostgreSQL, in the same transaction as its provider-cost events, and a worker
|
|
8
|
+
* delivers it to `POST https://api.oxy.so/internal/kaana/v1/settlements`,
|
|
9
|
+
* signed by Kaana under {@link SETTLEMENT_SIGNATURE_DOMAIN}. Oxy settles
|
|
10
|
+
* idempotently on `grantId` and reconciles by reading
|
|
11
|
+
* `GET https://kaana.ai/internal/v1/settlements?cursor=`. Units, never money:
|
|
12
|
+
* Oxy prices with the `priceVersionId` the grant admitted for the line served.
|
|
13
|
+
*
|
|
14
|
+
* New in contract set 4.0.0.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
/** The signature domain of a settlement, under Kaana's own settlement key. */
|
|
18
|
+
export declare const SETTLEMENT_SIGNATURE_DOMAIN = "kaana-oxy-settlement:v1";
|
|
19
|
+
/** One granted request's settlement, as the outbox delivers it. */
|
|
20
|
+
export declare const settlementSchema: z.ZodObject<{
|
|
21
|
+
schemaVersion: z.ZodLiteral<1>;
|
|
22
|
+
settlementId: z.ZodString;
|
|
23
|
+
grantId: z.ZodString;
|
|
24
|
+
requestId: z.ZodString;
|
|
25
|
+
reservationId: z.ZodOptional<z.ZodString>;
|
|
26
|
+
meteredUsageId: z.ZodOptional<z.ZodString>;
|
|
27
|
+
servedStep: z.ZodOptional<z.ZodNumber>;
|
|
28
|
+
report: z.ZodObject<{
|
|
29
|
+
schemaVersion: z.ZodLiteral<2>;
|
|
30
|
+
requestId: z.ZodString;
|
|
31
|
+
generationId: z.ZodOptional<z.ZodString>;
|
|
32
|
+
attribution: z.ZodObject<{
|
|
33
|
+
principal: z.ZodObject<{
|
|
34
|
+
billing: z.ZodObject<{
|
|
35
|
+
accountId: z.core.$ZodBranded<z.ZodString, "OxyAccountId", "out">;
|
|
36
|
+
}, z.core.$strict>;
|
|
37
|
+
applicationId: z.ZodString;
|
|
38
|
+
credentialId: z.ZodString;
|
|
39
|
+
environment: z.ZodEnum<{
|
|
40
|
+
development: "development";
|
|
41
|
+
staging: "staging";
|
|
42
|
+
production: "production";
|
|
43
|
+
}>;
|
|
44
|
+
inferenceScopes: z.ZodArray<z.ZodEnum<{
|
|
45
|
+
"inference:invoke": "inference:invoke";
|
|
46
|
+
"inference:models:read": "inference:models:read";
|
|
47
|
+
"inference:usage:read": "inference:usage:read";
|
|
48
|
+
"inference:routing:read": "inference:routing:read";
|
|
49
|
+
"inference:routing:write": "inference:routing:write";
|
|
50
|
+
"inference:providers:read": "inference:providers:read";
|
|
51
|
+
"inference:providers:write": "inference:providers:write";
|
|
52
|
+
}>>;
|
|
53
|
+
}, z.core.$strip>;
|
|
54
|
+
userId: z.ZodOptional<z.core.$ZodBranded<z.ZodString, "DelegatedUserId", "out">>;
|
|
55
|
+
requestId: z.ZodString;
|
|
56
|
+
generationId: z.ZodOptional<z.ZodString>;
|
|
57
|
+
}, z.core.$strip>;
|
|
58
|
+
outcome: z.ZodEnum<{
|
|
59
|
+
cancelled: "cancelled";
|
|
60
|
+
completed: "completed";
|
|
61
|
+
partial: "partial";
|
|
62
|
+
failed: "failed";
|
|
63
|
+
}>;
|
|
64
|
+
units: z.ZodArray<z.ZodObject<{
|
|
65
|
+
unit: z.ZodEnum<{
|
|
66
|
+
input_tokens: "input_tokens";
|
|
67
|
+
cached_input_tokens: "cached_input_tokens";
|
|
68
|
+
output_tokens: "output_tokens";
|
|
69
|
+
reasoning_tokens: "reasoning_tokens";
|
|
70
|
+
requests: "requests";
|
|
71
|
+
images: "images";
|
|
72
|
+
audio_input_milliseconds: "audio_input_milliseconds";
|
|
73
|
+
audio_output_milliseconds: "audio_output_milliseconds";
|
|
74
|
+
video_milliseconds: "video_milliseconds";
|
|
75
|
+
characters: "characters";
|
|
76
|
+
embeddings: "embeddings";
|
|
77
|
+
audio_input_tokens: "audio_input_tokens";
|
|
78
|
+
cached_audio_input_tokens: "cached_audio_input_tokens";
|
|
79
|
+
audio_output_tokens: "audio_output_tokens";
|
|
80
|
+
session_milliseconds: "session_milliseconds";
|
|
81
|
+
input_transcription_milliseconds: "input_transcription_milliseconds";
|
|
82
|
+
}>;
|
|
83
|
+
quantity: z.ZodNumber;
|
|
84
|
+
}, z.core.$strict>>;
|
|
85
|
+
usageSource: z.ZodEnum<{
|
|
86
|
+
provider_reported: "provider_reported";
|
|
87
|
+
oxy_measured: "oxy_measured";
|
|
88
|
+
estimated: "estimated";
|
|
89
|
+
}>;
|
|
90
|
+
resolvedModelReference: z.ZodString;
|
|
91
|
+
servingProvider: z.ZodString;
|
|
92
|
+
deploymentId: z.ZodString;
|
|
93
|
+
routeSwitches: z.ZodNumber;
|
|
94
|
+
fundingTier: z.ZodOptional<z.ZodEnum<{
|
|
95
|
+
free: "free";
|
|
96
|
+
credit: "credit";
|
|
97
|
+
paid: "paid";
|
|
98
|
+
}>>;
|
|
99
|
+
startedAt: z.ZodString;
|
|
100
|
+
completedAt: z.ZodString;
|
|
101
|
+
timeToFirstTokenMs: z.ZodOptional<z.ZodNumber>;
|
|
102
|
+
}, z.core.$strip>;
|
|
103
|
+
createdAt: z.ZodString;
|
|
104
|
+
}, z.core.$strict>;
|
|
105
|
+
/** Oxy's answer to a delivered settlement. A redelivery is `duplicate`, never a second charge. */
|
|
106
|
+
export declare const settlementAcknowledgementSchema: z.ZodObject<{
|
|
107
|
+
schemaVersion: z.ZodLiteral<1>;
|
|
108
|
+
settlementId: z.ZodString;
|
|
109
|
+
status: z.ZodEnum<{
|
|
110
|
+
duplicate: "duplicate";
|
|
111
|
+
settled: "settled";
|
|
112
|
+
}>;
|
|
113
|
+
}, z.core.$strict>;
|
|
114
|
+
/** `GET /internal/v1/settlements?cursor=`: what Oxy reconciles against. */
|
|
115
|
+
export declare const settlementPageSchema: z.ZodObject<{
|
|
116
|
+
data: z.ZodArray<z.ZodObject<{
|
|
117
|
+
schemaVersion: z.ZodLiteral<1>;
|
|
118
|
+
settlementId: z.ZodString;
|
|
119
|
+
grantId: z.ZodString;
|
|
120
|
+
requestId: z.ZodString;
|
|
121
|
+
reservationId: z.ZodOptional<z.ZodString>;
|
|
122
|
+
meteredUsageId: z.ZodOptional<z.ZodString>;
|
|
123
|
+
servedStep: z.ZodOptional<z.ZodNumber>;
|
|
124
|
+
report: z.ZodObject<{
|
|
125
|
+
schemaVersion: z.ZodLiteral<2>;
|
|
126
|
+
requestId: z.ZodString;
|
|
127
|
+
generationId: z.ZodOptional<z.ZodString>;
|
|
128
|
+
attribution: z.ZodObject<{
|
|
129
|
+
principal: z.ZodObject<{
|
|
130
|
+
billing: z.ZodObject<{
|
|
131
|
+
accountId: z.core.$ZodBranded<z.ZodString, "OxyAccountId", "out">;
|
|
132
|
+
}, z.core.$strict>;
|
|
133
|
+
applicationId: z.ZodString;
|
|
134
|
+
credentialId: z.ZodString;
|
|
135
|
+
environment: z.ZodEnum<{
|
|
136
|
+
development: "development";
|
|
137
|
+
staging: "staging";
|
|
138
|
+
production: "production";
|
|
139
|
+
}>;
|
|
140
|
+
inferenceScopes: z.ZodArray<z.ZodEnum<{
|
|
141
|
+
"inference:invoke": "inference:invoke";
|
|
142
|
+
"inference:models:read": "inference:models:read";
|
|
143
|
+
"inference:usage:read": "inference:usage:read";
|
|
144
|
+
"inference:routing:read": "inference:routing:read";
|
|
145
|
+
"inference:routing:write": "inference:routing:write";
|
|
146
|
+
"inference:providers:read": "inference:providers:read";
|
|
147
|
+
"inference:providers:write": "inference:providers:write";
|
|
148
|
+
}>>;
|
|
149
|
+
}, z.core.$strip>;
|
|
150
|
+
userId: z.ZodOptional<z.core.$ZodBranded<z.ZodString, "DelegatedUserId", "out">>;
|
|
151
|
+
requestId: z.ZodString;
|
|
152
|
+
generationId: z.ZodOptional<z.ZodString>;
|
|
153
|
+
}, z.core.$strip>;
|
|
154
|
+
outcome: z.ZodEnum<{
|
|
155
|
+
cancelled: "cancelled";
|
|
156
|
+
completed: "completed";
|
|
157
|
+
partial: "partial";
|
|
158
|
+
failed: "failed";
|
|
159
|
+
}>;
|
|
160
|
+
units: z.ZodArray<z.ZodObject<{
|
|
161
|
+
unit: z.ZodEnum<{
|
|
162
|
+
input_tokens: "input_tokens";
|
|
163
|
+
cached_input_tokens: "cached_input_tokens";
|
|
164
|
+
output_tokens: "output_tokens";
|
|
165
|
+
reasoning_tokens: "reasoning_tokens";
|
|
166
|
+
requests: "requests";
|
|
167
|
+
images: "images";
|
|
168
|
+
audio_input_milliseconds: "audio_input_milliseconds";
|
|
169
|
+
audio_output_milliseconds: "audio_output_milliseconds";
|
|
170
|
+
video_milliseconds: "video_milliseconds";
|
|
171
|
+
characters: "characters";
|
|
172
|
+
embeddings: "embeddings";
|
|
173
|
+
audio_input_tokens: "audio_input_tokens";
|
|
174
|
+
cached_audio_input_tokens: "cached_audio_input_tokens";
|
|
175
|
+
audio_output_tokens: "audio_output_tokens";
|
|
176
|
+
session_milliseconds: "session_milliseconds";
|
|
177
|
+
input_transcription_milliseconds: "input_transcription_milliseconds";
|
|
178
|
+
}>;
|
|
179
|
+
quantity: z.ZodNumber;
|
|
180
|
+
}, z.core.$strict>>;
|
|
181
|
+
usageSource: z.ZodEnum<{
|
|
182
|
+
provider_reported: "provider_reported";
|
|
183
|
+
oxy_measured: "oxy_measured";
|
|
184
|
+
estimated: "estimated";
|
|
185
|
+
}>;
|
|
186
|
+
resolvedModelReference: z.ZodString;
|
|
187
|
+
servingProvider: z.ZodString;
|
|
188
|
+
deploymentId: z.ZodString;
|
|
189
|
+
routeSwitches: z.ZodNumber;
|
|
190
|
+
fundingTier: z.ZodOptional<z.ZodEnum<{
|
|
191
|
+
free: "free";
|
|
192
|
+
credit: "credit";
|
|
193
|
+
paid: "paid";
|
|
194
|
+
}>>;
|
|
195
|
+
startedAt: z.ZodString;
|
|
196
|
+
completedAt: z.ZodString;
|
|
197
|
+
timeToFirstTokenMs: z.ZodOptional<z.ZodNumber>;
|
|
198
|
+
}, z.core.$strip>;
|
|
199
|
+
createdAt: z.ZodString;
|
|
200
|
+
}, z.core.$strict>>;
|
|
201
|
+
nextCursor: z.ZodOptional<z.ZodString>;
|
|
202
|
+
}, z.core.$strict>;
|
|
203
|
+
export type Settlement = z.infer<typeof settlementSchema>;
|
|
204
|
+
export type SettlementAcknowledgement = z.infer<typeof settlementAcknowledgementSchema>;
|
|
205
|
+
export type SettlementPage = z.infer<typeof settlementPageSchema>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Settlement: exactly once, from Kaana's measurement (Kaana ADR 0002,
|
|
3
|
+
* decision 3).
|
|
4
|
+
*
|
|
5
|
+
* When a granted request ends — success, error, cancellation or the close of a
|
|
6
|
+
* realtime session — Kaana writes the settlement into a durable outbox in its
|
|
7
|
+
* PostgreSQL, in the same transaction as its provider-cost events, and a worker
|
|
8
|
+
* delivers it to `POST https://api.oxy.so/internal/kaana/v1/settlements`,
|
|
9
|
+
* signed by Kaana under {@link SETTLEMENT_SIGNATURE_DOMAIN}. Oxy settles
|
|
10
|
+
* idempotently on `grantId` and reconciles by reading
|
|
11
|
+
* `GET https://kaana.ai/internal/v1/settlements?cursor=`. Units, never money:
|
|
12
|
+
* Oxy prices with the `priceVersionId` the grant admitted for the line served.
|
|
13
|
+
*
|
|
14
|
+
* New in contract set 4.0.0.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
import { inferenceTimestampSchema, requestIdSchema } from '../public/identifiers.js';
|
|
18
|
+
import { normalizedUsageReportSchema } from './usage.js';
|
|
19
|
+
/** The signature domain of a settlement, under Kaana's own settlement key. */
|
|
20
|
+
export const SETTLEMENT_SIGNATURE_DOMAIN = 'kaana-oxy-settlement:v1';
|
|
21
|
+
/** One granted request's settlement, as the outbox delivers it. */
|
|
22
|
+
export const settlementSchema = z
|
|
23
|
+
.object({
|
|
24
|
+
schemaVersion: z.literal(1),
|
|
25
|
+
/** The outbox row; stable across redeliveries. */
|
|
26
|
+
settlementId: z.string().min(1).max(128),
|
|
27
|
+
/** The idempotency key of the settlement: one grant settles once. */
|
|
28
|
+
grantId: z.string().min(1).max(128),
|
|
29
|
+
requestId: requestIdSchema,
|
|
30
|
+
/** The grant's hold, when it had one. */
|
|
31
|
+
reservationId: z.string().min(1).max(128).optional(),
|
|
32
|
+
/** The grant's capacity slot, when it claimed one; settling releases it. */
|
|
33
|
+
meteredUsageId: z.string().min(1).max(128).optional(),
|
|
34
|
+
/** The step that served, by position in the grant. Absent when nothing was served. */
|
|
35
|
+
servedStep: z.number().int().nonnegative().max(7).optional(),
|
|
36
|
+
report: normalizedUsageReportSchema,
|
|
37
|
+
createdAt: inferenceTimestampSchema,
|
|
38
|
+
})
|
|
39
|
+
.strict()
|
|
40
|
+
.superRefine((settlement, ctx) => {
|
|
41
|
+
if (settlement.report.requestId !== settlement.requestId) {
|
|
42
|
+
ctx.addIssue({
|
|
43
|
+
code: 'custom',
|
|
44
|
+
path: ['report', 'requestId'],
|
|
45
|
+
message: 'the report settles this request',
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
if (settlement.report.outcome === 'completed' && settlement.servedStep === undefined) {
|
|
49
|
+
ctx.addIssue({
|
|
50
|
+
code: 'custom',
|
|
51
|
+
path: ['servedStep'],
|
|
52
|
+
message: 'a completed request was served by a step of its grant',
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
/** Oxy's answer to a delivered settlement. A redelivery is `duplicate`, never a second charge. */
|
|
57
|
+
export const settlementAcknowledgementSchema = z
|
|
58
|
+
.object({
|
|
59
|
+
schemaVersion: z.literal(1),
|
|
60
|
+
settlementId: z.string().min(1).max(128),
|
|
61
|
+
status: z.enum(['settled', 'duplicate']),
|
|
62
|
+
})
|
|
63
|
+
.strict();
|
|
64
|
+
/** `GET /internal/v1/settlements?cursor=`: what Oxy reconciles against. */
|
|
65
|
+
export const settlementPageSchema = z
|
|
66
|
+
.object({
|
|
67
|
+
data: z.array(settlementSchema),
|
|
68
|
+
/** Absent on the last page. */
|
|
69
|
+
nextCursor: z.string().min(1).max(512).optional(),
|
|
70
|
+
})
|
|
71
|
+
.strict();
|