@namzu/openai 4.5.0 → 4.6.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/CHANGELOG.md +6 -0
- package/README.md +34 -0
- package/dist/client.d.ts +2 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +46 -7
- package/dist/client.js.map +1 -1
- package/dist/codex.d.ts +10 -0
- package/dist/codex.d.ts.map +1 -1
- package/dist/codex.js +88 -6
- package/dist/codex.js.map +1 -1
- package/dist/fast-mode.d.ts +93 -0
- package/dist/fast-mode.d.ts.map +1 -0
- package/dist/fast-mode.js +172 -0
- package/dist/fast-mode.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/client.ts +52 -7
- package/src/codex.ts +114 -11
- package/src/fast-mode.ts +250 -0
- package/src/index.ts +15 -0
package/src/codex.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AssistantTextPart,
|
|
3
3
|
ChatCompletionParams,
|
|
4
|
+
InferenceSpeed,
|
|
4
5
|
LLMProvider,
|
|
5
6
|
ModelInfo,
|
|
6
7
|
ProviderCapabilities,
|
|
@@ -31,6 +32,7 @@ import type {
|
|
|
31
32
|
Tool,
|
|
32
33
|
} from 'openai/resources/responses/responses'
|
|
33
34
|
|
|
35
|
+
import { type FastTier, assertFastModeAccepted, servedSpeed } from './fast-mode.js'
|
|
34
36
|
import type { CodexConfig } from './types.js'
|
|
35
37
|
|
|
36
38
|
export const CODEX_CAPABILITIES: ProviderCapabilities = {
|
|
@@ -336,12 +338,16 @@ export function toCodexTools(params: ChatCompletionParams): Tool[] | undefined {
|
|
|
336
338
|
return tools.length ? tools : undefined
|
|
337
339
|
}
|
|
338
340
|
|
|
339
|
-
function responseUsage(
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
341
|
+
function responseUsage(
|
|
342
|
+
usage: {
|
|
343
|
+
input_tokens?: number
|
|
344
|
+
output_tokens?: number
|
|
345
|
+
total_tokens?: number
|
|
346
|
+
input_tokens_details?: { cached_tokens?: number }
|
|
347
|
+
},
|
|
348
|
+
servedTier?: unknown,
|
|
349
|
+
requestedSpeed?: InferenceSpeed,
|
|
350
|
+
): TokenUsage {
|
|
345
351
|
const promptTokens = usage.input_tokens ?? 0
|
|
346
352
|
const completionTokens = usage.output_tokens ?? 0
|
|
347
353
|
return {
|
|
@@ -350,9 +356,32 @@ function responseUsage(usage: {
|
|
|
350
356
|
totalTokens: usage.total_tokens ?? promptTokens + completionTokens,
|
|
351
357
|
cachedTokens: usage.input_tokens_details?.cached_tokens ?? 0,
|
|
352
358
|
cacheWriteTokens: 0,
|
|
359
|
+
...servedSpeed(servedTier, requestedSpeed),
|
|
353
360
|
}
|
|
354
361
|
}
|
|
355
362
|
|
|
363
|
+
/**
|
|
364
|
+
* The speed tiers a model listing entry offers, from the current
|
|
365
|
+
* `service_tiers` objects and the older `additional_speed_tiers` strings.
|
|
366
|
+
* Only the two tiers this driver can request are kept: `priority` (or `fast`)
|
|
367
|
+
* is fast, `ultrafast` is ultrafast; anything else, flex included, is not a
|
|
368
|
+
* speed tier here.
|
|
369
|
+
*/
|
|
370
|
+
function subscriptionSpeedTiers(item: Record<string, unknown>): readonly FastTier[] {
|
|
371
|
+
const tiers = new Set<FastTier>()
|
|
372
|
+
const note = (id: unknown) => {
|
|
373
|
+
if (id === 'priority' || id === 'fast') tiers.add('fast')
|
|
374
|
+
else if (id === 'ultrafast') tiers.add('ultrafast')
|
|
375
|
+
}
|
|
376
|
+
if (Array.isArray(item.service_tiers)) {
|
|
377
|
+
for (const entry of item.service_tiers) note(record(entry)?.id)
|
|
378
|
+
}
|
|
379
|
+
if (Array.isArray(item.additional_speed_tiers)) {
|
|
380
|
+
for (const entry of item.additional_speed_tiers) note(entry)
|
|
381
|
+
}
|
|
382
|
+
return (['fast', 'ultrafast'] as const).filter((tier) => tiers.has(tier))
|
|
383
|
+
}
|
|
384
|
+
|
|
356
385
|
function buildRequest(
|
|
357
386
|
params: ChatCompletionParams,
|
|
358
387
|
model: string,
|
|
@@ -422,6 +451,19 @@ function buildRequest(
|
|
|
422
451
|
return request as ResponseCreateParamsStreaming
|
|
423
452
|
}
|
|
424
453
|
|
|
454
|
+
/**
|
|
455
|
+
* What an account's model listing said, kept for the life of the process so a
|
|
456
|
+
* provider rebuilt around a refreshed token still honours an advertised tier.
|
|
457
|
+
* Keyed by account and endpoint, never by token.
|
|
458
|
+
*/
|
|
459
|
+
const listingByAccount = new Map<
|
|
460
|
+
string,
|
|
461
|
+
{
|
|
462
|
+
speedTiers: Map<string, readonly FastTier[]>
|
|
463
|
+
reasoningProfiles: Map<string, SubscriptionReasoningProfile>
|
|
464
|
+
}
|
|
465
|
+
>()
|
|
466
|
+
|
|
425
467
|
export class CodexProvider implements LLMProvider {
|
|
426
468
|
readonly id = 'codex'
|
|
427
469
|
readonly name = 'OpenAI Codex'
|
|
@@ -430,6 +472,9 @@ export class CodexProvider implements LLMProvider {
|
|
|
430
472
|
private client: OpenAI
|
|
431
473
|
private defaultModel?: string
|
|
432
474
|
private reasoningProfiles = new Map<string, SubscriptionReasoningProfile>()
|
|
475
|
+
/** Per model, the speed tiers the account's listing offers. Absent until a listing has answered. */
|
|
476
|
+
private speedTiers?: Map<string, readonly FastTier[]>
|
|
477
|
+
private readonly listingKey: string
|
|
433
478
|
|
|
434
479
|
constructor(config: CodexConfig) {
|
|
435
480
|
if (!config.accessToken) throw new Error('Codex access token is required.')
|
|
@@ -448,6 +493,12 @@ export class CodexProvider implements LLMProvider {
|
|
|
448
493
|
},
|
|
449
494
|
})
|
|
450
495
|
this.defaultModel = config.model
|
|
496
|
+
this.listingKey = `${config.accountId}\u0000${config.baseURL ?? DEFAULT_CODEX_BASE_URL}`
|
|
497
|
+
const known = listingByAccount.get(this.listingKey)
|
|
498
|
+
if (known) {
|
|
499
|
+
this.speedTiers = known.speedTiers
|
|
500
|
+
this.reasoningProfiles = known.reasoningProfiles
|
|
501
|
+
}
|
|
451
502
|
}
|
|
452
503
|
|
|
453
504
|
private resolveModel(params: ChatCompletionParams): string {
|
|
@@ -464,6 +515,16 @@ export class CodexProvider implements LLMProvider {
|
|
|
464
515
|
return this.reasoningProfiles.get(model.toLowerCase())?.default
|
|
465
516
|
}
|
|
466
517
|
|
|
518
|
+
/**
|
|
519
|
+
* The speed tiers the account's listing offers for `model`. `undefined`
|
|
520
|
+
* until a listing has answered; once one has, a model it does not carry
|
|
521
|
+
* offers none.
|
|
522
|
+
*/
|
|
523
|
+
speedTiersFor(model: string): readonly FastTier[] | undefined {
|
|
524
|
+
if (this.speedTiers === undefined) return undefined
|
|
525
|
+
return this.speedTiers.get(model.toLowerCase()) ?? []
|
|
526
|
+
}
|
|
527
|
+
|
|
467
528
|
async *chatStream(params: ChatCompletionParams): AsyncIterable<StreamChunk> {
|
|
468
529
|
const model = this.resolveModel(params)
|
|
469
530
|
const targetRoute = params.providerRoute ?? {
|
|
@@ -471,11 +532,30 @@ export class CodexProvider implements LLMProvider {
|
|
|
471
532
|
model,
|
|
472
533
|
chainIndex: 0,
|
|
473
534
|
}
|
|
474
|
-
const
|
|
535
|
+
const built = buildRequest(params, model, targetRoute, this.reasoningEffortLevelsFor(model))
|
|
536
|
+
// A tier the account's listing does not offer is refused here, before
|
|
537
|
+
// transport: the backend would drop it and serve standard speed while
|
|
538
|
+
// the caller believes the request was honoured.
|
|
539
|
+
const fast =
|
|
540
|
+
params.speed === 'fast' || params.speed === 'ultrafast'
|
|
541
|
+
? assertFastModeAccepted({
|
|
542
|
+
route: 'subscription',
|
|
543
|
+
model,
|
|
544
|
+
tier: params.speed,
|
|
545
|
+
advertised: this.speedTiersFor(model),
|
|
546
|
+
firstPartyEndpoint: true,
|
|
547
|
+
})
|
|
548
|
+
: undefined
|
|
549
|
+
const request = (
|
|
550
|
+
fast ? { ...built, service_tier: fast.wire } : built
|
|
551
|
+
) as ResponseCreateParamsStreaming
|
|
552
|
+
const requestedSpeed =
|
|
553
|
+
params.speed === 'fast' || params.speed === 'ultrafast' ? params.speed : undefined
|
|
475
554
|
let stream: AsyncIterable<ResponseStreamEvent>
|
|
476
555
|
try {
|
|
477
556
|
stream = (await this.client.responses.create(request, {
|
|
478
557
|
signal: params.signal,
|
|
558
|
+
...(fast?.header ? { headers: fast.header } : {}),
|
|
479
559
|
})) as AsyncIterable<ResponseStreamEvent>
|
|
480
560
|
} catch (error) {
|
|
481
561
|
if (isCallerAbortError(error, params.signal)) throw params.signal?.reason ?? error
|
|
@@ -541,7 +621,10 @@ export class CodexProvider implements LLMProvider {
|
|
|
541
621
|
event.item.type === 'message' &&
|
|
542
622
|
(event.item.phase === 'commentary' || event.item.phase === 'final_answer')
|
|
543
623
|
)
|
|
544
|
-
textItems.set(event.item.id, {
|
|
624
|
+
textItems.set(event.item.id, {
|
|
625
|
+
id: event.item.id,
|
|
626
|
+
phase: event.item.phase,
|
|
627
|
+
})
|
|
545
628
|
if (event.item.type === 'web_search_call') {
|
|
546
629
|
yield {
|
|
547
630
|
id: responseId,
|
|
@@ -678,7 +761,10 @@ export class CodexProvider implements LLMProvider {
|
|
|
678
761
|
const part = textParts[partIndex]
|
|
679
762
|
if (part) textParts[partIndex] = { ...part, text: part.text + suffix }
|
|
680
763
|
// The driver's text, not the model's.
|
|
681
|
-
yield {
|
|
764
|
+
yield {
|
|
765
|
+
id: responseId,
|
|
766
|
+
delta: { content: suffix, contentOrigin: 'driver' },
|
|
767
|
+
}
|
|
682
768
|
}
|
|
683
769
|
const replayState: CodexReplayState = {
|
|
684
770
|
kind: 'namzu.codex.responses',
|
|
@@ -695,7 +781,11 @@ export class CodexProvider implements LLMProvider {
|
|
|
695
781
|
delta: {},
|
|
696
782
|
...(phased ? { textParts } : {}),
|
|
697
783
|
finishReason: calls.length > 0 ? 'tool_calls' : 'stop',
|
|
698
|
-
usage: responseUsage(
|
|
784
|
+
usage: responseUsage(
|
|
785
|
+
event.response.usage ?? {},
|
|
786
|
+
(event.response as { service_tier?: unknown }).service_tier,
|
|
787
|
+
requestedSpeed,
|
|
788
|
+
),
|
|
699
789
|
replayState,
|
|
700
790
|
}
|
|
701
791
|
break
|
|
@@ -715,7 +805,11 @@ export class CodexProvider implements LLMProvider {
|
|
|
715
805
|
delta: {},
|
|
716
806
|
finishReason: reason === 'content_filter' ? 'content_filter' : 'length',
|
|
717
807
|
...(reason === 'content_filter' ? { refusal: {} } : {}),
|
|
718
|
-
usage: responseUsage(
|
|
808
|
+
usage: responseUsage(
|
|
809
|
+
event.response.usage ?? {},
|
|
810
|
+
(event.response as { service_tier?: unknown }).service_tier,
|
|
811
|
+
requestedSpeed,
|
|
812
|
+
),
|
|
719
813
|
}
|
|
720
814
|
break
|
|
721
815
|
}
|
|
@@ -749,6 +843,7 @@ export class CodexProvider implements LLMProvider {
|
|
|
749
843
|
})) as { models?: unknown }
|
|
750
844
|
signal?.throwIfAborted()
|
|
751
845
|
const profiles = new Map<string, SubscriptionReasoningProfile>()
|
|
846
|
+
const tiersBySlug = new Map<string, readonly FastTier[]>()
|
|
752
847
|
const models = (Array.isArray(response.models) ? response.models : []).flatMap(
|
|
753
848
|
(value): ModelInfo[] => {
|
|
754
849
|
const item = record(value)
|
|
@@ -756,6 +851,8 @@ export class CodexProvider implements LLMProvider {
|
|
|
756
851
|
if (item.visibility === 'hide' || item.visibility === 'hidden') return []
|
|
757
852
|
const profile = subscriptionReasoningProfile(item)
|
|
758
853
|
if (profile) profiles.set(item.slug.toLowerCase(), profile)
|
|
854
|
+
const speedTiers = subscriptionSpeedTiers(item)
|
|
855
|
+
tiersBySlug.set(item.slug.toLowerCase(), speedTiers)
|
|
759
856
|
return [
|
|
760
857
|
{
|
|
761
858
|
id: item.slug,
|
|
@@ -764,11 +861,17 @@ export class CodexProvider implements LLMProvider {
|
|
|
764
861
|
supportsStreaming: true,
|
|
765
862
|
...(profile ? { reasoningEffortLevels: profile.levels } : {}),
|
|
766
863
|
...(profile?.default !== undefined ? { reasoningEffortDefault: profile.default } : {}),
|
|
864
|
+
speedTiers,
|
|
767
865
|
},
|
|
768
866
|
]
|
|
769
867
|
},
|
|
770
868
|
)
|
|
771
869
|
this.reasoningProfiles = profiles
|
|
870
|
+
this.speedTiers = tiersBySlug
|
|
871
|
+
listingByAccount.set(this.listingKey, {
|
|
872
|
+
speedTiers: tiersBySlug,
|
|
873
|
+
reasoningProfiles: profiles,
|
|
874
|
+
})
|
|
772
875
|
return models
|
|
773
876
|
}
|
|
774
877
|
|
package/src/fast-mode.ts
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { ProviderRequestError } from '@namzu/sdk'
|
|
2
|
+
import type { InferenceSpeed, TokenUsage } from '@namzu/sdk'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Fast mode on OpenAI models: a premium low-latency tier, asked for with
|
|
6
|
+
* `speed: 'fast'` (or `'ultrafast'` where the route offers it).
|
|
7
|
+
*
|
|
8
|
+
* Two routes, decided differently because the evidence differs:
|
|
9
|
+
*
|
|
10
|
+
* - `api` (an API key, Chat Completions): a table of exact model ids, from the
|
|
11
|
+
* Fast rows of the vendor's API pricing page and its fast-mode guide
|
|
12
|
+
* (checked 2026-11-22). A table and not a floor, because OpenAI ids do not
|
|
13
|
+
* follow one family-and-version grammar. A model absent from the table is
|
|
14
|
+
* refused until a one-line change, which is cheaper than sending a tier a
|
|
15
|
+
* model rejects. `ultrafast` is never sent here: Chat Completions has no such
|
|
16
|
+
* value.
|
|
17
|
+
* - `subscription` (the Codex subscription, Responses): no model list in code.
|
|
18
|
+
* The account's own model listing says which tiers a model offers
|
|
19
|
+
* (`ModelInfo.speedTiers`), and a tier is admitted only when it is listed.
|
|
20
|
+
*
|
|
21
|
+
* Everything else is refused here, before transport, so the rule is one rule:
|
|
22
|
+
* `fast` means fast or an error, never a quiet standard-speed reply.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export type OpenAIFastRoute = 'api' | 'subscription'
|
|
26
|
+
export type FastTier = 'fast' | 'ultrafast'
|
|
27
|
+
|
|
28
|
+
export interface OpenAIFastRate {
|
|
29
|
+
/** `billing`: a multiple of the per-token rate. `plan`: the subscription allowance drains faster. */
|
|
30
|
+
readonly kind: 'billing' | 'plan'
|
|
31
|
+
/** Stated by a source for this model; absent when no source states one. Never invented. */
|
|
32
|
+
readonly multiplier?: number
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type OpenAIFastVerdict =
|
|
36
|
+
| {
|
|
37
|
+
readonly ok: true
|
|
38
|
+
readonly tier: FastTier
|
|
39
|
+
readonly wire: 'priority' | 'ultrafast'
|
|
40
|
+
readonly rate: OpenAIFastRate
|
|
41
|
+
}
|
|
42
|
+
| { readonly ok: false; readonly reason: string }
|
|
43
|
+
|
|
44
|
+
export interface OpenAIFastInput {
|
|
45
|
+
readonly route: OpenAIFastRoute
|
|
46
|
+
readonly model: string
|
|
47
|
+
readonly tier: FastTier
|
|
48
|
+
/** Subscription only: the tiers the account's model listing established for `model`. Absent: not loaded. */
|
|
49
|
+
readonly advertised?: readonly FastTier[]
|
|
50
|
+
/** Whether requests go to OpenAI's own endpoint. Only the `api` route reads it. */
|
|
51
|
+
readonly firstPartyEndpoint: boolean
|
|
52
|
+
/** The host requests go to, named in the refusal when it is not OpenAI's own. */
|
|
53
|
+
readonly host?: string
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Models that take Fast on the API, with the vendor's multiple of every rate.
|
|
58
|
+
* Source: the Fast table of the API pricing page, checked 2026-11-22. The
|
|
59
|
+
* multiple is per model: gpt-5.5 is billed at 2.5 times, the others at 2.
|
|
60
|
+
*/
|
|
61
|
+
export const OPENAI_FAST_TABLE: Readonly<Record<string, number>> = {
|
|
62
|
+
'gpt-5.4': 2,
|
|
63
|
+
'gpt-5.4-mini': 2,
|
|
64
|
+
'gpt-5.5': 2.5,
|
|
65
|
+
'gpt-5.6-sol': 2,
|
|
66
|
+
'gpt-5.6-terra': 2,
|
|
67
|
+
'gpt-5.6-luna': 2,
|
|
68
|
+
'gpt-6-sol': 2,
|
|
69
|
+
'gpt-6-luna': 2,
|
|
70
|
+
'gpt-6-astra': 2,
|
|
71
|
+
'gpt-6.1-sol': 2,
|
|
72
|
+
'gpt-6.1-terra': 2,
|
|
73
|
+
'gpt-6.2-sol': 2,
|
|
74
|
+
'gpt-6.2-astra': 2,
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* How much faster the Codex plan allowance drains on a tier, where a source
|
|
79
|
+
* states it for the model. List-price figures: Ultrafast carries a promotion
|
|
80
|
+
* until 2027-02-28 and these are the figures after it, so a message never
|
|
81
|
+
* understates. Source: the vendor's Codex pricing page, checked 2026-11-22.
|
|
82
|
+
* A model absent here has no stated figure, and the message gives none.
|
|
83
|
+
*/
|
|
84
|
+
export const PLAN_MULTIPLIER: Readonly<Record<FastTier, Readonly<Record<string, number>>>> = {
|
|
85
|
+
fast: {
|
|
86
|
+
'gpt-6.2-sol': 2,
|
|
87
|
+
'gpt-6.2-astra': 2,
|
|
88
|
+
'gpt-6.1-sol': 2.5,
|
|
89
|
+
'gpt-6.1-terra': 2.5,
|
|
90
|
+
'gpt-6-astra': 2.5,
|
|
91
|
+
'gpt-6-sol': 2.5,
|
|
92
|
+
'gpt-6-luna': 2.5,
|
|
93
|
+
},
|
|
94
|
+
ultrafast: {
|
|
95
|
+
'gpt-6.2-sol': 6,
|
|
96
|
+
'gpt-6.2-astra': 6,
|
|
97
|
+
'gpt-6-astra': 8,
|
|
98
|
+
'gpt-6.1-sol': 8,
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const SNAPSHOT_SUFFIX = /-\d{4}-\d{2}-\d{2}$/
|
|
103
|
+
|
|
104
|
+
function normalise(model: string): string {
|
|
105
|
+
return model.toLowerCase().replace(SNAPSHOT_SUFFIX, '')
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The model list a refusal names, generated from the table so it is never typed twice. */
|
|
109
|
+
function offeredModelsText(): string {
|
|
110
|
+
const ids = Object.keys(OPENAI_FAST_TABLE)
|
|
111
|
+
const named = ids.filter((id) => !id.startsWith('gpt-6'))
|
|
112
|
+
const families = [
|
|
113
|
+
...new Set(ids.filter((id) => id.startsWith('gpt-6')).map((id) => id.replace(/-[a-z]+$/, ''))),
|
|
114
|
+
]
|
|
115
|
+
const familyText =
|
|
116
|
+
families.length > 1
|
|
117
|
+
? `${families.slice(0, -1).join(', ')} and ${families[families.length - 1]}`
|
|
118
|
+
: (families[0] ?? '')
|
|
119
|
+
return `${named.join(', ')}, and the ${familyText} models (sol, terra, luna and astra where they exist)`
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const refuse = (reason: string): OpenAIFastVerdict => ({ ok: false, reason })
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Whether `tier` may be sent to `model` on `route`, and what it costs. Pure:
|
|
126
|
+
* the request path and a caller deciding whether to offer the mode use this
|
|
127
|
+
* one function, so they cannot disagree.
|
|
128
|
+
*/
|
|
129
|
+
export function resolveFastMode(input: OpenAIFastInput): OpenAIFastVerdict {
|
|
130
|
+
const { route, model, tier } = input
|
|
131
|
+
const id = normalise(model)
|
|
132
|
+
if (route === 'api') {
|
|
133
|
+
if (tier === 'ultrafast') {
|
|
134
|
+
return refuse(
|
|
135
|
+
'Ultrafast is offered through the OpenAI (Codex subscription) sign-in only. /fast turns on fast mode here.',
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
if (!input.firstPartyEndpoint) {
|
|
139
|
+
return refuse(
|
|
140
|
+
`Fast mode is offered on OpenAI's own endpoint only, and requests here go to ${input.host ?? 'another host'}.`,
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
const multiplier = OPENAI_FAST_TABLE[id]
|
|
144
|
+
if (multiplier === undefined) {
|
|
145
|
+
return refuse(
|
|
146
|
+
`Model "${model}" has no fast mode. It is offered on ${offeredModelsText()}; choose one of those with /model.`,
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
ok: true,
|
|
151
|
+
tier: 'fast',
|
|
152
|
+
wire: 'priority',
|
|
153
|
+
rate: { kind: 'billing', multiplier },
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (input.advertised === undefined) {
|
|
157
|
+
return refuse(
|
|
158
|
+
'Fast mode needs your Codex model list, which has not loaded yet. Try /fast again in a moment.',
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
if (!input.advertised.includes(tier)) {
|
|
162
|
+
return refuse(
|
|
163
|
+
tier === 'ultrafast'
|
|
164
|
+
? `Your Codex subscription does not list Ultrafast for model "${model}". It is offered on some plans and models only; /fast turns on fast mode instead.`
|
|
165
|
+
: `Your Codex subscription does not list fast mode for model "${model}". Pick another model with /model.`,
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
const multiplier = PLAN_MULTIPLIER[tier][id]
|
|
169
|
+
return {
|
|
170
|
+
ok: true,
|
|
171
|
+
tier,
|
|
172
|
+
wire: tier === 'fast' ? 'priority' : 'ultrafast',
|
|
173
|
+
rate: { kind: 'plan', ...(multiplier !== undefined ? { multiplier } : {}) },
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Whether this model takes `tier` on this route right now. */
|
|
178
|
+
export function supportsFastMode(input: OpenAIFastInput): boolean {
|
|
179
|
+
return resolveFastMode(input).ok
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Refuse, before transport, a tier the route would drop or answer with an
|
|
184
|
+
* error. Returns the wire value to send and, on the subscription route, the
|
|
185
|
+
* routing header that goes with it. The error is the `bad_request` the
|
|
186
|
+
* vendor's 400 would have become, with its own code.
|
|
187
|
+
*/
|
|
188
|
+
export function assertFastModeAccepted(input: OpenAIFastInput): {
|
|
189
|
+
wire: 'priority' | 'ultrafast'
|
|
190
|
+
header?: Record<string, string>
|
|
191
|
+
} {
|
|
192
|
+
const verdict = resolveFastMode(input)
|
|
193
|
+
if (!verdict.ok) {
|
|
194
|
+
throw new ProviderRequestError({
|
|
195
|
+
kind: 'bad_request',
|
|
196
|
+
providerId: input.route === 'api' ? 'openai' : 'codex',
|
|
197
|
+
providerCode: 'fast_mode_unsupported',
|
|
198
|
+
detail: verdict.reason,
|
|
199
|
+
})
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
wire: verdict.wire,
|
|
203
|
+
...(input.route === 'subscription'
|
|
204
|
+
? {
|
|
205
|
+
header: {
|
|
206
|
+
'x-codex-routing-hint': `model=${input.model};tier=${verdict.wire}`,
|
|
207
|
+
},
|
|
208
|
+
}
|
|
209
|
+
: {}),
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Whether a base URL is OpenAI's own API host (no URL at all means the default, which is). */
|
|
214
|
+
export function isFirstPartyOpenAIEndpoint(baseURL: string | undefined): boolean {
|
|
215
|
+
if (!baseURL) return true
|
|
216
|
+
try {
|
|
217
|
+
return new URL(baseURL).hostname === 'api.openai.com'
|
|
218
|
+
} catch {
|
|
219
|
+
return true
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* What a response's `service_tier` says about the call, as the fields
|
|
225
|
+
* `TokenUsage` carries. `priority` and `fast` are Fast and are reported
|
|
226
|
+
* through `speed`, never through `serviceTier` (which the cost rule reads as
|
|
227
|
+
* "unpriced" for `priority`). `default` and `auto` are standard: a request
|
|
228
|
+
* downgraded under load bills at the standard rate. No echo means the tier
|
|
229
|
+
* asked for, so an unreported speed is never priced below what was requested.
|
|
230
|
+
*/
|
|
231
|
+
export function servedSpeed(
|
|
232
|
+
served: unknown,
|
|
233
|
+
requested: InferenceSpeed | undefined,
|
|
234
|
+
): Pick<TokenUsage, 'speed' | 'serviceTier'> {
|
|
235
|
+
if (typeof served === 'string' && served.length > 0) {
|
|
236
|
+
switch (served) {
|
|
237
|
+
case 'priority':
|
|
238
|
+
case 'fast':
|
|
239
|
+
return { speed: 'fast' }
|
|
240
|
+
case 'ultrafast':
|
|
241
|
+
return { speed: 'ultrafast' }
|
|
242
|
+
case 'default':
|
|
243
|
+
case 'auto':
|
|
244
|
+
return {}
|
|
245
|
+
default:
|
|
246
|
+
return { serviceTier: served }
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return requested === 'fast' || requested === 'ultrafast' ? { speed: requested } : {}
|
|
250
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -39,6 +39,21 @@ export {
|
|
|
39
39
|
CODEX_CAPABILITIES,
|
|
40
40
|
CodexProvider,
|
|
41
41
|
} from './codex.js'
|
|
42
|
+
export {
|
|
43
|
+
OPENAI_FAST_TABLE,
|
|
44
|
+
PLAN_MULTIPLIER,
|
|
45
|
+
isFirstPartyOpenAIEndpoint,
|
|
46
|
+
resolveFastMode as resolveFastModeVerdict,
|
|
47
|
+
servedSpeed,
|
|
48
|
+
supportsFastMode,
|
|
49
|
+
} from './fast-mode.js'
|
|
50
|
+
export type {
|
|
51
|
+
FastTier,
|
|
52
|
+
OpenAIFastInput,
|
|
53
|
+
OpenAIFastRate,
|
|
54
|
+
OpenAIFastRoute,
|
|
55
|
+
OpenAIFastVerdict,
|
|
56
|
+
} from './fast-mode.js'
|
|
42
57
|
export type {
|
|
43
58
|
CodexConfig,
|
|
44
59
|
CodexProviderConfig,
|