From 6665152950eeb2eeb958642eb88279d11205ab48 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Fri, 12 Jun 2026 18:46:29 -0400 Subject: [PATCH] refactor(ai_client): _send_llama_result() returns Result[str] --- src/ai_client.py | 67 +++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/src/ai_client.py b/src/ai_client.py index bca4f72f..c98736e2 100644 --- a/src/ai_client.py +++ b/src/ai_client.py @@ -2471,45 +2471,48 @@ def _ensure_llama_client() -> Any: _llama_client = openai.OpenAI(api_key=_llama_api_key, base_url=_llama_base_url) return _llama_client -def _send_llama(md_content: str, user_message: str, base_dir: str, +def _send_llama_result(md_content: str, user_message: str, base_dir: str, file_items: list[dict[str, Any]] | None = None, discussion_history: str = "", stream: bool = False, pre_tool_callback: Optional[Callable[[str, str, Optional[Callable[[str], str]]], Optional[str]]] = None, qa_callback: Optional[Callable[[str], str]] = None, stream_callback: Optional[Callable[[str], None]] = None, - patch_callback: Optional[Callable[[str, str], Optional[str]]] = None) -> str: - if "localhost" in _llama_base_url or "127.0.0.1" in _llama_base_url: - return _send_llama_native(md_content, user_message, base_dir, file_items, discussion_history, stream, pre_tool_callback, qa_callback, stream_callback, patch_callback) - from src.openai_compatible import OpenAICompatibleRequest - client = _ensure_llama_client() - tools: list[dict[str, Any]] | None = _get_deepseek_tools() or None - with _llama_history_lock: - user_content = user_message - if file_items: - for fi in file_items: - if fi.get("is_image") and fi.get("base64_data"): - user_content = f"[IMAGE: {fi.get('path', 'attachment')}]\n{user_content}" - if discussion_history and not _llama_history: - _llama_history.append({"role": "user", "content": f"[DISCUSSION HISTORY]\n\n{discussion_history}\n\n---\n\n{user_message}"}) - else: - _llama_history.append({"role": "user", "content": user_content}) - def _build_llama_request(_round_idx: int) -> OpenAICompatibleRequest: + patch_callback: Optional[Callable[[str, str], Optional[str]]] = None) -> Result[str]: + from src.openai_compatible import OpenAICompatibleRequest, _classify_openai_compatible_error + try: + if "localhost" in _llama_base_url or "127.0.0.1" in _llama_base_url: + return _send_llama_native(md_content, user_message, base_dir, file_items, discussion_history, stream, pre_tool_callback, qa_callback, stream_callback, patch_callback) + client = _ensure_llama_client() + tools: list[dict[str, Any]] | None = _get_deepseek_tools() or None with _llama_history_lock: - messages: list[dict[str, Any]] = [{"role": "system", "content": f"{_get_combined_system_prompt()}\n\n\n{md_content}\n"}] - messages.extend(_llama_history) - return OpenAICompatibleRequest( - messages=messages, model=_model, temperature=_temperature, top_p=_top_p, - max_tokens=_max_tokens, stream=stream, stream_callback=stream_callback, - tools=tools, tool_choice="auto" if tools else "auto", - ) - caps = get_capabilities("llama", _model) - return run_with_tool_loop( - client, _build_llama_request, capabilities=caps, - pre_tool_callback=pre_tool_callback, qa_callback=qa_callback, stream_callback=stream_callback, - patch_callback=patch_callback, base_dir=base_dir, vendor_name="llama", - history_lock=_llama_history_lock, history=_llama_history, - ) + user_content = user_message + if file_items: + for fi in file_items: + if fi.get("is_image") and fi.get("base64_data"): + user_content = f"[IMAGE: {fi.get('path', 'attachment')}]\n{user_content}" + if discussion_history and not _llama_history: + _llama_history.append({"role": "user", "content": f"[DISCUSSION HISTORY]\n\n{discussion_history}\n\n---\n\n{user_message}"}) + else: + _llama_history.append({"role": "user", "content": user_content}) + def _build_llama_request(_round_idx: int) -> OpenAICompatibleRequest: + with _llama_history_lock: + messages: list[dict[str, Any]] = [{"role": "system", "content": f"{_get_combined_system_prompt()}\n\n\n{md_content}\n"}] + messages.extend(_llama_history) + return OpenAICompatibleRequest( + messages=messages, model=_model, temperature=_temperature, top_p=_top_p, + max_tokens=_max_tokens, stream=stream, stream_callback=stream_callback, + tools=tools, tool_choice="auto" if tools else "auto", + ) + caps = get_capabilities("llama", _model) + return Result(data=run_with_tool_loop( + client, _build_llama_request, capabilities=caps, + pre_tool_callback=pre_tool_callback, qa_callback=qa_callback, stream_callback=stream_callback, + patch_callback=patch_callback, base_dir=base_dir, vendor_name="llama", + history_lock=_llama_history_lock, history=_llama_history, + )) + except Exception as exc: + return Result(data="", errors=[_classify_openai_compatible_error(exc, source="ai_client.llama")]) OLLAMA_DEFAULT_BASE_URL: str = "http://localhost:11434"