@modelprofile.com/flexharness-providers 16.1.0 → 16.2.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 +25 -0
- package/dist_ts_providers/anthropic/smartai.pricing.anthropic.js +168 -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 +1 -0
- package/ts_providers/anthropic/plugins.ts +7 -0
- package/ts_providers/anthropic/readme.md +21 -0
- package/ts_providers/anthropic/smartai.pricing.anthropic.ts +186 -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
|
@@ -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
|
+
}
|
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
import type * as plugins from './plugins.js';
|
|
2
2
|
import { OPENAI_CHATGPT_CODEX_BASE_URL } from './smartai.chatgpt.openai.js';
|
|
3
3
|
|
|
4
|
+
// Only transports owned here and known not to change the request body may retain
|
|
5
|
+
// native policy confirmation on OpenAI's API. Arbitrary custom fetch middleware is opaque.
|
|
6
|
+
const transparentTransports = new WeakSet<NonNullable<plugins.OpenAIProviderSettings['fetch']>>();
|
|
7
|
+
|
|
8
|
+
export function registerOpenAiCacheTransparentTransport(
|
|
9
|
+
transport: NonNullable<plugins.OpenAIProviderSettings['fetch']>,
|
|
10
|
+
): void {
|
|
11
|
+
transparentTransports.add(transport);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Whether a connection's transport sends the body the adapter serialized: the global fetch, or a registered one. */
|
|
15
|
+
export function isOpenAiCacheTransparentTransport(
|
|
16
|
+
transport: plugins.OpenAIProviderSettings['fetch'],
|
|
17
|
+
): boolean {
|
|
18
|
+
return transport === undefined || transparentTransports.has(transport);
|
|
19
|
+
}
|
|
20
|
+
|
|
4
21
|
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
5
22
|
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
6
23
|
|
|
@@ -121,17 +121,22 @@ const boundedString = (
|
|
|
121
121
|
return valueArg;
|
|
122
122
|
};
|
|
123
123
|
|
|
124
|
-
|
|
124
|
+
/**
|
|
125
|
+
* Reads a JSON body of at most `maximumBytesArg` bytes; an oversized or unparsable body fails with
|
|
126
|
+
* `protocolErrorArg()`, and an abort with the signal's reason. Shared by the OpenAI model reads.
|
|
127
|
+
*/
|
|
128
|
+
export const readOpenAiBoundedJson = async (
|
|
125
129
|
responseArg: Response,
|
|
126
130
|
signalArg: AbortSignal | undefined,
|
|
127
131
|
maximumBytesArg: number,
|
|
132
|
+
protocolErrorArg: () => Error,
|
|
128
133
|
): Promise<unknown> => {
|
|
129
134
|
const declared = responseArg.headers.get('content-length');
|
|
130
135
|
if (declared !== null && (!/^\d+$/.test(declared) || Number(declared) > maximumBytesArg)) {
|
|
131
136
|
await responseArg.body?.cancel().catch(() => undefined);
|
|
132
|
-
throw
|
|
137
|
+
throw protocolErrorArg();
|
|
133
138
|
}
|
|
134
|
-
if (!responseArg.body) throw
|
|
139
|
+
if (!responseArg.body) throw protocolErrorArg();
|
|
135
140
|
const reader = responseArg.body.getReader();
|
|
136
141
|
const chunks: Uint8Array[] = [];
|
|
137
142
|
let size = 0;
|
|
@@ -150,7 +155,7 @@ const readBoundedJson = async (
|
|
|
150
155
|
size += value.byteLength;
|
|
151
156
|
if (size > maximumBytesArg) {
|
|
152
157
|
await reader.cancel().catch(() => undefined);
|
|
153
|
-
throw
|
|
158
|
+
throw protocolErrorArg();
|
|
154
159
|
}
|
|
155
160
|
chunks.push(value);
|
|
156
161
|
}
|
|
@@ -169,7 +174,7 @@ const readBoundedJson = async (
|
|
|
169
174
|
try {
|
|
170
175
|
return JSON.parse(new TextDecoder().decode(body)) as unknown;
|
|
171
176
|
} catch {
|
|
172
|
-
throw
|
|
177
|
+
throw protocolErrorArg();
|
|
173
178
|
}
|
|
174
179
|
};
|
|
175
180
|
|
|
@@ -381,5 +386,5 @@ export async function listOpenAiChatGptModels(
|
|
|
381
386
|
}
|
|
382
387
|
throw new OpenAiChatGptModelListError('HTTP_ERROR', response.status);
|
|
383
388
|
}
|
|
384
|
-
return projectCatalog(await
|
|
389
|
+
return projectCatalog(await readOpenAiBoundedJson(response, signal, maximumCatalogBytes, protocolError));
|
|
385
390
|
}
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import * as plugins from './plugins.js';
|
|
2
|
+
import type { TOpenAiReasoningEffort } from './index.js';
|
|
3
|
+
import type {
|
|
4
|
+
IOpenAiChatGptModelReasoningEffort,
|
|
5
|
+
IOpenAiChatGptModelServiceTier,
|
|
6
|
+
TOpenAiChatGptInputModality,
|
|
7
|
+
} from './smartai.chatgpt.catalog.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A model of OpenAI's public API, as OpenAI's model page states it, in the vocabulary of `IOpenAiChatGptModel`
|
|
11
|
+
* so a consumer reads both catalogs alike. Only what the page states is here: a field the page leaves out is absent,
|
|
12
|
+
* never filled in.
|
|
13
|
+
*/
|
|
14
|
+
export interface IOpenAiApiModel {
|
|
15
|
+
readonly providerId: 'openai';
|
|
16
|
+
readonly modelId: string;
|
|
17
|
+
/** Other ids the model page lists for the model, such as dated snapshots. */
|
|
18
|
+
readonly snapshots: readonly string[];
|
|
19
|
+
readonly displayName: string;
|
|
20
|
+
readonly description: string;
|
|
21
|
+
/** The `reasoning.effort` values the model accepts, lowest first. The pages give no per-effort description. */
|
|
22
|
+
readonly reasoningEfforts: readonly IOpenAiChatGptModelReasoningEffort[];
|
|
23
|
+
/** The effort the model uses when a request names none; absent when OpenAI's pages do not state it. */
|
|
24
|
+
readonly defaultReasoningEffort?: TOpenAiReasoningEffort;
|
|
25
|
+
readonly inputModalities: readonly TOpenAiChatGptInputModality[];
|
|
26
|
+
/**
|
|
27
|
+
* The `service_tier` values besides Standard (`default`) the model is served in: `flex`, `priority` (Fast mode;
|
|
28
|
+
* `fast` names it in a request too) and `ultrafast`. The pages give no per-tier description.
|
|
29
|
+
*/
|
|
30
|
+
readonly serviceTiers: readonly IOpenAiChatGptModelServiceTier[];
|
|
31
|
+
/** The context window in tokens. */
|
|
32
|
+
readonly contextWindow: number;
|
|
33
|
+
/** The input limit in tokens, when the page states one. */
|
|
34
|
+
readonly maxInputTokens?: number;
|
|
35
|
+
readonly maxOutputTokens: number;
|
|
36
|
+
readonly source: plugins.IModelSource;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const checkedAt = '2026-10-21';
|
|
40
|
+
const modelPage = (modelIdArg: string): plugins.IModelSource =>
|
|
41
|
+
({ url: `https://developers.openai.com/api/docs/models/${modelIdArg}`, checkedAt });
|
|
42
|
+
|
|
43
|
+
const efforts = (...effortsArg: TOpenAiReasoningEffort[]): IOpenAiChatGptModelReasoningEffort[] =>
|
|
44
|
+
effortsArg.map((effort) => ({ effort, description: '' }));
|
|
45
|
+
const tier = (idArg: 'flex' | 'priority' | 'ultrafast', nameArg: string): IOpenAiChatGptModelServiceTier =>
|
|
46
|
+
({ id: idArg, name: nameArg, description: '' });
|
|
47
|
+
const flexAndFast = [tier('flex', 'Flex'), tier('priority', 'Fast')];
|
|
48
|
+
const flexFastAndUltrafast = [...flexAndFast, tier('ultrafast', 'Ultrafast')];
|
|
49
|
+
const textAndImage: TOpenAiChatGptInputModality[] = ['text', 'image'];
|
|
50
|
+
|
|
51
|
+
const deepFreeze = <T>(valueArg: T): T => {
|
|
52
|
+
if (typeof valueArg === 'object' && valueArg !== null && !Object.isFrozen(valueArg)) {
|
|
53
|
+
Object.freeze(valueArg);
|
|
54
|
+
for (const entry of Object.values(valueArg)) deepFreeze(entry);
|
|
55
|
+
}
|
|
56
|
+
return valueArg;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The models of OpenAI's public API this package describes, newest first, each read from its model page
|
|
61
|
+
* (`source`) on 2026-10-21. GPT-6 Astra's page states no default effort, and the GPT-5.5 and GPT-5.4 pages state
|
|
62
|
+
* no input limit. The `reasoningEfforts`, defaults and tiers of GPT-6 Astra and GPT-6.1 Sol are also stated in
|
|
63
|
+
* https://developers.openai.com/api/docs/guides/latest-model and the tiers in
|
|
64
|
+
* https://developers.openai.com/api/docs/pricing.
|
|
65
|
+
*/
|
|
66
|
+
export const OPENAI_API_MODELS: readonly IOpenAiApiModel[] = deepFreeze([
|
|
67
|
+
{
|
|
68
|
+
providerId: 'openai', modelId: 'gpt-6-astra', snapshots: [], displayName: 'GPT-6 Astra',
|
|
69
|
+
description: 'Our most capable model for the most demanding work.',
|
|
70
|
+
reasoningEfforts: efforts('low', 'medium', 'high', 'xhigh', 'max'),
|
|
71
|
+
inputModalities: textAndImage, serviceTiers: flexFastAndUltrafast,
|
|
72
|
+
contextWindow: 1_050_000, maxInputTokens: 922_000, maxOutputTokens: 128_000, source: modelPage('gpt-6-astra'),
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
providerId: 'openai', modelId: 'gpt-6.1-sol', snapshots: [], displayName: 'GPT-6.1 Sol',
|
|
76
|
+
description: 'Near-Astra performance for complex work at a lower cost.',
|
|
77
|
+
reasoningEfforts: efforts('low', 'medium', 'high', 'xhigh', 'max'), defaultReasoningEffort: 'medium',
|
|
78
|
+
inputModalities: textAndImage, serviceTiers: flexFastAndUltrafast,
|
|
79
|
+
contextWindow: 1_050_000, maxInputTokens: 922_000, maxOutputTokens: 128_000, source: modelPage('gpt-6.1-sol'),
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
providerId: 'openai', modelId: 'gpt-6-sol', snapshots: [], displayName: 'GPT-6 Sol',
|
|
83
|
+
description: 'Built to power complex coding and agentic workflows.',
|
|
84
|
+
reasoningEfforts: efforts('none', 'low', 'medium', 'high', 'xhigh', 'max'), defaultReasoningEffort: 'medium',
|
|
85
|
+
inputModalities: textAndImage, serviceTiers: flexAndFast,
|
|
86
|
+
contextWindow: 1_050_000, maxInputTokens: 922_000, maxOutputTokens: 128_000, source: modelPage('gpt-6-sol'),
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
providerId: 'openai', modelId: 'gpt-6-luna', snapshots: [], displayName: 'GPT-6 Luna',
|
|
90
|
+
description: 'Our most efficient model for focused, high-volume tasks.',
|
|
91
|
+
reasoningEfforts: efforts('none', 'low', 'medium', 'high', 'xhigh', 'max'), defaultReasoningEffort: 'medium',
|
|
92
|
+
inputModalities: textAndImage, serviceTiers: flexAndFast,
|
|
93
|
+
contextWindow: 1_050_000, maxInputTokens: 922_000, maxOutputTokens: 128_000, source: modelPage('gpt-6-luna'),
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
providerId: 'openai', modelId: 'gpt-5.6-sol', snapshots: [], displayName: 'GPT-5.6 Sol',
|
|
97
|
+
description: 'GPT-5.6 flagship model for complex professional work',
|
|
98
|
+
reasoningEfforts: efforts('none', 'low', 'medium', 'high', 'xhigh', 'max'), defaultReasoningEffort: 'medium',
|
|
99
|
+
inputModalities: textAndImage, serviceTiers: flexAndFast,
|
|
100
|
+
contextWindow: 1_050_000, maxInputTokens: 922_000, maxOutputTokens: 128_000, source: modelPage('gpt-5.6-sol'),
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
providerId: 'openai', modelId: 'gpt-5.6-terra', snapshots: [], displayName: 'GPT-5.6 Terra',
|
|
104
|
+
description: 'GPT-5.6 model that balances intelligence and cost',
|
|
105
|
+
reasoningEfforts: efforts('none', 'low', 'medium', 'high', 'xhigh', 'max'), defaultReasoningEffort: 'medium',
|
|
106
|
+
inputModalities: textAndImage, serviceTiers: flexAndFast,
|
|
107
|
+
contextWindow: 1_050_000, maxInputTokens: 922_000, maxOutputTokens: 128_000, source: modelPage('gpt-5.6-terra'),
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
providerId: 'openai', modelId: 'gpt-5.6-luna', snapshots: [], displayName: 'GPT-5.6 Luna',
|
|
111
|
+
description: 'GPT-5.6 model optimized for cost-sensitive workloads',
|
|
112
|
+
reasoningEfforts: efforts('none', 'low', 'medium', 'high', 'xhigh', 'max'), defaultReasoningEffort: 'medium',
|
|
113
|
+
inputModalities: textAndImage, serviceTiers: flexAndFast,
|
|
114
|
+
contextWindow: 1_050_000, maxInputTokens: 922_000, maxOutputTokens: 128_000, source: modelPage('gpt-5.6-luna'),
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
providerId: 'openai', modelId: 'gpt-5.5', snapshots: ['gpt-5.5-2026-04-23'], displayName: 'GPT-5.5',
|
|
118
|
+
description: 'A new class of intelligence for coding and professional work.',
|
|
119
|
+
reasoningEfforts: efforts('none', 'low', 'medium', 'high', 'xhigh'), defaultReasoningEffort: 'medium',
|
|
120
|
+
inputModalities: textAndImage, serviceTiers: flexAndFast,
|
|
121
|
+
contextWindow: 1_050_000, maxOutputTokens: 128_000, source: modelPage('gpt-5.5'),
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
providerId: 'openai', modelId: 'gpt-5.4', snapshots: ['gpt-5.4-2026-03-05'], displayName: 'GPT-5.4',
|
|
125
|
+
description: 'A more affordable model for coding and professional work.',
|
|
126
|
+
reasoningEfforts: efforts('none', 'low', 'medium', 'high', 'xhigh'), defaultReasoningEffort: 'none',
|
|
127
|
+
inputModalities: textAndImage, serviceTiers: flexAndFast,
|
|
128
|
+
contextWindow: 1_050_000, maxOutputTokens: 128_000, source: modelPage('gpt-5.4'),
|
|
129
|
+
},
|
|
130
|
+
] satisfies IOpenAiApiModel[]);
|
|
131
|
+
|
|
132
|
+
/** The `OPENAI_API_MODELS` entry for exactly this model id or one of its listed snapshots; nothing else matches. */
|
|
133
|
+
export function getOpenAiApiModel(modelIdArg: string): IOpenAiApiModel | undefined {
|
|
134
|
+
return OPENAI_API_MODELS.find((model) => model.modelId === modelIdArg || model.snapshots.includes(modelIdArg));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
138
|
+
// Prices
|
|
139
|
+
|
|
140
|
+
/** USD per million tokens as an integer rate (micro-USD per million tokens). */
|
|
141
|
+
const usd = (dollarsArg: number): number => Math.round(dollarsArg * 1_000_000);
|
|
142
|
+
|
|
143
|
+
/** A price row as the pricing page prints it: input, output, cached input, 30-minute write, 12-hour write. */
|
|
144
|
+
type TOpenAiPriceRow = readonly [input: number, output: number, cached: number, write30m: number, write12h: number];
|
|
145
|
+
|
|
146
|
+
const rates = ([input, output, cached, write30m, write12h]: TOpenAiPriceRow): plugins.IModelTokenRates => ({
|
|
147
|
+
input: usd(input), output: usd(output), cacheRead: usd(cached),
|
|
148
|
+
cacheWrite: { '30m': usd(write30m), '12h': usd(write12h) },
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
/** Short context is a prompt of at most 272K input tokens; long context is above it. */
|
|
152
|
+
const shortContextTokens = 272_000;
|
|
153
|
+
|
|
154
|
+
const service = (shortArg: TOpenAiPriceRow, longArg?: TOpenAiPriceRow): plugins.IModelServicePrice => ({
|
|
155
|
+
bands: longArg
|
|
156
|
+
? [{ maxPromptTokens: shortContextTokens, rates: rates(shortArg) }, { rates: rates(longArg) }]
|
|
157
|
+
: [{ maxPromptTokens: shortContextTokens, rates: rates(shortArg) }],
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* GPT-5.5 and earlier charge no more for a cache write than for ordinary input
|
|
162
|
+
* (https://developers.openai.com/api/docs/guides/prompt-caching, "Summary of model differences"), so a write is
|
|
163
|
+
* priced at the input rate under either lifetime name.
|
|
164
|
+
*/
|
|
165
|
+
const withoutWriteCharge = (inputArg: number, outputArg: number, cachedArg: number): TOpenAiPriceRow =>
|
|
166
|
+
[inputArg, outputArg, cachedArg, inputArg, inputArg];
|
|
167
|
+
|
|
168
|
+
const pricingSource: plugins.IModelSource = { url: 'https://developers.openai.com/api/docs/pricing', checkedAt };
|
|
169
|
+
|
|
170
|
+
/** Web search $10 and file search $2.50 per 1,000 calls; a reader of Responses usage cannot count them (see readme). */
|
|
171
|
+
const toolCallFees = { web_search: 10_000, file_search: 2_500 };
|
|
172
|
+
|
|
173
|
+
const openAiPrice = (
|
|
174
|
+
modelIdArg: string,
|
|
175
|
+
servicesArg: { standard: plugins.IModelServicePrice; flex: plugins.IModelServicePrice; fast: plugins.IModelServicePrice;
|
|
176
|
+
ultrafast?: plugins.IModelServicePrice },
|
|
177
|
+
): plugins.IModelPrice => {
|
|
178
|
+
const model = getOpenAiApiModel(modelIdArg);
|
|
179
|
+
if (!model) throw new Error(`No OpenAI API model ${modelIdArg}.`);
|
|
180
|
+
return {
|
|
181
|
+
providerId: 'openai', modelId: modelIdArg, aliases: model.snapshots,
|
|
182
|
+
// The response reports Fast mode as `priority`; OpenAI's Fast-mode guide names `fast` for responses of later
|
|
183
|
+
// models, so both names carry its price.
|
|
184
|
+
services: {
|
|
185
|
+
default: servicesArg.standard, flex: servicesArg.flex, priority: servicesArg.fast, fast: servicesArg.fast,
|
|
186
|
+
...(servicesArg.ultrafast ? { ultrafast: servicesArg.ultrafast } : {}),
|
|
187
|
+
},
|
|
188
|
+
toolCallFees, source: pricingSource,
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The prices of `OPENAI_API_MODELS` from https://developers.openai.com/api/docs/pricing, checked 2026-10-21, keyed
|
|
194
|
+
* by the response's `service_tier`. A tier the page prices no long context for (Fast on GPT-5.5 and GPT-5.4) has
|
|
195
|
+
* only the short band, so a longer prompt is refused. GPT-5.6 Sol's prices are promotional, at least through
|
|
196
|
+
* 2026-11-21.
|
|
197
|
+
*/
|
|
198
|
+
export const OPENAI_MODEL_PRICES: readonly plugins.IModelPrice[] = deepFreeze([
|
|
199
|
+
openAiPrice('gpt-6-astra', {
|
|
200
|
+
standard: service([10, 50, 1, 12.5, 25], [20, 75, 2, 25, 50]),
|
|
201
|
+
flex: service([5, 25, 0.5, 6.25, 12.5], [10, 37.5, 1, 12.5, 25]),
|
|
202
|
+
fast: service([20, 100, 2, 25, 50], [40, 150, 4, 50, 100]),
|
|
203
|
+
ultrafast: service([60, 300, 6, 75, 150], [120, 450, 12, 150, 300]),
|
|
204
|
+
}),
|
|
205
|
+
openAiPrice('gpt-6.1-sol', {
|
|
206
|
+
standard: service([2, 10, 0.1, 2.5, 5], [4, 15, 0.2, 5, 10]),
|
|
207
|
+
flex: service([1, 5, 0.05, 1.25, 2.5], [2, 7.5, 0.1, 2.5, 5]),
|
|
208
|
+
fast: service([4, 20, 0.2, 5, 10], [8, 30, 0.4, 10, 20]),
|
|
209
|
+
ultrafast: service([12, 60, 0.6, 15, 30], [24, 90, 1.2, 30, 60]),
|
|
210
|
+
}),
|
|
211
|
+
openAiPrice('gpt-6-sol', {
|
|
212
|
+
standard: service([2, 10, 0.2, 2.5, 5], [4, 15, 0.4, 5, 10]),
|
|
213
|
+
flex: service([1, 5, 0.1, 1.25, 2.5], [2, 7.5, 0.2, 2.5, 5]),
|
|
214
|
+
fast: service([4, 20, 0.4, 5, 10], [8, 30, 0.8, 10, 20]),
|
|
215
|
+
}),
|
|
216
|
+
openAiPrice('gpt-6-luna', {
|
|
217
|
+
standard: service([0.1, 0.5, 0.01, 0.125, 0.25], [0.2, 0.75, 0.02, 0.25, 0.5]),
|
|
218
|
+
flex: service([0.05, 0.25, 0.005, 0.0625, 0.125], [0.1, 0.375, 0.01, 0.125, 0.25]),
|
|
219
|
+
fast: service([0.2, 1, 0.02, 0.25, 0.5], [0.4, 1.5, 0.04, 0.5, 1]),
|
|
220
|
+
}),
|
|
221
|
+
openAiPrice('gpt-5.6-sol', {
|
|
222
|
+
standard: service([4, 20, 0.4, 5, 10], [8, 30, 0.8, 10, 20]),
|
|
223
|
+
flex: service([2, 10, 0.2, 2.5, 5], [4, 15, 0.4, 5, 10]),
|
|
224
|
+
fast: service([8, 40, 0.8, 10, 20], [16, 60, 1.6, 20, 40]),
|
|
225
|
+
}),
|
|
226
|
+
openAiPrice('gpt-5.6-terra', {
|
|
227
|
+
standard: service([2, 12, 0.2, 2.5, 5], [4, 18, 0.4, 5, 10]),
|
|
228
|
+
flex: service([1, 6, 0.1, 1.25, 2.5], [2, 9, 0.2, 2.5, 5]),
|
|
229
|
+
fast: service([4, 24, 0.4, 5, 10], [8, 36, 0.8, 10, 20]),
|
|
230
|
+
}),
|
|
231
|
+
openAiPrice('gpt-5.6-luna', {
|
|
232
|
+
standard: service([0.2, 1.2, 0.02, 0.25, 0.5], [0.4, 1.8, 0.04, 0.5, 1]),
|
|
233
|
+
flex: service([0.1, 0.6, 0.01, 0.125, 0.25], [0.2, 0.9, 0.02, 0.25, 0.5]),
|
|
234
|
+
fast: service([0.4, 2.4, 0.04, 0.5, 1], [0.8, 3.6, 0.08, 1, 2]),
|
|
235
|
+
}),
|
|
236
|
+
openAiPrice('gpt-5.5', {
|
|
237
|
+
standard: service(withoutWriteCharge(5, 30, 0.5), withoutWriteCharge(10, 45, 1)),
|
|
238
|
+
flex: service(withoutWriteCharge(2.5, 15, 0.25), withoutWriteCharge(5, 22.5, 0.5)),
|
|
239
|
+
fast: service(withoutWriteCharge(12.5, 75, 1.25)),
|
|
240
|
+
}),
|
|
241
|
+
openAiPrice('gpt-5.4', {
|
|
242
|
+
standard: service(withoutWriteCharge(2.5, 15, 0.25), withoutWriteCharge(5, 22.5, 0.5)),
|
|
243
|
+
flex: service(withoutWriteCharge(1.25, 7.5, 0.13), withoutWriteCharge(2.5, 11.25, 0.25)),
|
|
244
|
+
fast: service(withoutWriteCharge(5, 30, 0.5)),
|
|
245
|
+
}),
|
|
246
|
+
] satisfies plugins.IModelPrice[]);
|
|
247
|
+
|
|
248
|
+
/** The price of exactly this model id or one of its listed snapshots; `undefined` for any other id. */
|
|
249
|
+
export function getOpenAiModelPrice(modelIdArg: string): plugins.IModelPrice | undefined {
|
|
250
|
+
return OPENAI_MODEL_PRICES.find((price) => price.modelId === modelIdArg || (price.aliases ?? []).includes(modelIdArg));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
254
|
+
// Usage
|
|
255
|
+
|
|
256
|
+
const isRecord = (valueArg: unknown): valueArg is Record<string, unknown> =>
|
|
257
|
+
typeof valueArg === 'object' && valueArg !== null && !Array.isArray(valueArg);
|
|
258
|
+
|
|
259
|
+
const tokenCount = (valueArg: unknown, nameArg: string): number => {
|
|
260
|
+
if (!Number.isSafeInteger(valueArg) || (valueArg as number) < 0) {
|
|
261
|
+
throw new plugins.ModelUsageReadError(`OpenAI usage ${nameArg} is not a token count.`);
|
|
262
|
+
}
|
|
263
|
+
return valueArg as number;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const optionalTokenCount = (valueArg: unknown, nameArg: string): number | undefined =>
|
|
267
|
+
valueArg === undefined || valueArg === null ? undefined : tokenCount(valueArg, nameArg);
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Reads a Responses API `usage` object, raw as the API sends it, with the response's `service_tier` (the tier that
|
|
271
|
+
* served the call). `input_tokens` counts every input token, cache reads and writes included; the writes are split
|
|
272
|
+
* by lifetime from `input_tokens_details.cache_write_tokens_details` (`ttl_30m_tokens`, `ttl_12h_tokens`), and
|
|
273
|
+
* writes the response does not split go to `unknownLifetimeCacheWriteTokens`. Output includes reasoning. The usage
|
|
274
|
+
* names no server-tool calls: count `web_search_call` and `file_search_call` output items into `toolCalls`.
|
|
275
|
+
* A usage that is not complete throws a `ModelUsageReadError`.
|
|
276
|
+
*/
|
|
277
|
+
export function readOpenAiResponsesUsage(rawUsageArg: unknown, serviceTierArg: unknown): plugins.IModelPricedUsage {
|
|
278
|
+
if (!isRecord(rawUsageArg)) throw new plugins.ModelUsageReadError('OpenAI usage is not an object.');
|
|
279
|
+
if (typeof serviceTierArg !== 'string' || serviceTierArg.length === 0) {
|
|
280
|
+
throw new plugins.ModelUsageReadError('OpenAI response names no service tier.');
|
|
281
|
+
}
|
|
282
|
+
const inputTokens = tokenCount(rawUsageArg.input_tokens, 'input_tokens');
|
|
283
|
+
const outputTokens = tokenCount(rawUsageArg.output_tokens, 'output_tokens');
|
|
284
|
+
const rawDetails = rawUsageArg.input_tokens_details;
|
|
285
|
+
if (rawDetails !== undefined && rawDetails !== null && !isRecord(rawDetails)) {
|
|
286
|
+
throw new plugins.ModelUsageReadError('OpenAI usage input_tokens_details is not an object.');
|
|
287
|
+
}
|
|
288
|
+
const details = isRecord(rawDetails) ? rawDetails : undefined;
|
|
289
|
+
const cachedTokens = optionalTokenCount(details?.cached_tokens, 'cached_tokens') ?? 0;
|
|
290
|
+
const writeTokens = optionalTokenCount(details?.cache_write_tokens, 'cache_write_tokens') ?? 0;
|
|
291
|
+
const writeDetails = details?.cache_write_tokens_details;
|
|
292
|
+
const cacheWriteTokens: Record<string, number> = {};
|
|
293
|
+
let unsplitWrites = writeTokens;
|
|
294
|
+
if (writeDetails !== undefined && writeDetails !== null) {
|
|
295
|
+
if (!isRecord(writeDetails)) {
|
|
296
|
+
throw new plugins.ModelUsageReadError('OpenAI usage cache_write_tokens_details is not an object.');
|
|
297
|
+
}
|
|
298
|
+
const write30m = optionalTokenCount(writeDetails.ttl_30m_tokens, 'ttl_30m_tokens') ?? 0;
|
|
299
|
+
const write12h = optionalTokenCount(writeDetails.ttl_12h_tokens, 'ttl_12h_tokens') ?? 0;
|
|
300
|
+
unsplitWrites = writeTokens - write30m - write12h;
|
|
301
|
+
if (unsplitWrites < 0) throw new plugins.ModelUsageReadError('OpenAI usage splits more cache writes than it counts.');
|
|
302
|
+
if (write30m > 0) cacheWriteTokens['30m'] = write30m;
|
|
303
|
+
if (write12h > 0) cacheWriteTokens['12h'] = write12h;
|
|
304
|
+
}
|
|
305
|
+
const uncachedInputTokens = inputTokens - cachedTokens - writeTokens;
|
|
306
|
+
if (uncachedInputTokens < 0) throw new plugins.ModelUsageReadError('OpenAI usage counts more cached input than input.');
|
|
307
|
+
return {
|
|
308
|
+
service: serviceTierArg, uncachedInputTokens, cacheReadTokens: cachedTokens, cacheWriteTokens,
|
|
309
|
+
...(unsplitWrites > 0 ? { unknownLifetimeCacheWriteTokens: unsplitWrites } : {}),
|
|
310
|
+
outputTokens,
|
|
311
|
+
};
|
|
312
|
+
}
|