Handle slow AI backend timeouts

This commit is contained in:
Hermes Agent
2026-09-11 13:42:42 +02:00
parent 9320217e0f
commit b2d2ba58fc
6 changed files with 56 additions and 5 deletions
+14
View File
@@ -34,6 +34,13 @@ def _ask_ollama(prompt: str, config: AppConfig) -> str:
response = requests.post(url, json=payload, timeout=config.request_timeout_seconds)
response.raise_for_status()
data = response.json()
except requests.Timeout as exc:
timeout_label = "désactivé" if config.request_timeout_seconds is None else f"{config.request_timeout_seconds:.0f} s"
raise AIClientError(
"Ollama n'a pas répondu avant le délai configuré "
f"({timeout_label}). Le modèle est peut-être en chargement ou trop lent; "
"augmentez request_timeout_seconds dans config.json, ou mettez 0 pour désactiver le timeout."
) from exc
except requests.RequestException as exc:
raise AIClientError(f"Erreur Ollama: {exc}") from exc
except ValueError as exc:
@@ -65,6 +72,13 @@ def _ask_gemini(prompt: str, config: AppConfig) -> str:
)
response.raise_for_status()
data = response.json()
except requests.Timeout as exc:
timeout_label = "désactivé" if config.request_timeout_seconds is None else f"{config.request_timeout_seconds:.0f} s"
raise AIClientError(
"Gemini n'a pas répondu avant le délai configuré "
f"({timeout_label}). Augmentez request_timeout_seconds dans config.json, "
"ou mettez 0 pour désactiver le timeout."
) from exc
except requests.RequestException as exc:
raise AIClientError(f"Erreur Gemini: {exc}") from exc
except ValueError as exc:
+9 -2
View File
@@ -15,7 +15,7 @@ class AppConfig:
api_key: str = ""
server_url: str = "http://localhost:11434"
hotkey: str = "ctrl+alt+a"
request_timeout_seconds: float = 120.0
request_timeout_seconds: float | None = 300.0
copy_wait_seconds: float = 1.0
type_delay_seconds: float = 0.0
restore_clipboard: bool = True
@@ -33,6 +33,13 @@ def _coerce_provider(value: Any) -> Provider:
return provider # type: ignore[return-value]
def _coerce_timeout(value: Any) -> float | None:
timeout = float(value)
if timeout <= 0:
return None
return timeout
def load_config(path: str | Path = "config.json") -> AppConfig:
cfg_path = Path(path)
if not cfg_path.exists():
@@ -46,7 +53,7 @@ def load_config(path: str | Path = "config.json") -> AppConfig:
api_key=str(data.get("api_key", "")),
server_url=str(data.get("server_url", "http://localhost:11434")).rstrip("/"),
hotkey=str(data.get("hotkey", "ctrl+alt+a")).lower(),
request_timeout_seconds=float(data.get("request_timeout_seconds", 120)),
request_timeout_seconds=_coerce_timeout(data.get("request_timeout_seconds", 300)),
copy_wait_seconds=float(data.get("copy_wait_seconds", 1)),
type_delay_seconds=float(data.get("type_delay_seconds", 0)),
restore_clipboard=bool(data.get("restore_clipboard", True)),