mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-07-14 12:48:03 +00:00
Gate email auto translate behind active chat
This commit is contained in:
+26
-2
@@ -69,6 +69,29 @@ def should_track_interactive_request(path: str, method: str = "GET") -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def _has_active_chat_stream() -> bool:
|
||||||
|
"""Best-effort check for foreground model work that outlives HTTP requests.
|
||||||
|
|
||||||
|
Chat/agent streams are detached from the browser SSE so a stream can keep
|
||||||
|
running after the request that started it has returned. Background LLM
|
||||||
|
tasks must still wait for those runs; otherwise helpers like email
|
||||||
|
auto-translate compete with the user's active chat on the same local model.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
from routes import chat_routes as _chat_routes
|
||||||
|
active_streams = getattr(_chat_routes, "_active_streams", {}) or {}
|
||||||
|
if active_streams:
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
from src import agent_runs
|
||||||
|
runs = getattr(agent_runs, "_RUNS", {}) or {}
|
||||||
|
return any(getattr(run, "status", None) == "running" for run in runs.values())
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def track_interactive_request(path: str = "", method: str = ""):
|
async def track_interactive_request(path: str = "", method: str = ""):
|
||||||
global _ACTIVE_REQUESTS, _LAST_ACTIVITY
|
global _ACTIVE_REQUESTS, _LAST_ACTIVITY
|
||||||
@@ -109,11 +132,12 @@ async def wait_for_interactive_quiet(label: str = "") -> bool:
|
|||||||
async with cond:
|
async with cond:
|
||||||
now = time.monotonic()
|
now = time.monotonic()
|
||||||
quiet_remaining = quiet - (now - _LAST_ACTIVITY)
|
quiet_remaining = quiet - (now - _LAST_ACTIVITY)
|
||||||
if _ACTIVE_REQUESTS <= 0 and quiet_remaining <= 0:
|
active_stream = _has_active_chat_stream()
|
||||||
|
if _ACTIVE_REQUESTS <= 0 and quiet_remaining <= 0 and not active_stream:
|
||||||
return waited
|
return waited
|
||||||
|
|
||||||
waited = True
|
waited = True
|
||||||
timeout = 0.25 if _ACTIVE_REQUESTS > 0 else min(max(quiet_remaining, 0.05), 0.5)
|
timeout = 0.25 if (_ACTIVE_REQUESTS > 0 or active_stream) else min(max(quiet_remaining, 0.05), 0.5)
|
||||||
if deadline is not None:
|
if deadline is not None:
|
||||||
remaining = deadline - now
|
remaining = deadline - now
|
||||||
if remaining <= 0:
|
if remaining <= 0:
|
||||||
|
|||||||
@@ -6356,6 +6356,18 @@ async function _maybeAutoTranslateEmail(reader) {
|
|||||||
const res = await fetch(`${API_BASE}/api/email/config`);
|
const res = await fetch(`${API_BASE}/api/email/config`);
|
||||||
const cfg = await res.json();
|
const cfg = await res.json();
|
||||||
if (!cfg || !cfg.email_auto_translate) return;
|
if (!cfg || !cfg.email_auto_translate) return;
|
||||||
|
try {
|
||||||
|
const sid = window.sessionModule?.getCurrentSessionId?.() || '';
|
||||||
|
if (sid) {
|
||||||
|
const ctrl = new AbortController();
|
||||||
|
const timer = setTimeout(() => ctrl.abort(), 800);
|
||||||
|
const statusRes = await fetch(`${API_BASE}/api/chat/stream_status/${encodeURIComponent(sid)}`, {
|
||||||
|
signal: ctrl.signal,
|
||||||
|
}).catch(() => null);
|
||||||
|
clearTimeout(timer);
|
||||||
|
if (statusRes && statusRes.ok) return;
|
||||||
|
}
|
||||||
|
} catch (_) {}
|
||||||
await _translateEmail(reader, cfg.email_translate_language || 'English', { auto: true });
|
await _translateEmail(reader, cfg.email_translate_language || 'English', { auto: true });
|
||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user