@bastani/pi-ai 0.9.28-alpha.5 → 0.9.29-alpha.1
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/CHANGELOG.md +52 -0
- package/README.md +29 -2
- package/dist/api/openai-codex-responses.d.ts.map +1 -1
- package/dist/api/openai-codex-responses.js +182 -146
- package/dist/api/openai-codex-responses.js.map +1 -1
- package/dist/api/openai-images.d.ts +17 -0
- package/dist/api/openai-images.d.ts.map +1 -0
- package/dist/api/openai-images.js +114 -0
- package/dist/api/openai-images.js.map +1 -0
- package/dist/api/openai-images.lazy.d.ts +3 -0
- package/dist/api/openai-images.lazy.d.ts.map +1 -0
- package/dist/api/openai-images.lazy.js +4 -0
- package/dist/api/openai-images.lazy.js.map +1 -0
- package/dist/api/openai-responses-shared.d.ts +26 -0
- package/dist/api/openai-responses-shared.d.ts.map +1 -1
- package/dist/api/openai-responses-shared.js +81 -0
- package/dist/api/openai-responses-shared.js.map +1 -1
- package/dist/api/openai-responses.d.ts.map +1 -1
- package/dist/api/openai-responses.js +102 -10
- package/dist/api/openai-responses.js.map +1 -1
- package/dist/env-api-keys.d.ts.map +1 -1
- package/dist/env-api-keys.js +1 -0
- package/dist/env-api-keys.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/models.d.ts +4 -2
- package/dist/models.d.ts.map +1 -1
- package/dist/models.generated.d.ts +4 -0
- package/dist/models.generated.d.ts.map +1 -1
- package/dist/models.generated.js +4 -0
- package/dist/models.generated.js.map +1 -1
- package/dist/models.js +2 -1
- package/dist/models.js.map +1 -1
- package/dist/providers/all.d.ts.map +1 -1
- package/dist/providers/all.js +2 -0
- package/dist/providers/all.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/baseten.json +1 -1
- package/dist/providers/data/cloudflare-ai-gateway.json +1 -1
- package/dist/providers/data/cloudflare-workers-ai.json +1 -1
- package/dist/providers/data/google-vertex.json +1 -1
- package/dist/providers/data/mistral.json +1 -1
- package/dist/providers/data/openai-codex.json +1 -1
- package/dist/providers/data/openai-images.json +1 -0
- package/dist/providers/data/openai.json +1 -1
- package/dist/providers/data/opencode-go.json +1 -1
- package/dist/providers/data/opencode.json +1 -1
- package/dist/providers/data/openrouter.json +1 -1
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/providers/images/register-builtins.d.ts +1 -0
- package/dist/providers/images/register-builtins.d.ts.map +1 -1
- package/dist/providers/images/register-builtins.js +18 -0
- package/dist/providers/images/register-builtins.js.map +1 -1
- package/dist/providers/openai-images.d.ts +3 -0
- package/dist/providers/openai-images.d.ts.map +1 -0
- package/dist/providers/openai-images.js +19 -0
- package/dist/providers/openai-images.js.map +1 -0
- package/dist/providers/openai-images.models.d.ts +5 -0
- package/dist/providers/openai-images.models.d.ts.map +1 -0
- package/dist/providers/openai-images.models.js +8 -0
- package/dist/providers/openai-images.models.js.map +1 -0
- package/dist/types.d.ts +16 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,58 @@ This package is a Bastani fork of `@earendil-works/pi-ai`. Upstream history at t
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
## [0.9.29-alpha.1] - 2026-10-08
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- Added the `ultrafast` service tier to `gpt-6.1-sol` and `gpt-5.6-sol` on `openai` and `openai-api`, and to `gpt-6.1-sol` on `openai-codex`. GPT-6.1 Sol uses OpenAI's published Ultrafast rates ($12 input, $60 output per million tokens), with cache rates derived at the same six-times-Standard ratio; GPT-5.6 Sol Ultrafast is a preview without published rates and is priced at six times its Standard rates ([#3529](https://github.com/bastani-inc/atomic/issues/3529)).
|
|
12
|
+
- Added the `openai-images` provider (`openaiImagesProvider()`) and the `openai-images` image API for OpenAI's Images API, with `gpt-image-2.5-sunburst`, `gpt-image-2.5-flare`, `gpt-image-2`, `gpt-image-1.5`, `gpt-image-1`, and `gpt-image-1-mini`. Text input becomes the prompt; image inputs send the request to `/images/edits`. `OpenAIImagesOptions` adds `size`, `quality`, `n`, and `background`, and `Models.generateImages()` options are now typed per image API through `ImageApiOptionsMap`. Like `openai-decisions`, the provider has no login of its own: it reuses the API key stored for `openai-api`, then the one stored for `openai`, then `OPENAI_API_KEY`, and skips a ChatGPT sign-in on `openai` ([#3527](https://github.com/bastani-inc/atomic/issues/3527)).
|
|
13
|
+
|
|
14
|
+
### Changed
|
|
15
|
+
|
|
16
|
+
- OpenAI Responses and OpenAI Codex Responses requests now retry once at the default tier when the server rejects the requested `service_tier` before producing output, and record a `service_tier_unavailable` warning diagnostic on the assistant message once the default-tier request is accepted. On OpenAI Responses with an API key, a response that reports a slower tier than requested records the same warning. Requests made with a ChatGPT sign-in token, on `openai` or `openai-codex`, do not warn on a reported `default` and are priced at the requested tier, because the ChatGPT backend reports `default` for every tier it applies. Otherwise pricing follows the tier the server reports ([#3529](https://github.com/bastani-inc/atomic/issues/3529)).
|
|
17
|
+
|
|
18
|
+
## [0.9.28] - 2026-10-08
|
|
19
|
+
|
|
20
|
+
### Breaking Changes
|
|
21
|
+
|
|
22
|
+
- Stream functions must return `AssistantMessageEventStream`, for example from `createAssistantMessageEventStream()`. Plain `EventStream` subclasses are no longer assignable to this response stream type.
|
|
23
|
+
|
|
24
|
+
### Added
|
|
25
|
+
|
|
26
|
+
- Added `ApiKeyAuth.borrowCredentialsFrom` and `sharedApiKeyAuth()`, which let a provider without a login of its own reuse the stored API key of the first listed provider that has one, then fall back to its environment variables. A borrowing provider has no stored credential of its own, and stored OAuth credentials of the listed providers are skipped.
|
|
27
|
+
- Added `openaiApiProvider()` (`openai-api`) and `anthropicApiProvider()` (`anthropic-api`): API-key-only twins of `openai` and `anthropic` that list the same chat models under their own provider ID, so a subscription login on the source provider no longer displaces the API key.
|
|
28
|
+
- Added Claude Haiku 5.5 with adaptive thinking, `xhigh`/`max` effort, prompt-length pricing tiers, and mid-conversation effort, system-message, and tool changes. Bedrock supports adaptive thinking, native `xhigh`, thinking block binding, and prompt caching for Haiku 5.5.
|
|
29
|
+
- Added optional `durationMs` to assistant messages and tool results. Assistant response streams measure elapsed time with a monotonic clock, preserving existing measurements and leaving deferred results untimed.
|
|
30
|
+
- Added `LoginOptions.agentName` for apps to identify themselves during OpenAI ChatGPT and Codex browser login.
|
|
31
|
+
- Added OpenAI's Decisions classifier API and the `openai/gpt-6-luna` classifier, available with an API key but not Sign in with ChatGPT credentials.
|
|
32
|
+
- Added optional `images` to classifier contexts. Unsupported models and classifier APIs return an error result instead of ignoring images.
|
|
33
|
+
- Added Azure Foundry Chat Completions and `azure/deepseek-v4-pro`, with Azure endpoint resolution, deployment-name mapping, and supported reasoning efforts.
|
|
34
|
+
|
|
35
|
+
### Changed
|
|
36
|
+
|
|
37
|
+
- `openai-decisions` no longer has a login or stored credential of its own. It reuses the API key stored for `openai-api`, then the one stored for `openai`, then `OPENAI_API_KEY`; a ChatGPT sign-in on `openai` is skipped.
|
|
38
|
+
- Moved the GPT-6 Luna Decisions classifier to the `openai-decisions` provider (`openaiDecisionsProvider()`), with its own API-key credential. The `openai` provider no longer lists classifiers.
|
|
39
|
+
- The faux provider compares prompt-cache prefixes message by message without changing simulated usage counts.
|
|
40
|
+
- Renamed the Azure provider to `azure`, retaining the `azure-openai-responses` API ID and compatibility with the former provider name and exports.
|
|
41
|
+
|
|
42
|
+
### Fixed
|
|
43
|
+
|
|
44
|
+
- Fixed Kimi K3 cost estimates on Moonshot providers to use official pricing without cache-write charges.
|
|
45
|
+
- Fixed Magistral models retaining Mistral's `prompt_mode` reasoning controls when catalog metadata advertises effort levels.
|
|
46
|
+
- Fixed failed lazy API setup messages using the failure time instead of the request start as their timestamp.
|
|
47
|
+
- Fixed prompt-length pricing tiers being lost when generating model catalogs from AI Gateway, OpenRouter, and models.dev.
|
|
48
|
+
- Corrected the Claude Sonnet 5.5 fallback catalog's cache-read rate to the official $0.10 per million tokens.
|
|
49
|
+
- Fixed Bedrock Claude Haiku 5.5 requests sending unsupported caller temperature settings.
|
|
50
|
+
- Reduced context-limit request failures by estimating new input at 3.5 characters per token when calculating output limits.
|
|
51
|
+
- Fixed transient `server_busy`, `servers are currently busy`, and Mistral `finish_reason: "error"` responses ending the turn instead of being retried.
|
|
52
|
+
- Fixed Anthropic browser login when port 53692 is reserved or in use by falling back to a free loopback port. Copy-code login remains available for headless use.
|
|
53
|
+
- Fixed Codex requests ignoring caller and model overrides for `originator` and `User-Agent` headers; authentication headers remain authoritative.
|
|
54
|
+
- Fixed the selected reasoning effort not reaching OpenAI GPT models on Bedrock Converse. GPT-oss effort is clamped to `low`, `medium`, or `high`; Astra keeps its existing payload.
|
|
55
|
+
- Fixed Radius models disabled by an organization still appearing in the catalog. Fetched or cached gateway catalogs now replace the bundled defaults, including empty catalogs.
|
|
56
|
+
- Fixed OpenAI Decisions accepting unknown choices or out-of-range probabilities, confidence, and scores as successful answers, and failing to return error results when request hooks or fetch throw null or undefined.
|
|
57
|
+
- Retry transient HTTP/2 errors containing "pending stream has been canceled".
|
|
58
|
+
|
|
7
59
|
## [0.9.28-alpha.5] - 2026-10-08
|
|
8
60
|
|
|
9
61
|
### Added
|
package/README.md
CHANGED
|
@@ -404,7 +404,7 @@ if (modelAuth) {
|
|
|
404
404
|
|
|
405
405
|
Both overloads resolve credentials, refresh expired OAuth when necessary, and may return an auth-derived `apiKey`, `headers`, or `baseUrl`. `getAuth()` resolves `undefined` for unconfigured providers and rejects with `ModelsError` when something is actually broken (`"oauth"`: token refresh failed, credential preserved for re-login; `"auth"`: key resolution or credential store failure). Request paths surface the same failures as stream errors.
|
|
406
406
|
|
|
407
|
-
A provider can reuse other providers' stored API keys instead of offering its own login. List their IDs in `ApiKeyAuth.borrowCredentialsFrom`, or build the auth with `sharedApiKeyAuth(name, envVars, providerIds)`. The provider then has no stored credential of its own: the first listed provider with a stored API key supplies it, in the order given. Stored OAuth credentials are skipped, and the environment variables apply when none qualifies. `openai-decisions`
|
|
407
|
+
A provider can reuse other providers' stored API keys instead of offering its own login. List their IDs in `ApiKeyAuth.borrowCredentialsFrom`, or build the auth with `sharedApiKeyAuth(name, envVars, providerIds)`. The provider then has no stored credential of its own: the first listed provider with a stored API key supplies it, in the order given. Stored OAuth credentials are skipped, and the environment variables apply when none qualifies. `openai-decisions` and `openai-images` use this with `['openai-api', 'openai']`.
|
|
408
408
|
|
|
409
409
|
`getAuth()`, `checkAuth()`, `getAvailable()`, login, and logout accept optional caller cancellation through their existing options or interaction objects and remain unbounded when no signal is supplied. Provider `login`, `ApiKeyAuth.check`, `ApiKeyAuth.resolve`, and `OAuthAuth.refresh` implementations always receive a concrete signal and must honor it for blocking work.
|
|
410
410
|
|
|
@@ -828,7 +828,7 @@ const pixels = createProvider({
|
|
|
828
828
|
models.setProvider(pixels);
|
|
829
829
|
```
|
|
830
830
|
|
|
831
|
-
The old global API (`getImageModel()` / `getImageModels()` / `getImageProviders()` / `generateImages()`) remains available on the [compat entrypoint](#migrating-from-the-old-global-api):
|
|
831
|
+
The old global API (`getImageModel()` / `getImageModels()` / `getImageProviders()` / `generateImages()`) remains available on the [compat entrypoint](#migrating-from-the-old-global-api). Its `generateImages()` dispatches on `model.api` and takes `apiKey` explicitly:
|
|
832
832
|
|
|
833
833
|
```typescript
|
|
834
834
|
import { getImageModel, generateImages } from '@bastani/pi-ai/compat';
|
|
@@ -841,6 +841,18 @@ const result = await generateImages(model, {
|
|
|
841
841
|
});
|
|
842
842
|
```
|
|
843
843
|
|
|
844
|
+
`registerImagesApiProvider({ api, generateImages })` from the compat entrypoint is the extension point for image backends that are not built in. It registers an implementation for an image API ID, and the global `generateImages()` then dispatches models with that `api` to it:
|
|
845
|
+
|
|
846
|
+
```typescript
|
|
847
|
+
import { generateImages, registerImagesApiProvider } from '@bastani/pi-ai/compat';
|
|
848
|
+
|
|
849
|
+
registerImagesApiProvider({
|
|
850
|
+
api: 'pixels-images',
|
|
851
|
+
generateImages: async (model, context, options) => { /* call the backend with options?.apiKey */ },
|
|
852
|
+
});
|
|
853
|
+
const result = await generateImages(pixelsModel, context, { apiKey: process.env.PIXELS_API_KEY });
|
|
854
|
+
```
|
|
855
|
+
|
|
844
856
|
Some models also support image input:
|
|
845
857
|
|
|
846
858
|
```typescript
|
|
@@ -862,6 +874,21 @@ console.log(model.input); // ['text'] or ['text', 'image']
|
|
|
862
874
|
console.log(model.output); // ['image'] or ['image', 'text']
|
|
863
875
|
```
|
|
864
876
|
|
|
877
|
+
### OpenAI Images API
|
|
878
|
+
|
|
879
|
+
The `openai-images` provider serves OpenAI's GPT Image models (`gpt-image-2.5-sunburst`, `gpt-image-2.5-flare`, `gpt-image-2`, `gpt-image-1.5`, `gpt-image-1`, `gpt-image-1-mini`) through the Images API. It borrows the API key stored for `openai-api`, then the one stored for `openai`, then `OPENAI_API_KEY`; a ChatGPT OAuth credential on `openai` is skipped, so with only that credential the models are unavailable and `generateImages()` returns `Provider is not configured: openai-images`. Text blocks become the `prompt`. Without image blocks the request goes to `/images/generations`; with them it goes to `/images/edits`, uploading each image block. `OpenAIImagesOptions` adds `size`, `quality`, `n`, and `background`, each sent only when set:
|
|
880
|
+
|
|
881
|
+
```typescript
|
|
882
|
+
import { getBuiltinImageModel } from '@bastani/pi-ai/providers/all';
|
|
883
|
+
|
|
884
|
+
const flare = getBuiltinImageModel('openai-images', 'gpt-image-2.5-flare');
|
|
885
|
+
const result = await models.generateImages(flare, {
|
|
886
|
+
input: [{ type: 'text', text: 'A red fox in the snow, watercolor' }]
|
|
887
|
+
}, { size: '1024x1024', quality: 'low' });
|
|
888
|
+
```
|
|
889
|
+
|
|
890
|
+
Each returned image is an `ImageContent` block whose `mimeType` follows the response's `output_format`.
|
|
891
|
+
|
|
865
892
|
### Notes and Limitations
|
|
866
893
|
|
|
867
894
|
- Image models and chat models share `Models` and `Provider`; list them with `getModelsOfType('image')` and run them with `generateImages()`, never the chat/stream APIs.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-codex-responses.d.ts","sourceRoot":"","sources":["../../src/api/openai-codex-responses.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAMX,mBAAmB,EACnB,cAAc,EACd,aAAa,EAGb,MAAM,aAAa,CAAC;AAwBrB,OAAO,
|
|
1
|
+
{"version":3,"file":"openai-codex-responses.d.ts","sourceRoot":"","sources":["../../src/api/openai-codex-responses.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAMX,mBAAmB,EACnB,cAAc,EACd,aAAa,EAGb,MAAM,aAAa,CAAC;AAwBrB,OAAO,EASN,KAAK,oBAAoB,EAGzB,MAAM,8BAA8B,CAAC;AAkCtC,MAAM,WAAW,2BAA4B,SAAQ,aAAa;IACjE,eAAe,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,CAAC;IACnF,gBAAgB,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,UAAU,GAAG,KAAK,GAAG,IAAI,GAAG,IAAI,CAAC;IACzE,WAAW,CAAC,EAAE,oBAAoB,CAAC;IACnC,aAAa,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;IAC1C,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,UAAU,CAAC;CAC1C;AAwJD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,wBAAwB,EAAE,2BAA2B,CA8TxF,CAAC;AAEF,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,wBAAwB,EAAE,mBAAmB,CAqBtF,CAAC;AAqXF,MAAM,WAAW,8BAA8B;IAC9C,QAAQ,EAAE,MAAM,CAAC;IACjB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,iBAAiB,EAAE,MAAM,CAAC;IAC1B,mBAAmB,EAAE,MAAM,CAAC;IAC5B,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,MAAM,CAAC;IACvB,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,iBAAiB,EAAE,MAAM,CAAC;IAC1B,YAAY,EAAE,MAAM,CAAC;IACrB,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC5B;AA0BD,wBAAgB,iCAAiC,CAAC,SAAS,EAAE,MAAM,GAAG,8BAA8B,GAAG,SAAS,CAG/G;AAED,wBAAgB,mCAAmC,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAQ5E;AAED,wBAAgB,iCAAiC,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAc1E"}
|
|
@@ -13,7 +13,7 @@ import { getDeclaredTools, getInitialSystemMessage, normalizeContext, resolveTra
|
|
|
13
13
|
import { uuidv7 } from "../utils/uuid.js";
|
|
14
14
|
import { createGrammarToolInputProperties } from "./constrained-sampling.js";
|
|
15
15
|
import { clampOpenAIPromptCacheKey } from "./openai-prompt-cache.js";
|
|
16
|
-
import { applyServiceTierPricing, assertPayloadPreservesFastRoute, codexServiceTierForRequest, convertResponsesMessages, convertResponsesTools, processResponsesStream, resolveRequestedServiceTier, } from "./openai-responses-shared.js";
|
|
16
|
+
import { appendServiceTierRejectedWarning, applyServiceTierPricing, assertPayloadPreservesFastRoute, codexServiceTierForRequest, convertResponsesMessages, convertResponsesTools, isServiceTierRejection, processResponsesStream, resolveRequestedServiceTier, resolveChatGPTBackendServiceTier, } from "./openai-responses-shared.js";
|
|
17
17
|
import { buildBaseOptions } from "./simple-options.js";
|
|
18
18
|
// ============================================================================
|
|
19
19
|
// Configuration
|
|
@@ -181,161 +181,184 @@ export const stream = (model, context, options) => {
|
|
|
181
181
|
const websocketRequestId = codexSessionId || uuidv7();
|
|
182
182
|
const sseHeaders = buildSSEHeaders(model.headers, options?.headers, accountId, apiKey, codexSessionId);
|
|
183
183
|
const websocketHeaders = buildWebSocketHeaders(model.headers, options?.headers, accountId, apiKey, websocketRequestId);
|
|
184
|
-
const bodyJson = JSON.stringify(body);
|
|
185
184
|
const httpTimeoutMs = normalizeTimeoutMs(options?.timeoutMs);
|
|
186
185
|
const websocketConnectTimeoutMs = normalizeTimeoutMs(options?.websocketConnectTimeoutMs);
|
|
187
186
|
const transport = options?.transport || "auto";
|
|
188
187
|
let startEmitted = false;
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
188
|
+
const emitStart = () => {
|
|
189
|
+
if (!startEmitted) {
|
|
190
|
+
startEmitted = true;
|
|
191
|
+
stream.push({ type: "start", partial: output });
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
// One request over WebSocket (falling back to SSE), processed through to a successful stop reason.
|
|
195
|
+
const runRequest = async (requestBody, requestSseHeaders, requestWebsocketHeaders, requestServiceTier) => {
|
|
196
|
+
const bodyJson = JSON.stringify(requestBody);
|
|
197
|
+
const websocketDisabledForSession = transport !== "sse" && isWebSocketSseFallbackActive(cacheSessionId);
|
|
198
|
+
if (websocketDisabledForSession) {
|
|
199
|
+
recordWebSocketSseFallback(cacheSessionId);
|
|
200
|
+
}
|
|
201
|
+
if (transport !== "sse" && !websocketDisabledForSession) {
|
|
202
|
+
let websocketStarted = false;
|
|
203
|
+
let retriedWebSocketConnectionLimit = false;
|
|
204
|
+
let retriedMissingWebSocketContinuation = false;
|
|
205
|
+
while (true) {
|
|
206
|
+
websocketStarted = false;
|
|
207
|
+
try {
|
|
208
|
+
await processWebSocketStream(resolveCodexWebSocketUrl(model.baseUrl), requestBody, requestWebsocketHeaders, output, stream, model, () => {
|
|
209
|
+
websocketStarted = true;
|
|
210
|
+
emitStart();
|
|
211
|
+
}, streamDeadline.deadlineMs, websocketConnectTimeoutMs, cacheSessionId, accountId, grammarToolInputProperties, requestServiceTier, options);
|
|
212
|
+
if (options?.signal?.aborted) {
|
|
213
|
+
throw new Error("Request was aborted");
|
|
214
|
+
}
|
|
215
|
+
assertSuccessfulOutput(output);
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
const aborted = options?.signal?.aborted;
|
|
220
|
+
const connectionLimitBeforeStart = !websocketStarted && isWebSocketConnectionLimitReachedError(error);
|
|
221
|
+
const previousResponseNotFound = isPreviousResponseNotFoundError(error);
|
|
222
|
+
if (!aborted && previousResponseNotFound && !retriedMissingWebSocketContinuation) {
|
|
223
|
+
retriedMissingWebSocketContinuation = true;
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
if (!aborted && connectionLimitBeforeStart && !retriedWebSocketConnectionLimit) {
|
|
227
|
+
retriedWebSocketConnectionLimit = true;
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (aborted || (isCodexNonTransportError(error) && !connectionLimitBeforeStart)) {
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
233
|
+
appendAssistantMessageDiagnostic(output, createAssistantMessageDiagnostic("provider_transport_failure", error, {
|
|
234
|
+
configuredTransport: transport,
|
|
235
|
+
...(websocketStarted ? {} : { fallbackTransport: "sse" }),
|
|
236
|
+
eventsEmitted: websocketStarted,
|
|
237
|
+
phase: websocketStarted ? "after_message_stream_start" : "before_message_stream_start",
|
|
238
|
+
requestBytes: new TextEncoder().encode(bodyJson).byteLength,
|
|
239
|
+
}));
|
|
240
|
+
recordWebSocketFailure(cacheSessionId, error);
|
|
241
|
+
if (websocketStarted) {
|
|
242
|
+
throw error;
|
|
243
|
+
}
|
|
244
|
+
recordWebSocketSseFallback(cacheSessionId);
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
// Compress the request body once for the SSE path. The Codex backend
|
|
250
|
+
// decodes Content-Encoding: zstd; the WebSocket transport above sends the
|
|
251
|
+
// uncompressed JSON frame, matching the official Codex client.
|
|
252
|
+
const compressedBody = compressRequestBodyZstd(bodyJson);
|
|
253
|
+
if (compressedBody) {
|
|
254
|
+
requestSseHeaders.set("content-encoding", "zstd");
|
|
255
|
+
}
|
|
256
|
+
const sseBody = compressedBody ?? bodyJson;
|
|
257
|
+
// Fetch with retry logic for rate limits and transient errors
|
|
258
|
+
let response;
|
|
259
|
+
let lastError;
|
|
260
|
+
const maxRetries = options?.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
261
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
262
|
+
if (options?.signal?.aborted) {
|
|
263
|
+
throw new Error("Request was aborted");
|
|
264
|
+
}
|
|
199
265
|
try {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
266
|
+
const headerTimeoutSignal = httpTimeoutMs !== undefined && httpTimeoutMs > 0 ? AbortSignal.timeout(httpTimeoutMs) : undefined;
|
|
267
|
+
const combinedSignal = combineAbortSignals([streamDeadline.signal, headerTimeoutSignal]);
|
|
268
|
+
try {
|
|
269
|
+
response = await (options?.fetch ?? globalThis.fetch)(resolveCodexUrl(model.baseUrl), {
|
|
270
|
+
method: "POST",
|
|
271
|
+
headers: requestSseHeaders,
|
|
272
|
+
body: sseBody,
|
|
273
|
+
signal: combinedSignal.signal,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
catch (error) {
|
|
277
|
+
if (headerTimeoutSignal?.aborted && !options?.signal?.aborted) {
|
|
278
|
+
throw new Error(`Codex SSE response headers timed out after ${httpTimeoutMs}ms`);
|
|
205
279
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
280
|
+
throw error;
|
|
281
|
+
}
|
|
282
|
+
finally {
|
|
283
|
+
combinedSignal.cleanup();
|
|
284
|
+
}
|
|
285
|
+
await options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);
|
|
286
|
+
if (response.ok) {
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
const errorText = await response.text();
|
|
290
|
+
if (attempt < maxRetries && isRetryableError(response.status, errorText)) {
|
|
291
|
+
const retryAfterDelayMs = getRetryAfterDelayMs(response.headers);
|
|
292
|
+
const delayMs = retryAfterDelayMs === undefined
|
|
293
|
+
? BASE_DELAY_MS * 2 ** attempt
|
|
294
|
+
: validateRetryDelayMs(retryAfterDelayMs, options);
|
|
295
|
+
await sleep(delayMs, options?.signal);
|
|
296
|
+
continue;
|
|
209
297
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
message: output,
|
|
298
|
+
// Parse error for friendly message on final attempt or non-retryable error
|
|
299
|
+
const fakeResponse = new Response(errorText, {
|
|
300
|
+
status: response.status,
|
|
301
|
+
statusText: response.statusText,
|
|
215
302
|
});
|
|
216
|
-
|
|
217
|
-
|
|
303
|
+
const info = await parseErrorResponse(fakeResponse);
|
|
304
|
+
throw new Error(info.friendlyMessage || info.message);
|
|
218
305
|
}
|
|
219
306
|
catch (error) {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
retriedMissingWebSocketContinuation = true;
|
|
225
|
-
continue;
|
|
307
|
+
if (error instanceof Error) {
|
|
308
|
+
if (error.name === "AbortError" || error.message === "Request was aborted") {
|
|
309
|
+
throw new Error("Request was aborted");
|
|
310
|
+
}
|
|
226
311
|
}
|
|
227
|
-
|
|
228
|
-
|
|
312
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
313
|
+
// Network errors are retryable
|
|
314
|
+
if (attempt < maxRetries &&
|
|
315
|
+
!(lastError instanceof RetryDelayExceededError) &&
|
|
316
|
+
!lastError.message.includes("usage limit")) {
|
|
317
|
+
const delayMs = BASE_DELAY_MS * 2 ** attempt;
|
|
318
|
+
await sleep(delayMs, options?.signal);
|
|
229
319
|
continue;
|
|
230
320
|
}
|
|
231
|
-
|
|
232
|
-
throw error;
|
|
233
|
-
}
|
|
234
|
-
appendAssistantMessageDiagnostic(output, createAssistantMessageDiagnostic("provider_transport_failure", error, {
|
|
235
|
-
configuredTransport: transport,
|
|
236
|
-
...(websocketStarted ? {} : { fallbackTransport: "sse" }),
|
|
237
|
-
eventsEmitted: websocketStarted,
|
|
238
|
-
phase: websocketStarted ? "after_message_stream_start" : "before_message_stream_start",
|
|
239
|
-
requestBytes: new TextEncoder().encode(bodyJson).byteLength,
|
|
240
|
-
}));
|
|
241
|
-
recordWebSocketFailure(cacheSessionId, error);
|
|
242
|
-
if (websocketStarted) {
|
|
243
|
-
throw error;
|
|
244
|
-
}
|
|
245
|
-
recordWebSocketSseFallback(cacheSessionId);
|
|
246
|
-
break;
|
|
321
|
+
throw lastError;
|
|
247
322
|
}
|
|
248
323
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
const sseBody = compressedBody ?? bodyJson;
|
|
258
|
-
// Fetch with retry logic for rate limits and transient errors
|
|
259
|
-
let response;
|
|
260
|
-
let lastError;
|
|
261
|
-
const maxRetries = options?.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
262
|
-
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
324
|
+
if (!response?.ok) {
|
|
325
|
+
throw lastError ?? new Error("Failed after retries");
|
|
326
|
+
}
|
|
327
|
+
if (!response.body) {
|
|
328
|
+
throw new Error("No response body");
|
|
329
|
+
}
|
|
330
|
+
emitStart();
|
|
331
|
+
await processStream(response, output, stream, model, grammarToolInputProperties, requestServiceTier, options, streamDeadline);
|
|
263
332
|
if (options?.signal?.aborted) {
|
|
264
333
|
throw new Error("Request was aborted");
|
|
265
334
|
}
|
|
335
|
+
assertSuccessfulOutput(output);
|
|
336
|
+
};
|
|
337
|
+
try {
|
|
338
|
+
await runRequest(body, sseHeaders, websocketHeaders, resolveCodexRequestServiceTier(model, options?.serviceTier));
|
|
339
|
+
}
|
|
340
|
+
catch (error) {
|
|
341
|
+
const rejectedServiceTier = body.service_tier;
|
|
342
|
+
if (rejectedServiceTier == null ||
|
|
343
|
+
options?.signal?.aborted ||
|
|
344
|
+
output.content.length > 0 ||
|
|
345
|
+
!isCodexServiceTierRejection(error)) {
|
|
346
|
+
throw error;
|
|
347
|
+
}
|
|
348
|
+
// The backend refused the tier before producing output: retry once at the default tier, with no
|
|
349
|
+
// service_tier and no tier routing hint.
|
|
350
|
+
const { service_tier: _rejectedServiceTier, ...defaultTierBody } = body;
|
|
351
|
+
let ranAtDefault = false;
|
|
266
352
|
try {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
try {
|
|
270
|
-
response = await (options?.fetch ?? globalThis.fetch)(resolveCodexUrl(model.baseUrl), {
|
|
271
|
-
method: "POST",
|
|
272
|
-
headers: sseHeaders,
|
|
273
|
-
body: sseBody,
|
|
274
|
-
signal: combinedSignal.signal,
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
catch (error) {
|
|
278
|
-
if (headerTimeoutSignal?.aborted && !options?.signal?.aborted) {
|
|
279
|
-
throw new Error(`Codex SSE response headers timed out after ${httpTimeoutMs}ms`);
|
|
280
|
-
}
|
|
281
|
-
throw error;
|
|
282
|
-
}
|
|
283
|
-
finally {
|
|
284
|
-
combinedSignal.cleanup();
|
|
285
|
-
}
|
|
286
|
-
await options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);
|
|
287
|
-
if (response.ok) {
|
|
288
|
-
break;
|
|
289
|
-
}
|
|
290
|
-
const errorText = await response.text();
|
|
291
|
-
if (attempt < maxRetries && isRetryableError(response.status, errorText)) {
|
|
292
|
-
const retryAfterDelayMs = getRetryAfterDelayMs(response.headers);
|
|
293
|
-
const delayMs = retryAfterDelayMs === undefined
|
|
294
|
-
? BASE_DELAY_MS * 2 ** attempt
|
|
295
|
-
: validateRetryDelayMs(retryAfterDelayMs, options);
|
|
296
|
-
await sleep(delayMs, options?.signal);
|
|
297
|
-
continue;
|
|
298
|
-
}
|
|
299
|
-
// Parse error for friendly message on final attempt or non-retryable error
|
|
300
|
-
const fakeResponse = new Response(errorText, {
|
|
301
|
-
status: response.status,
|
|
302
|
-
statusText: response.statusText,
|
|
303
|
-
});
|
|
304
|
-
const info = await parseErrorResponse(fakeResponse);
|
|
305
|
-
throw new Error(info.friendlyMessage || info.message);
|
|
353
|
+
await runRequest(defaultTierBody, withoutCodexRoutingHint(sseHeaders), withoutCodexRoutingHint(websocketHeaders), undefined);
|
|
354
|
+
ranAtDefault = true;
|
|
306
355
|
}
|
|
307
|
-
|
|
308
|
-
if (
|
|
309
|
-
|
|
310
|
-
throw new Error("Request was aborted");
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
lastError = error instanceof Error ? error : new Error(String(error));
|
|
314
|
-
// Network errors are retryable
|
|
315
|
-
if (attempt < maxRetries &&
|
|
316
|
-
!(lastError instanceof RetryDelayExceededError) &&
|
|
317
|
-
!lastError.message.includes("usage limit")) {
|
|
318
|
-
const delayMs = BASE_DELAY_MS * 2 ** attempt;
|
|
319
|
-
await sleep(delayMs, options?.signal);
|
|
320
|
-
continue;
|
|
356
|
+
finally {
|
|
357
|
+
if (ranAtDefault || output.content.length > 0) {
|
|
358
|
+
appendServiceTierRejectedWarning(output, model, rejectedServiceTier);
|
|
321
359
|
}
|
|
322
|
-
throw lastError;
|
|
323
360
|
}
|
|
324
361
|
}
|
|
325
|
-
if (!response?.ok) {
|
|
326
|
-
throw lastError ?? new Error("Failed after retries");
|
|
327
|
-
}
|
|
328
|
-
if (!response.body) {
|
|
329
|
-
throw new Error("No response body");
|
|
330
|
-
}
|
|
331
|
-
if (!startEmitted) {
|
|
332
|
-
startEmitted = true;
|
|
333
|
-
stream.push({ type: "start", partial: output });
|
|
334
|
-
}
|
|
335
|
-
await processStream(response, output, stream, model, grammarToolInputProperties, options, streamDeadline);
|
|
336
|
-
if (options?.signal?.aborted) {
|
|
337
|
-
throw new Error("Request was aborted");
|
|
338
|
-
}
|
|
339
362
|
assertSuccessfulOutput(output);
|
|
340
363
|
stream.push({ type: "done", reason: output.stopReason, message: output });
|
|
341
364
|
stream.end();
|
|
@@ -441,13 +464,6 @@ function buildRequestBody(model, context, options, cacheSessionId, grammarToolIn
|
|
|
441
464
|
function resolveCodexRequestServiceTier(model, optionsServiceTier) {
|
|
442
465
|
return codexServiceTierForRequest(model, resolveRequestedServiceTier(model, optionsServiceTier));
|
|
443
466
|
}
|
|
444
|
-
function resolveCodexServiceTier(responseServiceTier, requestServiceTier) {
|
|
445
|
-
if (responseServiceTier === "default" &&
|
|
446
|
-
(requestServiceTier === "flex" || requestServiceTier === "priority" || requestServiceTier === "ultrafast")) {
|
|
447
|
-
return requestServiceTier;
|
|
448
|
-
}
|
|
449
|
-
return responseServiceTier ?? requestServiceTier;
|
|
450
|
-
}
|
|
451
467
|
function resolveCodexUrl(baseUrl) {
|
|
452
468
|
const raw = baseUrl && baseUrl.trim().length > 0 ? baseUrl : DEFAULT_CODEX_BASE_URL;
|
|
453
469
|
const normalized = raw.replace(/\/+$/, "");
|
|
@@ -468,12 +484,12 @@ function resolveCodexWebSocketUrl(baseUrl) {
|
|
|
468
484
|
// ============================================================================
|
|
469
485
|
// Response Processing
|
|
470
486
|
// ============================================================================
|
|
471
|
-
async function processStream(response, output, stream, model, grammarToolInputProperties, options, streamDeadline) {
|
|
487
|
+
async function processStream(response, output, stream, model, grammarToolInputProperties, requestServiceTier, options, streamDeadline) {
|
|
472
488
|
const events = mapCodexEvents(parseSSE(response, streamDeadline.signal), output, model, options?.onProviderStreamEvent);
|
|
473
489
|
await processResponsesStream(withStreamDeadline(events, streamDeadline.deadlineMs, streamDeadline.abort), output, stream, model, {
|
|
474
|
-
serviceTier:
|
|
490
|
+
serviceTier: requestServiceTier,
|
|
475
491
|
grammarToolInputProperties,
|
|
476
|
-
resolveServiceTier:
|
|
492
|
+
resolveServiceTier: resolveChatGPTBackendServiceTier,
|
|
477
493
|
applyServiceTierPricing: (usage, serviceTier) => applyServiceTierPricing(usage, serviceTier, model),
|
|
478
494
|
});
|
|
479
495
|
}
|
|
@@ -515,6 +531,26 @@ function isWebSocketConnectionLimitReachedError(error) {
|
|
|
515
531
|
function isPreviousResponseNotFoundError(error) {
|
|
516
532
|
return error instanceof CodexApiError && error.code === PREVIOUS_RESPONSE_NOT_FOUND_CODE;
|
|
517
533
|
}
|
|
534
|
+
/**
|
|
535
|
+
* Whether the Codex backend refused the request's `service_tier`: an HTTP 400 or a stream `error` event whose
|
|
536
|
+
* message names the tier, or an error event with `param: "service_tier"`.
|
|
537
|
+
*/
|
|
538
|
+
function isCodexServiceTierRejection(error) {
|
|
539
|
+
if (isServiceTierRejection(error))
|
|
540
|
+
return true;
|
|
541
|
+
if (!(error instanceof CodexApiError) || error.payload === undefined)
|
|
542
|
+
return false;
|
|
543
|
+
const nested = error.payload.error;
|
|
544
|
+
const nestedParam = typeof nested === "object" && nested !== null ? nested.param : undefined;
|
|
545
|
+
return error.payload.param === "service_tier" || nestedParam === "service_tier";
|
|
546
|
+
}
|
|
547
|
+
/** Atomic's Codex fast routing names the requested tier in this header; a default-tier retry must not carry it. */
|
|
548
|
+
const CODEX_ROUTING_HINT_HEADER = "x-codex-routing-hint";
|
|
549
|
+
function withoutCodexRoutingHint(headers) {
|
|
550
|
+
const defaultTierHeaders = new Headers(headers);
|
|
551
|
+
defaultTierHeaders.delete(CODEX_ROUTING_HINT_HEADER);
|
|
552
|
+
return defaultTierHeaders;
|
|
553
|
+
}
|
|
518
554
|
function extractCodexEventError(event) {
|
|
519
555
|
const nested = event.error && typeof event.error === "object" ? event.error : undefined;
|
|
520
556
|
return {
|
|
@@ -1163,7 +1199,7 @@ async function* startWebSocketOutputOnFirstEvent(events, onStart) {
|
|
|
1163
1199
|
yield event;
|
|
1164
1200
|
}
|
|
1165
1201
|
}
|
|
1166
|
-
async function processWebSocketStream(url, body, headers, output, stream, model, onStart, idleTimeoutMs, websocketConnectTimeoutMs, cacheSessionId, accountId, grammarToolInputProperties, options) {
|
|
1202
|
+
async function processWebSocketStream(url, body, headers, output, stream, model, onStart, idleTimeoutMs, websocketConnectTimeoutMs, cacheSessionId, accountId, grammarToolInputProperties, requestServiceTier, options) {
|
|
1167
1203
|
const { socket, entry, reused, release } = await acquireWebSocket(url, headers, cacheSessionId, accountId, options?.signal, websocketConnectTimeoutMs, options?.env);
|
|
1168
1204
|
let keepConnection = true;
|
|
1169
1205
|
const useCachedContext = options?.transport === "websocket-cached" || options?.transport === "auto";
|
|
@@ -1197,9 +1233,9 @@ async function processWebSocketStream(url, body, headers, output, stream, model,
|
|
|
1197
1233
|
try {
|
|
1198
1234
|
socket.send(JSON.stringify({ type: "response.create", ...requestBody }));
|
|
1199
1235
|
await processResponsesStream(startWebSocketOutputOnFirstEvent(mapCodexEvents(parseWebSocket(socket, options?.signal, idleTimeoutMs), output, model, options?.onProviderStreamEvent), onStart), output, stream, model, {
|
|
1200
|
-
serviceTier:
|
|
1236
|
+
serviceTier: requestServiceTier,
|
|
1201
1237
|
grammarToolInputProperties,
|
|
1202
|
-
resolveServiceTier:
|
|
1238
|
+
resolveServiceTier: resolveChatGPTBackendServiceTier,
|
|
1203
1239
|
applyServiceTierPricing: (usage, serviceTier) => applyServiceTierPricing(usage, serviceTier, model),
|
|
1204
1240
|
});
|
|
1205
1241
|
if (options?.signal?.aborted) {
|