@modelprofile.com/flexharness-providers 9.0.0 → 9.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.
Files changed (109) hide show
  1. package/dist_ts_anthropic_messages/errors.d.ts +22 -0
  2. package/dist_ts_anthropic_messages/errors.js +100 -0
  3. package/dist_ts_anthropic_messages/index.d.ts +4 -0
  4. package/dist_ts_anthropic_messages/index.js +4 -0
  5. package/dist_ts_anthropic_messages/interfaces.d.ts +185 -0
  6. package/dist_ts_anthropic_messages/interfaces.js +2 -0
  7. package/dist_ts_anthropic_messages/openai.d.ts +19 -0
  8. package/dist_ts_anthropic_messages/openai.js +48 -0
  9. package/dist_ts_anthropic_messages/plugins.d.ts +4 -0
  10. package/dist_ts_anthropic_messages/plugins.js +4 -0
  11. package/dist_ts_anthropic_messages/request.d.ts +109 -0
  12. package/dist_ts_anthropic_messages/request.js +554 -0
  13. package/dist_ts_anthropic_messages/service.d.ts +8 -0
  14. package/dist_ts_anthropic_messages/service.js +274 -0
  15. package/dist_ts_anthropic_messages/signature.d.ts +16 -0
  16. package/dist_ts_anthropic_messages/signature.js +91 -0
  17. package/dist_ts_anthropic_messages/stream.d.ts +48 -0
  18. package/dist_ts_anthropic_messages/stream.js +220 -0
  19. package/dist_ts_anthropic_messages_node/index.d.ts +8 -0
  20. package/dist_ts_anthropic_messages_node/index.js +93 -0
  21. package/dist_ts_anthropic_messages_node/plugins.d.ts +6 -0
  22. package/dist_ts_anthropic_messages_node/plugins.js +4 -0
  23. package/dist_ts_openai_account/plugins.d.ts +2 -0
  24. package/dist_ts_openai_account/plugins.js +4 -1
  25. package/dist_ts_openai_account/smartai.providers.interfaces.d.ts +6 -22
  26. package/dist_ts_openai_account/smartai.providers.openai.js +15 -134
  27. package/dist_ts_openai_auth/index.d.ts +1 -2
  28. package/dist_ts_openai_auth/index.js +2 -3
  29. package/dist_ts_openai_auth/interfaces.d.ts +1 -1
  30. package/dist_ts_openai_auth/smartai.auth.openai.d.ts +3 -15
  31. package/dist_ts_openai_auth/smartai.auth.openai.js +3 -17
  32. package/dist_ts_openai_auth/smartai.connection.openai.d.ts +1 -1
  33. package/dist_ts_openai_auth/smartai.connection.openai.js +2 -3
  34. package/dist_ts_providers/anthropic/index.d.ts +1 -1
  35. package/dist_ts_providers/anthropic/index.js +1 -1
  36. package/dist_ts_providers/anthropic/plugins.d.ts +1 -1
  37. package/dist_ts_providers/google/index.d.ts +1 -1
  38. package/dist_ts_providers/google/index.js +1 -1
  39. package/dist_ts_providers/google/plugins.d.ts +1 -1
  40. package/dist_ts_providers/groq/index.d.ts +1 -1
  41. package/dist_ts_providers/groq/index.js +1 -1
  42. package/dist_ts_providers/groq/plugins.d.ts +1 -1
  43. package/dist_ts_providers/mistral/index.d.ts +1 -1
  44. package/dist_ts_providers/mistral/index.js +1 -1
  45. package/dist_ts_providers/mistral/plugins.d.ts +1 -1
  46. package/dist_ts_providers/openai/index.d.ts +5 -1
  47. package/dist_ts_providers/openai/index.js +5 -1
  48. package/dist_ts_providers/openai/plugins.d.ts +4 -4
  49. package/dist_ts_providers/openai/plugins.js +3 -3
  50. package/dist_ts_providers/openai/smartai.chatgpt.catalog.d.ts +61 -0
  51. package/dist_ts_providers/openai/smartai.chatgpt.catalog.js +252 -0
  52. package/{dist_ts_openai_auth/smartai.limits.openai.d.ts → dist_ts_providers/openai/smartai.chatgpt.limits.d.ts} +2 -2
  53. package/dist_ts_providers/openai/smartai.chatgpt.limits.js +49 -0
  54. package/dist_ts_providers/openai/smartai.chatgpt.middleware.d.ts +10 -0
  55. package/dist_ts_providers/openai/smartai.chatgpt.middleware.js +60 -0
  56. package/dist_ts_providers/openai/smartai.chatgpt.openai.d.ts +92 -0
  57. package/dist_ts_providers/openai/smartai.chatgpt.openai.js +160 -0
  58. package/dist_ts_providers/perplexity/index.d.ts +1 -1
  59. package/dist_ts_providers/perplexity/index.js +1 -1
  60. package/dist_ts_providers/perplexity/plugins.d.ts +1 -1
  61. package/dist_ts_providers/xai/index.d.ts +1 -1
  62. package/dist_ts_providers/xai/index.js +1 -1
  63. package/dist_ts_providers/xai/plugins.d.ts +1 -1
  64. package/package.json +14 -2
  65. package/readme.md +9 -0
  66. package/ts_anthropic_messages/errors.ts +124 -0
  67. package/ts_anthropic_messages/index.ts +24 -0
  68. package/ts_anthropic_messages/interfaces.ts +173 -0
  69. package/ts_anthropic_messages/openai.ts +58 -0
  70. package/ts_anthropic_messages/plugins.ts +26 -0
  71. package/ts_anthropic_messages/readme.md +129 -0
  72. package/ts_anthropic_messages/request.ts +652 -0
  73. package/ts_anthropic_messages/service.ts +312 -0
  74. package/ts_anthropic_messages/signature.ts +107 -0
  75. package/ts_anthropic_messages/stream.ts +260 -0
  76. package/ts_anthropic_messages_node/index.ts +99 -0
  77. package/ts_anthropic_messages_node/plugins.ts +7 -0
  78. package/ts_anthropic_messages_node/readme.md +50 -0
  79. package/ts_openai_account/plugins.ts +5 -0
  80. package/ts_openai_account/smartai.providers.interfaces.ts +11 -25
  81. package/ts_openai_account/smartai.providers.openai.ts +16 -135
  82. package/ts_openai_auth/index.ts +4 -2
  83. package/ts_openai_auth/interfaces.ts +1 -1
  84. package/ts_openai_auth/smartai.auth.openai.ts +12 -24
  85. package/ts_openai_auth/smartai.connection.openai.ts +5 -3
  86. package/ts_providers/anthropic/index.ts +1 -1
  87. package/ts_providers/anthropic/plugins.ts +1 -0
  88. package/ts_providers/google/index.ts +1 -1
  89. package/ts_providers/google/plugins.ts +1 -1
  90. package/ts_providers/groq/index.ts +1 -1
  91. package/ts_providers/groq/plugins.ts +1 -1
  92. package/ts_providers/mistral/index.ts +1 -1
  93. package/ts_providers/mistral/plugins.ts +1 -1
  94. package/ts_providers/openai/index.ts +30 -1
  95. package/ts_providers/openai/plugins.ts +5 -3
  96. package/ts_providers/openai/readme.md +60 -7
  97. package/ts_providers/openai/smartai.chatgpt.catalog.ts +319 -0
  98. package/{ts_openai_auth/smartai.limits.openai.ts → ts_providers/openai/smartai.chatgpt.limits.ts} +6 -7
  99. package/ts_providers/openai/smartai.chatgpt.middleware.ts +71 -0
  100. package/ts_providers/openai/smartai.chatgpt.openai.ts +271 -0
  101. package/ts_providers/perplexity/index.ts +1 -1
  102. package/ts_providers/perplexity/plugins.ts +1 -1
  103. package/ts_providers/readme.md +9 -0
  104. package/ts_providers/xai/index.ts +1 -1
  105. package/ts_providers/xai/plugins.ts +1 -1
  106. package/dist_ts_openai_auth/smartai.limits.openai.js +0 -49
  107. package/dist_ts_openai_auth/smartai.middleware.openai.d.ts +0 -6
  108. package/dist_ts_openai_auth/smartai.middleware.openai.js +0 -47
  109. package/ts_openai_auth/smartai.middleware.openai.ts +0 -55
@@ -18,9 +18,8 @@ for OpenAI's European data residency. `api: 'chat'` calls `/chat/completions` in
18
18
  their base URL and key.
19
19
 
20
20
  `api` applies to a supplied `connection` too; a connection that declares the APIs it serves refuses any other.
21
- The ChatGPT connections from the `/auth` subpath serve the Responses API only (the Codex backend
22
- has no Chat Completions endpoint), so asking one of them for `api: 'chat'` is refused when the
23
- model is constructed.
21
+ The ChatGPT connections serve the Responses API only (the Codex backend has no Chat Completions
22
+ endpoint), so asking one of them for `api: 'chat'` is refused when the model is constructed.
24
23
 
25
24
  Provider options differ per API. `IOpenAiProviderOptions` types the Responses options;
26
25
  `IOpenAiChatProviderOptions` types the Chat Completions options. Responses-only options such as
@@ -47,10 +46,64 @@ usage and rate limits section. A `connection` can add its own limit dialect with
47
46
  `classifyLimit`, which is consulted first. `classifyOpenAiLimit` and `readOpenAiError` are
48
47
  exported for connections that extend the OpenAI dialect.
49
48
 
50
- ChatGPT connections come from the `/auth` subpath. Use the dynamic connection
51
- factory for delegated authority access so every provider HTTP request resolves
52
- a current token while the connection retains its pinned account and residency
53
- identity.
49
+ ## ChatGPT with access from an account authority
50
+
51
+ This subpath runs inference and reads the model catalog of a ChatGPT account with access alone:
52
+ an access token plus the workspace (`accountId`) and residency (`isFedrampAccount`) its issuer
53
+ states. It never signs in, never refreshes and never reads identity out of the token; the
54
+ account authority that resolved the access owns all of that. The shape is the answer of the
55
+ authswitch authority's `resolveAccess`.
56
+
57
+ `createOpenAiChatGptResolvingModelConnection()` resolves access immediately before every provider
58
+ HTTP request, so a multi-step tool run never holds the token that admitted it. The connection is
59
+ pinned to `expectedIdentity`: an access that states another workspace or residency, an empty
60
+ token, an endpoint outside the Codex backend, a redirect or an abort stops the request before the
61
+ transport is called.
62
+
63
+ When the resolved access carries the authority's `generation` and the provider answers 401, the
64
+ connection resolves once more with `rejectedGeneration` set to that generation, so the authority
65
+ renews exactly the token that was rejected, and sends the request again with the same body. A
66
+ second 401, or a 401 for access without a `generation`, fails with an `OpenAiChatGptAuthError`
67
+ whose `status` is 401.
68
+
69
+ ```typescript
70
+ import {
71
+ createOpenAiChatGptResolvingModelConnection,
72
+ createOpenAiModelProvider,
73
+ } from '@modelprofile.com/flexharness-providers/openai';
74
+
75
+ const connection = createOpenAiChatGptResolvingModelConnection({
76
+ expectedIdentity: { accountId, isFedrampAccount },
77
+ resolveAccess: async ({ signal, rejectedGeneration }) => {
78
+ const access = await authority.resolveAccess(
79
+ { bindingId, capability, minValidityMs: 60_000, rejectedGrantGeneration: rejectedGeneration },
80
+ signal,
81
+ );
82
+ return { ...access, generation: access.grantGeneration };
83
+ },
84
+ });
85
+ const model = createOpenAiModelProvider().getModel({ provider: 'openai', model: 'gpt-5.5', connection });
86
+ ```
87
+
88
+ `createOpenAiChatGptAccessModelConnection(access)` sends one fixed access with the same headers and
89
+ checks; its 401 fails the same way.
90
+
91
+ `listOpenAiChatGptModels(access, { signal })` reads the account's Codex model catalog, hidden models
92
+ included, in the catalog's priority order, with the first listed model marked `isDefault`. A
93
+ rejected token fails with an `OpenAiChatGptAuthError` whose `status` is 401, so a caller resolves
94
+ again with `rejectedGeneration` and repeats the read; an abort rejects with the signal's reason;
95
+ anything else is an `OpenAiChatGptModelListError` whose `code` is `NETWORK_FAILED`, `HTTP_ERROR`
96
+ (with `status`) or `PROTOCOL_ERROR`. A model whose catalog entry states its context window carries it as
97
+ `contextWindow`, in tokens.
98
+
99
+ Both ChatGPT connections classify Codex limits with `classifyOpenAiChatGptLimit` and move the system
100
+ messages a prompt opens with into the Responses `instructions` with
101
+ `createOpenAiChatGptInstructionsMiddleware()`; a system message later in the conversation stays in its
102
+ place as a `developer` input item. Both are exported here for connections of their own.
103
+
104
+ The `/auth` subpath keeps its connections, which derive identity from the token itself, and
105
+ re-exports the ChatGPT limit classifier, the instructions middleware, the Codex base URL, the
106
+ default originator and `OpenAiChatGptAuthError` from here.
54
107
 
55
108
  ## Issue Reporting and Security
56
109
 
@@ -0,0 +1,319 @@
1
+ // OpenAI protocol portions are adapted in TypeScript; see third-party-notices.md.
2
+ import {
3
+ createOpenAiChatGptAccessHeaders,
4
+ OPENAI_CHATGPT_CODEX_BASE_URL,
5
+ OPENAI_CHATGPT_DEFAULT_ORIGINATOR,
6
+ OpenAiChatGptAuthError,
7
+ type IOpenAiChatGptReadAccess,
8
+ } from './smartai.chatgpt.openai.js';
9
+
10
+ /** The Codex client version the model catalog is requested for; the backend shapes its catalog by it. */
11
+ export const OPENAI_CHATGPT_CATALOG_CLIENT_VERSION = '0.146.1';
12
+
13
+ const maximumCatalogBytes = 2 * 1024 * 1024;
14
+ const maximumCatalogModels = 256;
15
+
16
+ export type TOpenAiChatGptInputModality = 'text' | 'image' | 'audio';
17
+
18
+ export interface IOpenAiChatGptModelReasoningEffort {
19
+ effort: string;
20
+ description: string;
21
+ }
22
+
23
+ export interface IOpenAiChatGptModelServiceTier {
24
+ id: string;
25
+ name: string;
26
+ description: string;
27
+ }
28
+
29
+ /** One model of a ChatGPT account's Codex catalog, in the catalog's own priority order. */
30
+ export interface IOpenAiChatGptModel {
31
+ providerId: 'openai';
32
+ modelId: string;
33
+ wireModel: string;
34
+ displayName: string;
35
+ description: string;
36
+ hidden: boolean;
37
+ reasoningEfforts: IOpenAiChatGptModelReasoningEffort[];
38
+ defaultReasoningEffort: string;
39
+ inputModalities: TOpenAiChatGptInputModality[];
40
+ supportsPersonality: boolean;
41
+ serviceTiers: IOpenAiChatGptModelServiceTier[];
42
+ defaultServiceTier?: string;
43
+ /** The context window in tokens, as the catalog states it; absent when the catalog states none. */
44
+ contextWindow?: number;
45
+ /** The first listed model, or the first model when none is listed. */
46
+ isDefault: boolean;
47
+ }
48
+
49
+ /**
50
+ * Why a catalog read failed, other than a rejected access token or an abort.
51
+ *
52
+ * - `NETWORK_FAILED`: the request did not complete.
53
+ * - `HTTP_ERROR`: the backend answered with an error status other than 401; `status` names it.
54
+ * - `PROTOCOL_ERROR`: the answer was oversized or not a catalog this package can read.
55
+ */
56
+ export type TOpenAiChatGptModelListErrorCode = 'NETWORK_FAILED' | 'HTTP_ERROR' | 'PROTOCOL_ERROR';
57
+
58
+ export class OpenAiChatGptModelListError extends Error {
59
+ public readonly code: TOpenAiChatGptModelListErrorCode;
60
+ public readonly status?: number;
61
+
62
+ constructor(codeArg: TOpenAiChatGptModelListErrorCode, statusArg?: number) {
63
+ super(`OpenAI ChatGPT model catalog request failed (${codeArg}${statusArg === undefined ? '' : ` ${statusArg}`}).`);
64
+ this.name = 'OpenAiChatGptModelListError';
65
+ this.code = codeArg;
66
+ if (statusArg !== undefined) this.status = statusArg;
67
+ }
68
+ }
69
+
70
+ export interface IOpenAiChatGptModelListOptions {
71
+ signal?: AbortSignal;
72
+ /** Stable provider originator header. Defaults to `smartai`. */
73
+ originator?: string;
74
+ /** Optional underlying transport. Defaults to the current global fetch. */
75
+ fetch?: typeof fetch;
76
+ }
77
+
78
+ const protocolError = (): OpenAiChatGptModelListError => new OpenAiChatGptModelListError('PROTOCOL_ERROR');
79
+
80
+ const isRecord = (valueArg: unknown): valueArg is Record<string, unknown> =>
81
+ typeof valueArg === 'object' && valueArg !== null && !Array.isArray(valueArg);
82
+
83
+ const textEncoder = new TextEncoder();
84
+
85
+ const boundedString = (
86
+ valueArg: unknown,
87
+ maximumBytesArg: number,
88
+ allowEmptyArg = false,
89
+ ): string => {
90
+ if (
91
+ typeof valueArg !== 'string'
92
+ || (!allowEmptyArg && valueArg.length === 0)
93
+ || textEncoder.encode(valueArg).byteLength > maximumBytesArg
94
+ ) {
95
+ throw protocolError();
96
+ }
97
+ return valueArg;
98
+ };
99
+
100
+ const readBoundedJson = async (
101
+ responseArg: Response,
102
+ signalArg: AbortSignal | undefined,
103
+ maximumBytesArg: number,
104
+ ): Promise<unknown> => {
105
+ const declared = responseArg.headers.get('content-length');
106
+ if (declared !== null && (!/^\d+$/.test(declared) || Number(declared) > maximumBytesArg)) {
107
+ await responseArg.body?.cancel().catch(() => undefined);
108
+ throw protocolError();
109
+ }
110
+ if (!responseArg.body) throw protocolError();
111
+ const reader = responseArg.body.getReader();
112
+ const chunks: Uint8Array[] = [];
113
+ let size = 0;
114
+ let completed = false;
115
+ const onAbort = (): void => {
116
+ void reader.cancel().catch(() => undefined);
117
+ };
118
+ signalArg?.addEventListener('abort', onAbort, { once: true });
119
+ if (signalArg?.aborted) onAbort();
120
+ try {
121
+ while (true) {
122
+ signalArg?.throwIfAborted();
123
+ const { done, value } = await reader.read();
124
+ signalArg?.throwIfAborted();
125
+ if (done) break;
126
+ size += value.byteLength;
127
+ if (size > maximumBytesArg) {
128
+ await reader.cancel().catch(() => undefined);
129
+ throw protocolError();
130
+ }
131
+ chunks.push(value);
132
+ }
133
+ completed = true;
134
+ } finally {
135
+ signalArg?.removeEventListener('abort', onAbort);
136
+ if (!completed) void reader.cancel().catch(() => undefined);
137
+ reader.releaseLock();
138
+ }
139
+ const body = new Uint8Array(size);
140
+ let offset = 0;
141
+ for (const chunk of chunks) {
142
+ body.set(chunk, offset);
143
+ offset += chunk.byteLength;
144
+ }
145
+ try {
146
+ return JSON.parse(new TextDecoder().decode(body)) as unknown;
147
+ } catch {
148
+ throw protocolError();
149
+ }
150
+ };
151
+
152
+ const projectCatalog = (valueArg: unknown): IOpenAiChatGptModel[] => {
153
+ if (!isRecord(valueArg) || !Array.isArray(valueArg.models) || valueArg.models.length > maximumCatalogModels) {
154
+ throw protocolError();
155
+ }
156
+ const modelIds = new Set<string>();
157
+ const entries = valueArg.models.map((rawModel, sourceIndex) => {
158
+ if (!isRecord(rawModel)) throw protocolError();
159
+ const modelId = boundedString(rawModel.slug, 256);
160
+ if (modelIds.has(modelId)) throw protocolError();
161
+ modelIds.add(modelId);
162
+ const visibility = rawModel.visibility;
163
+ if (visibility !== 'list' && visibility !== 'hide' && visibility !== 'none') {
164
+ throw protocolError();
165
+ }
166
+ if (!Number.isSafeInteger(rawModel.priority)) throw protocolError();
167
+ if (!Array.isArray(rawModel.supported_reasoning_levels) || rawModel.supported_reasoning_levels.length > 16) {
168
+ throw protocolError();
169
+ }
170
+ const reasoningEfforts = rawModel.supported_reasoning_levels.map((rawEffort) => {
171
+ if (!isRecord(rawEffort)) throw protocolError();
172
+ return {
173
+ effort: boundedString(rawEffort.effort, 128),
174
+ description: boundedString(rawEffort.description, 1024, true),
175
+ };
176
+ });
177
+ const modalities = rawModel.input_modalities ?? ['text', 'image'];
178
+ if (
179
+ !Array.isArray(modalities)
180
+ || modalities.length > 3
181
+ || modalities.some((entry) => entry !== 'text' && entry !== 'image' && entry !== 'audio')
182
+ || new Set(modalities).size !== modalities.length
183
+ ) {
184
+ throw protocolError();
185
+ }
186
+ const rawServiceTiers = rawModel.service_tiers ?? [];
187
+ if (!Array.isArray(rawServiceTiers) || rawServiceTiers.length > 16) {
188
+ throw protocolError();
189
+ }
190
+ const serviceTiers = rawServiceTiers.map((rawTier) => {
191
+ if (!isRecord(rawTier)) throw protocolError();
192
+ return {
193
+ id: boundedString(rawTier.id, 128),
194
+ name: boundedString(rawTier.name, 256),
195
+ description: boundedString(rawTier.description, 1024, true),
196
+ };
197
+ });
198
+ const contextWindow = rawModel.context_window;
199
+ if (
200
+ contextWindow !== null
201
+ && contextWindow !== undefined
202
+ && (!Number.isSafeInteger(contextWindow) || (contextWindow as number) < 1)
203
+ ) {
204
+ throw protocolError();
205
+ }
206
+ const modelMessages = isRecord(rawModel.model_messages) ? rawModel.model_messages : undefined;
207
+ const variables = isRecord(modelMessages?.instructions_variables)
208
+ ? modelMessages.instructions_variables
209
+ : undefined;
210
+ const supportsPersonality = typeof modelMessages?.instructions_template === 'string'
211
+ && modelMessages.instructions_template.includes('{{ personality }}')
212
+ && typeof variables?.personality_default === 'string'
213
+ && typeof variables.personality_friendly === 'string'
214
+ && typeof variables.personality_pragmatic === 'string';
215
+ return {
216
+ priority: rawModel.priority as number,
217
+ sourceIndex,
218
+ model: {
219
+ providerId: 'openai' as const,
220
+ modelId,
221
+ wireModel: modelId,
222
+ displayName: boundedString(rawModel.display_name, 512),
223
+ description: rawModel.description === null || rawModel.description === undefined
224
+ ? ''
225
+ : boundedString(rawModel.description, 4096, true),
226
+ hidden: visibility !== 'list',
227
+ reasoningEfforts,
228
+ defaultReasoningEffort: rawModel.default_reasoning_level === null
229
+ || rawModel.default_reasoning_level === undefined
230
+ ? 'none'
231
+ : boundedString(rawModel.default_reasoning_level, 128),
232
+ inputModalities: modalities as TOpenAiChatGptInputModality[],
233
+ supportsPersonality,
234
+ serviceTiers,
235
+ ...(rawModel.default_service_tier === null || rawModel.default_service_tier === undefined
236
+ ? {}
237
+ : { defaultServiceTier: boundedString(rawModel.default_service_tier, 128) }),
238
+ ...(contextWindow === null || contextWindow === undefined ? {} : { contextWindow: contextWindow as number }),
239
+ isDefault: false,
240
+ },
241
+ };
242
+ });
243
+ entries.sort((left, right) => left.priority - right.priority || left.sourceIndex - right.sourceIndex);
244
+ const models = entries.map((entry) => entry.model);
245
+ const defaultModel = models.find((model) => !model.hidden) ?? models[0];
246
+ if (defaultModel) defaultModel.isDefault = true;
247
+ return models;
248
+ };
249
+
250
+ /**
251
+ * Reads the Codex model catalog of one ChatGPT account with access alone.
252
+ *
253
+ * It returns every model, hidden ones included, in the catalog's priority order. A rejected access token
254
+ * fails with an `OpenAiChatGptAuthError` whose `status` is 401, so a caller that holds a renewable access
255
+ * resolves it again and repeats the read. An abort rejects with the signal's reason; every other failure
256
+ * is an `OpenAiChatGptModelListError`.
257
+ */
258
+ export async function listOpenAiChatGptModels(
259
+ accessArg: IOpenAiChatGptReadAccess,
260
+ optionsArg: IOpenAiChatGptModelListOptions = {},
261
+ ): Promise<IOpenAiChatGptModel[]> {
262
+ if (
263
+ !accessArg
264
+ || typeof accessArg.accessToken !== 'string'
265
+ || accessArg.accessToken.length === 0
266
+ || (accessArg.accountId !== undefined && (typeof accessArg.accountId !== 'string' || accessArg.accountId.length === 0))
267
+ || typeof accessArg.isFedrampAccount !== 'boolean'
268
+ ) {
269
+ throw new OpenAiChatGptAuthError('OpenAI ChatGPT access is invalid.');
270
+ }
271
+ const transport = optionsArg.fetch ?? globalThis.fetch;
272
+ if (typeof transport !== 'function') {
273
+ throw new OpenAiChatGptAuthError('fetch is not available for the OpenAI ChatGPT model catalog.');
274
+ }
275
+ const signal = optionsArg.signal;
276
+ signal?.throwIfAborted();
277
+ const url = `${OPENAI_CHATGPT_CODEX_BASE_URL}/models?client_version=${encodeURIComponent(OPENAI_CHATGPT_CATALOG_CLIENT_VERSION)}`;
278
+ let response: Response;
279
+ let onAbort: (() => void) | undefined;
280
+ try {
281
+ const fetchPromise = transport(url, {
282
+ method: 'GET',
283
+ redirect: 'error',
284
+ signal,
285
+ headers: {
286
+ Accept: 'application/json',
287
+ ...createOpenAiChatGptAccessHeaders(accessArg, optionsArg.originator ?? OPENAI_CHATGPT_DEFAULT_ORIGINATOR),
288
+ version: OPENAI_CHATGPT_CATALOG_CLIENT_VERSION,
289
+ },
290
+ });
291
+ if (!signal) {
292
+ response = await fetchPromise;
293
+ } else {
294
+ // A transport that ignores the signal must not hold the caller: the abort settles the read, and a
295
+ // response that arrives later is released.
296
+ void fetchPromise.then((lateResponse) => {
297
+ if (signal.aborted) void lateResponse.body?.cancel().catch(() => undefined);
298
+ }).catch(() => undefined);
299
+ const aborted = new Promise<never>((_resolve, reject) => {
300
+ onAbort = () => reject(signal.reason);
301
+ signal.addEventListener('abort', onAbort, { once: true });
302
+ });
303
+ response = await Promise.race([fetchPromise, aborted]);
304
+ }
305
+ } catch {
306
+ if (signal?.aborted) throw signal.reason;
307
+ throw new OpenAiChatGptModelListError('NETWORK_FAILED');
308
+ } finally {
309
+ if (onAbort) signal?.removeEventListener('abort', onAbort);
310
+ }
311
+ if (!response.ok) {
312
+ await response.body?.cancel().catch(() => undefined);
313
+ if (response.status === 401) {
314
+ throw new OpenAiChatGptAuthError('OpenAI ChatGPT rejected the access token.', { status: 401 });
315
+ }
316
+ throw new OpenAiChatGptModelListError('HTTP_ERROR', response.status);
317
+ }
318
+ return projectCatalog(await readBoundedJson(response, signal, maximumCatalogBytes));
319
+ }
@@ -1,6 +1,5 @@
1
- import { readEpochSeconds } from '@modelprofile.com/flexharness-models';
2
- import type { IModelCallFailure, TModelLimitClassifier } from '@modelprofile.com/flexharness-models';
3
- import { readOpenAiError } from '@modelprofile.com/flexharness-providers/openai';
1
+ import * as plugins from './plugins.js';
2
+ import { readOpenAiError } from './smartai.limits.openai.js';
4
3
 
5
4
  const codexUsageLimitIdentifiers: ReadonlySet<string> = new Set(['usage_limit_reached', 'usage_not_included']);
6
5
  const codexWindows = ['primary', 'secondary'] as const;
@@ -16,11 +15,11 @@ function formatWindowMinutes(minutes: number): string {
16
15
  return `${minutes}m`;
17
16
  }
18
17
 
19
- function readExhaustedWindow(headers: IModelCallFailure['headers']): ICodexWindow | undefined {
18
+ function readExhaustedWindow(headers: plugins.IModelCallFailure['headers']): ICodexWindow | undefined {
20
19
  const exhausted = codexWindows.flatMap((name): ICodexWindow[] => {
21
20
  const usedPercent = Number(headers[`x-codex-${name}-used-percent`]);
22
21
  const minutes = Number(headers[`x-codex-${name}-window-minutes`]);
23
- const resetsAt = readEpochSeconds(headers[`x-codex-${name}-reset-at`]);
22
+ const resetsAt = plugins.readEpochSeconds(headers[`x-codex-${name}-reset-at`]);
24
23
  if (!(usedPercent >= 100) || resetsAt === undefined) return [];
25
24
  return [{ label: Number.isSafeInteger(minutes) && minutes > 0 ? formatWindowMinutes(minutes) : name, resetsAt }];
26
25
  });
@@ -38,11 +37,11 @@ function readResetsInSeconds(value: unknown, observedAt: number): number | undef
38
37
  * The ChatGPT Codex backend reports a spent plan quota as `usage_limit_reached`, and a plan
39
38
  * without Codex access as `usage_not_included`, in the 429 body or a `response.failed` frame.
40
39
  */
41
- export const classifyOpenAiChatGptLimit: TModelLimitClassifier = (failure) => {
40
+ export const classifyOpenAiChatGptLimit: plugins.TModelLimitClassifier = (failure) => {
42
41
  const error = readOpenAiError(failure.body);
43
42
  if (!error?.identifiers.some((identifier) => codexUsageLimitIdentifiers.has(identifier))) return undefined;
44
43
  const exhaustedWindow = readExhaustedWindow(failure.headers);
45
- const resetsAt = readEpochSeconds(error.fields.resets_at)
44
+ const resetsAt = plugins.readEpochSeconds(error.fields.resets_at)
46
45
  ?? readResetsInSeconds(error.fields.resets_in_seconds, failure.observedAt)
47
46
  ?? exhaustedWindow?.resetsAt;
48
47
  const planType = error.fields.plan_type;
@@ -0,0 +1,71 @@
1
+ import type * as plugins from './plugins.js';
2
+
3
+ const isNonEmptyString = (value: unknown): value is string => typeof value === 'string' && value.trim().length > 0;
4
+
5
+ type TPrompt = plugins.LanguageModelV4CallOptions['prompt'];
6
+
7
+ /** The number of system messages the prompt opens with, before its first other message. */
8
+ const countLeadingSystemMessages = (prompt: TPrompt): number => {
9
+ const firstOther = prompt.findIndex((message) => message.role !== 'system');
10
+ return firstOther === -1 ? prompt.length : firstOther;
11
+ };
12
+
13
+ const joinSystemInstructions = (messages: TPrompt): string | undefined => {
14
+ const instructions = messages
15
+ .map((message) => message.content)
16
+ .filter(isNonEmptyString);
17
+
18
+ return instructions.length > 0 ? instructions.join('\n') : undefined;
19
+ };
20
+
21
+ const hasUnsupportedChatGptCacheRetention = (options: plugins.JSONObject): boolean => {
22
+ return options.promptCacheRetention !== undefined || options.prompt_cache_retention !== undefined;
23
+ };
24
+
25
+ /**
26
+ * ChatGPT's Codex backend requires top-level Responses API instructions.
27
+ *
28
+ * The system messages a prompt opens with become those instructions. A system message later in the
29
+ * conversation keeps its place and is sent as a `developer` input item, the role the Codex backend reads
30
+ * mid-conversation instructions in, so moving it would neither reorder the conversation nor change the
31
+ * instructions prefix a prompt cache keys on. A caller's own `instructions` or `systemMessageMode` is kept.
32
+ */
33
+ export function createOpenAiChatGptInstructionsMiddleware(): plugins.LanguageModelV4Middleware {
34
+ return {
35
+ specificationVersion: 'v4',
36
+ transformParams: async ({ params }) => {
37
+ const providerOptions = params.providerOptions ?? {};
38
+ const openAiProviderOptions = providerOptions.openai ?? {};
39
+ const leadingCount = countLeadingSystemMessages(params.prompt);
40
+ const shouldHoist = leadingCount > 0 && !isNonEmptyString(openAiProviderOptions.instructions);
41
+ const instructions = shouldHoist ? joinSystemInstructions(params.prompt.slice(0, leadingCount)) : undefined;
42
+ const prompt = shouldHoist ? params.prompt.slice(leadingCount) : params.prompt;
43
+ const keepsSystemMessages = prompt.some((message) => message.role === 'system');
44
+ const shouldSetDeveloperMode = keepsSystemMessages && openAiProviderOptions.systemMessageMode === undefined;
45
+ const shouldStripCacheRetention = hasUnsupportedChatGptCacheRetention(openAiProviderOptions);
46
+
47
+ if (!shouldHoist && !shouldSetDeveloperMode && !shouldStripCacheRetention) {
48
+ return params;
49
+ }
50
+
51
+ const nextOpenAiProviderOptions: plugins.JSONObject = { ...openAiProviderOptions };
52
+ delete nextOpenAiProviderOptions.promptCacheRetention;
53
+ delete nextOpenAiProviderOptions.prompt_cache_retention;
54
+ if (instructions) {
55
+ nextOpenAiProviderOptions.instructions = instructions;
56
+ }
57
+ if (shouldSetDeveloperMode) {
58
+ nextOpenAiProviderOptions.systemMessageMode = 'developer';
59
+ }
60
+
61
+ return {
62
+ ...params,
63
+ prompt,
64
+ providerOptions: {
65
+ ...providerOptions,
66
+ openai: nextOpenAiProviderOptions,
67
+ },
68
+ } satisfies plugins.LanguageModelV4CallOptions;
69
+ },
70
+ };
71
+ }