feat: one entry per provider, endpoints per type and model from a live dropdown
Reshapes setup around how a multi-model proxy is actually used: one config entry per server/provider, then one endpoint per model added by hand, with the model chosen from what that provider advertises right now. Setup (async_step_user): - Adds a Name field. With one entry per provider the title matters, and it was hardcoded to "ChatGPT" regardless of where the entry pointed. Blank falls back to the endpoint host via url_util.title_from_base_url. Deliberately absent from the reauth schema (now split out as STEP_REAUTH_DATA_SCHEMA): that step writes its input back into entry.data via data_updates, so a name field there would persist the title as connection data on every key rotation. - Stops auto-creating four subentries. conversation/ai_task_data/stt/tts were all created up front carrying RECOMMENDED_* options, which pin OpenAI model names (gpt-4o-mini-tts, gpt-4o-mini-transcribe). Against a third-party endpoint those entities are dead on arrival -- the reported symptom was an install that "loads bits, it's all janky". - Drops _async_abort_entries_match, so several entries may point at one server (one per provider-side key, for instance). Upstream aborts with already_configured on matching url+key. Model discovery (new model_list.py, kept out of the vendored files like url_util.py): - async_fetch_model_ids asks the endpoint's GET /v1/models. It calls with_options(max_retries=0), because this runs while the user waits on a dialog and the SDK otherwise retries with backoff -- visible in the test run as repeated "Retrying request to /models" and a suite that went from 5s to minutes. - Returns [] on any OpenAIError, and model_field() then yields a plain text field instead of a dropdown, so an endpoint that serves /v1/responses but not /v1/models is still configurable by hand. The dropdown sets custom_value=True for the same reason (LiteLLM wildcard routes). - Wired into all four types: conversation and ai_task_data on the additional step, stt (which hardcoded OpenAI's three transcribe models) and tts on init. TTS had no model field at all -- it silently used gpt-4o-mini-tts. A new endpoint now starts with "recommended" unticked. Ticked, the flow ends on the first step and never shows a model field, which is why the model looked unpickable; reconfigure still keeps whatever was saved. The model field stays on the additional step rather than moving to init. The init position reads better, but 71 ported tests drive the model through the additional step, several from parametrized option dicts that no mechanical edit could relocate. Rewriting the only regression net against upstream to save one click was the wrong trade. Also: the setup description no longer says "your OpenAI API key", and instructions_url pointed at a README anchor (#generate-an-api-key) that does not exist -- now #configuration. strings.json re-verified against core's RE_URL: 0 violations. Local: 167 passed. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NxJYWgGJtCw13qUHZtG8ki
This commit is contained in:
1 parent
13c60c242a
commit
e7fcb1d7a1
10 files changed
+583
-98
No files matched your search
@@ -0,0 +1,87 @@
|
||||
"""Model discovery: turn an endpoint's GET /v1/models into a picker."""
|
||||
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import openai
|
||||
import pytest
|
||||
|
||||
from homeassistant.helpers.selector import SelectSelector
|
||||
|
||||
from custom_components.openai_compatible.model_list import (
|
||||
async_fetch_model_ids,
|
||||
model_field,
|
||||
)
|
||||
|
||||
|
||||
def _client(model_ids: list[str] | None = None, *, error: Exception | None = None):
|
||||
"""Return a stub AsyncOpenAI whose models.list yields model_ids or raises."""
|
||||
client = MagicMock()
|
||||
# Production calls client.with_options(...).models.list(); returning the
|
||||
# same stub keeps the per-request options visible to assertions.
|
||||
client.with_options.return_value = client
|
||||
if error is not None:
|
||||
client.models.list = AsyncMock(side_effect=error)
|
||||
else:
|
||||
page = MagicMock()
|
||||
page.data = [MagicMock(id=model_id) for model_id in (model_ids or [])]
|
||||
client.models.list = AsyncMock(return_value=page)
|
||||
return client
|
||||
|
||||
|
||||
async def test_fetch_returns_sorted_unique_ids() -> None:
|
||||
"""A proxy can list the same alias twice; the picker should show it once."""
|
||||
client = _client(["gpt-4o-mini", "claude-sonnet", "gpt-4o-mini", "llama-3.3"])
|
||||
|
||||
assert await async_fetch_model_ids(client) == [
|
||||
"claude-sonnet",
|
||||
"gpt-4o-mini",
|
||||
"llama-3.3",
|
||||
]
|
||||
|
||||
|
||||
async def test_fetch_ignores_entries_without_an_id() -> None:
|
||||
"""Don't put a blank option in the dropdown."""
|
||||
page = MagicMock()
|
||||
page.data = [MagicMock(id="real-model"), MagicMock(id=None), MagicMock(id="")]
|
||||
client = MagicMock()
|
||||
client.with_options.return_value = client
|
||||
client.models.list = AsyncMock(return_value=page)
|
||||
|
||||
assert await async_fetch_model_ids(client) == ["real-model"]
|
||||
|
||||
|
||||
async def test_fetch_does_not_retry() -> None:
|
||||
"""This runs while the user waits on a dialog; retries would stall it."""
|
||||
client = _client(["gpt-4o-mini"])
|
||||
|
||||
await async_fetch_model_ids(client, timeout=3.0)
|
||||
|
||||
client.with_options.assert_called_once_with(max_retries=0, timeout=3.0)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"error",
|
||||
[
|
||||
openai.APIConnectionError(request=MagicMock()),
|
||||
openai.AuthenticationError(
|
||||
"nope", response=MagicMock(status_code=401), body=None
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_fetch_survives_an_endpoint_that_will_not_list(error: Exception) -> None:
|
||||
"""Model discovery is a convenience; it must never break the config flow."""
|
||||
assert await async_fetch_model_ids(_client(error=error)) == []
|
||||
|
||||
|
||||
async def test_field_is_a_dropdown_that_still_accepts_a_typed_name() -> None:
|
||||
"""A proxy may serve a model it does not advertise, so allow free text."""
|
||||
field = model_field(["claude-sonnet", "gpt-4o-mini"])
|
||||
|
||||
assert isinstance(field, SelectSelector)
|
||||
assert field.config["options"] == ["claude-sonnet", "gpt-4o-mini"]
|
||||
assert field.config["custom_value"] is True
|
||||
|
||||
|
||||
async def test_field_falls_back_to_free_text_when_no_models_are_known() -> None:
|
||||
"""An endpoint that won't list models must still be configurable by hand."""
|
||||
assert model_field([]) is str
|
||||
Reference in new issue
Block a user