From e4de539bf31b26a82daccdbbe94fab15e7eea978 Mon Sep 17 00:00:00 2001 From: Kimi Agent Date: Thu, 19 Mar 2026 19:18:22 -0400 Subject: [PATCH] fix: extract ollama_url normalization into shared utility (#508) Co-authored-by: Kimi Agent Co-committed-by: Kimi Agent --- src/config.py | 12 +++++++++++- src/dashboard/routes/health.py | 2 +- src/infrastructure/models/multimodal.py | 6 +++--- src/timmy/agent.py | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/config.py b/src/config.py index 77f402d..550743b 100644 --- a/src/config.py +++ b/src/config.py @@ -10,6 +10,11 @@ from pydantic_settings import BaseSettings, SettingsConfigDict APP_START_TIME: _datetime = _datetime.now(UTC) +def normalize_ollama_url(url: str) -> str: + """Replace localhost with 127.0.0.1 to avoid IPv6 resolution delays.""" + return url.replace("localhost", "127.0.0.1") + + class Settings(BaseSettings): """Central configuration — all env-var access goes through this class.""" @@ -19,6 +24,11 @@ class Settings(BaseSettings): # Ollama host — override with OLLAMA_URL env var or .env file ollama_url: str = "http://localhost:11434" + @property + def normalized_ollama_url(self) -> str: + """Return ollama_url with localhost replaced by 127.0.0.1.""" + return normalize_ollama_url(self.ollama_url) + # LLM model passed to Agno/Ollama — override with OLLAMA_MODEL # qwen3:30b is the primary model — better reasoning and tool calling # than llama3.1:8b-instruct while still running locally on modest hardware. @@ -392,7 +402,7 @@ def check_ollama_model_available(model_name: str) -> bool: import json import urllib.request - url = settings.ollama_url.replace("localhost", "127.0.0.1") + url = settings.normalized_ollama_url req = urllib.request.Request( f"{url}/api/tags", method="GET", diff --git a/src/dashboard/routes/health.py b/src/dashboard/routes/health.py index f223ea1..7b57ac4 100644 --- a/src/dashboard/routes/health.py +++ b/src/dashboard/routes/health.py @@ -65,7 +65,7 @@ def _check_ollama_sync() -> DependencyStatus: try: import urllib.request - url = settings.ollama_url.replace("localhost", "127.0.0.1") + url = settings.normalized_ollama_url req = urllib.request.Request( f"{url}/api/tags", method="GET", diff --git a/src/infrastructure/models/multimodal.py b/src/infrastructure/models/multimodal.py index 5c0181a..402f46b 100644 --- a/src/infrastructure/models/multimodal.py +++ b/src/infrastructure/models/multimodal.py @@ -13,7 +13,7 @@ import logging from dataclasses import dataclass, field from enum import Enum, auto -from config import settings +from config import normalize_ollama_url, settings logger = logging.getLogger(__name__) @@ -307,7 +307,7 @@ class MultiModalManager: import json import urllib.request - url = self.ollama_url.replace("localhost", "127.0.0.1") + url = normalize_ollama_url(self.ollama_url) req = urllib.request.Request( f"{url}/api/tags", method="GET", @@ -462,7 +462,7 @@ class MultiModalManager: logger.info("Pulling model: %s", model_name) - url = self.ollama_url.replace("localhost", "127.0.0.1") + url = normalize_ollama_url(self.ollama_url) req = urllib.request.Request( f"{url}/api/pull", method="POST", diff --git a/src/timmy/agent.py b/src/timmy/agent.py index 6aa67bd..bcbdee2 100644 --- a/src/timmy/agent.py +++ b/src/timmy/agent.py @@ -63,7 +63,7 @@ def _pull_model(model_name: str) -> bool: logger.info("Pulling model: %s", model_name) - url = settings.ollama_url.replace("localhost", "127.0.0.1") + url = settings.normalized_ollama_url req = urllib.request.Request( f"{url}/api/pull", method="POST",