Files
DeepTutor/tests/services/test_task_models.py
Bingxi Zhao (Frank) a9f67eb3aa feat(settings): connections layer + per-task model pinning
Model configuration was one layer replicated seven times: every service
stored its own profile with its own credentials, so a single OpenRouter
key had to be pasted into five separate pages. Every comparable product
(Dify, Open WebUI, Cherry Studio, RAGFlow) splits this in two — a
credential entered once, then a per-purpose assignment — so this adds
that split without disturbing what already works.

Connections
  A connection holds one vendor credential (api_key / api_version /
  extra_headers, plus an optional endpoint override). Linking creates one
  ordinary profile per service that can use it, and the credential is
  mirrored down on save rather than resolved on read — so every consumer
  of the catalog keeps reading self-contained profiles and a linked
  profile resolves exactly like a hand-typed one. Rotating the key in one
  place reaches every linked profile; deleting a connection unlinks
  rather than wipes. Anyone wanting a different key per service simply
  never makes a connection.

  Which services a vendor can serve is derived from the six provider
  tables that already exist (new `connection_targets` in GET /settings),
  so the web app keeps no second copy and adding a vendor to a service
  table is enough to make it connectable.

Task models
  Naming a conversation and writing the three home-screen starting points
  are LLM calls nobody asked for — short, frequent, and fine on a small
  fast model. Both can now be pinned to their own configured model from
  Settings > Models > Task models. Unpinned is the default and a true
  no-op: the title still follows the turn's own model and the starters
  the active one, exactly as before. A pin at a deleted profile reverts
  to inheriting rather than stranding the call.
2026-08-29 11:50:10 +08:00

83 lines
2.7 KiB
Python

"""Task models: pinned when asked for, inherited in every other case."""
from __future__ import annotations
from pathlib import Path
from typing import Any
from deeptutor.services.config.model_catalog import ModelCatalogService
from deeptutor.services.model_selection.tasks import (
TASK_SESSION_TITLE,
TASK_STARTERS,
resolve_task_selection,
)
def _catalog_with_tasks(service: ModelCatalogService, tasks: dict[str, Any]) -> dict[str, Any]:
catalog = service.load()
catalog["services"]["llm"]["profiles"] = [
{
"id": "llm-1",
"name": "OpenRouter",
"binding": "openrouter",
"base_url": "https://openrouter.ai/api/v1",
"api_key": "sk-or",
"models": [
{"id": "model-big", "model": "anthropic/claude-opus-4"},
{"id": "model-small", "model": "openai/gpt-5-mini"},
],
}
]
catalog["services"]["llm"]["tasks"] = tasks
return catalog
def test_a_pinned_task_resolves_to_its_profile_and_model(tmp_path: Path) -> None:
service = ModelCatalogService(path=tmp_path / "model_catalog.json")
catalog = service.save(
_catalog_with_tasks(
service,
{TASK_STARTERS: {"profile_id": "llm-1", "model_id": "model-small"}},
)
)
selection = resolve_task_selection(TASK_STARTERS, catalog)
assert selection is not None
assert (selection.profile_id, selection.model_id) == ("llm-1", "model-small")
def test_an_unpinned_task_inherits(tmp_path: Path) -> None:
service = ModelCatalogService(path=tmp_path / "model_catalog.json")
catalog = service.save(_catalog_with_tasks(service, {}))
assert resolve_task_selection(TASK_SESSION_TITLE, catalog) is None
def test_a_task_pointing_at_a_deleted_model_falls_back_to_inheriting(tmp_path: Path) -> None:
service = ModelCatalogService(path=tmp_path / "model_catalog.json")
catalog = service.save(
_catalog_with_tasks(
service,
{TASK_SESSION_TITLE: {"profile_id": "llm-1", "model_id": "model-deleted"}},
)
)
# Normalization drops the dangling pointer rather than leaving a task
# aimed at a model that no longer exists.
assert catalog["services"]["llm"]["tasks"] == {}
assert resolve_task_selection(TASK_SESSION_TITLE, catalog) is None
def test_unknown_task_names_are_dropped(tmp_path: Path) -> None:
service = ModelCatalogService(path=tmp_path / "model_catalog.json")
catalog = service.save(
_catalog_with_tasks(
service,
{"not_a_task": {"profile_id": "llm-1", "model_id": "model-big"}},
)
)
assert catalog["services"]["llm"]["tasks"] == {}
assert resolve_task_selection("not_a_task", catalog) is None