diff --git a/db/config.go b/db/config.go
index 0d54320f..f5c91ba0 100644
--- a/db/config.go
+++ b/db/config.go
@@ -4,10 +4,12 @@ import (
"context"
"database/sql"
"encoding/json"
+ "strings"
+ "time"
+
"github.com/Autumn-27/artex/llmnet"
"github.com/Autumn-27/artex/locale"
"github.com/Autumn-27/artex/opencodesdk"
- "time"
)
// ---------- LLM profiles ----------
@@ -163,7 +165,7 @@ ORDER BY is_default DESC, priority DESC, id ASC`)
return out, rows.Err()
}
-// SaveProfile inserts (id==0) or updates a profile. Empty apiKey on update keeps existing.
+// SaveProfile inserts (id==0) or updates a profile, retaining an omitted key when its scope allows.
func (d *DB) SaveProfile(p *LLMProfile) (int64, error) {
if p.Format == opencodesdk.Format && !opencodesdk.ValidModel(p.Model) {
return 0, locale.NewError("OpenCode SDK model must be providerID/modelID")
@@ -184,24 +186,30 @@ VALUES ($1,$2,NULLIF($3,''),NULLIF($4,''),$5,NULLIF($6,''),NULLIF($7,''),$8,$9,$
r.Connect.Attempts, r.Connect.IntervalMS, r.Empty.Attempts, r.Empty.IntervalMS, r.Stream.Attempts, r.Stream.IntervalMS, p.KeepAlive, p.MaxConcurrent).Scan(&id)
return id, err
}
- if p.APIKey == "" {
- // Lock the credential scope and update key/hint together; concurrent edits cannot reuse a token from another bridge.
+ if strings.TrimSpace(p.APIKey) == "" {
+ // Lock the credential scope and update key/hint together so concurrent edits use the current endpoint.
tx, err := d.Begin()
if err != nil {
return p.ID, err
}
defer tx.Rollback()
var oldFormat, oldURL string
- if err = tx.QueryRow(`SELECT format,COALESCE(base_url,'') FROM llm_profiles WHERE id=$1 FOR UPDATE`, p.ID).Scan(&oldFormat, &oldURL); err != nil {
+ var active bool
+ if err = tx.QueryRow(`SELECT format,COALESCE(base_url,''),is_default FROM llm_profiles WHERE id=$1 FOR UPDATE`, p.ID).Scan(&oldFormat, &oldURL, &active); err != nil {
if err == sql.ErrNoRows {
return p.ID, nil
}
return p.ID, err
}
clearSDK := (oldFormat == opencodesdk.Format || p.Format == opencodesdk.Format) && (oldFormat != p.Format || !opencodesdk.SameEndpoint(oldURL, p.BaseURL))
+ crossesGateway := llmnet.CrossesCheaperInferenceOrigin(oldURL, p.BaseURL)
+ if active && crossesGateway && (p.Format == opencodesdk.Format || (p.Format != "ollama" && !llmnet.IsLocalEndpoint(p.BaseURL))) {
+ return p.ID, ErrActiveLLMProfileKeyRequired
+ }
+ clearKey := clearSDK || crossesGateway
_, err = tx.Exec(`UPDATE llm_profiles SET name=$1,format=$2,api_key=CASE WHEN $26 OR ($2='ollama' AND format<>'ollama') THEN NULL ELSE api_key END,api_key_hint=CASE WHEN $26 OR ($2='ollama' AND format<>'ollama') THEN NULL ELSE api_key_hint END,base_url=NULLIF($3,''),proxy=NULLIF($4,''),model=$5,rate_per_second=$6,rate_per_minute=$7,context_window_k=$8,reasoning_effort=$9,priority=$10,pool_exclude=$11,thinking_type=$12,streaming=$13,max_tokens=$14,max_tokens_field=$15,session_header_key=$16,retry_connect_attempts=$17,retry_connect_interval_ms=$18,retry_empty_attempts=$19,retry_empty_interval_ms=$20,retry_stream_attempts=$21,retry_stream_interval_ms=$22,keep_alive=$23,llm_max_concurrent=$24 WHERE id=$25`,
p.Name, p.Format, p.BaseURL, p.Proxy, p.Model, p.RatePerSecond, p.RatePerMinute, p.ContextWindowK, p.ReasoningEffort, p.Priority, p.PoolExclude, p.ThinkingType, p.Streaming, p.MaxTokens, p.MaxTokensField, p.SessionHeaderKey,
- r.Connect.Attempts, r.Connect.IntervalMS, r.Empty.Attempts, r.Empty.IntervalMS, r.Stream.Attempts, r.Stream.IntervalMS, p.KeepAlive, p.MaxConcurrent, p.ID, clearSDK)
+ r.Connect.Attempts, r.Connect.IntervalMS, r.Empty.Attempts, r.Empty.IntervalMS, r.Stream.Attempts, r.Stream.IntervalMS, p.KeepAlive, p.MaxConcurrent, p.ID, clearKey)
if err != nil {
return p.ID, err
}
@@ -215,6 +223,7 @@ VALUES ($1,$2,NULLIF($3,''),NULLIF($4,''),$5,NULLIF($6,''),NULLIF($7,''),$8,$9,$
var (
ErrActiveLLMProfileDelete = locale.NewError("cannot delete the active LLM profile; activate another profile first")
+ ErrActiveLLMProfileKeyRequired = locale.NewError("API key was not provided")
ErrLLMProfileReferencesChanged = locale.NewError("LLM profile references changed while deleting; retry the request")
ErrLLMProfileNotFound = locale.NewError("LLM profile not found")
)
diff --git a/db/config_sdk_test.go b/db/config_sdk_test.go
index c4602f66..a82d57b8 100644
--- a/db/config_sdk_test.go
+++ b/db/config_sdk_test.go
@@ -1,11 +1,12 @@
package db
import (
+ "errors"
"fmt"
"testing"
)
-func TestSDKProfileCredentialTransitions(t *testing.T) {
+func TestProfileCredentialTransitions(t *testing.T) {
d, err := Open(testDSN(t))
if err != nil {
t.Fatal(err)
@@ -21,10 +22,16 @@ func TestSDKProfileCredentialTransitions(t *testing.T) {
{"cloud to SDK", "openai", "https://api.openai.com/v1", "opencode-sdk", "http://localhost:4097", "", ""},
{"SDK explicit replacement", "opencode-sdk", "http://localhost:4097/a", "opencode-sdk", "http://localhost:4097/b", "new-token", "new-token"},
{"cloud unchanged behavior", "openai", "https://one.example/v1", "openai", "https://two.example/v1", "", "old-token"},
+ {"cloud to Cheaper Inference", "openai", "https://api.openai.com/v1", "openai", "https://api.cheaperinference.com/v1", "", ""},
+ {"Cheaper Inference to cloud", "openai", "https://api.cheaperinference.com/v1", "openai", "", "", ""},
+ {"Cheaper Inference same origin", "openai", "https://api.cheaperinference.com/v1", "openai", "https://API.CHEAPERINFERENCE.COM.:443/custom", "", "old-token"},
+ {"Cheaper Inference different scheme", "openai", "https://api.cheaperinference.com/v1", "openai", "http://api.cheaperinference.com/v1", "", ""},
+ {"Cheaper Inference different port", "openai", "https://api.cheaperinference.com/v1", "openai", "https://api.cheaperinference.com:8443/v1", "", ""},
+ {"Cheaper Inference explicit replacement", "openai", "https://api.openai.com/v1", "openai", "https://api.cheaperinference.com/v1", "new-token", "new-token"},
}
for i, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
- p := &LLMProfile{Name: fmt.Sprintf("sdk-token-transition-%d", i), Format: tc.oldFormat, BaseURL: tc.oldURL, Model: "opencode/test", APIKey: "old-token", Streaming: true}
+ p := &LLMProfile{Name: fmt.Sprintf("credential-transition-%d", i), Format: tc.oldFormat, BaseURL: tc.oldURL, Model: "opencode/test", APIKey: "old-token", Streaming: true}
id, e := d.SaveProfile(p)
if e != nil {
t.Fatal(e)
@@ -57,6 +64,69 @@ func TestSDKProfileCredentialTransitions(t *testing.T) {
}
}
+func TestActiveGatewayCredentialTransitionRequiresKey(t *testing.T) {
+ d, err := Open(testDSN(t))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer d.Close()
+ previousActive, err := d.ActiveProfile()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if previousActive != nil {
+ defer d.SetActiveProfile(previousActive.ID)
+ }
+
+ const gateway = "https://api.cheaperinference.com/v1"
+ for _, tc := range []struct {
+ name, format, target, key string
+ wantError bool
+ }{
+ {"cloud default needs a key", "openai", "", "", true},
+ {"whitespace is not a key", "openai", "", " ", true},
+ {"SDK bridge needs a key", "opencode-sdk", "http://127.0.0.1:4097", "", true},
+ {"local API can be keyless", "openai", "http://127.0.0.1:8080/v1", "", false},
+ {"Ollama can be keyless", "ollama", "http://127.0.0.1:11434", "", false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ p := &LLMProfile{Name: "active-gateway-scope", Format: "openai", BaseURL: gateway, Model: "opencode/test", APIKey: "old-token"}
+ id, err := d.SaveProfile(p)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer d.Exec(`DELETE FROM llm_profiles WHERE id=$1`, id)
+ if err := d.SetActiveProfile(id); err != nil {
+ t.Fatal(err)
+ }
+
+ p.ID, p.Format, p.BaseURL, p.APIKey = id, tc.format, tc.target, tc.key
+ _, err = d.SaveProfile(p)
+ wantFormat, wantURL, wantKey, wantHint := tc.format, tc.target, "", ""
+ if tc.wantError {
+ if !errors.Is(err, ErrActiveLLMProfileKeyRequired) {
+ t.Fatalf("save error = %v, want active profile key required", err)
+ }
+ wantFormat, wantURL, wantKey, wantHint = "openai", gateway, "old-token", "…oken"
+ } else if err != nil {
+ t.Fatal(err)
+ }
+
+ got, err := d.ActiveProfile()
+ if err != nil || got == nil {
+ t.Fatalf("active profile = %v, err = %v", got, err)
+ }
+ var hint string
+ if err := d.QueryRow(`SELECT COALESCE(api_key_hint,'') FROM llm_profiles WHERE id=$1`, id).Scan(&hint); err != nil {
+ t.Fatal(err)
+ }
+ if got.ID != id || got.Format != wantFormat || got.BaseURL != wantURL || got.APIKey != wantKey || hint != wantHint {
+ t.Fatalf("active credential or endpoint differs from expected state: id=%d format=%q url=%q hint=%q", got.ID, got.Format, got.BaseURL, hint)
+ }
+ })
+ }
+}
+
func TestSDKFormatUpgradePreservesExistingProfiles(t *testing.T) {
d, err := Open(testDSN(t))
if err != nil {
diff --git a/docs/es/llm-providers.md b/docs/es/llm-providers.md
index 3a44792a..3f92b141 100644
--- a/docs/es/llm-providers.md
+++ b/docs/es/llm-providers.md
@@ -114,6 +114,24 @@ SCOPEWEAVER_LLM_MODEL=glm-5.3-flash
`SCOPEWEAVER_LLM_PROVIDER=opencode-go` se resuelve al formato de protocolo OpenAI Chat Completions; la URL base y el modelo no definidos usan los valores de la tabla anterior.
+## Cheaper Inference (pasarela compatible con OpenAI)
+
+Abre **LLM → Nuevo → Plantilla de configuración** y elige **Cheaper Inference · gpt-5.4-mini**.
+
+| Campo | Valor |
+| --- | --- |
+| Formato | OpenAI Chat Completions |
+| URL base | `https://api.cheaperinference.com/v1` |
+| Modelo | `gpt-5.4-mini` |
+| Pensamiento | omitido (predeterminado del modelo) |
+| Esfuerzo de razonamiento | omitido (predeterminado del modelo) |
+| Ventana de contexto (K) | `0` (200K por defecto) |
+| Límite de salida | `16384`, enviado como `max_completion_tokens` |
+
+Cheaper Inference es una pasarela LLM compatible con OpenAI; una sola clave de API da acceso a modelos de varios laboratorios. El ID del modelo y sus límites (contexto de 400K, salida de 128K) se comprobaron en la lista pública de modelos `https://api.cheaperinference.com/public/models` el 2026-11-05. Los modelos GPT-5.4 reciben el límite de salida como `max_completion_tokens`; la plantilla omite el esfuerzo para usar el predeterminado del modelo. **Cargar modelos** lee la lista `/v1/models` de la pasarela, que requiere la clave; el campo sigue editable para otros ID como `deepseek-v4-flash` o `claude-sonnet-5`.
+
+**Credencial:** crea una clave en https://cheaperinference.com (documentación: https://cheaperinference.com/docs). Al elegir esta plantilla, o al cambiar la **URL base** hacia o desde la pasarela, se borra la clave que ya hubiera en el formulario, de modo que una clave de otro endpoint nunca se envía a la pasarela ni una clave de la pasarela a otro destino. Después de elegir la plantilla, pega tu clave de Cheaper Inference en **Clave API**; **Cargar modelos**, **Probar conexión** y el guardado se detienen con un aviso de clave ausente hasta que la introduzcas. Volver a elegir la plantilla o cambiar solo la ruta en la pasarela conserva la clave introducida. La plantilla no guarda ni activa nada. La plantilla usa la ruta existente de OpenAI Chat Completions; está cubierta por pruebas unitarias de la plantilla, no por llamadas reales.
+
## Controlar el uso de tokens
Cada turno de agente reenvía las instrucciones de sistema, esquemas de herramientas y conversación acumulada. La entrada crece turno a turno hasta que la compresión acorta el historial, y la caché del proveedor determina cuánto se factura a la tarifa menor de caché. Esta sección responde a la [incidencia #24](https://github.com/cskwork/scopeweaver/issues/24) para perfiles de nube. Ollama nativo tiene protecciones propias descritas [abajo](#protecciones-durante-la-ejecución).
diff --git a/docs/ja/llm-providers.md b/docs/ja/llm-providers.md
index 5b6ad0fa..995b6c2f 100644
--- a/docs/ja/llm-providers.md
+++ b/docs/ja/llm-providers.md
@@ -119,6 +119,25 @@ SCOPEWEAVER_LLM_MODEL=glm-5.3-flash
`SCOPEWEAVER_LLM_PROVIDER=opencode-go` は OpenAI Chat Completions の通信形式に対応し、ベース URL とモデルが未設定なら上記表の既定値を使います。
+
+## Cheaper Inference(OpenAI 互換ゲートウェイ)
+
+**LLM → 新規 → 設定テンプレート**で **Cheaper Inference · gpt-5.4-mini** を選択します。
+
+| フィールド | 値 |
+| --- | --- |
+| 形式 | OpenAI Chat Completions |
+| ベース URL | `https://api.cheaperinference.com/v1` |
+| モデル | `gpt-5.4-mini` |
+| 思考機能 | 省略(モデルの既定値)|
+| 推論強度 | 省略(モデルの既定値)|
+| コンテキストウィンドウ(K)| `0`(既定 200K)|
+| 出力上限 | `16384`、`max_completion_tokens` として送信 |
+
+Cheaper Inference は OpenAI 互換の LLM ゲートウェイで、1 つの API キーで複数のラボのモデルを使えます。モデル ID と上限(コンテキスト 400K、出力 128K)は 2026-11-05 に公開モデル一覧 `https://api.cheaperinference.com/public/models` で確認しました。GPT-5.4 モデルは出力上限を `max_completion_tokens` で受け取ります。テンプレートは推論強度を省略し、モデルの既定値を使います。**モデルを読み込む**はゲートウェイの `/v1/models` 一覧を取得し、キーが必要です。モデル欄は `deepseek-v4-flash` や `claude-sonnet-5` など他の ID に編集できます。
+
+**認証情報:** https://cheaperinference.com でキーを作成してください(ドキュメント: https://cheaperinference.com/docs)。このテンプレートを選ぶか、**ベース URL** をゲートウェイへ、またはゲートウェイから変更すると、フォームに入力済みのキーは消去されます。これにより、別のエンドポイント用のキーがゲートウェイへ、ゲートウェイのキーが他の宛先へ送られることはありません。テンプレートを選んだ後、Cheaper Inference のキーを **API キー**に明示的に貼り付けてください。入力するまで、**モデルを読み込む**、**接続テスト**、保存はキー未入力のメッセージで停止します。テンプレートを選び直す場合や、ゲートウェイ上でパスだけを変更する場合は、入力済みのキーを保持します。テンプレートは何も保存・有効化しません。テンプレートは既存の OpenAI Chat Completions の経路を使い、確認はテンプレートの単体テストのみで、実際の呼び出しでは確認していません。
+
## トークン使用量の管理
diff --git a/docs/ko/llm-providers.md b/docs/ko/llm-providers.md
index 9f79cb07..02493b61 100644
--- a/docs/ko/llm-providers.md
+++ b/docs/ko/llm-providers.md
@@ -116,6 +116,24 @@ SCOPEWEAVER_LLM_MODEL=glm-5.3-flash
`SCOPEWEAVER_LLM_PROVIDER=opencode-go`는 OpenAI Chat Completions 와이어 형식으로 해석되며, 기본 URL과 모델이 비어 있으면 위 표의 기본값을 사용합니다.
+## Cheaper Inference(OpenAI 호환 게이트웨이)
+
+**LLM → 새로 만들기 → 설정 템플릿**에서 **Cheaper Inference · gpt-5.4-mini**를 고르세요.
+
+| 필드 | 값 |
+| --- | --- |
+| 형식 | OpenAI Chat Completions |
+| 기본 URL | `https://api.cheaperinference.com/v1` |
+| 모델 | `gpt-5.4-mini` |
+| 추론 | 생략(모델 기본값) |
+| 강도 | 생략(모델 기본값) |
+| 문맥 창(K) | `0`(기본 200K) |
+| 출력 한도 | `16384`, `max_completion_tokens`로 전송 |
+
+Cheaper Inference는 OpenAI 호환 LLM 게이트웨이이며, API 키 하나로 여러 연구소의 모델을 사용할 수 있습니다. 모델 ID와 한도(문맥 400K, 출력 128K)는 2026-11-05에 공개 모델 목록 `https://api.cheaperinference.com/public/models`에서 확인했습니다. GPT-5.4 모델은 출력 한도를 `max_completion_tokens`로 받으며, 템플릿은 강도를 생략해 모델 기본값을 따릅니다. **모델 불러오기**는 게이트웨이의 `/v1/models` 목록을 읽으며 키가 필요합니다. 모델 필드는 `deepseek-v4-flash`, `claude-sonnet-5` 같은 다른 ID로 수정할 수 있습니다.
+
+**자격 증명:** https://cheaperinference.com 에서 키를 만드세요(문서: https://cheaperinference.com/docs). 이 템플릿을 고르거나 **기본 URL**을 게이트웨이로 또는 게이트웨이에서 바꾸면 폼에 있던 키를 지워, 다른 엔드포인트용 키가 게이트웨이로 가거나 게이트웨이 키가 다른 곳으로 가지 않게 합니다. 템플릿을 고른 뒤 Cheaper Inference 키를 **API 키**에 직접 붙여넣으세요. 키를 입력하기 전에는 **모델 불러오기**, **연결 테스트**, 저장이 키 누락 메시지와 함께 멈춥니다. 템플릿을 다시 고르거나 게이트웨이에서 경로만 바꾸면 입력한 키가 유지됩니다. 템플릿은 아무것도 저장하거나 활성화하지 않습니다. 템플릿은 기존 OpenAI Chat Completions 경로를 사용하며, 실제 호출이 아니라 템플릿 단위 테스트로만 확인했습니다.
+
## 토큰 사용량 줄이기
에이전트는 턴마다 시스템 프롬프트, 도구 스키마, 지금까지의 대화를 다시 보냅니다. 그래서 입력은 압축으로 기록이 줄어들 때까지 턴마다 늘어나고, 반복되는 입력 중 얼마가 더 싼 캐시 요금으로 과금되는지는 제공자의 프롬프트 캐시가 정합니다. 이 절은 클라우드 프로필 기준으로 [이슈 #24](https://github.com/cskwork/scopeweaver/issues/24)에 답합니다. 기본 Ollama의 보호 장치는 [아래](#실행-중-보호-장치)에 따로 설명합니다.
diff --git a/docs/llm-providers.md b/docs/llm-providers.md
index 3510ff58..07842539 100644
--- a/docs/llm-providers.md
+++ b/docs/llm-providers.md
@@ -116,6 +116,24 @@ SCOPEWEAVER_LLM_MODEL=glm-5.3-flash
`SCOPEWEAVER_LLM_PROVIDER=opencode-go` resolves to the OpenAI Chat Completions wire format; an unset base URL and model default to the values in the table above.
+## Cheaper Inference (OpenAI-compatible gateway)
+
+Open **LLM → New → Configuration template** and choose **Cheaper Inference · gpt-5.4-mini**.
+
+| Field | Value |
+| --- | --- |
+| Format | OpenAI Chat Completions |
+| Base URL | `https://api.cheaperinference.com/v1` |
+| Model | `gpt-5.4-mini` |
+| Thinking | omitted (model default) |
+| Reasoning effort | omitted (model default) |
+| Context window (K) | `0` (200K default) |
+| Output limit | `16384`, sent as `max_completion_tokens` |
+
+Cheaper Inference is an OpenAI-compatible LLM gateway; one API key gives access to models from several labs. The model id and its limits (400K context, 128K output) were checked in the public model list at `https://api.cheaperinference.com/public/models` on 2026-11-05. GPT-5.4 models take the output limit as `max_completion_tokens`; the template omits effort so the model default applies. **Load models** reads the gateway's `/v1/models` list, which requires the key; the model field stays editable for other ids such as `deepseek-v4-flash` or `claude-sonnet-5`.
+
+**Credential:** create a key at https://cheaperinference.com (documentation: https://cheaperinference.com/docs). Choosing this template, or changing **Base URL** to or from the gateway, clears any key already in the form so a key for another endpoint is never sent to the gateway, and a gateway key is never sent elsewhere. After choosing the template, paste your Cheaper Inference key into **API key**; **Load models**, **Test connection** and saving stop with a missing-key message until you enter it. Re-selecting the template or changing only the path on the gateway keeps an entered key. The template does not save or activate anything. The template uses the existing OpenAI Chat Completions path; it is covered by template unit tests, not by live calls.
+
## Controlling token use
Every agent turn resends the system prompt, the tool schemas and the conversation so far. Input therefore grows turn by turn until compaction shortens the history, and provider prompt caching decides how much of the repeated input is billed at the cheaper cached rate. This section answers [issue #24](https://github.com/cskwork/scopeweaver/issues/24) for cloud profiles. Native Ollama has its own safeguards, described [below](#runtime-safeguards).
diff --git a/docs/zh-Hans/llm-providers.md b/docs/zh-Hans/llm-providers.md
index abb8d329..52703c17 100644
--- a/docs/zh-Hans/llm-providers.md
+++ b/docs/zh-Hans/llm-providers.md
@@ -126,6 +126,26 @@ SCOPEWEAVER_LLM_MODEL=glm-5.3-flash
`SCOPEWEAVER_LLM_PROVIDER=opencode-go` 解析为 OpenAI Chat Completions 传输格式;未设基础 URL 与模型时采上表默认值。
+
+
+## Cheaper Inference(OpenAI 兼容网关)
+
+开启「LLM → 新增 → 配置模板」,选择「Cheaper Inference · gpt-5.4-mini」。
+
+| 字段 | 值 |
+| --- | --- |
+| 格式 | OpenAI Chat Completions |
+| 基础 URL | `https://api.cheaperinference.com/v1` |
+| 模型 | `gpt-5.4-mini` |
+| 思考 | 省略(模型默认) |
+| 推理强度 | 省略(模型默认) |
+| 上下文视窗(K) | `0`(默认 200K) |
+| 输出上限 | `16384`,以 `max_completion_tokens` 传送 |
+
+Cheaper Inference 是兼容 OpenAI 的 LLM 网关;一个 API 密钥即可使用多家实验室的模型。模型 ID 及其上限(上下文 400K、输出 128K)于 2026-11-05 在公开模型清单 `https://api.cheaperinference.com/public/models` 中检查。GPT-5.4 模型以 `max_completion_tokens` 接收输出上限;模板省略推理强度,沿用模型默认值。「加载模型」读取网关的 `/v1/models` 清单,需要密钥;模型栏仍可输入 `deepseek-v4-flash`、`claude-sonnet-5` 等其他 ID。
+
+**凭证:** 在 https://cheaperinference.com 创建密钥(文档:https://cheaperinference.com/docs)。选择此模板,或将「基础 URL」改为网关或从网关改为其他地址时,会清除表单中已有的密钥,确保其他端点的密钥不会发送到网关,网关密钥也不会发送到其他地址。选择模板后,请将 Cheaper Inference 密钥明确贴入「API 密钥」;输入前,「加载模型」「测试连接」和保存都会以缺少密钥的提示停止。重新选择此模板或只修改网关上的路径时,会保留已输入的密钥。模板不保存也不启用任何配置。模板使用现有的 OpenAI Chat Completions 路径,仅由模板单元测试覆盖,未经实际调用验证。
+
## 控制 token 用量
diff --git a/docs/zh-Hant/llm-providers.md b/docs/zh-Hant/llm-providers.md
index 8dd7033d..8f56d018 100644
--- a/docs/zh-Hant/llm-providers.md
+++ b/docs/zh-Hant/llm-providers.md
@@ -126,6 +126,26 @@ SCOPEWEAVER_LLM_MODEL=glm-5.3-flash
`SCOPEWEAVER_LLM_PROVIDER=opencode-go` 解析為 OpenAI Chat Completions 傳輸格式;未設基底 URL 與模型時採上表預設值。
+
+
+## Cheaper Inference(OpenAI 相容閘道)
+
+開啟「LLM → 新增 → 設定範本」,選擇「Cheaper Inference · gpt-5.4-mini」。
+
+| 欄位 | 值 |
+| --- | --- |
+| 格式 | OpenAI Chat Completions |
+| 基底 URL | `https://api.cheaperinference.com/v1` |
+| 模型 | `gpt-5.4-mini` |
+| 思考 | 省略(模型預設) |
+| 推理強度 | 省略(模型預設) |
+| 上下文視窗(K) | `0`(預設 200K) |
+| 輸出上限 | `16384`,以 `max_completion_tokens` 傳送 |
+
+Cheaper Inference 是相容 OpenAI 的 LLM 閘道;一組 API 金鑰即可使用多家實驗室的模型。模型 ID 及其上限(上下文 400K、輸出 128K)於 2026-11-05 在公開模型清單 `https://api.cheaperinference.com/public/models` 中檢查。GPT-5.4 模型以 `max_completion_tokens` 接收輸出上限;範本省略推理強度,沿用模型預設值。「載入模型」讀取閘道的 `/v1/models` 清單,需要金鑰;模型欄仍可輸入 `deepseek-v4-flash`、`claude-sonnet-5` 等其他 ID。
+
+**憑證:** 在 https://cheaperinference.com 建立金鑰(文件:https://cheaperinference.com/docs)。選擇此範本,或將「基底 URL」改為閘道或從閘道改為其他位址時,會清除表單中已有的金鑰,確保其他端點的金鑰不會傳送到閘道,閘道金鑰也不會傳送到其他位址。選擇範本後,請將 Cheaper Inference 金鑰明確貼入「API 金鑰」;輸入前,「載入模型」「測試連線」和儲存都會以缺少金鑰的提示停止。重新選擇此範本或只修改閘道上的路徑時,會保留已輸入的金鑰。範本不儲存也不啟用任何設定。範本使用現有的 OpenAI Chat Completions 路徑,僅由範本單元測試涵蓋,未經實際呼叫驗證。
+
## 控制 token 用量
diff --git a/llmnet/credential_origin.go b/llmnet/credential_origin.go
new file mode 100644
index 00000000..d92591df
--- /dev/null
+++ b/llmnet/credential_origin.go
@@ -0,0 +1,47 @@
+package llmnet
+
+import (
+ "net/url"
+ "strconv"
+ "strings"
+
+ "golang.org/x/net/idna"
+)
+
+const cheaperInferenceHost = "api.cheaperinference.com"
+
+// CrossesCheaperInferenceOrigin reports whether a destination change must discard a
+// Cheaper Inference key or the key from the previous endpoint. Paths do not change origin.
+func CrossesCheaperInferenceOrigin(previousURL, nextURL string) bool {
+ previous, next := parseEndpointOrigin(previousURL), parseEndpointOrigin(nextURL)
+ return (previous.host == cheaperInferenceHost || next.host == cheaperInferenceHost) && previous != next
+}
+
+type endpointOrigin struct {
+ scheme string
+ host string
+ port uint16
+}
+
+func parseEndpointOrigin(raw string) endpointOrigin {
+ u, err := url.Parse(strings.TrimSpace(raw))
+ if err != nil || (u.Scheme != "http" && u.Scheme != "https") {
+ return endpointOrigin{}
+ }
+ host, err := idna.Lookup.ToASCII(u.Hostname())
+ host = strings.TrimRight(strings.ToLower(host), ".")
+ if err != nil || host == "" {
+ return endpointOrigin{}
+ }
+ port := uint64(443)
+ if u.Scheme == "http" {
+ port = 80
+ }
+ if value := u.Port(); value != "" {
+ port, err = strconv.ParseUint(value, 10, 16)
+ if err != nil {
+ return endpointOrigin{}
+ }
+ }
+ return endpointOrigin{scheme: u.Scheme, host: host, port: uint16(port)}
+}
diff --git a/llmnet/credential_origin_test.go b/llmnet/credential_origin_test.go
new file mode 100644
index 00000000..8b2ccce4
--- /dev/null
+++ b/llmnet/credential_origin_test.go
@@ -0,0 +1,35 @@
+package llmnet
+
+import "testing"
+
+func TestCrossesCheaperInferenceOrigin(t *testing.T) {
+ const gateway = "https://api.cheaperinference.com/v1"
+ tests := []struct {
+ name string
+ previous string
+ next string
+ wantCrossings bool
+ }{
+ {"paths", gateway, "https://api.cheaperinference.com/custom", false},
+ {"HTTPS default port and host casing", gateway, " HTTPS://API.CHEAPERINFERENCE.COM..:00443/custom ", false},
+ {"HTTP default port", "http://api.cheaperinference.com/v1", "http://api.cheaperinference.com:80", false},
+ {"internationalized hostname", gateway, "https://api。cheaperinference。com/v1", false},
+ {"scheme", gateway, "http://api.cheaperinference.com/v1", true},
+ {"port", gateway, "https://api.cheaperinference.com:8443/v1", true},
+ {"other provider", gateway, "https://other.example/v1", true},
+ {"implicit provider default", gateway, "", true},
+ {"non HTTP destination", gateway, "ftp://api.cheaperinference.com/v1", true},
+ {"lookalike suffix", "https://api.cheaperinference.com.example/v1", "https://other.example/v1", false},
+ {"unrelated providers", "https://one.example/v1", "http://two.example/v2", false},
+ }
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := CrossesCheaperInferenceOrigin(tc.previous, tc.next); got != tc.wantCrossings {
+ t.Errorf("forward crossing = %t, want %t", got, tc.wantCrossings)
+ }
+ if got := CrossesCheaperInferenceOrigin(tc.next, tc.previous); got != tc.wantCrossings {
+ t.Errorf("reverse crossing = %t, want %t", got, tc.wantCrossings)
+ }
+ })
+ }
+}
diff --git a/server/llm_gateway_test.go b/server/llm_gateway_test.go
index 63ff8a99..bd6aa74c 100644
--- a/server/llm_gateway_test.go
+++ b/server/llm_gateway_test.go
@@ -11,6 +11,8 @@ import (
"testing"
"github.com/Autumn-27/artex/agent"
+ "github.com/Autumn-27/artex/db"
+ "github.com/Autumn-27/norma/llm"
)
func TestConnectionLocalGatewayCredentialReuse(t *testing.T) {
@@ -97,18 +99,152 @@ func TestConnectionNativeTargetDoesNotReuseActiveGatewayKey(t *testing.T) {
}
}
+func TestCheaperInferenceCredentialFallbackStaysAtGatewayOrigin(t *testing.T) {
+ t.Setenv("SCOPEWEAVER_CONFIG", filepath.Join(t.TempDir(), "missing.json"))
+ t.Setenv("SCOPEWEAVER_LOCAL_LLM_ONLY", "false")
+ const gateway = "http://api.cheaperinference.com/v1"
+ // The URL identifies a nonlocal endpoint. Any attempted HTTP request goes
+ // through the loopback fixture proxy and cannot reach a provider.
+ const other = "http://203.0.113.7/v1"
+
+ for _, source := range []string{"active", "stored"} {
+ t.Run(source, func(t *testing.T) {
+ var calls atomic.Int32
+ proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ calls.Add(1)
+ if r.Host != "api.cheaperinference.com" || r.Header.Get("Authorization") != "Bearer fixture-gateway-key" {
+ t.Error("unexpected proxy destination or authorization")
+ http.Error(w, "fixture authentication failed", http.StatusUnauthorized)
+ return
+ }
+ w.Header().Set("Content-Type", "application/json")
+ if r.Method == http.MethodGet {
+ fmt.Fprint(w, `{"data":[{"id":"fixture-model"}]}`)
+ } else {
+ fmt.Fprint(w, `{"choices":[{"message":{"role":"assistant","content":"OK"},"finish_reason":"stop"}]}`)
+ }
+ }))
+ defer proxy.Close()
+
+ body := map[string]any{"provider": "openai", "model": "fixture-model", "proxy": proxy.URL, "streaming": false}
+ s := &Server{m: &Manager{}}
+ handlers := map[string]http.HandlerFunc{"connection": s.testLLM}
+ if source == "active" {
+ s.llmCfg = agent.ConfigFrom("openai", "fixture-model", gateway, "fixture-gateway-key", proxy.URL)
+ } else {
+ s.m = backportTestManager(t)
+ id, err := s.m.pg.SaveProfile(&db.LLMProfile{Name: "gateway-key-scope-test", Format: "openai", BaseURL: gateway, Model: "fixture-model", APIKey: "fixture-gateway-key"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer s.m.pg.DeleteProfile(id)
+ body["profile_id"] = id
+ handlers["models"] = s.pgListModels
+ }
+ for name, h := range handlers {
+ t.Run(name, func(t *testing.T) {
+ body["base_url"] = other
+ before := calls.Load()
+ if got := callGatewayHandler(t, h, body); got["ok"] != false || calls.Load() != before {
+ t.Fatalf("cross-origin fallback response = %v, proxy calls = %d", got, calls.Load()-before)
+ }
+ body["base_url"] = gateway
+ if source == "stored" {
+ body["base_url"] = "http://api.cheaperinference.com/custom"
+ }
+ if got := callGatewayHandler(t, h, body); got["ok"] != true || calls.Load() != before+1 {
+ t.Fatalf("same-origin fallback response = %v, proxy calls = %d", got, calls.Load()-before)
+ }
+ })
+ }
+ })
+ }
+}
+
+func TestConnectionOpenAIMaxTokensField(t *testing.T) {
+ t.Setenv("SCOPEWEAVER_CONFIG", filepath.Join(t.TempDir(), "missing.json"))
+ t.Setenv("SCOPEWEAVER_LOCAL_LLM_ONLY", "false")
+ var lastRequest atomic.Pointer[map[string]any]
+ fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ var body map[string]any
+ if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
+ t.Errorf("decode fixture request: %v", err)
+ http.Error(w, "bad fixture request", http.StatusBadRequest)
+ return
+ }
+ lastRequest.Store(&body)
+ w.Header().Set("Content-Type", "application/json")
+ fmt.Fprint(w, `{"choices":[{"message":{"role":"assistant","content":"OK"},"finish_reason":"stop"}]}`)
+ }))
+ defer fixture.Close()
+ s := &Server{m: &Manager{}}
+
+ check := func(t *testing.T, input map[string]any, wantField string) {
+ t.Helper()
+ if got := callGatewayHandler(t, s.testLLM, input); got["ok"] != true {
+ t.Fatalf("connection response = %v", got)
+ }
+ otherField := "max_tokens"
+ if wantField == otherField {
+ otherField = llm.MaxTokensFieldCompletion
+ }
+ wire := lastRequest.Swap(nil)
+ if wire == nil {
+ t.Fatal("connection succeeded without a request to the fixture")
+ }
+ _, hasOtherField := (*wire)[otherField]
+ if (*wire)[wantField] != float64(8192) || hasOtherField {
+ t.Fatalf("wire token fields = %v, %v; want only %s = 8192", (*wire)[wantField], (*wire)[otherField], wantField)
+ }
+ }
+
+ for _, tc := range []struct{ name, submitted, want string }{
+ {"submitted override", llm.MaxTokensFieldCompletion, llm.MaxTokensFieldCompletion},
+ {"legacy omitted field", "", "max_tokens"},
+ {"unknown value", "max_output_tokens", "max_tokens"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ body := map[string]any{"provider": "openai", "model": "gpt-5.4-mini", "base_url": fixture.URL, "streaming": false, "max_tokens": 16384}
+ if tc.submitted != "" {
+ body["max_tokens_field"] = tc.submitted
+ }
+ check(t, body, tc.want)
+ })
+ }
+ t.Run("saved profile", func(t *testing.T) {
+ s.m = backportTestManager(t)
+ id, err := s.m.pg.SaveProfile(&db.LLMProfile{Name: "gateway-saved-token-field", Format: "openai", BaseURL: fixture.URL, Model: "gpt-5.4-mini", MaxTokensField: llm.MaxTokensFieldCompletion})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer s.m.pg.DeleteProfile(id)
+ body := map[string]any{"provider": "openai", "model": "gpt-5.4-mini", "base_url": fixture.URL + "/chat/completions", "streaming": false, "profile_id": id}
+ check(t, body, llm.MaxTokensFieldCompletion)
+ body["model"] = "other-model"
+ check(t, body, "max_tokens")
+ body["model"] = "gpt-5.4-mini"
+ body["max_tokens_field"] = ""
+ check(t, body, "max_tokens")
+ })
+}
+
func testGatewayConnection(t *testing.T, s *Server, format, baseURL, proxy string) map[string]any {
t.Helper()
model := "fixture-model"
if format == "ollama" {
model = "test:latest"
}
- raw, err := json.Marshal(map[string]any{"provider": format, "model": model, "base_url": baseURL, "proxy": proxy, "api_key": "", "streaming": false})
+ return callGatewayHandler(t, s.testLLM, map[string]any{"provider": format, "model": model, "base_url": baseURL, "proxy": proxy, "api_key": "", "streaming": false})
+}
+
+func callGatewayHandler(t *testing.T, h http.HandlerFunc, body map[string]any) map[string]any {
+ t.Helper()
+ raw, err := json.Marshal(body)
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
- s.testLLM(response, httptest.NewRequest(http.MethodPost, "/api/llm/test", bytes.NewReader(raw)))
+ h(response, httptest.NewRequest(http.MethodPost, "/api/llm/test", bytes.NewReader(raw)))
var result map[string]any
if err := json.Unmarshal(response.Body.Bytes(), &result); err != nil || response.Code != http.StatusOK {
t.Fatalf("HTTP %d response=%s decode error=%v", response.Code, response.Body, err)
diff --git a/server/opencode_sdk.go b/server/opencode_sdk.go
index 0144405c..f0ba668e 100644
--- a/server/opencode_sdk.go
+++ b/server/opencode_sdk.go
@@ -10,13 +10,6 @@ import (
"github.com/Autumn-27/artex/opencodesdk"
)
-// Bridge tokens cannot cross formats or normalized endpoint paths, including SDK-to-SDK edits.
-func sdkCredentialCompatible(cfg agent.Config, storedFormat, storedURL string) bool {
- if cfg.Provider() != opencodesdk.Format && storedFormat != opencodesdk.Format {
- return true
- }
- return cfg.Provider() == opencodesdk.Format && storedFormat == opencodesdk.Format && opencodesdk.SameEndpoint(cfg.BaseURL, storedURL)
-}
func (s *Server) listOpenCodeSDKModels(w http.ResponseWriter, r *http.Request, cfg agent.Config) {
fail := func(err error) {
writeJSON(w, 200, map[string]any{"ok": false, "error": locale.ErrorMessage(responseLanguage(w), err)})
diff --git a/server/server.go b/server/server.go
index 95d4fd49..19c5d306 100644
--- a/server/server.go
+++ b/server/server.go
@@ -23,6 +23,7 @@ import (
"github.com/Autumn-27/artex/agent"
"github.com/Autumn-27/artex/chatstate"
"github.com/Autumn-27/artex/db"
+ "github.com/Autumn-27/artex/llmnet"
"github.com/Autumn-27/artex/llmpool"
"github.com/Autumn-27/artex/llmrec"
"github.com/Autumn-27/artex/locale"
@@ -1368,7 +1369,15 @@ func (s *Server) getLLM(w http.ResponseWriter, r *http.Request) {
})
}
-// setLLM configures the LLM at runtime. A blank api_key keeps the existing key.
+// profileCredentialCompatible applies the endpoint scope of a stored credential.
+func profileCredentialCompatible(cfg agent.Config, storedFormat, storedURL string) bool {
+ if cfg.Provider() == opencodesdk.Format || storedFormat == opencodesdk.Format {
+ return cfg.Provider() == opencodesdk.Format && storedFormat == opencodesdk.Format && opencodesdk.SameEndpoint(cfg.BaseURL, storedURL)
+ }
+ return !llmnet.CrossesCheaperInferenceOrigin(storedURL, cfg.BaseURL)
+}
+
+// setLLM configures the LLM at runtime. A blank api_key keeps a compatible active key.
func (s *Server) setLLM(w http.ResponseWriter, r *http.Request) {
var req struct {
KeepAlive string `json:"keep_alive"`
@@ -1403,11 +1412,7 @@ func (s *Server) setLLM(w http.ResponseWriter, r *http.Request) {
if cfg.APIKey == "" && !cfg.Ready() {
s.cfgMu.Lock()
active := s.llmCfg
- if cfg.Provider() == opencodesdk.Format {
- if sdkCredentialCompatible(cfg, active.Provider(), active.BaseURL) {
- cfg.APIKey = active.APIKey
- }
- } else if active.Provider() != opencodesdk.Format {
+ if profileCredentialCompatible(cfg, active.Provider(), active.BaseURL) {
cfg.APIKey = active.APIKey
}
s.cfgMu.Unlock()
@@ -1443,20 +1448,21 @@ func (s *Server) setLLM(w http.ResponseWriter, r *http.Request) {
// testLLM makes a real minimal completion to verify the config works.
func (s *Server) testLLM(w http.ResponseWriter, r *http.Request) {
var req struct {
- KeepAlive string `json:"keep_alive"`
- MaxConcurrent int `json:"llm_max_concurrent"`
- ContextWindowK int `json:"context_window_k"`
- MaxTokens int `json:"max_tokens"`
- Provider string `json:"provider"`
- Model string `json:"model"`
- BaseURL string `json:"base_url"`
- Proxy string `json:"proxy"`
- APIKey string `json:"api_key"`
- ThinkingType string `json:"thinking_type"`
- ReasoningEffort string `json:"reasoning_effort"`
- ProfileID *int64 `json:"profile_id"` // Existing profile under test; an empty api_key uses its stored key.
- Streaming *bool `json:"streaming"` // Omitted means streaming, matching profile-save defaults.
- SessionHeaderKey string `json:"session_header_key"` // Optional custom session header, also sent during testing with a fresh random session ID.
+ KeepAlive string `json:"keep_alive"`
+ MaxConcurrent int `json:"llm_max_concurrent"`
+ ContextWindowK int `json:"context_window_k"`
+ MaxTokens int `json:"max_tokens"`
+ MaxTokensField *string `json:"max_tokens_field"`
+ Provider string `json:"provider"`
+ Model string `json:"model"`
+ BaseURL string `json:"base_url"`
+ Proxy string `json:"proxy"`
+ APIKey string `json:"api_key"`
+ ThinkingType string `json:"thinking_type"`
+ ReasoningEffort string `json:"reasoning_effort"`
+ ProfileID *int64 `json:"profile_id"` // Existing profile under test; an empty api_key uses its stored key.
+ Streaming *bool `json:"streaming"` // Omitted means streaming, matching profile-save defaults.
+ SessionHeaderKey string `json:"session_header_key"` // Optional custom session header, also sent during testing with a fresh random session ID.
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, 400, err)
@@ -1485,9 +1491,10 @@ func (s *Server) testLLM(w http.ResponseWriter, r *http.Request) {
// API-key precedence: submitted form, specified profile's stored key, then global configuration.
// Stored keys never return to the browser; testing an existing profile may require reading its key from DB.
// Likewise, fall back to the stored custom session-header name when omitted by the form.
+ maxTokensField := req.MaxTokensField
if req.ProfileID != nil {
if p, err := s.m.pg.ProfileByID(*req.ProfileID); err == nil && p != nil {
- if cfg.APIKey == "" && (cfg.Provider() != "ollama" || p.Format == "ollama") && sdkCredentialCompatible(cfg, p.Format, p.BaseURL) {
+ if cfg.APIKey == "" && (cfg.Provider() != "ollama" || p.Format == "ollama") && profileCredentialCompatible(cfg, p.Format, p.BaseURL) {
cfg.APIKey = p.APIKey
}
if cfg.SessionHeaderKey == "" {
@@ -1502,8 +1509,17 @@ func (s *Server) testLLM(w http.ResponseWriter, r *http.Request) {
if req.MaxTokens == 0 {
cfg.MaxTokens = p.MaxTokens
}
+ if maxTokensField == nil && cfg.Format == llm.FormatOpenAI && p.Format == "openai" {
+ saved := agent.ConfigFrom(p.Format, p.Model, p.BaseURL, "", "")
+ if cfg.BaseURL == saved.BaseURL && cfg.Model == saved.Model {
+ maxTokensField = &p.MaxTokensField
+ }
+ }
}
}
+ if cfg.Format == llm.FormatOpenAI && maxTokensField != nil && *maxTokensField == llm.MaxTokensFieldCompletion {
+ cfg.MaxTokensField = llm.MaxTokensFieldCompletion
+ }
// Request and saved-profile headers take precedence over provider defaults.
if cfg.SessionHeaderKey == "" {
cfg.SessionHeaderKey = providerSessionHeaderKey
@@ -1516,7 +1532,7 @@ func (s *Server) testLLM(w http.ResponseWriter, r *http.Request) {
// Reuse local gateway credentials only for the same format and transport target.
// Other local endpoints and native Ollama remain deliberately keyless.
sameGateway := cfg.Provider() != "ollama" && cfg.Format == active.Format && cfg.BaseURL == active.BaseURL && cfg.Proxy == active.Proxy
- if (cfg.Provider() == opencodesdk.Format && sdkCredentialCompatible(cfg, active.Provider(), active.BaseURL)) || (cfg.Provider() != opencodesdk.Format && active.Provider() != opencodesdk.Format && (!cfg.Ready() || sameGateway)) {
+ if profileCredentialCompatible(cfg, active.Provider(), active.BaseURL) && (cfg.Provider() == opencodesdk.Format || sameGateway || !cfg.Ready()) {
cfg.APIKey = active.APIKey
}
}
diff --git a/server/server_mgmt.go b/server/server_mgmt.go
index 54fcb16f..7961e020 100644
--- a/server/server_mgmt.go
+++ b/server/server_mgmt.go
@@ -1866,6 +1866,10 @@ func (s *Server) pgSaveProfile(w http.ResponseWriter, r *http.Request) {
p.MaxTokensField = ""
}
id, err := pg.SaveProfile(&p)
+ if errors.Is(err, db.ErrActiveLLMProfileKeyRequired) {
+ writeError(w, http.StatusBadRequest, err)
+ return
+ }
if err != nil {
writeError(w, 500, err)
return
@@ -2050,7 +2054,7 @@ func (s *Server) pgListModels(w http.ResponseWriter, r *http.Request) {
apiKey := strings.TrimSpace(req.APIKey)
if apiKey == "" && req.ProfileID != nil {
if p, err := s.m.pg.ProfileByID(*req.ProfileID); err == nil && p != nil {
- if (req.Provider != "ollama" || p.Format == "ollama") && sdkCredentialCompatible(agent.ConfigFrom(req.Provider, "", req.BaseURL, "", req.Proxy), p.Format, p.BaseURL) {
+ if (req.Provider != "ollama" || p.Format == "ollama") && profileCredentialCompatible(agent.ConfigFrom(req.Provider, "", req.BaseURL, "", req.Proxy), p.Format, p.BaseURL) {
apiKey = p.APIKey
}
}
diff --git a/web/src/app/(main)/system/llm/page.tsx b/web/src/app/(main)/system/llm/page.tsx
index f2da9f99..a3e06d05 100644
--- a/web/src/app/(main)/system/llm/page.tsx
+++ b/web/src/app/(main)/system/llm/page.tsx
@@ -29,7 +29,7 @@ import { Separator } from "@/components/ui/separator";
import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle } from "@/components/ui/sheet";
import { Switch } from "@/components/ui/switch";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
-import { applyLLMProfilePreset, canReuseSDKBridgeToken, getLLMProfilePreset, isCloudEconomyPreset, isOllamaPreset, selectInstalledOllamaModel, type LLMProfilePresetId } from "@/lib/llm-profile-presets";
+import { applyLLMProfilePreset, canReuseSDKBridgeToken, crossesCheaperInferenceBoundary, getLLMProfilePreset, isCloudEconomyPreset, isOllamaPreset, requiresExplicitGatewayKey, selectInstalledOllamaModel, type LLMProfilePresetId } from "@/lib/llm-profile-presets";
import { api } from "@/lib/api";
import { pullCommand } from "@/lib/ollama-pull";
import type { LLMModelDetail, LLMPoolMember, LLMPoolStatus, LLMProfile, LLMRetryOverride, LLMTestResult } from "@/lib/types";
@@ -74,6 +74,7 @@ const CLOUD_PRESETS = [
{ id: "kimi", key: "providers.kimi", help: "providers.kimiHelp" },
{ id: "openrouter", key: "providers.openrouter", help: "providers.openrouterHelp" },
{ id: "opencode-go", key: "providers.opencodeGo", help: "providers.opencodeGoHelp" },
+ { id: "cheaper-inference", key: "providers.cheaperInference", help: "providers.cheaperInferenceHelp" },
] as const;
const OLLAMA_PRESETS = [
{ id: "ollama-host", key: "providers.ollamaHost", help: "providers.ollamaHostHelp" },
@@ -425,7 +426,8 @@ function ProfileSheet({
function applyPreset(id: string) {
if (!isNew) return;
invalidateModels();
- const next = applyLLMProfilePreset({ name, format, api_key: apiKey, proxy, session_header_key: sessionHeaderKey }, id);
+ // The current base URL lets the helper drop a key that would cross the Cheaper Inference gateway boundary.
+ const next = applyLLMProfilePreset({ name, format, base_url: baseUrl, api_key: apiKey, proxy, session_header_key: sessionHeaderKey }, id);
setPreset(id as LLMProfilePresetId);
setApiKey(next.api_key);
setFormat(next.format);
@@ -457,8 +459,18 @@ function ProfileSheet({
const detailOf = (m: string) => modelDetails.find((d) => (d.name ?? d.model) === m || d.model === m);
// Warn only for explicit metadata: the model list or a native test reported capabilities without tools.
const isSDK = format === "opencode-sdk";
+ const storedBaseUrl = profile?.base_url ?? "";
const retainedKeyHint = isSDK || profile?.format === "opencode-sdk"
- ? (canReuseSDKBridgeToken(profile, format, baseUrl) ? keyHint : "") : keyHint;
+ ? (canReuseSDKBridgeToken(profile, format, baseUrl) ? keyHint : "")
+ : crossesCheaperInferenceBoundary(storedBaseUrl, baseUrl) ? "" : keyHint;
+ // A blank key would let the server fall back to a stored or active key for another destination.
+ // Native Ollama and the SDK bridge keep their own credential isolation.
+ function gatewayKeyMissing(target: { format: string; baseUrl: string; apiKey: string } = { format, baseUrl, apiKey }) {
+ if (target.format === "ollama" || target.format === "opencode-sdk") return false;
+ if (!requiresExplicitGatewayKey(storedBaseUrl, target.baseUrl, target.apiKey)) return false;
+ toast.error(swt("interface.m1335"));
+ return true;
+ }
const keyPlaceholder = isSDK ? swt("providers.sdkTokenPlaceholder") : format === "ollama" ? swt("providers.keyOptional") : "sk-…";
const pull = diag && !diag.ok ? pullCommand(diag.error ?? "", model) : "";
const testedModel = diag?.ok && diag.model === model.trim() ? diag : undefined;
@@ -501,6 +513,7 @@ function ProfileSheet({
target = { format, baseUrl, apiKey, proxy },
selectDefault = false,
) {
+ if (gatewayKeyMissing(target)) return;
const request = ++modelsRequest.current;
setLoadingModels(true);
setModelsError("");
@@ -534,7 +547,7 @@ function ProfileSheet({
}
async function testConnection() {
- if (testing) return;
+ if (testing || gatewayKeyMissing()) return;
setTesting(true);
setDiag(null);
try {
@@ -555,6 +568,7 @@ function ProfileSheet({
context_window_k: Number(cw) || 0,
keep_alive: keepAlive.trim(),
max_tokens: Math.max(0, Number(maxTokens) || 0),
+ max_tokens_field: format === "openai" ? toStore(maxTokensField) : undefined,
llm_max_concurrent: Math.max(0, Number(maxConcurrent) || 0),
},
);
@@ -577,7 +591,7 @@ function ProfileSheet({
toast.error(swt("interface.m1379"));
return;
}
- if (saving) return;
+ if (saving || gatewayKeyMissing()) return;
setSaving(true);
try {
const { id } = await api.saveLLMProfile({
@@ -819,7 +833,7 @@ function ProfileSheet({
className="font-mono"
placeholder={isSDK ? "http://127.0.0.1:4097" : "https://api.openai.com/v1"}
value={baseUrl}
- onChange={(e) => { invalidateModels(); if (isSDK) setApiKey(""); setBaseUrl(e.target.value); }}
+ onChange={(e) => { invalidateModels(); if (isSDK || crossesCheaperInferenceBoundary(baseUrl, e.target.value)) setApiKey(""); setBaseUrl(e.target.value); }}
/>
diff --git a/web/src/i18n/i18n.test.mjs b/web/src/i18n/i18n.test.mjs
index a07bfe47..ead0e079 100644
--- a/web/src/i18n/i18n.test.mjs
+++ b/web/src/i18n/i18n.test.mjs
@@ -95,8 +95,9 @@ function checkCatalog(source, translated, locale) {
test("every shipped catalog is complete and preserves placeholders and rich tags", () => {
// 3271 existing six-language keys + 12 OpenCode SDK bridge labels and guidance
- // + 3 unfinished-worker soft-delete fragments (#62).
- assert.equal(Object.keys(catalogs.en).length, 3286);
+ // + 3 unfinished-worker soft-delete fragments (#62)
+ // + 2 Cheaper Inference template keys (providers.cheaperInference, providers.cheaperInferenceHelp).
+ assert.equal(Object.keys(catalogs.en).length, 3288);
assert.deepEqual(catalogLocales.sort(), [...LOCALES].sort());
assert.deepEqual(Object.keys(catalogs).sort(), [...LOCALES].sort());
for (const locale of LOCALES) checkCatalog(catalogs.en, catalogs[locale], locale);
diff --git a/web/src/i18n/messages/es.ts b/web/src/i18n/messages/es.ts
index d6de3102..a780857e 100644
--- a/web/src/i18n/messages/es.ts
+++ b/web/src/i18n/messages/es.ts
@@ -16,6 +16,7 @@ export const providers: Record = {
"kimi": "Moonshot Kimi · kimi-k3 (económico)",
"openrouter": "OpenRouter · DeepSeek Flash (económico)",
"opencodeGo": "OpenCode Go · glm-5.3-flash",
+ "cheaperInference": "Cheaper Inference · gpt-5.4-mini",
"deepseekHelp": "Valores económicos opcionales: esfuerzo de razonamiento low, límite de salida de 16,384 tokens enviado como max_tokens y ventana de contexto de 0 (200K por defecto). El pensamiento mantiene el valor predeterminado de DeepSeek. El ahorro no se ha medido; aumenta el esfuerzo o el límite si los resultados se cortan o son demasiado superficiales.",
"claudeHelp": "Valores económicos opcionales: pensamiento desactivado, esfuerzo low, límite de salida de 16,384 tokens y ventana de contexto de 0 (200K por defecto). Se desactiva el pensamiento para ahorrar tokens de salida; elige Adaptativo o No enviar para tareas más difíciles (Sonnet y Opus 5.5 necesitan una de estas opciones). El ahorro no se ha medido.",
"openaiHelp": "Valores económicos opcionales para Responses API: esfuerzo de razonamiento low, límite de salida de 16,384 tokens enviado como max_output_tokens y ventana de contexto de 0 (200K por defecto). El ahorro no se ha medido; aumenta el esfuerzo si los resultados son demasiado superficiales.",
@@ -23,6 +24,7 @@ export const providers: Record = {
"kimiHelp": "Valores económicos opcionales: esfuerzo de razonamiento low, límite de salida de 16,384 tokens enviado como max_completion_tokens y ventana de contexto de 0 (200K por defecto). El ahorro no se ha medido; aumenta el esfuerzo si los resultados son demasiado superficiales.",
"openrouterHelp": "DeepSeek V4.1 Flash a través de OpenRouter. Esfuerzo low, límite de salida de 16,384 tokens enviado como max_tokens y ventana de contexto de 0 (200K por defecto). OpenRouter almacena automáticamente en caché los prefijos repetidos. El ahorro no se ha medido.",
"opencodeGoHelp": "La pasarela de suscripción OpenCode Go (https://opencode.ai/zen/go/v1, compatible con OpenAI). Esfuerzo de razonamiento max, límite de salida de 16,384 tokens enviado como max_tokens y ventana de contexto de 0 (200K por defecto). La pasarela rechaza las solicitudes sin la cabecera x-opencode-session, por lo que esta plantilla rellena el campo Cabecera de sesión. Usa tu clave de suscripción de OpenCode (OPENCODE_API_KEY en opencode); está sujeta a las condiciones de suscripción de opencode: confirma que se permite usarla desde ScopeWeaver antes de guardar.",
+ "cheaperInferenceHelp": "Pasarela compatible con OpenAI de Cheaper Inference (https://api.cheaperinference.com/v1). Esfuerzo de razonamiento omitido (predeterminado del modelo), límite de salida de 16,384 tokens enviado como max_completion_tokens y ventana de contexto de 0 (200K por defecto). Cargar modelos muestra los demás ID de modelo disponibles para tu clave. Al elegir esta plantilla, o al cambiar la URL base hacia o desde la pasarela, se borra la clave introducida para otro endpoint; introduce explícitamente tu clave de API de Cheaper Inference.",
opencodeSdk: "OpenCode SDK · puente",
sdkGroup: "Puente SDK",
sdkHelp: "Entornos del puente: CLI/cliente native 2.3.0 (predeterminado; conserva los argumentos originales de las herramientas) o SDK legacy 1.19.4 (JSON con estructura segura; no conserva los bytes originales). Ninguno es compatible con Windows. ScopeWeaver mantiene el control de la ejecución y aprobación de las herramientas del sistema anfitrión.",
diff --git a/web/src/i18n/messages/ja.ts b/web/src/i18n/messages/ja.ts
index 489430d3..bdc3c766 100644
--- a/web/src/i18n/messages/ja.ts
+++ b/web/src/i18n/messages/ja.ts
@@ -3392,6 +3392,7 @@ export const providers: Record = {
kimi: "Moonshot Kimi · kimi-k3(節約)",
openrouter: "OpenRouter · DeepSeek Flash(節約)",
opencodeGo: "OpenCode Go · glm-5.3-flash",
+ cheaperInference: "Cheaper Inference · gpt-5.4-mini",
deepseekHelp:
"選択した場合の節約用既定値: 推論強度 low、max_tokens として送信する出力上限 16,384 トークン、コンテキストウィンドウ 0(既定 200K)。思考機能は DeepSeek の既定値のままです。削減効果は未測定です。結果が途中で切れる場合や内容が浅い場合は、推論強度や上限を上げてください。",
claudeHelp:
@@ -3406,6 +3407,8 @@ export const providers: Record = {
"OpenRouter 経由の DeepSeek V4.1 Flash です。推論強度 low、max_tokens として送信する出力上限 16,384 トークン、コンテキストウィンドウ 0(既定 200K)を使用します。OpenRouter は繰り返されるプレフィックスを自動でキャッシュします。削減効果は未測定です。",
opencodeGoHelp:
"OpenCode Go のサブスクリプションゲートウェイ(https://opencode.ai/zen/go/v1、OpenAI 互換)です。推論強度 max、max_tokens として送信する出力上限 16,384 トークン、コンテキストウィンドウ 0(既定 200K)を使用します。ゲートウェイは x-opencode-session ヘッダーのないリクエストを拒否するため、このテンプレートが「セッションヘッダー」欄を設定します。OpenCode のサブスクリプションキー(opencode の OPENCODE_API_KEY)を使用します。このキーは opencode 独自のサブスクリプション規約に従うため、保存前に ScopeWeaver からの利用が認められることを確認してください。",
+ cheaperInferenceHelp:
+ "Cheaper Inference の OpenAI 互換ゲートウェイ(https://api.cheaperinference.com/v1)。推論強度は省略(モデルの既定値)、出力上限 16,384 トークン(max_completion_tokens として送信)、コンテキストウィンドウ 0(既定 200K)。「モデルを読み込む」で、このキーで使える他のモデル ID を表示します。このテンプレートを選ぶか、ベース URL をゲートウェイへ、またはゲートウェイから変更すると、別のエンドポイント用に入力したキーは消去されます。Cheaper Inference の API キーを明示的に入力してください。",
opencodeSdk: "OpenCode SDK · ブリッジ",
sdkGroup: "SDK ブリッジ",
sdkHelp: "ブリッジの実行環境は native CLI/クライアント 2.3.0(既定、ツール引数の原文を保持)または legacy SDK 1.19.4(安全な構造の JSON。元のバイト列は保持しません)です。どちらも Windows には対応していません。ホスト側のツール実行と承認は引き続き ScopeWeaver が管理します。",
diff --git a/web/src/i18n/messages/providers.ts b/web/src/i18n/messages/providers.ts
index f4cb8be6..4b86b47c 100644
--- a/web/src/i18n/messages/providers.ts
+++ b/web/src/i18n/messages/providers.ts
@@ -14,6 +14,7 @@ export const en = {
kimi: "Moonshot Kimi · kimi-k3 (economy)",
openrouter: "OpenRouter · DeepSeek Flash (economy)",
opencodeGo: "OpenCode Go · glm-5.3-flash",
+ cheaperInference: "Cheaper Inference · gpt-5.4-mini",
deepseekHelp: "Opt-in economy defaults: reasoning effort low, output limit 16,384 tokens sent as max_tokens, context window 0 (200K default). Thinking stays at the DeepSeek default. Savings are not measured; raise effort or the limit if results are cut off or too shallow.",
claudeHelp: "Opt-in economy defaults: thinking disabled, effort low, output limit 16,384 tokens, context window 0 (200K default). Thinking is off to save output tokens; choose Adaptive or Do not send for harder tasks (Sonnet and Opus 5.5 need one of these). Savings are not measured.",
openaiHelp: "Opt-in economy defaults on the Responses API: reasoning effort low, output limit 16,384 tokens sent as max_output_tokens, context window 0 (200K default). Savings are not measured; raise effort if results are too shallow.",
@@ -21,6 +22,7 @@ export const en = {
kimiHelp: "Opt-in economy defaults: reasoning effort low, output limit 16,384 tokens sent as max_completion_tokens, context window 0 (200K default). Savings are not measured; raise effort if results are too shallow.",
openrouterHelp: "DeepSeek V4.1 Flash through OpenRouter. Low effort, output limit 16,384 tokens sent as max_tokens, context window 0 (200K default). OpenRouter caches repeated prefixes automatically. Savings are not measured.",
opencodeGoHelp: "The OpenCode Go subscription gateway (https://opencode.ai/zen/go/v1, OpenAI-compatible). Reasoning effort max, output limit 16,384 tokens sent as max_tokens, context window 0 (200K default). The gateway rejects requests without the x-opencode-session header, so this template fills the Session header field for you. Uses your OpenCode subscription key (OPENCODE_API_KEY in opencode); the key is tied to opencode's own subscription terms — confirm that using it from ScopeWeaver is permitted before saving.",
+ cheaperInferenceHelp: "Cheaper Inference, an OpenAI-compatible gateway (https://api.cheaperinference.com/v1). Reasoning effort omitted (model default), output limit 16,384 tokens sent as max_completion_tokens, context window 0 (200K default). Load models lists the other model ids available to your key. Choosing this template, or changing the Base URL to or from the gateway, clears a key entered for another endpoint; enter your Cheaper Inference API key explicitly.",
opencodeSdk: "OpenCode SDK · bridge",
sdkGroup: "SDK bridge",
sdkHelp: "Bridge runtimes: native CLI/client 2.3.0 (default; preserves raw tool arguments) or legacy SDK 1.19.4 (safe structural JSON; original bytes are not preserved). Both are unsupported on Windows. ScopeWeaver retains host tool execution and approvals.",
@@ -96,6 +98,7 @@ export const ko: Record = {
kimi: "Moonshot Kimi · kimi-k3(절약형)",
openrouter: "OpenRouter · DeepSeek Flash(절약형)",
opencodeGo: "OpenCode Go · glm-5.3-flash",
+ cheaperInference: "Cheaper Inference · gpt-5.4-mini",
deepseekHelp: "선택형 절약 기본값: 추론 강도 low, 출력 한도 16,384 토큰(max_tokens로 전송), 문맥 창 0(기본 200K). 추론 여부는 DeepSeek 기본값을 따릅니다. 절감 효과는 측정하지 않았습니다. 결과가 잘리거나 얕으면 강도나 한도를 올리세요.",
claudeHelp: "선택형 절약 기본값: 추론 끔, 강도 low, 출력 한도 16,384 토큰, 문맥 창 0(기본 200K). 출력 토큰을 줄이려고 추론을 끕니다. 어려운 작업에는 적응형 또는 보내지 않음을 고르세요(Sonnet·Opus 5.5는 둘 중 하나 필요). 절감 효과는 측정하지 않았습니다.",
openaiHelp: "Responses API용 선택형 절약 기본값: 추론 강도 low, 출력 한도 16,384 토큰(max_output_tokens로 전송), 문맥 창 0(기본 200K). 절감 효과는 측정하지 않았습니다. 결과가 얕으면 강도를 올리세요.",
@@ -103,6 +106,7 @@ export const ko: Record = {
kimiHelp: "선택형 절약 기본값: 추론 강도 low, 출력 한도 16,384 토큰(max_completion_tokens로 전송), 문맥 창 0(기본 200K). 절감 효과는 측정하지 않았습니다. 결과가 얕으면 강도를 올리세요.",
openrouterHelp: "OpenRouter를 통한 DeepSeek V4.1 Flash입니다. 강도 low, 출력 한도 16,384 토큰(max_tokens로 전송), 문맥 창 0(기본 200K). OpenRouter는 반복되는 앞부분을 자동으로 캐시합니다. 절감 효과는 측정하지 않았습니다.",
opencodeGoHelp: "OpenCode Go 구독 게이트웨이(https://opencode.ai/zen/go/v1, OpenAI 호환)입니다. 추론 강도 max, 출력 한도 16,384 토큰(max_tokens로 전송), 문맥 창 0(기본 200K). 게이트웨이는 x-opencode-session 헤더가 없는 요청을 거부하므로 이 템플릿이 세션 헤더 필드를 채워 줍니다. opencode의 구독 키(OPENCODE_API_KEY)를 사용합니다. 키는 opencode 구독 약관에 묶여 있으므로 ScopeWeaver에서 사용해도 되는지 먼저 확인하세요.",
+ cheaperInferenceHelp: "Cheaper Inference OpenAI 호환 게이트웨이(https://api.cheaperinference.com/v1)입니다. 추론 강도 생략(모델 기본값), 출력 한도 16,384 토큰(max_completion_tokens로 전송), 문맥 창 0(기본 200K). 모델 불러오기는 키로 사용할 수 있는 다른 모델 ID를 보여 줍니다. 이 템플릿을 고르거나 기본 URL을 게이트웨이로 또는 게이트웨이에서 바꾸면 다른 엔드포인트용으로 입력한 키가 지워지므로 Cheaper Inference API 키를 직접 입력하세요.",
opencodeSdk: "OpenCode SDK · 브리지",
sdkGroup: "SDK 브리지",
sdkHelp: "브리지 런타임은 native CLI·클라이언트 2.3.0(기본값, 도구 인자 원본 보존) 또는 legacy SDK 1.19.4(안전한 구조의 JSON, 원본 바이트는 보존하지 않음)입니다. 둘 다 Windows는 지원하지 않습니다. 호스트 도구 실행과 승인은 ScopeWeaver가 관리합니다.",
diff --git a/web/src/i18n/messages/zh-hans.ts b/web/src/i18n/messages/zh-hans.ts
index cfffb643..f99f4c8c 100644
--- a/web/src/i18n/messages/zh-hans.ts
+++ b/web/src/i18n/messages/zh-hans.ts
@@ -3295,6 +3295,7 @@ export const providers: Record = {
kimi: "月之暗面 Kimi · kimi-k3(经济型)",
openrouter: "OpenRouter · DeepSeek Flash(经济型)",
opencodeGo: "OpenCode Go · glm-5.3-flash(订阅网关)",
+ cheaperInference: "Cheaper Inference · gpt-5.4-mini(OpenAI 兼容网关)",
deepseekHelp:
"可选的经济型默认值:推理强度 low,输出上限 16,384 token(以 max_tokens 发送),上下文窗口 0(默认 200K)。是否思考沿用 DeepSeek 默认。节省效果未经测量;结果被截断或过浅时请提高强度或上限。",
claudeHelp:
@@ -3309,6 +3310,8 @@ export const providers: Record = {
"通过 OpenRouter 使用 DeepSeek V4.1 Flash。强度 low,输出上限 16,384 token(以 max_tokens 发送),上下文窗口 0(默认 200K)。OpenRouter 会自动缓存重复的前缀。节省效果未经测量。",
opencodeGoHelp:
"OpenCode Go 订阅网关(https://opencode.ai/zen/go/v1,OpenAI 兼容)。推理强度 max,输出上限 16,384 token(以 max_tokens 发送),上下文窗口 0(默认 200K)。网关会拒绝缺少 x-opencode-session 标头的请求,因此该模板会自动填写“会话标头”字段。使用你的 OpenCode 订阅密钥(opencode 中的 OPENCODE_API_KEY)。该密钥受 opencode 订阅条款约束,保存前请确认允许在 ScopeWeaver 中使用。",
+ cheaperInferenceHelp:
+ "Cheaper Inference OpenAI 兼容网关(https://api.cheaperinference.com/v1)。省略推理强度(模型默认),输出上限 16,384 token(以 max_completion_tokens 发送),上下文窗口 0(默认 200K)。“加载模型”会列出该密钥可用的其他模型 ID。选择此模板,或将 Base URL 改为网关或从网关改为其他地址时,会清除为其他端点输入的密钥;请明确输入你的 Cheaper Inference API 密钥。",
opencodeSdk: "OpenCode SDK · 桥接服务",
sdkGroup: "SDK 桥接服务",
sdkHelp: "桥接运行时:native CLI/客户端 2.3.0(默认,保留原始工具参数)或 legacy SDK 1.19.4(安全的结构化 JSON,不保留原始字节)。两者均不支持 Windows。主机工具执行和审批仍由 ScopeWeaver 管理。",
diff --git a/web/src/i18n/messages/zh-hant.ts b/web/src/i18n/messages/zh-hant.ts
index 69c6ec43..4d724904 100644
--- a/web/src/i18n/messages/zh-hant.ts
+++ b/web/src/i18n/messages/zh-hant.ts
@@ -3295,6 +3295,7 @@ export const providers: Record = {
kimi: "月之暗面 Kimi · kimi-k3(經濟型)",
openrouter: "OpenRouter · DeepSeek Flash(經濟型)",
opencodeGo: "OpenCode Go · glm-5.3-flash(訂閱閘道器)",
+ cheaperInference: "Cheaper Inference · gpt-5.4-mini(OpenAI 相容閘道器)",
deepseekHelp:
"可選的經濟型預設值:推理強度 low,輸出上限 16,384 token(以 max_tokens 傳送),上下文視窗 0(預設 200K)。是否思考沿用 DeepSeek 預設。節省效果未經測量;結果被截斷或過淺時請提高強度或上限。",
claudeHelp:
@@ -3309,6 +3310,8 @@ export const providers: Record = {
"透過 OpenRouter 使用 DeepSeek V4.1 Flash。強度 low,輸出上限 16,384 token(以 max_tokens 傳送),上下文視窗 0(預設 200K)。OpenRouter 會自動快取重複的字首。節省效果未經測量。",
opencodeGoHelp:
"OpenCode Go 訂閱閘道器(https://opencode.ai/zen/go/v1,OpenAI 相容)。推理強度 max,輸出上限 16,384 token(以 max_tokens 傳送),上下文視窗 0(預設 200K)。閘道器會拒絕缺少 x-opencode-session 標頭的請求,因此該模板會自動填寫“工作階段標頭”欄位。使用你的 OpenCode 訂閱金鑰(opencode 中的 OPENCODE_API_KEY)。該金鑰受 opencode 訂閱條款約束,儲存前請確認允許在 ScopeWeaver 中使用。",
+ cheaperInferenceHelp:
+ "Cheaper Inference OpenAI 相容閘道器(https://api.cheaperinference.com/v1)。省略推理強度(模型預設),輸出上限 16,384 token(以 max_completion_tokens 傳送),上下文視窗 0(預設 200K)。「載入模型」會列出此金鑰可用的其他模型 ID。選擇此模板,或將 Base URL 改為閘道器或從閘道器改為其他位址時,會清除為其他端點輸入的金鑰;請明確輸入你的 Cheaper Inference API 金鑰。",
opencodeSdk: "OpenCode SDK · 橋接服務",
sdkGroup: "SDK 橋接服務",
sdkHelp: "橋接執行階段:native CLI/用戶端 2.3.0(預設,保留原始工具引數)或 legacy SDK 1.19.4(安全的結構化 JSON,不保留原始位元組)。兩者均不支援 Windows。主機工具執行與核准仍由 ScopeWeaver 管理。",
diff --git a/web/src/lib/api.ts b/web/src/lib/api.ts
index 36cf88da..7fb98197 100644
--- a/web/src/lib/api.ts
+++ b/web/src/lib/api.ts
@@ -964,12 +964,17 @@ export const api = {
profile_id?: number,
streaming = true, // Test the profile's actual transport mode so nonstreaming failures are caught before conversations.
session_header_key = "", // Nonempty sends this custom header on test requests with a one-time session ID.
- // Native Ollama uses these for num_ctx, residency and num_predict; other formats ignore them.
- native: { context_window_k?: number; keep_alive?: string; max_tokens?: number; llm_max_concurrent?: number } = {},
+ options: {
+ context_window_k?: number;
+ keep_alive?: string;
+ max_tokens?: number;
+ max_tokens_field?: string;
+ llm_max_concurrent?: number;
+ } = {},
) =>
// reply contains truncated actual model output; an entirely empty response fails the backend test.
post("/llm/test", {
- ...native,
+ ...options,
provider,
model,
base_url,
diff --git a/web/src/lib/llm-profile-presets.test.mjs b/web/src/lib/llm-profile-presets.test.mjs
index 0fc9eed0..2b246c5c 100644
--- a/web/src/lib/llm-profile-presets.test.mjs
+++ b/web/src/lib/llm-profile-presets.test.mjs
@@ -1,6 +1,6 @@
import assert from "node:assert/strict";
import test from "node:test";
-import { applyLLMProfilePreset, canReuseSDKBridgeToken, getLLMProfilePreset, isOllamaPreset, selectInstalledOllamaModel, LLM_PROFILE_PRESET_IDS } from "./llm-profile-presets.ts";
+import { applyLLMProfilePreset, canReuseSDKBridgeToken, crossesCheaperInferenceBoundary, getLLMProfilePreset, isOllamaPreset, requiresExplicitGatewayKey, selectInstalledOllamaModel, LLM_PROFILE_PRESET_IDS } from "./llm-profile-presets.ts";
test("OpenCode SDK template isolates bridge credentials and waits for explicit model discovery", () => {
const draft = Object.freeze({ name: "SDK", api_key: "unsaved-cloud-key", proxy: "http://proxy:8080", session_header_key: "x-opencode-session", streaming: false, rate_per_minute: 7 });
@@ -66,6 +66,8 @@ test("cloud economy templates fill the documented endpoint, model and opt-in out
// The opencode-go gateway template deliberately fills effort max at the user's request
// and the required x-opencode-session header (HTTP 400 MissingSessionID without it).
"opencode-go": ["openai", "https://opencode.ai/zen/go/v1", "glm-5.3-flash", "", "max", "", "x-opencode-session"],
+ // The cheaper-inference gateway template omits effort (model default) and sends max_completion_tokens.
+ "cheaper-inference": ["openai", "https://api.cheaperinference.com/v1", "gpt-5.4-mini", "", "", "max_completion_tokens"],
};
for (const [id, [format, baseURL, model, thinking, effort, field, sessionHeader]] of Object.entries(expected)) {
assert.ok(LLM_PROFILE_PRESET_IDS.includes(id), id);
@@ -87,6 +89,7 @@ test("cloud economy templates fill the documented endpoint, model and opt-in out
test("cloud economy templates keep the entered key and preferences, and switching between them changes only template fields", () => {
const draft = Object.freeze({ name: "Economy", api_key: "user-entered-key", proxy: "http://proxy:8080", session_header_key: "x-session", streaming: false, rate_per_minute: 7 });
let previous = draft;
+ // The Cheaper Inference gateway is excluded: crossing into or out of it clears the key (tested below).
for (const id of ["deepseek", "claude", "openai", "qwen", "kimi", "openrouter", "zai-general", "deepseek"]) {
const next = applyLLMProfilePreset(previous, id);
for (const key of Object.keys(draft)) assert.equal(next[key], draft[key], `${id} keeps ${key}`);
@@ -100,12 +103,65 @@ test("cloud economy templates keep the entered key and preferences, and switchin
for (const key of ["name", "api_key", "proxy", "streaming", "rate_per_minute"]) assert.equal(oc[key], draft[key], `opencode-go keeps ${key}`);
assert.equal(oc.session_header_key, "x-opencode-session", "opencode-go fills its required session header");
assert.deepEqual({ ...oc }, { ...draft, ...getLLMProfilePreset("opencode-go") }, "opencode-go leaves no fields from the previous template");
- for (const id of ["deepseek", "claude", "openai", "qwen", "kimi", "openrouter", "opencode-go"]) {
+ for (const id of ["deepseek", "claude", "openai", "qwen", "kimi", "openrouter", "opencode-go", "cheaper-inference"]) {
const preset = getLLMProfilePreset(id);
for (const key of ["api_key", "keep_alive", "llm_max_concurrent"]) assert.equal(key in preset, false, `${id} never sets ${key}`);
}
});
+test("the Cheaper Inference template never carries a key across the gateway boundary", () => {
+ const gateway = getLLMProfilePreset("cheaper-inference").base_url;
+ const base = Object.freeze({ name: "Gateway", proxy: "http://proxy:8080", session_header_key: "x-session", streaming: false, rate_per_minute: 7 });
+ // Into the gateway: a key entered for another provider (or an unknown previous destination) is cleared.
+ for (const from of ["deepseek", "openai", "openrouter", "opencode-go", "zai-general"]) {
+ const draft = Object.freeze({ ...base, api_key: "synthetic-other-provider-key", base_url: getLLMProfilePreset(from).base_url });
+ const next = applyLLMProfilePreset(draft, "cheaper-inference");
+ assert.equal(next.api_key, "", `${from} -> cheaper-inference clears the key`);
+ assert.equal(next.base_url, gateway);
+ for (const key of ["name", "proxy", "streaming", "rate_per_minute"]) assert.equal(next[key], draft[key], `${from} -> cheaper-inference keeps ${key}`);
+ assert.equal(draft.api_key, "synthetic-other-provider-key", "the original draft is not mutated");
+ }
+ for (const previousURL of ["", "https://api.example.invalid/v1", "http://api.cheaperinference.com/v1", "https://api.cheaperinference.com:8443/v1", "https://api.cheaperinference.com.example.invalid/v1", "https://api.cheaperinference.com@example.invalid/v1"]) {
+ assert.equal(applyLLMProfilePreset({ ...base, api_key: "synthetic-key", base_url: previousURL }, "cheaper-inference").api_key, "", `from ${previousURL || "blank"}`);
+ }
+ assert.equal(applyLLMProfilePreset({ ...base, api_key: "synthetic-key" }, "cheaper-inference").api_key, "", "a draft without base_url counts as an unknown destination");
+ // Out of the gateway: the gateway key is cleared for every other template.
+ for (const to of LLM_PROFILE_PRESET_IDS.filter((id) => id !== "cheaper-inference")) {
+ const next = applyLLMProfilePreset(Object.freeze({ ...base, format: "openai", api_key: "synthetic-gateway-key", base_url: gateway }), to);
+ assert.equal(next.api_key, "", `cheaper-inference -> ${to} clears the key`);
+ }
+ // Same effective destination: re-selecting the template or a path-only change keeps the entered key.
+ for (const sameURL of [gateway, "https://api.cheaperinference.com/v1/", " HTTPS://API.CHEAPERINFERENCE.COM/v1 ", "https://api.cheaperinference.com:443/other"]) {
+ const next = applyLLMProfilePreset(Object.freeze({ ...base, api_key: "synthetic-gateway-key", base_url: sameURL }), "cheaper-inference");
+ assert.equal(next.api_key, "synthetic-gateway-key", `same gateway origin ${sameURL} keeps the key`);
+ }
+ // Unrelated cloud switches keep the legacy behavior.
+ const legacy = applyLLMProfilePreset({ ...base, api_key: "synthetic-key", base_url: getLLMProfilePreset("deepseek").base_url }, "openai");
+ assert.equal(legacy.api_key, "synthetic-key");
+});
+
+test("editing the Base URL or relying on a stored key never crosses the Cheaper Inference gateway boundary", () => {
+ const gateway = "https://api.cheaperinference.com/v1";
+ const other = "https://api.example.invalid/v1";
+ assert.equal(crossesCheaperInferenceBoundary(other, gateway), true, "into the gateway");
+ assert.equal(crossesCheaperInferenceBoundary(gateway, other), true, "out of the gateway");
+ assert.equal(crossesCheaperInferenceBoundary("", gateway), true, "blank to the gateway");
+ assert.equal(crossesCheaperInferenceBoundary(gateway, "https://api.cheaperinference.co"), true, "a partially edited host leaves the gateway");
+ assert.equal(crossesCheaperInferenceBoundary(gateway, "http://api.cheaperinference.com/v1"), true, "a scheme change is a different origin");
+ assert.equal(crossesCheaperInferenceBoundary(gateway, "https://api.cheaperinference.com/v1/models"), false, "same origin");
+ assert.equal(crossesCheaperInferenceBoundary(gateway, "https://api.cheaperinference.com./v1"), false, "trailing host dot");
+ assert.equal(crossesCheaperInferenceBoundary(other, "https://api.deepseek.com"), false, "unrelated switches keep legacy behavior");
+ assert.equal(crossesCheaperInferenceBoundary("", ""), false);
+ // A blank key field would fall back to a stored or active key on the server.
+ assert.equal(requiresExplicitGatewayKey("", gateway, ""), true, "new gateway profile needs an entered key");
+ assert.equal(requiresExplicitGatewayKey("", gateway, " "), true);
+ assert.equal(requiresExplicitGatewayKey("", gateway, "synthetic-gateway-key"), false);
+ assert.equal(requiresExplicitGatewayKey(other, gateway, ""), true, "stored other-provider key never reaches the gateway");
+ assert.equal(requiresExplicitGatewayKey(gateway, other, ""), true, "stored gateway key never reaches another provider");
+ assert.equal(requiresExplicitGatewayKey(gateway, "https://api.cheaperinference.com/v1/", ""), false, "same gateway keeps its stored key");
+ assert.equal(requiresExplicitGatewayKey(other, "https://api.deepseek.com", ""), false, "unrelated edits keep legacy fallback");
+});
+
test("applying a template preserves entered secrets and unrelated draft preferences", () => {
const draft = Object.freeze({ name: "My profile", api_key: "user-entered-key", proxy: "http://user:pass@proxy:8080", username: "operator", session_header_key: "x-session", streaming: false, rate_per_minute: 7, model: "custom-model", base_url: "https://previous.example" });
const next = applyLLMProfilePreset(draft, "zai-general");
diff --git a/web/src/lib/llm-profile-presets.ts b/web/src/lib/llm-profile-presets.ts
index 1cf2647c..463a7b2b 100644
--- a/web/src/lib/llm-profile-presets.ts
+++ b/web/src/lib/llm-profile-presets.ts
@@ -14,6 +14,7 @@ export const LLM_PROFILE_PRESET_IDS = [
"kimi",
"openrouter",
"opencode-go",
+ "cheaper-inference",
"opencode-sdk",
"ollama-host",
"ollama-docker-host",
@@ -36,7 +37,8 @@ export interface ZaiPresetFields {
// Opt-in economy defaults: low effort and a 16384-token output cap. context_window_k 0 keeps
// the 200K compaction default. Empty thinking/effort values omit the request field.
-// The opencode-go gateway template deliberately selects effort max at the user's request;
+// The opencode-go gateway template deliberately selects effort max at the user's request,
+// and the cheaper-inference gateway template omits effort so the model default applies;
// every other cloud template keeps the measured economy low default.
export interface CloudPresetFields {
format: "openai" | "anthropic" | "openai-responses";
@@ -111,6 +113,11 @@ const CLOUD_TARGETS: Record = {
// The gateway rejects requests without x-opencode-session (HTTP 400 MissingSessionID), so the
// template fills the required session header.
"opencode-go": { format: "openai", base_url: "https://opencode.ai/zen/go/v1", model: "glm-5.3-flash", thinking_type: "", reasoning_effort: "max", max_tokens_field: "", session_header_key: "x-opencode-session" },
+ // Cheaper Inference (OpenAI-compatible gateway) at https://api.cheaperinference.com/v1. The model id
+ // and its limits (400K context, 128K output) were checked in the public list at
+ // https://api.cheaperinference.com/public/models on 2026-11-05. GPT-5.4 models take the output
+ // limit as max_completion_tokens; effort is omitted so the model default applies.
+ "cheaper-inference": { format: "openai", base_url: "https://api.cheaperinference.com/v1", model: "gpt-5.4-mini", thinking_type: "", reasoning_effort: "", max_tokens_field: "max_completion_tokens" },
};
export function isCloudEconomyPreset(id: string): boolean {
@@ -164,8 +171,41 @@ export function getLLMProfilePreset(id: string): Readonly(draft: T, id: string): T & LLMProfilePresetFields {
@@ -173,6 +213,10 @@ export function applyLLMProfilePreset(draft: T, id: string): T
if ("format" in draft && draft.format === "opencode-sdk" && preset.format !== "opencode-sdk") {
return { ...draft, ...preset, api_key: "" };
}
+ const previousBaseUrl = "base_url" in draft && typeof draft.base_url === "string" ? draft.base_url : "";
+ if ("api_key" in draft && crossesCheaperInferenceBoundary(previousBaseUrl, preset.base_url)) {
+ return { ...draft, ...preset, api_key: "" };
+ }
return { ...draft, ...preset };
}