Add selectable LLM profiles and treat LAN Ollama as local.
Saved presets keep URL and model per stage so detect can switch to Ollama without re-entering settings or sending plaintext to OpenRouter. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
af6fe55577
commit
bef422a429
20 files changed
+903
-114
No files matched your search
+30
-7
@@ -1,6 +1,7 @@
|
||||
"""Two independently configured OpenAI-compatible providers: generate vs detect."""
|
||||
from __future__ import annotations
|
||||
|
||||
import ipaddress
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
@@ -107,9 +108,27 @@ def _truthy(name: str) -> bool:
|
||||
return os.environ.get(name, "").strip().lower() in {"1", "true", "yes", "on"}
|
||||
|
||||
|
||||
def is_local_url(url: str) -> bool:
|
||||
"""Loopback, RFC1918/ULA and .local stay in the local trusted zone.
|
||||
|
||||
Homelab Ollama (e.g. 192.168.2.144) is local, not an external provider.
|
||||
"""
|
||||
host = (urlparse(url or "").hostname or "").lower()
|
||||
if not host:
|
||||
return False
|
||||
if host in {"localhost", "127.0.0.1", "::1", "ollama"}:
|
||||
return True
|
||||
if host.endswith(".local"):
|
||||
return True
|
||||
try:
|
||||
ip = ipaddress.ip_address(host)
|
||||
except ValueError:
|
||||
return False
|
||||
return bool(ip.is_loopback or ip.is_private or ip.is_link_local)
|
||||
|
||||
|
||||
def _is_local_url(url: str) -> bool:
|
||||
host = (urlparse(url).hostname or "").lower()
|
||||
return host in {"localhost", "127.0.0.1", "::1", "ollama"}
|
||||
return is_local_url(url)
|
||||
|
||||
|
||||
def _setting(role: str) -> dict:
|
||||
@@ -125,9 +144,9 @@ def _text(env_name: str, db_value: str | None, default: str = "") -> str:
|
||||
env = (os.environ.get(env_name) or "").strip()
|
||||
if env:
|
||||
return env
|
||||
if db_value:
|
||||
return str(db_value).strip()
|
||||
return default
|
||||
if db_value is None:
|
||||
return default
|
||||
return str(db_value).strip()
|
||||
|
||||
|
||||
def _flag(env_name: str, db_value, default: bool = True) -> bool:
|
||||
@@ -155,9 +174,11 @@ def generate_provider() -> ProviderConfig | None:
|
||||
key = (os.environ.get("KANSHO_PROVIDER_KEY") or "").strip()
|
||||
url = _text("KANSHO_PROVIDER_URL", row.get("url"), "https://openrouter.ai/api/v1/chat/completions")
|
||||
model = _text("KANSHO_PROVIDER_MODEL", row.get("model"), "openai/gpt-4o-mini")
|
||||
local = _is_local_url(url)
|
||||
local = is_local_url(url)
|
||||
zdr = local or _flag("KANSHO_PROVIDER_ZDR", row.get("zdr"), True)
|
||||
no_train = local or _flag("KANSHO_PROVIDER_NO_TRAIN", row.get("no_train"), True)
|
||||
if not url or not model:
|
||||
return None
|
||||
if not local and not key:
|
||||
return None
|
||||
if not local and (not zdr or not no_train):
|
||||
@@ -193,8 +214,10 @@ def detect_provider() -> ProviderConfig | None:
|
||||
if not url:
|
||||
return None
|
||||
model = _text("KANSHO_DETECT_PROVIDER_MODEL", row.get("model"), "openai/gpt-4.1-nano")
|
||||
if not model:
|
||||
return None
|
||||
key = (os.environ.get("KANSHO_DETECT_PROVIDER_KEY") or "").strip()
|
||||
local = _is_local_url(url)
|
||||
local = is_local_url(url)
|
||||
if not key and not local:
|
||||
key = (os.environ.get("KANSHO_PROVIDER_KEY") or "").strip()
|
||||
zdr = local or _flag("KANSHO_DETECT_ZDR", row.get("zdr"), True)
|
||||
|
||||
Reference in new issue
Block a user