fix(codex): honor configured GPT context windows (#8385)

Explicit provider-scoped or per-model context windows now apply to mirrored official GPT catalog entries (context_window and max_context_window), with the auto-compact limit kept in sync; official defaults remain when nothing is configured. Gemini input-overflow errors on the Codex Chat path are normalized to context_length_exceeded and returned as a response.failed stream so Codex can compact, and non-string SSE error codes are stringified so upstream messages survive.

Fixes #8366
This commit is contained in:
funkpopo authored and GitHub committed 2026-10-19 16:24:54 +08:00
1 parent 8d8dc456f0
commit a94f997f1e
8 files changed
+594 -72

No files matched your search

@@ -504,6 +504,14 @@ The form for every non-official Codex provider has a **Model Mapping** table tha
- When **Default Model** is left empty, the first row of the mapping table is used as the default
- **Codex must be restarted** to refresh the model list after changes (`model_catalog_json` is loaded at Codex startup)
GPT models retain the prompts, tools, and reasoning levels from Codex's official catalog, but **an explicit Context Window overrides the catalog default**; leaving it blank keeps that default. Set it to a window supported by your upstream: OpenAI documents 1,050,000 tokens for [GPT-6.1 Sol](https://developers.openai.com/api/docs/models/gpt-6.1-sol) and [GPT-6 Astra](https://developers.openai.com/api/docs/models/gpt-6-astra), while third-party providers may impose their own limits.
Codex may display a smaller usable window. With the official catalog's 95% usable proportion, 272,000 appears as about 258k, 1,000,000 as about 950k, and 1,050,000 as about 997.5k. The advanced [`model_context_window`](https://developers.openai.com/codex/config-reference/#model_context_window) setting overrides all model windows for that provider; `model_auto_compact_token_limit` is the token threshold for automatic compaction, rather than the window size. In aggregation mode, each provider's window and compaction threshold are applied to its own model entries.
When a window is explicitly configured, CC Switch synchronizes the catalog's context window, maximum window, and automatic compaction threshold. Unless `model_auto_compact_token_limit` is set separately, the threshold is 90% of the window (for example, 1,000,000 → 900,000 and 272,000 → 244,800). Provider-wide settings take precedence over per-model settings.
For third-party models such as Gemini, a blank window uses the generic catalog default of 128,000. At 95%, Codex displays about 122k; this does not describe the upstream model's actual limit. Set the window according to your provider's supported limit. If Gemini still rejects the input token count, the local route converts that error into a context-window failure Codex can recognize for compaction recovery. Tokenization differences, images, and tool overhead can also affect the actual request size.
#### Reasoning Capability
When the upstream format is Chat Completions, local routing converts Codex's outgoing reasoning request into parameters the upstream understands. The **Reasoning Capability** group in Advanced Options has two toggles:
@@ -504,6 +504,14 @@ Codex の非公式プロバイダーのフォームには、すべて **モデ
- **デフォルトモデル** を空欄にすると、マッピング表の 1 行目がデフォルトで使用されます
- 変更後、モデルリストを更新するには **Codex の再起動が必要** です(`model_catalog_json` は Codex 起動時に読み込まれます)
GPT モデルでは Codex 公式カタログのプロンプト、ツール、思考レベルを保持しますが、**コンテキストウィンドウを明示すると既定値を上書きできます**。空欄の場合は公式カタログの既定値を使用します。上流が対応する値を指定してください。OpenAI の [GPT-6.1 Sol](https://developers.openai.com/api/docs/models/gpt-6.1-sol) と [GPT-6 Astra](https://developers.openai.com/api/docs/models/gpt-6-astra) の仕様はどちらも 1,050,000 トークンですが、第三者プロバイダーでは独自の制限がある場合があります。
Codex が表示する利用可能なウィンドウは入力値より小さくなる場合があります。公式カタログの利用可能割合が 95% の場合、272,000 は約 258k、1,000,000 は約 950k、1,050,000 は約 997.5k と表示されます。詳細設定の [`model_context_window`](https://developers.openai.com/codex/config-reference/#model_context_window) はそのプロバイダーの全モデルのウィンドウを上書きします。`model_auto_compact_token_limit` は自動圧縮を開始するトークン数であり、ウィンドウのサイズではありません。集約モードでは各プロバイダーのウィンドウと圧縮しきい値を、それぞれのモデルエントリーに適用します。
ウィンドウを明示した場合、CC Switch はカタログのコンテキストウィンドウ、最大ウィンドウ、自動圧縮しきい値を同期します。`model_auto_compact_token_limit` を別途指定しない場合、圧縮しきい値はウィンドウの 90% です(例:1,000,000 → 900,000、272,000 → 244,800)。プロバイダー全体の設定はモデルごとの設定より優先されます。
Gemini などの第三者モデルでウィンドウが空欄の場合、汎用カタログの既定値は 128,000 です。利用可能割合が 95% なら Codex は約 122k と表示しますが、これは上流モデルの実際の上限を示しません。プロバイダーが対応する値を指定してください。Gemini が入力トークン数の超過を返した場合、ローカルルートは Codex が圧縮の復旧処理で認識できるコンテキスト超過イベントに変換します。モデル間のトークン分割の違い、画像、ツールの追加情報も実際のリクエストサイズに影響します。
#### 思考能力(Reasoning)
上流フォーマットが Chat Completions の場合、ローカルルーティングは Codex が送る思考リクエストを上流が理解できるパラメータに変換します。高度なオプションの **思考能力** グループには 2 つのスイッチがあります:
@@ -504,6 +504,14 @@ Codex 非官方供应商的表单里都有 **模型映射** 表,用于声明
- **默认模型** 留空时,默认使用映射表的第一行
- **修改后需要重启 Codex** 才能刷新模型列表(`model_catalog_json` 在 Codex 启动时加载)
GPT 模型保留 Codex 官方目录的提示词、工具和思考档位,但**上下文窗口可以显式覆盖**;留空时沿用官方目录的默认值。请按上游实际支持的窗口填写:[OpenAI 的 GPT-6.1 Sol](https://developers.openai.com/api/docs/models/gpt-6.1-sol) 和 [GPT-6 Astra](https://developers.openai.com/api/docs/models/gpt-6-astra) 模型文档均标明 1,050,000 token,第三方供应商的限制仍以该供应商为准。
Codex 界面显示的可用窗口可能小于填写值。例如,官方目录的可用比例为 95% 时,272,000 显示约 258k,1,000,000 显示约 950k,1,050,000 显示约 997.5k。高级配置中的 [`model_context_window`](https://developers.openai.com/codex/config-reference/#model_context_window) 会统一覆盖该供应商的所有模型窗口;`model_auto_compact_token_limit` 则是触发自动压缩的 token 阈值,不是窗口大小。聚合模式下,每家供应商的窗口和压缩阈值分别写入各自的模型条目。
显式设置窗口时,CC Switch 会同步目录中的上下文窗口、最大窗口和自动压缩阈值;没有单独设置 `model_auto_compact_token_limit` 时,压缩阈值取窗口的 90%(例如 1,000,000 → 900,000,272,000 → 244,800)。供应商级设置优先于逐模型设置。
Gemini 等第三方模型的窗口留空时,通用目录默认使用 128,000;按 95% 计算,Codex 显示约 122k,这不代表上游只支持 122k。请按该供应商的实际限制填写窗口。如果 Gemini 仍返回输入 token 超限,本地路由会将其转换为 Codex 能识别的上下文超限事件,使客户端能够进入压缩恢复流程;不同模型的分词方式及图片、工具开销也会影响实际请求大小。
#### 思考能力(Reasoning)
上游格式为 Chat Completions 时,本地路由会把 Codex 发出的思考请求转换成上游能理解的参数。高级选项的 **思考能力** 分组有两个开关: