Merge remote-tracking branch 'origin/dev'

This commit is contained in:
pewdiepie-archdaemon
2026-06-30 10:26:46 +00:00
34 changed files with 607 additions and 316 deletions
+9 -2
View File
@@ -18,6 +18,13 @@ DEFAULT_BUDGET = 6000
DEFAULT_HEADROOM = 0.85
def _int_or_zero(value) -> int:
try:
return int(value or 0)
except (TypeError, ValueError):
return 0
def compute_input_token_budget(
configured: int,
context_length: int,
@@ -48,8 +55,8 @@ def compute_input_token_budget(
- When the window is unknown (context_length <= 0), use the conservative
``default`` budget and do NOT scale off the fallback.
"""
configured = int(configured or 0)
context_length = int(context_length or 0)
configured = _int_or_zero(configured)
context_length = _int_or_zero(context_length)
if explicit and configured > 0:
return min(configured, context_length) if context_length > 0 else configured
+15 -3
View File
@@ -345,6 +345,18 @@ def _normalize_ollama_url(url: str) -> str:
return base.rstrip("/") + "/chat"
def _normalize_openai_chat_url(url: str) -> str:
"""Ensure an OpenAI-compatible base URL points at /chat/completions."""
base = (url or "").strip().rstrip("/")
if not base:
return base
if base.endswith("/chat/completions") or base.endswith("/completions"):
return base
if base.endswith("/models"):
base = base[: -len("/models")].rstrip("/")
return base + "/chat/completions"
def _ollama_normalize_messages(messages: List[Dict]) -> List[Dict]:
"""Adapt Odysseus' canonical OpenAI-style messages to native Ollama /api/chat.
@@ -1564,7 +1576,7 @@ def llm_call(url: str, model: str, messages: List[Dict], temperature: float = LL
stream=False, num_ctx=get_context_length(url, model),
)
else:
target_url = url
target_url = _normalize_openai_chat_url(url)
if provider == "copilot":
from src.copilot import apply_request_headers
apply_request_headers(h, messages_copy)
@@ -1768,7 +1780,7 @@ async def llm_call_async(
stream=False, num_ctx=get_context_length(url, model),
)
else:
target_url = url
target_url = _normalize_openai_chat_url(url)
h = _provider_headers(provider, headers)
if provider == "copilot":
from src.copilot import apply_request_headers
@@ -1891,7 +1903,7 @@ async def stream_llm(url: str, model: str, messages: List[Dict], temperature: fl
h = _provider_headers(provider, headers)
payload = _build_chatgpt_responses_payload(model, messages_copy, temperature, max_tokens, stream=True)
else:
target_url = url
target_url = _normalize_openai_chat_url(url)
payload = {
"model": model,
"messages": messages_copy,
+4 -1
View File
@@ -68,6 +68,8 @@ def read_text_file(path: str) -> str:
def split_chunks(text: str, size: int = config.CHUNK_SIZE, overlap: int = config.CHUNK_OVERLAP) -> List[str]:
"""Split text into overlapping chunks."""
if not isinstance(text, str):
return []
text = text.strip()
if not text:
return []
@@ -87,7 +89,8 @@ def split_chunks(text: str, size: int = config.CHUNK_SIZE, overlap: int = config
def tokenize(s: str) -> Set[str]:
"""Tokenize string into words, excluding stop words."""
tokens = re.findall(r"[A-Za-z0-9_\-]+", (s or "").lower())
text = s if isinstance(s, str) else ""
tokens = re.findall(r"[A-Za-z0-9_\-]+", text.lower())
return set(t for t in tokens if t not in config.STOP_WORDS and len(t) > 1)
def load_personal_index(
+6
View File
@@ -79,12 +79,18 @@ def check_outbound_url(
if not raw_ips:
return False, "host does not resolve"
saw_ip = False
for raw in raw_ips:
if not isinstance(raw, str):
continue
try:
ip = ipaddress.ip_address(raw.split("%")[0]) # strip IPv6 zone id
except ValueError:
continue
saw_ip = True
reason = _classify(ip, block_private=block_private)
if reason:
return False, reason
if not saw_ip:
return False, "host does not resolve to an IP"
return True, "ok"