@modelprofile.com/flexharness-providers 16.1.0 → 16.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/dist_ts_providers/anthropic/index.d.ts +1 -0
- package/dist_ts_providers/anthropic/index.js +2 -1
- package/dist_ts_providers/anthropic/plugins.d.ts +3 -3
- package/dist_ts_providers/anthropic/plugins.js +3 -3
- package/dist_ts_providers/anthropic/smartai.pricing.anthropic.d.ts +57 -0
- package/dist_ts_providers/anthropic/smartai.pricing.anthropic.js +245 -0
- package/dist_ts_providers/openai/index.d.ts +4 -1
- package/dist_ts_providers/openai/index.js +6 -4
- package/dist_ts_providers/openai/plugins.d.ts +3 -3
- package/dist_ts_providers/openai/plugins.js +3 -3
- package/dist_ts_providers/openai/smartai.apikey.openai.d.ts +67 -0
- package/dist_ts_providers/openai/smartai.apikey.openai.js +171 -0
- package/dist_ts_providers/openai/smartai.cache.openai.d.ts +3 -0
- package/dist_ts_providers/openai/smartai.cache.openai.js +11 -1
- package/dist_ts_providers/openai/smartai.chatgpt.catalog.d.ts +5 -0
- package/dist_ts_providers/openai/smartai.chatgpt.catalog.js +11 -7
- package/dist_ts_providers/openai/smartai.models.openai.d.ts +60 -0
- package/dist_ts_providers/openai/smartai.models.openai.js +250 -0
- package/package.json +2 -2
- package/ts_providers/anthropic/index.ts +9 -0
- package/ts_providers/anthropic/plugins.ts +7 -0
- package/ts_providers/anthropic/readme.md +41 -0
- package/ts_providers/anthropic/smartai.pricing.anthropic.ts +285 -0
- package/ts_providers/openai/index.ts +26 -5
- package/ts_providers/openai/plugins.ts +7 -0
- package/ts_providers/openai/readme.md +50 -1
- package/ts_providers/openai/smartai.apikey.openai.ts +215 -0
- package/ts_providers/openai/smartai.cache.openai.ts +17 -0
- package/ts_providers/openai/smartai.chatgpt.catalog.ts +11 -6
- package/ts_providers/openai/smartai.models.openai.ts +312 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import * as plugins from './plugins.js';
|
|
2
|
+
|
|
3
|
+
const checkedAt = '2026-10-21';
|
|
4
|
+
const pricingSource: plugins.IModelSource = { url: 'https://platform.claude.com/docs/en/about-claude/pricing', checkedAt };
|
|
5
|
+
|
|
6
|
+
/** USD per million tokens as an integer rate (micro-USD per million tokens). */
|
|
7
|
+
const usd = (dollarsArg: number): number => Math.round(dollarsArg * 1_000_000);
|
|
8
|
+
|
|
9
|
+
/** A price row as the pricing page prints it: base input, 5-minute write, 1-hour write, cache hit, output. */
|
|
10
|
+
type TAnthropicPriceRow = readonly [input: number, write5m: number, write1h: number, cacheHit: number, output: number];
|
|
11
|
+
|
|
12
|
+
const rates = ([input, write5m, write1h, cacheHit, output]: TAnthropicPriceRow): plugins.IModelTokenRates => ({
|
|
13
|
+
input: usd(input), output: usd(output), cacheRead: usd(cacheHit), cacheWrite: { '5m': usd(write5m), '1h': usd(write1h) },
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const flat = (rowArg: TAnthropicPriceRow): plugins.IModelServicePrice => ({ bands: [{ rates: rates(rowArg) }] });
|
|
17
|
+
|
|
18
|
+
/** Web search is $10 per 1,000 searches; web fetch costs nothing beyond its tokens. */
|
|
19
|
+
const toolCallFees = { web_search: 10_000, web_fetch: 0 };
|
|
20
|
+
|
|
21
|
+
/** `inference_geo: "us"` multiplies every token category by 1.1 on Claude 4.6 and later models. */
|
|
22
|
+
const usInference = { 'inference_geo:us': 1100 };
|
|
23
|
+
|
|
24
|
+
interface IAnthropicPriceEntry {
|
|
25
|
+
modelId: string;
|
|
26
|
+
aliases?: readonly string[];
|
|
27
|
+
standard: plugins.IModelServicePrice;
|
|
28
|
+
/** Fast mode (`speed: "fast"`), on the models the page prices it for. */
|
|
29
|
+
fast?: plugins.IModelServicePrice;
|
|
30
|
+
/** Whether the model takes `inference_geo` (Claude 4.6 and later). */
|
|
31
|
+
inferenceGeo: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const anthropicPrice = (entryArg: IAnthropicPriceEntry): plugins.IModelPrice => ({
|
|
35
|
+
providerId: 'anthropic', modelId: entryArg.modelId,
|
|
36
|
+
...(entryArg.aliases ? { aliases: entryArg.aliases } : {}),
|
|
37
|
+
services: { standard: entryArg.standard, ...(entryArg.fast ? { fast: entryArg.fast } : {}) },
|
|
38
|
+
toolCallFees,
|
|
39
|
+
...(entryArg.inferenceGeo ? { surcharges: usInference } : {}),
|
|
40
|
+
source: pricingSource,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const deepFreeze = <T>(valueArg: T): T => {
|
|
44
|
+
if (typeof valueArg === 'object' && valueArg !== null && !Object.isFrozen(valueArg)) {
|
|
45
|
+
Object.freeze(valueArg);
|
|
46
|
+
for (const entry of Object.values(valueArg)) deepFreeze(entry);
|
|
47
|
+
}
|
|
48
|
+
return valueArg;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The Claude API prices of the models Anthropic lists as available, from
|
|
53
|
+
* https://platform.claude.com/docs/en/about-claude/pricing, checked 2026-10-21, keyed by the service the response
|
|
54
|
+
* reports: `standard`, and `fast` for Fast mode. Priority Tier has no published per-token price, so it is absent and a
|
|
55
|
+
* call served in it is refused. Fast-mode cache rates apply the page's caching multipliers to the Fast-mode base
|
|
56
|
+
* price (1.25× and 2× for writes; 0.05× hits on Claude Opus 5.5, 0.1× otherwise). Model ids and dated snapshots are
|
|
57
|
+
* from the model pages (https://platform.claude.com/docs/en/models/<model>/overview).
|
|
58
|
+
*/
|
|
59
|
+
export const ANTHROPIC_MODEL_PRICES: readonly plugins.IModelPrice[] = deepFreeze([
|
|
60
|
+
anthropicPrice({ modelId: 'claude-fable-5-1', standard: flat([10, 12.5, 20, 0.25, 50]), inferenceGeo: true }),
|
|
61
|
+
anthropicPrice({
|
|
62
|
+
modelId: 'claude-opus-5-5', standard: flat([4, 5, 8, 0.2, 20]), fast: flat([8, 10, 16, 0.4, 40]), inferenceGeo: true,
|
|
63
|
+
}),
|
|
64
|
+
anthropicPrice({ modelId: 'claude-sonnet-5-5', standard: flat([2, 2.5, 4, 0.1, 10]), inferenceGeo: true }),
|
|
65
|
+
anthropicPrice({
|
|
66
|
+
modelId: 'claude-haiku-5-5',
|
|
67
|
+
// Priced by prompt length: a prompt over 100,000 tokens, cache reads and writes included, pays the higher prices.
|
|
68
|
+
standard: { bands: [
|
|
69
|
+
{ maxPromptTokens: 100_000, rates: rates([0.1, 0.125, 0.2, 0.01, 0.5]) },
|
|
70
|
+
{ rates: rates([0.5, 0.625, 1, 0.05, 2.5]) },
|
|
71
|
+
] },
|
|
72
|
+
inferenceGeo: true,
|
|
73
|
+
}),
|
|
74
|
+
anthropicPrice({ modelId: 'claude-fable-5', standard: flat([10, 12.5, 20, 1, 50]), inferenceGeo: true }),
|
|
75
|
+
anthropicPrice({
|
|
76
|
+
modelId: 'claude-opus-5', standard: flat([5, 6.25, 10, 0.5, 25]), fast: flat([10, 12.5, 20, 1, 50]), inferenceGeo: true,
|
|
77
|
+
}),
|
|
78
|
+
anthropicPrice({
|
|
79
|
+
modelId: 'claude-opus-4-8', standard: flat([5, 6.25, 10, 0.5, 25]), fast: flat([10, 12.5, 20, 1, 50]), inferenceGeo: true,
|
|
80
|
+
}),
|
|
81
|
+
anthropicPrice({ modelId: 'claude-opus-4-7', standard: flat([5, 6.25, 10, 0.5, 25]), inferenceGeo: true }),
|
|
82
|
+
anthropicPrice({ modelId: 'claude-opus-4-6', standard: flat([5, 6.25, 10, 0.5, 25]), inferenceGeo: true }),
|
|
83
|
+
anthropicPrice({
|
|
84
|
+
modelId: 'claude-opus-4-5', aliases: ['claude-opus-4-5-20251101'], standard: flat([5, 6.25, 10, 0.5, 25]), inferenceGeo: false,
|
|
85
|
+
}),
|
|
86
|
+
anthropicPrice({ modelId: 'claude-sonnet-5', standard: flat([2, 2.5, 4, 0.2, 10]), inferenceGeo: true }),
|
|
87
|
+
anthropicPrice({ modelId: 'claude-sonnet-4-6', standard: flat([3, 3.75, 6, 0.3, 15]), inferenceGeo: true }),
|
|
88
|
+
anthropicPrice({
|
|
89
|
+
modelId: 'claude-sonnet-4-5', aliases: ['claude-sonnet-4-5-20250929'], standard: flat([3, 3.75, 6, 0.3, 15]), inferenceGeo: false,
|
|
90
|
+
}),
|
|
91
|
+
anthropicPrice({
|
|
92
|
+
modelId: 'claude-haiku-4-5', aliases: ['claude-haiku-4-5-20251001'], standard: flat([1, 1.25, 2, 0.1, 5]), inferenceGeo: false,
|
|
93
|
+
}),
|
|
94
|
+
] satisfies plugins.IModelPrice[]);
|
|
95
|
+
|
|
96
|
+
/** The price of exactly this model id or one of its listed snapshots; `undefined` for any other id. */
|
|
97
|
+
export function getAnthropicModelPrice(modelIdArg: string): plugins.IModelPrice | undefined {
|
|
98
|
+
return ANTHROPIC_MODEL_PRICES.find((price) => price.modelId === modelIdArg || (price.aliases ?? []).includes(modelIdArg));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
102
|
+
// Server tools
|
|
103
|
+
|
|
104
|
+
/** What one server-tool `type` of a Messages request costs beyond the model's tokens. */
|
|
105
|
+
export interface IAnthropicServerToolPrice {
|
|
106
|
+
/** The exact `type` of the tool definition in the request's `tools`. */
|
|
107
|
+
type: string;
|
|
108
|
+
/**
|
|
109
|
+
* Integer micro-USD per call, by the name `usage.server_tool_use` counts the calls under (`web_search` for
|
|
110
|
+
* `web_search_requests`), for every call a request declaring this tool may be billed for. `{}` is a tool that
|
|
111
|
+
* costs only its tokens and has no counter.
|
|
112
|
+
*/
|
|
113
|
+
toolCallFees: Readonly<Record<string, number>>;
|
|
114
|
+
source: plugins.IModelSource;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const serverToolsCheckedAt = '2026-10-22';
|
|
118
|
+
const serverToolPricingSource: plugins.IModelSource = { url: pricingSource.url, checkedAt: serverToolsCheckedAt };
|
|
119
|
+
const toolSearchSource: plugins.IModelSource = {
|
|
120
|
+
url: 'https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool', checkedAt: serverToolsCheckedAt,
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* With `web_search_20260209`, `web_fetch_20260209` or a later version in the request, code execution in that request
|
|
125
|
+
* (dynamic filtering and code Claude runs directly) costs nothing beyond its tokens.
|
|
126
|
+
*/
|
|
127
|
+
const freeCodeExecution = { code_execution: 0 };
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The server tools whose cost is published, by exact `type`: every version of web search and web fetch the tool
|
|
131
|
+
* reference lists (https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-reference), at the fees of
|
|
132
|
+
* https://platform.claude.com/docs/en/about-claude/pricing ($10 per 1,000 searches, no fee for fetches, free code
|
|
133
|
+
* execution with the 2026 versions), and the dated tool search variants, which are not metered and cost only the
|
|
134
|
+
* tool definitions they load, as input tokens (the tool search page's "Usage"), all checked 2026-10-22. Undated
|
|
135
|
+
* aliases (`tool_search_tool_regex`, which resolves to the latest version), code execution (billed by container
|
|
136
|
+
* time), the advisor (billed at the advisor model's token rates) and the MCP connector are absent, so a request
|
|
137
|
+
* declaring them cannot be priced.
|
|
138
|
+
*/
|
|
139
|
+
export const ANTHROPIC_SERVER_TOOL_PRICES: readonly IAnthropicServerToolPrice[] = deepFreeze<IAnthropicServerToolPrice[]>([
|
|
140
|
+
{ type: 'web_search_20250305', toolCallFees: { web_search: toolCallFees.web_search }, source: serverToolPricingSource },
|
|
141
|
+
{ type: 'web_search_20260209', toolCallFees: { web_search: toolCallFees.web_search, ...freeCodeExecution }, source: serverToolPricingSource },
|
|
142
|
+
{ type: 'web_search_20260318', toolCallFees: { web_search: toolCallFees.web_search, ...freeCodeExecution }, source: serverToolPricingSource },
|
|
143
|
+
{ type: 'web_fetch_20250910', toolCallFees: { web_fetch: toolCallFees.web_fetch }, source: serverToolPricingSource },
|
|
144
|
+
{ type: 'web_fetch_20260209', toolCallFees: { web_fetch: toolCallFees.web_fetch, ...freeCodeExecution }, source: serverToolPricingSource },
|
|
145
|
+
{ type: 'web_fetch_20260309', toolCallFees: { web_fetch: toolCallFees.web_fetch, ...freeCodeExecution }, source: serverToolPricingSource },
|
|
146
|
+
{ type: 'web_fetch_20260318', toolCallFees: { web_fetch: toolCallFees.web_fetch, ...freeCodeExecution }, source: serverToolPricingSource },
|
|
147
|
+
{ type: 'tool_search_tool_regex_20251119', toolCallFees: {}, source: toolSearchSource },
|
|
148
|
+
{ type: 'tool_search_tool_bm25_20251119', toolCallFees: {}, source: toolSearchSource },
|
|
149
|
+
]);
|
|
150
|
+
|
|
151
|
+
/** The price of exactly this server-tool `type`; `undefined` for any other type, which a request cannot be priced with. */
|
|
152
|
+
export function getAnthropicServerToolPrice(typeArg: string): IAnthropicServerToolPrice | undefined {
|
|
153
|
+
return ANTHROPIC_SERVER_TOOL_PRICES.find((tool) => tool.type === typeArg);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The price of one request's answers: the model's price with the call fees of the server tools the request declares
|
|
158
|
+
* (`serverToolTypesArg`, the `type` of each server tool in its `tools`; client tools are not passed). `undefined`
|
|
159
|
+
* when one of the types has no price, so the request cannot be priced.
|
|
160
|
+
*/
|
|
161
|
+
export function getAnthropicRequestPrice(
|
|
162
|
+
priceArg: plugins.IModelPrice,
|
|
163
|
+
serverToolTypesArg: readonly string[],
|
|
164
|
+
): plugins.IModelPrice | undefined {
|
|
165
|
+
const fees: Record<string, number> = { ...priceArg.toolCallFees };
|
|
166
|
+
for (const type of serverToolTypesArg) {
|
|
167
|
+
const tool = getAnthropicServerToolPrice(type);
|
|
168
|
+
if (!tool) return undefined;
|
|
169
|
+
for (const [name, fee] of Object.entries(tool.toolCallFees)) {
|
|
170
|
+
if (Object.prototype.hasOwnProperty.call(fees, name) && fees[name] !== fee) {
|
|
171
|
+
throw new Error(`Anthropic prices disagree on the ${name} call fee of ${priceArg.modelId} and ${type}.`);
|
|
172
|
+
}
|
|
173
|
+
fees[name] = fee;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return { ...priceArg, toolCallFees: fees };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const isRecord = (valueArg: unknown): valueArg is Record<string, unknown> =>
|
|
180
|
+
typeof valueArg === 'object' && valueArg !== null && !Array.isArray(valueArg);
|
|
181
|
+
|
|
182
|
+
const tokenCount = (valueArg: unknown, nameArg: string): number => {
|
|
183
|
+
if (!Number.isSafeInteger(valueArg) || (valueArg as number) < 0) {
|
|
184
|
+
throw new plugins.ModelUsageReadError(`Anthropic usage ${nameArg} is not a count.`);
|
|
185
|
+
}
|
|
186
|
+
return valueArg as number;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const optionalTokenCount = (valueArg: unknown, nameArg: string): number => valueArg === undefined || valueArg === null
|
|
190
|
+
? 0
|
|
191
|
+
: tokenCount(valueArg, nameArg);
|
|
192
|
+
|
|
193
|
+
const optionalRecord = (valueArg: unknown, nameArg: string): Record<string, unknown> | undefined => {
|
|
194
|
+
if (valueArg === undefined || valueArg === null) return undefined;
|
|
195
|
+
if (!isRecord(valueArg)) throw new plugins.ModelUsageReadError(`Anthropic usage ${nameArg} is not an object.`);
|
|
196
|
+
return valueArg;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* The priced service of a tier and speed: `standard` at standard speed, `fast` for Fast mode on the standard tier.
|
|
201
|
+
* Any other speed names a service no price has (`<tier>:speed:<speed>`), so the call is refused as unpriced.
|
|
202
|
+
*/
|
|
203
|
+
const serviceOf = (tierArg: string, speedArg: string | undefined): string => {
|
|
204
|
+
if (speedArg === undefined || speedArg === 'standard') return tierArg;
|
|
205
|
+
if (speedArg === 'fast' && tierArg === 'standard') return 'fast';
|
|
206
|
+
return `${tierArg}:speed:${speedArg}`;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Accepts `usage.iterations` only when every entry is a `message` iteration, whose tokens the top-level counts already
|
|
211
|
+
* sum. Any other iteration, such as an advisor's `advisor_message`, is billed at rates of its own and left out of the
|
|
212
|
+
* top-level counts (https://platform.claude.com/docs/en/agents-and-tools/tool-use/advisor-tool), so the usage
|
|
213
|
+
* cannot be read completely.
|
|
214
|
+
*/
|
|
215
|
+
const readIterations = (valueArg: unknown): void => {
|
|
216
|
+
if (valueArg === undefined || valueArg === null) return;
|
|
217
|
+
if (!Array.isArray(valueArg)) throw new plugins.ModelUsageReadError('Anthropic usage iterations is not a list.');
|
|
218
|
+
for (const iteration of valueArg) {
|
|
219
|
+
if (!isRecord(iteration) || typeof iteration.type !== 'string') {
|
|
220
|
+
throw new plugins.ModelUsageReadError('Anthropic usage names an unreadable iteration.');
|
|
221
|
+
}
|
|
222
|
+
if (iteration.type !== 'message') {
|
|
223
|
+
throw new plugins.ModelUsageReadError(`Anthropic usage has a ${iteration.type} iteration, which its top-level counts leave out.`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Reads a Messages API `usage` object, raw as the API sends it at the end of a call (a stream's usage merged from
|
|
230
|
+
* `message_start` and the last `message_delta`).
|
|
231
|
+
*
|
|
232
|
+
* `input_tokens` excludes the cache; cache writes are split by lifetime from `cache_creation`
|
|
233
|
+
* (`ephemeral_5m_input_tokens`, `ephemeral_1h_input_tokens`), and writes it does not split go to
|
|
234
|
+
* `unknownLifetimeCacheWriteTokens`. The service is `service_tier`, or `fast` for a standard-tier call whose `speed`
|
|
235
|
+
* is `fast`; any other speed names a service no price has. An `inference_geo` other than `global` is the surcharge
|
|
236
|
+
* `inference_geo:<value>`, which a price without that surcharge refuses. Server-tool calls come
|
|
237
|
+
* from `server_tool_use` (`web_search_requests` as `web_search`, and so on). A usage that is not complete throws a
|
|
238
|
+
* `ModelUsageReadError`, and so does one with a `usage.iterations` entry other than `message` (an advisor's
|
|
239
|
+
* `advisor_message`, for example), whose tokens the top-level counts leave out.
|
|
240
|
+
*/
|
|
241
|
+
export function readAnthropicMessagesUsage(rawUsageArg: unknown): plugins.IModelPricedUsage {
|
|
242
|
+
if (!isRecord(rawUsageArg)) throw new plugins.ModelUsageReadError('Anthropic usage is not an object.');
|
|
243
|
+
const serviceTier = rawUsageArg.service_tier;
|
|
244
|
+
if (typeof serviceTier !== 'string' || serviceTier.length === 0) {
|
|
245
|
+
throw new plugins.ModelUsageReadError('Anthropic usage names no service tier.');
|
|
246
|
+
}
|
|
247
|
+
const speed = rawUsageArg.speed;
|
|
248
|
+
if (speed !== undefined && speed !== null && (typeof speed !== 'string' || speed.length === 0)) {
|
|
249
|
+
throw new plugins.ModelUsageReadError('Anthropic usage names an unreadable speed.');
|
|
250
|
+
}
|
|
251
|
+
const writeTotal = optionalTokenCount(rawUsageArg.cache_creation_input_tokens, 'cache_creation_input_tokens');
|
|
252
|
+
const cacheCreation = optionalRecord(rawUsageArg.cache_creation, 'cache_creation');
|
|
253
|
+
const cacheWriteTokens: Record<string, number> = {};
|
|
254
|
+
let unsplitWrites = writeTotal;
|
|
255
|
+
if (cacheCreation) {
|
|
256
|
+
const write5m = optionalTokenCount(cacheCreation.ephemeral_5m_input_tokens, 'ephemeral_5m_input_tokens');
|
|
257
|
+
const write1h = optionalTokenCount(cacheCreation.ephemeral_1h_input_tokens, 'ephemeral_1h_input_tokens');
|
|
258
|
+
unsplitWrites = writeTotal - write5m - write1h;
|
|
259
|
+
if (unsplitWrites < 0) throw new plugins.ModelUsageReadError('Anthropic usage splits more cache writes than it counts.');
|
|
260
|
+
if (write5m > 0) cacheWriteTokens['5m'] = write5m;
|
|
261
|
+
if (write1h > 0) cacheWriteTokens['1h'] = write1h;
|
|
262
|
+
}
|
|
263
|
+
const toolCalls: Record<string, number> = {};
|
|
264
|
+
const serverToolUse = optionalRecord(rawUsageArg.server_tool_use, 'server_tool_use');
|
|
265
|
+
for (const [name, count] of Object.entries(serverToolUse ?? {})) {
|
|
266
|
+
if (!name.endsWith('_requests')) continue;
|
|
267
|
+
const calls = optionalTokenCount(count, name);
|
|
268
|
+
if (calls > 0) toolCalls[name.slice(0, -'_requests'.length)] = calls;
|
|
269
|
+
}
|
|
270
|
+
const geo = rawUsageArg.inference_geo;
|
|
271
|
+
if (geo !== undefined && geo !== null && (typeof geo !== 'string' || geo.length === 0)) {
|
|
272
|
+
throw new plugins.ModelUsageReadError('Anthropic usage names an unreadable inference_geo.');
|
|
273
|
+
}
|
|
274
|
+
readIterations(rawUsageArg.iterations);
|
|
275
|
+
return {
|
|
276
|
+
service: serviceOf(serviceTier, typeof speed === 'string' ? speed : undefined),
|
|
277
|
+
uncachedInputTokens: tokenCount(rawUsageArg.input_tokens, 'input_tokens'),
|
|
278
|
+
cacheReadTokens: optionalTokenCount(rawUsageArg.cache_read_input_tokens, 'cache_read_input_tokens'),
|
|
279
|
+
cacheWriteTokens,
|
|
280
|
+
...(unsplitWrites > 0 ? { unknownLifetimeCacheWriteTokens: unsplitWrites } : {}),
|
|
281
|
+
outputTokens: tokenCount(rawUsageArg.output_tokens, 'output_tokens'),
|
|
282
|
+
...(Object.keys(toolCalls).length > 0 ? { toolCalls } : {}),
|
|
283
|
+
...(typeof geo === 'string' && geo !== 'global' ? { surcharges: [`inference_geo:${geo}`] } : {}),
|
|
284
|
+
};
|
|
285
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as plugins from './plugins.js';
|
|
2
|
-
import { readOpenAiEffectiveCachePolicy } from './smartai.cache.openai.js';
|
|
2
|
+
import { isOpenAiCacheTransparentTransport, readOpenAiEffectiveCachePolicy } from './smartai.cache.openai.js';
|
|
3
3
|
import { classifyOpenAiLimit } from './smartai.limits.openai.js';
|
|
4
|
+
import { OPENAI_API_BASE_URL } from './smartai.apikey.openai.js';
|
|
4
5
|
|
|
5
6
|
export { classifyOpenAiLimit, readOpenAiError, type IOpenAiErrorDetails } from './smartai.limits.openai.js';
|
|
6
7
|
export { classifyOpenAiChatGptLimit } from './smartai.chatgpt.limits.js';
|
|
@@ -33,8 +34,30 @@ export {
|
|
|
33
34
|
type TOpenAiChatGptInputModality,
|
|
34
35
|
type TOpenAiChatGptModelListErrorCode,
|
|
35
36
|
} from './smartai.chatgpt.catalog.js';
|
|
37
|
+
export {
|
|
38
|
+
createOpenAiApiKeyConnection,
|
|
39
|
+
createOpenAiApiKeyHeaders,
|
|
40
|
+
isOpenAiApiRequestUrl,
|
|
41
|
+
listOpenAiApiKeyModels,
|
|
42
|
+
OPENAI_API_BASE_URL,
|
|
43
|
+
OpenAiApiKeyAuthError,
|
|
44
|
+
OpenAiApiModelListError,
|
|
45
|
+
type IOpenAiApiKeyAccess,
|
|
46
|
+
type IOpenAiApiKeyConnectionOptions,
|
|
47
|
+
type IOpenAiApiKeyModel,
|
|
48
|
+
type IOpenAiApiKeyModelListOptions,
|
|
49
|
+
} from './smartai.apikey.openai.js';
|
|
50
|
+
export {
|
|
51
|
+
getOpenAiApiModel,
|
|
52
|
+
getOpenAiModelPrice,
|
|
53
|
+
OPENAI_API_MODELS,
|
|
54
|
+
OPENAI_MODEL_PRICES,
|
|
55
|
+
readOpenAiResponsesUsage,
|
|
56
|
+
type IOpenAiApiModel,
|
|
57
|
+
} from './smartai.models.openai.js';
|
|
36
58
|
|
|
37
|
-
|
|
59
|
+
/** A Responses API `reasoning.effort`; which ones a model accepts is the model's (`OPENAI_API_MODELS`). */
|
|
60
|
+
export type TOpenAiReasoningEffort = 'none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max';
|
|
38
61
|
export type TOpenAiTextVerbosity = 'low' | 'medium' | 'high';
|
|
39
62
|
/** `responses` calls `/responses`; `chat` calls `/chat/completions`, the API most OpenAI-compatible servers implement. */
|
|
40
63
|
export type TOpenAiApi = 'responses' | 'chat';
|
|
@@ -64,8 +87,6 @@ export interface IOpenAiModelOptions extends plugins.ISmartAiOptions {
|
|
|
64
87
|
api?: TOpenAiApi;
|
|
65
88
|
}
|
|
66
89
|
|
|
67
|
-
const OPENAI_API_BASE_URL = 'https://api.openai.com/v1';
|
|
68
|
-
|
|
69
90
|
export const createOpenAiModelProvider = (): plugins.IModelProvider<IOpenAiModelOptions, plugins.LanguageModelV4> => ({
|
|
70
91
|
id: 'openai',
|
|
71
92
|
getModel: (options) => {
|
|
@@ -92,7 +113,7 @@ export const createOpenAiModelProvider = (): plugins.IModelProvider<IOpenAiModel
|
|
|
92
113
|
policy: readOpenAiEffectiveCachePolicy(
|
|
93
114
|
input instanceof Request ? input.url : String(input),
|
|
94
115
|
init?.body,
|
|
95
|
-
settings.fetch
|
|
116
|
+
isOpenAiCacheTransparentTransport(settings.fetch),
|
|
96
117
|
),
|
|
97
118
|
});
|
|
98
119
|
return (settings.fetch ?? globalThis.fetch)(input, init);
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
createTextFilePartMiddleware,
|
|
14
14
|
readEpochSeconds,
|
|
15
15
|
readJsonRecord,
|
|
16
|
+
ModelUsageReadError,
|
|
16
17
|
registerSmartAiCachePolicyObserver,
|
|
17
18
|
} from '@modelprofile.com/flexharness-models';
|
|
18
19
|
export {
|
|
@@ -20,10 +21,16 @@ export {
|
|
|
20
21
|
createTextFilePartMiddleware,
|
|
21
22
|
readEpochSeconds,
|
|
22
23
|
readJsonRecord,
|
|
24
|
+
ModelUsageReadError,
|
|
23
25
|
registerSmartAiCachePolicyObserver,
|
|
24
26
|
};
|
|
25
27
|
export type {
|
|
26
28
|
IModelCallFailure,
|
|
29
|
+
IModelPrice,
|
|
30
|
+
IModelPricedUsage,
|
|
31
|
+
IModelServicePrice,
|
|
32
|
+
IModelSource,
|
|
33
|
+
IModelTokenRates,
|
|
27
34
|
IModelProvider,
|
|
28
35
|
ISmartAiOpenAiEffectiveCachePolicy,
|
|
29
36
|
ISmartAiOptions,
|
|
@@ -121,6 +121,54 @@ The `/auth` subpath keeps its connections, which derive identity from the token
|
|
|
121
121
|
re-exports the ChatGPT limit classifier, the instructions middleware, the Codex base URL, the
|
|
122
122
|
default originator and `OpenAiChatGptAuthError` from here.
|
|
123
123
|
|
|
124
|
+
## OpenAI API key
|
|
125
|
+
|
|
126
|
+
`createOpenAiApiKeyConnection({ apiKey, fetch? })` is an OpenAI API connection on an API key, for a gateway
|
|
127
|
+
that holds the key and forwards requests for its clients. It sends the request to
|
|
128
|
+
`https://api.openai.com/v1/*` only (`OPENAI_API_BASE_URL`; `isOpenAiApiRequestUrl` is the check), with
|
|
129
|
+
`redirect: 'error'`, `Authorization: Bearer <key>` replacing any credential the request carried, and without
|
|
130
|
+
`originator`, `ChatGPT-Account-ID`, `X-OpenAI-Fedramp`, `OpenAI-Organization` and `OpenAI-Project`: the key
|
|
131
|
+
bills to the organization and project it was issued for, and a client cannot move the request to another one
|
|
132
|
+
the key reaches. Every other header and the body travel unchanged. A 401 (key rejected) or 403 (key without the
|
|
133
|
+
permission the request needs, such as a restricted key) fails with `OpenAiApiKeyAuthError` whose `status` names
|
|
134
|
+
it; every other status is the caller's. Use it as `connection` of
|
|
135
|
+
`createOpenAiModelProvider().getModel(...)`, on either API, or through `connection.settings.fetch`.
|
|
136
|
+
`createOpenAiApiKeyHeaders` applies the same header rules. Without a custom `fetch` the connection keeps the
|
|
137
|
+
prompt-cache timing of the API endpoint (see below).
|
|
138
|
+
|
|
139
|
+
`listOpenAiApiKeyModels(access, { signal, fetch })` reads `GET /v1/models`, one unpaged list of at most 1 MiB
|
|
140
|
+
and 1000 models, as `{ providerId, modelId, created, ownedBy, shutdownDate? }`, the API's own data. A rejected
|
|
141
|
+
key fails with `OpenAiApiKeyAuthError` (`status` 401), a key without model read likewise with `status` 403; an abort rejects with the signal's reason; other failures
|
|
142
|
+
are `OpenAiApiModelListError` with `NETWORK_FAILED`, `HTTP_ERROR` (and `status`) or `PROTOCOL_ERROR`.
|
|
143
|
+
|
|
144
|
+
`OPENAI_API_MODELS` describes the public-API models (GPT-6 Astra, GPT-6.1 Sol, GPT-6 Sol, GPT-6 Luna, GPT-5.6
|
|
145
|
+
Sol, Terra and Luna, GPT-5.5, GPT-5.4) as their model pages state them, checked 2026-10-21, in the vocabulary of
|
|
146
|
+
`IOpenAiChatGptModel`: `reasoningEfforts` (`TOpenAiReasoningEffort`, `max` included), `defaultReasoningEffort`
|
|
147
|
+
where the page states it (GPT-6 Astra's does not), `inputModalities`, `serviceTiers` beyond Standard (`flex`,
|
|
148
|
+
`priority` for Fast mode, `ultrafast` on GPT-6 Astra and GPT-6.1 Sol), `contextWindow`, `maxInputTokens` where
|
|
149
|
+
stated, `maxOutputTokens`, dated `snapshots` and `source`. `getOpenAiApiModel(id)` matches the id or a listed
|
|
150
|
+
snapshot exactly.
|
|
151
|
+
|
|
152
|
+
### Prices and usage
|
|
153
|
+
|
|
154
|
+
`OPENAI_MODEL_PRICES` holds the prices of those models from https://developers.openai.com/api/docs/pricing,
|
|
155
|
+
checked 2026-10-21, as `IModelPrice` of `@modelprofile.com/flexharness-models`, keyed by the response's
|
|
156
|
+
`service_tier`: `default`, `flex`, `priority` (also under `fast`), and `ultrafast` where the page prices it. Each
|
|
157
|
+
tier has a short-context band (prompts of at most 272,000 input tokens) and a long one; Fast mode on GPT-5.5 and
|
|
158
|
+
GPT-5.4 has no long-context price, so such a call is refused. Cache writes are priced by lifetime (`30m`,
|
|
159
|
+
`12h`); GPT-5.5 and GPT-5.4 charge a write as ordinary input. Web search costs $10 and file search $2.50 per
|
|
160
|
+
1,000 calls. `getOpenAiModelPrice(id)` matches the id or a listed snapshot exactly; any other id is `undefined`.
|
|
161
|
+
A request sent with `service_tier: "default"` is served at Standard prices
|
|
162
|
+
(https://developers.openai.com/api/reference/resources/responses/methods/create); without it, a project set to
|
|
163
|
+
Fast serves the request at Fast prices.
|
|
164
|
+
|
|
165
|
+
`readOpenAiResponsesUsage(usage, serviceTier)` reads a Responses `usage` raw with the response's
|
|
166
|
+
`service_tier`: `input_tokens` includes cache reads and writes; `input_tokens_details.cache_write_tokens_details`
|
|
167
|
+
splits the writes into `30m` and `12h`, and writes it does not split are `unknownLifetimeCacheWriteTokens`.
|
|
168
|
+
The usage names no tool calls: count the response's `web_search_call` and `file_search_call` items into
|
|
169
|
+
`toolCalls`. A usage without a service tier, or with a malformed or inconsistent count, throws
|
|
170
|
+
`ModelUsageReadError`.
|
|
171
|
+
|
|
124
172
|
## Prompt cache timing
|
|
125
173
|
|
|
126
174
|
Each model call reports the cache settings of the request it sent to an observer
|
|
@@ -132,7 +180,8 @@ into the documented idle budget: 30 minutes for GPT-5.6 and later on the API, 30
|
|
|
132
180
|
minutes for `24h` retention on earlier models, and 5 minutes for in-memory
|
|
133
181
|
retention, for no retention and for every ChatGPT request, since that backend
|
|
134
182
|
takes no retention and documents none. API requests count only without a custom
|
|
135
|
-
`fetch`, which may change the body
|
|
183
|
+
`fetch`, which may change the body, or through `createOpenAiApiKeyConnection`
|
|
184
|
+
on the global transport; other endpoints report nothing. The
|
|
136
185
|
response's `input_tokens_details.cached_tokens` arrives as the usage's
|
|
137
186
|
`cacheReadTokens`.
|
|
138
187
|
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import type { IOpenAiModelConnection } from './index.js';
|
|
2
|
+
import { registerOpenAiCacheTransparentTransport } from './smartai.cache.openai.js';
|
|
3
|
+
import {
|
|
4
|
+
readOpenAiBoundedJson,
|
|
5
|
+
type TOpenAiChatGptModelListErrorCode,
|
|
6
|
+
} from './smartai.chatgpt.catalog.js';
|
|
7
|
+
import type { TOpenAiChatGptRequestOutcome } from './smartai.chatgpt.openai.js';
|
|
8
|
+
|
|
9
|
+
/** The OpenAI API every API-key request goes to; the AI SDK's versioned base URL. */
|
|
10
|
+
export const OPENAI_API_BASE_URL = 'https://api.openai.com/v1';
|
|
11
|
+
|
|
12
|
+
export class OpenAiApiKeyAuthError extends Error {
|
|
13
|
+
public status?: number;
|
|
14
|
+
public requestOutcome?: TOpenAiChatGptRequestOutcome;
|
|
15
|
+
|
|
16
|
+
constructor(message: string, options: { status?: number; requestOutcome?: TOpenAiChatGptRequestOutcome } = {}) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = 'OpenAiApiKeyAuthError';
|
|
19
|
+
this.status = options.status;
|
|
20
|
+
this.requestOutcome = options.requestOutcome;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** An OpenAI API key, as the OpenAI platform issues it. */
|
|
25
|
+
export interface IOpenAiApiKeyAccess {
|
|
26
|
+
readonly apiKey: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface IOpenAiApiKeyConnectionOptions extends IOpenAiApiKeyAccess {
|
|
30
|
+
/** Optional underlying transport. Defaults to the current global fetch. */
|
|
31
|
+
readonly fetch?: typeof fetch;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const apiBaseUrl = new URL(OPENAI_API_BASE_URL);
|
|
35
|
+
|
|
36
|
+
/** Whether a request URL is under `OPENAI_API_BASE_URL` and carries no credentials. */
|
|
37
|
+
export function isOpenAiApiRequestUrl(urlArg: string): boolean {
|
|
38
|
+
const url = new URL(urlArg);
|
|
39
|
+
return url.origin === apiBaseUrl.origin && url.username === '' && url.password === ''
|
|
40
|
+
&& (url.pathname === apiBaseUrl.pathname || url.pathname.startsWith(`${apiBaseUrl.pathname}/`));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The statuses that refuse the key itself: 401, a key OpenAI does not accept, and 403, a key without the permission
|
|
45
|
+
* the request needs (a restricted key without model read, for example). Neither changes by sending again.
|
|
46
|
+
*/
|
|
47
|
+
const isKeyRefusal = (statusArg: number): boolean => statusArg === 401 || statusArg === 403;
|
|
48
|
+
|
|
49
|
+
const keyRefused = (statusArg: number, requestOutcomeArg?: TOpenAiChatGptRequestOutcome): OpenAiApiKeyAuthError =>
|
|
50
|
+
new OpenAiApiKeyAuthError(statusArg === 401 ? 'OpenAI rejected the API key.' : 'The API key lacks the permission this request needs.',
|
|
51
|
+
{ status: statusArg, ...(requestOutcomeArg === undefined ? {} : { requestOutcome: requestOutcomeArg }) });
|
|
52
|
+
|
|
53
|
+
const requireAccess = (accessArg: IOpenAiApiKeyAccess | undefined): IOpenAiApiKeyAccess => {
|
|
54
|
+
if (!accessArg || typeof accessArg.apiKey !== 'string' || accessArg.apiKey.length === 0) {
|
|
55
|
+
throw new OpenAiApiKeyAuthError('OpenAI API key is invalid.', { requestOutcome: 'notSent' });
|
|
56
|
+
}
|
|
57
|
+
return Object.freeze({ apiKey: accessArg.apiKey });
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Headers an API-key request never carries from the request it authenticates:
|
|
62
|
+
* - the ChatGPT backend's identity headers (`originator`, `ChatGPT-Account-ID`, `X-OpenAI-Fedramp`), which a
|
|
63
|
+
* client of a ChatGPT login sends and which mean nothing to the API;
|
|
64
|
+
* - `OpenAI-Organization` and `OpenAI-Project`: a key bills to the organization and project it was issued for,
|
|
65
|
+
* and a client must not move the request, and its spend, to another one the key can reach.
|
|
66
|
+
*/
|
|
67
|
+
const strippedRequestHeaders: readonly string[] = [
|
|
68
|
+
'originator',
|
|
69
|
+
'chatgpt-account-id',
|
|
70
|
+
'x-openai-fedramp',
|
|
71
|
+
'openai-organization',
|
|
72
|
+
'openai-project',
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The headers an API-key request authenticates with, over the request's own: the key replaces any credential the
|
|
77
|
+
* request carried, and the headers of `strippedRequestHeaders` are removed. Shared by inference and the model list.
|
|
78
|
+
*/
|
|
79
|
+
export function createOpenAiApiKeyHeaders(accessArg: IOpenAiApiKeyAccess, headersArg?: HeadersInit): Headers {
|
|
80
|
+
const headers = new Headers(headersArg);
|
|
81
|
+
for (const name of strippedRequestHeaders) headers.delete(name);
|
|
82
|
+
headers.set('authorization', `Bearer ${accessArg.apiKey}`);
|
|
83
|
+
return headers;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* An OpenAI API connection on an API key, for inference billed to the key's organization and project.
|
|
88
|
+
*
|
|
89
|
+
* Requests go only to `https://api.openai.com/v1`, without following redirects, with the key as the bearer and
|
|
90
|
+
* without ChatGPT, organization or project headers. A provider 401 (key rejected) or 403 (key lacks the permission)
|
|
91
|
+
* fails with an `OpenAiApiKeyAuthError` whose `status` names it, since a key does not renew; every other status is
|
|
92
|
+
* the caller's. Without a custom
|
|
93
|
+
* `fetch` the connection keeps the provider's prompt-cache timing confirmation.
|
|
94
|
+
*/
|
|
95
|
+
export const createOpenAiApiKeyConnection = (optionsArg: IOpenAiApiKeyConnectionOptions): IOpenAiModelConnection => {
|
|
96
|
+
const access = requireAccess(optionsArg);
|
|
97
|
+
const transport = optionsArg.fetch ?? globalThis.fetch;
|
|
98
|
+
if (typeof transport !== 'function') throw new OpenAiApiKeyAuthError('fetch is not available for OpenAI inference.');
|
|
99
|
+
|
|
100
|
+
const fetchWithApiKey: typeof fetch = async (inputArg, initArg) => {
|
|
101
|
+
const request = new Request(inputArg, { ...initArg, redirect: 'error' });
|
|
102
|
+
if (!isOpenAiApiRequestUrl(request.url)) {
|
|
103
|
+
throw new OpenAiApiKeyAuthError('OpenAI API key request URL is outside the OpenAI API.', { requestOutcome: 'notSent' });
|
|
104
|
+
}
|
|
105
|
+
const authenticated = new Request(request, { headers: createOpenAiApiKeyHeaders(access, request.headers) });
|
|
106
|
+
authenticated.signal.throwIfAborted();
|
|
107
|
+
const response = await transport(authenticated);
|
|
108
|
+
if (!isKeyRefusal(response.status)) return response;
|
|
109
|
+
await response.body?.cancel().catch(() => undefined);
|
|
110
|
+
throw keyRefused(response.status, 'outcomeUnknown');
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
if (optionsArg.fetch === undefined) registerOpenAiCacheTransparentTransport(fetchWithApiKey);
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
// The transport authenticates every request; the empty key keeps the AI SDK from reading its own.
|
|
117
|
+
settings: { apiKey: '', baseURL: OPENAI_API_BASE_URL, fetch: fetchWithApiKey },
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/** A model an API key may use, as `GET /v1/models` lists it. */
|
|
122
|
+
export interface IOpenAiApiKeyModel {
|
|
123
|
+
providerId: 'openai';
|
|
124
|
+
modelId: string;
|
|
125
|
+
/** When the model was created, in Unix seconds. */
|
|
126
|
+
created: number;
|
|
127
|
+
/** The organization that owns the model, such as `openai` or `system`. */
|
|
128
|
+
ownedBy: string;
|
|
129
|
+
/** The date the model shuts down, as the API states it, when it states one. */
|
|
130
|
+
shutdownDate?: string;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export class OpenAiApiModelListError extends Error {
|
|
134
|
+
public readonly code: TOpenAiChatGptModelListErrorCode;
|
|
135
|
+
public readonly status?: number;
|
|
136
|
+
|
|
137
|
+
constructor(codeArg: TOpenAiChatGptModelListErrorCode, statusArg?: number) {
|
|
138
|
+
super(`OpenAI model list request failed (${codeArg}${statusArg === undefined ? '' : ` ${statusArg}`}).`);
|
|
139
|
+
this.name = 'OpenAiApiModelListError';
|
|
140
|
+
this.code = codeArg;
|
|
141
|
+
if (statusArg !== undefined) this.status = statusArg;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface IOpenAiApiKeyModelListOptions {
|
|
146
|
+
signal?: AbortSignal;
|
|
147
|
+
/** Optional underlying transport. Defaults to the current global fetch. */
|
|
148
|
+
fetch?: typeof fetch;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const maximumListBytes = 1024 * 1024;
|
|
152
|
+
const maximumModels = 1000;
|
|
153
|
+
const textEncoder = new TextEncoder();
|
|
154
|
+
const protocolError = (): OpenAiApiModelListError => new OpenAiApiModelListError('PROTOCOL_ERROR');
|
|
155
|
+
const isRecord = (valueArg: unknown): valueArg is Record<string, unknown> =>
|
|
156
|
+
typeof valueArg === 'object' && valueArg !== null && !Array.isArray(valueArg);
|
|
157
|
+
const boundedString = (valueArg: unknown, maximumBytesArg: number): string => {
|
|
158
|
+
if (typeof valueArg !== 'string' || valueArg.length === 0 || textEncoder.encode(valueArg).byteLength > maximumBytesArg) {
|
|
159
|
+
throw protocolError();
|
|
160
|
+
}
|
|
161
|
+
return valueArg;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Reads the models an API key may use from `GET /v1/models`, one unpaged list, at most 1 MiB and 1000 models. A
|
|
166
|
+
* refused key fails with an `OpenAiApiKeyAuthError` whose `status` is 401 (rejected) or 403 (no permission to read
|
|
167
|
+
* models); an abort rejects with the signal's
|
|
168
|
+
* reason; every other failure is an `OpenAiApiModelListError` whose `code` is `NETWORK_FAILED`, `HTTP_ERROR` (with
|
|
169
|
+
* `status`) or `PROTOCOL_ERROR`.
|
|
170
|
+
*/
|
|
171
|
+
export async function listOpenAiApiKeyModels(
|
|
172
|
+
accessArg: IOpenAiApiKeyAccess,
|
|
173
|
+
optionsArg: IOpenAiApiKeyModelListOptions = {},
|
|
174
|
+
): Promise<IOpenAiApiKeyModel[]> {
|
|
175
|
+
const access = requireAccess(accessArg);
|
|
176
|
+
const transport = optionsArg.fetch ?? globalThis.fetch;
|
|
177
|
+
if (typeof transport !== 'function') throw new OpenAiApiKeyAuthError('fetch is not available for the OpenAI model list.');
|
|
178
|
+
const signal = optionsArg.signal;
|
|
179
|
+
signal?.throwIfAborted();
|
|
180
|
+
let response: Response;
|
|
181
|
+
try {
|
|
182
|
+
response = await transport(`${OPENAI_API_BASE_URL}/models`, {
|
|
183
|
+
method: 'GET', redirect: 'error', signal,
|
|
184
|
+
headers: createOpenAiApiKeyHeaders(access, { accept: 'application/json' }),
|
|
185
|
+
});
|
|
186
|
+
} catch {
|
|
187
|
+
if (signal?.aborted) throw signal.reason;
|
|
188
|
+
throw new OpenAiApiModelListError('NETWORK_FAILED');
|
|
189
|
+
}
|
|
190
|
+
if (!response.ok) {
|
|
191
|
+
await response.body?.cancel().catch(() => undefined);
|
|
192
|
+
if (isKeyRefusal(response.status)) throw keyRefused(response.status);
|
|
193
|
+
throw new OpenAiApiModelListError('HTTP_ERROR', response.status);
|
|
194
|
+
}
|
|
195
|
+
const body = await readOpenAiBoundedJson(response, signal, maximumListBytes, protocolError);
|
|
196
|
+
if (!isRecord(body) || (body.object !== undefined && body.object !== 'list') || !Array.isArray(body.data)
|
|
197
|
+
|| body.data.length > maximumModels) {
|
|
198
|
+
throw protocolError();
|
|
199
|
+
}
|
|
200
|
+
const modelIds = new Set<string>();
|
|
201
|
+
return body.data.map((raw): IOpenAiApiKeyModel => {
|
|
202
|
+
if (!isRecord(raw) || (raw.object !== undefined && raw.object !== 'model')) throw protocolError();
|
|
203
|
+
const modelId = boundedString(raw.id, 256);
|
|
204
|
+
if (modelIds.has(modelId)) throw protocolError();
|
|
205
|
+
modelIds.add(modelId);
|
|
206
|
+
if (!Number.isSafeInteger(raw.created) || (raw.created as number) < 0) throw protocolError();
|
|
207
|
+
const shutdownDate = raw.shutdown_date === undefined || raw.shutdown_date === null
|
|
208
|
+
? undefined
|
|
209
|
+
: boundedString(raw.shutdown_date, 64);
|
|
210
|
+
return {
|
|
211
|
+
providerId: 'openai', modelId, created: raw.created as number, ownedBy: boundedString(raw.owned_by, 256),
|
|
212
|
+
...(shutdownDate === undefined ? {} : { shutdownDate }),
|
|
213
|
+
};
|
|
214
|
+
});
|
|
215
|
+
}
|