feat(c6): harden entertainment capability boundaries
这个提交存在于:
父节点
d26ba395b1
当前提交
d006b85773
共有 10 个文件被更改,包括 1259 次插入 和 19 次删除
62
atrelay.py
62
atrelay.py
|
|
@ -1022,6 +1022,68 @@ class AtRelayMixin:
|
|||
+ "\n这些只用于理解对方为什么接话或道谢;不要主动复述工具名、内部记录或没必要说明来源。"
|
||||
)
|
||||
|
||||
def _atrelay_tool_authorization(self, event: AstrMessageEvent | None) -> tuple[bool, str]:
|
||||
"""Require the configured private-companion owner for relay actions."""
|
||||
requester_id = ""
|
||||
if event is not None:
|
||||
try:
|
||||
requester_id = self._permission_identity_id(event.get_sender_id())
|
||||
except Exception:
|
||||
requester_id = ""
|
||||
checker = getattr(self, "_is_private_companion_owner_user_id", None)
|
||||
allowed = bool(requester_id and callable(checker) and checker(requester_id))
|
||||
if not allowed:
|
||||
logger.info(
|
||||
"[PrivateCompanion] relay authorization denied: sender=%s umo=%s",
|
||||
requester_id or "-",
|
||||
_single_line(getattr(event, "unified_msg_origin", ""), 120),
|
||||
)
|
||||
return allowed, requester_id
|
||||
|
||||
def _atrelay_known_group_ids(self) -> set[str]:
|
||||
known: set[str] = set()
|
||||
data = getattr(self, "data", None)
|
||||
if not isinstance(data, dict):
|
||||
return known
|
||||
for key in ("groups", "worldbook_group_profiles"):
|
||||
section = data.get(key)
|
||||
if not isinstance(section, dict):
|
||||
continue
|
||||
for raw_id, item in section.items():
|
||||
gid = _single_line(item.get("group_id"), 40) if isinstance(item, dict) else ""
|
||||
gid = gid or _single_line(raw_id, 40)
|
||||
if gid:
|
||||
known.add(gid)
|
||||
return known
|
||||
|
||||
def _atrelay_target_group_allowed(self, group_id: Any, event: AstrMessageEvent | None = None) -> str:
|
||||
target = _single_line(group_id, 40)
|
||||
if not target:
|
||||
return "发送失败:群号格式不正确"
|
||||
blacklist_getter = getattr(self, "_configured_group_blacklist_ids", None)
|
||||
blacklist = set(blacklist_getter()) if callable(blacklist_getter) else set()
|
||||
if target in blacklist:
|
||||
logger.info("[PrivateCompanion] relay group denied by blacklist: group=%s", target)
|
||||
return "发送失败:目标群不在允许转述范围内"
|
||||
whitelist_getter = getattr(self, "_configured_group_ids", None)
|
||||
allowed = set(whitelist_getter()) if callable(whitelist_getter) else set()
|
||||
allowed |= self._atrelay_known_group_ids()
|
||||
extractor = getattr(self, "_extract_group_id_from_event", None)
|
||||
if event is not None and callable(extractor):
|
||||
try:
|
||||
current_group = _single_line(extractor(event), 40)
|
||||
except Exception:
|
||||
current_group = ""
|
||||
if current_group:
|
||||
allowed.add(current_group)
|
||||
if not allowed:
|
||||
logger.warning("[PrivateCompanion] relay group allow-set empty; compatibility pass: group=%s", target)
|
||||
return ""
|
||||
if target not in allowed:
|
||||
logger.info("[PrivateCompanion] relay group denied: group=%s", target)
|
||||
return "发送失败:目标群不在允许转述范围内"
|
||||
return ""
|
||||
|
||||
def _note_atrelay_send(
|
||||
self,
|
||||
kind: str,
|
||||
|
|
|
|||
|
|
@ -3321,11 +3321,41 @@ class CommandHandlersMixin:
|
|||
clean = "reference"
|
||||
return f"{clean}_{int(_now_ts() * 1000)}_{uuid.uuid4().hex[:8]}"
|
||||
|
||||
def _photo_reference_copy_local_file(self, source_path: Path, *, stem: str = "reference") -> str:
|
||||
def _photo_reference_path_within_data_dir(self, path: Path) -> bool:
|
||||
"""Return whether a reference path is inside this plugin's data tree."""
|
||||
try:
|
||||
root = Path(self.data_dir).resolve()
|
||||
resolved = path.resolve()
|
||||
except Exception:
|
||||
return False
|
||||
try:
|
||||
return resolved.is_relative_to(root)
|
||||
except AttributeError: # Python < 3.9
|
||||
return str(resolved) == str(root) or str(resolved).startswith(str(root) + os.sep)
|
||||
|
||||
def _photo_reference_copy_local_file(
|
||||
self,
|
||||
source_path: Path,
|
||||
*,
|
||||
stem: str = "reference",
|
||||
trusted: bool = True,
|
||||
) -> str:
|
||||
"""Copy a reference image into the plugin-owned reference directory.
|
||||
|
||||
Untrusted/model-controlled sources may only read files below the plugin
|
||||
data directory. Explicit administrator configuration keeps the legacy
|
||||
trusted behavior for paths outside that directory.
|
||||
"""
|
||||
try:
|
||||
resolved = source_path.resolve()
|
||||
except Exception:
|
||||
resolved = source_path
|
||||
if not trusted and not self._photo_reference_path_within_data_dir(resolved):
|
||||
logger.warning(
|
||||
"[PrivateCompanion] 参考图越权本地路径已拒绝: %s",
|
||||
_single_line(str(resolved), 200),
|
||||
)
|
||||
return ""
|
||||
if not resolved.exists() or not resolved.is_file():
|
||||
return ""
|
||||
suffix = resolved.suffix.lower()
|
||||
|
|
@ -3358,7 +3388,20 @@ class CommandHandlersMixin:
|
|||
except Exception:
|
||||
return ""
|
||||
|
||||
async def _photo_reference_source_to_stable_path(self, source: str, *, stem: str = "reference", event: AstrMessageEvent | None = None) -> str:
|
||||
async def _photo_reference_source_to_stable_path(
|
||||
self,
|
||||
source: str,
|
||||
*,
|
||||
stem: str = "reference",
|
||||
event: AstrMessageEvent | None = None,
|
||||
trusted: bool = True,
|
||||
) -> str:
|
||||
"""Normalize a reference source into a stable plugin-local path.
|
||||
|
||||
Model-controlled sources are restricted to plugin data paths and public
|
||||
remote hosts; administrator-configured sources retain the legacy trust
|
||||
boundary.
|
||||
"""
|
||||
text = str(source or "").strip()
|
||||
if not text:
|
||||
return ""
|
||||
|
|
@ -3369,15 +3412,45 @@ class CommandHandlersMixin:
|
|||
downloader = getattr(self, "_persist_private_remote_image_source", None)
|
||||
if callable(downloader):
|
||||
try:
|
||||
downloaded = await downloader(text, self._photo_reference_image_dir(), self._photo_reference_stem(f"{stem}_remote"))
|
||||
downloaded = await downloader(
|
||||
text,
|
||||
self._photo_reference_image_dir(),
|
||||
self._photo_reference_stem(f"{stem}_remote"),
|
||||
public_hosts_only=not trusted,
|
||||
)
|
||||
except TypeError:
|
||||
# Older mixins may not accept the security keyword; reject
|
||||
# untrusted input instead of silently downgrading it.
|
||||
if not trusted:
|
||||
return ""
|
||||
try:
|
||||
downloaded = await downloader(
|
||||
text,
|
||||
self._photo_reference_image_dir(),
|
||||
self._photo_reference_stem(f"{stem}_remote"),
|
||||
)
|
||||
except Exception:
|
||||
downloaded = ""
|
||||
except Exception:
|
||||
downloaded = ""
|
||||
if downloaded:
|
||||
return self._photo_reference_copy_local_file(Path(downloaded), stem=stem) or downloaded
|
||||
copied = self._photo_reference_copy_local_file(
|
||||
Path(downloaded),
|
||||
stem=stem,
|
||||
trusted=trusted,
|
||||
)
|
||||
if copied:
|
||||
return copied
|
||||
if trusted:
|
||||
return str(downloaded)
|
||||
return ""
|
||||
local_text = text[len("file://"):] if text.startswith("file://") else text
|
||||
try:
|
||||
copied = self._photo_reference_copy_local_file(Path(local_text), stem=stem)
|
||||
copied = self._photo_reference_copy_local_file(
|
||||
Path(local_text),
|
||||
stem=stem,
|
||||
trusted=trusted,
|
||||
)
|
||||
if copied:
|
||||
return copied
|
||||
except (OSError, ValueError):
|
||||
|
|
@ -3389,7 +3462,12 @@ class CommandHandlersMixin:
|
|||
except Exception:
|
||||
resolved = ""
|
||||
if resolved and resolved != text:
|
||||
return await self._photo_reference_source_to_stable_path(resolved, stem=stem, event=event)
|
||||
return await self._photo_reference_source_to_stable_path(
|
||||
resolved,
|
||||
stem=stem,
|
||||
event=event,
|
||||
trusted=trusted,
|
||||
)
|
||||
return ""
|
||||
|
||||
async def _photo_reference_sources_from_current_event(self, event: AstrMessageEvent, user_id: str) -> list[str]:
|
||||
|
|
|
|||
53
helpers.py
53
helpers.py
|
|
@ -3,11 +3,14 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import ipaddress
|
||||
import socket
|
||||
import time
|
||||
import unicodedata
|
||||
import zoneinfo
|
||||
from datetime import date, datetime
|
||||
from typing import Any
|
||||
from urllib.parse import urlparse
|
||||
|
||||
_today_key_timezone = ""
|
||||
|
||||
|
|
@ -94,6 +97,56 @@ def _single_line(text: Any, limit: int = 80) -> str:
|
|||
return normalized[:limit]
|
||||
|
||||
|
||||
def _url_host_is_public(url: Any) -> bool:
|
||||
"""Return True only for http(s) URLs whose resolved hosts are all public.
|
||||
|
||||
Model-controlled reference images and remote image URLs must not be able to
|
||||
probe loopback, private-network, link-local, reserved, multicast, or cloud
|
||||
metadata endpoints. Resolution failures and any ambiguous address are
|
||||
rejected conservatively. This is a pre-download check; it cannot remove
|
||||
every DNS rebinding (TOCTOU) risk by itself.
|
||||
"""
|
||||
text = str(url or "").strip()
|
||||
if not re.match(r"^https?://", text, flags=re.I):
|
||||
return False
|
||||
try:
|
||||
host = urlparse(text).hostname or ""
|
||||
except ValueError:
|
||||
return False
|
||||
if not host:
|
||||
return False
|
||||
try:
|
||||
infos = socket.getaddrinfo(host, None)
|
||||
except (socket.gaierror, UnicodeError, OSError, ValueError):
|
||||
return False
|
||||
addresses = {
|
||||
info[4][0]
|
||||
for info in infos
|
||||
if isinstance(info, tuple) and len(info) >= 5 and info[4]
|
||||
}
|
||||
if not addresses:
|
||||
return False
|
||||
for address in addresses:
|
||||
try:
|
||||
ip = ipaddress.ip_address(str(address).split("%", 1)[0])
|
||||
except ValueError:
|
||||
return False
|
||||
mapped = getattr(ip, "ipv4_mapped", None)
|
||||
for candidate in (ip, mapped):
|
||||
if candidate is None:
|
||||
continue
|
||||
if (
|
||||
candidate.is_private
|
||||
or candidate.is_loopback
|
||||
or candidate.is_link_local
|
||||
or candidate.is_reserved
|
||||
or candidate.is_multicast
|
||||
or candidate.is_unspecified
|
||||
):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _path_text(value: Any, limit: int = 1000) -> str:
|
||||
"""Normalize a configured path without changing legal internal whitespace."""
|
||||
normalized = str(value or "").replace("\r", "").replace("\n", "").strip()
|
||||
|
|
|
|||
|
|
@ -1736,19 +1736,38 @@ class LlmToolActionsMixin:
|
|||
or kwargs.get("image_path")
|
||||
or kwargs.get("image_url"),
|
||||
1000,
|
||||
)
|
||||
).strip().strip('"').strip("'")
|
||||
if reference_path:
|
||||
# The model controls this value. Resolve it through the guarded
|
||||
# reference boundary before any downstream image provider sees it.
|
||||
resolver = getattr(self, "_photo_reference_source_to_stable_path", None)
|
||||
stable = ""
|
||||
if callable(resolver):
|
||||
try:
|
||||
stable = await resolver(reference_path, stem="tool", event=event)
|
||||
if stable:
|
||||
reference_path = stable
|
||||
stable = await resolver(reference_path, stem="tool", event=event, trusted=False)
|
||||
except Exception as exc:
|
||||
return json.dumps(
|
||||
{"status": "error", "message": f"参考图解析失败:{_single_line(exc, 160)}"},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
if not stable:
|
||||
logger.warning(
|
||||
"[PrivateCompanion] 生图参考图来源已拒绝: source=%s",
|
||||
_single_line(reference_path, 200),
|
||||
)
|
||||
return json.dumps(
|
||||
{
|
||||
"status": "invalid_reference",
|
||||
"success": False,
|
||||
"generated": False,
|
||||
"sent": False,
|
||||
"message": "这张参考图不能使用。参考图只支持当前消息里的图片、插件数据目录内的图片,或公网图片链接。",
|
||||
"must_not_claim_sent": True,
|
||||
"retryable": False,
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
reference_path = stable
|
||||
if intent_kind == "edit" and not reference_path:
|
||||
context_resolver = getattr(self, "_photo_reference_image_from_command_context", None)
|
||||
if callable(context_resolver):
|
||||
|
|
@ -3084,6 +3103,9 @@ class LlmToolActionsMixin:
|
|||
async def _pc_relay_message_impl(self, event: AstrMessageEvent, **kwargs) -> str:
|
||||
if not self.enable_atrelay_tools:
|
||||
return json.dumps({"status": "disabled", "message": "跨会话转述工具未启用"}, ensure_ascii=False)
|
||||
authorized, _requester_id = self._atrelay_tool_authorization(event)
|
||||
if not authorized:
|
||||
return json.dumps({"status": "forbidden", "message": "跨会话转述仅允许主人使用"}, ensure_ascii=False)
|
||||
destination_raw = _single_line(
|
||||
kwargs.get("destination")
|
||||
or kwargs.get("target_scope")
|
||||
|
|
@ -3173,6 +3195,9 @@ class LlmToolActionsMixin:
|
|||
if group_result.get("status") != "success":
|
||||
return json.dumps(group_result, ensure_ascii=False)
|
||||
group_id = _single_line(group_result.get("group_id"), 40)
|
||||
group_guard = self._atrelay_target_group_allowed(group_id, event)
|
||||
if group_guard:
|
||||
return json.dumps({"status": "forbidden", "message": group_guard}, ensure_ascii=False)
|
||||
send_text = await self._rewrite_atrelay_message_with_llm(
|
||||
event,
|
||||
destination="group",
|
||||
|
|
@ -3320,6 +3345,9 @@ class LlmToolActionsMixin:
|
|||
async def _pc_send_to_group_impl(self, event: AstrMessageEvent, **kwargs) -> str:
|
||||
if not self.enable_atrelay_tools:
|
||||
return "发送失败:跨群转述工具未启用"
|
||||
authorized, _requester_id = self._atrelay_tool_authorization(event)
|
||||
if not authorized:
|
||||
return "发送失败:跨会话转述仅允许主人使用"
|
||||
group_id = kwargs.get("group_id") or kwargs.get("group") or kwargs.get("target_group") or ""
|
||||
message = kwargs.get("message") or kwargs.get("text") or kwargs.get("content") or kwargs.get("msg") or ""
|
||||
at_user = kwargs.get("at_user") or kwargs.get("at") or kwargs.get("target_user") or kwargs.get("user_id") or ""
|
||||
|
|
@ -3329,6 +3357,9 @@ class LlmToolActionsMixin:
|
|||
relay_mode = kwargs.get("relay_mode") or kwargs.get("mode") or ""
|
||||
sensitive_confirmed = kwargs.get("sensitive_confirmed", kwargs.get("confirmed", False))
|
||||
target_group = _single_line(group_id, 40)
|
||||
group_guard = self._atrelay_target_group_allowed(target_group, event)
|
||||
if group_guard:
|
||||
return group_guard
|
||||
text = self._normalize_atrelay_text(message, limit=800)
|
||||
relay_mode_normalized = self._normalize_atrelay_relay_mode(relay_mode)
|
||||
if not target_group.isdigit():
|
||||
|
|
@ -3393,6 +3424,9 @@ class LlmToolActionsMixin:
|
|||
async def _pc_send_to_private_user_impl(self, event: AstrMessageEvent, **kwargs) -> str:
|
||||
if not self.enable_atrelay_tools:
|
||||
return "发送失败:跨群转述工具未启用"
|
||||
authorized, _requester_id = self._atrelay_tool_authorization(event)
|
||||
if not authorized:
|
||||
return "发送失败:跨会话转述仅允许主人使用"
|
||||
user_id = kwargs.get("user_id") or kwargs.get("qq") or kwargs.get("target_user") or kwargs.get("target") or ""
|
||||
message = kwargs.get("message") or kwargs.get("text") or kwargs.get("content") or kwargs.get("msg") or ""
|
||||
relay_mode = kwargs.get("relay_mode") or kwargs.get("mode") or ""
|
||||
|
|
@ -3507,6 +3541,9 @@ class LlmToolActionsMixin:
|
|||
async def _pc_schedule_group_relay_impl(self, event: AstrMessageEvent, **kwargs) -> str:
|
||||
if not self.enable_atrelay_tools:
|
||||
return "挂起失败:跨群转述工具未启用"
|
||||
authorized, _requester_id = self._atrelay_tool_authorization(event)
|
||||
if not authorized:
|
||||
return "挂起失败:跨会话转述仅允许主人使用"
|
||||
group_id = kwargs.get("group_id") or kwargs.get("group") or kwargs.get("target_group") or ""
|
||||
at_user = kwargs.get("at_user") or kwargs.get("target_user") or kwargs.get("user_id") or kwargs.get("name") or kwargs.get("nickname") or ""
|
||||
message = kwargs.get("message") or kwargs.get("text") or kwargs.get("content") or kwargs.get("msg") or ""
|
||||
|
|
@ -3514,6 +3551,9 @@ class LlmToolActionsMixin:
|
|||
sensitive_confirmed = kwargs.get("sensitive_confirmed", kwargs.get("confirmed", False))
|
||||
expire_hours = kwargs.get("expire_hours", kwargs.get("ttl_hours", 24))
|
||||
target_group = _single_line(group_id, 40) or self._extract_group_id_from_event(event)
|
||||
group_guard = self._atrelay_target_group_allowed(target_group, event)
|
||||
if group_guard:
|
||||
return group_guard.replace("发送失败", "挂起失败", 1)
|
||||
text = self._normalize_atrelay_text(message, limit=800)
|
||||
if not target_group.isdigit():
|
||||
return "挂起失败:群号格式不正确"
|
||||
|
|
|
|||
139
private_image.py
139
private_image.py
|
|
@ -10,6 +10,7 @@ import json
|
|||
import os
|
||||
import re
|
||||
import shutil
|
||||
import tempfile
|
||||
import time
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
|
@ -29,9 +30,28 @@ from astrbot.core import file_token_service
|
|||
from astrbot.core.astr_main_agent import MainAgentBuildConfig, build_main_agent
|
||||
from astrbot.core.utils.astrbot_path import get_astrbot_data_path
|
||||
|
||||
from .helpers import _missing_optional_model_dependency, _now_ts, _safe_float, _safe_int, _single_line, _strip_internal_message_blocks, _today_key
|
||||
from .helpers import _missing_optional_model_dependency, _now_ts, _safe_float, _safe_int, _single_line, _strip_internal_message_blocks, _today_key, _url_host_is_public
|
||||
from .segmented_message import split_plain_component_chain
|
||||
|
||||
|
||||
# 识图临时文件的存活上限。单次识图(含重试与多 provider 回退)远低于这个数量级,
|
||||
# 超过还留着的一定是被异常或取消绕过了清理的残留。
|
||||
PREPARED_IMAGE_MAX_AGE_SECONDS = 30 * 60
|
||||
|
||||
|
||||
class _PublicOnlyRedirectHandler(urllib.request.HTTPRedirectHandler):
|
||||
"""Re-check redirect targets so public image URLs cannot jump to SSRF targets."""
|
||||
|
||||
def redirect_request(self, req, fp, code, msg, headers, newurl): # type: ignore[override]
|
||||
if not _url_host_is_public(newurl):
|
||||
logger.warning(
|
||||
"[PrivateCompanion] 远程图片重定向到非公网地址,已拒绝: url=%s",
|
||||
_single_line(newurl, 160),
|
||||
)
|
||||
return None
|
||||
return super().redirect_request(req, fp, code, msg, headers, newurl)
|
||||
|
||||
|
||||
class PrivateImageMixin:
|
||||
"""Methods split from main.PrivateCompanionPlugin."""
|
||||
|
||||
|
|
@ -177,6 +197,37 @@ class PrivateImageMixin:
|
|||
visit(raw)
|
||||
return [source for source in sources if source]
|
||||
|
||||
def _private_image_local_path_is_allowed(self, path: Path) -> bool:
|
||||
"""Allow image files only from plugin/AstrBot/temp storage roots."""
|
||||
try:
|
||||
resolved = path.resolve()
|
||||
except Exception:
|
||||
return False
|
||||
roots: list[Path] = []
|
||||
for candidate in (
|
||||
getattr(self, "data_dir", ""),
|
||||
tempfile.gettempdir(),
|
||||
):
|
||||
if candidate:
|
||||
try:
|
||||
roots.append(Path(candidate).resolve())
|
||||
except Exception:
|
||||
continue
|
||||
try:
|
||||
astrbot_root = Path(get_astrbot_data_path()).resolve()
|
||||
except Exception:
|
||||
astrbot_root = None
|
||||
if astrbot_root is not None:
|
||||
roots.append(astrbot_root)
|
||||
for root in roots:
|
||||
try:
|
||||
if resolved.is_relative_to(root):
|
||||
return True
|
||||
except AttributeError: # Python < 3.9
|
||||
if str(resolved) == str(root) or str(resolved).startswith(str(root) + os.sep):
|
||||
return True
|
||||
return False
|
||||
|
||||
async def _persist_private_inbound_images(self, event: AstrMessageEvent, user_id: str) -> list[str]:
|
||||
result: list[str] = []
|
||||
target_dir = Path(self.data_dir) / "private_inbound_images" / re.sub(r"[^0-9A-Za-z_.-]+", "_", str(user_id or "unknown"))
|
||||
|
|
@ -217,6 +268,12 @@ class PrivateImageMixin:
|
|||
continue
|
||||
source_path = Path(source)
|
||||
if source_path.exists() and source_path.is_file():
|
||||
if not self._private_image_local_path_is_allowed(source_path):
|
||||
logger.warning(
|
||||
"[PrivateCompanion] 私聊图片越权本地路径已拒绝: path=%s",
|
||||
_single_line(source, 200),
|
||||
)
|
||||
continue
|
||||
suffix = source_path.suffix.lower() if source_path.suffix.lower() in {".jpg", ".jpeg", ".png", ".webp", ".gif"} else ".jpg"
|
||||
target = target_dir / f"{now_ms}_{index}{suffix}"
|
||||
try:
|
||||
|
|
@ -226,7 +283,12 @@ class PrivateImageMixin:
|
|||
except Exception as exc:
|
||||
logger.debug("[PrivateCompanion] 私聊图片暂存失败: %s", exc)
|
||||
if re.match(r"^https?://", source, flags=re.I):
|
||||
persisted = await self._persist_private_remote_image_source(source, target_dir, f"{now_ms}_{index}")
|
||||
persisted = await self._persist_private_remote_image_source(
|
||||
source,
|
||||
target_dir,
|
||||
f"{now_ms}_{index}",
|
||||
public_hosts_only=True,
|
||||
)
|
||||
if persisted:
|
||||
result.append(persisted)
|
||||
continue
|
||||
|
|
@ -236,18 +298,41 @@ class PrivateImageMixin:
|
|||
for source in self._raw_private_image_sources(event):
|
||||
if not source or source in result:
|
||||
continue
|
||||
persisted = await self._persist_private_remote_image_source(source, target_dir, f"{now_ms}_raw_{len(result) + 1}")
|
||||
persisted = await self._persist_private_remote_image_source(
|
||||
source,
|
||||
target_dir,
|
||||
f"{now_ms}_raw_{len(result) + 1}",
|
||||
public_hosts_only=True,
|
||||
)
|
||||
if persisted:
|
||||
result.append(persisted)
|
||||
continue
|
||||
if re.match(r"^https?://", source, flags=re.I):
|
||||
# A rejected remote download must not fall back to the raw
|
||||
# URL, otherwise the downstream model/provider could fetch
|
||||
# a private or metadata endpoint itself.
|
||||
continue
|
||||
if self._private_image_source_to_model_url(source):
|
||||
result.append(source)
|
||||
return result
|
||||
|
||||
async def _persist_private_remote_image_source(self, source: str, target_dir: Path, stem: str) -> str:
|
||||
async def _persist_private_remote_image_source(
|
||||
self,
|
||||
source: str,
|
||||
target_dir: Path,
|
||||
stem: str,
|
||||
*,
|
||||
public_hosts_only: bool = False,
|
||||
) -> str:
|
||||
text = str(source or "").strip()
|
||||
if not re.match(r"^https?://", text, flags=re.I):
|
||||
return ""
|
||||
if public_hosts_only and not await asyncio.to_thread(_url_host_is_public, text):
|
||||
logger.warning(
|
||||
"[PrivateCompanion] 远程图片地址不是公网地址,已拒绝下载: url=%s",
|
||||
_single_line(text, 160),
|
||||
)
|
||||
return ""
|
||||
|
||||
def download() -> str:
|
||||
try:
|
||||
|
|
@ -258,7 +343,17 @@ class PrivateImageMixin:
|
|||
"Accept": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
|
||||
},
|
||||
)
|
||||
with urllib.request.urlopen(request, timeout=15) as response:
|
||||
opener = (
|
||||
urllib.request.build_opener(_PublicOnlyRedirectHandler())
|
||||
if public_hosts_only
|
||||
else None
|
||||
)
|
||||
response_cm = (
|
||||
opener.open(request, timeout=15)
|
||||
if opener is not None
|
||||
else urllib.request.urlopen(request, timeout=15)
|
||||
)
|
||||
with response_cm as response:
|
||||
content_type = str(response.headers.get("Content-Type") or "").lower()
|
||||
length = _safe_int(response.headers.get("Content-Length"), 0, 0)
|
||||
max_bytes = 12 * 1024 * 1024
|
||||
|
|
@ -307,11 +402,17 @@ class PrivateImageMixin:
|
|||
target_dir.mkdir(parents=True, exist_ok=True)
|
||||
except Exception:
|
||||
return []
|
||||
self._sweep_stale_prepared_image_files(target_dir)
|
||||
prepared: list[str] = []
|
||||
now_ms = int(_now_ts() * 1000)
|
||||
for index, source in enumerate([str(item).strip() for item in (image_sources or []) if str(item or "").strip()][:12], 1):
|
||||
if re.match(r"^https?://", source, flags=re.I):
|
||||
persisted = await self._persist_private_remote_image_source(source, target_dir, f"{now_ms}_{index}")
|
||||
persisted = await self._persist_private_remote_image_source(
|
||||
source,
|
||||
target_dir,
|
||||
f"{now_ms}_{index}",
|
||||
public_hosts_only=True,
|
||||
)
|
||||
if persisted and persisted not in prepared:
|
||||
prepared.append(persisted)
|
||||
continue
|
||||
|
|
@ -319,6 +420,24 @@ class PrivateImageMixin:
|
|||
prepared.append(source)
|
||||
return prepared
|
||||
|
||||
def _sweep_stale_prepared_image_files(self, target_dir: Path) -> int:
|
||||
"""Remove stale downloaded images left behind by cancellation or errors."""
|
||||
removed = 0
|
||||
try:
|
||||
deadline = _now_ts() - PREPARED_IMAGE_MAX_AGE_SECONDS
|
||||
for path in target_dir.iterdir():
|
||||
try:
|
||||
if path.is_file() and path.stat().st_mtime < deadline:
|
||||
path.unlink(missing_ok=True)
|
||||
removed += 1
|
||||
except Exception:
|
||||
continue
|
||||
except Exception:
|
||||
return removed
|
||||
if removed:
|
||||
logger.info("[PrivateCompanion] 已清理识图临时文件残留: dir=%s removed=%s", target_dir.name, removed)
|
||||
return removed
|
||||
|
||||
def _cleanup_prepared_image_sources(self, sources: list[str], *, namespace: str) -> None:
|
||||
"""Remove only temporary files downloaded into this plugin's vision namespace."""
|
||||
try:
|
||||
|
|
@ -353,7 +472,7 @@ class PrivateImageMixin:
|
|||
if re.match(r"^https?://", text, flags=re.I):
|
||||
continue
|
||||
path = Path(text)
|
||||
if not path.exists() or not path.is_file():
|
||||
if not path.exists() or not path.is_file() or not self._private_image_local_path_is_allowed(path):
|
||||
continue
|
||||
ref = str(path.resolve())
|
||||
if ref not in refs:
|
||||
|
|
@ -373,6 +492,8 @@ class PrivateImageMixin:
|
|||
path = Path(text)
|
||||
if not path.exists() or not path.is_file():
|
||||
return ""
|
||||
if not self._private_image_local_path_is_allowed(path):
|
||||
return ""
|
||||
suffix = path.suffix.lower()
|
||||
mime = "image/png" if suffix == ".png" else "image/webp" if suffix == ".webp" else "image/gif" if suffix == ".gif" else "image/jpeg"
|
||||
try:
|
||||
|
|
@ -396,7 +517,7 @@ class PrivateImageMixin:
|
|||
if text.startswith("file://"):
|
||||
text = text[len("file://"):]
|
||||
path = Path(text)
|
||||
if path.exists() and path.is_file():
|
||||
if path.exists() and path.is_file() and self._private_image_local_path_is_allowed(path):
|
||||
return "sha256:" + hashlib.sha256(path.read_bytes()).hexdigest()
|
||||
except Exception as exc:
|
||||
logger.debug("[PrivateCompanion] 私聊图片缓存键生成失败: %s", exc)
|
||||
|
|
@ -1650,7 +1771,7 @@ class PrivateImageMixin:
|
|||
if text.startswith("file://"):
|
||||
text = text[len("file://"):]
|
||||
path = Path(text)
|
||||
if not path.exists() or not path.is_file():
|
||||
if not path.exists() or not path.is_file() or not self._private_image_local_path_is_allowed(path):
|
||||
return b""
|
||||
if path.suffix.lower() != ".gif":
|
||||
head = path.read_bytes()[:6]
|
||||
|
|
|
|||
168
tests/test_c6_atrelay_security.py
普通文件
168
tests/test_c6_atrelay_security.py
普通文件
|
|
@ -0,0 +1,168 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import ast
|
||||
from pathlib import Path
|
||||
import types
|
||||
import unittest
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
class _Logger:
|
||||
def info(self, *args, **kwargs):
|
||||
return None
|
||||
|
||||
def warning(self, *args, **kwargs):
|
||||
return None
|
||||
|
||||
|
||||
def _single_line(value, limit=240):
|
||||
text = " ".join(str(value or "").split())
|
||||
return text[:limit]
|
||||
|
||||
|
||||
def _load_security_helpers():
|
||||
source = (ROOT / "atrelay.py").read_text(encoding="utf-8")
|
||||
tree = ast.parse(source)
|
||||
names = {
|
||||
"_atrelay_tool_authorization",
|
||||
"_atrelay_known_group_ids",
|
||||
"_atrelay_target_group_allowed",
|
||||
}
|
||||
nodes = [node for node in tree.body if isinstance(node, ast.ClassDef) and node.name == "AtRelayMixin"]
|
||||
methods = [node for node in nodes[0].body if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name in names]
|
||||
module = ast.Module(
|
||||
body=[ast.parse("from __future__ import annotations").body[0], *methods],
|
||||
type_ignores=[],
|
||||
)
|
||||
namespace = {"_single_line": _single_line, "logger": _Logger()}
|
||||
exec(compile(ast.fix_missing_locations(module), str(ROOT / "atrelay.py"), "exec"), namespace)
|
||||
return namespace
|
||||
|
||||
|
||||
class _Harness:
|
||||
def __init__(self, *, owner=True, whitelist=None, blacklist=None, data=None, current_group=""):
|
||||
self._owner = owner
|
||||
self._whitelist = set(whitelist or ())
|
||||
self._blacklist = set(blacklist or ())
|
||||
self.data = data or {}
|
||||
self.current_group = current_group
|
||||
|
||||
def _permission_identity_id(self, value):
|
||||
return str(value or "")
|
||||
|
||||
def _is_private_companion_owner_user_id(self, value):
|
||||
return self._owner and value == "owner"
|
||||
|
||||
def _configured_group_ids(self):
|
||||
return self._whitelist
|
||||
|
||||
def _configured_group_blacklist_ids(self):
|
||||
return self._blacklist
|
||||
|
||||
def _extract_group_id_from_event(self, event):
|
||||
return self.current_group
|
||||
|
||||
|
||||
class AtRelaySecurityTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.helpers = _load_security_helpers()
|
||||
cls.source = (ROOT / "atrelay.py").read_text(encoding="utf-8") + (ROOT / "llm_tool_actions.py").read_text(encoding="utf-8")
|
||||
|
||||
def _bind(self, harness, name):
|
||||
method = self.helpers[name]
|
||||
return types.MethodType(method, harness)
|
||||
|
||||
def test_non_owner_is_rejected_and_owner_is_allowed(self):
|
||||
event = types.SimpleNamespace(unified_msg_origin="aiocqhttp:FriendMessage:owner")
|
||||
event.get_sender_id = lambda: "guest"
|
||||
denied, _ = self._bind(_Harness(owner=False), "_atrelay_tool_authorization")(event)
|
||||
self.assertFalse(denied)
|
||||
event.get_sender_id = lambda: "owner"
|
||||
allowed, _ = self._bind(_Harness(owner=True), "_atrelay_tool_authorization")(event)
|
||||
self.assertTrue(allowed)
|
||||
|
||||
def test_blacklist_takes_precedence_over_whitelist(self):
|
||||
harness = _Harness(whitelist={"100"}, blacklist={"100"})
|
||||
harness._atrelay_known_group_ids = self._bind(harness, "_atrelay_known_group_ids")
|
||||
result = self._bind(harness, "_atrelay_target_group_allowed")("100", None)
|
||||
self.assertTrue(result)
|
||||
self.assertIn("发送失败", result)
|
||||
|
||||
def test_unknown_group_is_rejected_when_allow_set_exists(self):
|
||||
harness = _Harness(whitelist={"100"})
|
||||
harness._atrelay_known_group_ids = self._bind(harness, "_atrelay_known_group_ids")
|
||||
result = self._bind(harness, "_atrelay_target_group_allowed")("999", None)
|
||||
self.assertIn("发送失败", result)
|
||||
|
||||
def test_empty_group_set_keeps_owner_checked_compatibility_pass(self):
|
||||
harness = _Harness()
|
||||
harness._atrelay_known_group_ids = self._bind(harness, "_atrelay_known_group_ids")
|
||||
result = self._bind(harness, "_atrelay_target_group_allowed")("999", None)
|
||||
self.assertEqual(result, "")
|
||||
|
||||
def test_known_and_current_groups_extend_allow_set(self):
|
||||
harness = _Harness(data={"groups": {"200": {"name": "observed"}}}, current_group="300")
|
||||
harness._atrelay_known_group_ids = self._bind(harness, "_atrelay_known_group_ids")
|
||||
checker = self._bind(harness, "_atrelay_target_group_allowed")
|
||||
self.assertEqual(checker("200", None), "")
|
||||
self.assertEqual(checker("300", types.SimpleNamespace()), "")
|
||||
|
||||
def test_audit_log_contract_truncates_body_and_keeps_safe_metadata(self):
|
||||
tree = ast.parse(self.source)
|
||||
note_nodes = [
|
||||
node
|
||||
for node in ast.walk(tree)
|
||||
if isinstance(node, ast.FunctionDef) and node.name == "_note_atrelay_send"
|
||||
]
|
||||
self.assertEqual(len(note_nodes), 1)
|
||||
note_source = ast.get_source_segment(self.source, note_nodes[0]) or ""
|
||||
self.assertIn("_normalize_atrelay_text(text, limit=300)", note_source)
|
||||
append_dicts = [
|
||||
node.args[0]
|
||||
for node in ast.walk(note_nodes[0])
|
||||
if isinstance(node, ast.Call)
|
||||
and isinstance(node.func, ast.Attribute)
|
||||
and node.func.attr == "append"
|
||||
and node.args
|
||||
and isinstance(node.args[0], ast.Dict)
|
||||
]
|
||||
self.assertTrue(append_dicts)
|
||||
keys = {key.value for key in append_dicts[0].keys if isinstance(key, ast.Constant)}
|
||||
self.assertNotIn("text", keys)
|
||||
self.assertIn("signature", keys)
|
||||
|
||||
def test_authorization_and_group_guard_precede_rewrite_or_send(self):
|
||||
tree = ast.parse(self.source)
|
||||
wanted = {
|
||||
"_pc_relay_message_impl",
|
||||
"_pc_send_to_group_impl",
|
||||
"_pc_send_to_private_user_impl",
|
||||
"_pc_schedule_group_relay_impl",
|
||||
}
|
||||
functions = {
|
||||
node.name: node
|
||||
for node in ast.walk(tree)
|
||||
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name in wanted
|
||||
}
|
||||
for name, node in functions.items():
|
||||
calls = []
|
||||
for child in ast.walk(node):
|
||||
if isinstance(child, ast.Call) and isinstance(child.func, ast.Attribute):
|
||||
calls.append((child.func.attr, child.lineno))
|
||||
auth_line = next(line for method, line in calls if method == "_atrelay_tool_authorization")
|
||||
first_sensitive_line = min(
|
||||
line
|
||||
for method, line in calls
|
||||
if method in {"_rewrite_atrelay_message_with_llm", "_send_atrelay_chain_to_target", "_resolve_atrelay_target_user"}
|
||||
)
|
||||
self.assertLess(auth_line, first_sensitive_line, name)
|
||||
if "group" in name or name == "_pc_relay_message_impl":
|
||||
guard_line = next(line for method, line in calls if method == "_atrelay_target_group_allowed")
|
||||
self.assertLess(guard_line, first_sensitive_line, name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
282
tests/test_c6_capability_matrix.py
普通文件
282
tests/test_c6_capability_matrix.py
普通文件
|
|
@ -0,0 +1,282 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import ast
|
||||
import json
|
||||
from collections import Counter
|
||||
from pathlib import Path
|
||||
import re
|
||||
import unittest
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
REQUIRED_MODULES = {
|
||||
"atrelay.py",
|
||||
"command_handlers.py",
|
||||
"creative.py",
|
||||
"group_member_safety.py",
|
||||
"group_observation.py",
|
||||
"group_wakeup.py",
|
||||
"llm_tool_actions.py",
|
||||
"news_exploration.py",
|
||||
"page_api.py",
|
||||
"page_api_qzone.py",
|
||||
"page_api_users_groups.py",
|
||||
"private_image.py",
|
||||
"private_reading.py",
|
||||
"proactive_engine.py",
|
||||
"qzone_integration.py",
|
||||
"qzone_media.py",
|
||||
"qzone_recent_parser.py",
|
||||
"qzone_selection.py",
|
||||
"tts_enhancement.py",
|
||||
}
|
||||
|
||||
REQUIRED_PAGE_ASSETS = {
|
||||
"pages/陪伴面板/index.html",
|
||||
"pages/陪伴面板/app.js",
|
||||
"pages/陪伴面板/app.css",
|
||||
"pages/陪伴面板/js/panels/qzone-panel.js",
|
||||
"pages/陪伴面板/js/features/daily-outfit.js",
|
||||
}
|
||||
|
||||
EXPECTED_QZONE_ROUTES = [
|
||||
("/qzone/status", "get_qzone_status", "GET"),
|
||||
("/qzone/health", "get_qzone_status", "GET"),
|
||||
("/qzone/summary", "get_qzone_status", "GET"),
|
||||
("/qzone/state", "get_qzone_status", "GET"),
|
||||
("/qzone/feed", "get_qzone_feed", "GET"),
|
||||
("/qzone/feeds", "get_qzone_feed", "GET"),
|
||||
("/qzone/list", "get_qzone_feed", "GET"),
|
||||
("/qzone/detail", "get_qzone_detail", "GET"),
|
||||
("/qzone/post", "get_qzone_detail", "GET"),
|
||||
("/qzone/item", "get_qzone_detail", "GET"),
|
||||
("/qzone/refresh_cookies", "refresh_qzone_cookies", "POST"),
|
||||
("/qzone/refresh-cookies", "refresh_qzone_cookies", "POST"),
|
||||
("/qzone/cookies/refresh", "refresh_qzone_cookies", "POST"),
|
||||
("/qzone/cookie/refresh", "refresh_qzone_cookies", "POST"),
|
||||
("/qzone/refresh", "refresh_qzone_cookies", "POST"),
|
||||
("/qzone/publish", "publish_qzone_post", "POST"),
|
||||
("/qzone/post/publish", "publish_qzone_post", "POST"),
|
||||
("/qzone/post", "publish_qzone_post", "POST"),
|
||||
("/qzone/like", "like_qzone_post", "POST"),
|
||||
("/qzone/post/like", "like_qzone_post", "POST"),
|
||||
("/qzone/comment", "comment_qzone_post", "POST"),
|
||||
("/qzone/post/comment", "comment_qzone_post", "POST"),
|
||||
("/qzone/delete", "delete_qzone_post", "POST"),
|
||||
("/qzone/post/delete", "delete_qzone_post", "POST"),
|
||||
]
|
||||
|
||||
EXPECTED_LLM_TOOLS = {
|
||||
"pc_qzone_view_feed",
|
||||
"pc_qzone_publish_feed",
|
||||
"pc_generate_photo",
|
||||
"pc_find_reaction_image",
|
||||
"pc_manage_memo",
|
||||
"pc_manage_schedule",
|
||||
"pc_view_creative_work",
|
||||
"pc_get_group_id_by_name",
|
||||
"pc_get_user_id_by_name",
|
||||
"pc_query_relation_person",
|
||||
"pc_get_specified_group_members",
|
||||
"pc_query_interaction",
|
||||
"pc_relay_message",
|
||||
"pc_send_to_group",
|
||||
"pc_send_to_private_user",
|
||||
"pc_send_to_groups",
|
||||
"pc_send_to_private_users",
|
||||
"pc_schedule_group_relay",
|
||||
}
|
||||
|
||||
# The production code does not expose a literal ``optional_tasks`` object.
|
||||
# These are the stable config switches that keep its optional C6 capabilities
|
||||
# routable without importing AstrBot or instantiating the plugin.
|
||||
OPTIONAL_TASKS = {
|
||||
"bilibili": "enable_bilibili_integration",
|
||||
"news": "enable_news_integration",
|
||||
"web_exploration": "enable_web_exploration",
|
||||
"qzone": "enable_qzone_integration",
|
||||
"reading": "enable_private_reading_integration",
|
||||
"group_companion": "enable_group_companion",
|
||||
"group_wakeup": "enable_group_wakeup_enhancement",
|
||||
"group_safety": "enable_group_member_safety",
|
||||
"atrelay": "enable_atrelay_tools",
|
||||
"outfit": "enable_daily_outfit_photo",
|
||||
"photo": "enable_natural_language_photo_generation",
|
||||
"tts": "enable_tts_enhancement",
|
||||
}
|
||||
|
||||
|
||||
def _parse(path: Path) -> ast.Module:
|
||||
return ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
|
||||
|
||||
|
||||
def _source(*relative_paths: str) -> str:
|
||||
return "\n".join((ROOT / relative_path).read_text(encoding="utf-8") for relative_path in relative_paths)
|
||||
|
||||
|
||||
def _literal(node: ast.AST) -> object:
|
||||
try:
|
||||
return ast.literal_eval(node)
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
|
||||
|
||||
def _qzone_routes() -> list[tuple[str, str, str]]:
|
||||
tree = _parse(ROOT / "page_api.py")
|
||||
for node in ast.walk(tree):
|
||||
if not isinstance(node, ast.Assign) or not isinstance(node.value, ast.List):
|
||||
continue
|
||||
if not any(isinstance(target, ast.Name) and target.id == "routes" for target in node.targets):
|
||||
continue
|
||||
routes: list[tuple[str, str, str]] = []
|
||||
for item in node.value.elts:
|
||||
if not isinstance(item, ast.Tuple) or len(item.elts) < 3:
|
||||
continue
|
||||
path, handler, methods = item.elts[:3]
|
||||
path_value = _literal(path)
|
||||
handler_name = handler.attr if isinstance(handler, ast.Attribute) else None
|
||||
method_values = _literal(methods)
|
||||
if isinstance(path_value, str) and path_value.startswith("/qzone/") and handler_name and isinstance(method_values, list):
|
||||
routes.extend((path_value, handler_name, str(method)) for method in method_values)
|
||||
return routes
|
||||
return []
|
||||
|
||||
|
||||
def _llm_tool_names() -> set[str]:
|
||||
names: set[str] = set()
|
||||
for path in ROOT.glob("*.py"):
|
||||
tree = _parse(path)
|
||||
for node in ast.walk(tree):
|
||||
if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
|
||||
continue
|
||||
for decorator in node.decorator_list:
|
||||
if not isinstance(decorator, ast.Call) or not isinstance(decorator.func, ast.Attribute):
|
||||
continue
|
||||
if decorator.func.attr != "llm_tool":
|
||||
continue
|
||||
for keyword in decorator.keywords:
|
||||
if keyword.arg == "name" and isinstance(keyword.value, ast.Constant) and isinstance(keyword.value.value, str):
|
||||
names.add(keyword.value.value)
|
||||
return names
|
||||
|
||||
|
||||
def _command_mappings() -> dict[str, set[str]]:
|
||||
tree = _parse(ROOT / "main.py")
|
||||
mappings: dict[str, set[str]] = {}
|
||||
for node in ast.walk(tree):
|
||||
if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
|
||||
continue
|
||||
for decorator in node.decorator_list:
|
||||
if not isinstance(decorator, ast.Call) or not isinstance(decorator.func, ast.Attribute):
|
||||
continue
|
||||
if decorator.func.attr != "command" or not decorator.args:
|
||||
continue
|
||||
command = _literal(decorator.args[0])
|
||||
aliases = next((keyword.value for keyword in decorator.keywords if keyword.arg == "alias"), None)
|
||||
alias_values = _literal(aliases) if aliases is not None else set()
|
||||
mappings[str(command)] = {str(value) for value in alias_values} if isinstance(alias_values, set) else set()
|
||||
return mappings
|
||||
|
||||
|
||||
class C6CapabilityMatrixTests(unittest.TestCase):
|
||||
def test_required_capability_modules_and_page_assets_exist(self) -> None:
|
||||
missing_modules = sorted(name for name in REQUIRED_MODULES if not (ROOT / name).is_file())
|
||||
missing_assets = sorted(name for name in REQUIRED_PAGE_ASSETS if not (ROOT / name).is_file())
|
||||
self.assertEqual(missing_modules, [], f"missing chat-side capability modules: {missing_modules}")
|
||||
self.assertEqual(missing_assets, [], f"missing companion page assets: {missing_assets}")
|
||||
|
||||
main_source = (ROOT / "main.py").read_text(encoding="utf-8")
|
||||
directly_routed_modules = REQUIRED_MODULES - {
|
||||
"page_api.py",
|
||||
"page_api_qzone.py",
|
||||
"page_api_users_groups.py",
|
||||
"qzone_media.py",
|
||||
"qzone_recent_parser.py",
|
||||
"qzone_selection.py",
|
||||
}
|
||||
for module_name in directly_routed_modules:
|
||||
self.assertIn(f".{module_name[:-3]}", main_source, f"main.py no longer routes {module_name}")
|
||||
|
||||
def test_qzone_route_matrix_keeps_24_entries_and_handlers(self) -> None:
|
||||
actual = _qzone_routes()
|
||||
self.assertEqual(len(actual), 24, "C6 expects 24 /qzone/* route entries")
|
||||
self.assertEqual(Counter(actual), Counter(EXPECTED_QZONE_ROUTES))
|
||||
self.assertEqual({handler for _, handler, _ in actual}, {
|
||||
"get_qzone_status",
|
||||
"get_qzone_feed",
|
||||
"get_qzone_detail",
|
||||
"refresh_qzone_cookies",
|
||||
"publish_qzone_post",
|
||||
"like_qzone_post",
|
||||
"comment_qzone_post",
|
||||
"delete_qzone_post",
|
||||
})
|
||||
|
||||
def test_original_llm_tool_matrix_is_present(self) -> None:
|
||||
actual = _llm_tool_names()
|
||||
self.assertGreaterEqual(len(actual), 18)
|
||||
self.assertTrue(EXPECTED_LLM_TOOLS <= actual, sorted(EXPECTED_LLM_TOOLS - actual))
|
||||
|
||||
def test_optional_tasks_are_declared_in_schema_and_used_by_chat_side(self) -> None:
|
||||
schema = json.loads((ROOT / "_conf_schema.json").read_text(encoding="utf-8"))
|
||||
self.assertIsInstance(schema, dict)
|
||||
schema_keys = set(schema)
|
||||
source = _source("main.py", "page_api.py")
|
||||
for task, config_key in OPTIONAL_TASKS.items():
|
||||
self.assertIn(config_key, schema_keys, f"optional C6 task {task} lost schema key {config_key}")
|
||||
self.assertRegex(source, rf"\b{re.escape(config_key)}\b", f"optional C6 task {task} is not routed")
|
||||
self.assertIn("enabled_proactive_actions", schema_keys)
|
||||
self.assertIn("enabled_proactive_actions", source)
|
||||
|
||||
def test_command_mapping_and_reason_window_hooks_remain(self) -> None:
|
||||
mappings = _command_mappings()
|
||||
self.assertIn("陪伴", mappings)
|
||||
self.assertIn("陪伴群", mappings)
|
||||
self.assertTrue({"私聊陪伴", "主动陪伴"} <= mappings["陪伴"])
|
||||
self.assertTrue({"群陪伴", "群聊陪伴"} <= mappings["陪伴群"])
|
||||
|
||||
tree = _parse(ROOT / "proactive_engine.py")
|
||||
reason_window_methods = {
|
||||
node.name
|
||||
for node in ast.walk(tree)
|
||||
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name == "_reason_windows"
|
||||
}
|
||||
self.assertEqual(reason_window_methods, {"_reason_windows"})
|
||||
source = (ROOT / "proactive_engine.py").read_text(encoding="utf-8")
|
||||
self.assertGreaterEqual(source.count("_reason_windows("), 4)
|
||||
|
||||
def test_c6_domain_entrypoints_are_still_reachable(self) -> None:
|
||||
source = _source(
|
||||
"main.py",
|
||||
"command_handlers.py",
|
||||
"news_exploration.py",
|
||||
"page_api.py",
|
||||
"private_image.py",
|
||||
"private_reading.py",
|
||||
"group_member_safety.py",
|
||||
"group_wakeup.py",
|
||||
"atrelay.py",
|
||||
"tts_enhancement.py",
|
||||
)
|
||||
markers = {
|
||||
"QQ space": ("qzone", "QZONE_COOKIE"),
|
||||
"news": ("news", "NEWS_PROVIDER_ID"),
|
||||
"Bilibili": ("bilibili", "enable_bilibili_integration"),
|
||||
"web exploration": ("web_exploration", "WEB_EXPLORATION_PROVIDER_ID"),
|
||||
"group safety": ("group_member_safety", "enable_group_member_safety"),
|
||||
"group wakeup": ("group_wakeup", "enable_group_wakeup_enhancement"),
|
||||
"outfit": ("daily_outfit", "enable_daily_outfit_photo"),
|
||||
"image generation": ("pc_generate_photo", "photo_generation_backend"),
|
||||
"TTS": ("tts", "enable_tts_enhancement"),
|
||||
"history import": ("历史对话导入", "HISTORY_SUMMARY_PROVIDER_ID"),
|
||||
"AtRelay": ("atrelay", "pc_relay_message"),
|
||||
"page API": ("register_routes", "/qzone/status"),
|
||||
}
|
||||
for capability, required_markers in markers.items():
|
||||
self.assertTrue(all(marker in source for marker in required_markers), capability)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
189
tests/test_c6_image_security.py
普通文件
189
tests/test_c6_image_security.py
普通文件
|
|
@ -0,0 +1,189 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import importlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import tempfile
|
||||
import types
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def _install_astrbot_stubs() -> None:
|
||||
class _Logger:
|
||||
def __getattr__(self, _name: str):
|
||||
return lambda *args, **kwargs: None
|
||||
|
||||
def ensure_module(name: str) -> types.ModuleType:
|
||||
module = sys.modules.get(name)
|
||||
if not isinstance(module, types.ModuleType):
|
||||
module = types.ModuleType(name)
|
||||
sys.modules[name] = module
|
||||
# Earlier unittest modules install a minimal ``astrbot.api`` module.
|
||||
# Marking it as a package lets this test add the submodules required by
|
||||
# the real image/command imports without depending on import order.
|
||||
if name in {"astrbot", "astrbot.api", "astrbot.core", "astrbot.core.agent", "astrbot.core.utils"}:
|
||||
module.__path__ = getattr(module, "__path__", [])
|
||||
return module
|
||||
|
||||
astrbot = ensure_module("astrbot")
|
||||
api = ensure_module("astrbot.api")
|
||||
if not hasattr(api, "logger"):
|
||||
api.logger = _Logger()
|
||||
else:
|
||||
for method_name in ("debug", "info", "warning", "error", "exception"):
|
||||
if not hasattr(api.logger, method_name):
|
||||
setattr(api.logger, method_name, lambda *args, **kwargs: None)
|
||||
event = ensure_module("astrbot.api.event")
|
||||
event.AstrMessageEvent = getattr(event, "AstrMessageEvent", object)
|
||||
message_components = ensure_module("astrbot.api.message_components")
|
||||
message_components.Image = getattr(message_components, "Image", type("Image", (), {}))
|
||||
message_components.Plain = getattr(message_components, "Plain", type("Plain", (), {}))
|
||||
provider = ensure_module("astrbot.api.provider")
|
||||
provider.ProviderRequest = getattr(provider, "ProviderRequest", type("ProviderRequest", (), {}))
|
||||
|
||||
core = ensure_module("astrbot.core")
|
||||
if not hasattr(core, "file_token_service"):
|
||||
core.file_token_service = types.SimpleNamespace()
|
||||
agent = ensure_module("astrbot.core.agent")
|
||||
agent_message = ensure_module("astrbot.core.agent.message")
|
||||
agent_message.AssistantMessageSegment = getattr(
|
||||
agent_message, "AssistantMessageSegment", type("AssistantMessageSegment", (), {})
|
||||
)
|
||||
agent_message.UserMessageSegment = getattr(
|
||||
agent_message, "UserMessageSegment", type("UserMessageSegment", (), {})
|
||||
)
|
||||
astr_main_agent = ensure_module("astrbot.core.astr_main_agent")
|
||||
astr_main_agent.MainAgentBuildConfig = getattr(
|
||||
astr_main_agent, "MainAgentBuildConfig", type("MainAgentBuildConfig", (), {})
|
||||
)
|
||||
if not hasattr(astr_main_agent, "build_main_agent"):
|
||||
astr_main_agent.build_main_agent = lambda *args, **kwargs: None
|
||||
utils = ensure_module("astrbot.core.utils")
|
||||
astrbot_path = ensure_module("astrbot.core.utils.astrbot_path")
|
||||
if not hasattr(astrbot_path, "get_astrbot_data_path"):
|
||||
astrbot_path.get_astrbot_data_path = lambda: tempfile.gettempdir()
|
||||
|
||||
|
||||
def _load_modules():
|
||||
_install_astrbot_stubs()
|
||||
package_name = "c6_image_security_companion"
|
||||
package = types.ModuleType(package_name)
|
||||
package.__path__ = [str(ROOT)]
|
||||
sys.modules[package_name] = package
|
||||
helpers = importlib.import_module(f"{package_name}.helpers")
|
||||
command_handlers = importlib.import_module(f"{package_name}.command_handlers")
|
||||
private_image = importlib.import_module(f"{package_name}.private_image")
|
||||
return helpers, command_handlers.CommandHandlersMixin, private_image
|
||||
|
||||
|
||||
helpers, CommandHandlersMixin, private_image = _load_modules()
|
||||
|
||||
|
||||
class _Host(CommandHandlersMixin, private_image.PrivateImageMixin):
|
||||
def __init__(self, data_dir: Path) -> None:
|
||||
self.data_dir = str(data_dir)
|
||||
|
||||
|
||||
class ImageSecurityTests(unittest.TestCase):
|
||||
def test_url_host_classifier_rejects_private_and_accepts_public_resolution(self) -> None:
|
||||
private_info = [(2, 1, 6, "", ("127.0.0.1", 0))]
|
||||
public_info = [(2, 1, 6, "", ("93.184.216.34", 0))]
|
||||
with patch.object(helpers.socket, "getaddrinfo", return_value=private_info):
|
||||
self.assertFalse(helpers._url_host_is_public("https://example.test/image.png"))
|
||||
with patch.object(helpers.socket, "getaddrinfo", return_value=public_info):
|
||||
self.assertTrue(helpers._url_host_is_public("https://example.test/image.png"))
|
||||
self.assertFalse(helpers._url_host_is_public("file:///etc/passwd"))
|
||||
|
||||
def test_untrusted_reference_path_and_internal_url_are_rejected(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temporary:
|
||||
root = Path(temporary)
|
||||
data_dir = root / "plugin-data"
|
||||
data_dir.mkdir()
|
||||
outside = root / "outside.png"
|
||||
outside.write_bytes(b"not a secret image")
|
||||
host = _Host(data_dir)
|
||||
|
||||
self.assertEqual("", host._photo_reference_copy_local_file(outside, trusted=False))
|
||||
self.assertTrue(host._photo_reference_copy_local_file(outside, trusted=True))
|
||||
self.assertEqual(
|
||||
"",
|
||||
asyncio.run(
|
||||
host._photo_reference_source_to_stable_path(
|
||||
"http://169.254.169.254/latest/meta-data/",
|
||||
trusted=False,
|
||||
)
|
||||
),
|
||||
)
|
||||
|
||||
def test_private_image_source_guard_does_not_encode_arbitrary_local_file(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temporary:
|
||||
root = Path(temporary)
|
||||
data_dir = root / "plugin-data"
|
||||
data_dir.mkdir()
|
||||
outside = root / "outside.png"
|
||||
outside.write_bytes(b"private")
|
||||
host = _Host(data_dir)
|
||||
with patch.object(private_image.tempfile, "gettempdir", return_value=str(data_dir / "tmp")), patch.object(
|
||||
private_image, "get_astrbot_data_path", return_value=str(data_dir / "astrbot")
|
||||
):
|
||||
self.assertEqual("", host._private_image_source_to_model_url(str(outside)))
|
||||
|
||||
inside = data_dir / "inside.png"
|
||||
inside.write_bytes(b"image")
|
||||
self.assertTrue(host._private_image_source_to_model_url(str(inside)).startswith("data:image/png;base64,"))
|
||||
|
||||
def test_public_only_redirect_handler_rejects_private_target(self) -> None:
|
||||
handler = private_image._PublicOnlyRedirectHandler()
|
||||
with patch.object(private_image, "_url_host_is_public", return_value=False):
|
||||
self.assertIsNone(handler.redirect_request(None, None, 302, "", {}, "http://127.0.0.1/secret"))
|
||||
|
||||
def test_stale_prepared_image_cleanup_removes_only_old_files(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temporary:
|
||||
target_dir = Path(temporary)
|
||||
old_file = target_dir / "old.png"
|
||||
fresh_file = target_dir / "fresh.png"
|
||||
old_file.write_bytes(b"old")
|
||||
fresh_file.write_bytes(b"fresh")
|
||||
old_timestamp = private_image._now_ts() - private_image.PREPARED_IMAGE_MAX_AGE_SECONDS - 5
|
||||
os.utime(old_file, (old_timestamp, old_timestamp))
|
||||
host = _Host(Path(temporary) / "plugin-data")
|
||||
|
||||
self.assertEqual(1, host._sweep_stale_prepared_image_files(target_dir))
|
||||
self.assertFalse(old_file.exists())
|
||||
self.assertTrue(fresh_file.exists())
|
||||
|
||||
def test_remote_download_guard_short_circuits_before_network(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temporary:
|
||||
host = _Host(Path(temporary))
|
||||
with patch.object(private_image.urllib.request, "urlopen") as urlopen:
|
||||
result = asyncio.run(
|
||||
host._persist_private_remote_image_source(
|
||||
"http://10.0.0.8/image.png",
|
||||
Path(temporary),
|
||||
"probe",
|
||||
public_hosts_only=True,
|
||||
)
|
||||
)
|
||||
self.assertEqual("", result)
|
||||
urlopen.assert_not_called()
|
||||
|
||||
def test_rejected_remote_source_is_not_passed_through_to_the_model(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temporary:
|
||||
host = _Host(Path(temporary))
|
||||
host._event_components = lambda _event: []
|
||||
host._raw_private_image_sources = lambda _event: [
|
||||
"http://169.254.169.254/latest/meta-data/"
|
||||
]
|
||||
with patch.object(private_image, "_url_host_is_public", return_value=False):
|
||||
result = asyncio.run(host._persist_private_inbound_images(object(), "owner"))
|
||||
self.assertEqual([], result)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
190
tests/test_c6_provider_token_logs.py
普通文件
190
tests/test_c6_provider_token_logs.py
普通文件
|
|
@ -0,0 +1,190 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import types
|
||||
import unittest
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def _install_astrbot_stubs() -> None:
|
||||
"""Install only the AstrBot surface imported by token_budget."""
|
||||
astrbot = sys.modules.get("astrbot")
|
||||
if not isinstance(astrbot, types.ModuleType):
|
||||
astrbot = types.ModuleType("astrbot")
|
||||
sys.modules["astrbot"] = astrbot
|
||||
api = sys.modules.get("astrbot.api")
|
||||
if not isinstance(api, types.ModuleType):
|
||||
api = types.ModuleType("astrbot.api")
|
||||
sys.modules["astrbot.api"] = api
|
||||
|
||||
logger = getattr(api, "logger", None)
|
||||
if logger is None:
|
||||
class _Logger:
|
||||
def __getattr__(self, _name: str):
|
||||
return lambda *args, **kwargs: None
|
||||
|
||||
api.logger = _Logger()
|
||||
else:
|
||||
for method_name in ("debug", "info", "warning", "error", "exception"):
|
||||
if not hasattr(logger, method_name):
|
||||
setattr(logger, method_name, lambda *args, **kwargs: None)
|
||||
|
||||
|
||||
def _load_mixin():
|
||||
_install_astrbot_stubs()
|
||||
package_name = "c6_provider_token_logs_companion"
|
||||
package = types.ModuleType(package_name)
|
||||
package.__path__ = [str(ROOT)]
|
||||
sys.modules[package_name] = package
|
||||
module = importlib.import_module(f"{package_name}.token_budget")
|
||||
return module.TokenBudgetMixin
|
||||
|
||||
|
||||
TokenBudgetMixin = _load_mixin()
|
||||
|
||||
|
||||
class _MetaProvider:
|
||||
def __init__(self, provider_id: str):
|
||||
self.provider_id = provider_id
|
||||
|
||||
def meta(self):
|
||||
return types.SimpleNamespace(id=self.provider_id)
|
||||
|
||||
|
||||
class _Context:
|
||||
def __init__(self, provider=None, responses=None):
|
||||
self.provider = provider
|
||||
self.responses = list(responses or [])
|
||||
self.calls = []
|
||||
|
||||
def get_using_provider(self, *args, **kwargs):
|
||||
return self.provider
|
||||
|
||||
async def llm_generate(self, **kwargs):
|
||||
self.calls.append(kwargs)
|
||||
response = self.responses.pop(0)
|
||||
if isinstance(response, BaseException):
|
||||
raise response
|
||||
return response
|
||||
|
||||
|
||||
class _Response:
|
||||
def __init__(self, text: str):
|
||||
self.completion_text = text
|
||||
|
||||
|
||||
class _Host(TokenBudgetMixin):
|
||||
def __init__(self, *, context=None, provider_id=""):
|
||||
self.context = context
|
||||
self.llm_provider_id = provider_id
|
||||
self.data = {}
|
||||
self.daily_token_limit = 0
|
||||
self.daily_token_soft_limit = 0
|
||||
self.enable_daily_token_soft_limit = True
|
||||
self.model_fallback_overrides = {}
|
||||
self.saved = 0
|
||||
|
||||
def _save_data_sync(self):
|
||||
self.saved += 1
|
||||
|
||||
|
||||
class ProviderResolutionTests(unittest.TestCase):
|
||||
def test_explicit_provider_wins_over_context_provider(self):
|
||||
host = _Host(context=_Context(_MetaProvider("context-provider")), provider_id="configured-provider")
|
||||
|
||||
self.assertEqual("explicit-provider", host._resolve_chat_provider_id("explicit-provider"))
|
||||
self.assertEqual("configured-provider", host._resolve_chat_provider_id())
|
||||
|
||||
def test_context_meta_and_config_are_used_when_no_explicit_provider(self):
|
||||
host = _Host(context=_Context(_MetaProvider("meta-provider")))
|
||||
self.assertEqual("meta-provider", host._resolve_chat_provider_id())
|
||||
|
||||
class ConfigOnlyProvider:
|
||||
provider_config = {"provider_id": "config-provider"}
|
||||
|
||||
config_host = _Host(context=_Context(ConfigOnlyProvider()))
|
||||
self.assertEqual("config-provider", config_host._resolve_chat_provider_id())
|
||||
|
||||
def test_missing_provider_registry_returns_empty_string(self):
|
||||
host = _Host(context=_Context(None))
|
||||
self.assertEqual("", host._resolve_chat_provider_id())
|
||||
|
||||
|
||||
class ProviderFallbackTests(unittest.TestCase):
|
||||
def test_failed_primary_provider_uses_fallback_without_leaking_exception(self):
|
||||
secret = "PRIMARY_PROVIDER_SECRET_SHOULD_NOT_BE_RETURNED"
|
||||
context = _Context(responses=[RuntimeError(secret), _Response("备用 provider 成功")])
|
||||
host = _Host(context=context, provider_id="primary-provider")
|
||||
host.model_fallback_overrides = {"FAST_RESPONSE_PROVIDER_ID": "fallback-provider"}
|
||||
|
||||
result = asyncio.run(host._llm_call("hello", task="voice", timeout_key="FAST_RESPONSE_PROVIDER_ID"))
|
||||
|
||||
self.assertEqual("备用 provider 成功", result)
|
||||
self.assertNotIn(secret, result or "")
|
||||
self.assertEqual(["primary-provider", "fallback-provider"], [item["chat_provider_id"] for item in context.calls])
|
||||
recent = host.data["token_usage"]["recent"]
|
||||
self.assertTrue(any(item["provider"] == "fallback-provider" and item["success"] for item in recent))
|
||||
|
||||
|
||||
class TokenLogBoundaryTests(unittest.TestCase):
|
||||
def test_internal_usage_keeps_only_lengths_and_bounded_error(self):
|
||||
prompt = "PROMPT_BODY_" + ("p" * 5000)
|
||||
completion = "COMPLETION_BODY_" + ("c" * 5000)
|
||||
error = "ERROR_BODY_" + ("e" * 1000)
|
||||
host = _Host()
|
||||
|
||||
host._record_llm_usage(
|
||||
provider_id="provider",
|
||||
task="task",
|
||||
prompt=prompt,
|
||||
completion=completion,
|
||||
elapsed_ms=12,
|
||||
success=False,
|
||||
error=error,
|
||||
)
|
||||
|
||||
item = host.data["token_usage"]["recent"][-1]
|
||||
self.assertEqual(len(prompt), item["prompt_chars"])
|
||||
self.assertEqual(len(completion), item["completion_chars"])
|
||||
self.assertLessEqual(len(item["error"]), 160)
|
||||
serialized = repr(host.data["token_usage"])
|
||||
self.assertNotIn(prompt, serialized)
|
||||
self.assertNotIn(completion, serialized)
|
||||
self.assertNotIn(error, serialized)
|
||||
|
||||
def test_external_usage_has_bounded_identity_fields_and_no_raw_bodies(self):
|
||||
prompt = "EXTERNAL_PROMPT_" + ("p" * 5000)
|
||||
completion = "EXTERNAL_COMPLETION_" + ("c" * 5000)
|
||||
host = _Host()
|
||||
|
||||
host._record_external_llm_usage(
|
||||
provider_id="provider",
|
||||
task="external-task",
|
||||
prompt=prompt,
|
||||
completion=completion,
|
||||
elapsed_ms=4,
|
||||
success=True,
|
||||
session_id="session-" + ("s" * 5000),
|
||||
sender_id="sender-" + ("d" * 5000),
|
||||
message_type="message-type-" + ("m" * 5000),
|
||||
)
|
||||
|
||||
item = host.data["token_usage"]["external"]["recent"][-1]
|
||||
self.assertLessEqual(len(item["session"]), 160)
|
||||
self.assertLessEqual(len(item["sender"]), 80)
|
||||
self.assertLessEqual(len(item["message_type"]), 20)
|
||||
self.assertLessEqual(len(item["task"]), 40)
|
||||
self.assertEqual(len(prompt), item["prompt_chars"])
|
||||
self.assertEqual(len(completion), item["completion_chars"])
|
||||
serialized = repr(host.data["token_usage"]["external"])
|
||||
self.assertNotIn(prompt, serialized)
|
||||
self.assertNotIn(completion, serialized)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
@ -730,6 +730,18 @@ class TokenBudgetMixin:
|
|||
provider_id = self._provider_id_from_instance(provider)
|
||||
if provider_id:
|
||||
return provider_id
|
||||
# get_using_provider 拿不到时再兜两层:AstrBot 已经选出了默认对话模型
|
||||
# (启动日志会打印 "Selected ... as default chat model provider"),
|
||||
# 插件这边不该因为一条取值路径不通就整个放弃;否则所有没有显式配置
|
||||
# provider 的任务(日程、日记)会静默退化成模板兜底,且无从归因。
|
||||
fallback_id = self._chat_provider_id_from_registry(context)
|
||||
if fallback_id:
|
||||
logger.info("[PrivateCompanion] 默认对话 Provider 经注册表兜底解析: %s", fallback_id)
|
||||
return fallback_id
|
||||
logger.warning(
|
||||
"[PrivateCompanion] 无法解析默认对话 Provider:get_using_provider 与注册表兜底都没有结果;"
|
||||
"未显式配置 provider 的模型任务将退化为模板兜底"
|
||||
)
|
||||
return ""
|
||||
|
||||
@staticmethod
|
||||
|
|
@ -754,6 +766,51 @@ class TokenBudgetMixin:
|
|||
def _resolve_chat_provider_id(self, provider_id: str | None = None, *, umo: str = "") -> str:
|
||||
return str(provider_id or self.llm_provider_id or self._default_chat_provider_id(umo) or "").strip()
|
||||
|
||||
def _chat_provider_id_from_registry(self, context: Any) -> str:
|
||||
"""从 AstrBot Provider 注册表/配置里兜底取一个已加载的对话 Provider。"""
|
||||
get_all = getattr(context, "get_all_providers", None)
|
||||
if callable(get_all):
|
||||
try:
|
||||
providers = get_all() or []
|
||||
except Exception:
|
||||
providers = []
|
||||
for provider in providers:
|
||||
provider_id = self._provider_id_from_instance(provider)
|
||||
if provider_id:
|
||||
return provider_id
|
||||
|
||||
config = None
|
||||
getter = getattr(context, "get_config", None)
|
||||
if callable(getter):
|
||||
try:
|
||||
config = getter()
|
||||
except Exception:
|
||||
config = None
|
||||
settings = config.get("provider_settings") if isinstance(config, dict) else None
|
||||
if isinstance(settings, dict):
|
||||
value = _single_line(settings.get("default_provider_id"), 160)
|
||||
# 配置里存在 default provider 不代表实例已经装载;启动竞态时
|
||||
# 必须通过注册表确认,避免把“未就绪”误报成可用。
|
||||
if value and self._provider_instance_exists(context, value):
|
||||
return value
|
||||
return ""
|
||||
|
||||
@staticmethod
|
||||
def _provider_instance_exists(context: Any, provider_id: str) -> bool:
|
||||
getter = getattr(context, "get_provider_by_id", None)
|
||||
if not callable(getter):
|
||||
return False
|
||||
try:
|
||||
return getter(provider_id) is not None
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def _chat_provider_ready(self) -> bool:
|
||||
"""Return whether an explicit or currently loaded chat Provider is ready."""
|
||||
if _single_line(getattr(self, "llm_provider_id", ""), 160):
|
||||
return True
|
||||
return bool(self._default_chat_provider_id())
|
||||
|
||||
@staticmethod
|
||||
def _normalize_model_timeout_overrides(value: Any) -> dict[str, int]:
|
||||
raw = value
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用