@modelprofile.com/flexharness-providers 9.0.0 → 9.1.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_openai_account/plugins.d.ts +2 -0
- package/dist_ts_openai_account/plugins.js +4 -1
- package/dist_ts_openai_account/smartai.providers.interfaces.d.ts +6 -22
- package/dist_ts_openai_account/smartai.providers.openai.js +15 -134
- package/dist_ts_openai_auth/index.d.ts +1 -2
- package/dist_ts_openai_auth/index.js +2 -3
- package/dist_ts_openai_auth/interfaces.d.ts +1 -1
- package/dist_ts_openai_auth/smartai.auth.openai.d.ts +3 -15
- package/dist_ts_openai_auth/smartai.auth.openai.js +3 -17
- package/dist_ts_openai_auth/smartai.connection.openai.d.ts +1 -1
- package/dist_ts_openai_auth/smartai.connection.openai.js +2 -3
- package/dist_ts_providers/openai/index.d.ts +4 -0
- package/dist_ts_providers/openai/index.js +5 -1
- package/dist_ts_providers/openai/plugins.d.ts +4 -4
- package/dist_ts_providers/openai/plugins.js +3 -3
- package/dist_ts_providers/openai/smartai.chatgpt.catalog.d.ts +59 -0
- package/dist_ts_providers/openai/smartai.chatgpt.catalog.js +245 -0
- package/{dist_ts_openai_auth/smartai.limits.openai.d.ts → dist_ts_providers/openai/smartai.chatgpt.limits.d.ts} +2 -2
- package/dist_ts_providers/openai/smartai.chatgpt.limits.js +49 -0
- package/{dist_ts_openai_auth/smartai.middleware.openai.d.ts → dist_ts_providers/openai/smartai.chatgpt.middleware.d.ts} +2 -2
- package/dist_ts_providers/openai/smartai.chatgpt.middleware.js +47 -0
- package/dist_ts_providers/openai/smartai.chatgpt.openai.d.ts +92 -0
- package/dist_ts_providers/openai/smartai.chatgpt.openai.js +160 -0
- package/package.json +2 -2
- package/readme.md +8 -0
- package/ts_openai_account/plugins.ts +5 -0
- package/ts_openai_account/smartai.providers.interfaces.ts +11 -25
- package/ts_openai_account/smartai.providers.openai.ts +16 -135
- package/ts_openai_auth/index.ts +4 -2
- package/ts_openai_auth/interfaces.ts +1 -1
- package/ts_openai_auth/smartai.auth.openai.ts +12 -24
- package/ts_openai_auth/smartai.connection.openai.ts +5 -3
- package/ts_providers/openai/index.ts +29 -0
- package/ts_providers/openai/plugins.ts +4 -3
- package/ts_providers/openai/readme.md +58 -7
- package/ts_providers/openai/smartai.chatgpt.catalog.ts +308 -0
- package/{ts_openai_auth/smartai.limits.openai.ts → ts_providers/openai/smartai.chatgpt.limits.ts} +6 -7
- package/{ts_openai_auth/smartai.middleware.openai.ts → ts_providers/openai/smartai.chatgpt.middleware.ts} +6 -6
- package/ts_providers/openai/smartai.chatgpt.openai.ts +271 -0
- package/ts_providers/readme.md +8 -0
- package/dist_ts_openai_auth/smartai.limits.openai.js +0 -49
- package/dist_ts_openai_auth/smartai.middleware.openai.js +0 -47
|
@@ -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
|
|
22
|
-
|
|
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,62 @@ 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
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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`.
|
|
97
|
+
|
|
98
|
+
Both ChatGPT connections classify Codex limits with `classifyOpenAiChatGptLimit` and move system
|
|
99
|
+
prompts into the Responses `instructions` with `createOpenAiChatGptInstructionsMiddleware()`; both
|
|
100
|
+
are exported here for connections of their own.
|
|
101
|
+
|
|
102
|
+
The `/auth` subpath keeps its connections, which derive identity from the token itself, and
|
|
103
|
+
re-exports the ChatGPT limit classifier, the instructions middleware, the Codex base URL, the
|
|
104
|
+
default originator and `OpenAiChatGptAuthError` from here.
|
|
54
105
|
|
|
55
106
|
## Issue Reporting and Security
|
|
56
107
|
|
|
@@ -0,0 +1,308 @@
|
|
|
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 first listed model, or the first model when none is listed. */
|
|
44
|
+
isDefault: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Why a catalog read failed, other than a rejected access token or an abort.
|
|
49
|
+
*
|
|
50
|
+
* - `NETWORK_FAILED`: the request did not complete.
|
|
51
|
+
* - `HTTP_ERROR`: the backend answered with an error status other than 401; `status` names it.
|
|
52
|
+
* - `PROTOCOL_ERROR`: the answer was oversized or not a catalog this package can read.
|
|
53
|
+
*/
|
|
54
|
+
export type TOpenAiChatGptModelListErrorCode = 'NETWORK_FAILED' | 'HTTP_ERROR' | 'PROTOCOL_ERROR';
|
|
55
|
+
|
|
56
|
+
export class OpenAiChatGptModelListError extends Error {
|
|
57
|
+
public readonly code: TOpenAiChatGptModelListErrorCode;
|
|
58
|
+
public readonly status?: number;
|
|
59
|
+
|
|
60
|
+
constructor(codeArg: TOpenAiChatGptModelListErrorCode, statusArg?: number) {
|
|
61
|
+
super(`OpenAI ChatGPT model catalog request failed (${codeArg}${statusArg === undefined ? '' : ` ${statusArg}`}).`);
|
|
62
|
+
this.name = 'OpenAiChatGptModelListError';
|
|
63
|
+
this.code = codeArg;
|
|
64
|
+
if (statusArg !== undefined) this.status = statusArg;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface IOpenAiChatGptModelListOptions {
|
|
69
|
+
signal?: AbortSignal;
|
|
70
|
+
/** Stable provider originator header. Defaults to `smartai`. */
|
|
71
|
+
originator?: string;
|
|
72
|
+
/** Optional underlying transport. Defaults to the current global fetch. */
|
|
73
|
+
fetch?: typeof fetch;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const protocolError = (): OpenAiChatGptModelListError => new OpenAiChatGptModelListError('PROTOCOL_ERROR');
|
|
77
|
+
|
|
78
|
+
const isRecord = (valueArg: unknown): valueArg is Record<string, unknown> =>
|
|
79
|
+
typeof valueArg === 'object' && valueArg !== null && !Array.isArray(valueArg);
|
|
80
|
+
|
|
81
|
+
const textEncoder = new TextEncoder();
|
|
82
|
+
|
|
83
|
+
const boundedString = (
|
|
84
|
+
valueArg: unknown,
|
|
85
|
+
maximumBytesArg: number,
|
|
86
|
+
allowEmptyArg = false,
|
|
87
|
+
): string => {
|
|
88
|
+
if (
|
|
89
|
+
typeof valueArg !== 'string'
|
|
90
|
+
|| (!allowEmptyArg && valueArg.length === 0)
|
|
91
|
+
|| textEncoder.encode(valueArg).byteLength > maximumBytesArg
|
|
92
|
+
) {
|
|
93
|
+
throw protocolError();
|
|
94
|
+
}
|
|
95
|
+
return valueArg;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const readBoundedJson = async (
|
|
99
|
+
responseArg: Response,
|
|
100
|
+
signalArg: AbortSignal | undefined,
|
|
101
|
+
maximumBytesArg: number,
|
|
102
|
+
): Promise<unknown> => {
|
|
103
|
+
const declared = responseArg.headers.get('content-length');
|
|
104
|
+
if (declared !== null && (!/^\d+$/.test(declared) || Number(declared) > maximumBytesArg)) {
|
|
105
|
+
await responseArg.body?.cancel().catch(() => undefined);
|
|
106
|
+
throw protocolError();
|
|
107
|
+
}
|
|
108
|
+
if (!responseArg.body) throw protocolError();
|
|
109
|
+
const reader = responseArg.body.getReader();
|
|
110
|
+
const chunks: Uint8Array[] = [];
|
|
111
|
+
let size = 0;
|
|
112
|
+
let completed = false;
|
|
113
|
+
const onAbort = (): void => {
|
|
114
|
+
void reader.cancel().catch(() => undefined);
|
|
115
|
+
};
|
|
116
|
+
signalArg?.addEventListener('abort', onAbort, { once: true });
|
|
117
|
+
if (signalArg?.aborted) onAbort();
|
|
118
|
+
try {
|
|
119
|
+
while (true) {
|
|
120
|
+
signalArg?.throwIfAborted();
|
|
121
|
+
const { done, value } = await reader.read();
|
|
122
|
+
signalArg?.throwIfAborted();
|
|
123
|
+
if (done) break;
|
|
124
|
+
size += value.byteLength;
|
|
125
|
+
if (size > maximumBytesArg) {
|
|
126
|
+
await reader.cancel().catch(() => undefined);
|
|
127
|
+
throw protocolError();
|
|
128
|
+
}
|
|
129
|
+
chunks.push(value);
|
|
130
|
+
}
|
|
131
|
+
completed = true;
|
|
132
|
+
} finally {
|
|
133
|
+
signalArg?.removeEventListener('abort', onAbort);
|
|
134
|
+
if (!completed) void reader.cancel().catch(() => undefined);
|
|
135
|
+
reader.releaseLock();
|
|
136
|
+
}
|
|
137
|
+
const body = new Uint8Array(size);
|
|
138
|
+
let offset = 0;
|
|
139
|
+
for (const chunk of chunks) {
|
|
140
|
+
body.set(chunk, offset);
|
|
141
|
+
offset += chunk.byteLength;
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
return JSON.parse(new TextDecoder().decode(body)) as unknown;
|
|
145
|
+
} catch {
|
|
146
|
+
throw protocolError();
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const projectCatalog = (valueArg: unknown): IOpenAiChatGptModel[] => {
|
|
151
|
+
if (!isRecord(valueArg) || !Array.isArray(valueArg.models) || valueArg.models.length > maximumCatalogModels) {
|
|
152
|
+
throw protocolError();
|
|
153
|
+
}
|
|
154
|
+
const modelIds = new Set<string>();
|
|
155
|
+
const entries = valueArg.models.map((rawModel, sourceIndex) => {
|
|
156
|
+
if (!isRecord(rawModel)) throw protocolError();
|
|
157
|
+
const modelId = boundedString(rawModel.slug, 256);
|
|
158
|
+
if (modelIds.has(modelId)) throw protocolError();
|
|
159
|
+
modelIds.add(modelId);
|
|
160
|
+
const visibility = rawModel.visibility;
|
|
161
|
+
if (visibility !== 'list' && visibility !== 'hide' && visibility !== 'none') {
|
|
162
|
+
throw protocolError();
|
|
163
|
+
}
|
|
164
|
+
if (!Number.isSafeInteger(rawModel.priority)) throw protocolError();
|
|
165
|
+
if (!Array.isArray(rawModel.supported_reasoning_levels) || rawModel.supported_reasoning_levels.length > 16) {
|
|
166
|
+
throw protocolError();
|
|
167
|
+
}
|
|
168
|
+
const reasoningEfforts = rawModel.supported_reasoning_levels.map((rawEffort) => {
|
|
169
|
+
if (!isRecord(rawEffort)) throw protocolError();
|
|
170
|
+
return {
|
|
171
|
+
effort: boundedString(rawEffort.effort, 128),
|
|
172
|
+
description: boundedString(rawEffort.description, 1024, true),
|
|
173
|
+
};
|
|
174
|
+
});
|
|
175
|
+
const modalities = rawModel.input_modalities ?? ['text', 'image'];
|
|
176
|
+
if (
|
|
177
|
+
!Array.isArray(modalities)
|
|
178
|
+
|| modalities.length > 3
|
|
179
|
+
|| modalities.some((entry) => entry !== 'text' && entry !== 'image' && entry !== 'audio')
|
|
180
|
+
|| new Set(modalities).size !== modalities.length
|
|
181
|
+
) {
|
|
182
|
+
throw protocolError();
|
|
183
|
+
}
|
|
184
|
+
const rawServiceTiers = rawModel.service_tiers ?? [];
|
|
185
|
+
if (!Array.isArray(rawServiceTiers) || rawServiceTiers.length > 16) {
|
|
186
|
+
throw protocolError();
|
|
187
|
+
}
|
|
188
|
+
const serviceTiers = rawServiceTiers.map((rawTier) => {
|
|
189
|
+
if (!isRecord(rawTier)) throw protocolError();
|
|
190
|
+
return {
|
|
191
|
+
id: boundedString(rawTier.id, 128),
|
|
192
|
+
name: boundedString(rawTier.name, 256),
|
|
193
|
+
description: boundedString(rawTier.description, 1024, true),
|
|
194
|
+
};
|
|
195
|
+
});
|
|
196
|
+
const modelMessages = isRecord(rawModel.model_messages) ? rawModel.model_messages : undefined;
|
|
197
|
+
const variables = isRecord(modelMessages?.instructions_variables)
|
|
198
|
+
? modelMessages.instructions_variables
|
|
199
|
+
: undefined;
|
|
200
|
+
const supportsPersonality = typeof modelMessages?.instructions_template === 'string'
|
|
201
|
+
&& modelMessages.instructions_template.includes('{{ personality }}')
|
|
202
|
+
&& typeof variables?.personality_default === 'string'
|
|
203
|
+
&& typeof variables.personality_friendly === 'string'
|
|
204
|
+
&& typeof variables.personality_pragmatic === 'string';
|
|
205
|
+
return {
|
|
206
|
+
priority: rawModel.priority as number,
|
|
207
|
+
sourceIndex,
|
|
208
|
+
model: {
|
|
209
|
+
providerId: 'openai' as const,
|
|
210
|
+
modelId,
|
|
211
|
+
wireModel: modelId,
|
|
212
|
+
displayName: boundedString(rawModel.display_name, 512),
|
|
213
|
+
description: rawModel.description === null || rawModel.description === undefined
|
|
214
|
+
? ''
|
|
215
|
+
: boundedString(rawModel.description, 4096, true),
|
|
216
|
+
hidden: visibility !== 'list',
|
|
217
|
+
reasoningEfforts,
|
|
218
|
+
defaultReasoningEffort: rawModel.default_reasoning_level === null
|
|
219
|
+
|| rawModel.default_reasoning_level === undefined
|
|
220
|
+
? 'none'
|
|
221
|
+
: boundedString(rawModel.default_reasoning_level, 128),
|
|
222
|
+
inputModalities: modalities as TOpenAiChatGptInputModality[],
|
|
223
|
+
supportsPersonality,
|
|
224
|
+
serviceTiers,
|
|
225
|
+
...(rawModel.default_service_tier === null || rawModel.default_service_tier === undefined
|
|
226
|
+
? {}
|
|
227
|
+
: { defaultServiceTier: boundedString(rawModel.default_service_tier, 128) }),
|
|
228
|
+
isDefault: false,
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
});
|
|
232
|
+
entries.sort((left, right) => left.priority - right.priority || left.sourceIndex - right.sourceIndex);
|
|
233
|
+
const models = entries.map((entry) => entry.model);
|
|
234
|
+
const defaultModel = models.find((model) => !model.hidden) ?? models[0];
|
|
235
|
+
if (defaultModel) defaultModel.isDefault = true;
|
|
236
|
+
return models;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Reads the Codex model catalog of one ChatGPT account with access alone.
|
|
241
|
+
*
|
|
242
|
+
* It returns every model, hidden ones included, in the catalog's priority order. A rejected access token
|
|
243
|
+
* fails with an `OpenAiChatGptAuthError` whose `status` is 401, so a caller that holds a renewable access
|
|
244
|
+
* resolves it again and repeats the read. An abort rejects with the signal's reason; every other failure
|
|
245
|
+
* is an `OpenAiChatGptModelListError`.
|
|
246
|
+
*/
|
|
247
|
+
export async function listOpenAiChatGptModels(
|
|
248
|
+
accessArg: IOpenAiChatGptReadAccess,
|
|
249
|
+
optionsArg: IOpenAiChatGptModelListOptions = {},
|
|
250
|
+
): Promise<IOpenAiChatGptModel[]> {
|
|
251
|
+
if (
|
|
252
|
+
!accessArg
|
|
253
|
+
|| typeof accessArg.accessToken !== 'string'
|
|
254
|
+
|| accessArg.accessToken.length === 0
|
|
255
|
+
|| (accessArg.accountId !== undefined && (typeof accessArg.accountId !== 'string' || accessArg.accountId.length === 0))
|
|
256
|
+
|| typeof accessArg.isFedrampAccount !== 'boolean'
|
|
257
|
+
) {
|
|
258
|
+
throw new OpenAiChatGptAuthError('OpenAI ChatGPT access is invalid.');
|
|
259
|
+
}
|
|
260
|
+
const transport = optionsArg.fetch ?? globalThis.fetch;
|
|
261
|
+
if (typeof transport !== 'function') {
|
|
262
|
+
throw new OpenAiChatGptAuthError('fetch is not available for the OpenAI ChatGPT model catalog.');
|
|
263
|
+
}
|
|
264
|
+
const signal = optionsArg.signal;
|
|
265
|
+
signal?.throwIfAborted();
|
|
266
|
+
const url = `${OPENAI_CHATGPT_CODEX_BASE_URL}/models?client_version=${encodeURIComponent(OPENAI_CHATGPT_CATALOG_CLIENT_VERSION)}`;
|
|
267
|
+
let response: Response;
|
|
268
|
+
let onAbort: (() => void) | undefined;
|
|
269
|
+
try {
|
|
270
|
+
const fetchPromise = transport(url, {
|
|
271
|
+
method: 'GET',
|
|
272
|
+
redirect: 'error',
|
|
273
|
+
signal,
|
|
274
|
+
headers: {
|
|
275
|
+
Accept: 'application/json',
|
|
276
|
+
...createOpenAiChatGptAccessHeaders(accessArg, optionsArg.originator ?? OPENAI_CHATGPT_DEFAULT_ORIGINATOR),
|
|
277
|
+
version: OPENAI_CHATGPT_CATALOG_CLIENT_VERSION,
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
if (!signal) {
|
|
281
|
+
response = await fetchPromise;
|
|
282
|
+
} else {
|
|
283
|
+
// A transport that ignores the signal must not hold the caller: the abort settles the read, and a
|
|
284
|
+
// response that arrives later is released.
|
|
285
|
+
void fetchPromise.then((lateResponse) => {
|
|
286
|
+
if (signal.aborted) void lateResponse.body?.cancel().catch(() => undefined);
|
|
287
|
+
}).catch(() => undefined);
|
|
288
|
+
const aborted = new Promise<never>((_resolve, reject) => {
|
|
289
|
+
onAbort = () => reject(signal.reason);
|
|
290
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
291
|
+
});
|
|
292
|
+
response = await Promise.race([fetchPromise, aborted]);
|
|
293
|
+
}
|
|
294
|
+
} catch {
|
|
295
|
+
if (signal?.aborted) throw signal.reason;
|
|
296
|
+
throw new OpenAiChatGptModelListError('NETWORK_FAILED');
|
|
297
|
+
} finally {
|
|
298
|
+
if (onAbort) signal?.removeEventListener('abort', onAbort);
|
|
299
|
+
}
|
|
300
|
+
if (!response.ok) {
|
|
301
|
+
await response.body?.cancel().catch(() => undefined);
|
|
302
|
+
if (response.status === 401) {
|
|
303
|
+
throw new OpenAiChatGptAuthError('OpenAI ChatGPT rejected the access token.', { status: 401 });
|
|
304
|
+
}
|
|
305
|
+
throw new OpenAiChatGptModelListError('HTTP_ERROR', response.status);
|
|
306
|
+
}
|
|
307
|
+
return projectCatalog(await readBoundedJson(response, signal, maximumCatalogBytes));
|
|
308
|
+
}
|
package/{ts_openai_auth/smartai.limits.openai.ts → ts_providers/openai/smartai.chatgpt.limits.ts}
RENAMED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
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;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import type * as plugins from './plugins.js';
|
|
2
2
|
|
|
3
3
|
const isNonEmptyString = (value: unknown): value is string => typeof value === 'string' && value.trim().length > 0;
|
|
4
4
|
|
|
5
|
-
const getSystemInstructions = (prompt: LanguageModelV4CallOptions['prompt']): string | undefined => {
|
|
5
|
+
const getSystemInstructions = (prompt: plugins.LanguageModelV4CallOptions['prompt']): string | undefined => {
|
|
6
6
|
const instructions = prompt
|
|
7
7
|
.filter((message) => message.role === 'system')
|
|
8
8
|
.map((message) => message.content)
|
|
@@ -11,7 +11,7 @@ const getSystemInstructions = (prompt: LanguageModelV4CallOptions['prompt']): st
|
|
|
11
11
|
return instructions.length > 0 ? instructions.join('\n') : undefined;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
-
const hasUnsupportedChatGptCacheRetention = (options: JSONObject): boolean => {
|
|
14
|
+
const hasUnsupportedChatGptCacheRetention = (options: plugins.JSONObject): boolean => {
|
|
15
15
|
return options.promptCacheRetention !== undefined || options.prompt_cache_retention !== undefined;
|
|
16
16
|
};
|
|
17
17
|
|
|
@@ -19,7 +19,7 @@ const hasUnsupportedChatGptCacheRetention = (options: JSONObject): boolean => {
|
|
|
19
19
|
* ChatGPT's Codex backend requires top-level Responses API instructions.
|
|
20
20
|
* The standard OpenAI provider otherwise serializes system prompts as input items.
|
|
21
21
|
*/
|
|
22
|
-
export function createOpenAiChatGptInstructionsMiddleware(): LanguageModelV4Middleware {
|
|
22
|
+
export function createOpenAiChatGptInstructionsMiddleware(): plugins.LanguageModelV4Middleware {
|
|
23
23
|
return {
|
|
24
24
|
specificationVersion: 'v4',
|
|
25
25
|
transformParams: async ({ params }) => {
|
|
@@ -33,7 +33,7 @@ export function createOpenAiChatGptInstructionsMiddleware(): LanguageModelV4Midd
|
|
|
33
33
|
return params;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
const nextOpenAiProviderOptions: JSONObject = { ...openAiProviderOptions };
|
|
36
|
+
const nextOpenAiProviderOptions: plugins.JSONObject = { ...openAiProviderOptions };
|
|
37
37
|
delete nextOpenAiProviderOptions.promptCacheRetention;
|
|
38
38
|
delete nextOpenAiProviderOptions.prompt_cache_retention;
|
|
39
39
|
if (shouldApplyInstructions) {
|
|
@@ -49,7 +49,7 @@ export function createOpenAiChatGptInstructionsMiddleware(): LanguageModelV4Midd
|
|
|
49
49
|
...providerOptions,
|
|
50
50
|
openai: nextOpenAiProviderOptions,
|
|
51
51
|
},
|
|
52
|
-
} satisfies LanguageModelV4CallOptions;
|
|
52
|
+
} satisfies plugins.LanguageModelV4CallOptions;
|
|
53
53
|
},
|
|
54
54
|
};
|
|
55
55
|
}
|