fix(ai): offload model resolution from async paths

Wrap blocking _resolve_model calls in asyncio.to_thread across async model interaction paths so endpoint/model resolution does not stall the event loop. Preserve owner-scoped resolution and add focused regression coverage.
This commit is contained in:
tanmayraut45
2026-06-28 05:18:35 +05:30
committed by GitHub
parent 8b110c28e6
commit c01c09559a
8 changed files with 80 additions and 14 deletions
+5 -4
View File
@@ -14,6 +14,7 @@ These are agent tools — the LLM writes fenced code blocks and they execute
through the standard agent_tools.py pipeline.
"""
import asyncio
import json
import logging
import uuid
@@ -229,7 +230,7 @@ async def do_pipeline(content: str, session_id: Optional[str] = None, owner: Opt
if not model_spec or not instruction:
return {"error": f"Step {i + 1}: both 'model' and 'instruction' are required"}
try:
url, model, headers = _resolve_model(model_spec, owner=owner)
url, model, headers = await asyncio.to_thread(_resolve_model, model_spec, owner=owner)
resolved.append((url, model, headers, instruction))
except ValueError as e:
return {"error": f"Step {i + 1}: {e}"}
@@ -624,7 +625,7 @@ async def do_ui_control(content: str, session_id: Optional[str] = None, owner: O
# Resolve the model to validate it exists
try:
url, model_id, headers = _resolve_model(model_spec, owner=owner)
url, model_id, headers = await asyncio.to_thread(_resolve_model, model_spec, owner=owner)
except ValueError as e:
return {"error": str(e)}
@@ -914,7 +915,7 @@ async def do_generate_image(content: str, session_id: Optional[str] = None, owne
if not model_spec:
for candidate in ("gpt-image-1.5", "gpt-image-1", "dall-e-3"):
try:
_resolve_model(candidate, owner=owner)
await asyncio.to_thread(_resolve_model, candidate, owner=owner)
model_spec = candidate
break
except ValueError:
@@ -958,7 +959,7 @@ async def do_generate_image(content: str, session_id: Optional[str] = None, owne
# Resolve the model to find the right endpoint
try:
url, model_id, headers = _resolve_model(model_spec, owner=owner)
url, model_id, headers = await asyncio.to_thread(_resolve_model, model_spec, owner=owner)
except ValueError:
return {"error": f"No endpoint found with image model '{model_spec}'. "
"Configure an OpenAI-compatible endpoint with image generation support."}