From d4d7d1ab14cc621bba4d930d632b91d0c4e54b62 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Fri, 12 Jun 2026 18:47:14 -0400 Subject: [PATCH] refactor(ai_client): _send_llama_native_result() returns Result[str] --- src/ai_client.py | 51 +++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/ai_client.py b/src/ai_client.py index c98736e2..97005fd9 100644 --- a/src/ai_client.py +++ b/src/ai_client.py @@ -2536,36 +2536,39 @@ def ollama_chat( resp = requests.post(f"{base_url}/api/chat", json=payload, timeout=120) return resp.json() -def _send_llama_native(md_content: str, user_message: str, base_dir: str, +def _send_llama_native_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: - base_url = _llama_base_url.replace("/v1", "") - with _llama_history_lock: - 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_message}) - messages: list[dict[str, Any]] = [{"role": "system", "content": f"{_get_combined_system_prompt()}\n\n\n{md_content}\n"}] - messages.extend(_llama_history) - images: list[str] = [] - if file_items: - for fi in file_items: - if fi.get("is_image") and fi.get("base64_data"): - images.append(fi["base64_data"]) - response = ollama_chat(_model, messages, images=images, base_url=base_url) - text = response.get("message", {}).get("content", "") - thinking = response.get("message", {}).get("thinking", "") - with _llama_history_lock: - msg: dict[str, Any] = {"role": "assistant", "content": text or None} - if thinking: - msg["thinking"] = thinking - _llama_history.append(msg) - return (f"\n{thinking}\n\n" if thinking else "") + text + patch_callback: Optional[Callable[[str, str], Optional[str]]] = None) -> Result[str]: + try: + base_url = _llama_base_url.replace("/v1", "") + with _llama_history_lock: + 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_message}) + messages: list[dict[str, Any]] = [{"role": "system", "content": f"{_get_combined_system_prompt()}\n\n\n{md_content}\n"}] + messages.extend(_llama_history) + images: list[str] = [] + if file_items: + for fi in file_items: + if fi.get("is_image") and fi.get("base64_data"): + images.append(fi["base64_data"]) + response = ollama_chat(_model, messages, images=images, base_url=base_url) + text = response.get("message", {}).get("content", "") + thinking = response.get("message", {}).get("thinking", "") + with _llama_history_lock: + msg: dict[str, Any] = {"role": "assistant", "content": text or None} + if thinking: + msg["thinking"] = thinking + _llama_history.append(msg) + return Result(data=(f"\n{thinking}\n\n" if thinking else "") + text) + except Exception as exc: + return Result(data="", errors=[ErrorInfo(kind=ErrorKind.INTERNAL, message=str(exc), source="ai_client.llama_native", original=exc)]) def _list_llama_models() -> list[str]: from src.vendor_capabilities import list_models_for_vendor return list_models_for_vendor("llama")