@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.
Files changed (30) hide show
  1. package/dist_ts_providers/anthropic/index.d.ts +1 -0
  2. package/dist_ts_providers/anthropic/index.js +2 -1
  3. package/dist_ts_providers/anthropic/plugins.d.ts +3 -3
  4. package/dist_ts_providers/anthropic/plugins.js +3 -3
  5. package/dist_ts_providers/anthropic/smartai.pricing.anthropic.d.ts +57 -0
  6. package/dist_ts_providers/anthropic/smartai.pricing.anthropic.js +245 -0
  7. package/dist_ts_providers/openai/index.d.ts +4 -1
  8. package/dist_ts_providers/openai/index.js +6 -4
  9. package/dist_ts_providers/openai/plugins.d.ts +3 -3
  10. package/dist_ts_providers/openai/plugins.js +3 -3
  11. package/dist_ts_providers/openai/smartai.apikey.openai.d.ts +67 -0
  12. package/dist_ts_providers/openai/smartai.apikey.openai.js +171 -0
  13. package/dist_ts_providers/openai/smartai.cache.openai.d.ts +3 -0
  14. package/dist_ts_providers/openai/smartai.cache.openai.js +11 -1
  15. package/dist_ts_providers/openai/smartai.chatgpt.catalog.d.ts +5 -0
  16. package/dist_ts_providers/openai/smartai.chatgpt.catalog.js +11 -7
  17. package/dist_ts_providers/openai/smartai.models.openai.d.ts +60 -0
  18. package/dist_ts_providers/openai/smartai.models.openai.js +250 -0
  19. package/package.json +2 -2
  20. package/ts_providers/anthropic/index.ts +9 -0
  21. package/ts_providers/anthropic/plugins.ts +7 -0
  22. package/ts_providers/anthropic/readme.md +41 -0
  23. package/ts_providers/anthropic/smartai.pricing.anthropic.ts +285 -0
  24. package/ts_providers/openai/index.ts +26 -5
  25. package/ts_providers/openai/plugins.ts +7 -0
  26. package/ts_providers/openai/readme.md +50 -1
  27. package/ts_providers/openai/smartai.apikey.openai.ts +215 -0
  28. package/ts_providers/openai/smartai.cache.openai.ts +17 -0
  29. package/ts_providers/openai/smartai.chatgpt.catalog.ts +11 -6
  30. package/ts_providers/openai/smartai.models.openai.ts +312 -0
@@ -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
- const readBoundedJson = async (
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 protocolError();
137
+ throw protocolErrorArg();
133
138
  }
134
- if (!responseArg.body) throw protocolError();
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 protocolError();
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 protocolError();
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 readBoundedJson(response, signal, maximumCatalogBytes));
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
+ }