Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(backends): coerce OpenCode 1.x /provider list response into id map

cyhd92e3dca1575
diff --git a/tests/test_opencode_provider_catalog_coercion.py b/tests/test_opencode_provider_catalog_coercion.py
new file mode 100644
index 000000000..40ff37e69
--- /dev/null
+++ b/tests/test_opencode_provider_catalog_coercion.py
@@ -0,0 +1,92 @@
+"""Regression tests for the OpenCode ``/provider`` payload normalizer.
+
+Pins the schema-coercion helper that backs the Settings → Backends →
+OpenCode catalog. Codex round-4 feedback on PR #282 observed an empty
+provider grid in the regression environment: OpenCode 1.x returns
+``{all: [Provider, ...]}`` (list), but the previous parser only accepted
+``{all: {pid: Provider}}`` (dict). Both shapes — plus the legacy
+``{providers: [...]}`` top-level form — must now round-trip into an
+id-keyed map so the UI never silently goes empty after an OpenCode
+upgrade-in-place.
+"""
+
+from __future__ import annotations
+
+from vibe.api import _coerce_opencode_provider_catalog
+
+
+def test_opencode_1x_list_shape_is_coerced_to_id_map() -> None:
+ payload = {
+ "all": [
+ {"id": "302ai", "name": "302.AI", "models": {}},
+ {"id": "anthropic", "name": "Anthropic", "models": {}},
+ {"id": "openai", "name": "OpenAI", "models": {}},
+ ],
+ "default": {"302ai": "qwen3-235b-a22b"},
+ "connected": ["anthropic", "openai"],
+ }
+ result = _coerce_opencode_provider_catalog(payload)
+ assert set(result.keys()) == {"302ai", "anthropic", "openai"}
+ assert result["anthropic"]["name"] == "Anthropic"
+
+
+def test_opencode_pre_1x_dict_shape_passes_through() -> None:
+ payload = {
+ "all": {
+ "anthropic": {"id": "anthropic", "name": "Anthropic"},
+ "openai": {"id": "openai", "name": "OpenAI"},
+ },
+ "default": {},
+ "connected": [],
+ }
+ result = _coerce_opencode_provider_catalog(payload)
+ assert result == payload["all"]
+
+
+def test_legacy_top_level_providers_list_is_coerced() -> None:
+ payload = {
+ "providers": [
+ {"id": "anthropic", "name": "Anthropic"},
+ {"id": "openai", "name": "OpenAI"},
+ ],
+ }
+ result = _coerce_opencode_provider_catalog(payload)
+ assert set(result.keys()) == {"anthropic", "openai"}
+
+
+def test_list_entries_without_id_are_skipped() -> None:
+ payload = {
+ "all": [
+ {"id": "anthropic", "name": "Anthropic"},
+ {"name": "MissingID"}, # malformed — no id
+ "not-a-dict", # malformed — wrong type
+ {"id": "openai", "name": "OpenAI"},
+ ],
+ }
+ result = _coerce_opencode_provider_catalog(payload)
+ assert set(result.keys()) == {"anthropic", "openai"}
+
+
+def test_unknown_payload_returns_empty_map() -> None:
+ assert _coerce_opencode_provider_catalog(None) == {}
+ assert _coerce_opencode_provider_catalog([]) == {}
+ assert _coerce_opencode_provider_catalog({"unrelated": "data"}) == {}
+ assert _coerce_opencode_provider_catalog({"all": "not-a-collection"}) == {}
+
+
+def test_list_shape_preserves_provider_payload() -> None:
+ payload = {
+ "all": [
+ {
+ "id": "anthropic",
+ "name": "Anthropic",
+ "env": ["ANTHROPIC_API_KEY"],
+ "options": {"baseURL": "https://api.anthropic.com"},
+ "models": {"claude-sonnet-4-5": {"id": "claude-sonnet-4-5"}},
+ },
+ ],
+ }
+ result = _coerce_opencode_provider_catalog(payload)
+ assert result["anthropic"]["env"] == ["ANTHROPIC_API_KEY"]
+ assert result["anthropic"]["options"] == {"baseURL": "https://api.anthropic.com"}
+ assert "claude-sonnet-4-5" in result["anthropic"]["models"]
diff --git a/ui/src/i18n/en.json b/ui/src/i18n/en.json
index 0131bf957..8e540e8f0 100644
--- a/ui/src/i18n/en.json
+++ b/ui/src/i18n/en.json
@@ -182,7 +182,7 @@
"opencodeProvidersEmpty": "OpenCode returned no providers. Run `opencode auth list` from a terminal to diagnose.",
"opencodeProvidersFilterEmpty": "No providers match the current search or filter.",
"opencodeProvidersError": "Could not load the OpenCode provider catalog.",
- "opencodeProvidersInfo": "Provider data is fetched live from the OpenCode server. Saving an API key writes to `~/.local/share/opencode/auth.json` via the server's `/auth` endpoint.",
+ "opencodeProvidersInfo": "Provider data is fetched live from the OpenCode server.",
"opencodeProviderModelsCount": "{{count}} models",
"opencodeProviderModelsEmpty": "No models reported by this provider.",
"opencodeProviderModels": "Models",
diff --git a/ui/src/i18n/zh.json b/ui/src/i18n/zh.json
index 0d9dc2020..e4d0c7568 100644
--- a/ui/src/i18n/zh.json
+++ b/ui/src/i18n/zh.json
@@ -182,7 +182,7 @@
"opencodeProvidersEmpty": "OpenCode 未返回任何服务商,建议在终端执行 `opencode auth list` 检查。",
"opencodeProvidersFilterEmpty": "没有符合当前搜索或筛选的服务商。",
"opencodeProvidersError": "无法读取 OpenCode 服务商列表。",
- "opencodeProvidersInfo": "服务商数据从 OpenCode 服务器实时拉取。保存 API Key 会通过 Server 的 `/auth` 接口写入 `~/.local/share/opencode/auth.json`。",
+ "opencodeProvidersInfo": "服务商数据从 OpenCode 服务器实时拉取。",
"opencodeProviderModelsCount": "{{count}} 个模型",
"opencodeProviderModelsEmpty": "该服务商未上报模型。",
"opencodeProviderModels": "模型列表",
diff --git a/vibe/api.py b/vibe/api.py
index 18b004a86..510456d0d 100644
--- a/vibe/api.py
+++ b/vibe/api.py
@@ -2413,6 +2413,38 @@ def _is_local_provider(provider_id: str, auth_methods: list) -> bool:
return False
+def _coerce_opencode_provider_catalog(providers_raw) -> dict:
+ """Normalize OpenCode ``/provider`` payloads into an id-keyed map.
+
+ OpenCode 1.x returns ``{all: [Provider, ...], default: {...},
+ connected: [...]}`` where ``all`` is a list. A pre-1.x prototype
+ returned ``{all: {pid: Provider}}`` (dict). The original legacy shape
+ was ``{providers: [...]}`` under a different top-level key. Tolerate
+ all three so an OpenCode upgrade-in-place or a stale client cannot
+ leave the Settings grid empty.
+ """
+ if not isinstance(providers_raw, dict):
+ return {}
+ out: dict = {}
+ raw_all = providers_raw.get("all")
+ if isinstance(raw_all, dict):
+ return raw_all
+ if isinstance(raw_all, list):
+ for entry in raw_all:
+ if isinstance(entry, dict):
+ pid = entry.get("id")
+ if pid:
+ out[pid] = entry
+ return out
+ legacy = providers_raw.get("providers")
+ if isinstance(legacy, list):
+ for entry in legacy:
+ pid = entry.get("id") if isinstance(entry, dict) else None
+ if pid:
+ out[pid] = entry
+ return out
+
+
async def _get_opencode_providers_async() -> dict:
"""Build the merged provider catalog reported to the Settings UI."""
server = await _opencode_get_server()
@@ -2430,19 +2462,7 @@ async def _get_opencode_providers_async() -> dict:
finally:
await server.close_http_session(loop=request_loop)
- # OpenCode 0.5+: ``/provider`` returns ``{all: {...}, default: {...},
- # connected: [...]}``. Older builds returned just ``{providers: [...]}``;
- # accept both shapes so an upgrade-in-place doesn't break the UI.
- all_providers: dict = {}
- if isinstance(providers_raw, dict):
- if isinstance(providers_raw.get("all"), dict):
- all_providers = providers_raw["all"]
- elif isinstance(providers_raw.get("providers"), list):
- # legacy shape — coerce list of dicts into id-keyed map
- for entry in providers_raw["providers"]:
- pid = entry.get("id") if isinstance(entry, dict) else None
- if pid:
- all_providers[pid] = entry
+ all_providers = _coerce_opencode_provider_catalog(providers_raw)
connected = providers_raw.get("connected") if isinstance(providers_raw, dict) else None
connected_set = {pid for pid in connected if isinstance(pid, str)} if isinstance(connected, list) else set()