-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathschema.ts
More file actions
211 lines (203 loc) · 6.25 KB
/
Copy pathschema.ts
File metadata and controls
211 lines (203 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { z } from "zod";
import { ModelFamily } from "./family";
const Cost = z.object({
input: z.number().min(0, "Input price cannot be negative"),
output: z.number().min(0, "Output price cannot be negative"),
reasoning: z.number().min(0, "Input price cannot be negative").optional(),
cache_read: z
.number()
.min(0, "Cache read price cannot be negative")
.optional(),
cache_write: z
.number()
.min(0, "Cache write price cannot be negative")
.optional(),
input_audio: z
.number()
.min(0, "Audio input price cannot be negative")
.optional(),
output_audio: z
.number()
.min(0, "Audio output price cannot be negative")
.optional(),
});
/**
* A pricing tier that applies when the total input tokens (input + cache_read)
* meet or exceed the min_context threshold. Matches OpenRouter's tiered pricing
* format: https://openrouter.ai/docs/guides/get-started/for-providers
*/
const ContextTier = z.object({
min_context: z
.number()
.int()
.positive("min_context must be a positive integer (token count)"),
input: z.number().min(0, "Input price cannot be negative"),
output: z.number().min(0, "Output price cannot be negative"),
cache_read: z
.number()
.min(0, "Cache read price cannot be negative")
.optional(),
cache_write: z
.number()
.min(0, "Cache write price cannot be negative")
.optional(),
});
export const Model = z
.object({
id: z.string(),
name: z.string().min(1, "Model name cannot be empty"),
family: ModelFamily.optional(),
attachment: z.boolean(),
reasoning: z.boolean(),
tool_call: z.boolean(),
interleaved: z
.union([
z.literal(true),
z
.object({
field: z.enum(["reasoning_content", "reasoning_details"]),
})
.strict(),
])
.optional(),
structured_output: z.boolean().optional(),
temperature: z.boolean().optional(),
knowledge: z
.string()
.regex(/^\d{4}-\d{2}(-\d{2})?$/, {
message: "Must be in YYYY-MM or YYYY-MM-DD format",
})
.optional(),
release_date: z.string().regex(/^\d{4}-\d{2}(-\d{2})?$/, {
message: "Must be in YYYY-MM or YYYY-MM-DD format",
}),
last_updated: z.string().regex(/^\d{4}-\d{2}(-\d{2})?$/, {
message: "Must be in YYYY-MM or YYYY-MM-DD format",
}),
modalities: z.object({
input: z.array(z.enum(["text", "audio", "image", "video", "pdf"])),
output: z.array(z.enum(["text", "audio", "image", "video", "pdf"])),
}),
open_weights: z.boolean(),
cost: Cost.extend({
/**
* @deprecated Use `context_tiers` instead.
* Kept for backward compatibility — consumers should merge this into
* context_tiers if present.
*/
context_over_200k: Cost.optional(),
/**
* Ordered array of pricing tiers that apply when total input tokens
* (input + cache_read) meet or exceed the tier's min_context threshold.
* Only the highest matching tier is applied.
*
* Example (TOML):
* ```toml
* [[cost.context_tiers]]
* min_context = 200_000
* input = 2.00
* output = 6.00
* cache_read = 0.40
*
* [[cost.context_tiers]]
* min_context = 500_000
* input = 4.00
* output = 12.00
* cache_read = 0.80
* ```
*/
context_tiers: z
.array(ContextTier)
.max(4, "Maximum 4 context tiers supported")
.optional(),
}).optional(),
limit: z.object({
context: z.number().min(0, "Context window must be positive"),
input: z.number().min(0, "Input tokens must be positive").optional(),
output: z.number().min(0, "Output tokens must be positive"),
}),
status: z.enum(["alpha", "beta", "deprecated"]).optional(),
provider: z
.object({
npm: z.string().optional(),
api: z.string().optional(),
shape: z.enum(["responses", "completions"]).optional(),
})
.optional(),
})
.strict()
.refine(
(data) => {
return !(data.reasoning === false && data.cost?.reasoning !== undefined);
},
{
message: "Cannot set cost.reasoning when reasoning is false",
path: ["cost", "reasoning"],
},
)
.refine(
(data) => {
const tiers = data.cost?.context_tiers;
if (!tiers || tiers.length < 2) return true;
// Ensure tiers are sorted by min_context ascending
for (let i = 1; i < tiers.length; i++) {
if (tiers[i]!.min_context <= tiers[i - 1]!.min_context) {
return false;
}
}
return true;
},
{
message:
"context_tiers must be sorted by min_context in ascending order",
path: ["cost", "context_tiers"],
},
);
export type Model = z.infer<typeof Model>;
export const Provider = z
.object({
id: z.string(),
env: z.array(z.string()).min(1, "Provider env cannot be empty"),
npm: z.string().min(1, "Provider npm module cannot be empty"),
api: z.string().optional(),
name: z.string().min(1, "Provider name cannot be empty"),
doc: z
.string()
.min(
1,
"Please provide a link to the provider documentation where models are listed",
),
models: z.record(Model),
})
.strict()
.refine(
(data) => {
const isOpenAI = data.npm === "@ai-sdk/openai";
const isOpenAIcompatible = data.npm === "@ai-sdk/openai-compatible";
const isOpenrouter = data.npm === "@openrouter/ai-sdk-provider";
const isAnthropic = data.npm === "@ai-sdk/anthropic";
const hasApi = data.api !== undefined;
return (
// openai-compatible: must have api
(isOpenAIcompatible && hasApi) ||
// openrouter: must have api
(isOpenrouter && hasApi) ||
// anthropic: api optional (always allowed)
isAnthropic ||
// openai: api optional (always allowed)
isOpenAI ||
// all others: must NOT have api
(!isOpenAI &&
!isOpenAIcompatible &&
!isOpenrouter &&
!isAnthropic &&
!hasApi)
);
},
{
message:
"'api' is required for openai-compatible and openrouter, optional for anthropic and openai, forbidden otherwise",
path: ["api"],
},
);
export type Provider = z.infer<typeof Provider>;