core/cli: skip blocking GET /models on chat ready

Only fetch remote catalog when interactive model pick needs it; otherwise
use config/--model/session and warm Tab cache in the background. Cap list
fetch at 5s so a dead API cannot hang startup.
This commit is contained in:
2026-07-19 02:08:58 +08:00
parent d473cd2508
commit 5b51de12e0
5 changed files with 114 additions and 14 deletions
+37
View File
@@ -8,6 +8,7 @@ from plyngent.cli.models_source import (
fetch_remote_model_ids,
merge_model_choices,
model_choices_for_provider,
needs_remote_models_for_selection,
)
from plyngent.config.models import ModelConfig, OpenAICompatibleProvider
@@ -55,3 +56,39 @@ async def test_fetch_remote_model_ids() -> None:
with pytest.raises(TypeError, match="does not support"):
_ = await fetch_remote_model_ids(object())
@pytest.mark.asyncio
async def test_fetch_remote_model_ids_timeout() -> None:
import asyncio
class Slow:
async def models(self) -> list[str]:
await asyncio.sleep(10)
return ["late"]
with pytest.raises(TimeoutError):
_ = await fetch_remote_model_ids(Slow(), timeout_seconds=0.05)
def test_needs_remote_models_for_selection() -> None:
multi = OpenAICompatibleProvider(
access_key_or_token="sk",
url="https://x/v1",
models={"a": ModelConfig(), "b": ModelConfig()},
)
single = OpenAICompatibleProvider(
access_key_or_token="sk",
url="https://x/v1",
models={"only": ModelConfig()},
)
empty = OpenAICompatibleProvider(
access_key_or_token="sk",
url="https://x/v1",
models={},
)
assert needs_remote_models_for_selection(multi, preferred_model="a", interactive=True) is False
assert needs_remote_models_for_selection(multi, preferred_model=None, interactive=False) is False
assert needs_remote_models_for_selection(single, preferred_model=None, interactive=True) is False
assert needs_remote_models_for_selection(multi, preferred_model=None, interactive=True) is True
assert needs_remote_models_for_selection(empty, preferred_model=None, interactive=True) is True