Files
2026-11-02 22:54:14 +08:00

5143 lines
192 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
app/api/tab_routes.py - 标签页路由
职责:
- /api/tab-pool/tabs - 获取标签页列表
- /tab/{index}/v1/chat/completions - 指定标签页的聊天接口
- /url/{domain}/v1/chat/completions - 按域名路由选择标签页的聊天接口
"""
import json
import os
import random
import re
import time
import asyncio
import queue
import threading
from pathlib import Path
from typing import Optional, Any, Dict, List, Mapping
from urllib.parse import quote
from fastapi import APIRouter, Request, HTTPException, Depends, Header, Query
from fastapi.params import Param
from fastapi.responses import JSONResponse
from app.api.streaming_response import RequestStreamingResponse as StreamingResponse
from pydantic import BaseModel, Field, field_validator
from app.core.config import _request_context, atomic_write_json, get_logger, SSEFormatter
from app.core import get_browser
from app.services.request_manager import (
request_manager,
RequestContext,
RequestStatus,
watch_client_disconnect,
cancel_storm_guard,
)
from app.services.request_lifecycle import (
TrackedWorkerExecutionCancelled,
request_finalization,
cleanup_worker_thread_after_request,
get_max_request_execute_time_sec,
mark_request_hard_timeout,
put_worker_queue_item,
run_in_request_context,
run_tracked_blocking_call,
wait_worker_queue_item,
)
from app.services.arena_direct_models import (
build_openai_model_entries,
get_arena_direct_catalog_for_tab,
list_arena_direct_models,
)
from app.services.tool_calling import (
build_tool_completion_response,
complete_tool_calling_roundtrip_async,
decode_browser_non_stream_payload,
extract_tool_calling_assistant_content,
get_tool_calling_allow_media_postprocess,
has_tool_calling_request,
iter_tool_stream_chunks,
normalize_tool_request,
summarize_messages_for_debug,
)
from app.services.error_metadata import (
resolve_error_metadata,
build_error_response,
)
from app.api.openai_stop import (
apply_stop_sequences_to_text,
build_stop_sequence_stream_state,
extract_openai_sse_error_message,
filter_openai_stop_sse_chunk,
flush_openai_stop_state,
iter_openai_sse_payloads,
sse_chunk_has_done,
)
from app.api.deps import verify_dashboard_auth as verify_auth, verify_service_auth
from app.utils.site_url import (
encode_tab_url_route_token,
extract_remote_site_domain,
get_canonical_route_domain,
normalize_exact_tab_url,
normalize_route_domain,
route_domain_matches,
tab_url_matches,
)
from app.utils.tab_route_groups import (
normalize_route_group_id,
normalize_route_groups,
route_group_member_key,
route_groups_by_id,
)
from app.core.tab_pool_parts._utils import _should_skip_pool_url
logger = get_logger("API.TAB")
router = APIRouter()
MODEL_LIST_CREATED = int(time.time())
def _reset_stream_request_context(request_context_token: Any) -> None:
"""Reset request logging context when the streaming generator owns its Context.
Starlette can finalize a disconnected streaming response from a separate
Context. ContextVar tokens cannot be reset there, but that Context has no
request value to restore.
"""
try:
_request_context.reset(request_context_token)
except ValueError as exc:
if "different Context" not in str(exc):
raise
logger.debug("流式请求上下文已在其他 Context 中结束,跳过重置")
def _format_rfc3339_timestamp(value: Any) -> str:
try:
timestamp = int(value or MODEL_LIST_CREATED)
except Exception:
timestamp = MODEL_LIST_CREATED
return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(timestamp))
def _build_claude_route_model_id(_value: Any) -> str:
# Claude filters gateway model IDs that contain provider names like "glm".
return "claude-sonnet-4-5"
def _build_route_models_payload(
*,
model_id: str,
display_name: str,
anthropic_version: Optional[str] = None,
) -> Dict[str, Any]:
entry = {
"id": model_id,
"object": "model",
"type": "model",
"created": MODEL_LIST_CREATED,
"owned_by": "universal-web-api",
"display_name": display_name or model_id,
}
if anthropic_version:
item = {
"type": "model",
"id": entry["id"],
"display_name": entry["display_name"],
"created_at": _format_rfc3339_timestamp(entry["created"]),
}
return {
"data": [item],
"has_more": False,
"first_id": item["id"],
"last_id": item["id"],
}
return {
"object": "list",
"data": [entry],
}
def _build_route_model_entries_payload(
entries: List[Dict[str, Any]],
*,
anthropic_version: Optional[str] = None,
) -> Dict[str, Any]:
if anthropic_version:
data = [
{
"type": "model",
"id": str(item.get("id") or ""),
"display_name": str(item.get("display_name") or item.get("id") or ""),
"created_at": _format_rfc3339_timestamp(item.get("created")),
}
for item in entries
if str(item.get("id") or "").strip()
]
return {
"data": data,
"has_more": False,
"first_id": data[0]["id"] if data else None,
"last_id": data[-1]["id"] if data else None,
}
return {"object": "list", "data": entries}
def _unwrap_fastapi_param_value(value: Any) -> Any:
if isinstance(value, Param):
return value.default
return value
def _normalize_optional_tab_index_value(value: Any) -> Optional[int]:
value = _unwrap_fastapi_param_value(value)
if value in (None, ""):
return None
return int(value)
HEADER_VALUE_QUOTE_SAFE = ":/?#[]@!$&'()*+,;=%-._~"
def _encode_response_header_value(value: Any) -> str:
text = "" if value is None else str(value)
# 判据用 ASCII 而不是 latin-1:latin-1 可编码 U+0080–U+00FF(如预设名 "Café"),
# 于是这些字符会以裸字节进响应头,按 UTF-8 解析响应头的客户端
# (Node/undici、Go 等)会拿到乱码甚至解码失败。非 ASCII 一律百分号编码。
needs_quote = (not text.isascii()) or any(ord(ch) < 32 or ord(ch) == 127 for ch in text)
if not needs_quote:
return text
return quote(text, safe=HEADER_VALUE_QUOTE_SAFE)
def _encode_response_headers(headers: Optional[Mapping[str, Any]]) -> Dict[str, str]:
if not headers:
return {}
return {str(key): _encode_response_header_value(value) for key, value in headers.items()}
def _get_tab_pool_allocation_mode(tab_pool: Any) -> str:
mode = str(getattr(tab_pool, "allocation_mode", "") or "").strip() or "first_idle"
valid_modes = {"first_idle", "round_robin", "random"}
return mode if mode in valid_modes else "first_idle"
def _get_config_allocation_mode(tab_pool_config: Any) -> str:
source = tab_pool_config if isinstance(tab_pool_config, dict) else {}
mode = str(source.get("allocation_mode") or "").strip().lower() or "first_idle"
valid_modes = {"first_idle", "round_robin", "random"}
return mode if mode in valid_modes else "first_idle"
def _attach_preset_info_to_tabs(
tabs: List[Dict[str, Any]],
config_engine: Any,
auto_remember_url_presets: bool = True
) -> None:
preset_cache: Dict[str, tuple[List[str], Optional[str]]] = {}
preset_overrides = _read_preset_overrides() if auto_remember_url_presets else {"urls": {}}
for tab_info in tabs:
domain = str(tab_info.get("current_domain") or tab_info.get("route_domain") or "").strip()
normalized_domain = normalize_route_domain(domain) or domain
canonical_domain = get_canonical_route_domain(normalized_domain) or ""
candidate_domains = [
item for item in (normalized_domain, canonical_domain)
if item and item not in {""}
]
candidate_domains = list(dict.fromkeys(candidate_domains))
preset_name = tab_info.get("preset_name")
override_source = str(tab_info.get("preset_override_source") or "").strip()
current_url = str(tab_info.get("url") or "").strip()
url_key = normalize_exact_tab_url(current_url)
if auto_remember_url_presets and not override_source and url_key and url_key in preset_overrides.get("urls", {}):
override_source = "url"
if not candidate_domains:
tab_info["preset_route_domain"] = ""
tab_info["preset_domain_route_prefix"] = ""
tab_info["available_presets"] = []
tab_info["default_preset"] = None
tab_info["effective_preset_name"] = preset_name
tab_info["preset_override_source"] = override_source
tab_info["is_using_default_preset"] = not bool(preset_name)
continue
resolved_domain = candidate_domains[0]
available_presets: List[str] = []
default_preset: Optional[str] = None
for candidate_domain in candidate_domains:
if candidate_domain not in preset_cache:
try:
preset_cache[candidate_domain] = (
config_engine.list_presets(candidate_domain),
config_engine.get_default_preset(candidate_domain),
)
except Exception as e:
logger.debug(f"读取标签页预设失败: {candidate_domain}: {e}")
preset_cache[candidate_domain] = ([], None)
candidate_presets, candidate_default = preset_cache[candidate_domain]
if candidate_presets or candidate_default:
resolved_domain = candidate_domain
available_presets = candidate_presets
default_preset = candidate_default
break
if candidate_domain == candidate_domains[0]:
available_presets = candidate_presets
default_preset = candidate_default
tab_info["preset_route_domain"] = resolved_domain
tab_info["preset_domain_route_prefix"] = f"/url/{resolved_domain}" if resolved_domain else ""
tab_info["available_presets"] = available_presets
tab_info["default_preset"] = default_preset
tab_info["effective_preset_name"] = preset_name or default_preset
tab_info["preset_override_source"] = override_source
tab_info["is_using_default_preset"] = not bool(preset_name)
FOLLOW_DEFAULT_PRESET = "__DEFAULT__"
STREAM_QUEUE_POLL_TIMEOUT = 0.5
SSE_HEARTBEAT_INTERVAL = 5.0
TAB_POOL_ALLOCATION_OPTIONS = [
{"value": "first_idle", "label": "优先空闲"},
{"value": "round_robin", "label": "轮询"},
{"value": "random", "label": "随机"},
]
TAB_ROUTE_METHOD_OPTIONS = [
{"value": "domain", "label": "站点域名路由"},
{"value": "route_group", "label": "标签页路由组"},
{"value": "fixed_tab", "label": "固定标签页路由"},
{"value": "exact_url", "label": "标签页 URL 路由"},
{"value": "exact_url_preset", "label": "URL 绑定预设路由"},
]
DEFAULT_TAB_ROUTE_METHODS = {"domain", "route_group", "fixed_tab", "exact_url", "exact_url_preset"}
TAB_SELECTOR_OPTIONS = {"first_idle", "round_robin", "random"}
_route_round_robin_cursor: Dict[str, int] = {}
_route_round_robin_lock = threading.Lock()
_browser_config_lock = threading.RLock()
_model_name_overrides_lock = threading.RLock()
_preset_overrides_lock = threading.RLock()
MODEL_NAME_OVERRIDES_PATH = Path("config/model_name_overrides.local.json")
PRESET_OVERRIDES_PATH = Path("config/preset_overrides.local.json")
async def _cleanup_route_worker_thread(
worker_thread: Optional[threading.Thread],
ctx: RequestContext,
*,
fast_returned_on_audio: bool = False,
done_emitted: bool = False,
) -> bool:
if not isinstance(worker_thread, threading.Thread) or not worker_thread.is_alive():
return False
if fast_returned_on_audio:
ctx.mark_worker_stop_requested("audio_media_fast_return")
ctx.mark_completed()
return await cleanup_worker_thread_after_request(
worker_thread,
ctx,
completed=True,
retire_reason="worker_audio_fast_return_timeout",
completed_join_timeout=0.2,
)
elif ctx.status == RequestStatus.COMPLETED:
return await cleanup_worker_thread_after_request(
worker_thread,
ctx,
completed=True,
retire_reason="worker_cleanup_timeout",
completed_join_timeout=0.2,
)
else:
return await cleanup_worker_thread_after_request(
worker_thread,
ctx,
completed=False,
cancel_reason="cleanup",
join_timeout=5.0,
retire_reason="worker_cleanup_timeout",
)
def _put_route_worker_queue_item(
chunk_queue: queue.Queue,
ctx: RequestContext,
item: Any,
*,
final: bool = False,
) -> bool:
return put_worker_queue_item(
chunk_queue,
ctx,
item,
final=final,
poll_timeout=0.01 if ctx.should_stop() else STREAM_QUEUE_POLL_TIMEOUT,
)
def _read_browser_config() -> Dict[str, Any]:
config_path = Path("config/browser_config.json")
if not config_path.exists():
return {}
with open(config_path, "r", encoding="utf-8-sig") as f:
return json.load(f)
def _write_browser_config_unlocked(payload: Dict[str, Any]) -> None:
atomic_write_json(Path("config/browser_config.json"), payload)
def _write_browser_config(payload: Dict[str, Any]) -> None:
with _browser_config_lock:
_write_browser_config_unlocked(payload)
def _extract_stream_error_message(chunk: Any) -> str:
return extract_openai_sse_error_message(chunk)
def _extract_chunk_media_items(data: Dict[str, Any]) -> List[Dict[str, Any]]:
media_items: List[Dict[str, Any]] = []
top_level_media = data.get("media")
if isinstance(top_level_media, list):
media_items.extend(item for item in top_level_media if isinstance(item, dict))
choices = data.get("choices")
if isinstance(choices, list) and choices:
delta = choices[0].get("delta", {})
if isinstance(delta, dict):
delta_media = delta.get("media")
if isinstance(delta_media, list):
media_items.extend(item for item in delta_media if isinstance(item, dict))
media_items.extend(_extract_content_part_media_items(delta.get("content")))
return media_items
def _extract_media_part_ref(value: Any) -> str:
if isinstance(value, dict):
return str(value.get("url") or value.get("data_uri") or "").strip()
return str(value or "").strip()
def _extract_content_part_media_items(content: Any) -> List[Dict[str, Any]]:
if isinstance(content, dict):
content = [content]
if not isinstance(content, list):
return []
media_items: List[Dict[str, Any]] = []
for part in content:
if not isinstance(part, dict):
continue
part_type = str(part.get("type") or "").strip().lower()
ref = ""
media_type = ""
if part_type in {"image_url", "input_image", "output_image"}:
ref = _extract_media_part_ref(
part.get("image_url") or part.get("url") or part.get("data_uri")
)
media_type = "image"
elif part_type in {"audio_url", "input_audio", "output_audio"}:
ref = _extract_media_part_ref(
part.get("audio_url") or part.get("input_audio") or part.get("url")
)
media_type = "audio"
elif part_type in {"video_url", "input_video", "output_video"}:
ref = _extract_media_part_ref(
part.get("video_url") or part.get("input_video") or part.get("url")
)
media_type = "video"
if not ref:
continue
media_item: Dict[str, Any] = {"media_type": media_type}
if ref.startswith("data:"):
media_item["data_uri"] = ref
else:
media_item["url"] = ref
detail = str(part.get("detail") or "").strip()
if detail:
media_item["detail"] = detail
mime = str(part.get("mime_type") or part.get("mime") or "").strip()
if mime:
media_item["mime"] = mime
label = str(part.get("label") or "").strip()
if label:
media_item["label"] = label
media_items.append(media_item)
return media_items
def _extract_delta_content_text(content: Any) -> str:
if content is None:
return ""
if isinstance(content, str):
return content
if isinstance(content, list):
parts: List[str] = []
for item in content:
if isinstance(item, str):
parts.append(item)
continue
if not isinstance(item, dict):
continue
item_type = str(item.get("type") or "").strip().lower()
if item_type in {"text", "input_text", "output_text"}:
parts.append(str(item.get("text") or ""))
return "".join(parts)
if isinstance(content, dict):
item_type = str(content.get("type") or "").strip().lower()
if item_type in {"text", "input_text", "output_text"}:
return str(content.get("text") or "")
return ""
def _iter_sse_payloads(chunk: Any) -> List[Dict[str, Any]]:
return iter_openai_sse_payloads(chunk)
def _make_buffered_sse_payload_parser():
buffer = ""
def parse(chunk: Any) -> List[Dict[str, Any]]:
nonlocal buffer
if not isinstance(chunk, str) or not chunk:
return []
buffer += chunk.replace("\r\n", "\n").replace("\r", "\n")
payloads: List[Dict[str, Any]] = []
while "\n\n" in buffer:
frame, buffer = buffer.split("\n\n", 1)
if not frame.strip():
continue
payloads.extend(_iter_sse_payloads(frame + "\n\n"))
return payloads
def flush() -> List[Dict[str, Any]]:
nonlocal buffer
tail = buffer
buffer = ""
if not tail.strip():
return []
return _iter_sse_payloads(tail + "\n\n")
parse.flush = flush # type: ignore[attr-defined]
return parse
def _consume_non_stream_sse_payload(
data: Dict[str, Any],
*,
collected_content: List[str],
collected_media: List[Dict[str, Any]],
) -> Optional[Dict[str, Any]]:
if "error" in data:
return data
media_items = _extract_chunk_media_items(data)
collected_media.extend(media_items)
if "choices" in data and data["choices"]:
delta = data["choices"][0].get("delta", {})
content = delta.get("content", "")
content_text = _extract_delta_content_text(content)
if content_text:
collected_content.append(content_text)
return None
def _extract_sse_chunk_media_items(chunk: Any) -> List[Dict[str, Any]]:
media_items: List[Dict[str, Any]] = []
for payload in _iter_sse_payloads(chunk):
media_items.extend(_extract_chunk_media_items(payload))
return media_items
def _has_audio_media(media_items: List[Dict[str, Any]]) -> bool:
return any(
str(item.get("media_type") or "").strip().lower() == "audio"
for item in media_items or []
if isinstance(item, dict)
)
def _should_fast_return_on_audio_media(body: "ChatRequest") -> bool:
text = " ".join(
str(value or "").strip().lower()
for value in (
getattr(body, "preset_name", None),
getattr(body, "model", None),
)
if value
)
if not text:
return False
markers = ("朗读", "语音朗读", "read aloud", "text-to-speech", "tts", "voice")
return any(marker in text for marker in markers)
def _pack_audio_fast_return_chunks(body: "ChatRequest") -> List[str]:
chunks: List[str] = []
finish_chunk = SSEFormatter.pack_finish(model=body.model)
emit_finish, _finish_had_done = _split_sse_done_frame(finish_chunk)
if emit_finish:
chunks.append(emit_finish)
usage_chunk = _maybe_pack_stream_usage_chunk(body)
if usage_chunk:
chunks.append(usage_chunk)
chunks.append(_pack_done())
return chunks
def _dedupe_media_items(media_items: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
deduped: List[Dict[str, Any]] = []
seen = set()
for item in media_items or []:
media_type = str(item.get("media_type") or "").strip().lower()
ref = str(item.get("url") or item.get("data_uri") or "").strip()
key = (media_type, ref)
if key in seen:
continue
seen.add(key)
deduped.append(item)
return deduped
def _cleanup_non_stream_content(content: str) -> str:
placeholder_pattern = re.compile(
r"^\s*https?://(?:[\w.-]+\.)?googleusercontent\.com/(?:image_generation_content|generated_music_content)/\d+\s*$",
re.IGNORECASE | re.MULTILINE,
)
cleaned = placeholder_pattern.sub("", content or "")
return re.sub(r"\n{3,}", "\n\n", cleaned).strip()
def _normalize_tab_selector(value: str, default: str = "first_idle") -> str:
selector = str(value or "").strip().lower()
if selector in TAB_SELECTOR_OPTIONS:
return selector
return default
def _normalize_enabled_route_methods(value: Any) -> List[str]:
if not isinstance(value, list):
return [item["value"] for item in TAB_ROUTE_METHOD_OPTIONS]
normalized: List[str] = []
seen = set()
for item in value:
method = str(item or "").strip().lower()
if method in DEFAULT_TAB_ROUTE_METHODS and method not in seen:
seen.add(method)
normalized.append(method)
if not normalized:
return [item["value"] for item in TAB_ROUTE_METHOD_OPTIONS]
return normalized
def _normalize_excluded_urls(value: Any) -> List[str]:
if not isinstance(value, list):
return []
normalized: List[str] = []
seen = set()
for item in value:
text = str(item or "").strip()
normalized_text = normalize_exact_tab_url(text) or text
if not normalized_text or normalized_text in seen:
continue
seen.add(normalized_text)
normalized.append(normalized_text)
return normalized
def _normalize_model_name(value: Any) -> str:
return str(value or "").strip()
def _normalize_model_name_key(value: Any) -> str:
return _normalize_model_name(value).lower()
def _normalize_model_name_overrides(value: Any) -> Dict[str, Dict[str, str]]:
payload = value if isinstance(value, dict) else {}
normalized: Dict[str, Dict[str, str]] = {"sites": {}, "urls": {}}
sites = payload.get("sites") if isinstance(payload, dict) else {}
if isinstance(sites, dict):
for key, model_name in sites.items():
route_key = normalize_route_domain(key)
display_name = _normalize_model_name(model_name)
if route_key and display_name:
normalized["sites"][route_key] = display_name
urls = payload.get("urls") if isinstance(payload, dict) else {}
if isinstance(urls, dict):
for key, model_name in urls.items():
url_key = normalize_exact_tab_url(str(key or "").strip())
display_name = _normalize_model_name(model_name)
if url_key and display_name:
normalized["urls"][url_key] = display_name
return normalized
def _get_legacy_model_name_overrides_from_config(config: Optional[Dict[str, Any]] = None) -> Dict[str, Dict[str, str]]:
payload = config if isinstance(config, dict) else _read_browser_config()
tab_pool_config = payload.get("tab_pool") if isinstance(payload, dict) else {}
if not isinstance(tab_pool_config, dict):
tab_pool_config = {}
return _normalize_model_name_overrides(tab_pool_config.get("model_name_overrides"))
def _read_model_name_overrides_unlocked() -> Dict[str, Dict[str, str]]:
if MODEL_NAME_OVERRIDES_PATH.exists():
try:
with open(MODEL_NAME_OVERRIDES_PATH, "r", encoding="utf-8-sig") as f:
return _normalize_model_name_overrides(json.load(f))
except Exception as e:
logger.warning(f"读取模型显示名称本地配置失败: {e}")
return {"sites": {}, "urls": {}}
return _get_legacy_model_name_overrides_from_config()
def _read_model_name_overrides() -> Dict[str, Dict[str, str]]:
with _model_name_overrides_lock:
return _read_model_name_overrides_unlocked()
def _write_model_name_overrides_unlocked(overrides: Dict[str, Dict[str, str]]) -> Dict[str, Dict[str, str]]:
normalized = _normalize_model_name_overrides(overrides)
atomic_write_json(MODEL_NAME_OVERRIDES_PATH, normalized)
return normalized
def _get_tab_pool_model_name_overrides(tab_pool: Any, config: Optional[Dict[str, Any]] = None) -> Dict[str, Dict[str, str]]:
try:
overrides = getattr(tab_pool, "model_name_overrides", None)
if overrides is not None:
return _normalize_model_name_overrides(overrides)
except Exception:
pass
return _read_model_name_overrides()
def _sync_tab_pool_model_name_overrides(overrides: Dict[str, Dict[str, str]]) -> bool:
try:
from app.core.config import BrowserConstants
if hasattr(BrowserConstants, "reload"):
BrowserConstants.reload()
except Exception as reload_error:
logger.warning(f"热重载浏览器常量失败: {reload_error}")
try:
browser = get_browser(auto_connect=False)
browser.tab_pool.apply_runtime_config(model_name_overrides=overrides)
return True
except Exception as sync_error:
logger.warning(f"同步模型显示名称配置失败: {sync_error}")
return False
def _normalize_preset_overrides(value: Any) -> Dict[str, Dict[str, str]]:
payload = value if isinstance(value, dict) else {}
normalized = {"urls": {}}
urls = payload.get("urls") if isinstance(payload, dict) else {}
if isinstance(urls, dict):
for key, preset_name in urls.items():
url_key = normalize_exact_tab_url(str(key or "").strip())
p_name = str(preset_name or "").strip()
if url_key and p_name:
normalized["urls"][url_key] = p_name
return normalized
def _get_legacy_preset_overrides_from_config(config: Optional[Dict[str, Any]] = None) -> Dict[str, Dict[str, str]]:
payload = config if isinstance(config, dict) else _read_browser_config()
tab_pool_config = payload.get("tab_pool") if isinstance(payload, dict) else {}
if not isinstance(tab_pool_config, dict):
tab_pool_config = {}
return _normalize_preset_overrides(tab_pool_config.get("preset_overrides"))
def _read_preset_overrides_unlocked() -> Dict[str, Dict[str, str]]:
if PRESET_OVERRIDES_PATH.exists():
try:
with open(PRESET_OVERRIDES_PATH, "r", encoding="utf-8-sig") as f:
return _normalize_preset_overrides(json.load(f))
except Exception as e:
logger.warning(f"读取预设本地配置失败: {e}")
return {"urls": {}}
return _get_legacy_preset_overrides_from_config()
def _read_preset_overrides() -> Dict[str, Dict[str, str]]:
with _preset_overrides_lock:
return _read_preset_overrides_unlocked()
def _write_preset_overrides_unlocked(overrides: Dict[str, Dict[str, str]]) -> Dict[str, Dict[str, str]]:
normalized = _normalize_preset_overrides(overrides)
atomic_write_json(PRESET_OVERRIDES_PATH, normalized)
return normalized
def _sync_tab_pool_preset_overrides(overrides: Dict[str, Dict[str, str]]) -> bool:
try:
browser = get_browser(auto_connect=False)
browser.tab_pool.apply_runtime_config(preset_overrides=overrides)
return True
except Exception as sync_error:
logger.warning(f"同步预设记忆配置失败: {sync_error}")
return False
def _get_auto_remember_url_presets_from_config(config: Optional[Dict[str, Any]] = None) -> bool:
payload = config if isinstance(config, dict) else _read_browser_config()
tab_pool_config = payload.get("tab_pool") if isinstance(payload, dict) else {}
if not isinstance(tab_pool_config, dict):
tab_pool_config = {}
return _coerce_bool(tab_pool_config.get("auto_remember_url_presets"), False)
def _get_tab_pool_auto_remember_url_presets(tab_pool: Any, config: Optional[Dict[str, Any]] = None) -> bool:
try:
flag = getattr(tab_pool, "auto_remember_url_presets", None)
if flag is not None:
return bool(flag)
except Exception:
pass
return _get_auto_remember_url_presets_from_config(config)
def _coerce_bool(value: Any, default: bool = False) -> bool:
if isinstance(value, bool):
return value
if value is None:
return default
return str(value).strip().lower() in {"1", "true", "yes", "y", "on"}
def _get_enabled_route_methods_from_config(config: Optional[Dict[str, Any]] = None) -> List[str]:
payload = config if isinstance(config, dict) else _read_browser_config()
tab_pool_config = payload.get("tab_pool") if isinstance(payload, dict) else {}
if not isinstance(tab_pool_config, dict):
tab_pool_config = {}
return _normalize_enabled_route_methods(tab_pool_config.get("enabled_route_methods"))
def _get_excluded_urls_from_config(config: Optional[Dict[str, Any]] = None) -> List[str]:
payload = config if isinstance(config, dict) else _read_browser_config()
tab_pool_config = payload.get("tab_pool") if isinstance(payload, dict) else {}
if not isinstance(tab_pool_config, dict):
tab_pool_config = {}
return _normalize_excluded_urls(tab_pool_config.get("excluded_urls"))
def _get_route_groups_from_config(config: Optional[Dict[str, Any]] = None) -> List[Dict[str, Any]]:
payload = config if isinstance(config, dict) else _read_browser_config()
tab_pool_config = payload.get("tab_pool") if isinstance(payload, dict) else {}
if not isinstance(tab_pool_config, dict):
tab_pool_config = {}
return normalize_route_groups(tab_pool_config.get("route_groups"))
def _get_tab_pool_route_groups(tab_pool: Any, config: Optional[Dict[str, Any]] = None) -> List[Dict[str, Any]]:
try:
if hasattr(tab_pool, "get_route_groups_snapshot"):
return tab_pool.get_route_groups_snapshot()
route_groups = getattr(tab_pool, "route_groups", None)
if route_groups is not None:
return normalize_route_groups(route_groups)
except Exception:
pass
return _get_route_groups_from_config(config)
def _get_enabled_route_methods(config: Optional[Dict[str, Any]] = None) -> List[str]:
payload = config if isinstance(config, dict) else _read_browser_config()
tab_pool_config = payload.get("tab_pool") if isinstance(payload, dict) else {}
if not isinstance(tab_pool_config, dict):
tab_pool_config = {}
return _normalize_enabled_route_methods(tab_pool_config.get("enabled_route_methods"))
def _route_method_guard(method: str):
"""把路由方式开关做成 FastAPI 依赖,而不是在处理函数体里内联校验。
/url/{d}/{preset}/... 会在 Python 层直接调用 chat_with_route_domain,
app/api/chat.py 的 arena catalog 分支、anthropic_routes 也一样。若把校验写在
函数体里,这些内部委派会一并被拦下——关掉"站点域名路由"会连普通的
/v1/chat/completions 都 403。依赖只在 HTTP 进入该条路由时执行,内部调用不触发,
拦截面正好落在用户在面板上看到的那几条前缀上。
"""
async def _guard() -> None:
_ensure_route_method_enabled(method)
return _guard
def _ensure_route_method_enabled(method: str) -> None:
"""拦截被关闭的寻址方式。
enabled_route_methods 此前只被读写和回显,没有任何路由分支消费它:
面板上取消勾选"固定标签页路由"后,/tab/1/v1/chat/completions 照样 200,
对外暴露服务时会给出"已关掉"的错觉。读配置失败时保持放行(fail-open),
不因为配置文件问题把正常路由全部打死。
"""
normalized = str(method or "").strip().lower()
if not normalized:
return
try:
enabled = _get_enabled_route_methods()
except Exception as e:
logger.debug(f"读取已启用路由方式失败(放行): {e}")
return
if normalized not in enabled:
label = next(
(item["label"] for item in TAB_ROUTE_METHOD_OPTIONS if item["value"] == normalized),
normalized,
)
raise HTTPException(status_code=403, detail=f"路由方式已在标签页池设置中关闭: {label}")
def _get_tab_pool_excluded_urls(tab_pool: Any, config: Optional[Dict[str, Any]] = None) -> List[str]:
try:
excluded_urls = getattr(tab_pool, "excluded_urls", None)
if excluded_urls is not None:
return _normalize_excluded_urls(excluded_urls)
except Exception:
pass
return _get_excluded_urls_from_config(config)
def _get_preserve_error_tabs_from_config(config: Optional[Dict[str, Any]] = None) -> bool:
payload = config if isinstance(config, dict) else _read_browser_config()
tab_pool_config = payload.get("tab_pool") if isinstance(payload, dict) else {}
if not isinstance(tab_pool_config, dict):
tab_pool_config = {}
return _coerce_bool(tab_pool_config.get("preserve_error_tabs"), False)
def _get_tab_pool_preserve_error_tabs(tab_pool: Any, config: Optional[Dict[str, Any]] = None) -> bool:
try:
value = getattr(tab_pool, "preserve_error_tabs", None)
if value is not None:
return _coerce_bool(value, False)
except Exception:
pass
return _get_preserve_error_tabs_from_config(config)
def _tab_item_is_excluded(item: Dict[str, Any], excluded_urls: List[str]) -> bool:
return bool(_get_tab_item_exclusion_url(item, excluded_urls))
def _get_tab_item_exclusion_url(item: Dict[str, Any], excluded_urls: List[str]) -> str:
if not excluded_urls:
return ""
actual_url = str(item.get("url") or "").strip()
if not actual_url:
return ""
for excluded_url in excluded_urls:
if tab_url_matches(excluded_url, actual_url):
return excluded_url
return ""
def _get_pool_default_selector(browser) -> str:
"""当路由接口未显式传 selector 时,跟随标签页池当前分配模式。"""
try:
return _get_tab_pool_allocation_mode(browser.tab_pool)
except Exception as e:
logger.debug(f"读取标签页池默认分配模式失败,回退 first_idle: {e}")
return "first_idle"
def _get_tab_info_by_index(browser, tab_index: int) -> Optional[Dict[str, Any]]:
tabs = browser.tab_pool.get_tabs_with_index()
for item in tabs:
if int(item.get("persistent_index") or 0) == int(tab_index):
return item
return None
def _get_tabs_by_exact_url(browser, exact_url: str) -> List[Dict[str, Any]]:
target = normalize_exact_tab_url(exact_url)
if not target:
return []
matches: List[Dict[str, Any]] = []
for item in browser.tab_pool.get_tabs_with_index():
actual_url = str(item.get("url") or "").strip()
if tab_url_matches(target, actual_url):
matches.append(item)
return matches
def _get_tabs_by_url_route_token(browser, url_token: str) -> List[Dict[str, Any]]:
target = str(url_token or "").strip().lower()
if not target:
return []
matches: List[Dict[str, Any]] = []
for item in browser.tab_pool.get_tabs_with_index():
item_token = str(item.get("url_route_token") or "").strip().lower()
if item_token and item_token == target:
matches.append(item)
continue
actual_url = str(item.get("url") or "").strip()
if encode_tab_url_route_token(actual_url) == target:
matches.append(item)
return matches
def _get_tabs_by_exposed_model_name(browser, model_name: str) -> List[Dict[str, Any]]:
target = _normalize_model_name_key(model_name)
if not target:
return []
# Model-name routing is still dynamic routing. Keep it consistent with
# domain routing so tabs explicitly excluded from the pool can never be
# selected just because they expose the same model name.
excluded_urls = _get_tab_pool_excluded_urls(browser.tab_pool)
matches: List[Dict[str, Any]] = []
for item in browser.tab_pool.get_tabs_with_index():
exposed_name = _normalize_model_name_key(item.get("exposed_model_name"))
if (
exposed_name
and exposed_name == target
and not _tab_item_is_excluded(item, excluded_urls)
):
matches.append(item)
return matches
def _list_candidate_tabs(browser, route_domain: str = "") -> List[Dict[str, Any]]:
tabs = browser.tab_pool.get_tabs_with_index()
target = normalize_route_domain(route_domain)
excluded_urls = _get_tab_pool_excluded_urls(browser.tab_pool)
if not target:
# 无域名约束时同样要过滤"已从动态路由排除"的标签页,
# 否则这条分支会成为绕过 excluded_urls 的后门。
return [item for item in tabs if not _tab_item_is_excluded(item, excluded_urls)]
result: List[Dict[str, Any]] = []
for item in tabs:
actual_domain = str(item.get("current_domain") or item.get("route_domain") or "").strip()
if (
actual_domain
and route_domain_matches(target, actual_domain)
and not _tab_item_is_excluded(item, excluded_urls)
):
result.append(item)
return result
def _select_round_robin_tab(candidates: List[Dict[str, Any]], cursor_key: str) -> Dict[str, Any]:
if not candidates:
raise HTTPException(status_code=404, detail="没有可用标签页")
with _route_round_robin_lock:
last_index = _route_round_robin_cursor.get(cursor_key, -1)
chosen: Optional[Dict[str, Any]] = None
chosen_index: Optional[int] = None
wrap_chosen: Optional[Dict[str, Any]] = None
wrap_index: Optional[int] = None
for item in candidates:
current_index = _tab_persistent_index(item)
if wrap_index is None or current_index < wrap_index:
wrap_chosen = item
wrap_index = current_index
if current_index > last_index and (chosen_index is None or current_index < chosen_index):
chosen = item
chosen_index = current_index
if chosen is None:
chosen = wrap_chosen
chosen_index = wrap_index
if chosen is None or chosen_index is None:
raise HTTPException(status_code=404, detail="没有可用标签页")
_route_round_robin_cursor[cursor_key] = int(chosen.get("persistent_index") or 0)
return chosen
def _tab_persistent_index(item: Dict[str, Any]) -> int:
return int(item.get("persistent_index") or 0)
def _resolve_target_tab(
browser,
*,
route_domain: str = "",
exact_url: str = "",
url_token: str = "",
model_name: str = "",
tab_index: Optional[int] = None,
selector: str = "first_idle",
) -> Dict[str, Any]:
target_route = normalize_route_domain(route_domain)
target_exact_url = normalize_exact_tab_url(exact_url)
target_url_token = str(url_token or "").strip().lower()
target_model_name = _normalize_model_name_key(model_name)
if tab_index is not None:
tab_info = _get_tab_info_by_index(browser, int(tab_index))
if tab_info is None:
raise HTTPException(status_code=404, detail=f"标签页 #{tab_index} 不存在")
actual_domain = str(tab_info.get("current_domain") or tab_info.get("route_domain") or "").strip()
if target_route and not route_domain_matches(target_route, actual_domain):
raise HTTPException(
status_code=400,
detail=f"标签页 #{tab_index} 不属于域名路由 '{target_route}'",
)
actual_url = str(tab_info.get("url") or "").strip()
if target_exact_url and not tab_url_matches(target_exact_url, actual_url):
raise HTTPException(
status_code=400,
detail="指定标签页与 URL 路由不匹配",
)
actual_url_token = str(tab_info.get("url_route_token") or "").strip().lower()
if target_url_token and (
actual_url_token != target_url_token
and encode_tab_url_route_token(actual_url) != target_url_token
):
raise HTTPException(
status_code=400,
detail="指定标签页与 URL 路由不匹配",
)
if target_model_name:
actual_model_name = _normalize_model_name_key(tab_info.get("exposed_model_name"))
if actual_model_name != target_model_name:
raise HTTPException(
status_code=400,
detail="指定标签页与模型显示名称不匹配",
)
return tab_info
# exact_url / url_token 两条分支的解析结果会作为 resolved_tab_index 硬绑定传给
# execute_workflow_for_exact_url(下游只认这一个编号),所以必须和 model / domain
# 分支一样优先选空闲标签页;否则同 URL 开了多个标签页时,轮询游标可能挑中正忙的
# 那个,请求白等 60 秒超时,而旁边空闲的标签页全程没人用。
if target_exact_url:
matches = _get_tabs_by_exact_url(browser, target_exact_url)
if not matches:
raise HTTPException(status_code=404, detail="URL 路由没有匹配的已打开标签页")
idle_matches = [
item for item in matches
if str(item.get("status") or "").strip().lower() == "idle"
]
return _select_round_robin_tab(idle_matches or matches, f"exact_url::{target_exact_url}")
if target_url_token:
matches = _get_tabs_by_url_route_token(browser, target_url_token)
if not matches:
raise HTTPException(status_code=404, detail="URL 路由没有匹配的已打开标签页")
idle_matches = [
item for item in matches
if str(item.get("status") or "").strip().lower() == "idle"
]
return _select_round_robin_tab(idle_matches or matches, f"url_token::{target_url_token}")
if target_model_name:
matches = _get_tabs_by_exposed_model_name(browser, target_model_name)
if not matches:
raise HTTPException(status_code=404, detail="模型显示名称没有匹配的已打开标签页")
idle_matches = [
item for item in matches
if str(item.get("status") or "").strip().lower() == "idle"
]
return _select_round_robin_tab(idle_matches or matches, f"model::{target_model_name}")
candidates = _list_candidate_tabs(browser, target_route)
if not candidates:
if target_route:
raise HTTPException(status_code=404, detail=f"域名路由 '{target_route}' 没有匹配的标签页")
raise HTTPException(status_code=404, detail="没有匹配的标签页")
idle_candidates = [
item for item in candidates
if str(item.get("status") or "").strip().lower() == "idle"
]
pool = idle_candidates or candidates
selector = _normalize_tab_selector(selector)
if selector == "random":
return random.choice(pool)
if selector == "round_robin":
cursor_key = target_route or "__all__"
return _select_round_robin_tab(pool, cursor_key)
return min(pool, key=_tab_persistent_index)
def _build_tab_resolution_headers(
tab_info: Optional[Dict[str, Any]],
*,
route_domain: str = "",
exact_url: str = "",
model_name: str = "",
selector: str = "",
preset_name: str = "",
route_group: str = "",
route_group_live_member_count: Optional[int] = None,
route_group_idle_member_count: Optional[int] = None,
route_group_busy_member_count: Optional[int] = None,
) -> Dict[str, str]:
headers: Dict[str, str] = {}
requested_route_domain = str(route_domain or "").strip()
requested_exact_url = str(exact_url or "").strip()
requested_model_name = _normalize_model_name(model_name)
requested_route_group = normalize_route_group_id(route_group)
if requested_route_group:
headers["X-Requested-Route-Group"] = requested_route_group
headers["X-Resolved-Route-Group"] = requested_route_group
if route_group_live_member_count is not None:
headers["X-Route-Group-Live-Member-Count"] = str(
max(int(route_group_live_member_count), 0)
)
if route_group_idle_member_count is not None:
headers["X-Route-Group-Idle-Member-Count"] = str(
max(int(route_group_idle_member_count), 0)
)
if route_group_busy_member_count is not None:
headers["X-Route-Group-Busy-Member-Count"] = str(
max(int(route_group_busy_member_count), 0)
)
if requested_route_domain:
headers["X-Resolved-Route-Domain"] = (
normalize_route_domain(requested_route_domain) or requested_route_domain
)
if requested_route_domain:
headers["X-Requested-Route-Domain"] = (
normalize_route_domain(requested_route_domain) or requested_route_domain
)
if requested_exact_url:
headers["X-Requested-Exact-Url"] = normalize_exact_tab_url(requested_exact_url) or requested_exact_url
if requested_model_name:
headers["X-Requested-Model-Name"] = requested_model_name
if selector:
headers["X-Tab-Selection-Mode"] = selector
if preset_name:
headers["X-Resolved-Preset-Name"] = preset_name
if not tab_info:
return _encode_response_headers(headers)
tab_index = int(tab_info.get("persistent_index") or 0)
if tab_index > 0:
headers["X-Resolved-Tab-Index"] = str(tab_index)
tab_id = str(tab_info.get("id") or "").strip()
if tab_id:
headers["X-Resolved-Tab-Id"] = tab_id
current_url = str(tab_info.get("url") or "").strip()
if current_url:
headers["X-Resolved-Tab-Url"] = current_url
if exact_url:
headers["X-Resolved-Exact-Url"] = normalize_exact_tab_url(exact_url) or exact_url
current_domain = str(tab_info.get("current_domain") or tab_info.get("route_domain") or route_domain or "").strip()
if current_domain:
headers["X-Resolved-Route-Domain"] = current_domain
exposed_model_name = _normalize_model_name(tab_info.get("exposed_model_name"))
if exposed_model_name:
headers["X-Resolved-Model-Name"] = exposed_model_name
return _encode_response_headers(headers)
def _resolve_route_group(browser: Any, group_id: str) -> Dict[str, Any]:
normalized_id = normalize_route_group_id(group_id)
if not normalized_id:
raise HTTPException(status_code=400, detail="标签页路由组 ID 无效")
groups = browser.tab_pool.get_route_groups_snapshot()
group = route_groups_by_id(groups).get(normalized_id)
if not group:
raise HTTPException(status_code=404, detail=f"标签页路由组 '{normalized_id}' 不存在")
runtime_group = next((
item
for item in groups
if isinstance(item, dict)
and normalize_route_group_id(item.get("id") or item.get("group_id"))
== normalized_id
), None)
if runtime_group:
for key in (
"live_member_count",
"idle_member_count",
"busy_member_count",
"live_tab_indices",
):
if key in runtime_group:
group[key] = runtime_group[key]
return group
def _build_stream_headers(extra: Optional[Dict[str, str]] = None) -> Dict[str, str]:
headers = {
"Cache-Control": "no-cache, no-transform",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
}
if extra:
headers.update(_encode_response_headers(extra))
return headers
def _get_tab_config_domain(tab_info: Dict[str, Any]) -> str:
domain = str(tab_info.get("current_domain") or tab_info.get("route_domain") or "").strip()
if domain:
return domain
url = str(tab_info.get("url") or "").strip()
try:
return extract_remote_site_domain(url) or ""
except Exception:
return ""
def _resolve_strict_tab_preset(tab_info: Dict[str, Any], preset_name: str) -> Dict[str, str]:
requested = str(preset_name or "").strip()
if not requested:
raise HTTPException(status_code=400, detail="预设名称不能为空")
raw_domain = _get_tab_config_domain(tab_info)
if not raw_domain:
raise HTTPException(status_code=400, detail="URL 路由已匹配标签页,但无法解析站点域名")
# 标签页记的是页面真实主机名(可能是 www. 前缀或 site_rules 里的别名),
# 而预设是按配置里的站点域名存的。这里和 _resolve_strict_domain_preset、
# /api/tab-pool/tabs 的预设回显保持一致,补上 canonical 域名候选,
# 否则面板上显示"有这个预设"、走 /tab/{i}?preset_name=... 却 404。
candidate_domains = [
item for item in (
normalize_route_domain(raw_domain) or raw_domain,
get_canonical_route_domain(raw_domain) or "",
) if item
]
candidate_domains = list(dict.fromkeys(candidate_domains)) or [raw_domain]
try:
from app.services.config_engine import config_engine
domain = candidate_domains[0]
preset_map: Dict[str, bool] = {}
resolved = requested
for candidate_domain in candidate_domains:
candidate_presets = config_engine.list_presets(candidate_domain)
candidate_preset_map = {str(name): True for name in candidate_presets}
candidate_resolved = config_engine._resolve_preset_alias_key(requested, candidate_preset_map)
if candidate_preset_map and candidate_resolved in candidate_preset_map:
domain = candidate_domain
preset_map = candidate_preset_map
resolved = candidate_resolved
break
if not preset_map:
domain = candidate_domain
preset_map = candidate_preset_map
resolved = candidate_resolved
except HTTPException:
raise
except Exception as e:
logger.error(f"校验 URL 绑定预设失败: {e}")
raise HTTPException(status_code=500, detail=f"校验预设失败: {e}")
if not preset_map:
raise HTTPException(status_code=404, detail=f"URL 路由对应站点 '{domain}' 没有可用预设")
if resolved not in preset_map:
raise HTTPException(status_code=404, detail=f"URL 路由对应站点 '{domain}' 找不到预设: {requested}")
return {
"domain": domain,
"preset_name": resolved,
}
def _resolve_strict_domain_preset(route_domain: str, preset_name: str) -> Dict[str, str]:
requested = str(preset_name or "").strip()
if not requested:
raise HTTPException(status_code=400, detail="预设名称不能为空")
raw_domain = normalize_route_domain(route_domain) or str(route_domain or "").strip()
if not raw_domain:
raise HTTPException(status_code=400, detail="域名路由不能为空")
canonical_domain = get_canonical_route_domain(raw_domain) or ""
candidate_domains = [
item for item in (raw_domain, canonical_domain)
if item
]
candidate_domains = list(dict.fromkeys(candidate_domains))
try:
from app.services.config_engine import config_engine
domain = candidate_domains[0]
preset_map: Dict[str, bool] = {}
resolved = requested
for candidate_domain in candidate_domains:
candidate_presets = config_engine.list_presets(candidate_domain)
candidate_preset_map = {str(name): True for name in candidate_presets}
candidate_resolved = config_engine._resolve_preset_alias_key(requested, candidate_preset_map)
if candidate_preset_map and candidate_resolved in candidate_preset_map:
domain = candidate_domain
preset_map = candidate_preset_map
resolved = candidate_resolved
break
if not preset_map:
domain = candidate_domain
preset_map = candidate_preset_map
resolved = candidate_resolved
except HTTPException:
raise
except Exception as e:
logger.error(f"校验域名路由预设失败: {e}")
raise HTTPException(status_code=500, detail=f"校验预设失败: {e}")
if not preset_map:
raise HTTPException(status_code=404, detail=f"域名路由对应站点 '{domain}' 没有可用预设")
if resolved not in preset_map:
raise HTTPException(status_code=404, detail=f"域名路由对应站点 '{domain}' 找不到预设: {requested}")
return {
"domain": domain,
"preset_name": resolved,
}
# ================= 请求模型 =================
class ChatRequest(BaseModel):
"""聊天请求模型"""
workflow_variables: Dict[str, Any] = Field(default_factory=dict)
@field_validator('workflow_variables')
@classmethod
def validate_workflow_variables(cls, value):
from app.core.workflow.flow_runtime import validate_inputs, FlowValidationError
try:
return validate_inputs(value)
except FlowValidationError as exc:
raise ValueError(str(exc)) from exc
model: str = Field(default="未知")
messages: list = Field(...)
stream: Optional[bool] = Field(default=False)
temperature: Optional[float] = Field(default=0.7, ge=0, le=2)
max_tokens: Optional[int] = Field(default=None, ge=1)
n: Optional[int] = Field(default=1, ge=1)
response_format: Optional[dict] = Field(default=None)
stop: Optional[Any] = Field(default=None)
tools: Optional[list] = Field(default=None)
tool_choice: Optional[Any] = Field(default=None)
parallel_tool_calls: Optional[bool] = Field(default=None)
functions: Optional[list] = Field(default=None)
function_call: Optional[Any] = Field(default=None)
preset_name: Optional[str] = Field(default=None)
stream_options: Optional[dict] = Field(default=None)
def _is_single_choice_request(body: ChatRequest) -> bool:
try:
return int(getattr(body, "n", 1) or 1) <= 1
except (TypeError, ValueError):
return True
def _apply_response_format_if_needed(body: ChatRequest) -> ChatRequest:
if bool(getattr(body, "_response_format_applied", False)):
return body
response_format = body.response_format
if not isinstance(response_format, dict):
return body
format_type = str(response_format.get("type") or "text").strip().lower() or "text"
if format_type == "text":
return body
try:
from app.api.chat import _apply_response_format
updated = body.model_copy(
update={
"messages": _apply_response_format(body.messages, response_format),
}
)
setattr(updated, "_response_format_applied", True)
return updated
except Exception as e:
logger.debug(f"response_format 转化失败(已忽略): {e}")
return body
def _maybe_pack_stream_usage_chunk(body: ChatRequest) -> Optional[str]:
try:
from app.api.chat import _maybe_pack_stream_usage_chunk as _pack_usage
return _pack_usage(body)
except Exception as e:
logger.debug(f"stream_options.include_usage 转化失败(已忽略): {e}")
return None
def _split_sse_done_frame(chunk: Any) -> tuple[str, bool]:
try:
from app.api.chat import _split_sse_done_frame as _split_done
return _split_done(chunk)
except Exception:
return str(chunk or ""), False
def _iter_stream_chunks_with_optional_usage(body: ChatRequest, chunks):
try:
from app.api.chat import _iter_stream_chunks_with_optional_usage as _iter_chunks
except Exception as e:
logger.debug(f"stream_options.include_usage 流式分块处理失败(已回退): {e}")
yield from chunks
return
yield from _iter_chunks(body, chunks)
class TabPoolConfigRequest(BaseModel):
"""标签页池配置更新请求。"""
# allocation_mode / enabled_route_methods 用 None 表示"本次请求没带这个字段",
# 与 excluded_urls / preserve_error_tabs / route_groups 的部分更新语义保持一致。
# 此前 allocation_mode 有默认值 "first_idle"、enabled_route_methods 为 None 时
# 被归一成"全部启用",于是任何只带部分字段的 PUT(例如只保存路由组)都会把
# 用户设好的分配模式和路由方式开关静默重置掉。
allocation_mode: Optional[str] = Field(default=None)
enabled_route_methods: Optional[List[str]] = Field(default=None)
excluded_urls: Optional[List[str]] = Field(default=None)
preserve_error_tabs: Optional[bool] = Field(default=None)
auto_remember_url_presets: Optional[bool] = Field(default=None)
route_groups: Optional[List[Dict[str, Any]]] = Field(default=None)
class TabModelNameRequest(BaseModel):
"""标签页暴露模型名更新请求。"""
model_name: Optional[str] = Field(default=None, max_length=200)
persist_scope: Optional[str] = Field(default=None)
reset: Optional[bool] = Field(default=False)
# ================= 标签页池 API =================
@router.get("/api/tab-pool/tabs")
async def get_tab_pool_tabs(authenticated: bool = Depends(verify_auth)):
"""
获取所有标签页及其持久编号和预设信息
返回格式:
{
"tabs": [
{
"persistent_index": 1,
"id": "gpt_1",
"url": "https://chatgpt.com/",
"status": "idle",
"route_prefix": "/url/chatgpt.com",
"tab_route_prefix": "/tab/1",
"domain_route_prefix": "/url/chatgpt.com",
"preset_name": null,
"available_presets": ["主预设", "无临时聊天"]
},
...
],
"count": 3
}
"""
try:
browser = get_browser(auto_connect=False)
tabs = browser.tab_pool.get_tabs_with_index()
allocation_mode = _get_tab_pool_allocation_mode(browser.tab_pool)
browser_config = _read_browser_config()
enabled_route_methods = _get_enabled_route_methods_from_config(browser_config)
excluded_urls = _get_tab_pool_excluded_urls(browser.tab_pool, browser_config)
preserve_error_tabs = _get_tab_pool_preserve_error_tabs(browser.tab_pool, browser_config)
auto_remember_url_presets = _get_tab_pool_auto_remember_url_presets(browser.tab_pool, browser_config)
route_groups = _get_tab_pool_route_groups(browser.tab_pool, browser_config)
for tab_info in tabs:
exclusion_url = _get_tab_item_exclusion_url(tab_info, excluded_urls)
tab_info["route_excluded"] = bool(exclusion_url)
tab_info["route_exclusion_url"] = exclusion_url
# 🆕 为每个标签页附加可用预设列表
try:
from app.services.config_engine import config_engine
_attach_preset_info_to_tabs(tabs, config_engine, auto_remember_url_presets)
except Exception as e:
logger.debug(f"获取预设列表失败: {e}")
for tab_info in tabs:
tab_info["available_presets"] = []
tab_info["default_preset"] = None
tab_info["effective_preset_name"] = tab_info.get("preset_name")
tab_info["is_using_default_preset"] = not bool(tab_info.get("preset_name"))
return {
"tabs": tabs,
"count": len(tabs),
"allocation_mode": allocation_mode,
"allocation_mode_options": TAB_POOL_ALLOCATION_OPTIONS,
"enabled_route_methods": enabled_route_methods,
"route_method_options": TAB_ROUTE_METHOD_OPTIONS,
"excluded_urls": excluded_urls,
"preserve_error_tabs": preserve_error_tabs,
"auto_remember_url_presets": auto_remember_url_presets,
"route_groups": route_groups,
}
except Exception as e:
logger.error(f"获取标签页列表失败: {e}")
# 修复:不再伪造 allocation_mode / preserve_error_tabs / route_groups 等持久化配置,
# 否则前端会把这些默认值当作真实配置展示并在下次保存时写回磁盘,抹掉用户设置。
return {
"tabs": [],
"count": 0,
"error": str(e),
"allocation_mode_options": TAB_POOL_ALLOCATION_OPTIONS,
"route_method_options": TAB_ROUTE_METHOD_OPTIONS,
}
@router.put("/api/tab-pool/tabs/{tab_index}/model-name")
async def update_tab_model_name(
tab_index: int,
body: TabModelNameRequest,
authenticated: bool = Depends(verify_auth),
):
"""更新标签页暴露给前端模型列表的名称。"""
if tab_index < 1:
raise HTTPException(status_code=400, detail="标签页编号必须大于 0")
model_name = _normalize_model_name(body.model_name)
persist_scope = str(body.persist_scope or "tab").strip().lower()
reset = bool(body.reset)
if persist_scope in {"", "temporary"}:
persist_scope = "tab"
if persist_scope not in {"tab", "site", "url"}:
raise HTTPException(status_code=400, detail="invalid_persist_scope")
if not reset and not model_name:
raise HTTPException(status_code=400, detail="模型显示名称不能为空")
browser = get_browser(auto_connect=False)
tab_info = _get_tab_info_by_index(browser, tab_index)
if tab_info is None:
raise HTTPException(status_code=404, detail=f"标签页 #{tab_index} 不存在")
route_key = normalize_route_domain(
tab_info.get("route_domain") or tab_info.get("current_domain") or ""
)
url_key = normalize_exact_tab_url(str(tab_info.get("url") or "").strip())
pool_synced = False
saved_scope = ""
removed_scopes: List[str] = []
if reset:
try:
browser.tab_pool.set_tab_model_name(tab_index, None)
except Exception as e:
logger.warning(f"清空标签页临时模型显示名称失败: {e}")
with _model_name_overrides_lock:
overrides = _read_model_name_overrides_unlocked()
if route_key and route_key in overrides["sites"]:
overrides["sites"].pop(route_key, None)
removed_scopes.append("site")
if url_key and url_key in overrides["urls"]:
overrides["urls"].pop(url_key, None)
removed_scopes.append("url")
overrides = _write_model_name_overrides_unlocked(overrides)
pool_synced = _sync_tab_pool_model_name_overrides(overrides)
elif persist_scope == "tab":
result = browser.tab_pool.set_tab_model_name(tab_index, model_name)
if not result.get("ok"):
raise HTTPException(status_code=404, detail=result.get("error") or "tab_not_found")
else:
if persist_scope == "site" and not route_key:
raise HTTPException(status_code=400, detail="当前标签页无法解析站点域名")
if persist_scope == "url" and not url_key:
raise HTTPException(status_code=400, detail="当前标签页无法解析网页 URL")
with _model_name_overrides_lock:
overrides = _read_model_name_overrides_unlocked()
if persist_scope == "site":
overrides["sites"][route_key] = model_name
else:
overrides["urls"][url_key] = model_name
overrides = _write_model_name_overrides_unlocked(overrides)
pool_synced = _sync_tab_pool_model_name_overrides(overrides)
browser.tab_pool.set_tab_model_name(tab_index, None)
saved_scope = persist_scope
refreshed_tab = _get_tab_info_by_index(browser, tab_index)
return {
"success": True,
"tab_index": tab_index,
"tab": refreshed_tab,
"saved_scope": saved_scope,
"removed_scopes": removed_scopes,
"pool_synced": pool_synced,
}
@router.put("/api/tab-pool/config")
async def update_tab_pool_config(
body: TabPoolConfigRequest,
authenticated: bool = Depends(verify_auth)
):
"""更新标签页池运行模式并持久化到 browser_config.json。"""
request_includes_allocation_mode = body.allocation_mode is not None
allocation_mode = str(body.allocation_mode or "").strip().lower()
if request_includes_allocation_mode and allocation_mode not in {"first_idle", "round_robin", "random"}:
raise HTTPException(status_code=400, detail="invalid_allocation_mode")
request_includes_enabled_route_methods = body.enabled_route_methods is not None
enabled_route_methods = _normalize_enabled_route_methods(body.enabled_route_methods)
request_includes_excluded_urls = body.excluded_urls is not None
excluded_urls = _normalize_excluded_urls(body.excluded_urls)
request_includes_preserve_error_tabs = body.preserve_error_tabs is not None
preserve_error_tabs = _coerce_bool(body.preserve_error_tabs, False)
request_includes_auto_remember_url_presets = body.auto_remember_url_presets is not None
auto_remember_url_presets = _coerce_bool(body.auto_remember_url_presets, False)
request_includes_route_groups = body.route_groups is not None
route_groups = normalize_route_groups(body.route_groups)
try:
with _browser_config_lock:
config = _read_browser_config()
tab_pool_config = config.get("tab_pool") or {}
if not isinstance(tab_pool_config, dict):
tab_pool_config = {}
if request_includes_allocation_mode:
tab_pool_config["allocation_mode"] = allocation_mode
if request_includes_enabled_route_methods:
tab_pool_config["enabled_route_methods"] = enabled_route_methods
if request_includes_excluded_urls:
tab_pool_config["excluded_urls"] = excluded_urls
if request_includes_preserve_error_tabs:
tab_pool_config["preserve_error_tabs"] = preserve_error_tabs
if request_includes_auto_remember_url_presets:
tab_pool_config["auto_remember_url_presets"] = auto_remember_url_presets
if request_includes_route_groups:
tab_pool_config["route_groups"] = route_groups
current_excluded_urls = _normalize_excluded_urls(tab_pool_config.get("excluded_urls"))
current_preserve_error_tabs = _coerce_bool(tab_pool_config.get("preserve_error_tabs"), False)
current_auto_remember_url_presets = _coerce_bool(tab_pool_config.get("auto_remember_url_presets"), False)
current_route_groups = normalize_route_groups(tab_pool_config.get("route_groups"))
current_allocation_mode = _get_config_allocation_mode(tab_pool_config)
current_enabled_route_methods = _normalize_enabled_route_methods(
tab_pool_config.get("enabled_route_methods")
)
config["tab_pool"] = tab_pool_config
_write_browser_config_unlocked(config)
try:
from app.core.config import BrowserConstants
if hasattr(BrowserConstants, "reload"):
BrowserConstants.reload()
except Exception as reload_error:
logger.warning(f"热重载浏览器常量失败: {reload_error}")
pool_synced = False
try:
browser = get_browser(auto_connect=False)
runtime_kwargs: Dict[str, Any] = {
"allocation_mode": current_allocation_mode,
"preserve_error_tabs": current_preserve_error_tabs,
"auto_remember_url_presets": current_auto_remember_url_presets,
}
if request_includes_excluded_urls:
runtime_kwargs["excluded_urls"] = excluded_urls
if request_includes_route_groups:
runtime_kwargs["route_groups"] = route_groups
browser.tab_pool.apply_runtime_config(**runtime_kwargs)
pool_synced = True
except Exception as sync_error:
logger.warning(f"同步运行中标签页池配置失败: {sync_error}")
return {
"success": True,
"message": "标签页池分配模式已更新",
"allocation_mode": current_allocation_mode,
"allocation_mode_options": TAB_POOL_ALLOCATION_OPTIONS,
"enabled_route_methods": current_enabled_route_methods,
"route_method_options": TAB_ROUTE_METHOD_OPTIONS,
"excluded_urls": current_excluded_urls,
"preserve_error_tabs": current_preserve_error_tabs,
"auto_remember_url_presets": current_auto_remember_url_presets,
"route_groups": current_route_groups,
"pool_synced": pool_synced,
}
except HTTPException:
raise
except Exception as e:
logger.error(f"更新标签页池配置失败: {e}")
raise HTTPException(status_code=500, detail=str(e))
# ================= 指定标签页的聊天 API =================
@router.get("/tab/{tab_index}/v1/models")
async def list_models_with_tab(
tab_index: int,
anthropic_version: Optional[str] = Header(None, alias="anthropic-version"),
authenticated: bool = Depends(verify_service_auth),
_route_method: None = Depends(_route_method_guard("fixed_tab")),
):
"""为指定标签页路由提供 OpenAI 兼容模型列表接口。"""
if tab_index < 1:
raise HTTPException(status_code=400, detail="标签页编号必须大于 0")
browser = get_browser(auto_connect=False)
tab_info = _get_tab_info_by_index(browser, tab_index)
if tab_info is None:
raise HTTPException(status_code=404, detail=f"标签页 #{tab_index} 不存在")
entries: List[Dict[str, Any]] = []
try:
from app.services.config_engine import config_engine
catalog_preset = get_arena_direct_catalog_for_tab(config_engine, tab_info)
if catalog_preset:
entries = build_openai_model_entries(
list_arena_direct_models(
browser,
catalog_config=catalog_preset.get("catalog"),
),
created=MODEL_LIST_CREATED,
)
except Exception as e:
logger.debug(f"读取标签页模型目录失败(已忽略): {e}")
if bool(tab_info.get("model_name_override_source")):
exposed_name = str(tab_info.get("exposed_model_name") or tab_info.get("model_name_override") or "").strip()
seen_ids = {str(e.get("id") or "").strip().lower() for e in entries}
if exposed_name and exposed_name.lower() not in seen_ids:
entries.append({
"id": exposed_name,
"object": "model",
"type": "model",
"created": MODEL_LIST_CREATED,
"owned_by": "universal-web-api",
"display_name": exposed_name,
})
if not entries:
entries = [{
"id": "web-browser",
"object": "model",
"type": "model",
"created": MODEL_LIST_CREATED,
"owned_by": "universal-web-api",
"display_name": "web-browser",
}]
payload = _build_route_model_entries_payload(
entries,
anthropic_version=anthropic_version,
)
response = JSONResponse(content=payload)
response.headers.update(_build_tab_resolution_headers(
tab_info,
selector="fixed",
))
return response
@router.get("/url/{route_domain}/models")
@router.get("/url/{route_domain}/v1/v1/models")
@router.get("/url/{route_domain}/v1/models")
async def list_models_with_route_domain(
route_domain: str,
tab_index: Optional[int] = Query(default=None, ge=1),
selector: Optional[str] = Query(default=None),
anthropic_version: Optional[str] = Header(None, alias="anthropic-version"),
authenticated: bool = Depends(verify_service_auth),
_route_method: None = Depends(_route_method_guard("domain")),
):
"""为域名路由提供 OpenAI 兼容模型列表接口。"""
route_key = str(route_domain or "").strip()
# 归一化后为空也要拒绝(例如 "..." / "."):否则 target_route 一路是空串,
# _list_candidate_tabs 退化成"任意标签页",tab_index 的归属校验也会失效。
if not route_key or not normalize_route_domain(route_key):
raise HTTPException(status_code=400, detail="域名路由不能为空")
browser = get_browser(auto_connect=False)
normalized_selector = _normalize_tab_selector(
selector,
default=_get_pool_default_selector(browser),
)
tab_info = _resolve_target_tab(
browser,
route_domain=route_key,
tab_index=tab_index,
selector=normalized_selector,
)
if route_domain_matches(route_key, "arena.ai"):
from app.services.config_engine import config_engine
catalog_preset = get_arena_direct_catalog_for_tab(config_engine, tab_info)
arena_entries = build_openai_model_entries(
list_arena_direct_models(
browser,
catalog_config=(catalog_preset or {}).get("catalog"),
) if catalog_preset else [],
created=MODEL_LIST_CREATED,
)
if arena_entries:
payload = _build_route_model_entries_payload(
arena_entries,
anthropic_version=anthropic_version,
)
response = JSONResponse(content=payload)
response.headers.update(
_build_tab_resolution_headers(
tab_info,
route_domain=route_key,
selector=normalized_selector,
)
)
return response
payload = _build_route_models_payload(
model_id=_build_claude_route_model_id(route_key),
display_name=f"Claude Code route: {route_key}",
anthropic_version=anthropic_version,
)
response = JSONResponse(content=payload)
response.headers.update(
_build_tab_resolution_headers(
tab_info,
route_domain=route_key,
selector=("tab_index" if tab_index is not None else normalized_selector),
)
)
return response
@router.get("/url/{route_domain}/{preset_name}/models")
@router.get("/url/{route_domain}/{preset_name}/v1/v1/models")
@router.get("/url/{route_domain}/{preset_name}/v1/models")
async def list_models_with_route_domain_and_preset(
route_domain: str,
preset_name: str,
tab_index: Optional[int] = Query(default=None, ge=1),
selector: Optional[str] = Query(default=None),
anthropic_version: Optional[str] = Header(None, alias="anthropic-version"),
authenticated: bool = Depends(verify_service_auth),
_route_method: None = Depends(_route_method_guard("domain")),
):
"""为域名+预设路径风格提供 OpenAI 兼容模型列表接口。"""
route_key = str(route_domain or "").strip()
# 归一化后为空也要拒绝(例如 "..." / "."):否则 target_route 一路是空串,
# _list_candidate_tabs 退化成"任意标签页",tab_index 的归属校验也会失效。
if not route_key or not normalize_route_domain(route_key):
raise HTTPException(status_code=400, detail="域名路由不能为空")
preset_resolution = _resolve_strict_domain_preset(route_key, preset_name)
browser = get_browser(auto_connect=False)
normalized_selector = _normalize_tab_selector(
selector,
default=_get_pool_default_selector(browser),
)
tab_info = _resolve_target_tab(
browser,
route_domain=route_key,
tab_index=tab_index,
selector=normalized_selector,
)
from app.services.config_engine import config_engine
catalog_preset = get_arena_direct_catalog_for_tab(
config_engine,
tab_info,
preset_name=preset_resolution["preset_name"],
)
if catalog_preset:
payload = _build_route_model_entries_payload(
build_openai_model_entries(
list_arena_direct_models(
browser,
catalog_config=catalog_preset["catalog"],
),
created=MODEL_LIST_CREATED,
),
anthropic_version=anthropic_version,
)
else:
payload = _build_route_models_payload(
model_id=_build_claude_route_model_id(f"{route_key}-{preset_resolution['preset_name']}"),
display_name=f"Claude Code route: {route_key} / {preset_resolution['preset_name']}",
anthropic_version=anthropic_version,
)
response = JSONResponse(content=payload)
response.headers.update(
_build_tab_resolution_headers(
tab_info,
route_domain=route_key,
selector=("tab_index" if tab_index is not None else normalized_selector),
preset_name=preset_resolution["preset_name"],
)
)
return response
@router.get("/tab-url/{url_token}/v1/models")
async def list_models_with_exact_tab_url(
url_token: str,
authenticated: bool = Depends(verify_service_auth),
_route_method: None = Depends(_route_method_guard("exact_url")),
):
"""为精确 URL 路由提供 OpenAI 兼容模型列表接口。"""
route_token = str(url_token or "").strip().lower()
if not route_token:
raise HTTPException(status_code=400, detail="URL 路由无效")
browser = get_browser(auto_connect=False)
tab_info = _resolve_target_tab(
browser,
url_token=route_token,
selector="round_robin",
)
payload = {
"object": "list",
"data": [
{
"id": "web-browser",
"object": "model",
"created": MODEL_LIST_CREATED,
"owned_by": "universal-web-api"
}
]
}
response = JSONResponse(content=payload)
response.headers.update(
_build_tab_resolution_headers(
tab_info,
exact_url=str(tab_info.get("url") or ""),
selector="exact_url",
)
)
return response
@router.get("/tab-url/{url_token}/{preset_name}/v1/models")
async def list_models_with_exact_tab_url_and_preset(
url_token: str,
preset_name: str,
authenticated: bool = Depends(verify_service_auth),
_route_method: None = Depends(_route_method_guard("exact_url_preset")),
):
"""为 URL 绑定预设路由提供 OpenAI 兼容模型列表接口。"""
route_token = str(url_token or "").strip().lower()
if not route_token:
raise HTTPException(status_code=400, detail="URL 路由无效")
browser = get_browser(auto_connect=False)
tab_info = _resolve_target_tab(
browser,
url_token=route_token,
selector="round_robin",
)
preset_resolution = _resolve_strict_tab_preset(tab_info, preset_name)
payload = {
"object": "list",
"data": [
{
"id": "web-browser",
"object": "model",
"created": MODEL_LIST_CREATED,
"owned_by": "universal-web-api"
}
]
}
response = JSONResponse(content=payload)
response.headers.update(
_build_tab_resolution_headers(
tab_info,
exact_url=str(tab_info.get("url") or ""),
selector="exact_url_preset",
preset_name=preset_resolution["preset_name"],
)
)
return response
def _collect_models_for_route_group(
browser: Any,
group: Dict[str, Any],
preset_name: Optional[str] = None,
anthropic_version: Optional[str] = None,
) -> Dict[str, Any]:
"""为路由组收集模型条目:
1. 优先提取对应预设中的 Model Catalog 模型(如 Arena Direct 模型列表);
2. 提取组内成员标签页的自定义 exposed_model_name;
3. 若均无具体模型,回退返回 group ID 聚合名称。
"""
resolved_preset = str(preset_name or group.get("preset_name") or "").strip() or None
route_domain = str(group.get("route_domain") or "").strip()
group_tabs: List[Dict[str, Any]] = []
try:
tabs_snapshot = browser.tab_pool.get_tabs_with_index()
raw_members = group.get("members") or []
group_member_keys = {route_group_member_key(m) for m in raw_members if m}
for t in tabs_snapshot:
t_url = normalize_exact_tab_url(str(t.get("url") or ""))
t_token = str(t.get("url_route_token") or "").strip().lower()
t_idx = int(t.get("persistent_index") or 0)
if (
f"{t_token}::#{t_idx}" in group_member_keys
or f"{t_token}::#*" in group_member_keys
or f"{t_url}::#{t_idx}" in group_member_keys
or f"{t_url}::#*" in group_member_keys
):
group_tabs.append(t)
if not group_tabs and route_domain:
group_tabs = [
t for t in tabs_snapshot
if route_domain_matches(route_domain, t.get("current_domain") or t.get("route_domain") or "")
]
except Exception as e:
logger.debug(f"读取路由组成员标签页失败(已忽略): {e}")
group_tabs = []
if not route_domain and group_tabs:
first_domain = str(group_tabs[0].get("current_domain") or group_tabs[0].get("route_domain") or "").strip()
if first_domain:
route_domain = first_domain
catalog_entries: List[Dict[str, Any]] = []
try:
from app.services.config_engine import config_engine
if route_domain and route_domain_matches(route_domain, "arena.ai"):
catalog_preset = None
for t in group_tabs:
catalog_preset = get_arena_direct_catalog_for_tab(
config_engine,
t,
preset_name=resolved_preset,
)
if catalog_preset:
break
if not catalog_preset:
catalog_preset = get_arena_direct_catalog_for_tab(
config_engine,
{
"status": "idle",
"url": f"https://{route_domain}/c",
"current_domain": route_domain,
"route_domain": route_domain,
},
preset_name=resolved_preset,
)
if catalog_preset:
catalog_entries = build_openai_model_entries(
list_arena_direct_models(
browser,
catalog_config=catalog_preset.get("catalog"),
),
created=MODEL_LIST_CREATED,
)
except Exception as e:
logger.debug(f"构建路由组模型目录失败(已忽略): {e}")
custom_entries: List[Dict[str, Any]] = []
seen_ids = {str(e.get("id") or "").strip().lower() for e in catalog_entries}
for t in group_tabs:
if bool(t.get("model_name_override_source")):
exp_name = str(t.get("exposed_model_name") or t.get("model_name_override") or "").strip()
if exp_name and exp_name.lower() not in seen_ids:
seen_ids.add(exp_name.lower())
custom_entries.append({
"id": exp_name,
"object": "model",
"type": "model",
"created": MODEL_LIST_CREATED,
"owned_by": "universal-web-api",
"display_name": exp_name,
})
all_entries = catalog_entries + custom_entries
if not all_entries:
all_entries = [{
"id": group["id"],
"object": "model",
"type": "model",
"created": MODEL_LIST_CREATED,
"owned_by": "universal-web-api",
"display_name": str(group.get("name") or group["id"]),
}]
return _build_route_model_entries_payload(
all_entries,
anthropic_version=anthropic_version,
)
@router.get("/group/{group_id}/v1/models")
async def list_models_with_route_group(
group_id: str,
anthropic_version: Optional[str] = Header(None, alias="anthropic-version"),
authenticated: bool = Depends(verify_service_auth),
_route_method: None = Depends(_route_method_guard("route_group")),
):
browser = get_browser(auto_connect=False)
group = _resolve_route_group(browser, group_id)
payload = _collect_models_for_route_group(
browser,
group,
preset_name=None,
anthropic_version=anthropic_version,
)
response = JSONResponse(content=payload)
response.headers.update(_build_tab_resolution_headers(
None,
route_domain=group.get("route_domain") or "",
selector=group.get("allocation_mode") or "round_robin",
route_group=group["id"],
route_group_live_member_count=group.get("live_member_count"),
route_group_idle_member_count=group.get("idle_member_count"),
route_group_busy_member_count=group.get("busy_member_count"),
))
return response
@router.get("/group/{group_id}/{preset_name}/v1/models")
async def list_models_with_route_group_and_preset(
group_id: str,
preset_name: str,
anthropic_version: Optional[str] = Header(None, alias="anthropic-version"),
authenticated: bool = Depends(verify_service_auth),
_route_method: None = Depends(_route_method_guard("route_group")),
):
browser = get_browser(auto_connect=False)
group = _resolve_route_group(browser, group_id)
route_domain = str(group.get("route_domain") or "").strip()
preset_resolution = (
_resolve_strict_domain_preset(route_domain, preset_name)
if route_domain
else {"domain": "", "preset_name": preset_name}
)
payload = _collect_models_for_route_group(
browser,
group,
preset_name=preset_resolution["preset_name"],
anthropic_version=anthropic_version,
)
response = JSONResponse(content=payload)
response.headers.update(_build_tab_resolution_headers(
None,
route_domain=route_domain,
selector=group.get("allocation_mode") or "round_robin",
preset_name=preset_resolution["preset_name"],
route_group=group["id"],
route_group_live_member_count=group.get("live_member_count"),
route_group_idle_member_count=group.get("idle_member_count"),
route_group_busy_member_count=group.get("busy_member_count"),
))
return response
async def _chat_with_resolved_tab(
request: Request,
body: ChatRequest,
ctx: RequestContext,
*,
tab_index: int,
resolved_headers: Optional[Dict[str, str]] = None,
):
body = _apply_response_format_if_needed(body)
headers = _encode_response_headers(resolved_headers)
if has_tool_calling_request(
messages=body.messages,
tools=body.tools,
functions=body.functions,
):
if body.stream:
return StreamingResponse(
_stream_tool_calling_with_tab_index(request, body, ctx, tab_index),
media_type="text/event-stream",
headers=_build_stream_headers(headers),
request_context=ctx,
)
response = await _non_stream_tool_calling_with_tab_index(request, body, ctx, tab_index)
response.headers.update(headers)
return response
if body.stream:
return StreamingResponse(
_stream_with_tab_index(request, body, ctx, tab_index),
media_type="text/event-stream",
headers=_build_stream_headers(headers),
request_context=ctx,
)
response = await _non_stream_with_tab_index(request, body, ctx, tab_index)
response.headers.update(headers)
return response
async def _chat_with_exact_url(
request: Request,
body: ChatRequest,
ctx: RequestContext,
*,
exact_url: str,
resolved_tab_index: Optional[int] = None,
resolved_headers: Optional[Dict[str, str]] = None,
):
body = _apply_response_format_if_needed(body)
headers = _encode_response_headers(resolved_headers)
if has_tool_calling_request(
messages=body.messages,
tools=body.tools,
functions=body.functions,
):
if body.stream:
return StreamingResponse(
_stream_tool_calling_with_exact_url(
request,
body,
ctx,
exact_url,
resolved_tab_index=resolved_tab_index,
),
media_type="text/event-stream",
headers=_build_stream_headers(headers),
request_context=ctx,
)
response = await _non_stream_tool_calling_with_exact_url(
request,
body,
ctx,
exact_url,
resolved_tab_index=resolved_tab_index,
)
response.headers.update(headers)
return response
if body.stream:
return StreamingResponse(
_stream_with_exact_url(
request,
body,
ctx,
exact_url,
resolved_tab_index=resolved_tab_index,
),
media_type="text/event-stream",
headers=_build_stream_headers(headers),
request_context=ctx,
)
response = await _non_stream_with_exact_url(
request,
body,
ctx,
exact_url,
resolved_tab_index=resolved_tab_index,
)
response.headers.update(headers)
return response
async def _chat_with_route_domain(
request: Request,
body: ChatRequest,
ctx: RequestContext,
*,
route_domain: str,
route_group_id: Optional[str] = None,
allocation_mode: Optional[str] = None,
resolved_headers: Optional[Dict[str, str]] = None,
):
body = _apply_response_format_if_needed(body)
headers = _encode_response_headers(resolved_headers)
if has_tool_calling_request(
messages=body.messages,
tools=body.tools,
functions=body.functions,
):
if body.stream:
return StreamingResponse(
_stream_tool_calling_with_route_domain(
request,
body,
ctx,
route_domain,
route_group_id=route_group_id,
allocation_mode=allocation_mode,
),
media_type="text/event-stream",
headers=_build_stream_headers(headers),
request_context=ctx,
)
response = await _non_stream_tool_calling_with_route_domain(
request,
body,
ctx,
route_domain,
route_group_id=route_group_id,
allocation_mode=allocation_mode,
)
response.headers.update(headers)
return response
if body.stream:
return StreamingResponse(
_stream_with_route_domain(
request,
body,
ctx,
route_domain,
route_group_id=route_group_id,
allocation_mode=allocation_mode,
),
media_type="text/event-stream",
headers=_build_stream_headers(headers),
request_context=ctx,
)
response = await _non_stream_with_route_domain(
request,
body,
ctx,
route_domain,
route_group_id=route_group_id,
allocation_mode=allocation_mode,
)
response.headers.update(headers)
return response
@router.post("/tab/{tab_index}/v1/chat/completions")
async def chat_with_tab(
tab_index: int,
request: Request,
body: ChatRequest,
preset_name: Optional[str] = Query(default=None),
authenticated: bool = Depends(verify_service_auth),
_route_method: None = Depends(_route_method_guard("fixed_tab")),
):
"""
使用指定编号的标签页进行聊天
路径参数:
- tab_index: 持久化标签页编号(1, 2, 3...)
"""
client_fp = cancel_storm_guard.get_client_fingerprint(request)
await cancel_storm_guard.maybe_backoff(client_fp)
if tab_index < 1:
raise HTTPException(status_code=400, detail="标签页编号必须大于 0")
browser = get_browser(auto_connect=False)
tab_info = _get_tab_info_by_index(browser, tab_index)
if tab_info is None:
raise HTTPException(status_code=404, detail=f"标签页 #{tab_index} 不存在")
requested_preset_name = str(preset_name or body.preset_name or "").strip()
resolved_preset_name = None
if requested_preset_name:
preset_resolution = _resolve_strict_tab_preset(
tab_info,
requested_preset_name,
)
resolved_preset_name = preset_resolution["preset_name"]
if resolved_preset_name != body.preset_name:
body = body.model_copy(update={"preset_name": resolved_preset_name})
ctx = request_manager.create_request(client_fp=client_fp)
try:
raw_input_len = sum(len(str(msg.get("content") or "")) for msg in body.messages if isinstance(msg, dict))
logger.info(f"[DIAG] 接收到的原始请求 messages 总字符长度: {raw_input_len} 字符, 消息数: {len(body.messages)}")
except Exception as e:
logger.debug(f"[DIAG] 估算原始请求长度失败: {e}")
request_manager.record_request_input(
ctx,
body.model_dump(),
endpoint=f"/tab/{tab_index}/v1/chat/completions",
route_domain=str((tab_info or {}).get("current_domain") or (tab_info or {}).get("route_domain") or ""),
tab_index=tab_index,
preset_name=resolved_preset_name,
)
with logger.context(ctx.request_id):
logger.info(f"开始 (标签页 #{tab_index}, preset={resolved_preset_name or '<follow-tab/default>'})")
resolved_headers = _build_tab_resolution_headers(
tab_info,
selector="fixed",
)
return await _chat_with_resolved_tab(
request,
body,
ctx,
tab_index=tab_index,
resolved_headers=resolved_headers,
)
@router.post("/url/{route_domain}/v1/chat/completions")
async def chat_with_route_domain(
route_domain: str,
request: Request,
body: ChatRequest,
tab_index: Optional[int] = Query(default=None, ge=1),
selector: Optional[str] = Query(default=None),
preset_name: Optional[str] = Query(default=None),
authenticated: bool = Depends(verify_service_auth),
_route_method: None = Depends(_route_method_guard("domain")),
):
"""使用指定域名路由匹配的标签页进行聊天。"""
client_fp = cancel_storm_guard.get_client_fingerprint(request)
await cancel_storm_guard.maybe_backoff(client_fp)
tab_index = _normalize_optional_tab_index_value(tab_index)
selector = _unwrap_fastapi_param_value(selector)
preset_name = _unwrap_fastapi_param_value(preset_name)
route_key = str(route_domain or "").strip()
# 归一化后为空也要拒绝(例如 "..." / "."):否则 target_route 一路是空串,
# _list_candidate_tabs 退化成"任意标签页",tab_index 的归属校验也会失效。
if not route_key or not normalize_route_domain(route_key):
raise HTTPException(status_code=400, detail="域名路由不能为空")
resolved_preset_name = str(preset_name or body.preset_name or "").strip() or None
if resolved_preset_name != body.preset_name:
body = body.model_copy(update={"preset_name": resolved_preset_name})
browser = get_browser(auto_connect=False)
normalized_selector = _normalize_tab_selector(
selector,
default=_get_pool_default_selector(browser),
)
tab_info = None
resolved_tab_index = None
if tab_index is not None:
tab_info = _resolve_target_tab(
browser,
route_domain=route_key,
tab_index=tab_index,
selector=normalized_selector,
)
resolved_tab_index = int(tab_info.get("persistent_index") or 0)
if resolved_tab_index < 1:
raise HTTPException(status_code=500, detail="resolved_tab_index_invalid")
resolved_headers = _build_tab_resolution_headers(
tab_info,
route_domain=route_key,
selector=("tab_index" if tab_index is not None else normalized_selector),
)
ctx = request_manager.create_request(client_fp=client_fp)
try:
raw_input_len = sum(len(str(msg.get("content") or "")) for msg in body.messages if isinstance(msg, dict))
logger.info(f"[DIAG] 接收到的原始请求 messages 总字符长度: {raw_input_len} 字符, 消息数: {len(body.messages)}")
except Exception as e:
logger.debug(f"[DIAG] 估算原始请求长度失败: {e}")
request_manager.record_request_input(
ctx,
body.model_dump(),
endpoint=f"/url/{route_key}/v1/chat/completions",
route_domain=str((tab_info or {}).get("current_domain") or (tab_info or {}).get("route_domain") or route_key),
tab_index=resolved_tab_index,
preset_name=resolved_preset_name,
)
with logger.context(ctx.request_id):
if tab_index is not None:
logger.info(
f"[ROUTE:TAB] 域名路由分发指定标签页: route={route_key}, tab_index=#{resolved_tab_index}, "
f"tab_url={(tab_info or {}).get('url')}, model={body.model!r}, "
f"preset={resolved_preset_name or '<follow-tab/default>'}"
)
return await _chat_with_resolved_tab(
request,
body,
ctx,
tab_index=resolved_tab_index,
resolved_headers=resolved_headers,
)
logger.info(
f"开始 (域名路由 {route_key} -> 动态同站点标签页, "
f"selector={normalized_selector}, "
f"preset={resolved_preset_name or '<follow-tab/default>'})"
)
return await _chat_with_route_domain(
request,
body,
ctx,
route_domain=route_key,
allocation_mode=normalized_selector,
resolved_headers=resolved_headers,
)
async def chat_with_exposed_model_name(
model_name: str,
request: Request,
body: ChatRequest,
authenticated: bool = True,
):
"""使用暴露模型名匹配同名标签页,并在同名集合中轮询。"""
client_fp = cancel_storm_guard.get_client_fingerprint(request)
await cancel_storm_guard.maybe_backoff(client_fp)
model_label = _normalize_model_name(model_name)
if not model_label:
raise HTTPException(status_code=400, detail="模型显示名称不能为空")
browser = get_browser(auto_connect=False)
tab_info = _resolve_target_tab(
browser,
model_name=model_label,
selector="round_robin",
)
resolved_tab_index = int(tab_info.get("persistent_index") or 0)
if resolved_tab_index < 1:
raise HTTPException(status_code=500, detail="resolved_tab_index_invalid")
resolved_headers = _build_tab_resolution_headers(
tab_info,
model_name=model_label,
selector="model_name_round_robin",
)
ctx = request_manager.create_request(client_fp=client_fp)
try:
raw_input_len = sum(len(str(msg.get("content") or "")) for msg in body.messages if isinstance(msg, dict))
logger.info(f"[DIAG] 接收到的原始请求 messages 总字符长度: {raw_input_len} 字符, 消息数: {len(body.messages)}")
except Exception as e:
logger.debug(f"[DIAG] 估算原始请求长度失败: {e}")
request_manager.record_request_input(
ctx,
body.model_dump(),
endpoint=f"/model-name/{model_label}/v1/chat/completions",
route_domain=str(tab_info.get("current_domain") or tab_info.get("route_domain") or ""),
tab_index=resolved_tab_index,
preset_name=body.preset_name,
)
with logger.context(ctx.request_id):
logger.info(
f"开始 (模型显示名称 {model_label} -> 标签页 #{resolved_tab_index}, "
f"preset={body.preset_name or '<follow-tab/default>'})"
)
return await _chat_with_resolved_tab(
request,
body,
ctx,
tab_index=resolved_tab_index,
resolved_headers=resolved_headers,
)
@router.post("/url/{route_domain}/{preset_name}/v1/chat/completions")
async def chat_with_route_domain_and_preset(
route_domain: str,
preset_name: str,
request: Request,
body: ChatRequest,
tab_index: Optional[int] = Query(default=None, ge=1),
selector: Optional[str] = Query(default=None),
authenticated: bool = Depends(verify_service_auth),
_route_method: None = Depends(_route_method_guard("domain")),
):
"""使用域名+预设路径风格进行聊天。路径中的预设优先级最高。"""
route_key = str(route_domain or "").strip()
preset_resolution = _resolve_strict_domain_preset(route_key, preset_name)
forced_preset_name = preset_resolution["preset_name"]
if forced_preset_name != body.preset_name:
body = body.model_copy(update={"preset_name": forced_preset_name})
return await chat_with_route_domain(
route_domain=route_key,
request=request,
body=body,
tab_index=tab_index,
selector=selector,
preset_name=forced_preset_name,
authenticated=authenticated,
)
@router.post("/group/{group_id}/v1/chat/completions")
async def chat_with_route_group(
group_id: str,
request: Request,
body: ChatRequest,
preset_name: Optional[str] = Query(default=None),
authenticated: bool = Depends(verify_service_auth),
_route_method: None = Depends(_route_method_guard("route_group")),
):
client_fp = cancel_storm_guard.get_client_fingerprint(request)
await cancel_storm_guard.maybe_backoff(client_fp)
browser = get_browser(auto_connect=False)
group = _resolve_route_group(browser, group_id)
route_domain = str(group.get("route_domain") or "").strip()
resolved_preset_name = str(
preset_name or group.get("preset_name") or body.preset_name or ""
).strip() or None
if resolved_preset_name:
if route_domain:
resolved_preset_name = _resolve_strict_domain_preset(
route_domain,
resolved_preset_name,
)["preset_name"]
else:
# 路由组的 route_domain 是可选字段(normalize_route_groups 允许留空,
# 组内成员选取也完全不依赖它),此前只要带预设就直接 400,
# 等于让"配了预设但没填域名"的组彻底不可用。没有域名时无法做站点级
# 校验,交给下游按实际命中的标签页所属站点解析(组成员本来也可能跨站)。
logger.debug(
f"路由组 '{group['id']}' 未配置站点域名,预设 "
f"'{resolved_preset_name}' 改由命中的标签页按其站点解析"
)
if resolved_preset_name != body.preset_name:
body = body.model_copy(update={"preset_name": resolved_preset_name})
ctx = request_manager.create_request(client_fp=client_fp)
try:
raw_input_len = sum(
len(str(msg.get("content") or ""))
for msg in body.messages if isinstance(msg, dict)
)
logger.info(
f"[DIAG] 接收到的原始请求 messages 总字符长度: {raw_input_len} 字符, "
f"消息数: {len(body.messages)}"
)
except Exception as e:
logger.debug(f"[DIAG] 估算原始请求长度失败: {e}")
request_manager.record_request_input(
ctx,
body.model_dump(),
endpoint=f"/group/{group['id']}/v1/chat/completions",
route_domain=route_domain,
route_group=group["id"],
preset_name=resolved_preset_name,
)
resolved_headers = _build_tab_resolution_headers(
None,
route_domain=route_domain,
selector=group.get("allocation_mode") or "round_robin",
preset_name=resolved_preset_name or "",
route_group=group["id"],
route_group_live_member_count=group.get("live_member_count"),
route_group_idle_member_count=group.get("idle_member_count"),
route_group_busy_member_count=group.get("busy_member_count"),
)
with logger.context(ctx.request_id):
logger.info(
f"开始 (标签页路由组 {group['id']} -> 组内动态标签页, "
f"selector={group.get('allocation_mode') or 'round_robin'}, "
f"preset={resolved_preset_name or '<follow-tab/default>'})"
)
return await _chat_with_route_domain(
request,
body,
ctx,
route_domain=route_domain,
route_group_id=group["id"],
allocation_mode=group.get("allocation_mode") or "round_robin",
resolved_headers=resolved_headers,
)
@router.post("/group/{group_id}/{preset_name}/v1/chat/completions")
async def chat_with_route_group_and_preset(
group_id: str,
preset_name: str,
request: Request,
body: ChatRequest,
authenticated: bool = Depends(verify_service_auth),
_route_method: None = Depends(_route_method_guard("route_group")),
):
return await chat_with_route_group(
group_id=group_id,
request=request,
body=body,
preset_name=preset_name,
authenticated=authenticated,
)
@router.post("/tab-url/{url_token}/v1/chat/completions")
async def chat_with_exact_tab_url(
url_token: str,
request: Request,
body: ChatRequest,
preset_name: Optional[str] = Query(default=None),
authenticated: bool = Depends(verify_service_auth),
_route_method: None = Depends(_route_method_guard("exact_url")),
):
"""使用标签页完整 URL 严格路由到唯一已打开标签页。"""
client_fp = cancel_storm_guard.get_client_fingerprint(request)
await cancel_storm_guard.maybe_backoff(client_fp)
route_token = str(url_token or "").strip().lower()
if not route_token:
raise HTTPException(status_code=400, detail="URL 路由无效")
resolved_preset_name = str(preset_name or body.preset_name or "").strip() or None
if resolved_preset_name != body.preset_name:
body = body.model_copy(update={"preset_name": resolved_preset_name})
browser = get_browser(auto_connect=False)
tab_info = _resolve_target_tab(
browser,
url_token=route_token,
selector="round_robin",
)
resolved_tab_index = int(tab_info.get("persistent_index") or 0)
if resolved_tab_index < 1:
raise HTTPException(status_code=500, detail="resolved_tab_index_invalid")
exact_url = str(tab_info.get("url") or "").strip()
resolved_headers = _build_tab_resolution_headers(
tab_info,
exact_url=exact_url,
selector="exact_url",
)
ctx = request_manager.create_request(client_fp=client_fp)
try:
raw_input_len = sum(len(str(msg.get("content") or "")) for msg in body.messages if isinstance(msg, dict))
logger.info(f"[DIAG] 接收到的原始请求 messages 总字符长度: {raw_input_len} 字符, 消息数: {len(body.messages)}")
except Exception as e:
logger.debug(f"[DIAG] 估算原始请求长度失败: {e}")
request_manager.record_request_input(
ctx,
body.model_dump(),
endpoint=f"/tab-url/{url_token}/v1/chat/completions",
route_domain=str(tab_info.get("current_domain") or tab_info.get("route_domain") or ""),
tab_index=resolved_tab_index,
preset_name=resolved_preset_name,
)
with logger.context(ctx.request_id):
logger.info(
f"开始 (URL 路由 {exact_url} -> 标签页 #{resolved_tab_index}, "
f"preset={resolved_preset_name or '<follow-tab/default>'})"
)
return await _chat_with_exact_url(
request,
body,
ctx,
exact_url=exact_url,
resolved_tab_index=resolved_tab_index,
resolved_headers=resolved_headers,
)
@router.post("/tab-url/{url_token}/{preset_name}/v1/chat/completions")
async def chat_with_exact_tab_url_and_preset(
url_token: str,
preset_name: str,
request: Request,
body: ChatRequest,
authenticated: bool = Depends(verify_service_auth),
_route_method: None = Depends(_route_method_guard("exact_url_preset")),
):
"""使用 URL 绑定预设路由进行聊天。URL 和预设都必须严格命中。"""
route_token = str(url_token or "").strip().lower()
if not route_token:
raise HTTPException(status_code=400, detail="URL 路由无效")
client_fp = cancel_storm_guard.get_client_fingerprint(request)
await cancel_storm_guard.maybe_backoff(client_fp)
browser = get_browser(auto_connect=False)
tab_info = _resolve_target_tab(
browser,
url_token=route_token,
selector="round_robin",
)
resolved_tab_index = int(tab_info.get("persistent_index") or 0)
if resolved_tab_index < 1:
raise HTTPException(status_code=500, detail="resolved_tab_index_invalid")
preset_resolution = _resolve_strict_tab_preset(tab_info, preset_name)
resolved_preset_name = preset_resolution["preset_name"]
if resolved_preset_name != body.preset_name:
body = body.model_copy(update={"preset_name": resolved_preset_name})
exact_url = str(tab_info.get("url") or "").strip()
resolved_headers = _build_tab_resolution_headers(
tab_info,
exact_url=exact_url,
selector="exact_url_preset",
preset_name=resolved_preset_name,
)
ctx = request_manager.create_request(client_fp=client_fp)
try:
raw_input_len = sum(len(str(msg.get("content") or "")) for msg in body.messages if isinstance(msg, dict))
logger.info(f"[DIAG] 接收到的原始请求 messages 总字符长度: {raw_input_len} 字符, 消息数: {len(body.messages)}")
except Exception as e:
logger.debug(f"[DIAG] 估算原始请求长度失败: {e}")
request_manager.record_request_input(
ctx,
body.model_dump(),
endpoint=f"/tab-url/{url_token}/{preset_name}/v1/chat/completions",
route_domain=preset_resolution["domain"],
tab_index=resolved_tab_index,
preset_name=resolved_preset_name,
)
with logger.context(ctx.request_id):
logger.info(
f"开始 (URL 绑定预设 {exact_url} -> 标签页 #{resolved_tab_index}, "
f"preset={resolved_preset_name})"
)
return await _chat_with_exact_url(
request,
body,
ctx,
exact_url=exact_url,
resolved_tab_index=resolved_tab_index,
resolved_headers=resolved_headers,
)
async def _stream_with_tab_index(
request: Request,
body: ChatRequest,
ctx: RequestContext,
tab_index: int
):
"""使用指定标签页的流式响应"""
request_context_token = _request_context.set(ctx.request_id)
disconnect_task = None
worker_thread = None
chunk_queue = None
fast_return_on_audio_media = _should_fast_return_on_audio_media(body)
fast_returned_on_audio = False
done_emitted = False
try:
disconnect_task = asyncio.create_task(
watch_client_disconnect(request, ctx, check_interval=0.3)
)
browser = get_browser(auto_connect=False)
request_manager.start_request(ctx)
chunk_queue: queue.Queue = queue.Queue(maxsize=100)
def worker():
gen = None
try:
# 🔑 使用指定标签页
gen = browser.execute_workflow_for_tab_index(
tab_index,
body.messages,
stream=True,
task_id=ctx.request_id,
preset_name=body.preset_name,
stop_checker=ctx.should_stop,
requested_model=body.model,
**({"workflow_variables": body.workflow_variables} if body.workflow_variables else {}),
)
for chunk in gen:
if ctx.should_stop():
cancel_reason = str(ctx.cancel_reason or "unknown")
if cancel_reason in {"cleanup", "client_disconnected", "coroutine_cancelled"}:
logger.debug(f"工作线程检测到停止: {cancel_reason}")
else:
logger.info(f"工作线程检测到取消: {cancel_reason}")
break
if not _put_route_worker_queue_item(chunk_queue, ctx, chunk):
logger.debug("工作线程停止入队,结束流式生产(tab_index)")
break
except Exception as e:
logger.error(f"工作线程异常: {e}")
_put_route_worker_queue_item(chunk_queue, ctx, ("ERROR", str(e)), final=True)
finally:
if gen is not None:
try:
gen.close()
except Exception as e:
logger.debug(f"关闭工作流生成器失败(忽略): {e}")
_put_route_worker_queue_item(chunk_queue, ctx, None, final=True)
worker_thread = threading.Thread(
target=run_in_request_context,
args=(ctx, worker),
daemon=True,
)
# 供 TabRecovery 判定旧 worker 是否退出(setattr 规避 dataclass 字段限制)
setattr(ctx, "worker_thread", worker_thread)
worker_thread.start()
# Browser workflows can spend several seconds acquiring a tab before
# their first result chunk. Emit a valid SSE frame immediately so an
# upstream short read timeout cannot turn one request into a retry loop.
keepalive_id = f"chatcmpl-{ctx.request_id}"
initial_keepalive = SSEFormatter.pack_keepalive(
model=body.model,
completion_id=keepalive_id,
)
request_manager.capture_response_chunk(ctx, initial_keepalive)
yield initial_keepalive
last_sse_emit_at = time.monotonic()
request_started_at = time.monotonic()
max_execute_time_sec = get_max_request_execute_time_sec()
client_disconnected = False
stop_state = build_stop_sequence_stream_state(
body.stop,
single_choice=_is_single_choice_request(body),
upstream_single_choice=True,
)
while True:
if mark_request_hard_timeout(
ctx,
request_started_at,
max_execute_time_sec,
label=f"tab_index={tab_index}",
):
request_manager.capture_error(ctx, "请求执行超过最大绝对超时", code="absolute_request_timeout")
ctx.mark_failed("absolute_request_timeout")
done_emitted = True
yield _pack_error_done("请求执行超过最大绝对超时,已强制中断", "absolute_request_timeout")
break
if await request.is_disconnected():
ctx.request_cancel("client_disconnected")
client_disconnected = True
break
try:
chunk = await wait_worker_queue_item(
chunk_queue,
timeout=STREAM_QUEUE_POLL_TIMEOUT,
)
except queue.Empty:
# 兜底:worker 的结束哨兵(None)在队列持续打满时可能被丢弃,
# 若 worker 线程已退出且队列已排空,直接收尾避免空转到绝对超时。
if not worker_thread.is_alive() and chunk_queue.empty():
logger.warning("工作线程已退出且队列为空(结束标记疑似丢失),提前收尾流式消费")
break
if time.monotonic() - last_sse_emit_at >= SSE_HEARTBEAT_INTERVAL:
yield SSEFormatter.pack_keepalive(
model=body.model,
completion_id=keepalive_id,
)
last_sse_emit_at = time.monotonic()
continue
if chunk is None:
break
if isinstance(chunk, tuple) and chunk[0] == "ERROR":
request_manager.capture_error(ctx, chunk[1], code="worker_error")
ctx.mark_failed(chunk[1])
done_emitted = True
yield _pack_error_done(f"执行错误: {chunk[1]}", "internal_error")
break
outgoing_chunks = filter_openai_stop_sse_chunk(chunk, stop_state, body.model)
saw_audio_media = False
for outgoing_chunk in outgoing_chunks:
emit_chunk, chunk_had_done = _split_sse_done_frame(outgoing_chunk)
if emit_chunk:
request_manager.capture_response_chunk(ctx, emit_chunk)
yield emit_chunk
if fast_return_on_audio_media and _has_audio_media(_extract_sse_chunk_media_items(emit_chunk)):
saw_audio_media = True
last_sse_emit_at = time.monotonic()
error_message = _extract_stream_error_message(emit_chunk or outgoing_chunk)
if error_message:
logger.error(f"流式响应返回错误事件(tab={tab_index}): {error_message}")
error_code = next((str(payload["error"].get("code") or "stream_error")
for payload in iter_openai_sse_payloads(emit_chunk or outgoing_chunk)
if isinstance(payload.get("error"), dict)), "stream_error")
request_manager.capture_error(ctx, error_message, code=error_code)
ctx.mark_failed(error_code)
done_chunk = _pack_done()
request_manager.capture_response_chunk(ctx, done_chunk)
done_emitted = True
yield done_chunk
break
if chunk_had_done:
usage_chunk = _maybe_pack_stream_usage_chunk(body)
if usage_chunk:
request_manager.capture_response_chunk(ctx, usage_chunk)
yield usage_chunk
done_chunk = _pack_done()
request_manager.capture_response_chunk(ctx, done_chunk)
done_emitted = True
ctx.mark_completed()
ctx.request_cancel("stream_done")
yield done_chunk
if done_emitted:
break
if stop_state.stopped:
done_emitted = True
ctx.request_cancel("stop_sequence")
ctx.mark_completed()
break
if ctx.status == RequestStatus.FAILED:
break
if saw_audio_media:
fast_returned_on_audio = True
ctx.request_cancel("audio_media_fast_return")
ctx.mark_completed()
logger.info(f"流式朗读响应已取得音频,提前结束(tab={tab_index})")
done_emitted = True
for fast_return_chunk in _pack_audio_fast_return_chunks(body):
request_manager.capture_response_chunk(ctx, fast_return_chunk)
yield fast_return_chunk
break
await asyncio.sleep(0)
if (
not client_disconnected
and not done_emitted
and ctx.status != RequestStatus.FAILED
):
for tail_chunk in flush_openai_stop_state(stop_state, body.model):
request_manager.capture_response_chunk(ctx, tail_chunk)
yield tail_chunk
usage_chunk = _maybe_pack_stream_usage_chunk(body)
if usage_chunk:
request_manager.capture_response_chunk(ctx, usage_chunk)
yield usage_chunk
done_chunk = _pack_done()
request_manager.capture_response_chunk(ctx, done_chunk)
done_emitted = True
if not ctx.should_stop() and ctx.status == RequestStatus.RUNNING:
ctx.mark_completed()
yield done_chunk
if not ctx.should_stop() and ctx.status == RequestStatus.RUNNING:
ctx.mark_completed()
except GeneratorExit:
if not ctx.is_terminal():
ctx.request_cancel("stream_closed")
raise
except asyncio.CancelledError:
ctx.request_cancel("coroutine_cancelled")
raise
except Exception as e:
logger.error(f"异常: {e}")
request_manager.capture_error(ctx, e, code="internal_error")
ctx.mark_failed(str(getattr(e, "code", None) or e))
done_emitted = True
yield _pack_error(f"执行错误: {str(e)}", "internal_error")
yield _pack_done()
finally:
with request_finalization(ctx, request_context_token):
await _cleanup_route_worker_thread(
worker_thread,
ctx,
fast_returned_on_audio=fast_returned_on_audio,
done_emitted=done_emitted,
)
if chunk_queue is not None:
try:
while not chunk_queue.empty():
chunk_queue.get_nowait()
except:
pass
if disconnect_task:
disconnect_task.cancel()
try:
await disconnect_task
except asyncio.CancelledError:
pass
async def _non_stream_with_tab_index(
request: Request,
body: ChatRequest,
ctx: RequestContext,
tab_index: int
) -> JSONResponse:
"""使用指定标签页的非流式响应"""
collected_content = []
collected_media = []
error_data = None
fast_return_on_audio_media = _should_fast_return_on_audio_media(body)
parse_sse_payloads = _make_buffered_sse_payload_parser()
async for chunk in _stream_with_tab_index(request, body, ctx, tab_index):
if isinstance(chunk, str):
for data in parse_sse_payloads(chunk):
try:
error_data = _consume_non_stream_sse_payload(
data,
collected_content=collected_content,
collected_media=collected_media,
)
if error_data:
break
if fast_return_on_audio_media and _has_audio_media(collected_media):
ctx.mark_completed()
break
except json.JSONDecodeError:
continue
if error_data or (fast_return_on_audio_media and _has_audio_media(collected_media)):
break
if not error_data and not (fast_return_on_audio_media and _has_audio_media(collected_media)):
for data in parse_sse_payloads.flush():
error_data = _consume_non_stream_sse_payload(
data,
collected_content=collected_content,
collected_media=collected_media,
)
if error_data:
break
error_meta = resolve_error_metadata(error_data) or resolve_error_metadata(ctx)
if error_meta:
return build_error_response(error_meta)
if error_data:
fallback_meta = resolve_error_metadata(error_data)
if fallback_meta:
return build_error_response(fallback_meta)
return JSONResponse(content=error_data, status_code=500)
full_content = apply_stop_sequences_to_text(
_cleanup_non_stream_content("".join(collected_content)),
body.stop,
)
response = SSEFormatter.pack_non_stream(
full_content,
model=body.model,
media=_dedupe_media_items(collected_media),
)
request_manager.capture_response_payload(ctx, response)
return JSONResponse(content=response)
async def _stream_with_route_domain(
request: Request,
body: ChatRequest,
ctx: RequestContext,
route_domain: str,
route_group_id: Optional[str] = None,
allocation_mode: Optional[str] = None,
):
"""使用指定域名路由的流式响应"""
request_context_token = _request_context.set(ctx.request_id)
disconnect_task = None
worker_thread = None
chunk_queue = None
fast_return_on_audio_media = _should_fast_return_on_audio_media(body)
fast_returned_on_audio = False
done_emitted = False
try:
disconnect_task = asyncio.create_task(
watch_client_disconnect(request, ctx, check_interval=0.3)
)
browser = get_browser(auto_connect=False)
request_manager.start_request(ctx)
chunk_queue = queue.Queue(maxsize=100)
def worker():
gen = None
try:
if route_group_id:
gen = browser.execute_workflow_for_route_group(
route_group_id,
body.messages,
stream=True,
task_id=ctx.request_id,
preset_name=body.preset_name,
stop_checker=ctx.should_stop,
allocation_mode=allocation_mode,
requested_model=body.model,
**({"workflow_variables": body.workflow_variables} if body.workflow_variables else {}),
)
else:
gen = browser.execute_workflow_for_route_domain(
route_domain,
body.messages,
stream=True,
task_id=ctx.request_id,
preset_name=body.preset_name,
stop_checker=ctx.should_stop,
allocation_mode=allocation_mode,
requested_model=body.model,
**({"workflow_variables": body.workflow_variables} if body.workflow_variables else {}),
)
for chunk in gen:
if ctx.should_stop():
cancel_reason = str(ctx.cancel_reason or "unknown")
if cancel_reason in {"cleanup", "client_disconnected", "coroutine_cancelled"}:
logger.debug(f"工作线程检测到停止: {cancel_reason}")
else:
logger.info(f"工作线程检测到取消: {cancel_reason}")
break
if not _put_route_worker_queue_item(chunk_queue, ctx, chunk):
logger.debug(
"工作线程停止入队,结束流式生产("
f"{'route_group=' + route_group_id if route_group_id else 'route_domain'}"
")"
)
break
except Exception as e:
logger.error(f"工作线程异常: {e}")
_put_route_worker_queue_item(chunk_queue, ctx, ("ERROR", str(e)), final=True)
finally:
if gen is not None:
try:
gen.close()
except Exception as e:
logger.debug(f"关闭工作流生成器失败(忽略): {e}")
_put_route_worker_queue_item(chunk_queue, ctx, None, final=True)
worker_thread = threading.Thread(
target=run_in_request_context,
args=(ctx, worker),
daemon=True,
)
# 供 TabRecovery 判定旧 worker 是否退出(setattr 规避 dataclass 字段限制)
setattr(ctx, "worker_thread", worker_thread)
worker_thread.start()
keepalive_id = f"chatcmpl-{ctx.request_id}"
initial_keepalive = SSEFormatter.pack_keepalive(
model=body.model,
completion_id=keepalive_id,
)
request_manager.capture_response_chunk(ctx, initial_keepalive)
yield initial_keepalive
last_sse_emit_at = time.monotonic()
request_started_at = time.monotonic()
max_execute_time_sec = get_max_request_execute_time_sec()
client_disconnected = False
stop_state = build_stop_sequence_stream_state(
body.stop,
single_choice=_is_single_choice_request(body),
upstream_single_choice=True,
)
while True:
if mark_request_hard_timeout(
ctx,
request_started_at,
max_execute_time_sec,
label=(f"route_group={route_group_id}" if route_group_id else f"route_domain={route_domain}"),
):
request_manager.capture_error(ctx, "请求执行超过最大绝对超时", code="absolute_request_timeout")
ctx.mark_failed("absolute_request_timeout")
done_emitted = True
yield _pack_error_done("请求执行超过最大绝对超时,已强制中断", "absolute_request_timeout")
break
if await request.is_disconnected():
ctx.request_cancel("client_disconnected")
client_disconnected = True
break
try:
chunk = await wait_worker_queue_item(
chunk_queue,
timeout=STREAM_QUEUE_POLL_TIMEOUT,
)
except queue.Empty:
# 兜底:worker 的结束哨兵(None)在队列持续打满时可能被丢弃,
# 若 worker 线程已退出且队列已排空,直接收尾避免空转到绝对超时。
if not worker_thread.is_alive() and chunk_queue.empty():
logger.warning("工作线程已退出且队列为空(结束标记疑似丢失),提前收尾流式消费")
break
if time.monotonic() - last_sse_emit_at >= SSE_HEARTBEAT_INTERVAL:
yield SSEFormatter.pack_keepalive(
model=body.model,
completion_id=keepalive_id,
)
last_sse_emit_at = time.monotonic()
continue
if chunk is None:
break
if isinstance(chunk, tuple) and chunk[0] == "ERROR":
request_manager.capture_error(ctx, chunk[1], code="worker_error")
ctx.mark_failed(chunk[1])
done_emitted = True
yield _pack_error_done(f"执行错误: {chunk[1]}", "internal_error")
break
outgoing_chunks = filter_openai_stop_sse_chunk(chunk, stop_state, body.model)
saw_audio_media = False
for outgoing_chunk in outgoing_chunks:
emit_chunk, chunk_had_done = _split_sse_done_frame(outgoing_chunk)
if emit_chunk:
request_manager.capture_response_chunk(ctx, emit_chunk)
yield emit_chunk
if fast_return_on_audio_media and _has_audio_media(_extract_sse_chunk_media_items(emit_chunk)):
saw_audio_media = True
last_sse_emit_at = time.monotonic()
error_message = _extract_stream_error_message(emit_chunk or outgoing_chunk)
if error_message:
route_label = f"route_group={route_group_id}" if route_group_id else f"route_domain={route_domain}"
logger.error(f"流式响应返回错误事件({route_label}): {error_message}")
error_code = next((str(payload["error"].get("code") or "stream_error")
for payload in iter_openai_sse_payloads(emit_chunk or outgoing_chunk)
if isinstance(payload.get("error"), dict)), "stream_error")
request_manager.capture_error(ctx, error_message, code=error_code)
ctx.mark_failed(error_code)
done_chunk = _pack_done()
request_manager.capture_response_chunk(ctx, done_chunk)
done_emitted = True
yield done_chunk
break
if chunk_had_done:
usage_chunk = _maybe_pack_stream_usage_chunk(body)
if usage_chunk:
request_manager.capture_response_chunk(ctx, usage_chunk)
yield usage_chunk
done_chunk = _pack_done()
request_manager.capture_response_chunk(ctx, done_chunk)
done_emitted = True
ctx.mark_completed()
ctx.request_cancel("stream_done")
yield done_chunk
if done_emitted:
break
if stop_state.stopped:
done_emitted = True
ctx.request_cancel("stop_sequence")
ctx.mark_completed()
break
if ctx.status == RequestStatus.FAILED:
break
if saw_audio_media:
fast_returned_on_audio = True
ctx.request_cancel("audio_media_fast_return")
ctx.mark_completed()
route_label = f"route_group={route_group_id}" if route_group_id else f"route_domain={route_domain}"
logger.info(f"流式朗读响应已取得音频,提前结束({route_label})")
done_emitted = True
for fast_return_chunk in _pack_audio_fast_return_chunks(body):
request_manager.capture_response_chunk(ctx, fast_return_chunk)
yield fast_return_chunk
break
await asyncio.sleep(0)
if (
not client_disconnected
and not done_emitted
and ctx.status != RequestStatus.FAILED
):
for tail_chunk in flush_openai_stop_state(stop_state, body.model):
request_manager.capture_response_chunk(ctx, tail_chunk)
yield tail_chunk
usage_chunk = _maybe_pack_stream_usage_chunk(body)
if usage_chunk:
request_manager.capture_response_chunk(ctx, usage_chunk)
yield usage_chunk
done_chunk = _pack_done()
request_manager.capture_response_chunk(ctx, done_chunk)
done_emitted = True
if not ctx.should_stop() and ctx.status == RequestStatus.RUNNING:
ctx.mark_completed()
yield done_chunk
if not ctx.should_stop() and ctx.status == RequestStatus.RUNNING:
ctx.mark_completed()
except GeneratorExit:
if not ctx.is_terminal():
ctx.request_cancel("stream_closed")
raise
except asyncio.CancelledError:
ctx.request_cancel("coroutine_cancelled")
raise
except Exception as e:
logger.error(f"异常: {e}")
request_manager.capture_error(ctx, e, code="internal_error")
ctx.mark_failed(str(getattr(e, "code", None) or e))
done_emitted = True
yield _pack_error(f"执行错误: {str(e)}", "internal_error")
yield _pack_done()
finally:
with request_finalization(ctx, request_context_token):
await _cleanup_route_worker_thread(
worker_thread,
ctx,
fast_returned_on_audio=fast_returned_on_audio,
done_emitted=done_emitted,
)
if chunk_queue is not None:
try:
while not chunk_queue.empty():
chunk_queue.get_nowait()
except:
pass
if disconnect_task:
disconnect_task.cancel()
try:
await disconnect_task
except asyncio.CancelledError:
pass
async def _non_stream_with_route_domain(
request: Request,
body: ChatRequest,
ctx: RequestContext,
route_domain: str,
route_group_id: Optional[str] = None,
allocation_mode: Optional[str] = None,
) -> JSONResponse:
"""使用指定域名路由的非流式响应"""
collected_content = []
collected_media = []
error_data = None
fast_return_on_audio_media = _should_fast_return_on_audio_media(body)
parse_sse_payloads = _make_buffered_sse_payload_parser()
async for chunk in _stream_with_route_domain(
request,
body,
ctx,
route_domain,
route_group_id=route_group_id,
allocation_mode=allocation_mode,
):
if isinstance(chunk, str):
for data in parse_sse_payloads(chunk):
try:
error_data = _consume_non_stream_sse_payload(
data,
collected_content=collected_content,
collected_media=collected_media,
)
if error_data:
break
if fast_return_on_audio_media and _has_audio_media(collected_media):
ctx.mark_completed()
break
except json.JSONDecodeError:
continue
if error_data or (fast_return_on_audio_media and _has_audio_media(collected_media)):
break
if not error_data and not (fast_return_on_audio_media and _has_audio_media(collected_media)):
for data in parse_sse_payloads.flush():
error_data = _consume_non_stream_sse_payload(
data,
collected_content=collected_content,
collected_media=collected_media,
)
if error_data:
break
error_meta = resolve_error_metadata(error_data) or resolve_error_metadata(ctx)
if error_meta:
return build_error_response(error_meta)
if error_data:
fallback_meta = resolve_error_metadata(error_data)
if fallback_meta:
return build_error_response(fallback_meta)
return JSONResponse(content=error_data, status_code=500)
full_content = apply_stop_sequences_to_text(
_cleanup_non_stream_content("".join(collected_content)),
body.stop,
)
response = SSEFormatter.pack_non_stream(
full_content,
model=body.model,
media=_dedupe_media_items(collected_media),
)
request_manager.capture_response_payload(ctx, response)
return JSONResponse(content=response)
async def _stream_with_exact_url(
request: Request,
body: ChatRequest,
ctx: RequestContext,
exact_url: str,
resolved_tab_index: Optional[int] = None,
):
"""使用精确 URL 路由的流式响应"""
request_context_token = _request_context.set(ctx.request_id)
disconnect_task = None
worker_thread = None
chunk_queue = None
fast_return_on_audio_media = _should_fast_return_on_audio_media(body)
fast_returned_on_audio = False
done_emitted = False
try:
disconnect_task = asyncio.create_task(
watch_client_disconnect(request, ctx, check_interval=0.3)
)
browser = get_browser(auto_connect=False)
request_manager.start_request(ctx)
chunk_queue = queue.Queue(maxsize=100)
def worker():
gen = None
try:
gen = browser.execute_workflow_for_exact_url(
exact_url,
body.messages,
stream=True,
task_id=ctx.request_id,
preset_name=body.preset_name,
stop_checker=ctx.should_stop,
resolved_tab_index=resolved_tab_index,
requested_model=body.model,
**({"workflow_variables": body.workflow_variables} if body.workflow_variables else {}),
)
for chunk in gen:
if ctx.should_stop():
cancel_reason = str(ctx.cancel_reason or "unknown")
if cancel_reason in {"cleanup", "client_disconnected", "coroutine_cancelled"}:
logger.debug(f"工作线程检测到停止: {cancel_reason}")
else:
logger.info(f"工作线程检测到取消: {cancel_reason}")
break
if not _put_route_worker_queue_item(chunk_queue, ctx, chunk):
logger.debug("工作线程停止入队,结束流式生产(exact_url)")
break
except Exception as e:
logger.error(f"工作线程异常: {e}")
_put_route_worker_queue_item(chunk_queue, ctx, ("ERROR", str(e)), final=True)
finally:
if gen is not None:
try:
gen.close()
except Exception as e:
logger.debug(f"关闭工作流生成器失败(忽略): {e}")
_put_route_worker_queue_item(chunk_queue, ctx, None, final=True)
worker_thread = threading.Thread(
target=run_in_request_context,
args=(ctx, worker),
daemon=True,
)
# 供 TabRecovery 判定旧 worker 是否退出(setattr 规避 dataclass 字段限制)
setattr(ctx, "worker_thread", worker_thread)
worker_thread.start()
keepalive_id = f"chatcmpl-{ctx.request_id}"
initial_keepalive = SSEFormatter.pack_keepalive(
model=body.model,
completion_id=keepalive_id,
)
request_manager.capture_response_chunk(ctx, initial_keepalive)
yield initial_keepalive
last_sse_emit_at = time.monotonic()
request_started_at = time.monotonic()
max_execute_time_sec = get_max_request_execute_time_sec()
client_disconnected = False
stop_state = build_stop_sequence_stream_state(
body.stop,
single_choice=_is_single_choice_request(body),
upstream_single_choice=True,
)
while True:
if mark_request_hard_timeout(
ctx,
request_started_at,
max_execute_time_sec,
label=f"exact_url={exact_url}",
):
request_manager.capture_error(ctx, "请求执行超过最大绝对超时", code="absolute_request_timeout")
ctx.mark_failed("absolute_request_timeout")
done_emitted = True
yield _pack_error_done("请求执行超过最大绝对超时,已强制中断", "absolute_request_timeout")
break
if await request.is_disconnected():
ctx.request_cancel("client_disconnected")
client_disconnected = True
break
try:
chunk = await wait_worker_queue_item(
chunk_queue,
timeout=STREAM_QUEUE_POLL_TIMEOUT,
)
except queue.Empty:
# 兜底:worker 的结束哨兵(None)在队列持续打满时可能被丢弃,
# 若 worker 线程已退出且队列已排空,直接收尾避免空转到绝对超时。
if not worker_thread.is_alive() and chunk_queue.empty():
logger.warning("工作线程已退出且队列为空(结束标记疑似丢失),提前收尾流式消费")
break
if time.monotonic() - last_sse_emit_at >= SSE_HEARTBEAT_INTERVAL:
yield SSEFormatter.pack_keepalive(
model=body.model,
completion_id=keepalive_id,
)
last_sse_emit_at = time.monotonic()
continue
if chunk is None:
break
if isinstance(chunk, tuple) and chunk[0] == "ERROR":
request_manager.capture_error(ctx, chunk[1], code="worker_error")
ctx.mark_failed(chunk[1])
done_emitted = True
yield _pack_error_done(f"执行错误: {chunk[1]}", "internal_error")
break
outgoing_chunks = filter_openai_stop_sse_chunk(chunk, stop_state, body.model)
saw_audio_media = False
for outgoing_chunk in outgoing_chunks:
emit_chunk, chunk_had_done = _split_sse_done_frame(outgoing_chunk)
if emit_chunk:
request_manager.capture_response_chunk(ctx, emit_chunk)
yield emit_chunk
if fast_return_on_audio_media and _has_audio_media(_extract_sse_chunk_media_items(emit_chunk)):
saw_audio_media = True
last_sse_emit_at = time.monotonic()
error_message = _extract_stream_error_message(emit_chunk or outgoing_chunk)
if error_message:
logger.error(f"流式响应返回错误事件(exact_url={exact_url}): {error_message}")
error_code = next((str(payload["error"].get("code") or "stream_error")
for payload in iter_openai_sse_payloads(emit_chunk or outgoing_chunk)
if isinstance(payload.get("error"), dict)), "stream_error")
request_manager.capture_error(ctx, error_message, code=error_code)
ctx.mark_failed(error_code)
done_chunk = _pack_done()
request_manager.capture_response_chunk(ctx, done_chunk)
done_emitted = True
yield done_chunk
break
if chunk_had_done:
usage_chunk = _maybe_pack_stream_usage_chunk(body)
if usage_chunk:
request_manager.capture_response_chunk(ctx, usage_chunk)
yield usage_chunk
done_chunk = _pack_done()
request_manager.capture_response_chunk(ctx, done_chunk)
done_emitted = True
ctx.mark_completed()
ctx.request_cancel("stream_done")
yield done_chunk
if done_emitted:
break
if stop_state.stopped:
done_emitted = True
ctx.request_cancel("stop_sequence")
ctx.mark_completed()
break
if ctx.status == RequestStatus.FAILED:
break
if saw_audio_media:
fast_returned_on_audio = True
ctx.request_cancel("audio_media_fast_return")
ctx.mark_completed()
logger.info(f"流式朗读响应已取得音频,提前结束(exact_url={exact_url})")
done_emitted = True
for fast_return_chunk in _pack_audio_fast_return_chunks(body):
request_manager.capture_response_chunk(ctx, fast_return_chunk)
yield fast_return_chunk
break
await asyncio.sleep(0)
if (
not client_disconnected
and not done_emitted
and ctx.status != RequestStatus.FAILED
):
for tail_chunk in flush_openai_stop_state(stop_state, body.model):
request_manager.capture_response_chunk(ctx, tail_chunk)
yield tail_chunk
usage_chunk = _maybe_pack_stream_usage_chunk(body)
if usage_chunk:
request_manager.capture_response_chunk(ctx, usage_chunk)
yield usage_chunk
done_chunk = _pack_done()
request_manager.capture_response_chunk(ctx, done_chunk)
done_emitted = True
if not ctx.should_stop() and ctx.status == RequestStatus.RUNNING:
ctx.mark_completed()
yield done_chunk
if not ctx.should_stop() and ctx.status == RequestStatus.RUNNING:
ctx.mark_completed()
except GeneratorExit:
if not ctx.is_terminal():
ctx.request_cancel("stream_closed")
raise
except asyncio.CancelledError:
ctx.request_cancel("coroutine_cancelled")
raise
except Exception as e:
logger.error(f"异常: {e}")
request_manager.capture_error(ctx, e, code="internal_error")
ctx.mark_failed(str(getattr(e, "code", None) or e))
done_emitted = True
yield _pack_error(f"执行错误: {str(e)}", "internal_error")
yield _pack_done()
finally:
with request_finalization(ctx, request_context_token):
await _cleanup_route_worker_thread(
worker_thread,
ctx,
fast_returned_on_audio=fast_returned_on_audio,
done_emitted=done_emitted,
)
if chunk_queue is not None:
try:
while not chunk_queue.empty():
chunk_queue.get_nowait()
except:
pass
if disconnect_task:
disconnect_task.cancel()
try:
await disconnect_task
except asyncio.CancelledError:
pass
async def _non_stream_with_exact_url(
request: Request,
body: ChatRequest,
ctx: RequestContext,
exact_url: str,
resolved_tab_index: Optional[int] = None,
) -> JSONResponse:
"""使用精确 URL 路由的非流式响应"""
collected_content = []
collected_media = []
error_data = None
fast_return_on_audio_media = _should_fast_return_on_audio_media(body)
parse_sse_payloads = _make_buffered_sse_payload_parser()
async for chunk in _stream_with_exact_url(
request,
body,
ctx,
exact_url,
resolved_tab_index=resolved_tab_index,
):
if isinstance(chunk, str):
for data in parse_sse_payloads(chunk):
try:
error_data = _consume_non_stream_sse_payload(
data,
collected_content=collected_content,
collected_media=collected_media,
)
if error_data:
break
if fast_return_on_audio_media and _has_audio_media(collected_media):
ctx.mark_completed()
break
except json.JSONDecodeError:
continue
if error_data or (fast_return_on_audio_media and _has_audio_media(collected_media)):
break
if not error_data and not (fast_return_on_audio_media and _has_audio_media(collected_media)):
for data in parse_sse_payloads.flush():
error_data = _consume_non_stream_sse_payload(
data,
collected_content=collected_content,
collected_media=collected_media,
)
if error_data:
break
error_meta = resolve_error_metadata(error_data) or resolve_error_metadata(ctx)
if error_meta:
return build_error_response(error_meta)
if error_data:
fallback_meta = resolve_error_metadata(error_data)
if fallback_meta:
return build_error_response(fallback_meta)
return JSONResponse(content=error_data, status_code=500)
full_content = apply_stop_sequences_to_text(
_cleanup_non_stream_content("".join(collected_content)),
body.stop,
)
response = SSEFormatter.pack_non_stream(
full_content,
model=body.model,
media=_dedupe_media_items(collected_media),
)
request_manager.capture_response_payload(ctx, response)
return JSONResponse(content=response)
def _execute_browser_non_stream_for_tab(
browser,
tab_index: int,
messages: List[Dict[str, Any]],
request_id: str,
preset_name: Optional[str] = None,
stop_checker=None,
requested_model: Optional[str] = None,
workflow_variables: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
payload = None
for chunk in browser.execute_workflow_for_tab_index(
tab_index,
messages,
stream=False,
task_id=request_id,
preset_name=preset_name,
stop_checker=stop_checker,
allow_media_postprocess=get_tool_calling_allow_media_postprocess(),
requested_model=requested_model,
**({"workflow_variables": workflow_variables} if workflow_variables else {}),
):
payload = chunk
if not payload:
raise RuntimeError("empty_browser_response")
data = decode_browser_non_stream_payload(payload)
if "error" in data:
error = data.get("error") or {}
from app.services.error_metadata import BrowserExecutionError
raise BrowserExecutionError(error)
return data
def _execute_browser_non_stream_for_route_domain(
browser,
route_domain: str,
messages: List[Dict[str, Any]],
request_id: str,
preset_name: Optional[str] = None,
stop_checker=None,
allocation_mode: Optional[str] = None,
route_group_id: Optional[str] = None,
requested_model: Optional[str] = None,
workflow_variables: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
payload = None
execute = (
browser.execute_workflow_for_route_group
if route_group_id
else browser.execute_workflow_for_route_domain
)
route_key = route_group_id or route_domain
for chunk in execute(
route_key,
messages,
stream=False,
task_id=request_id,
preset_name=preset_name,
stop_checker=stop_checker,
allocation_mode=allocation_mode,
allow_media_postprocess=get_tool_calling_allow_media_postprocess(),
requested_model=requested_model,
**({"workflow_variables": workflow_variables} if workflow_variables else {}),
):
payload = chunk
if not payload:
raise RuntimeError("empty_browser_response")
data = decode_browser_non_stream_payload(payload)
if "error" in data:
error = data.get("error") or {}
from app.services.error_metadata import BrowserExecutionError
raise BrowserExecutionError(error)
return data
def _execute_browser_non_stream_for_exact_url(
browser,
exact_url: str,
messages: List[Dict[str, Any]],
request_id: str,
preset_name: Optional[str] = None,
stop_checker=None,
resolved_tab_index: Optional[int] = None,
requested_model: Optional[str] = None,
workflow_variables: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
payload = None
for chunk in browser.execute_workflow_for_exact_url(
exact_url,
messages,
stream=False,
task_id=request_id,
preset_name=preset_name,
stop_checker=stop_checker,
resolved_tab_index=resolved_tab_index,
allow_media_postprocess=get_tool_calling_allow_media_postprocess(),
requested_model=requested_model,
**({"workflow_variables": workflow_variables} if workflow_variables else {}),
):
payload = chunk
if not payload:
raise RuntimeError("empty_browser_response")
data = decode_browser_non_stream_payload(payload)
if "error" in data:
error = data.get("error") or {}
from app.services.error_metadata import BrowserExecutionError
raise BrowserExecutionError(error)
return data
def _extract_assistant_content(response: Dict[str, Any]) -> str:
try:
return extract_tool_calling_assistant_content(response)
except Exception:
return ""
class _RouteToolCallingExecutionCancelled(Exception):
"""Raised when a route-bound tool-calling worker is still running after cancellation."""
def _get_route_tool_calling_cancel_reason(ctx: RequestContext) -> str:
reason = str(ctx.cancel_reason or "").strip()
return reason or "tool_calling_cancelled"
def _is_absolute_request_timeout_error(error: Any) -> bool:
return str(error or "").strip() == "absolute_request_timeout"
def _format_route_tool_calling_error(error: Any) -> tuple[str, str]:
if _is_absolute_request_timeout_error(error):
return "请求执行超过最大绝对超时,已强制中断", "absolute_request_timeout"
return f"执行错误: {error}", str(getattr(error, "code", None) or "tool_calling_failed")
async def _run_tracked_route_tool_calling_worker(
worker_fn,
*,
ctx: RequestContext,
worker_state: Dict[str, Any],
label: str,
) -> Any:
try:
return await run_tracked_blocking_call(
worker_fn,
ctx=ctx,
worker_state=worker_state,
label=label,
poll_timeout=STREAM_QUEUE_POLL_TIMEOUT,
)
except TrackedWorkerExecutionCancelled as e:
raise _RouteToolCallingExecutionCancelled(
str(e) or _get_route_tool_calling_cancel_reason(ctx)
)
async def _run_tool_calling_async_for_tab(
browser,
tab_index: int,
body: ChatRequest,
request_id: str,
stop_checker=None,
worker_state: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
legacy_function_call = bool(body.functions) and not bool(body.tools)
tools, tool_choice = normalize_tool_request(
tools=body.tools,
tool_choice=body.tool_choice,
functions=body.functions,
function_call=body.function_call,
)
try:
logger.debug(
"[tab] 请求消息摘要: "
f"{summarize_messages_for_debug(body.messages)}"
)
except Exception as e:
logger.debug(f"[tab] 请求消息摘要生成失败: {e}")
tracked_worker_state = worker_state if isinstance(worker_state, dict) else {}
async def _round_executor(browser_messages: List[Dict[str, str]]) -> str:
worker_fn = lambda: _extract_assistant_content(
_execute_browser_non_stream_for_tab(
browser=browser,
tab_index=tab_index,
messages=browser_messages,
request_id=request_id,
preset_name=body.preset_name,
stop_checker=stop_checker,
requested_model=body.model,
**({"workflow_variables": body.workflow_variables} if body.workflow_variables else {}),
)
)
if isinstance(tracked_worker_state.get("ctx"), RequestContext):
return await _run_tracked_route_tool_calling_worker(
worker_fn,
ctx=tracked_worker_state["ctx"],
worker_state=tracked_worker_state,
label=f"{request_id[:8]}-tab-round",
)
return await asyncio.to_thread(worker_fn)
parsed = await complete_tool_calling_roundtrip_async(
messages=body.messages,
tools=tools,
tool_choice=tool_choice,
parallel_tool_calls=(
False
if legacy_function_call and body.parallel_tool_calls is None
else body.parallel_tool_calls
),
round_executor=_round_executor,
stop_checker=stop_checker,
)
if not parsed.get("tool_calls"):
parsed = dict(parsed)
parsed["content"] = apply_stop_sequences_to_text(
str(parsed.get("content") or ""),
body.stop,
)
return build_tool_completion_response(
body.model,
parsed,
legacy_function_call=legacy_function_call,
)
async def _run_tool_calling_async_for_route_domain(
browser,
route_domain: str,
body: ChatRequest,
request_id: str,
stop_checker=None,
worker_state: Optional[Dict[str, Any]] = None,
allocation_mode: Optional[str] = None,
route_group_id: Optional[str] = None,
) -> Dict[str, Any]:
legacy_function_call = bool(body.functions) and not bool(body.tools)
tools, tool_choice = normalize_tool_request(
tools=body.tools,
tool_choice=body.tool_choice,
functions=body.functions,
function_call=body.function_call,
)
try:
logger.debug(
"[route] 请求消息摘要: "
f"{summarize_messages_for_debug(body.messages)}"
)
except Exception as e:
logger.debug(f"[route] 请求消息摘要生成失败: {e}")
tracked_worker_state = worker_state if isinstance(worker_state, dict) else {}
async def _round_executor(browser_messages: List[Dict[str, str]]) -> str:
worker_fn = lambda: _extract_assistant_content(
_execute_browser_non_stream_for_route_domain(
browser=browser,
route_domain=route_domain,
messages=browser_messages,
request_id=request_id,
preset_name=body.preset_name,
stop_checker=stop_checker,
allocation_mode=allocation_mode,
route_group_id=route_group_id,
requested_model=body.model,
**({"workflow_variables": body.workflow_variables} if body.workflow_variables else {}),
)
)
if isinstance(tracked_worker_state.get("ctx"), RequestContext):
return await _run_tracked_route_tool_calling_worker(
worker_fn,
ctx=tracked_worker_state["ctx"],
worker_state=tracked_worker_state,
label=f"{request_id[:8]}-route-round",
)
return await asyncio.to_thread(worker_fn)
parsed = await complete_tool_calling_roundtrip_async(
messages=body.messages,
tools=tools,
tool_choice=tool_choice,
parallel_tool_calls=(
False
if legacy_function_call and body.parallel_tool_calls is None
else body.parallel_tool_calls
),
round_executor=_round_executor,
stop_checker=stop_checker,
)
if not parsed.get("tool_calls"):
parsed = dict(parsed)
parsed["content"] = apply_stop_sequences_to_text(
str(parsed.get("content") or ""),
body.stop,
)
return build_tool_completion_response(
body.model,
parsed,
legacy_function_call=legacy_function_call,
)
async def _run_tool_calling_async_for_exact_url(
browser,
exact_url: str,
body: ChatRequest,
request_id: str,
stop_checker=None,
worker_state: Optional[Dict[str, Any]] = None,
resolved_tab_index: Optional[int] = None,
) -> Dict[str, Any]:
legacy_function_call = bool(body.functions) and not bool(body.tools)
tools, tool_choice = normalize_tool_request(
tools=body.tools,
tool_choice=body.tool_choice,
functions=body.functions,
function_call=body.function_call,
)
try:
logger.debug(
"[exact_url] 请求消息摘要: "
f"{summarize_messages_for_debug(body.messages)}"
)
except Exception as e:
logger.debug(f"[exact_url] 请求消息摘要生成失败: {e}")
tracked_worker_state = worker_state if isinstance(worker_state, dict) else {}
async def _round_executor(browser_messages: List[Dict[str, str]]) -> str:
worker_fn = lambda: _extract_assistant_content(
_execute_browser_non_stream_for_exact_url(
browser=browser,
exact_url=exact_url,
messages=browser_messages,
request_id=request_id,
preset_name=body.preset_name,
stop_checker=stop_checker,
resolved_tab_index=resolved_tab_index,
requested_model=body.model,
**({"workflow_variables": body.workflow_variables} if body.workflow_variables else {}),
)
)
if isinstance(tracked_worker_state.get("ctx"), RequestContext):
return await _run_tracked_route_tool_calling_worker(
worker_fn,
ctx=tracked_worker_state["ctx"],
worker_state=tracked_worker_state,
label=f"{request_id[:8]}-url-round",
)
return await asyncio.to_thread(worker_fn)
parsed = await complete_tool_calling_roundtrip_async(
messages=body.messages,
tools=tools,
tool_choice=tool_choice,
parallel_tool_calls=(
False
if legacy_function_call and body.parallel_tool_calls is None
else body.parallel_tool_calls
),
round_executor=_round_executor,
stop_checker=stop_checker,
)
if not parsed.get("tool_calls"):
parsed = dict(parsed)
parsed["content"] = apply_stop_sequences_to_text(
str(parsed.get("content") or ""),
body.stop,
)
return build_tool_completion_response(
body.model,
parsed,
legacy_function_call=legacy_function_call,
)
async def _complete_tool_calling_with_tab_index(
request: Request,
body: ChatRequest,
ctx: RequestContext,
tab_index: int,
) -> Dict[str, Any]:
disconnect_task = None
worker_state: Dict[str, Any] = {"thread": None, "label": None, "ctx": ctx}
try:
disconnect_task = asyncio.create_task(
watch_client_disconnect(request, ctx, check_interval=0.3)
)
browser = get_browser(auto_connect=False)
request_manager.start_request(ctx)
response = await _run_tool_calling_async_for_tab(
browser,
tab_index,
body,
ctx.request_id,
ctx.should_stop,
worker_state=worker_state,
)
request_manager.capture_response_payload(ctx, response)
if not ctx.should_stop() and ctx.status == RequestStatus.RUNNING:
ctx.mark_completed()
return response
except _RouteToolCallingExecutionCancelled:
cancel_reason = _get_route_tool_calling_cancel_reason(ctx)
if cancel_reason == "absolute_request_timeout":
request_manager.capture_error(ctx, "请求执行超过最大绝对超时", code="absolute_request_timeout")
ctx.mark_failed("absolute_request_timeout")
raise RuntimeError("absolute_request_timeout")
if not ctx.should_stop():
ctx.request_cancel(cancel_reason or "tool_calling_cancelled")
raise asyncio.CancelledError()
except asyncio.CancelledError:
ctx.request_cancel("coroutine_cancelled")
raise
except Exception as e:
logger.error(f"tool_calling_failed(tab={tab_index}): {e}")
request_manager.capture_error(ctx, e, code=getattr(e, "code", "tool_calling_failed"))
ctx.mark_failed(str(getattr(e, "code", None) or e))
raise
finally:
with request_finalization(ctx):
if disconnect_task:
disconnect_task.cancel()
try:
await disconnect_task
except asyncio.CancelledError:
pass
worker_thread = worker_state.get("thread")
await _cleanup_route_worker_thread(worker_thread, ctx)
worker_state["thread"] = None
worker_state["label"] = None
async def _complete_tool_calling_with_route_domain(
request: Request,
body: ChatRequest,
ctx: RequestContext,
route_domain: str,
allocation_mode: Optional[str] = None,
route_group_id: Optional[str] = None,
) -> Dict[str, Any]:
disconnect_task = None
worker_state: Dict[str, Any] = {"thread": None, "label": None, "ctx": ctx}
try:
disconnect_task = asyncio.create_task(
watch_client_disconnect(request, ctx, check_interval=0.3)
)
browser = get_browser(auto_connect=False)
request_manager.start_request(ctx)
response = await _run_tool_calling_async_for_route_domain(
browser,
route_domain,
body,
ctx.request_id,
ctx.should_stop,
worker_state=worker_state,
allocation_mode=allocation_mode,
route_group_id=route_group_id,
)
request_manager.capture_response_payload(ctx, response)
if not ctx.should_stop() and ctx.status == RequestStatus.RUNNING:
ctx.mark_completed()
return response
except _RouteToolCallingExecutionCancelled:
cancel_reason = _get_route_tool_calling_cancel_reason(ctx)
if cancel_reason == "absolute_request_timeout":
request_manager.capture_error(ctx, "请求执行超过最大绝对超时", code="absolute_request_timeout")
ctx.mark_failed("absolute_request_timeout")
raise RuntimeError("absolute_request_timeout")
if not ctx.should_stop():
ctx.request_cancel(cancel_reason or "tool_calling_cancelled")
raise asyncio.CancelledError()
except asyncio.CancelledError:
ctx.request_cancel("coroutine_cancelled")
raise
except Exception as e:
route_label = f"route_group={route_group_id}" if route_group_id else f"route_domain={route_domain}"
logger.error(f"tool_calling_failed({route_label}): {e}")
request_manager.capture_error(ctx, e, code=getattr(e, "code", "tool_calling_failed"))
ctx.mark_failed(str(getattr(e, "code", None) or e))
raise
finally:
with request_finalization(ctx):
if disconnect_task:
disconnect_task.cancel()
try:
await disconnect_task
except asyncio.CancelledError:
pass
worker_thread = worker_state.get("thread")
await _cleanup_route_worker_thread(worker_thread, ctx)
worker_state["thread"] = None
worker_state["label"] = None
async def _complete_tool_calling_with_exact_url(
request: Request,
body: ChatRequest,
ctx: RequestContext,
exact_url: str,
resolved_tab_index: Optional[int] = None,
) -> Dict[str, Any]:
disconnect_task = None
worker_state: Dict[str, Any] = {"thread": None, "label": None, "ctx": ctx}
try:
disconnect_task = asyncio.create_task(
watch_client_disconnect(request, ctx, check_interval=0.3)
)
browser = get_browser(auto_connect=False)
request_manager.start_request(ctx)
response = await _run_tool_calling_async_for_exact_url(
browser,
exact_url,
body,
ctx.request_id,
ctx.should_stop,
worker_state=worker_state,
resolved_tab_index=resolved_tab_index,
)
request_manager.capture_response_payload(ctx, response)
if not ctx.should_stop() and ctx.status == RequestStatus.RUNNING:
ctx.mark_completed()
return response
except _RouteToolCallingExecutionCancelled:
cancel_reason = _get_route_tool_calling_cancel_reason(ctx)
if cancel_reason == "absolute_request_timeout":
request_manager.capture_error(ctx, "请求执行超过最大绝对超时", code="absolute_request_timeout")
ctx.mark_failed("absolute_request_timeout")
raise RuntimeError("absolute_request_timeout")
if not ctx.should_stop():
ctx.request_cancel(cancel_reason or "tool_calling_cancelled")
raise asyncio.CancelledError()
except asyncio.CancelledError:
ctx.request_cancel("coroutine_cancelled")
raise
except Exception as e:
logger.error(f"tool_calling_failed(exact_url={exact_url}): {e}")
request_manager.capture_error(ctx, e, code=getattr(e, "code", "tool_calling_failed"))
ctx.mark_failed(str(getattr(e, "code", None) or e))
raise
finally:
with request_finalization(ctx):
if disconnect_task:
disconnect_task.cancel()
try:
await disconnect_task
except asyncio.CancelledError:
pass
worker_thread = worker_state.get("thread")
await _cleanup_route_worker_thread(worker_thread, ctx)
worker_state["thread"] = None
worker_state["label"] = None
async def _non_stream_tool_calling_with_tab_index(
request: Request,
body: ChatRequest,
ctx: RequestContext,
tab_index: int,
) -> JSONResponse:
try:
response = await _complete_tool_calling_with_tab_index(request, body, ctx, tab_index)
return JSONResponse(content=response)
except Exception as e:
message, code = _format_route_tool_calling_error(e)
ctx.mark_failed(message)
request_manager.capture_error(ctx, message, code=code)
request_manager.finish_request(ctx, success=False)
return JSONResponse(
content={
"error": {
"message": message,
"type": "execution_error",
"code": code,
}
},
status_code=500,
)
async def _non_stream_tool_calling_with_route_domain(
request: Request,
body: ChatRequest,
ctx: RequestContext,
route_domain: str,
allocation_mode: Optional[str] = None,
route_group_id: Optional[str] = None,
) -> JSONResponse:
try:
response = await _complete_tool_calling_with_route_domain(
request,
body,
ctx,
route_domain,
allocation_mode=allocation_mode,
route_group_id=route_group_id,
)
return JSONResponse(content=response)
except Exception as e:
message, code = _format_route_tool_calling_error(e)
ctx.mark_failed(message)
request_manager.capture_error(ctx, message, code=code)
request_manager.finish_request(ctx, success=False)
return JSONResponse(
content={
"error": {
"message": message,
"type": "execution_error",
"code": code,
}
},
status_code=500,
)
async def _non_stream_tool_calling_with_exact_url(
request: Request,
body: ChatRequest,
ctx: RequestContext,
exact_url: str,
resolved_tab_index: Optional[int] = None,
) -> JSONResponse:
try:
response = await _complete_tool_calling_with_exact_url(
request,
body,
ctx,
exact_url,
resolved_tab_index=resolved_tab_index,
)
return JSONResponse(content=response)
except Exception as e:
message, code = _format_route_tool_calling_error(e)
ctx.mark_failed(message)
request_manager.capture_error(ctx, message, code=code)
request_manager.finish_request(ctx, success=False)
return JSONResponse(
content={
"error": {
"message": message,
"type": "execution_error",
"code": code,
}
},
status_code=500,
)
async def _stream_tool_calling_with_tab_index(
request: Request,
body: ChatRequest,
ctx: RequestContext,
tab_index: int,
):
response_task = None
keepalive_id = f"chatcmpl-{ctx.request_id}"
try:
yield SSEFormatter.pack_keepalive(
model=body.model,
completion_id=keepalive_id,
)
response_task = asyncio.create_task(
_complete_tool_calling_with_tab_index(request, body, ctx, tab_index)
)
while True:
try:
response = await asyncio.wait_for(
asyncio.shield(response_task),
timeout=SSE_HEARTBEAT_INTERVAL,
)
break
except asyncio.TimeoutError:
yield SSEFormatter.pack_keepalive(
model=body.model,
completion_id=keepalive_id,
)
message = response.get("choices", [{}])[0].get("message", {}) or {}
legacy_function_call = bool(body.functions) and not bool(body.tools)
response_tool_calls = message.get("tool_calls") or []
if legacy_function_call and not response_tool_calls and isinstance(message.get("function_call"), dict):
response_tool_calls = [
{"type": "function", "function": message["function_call"]}
]
parsed = {
"content": message.get("content"),
"tool_calls": response_tool_calls,
}
for chunk in _iter_stream_chunks_with_optional_usage(
body,
iter_tool_stream_chunks(
body.model,
parsed,
legacy_function_call=legacy_function_call,
),
):
if await request.is_disconnected():
ctx.request_cancel("client_disconnected")
break
yield chunk
await asyncio.sleep(0)
except GeneratorExit:
if not ctx.is_terminal():
ctx.request_cancel("stream_closed")
raise
except asyncio.CancelledError:
ctx.request_cancel("coroutine_cancelled")
raise
except Exception as e:
message, code = _format_route_tool_calling_error(e)
ctx.mark_failed(message)
request_manager.capture_error(ctx, message, code=code)
request_manager.finish_request(ctx, success=False)
yield _pack_error(message, code)
yield _pack_done()
finally:
with request_finalization(ctx):
if response_task is not None and not response_task.done():
response_task.cancel()
try:
await response_task
except asyncio.CancelledError:
pass
async def _stream_tool_calling_with_route_domain(
request: Request,
body: ChatRequest,
ctx: RequestContext,
route_domain: str,
allocation_mode: Optional[str] = None,
route_group_id: Optional[str] = None,
):
response_task = None
keepalive_id = f"chatcmpl-{ctx.request_id}"
try:
yield SSEFormatter.pack_keepalive(
model=body.model,
completion_id=keepalive_id,
)
response_task = asyncio.create_task(
_complete_tool_calling_with_route_domain(
request,
body,
ctx,
route_domain,
allocation_mode=allocation_mode,
route_group_id=route_group_id,
)
)
while True:
try:
response = await asyncio.wait_for(
asyncio.shield(response_task),
timeout=SSE_HEARTBEAT_INTERVAL,
)
break
except asyncio.TimeoutError:
yield SSEFormatter.pack_keepalive(
model=body.model,
completion_id=keepalive_id,
)
message = response.get("choices", [{}])[0].get("message", {}) or {}
legacy_function_call = bool(body.functions) and not bool(body.tools)
response_tool_calls = message.get("tool_calls") or []
if legacy_function_call and not response_tool_calls and isinstance(message.get("function_call"), dict):
response_tool_calls = [
{"type": "function", "function": message["function_call"]}
]
parsed = {
"content": message.get("content"),
"tool_calls": response_tool_calls,
}
for chunk in _iter_stream_chunks_with_optional_usage(
body,
iter_tool_stream_chunks(
body.model,
parsed,
legacy_function_call=legacy_function_call,
),
):
if await request.is_disconnected():
ctx.request_cancel("client_disconnected")
break
yield chunk
await asyncio.sleep(0)
except GeneratorExit:
if not ctx.is_terminal():
ctx.request_cancel("stream_closed")
raise
except asyncio.CancelledError:
ctx.request_cancel("coroutine_cancelled")
raise
except Exception as e:
message, code = _format_route_tool_calling_error(e)
ctx.mark_failed(message)
request_manager.capture_error(ctx, message, code=code)
request_manager.finish_request(ctx, success=False)
yield _pack_error(message, code)
yield _pack_done()
finally:
with request_finalization(ctx):
if response_task is not None and not response_task.done():
response_task.cancel()
try:
await response_task
except asyncio.CancelledError:
pass
async def _stream_tool_calling_with_exact_url(
request: Request,
body: ChatRequest,
ctx: RequestContext,
exact_url: str,
resolved_tab_index: Optional[int] = None,
):
response_task = None
keepalive_id = f"chatcmpl-{ctx.request_id}"
try:
yield SSEFormatter.pack_keepalive(
model=body.model,
completion_id=keepalive_id,
)
response_task = asyncio.create_task(
_complete_tool_calling_with_exact_url(
request,
body,
ctx,
exact_url,
resolved_tab_index=resolved_tab_index,
)
)
while True:
try:
response = await asyncio.wait_for(
asyncio.shield(response_task),
timeout=SSE_HEARTBEAT_INTERVAL,
)
break
except asyncio.TimeoutError:
yield SSEFormatter.pack_keepalive(
model=body.model,
completion_id=keepalive_id,
)
message = response.get("choices", [{}])[0].get("message", {}) or {}
legacy_function_call = bool(body.functions) and not bool(body.tools)
response_tool_calls = message.get("tool_calls") or []
if legacy_function_call and not response_tool_calls and isinstance(message.get("function_call"), dict):
response_tool_calls = [
{"type": "function", "function": message["function_call"]}
]
parsed = {
"content": message.get("content"),
"tool_calls": response_tool_calls,
}
for chunk in _iter_stream_chunks_with_optional_usage(
body,
iter_tool_stream_chunks(
body.model,
parsed,
legacy_function_call=legacy_function_call,
),
):
if await request.is_disconnected():
ctx.request_cancel("client_disconnected")
break
yield chunk
await asyncio.sleep(0)
except GeneratorExit:
if not ctx.is_terminal():
ctx.request_cancel("stream_closed")
raise
except asyncio.CancelledError:
ctx.request_cancel("coroutine_cancelled")
raise
except Exception as e:
message, code = _format_route_tool_calling_error(e)
ctx.mark_failed(message)
request_manager.capture_error(ctx, message, code=code)
request_manager.finish_request(ctx, success=False)
yield _pack_error(message, code)
yield _pack_done()
finally:
with request_finalization(ctx):
if response_task is not None and not response_task.done():
response_task.cancel()
try:
await response_task
except asyncio.CancelledError:
pass
# ================= 预设管理 API =================
class PresetRequest(BaseModel):
"""预设操作请求"""
preset_name: str = Field(..., min_length=1, max_length=50)
class CreatePresetRequest(BaseModel):
"""创建预设请求"""
new_name: str = Field(..., min_length=1, max_length=50)
source_name: Optional[str] = Field(default=None)
class RenamePresetRequest(BaseModel):
"""重命名预设请求"""
old_name: str = Field(..., min_length=1, max_length=50)
new_name: str = Field(..., min_length=1, max_length=50)
class SetDefaultPresetRequest(BaseModel):
"""设置默认预设请求"""
preset_name: str = Field(..., min_length=1, max_length=50)
class TerminateTabRequest(BaseModel):
"""终止标签页当前任务请求"""
reason: str = Field(default="manual_terminate_from_tab_pool", max_length=120)
clear_page: bool = Field(default=True)
scope: str = Field(default="task", max_length=20)
expected_session_id: str = Field(default="", max_length=200)
expected_task_id: str = Field(default="", max_length=200)
@router.put("/api/tab-pool/tabs/{tab_index}/preset")
async def set_tab_preset(
tab_index: int,
body: PresetRequest,
authenticated: bool = Depends(verify_auth)
):
"""为指定标签页设置预设"""
try:
browser = get_browser(auto_connect=False)
tab_info = _get_tab_info_by_index(browser, tab_index)
if tab_info is None:
raise HTTPException(status_code=404, detail=f"标签页 #{tab_index} 不存在")
preset_value = None if body.preset_name == FOLLOW_DEFAULT_PRESET else body.preset_name
domain = str(tab_info.get("current_domain") or tab_info.get("route_domain") or "").strip()
if preset_value:
try:
from app.services.config_engine import config_engine
available = config_engine.list_presets(domain)
if available and preset_value not in available:
# 尝试别名容错
aliased = [p for p in available if p == f"预设_{preset_value}" or (preset_value.startswith("预设_") and p == preset_value[3:])]
if aliased:
preset_value = aliased[0]
else:
raise HTTPException(status_code=400, detail=f"预设 '{preset_value}' 在站点 '{domain}' 中不存在")
except HTTPException:
raise
except Exception as e:
logger.debug(f"校验站点预设可用性异常(已忽略): {e}")
success = browser.tab_pool.set_tab_preset(tab_index, preset_value)
if not success:
raise HTTPException(status_code=404, detail=f"标签页 #{tab_index} 不存在")
# 检查是否开启了按 URL 自动记忆预设(需过滤空白页及非网页)
auto_remember = _get_tab_pool_auto_remember_url_presets(browser.tab_pool)
tab_url = str(tab_info.get("url") or "").strip()
url_key = normalize_exact_tab_url(tab_url)
is_valid_web_url = bool(url_key and not _should_skip_pool_url(tab_url) and extract_remote_site_domain(tab_url))
if auto_remember and is_valid_web_url:
with _preset_overrides_lock:
overrides = _read_preset_overrides_unlocked()
if preset_value:
overrides["urls"][url_key] = preset_value
else:
overrides["urls"].pop(url_key, None)
overrides = _write_preset_overrides_unlocked(overrides)
_sync_tab_pool_preset_overrides(overrides)
preset_label = "跟随站点默认预设" if preset_value is None else body.preset_name
return {"success": True, "message": f"标签页 #{tab_index} 已切换到预设: {preset_label}"}
except HTTPException:
raise
except Exception as e:
logger.error(f"设置标签页预设失败: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/api/tab-pool/tabs/{tab_index}/terminate")
async def terminate_tab_task(
tab_index: int,
body: TerminateTabRequest,
authenticated: bool = Depends(verify_auth)
):
"""按标签页编号终止当前任务并释放占用。"""
if tab_index < 1:
raise HTTPException(status_code=400, detail="标签页编号必须大于 0")
try:
browser = get_browser(auto_connect=False)
result = await asyncio.to_thread(
browser.tab_pool.terminate_by_index,
tab_index,
reason=(body.reason or "manual_terminate_from_tab_pool"),
clear_page=bool(body.clear_page),
scope=(body.scope or "task"),
expected_session_id=body.expected_session_id,
expected_task_id=body.expected_task_id,
)
if not result.get("ok"):
if result.get("error") == "tab_not_found":
raise HTTPException(status_code=404, detail=f"标签页 #{tab_index} 不存在")
if result.get("error") in {
"session_ownership_changed",
"task_ownership_changed",
}:
raise HTTPException(
status_code=409,
detail="标签页任务已经变化,请刷新后重试",
)
raise HTTPException(status_code=400, detail=result.get("error", "terminate_failed"))
return result
except HTTPException:
raise
except Exception as e:
logger.error(f"终止标签页任务失败: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.get("/api/presets/{domain}")
async def get_site_presets(
domain: str,
authenticated: bool = Depends(verify_auth)
):
"""获取指定站点的所有预设"""
try:
from app.services.config_engine import config_engine
presets = config_engine.list_presets(domain)
default_preset = config_engine.get_default_preset(domain)
return {"domain": domain, "presets": presets, "default_preset": default_preset}
except Exception as e:
logger.error(f"获取预设列表失败: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/api/presets/{domain}")
async def create_site_preset(
domain: str,
body: CreatePresetRequest,
authenticated: bool = Depends(verify_auth)
):
"""为站点创建新预设(克隆自现有预设)"""
try:
from app.services.config_engine import config_engine
success = config_engine.create_preset(domain, body.new_name, body.source_name)
if success:
return {"success": True, "message": f"预设 '{body.new_name}' 已创建"}
else:
raise HTTPException(status_code=400, detail="创建失败(预设已存在或站点不存在)")
except HTTPException:
raise
except Exception as e:
logger.error(f"创建预设失败: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.put("/api/presets/{domain}/rename")
async def rename_site_preset(
domain: str,
body: RenamePresetRequest,
authenticated: bool = Depends(verify_auth)
):
"""重命名指定预设"""
try:
from app.services.config_engine import config_engine
success = config_engine.rename_preset(domain, body.old_name, body.new_name)
if success:
return {
"success": True,
"message": f"预设 '{body.old_name}' 已重命名为 '{body.new_name}'",
}
else:
raise HTTPException(status_code=400, detail="重命名失败(预设不存在或新名称已存在)")
except HTTPException:
raise
except Exception as e:
logger.error(f"重命名预设失败: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.put("/api/presets/{domain}/default")
async def set_site_default_preset(
domain: str,
body: SetDefaultPresetRequest,
authenticated: bool = Depends(verify_auth)
):
"""设置站点默认预设(本地覆盖)"""
try:
from app.services.config_engine import config_engine
success = config_engine.set_default_preset(domain, body.preset_name)
if success:
return {
"success": True,
"message": f"默认预设已设置为 '{body.preset_name}'(本地覆盖)",
"domain": domain,
"default_preset": body.preset_name
}
else:
raise HTTPException(status_code=400, detail="设置失败(站点或预设不存在)")
except HTTPException:
raise
except Exception as e:
logger.error(f"设置默认预设失败: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.delete("/api/presets/{domain}/{preset_name}")
async def delete_site_preset(
domain: str,
preset_name: str,
authenticated: bool = Depends(verify_auth)
):
"""删除指定预设(不能删除最后一个)"""
try:
from app.services.config_engine import config_engine
success = config_engine.delete_preset(domain, preset_name)
if success:
return {"success": True, "message": f"预设 '{preset_name}' 已删除"}
else:
raise HTTPException(status_code=400, detail="删除失败(预设不存在或是最后一个)")
except HTTPException:
raise
except Exception as e:
logger.error(f"删除预设失败: {e}")
raise HTTPException(status_code=500, detail=str(e))
def _pack_error(
message: str,
code: str = "error",
*,
error_type: str = "execution_error",
status_code: Optional[int] = None,
retryable: Optional[bool] = None,
param: Optional[str] = None,
extra: Optional[Dict[str, Any]] = None,
) -> str:
"""打包 SSE 错误"""
return SSEFormatter.pack_error(
message=message,
error_type=error_type,
code=code,
status_code=status_code,
retryable=retryable,
param=param,
extra=extra,
)
def _pack_done() -> str:
"""打包 SSE 结束标记"""
return "data: [DONE]\n\n"
def _pack_error_done(message: str, code: str = "error") -> str:
return f"{_pack_error(message, code)}{_pack_done()}"