From 1fa3c55ac4b84ea38002b89886885723f82d6458 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 16 Aug 2026 22:18:27 -0700 Subject: [PATCH] refac --- cptr/routers/gateway.py | 16 +++++----------- cptr/routers/git.py | 9 +++++---- cptr/utils/chat_task.py | 8 ++++++-- cptr/utils/memory.py | 3 ++- cptr/utils/skills.py | 4 ++-- cptr/utils/summarize.py | 4 ++-- cptr/utils/utility_models.py | 24 ++++++++++++++++++++++++ 7 files changed, 46 insertions(+), 22 deletions(-) create mode 100644 cptr/utils/utility_models.py diff --git a/cptr/routers/gateway.py b/cptr/routers/gateway.py index 2a0d1c6..718352a 100644 --- a/cptr/routers/gateway.py +++ b/cptr/routers/gateway.py @@ -505,12 +505,6 @@ _UTILITY_PATTERNS = [ "tags_generation", ] -_UTILITY_MODEL_CONFIG_KEYS = { - "summary_generation": "chat.context_compaction.model", - "title_generation": "chat.title_generation.model", -} - - async def _intercept_task( request: Request, body: ChatCompletionRequest, @@ -700,11 +694,11 @@ async def _resolve_utility_model(request: Request, workspace: str, app_state=Non candidates: list[str] = [] task_header = request.headers.get(OWUI_TASK_HEADER, "").strip() - preferred_config_key = _UTILITY_MODEL_CONFIG_KEYS.get(task_header) - if preferred_config_key: - preferred_model = await Config.get(preferred_config_key) - if isinstance(preferred_model, str) and preferred_model.strip(): - candidates.append(preferred_model.strip()) + if task_header: + from cptr.utils.utility_models import configured_utility_model + + if preferred_model := await configured_utility_model(task_header): + candidates.append(preferred_model) gateway_model = await Config.get("gateway.model") if isinstance(gateway_model, str) and gateway_model.strip(): diff --git a/cptr/routers/git.py b/cptr/routers/git.py index 48dd1ac..cf16755 100644 --- a/cptr/routers/git.py +++ b/cptr/routers/git.py @@ -642,13 +642,14 @@ async def generate_commit_message(request: Request, body: CommitMessageRequest): if not patch: raise HTTPException(status_code=400, detail="No staged changes") - from cptr.models import Config - message_model = body.model_id.strip() if isinstance(body.model_id, str) else None if message_model is None: - configured_model = await Config.get("git.commit_message_generation.model") - message_model = configured_model.strip() if isinstance(configured_model, str) else None + from cptr.utils.utility_models import configured_utility_model + + message_model = await configured_utility_model("git_commit_message_generation") if not message_model: + from cptr.models import Config + default_model = await Config.get("chat.default_model") message_model = default_model.strip() if isinstance(default_model, str) else None text = await generate_text( diff --git a/cptr/utils/chat_task.py b/cptr/utils/chat_task.py index d8fca22..25caba5 100644 --- a/cptr/utils/chat_task.py +++ b/cptr/utils/chat_task.py @@ -763,7 +763,9 @@ async def review_tool_approval( args_text = json.dumps(arguments, ensure_ascii=False, default=str) if len(args_text) > 4000: args_text = args_text[:3500] + "\n...(truncated)" - configured_model = await Config.get("tool_approval.review.model") + from cptr.utils.utility_models import configured_utility_model + + configured_model = await configured_utility_model("tool_approval_review") logger.info( "[tool-approval] auto review start tool=%s policy=%s active_model=%s review_model=%s args=%s", tool_name, @@ -828,9 +830,11 @@ async def generate_chat_title( truncated += "..." try: + from cptr.utils.utility_models import configured_utility_model + parsed = await generate_json( None, - model_id=await Config.get("chat.title_generation.model"), + model_id=await configured_utility_model("title_generation"), active_connection=connection, active_model=model, messages=[{"role": "user", "content": truncated}], diff --git a/cptr/utils/memory.py b/cptr/utils/memory.py index a665c4a..6b037c6 100644 --- a/cptr/utils/memory.py +++ b/cptr/utils/memory.py @@ -1652,13 +1652,14 @@ async def run_memory_review( ) -> None: try: from cptr.utils.ai import generate_json + from cptr.utils.utility_models import configured_utility_model memory_state = await read_memory_state(request, user_id, workspace) transcript = summarize_recent_conversation(conversation_messages, assistant_reply) prompt = build_memory_review_prompt(memory_state, workspace, transcript) parsed = await generate_json( None, - model_id=await Config.get("memory.background_review.model"), + model_id=await configured_utility_model("memory_background_review"), active_connection=model_connection, active_model=model, messages=[{"role": "user", "content": prompt}], diff --git a/cptr/utils/skills.py b/cptr/utils/skills.py index 195b7fd..807f332 100644 --- a/cptr/utils/skills.py +++ b/cptr/utils/skills.py @@ -799,9 +799,9 @@ async def run_skill_review( skill_create_requested: bool, ) -> None: try: - from cptr.models import Config from cptr.utils.ai import generate_json from cptr.utils.memory import summarize_recent_conversation + from cptr.utils.utility_models import configured_utility_model skills = discover_skills(workspace) transcript = summarize_recent_conversation(conversation_messages, assistant_reply) @@ -844,7 +844,7 @@ async def run_skill_review( ) parsed = await generate_json( None, - model_id=await Config.get("skills.background_review.model"), + model_id=await configured_utility_model("skills_background_review"), active_connection=model_connection, active_model=model, messages=[{"role": "user", "content": prompt}], diff --git a/cptr/utils/summarize.py b/cptr/utils/summarize.py index 3ea534d..b090180 100644 --- a/cptr/utils/summarize.py +++ b/cptr/utils/summarize.py @@ -63,12 +63,12 @@ async def summarize_messages( text = text[:15_000] + "\n...\n" + text[-10_000:] try: - from cptr.models import Config from cptr.utils.ai import generate_text + from cptr.utils.utility_models import configured_utility_model result = await generate_text( None, - model_id=await Config.get("chat.context_compaction.model"), + model_id=await configured_utility_model("summary_generation"), active_connection=connection, active_model=model, messages=[{"role": "user", "content": text}], diff --git a/cptr/utils/utility_models.py b/cptr/utils/utility_models.py new file mode 100644 index 0000000..90df0e8 --- /dev/null +++ b/cptr/utils/utility_models.py @@ -0,0 +1,24 @@ +"""Computer utility task model configuration.""" + +from __future__ import annotations + +UTILITY_MODEL_CONFIG_KEYS: dict[str, str] = { + "title_generation": "chat.title_generation.model", + "summary_generation": "chat.context_compaction.model", + "tool_approval_review": "tool_approval.review.model", + "memory_background_review": "memory.background_review.model", + "skills_background_review": "skills.background_review.model", + "git_commit_message_generation": "git.commit_message_generation.model", +} + + +async def configured_utility_model(task: str) -> str | None: + """Return the configured model id for a Computer utility task, if set.""" + key = UTILITY_MODEL_CONFIG_KEYS.get(task) + if not key: + return None + + from cptr.models import Config + + value = await Config.get(key) + return value.strip() if isinstance(value, str) and value.strip() else None