') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); emrg: add llm.jsonl rotation, enrich LLM errors with headers, make stream_options configurable by argszero · Pull Request #130 · argszero/emrg · GitHub
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
11 changes: 11 additions & 0 deletions emrg/config.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,9 @@ class LlmConfig:
context_window: int = 131072
auto_compact_threshold: float = 0.0
models: list[dict] = field(default_factory=list) # [[llm.models]] for /model switching
# stream_options: None means don't send stream_options at all (for APIs like Kimi).
# Default is {"include_usage": False} for OpenAI/DeepSeek compatibility.
stream_options: Optional[dict] = field(default_factory=lambda: {"include_usage": False})


@dataclass
Expand DownExpand Up@@ -59,6 +62,13 @@ def load_config() -> EmrgConfig:
data = tomllib.loads(content)
llm_data = data.get("llm", {})

# stream_options: None = no stream_options sent (for Kimi etc.)
raw_stream_opts = llm_data.get("stream_options")
if raw_stream_opts is None and "stream_options" not in llm_data:
stream_opts: Optional[dict] = {"include_usage": False} # default
else:
stream_opts = raw_stream_opts # explicit None disables it

llm = LlmConfig(
base_url=llm_data.get("base_url", "https://api.openai.com/v1"),
api_key=llm_data.get("api_key", ""),
Expand All@@ -69,6 +79,7 @@ def load_config() -> EmrgConfig:
context_window=llm_data.get("context_window", 131072),
auto_compact_threshold=llm_data.get("auto_compact_threshold", 0.0),
models=llm_data.get("models", []),
stream_options=stream_opts,
)

# Resolve ${ENV_VAR} placeholders in the API key
Expand Down
2 changes: 1 addition & 1 deletion emrg/server/daemon.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -578,7 +578,7 @@ def _build_history_section(self, session: Session) -> str:
f"- **Current history** (may be compacted): `{session.dir_path}/history.jsonl`",
f"- **Daily full history** (never compacted): `{session.dir_path}/history_{today}.jsonl`",
f"- Daily files are named `history_YYMMDD.jsonl`",
f"- LLM raw log: `{session.dir_path}/llm.jsonl`",
f"- LLM raw log: `{session.dir_path}/llm.jsonl` (rotated at 50MB, up to 2 backups)",
"",
"**To read history**: use the `read` tool on `history.jsonl` for the current",
"context, or on a specific `history_YYMMDD.jsonl` file for older messages.",
Expand Down
14 changes: 10 additions & 4 deletions emrg/server/llm.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -58,7 +58,8 @@ def _make_payload(
payload["tools"] = tools
if stream:
payload["stream"] = True
payload["stream_options"] = {"include_usage": False}
if self.config.stream_options is not None:
payload["stream_options"] = self.config.stream_options
return payload

def _headers(self) -> dict:
Expand DownExpand Up@@ -110,8 +111,11 @@ async def chat(
)
continue

logger.error("LLM error: %s %s", resp.status_code, text)
raise RuntimeError(f"LLM request failed: {resp.status_code} - {text}")
hdr = dict(resp.headers)
logger.error("LLM error: %s headers=%s body=%s", resp.status_code, hdr, text)
raise RuntimeError(
f"LLM request failed: {resp.status_code} headers={hdr} body={text}"
)

raise last_error # type: ignore[misc]

Expand DownExpand Up@@ -173,8 +177,10 @@ async def chat_stream(
)
continue
logger.error("LLM stream error: %s %s", resp.status_code, text[:500])
hdr = dict(resp.headers)
raise RuntimeError(
f"LLM stream request failed: {resp.status_code}"
f"LLM stream request failed: {resp.status_code} "
f"headers={hdr} body={text[:1000]}"
)

async for line in resp.aiter_lines():
Expand Down
39 changes: 38 additions & 1 deletion emrg/session.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,6 +15,7 @@

import json
import logging
import os
import secrets
from datetime import datetime
from pathlib import Path
Expand All@@ -23,6 +24,10 @@

logger = logging.getLogger(__name__)

# ── llm.jsonl rotation ────────────────────────────────────────
_LLM_LOG_MAX_BYTES = 50 * 1024 * 1024 # 50 MB
_LLM_LOG_BACKUP_COUNT = 2


def generate_session_id(cwd: Path) -> str:
"""Generate a human-friendly session ID: s_YYMMDD_HHMM_xxxx."""
Expand DownExpand Up@@ -181,17 +186,49 @@ def append_message(self, record: dict) -> None:
self._save_meta()

def append_llm(self, record: dict) -> None:
"""Append an LLM request/response record to llm.jsonl."""
"""Append an LLM request/response record to llm.jsonl.

Automatically rotates the file when it exceeds 50 MB, keeping
up to 2 backup files (llm.jsonl.1, llm.jsonl.2).
"""
if "timestamp" not in record:
record["timestamp"] = datetime.now().isoformat()

entry = {"timestamp": record.pop("timestamp")}
entry.update(record)

line = json.dumps(entry, ensure_ascii=False) + "\n"

# Rotate if file exceeds max size
self._rotate_llm_log()

with open(self._llm_path, "a") as f:
f.write(line)

def _rotate_llm_log(self) -> None:
"""Rotate llm.jsonl if it exceeds _LLM_LOG_MAX_BYTES."""
if not self._llm_path.exists():
return
try:
size = os.path.getsize(self._llm_path)
except OSError:
return
if size < _LLM_LOG_MAX_BYTES:
return

# Shift existing backups: .2 → .3 (delete), .1 → .2, main → .1
for i in range(_LLM_LOG_BACKUP_COUNT, 0, -1):
old = Path(str(self._llm_path) + f".{i}")
new = Path(str(self._llm_path) + f".{i + 1}")
if i == _LLM_LOG_BACKUP_COUNT and new.exists():
new.unlink()
if old.exists():
old.rename(new)

# Rename current to .1
backup_path = Path(str(self._llm_path) + ".1")
self._llm_path.rename(backup_path)

# ── History reading ───────────────────────────────────────

def _read_history(self) -> list[dict]:
Expand Down
Loading