Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion apps/api/app/services/ai_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

from pydantic import BaseModel

MAX_AI_CONTEXT_CHARS = 120_000
MAX_AI_OUTPUT_TOKENS = 4096
CLOUD_TIMEOUT_MS = 60_000

from app.core.ai_errors import (
AI_UNAVAILABLE_MESSAGE,
AIQuotaExceededError,
Expand Down Expand Up @@ -84,12 +88,17 @@ def call_cloud_ai(
gemini_client = client
if gemini_client is None:
from google import genai
gemini_client = genai.Client(api_key=settings.gemini_api_key)
from google.genai import types
gemini_client = genai.Client(
api_key=settings.gemini_api_key,
http_options=types.HttpOptions(timeout=CLOUD_TIMEOUT_MS)
)

from google.genai import types

config_kwargs: dict[str, Any] = {
"temperature": temperature,
"max_output_tokens": MAX_AI_OUTPUT_TOKENS,
}
if schema is not None:
config_kwargs["response_mime_type"] = "application/json"
Expand Down Expand Up @@ -124,6 +133,7 @@ def call_ollama(
"format": "json",
"options": {
"temperature": temperature,
"num_predict": MAX_AI_OUTPUT_TOKENS,
},
}

Expand Down Expand Up @@ -178,6 +188,10 @@ def generate_ai_response(
AIQuotaExceededError: When quota is exceeded and fallback is not available or failed.
AIUnavailableError: When AI services fail and cannot fulfill request.
"""

if len(prompt) > MAX_AI_CONTEXT_CHARS:
raise AIQuotaExceededError(f"AI context exceeds maximum allowed length of {MAX_AI_CONTEXT_CHARS} characters.")

mode = force_provider or settings.ai_provider_mode or "hybrid"

# Mode 1: Ollama only
Expand Down
90 changes: 90 additions & 0 deletions apps/api/tests/test_ai_quota.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

import pytest
from unittest.mock import patch, MagicMock

from app.services.ai_provider import (
MAX_AI_CONTEXT_CHARS,
MAX_AI_OUTPUT_TOKENS,
generate_ai_response,
call_cloud_ai,
call_ollama,
)
from app.core.ai_errors import AIQuotaExceededError, AIUnavailableError


def test_ai_context_length_limit():
"""AI context above the configured maximum is safely rejected."""
long_prompt = "a" * (MAX_AI_CONTEXT_CHARS + 1)

with pytest.raises(AIQuotaExceededError) as exc:
generate_ai_response(long_prompt)

assert "exceeds maximum allowed length" in str(exc.value.message)


def test_ai_output_capped():
"""Provider calls enforce output size limits."""
# Patch google.genai.Client directly
with patch("google.genai.Client") as mock_client_class:
mock_instance = MagicMock()
mock_client_class.return_value = mock_instance

with patch("app.services.ai_provider.settings.gemini_api_key", "fake-key"):
call_cloud_ai("test prompt", temperature=0.5)

mock_instance.models.generate_content.assert_called_once()
call_args = mock_instance.models.generate_content.call_args[1]
config = call_args["config"]
assert config.max_output_tokens == MAX_AI_OUTPUT_TOKENS


def test_ollama_output_capped():
"""Ollama fallback enforces output size limits (num_predict)."""
with patch("urllib.request.urlopen") as mock_urlopen:
mock_response = MagicMock()
mock_response.read.return_value = b'{"response": "test output"}'
mock_response.__enter__.return_value = mock_response
mock_urlopen.return_value = mock_response

call_ollama("test prompt")

mock_urlopen.assert_called_once()
request = mock_urlopen.call_args[0][0]
import json
body = json.loads(request.data.decode("utf-8"))
assert body["options"]["num_predict"] == MAX_AI_OUTPUT_TOKENS


def test_cloud_fallback_does_not_multiply_requests():
"""A failed cloud request does exactly ONE fallback to Ollama, not a retry loop."""
with patch("app.services.ai_provider.call_cloud_ai") as mock_cloud, \
patch("app.services.ai_provider.call_ollama") as mock_ollama:

# Simulate transient error on cloud (e.g., HTTP 503 equivalent)
class FakeTransientError(Exception):
status_code = 503

mock_cloud.side_effect = FakeTransientError("Cloud transient failure")
mock_ollama.return_value = "Ollama response"

with patch("app.services.ai_provider.settings.gemini_api_key", "fake-key"), \
patch("app.services.ai_provider.settings.ai_provider_mode", "hybrid"):

raw_text, provider = generate_ai_response("short prompt")

assert raw_text == "Ollama response"
assert provider == "ollama"
mock_cloud.assert_called_once()
mock_ollama.assert_called_once()


def test_provider_timeout_handled():
"""Provider timeouts raise controlled AIUnavailableError without crashing."""
with patch("urllib.request.urlopen") as mock_urlopen:
mock_urlopen.side_effect = TimeoutError("Connection timed out")

with pytest.raises(AIUnavailableError) as exc:
call_ollama("test prompt")

assert "timed out" in str(exc.value)

Loading