Private
Public Access
0
0
Files
manual_slop/tests/test_llama_provider.py
T
ed 25baa6fe25 feat(ai_client): add native Ollama adapter; route localhost to it
When _llama_base_url is localhost/127.0.0.1, _send_llama now
calls _send_llama_native (the native /api/chat adapter)
instead of the OpenAI-compat path. The native adapter
supports Ollama's vendor-specific fields: think, images,
thinking.

Functions added (in src/ai_client.py, per the naming
convention HARD RULE on no new src/*.py files):

  ollama_chat(model, messages, *, think='low', images=None,
              tools=None, base_url=OLLAMA_DEFAULT_BASE_URL)
    -> dict[str, Any]

  _send_llama_native(md_content, user_message, base_dir,
                     file_items=None, discussion_history='',
                     stream=False, ...callbacks) -> str

  OLLAMA_DEFAULT_BASE_URL: str = 'http://localhost:11434'

Implementation notes:
- requests loaded via _require_warmed('requests') (local
  scope; preserves startup_speedup_20260606 invariant that
  heavy SDKs are warmed on _io_pool, not imported at module
  level)
- _send_llama dispatches based on 'localhost' in
  _llama_base_url (same check already used by
  _get_llama_cost_tracking at line 2500)
- Removed orphan def stub at the old _send_llama body (the
  dead 'def _build_llama_request' that was overwritten by
  the real one — a known session issue with stale set_file_slice
  edits)
- Native adapter appends the 'thinking' field to history so
  subsequent rounds preserve the reasoning chain

Tests:
- 7 new tests in tests/test_llama_ollama_native.py:
  * ollama_chat hits /api/chat (not /v1/chat/completions)
  * ollama_chat includes 'think' param in payload
  * ollama_chat includes 'images' in payload
  * _send_llama_native wraps ollama_chat
  * _send_llama_native preserves 'thinking' field
  * _send_llama routes localhost to native (no openai client)
  * _send_llama keeps openai path for non-local (no POST)
- Updated test_send_llama_ollama_backend in test_llama_provider.py
  to mock the native path (was: mocked openai-compat; now:
  mocked requests.post)
- 103/103 vendor+tool+provider+import-isolation tests pass
  (no regressions; +7 new tests this commit)
- 4 audit scripts pass
2026-06-11 20:45:08 -04:00

72 lines
3.1 KiB
Python

from unittest.mock import MagicMock, patch
import pytest
from src import ai_client
@pytest.fixture(autouse=True)
def _reset_llama_state():
if hasattr(ai_client, '_llama_client'):
ai_client._llama_client = None
if hasattr(ai_client, '_llama_history'):
ai_client._llama_history = []
if hasattr(ai_client, '_llama_base_url'):
ai_client._llama_base_url = "http://localhost:11434/v1"
if hasattr(ai_client, '_llama_api_key'):
ai_client._llama_api_key = "ollama"
yield
def test_send_llama_ollama_backend(monkeypatch: pytest.MonkeyPatch) -> None:
ai_client._llama_base_url = "http://localhost:11434/v1"
ai_client.set_provider("llama", "llama-3.2-3b-preview")
mock_response = MagicMock()
mock_response.json.return_value = {
"message": {"role": "assistant", "content": "hi from ollama"},
"done": True,
}
mock_requests = MagicMock()
mock_requests.post.return_value = mock_response
with patch("src.ai_client._require_warmed", return_value=mock_requests):
result = ai_client._send_llama("system", "user", ".", None, "", False, None, None, None)
assert "hi from ollama" in result
called_url = mock_requests.post.call_args.args[0]
assert called_url == "http://localhost:11434/api/chat"
def test_send_llama_openrouter_backend(monkeypatch: pytest.MonkeyPatch) -> None:
ai_client._llama_base_url = "https://openrouter.ai/api/v1"
ai_client.set_provider("llama", "llama-3.1-70b-versatile")
captured_client = MagicMock()
captured_client.chat.completions.create.return_value = MagicMock(
choices=[MagicMock(message=MagicMock(content="hi from openrouter", tool_calls=[]))],
usage=MagicMock(prompt_tokens=5, completion_tokens=3),
)
with patch("src.ai_client._ensure_llama_client", return_value=captured_client) as ensure:
result = ai_client._send_llama("system", "user", ".", None, "", False, None, None, None)
assert result == "hi from openrouter"
assert ensure.called
def test_send_llama_custom_url(monkeypatch: pytest.MonkeyPatch) -> None:
ai_client._llama_base_url = "http://my-server:9999/v1"
mock_client = MagicMock()
mock_client.chat.completions.create.return_value = MagicMock(
choices=[MagicMock(message=MagicMock(content="hi from custom", tool_calls=[]))],
usage=MagicMock(prompt_tokens=5, completion_tokens=3),
)
with patch("src.ai_client._ensure_llama_client", return_value=mock_client):
result = ai_client._send_llama("system", "user", ".", None, "", False, None, None, None)
assert result == "hi from custom"
def test_llama_model_discovery_unions_ollama_and_openrouter() -> None:
from src.ai_client import _list_llama_models
models = _list_llama_models()
assert "llama-3.1-8b-instant" in models
assert "llama-3.2-11b-vision-preview" in models
assert "llama-3.3-70b-specdec" in models
def test_llama_3_2_vision_vision_capability() -> None:
from src.vendor_capabilities import get_capabilities
caps = get_capabilities("llama", "llama-3.2-11b-vision-preview")
assert caps.vision is True
def test_llama_local_backend_cost_tracking_false_for_ollama() -> None:
ai_client._llama_base_url = "http://localhost:11434/v1"
from src.ai_client import _get_llama_cost_tracking
assert _get_llama_cost_tracking() is False