Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----------
Expand Down Expand Up @@ -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")
Expand All @@ -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
}
Expand All @@ -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")
)
Expand Down
74 changes: 72 additions & 2 deletions db/config_sdk_test.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions docs/es/llm-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
19 changes: 19 additions & 0 deletions docs/ja/llm-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ SCOPEWEAVER_LLM_MODEL=glm-5.3-flash

`SCOPEWEAVER_LLM_PROVIDER=opencode-go` は OpenAI Chat Completions の通信形式に対応し、ベース URL とモデルが未設定なら上記表の既定値を使います。

<a id="cheaper-inference-openai-compatible-gateway"></a>
## 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 の経路を使い、確認はテンプレートの単体テストのみで、実際の呼び出しでは確認していません。

<a id="controlling-token-use"></a>
## トークン使用量の管理

Expand Down
18 changes: 18 additions & 0 deletions docs/ko/llm-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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의 보호 장치는 [아래](#실행-중-보호-장치)에 따로 설명합니다.
Expand Down
18 changes: 18 additions & 0 deletions docs/llm-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Loading
Loading