Compare commits

...
Author SHA1 Message Date
multica-builder 786b349752 feat: default UI language to zh and add LLM connection test
- First-open default language is now Simplified Chinese (i18n.svelte.ts);
  stored language choice still wins.
- New POST /api/settings/llm-test endpoint probes an OpenAI-compatible
  endpoint with a 1-token chat completion; falls back to a saved profile's
  secret so keys never travel back to the browser.
- Settings LLM cards gain a connection-test button with readable
  success/failure feedback (auth / timeout / connect / HTTP detail),
  localized in all seven dictionaries.
- Backend tests cover success, saved-secret reuse, 401, connection
  refused, and input validation against a local fake OpenAI server.
2026-09-27 20:04:46 +08:00
12 changed files with 385 additions and 12 deletions
@@ -1,7 +1,9 @@
from __future__ import annotations
import time
from typing import Any
import httpx
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, Field
@@ -11,6 +13,8 @@ router = APIRouter()
_LEGACY_LLM_KEYS = {"llm_base_url", "llm_model", "llm_api_key"}
_LLM_TEST_TIMEOUT_SEC = 15.0
class LlmProfileIn(BaseModel):
id: str | None = None
@@ -20,6 +24,13 @@ class LlmProfileIn(BaseModel):
api_key: str | None = None
class LlmTestIn(BaseModel):
profile_id: str | None = None
base_url: str | None = None
model: str | None = None
api_key: str | None = None
class SettingsUpdate(BaseModel):
llm_base_url: str | None = None
llm_model: str | None = None
@@ -106,3 +117,70 @@ async def update_settings(payload: SettingsUpdate) -> dict[str, Any]:
settings.save_config_file()
return settings.to_public_dict()
@router.post("/llm-test")
async def test_llm_connection(payload: LlmTestIn) -> dict[str, Any]:
settings.ensure_llm_profiles()
base_url = (payload.base_url or "").strip()
model = (payload.model or "").strip()
api_key = (payload.api_key or "").strip() or None
if payload.profile_id:
profile = next(
(p for p in settings.llm_profiles if p.get("id") == payload.profile_id), None
)
if profile is None:
raise HTTPException(status_code=404, detail="Unknown LLM profile")
if not base_url:
base_url = str(profile.get("base_url") or "").strip()
if not model:
model = str(profile.get("model") or "").strip()
if api_key is None:
key = profile.get("api_key")
if isinstance(key, str) and key.strip():
api_key = key
if not base_url or not model:
raise HTTPException(status_code=400, detail="base_url and model are required")
url = f"{base_url.rstrip('/')}/chat/completions"
headers = {"Content-Type": "application/json"}
if api_key:
headers["Authorization"] = f"Bearer {api_key}"
body = {
"model": model,
"messages": [{"role": "user", "content": "ping"}],
"max_tokens": 1,
"stream": False,
"temperature": 0,
}
started = time.perf_counter()
try:
async with httpx.AsyncClient(timeout=_LLM_TEST_TIMEOUT_SEC) as client:
resp = await client.post(url, headers=headers, json=body)
except httpx.TimeoutException:
return {
"ok": False,
"error": "timeout",
"detail": f"no response within {_LLM_TEST_TIMEOUT_SEC:.0f}s",
}
except httpx.HTTPError as exc:
return {"ok": False, "error": "connect", "detail": str(exc) or exc.__class__.__name__}
elapsed_ms = int((time.perf_counter() - started) * 1000)
if resp.status_code in (401, 403):
return {"ok": False, "error": "auth", "detail": f"HTTP {resp.status_code}"}
if resp.status_code == 404:
return {
"ok": False,
"error": "not_found",
"detail": "HTTP 404 — check base URL (OpenAI-compatible servers usually end with /v1)",
}
if resp.status_code >= 400:
text = resp.text[:200].strip().replace("\n", " ")
return {"ok": False, "error": "http", "detail": f"HTTP {resp.status_code}: {text}"}
try:
data = resp.json()
except ValueError:
return {"ok": False, "error": "http", "detail": "response is not JSON"}
if not (isinstance(data, dict) and data.get("choices")):
return {"ok": False, "error": "http", "detail": "response has no choices"}
return {"ok": True, "elapsed_ms": elapsed_ms}
@@ -0,0 +1,168 @@
"""LLM connection test endpoint: probe an OpenAI-compatible endpoint without paid keys."""
from __future__ import annotations
import json
import socket
import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import pytest
from calliope.config import settings
class _FakeOpenAIHandler(BaseHTTPRequestHandler):
server_version = "FakeOpenAI/1.0"
def do_POST(self):
length = int(self.headers.get("Content-Length") or 0)
body = json.loads(self.rfile.read(length) or b"{}")
auth = self.headers.get("Authorization") or ""
seen = {"path": self.path, "auth": auth, "body": body}
self.server.requests.append(seen)
if self.path != "/v1/chat/completions":
self._send(404, {"error": "not found"})
return
if self.server.require_auth and auth != f"Bearer {self.server.api_key}":
self._send(401, {"error": {"message": "Incorrect API key"}})
return
self._send(
200,
{
"id": "chatcmpl-test",
"object": "chat.completion",
"model": body.get("model", "fake"),
"choices": [
{"index": 0, "message": {"role": "assistant", "content": "pong"}, "finish_reason": "stop"}
],
},
)
def _send(self, status: int, payload: dict) -> None:
data = json.dumps(payload).encode("utf-8")
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
def log_message(self, format, *args):
pass
@pytest.fixture
def fake_openai():
server = ThreadingHTTPServer(("127.0.0.1", 0), _FakeOpenAIHandler)
server.requests = []
server.require_auth = False
server.api_key = ""
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
try:
yield server
finally:
server.shutdown()
server.server_close()
def _url(server) -> str:
return f"http://127.0.0.1:{server.server_address[1]}/v1"
def _snapshot() -> dict:
return {
"llm_profiles": [dict(p) for p in (settings.llm_profiles or [])],
"llm_active_id": settings.llm_active_id,
"llm_base_url": settings.llm_base_url,
"llm_model": settings.llm_model,
"llm_api_key": settings.llm_api_key,
}
def _restore(prev: dict) -> None:
for key, value in prev.items():
setattr(settings, key, value)
def test_llm_test_success(client, fake_openai):
r = client.post(
"/api/settings/llm-test",
json={"base_url": _url(fake_openai), "model": "fake-model", "api_key": "sk-test"},
)
assert r.status_code == 200
body = r.json()
assert body["ok"] is True
assert isinstance(body["elapsed_ms"], int)
assert fake_openai.requests[0]["path"] == "/v1/chat/completions"
assert fake_openai.requests[0]["auth"] == "Bearer sk-test"
assert fake_openai.requests[0]["body"]["model"] == "fake-model"
def test_llm_test_uses_saved_profile_secret(client, fake_openai):
prev = _snapshot()
fake_openai.require_auth = True
fake_openai.api_key = "sk-saved-secret"
try:
pid = "cccccccc-cccc-cccc-cccc-cccccccccccc"
r = client.post(
"/api/settings",
json={
"llm_profiles": [
{
"id": pid,
"name": "Fake",
"base_url": _url(fake_openai),
"model": "fake-model",
"api_key": "sk-saved-secret",
}
],
"llm_active_id": pid,
},
)
assert r.status_code == 200
assert r.json()["llm_profiles"][0]["api_key"] is True
r = client.post("/api/settings/llm-test", json={"profile_id": pid})
assert r.status_code == 200
assert r.json()["ok"] is True
assert fake_openai.requests[-1]["auth"] == "Bearer sk-saved-secret"
finally:
_restore(prev)
def test_llm_test_auth_rejected(client, fake_openai):
fake_openai.require_auth = True
fake_openai.api_key = "sk-right-one"
r = client.post(
"/api/settings/llm-test",
json={"base_url": _url(fake_openai), "model": "fake-model", "api_key": "sk-wrong-one"},
)
assert r.status_code == 200
body = r.json()
assert body["ok"] is False
assert body["error"] == "auth"
assert "401" in body["detail"]
def test_llm_test_connection_refused(client):
sock = socket.socket()
sock.bind(("127.0.0.1", 0))
port = sock.getsockname()[1]
sock.close()
r = client.post(
"/api/settings/llm-test",
json={"base_url": f"http://127.0.0.1:{port}/v1", "model": "fake-model"},
)
assert r.status_code == 200
body = r.json()
assert body["ok"] is False
assert body["error"] == "connect"
assert body["detail"]
def test_llm_test_validation(client):
r = client.post("/api/settings/llm-test", json={"model": "only-model"})
assert r.status_code == 400
r = client.post("/api/settings/llm-test", json={"profile_id": "does-not-exist"})
assert r.status_code == 404
+5
View File
@@ -267,6 +267,11 @@ export const settings = {
get: () => api<Settings>('/api/settings'),
update: (payload: Record<string, unknown>) =>
api<Settings>('/api/settings', { method: 'POST', body: JSON.stringify(payload) }),
testLlm: (payload: { profile_id?: string; base_url?: string; model?: string; api_key?: string }) =>
api<{ ok: boolean; elapsed_ms?: number; error?: string; detail?: string }>(
'/api/settings/llm-test',
{ method: 'POST', body: JSON.stringify(payload) },
),
};
export const workflows = {
+1 -1
View File
@@ -25,7 +25,7 @@ function initialLanguage(): Language {
// language this build no longer ships must not blank the UI. This runs on
// the server too, where storage is absent and we render the default.
const stored = storageGet(STORAGE_KEY);
return isLanguage(stored) ? stored : 'en';
return isLanguage(stored) ? stored : 'zh';
}
export const language = $state<{ current: Language }>({ current: initialLanguage() });
+6
View File
@@ -995,6 +995,12 @@ export const de: Dict = {
'settings.removeLlmTitle': 'LLM entfernen',
'settings.name': 'Name',
'settings.active': 'Aktiv',
'settings.testLlm': 'Verbindung testen',
'settings.llmTestOk': 'Verbindung erfolgreich ({ms} ms)',
'settings.llmTestFail': 'Verbindung fehlgeschlagen: {detail}',
'settings.llmTestAuth': 'Schlüssel abgelehnt ({detail})',
'settings.llmTestTimeout': 'Keine Antwort innerhalb von 15 s',
'settings.llmTestConnect': 'Server nicht erreichbar ({detail})',
'settings.agentModelsSection': 'Agentenmodell',
'settings.agentModelsLead': 'Modell für Story-Entwürfe und Agenten-Aufgaben.',
'settings.agentMaxStepsField': 'Max. Schritte',
+6
View File
@@ -995,6 +995,12 @@ export const en = {
'settings.removeLlmTitle': 'Remove LLM',
'settings.name': 'Name',
'settings.active': 'Active',
'settings.testLlm': 'Test connection',
'settings.llmTestOk': 'Connected ({ms} ms)',
'settings.llmTestFail': 'Connection failed: {detail}',
'settings.llmTestAuth': 'API key rejected ({detail})',
'settings.llmTestTimeout': 'No response within 15 s',
'settings.llmTestConnect': 'Could not reach the server ({detail})',
'settings.agentModelsSection': 'Agent model',
'settings.agentModelsLead': 'Model used for story drafting and agent tasks.',
'settings.agentMaxStepsField': 'Max steps',
+6
View File
@@ -995,6 +995,12 @@ export const es: Dict = {
'settings.removeLlmTitle': 'Quitar LLM',
'settings.name': 'Nombre',
'settings.active': 'Activo',
'settings.testLlm': 'Probar conexión',
'settings.llmTestOk': 'Conexión correcta ({ms} ms)',
'settings.llmTestFail': 'Error de conexión: {detail}',
'settings.llmTestAuth': 'Clave rechazada ({detail})',
'settings.llmTestTimeout': 'Sin respuesta en 15 s',
'settings.llmTestConnect': 'No se pudo conectar al servidor ({detail})',
'settings.agentModelsSection': 'Modelo del agente',
'settings.agentModelsLead': 'Modelo usado para redactar la historia y para las tareas del agente.',
'settings.agentMaxStepsField': 'Pasos máximos',
+6
View File
@@ -995,6 +995,12 @@ export const fr: Dict = {
'settings.removeLlmTitle': 'Retirer le LLM',
'settings.name': 'Nom',
'settings.active': 'Actif',
'settings.testLlm': 'Tester la connexion',
'settings.llmTestOk': 'Connexion réussie ({ms} ms)',
'settings.llmTestFail': 'Échec de connexion : {detail}',
'settings.llmTestAuth': 'Clé refusée ({detail})',
'settings.llmTestTimeout': 'Aucune réponse sous 15 s',
'settings.llmTestConnect': 'Serveur injoignable ({detail})',
'settings.agentModelsSection': 'Modèle agent',
'settings.agentModelsLead': "Modèle utilisé pour la rédaction de l'histoire et les tâches de l'agent.",
'settings.agentMaxStepsField': 'Étapes max.',
+6
View File
@@ -995,6 +995,12 @@ export const ja: Dict = {
'settings.removeLlmTitle': 'LLM を削除',
'settings.name': '名前',
'settings.active': '有効',
'settings.testLlm': '接続テスト',
'settings.llmTestOk': '接続成功({ms} ms)',
'settings.llmTestFail': '接続失敗:{detail}',
'settings.llmTestAuth': 'キーが拒否されました({detail})',
'settings.llmTestTimeout': '15 秒以内に応答なし',
'settings.llmTestConnect': 'サーバーに接続できません({detail})',
'settings.agentModelsSection': 'エージェントモデル',
'settings.agentModelsLead': 'ストーリーの下書きやエージェントタスクに使用するモデル。',
'settings.agentMaxStepsField': '最大ステップ数',
+6
View File
@@ -995,6 +995,12 @@ export const ko: Dict = {
'settings.removeLlmTitle': 'LLM 제거',
'settings.name': '이름',
'settings.active': '활성',
'settings.testLlm': '연결 테스트',
'settings.llmTestOk': '연결 성공 ({ms}ms)',
'settings.llmTestFail': '연결 실패: {detail}',
'settings.llmTestAuth': '키가 거부됨 ({detail})',
'settings.llmTestTimeout': '15초 내 응답 없음',
'settings.llmTestConnect': '서버에 연결할 수 없음 ({detail})',
'settings.agentModelsSection': '에이전트 모델',
'settings.agentModelsLead': '스토리 초안 작성과 에이전트 작업에 사용하는 모델입니다.',
'settings.agentMaxStepsField': '최대 단계',
+6
View File
@@ -995,6 +995,12 @@ export const zh: Dict = {
'settings.removeLlmTitle': '移除 LLM',
'settings.name': '名称',
'settings.active': '激活',
'settings.testLlm': '测试连接',
'settings.llmTestOk': '连接成功({ms} 毫秒)',
'settings.llmTestFail': '连接失败:{detail}',
'settings.llmTestAuth': '密钥被拒绝({detail})',
'settings.llmTestTimeout': '15 秒内无响应',
'settings.llmTestConnect': '无法连接服务器({detail})',
'settings.agentModelsSection': '智能体模型',
'settings.agentModelsLead': '用于故事起草和智能体任务的模型。',
'settings.agentMaxStepsField': '最大步数',
+91 -11
View File
@@ -33,6 +33,7 @@
let draft = $state<Record<string, unknown>>({});
let apiKeyDrafts = $state<Record<string, string>>({});
let llmTestState = $state<Record<string, { pending: boolean; ok?: boolean; message: string }>>({});
// Dirty tracking: any staged field (draft map or a typed API key) counts.
const dirtyKeys = $derived([
@@ -349,6 +350,53 @@
const nextKeys = { ...apiKeyDrafts };
delete nextKeys[id];
apiKeyDrafts = nextKeys;
const nextTests = { ...llmTestState };
delete nextTests[id];
llmTestState = nextTests;
}
function llmTestFailureText(res: { error?: string; detail?: string }): string {
let detail = res.detail ?? res.error ?? '';
if (res.error === 'auth') detail = t('settings.llmTestAuth', { detail });
else if (res.error === 'timeout') detail = t('settings.llmTestTimeout');
else if (res.error === 'connect') detail = t('settings.llmTestConnect', { detail });
return t('settings.llmTestFail', { detail });
}
async function testLlmConnection(s: Settings, profile: LlmProfile) {
ensureLlmDraft(s);
const typedKey = apiKeyDrafts[profile.id];
const savedOnServer = s.llm_profiles?.some((p) => p.id === profile.id) ?? false;
llmTestState = { ...llmTestState, [profile.id]: { pending: true, message: '' } };
try {
const res = await settings.testLlm({
...(typedKey || !savedOnServer ? {} : { profile_id: profile.id }),
base_url: profile.base_url.trim(),
model: profile.model.trim(),
...(typedKey ? { api_key: typedKey } : {}),
});
llmTestState = {
...llmTestState,
[profile.id]: res.ok
? {
pending: false,
ok: true,
message: t('settings.llmTestOk', { ms: res.elapsed_ms ?? '?' }),
}
: { pending: false, ok: false, message: llmTestFailureText(res) },
};
} catch (err) {
llmTestState = {
...llmTestState,
[profile.id]: {
pending: false,
ok: false,
message: t('settings.llmTestFail', {
detail: err instanceof Error ? err.message : String(err),
}),
},
};
}
}
</script>
@@ -389,16 +437,25 @@
{#each workingProfiles(s) as profile (profile.id)}
{@const active = workingActiveId(s) === profile.id}
<article class="llm-card" class:active>
<header class="llm-card-head">
<label class="llm-active">
<input
type="radio"
name="llm-active"
checked={active}
onchange={() => setActiveProfile(s, profile.id)}
/>
<span>{active ? t('settings.active') : t('settings.useThis')}</span>
</label>
<header class="llm-card-head">
<label class="llm-active">
<input
type="radio"
name="llm-active"
checked={active}
onchange={() => setActiveProfile(s, profile.id)}
/>
<span>{active ? t('settings.active') : t('settings.useThis')}</span>
</label>
<div class="llm-card-actions">
<Button
variant="secondary"
size="sm"
loading={llmTestState[profile.id]?.pending}
onclick={() => testLlmConnection(s, profile)}
>
{t('settings.testLlm')}
</Button>
{#if workingProfiles(s).length > 1}
<Button
variant="ghost"
@@ -410,7 +467,17 @@
{t('settings.remove')}
</Button>
{/if}
</header>
</div>
</header>
{#if llmTestState[profile.id] && !llmTestState[profile.id].pending && llmTestState[profile.id].message}
<p
class="test-result"
class:ok={llmTestState[profile.id].ok}
class:err={llmTestState[profile.id].ok === false}
>
{llmTestState[profile.id].message}
</p>
{/if}
<label class="field">
<span class="field-label">{t('settings.name')}</span>
<input
@@ -950,6 +1017,19 @@
gap: 12px;
margin-bottom: var(--space-sm);
}
.llm-card-actions {
display: flex;
align-items: center;
gap: 8px;
}
.test-result {
margin: 0 0 var(--space-sm);
font-size: 12px;
color: var(--error);
}
.test-result.ok {
color: var(--success);
}
.llm-active {
display: inline-flex;
align-items: center;