Initial commit

This commit is contained in:
Alex committed 2026-11-02 22:54:14 +08:00
commit 57fc3525eb
7311 files changed
+1291398

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
"""
app/core/workflow - 工作流执行模块
职责:
- 导出 WorkflowExecutor(保持向后兼容)
"""
from .executor import WorkflowExecutor
__all__ = ['WorkflowExecutor']
+325
View File
@@ -0,0 +1,325 @@
"""Arena Direct unexpected battle redirect detection and auto-recovery guard.
This module is exclusively used for arena.ai direct presets.
When a workflow is actively executing on an Arena direct preset and the page
is unexpectedly redirected to the default battle/home page (https://arena.ai),
this guard detects the deviation, navigates back to the correct Direct page
(either https://arena.ai/text/direct if workflow starts a new conversation,
or the pre-accident conversation URL if resuming an existing conversation),
and allows the workflow engine to restart the workflow cleanly.
"""
from __future__ import annotations
import json
import time
from typing import Any, Callable, Dict, List, Mapping, Optional
from urllib.parse import urlparse
from app.core.config import logger
from app.utils.site_url import extract_remote_site_domain, route_domain_matches
from app.core.driver import driver_for_tab
ARENA_DIRECT_DEFAULT_ENTRY_URL = "https://arena.ai/text/direct"
ARENA_DIRECT_UNEXPECTED_BATTLE_REDIRECT = "arena_direct_unexpected_battle_redirect"
class ArenaUnexpectedBattleRedirectError(RuntimeError):
"""Raised when an active Arena direct workflow detects the page unexpectedly redirected to battle/root."""
def __init__(
self,
message: str = "Arena direct workflow detected unexpected redirection to battle page",
*,
current_url: str = "",
target_url: str = "",
initial_url: str = "",
has_new_chat: bool = False,
) -> None:
super().__init__(message)
self.current_url = current_url
self.target_url = target_url
self.initial_url = initial_url
self.has_new_chat = has_new_chat
class ArenaDirectRecoveryError(RuntimeError):
"""Raised when navigating back to restore the Arena direct page fails."""
def is_arena_page_url(url: Any) -> bool:
"""Check if the given URL belongs to arena.ai or its subdomains."""
try:
raw = str(url or "").strip()
if not raw:
return False
hostname = (urlparse(raw).hostname or "").lower()
return hostname == "arena.ai" or hostname.endswith(".arena.ai")
except Exception:
return False
def is_arena_direct_url(url: Any) -> bool:
"""Check if the given URL is a valid Arena Direct page or conversation page.
Valid direct pages include:
- /text/direct, /direct, or their subpaths
- /c/... (specific direct chat conversation)
- /image/direct, /code/direct
"""
try:
raw = str(url or "").strip()
if not raw:
return False
parsed = urlparse(raw)
hostname = (parsed.hostname or "").lower()
if hostname != "arena.ai" and not hostname.endswith(".arena.ai"):
return False
path = (parsed.path or "").rstrip("/").lower()
if path in {"/text/direct", "/direct", "/image/direct", "/code/direct", "/search/direct"}:
return True
if path.startswith(("/text/direct/", "/direct/", "/image/direct/", "/code/direct/", "/search/direct/")):
return True
if path == "/c" or path.startswith("/c/"):
return True
return False
except Exception:
return False
def is_arena_unexpected_battle_url(url: Any) -> bool:
"""Check if the URL is on arena.ai but is NOT a direct page (i.e. default battle/home page).
Examples of battle / root paths:
- https://arena.ai or https://arena.ai/
- https://arena.ai/text or https://arena.ai/text/
- https://arena.ai/text/side-by-side
- https://arena.ai/side-by-side
- https://arena.ai/battle
"""
if not is_arena_page_url(url):
return False
return not is_arena_direct_url(url)
def is_arena_direct_preset(
domain: str,
preset_name: str = "",
site_config: Optional[Mapping[str, Any]] = None,
) -> bool:
"""Determine whether the specified preset operates in Arena Direct mode."""
if not domain:
return False
clean_domain = str(domain).strip().lower()
if not (route_domain_matches("arena.ai", clean_domain) or clean_domain == "arena.ai"):
return False
name_lower = str(preset_name or "").strip().lower()
# Exclude explicit negative keywords
negative_keywords = ("非直连", "not direct", "indirect", "battle", "双栏")
if any(neg in name_lower for neg in negative_keywords):
return False
if isinstance(site_config, (dict, Mapping)):
catalog = site_config.get("model_catalog")
if isinstance(catalog, dict) and str(catalog.get("source", "")).strip().lower() == "arena_direct":
return True
try:
from app.services.arena_model_catalog import get_arena_model_catalog
catalog = get_arena_model_catalog(domain or "arena.ai", preset_name)
if isinstance(catalog, dict) and str(catalog.get("source", "")).strip().lower() == "arena_direct" and catalog.get("enabled"):
return True
except Exception:
pass
if "direct" in name_lower or "直连" in name_lower:
return True
return False
def workflow_has_new_chat_step(workflow: Optional[List[Dict[str, Any]]]) -> bool:
"""Check if the given workflow definition contains a new conversation / new chat step."""
for step in workflow or []:
if not isinstance(step, dict):
continue
action = str(step.get("action", "") or "").strip().upper()
# 排除非选择器步骤(如 JS 执行、只读提示、等待等),防止 target 中的脚本路径意外触发新对话逻辑
if action in {"JS_EXEC", "READONLY_HINT", "WAIT", "PAGE_FETCH", "STREAM_WAIT", "STREAM_OUTPUT"}:
continue
if action in {"NEW_CHAT", "NEW_CONVERSATION"}:
return True
target = str(step.get("target", "") or "").strip().lower()
if target in {"new_chat_btn", "new_chat", "new_conversation"} or "new_chat" in target:
return True
return False
def resolve_arena_direct_entry_url(
initial_url: Optional[str] = None,
*,
modality: Any = "",
fallback_url: str = ARENA_DIRECT_DEFAULT_ENTRY_URL,
) -> str:
"""Resolve a Direct entry URL from preset modality, then page URL context."""
normalized_modality = str(modality or "").strip().lower()
modality_urls = {
"image": "https://arena.ai/image/direct",
"code": "https://arena.ai/code/direct",
"web": "https://arena.ai/code/direct",
"webdev": "https://arena.ai/code/direct",
"search": "https://arena.ai/search/direct",
"text": "https://arena.ai/text/direct",
"chat": "https://arena.ai/text/direct",
}
if normalized_modality in modality_urls:
return modality_urls[normalized_modality]
try:
path = (urlparse(str(initial_url or "").strip()).path or "").rstrip("/").lower()
except Exception:
path = ""
for prefix, entry_url in (
("/image/direct", "https://arena.ai/image/direct"),
("/code/direct", "https://arena.ai/code/direct"),
("/search/direct", "https://arena.ai/search/direct"),
("/text/direct", "https://arena.ai/text/direct"),
):
if path == prefix or path.startswith(f"{prefix}/"):
return entry_url
return str(fallback_url or ARENA_DIRECT_DEFAULT_ENTRY_URL).strip() or ARENA_DIRECT_DEFAULT_ENTRY_URL
def resolve_arena_direct_recovery_url(
workflow: Optional[List[Dict[str, Any]]],
initial_url: Optional[str] = None,
*,
skip_new_chat: bool = False,
fallback_url: str = ARENA_DIRECT_DEFAULT_ENTRY_URL,
) -> str:
"""Determine the recovery URL based on workflow steps and current session continuity.
- If the workflow starts a brand new conversation (has_new_chat is True and skip_new_chat is False):
return https://arena.ai/text/direct
- If the workflow resumes an existing conversation (skip_new_chat is True or workflow has NO new-chat step):
return initial_url if it is a valid Direct page/conversation URL, falling back to fallback_url.
"""
has_new_chat = workflow_has_new_chat_step(workflow)
wants_new_chat = has_new_chat and not skip_new_chat
if wants_new_chat:
return fallback_url
clean_initial = str(initial_url or "").strip()
if clean_initial and is_arena_direct_url(clean_initial):
return clean_initial
return fallback_url
def recover_arena_direct_page(
tab: Any,
target_url: str,
*,
input_selector: Optional[str] = None,
should_stop: Optional[Callable[[], bool]] = None,
timeout_seconds: float = 20.0,
clock: Callable[[], float] = time.monotonic,
sleep: Callable[[float], None] = time.sleep,
) -> None:
"""Navigate the tab to the specified target Direct URL and wait until it is ready."""
if should_stop is not None and should_stop():
raise ArenaDirectRecoveryError("Arena direct recovery cancelled before navigation")
clean_target = str(target_url or "").strip() or ARENA_DIRECT_DEFAULT_ENTRY_URL
logger.info(f"[ArenaDirectGuard] 正在导航跳转到目标页面以恢复 Direct 会话: {clean_target}")
start_time = clock()
deadline = start_time + max(1.0, float(timeout_seconds))
try:
tab.get(clean_target)
wait = getattr(tab, "wait", None)
if wait is not None and hasattr(wait, "doc_loaded"):
doc_load_timeout = max(0.5, min(10.0, deadline - clock()))
try:
wait.doc_loaded(timeout=doc_load_timeout)
except Exception:
pass
except Exception as exc:
raise ArenaDirectRecoveryError(f"Arena direct recovery navigation failed: {exc}") from exc
selector = str(input_selector or "textarea[name=\"message\"], textarea, [contenteditable=\"true\"]")
probe_script = f"""(() => {{
try {{
let el = null;
try {{
el = document.querySelector({json.dumps(selector)});
}} catch (e) {{
el = document.querySelector('textarea[name="message"], textarea, [contenteditable="true"]');
}}
const isVisible = (e) => {{
if (!e) return false;
const rect = e.getBoundingClientRect();
const style = window.getComputedStyle(e);
return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden';
}};
return {{
documentReady: document.readyState === 'complete' || document.readyState === 'interactive',
inputVisible: isVisible(el),
url: window.location.href
}};
}} catch (err) {{
return {{
documentReady: false,
inputVisible: false,
url: window.location.href,
error: String(err)
}};
}}
}})()"""
while clock() < deadline:
if should_stop is not None and should_stop():
raise ArenaDirectRecoveryError("Arena direct recovery cancelled")
try:
raw = driver_for_tab(tab).run_js(probe_script, timeout=2)
if isinstance(raw, dict) and raw.get("documentReady"):
current_url = str(raw.get("url") or "")
if is_arena_direct_url(current_url) and raw.get("inputVisible"):
logger.info(f"[ArenaDirectGuard] 页面与输入框已成功就绪: url={current_url}")
return
except ArenaDirectRecoveryError:
raise
except Exception:
pass
sleep(0.2)
# Check if we at least reached a valid direct URL
try:
final_url = getattr(tab, "url", "")
if is_arena_direct_url(final_url):
logger.warning("[ArenaDirectGuard] 恢复跳转后等待输入框超时,但页面位于 Direct 路由,尝试继续执行工作流")
return
except Exception:
pass
raise ArenaDirectRecoveryError(f"Arena direct recovery failed: page not restored within {timeout_seconds}s")
__all__ = [
"ARENA_DIRECT_DEFAULT_ENTRY_URL",
"ARENA_DIRECT_UNEXPECTED_BATTLE_REDIRECT",
"ArenaDirectRecoveryError",
"ArenaUnexpectedBattleRedirectError",
"is_arena_direct_preset",
"is_arena_direct_url",
"is_arena_page_url",
"is_arena_unexpected_battle_url",
"recover_arena_direct_page",
"resolve_arena_direct_recovery_url",
"workflow_has_new_chat_step",
]
+582
View File
@@ -0,0 +1,582 @@
"""Arena-only confirmation gate for the first matching stream request.
The network listener is started before the send action. This module owns the
period between that action and the normal ``NetworkMonitor.monitor()`` call so
that Arena's first-target deadline starts only after the page proves the send
was accepted.
"""
from __future__ import annotations
import json
from collections import Counter
import time
from dataclasses import dataclass
from typing import Any, Callable, Dict, Mapping, Optional
from urllib.parse import urlparse
from app.core.config import logger
from app.core.driver import driver_for_tab
ARENA_SEND_NO_TARGET_AFTER_RETRY = "arena_send_no_target_after_retry"
ARENA_PAGE_ERROR = "arena_page_error"
ARENA_NATIVE_STOP_SELECTOR = 'button[aria-label="Stop generation"]'
ARENA_UPLOAD_CARD_SELECTOR = (
'div.group.relative.overflow-hidden:has(img[src^="blob:"])'
':has(button[aria-label="Remove file"]):has(svg.animate-spin)'
)
ARENA_UPLOAD_ERROR_CARD_SELECTOR = (
'div.group.relative.overflow-hidden:has(img[src^="blob:"])'
':has(button[aria-label="Remove file"])'
':has([class*="interactive-negative"], [class*="destructive"], span.text-\\[10px\\])'
)
class ArenaConfirmedSendNoTarget(RuntimeError):
"""Arena accepted the send but no matching target stream arrived in time."""
class ArenaSendUnconfirmed(RuntimeError):
"""The page never supplied enough evidence that the message was accepted."""
class ArenaSendWatchdogCancelled(RuntimeError):
"""The request was cancelled or interrupted while waiting for confirmation."""
class ArenaSendRetryRefreshError(RuntimeError):
"""The required refresh after an Arena watchdog failure did not recover."""
class ArenaPageError(RuntimeError):
"""Arena page displayed a terminal error or policy restriction banner."""
def is_terminal_arena_page_error(message: Any) -> bool:
"""Check if the page error text represents an unrecoverable terminal error."""
lowered = str(message or "").lower()
if not lowered:
return False
verification_patterns = (
"captcha",
"turnstile",
"verification",
"verify you are human",
"challenge",
"recaptcha",
"人机",
"验证",
)
if any(vp in lowered for vp in verification_patterns):
return False
patterns = (
"not permitted to handle this",
"choose another model",
"violates our terms of use",
"this content violates",
"something went wrong with this response",
"access denied",
"response failed",
)
return any(p in lowered for p in patterns)
def is_arena_page_url(url: Any) -> bool:
"""Match only arena.ai and its subdomains, never legacy lmarena.ai."""
try:
hostname = (urlparse(str(url or "")).hostname or "").lower()
except Exception:
return False
return hostname == "arena.ai" or hostname.endswith(".arena.ai")
@dataclass(frozen=True)
class ArenaSendSnapshot:
document_ready: bool = False
input_visible: bool = False
input_empty: bool = False
stop_visible: bool = False
upload_in_progress: bool = False
upload_error: bool = False
upload_error_message: str = ""
page_error: bool = False
page_error_message: str = ""
page_error_items: tuple = ()
@property
def confirms_send(self) -> bool:
return (
self.document_ready
and self.input_visible
and self.input_empty
and self.stop_visible
and not self.upload_in_progress
and not self.upload_error
and not self.page_error
)
class ArenaSendWatchdog:
"""Wait for strict Arena send evidence before applying a fixed 12s deadline."""
POLL_INTERVAL_SECONDS = 0.2
CONFIRMED_TARGET_TIMEOUT_SECONDS = 12.0
UNCONFIRMED_SEND_TIMEOUT_SECONDS = 90.0
def __init__(
self,
*,
tab: Any,
network_monitor: Any,
selectors: Optional[Mapping[str, Any]] = None,
should_stop: Optional[Callable[[], bool]] = None,
workflow_interrupted: Optional[Callable[[], bool]] = None,
clock: Callable[[], float] = time.monotonic,
sleep: Callable[[float], None] = time.sleep,
poll_interval_seconds: float = POLL_INTERVAL_SECONDS,
confirmed_target_timeout_seconds: float = CONFIRMED_TARGET_TIMEOUT_SECONDS,
unconfirmed_send_timeout_seconds: float = UNCONFIRMED_SEND_TIMEOUT_SECONDS,
page_error_baseline: Optional[Mapping[str, Any]] = None,
) -> None:
self.tab = tab
self.network_monitor = network_monitor
self.selectors = dict(selectors or {})
self.should_stop = should_stop or (lambda: False)
self.workflow_interrupted = workflow_interrupted or (lambda: False)
self.clock = clock
self.sleep = sleep
self.poll_interval_seconds = max(0.01, float(poll_interval_seconds))
self.confirmed_target_timeout_seconds = max(
0.01, float(confirmed_target_timeout_seconds)
)
self.unconfirmed_send_timeout_seconds = max(
self.poll_interval_seconds,
float(unconfirmed_send_timeout_seconds),
)
self.page_error_baseline = dict(page_error_baseline or {})
@classmethod
def applies_to(cls, tab: Any) -> bool:
return is_arena_page_url(getattr(tab, "url", ""))
def wait_for_target(self) -> bool:
"""Return once a target is observed, or raise a precise watchdog error.
``poll_send_activity()`` caches any response object it reads. The later
normal network monitor therefore parses the exact same first target.
"""
if not self.applies_to(self.tab):
return False
started_at = self.clock()
unconfirmed_deadline = started_at + self.unconfirmed_send_timeout_seconds
confirmed_at: Optional[float] = None
stable_snapshots = 0
next_snapshot_at = started_at
while True:
self._raise_if_cancelled()
now = self.clock()
confirmed_deadline = (
confirmed_at + self.confirmed_target_timeout_seconds
if confirmed_at is not None
else None
)
deadline_reached = now >= unconfirmed_deadline or (
confirmed_deadline is not None and now >= confirmed_deadline
)
poll_timeout = min(
self.poll_interval_seconds,
max(0.01, unconfirmed_deadline - now),
)
if confirmed_deadline is not None:
poll_timeout = min(
poll_timeout,
max(0.01, confirmed_deadline - now),
)
activity = self._poll_network(poll_timeout)
if bool(activity.get("matched")):
logger.debug("[ArenaWatchdog] matching target observed before deadline")
return True
now = self.clock()
if confirmed_at is not None and now >= confirmed_deadline:
raise ArenaConfirmedSendNoTarget(
"Arena send was confirmed but no matching target stream arrived "
f"within {self.confirmed_target_timeout_seconds:.1f} seconds"
)
if deadline_reached or now >= unconfirmed_deadline:
raise ArenaSendUnconfirmed(
"Arena send confirmation was not observed within 90 seconds"
)
if now >= next_snapshot_at:
snapshot = self._read_snapshot()
if snapshot.upload_error:
error_msg = snapshot.upload_error_message or "Arena 图片附件上传失败 (Error)"
logger.error(f"[ArenaWatchdog] 检测到 Arena 附件上传失败拦截: {error_msg}")
raise ArenaPageError(error_msg)
if self._has_new_terminal_page_error(snapshot):
error_msg = snapshot.page_error_message or "Arena 页面提示错误"
logger.error(f"[ArenaWatchdog] 检测到页面明确错误拦截: {error_msg}")
raise ArenaPageError(error_msg)
if snapshot.confirms_send:
stable_snapshots += 1
if stable_snapshots >= 2 and confirmed_at is None:
confirmed_at = self.clock()
logger.info(
"[ArenaWatchdog] send confirmed by two stable DOM snapshots; "
"starting fixed target-stream deadline"
)
else:
stable_snapshots = 0
next_snapshot_at = now + self.poll_interval_seconds
now = self.clock()
if confirmed_at is not None and (
now - confirmed_at >= self.confirmed_target_timeout_seconds
):
raise ArenaConfirmedSendNoTarget(
"Arena send was confirmed but no matching target stream arrived "
f"within {self.confirmed_target_timeout_seconds:.1f} seconds"
)
wait_until = min(next_snapshot_at, unconfirmed_deadline)
if confirmed_at is not None:
wait_until = min(
wait_until,
confirmed_at + self.confirmed_target_timeout_seconds,
)
remaining = wait_until - self.clock()
if remaining > 0:
self.sleep(remaining)
def _poll_network(self, timeout: float) -> Dict[str, Any]:
try:
activity = self.network_monitor.poll_send_activity(timeout=timeout)
except Exception as exc:
logger.debug(f"[ArenaWatchdog] target poll failed: {exc}")
return {"seen": False, "matched": False, "error": str(exc)}
return activity if isinstance(activity, dict) else {"seen": False, "matched": False}
def _raise_if_cancelled(self) -> None:
try:
stopped = bool(self.should_stop())
except Exception:
stopped = False
try:
interrupted = bool(self.workflow_interrupted())
except Exception:
interrupted = False
if stopped or interrupted:
raise ArenaSendWatchdogCancelled("Arena send watchdog cancelled")
def _read_snapshot(self) -> ArenaSendSnapshot:
input_selector = str(self.selectors.get("input_box") or "")
stop_selector = str(self.selectors.get("stop_btn") or "")
script = self._snapshot_script(input_selector, stop_selector)
try:
raw = driver_for_tab(self.tab).run_js(script, timeout=2)
except Exception as exc:
logger.debug(f"[ArenaWatchdog] DOM snapshot failed: {exc}")
return ArenaSendSnapshot()
if not isinstance(raw, dict):
logger.debug("[ArenaWatchdog] DOM snapshot returned non-dict; ignoring")
return ArenaSendSnapshot()
raw_items = raw.get("pageErrorItems")
items = []
if isinstance(raw_items, list):
for item in raw_items:
if not isinstance(item, dict):
continue
text = str(item.get("text") or "").strip()
if text:
items.append({
"text": text,
"token": str(item.get("token") or "").strip(),
})
return ArenaSendSnapshot(
document_ready=bool(raw.get("documentReady")),
input_visible=bool(raw.get("inputVisible")),
input_empty=bool(raw.get("inputEmpty")),
stop_visible=bool(raw.get("stopVisible")),
upload_in_progress=bool(raw.get("uploadInProgress")),
upload_error=bool(raw.get("uploadError")),
upload_error_message=str(raw.get("uploadErrorMessage") or "").strip(),
page_error=bool(raw.get("pageError")),
page_error_message=str(raw.get("pageErrorMessage") or "").strip(),
page_error_items=tuple(items),
)
@classmethod
def capture_page_error_baseline(
cls,
*,
tab: Any,
selectors: Optional[Mapping[str, Any]] = None,
) -> Dict[str, Any]:
"""Capture visible terminal-error nodes before a send action.
The baseline is intentionally limited to error text/token data. DOM
nodes may be replaced by React after the send; matching text counts
prevent such marker loss from turning a historical banner into a new
terminal error.
"""
selectors = selectors or {}
script = cls._snapshot_script(
str(selectors.get("input_box") or ""),
str(selectors.get("stop_btn") or ""),
)
try:
raw = driver_for_tab(tab).run_js(script, timeout=2)
except Exception as exc:
logger.debug(f"[ArenaWatchdog] error baseline capture failed: {exc}")
return {"available": False, "items": []}
if not isinstance(raw, dict):
return {"available": False, "items": []}
items = raw.get("pageErrorItems")
if not isinstance(items, list):
return {"available": False, "items": []}
normalized = []
for item in items:
if not isinstance(item, dict):
continue
text = str(item.get("text") or "").strip()
if text:
normalized.append({
"text": text,
"token": str(item.get("token") or "").strip(),
})
return {"available": True, "items": normalized, "captured_at": time.time()}
def _has_new_terminal_page_error(self, snapshot: ArenaSendSnapshot) -> bool:
if not snapshot.page_error:
return False
if self.page_error_baseline.get("available") is False:
# A failed pre-submit read must not fall back to a page-wide scan:
# that would recreate the historical-error false positive.
return False
current_items = [
item for item in snapshot.page_error_items
if is_terminal_arena_page_error(item.get("text"))
]
baseline_items = self.page_error_baseline.get("items")
if not isinstance(baseline_items, list):
return is_terminal_arena_page_error(snapshot.page_error_message)
baseline_tokens = {
str(item.get("token") or "").strip()
for item in baseline_items
if isinstance(item, dict) and str(item.get("token") or "").strip()
}
baseline_text_by_token = {
str(item.get("token") or "").strip(): str(item.get("text") or "").strip().casefold()
for item in baseline_items
if isinstance(item, dict)
and str(item.get("token") or "").strip()
and str(item.get("text") or "").strip()
}
baseline_text_counts = Counter(
str(item.get("text") or "").strip().casefold()
for item in baseline_items
if isinstance(item, dict) and str(item.get("text") or "").strip()
)
current_text_counts = Counter(
str(item.get("text") or "").strip().casefold()
for item in current_items
if str(item.get("text") or "").strip()
)
unmatched_historical_text = Counter()
for item in current_items:
text = str(item.get("text") or "").strip()
token = str(item.get("token") or "").strip()
if token:
# A token that survived React reconciliation is authoritative:
# a different token means a newly mounted error node, even if
# its text is identical to a historical banner.
if token in baseline_tokens:
if baseline_text_by_token.get(token, text.casefold()) != text.casefold():
return True
continue
return True
key = text.casefold()
# Marker loss during React replacement leaves no token. In that
# case consume one historical occurrence by text, then treat any
# additional occurrence as a new error.
if baseline_text_counts.get(key, 0) > unmatched_historical_text[key]:
unmatched_historical_text[key] += 1
continue
return True
# A snapshot implementation without item details still gets the
# conservative legacy behavior; normal browser snapshots always have
# pageErrorItems, so historical banners are filtered above.
if not snapshot.page_error_items:
# With a captured baseline, an item-less snapshot is incomplete
# (for example during React replacement), not proof of a new error.
return False
return any(
current_text_counts[key] > baseline_text_counts.get(key, 0)
for key in current_text_counts
)
@staticmethod
def _snapshot_script(input_selector: str, stop_selector: str) -> str:
selectors = json.dumps(
{
"input": input_selector,
"stop": stop_selector,
"nativeStop": ARENA_NATIVE_STOP_SELECTOR,
},
ensure_ascii=True,
)
return f"""
return (() => {{
const selectors = {selectors};
const visible = (element) => {{
if (!element) return false;
const style = window.getComputedStyle(element);
const rect = element.getBoundingClientRect();
return rect.width > 0 && rect.height > 0 && style.display !== 'none'
&& style.visibility !== 'hidden' && style.opacity !== '0';
}};
const find = (selector) => {{
if (!selector) return null;
try {{ return document.querySelector(selector); }} catch (_) {{ return null; }}
}};
const input = find(selectors.input);
const inputValue = input
? String(typeof input.value === 'string' ? input.value : (input.innerText || input.textContent || ''))
: '';
const uploadCards = Array.from(document.querySelectorAll('img[src^="blob:"]')).map((image) => {{
return image.closest('div.group.relative.overflow-hidden') || image.parentElement;
}}).filter(Boolean);
const hasPendingUpload = uploadCards.some((card) => {{
return !!card.querySelector('button[aria-label="Remove file"]')
&& Array.from(card.querySelectorAll('svg.animate-spin')).some(visible);
}});
const errorUploadCards = uploadCards.filter((card) => {{
if (!visible(card)) return false;
const hasNegativeClass = Boolean(
(card.matches && card.matches('[class*="interactive-negative"], [class*="destructive"]'))
|| card.querySelector('[class*="interactive-negative"], [class*="destructive"]')
);
const cardText = String(card.innerText || card.textContent || '').trim();
const hasErrorWord = /\berror\b|上传失败|failed/i.test(cardText);
return !!card.querySelector('button[aria-label="Remove file"]') && (hasNegativeClass || hasErrorWord);
}});
const hasUploadError = errorUploadCards.length > 0;
const uploadErrorMessage = hasUploadError
? String(errorUploadCards[0].innerText || errorUploadCards[0].textContent || 'Arena 图片附件上传失败 (Error)').replace(/\\s+/g, ' ').trim()
: '';
const errorSelectors = [
'[role="alert"]', '[aria-live="assertive"]', '[data-testid*="error" i]',
'[class*="destructive" i]', '[data-state="error"]', '[data-status="error"]',
'div[class*="bg-destructive"]', 'div[class*="text-destructive"]'
];
const errorElements = Array.from(document.querySelectorAll(errorSelectors.join(','))).filter((element) => {{
if (!visible(element)) return false;
if (element.closest('.prose, pre, code, ol, [data-message-author-role]')) return false;
return true;
}});
const errorTexts = errorElements
.map((element) => String(element.innerText || element.textContent || element.getAttribute('title') || '').trim())
.filter(Boolean);
const combinedErrorText = errorTexts.join(' ').toLowerCase();
const errorPattern = /not permitted to handle this|choose another model|violates our terms of use|this content violates|something went wrong with this response|response failed|access denied/i;
const hasErrorMatch = errorPattern.test(combinedErrorText);
const pageError = hasErrorMatch;
const pageErrorItems = errorElements.map((element, index) => {{
let token = '';
try {{
token = String(element.getAttribute('data-codex-arena-error-token') || '');
if (!token) {{
token = `arena-error-${{Date.now()}}-${{index}}-${{Math.random().toString(36).slice(2)}}`;
element.setAttribute('data-codex-arena-error-token', token);
}}
}} catch (_) {{}}
const text = String(element.innerText || element.textContent || element.getAttribute('title') || '').trim();
return {{ text, token }};
}}).filter((item) => item.text);
let rawErrorMessage = errorTexts.find((t) => errorPattern.test(t.toLowerCase())) || (hasErrorMatch ? errorTexts[0] : '') || '';
const pageErrorMessage = rawErrorMessage.replace(/\\s+/g, ' ').trim();
return {{
documentReady: location.hostname === 'arena.ai' || location.hostname.endsWith('.arena.ai')
? document.readyState === 'complete'
: false,
inputVisible: visible(input),
inputEmpty: inputValue.trim().length === 0,
stopVisible: visible(find(selectors.stop)) || visible(find(selectors.nativeStop)),
uploadInProgress: hasPendingUpload,
uploadError: Boolean(hasUploadError),
uploadErrorMessage: String(uploadErrorMessage || ''),
pageError: Boolean(pageError),
pageErrorMessage: String(pageErrorMessage || ''),
pageErrorItems
}};
}})();
"""
def refresh_arena_page_for_retry(
tab: Any,
*,
input_selector: str,
should_stop: Optional[Callable[[], bool]] = None,
timeout_seconds: float = 30.0,
clock: Callable[[], float] = time.monotonic,
sleep: Callable[[float], None] = time.sleep,
) -> None:
"""Refresh Arena and wait until the configured input is available again."""
if not is_arena_page_url(getattr(tab, "url", "")):
raise ArenaSendRetryRefreshError("Arena retry refresh requested on a non-Arena page")
try:
tab.refresh(ignore_cache=True)
wait = getattr(tab, "wait", None)
if wait is not None and hasattr(wait, "doc_loaded"):
wait.doc_loaded(timeout=min(15.0, max(1.0, timeout_seconds)))
except Exception as exc:
raise ArenaSendRetryRefreshError(f"Arena retry refresh failed: {exc}") from exc
deadline = clock() + max(1.0, float(timeout_seconds))
selector = str(input_selector or "textarea, [contenteditable=\"true\"]")
while clock() < deadline:
try:
if should_stop is not None and should_stop():
raise ArenaSendWatchdogCancelled("Arena retry refresh cancelled")
raw = driver_for_tab(tab).run_js(
ArenaSendWatchdog._snapshot_script(selector, ""),
timeout=2,
)
if isinstance(raw, dict) and raw.get("documentReady") and raw.get("inputVisible"):
return
except ArenaSendWatchdogCancelled:
raise
except Exception:
pass
sleep(0.2)
raise ArenaSendRetryRefreshError("Arena retry refresh did not restore the input box")
__all__ = [
"ARENA_NATIVE_STOP_SELECTOR",
"ARENA_PAGE_ERROR",
"ARENA_SEND_NO_TARGET_AFTER_RETRY",
"ARENA_UPLOAD_CARD_SELECTOR",
"ARENA_UPLOAD_ERROR_CARD_SELECTOR",
"ArenaConfirmedSendNoTarget",
"ArenaPageError",
"ArenaSendRetryRefreshError",
"ArenaSendSnapshot",
"ArenaSendUnconfirmed",
"ArenaSendWatchdog",
"ArenaSendWatchdogCancelled",
"is_arena_page_url",
"is_terminal_arena_page_error",
"refresh_arena_page_for_retry",
]
File diff suppressed because it is too large. Load diff
+246
View File
@@ -0,0 +1,246 @@
"""Shared file transports and conservative upload transaction coordination.
An action acknowledgement is not upload readiness. Once an action might have
happened, no other transport is tried without reconciliation (fail closed).
"""
from __future__ import annotations
import base64
import json
import mimetypes
import time
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from app.core.config import WorkflowError
from app.core.tab_pool import get_clipboard_lock
from app.utils.attachments import attachment_config, type_allowed, MIB
from app.utils.platform import get_primary_modifier_key
from app.core.driver import as_element, driver_for_tab
class DispatchState(str, Enum):
NOT_DISPATCHED = "not_dispatched"
DISPATCHED = "dispatched"
UNKNOWN = "unknown"
@dataclass(frozen=True)
class DispatchResult:
state: DispatchState
transport: str
reason: str = ""
class AttachmentUploadCoordinator:
def __init__(self, tab, *, selectors=None, monitor=None, config=None,
cancelled=lambda: False, focus=None):
self.tab, self.monitor = tab, monitor
self.selectors = selectors or {}
self.config = attachment_config(config)
self.cancelled, self.focus = cancelled, focus
self.last_result = None
self.completed_paths = []
self.total_bytes = 0
self.tainted = False
self.resource_batch = None
self.planned_items = []
def _check(self):
if self.cancelled():
raise WorkflowError("attachment_cancelled")
if self.tainted:
raise WorkflowError("attachment_upload_unconfirmed")
def _element(self, selector):
if not selector:
return None
try:
return driver_for_tab(self.tab).find(selector, timeout=0.6)
except Exception:
return None
def _inputs(self):
configured = self.selectors.get("file_input")
try:
if configured:
return list(driver_for_tab(self.tab).find_all(configured, timeout=0.6) or [])
# Prefer composer-scoped lookup; retain legacy page lookup if no root was configured.
root_selector = self.selectors.get("composer_root")
root = self._element(root_selector) if root_selector else self.tab
return list(as_element(root).find_all('css:input[type="file"]', timeout=0.6) or []) if root else []
except Exception:
return []
def _file_input(self, path, mime):
for ele in self._inputs():
self._check()
try:
if ele.attr("disabled") is not None:
continue
accept = [v.strip().lower() for v in str(ele.attr("accept") or "").split(",") if v.strip()]
if not type_allowed(path.name, mime, accept):
continue
except Exception:
continue # not a usable target; no side effect yet
try:
# DrissionPage's file input uses CDP and dispatches its own change event.
# Never dispatch a second change here: frameworks may upload twice.
ele.input(str(path))
return DispatchResult(DispatchState.DISPATCHED, "file_input")
except Exception:
return DispatchResult(DispatchState.UNKNOWN, "file_input", "input_acknowledgement_missing")
return DispatchResult(DispatchState.NOT_DISPATCHED, "file_input", "no_compatible_input")
def _cdp_drop(self, path, mime):
zone = self._element(self.selectors.get("drop_zone"))
if zone is None:
return DispatchResult(DispatchState.NOT_DISPATCHED, "cdp_drop")
dropped = False
try:
point = as_element(zone).run_js("this.scrollIntoView({block:'center'}); const r=this.getBoundingClientRect(); return {x:r.x+r.width/2,y:r.y+r.height/2};")
if not isinstance(point, dict) or not all(isinstance(point.get(k), (int, float)) and point[k] > 0 for k in ("x", "y")):
return DispatchResult(DispatchState.NOT_DISPATCHED, "cdp_drop", "invalid_target")
data = {"items": [], "files": [str(path)], "dragOperationsMask": 1}
for event in ("dragEnter", "dragOver", "drop"):
self._check()
dropped = event == "drop"
driver_for_tab(self.tab).run_cdp("Input.dispatchDragEvent", type=event, x=point["x"], y=point["y"], data=data, modifiers=0)
return DispatchResult(DispatchState.DISPATCHED, "cdp_drop")
except WorkflowError:
raise
except Exception:
return DispatchResult(DispatchState.UNKNOWN if dropped else DispatchState.NOT_DISPATCHED, "cdp_drop", "cdp_unavailable")
def _js_drop(self, path, mime):
zone = self._element(self.selectors.get("drop_zone"))
if zone is None or path.stat().st_size > 4 * MIB:
return DispatchResult(DispatchState.NOT_DISPATCHED, "js_drop", "no_target_or_over_4mb")
# Bound the fallback's memory amplification. Large files use CDP/file input.
payload = json.dumps({"name": path.name, "mime": mime, "data": base64.b64encode(path.read_bytes()).decode("ascii")})
script = """
const p=PAYLOAD;
const bytes=Uint8Array.from(atob(p.data), c=>c.charCodeAt(0));
const dt=new DataTransfer(); dt.items.add(new File([bytes],p.name,{type:p.mime}));
for(const type of ['dragenter','dragover','drop']) this.dispatchEvent(new DragEvent(type,{bubbles:true,cancelable:true,dataTransfer:dt}));
return true;
""".replace("PAYLOAD", payload)
try:
as_element(zone).run_js(script)
return DispatchResult(DispatchState.DISPATCHED, "js_drop")
except Exception:
return DispatchResult(DispatchState.UNKNOWN, "js_drop", "drop_acknowledgement_missing")
def _clipboard(self, path, mime, image=False):
from app.utils.system_clipboard import (copy_file_to_native_clipboard, copy_image_to_native_clipboard,
ClipboardUnsupportedError, ClipboardDependencyError)
name = "image_clipboard" if image else "file_clipboard"
if image and not mime.startswith("image/"):
return DispatchResult(DispatchState.NOT_DISPATCHED, name)
pasted = False
try:
with get_clipboard_lock():
self._check()
if not self.focus or not self.focus():
return DispatchResult(DispatchState.NOT_DISPATCHED, name, "focus_not_confirmed")
copier = copy_image_to_native_clipboard if image else copy_file_to_native_clipboard
copier(str(path))
modifier = get_primary_modifier_key()
pasted = True
driver_for_tab(self.tab).actions.key_down(modifier).key_down("V").key_up("V").key_up(modifier)
return DispatchResult(DispatchState.DISPATCHED, name)
except (ClipboardUnsupportedError, ClipboardDependencyError):
return DispatchResult(DispatchState.NOT_DISPATCHED, name, "clipboard_unavailable")
except WorkflowError:
raise
except Exception:
return DispatchResult(DispatchState.UNKNOWN if pasted else DispatchState.NOT_DISPATCHED, name)
def _dispatch(self, name, path, mime):
if name == "file_input":
result = self._file_input(path, mime)
if result.state == DispatchState.NOT_DISPATCHED:
button = self._element(self.selectors.get("upload_btn"))
if button is not None:
try:
button.click()
except Exception:
pass # opening a chooser does not attach a file
self._check()
result = self._file_input(path, mime)
return result
if name == "cdp_drop":
return self._cdp_drop(path, mime)
if name == "js_drop":
return self._js_drop(path, mime)
return self._clipboard(path, mime, image=name == "image_clipboard")
def upload(self, filepath, mime=None):
self._check()
if not self.config["enabled"]:
raise WorkflowError("attachments_disabled")
if self.monitor is None:
raise WorkflowError("attachment_monitor_unavailable")
path = Path(filepath).resolve()
mime = mime or mimetypes.guess_type(path.name)[0] or "application/octet-stream"
try:
size = path.stat().st_size
except OSError:
raise WorkflowError("attachment_file_unavailable") from None
if not size:
raise WorkflowError("attachment_empty")
# Include not-yet-uploaded API attachments when uploading a generated prompt file.
# This catches combined quotas BEFORE the first upload side effect.
pending = [item for item in self.planned_items if str(item.path.resolve()) != str(path)
and str(item.path.resolve()) not in self.completed_paths]
if len(self.completed_paths) + 1 + len(pending) > self.config["max_count"]:
raise WorkflowError("attachment_count_exceeded")
if self.total_bytes + size + sum(item.byte_size for item in pending) > self.config["max_total_mb"] * MIB:
raise WorkflowError("attachment_size_exceeded")
if not type_allowed(path.name, mime, self.config["allowed_types"]):
raise WorkflowError("attachment_type_unsupported")
if len(self.completed_paths) >= self.config["max_count"]:
raise WorkflowError("attachment_count_exceeded")
if size > self.config["max_file_mb"] * MIB or self.total_bytes + size > self.config["max_total_mb"] * MIB:
raise WorkflowError("attachment_size_exceeded")
deadline = time.monotonic() + self.config["ready_timeout"]
names = [path.name] # no stem-only match against unrelated composer text
for name in self.config["transport_order"]:
self._check()
if time.monotonic() >= deadline:
break
baseline = self.monitor.begin_tracking(expected_names=names)
if not baseline:
raise WorkflowError("attachment_monitor_unavailable")
if self.resource_batch is not None:
self.resource_batch.may_be_reading = True
try:
result = self._dispatch(name, path, mime)
except WorkflowError:
raise
except Exception:
result = DispatchResult(DispatchState.UNKNOWN, name, "transport_failed")
self.last_result = result
if result.state == DispatchState.NOT_DISPATCHED:
continue
# Once a side effect is possible, no blind retry or transport fallback.
self.tainted = True
remaining = deadline - time.monotonic()
if remaining <= 0:
raise WorkflowError("attachment_upload_unconfirmed")
ready = self.monitor.wait_until_ready(
expected_names=names, require_observed=True, require_send_enabled=False,
accept_existing=False, start_new_tracking=False, require_fresh_preview=True,
max_wait=remaining, hard_max_wait=remaining, idle_timeout=remaining,
label=f"attachment:{name}",
)
if self.cancelled():
raise WorkflowError("attachment_cancelled")
if not ready.get("success"):
raise WorkflowError("attachment_upload_rejected" if ready.get("reason") == "upload_rejected" else "attachment_upload_unconfirmed")
self.tainted = False
self.completed_paths.append(str(path))
self.total_bytes += size
return True
raise WorkflowError("attachment_transport_unavailable")
+176
View File
@@ -0,0 +1,176 @@
"""
app/core/workflow/error_handlers.py - 通用工作流错误与重试恢复处理器注册中心
职责:
- 管理工作流重试错误分类与分发;
- 将针对特定厂商或站点的页面恢复逻辑(如 Arena Direct Battle 重定向恢复、Arena 监控重试)解耦为独立 Handler;
- 支持动态注册自定义可重试错误与处理回调。
"""
from __future__ import annotations
import logging
from typing import Any, Callable, Dict, Generator, List, Optional, Set
from app.core.config import logger
from app.core.workflow.arena_direct_guard import (
ARENA_DIRECT_UNEXPECTED_BATTLE_REDIRECT,
ArenaDirectRecoveryError,
recover_arena_direct_page,
)
from app.core.workflow.arena_send_watchdog import (
ARENA_SEND_NO_TARGET_AFTER_RETRY,
ArenaSendRetryRefreshError,
ArenaSendWatchdogCancelled,
refresh_arena_page_for_retry,
)
WorkflowRetryHandlerFunc = Callable[[Any, Any, Any], Optional[Generator[str, None, None]]]
_RETRY_HANDLERS: Dict[str, WorkflowRetryHandlerFunc] = {}
_RETRIABLE_ERROR_CODES: Set[str] = {
"send_unconfirmed",
"new_chat_transition_timeout",
"arena_direct_unexpected_battle_redirect",
"arena_send_no_target",
}
def register_retriable_error_code(error_code: str) -> None:
"""注册可重试的错误码。"""
code = str(error_code or "").strip().lower()
if code:
_RETRIABLE_ERROR_CODES.add(code)
def is_retriable_workflow_error(error_code: str) -> bool:
"""判断错误码是否为已注册的可重试错误。"""
code = str(error_code or "").strip().lower()
return code in _RETRIABLE_ERROR_CODES
def register_workflow_retry_handler(
error_code: str,
handler: WorkflowRetryHandlerFunc,
) -> None:
"""注册指定错误码的重试恢复处理器。"""
code = str(error_code or "").strip().lower()
if not code:
raise ValueError("error_code cannot be empty")
if not callable(handler):
raise ValueError(f"handler for {error_code} must be callable")
_RETRY_HANDLERS[code] = handler
register_retriable_error_code(code)
logger.debug(f"[WORKFLOW_ERROR] 已注册重试处理器: {code}")
def get_workflow_retry_handler(error_code: str) -> Optional[WorkflowRetryHandlerFunc]:
"""获取指定错误码的重试恢复处理器。"""
code = str(error_code or "").strip().lower()
return _RETRY_HANDLERS.get(code)
def _handle_arena_direct_unexpected_battle_redirect(
session: Any,
should_stop: Any,
formatter: Any,
) -> Optional[Generator[str, None, None]]:
def _gen():
try:
recovery_url = str(
getattr(session, "_arena_direct_recovery_url", "") or ""
).strip()
recovery_input_selector = str(
getattr(session, "_arena_direct_input_selector", "") or ""
).strip()
recover_arena_direct_page(
session.tab,
target_url=recovery_url,
input_selector=recovery_input_selector,
should_stop=should_stop,
)
setattr(session, "_arena_direct_recovered_retry", True)
except ArenaDirectRecoveryError as exc:
logger.error(
f"[{getattr(session, 'id', 'unknown')}] Arena direct recovery failed: {exc}"
)
yield formatter.pack_error(
f"stream_terminal_error:{exc}",
code="arena_direct_recovery_failed",
)
yield formatter.pack_finish()
except Exception as exc:
logger.error(
f"[{getattr(session, 'id', 'unknown')}] Arena direct recovery unexpected error: {exc}"
)
yield formatter.pack_error(
f"stream_terminal_error:{exc}",
code="arena_direct_recovery_failed",
)
yield formatter.pack_finish()
return _gen()
def _handle_arena_send_no_target(
session: Any,
should_stop: Any,
formatter: Any,
) -> Optional[Generator[str, None, None]]:
def _gen():
try:
input_selector = str(
getattr(session, "_arena_watchdog_input_selector", "")
or getattr(session, "_arena_direct_input_selector", "")
or 'textarea, [contenteditable="true"]'
).strip()
refresh_arena_page_for_retry(
session.tab,
input_selector=input_selector,
should_stop=should_stop,
)
except ArenaSendWatchdogCancelled:
return
except ArenaSendRetryRefreshError as exc:
logger.error(
f"[{getattr(session, 'id', 'unknown')}] Arena watchdog retry refresh failed: {exc}"
)
yield formatter.pack_error(
f"stream_terminal_error:{ARENA_SEND_NO_TARGET_AFTER_RETRY}",
code=ARENA_SEND_NO_TARGET_AFTER_RETRY,
status_code=422,
retryable=False,
)
yield formatter.pack_finish()
except Exception as exc:
logger.error(
f"[{getattr(session, 'id', 'unknown')}] Arena watchdog retry refresh unexpected error: {exc}"
)
yield formatter.pack_error(
f"stream_terminal_error:{ARENA_SEND_NO_TARGET_AFTER_RETRY}",
code=ARENA_SEND_NO_TARGET_AFTER_RETRY,
status_code=422,
retryable=False,
)
yield formatter.pack_finish()
return _gen()
# 注册内置错误恢复处理器
register_workflow_retry_handler(
ARENA_DIRECT_UNEXPECTED_BATTLE_REDIRECT,
_handle_arena_direct_unexpected_battle_redirect,
)
register_workflow_retry_handler(
"arena_send_no_target",
_handle_arena_send_no_target,
)
__all__ = [
"get_workflow_retry_handler",
"is_retriable_workflow_error",
"register_retriable_error_code",
"register_workflow_retry_handler",
]
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+957
View File
@@ -0,0 +1,957 @@
"""
app/core/workflow/executor_interaction.py - 工作流执行器页面交互 Mixin
主要功能:
- 管理后台标签页的保活、焦点模拟与可见性模拟
- 页面元素稳定性检测与可交互状态等待
- 跨标签页并发点击的执行排队机制
"""
import time
import threading
from contextlib import contextmanager
from typing import Any, Callable, Dict, Optional
from app.core.config import BrowserConstants, logger
from app.core.elements import ElementFinder
from app.core.page_lifecycle import (
BACKGROUND_WAKE_CDP_TIMEOUT,
BACKGROUND_WAKE_JS_TIMEOUT,
install_visibility_emulation,
restore_visibility_emulation,
)
from app.core.driver import as_element, driver_for_tab
class _PageInteractionGate:
"""Throttle active page interactions across tabs to reduce renderer spikes."""
def __init__(self):
self._condition = threading.Condition()
self._active_count = 0
self._next_slot_at = 0.0
@contextmanager
def hold(
self,
*,
label: str,
session_id: str = "",
max_concurrent: int = 1,
timeout: float = 20.0,
min_interval: float = 0.25,
cancel_checker: Optional[Callable[[], bool]] = None,
):
acquired = self._acquire(
label=label,
session_id=session_id,
max_concurrent=max_concurrent,
timeout=timeout,
cancel_checker=cancel_checker,
)
try:
yield acquired
finally:
if acquired:
self._release(min_interval=min_interval)
def _acquire(
self,
*,
label: str,
session_id: str,
max_concurrent: int,
timeout: float,
cancel_checker: Optional[Callable[[], bool]] = None,
) -> bool:
limit = max(1, int(max_concurrent or 1))
wait_timeout = max(0.0, float(timeout or 0.0))
deadline = time.time() + wait_timeout if wait_timeout > 0 else None
while True:
if cancel_checker and cancel_checker():
return False
with self._condition:
now = time.time()
if self._active_count < limit and now >= self._next_slot_at:
self._active_count += 1
return True
if deadline is not None:
remaining = deadline - now
if remaining <= 0:
self._active_count += 1
logger.warning(
f"[INTERACT] throttle wait exceeded for {session_id or '-'}:{label}, "
"continuing fail-open"
)
return True
else:
remaining = 0.25
next_gap = max(0.0, self._next_slot_at - now)
wait_for = min(max(0.05, next_gap), remaining) if deadline is not None else max(0.05, next_gap)
self._condition.wait(timeout=max(0.05, min(wait_for, 0.25)))
def _release(self, *, min_interval: float):
with self._condition:
if self._active_count > 0:
self._active_count -= 1
self._next_slot_at = max(self._next_slot_at, time.time() + max(0.0, float(min_interval or 0.0)))
self._condition.notify_all()
_PAGE_INTERACTION_GATE = _PageInteractionGate()
_INTERACTION_TIMING_LOG_THRESHOLD = 0.25
# ================= 工作流执行器 =================
class WorkflowExecutorInteractionMixin:
@staticmethod
def _log_interaction_timing(
label: str,
phase: str,
elapsed: float,
*,
prefix: str = "INTERACT",
extra: str = "",
) -> None:
if elapsed < _INTERACTION_TIMING_LOG_THRESHOLD:
return
suffix = f", {extra}" if extra else ""
logger.debug(
f"[{prefix}_TIMING] "
f"target={label or '-'}, phase={phase}, elapsed={elapsed:.2f}s{suffix}"
)
def _get_page_interaction_settings(self) -> Dict[str, Any]:
return {
"enabled": self._coerce_bool(
BrowserConstants.get("PAGE_INTERACTION_THROTTLE_ENABLED"),
True,
),
"max_concurrent": self._coerce_int(
BrowserConstants.get("PAGE_INTERACTION_MAX_CONCURRENT"),
3,
minimum=1,
),
"max_wait": self._coerce_float(
BrowserConstants.get("PAGE_INTERACTION_MAX_WAIT"),
20.0,
minimum=0.0,
),
"min_interval": self._coerce_float(
BrowserConstants.get("PAGE_INTERACTION_MIN_INTERVAL"),
0.25,
minimum=0.0,
),
"ready_timeout": self._coerce_float(
BrowserConstants.get("PAGE_INTERACTION_READY_TIMEOUT"),
1.5,
minimum=0.0,
),
"stable_samples": self._coerce_int(
BrowserConstants.get("PAGE_INTERACTION_STABLE_SAMPLES"),
2,
minimum=1,
),
"sample_interval": self._coerce_float(
BrowserConstants.get("PAGE_INTERACTION_SAMPLE_INTERVAL"),
0.12,
minimum=0.02,
),
"rect_tolerance": self._coerce_int(
BrowserConstants.get("PAGE_INTERACTION_RECT_TOLERANCE"),
3,
minimum=0,
),
}
def _get_input_stability_wait_settings(self) -> Dict[str, Any]:
advanced = self._site_advanced_config if isinstance(self._site_advanced_config, dict) else {}
timeout = self._coerce_float(
advanced.get("input_box_stability_wait_timeout"),
1.5,
minimum=0.2,
)
return {
"enabled": self._coerce_bool(
advanced.get("input_box_stability_wait_enabled"),
False,
),
"after_new_chat_only": self._coerce_bool(
advanced.get("input_box_stability_wait_after_new_chat_only"),
True,
),
"timeout": min(timeout, 10.0),
"stable_samples": 2,
"sample_interval": 0.18,
}
def _clear_target_element_cache(self, selector: str, target_key: str = "") -> None:
if selector:
self.finder.remove_from_cache(selector)
for fallback_selector in ElementFinder.FALLBACK_SELECTORS.get(target_key or "", []):
self.finder.remove_from_cache(fallback_selector)
@staticmethod
def _build_element_stability_signature(ele) -> Optional[tuple]:
if ele is None:
return None
backend_id = getattr(ele, "_backend_id", None)
tag = str(getattr(ele, "tag", "") or "")
try:
rect = getattr(ele, "rect", None)
location = getattr(rect, "location", None) or (0, 0)
size = getattr(rect, "size", None) or (0, 0)
return (
backend_id,
tag,
int(location[0]),
int(location[1]),
int(size[0]),
int(size[1]),
)
except Exception:
if backend_id is None and not tag:
return None
return (backend_id, tag)
def _wait_for_fill_target_stability(self, selector: str, target_key: str):
settings = self._get_input_stability_wait_settings()
if not settings["enabled"] or (target_key or "") != "input_box":
return None
if settings["after_new_chat_only"] and not self._input_stability_wait_pending:
return None
self._input_stability_wait_pending = False
stable_needed = max(1, int(settings["stable_samples"]))
sample_interval = max(0.05, float(settings["sample_interval"]))
deadline = time.time() + max(0.2, float(settings["timeout"]))
stable_count = 0
last_signature = None
latest_element = None
while time.time() < deadline:
if self._check_cancelled():
return latest_element
self._clear_target_element_cache(selector, target_key)
sample = self.finder.find_with_fallback(
selector,
target_key,
timeout=min(sample_interval, 0.3),
)
signature = self._build_element_stability_signature(sample)
latest_element = sample or latest_element
if signature is not None and signature == last_signature:
stable_count += 1
else:
stable_count = 1 if signature is not None else 0
last_signature = signature
if stable_count >= stable_needed and latest_element is not None:
logger.debug(
"[FILL_STABLE] 输入框已稳定 "
f"(target={target_key}, samples={stable_count}, timeout={settings['timeout']:.2f}s)"
)
return latest_element
time.sleep(sample_interval)
logger.debug_throttled(
f"fill.stability.{target_key or 'input'}",
f"[FILL_STABLE] 输入框稳定等待超时,继续沿用原流程: target={target_key}, timeout={settings['timeout']:.2f}s",
interval_sec=5.0,
)
return latest_element
def _note_fill_completion(self, text: str, *, after_new_chat: bool = False) -> None:
self._last_fill_completed_at = time.time()
self._last_fill_text_length = max(0, len(text or ""))
self._last_fill_after_new_chat = bool(after_new_chat)
logger.debug(
f"[FILL_COMPLETED] 输入框填充完成: expected_len={self._last_fill_text_length}, "
f"after_new_chat={after_new_chat}"
)
def _get_recent_fill_send_wait_timeout(self, target_key: str, default_timeout: float) -> float:
if (target_key or "") != "send_btn":
return default_timeout
completed_at = float(getattr(self, "_last_fill_completed_at", 0.0) or 0.0)
if completed_at <= 0:
return default_timeout
fill_age = time.time() - completed_at
if fill_age < 0 or fill_age > 12.0:
return default_timeout
text_len = int(getattr(self, "_last_fill_text_length", 0) or 0)
if text_len <= 0:
return default_timeout
extra_timeout = 0.0
if bool(getattr(self, "_last_fill_after_new_chat", False)):
extra_timeout += 1.2
if text_len >= 20000:
extra_timeout += min(2.8, text_len / 60000.0)
if extra_timeout <= 0:
return default_timeout
return min(6.0, max(default_timeout, default_timeout + extra_timeout))
def _refresh_target_element(self, selector: str, target_key: str, *, timeout: float = 0.3):
if not selector and not target_key:
return None
self._clear_target_element_cache(selector, target_key)
try:
return self.finder.find_with_fallback(
selector,
target_key,
timeout=timeout,
)
except Exception:
return None
def _element_accepts_text_input(self, ele) -> bool:
if ele is None:
return False
try:
result = driver_for_tab(self.tab).run_js(
"""
try {
const el = arguments[0];
if (!el || !el.isConnected) return false;
const tag = (el.tagName || '').toLowerCase();
return tag === 'textarea'
|| tag === 'input'
|| !!el.isContentEditable
|| el.getAttribute('contenteditable') === 'true';
} catch (e) {
return false;
}
""",
ele,
)
except Exception:
return False
return bool(result)
def _resolve_active_text_input(self):
try:
active_ele = driver_for_tab(self.tab).run_js("return document.activeElement")
except Exception:
active_ele = None
if self._element_accepts_text_input(active_ele):
try:
info = driver_for_tab(self.tab).run_js("""
const el = arguments[0];
return {
tag: (el.tagName || '').toLowerCase(),
id: el.id || '',
className: el.className || '',
name: el.name || ''
};
""", active_ele)
logger.debug(
f"[ACTIVE_INPUT_RESOLVED] Found active text input: "
f"tag={info.get('tag')!r}, id={info.get('id')!r}, "
f"class={info.get('className')!r}, name={info.get('name')!r}"
)
except Exception as e:
logger.debug(f"[ACTIVE_INPUT_RESOLVED] 获取活动输入框特征异常: {e}")
return active_ele
return None
@contextmanager
def _page_interaction_slot(self, action: str, target_key: str = ""):
settings = self._get_page_interaction_settings()
label = f"{action}:{target_key}" if target_key else str(action or "interaction")
if not settings["enabled"]:
with self._wake_page_for_interaction(label):
yield True
return
session_id = str(getattr(self.session, "id", "") or "")
gate_started = time.perf_counter()
with _PAGE_INTERACTION_GATE.hold(
label=label,
session_id=session_id,
max_concurrent=settings["max_concurrent"],
timeout=settings["max_wait"],
min_interval=settings["min_interval"],
cancel_checker=self._check_cancelled,
) as acquired:
self._log_interaction_timing(
label,
"gate_wait",
time.perf_counter() - gate_started,
extra=f"acquired={bool(acquired)}, max_concurrent={settings['max_concurrent']}",
)
if not acquired:
yield False
return
with self._wake_page_for_interaction(label):
yield True
def _get_workflow_wake_settings(self) -> Dict[str, bool]:
return {
"wake_before_interaction": self._coerce_bool(
BrowserConstants.get("WORKFLOW_WAKE_TAB_BEFORE_INTERACTION"),
True,
),
"focus_emulation": self._coerce_bool(
BrowserConstants.get("WORKFLOW_FOCUS_EMULATION_ON_INTERACTION"),
True,
),
}
def _should_keep_workflow_awake(self) -> bool:
if self.stealth_mode:
return True
settings = self._get_workflow_wake_settings()
return bool(settings["wake_before_interaction"])
@contextmanager
def workflow_execution_scope(self):
"""Keep the active workflow logically awake without stealing real foreground focus."""
if not self._should_keep_workflow_awake():
yield
return
self._workflow_scope_depth += 1
started_here = self._workflow_scope_depth == 1
if started_here:
self._begin_workflow_scope()
try:
yield
finally:
self._workflow_scope_depth = max(0, self._workflow_scope_depth - 1)
if started_here:
self._end_workflow_scope()
def _begin_workflow_scope(self):
settings = self._get_workflow_wake_settings()
scope_label = "STEALTH" if self.stealth_mode else "INTERACT"
enable_focus_emulation = self.stealth_mode or settings["focus_emulation"]
logger.debug(f"[{scope_label}] 工作流开始前启用后台保活")
self._workflow_focus_emulation_active = False
self._workflow_visibility_emulation_active = False
if enable_focus_emulation:
try:
phase_started = time.perf_counter()
try:
driver_for_tab(self.tab).run_cdp(
"Emulation.setFocusEmulationEnabled",
enabled=True,
_timeout=BACKGROUND_WAKE_CDP_TIMEOUT,
)
finally:
self._log_interaction_timing(
"workflow_start",
"focus_enable",
time.perf_counter() - phase_started,
prefix=scope_label,
)
self._workflow_focus_emulation_active = True
except Exception as e:
logger.debug_throttled(
f"workflow.focus_emulation.start.{scope_label.lower()}",
f"[{scope_label}] 工作流级焦点模拟启用失败(忽略): error={e}",
interval_sec=10.0,
)
try:
phase_started = time.perf_counter()
try:
install_result = install_visibility_emulation(
self.tab,
owner=self.session,
reason="workflow_start",
)
finally:
self._log_interaction_timing(
"workflow_start",
"visibility_install",
time.perf_counter() - phase_started,
prefix=scope_label,
)
self._workflow_visibility_emulation_active = True
if not install_result:
logger.debug_throttled(
f"workflow.visibility.state.{scope_label.lower()}",
f"[{scope_label}] 工作流级可见性模拟状态未完全确认,继续保留后台保活",
interval_sec=10.0,
)
except Exception as e:
logger.debug_throttled(
f"workflow.visibility.start.{scope_label.lower()}",
f"[{scope_label}] 工作流级可见性模拟启用失败(忽略): error={e}",
interval_sec=10.0,
)
try:
phase_started = time.perf_counter()
try:
driver_for_tab(self.tab).run_cdp(
"Page.setWebLifecycleState",
state="active",
_timeout=BACKGROUND_WAKE_CDP_TIMEOUT,
)
finally:
self._log_interaction_timing(
"workflow_start",
"lifecycle_active",
time.perf_counter() - phase_started,
prefix=scope_label,
)
except Exception as e:
logger.debug_throttled(
f"workflow.lifecycle.start.{scope_label.lower()}",
f"[{scope_label}] 工作流开始前页面唤醒失败(忽略): error={e}",
interval_sec=10.0,
)
try:
phase_started = time.perf_counter()
try:
driver_for_tab(self.tab).run_js("return document.readyState || '';", timeout=BACKGROUND_WAKE_JS_TIMEOUT)
finally:
self._log_interaction_timing(
"workflow_start",
"ready_probe",
time.perf_counter() - phase_started,
prefix=scope_label,
)
except Exception:
pass
def _end_workflow_scope(self):
scope_label = "STEALTH" if self.stealth_mode else "INTERACT"
try:
if self._workflow_focus_emulation_active:
phase_started = time.perf_counter()
try:
driver_for_tab(self.tab).run_cdp(
"Emulation.setFocusEmulationEnabled",
enabled=False,
_timeout=BACKGROUND_WAKE_CDP_TIMEOUT,
)
finally:
self._log_interaction_timing(
"workflow_end",
"focus_disable",
time.perf_counter() - phase_started,
prefix=scope_label,
)
except Exception as e:
logger.debug_throttled(
f"workflow.focus_emulation.end.{scope_label.lower()}",
f"[{scope_label}] 工作流级焦点模拟关闭失败(忽略): error={e}",
interval_sec=10.0,
)
finally:
self._workflow_focus_emulation_active = False
try:
phase_started = time.perf_counter()
try:
restore_visibility_emulation(self.tab, owner=self.session, reason="workflow_end")
finally:
self._log_interaction_timing(
"workflow_end",
"visibility_restore",
time.perf_counter() - phase_started,
prefix=scope_label,
)
finally:
self._workflow_visibility_emulation_active = False
@contextmanager
def _wake_page_for_interaction(self, label: str):
settings = self._get_workflow_wake_settings()
if self.stealth_mode:
with self._wake_page_for_stealth_interaction(label):
yield
return
if not settings["wake_before_interaction"]:
yield
return
focus_emulation_enabled = False
restore_visibility_after_interaction = False
try:
if settings["focus_emulation"] and not self._workflow_focus_emulation_active:
try:
phase_started = time.perf_counter()
try:
driver_for_tab(self.tab).run_cdp(
"Emulation.setFocusEmulationEnabled",
enabled=True,
_timeout=BACKGROUND_WAKE_CDP_TIMEOUT,
)
finally:
self._log_interaction_timing(
label,
"focus_enable",
time.perf_counter() - phase_started,
)
focus_emulation_enabled = True
except Exception as e:
logger.debug_throttled(
f"interaction.focus_emulation.{label}",
f"[INTERACT] 焦点模拟启用失败(忽略): target={label}, error={e}",
interval_sec=10.0,
)
try:
phase_started = time.perf_counter()
try:
install_visibility_emulation(self.tab, owner=self.session, reason=f"interaction:{label}")
finally:
self._log_interaction_timing(
label,
"visibility_install",
time.perf_counter() - phase_started,
)
restore_visibility_after_interaction = not self._workflow_visibility_emulation_active
except Exception as e:
logger.debug_throttled(
f"interaction.visibility.{label}",
f"[INTERACT] 可见性模拟启用失败(忽略): target={label}, error={e}",
interval_sec=10.0,
)
try:
phase_started = time.perf_counter()
try:
driver_for_tab(self.tab).run_cdp(
"Page.setWebLifecycleState",
state="active",
_timeout=BACKGROUND_WAKE_CDP_TIMEOUT,
)
finally:
self._log_interaction_timing(
label,
"lifecycle_active",
time.perf_counter() - phase_started,
)
except Exception as e:
logger.debug_throttled(
f"interaction.lifecycle_wake.{label}",
f"[INTERACT] 页面唤醒失败(忽略): target={label}, error={e}",
interval_sec=10.0,
)
try:
phase_started = time.perf_counter()
try:
driver_for_tab(self.tab).run_js(
"return {readyState: document.readyState || '', hidden: !!document.hidden, visibilityState: document.visibilityState || ''};",
timeout=BACKGROUND_WAKE_JS_TIMEOUT,
)
finally:
self._log_interaction_timing(
label,
"ready_probe",
time.perf_counter() - phase_started,
)
except Exception:
pass
yield
finally:
if restore_visibility_after_interaction:
try:
phase_started = time.perf_counter()
try:
restore_visibility_emulation(self.tab, owner=self.session, reason=f"interaction_end:{label}")
finally:
self._log_interaction_timing(
label,
"visibility_restore",
time.perf_counter() - phase_started,
)
except Exception:
pass
if focus_emulation_enabled:
try:
phase_started = time.perf_counter()
try:
driver_for_tab(self.tab).run_cdp(
"Emulation.setFocusEmulationEnabled",
enabled=False,
_timeout=BACKGROUND_WAKE_CDP_TIMEOUT,
)
finally:
self._log_interaction_timing(
label,
"focus_disable",
time.perf_counter() - phase_started,
)
except Exception:
pass
@contextmanager
def _wake_page_for_stealth_interaction(self, label: str):
"""
低熵模式下的最小唤醒。
不强制激活标签页或切到前台,只使用不会抢焦点的 CDP 能力,
尽量减少后台页被冻结、坐标读取或鼠标事件派发被拖延的概率。
"""
focus_emulation_enabled = False
restore_visibility_after_interaction = False
try:
if not self._workflow_focus_emulation_active:
try:
phase_started = time.perf_counter()
try:
driver_for_tab(self.tab).run_cdp(
"Emulation.setFocusEmulationEnabled",
enabled=True,
_timeout=BACKGROUND_WAKE_CDP_TIMEOUT,
)
finally:
self._log_interaction_timing(
label,
"focus_enable",
time.perf_counter() - phase_started,
prefix="STEALTH",
)
focus_emulation_enabled = True
except Exception as e:
logger.debug_throttled(
f"interaction.stealth_focus_emulation.{label}",
f"[STEALTH] 焦点模拟启用失败(忽略): target={label}, error={e}",
interval_sec=10.0,
)
try:
phase_started = time.perf_counter()
try:
install_visibility_emulation(self.tab, owner=self.session, reason=f"interaction:{label}")
finally:
self._log_interaction_timing(
label,
"visibility_install",
time.perf_counter() - phase_started,
prefix="STEALTH",
)
restore_visibility_after_interaction = not self._workflow_visibility_emulation_active
except Exception as e:
logger.debug_throttled(
f"interaction.stealth_visibility.{label}",
f"[STEALTH] 可见性模拟启用失败(忽略): target={label}, error={e}",
interval_sec=10.0,
)
try:
phase_started = time.perf_counter()
try:
driver_for_tab(self.tab).run_cdp(
"Page.setWebLifecycleState",
state="active",
_timeout=BACKGROUND_WAKE_CDP_TIMEOUT,
)
finally:
self._log_interaction_timing(
label,
"lifecycle_active",
time.perf_counter() - phase_started,
prefix="STEALTH",
)
except Exception as e:
logger.debug_throttled(
f"interaction.stealth_lifecycle.{label}",
f"[STEALTH] 页面唤醒失败(忽略): target={label}, error={e}",
interval_sec=10.0,
)
yield
finally:
if restore_visibility_after_interaction:
try:
phase_started = time.perf_counter()
try:
restore_visibility_emulation(self.tab, owner=self.session, reason=f"interaction_end:{label}")
finally:
self._log_interaction_timing(
label,
"visibility_restore",
time.perf_counter() - phase_started,
prefix="STEALTH",
)
except Exception:
pass
if focus_emulation_enabled:
try:
phase_started = time.perf_counter()
try:
driver_for_tab(self.tab).run_cdp(
"Emulation.setFocusEmulationEnabled",
enabled=False,
_timeout=BACKGROUND_WAKE_CDP_TIMEOUT,
)
finally:
self._log_interaction_timing(
label,
"focus_disable",
time.perf_counter() - phase_started,
prefix="STEALTH",
)
except Exception:
pass
@staticmethod
def _is_rect_stable(previous: Dict[str, Any], current: Dict[str, Any], tolerance: int) -> bool:
if not previous or not current:
return False
previous_rect = previous.get("rect") or {}
current_rect = current.get("rect") or {}
for key in ("x", "y", "width", "height"):
if abs(int(previous_rect.get(key, 0)) - int(current_rect.get(key, 0))) > tolerance:
return False
return True
def _sample_element_interactable_state(self, ele) -> Dict[str, Any]:
try:
state = as_element(ele).run_js(
"""
try {
const el = this;
if (!el || !el.isConnected) {
return { interactable: false, connected: false };
}
const rect = el.getBoundingClientRect();
const style = window.getComputedStyle(el);
const signalText = [
el.getAttribute('aria-label'),
el.getAttribute('title'),
el.getAttribute('data-testid'),
el.innerText,
el.textContent
].join(' ').toLowerCase();
const classText = String(el.className || '').toLowerCase();
const disabled = !!el.disabled || el.getAttribute('aria-disabled') === 'true';
const hidden = style.display === 'none'
|| style.visibility === 'hidden'
|| Number(style.opacity || '1') < 0.05;
const pointerEventsNone = style.pointerEvents === 'none';
const busy = el.getAttribute('aria-busy') === 'true'
|| /loading|pending|sending|uploading/.test(signalText)
|| /(^|[\\s:_-])(loading|pending|sending|uploading)(?=$|[\\s:_-])/.test(classText);
const sizeOk = rect.width >= 1 && rect.height >= 1;
const viewportWidth = window.innerWidth || document.documentElement.clientWidth || 0;
const viewportHeight = window.innerHeight || document.documentElement.clientHeight || 0;
const viewportOk = rect.bottom >= 0
&& rect.right >= 0
&& rect.top <= viewportHeight
&& rect.left <= viewportWidth;
return {
interactable: !disabled && !hidden && !pointerEventsNone && !busy && sizeOk && viewportOk,
connected: true,
disabled,
hidden,
busy,
pointerEventsNone,
rect: {
x: Math.round(rect.x || 0),
y: Math.round(rect.y || 0),
width: Math.round(rect.width || 0),
height: Math.round(rect.height || 0)
}
};
} catch (error) {
return {
interactable: false,
connected: false,
error: String(error && error.message ? error.message : error || '')
};
}
"""
)
except Exception as e:
return {
"interactable": False,
"connected": False,
"error": str(e),
}
return state if isinstance(state, dict) else {"interactable": False, "connected": False}
def _wait_for_element_interactable(self, ele, selector: str = "", target_key: str = ""):
settings = self._get_page_interaction_settings()
base_timeout = settings["ready_timeout"]
timeout = self._get_recent_fill_send_wait_timeout(target_key, base_timeout)
if timeout <= 0:
return ele
stable_needed = settings["stable_samples"]
sample_interval = settings["sample_interval"]
tolerance = settings["rect_tolerance"]
stable_count = 0
last_state: Optional[Dict[str, Any]] = None
deadline = time.time() + timeout
latest_element = ele
if timeout > base_timeout + 0.01:
logger.debug_throttled(
f"interaction.wait.extend.{target_key or 'element'}",
"[INTERACT] 检测到刚完成新会话/长文本填充,放宽发送按钮等待 "
f"(target={target_key or '-'}, timeout={timeout:.2f}s)",
interval_sec=5.0,
)
while time.time() < deadline:
if self._check_cancelled():
return latest_element
current_state = self._sample_element_interactable_state(latest_element)
if current_state.get("interactable"):
stable_count = stable_count + 1 if self._is_rect_stable(last_state, current_state, tolerance) else 1
if stable_count >= stable_needed:
return latest_element
last_state = current_state
time.sleep(min(sample_interval, 0.05))
continue
# 仅在当前元素未连接或不可交互时,才尝试刷新获取最新 DOM 节点
stable_count = 0
if target_key in {"input_box", "send_btn"} and (selector or target_key):
refreshed = self._refresh_target_element(
selector,
target_key,
timeout=max(sample_interval, 0.1),
)
if refreshed is not None:
latest_element = refreshed
current_state = self._sample_element_interactable_state(latest_element)
if current_state.get("interactable"):
stable_count = 1
if stable_count >= stable_needed:
return latest_element
last_state = current_state
time.sleep(sample_interval)
logger.debug_throttled(
f"interaction.wait.{target_key or 'element'}",
f"[INTERACT] 元素稳定等待超时: target={target_key or '-'}, state={last_state}",
interval_sec=5.0,
)
return latest_element
@@ -0,0 +1,227 @@
"""
Request transport mixin for WorkflowExecutor.
"""
from __future__ import annotations
from typing import Any, Dict, Optional
from app.core.config import logger, WorkflowError
from app.core.request_transport import (
REQUEST_TRANSPORT_MODE_PAGE_FETCH,
execute_request_transport,
get_default_request_transport_config,
)
class WorkflowExecutorRequestTransportMixin:
def _get_request_transport_mode(self) -> str:
return str(self._request_transport.get("mode") or get_default_request_transport_config().get("mode") or "workflow").strip().lower()
def _get_request_transport_profile(self) -> str:
return str(self._request_transport.get("profile") or "").strip()
def _get_request_transport_options(self) -> Dict[str, Any]:
options = self._request_transport.get("options") or {}
return dict(options) if isinstance(options, dict) else {}
def _get_request_transport_fallback_mode(self) -> str:
fallback_mode = str(self._get_request_transport_options().get("fallback_mode") or "workflow").strip().lower()
return fallback_mode if fallback_mode in {"workflow", "error"} else "workflow"
def _request_transport_enabled(self) -> bool:
return (
not self._request_transport_bypass
and self._get_request_transport_mode() == REQUEST_TRANSPORT_MODE_PAGE_FETCH
and bool(self._get_request_transport_profile())
and self._stream_mode == "network"
and self._network_monitor is not None
)
def _context_has_non_text_inputs(self, prompt: str = "") -> bool:
context = getattr(self, "_context", None) or {}
if bool(context.get("images")) or bool(context.get("attachments")):
return True
try:
if prompt and self._text_handler._should_use_file_paste(prompt):
return True
except Exception:
pass
return False
def _can_stage_request_transport(self, prompt: str = "") -> bool:
effective_prompt = str(prompt or "").strip()
if not effective_prompt:
return False
if not self._request_transport_enabled():
return False
if self._context_has_non_text_inputs(effective_prompt):
return False
return True
def _queue_request_transport_prompt(
self,
*,
selector: str,
target_key: str,
optional: bool,
prompt: str,
) -> None:
self._pending_request_transport_state = {
"selector": str(selector or ""),
"target_key": str(target_key or ""),
"optional": bool(optional),
"prompt": str(prompt or ""),
}
def _has_pending_request_transport_prompt(self) -> bool:
return bool(
isinstance(self._pending_request_transport_state, dict)
and str(self._pending_request_transport_state.get("prompt") or "").strip()
)
def _clear_request_transport_state(self) -> None:
self._pending_request_transport_state = None
def consume_last_request_transport_sent(self) -> bool:
sent = bool(self._last_request_transport_sent)
self._last_request_transport_sent = False
return sent
def _reset_request_transport_monitor_if_needed(self) -> None:
if self._network_monitor is None:
return
try:
self._network_monitor._cleanup()
except Exception as e:
logger.debug(f"[REQUEST_TRANSPORT] 清理网络监听失败(忽略): {e}")
def _ensure_cached_prompt_filled(self) -> None:
pending = self._pending_request_transport_state or {}
prompt = str(pending.get("prompt") or "").strip()
selector = str(pending.get("selector") or "")
target_key = str(pending.get("target_key") or "")
optional = bool(pending.get("optional", False))
if not prompt:
return
self._request_transport_bypass = True
try:
self._execute_fill(selector, prompt, target_key, optional)
finally:
self._request_transport_bypass = False
self._clear_request_transport_state()
def _stage_request_transport_from_context(
self,
*,
selector: str = "",
target_key: str = "",
optional: bool = False,
context: Optional[Dict[str, Any]] = None,
) -> bool:
prompt = str((context or {}).get("prompt") or "").strip()
if not self._can_stage_request_transport(prompt):
return False
self._queue_request_transport_prompt(
selector=selector,
target_key=target_key,
optional=optional,
prompt=prompt,
)
logger.debug(
f"[REQUEST_TRANSPORT] 已缓存页面直发 prompt "
f"(profile={self._get_request_transport_profile()}, chars={len(prompt)})"
)
return True
def _consume_request_transport_followup_steps(self, workflow: list, current_index: int) -> int:
if current_index < 0 or current_index >= len(workflow):
return current_index
if str(workflow[current_index].get("action") or "").strip() != "PAGE_FETCH":
return current_index
next_index = current_index + 1
consumed = 0
while next_index < len(workflow):
next_step = workflow[next_index] if isinstance(workflow[next_index], dict) else {}
next_action = str(next_step.get("action") or "").strip()
next_target = str(next_step.get("target") or "").strip()
if next_action in {"FILL_INPUT", "KEY_PRESS"}:
consumed += 1
next_index += 1
continue
if next_action == "CLICK" and next_target == "send_btn":
consumed += 1
next_index += 1
continue
if next_action == "WAIT":
lookahead = next_index + 1
while lookahead < len(workflow):
lookahead_step = workflow[lookahead] if isinstance(workflow[lookahead], dict) else {}
lookahead_action = str(lookahead_step.get("action") or "").strip()
lookahead_target = str(lookahead_step.get("target") or "").strip()
if lookahead_action == "WAIT":
lookahead += 1
continue
break
if (
lookahead < len(workflow)
and (
lookahead_action in {"FILL_INPUT", "KEY_PRESS"}
or (lookahead_action == "CLICK" and lookahead_target == "send_btn")
)
):
consumed += 1
next_index += 1
continue
break
if consumed > 0:
logger.debug(
f"[REQUEST_TRANSPORT] 页面直发成功后跳过 {consumed} 个回退步骤"
)
return next_index - 1
def _attempt_request_transport_send(self) -> bool:
if not self._has_pending_request_transport_prompt():
return False
if not self._request_transport_enabled():
return False
pending = self._pending_request_transport_state or {}
prompt = str(pending.get("prompt") or "").strip()
if not prompt:
return False
result = execute_request_transport(
self.tab,
self._request_transport,
prompt=prompt,
consume_response=False,
)
if result.get("ok"):
logger.info(
"[REQUEST_TRANSPORT] 页面直发已触发 "
f"(profile={self._get_request_transport_profile()}, status={result.get('status')}, "
f"session_id={result.get('session_id') or '-'})"
)
self._clear_request_transport_state()
self._last_request_transport_sent = True
return True
fallback_mode = self._get_request_transport_fallback_mode()
logger.warning(
"[REQUEST_TRANSPORT] 页面直发失败: "
f"profile={self._get_request_transport_profile()}, "
f"error={result.get('error')}, status={result.get('status')}, "
f"fallback={fallback_mode}"
)
if fallback_mode == "workflow":
self._reset_request_transport_monitor_if_needed()
return False
self._clear_request_transport_state()
raise WorkflowError(
f"request_transport_failed:{result.get('error') or result.get('status') or 'unknown'}"
)
File diff suppressed because it is too large. Load diff
+572
View File
@@ -0,0 +1,572 @@
"""Structured workflow v2: bounded compiler, request-local variables and safe control flow.
The compiler emits ordinary legacy action dictionaries interleaved with private
instructions. BrowserWorkflowMixin still executes EVERY leaf in its existing
media/ownership/cancellation lifecycle. No eval, Python attribute access or
arbitrary jumps. Public schema remains a list of {action,target,optional,value}.
"""
from __future__ import annotations
import copy
import json
import math
import re
import time
from urllib.parse import quote
from typing import Any
from app.core.config import WorkflowError, WorkflowCancelledError
from app.core.driver import as_element
CONTROL_ACTIONS = frozenset({"SET", "CAPTURE", "IF", "SWITCH", "GROUP", "GUARD", "TRY", "LABEL"})
LEAF_ACTIONS = frozenset({"FILL_INPUT", "SELECT_MODEL", "CLICK", "COORD_CLICK", "COORD_SCROLL", "STREAM_WAIT", "STREAM_OUTPUT", "KEY_PRESS", "WAIT", "JS_EXEC", "READONLY_HINT", "PAGE_FETCH"})
OPS = {"eq", "ne", "contains", "starts_with", "ends_with", "matches", "exists", "not_exists", "gt", "gte", "lt", "lte", "in"}
TRANSFORMS = {"trim", "lower", "upper", "string", "number", "boolean", "urlencode", "css_escape", "json"}
NAME = re.compile(r"^[A-Za-z_][A-Za-z0-9_]{0,63}$")
TOKEN = re.compile(r"\{\{\s*([A-Za-z_][\w.]*)\s*\}\}|(?<!\{)\{([A-Za-z_][\w.]*)\}(?!\})")
MISSING = object()
MAX_NODES, MAX_DEPTH, MAX_BYTES = 1000, 16, 1_048_576
class FlowValidationError(WorkflowError):
pass
class FlowVariableError(WorkflowError):
pass
class FlowLimitError(WorkflowError):
pass
def _size(value, limit=MAX_BYTES):
try:
if len(json.dumps(value, ensure_ascii=False, allow_nan=False).encode("utf-8")) > limit:
raise FlowValidationError("工作流数据超出大小限制")
except (TypeError, ValueError, RecursionError) as exc:
raise FlowValidationError("工作流必须是有限、可序列化的 JSON 数据") from exc
def validate_inputs(value):
if not isinstance(value, dict) or len(value) > 128:
raise FlowValidationError("workflow_variables 必须为不超过 128 个键的对象")
if any(not isinstance(k, str) or not NAME.fullmatch(k) or k.startswith("__") for k in value):
raise FlowValidationError("变量名只能包含字母、数字及下划线,不能以数字或双下划线开头")
_size(value, 65536)
return value
def has_control_flow(workflow):
return any(isinstance(step, dict) and (step.get("action") in CONTROL_ACTIONS or step.get("flow_version") == 2 or "selector" in step) for step in (workflow or []))
def _condition_valid(condition, depth=0):
if depth > MAX_DEPTH or not isinstance(condition, dict):
raise FlowValidationError("条件必须为对象,嵌套不能超过 16 层")
for mode in ("all", "any"):
if mode in condition:
if not isinstance(condition[mode], list) or not 1 <= len(condition[mode]) <= 64:
raise FlowValidationError("组合条件需要 1–64 个子条件")
for child in condition[mode]:
_condition_valid(child, depth + 1)
return
if "not" in condition:
_condition_valid(condition["not"], depth + 1)
return
if condition.get("op") not in OPS or "left" not in condition:
raise FlowValidationError("条件缺少 left 或使用了未知运算符")
if condition["op"] not in {"exists", "not_exists"} and "right" not in condition:
raise FlowValidationError("比较条件缺少 right")
def validate_workflow(workflow):
"""Validate all branches BEFORE any page action; never mutate user JSON."""
if not isinstance(workflow, list):
raise FlowValidationError("workflow 必须为数组")
_size(workflow)
count = 0
def walk(nodes, depth=0):
nonlocal count
if depth > MAX_DEPTH or not isinstance(nodes, list):
raise FlowValidationError("分支必须为数组,嵌套不能超过 16 层")
labels = {}
for i, node in enumerate(nodes):
count += 1
if count > MAX_NODES or not isinstance(node, dict):
raise FlowValidationError("步骤必须为对象,步骤总数不能超过 1000")
action = node.get("action")
if action not in CONTROL_ACTIONS | LEAF_ACTIONS:
raise FlowValidationError(f"无效工作流动作: {action}")
if "retry_safe" in node and not isinstance(node["retry_safe"], bool):
raise FlowValidationError("retry_safe 必须为布尔值")
if action not in CONTROL_ACTIONS:
continue
v = node.get("value")
if not isinstance(v, dict):
raise FlowValidationError(f"{action}.value 必须为对象")
if action in {"SET", "CAPTURE", "LABEL"}:
name = v.get("name", "")
if not isinstance(name, str) or not NAME.fullmatch(name) or name.startswith("__"):
raise FlowValidationError(f"{action} 需要合法的 name")
if action in {"SET", "CAPTURE"} and v.get("scope", "local") not in {"local", "global"}:
raise FlowValidationError("变量 scope 必须为 local 或 global")
if action == "SET":
if "value" not in v or v.get("mode", "assign") not in {"assign", "default"}:
raise FlowValidationError("SET 需要 value,mode 为 assign/default")
if action == "CAPTURE":
if v.get("source", "text") not in {"text", "attribute", "class", "url", "exists", "value"}:
raise FlowValidationError("不支持的 CAPTURE.source")
if v.get("source") == "attribute" and not v.get("attribute"):
raise FlowValidationError("捕获属性必须指定 attribute")
if not isinstance(v.get("timeout", 2), (int, float)) or not 0 <= v.get("timeout", 2) <= 30:
raise FlowValidationError("捕获超时需为 0–30 秒")
if action in {"IF", "GUARD"}:
_condition_valid(v.get("condition"))
if action == "SWITCH":
cases = v.get("cases")
if not isinstance(cases, list) or not 1 <= len(cases) <= 32:
raise FlowValidationError("多分支需要 1–32 个有序情况")
for case in cases:
if not isinstance(case, dict):
raise FlowValidationError("情况必须是对象")
if "label" in case and (not isinstance(case["label"], str) or len(case["label"]) > 160):
raise FlowValidationError("情况名称必须是 160 字以内的文本")
_condition_valid(case.get("condition"))
walk(case.get("steps", []), depth + 1)
walk(v.get("default", []), depth + 1)
if action == "IF":
walk(v.get("then", []), depth + 1)
walk(v.get("else", []), depth + 1)
if action in {"GROUP", "TRY"}:
walk(v.get("steps", []), depth + 1)
if action == "GROUP":
validate_inputs(v.get("variables", {}))
if action == "TRY":
attempts = v.get("attempts", 1)
if type(attempts) is not int or not 1 <= attempts <= 5:
raise FlowValidationError("TRY.attempts 必须为 1–5 的整数")
delay = v.get("delay", 0.3)
if type(delay) not in (int, float) or not 0 <= delay <= 5:
raise FlowValidationError("TRY.delay 必须为 0–5 秒")
if "retry_side_effects" in v and not isinstance(v["retry_side_effects"], bool):
raise FlowValidationError("retry_side_effects 必须为布尔值")
if "errors" in v and (not isinstance(v["errors"], list) or not v["errors"] or any(not isinstance(x, str) for x in v["errors"])):
raise FlowValidationError("TRY.errors 必须为非空错误类型数组")
if "fallback" in v:
walk(v["fallback"], depth + 1)
if action == "GUARD" and v.get("mode", "skip_group") not in {"skip_group", "goto"}:
raise FlowValidationError("GUARD.mode 必须为 skip_group/goto")
if action == "LABEL":
if v["name"] in labels:
raise FlowValidationError("同一分支内的锚点名称不能重复")
labels[v["name"]] = i
for i, node in enumerate(nodes):
if node.get("action") == "GUARD" and node["value"].get("mode") == "goto":
target = labels.get(node["value"].get("anchor"), -1)
if target <= i:
raise FlowValidationError("只能前往同一分支内位于后方的锚点")
walk(workflow)
return {"valid": True, "nodes": count, "version": 2 if has_control_flow(workflow) else 1}
class Variables:
"""Request globals, explicit lexical locals, read-only inputs/context namespaces."""
def __init__(self, context=None, inputs=None):
self.context = context if context is not None else {}
self.inputs = copy.deepcopy(validate_inputs(inputs if inputs is not None else {}))
self.globals = copy.deepcopy(self.inputs)
self.locals = []
self.error = {}
def merged(self):
result = dict(self.globals)
for scope in self.locals:
result.update(scope["values"])
return result
def lookup(self, path, missing=False):
parts = str(path).split(".")
if any(not NAME.fullmatch(p) or p.startswith("__") for p in parts):
raise FlowVariableError("不合法的变量路径")
namespaces = {"vars": self.merged(), "inputs": self.inputs, "context": self.context, "error": self.error}
if parts[0] in namespaces:
obj = namespaces[parts.pop(0)]
else:
obj = self.merged()
if parts[0] not in obj and parts[0] in self.context:
obj = self.context
for part in parts:
if not isinstance(obj, dict) or part not in obj:
if missing:
return MISSING
raise FlowVariableError(f"变量未定义: {path}")
obj = obj[part]
return copy.deepcopy(obj)
def resolve(self, value, depth=0):
if depth > 32:
raise FlowLimitError("表达式嵌套超过限制")
if isinstance(value, dict):
if "$literal" in value:
return copy.deepcopy(value["$literal"])
if "$var" in value:
result = self.lookup(value["$var"], missing="default" in value)
return self.resolve(value.get("default"), depth + 1) if result is MISSING else result
if "$map" in value:
source = self.resolve(value.get("input"), depth + 1)
table = value["$map"]
if not isinstance(table, dict):
raise FlowVariableError("$map 必须为字典")
key = str(source)
if key in table:
return self.resolve(table[key], depth + 1)
if "default" in value:
return self.resolve(value["default"], depth + 1)
raise FlowVariableError("映射中没有匹配值且未设置 default")
if "$transform" in value:
op = value["$transform"]
if op not in TRANSFORMS:
raise FlowVariableError("未知转换操作")
source = self.resolve(value.get("input"), depth + 1)
text = str(source) if source is not None else ""
if op in {"trim", "lower", "upper"}:
return {"trim": str.strip, "lower": str.lower, "upper": str.upper}[op](text)
if op == "string": return text
if op == "json": return json.dumps(source, ensure_ascii=False, allow_nan=False)
if op == "urlencode": return quote(text, safe="")
if op == "css_escape":
return "".join(c if (c.isascii() and (c.isalpha() or c in "_-" or (i > 0 and c.isdigit()))) else f"\\{ord(c):x} " for i, c in enumerate(text))
if op == "number":
try:
number = float(source)
if not math.isfinite(number): raise ValueError()
return number
except (ValueError, TypeError) as exc:
raise FlowVariableError("无法转换为有限数字") from exc
if op == "boolean":
if source in (True, False) and type(source) is bool: return source
if text.lower() in {"true", "1"}: return True
if text.lower() in {"false", "0", ""}: return False
raise FlowVariableError("布尔转换只接受 true/false/1/0")
return {k: self.resolve(v, depth + 1) for k, v in value.items()}
if isinstance(value, list):
return [self.resolve(v, depth + 1) for v in value]
if not isinstance(value, str):
return value
exact = TOKEN.fullmatch(value)
if exact:
return self.lookup(exact.group(1) or exact.group(2))
return TOKEN.sub(lambda m: str(self.lookup(m.group(1) or m.group(2))), value)
def set(self, name, value, scope="local", mode="assign"):
target = self.locals[-1]["values"] if scope == "local" and self.locals else self.globals
if mode == "default" and self.lookup("vars." + name, missing=True) is not MISSING:
return
_size(value, 65536)
target[name] = copy.deepcopy(value)
_size(self.merged(), 262144)
def condition(self, condition):
if "all" in condition: return all(self.condition(c) for c in condition["all"])
if "any" in condition: return any(self.condition(c) for c in condition["any"])
if "not" in condition: return not self.condition(condition["not"])
op, left = condition["op"], condition["left"]
if op in {"exists", "not_exists"}:
path = left.get("$var") if isinstance(left, dict) else None
if path is None and isinstance(left, str):
match = TOKEN.fullmatch(left)
path = (match.group(1) or match.group(2)) if match else None
result = self.lookup(path, missing=True) if path else self.resolve(left)
exists = result is not MISSING and result is not None
return exists if op == "exists" else not exists
a, b = self.resolve(left), self.resolve(condition["right"])
if op == "eq": return type(a) is type(b) and a == b
if op == "ne": return not (type(a) is type(b) and a == b)
if op in {"gt", "gte", "lt", "lte"}:
if type(a) not in (float, int) or type(b) not in (float, int):
raise FlowVariableError("大小比较要求数字,请先使用 number 转换")
return {"gt": a > b, "gte": a >= b, "lt": a < b, "lte": a <= b}[op]
if op == "contains":
if not isinstance(a, (str, list, dict)): raise FlowVariableError("contains 需要字符串、数组或对象")
return b in a
if op == "in":
if not isinstance(b, (str, list, dict)): raise FlowVariableError("in 需要字符串、数组或对象")
return a in b
if op in {"starts_with", "ends_with"}:
if not isinstance(a, str) or not isinstance(b, str): raise FlowVariableError("前后缀比较需要字符串")
return a.startswith(b) if op == "starts_with" else a.endswith(b)
if op == "matches":
if not isinstance(a, str) or not isinstance(b, str) or len(b) > 512 or len(a) > 65536:
raise FlowVariableError("正则需要字符串,模式≤512字符、输入≤65536字符")
import regex
try:
return bool(regex.search(b, a, timeout=0.025))
except (regex.error, TimeoutError) as exc:
raise FlowVariableError("正则无效或超过 25ms 求值预算") from exc
raise FlowVariableError("未知条件运算符")
class FlowProgram:
def __init__(self, workflow, context=None, inputs=None, capture=None, stop_checker=None, max_transitions=10000, deadline_seconds=300):
validate_workflow(workflow)
self.variables = Variables(context, inputs)
self.capture = capture
self.stop_checker = stop_checker or (lambda: False)
self.steps, self.trace, self.tries = [], [], []
self.output_emitted = False
self.transitions = 0
self.max_transitions = min(10000, max(1, max_transitions))
self.deadline = time.monotonic() + min(3600, max(1, deadline_seconds))
self._compile(copy.deepcopy(workflow), "root", None)
def _emit(self, op, path, **kwargs):
self.steps.append({"action": "__FLOW_INTERNAL", "target": "", "optional": False, "_op": op, "_path": path, **kwargs})
return len(self.steps) - 1
def _compile(self, nodes, path, group_end):
labels, jumps = {}, []
for n, node in enumerate(nodes):
p = f"{path}.{n}"
action, v = node["action"], node.get("value", {})
if action not in CONTROL_ACTIONS:
self.steps.append({**node, "_path": p})
elif action == "IF":
test = self._emit("if", p, condition=v["condition"])
self._compile(v.get("then", []), p + ".then", group_end)
jump = self._emit("jump", p)
self.steps[test]["to"] = len(self.steps)
self._compile(v.get("else", []), p + ".else", group_end)
self.steps[jump]["to"] = self._emit("merge", p)
elif action == "SWITCH":
test = self._emit("switch", p, cases=[])
exits = []
for case_index, case in enumerate(v["cases"]):
branch = f"case_{case_index}"
self.steps[test]["cases"].append({"condition": case["condition"], "to": len(self.steps), "branch": branch})
self._compile(case.get("steps", []), p + "." + branch, group_end)
exits.append(self._emit("jump", p))
self.steps[test]["default_to"] = len(self.steps)
self._compile(v.get("default", []), p + ".default", group_end)
merge = self._emit("merge", p)
for exit_index in exits:
self.steps[exit_index]["to"] = merge
elif action == "GROUP":
ref = {}
enter = self._emit("enter", p, variables=v.get("variables", {}), end=ref)
self._compile(v.get("steps", []), p + ".steps", ref)
ref["index"] = self._emit("leave", p, start=enter)
elif action == "TRY":
start = self._emit("try", p, policy=v)
self._compile(v.get("steps", []), p + ".steps", group_end)
done = self._emit("try_done", p, start=start)
self.steps[start]["fallback"] = len(self.steps) if "fallback" in v else None
self._compile(v.get("fallback", []), p + ".fallback", group_end)
end = self._emit("catch_done", p, start=start)
self.steps[start]["end"] = end
self.steps[done]["to"] = end + 1
elif action == "GUARD":
idx = self._emit("guard", p, condition=v["condition"], end=group_end)
if v.get("mode") == "goto": jumps.append((idx, v["anchor"]))
elif action == "LABEL":
labels[v["name"]] = self._emit("label", p)
else:
self._emit(action.lower(), p, spec=v, target=node.get("target", ""))
for idx, name in jumps:
self.steps[idx]["to"] = labels[name]
def _check(self, transition=True):
if self.stop_checker(): raise WorkflowCancelledError("workflow_cancelled")
if time.monotonic() > self.deadline: raise FlowLimitError("工作流运行时间超过控制流预算")
self.transitions += int(transition)
if self.transitions > self.max_transitions: raise FlowLimitError("工作流转换次数超过限制")
def _record(self, step, status, **meta):
# Deliberately exclude prompt, values, capture contents, selectors and error text.
if len(self.trace) < 500:
self.trace.append({"path": step.get("_path"), "action": step.get("_op", step.get("action")), "status": status, **meta})
def _seek(self, index):
self.variables.locals[:] = [s for s in self.variables.locals if s["start"] < index <= s["end"]]
while self.tries and not (self.tries[-1]["start"] < index <= self.tries[-1]["end"]):
frame = self.tries.pop()
self.variables.error = frame["previous_error"]
def next_leaf(self, index):
while index < len(self.steps):
self._check()
self._seek(index)
step = self.steps[index]
op = step.get("_op")
if not op:
return index
try:
next_index = index + 1
if op == "if":
result = self.variables.condition(step["condition"])
self._record(step, "branch", branch="then" if result else "else")
if not result: next_index = step["to"]
elif op == "switch":
next_index = step["default_to"]
branch = "default"
for case in step["cases"]:
self._check()
if self.variables.condition(case["condition"]):
next_index, branch = case["to"], case["branch"]
break
self._record(step, "branch", branch=branch)
elif op == "guard":
if self.variables.condition(step["condition"]):
next_index = step.get("to", (step["end"]["index"] if step.get("end") is not None else len(self.steps)))
self._record(step, "skipped")
else: self._record(step, "continued")
elif op == "jump": next_index = step["to"]
elif op == "merge": self._record(step, "merged")
elif op == "label": self._record(step, "label")
elif op == "enter":
values = self.variables.resolve(step["variables"])
self.variables.locals.append({"start": index, "end": step["end"]["index"], "values": values})
_size(self.variables.merged(), 262144)
self._record(step, "entered")
elif op == "leave":
if self.variables.locals and self.variables.locals[-1]["start"] == step["start"]:
self.variables.locals.pop()
self._record(step, "merged")
elif op == "set":
v = step["spec"]
if v.get("mode") != "default" or self.variables.lookup("vars." + v["name"], missing=True) is MISSING:
self.variables.set(v["name"], self.variables.resolve(v["value"]), v.get("scope", "local"), v.get("mode", "assign"))
self._record(step, "assigned", variable=v["name"])
elif op == "capture":
if self.capture is None: raise WorkflowError("缺少页面状态捕获适配器")
v = step["spec"]
spec = self.variables.resolve({k: val for k, val in v.items() if k not in {"name", "scope"}})
spec["name"] = v["name"] # Adapter metadata, never evaluated as page code.
result = self.capture(spec, str(self.variables.resolve(step["target"])))
self.variables.set(v["name"], result, v.get("scope", "local"))
self._record(step, "captured", variable=v["name"])
elif op == "try":
self.tries.append({"start": index, "end": step["end"], "fallback": step["fallback"], "policy": step["policy"],
"attempt": 1, "output": False, "effect": False, "catching": False, "previous_error": self.variables.error,
"snapshot": copy.deepcopy((self.variables.globals, self.variables.locals))})
self._record(step, "attempt", attempt=1)
elif op == "try_done":
next_index = step["to"]
elif op == "catch_done":
pass
index = next_index
except (FlowValidationError, FlowLimitError, WorkflowCancelledError):
raise
except Exception as exc:
recovered = self.recover(exc, index)
if recovered is None: raise
index = recovered
self._seek(index)
return index
def prepare_leaf(self, index, selectors):
self._check()
step = copy.deepcopy(self.steps[index])
step["target"] = str(self.variables.resolve(step.get("target", "")))
if step["action"] != "JS_EXEC":
step["value"] = self.variables.resolve(step.get("value"))
step["execution"] = self.variables.resolve(step.get("execution"))
raw = step.get("selector", selectors.get(step["target"], ""))
selector = self.variables.resolve(raw)
if selector is not None and not isinstance(selector, str): raise FlowVariableError("定位器必须解析为字符串")
return step, selector or ""
def leaf_context(self, step):
result = {**self.variables.context, "vars": self.variables.merged(), "inputs": self.variables.inputs, "error": self.variables.error}
if step["action"] == "FILL_INPUT" and step.get("value") is not None:
result["prompt"] = str(step["value"])
return result
def events(self, iterator, index):
step = self.steps[index]
self._record(step, "started")
safe = step.get("retry_safe") is True or step["action"] in {"WAIT", "READONLY_HINT"}
for frame in self.tries:
frame["effect"] |= not safe
error = None
try:
for chunk in iterator:
try:
payload = json.loads(str(chunk).removeprefix("data: ").strip())
except (ValueError, TypeError): payload = None
if isinstance(payload, dict) and payload.get("error"):
error = payload["error"]
if isinstance(error, dict) and str(error.get("code", "")).startswith("attachment"):
raise WorkflowError(error["code"])
self._check(transition=False)
continue # A recovered failure must never leak an error SSE to the client.
self._check(transition=False)
self.output_emitted = True
for frame in self.tries: frame["output"] = True
yield chunk
finally:
if hasattr(iterator, "close"):
iterator.close()
self._check(transition=False)
if error:
raise WorkflowError(str(error.get("message", "workflow_step_failed")) if isinstance(error, dict) else str(error))
self._record(step, "completed")
def recover(self, error, index):
self._record(self.steps[index], "failed", error_type=type(error).__name__)
if self.output_emitted:
return None
if isinstance(error, (FlowValidationError, FlowLimitError, WorkflowCancelledError)):
return None
message = str(error)
if message.startswith(("stream_terminal_error:", "attachment", "file_paste_length_error", "workflow_cancelled")) or message in {"new_chat_transition_timeout", "send_unconfirmed", "arena_send_no_target", "stream_recovery_exhausted", "arena_direct_unexpected_battle_redirect"}:
return None
self._check()
for frame in reversed(self.tries):
policy = frame["policy"]
allowed = policy.get("errors", ["*"])
if frame["catching"] or ("*" not in allowed and type(error).__name__ not in allowed): continue
if frame["output"]: return None
if frame["effect"] and not policy.get("retry_side_effects", False): return None
if frame["attempt"] < policy.get("attempts", 1) and (not frame["effect"] or policy.get("retry_side_effects", False)):
frame["attempt"] += 1
self.variables.globals, self.variables.locals = copy.deepcopy(frame["snapshot"])
self.tries[:] = self.tries[:self.tries.index(frame) + 1]
end = time.monotonic() + policy.get("delay", 0.3)
while time.monotonic() < end:
self._check()
time.sleep(min(0.05, max(0, end - time.monotonic())))
self._record(self.steps[frame["start"]], "retry", attempt=frame["attempt"])
return frame["start"] + 1
if frame["fallback"] is not None:
frame["catching"] = True
self.tries[:] = self.tries[:self.tries.index(frame) + 1]
self.variables.error = {"type": type(error).__name__, "attempt": frame["attempt"]}
self._record(self.steps[frame["start"]], "fallback")
return frame["fallback"]
return None
def capture_page_state(executor, spec, target, variables=None):
"""Read-only capture using ElementFinder, not interpolated JavaScript source."""
if executor._check_cancelled(): raise WorkflowCancelledError("workflow_cancelled")
source = spec.get("source", "text")
if source == "url": return str(getattr(executor.tab, "url", ""))[:65536]
selector = spec.get("selector") or executor._selectors.get(target, "")
if variables is not None:
selector = variables.resolve(selector)
if not selector: raise WorkflowError("CAPTURE 需要 target 或 selector")
element = executor.finder.find(selector, timeout=spec.get("timeout", 2))
if source == "exists": return bool(element)
if not element:
if "default" in spec: return spec["default"]
raise WorkflowError("CAPTURE 未找到页面元素")
if source == "text": result = element.text
elif source == "class": result = element.attr("class")
elif source == "attribute": result = element.attr(spec["attribute"])
elif source == "value": result = as_element(element).run_js("return this.value;")
else: raise FlowValidationError("不支持的捕获类型")
if result is None and "default" in spec: return spec["default"]
if isinstance(result, str) and len(result) > 65536: raise FlowLimitError("捕获结果超过 65536 字符")
return result
+33
View File
@@ -0,0 +1,33 @@
"""Backward-compatible image entry point; all attachments share one coordinator."""
import mimetypes
import time
from .attachment_upload import AttachmentUploadCoordinator
class ImageInputHandler:
def __init__(self, tab, stealth_mode, smart_delay_fn, check_cancelled_fn,
attachment_monitor=None, focus_input_fn=None, selectors=None,
coordinator=None):
self._coordinator = coordinator or AttachmentUploadCoordinator(
tab, selectors=selectors, monitor=attachment_monitor,
cancelled=check_cancelled_fn, focus=focus_input_fn,
)
self._focus_input = focus_input_fn
self._recent_image_upload_at = 0.0
def has_recent_attachment_upload(self, window=45.0):
return self._recent_image_upload_at > 0 and time.time() - self._recent_image_upload_at <= window
def paste_attachments(self, attachments):
self._coordinator.focus = self._focus_input
for item in attachments:
self._coordinator.upload(item.path, item.mime)
self._recent_image_upload_at = time.time()
return True
def paste_images(self, image_paths):
self._coordinator.focus = self._focus_input
for path in image_paths:
self._coordinator.upload(path, mimetypes.guess_type(str(path))[0] or "image/png")
self._recent_image_upload_at = time.time()
return True
+140
View File
@@ -0,0 +1,140 @@
"""Length-aware prompt chunk planning for sequential browser workflows."""
from dataclasses import dataclass
from math import ceil
from typing import Callable, List
CHUNK_SEPARATOR = "\n\n"
class InputChunkingError(ValueError):
"""Raised when the configured limit cannot hold a valid chunk."""
@dataclass(frozen=True)
class PromptChunk:
index: int
total: int
content: str
instruction: str
prompt: str
def render_chunk_instruction(index: int, total: int) -> str:
if index < total:
return (
f"该内容为第 {index}/{total} 部分,请只回复“理解”,等待第 {index + 1}/{total} "
"部分内容后,再联系上下文进行完整回复。"
)
return (
f"该内容为第 {index}/{total} 部分,也是最后一部分。"
"请联系此前收到的所有分块内容,对完整请求进行回复。"
)
def _instruction_suffix(instruction: str) -> str:
return f"{CHUNK_SEPARATOR}{instruction}" if instruction else ""
def _capacities(
total: int,
limit: int,
instruction_renderer: Callable[[int, int], str],
) -> tuple[List[str], List[int]]:
instructions = [instruction_renderer(index, total) for index in range(1, total + 1)]
# Keep one character of headroom for chunked sends. This matches the
# configured-limit semantics used by sites that reject an exact-boundary input.
capacities = [limit - 1 - len(_instruction_suffix(instruction)) for instruction in instructions]
return instructions, capacities
def plan_prompt_chunks(
content: str,
limit: int,
*,
instruction_renderer: Callable[[int, int], str] = render_chunk_instruction,
) -> List[PromptChunk]:
"""Split content into the fewest valid prompts without exceeding ``limit``.
Once the minimum chunk count is known, early chunks start near ``limit / n``.
They grow only when required to keep the remaining content inside later chunks.
"""
text = str(content or "")
try:
max_length = int(limit)
except (TypeError, ValueError) as exc:
raise InputChunkingError("分块限制必须是整数") from exc
if max_length <= 0:
raise InputChunkingError("分块限制必须大于 0")
if len(text) <= max_length:
return [PromptChunk(1, 1, text, "", text)]
if not text:
return [PromptChunk(1, 1, "", "", "")]
total = max(2, ceil(len(text) / max_length))
instructions: List[str] = []
capacities: List[int] = []
while total <= len(text):
instructions, capacities = _capacities(total, max_length, instruction_renderer)
if any(capacity <= 0 for capacity in capacities):
raise InputChunkingError("分块限制过小,无法容纳分块说明词")
total_capacity = sum(capacities)
if total_capacity >= len(text):
break
capacity_deficit = len(text) - total_capacity
total += max(1, ceil(capacity_deficit / max(capacities)))
else:
raise InputChunkingError("分块限制过小,无法容纳分块说明词")
chunks: List[PromptChunk] = []
offset = 0
remaining = len(text)
target_prompt_length = max_length // total
future_capacity = sum(capacities[1:])
for position in range(total):
capacity = capacities[position]
minimum_content = max(1, remaining - future_capacity)
suffix = _instruction_suffix(instructions[position])
preferred_content = max(1, target_prompt_length - len(suffix))
if position == total - 1:
content_length = remaining
else:
content_length = min(capacity, max(minimum_content, preferred_content))
if content_length <= 0 or content_length > capacity:
raise InputChunkingError("无法在限制内生成有效分块")
part_content = text[offset : offset + content_length]
prompt = f"{part_content}{suffix}"
chunks.append(
PromptChunk(
index=position + 1,
total=total,
content=part_content,
instruction=instructions[position],
prompt=prompt,
)
)
offset += content_length
remaining -= content_length
if position + 1 < total:
future_capacity -= capacities[position + 1]
if offset != len(text) or "".join(chunk.content for chunk in chunks) != text:
raise InputChunkingError("分块内容校验失败")
if any(len(chunk.prompt) > max_length for chunk in chunks):
raise InputChunkingError("分块结果超过长度限制")
return chunks
__all__ = [
"InputChunkingError",
"PromptChunk",
"plan_prompt_chunks",
"render_chunk_instruction",
]
+351
View File
@@ -0,0 +1,351 @@
"""
app/core/workflow/model_selection.py - 通用模型选择与切换协议处理器
职责:
- 规范化工作流中的 SELECT_MODEL 动作执行接口;
- 提供模型选择处理器的注册与分发机制;
- 实现 Arena Direct 及通用模型的选择算法。
"""
from __future__ import annotations
import logging
import time
from typing import Any, Callable, Dict, Optional, Protocol
from app.core.config import ElementNotFoundError, WorkflowError, logger
from app.services.arena_direct_models import resolve_arena_direct_model
ModelSelectionHandlerFunc = Callable[[Any, str, str, Any, Optional[dict], bool], None]
_MODEL_SELECTION_HANDLERS: Dict[str, ModelSelectionHandlerFunc] = {}
def register_model_selection_handler(
name: str,
handler: ModelSelectionHandlerFunc,
) -> None:
"""注册专有模型选择处理器。"""
key = str(name or "").strip().lower()
if not key:
raise ValueError("handler name cannot be empty")
if not callable(handler):
raise ValueError(f"handler for {name} must be callable")
_MODEL_SELECTION_HANDLERS[key] = handler
logger.debug(f"[MODEL_SELECTION] 已注册模型选择处理器: {key}")
def get_model_selection_handler(name: str) -> Optional[ModelSelectionHandlerFunc]:
"""获取指定模型选择处理器。"""
key = str(name or "").strip().lower()
return _MODEL_SELECTION_HANDLERS.get(key)
def arena_direct_select_model(
executor: Any,
selector: str,
target_key: str,
value: Any,
context: Optional[dict],
optional: bool,
) -> None:
"""Arena Direct 专有模型选择实现。"""
requested_model = str((context or {}).get("model") or "").strip()
if not requested_model:
logger.debug("[SELECT_MODEL] 请求未指定模型,保持页面当前选择")
return
model = resolve_arena_direct_model(
executor.tab,
requested_model,
catalog_config=(context or {}).get("model_catalog"),
)
if not model:
if optional:
logger.warning(
"[SELECT_MODEL] Arena Direct 模型已不在当前页面目录,跳过可选步骤: "
f"model={executor._compact_log_value(requested_model, 100)}"
)
return
raise WorkflowError("arena_direct_model_not_available")
settings = value if isinstance(value, dict) else {}
timeout = executor._coerce_float(settings.get("timeout"), 3.0, minimum=0.5)
trigger_selector = str(
selector
or 'button[aria-haspopup="dialog"]:has(span.flex-1.truncate.text-start), button[aria-haspopup="dialog"]:has(span.flex-1.truncate.text-left), button[aria-haspopup="dialog"]:has(span.flex-1.truncate)'
).strip()
dialog_opened = False
try:
with executor._page_interaction_slot("SELECT_MODEL", target_key or "model_select_btn") as acquired:
if not acquired or executor._check_cancelled():
return
triggers = executor._find_visible_elements(trigger_selector)
trigger = executor._first_positioned_element(
triggers,
lambda ele: bool(executor._model_element_label(ele)),
)
if trigger is None:
mode_buttons = executor._find_visible_elements('button[role="combobox"]')
mode_button = executor._first_positioned_element(
mode_buttons,
lambda ele: bool(str(getattr(ele, "text", "") or "").strip()),
)
if mode_button is None:
raise ElementNotFoundError("Arena 模式选择按钮未找到")
current_mode = str(getattr(mode_button, "text", "") or "").strip()
if not current_mode.casefold().startswith("direct"):
executor._stealth_click_element(
mode_button,
target_key="arena_mode_select_btn",
selector='button[role="combobox"]',
)
dialog_opened = True
direct_option = None
mode_deadline = time.time() + timeout
while time.time() < mode_deadline and not executor._check_cancelled():
for candidate in executor._find_visible_elements('[role="option"]'):
label = str(getattr(candidate, "text", "") or "").strip()
if (
label.casefold().startswith("direct")
and executor._get_element_viewport_pos(candidate) is not None
):
direct_option = candidate
break
if direct_option is not None:
break
time.sleep(0.1)
if direct_option is None:
raise ElementNotFoundError("Arena Direct 模式选项未找到")
executor._stealth_click_element(
direct_option,
target_key="arena_mode_direct_option",
selector='[role="option"]',
)
time.sleep(0.2)
dialog_opened = False
refind_deadline = time.time() + timeout
while time.time() < refind_deadline and not executor._check_cancelled():
triggers = executor._find_visible_elements(trigger_selector)
trigger = executor._first_positioned_element(
triggers,
lambda ele: bool(executor._model_element_label(ele)),
)
if trigger is not None:
break
time.sleep(0.05)
if trigger is None:
raise ElementNotFoundError("Arena Direct 模型选择按钮未找到")
current_label = executor._model_element_label(trigger)
if executor._model_label_matches(current_label, model):
logger.info(
"[SELECT_MODEL] 页面已是目标模型,跳过切换: "
f"model={model['display_name']}"
)
return
executor._stealth_click_element(
trigger,
target_key=target_key or "model_select_btn",
selector=trigger_selector,
)
dialog_opened = True
deadline = time.time() + timeout
search_input = None
while time.time() < deadline and not executor._check_cancelled():
inputs = executor._find_visible_elements('input[placeholder="Search models"]')
search_input = executor._first_positioned_element(inputs)
if search_input is not None:
break
time.sleep(0.05)
if search_input is None:
raise ElementNotFoundError("Arena Direct 模型搜索框未出现")
search_text = str(
model.get("search_name")
or model.get("display_name")
or model.get("public_name")
or model.get("name")
or ""
).strip()
executor._text_handler.fill_via_clipboard_no_click(search_input, search_text)
exact_identifiers = {
str(model.get(key) or "").strip().casefold()
for key in ("arena_model_id", "name", "search_name", "display_name", "public_name")
if str(model.get(key) or "").strip()
}
if requested_model:
exact_identifiers.add(str(requested_model).strip().casefold())
fuzzy_targets = [
str(model.get(key) or "").strip().casefold()
for key in ("search_name", "display_name", "public_name", "name")
if str(model.get(key) or "").strip()
]
option = None
while time.time() < deadline and not executor._check_cancelled():
candidates = executor._find_visible_elements('[role="option"]')
if not candidates:
candidates = executor._find_visible_elements(
'[data-radix-collection-item], [cmdk-item], [role="listbox"] > *'
)
positioned_candidates = [
c for c in candidates
if executor._get_element_viewport_pos(c) is not None
]
# --- Pass 1: 精确标识符全等匹配 ---
for candidate in positioned_candidates:
cand_data_val = ""
try:
cand_data_val = str(candidate.attr("data-value") or "").strip().casefold()
except Exception:
pass
cand_val = ""
try:
cand_val = str(candidate.attr("value") or "").strip().casefold()
except Exception:
pass
cand_id = ""
try:
cand_id = str(candidate.attr("id") or "").strip().casefold()
except Exception:
pass
cand_label = executor._model_element_label(candidate).casefold()
cand_raw = str(getattr(candidate, "text", "") or getattr(candidate, "raw_text", "") or "").strip().casefold()
cand_first_line = cand_raw.splitlines()[0].strip() if cand_raw else ""
cand_keys = {k for k in (cand_data_val, cand_val, cand_id, cand_label, cand_raw, cand_first_line) if k}
if cand_keys.intersection(exact_identifiers):
option = candidate
break
# --- Pass 2: 前缀及词边界匹配 ---
if option is None:
for candidate in positioned_candidates:
cand_data_val = ""
try:
cand_data_val = str(candidate.attr("data-value") or "").strip().casefold()
except Exception:
pass
cand_label = executor._model_element_label(candidate).casefold()
cand_raw = str(getattr(candidate, "text", "") or getattr(candidate, "raw_text", "") or "").strip().casefold()
cand_first_line = cand_raw.splitlines()[0].strip() if cand_raw else ""
for target_name in fuzzy_targets:
if not target_name:
continue
if (
cand_data_val == target_name
or cand_label == target_name
or cand_first_line == target_name
or cand_first_line.startswith(target_name)
or cand_label.startswith(target_name)
):
option = candidate
break
if option is not None:
break
# --- Pass 3: 包含关系保底匹配 ---
if option is None:
for candidate in positioned_candidates:
cand_label = executor._model_element_label(candidate).casefold()
cand_raw = str(getattr(candidate, "text", "") or getattr(candidate, "raw_text", "") or "").strip().casefold()
cand_first_line = cand_raw.splitlines()[0].strip() if cand_raw else ""
for target_name in fuzzy_targets:
if not target_name:
continue
if target_name in cand_first_line or target_name in cand_label:
option = candidate
break
if option is not None:
break
if option is not None:
break
time.sleep(0.05)
if option is None:
raise ElementNotFoundError(
f"Arena Direct 模型选项未找到: {model['display_name']}"
)
executor._stealth_click_element(
option,
target_key="arena_model_option",
selector='[role="option"]',
)
dialog_opened = False
verify_deadline = time.time() + min(timeout, 2.0)
while time.time() < verify_deadline and not executor._check_cancelled():
selected_triggers = executor._find_visible_elements(trigger_selector)
selected = executor._first_positioned_element(
selected_triggers,
lambda ele: executor._model_label_matches(
executor._model_element_label(ele),
model,
),
)
if selected is not None:
logger.info(
"[SELECT_MODEL] Arena Direct 模型切换完成: "
f"{current_label or '-'} -> {model['display_name']}"
)
return
time.sleep(0.08)
raise WorkflowError("arena_direct_model_switch_unconfirmed")
except Exception as exc:
if optional:
logger.warning(f"[SELECT_MODEL] 可选模型切换失败,已跳过: {exc}")
return
raise
finally:
if dialog_opened:
executor._close_arena_model_dialog()
# 注册内置模型选择处理器
register_model_selection_handler("arena_direct", arena_direct_select_model)
register_model_selection_handler("default", arena_direct_select_model)
def execute_model_selection(
executor: Any,
selector: str,
target_key: str,
value: Any,
context: Optional[dict],
optional: bool,
handler_name: str = "arena_direct",
) -> None:
"""分发并执行模型选择。"""
handler = get_model_selection_handler(handler_name) or get_model_selection_handler("default")
if handler is None:
raise WorkflowError(f"No model selection handler registered for {handler_name}")
handler(executor, selector, target_key, value, context, optional)
__all__ = [
"arena_direct_select_model",
"execute_model_selection",
"get_model_selection_handler",
"register_model_selection_handler",
]
+39
View File
@@ -0,0 +1,39 @@
"""Side-effect-free path preview. Uses the production compiler, never a browser."""
from app.core.workflow.flow_runtime import FlowProgram, FlowValidationError, validate_inputs, validate_workflow
def preview_workflow(data):
workflow = data.get("workflow")
validate_workflow(workflow)
inputs = validate_inputs(data.get("workflow_variables", {}))
captures = validate_inputs(data.get("capture_values", {}))
def capture(spec, target):
name = spec["name"]
if name not in captures:
# A missing fixture is not a page failure and must not trigger TRY.
raise FlowValidationError(f"请提供页面状态样例:{name}(模拟不会访问网页)")
return captures[name]
program = FlowProgram(workflow, inputs=inputs,
context={"model": str(data.get("model") or ""), "prompt": str(data.get("prompt") or "")},
capture=capture, deadline_seconds=5)
planned = []
index = 0
try:
while index < len(program.steps):
index = program.next_leaf(index)
if index >= len(program.steps):
break
step, _ = program.prepare_leaf(index, {})
planned.append({"path": step["_path"], "action": step["action"]})
# Deliberately do not invoke events(): planned is not completed,
# and no fictitious success/output/side-effect flags are recorded.
program._record(step, "planned")
index += 1
except Exception as exc:
return {"success": False, "mode": "simulation", "message": str(exc),
"trace": program.trace, "planned_actions": planned}
return {"success": True, "mode": "simulation",
"message": "模拟完成 · 只计算分支路径,未操作网页;不验证网页动作、重试或兜底效果。",
"trace": program.trace, "planned_actions": planned}
+569
View File
@@ -0,0 +1,569 @@
"""
app/core/workflow/script_loader.py - 通用工作流 JavaScript 脚本加载与执行器辅助类
职责:
1. 安全路径解析与沙箱目录限制(仅允许 custom_scripts/ 与 scripts/ 目录,杜绝路径遍历)
2. 基于文件 mtime 的内存脚本缓存与热重载
3. 请求上下文宏变量插值({{context.model}}, {{context.prompt}}, {{context.session_id}} 等)
4. 运行时入参闭包包装 (async function(__CONTEXT__, __ARGS__) { ... })(context, args)
5. 脚本文件自动扫描与 JSDoc 元数据解析
6. 100% 向后兼容传统内联 JS 字符串
"""
import json
import os
import re
import time
import hashlib
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
from app.core.config import get_logger, WorkflowError
logger = get_logger("WORKFLOW.SCRIPT_LOADER")
# 宏变量匹配正则:{{ context.xxx }} 或 {{context.xxx}}
MACRO_PATTERN = re.compile(r"\{\{\s*context\.([a-zA-Z0-9_.]+)\s*\}\}")
# JSDoc @description 或首行注释匹配正则
JSDOC_DESC_PATTERN = re.compile(
r"@description\s+([^\r\n*]+)",
re.IGNORECASE,
)
BLOCK_COMMENT_FIRST_LINE_PATTERN = re.compile(
r"/\*\*\s*[\r\n]+\s*\*\s*([^\r\n*]+)",
)
SINGLE_LINE_COMMENT_PATTERN = re.compile(
r"^\s*//\s*([^\r\n]+)",
re.MULTILINE,
)
class ScriptLoader:
"""工作流脚本安全加载与模板处理器"""
# 常见 JS 语法特征字符与关键字,用于防止单行内联 JS 被误判为文件路径
JS_SYNTAX_CHARS = frozenset("();{}\"'=><+*!?[]|&,")
JS_KEYWORDS = (
"return ", "const ", "let ", "var ", "function", "async ", "await ",
"if ", "for ", "while ", "throw ", "try ", "catch ", "import ", "export ",
"document.", "window.", "console.", "math.", "json."
)
def __init__(self, base_dir: Optional[Path] = None):
if base_dir is None:
# 当前文件路径为 app/core/workflow/script_loader.py -> 项目根目录为 3 层上级
self.base_dir = Path(__file__).resolve().parents[3]
else:
self.base_dir = Path(base_dir).resolve()
# 允许加载脚本的根目录集合
self.allowed_dirs: List[Path] = [
(self.base_dir / "custom_scripts").resolve(),
(self.base_dir / "scripts").resolve(),
]
# 内存缓存:{ absolute_path_str: ((mtime_ns, ctime_ns, size, ino), content) }
self._cache: Dict[str, Tuple[Tuple[int, int, int, int], str]] = {}
def clear_cache(self) -> None:
"""清空脚本内存缓存"""
self._cache.clear()
def _normalize_target_path_str(self, script_target: str) -> str:
"""去除 URI 前缀并将反斜杠归一化"""
raw = str(script_target or "").strip()
if raw.startswith("file://"):
raw = raw[7:].lstrip("/\\")
elif raw.startswith("scripts://"):
raw = raw[10:].lstrip("/\\")
raw = os.path.join("scripts", raw)
else:
raw = raw.lstrip("/\\")
return raw
def is_script_target(self, target_or_code: Any) -> bool:
"""
严格判断 target_or_code 是否为外部脚本文件路径或 URI。
拒绝任何包含换行符、常见 JS 语法符号或关键字的内联代码。
"""
if not isinstance(target_or_code, str):
return False
s = target_or_code.strip()
if not s or "\n" in s or "\r" in s:
return False
# 明确协议前缀:file:// 或 scripts://
if s.startswith("file://") or s.startswith("scripts://"):
return True
# 若包含常见 JS 语法字符或关键字,绝非文件路径
if any(c in s for c in self.JS_SYNTAX_CHARS):
return False
s_lower = s.lower()
if any(kw in s_lower for kw in self.JS_KEYWORDS):
return False
norm = s.replace("\\", "/")
# 若以目录前缀开头且以 .js 结尾
if (norm.startswith("custom_scripts/") or norm.startswith("scripts/")) and norm.lower().endswith(".js"):
return True
# 若以 .js 结尾且为合法单文件名(无空格,无路径分隔符)
if norm.lower().endswith(".js") and "/" not in norm and " " not in norm:
return True
return False
def resolve_script_path(self, script_target: str) -> Path:
"""
解析并校验脚本路径安全性,防止路径遍历攻击。
支持相对路径、URI 以及单文件名在 allowed_dirs 中自动寻址。
若路径越界或不合法,抛出 WorkflowError。
若文件不存在,抛出 WorkflowError。
"""
norm_str = self._normalize_target_path_str(script_target)
if not norm_str:
raise WorkflowError("js_script_target_empty")
# 1. 尝试直接以 base_dir 构造候选路径
candidate_path = (self.base_dir / norm_str).resolve()
# 检查是否命中允许的沙箱目录
matched_safe_dir = False
for allowed_dir in self.allowed_dirs:
try:
candidate_path.relative_to(allowed_dir)
matched_safe_dir = True
break
except ValueError:
continue
# 2. 若未命中且 norm_str 为无目录结构的纯单文件名,按优先级在 allowed_dirs 中寻找
if not matched_safe_dir and "/" not in norm_str.replace("\\", "/"):
for allowed_dir in self.allowed_dirs:
sub_candidate = (allowed_dir / norm_str).resolve()
try:
sub_candidate.relative_to(allowed_dir)
if sub_candidate.is_file():
candidate_path = sub_candidate
matched_safe_dir = True
break
except ValueError:
continue
if not matched_safe_dir:
logger.warning(
f"[ScriptLoader] 拒绝非法的跨目录脚本路径: {script_target!r} -> {candidate_path}"
)
raise WorkflowError(f"js_script_path_forbidden: {script_target}")
if not candidate_path.suffix.lower() == ".js":
logger.warning(
f"[ScriptLoader] 拒绝非 .js 后缀文件: {script_target!r} -> {candidate_path}"
)
raise WorkflowError(f"js_script_invalid_extension: {script_target}")
if not candidate_path.is_file():
logger.warning(
f"[ScriptLoader] 脚本文件不存在: {script_target!r} -> {candidate_path}"
)
raise WorkflowError(f"js_script_not_found: {script_target}")
return candidate_path
def load_script_content(self, file_path: Path) -> str:
"""
基于 mtime 内存缓存读取脚本文件内容,修改即时重载。
"""
path_key = str(file_path.resolve())
try:
st = file_path.stat()
except Exception as e:
raise WorkflowError(f"js_script_stat_failed: {e}")
# B7:只比 st_mtime(浮点秒,部分文件系统精度 1~2s)会在「同一时间戳内被替换」时
# 返回旧脚本。签名改为 纳秒 mtime + ctime + 大小 + inode(原子替换会换 inode);
# 且文件在最近 2 秒内变动过时不信任缓存(racy-git 同款处理),直接重读。
signature = (st.st_mtime_ns, st.st_ctime_ns, st.st_size, getattr(st, "st_ino", 0))
racy = (time.time_ns() - st.st_mtime_ns) < 2_000_000_000
if path_key in self._cache and not racy:
cached_signature, cached_content = self._cache[path_key]
if cached_signature == signature:
return cached_content
try:
content = file_path.read_text(encoding="utf-8-sig")
self._cache[path_key] = (signature, content)
logger.debug(f"[ScriptLoader] 已加载脚本文件 (mtime_ns={st.st_mtime_ns}): {file_path.name}")
return content
except Exception as e:
raise WorkflowError(f"js_script_read_failed: {e}")
@staticmethod
def _lookup_context_value(context: Dict[str, Any], key_path: str) -> Any:
"""根据点分路径从 context 字典中查找值"""
parts = key_path.split(".")
current = context
for part in parts:
if isinstance(current, dict) and part in current:
current = current[part]
else:
return None
return current
def interpolate_macros(
self,
data: Any,
context: Optional[Dict[str, Any]],
is_code_template: bool = False,
) -> Any:
"""
递归对字符串、字典或列表中的 {{context.key}} 进行宏变量插值替换。
当 is_code_template 为 True 时,对字符串值进行安全转义(json.dumps[1:-1]),
防止破坏外层 JS 代码中的字符串字面量或注入非法换行。
"""
if context is None:
context = {}
if isinstance(data, str):
if "{{" not in data:
return data
# 如果整个字符串恰好就是一个 {{context.xxx}},且不是代码模板模式,直接返回上下文中的原始值
exact_match = re.fullmatch(r"\{\{\s*context\.([a-zA-Z0-9_.]+)\s*\}\}", data.strip())
if exact_match and not is_code_template:
val = self._lookup_context_value(context, exact_match.group(1).strip())
return val if val is not None else ""
def _repl(match: re.Match) -> str:
key_path = match.group(1).strip()
val = self._lookup_context_value(context, key_path)
if val is None:
return ""
if isinstance(val, (dict, list, bool, int, float)):
return json.dumps(val, ensure_ascii=False, default=str)
val_str = str(val)
if is_code_template:
return json.dumps(val_str, ensure_ascii=False)[1:-1]
return val_str
return MACRO_PATTERN.sub(_repl, data)
elif isinstance(data, dict):
return {
k: self.interpolate_macros(v, context, is_code_template=is_code_template)
for k, v in data.items()
}
elif isinstance(data, list):
return [
self.interpolate_macros(item, context, is_code_template=is_code_template)
for item in data
]
return data
def wrap_script_closure(
self,
code: str,
context: Optional[Dict[str, Any]] = None,
args: Optional[Any] = None,
script_id: str = "",
lifecycle: str = "workflow",
owner_id: str = "",
) -> str:
"""
将脚本代码包装为标准的异步闭包执行体:
return (async function(__CONTEXT__, __ARGS__) {
<code>
})(context_json, args_json)
"""
safe_context = context if isinstance(context, dict) else {}
safe_args = args if (args is not None and args != "") else {}
# 若 args 为 JSON 字符串,尝试反序列化
if isinstance(safe_args, str):
trimmed = safe_args.strip()
if (trimmed.startswith("{") and trimmed.endswith("}")) or (trimmed.startswith("[") and trimmed.endswith("]")):
try:
safe_args = json.loads(trimmed)
except (TypeError, ValueError, RecursionError):
pass
context_json = json.dumps(safe_context, ensure_ascii=False, default=str)
args_json = json.dumps(safe_args, ensure_ascii=False, default=str)
# The page-side registry gives managed scripts an explicit disposal scope.
# It cannot undo arbitrary side effects automatically; scripts must register
# cleanup callbacks for globals, listeners, timers, and other resources.
lifecycle_value = str(lifecycle or "workflow").strip().lower()
if lifecycle_value not in {"workflow", "resident", "step"}:
lifecycle_value = "workflow"
safe_script_id = str(script_id or "inline").strip() or "inline"
safe_owner_id = str(owner_id or "default").strip() or "default"
lifecycle_bootstrap = (
"const __uwaLifecycle = (window.__UWA_SCRIPT_LIFECYCLE__ "
"|| (window.__UWA_SCRIPT_LIFECYCLE__ = (function(){"
"const entries = new Map(); let current = null; let activeOwner = null;"
"const disposeEntry = (entry) => { if (!entry || entry.disposed) return; entry.disposed = true;"
"try { if (entry.controller) entry.controller.abort(); } catch (_) {}"
"try { for (const fn of entry.cleanups.splice(0)) { try { fn(); } catch (_) {} } } catch (_) {}"
"if (entries.get(entry.id) === entry) entries.delete(entry.id); };"
"const activate = (id, policy, owner) => { const active = !activeOwner || activeOwner === owner;"
"if (!active) return {id, policy, owner, active:false, controller:null, signal:null, cleanups:[], disposed:true};"
"const previous = entries.get(id); if (previous) disposeEntry(previous);"
"const controller = typeof AbortController === 'function' ? new AbortController() : null;"
"const entry = {id, policy, owner, active, controller, signal: controller ? controller.signal : null, cleanups: [], disposed:false};"
"entries.set(id, entry); current = entry; return entry; };"
"const registerCleanup = (fn) => { if (current && typeof fn === 'function') {"
"current.cleanups.push(fn); return fn; } return null; };"
"const cleanup = (id) => disposeEntry(entries.get(id));"
"const beginWorkflow = (owner) => { activeOwner = owner; for (const entry of Array.from(entries.values())) {"
"if (entry.policy !== 'resident' && entry.owner !== owner) disposeEntry(entry); } };"
"const cleanupOwner = (owner) => { for (const entry of Array.from(entries.values())) {"
"if (entry.owner === owner && entry.policy !== 'resident') disposeEntry(entry); } };"
"const cleanupWorkflow = () => { for (const entry of Array.from(entries.values())) {"
"if (entry.policy !== 'resident') disposeEntry(entry); } };"
"const cleanupAll = () => { for (const entry of Array.from(entries.values())) disposeEntry(entry); };"
"return {activate, registerCleanup, cleanup, beginWorkflow, cleanupOwner, cleanupWorkflow, cleanupAll};"
"})()));"
f"const __uwaScope = __uwaLifecycle.activate({json.dumps(safe_script_id, ensure_ascii=False)}, {json.dumps(lifecycle_value)}, {json.dumps(safe_owner_id, ensure_ascii=False)});"
"const __uwaPreviousScope = window.__UWA_ACTIVE_SCRIPT_SCOPE__;"
"window.__UWA_ACTIVE_SCRIPT_SCOPE__ = __uwaScope;"
"const __uwaRegisterCleanup = (fn) => { if (__uwaScope && typeof fn === 'function') {"
"__uwaScope.cleanups.push(fn); return fn; } return null; };"
"const __uwaSignal = __uwaScope.signal;"
)
lifecycle_restore = (
"window.__UWA_ACTIVE_SCRIPT_SCOPE__ = __uwaPreviousScope;"
)
# 包装为闭包
wrapped = (
f"return (async function(__CONTEXT__, __ARGS__) {{\n"
f"{lifecycle_bootstrap}\n"
"if (__uwaScope.active) { try {\n"
f"{code}\n"
"} finally {\n"
f"{lifecycle_restore}\n"
"} }\n"
f"}})({context_json}, {args_json});"
)
return wrapped
def resolve_and_load(
self,
script_target: Optional[str] = None,
code_or_args: Optional[Any] = None,
context: Optional[Dict[str, Any]] = None,
script_id: str = "",
lifecycle: str = "workflow",
owner_id: str = "",
) -> str:
"""
统一解析并准备可直接在页面执行的 JavaScript 代码。
分支 1:外部脚本模式(script_target 显式非空)
- 强制按外部脚本文件加载与解析(若路径不合法或文件不存在,立即 Fail-Fast 抛错,绝不静默穿透回退为内联 JS);
- 插值替换宏变量;
- 将 code_or_args 作为 __ARGS__、context 作为 __CONTEXT__ 包装进闭包。
分支 2:向后兼容传统内联 JS 代码(script_target 为空,code_or_args 承载代码)
- 如果 code_or_args 形式为脚本文件路径,则按文件加载;
- 否则对内联代码进行宏变量插值,并直接返回该代码字符串执行。
"""
ctx = context if isinstance(context, dict) else {}
raw_target = str(script_target or "").strip()
ctx_model = ctx.get("model", "")
ctx_prompt = str(ctx.get("prompt", "") or "")[:80]
ctx_session = ctx.get("session_id", "")
logger.debug(
f"[ScriptLoader:RESOLVE] 开始解析脚本: target={raw_target!r}, "
f"context={{model: {ctx_model!r}, session_id: {ctx_session!r}, prompt_preview: {ctx_prompt!r}}}, "
f"code_or_args_preview={str(code_or_args)[:120]!r}"
)
# 分支 1:显式指定了 script_target,严格按照外部脚本执行(Fail-Fast)
if raw_target:
file_path = self.resolve_script_path(raw_target)
relative_id = file_path.relative_to(self.base_dir).as_posix()
effective_script_id = script_id or relative_id
raw_code = self.load_script_content(file_path)
interpolated_code = self.interpolate_macros(raw_code, ctx, is_code_template=True)
# 智能解析 code_or_args(若为 JSON 字符串则先反序列化)
parsed_args = code_or_args
if isinstance(parsed_args, str):
trimmed = parsed_args.strip()
if (trimmed.startswith("{") and trimmed.endswith("}")) or (trimmed.startswith("[") and trimmed.endswith("]")):
try:
parsed_args = json.loads(trimmed)
except (TypeError, ValueError, RecursionError):
pass
# 对入参 args 进行宏变量插值
interpolated_args = self.interpolate_macros(parsed_args, ctx, is_code_template=False)
logger.debug(
f"[ScriptLoader:MACRO] 外部脚本宏插值完成: file={file_path.name}, "
f"code_len={len(interpolated_code)}, args={str(interpolated_args)[:150]}"
)
wrapped = self.wrap_script_closure(
code=interpolated_code,
context=ctx,
args=interpolated_args,
script_id=effective_script_id,
lifecycle=lifecycle,
owner_id=owner_id,
)
logger.debug(
f"[ScriptLoader:WRAP] 闭包构建完成: file={file_path.name}, "
f"wrapped_len={len(wrapped)}, context_keys={list(ctx.keys())}"
)
return wrapped
# 分支 2:script_target 为空,检查 code_or_args 是否为脚本文件路径
if isinstance(code_or_args, str) and self.is_script_target(code_or_args):
file_path = self.resolve_script_path(code_or_args)
relative_id = file_path.relative_to(self.base_dir).as_posix()
effective_script_id = script_id or relative_id
raw_code = self.load_script_content(file_path)
interpolated_code = self.interpolate_macros(raw_code, ctx, is_code_template=True)
logger.debug(
f"[ScriptLoader:MACRO] 纯路径脚本宏插值完成: file={file_path.name}, code_len={len(interpolated_code)}"
)
wrapped = self.wrap_script_closure(
code=interpolated_code,
context=ctx,
args={},
script_id=effective_script_id,
lifecycle=lifecycle,
owner_id=owner_id,
)
logger.debug(
f"[ScriptLoader:WRAP] 闭包构建完成: file={file_path.name}, wrapped_len={len(wrapped)}"
)
return wrapped
# 传统内联 JS 字符串
inline_code = str(code_or_args or "").strip()
if not inline_code:
raise WorkflowError("js_exec_empty")
# 对内联代码执行宏变量插值(代码模式)
interpolated_inline = str(self.interpolate_macros(inline_code, ctx, is_code_template=True))
logger.debug(
f"[ScriptLoader:INLINE] 内联脚本宏插值完成: original={inline_code[:80]!r}, "
f"interpolated={interpolated_inline[:80]!r}"
)
# Inline JS also participates in the lifecycle registry. Hashing keeps the
# identifier stable without exposing the full source in page globals.
# Preserve the historical raw-inline execution unless lifecycle control
# was explicitly requested for this inline step.
if not script_id and str(lifecycle or "workflow").strip().lower() == "workflow":
return interpolated_inline
effective_script_id = script_id or (
"inline:" + hashlib.sha256(interpolated_inline.encode("utf-8")).hexdigest()[:16]
)
return self.wrap_script_closure(
code=interpolated_inline,
context=ctx,
args={},
script_id=effective_script_id,
lifecycle=lifecycle,
owner_id=owner_id,
)
def extract_script_description(self, content: str, default_name: str = "") -> str:
"""从 JavaScript 脚本头部注释提取描述信息"""
head_sample = content[:2000]
# 1. 优先提取 @description
m = JSDOC_DESC_PATTERN.search(head_sample)
if m:
return m.group(1).strip()
# 2. 提取 JSDoc 首行
m = BLOCK_COMMENT_FIRST_LINE_PATTERN.search(head_sample)
if m:
desc = m.group(1).strip()
if desc and not desc.startswith("@"):
return desc
# 3. 提取单行注释 // ...
for match in SINGLE_LINE_COMMENT_PATTERN.finditer(head_sample):
desc = match.group(1).strip()
# 排除常见非描述单行注释(如 ==, --, eslint, @ts-check, jshint, / 等)
if (
desc
and not desc.startswith("==")
and not desc.startswith("--")
and not desc.startswith("@")
and not desc.lower().startswith("eslint")
and not desc.lower().startswith("jshint")
and not desc.lower().startswith("istanbul")
):
return desc
return default_name or "未提供描述"
def scan_available_scripts(self) -> List[Dict[str, Any]]:
"""
安全扫描 custom_scripts/ 与 scripts/ 目录下的所有 .js 文件,
返回相对路径、URI、文件名、所属分类、修改时间与 JSDoc 描述。
"""
results: List[Dict[str, Any]] = []
scan_roots = [
("custom_scripts", self.base_dir / "custom_scripts", "custom"),
("scripts", self.base_dir / "scripts", "scripts"),
]
seen_rel_paths = set()
for folder_name, folder_path, category in scan_roots:
if not folder_path.is_dir():
continue
for root, dirs, files in os.walk(folder_path):
# 排除隐藏目录与 node_modules
dirs[:] = [d for d in dirs if not d.startswith(".") and d != "node_modules" and d != "__pycache__"]
for file_name in files:
if not file_name.lower().endswith(".js"):
continue
full_path = Path(root) / file_name
try:
rel_path = full_path.relative_to(self.base_dir).as_posix()
except ValueError:
continue
if rel_path in seen_rel_paths:
continue
seen_rel_paths.add(rel_path)
try:
stat = full_path.stat()
mtime = int(stat.st_mtime)
content = self.load_script_content(full_path)
description = self.extract_script_description(content, file_name)
except Exception as e:
logger.debug(f"[ScriptLoader] 扫描脚本元数据失败 {rel_path}: {e}")
mtime = 0
description = "脚本元数据读取失败"
results.append({
"path": rel_path,
"uri": f"file://{rel_path}",
"name": file_name,
"category": category,
"description": description,
"mtime": mtime,
})
# 按分类与路径排序
results.sort(key=lambda x: (0 if x["category"] == "custom" else 1, x["path"]))
return results
# 全局单例
script_loader = ScriptLoader()
File diff suppressed because it is too large. Load diff