mirror of
https://github.com/openswarm-ai/openswarm.git
synced 2026-08-25 05:52:22 +02:00
[eric] split: move aux LLM helpers out of agent_manager
This commit is contained in:
@@ -49,32 +49,13 @@ from backend.apps.agents.tool_catalog import (
|
||||
_get_denied_tool_names,
|
||||
_is_fully_denied,
|
||||
)
|
||||
from backend.apps.agents.aux_llm import _safe_resp_text
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
os.environ.setdefault("CLAUDE_CODE_STREAM_CLOSE_TIMEOUT", "3600000")
|
||||
|
||||
|
||||
def _safe_resp_text(resp) -> str:
|
||||
"""Extract text from an Anthropic-shape response, tolerating Gemini/OpenAI
|
||||
edge cases. Gemini through 9Router occasionally returns `content=[]` (e.g.
|
||||
safety stop, function-call-only turn) which makes `resp.content[0].text`
|
||||
raise `'NoneType' object is not subscriptable` and bubbles up as a
|
||||
fallback-required path. This walks the content list looking for the first
|
||||
text block and returns "" if none exists, so callers can decide their own
|
||||
fallback without a raw IndexError.
|
||||
"""
|
||||
try:
|
||||
blocks = getattr(resp, "content", None) or []
|
||||
for b in blocks:
|
||||
t = getattr(b, "text", None)
|
||||
if isinstance(t, str) and t:
|
||||
return t
|
||||
return ""
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
|
||||
def _apply_context_window(session, settings=None) -> None:
|
||||
"""Set session.context_window from the registry for its (provider, model).
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
def _safe_resp_text(resp) -> str:
|
||||
"""Extract text from an Anthropic-shape response, tolerating Gemini/OpenAI
|
||||
edge cases. Gemini through 9Router occasionally returns `content=[]` (e.g.
|
||||
safety stop, function-call-only turn) which makes `resp.content[0].text`
|
||||
raise `'NoneType' object is not subscriptable` and bubbles up as a
|
||||
fallback-required path. This walks the content list looking for the first
|
||||
text block and returns "" if none exists, so callers can decide their own
|
||||
fallback without a raw IndexError.
|
||||
"""
|
||||
try:
|
||||
blocks = getattr(resp, "content", None) or []
|
||||
for b in blocks:
|
||||
t = getattr(b, "text", None)
|
||||
if isinstance(t, str) and t:
|
||||
return t
|
||||
return ""
|
||||
except Exception:
|
||||
return ""
|
||||
Reference in New Issue
Block a user