3422 行
158 KiB
Python
3422 行
158 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import asyncio
|
|||
|
|
import json
|
|||
|
|
import hashlib
|
|||
|
|
import html
|
|||
|
|
import random
|
|||
|
|
import re
|
|||
|
|
import time
|
|||
|
|
from http.cookies import SimpleCookie
|
|||
|
|
from pathlib import Path
|
|||
|
|
from types import SimpleNamespace
|
|||
|
|
from typing import Any
|
|||
|
|
from urllib.parse import urlparse
|
|||
|
|
|
|||
|
|
from astrbot.api import logger
|
|||
|
|
from astrbot.api.event import AstrMessageEvent
|
|||
|
|
|
|||
|
|
from .helpers import _now_ts, _path_text, _safe_float, _safe_int, _single_line
|
|||
|
|
from .qzone_media import QzoneIntegrationError, QzoneMediaMixin
|
|||
|
|
|
|||
|
|
|
|||
|
|
class QzoneMixin(QzoneMediaMixin):
|
|||
|
|
"""QQ Zone integration helpers."""
|
|||
|
|
|
|||
|
|
_QZONE_COOKIE_DOMAIN = "user.qzone.qq.com"
|
|||
|
|
_QZONE_COOKIE_ACTIONS = ("get_cookies", "get_credentials")
|
|||
|
|
_QZONE_LOGIN_INFO_ACTIONS = ("get_login_info",)
|
|||
|
|
_QZONE_ACTION_CALLER_ATTRS = ("call_action", "call_api", "call", "api_call", "send_action")
|
|||
|
|
_QZONE_ACTION_OWNER_ATTRS = (
|
|||
|
|
"api",
|
|||
|
|
"bot",
|
|||
|
|
"client",
|
|||
|
|
"adapter",
|
|||
|
|
"connection",
|
|||
|
|
"onebot",
|
|||
|
|
"platform",
|
|||
|
|
"platform_impl",
|
|||
|
|
"impl",
|
|||
|
|
"instance",
|
|||
|
|
)
|
|||
|
|
_QZONE_COOKIE_VALUE_KEYS = (
|
|||
|
|
"cookies",
|
|||
|
|
"cookie",
|
|||
|
|
"cookie_text",
|
|||
|
|
"cookie_str",
|
|||
|
|
"cookies_str",
|
|||
|
|
"data",
|
|||
|
|
"result",
|
|||
|
|
"retdata",
|
|||
|
|
"ret_data",
|
|||
|
|
"payload",
|
|||
|
|
"response",
|
|||
|
|
)
|
|||
|
|
_QZONE_COOKIE_SECRET_KEYS = ("p_skey", "skey", "pskey", "skey2")
|
|||
|
|
_QZONE_COOKIE_DOMAIN_FALLBACKS = (
|
|||
|
|
"user.qzone.qq.com",
|
|||
|
|
"qzone.qq.com",
|
|||
|
|
"h5.qzone.qq.com",
|
|||
|
|
"mobile.qzone.qq.com",
|
|||
|
|
"taotao.qzone.qq.com",
|
|||
|
|
"qun.qzone.qq.com",
|
|||
|
|
"ti.qq.com",
|
|||
|
|
"qq.com",
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
def _qzone_plugin_dir(self) -> Path:
|
|||
|
|
candidates = [
|
|||
|
|
Path(__file__).resolve().parent.parent / "astrbot_plugin_qzone",
|
|||
|
|
Path(self.data_dir).parent.parent / "plugins" / "astrbot_plugin_qzone",
|
|||
|
|
]
|
|||
|
|
for path in candidates:
|
|||
|
|
if (path / "main.py").exists():
|
|||
|
|
return path
|
|||
|
|
return candidates[0]
|
|||
|
|
|
|||
|
|
def _find_qzone_instance(self) -> Any | None:
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
def _qzone_platform_supported(self, event: AstrMessageEvent | None = None) -> bool:
|
|||
|
|
platform_supports = getattr(self, "_platform_supports", None)
|
|||
|
|
if event is not None and callable(platform_supports):
|
|||
|
|
return bool(platform_supports("qzone", event=event))
|
|||
|
|
platform_available = getattr(self, "_platform_kind_available", None)
|
|||
|
|
if callable(platform_available):
|
|||
|
|
return bool(platform_available("onebot"))
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_platform_unavailable_message() -> str:
|
|||
|
|
return "QQ 官方机器人不支持 QQ 空间;该能力仅在 OneBot/aiocqhttp 平台可用。"
|
|||
|
|
|
|||
|
|
def _qzone_available(self, event: AstrMessageEvent | None = None) -> bool:
|
|||
|
|
return bool(self.enable_qzone_integration and self._qzone_platform_supported(event))
|
|||
|
|
|
|||
|
|
def _qzone_note_event_bot(self, event: AstrMessageEvent | None) -> None:
|
|||
|
|
"""Cache the latest OneBot connection for background Qzone jobs."""
|
|||
|
|
if event is not None and not self._qzone_platform_supported(event):
|
|||
|
|
return
|
|||
|
|
bot = getattr(event, "bot", None) if event is not None else None
|
|||
|
|
if bot is None:
|
|||
|
|
return
|
|||
|
|
for candidate in self._qzone_runtime_bot_candidates(bot):
|
|||
|
|
if self._qzone_runtime_bot_usable(candidate):
|
|||
|
|
self._qzone_last_bot = candidate
|
|||
|
|
self._qzone_clear_no_onebot_auth_failure()
|
|||
|
|
return
|
|||
|
|
self._qzone_last_bot = bot
|
|||
|
|
|
|||
|
|
def _qzone_clear_no_onebot_auth_failure(self) -> None:
|
|||
|
|
state = self._qzone_state_dict()
|
|||
|
|
if not isinstance(state, dict):
|
|||
|
|
return
|
|||
|
|
reason = str(state.get("last_auth_failure_reason") or "")
|
|||
|
|
if "没有可用的 OneBot 连接" not in reason and "未配置手动 QZONE_COOKIE" not in reason:
|
|||
|
|
return
|
|||
|
|
clearer = getattr(self, "_qzone_clear_auth_failure", None)
|
|||
|
|
if callable(clearer):
|
|||
|
|
clearer(state)
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_runtime_bot_usable(candidate: Any) -> bool:
|
|||
|
|
if candidate is None:
|
|||
|
|
return False
|
|||
|
|
if any(callable(getattr(candidate, name, None)) for name in ("get_cookies", "get_credentials", "get_login_info")):
|
|||
|
|
return True
|
|||
|
|
if any(callable(getattr(candidate, name, None)) for name in QzoneMixin._QZONE_ACTION_CALLER_ATTRS):
|
|||
|
|
return True
|
|||
|
|
api = getattr(candidate, "api", None)
|
|||
|
|
return any(callable(getattr(api, name, None)) for name in QzoneMixin._QZONE_ACTION_CALLER_ATTRS)
|
|||
|
|
|
|||
|
|
def _qzone_runtime_bot_candidates(self, source: Any) -> list[Any]:
|
|||
|
|
"""Return likely OneBot client objects from an AstrBot platform wrapper."""
|
|||
|
|
if source is None:
|
|||
|
|
return []
|
|||
|
|
candidates: list[Any] = [source]
|
|||
|
|
for attr in (
|
|||
|
|
"bot",
|
|||
|
|
"client",
|
|||
|
|
"adapter",
|
|||
|
|
"connection",
|
|||
|
|
"onebot",
|
|||
|
|
"platform",
|
|||
|
|
"platform_impl",
|
|||
|
|
"impl",
|
|||
|
|
"instance",
|
|||
|
|
):
|
|||
|
|
try:
|
|||
|
|
value = getattr(source, attr, None)
|
|||
|
|
except Exception:
|
|||
|
|
value = None
|
|||
|
|
if value is not None:
|
|||
|
|
candidates.append(value)
|
|||
|
|
api = getattr(source, "api", None)
|
|||
|
|
if api is not None:
|
|||
|
|
candidates.append(api)
|
|||
|
|
deduped: list[Any] = []
|
|||
|
|
seen: set[int] = set()
|
|||
|
|
for item in candidates:
|
|||
|
|
marker = id(item)
|
|||
|
|
if marker in seen:
|
|||
|
|
continue
|
|||
|
|
seen.add(marker)
|
|||
|
|
deduped.append(item)
|
|||
|
|
return deduped
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_unique_texts(items: list[str]) -> list[str]:
|
|||
|
|
seen: set[str] = set()
|
|||
|
|
result: list[str] = []
|
|||
|
|
for item in items:
|
|||
|
|
text = str(item or "").strip()
|
|||
|
|
if not text or text in seen:
|
|||
|
|
continue
|
|||
|
|
seen.add(text)
|
|||
|
|
result.append(text)
|
|||
|
|
return result
|
|||
|
|
|
|||
|
|
def _qzone_cookie_domain_candidates(self, configured_domain: str = "") -> list[str]:
|
|||
|
|
domain = str(configured_domain or self._QZONE_COOKIE_DOMAIN or "").strip()
|
|||
|
|
candidates: list[str] = []
|
|||
|
|
if domain:
|
|||
|
|
candidates.append(domain)
|
|||
|
|
if "://" in domain:
|
|||
|
|
parsed = urlparse(domain)
|
|||
|
|
host = parsed.netloc or parsed.path
|
|||
|
|
if host:
|
|||
|
|
candidates.extend([host, f"https://{host}", f"https://{host}/"])
|
|||
|
|
else:
|
|||
|
|
candidates.extend([f"https://{domain}", f"https://{domain}/"])
|
|||
|
|
for fallback in self._QZONE_COOKIE_DOMAIN_FALLBACKS:
|
|||
|
|
candidates.extend([fallback, f"https://{fallback}", f"https://{fallback}/"])
|
|||
|
|
return self._qzone_unique_texts(candidates)
|
|||
|
|
|
|||
|
|
def _qzone_iter_action_callers(self, bot: Any) -> list[Any]:
|
|||
|
|
callers: list[Any] = []
|
|||
|
|
seen_owners: set[int] = set()
|
|||
|
|
seen_callers: set[int] = set()
|
|||
|
|
owners: list[Any] = [bot]
|
|||
|
|
index = 0
|
|||
|
|
while index < len(owners):
|
|||
|
|
owner = owners[index]
|
|||
|
|
index += 1
|
|||
|
|
if owner is None:
|
|||
|
|
continue
|
|||
|
|
marker = id(owner)
|
|||
|
|
if marker in seen_owners:
|
|||
|
|
continue
|
|||
|
|
seen_owners.add(marker)
|
|||
|
|
for attr in self._QZONE_ACTION_CALLER_ATTRS:
|
|||
|
|
try:
|
|||
|
|
caller = getattr(owner, attr, None)
|
|||
|
|
except Exception:
|
|||
|
|
caller = None
|
|||
|
|
if callable(caller) and id(caller) not in seen_callers:
|
|||
|
|
seen_callers.add(id(caller))
|
|||
|
|
callers.append(caller)
|
|||
|
|
for attr in self._QZONE_ACTION_OWNER_ATTRS:
|
|||
|
|
try:
|
|||
|
|
nested = getattr(owner, attr, None)
|
|||
|
|
except Exception:
|
|||
|
|
nested = None
|
|||
|
|
if nested is not None and id(nested) not in seen_owners:
|
|||
|
|
owners.append(nested)
|
|||
|
|
return callers
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_invoke_action_callable(callable_obj: Any, action: str, params: dict[str, Any]) -> Any:
|
|||
|
|
attempts: list[tuple[tuple[Any, ...], dict[str, Any]]] = []
|
|||
|
|
if action:
|
|||
|
|
envelope = dict(params)
|
|||
|
|
attempts.extend(
|
|||
|
|
[
|
|||
|
|
((action,), dict(params)),
|
|||
|
|
((), {"action": action, **params}),
|
|||
|
|
((action, params), {}),
|
|||
|
|
((action,), {"params": params}),
|
|||
|
|
((), {"action": action, "params": params}),
|
|||
|
|
(({"action": action, "params": envelope},), {}),
|
|||
|
|
(({"action": action, "data": envelope},), {}),
|
|||
|
|
(({"action": action, "payload": envelope},), {}),
|
|||
|
|
(({"api": action, "params": envelope},), {}),
|
|||
|
|
(({"api": action, "data": envelope},), {}),
|
|||
|
|
((action,), {"data": params}),
|
|||
|
|
((), {"action": action, "data": params}),
|
|||
|
|
((action,), {"payload": params}),
|
|||
|
|
((), {"action": action, "payload": params}),
|
|||
|
|
]
|
|||
|
|
)
|
|||
|
|
else:
|
|||
|
|
attempts.extend(
|
|||
|
|
[
|
|||
|
|
((), dict(params)),
|
|||
|
|
((params,), {}),
|
|||
|
|
((), {"params": params}),
|
|||
|
|
((), {"data": params}),
|
|||
|
|
((), {"payload": params}),
|
|||
|
|
]
|
|||
|
|
)
|
|||
|
|
last_error: TypeError | None = None
|
|||
|
|
for args, kwargs in attempts:
|
|||
|
|
try:
|
|||
|
|
return callable_obj(*args, **kwargs)
|
|||
|
|
except TypeError as exc:
|
|||
|
|
last_error = exc
|
|||
|
|
continue
|
|||
|
|
if last_error is not None:
|
|||
|
|
raise last_error
|
|||
|
|
return callable_obj(action, **params) if action else callable_obj(**params)
|
|||
|
|
|
|||
|
|
async def _qzone_call_onebot_action(self, bot: Any, action: str, **params: Any) -> Any:
|
|||
|
|
direct = getattr(bot, action, None)
|
|||
|
|
if callable(direct):
|
|||
|
|
result = self._qzone_invoke_action_callable(direct, "", params)
|
|||
|
|
return await result if hasattr(result, "__await__") else result
|
|||
|
|
last_error: Exception | None = None
|
|||
|
|
for caller in self._qzone_iter_action_callers(bot):
|
|||
|
|
try:
|
|||
|
|
result = self._qzone_invoke_action_callable(caller, action, params)
|
|||
|
|
return await result if hasattr(result, "__await__") else result
|
|||
|
|
except Exception as exc:
|
|||
|
|
last_error = exc
|
|||
|
|
continue
|
|||
|
|
if last_error is not None:
|
|||
|
|
raise last_error
|
|||
|
|
raise RuntimeError("OneBot client does not expose get_cookies/get_credentials")
|
|||
|
|
|
|||
|
|
def _qzone_find_runtime_bot(self) -> Any | None:
|
|||
|
|
bot = getattr(self, "_qzone_last_bot", None)
|
|||
|
|
if self._qzone_runtime_bot_usable(bot):
|
|||
|
|
return bot
|
|||
|
|
context = getattr(self, "context", None)
|
|||
|
|
if context is not None:
|
|||
|
|
try:
|
|||
|
|
platform = context.get_platform("aiocqhttp")
|
|||
|
|
except Exception:
|
|||
|
|
platform = None
|
|||
|
|
if platform is not None:
|
|||
|
|
direct_bot = getattr(platform, "bot", None)
|
|||
|
|
if self._qzone_runtime_bot_usable(direct_bot):
|
|||
|
|
self._qzone_last_bot = direct_bot
|
|||
|
|
return direct_bot
|
|||
|
|
for candidate in self._qzone_runtime_bot_candidates(platform):
|
|||
|
|
if self._qzone_runtime_bot_usable(candidate):
|
|||
|
|
self._qzone_last_bot = candidate
|
|||
|
|
return candidate
|
|||
|
|
for candidate in self._qzone_runtime_bot_candidates(bot):
|
|||
|
|
if self._qzone_runtime_bot_usable(candidate):
|
|||
|
|
self._qzone_last_bot = candidate
|
|||
|
|
return candidate
|
|||
|
|
platform_manager = getattr(getattr(self, "context", None), "platform_manager", None)
|
|||
|
|
platform_lists: list[Any] = []
|
|||
|
|
for attr in ("platform_insts", "platform_instances", "instances", "platforms"):
|
|||
|
|
try:
|
|||
|
|
value = getattr(platform_manager, attr, None)
|
|||
|
|
except Exception:
|
|||
|
|
value = None
|
|||
|
|
if value:
|
|||
|
|
platform_lists.append(value.values() if isinstance(value, dict) else value)
|
|||
|
|
for platforms in platform_lists:
|
|||
|
|
try:
|
|||
|
|
iterable = list(platforms or [])
|
|||
|
|
except Exception:
|
|||
|
|
iterable = []
|
|||
|
|
for inst in iterable:
|
|||
|
|
for candidate in self._qzone_runtime_bot_candidates(inst):
|
|||
|
|
if self._qzone_runtime_bot_usable(candidate):
|
|||
|
|
self._qzone_last_bot = candidate
|
|||
|
|
return candidate
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
async def _qzone_try_direct_cookie_fetch(self, bot: Any, domain: str) -> dict[str, str]:
|
|||
|
|
merged: dict[str, str] = {}
|
|||
|
|
login_uin = await self._qzone_fetch_login_uin(bot)
|
|||
|
|
for action in self._QZONE_COOKIE_ACTIONS:
|
|||
|
|
for candidate_domain in self._qzone_cookie_domain_candidates(domain):
|
|||
|
|
for params in ({"domain": candidate_domain}, {}):
|
|||
|
|
try:
|
|||
|
|
result = await asyncio.wait_for(self._qzone_call_onebot_action(bot, action, **params), timeout=8.0)
|
|||
|
|
except Exception:
|
|||
|
|
continue
|
|||
|
|
cookie_text = self._qzone_extract_cookie_text(result)
|
|||
|
|
if not cookie_text:
|
|||
|
|
continue
|
|||
|
|
cookies = self._qzone_parse_cookie_text(cookie_text)
|
|||
|
|
if login_uin and not self._qzone_normalize_uin(cookies):
|
|||
|
|
cookies["uin"] = f"o{login_uin}"
|
|||
|
|
cookies["p_uin"] = f"o{login_uin}"
|
|||
|
|
merged.update(cookies)
|
|||
|
|
if self._qzone_cookie_has_identity_and_secret(merged):
|
|||
|
|
return merged
|
|||
|
|
return merged
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_gtk(p_skey: str) -> str:
|
|||
|
|
hash_val = 5381
|
|||
|
|
for ch in str(p_skey or ""):
|
|||
|
|
hash_val += (hash_val << 5) + ord(ch)
|
|||
|
|
return str(hash_val & 0x7FFFFFFF)
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_normalize_cookie_fields(cookies: dict[str, Any]) -> dict[str, str]:
|
|||
|
|
aliases = {
|
|||
|
|
"pskey": "p_skey",
|
|||
|
|
"p-skey": "p_skey",
|
|||
|
|
"p_skey": "p_skey",
|
|||
|
|
"p_uin": "p_uin",
|
|||
|
|
"ptui_loginuin": "ptui_loginuin",
|
|||
|
|
"csrf-token": "csrf_token",
|
|||
|
|
"csrf_token": "csrf_token",
|
|||
|
|
"bkn": "g_tk",
|
|||
|
|
"gtk": "g_tk",
|
|||
|
|
}
|
|||
|
|
normalized: dict[str, str] = {}
|
|||
|
|
for key, value in (cookies or {}).items():
|
|||
|
|
if value in (None, ""):
|
|||
|
|
continue
|
|||
|
|
original = str(key).strip()
|
|||
|
|
if not original:
|
|||
|
|
continue
|
|||
|
|
text = str(value).strip().strip('"')
|
|||
|
|
if not text:
|
|||
|
|
continue
|
|||
|
|
alias_key = original.lower().replace("-", "_")
|
|||
|
|
canonical = aliases.get(alias_key, aliases.get(original.lower(), original))
|
|||
|
|
normalized.setdefault(original, text)
|
|||
|
|
normalized.setdefault(canonical, text)
|
|||
|
|
if "uin" in normalized and "p_uin" not in normalized:
|
|||
|
|
normalized["p_uin"] = normalized["uin"]
|
|||
|
|
if "p_uin" in normalized and "uin" not in normalized:
|
|||
|
|
normalized["uin"] = normalized["p_uin"]
|
|||
|
|
return normalized
|
|||
|
|
|
|||
|
|
@classmethod
|
|||
|
|
def _qzone_parse_cookie_text(cls, cookie_text: str) -> dict[str, str]:
|
|||
|
|
raw = str(cookie_text or "").strip()
|
|||
|
|
if not raw:
|
|||
|
|
return {}
|
|||
|
|
if raw.lower().startswith("cookie:"):
|
|||
|
|
raw = raw.split(":", 1)[1].strip()
|
|||
|
|
raw = raw.replace("\r", ";").replace("\n", ";")
|
|||
|
|
if raw.startswith(("{", "[")):
|
|||
|
|
try:
|
|||
|
|
payload = json.loads(raw)
|
|||
|
|
except Exception:
|
|||
|
|
payload = None
|
|||
|
|
if isinstance(payload, dict):
|
|||
|
|
return cls._qzone_normalize_cookie_fields(payload)
|
|||
|
|
try:
|
|||
|
|
return cls._qzone_normalize_cookie_fields({key: morsel.value for key, morsel in SimpleCookie(raw).items()})
|
|||
|
|
except Exception:
|
|||
|
|
parsed: dict[str, str] = {}
|
|||
|
|
for part in raw.split(";"):
|
|||
|
|
if "=" not in part:
|
|||
|
|
continue
|
|||
|
|
key, value = part.split("=", 1)
|
|||
|
|
key = key.strip()
|
|||
|
|
if key:
|
|||
|
|
parsed[key] = value.strip().strip('"')
|
|||
|
|
return cls._qzone_normalize_cookie_fields(parsed)
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_cookie_header(cookies: dict[str, Any]) -> str:
|
|||
|
|
return "; ".join(f"{key}={value}" for key, value in (cookies or {}).items() if key and value not in (None, ""))
|
|||
|
|
|
|||
|
|
def _qzone_extract_cookie_text(self, payload: Any, *, _depth: int = 0, _seen: set[int] | None = None) -> str:
|
|||
|
|
if _seen is None:
|
|||
|
|
_seen = set()
|
|||
|
|
if payload is None or _depth > 8:
|
|||
|
|
return ""
|
|||
|
|
if isinstance(payload, bytes):
|
|||
|
|
try:
|
|||
|
|
payload = payload.decode("utf-8")
|
|||
|
|
except Exception:
|
|||
|
|
return ""
|
|||
|
|
if isinstance(payload, str):
|
|||
|
|
text = payload.strip()
|
|||
|
|
if text.startswith(("{", "[")):
|
|||
|
|
try:
|
|||
|
|
return self._qzone_extract_cookie_text(json.loads(text), _depth=_depth + 1, _seen=_seen)
|
|||
|
|
except Exception:
|
|||
|
|
pass
|
|||
|
|
return text if "=" in text and re.search(r"\b(?:uin|p_uin|skey|p_skey|pskey|g_tk|gtk|bkn)\s*=", text, re.I) else ""
|
|||
|
|
if isinstance(payload, (list, tuple)):
|
|||
|
|
parts = [self._qzone_extract_cookie_text(item, _depth=_depth + 1, _seen=_seen) for item in payload]
|
|||
|
|
cookies: dict[str, str] = {}
|
|||
|
|
for part in parts:
|
|||
|
|
cookies.update(self._qzone_parse_cookie_text(part))
|
|||
|
|
return self._qzone_cookie_header(cookies)
|
|||
|
|
if not isinstance(payload, dict):
|
|||
|
|
return ""
|
|||
|
|
obj_id = id(payload)
|
|||
|
|
if obj_id in _seen:
|
|||
|
|
return ""
|
|||
|
|
_seen.add(obj_id)
|
|||
|
|
name = payload.get("name") or payload.get("key")
|
|||
|
|
value = payload.get("value")
|
|||
|
|
if name and value not in (None, ""):
|
|||
|
|
return f"{name}={value}"
|
|||
|
|
cookie_keys = set(self._QZONE_COOKIE_VALUE_KEYS)
|
|||
|
|
allow = {
|
|||
|
|
"uin",
|
|||
|
|
"p_uin",
|
|||
|
|
"ptui_loginuin",
|
|||
|
|
"luin",
|
|||
|
|
"skey",
|
|||
|
|
"p_skey",
|
|||
|
|
"pskey",
|
|||
|
|
"skey2",
|
|||
|
|
"pt4_token",
|
|||
|
|
"pt_key",
|
|||
|
|
"pt_login_sig",
|
|||
|
|
"clientkey",
|
|||
|
|
"superkey",
|
|||
|
|
"qzonetoken",
|
|||
|
|
"qm_keyst",
|
|||
|
|
"qm_sid",
|
|||
|
|
"o_cookie",
|
|||
|
|
"uin_cookie",
|
|||
|
|
"rv2",
|
|||
|
|
"ptcz",
|
|||
|
|
"lskey",
|
|||
|
|
"ldw",
|
|||
|
|
"g_tk",
|
|||
|
|
"gtk",
|
|||
|
|
"bkn",
|
|||
|
|
"csrf_token",
|
|||
|
|
"qqmusic_key",
|
|||
|
|
}
|
|||
|
|
cookies = {
|
|||
|
|
str(key): value
|
|||
|
|
for key, value in payload.items()
|
|||
|
|
if str(key).lower().replace("-", "_") in allow and value not in (None, "")
|
|||
|
|
}
|
|||
|
|
parts = [self._qzone_cookie_header(self._qzone_normalize_cookie_fields(cookies))] if cookies else []
|
|||
|
|
for key in cookie_keys:
|
|||
|
|
if key in payload:
|
|||
|
|
text = self._qzone_extract_cookie_text(payload.get(key), _depth=_depth + 1, _seen=_seen)
|
|||
|
|
if text:
|
|||
|
|
parts.append(text)
|
|||
|
|
for value in payload.values():
|
|||
|
|
if isinstance(value, (dict, list, tuple, str, bytes)):
|
|||
|
|
text = self._qzone_extract_cookie_text(value, _depth=_depth + 1, _seen=_seen)
|
|||
|
|
if text:
|
|||
|
|
parts.append(text)
|
|||
|
|
merged: dict[str, str] = {}
|
|||
|
|
for part in parts:
|
|||
|
|
merged.update(self._qzone_parse_cookie_text(part))
|
|||
|
|
return self._qzone_cookie_header(merged)
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_normalize_uin(cookies: dict[str, Any]) -> int:
|
|||
|
|
for key in ("uin", "p_uin", "ptui_loginuin", "luin"):
|
|||
|
|
raw = str(cookies.get(key) or "").strip().lstrip("oO")
|
|||
|
|
if raw.isdigit():
|
|||
|
|
return int(raw)
|
|||
|
|
return 0
|
|||
|
|
|
|||
|
|
def _qzone_cookie_has_identity_and_secret(self, cookies: dict[str, Any]) -> bool:
|
|||
|
|
normalized = self._qzone_normalize_cookie_fields(cookies or {})
|
|||
|
|
return bool(
|
|||
|
|
self._qzone_normalize_uin(normalized)
|
|||
|
|
and any(str(normalized.get(key) or "").strip() for key in self._QZONE_COOKIE_SECRET_KEYS)
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
def _qzone_note_cookie_fetch_status(
|
|||
|
|
self,
|
|||
|
|
status: str,
|
|||
|
|
*,
|
|||
|
|
cookies: dict[str, Any] | None = None,
|
|||
|
|
ctx: dict[str, Any] | None = None,
|
|||
|
|
reason: str = "",
|
|||
|
|
) -> None:
|
|||
|
|
try:
|
|||
|
|
state = self.data.setdefault("qzone_integration", {})
|
|||
|
|
if not isinstance(state, dict):
|
|||
|
|
self.data["qzone_integration"] = {}
|
|||
|
|
state = self.data["qzone_integration"]
|
|||
|
|
source = ctx.get("cookies") if isinstance(ctx, dict) else cookies
|
|||
|
|
normalized = self._qzone_normalize_cookie_fields(source or {})
|
|||
|
|
state["last_cookie_fetch_status"] = _single_line(status, 40)
|
|||
|
|
state["last_cookie_fetch_at"] = _now_ts()
|
|||
|
|
state["last_cookie_fetch_has_uin"] = bool(self._qzone_normalize_uin(normalized))
|
|||
|
|
state["last_cookie_fetch_has_skey"] = bool(normalized.get("skey"))
|
|||
|
|
state["last_cookie_fetch_has_p_skey"] = bool(normalized.get("p_skey") or normalized.get("pskey"))
|
|||
|
|
if isinstance(ctx, dict) and ctx.get("uin"):
|
|||
|
|
state["last_cookie_fetch_uin"] = str(ctx.get("uin"))
|
|||
|
|
if reason:
|
|||
|
|
state["last_cookie_fetch_reason"] = _single_line(reason, 160)
|
|||
|
|
elif status == "ok":
|
|||
|
|
state.pop("last_cookie_fetch_reason", None)
|
|||
|
|
if callable(getattr(self, "_save_data_sync", None)):
|
|||
|
|
self._save_data_sync()
|
|||
|
|
except Exception:
|
|||
|
|
logger.debug("[PrivateCompanion] QQ 空间 Cookie 状态记录失败", exc_info=True)
|
|||
|
|
|
|||
|
|
async def _qzone_fetch_login_uin(self, bot: Any) -> int:
|
|||
|
|
for action in self._QZONE_LOGIN_INFO_ACTIONS:
|
|||
|
|
try:
|
|||
|
|
payload = await asyncio.wait_for(self._qzone_call_onebot_action(bot, action), timeout=5.0)
|
|||
|
|
except Exception:
|
|||
|
|
continue
|
|||
|
|
if isinstance(payload, str) and payload.strip().startswith(("{", "[")):
|
|||
|
|
try:
|
|||
|
|
payload = json.loads(payload)
|
|||
|
|
except Exception:
|
|||
|
|
pass
|
|||
|
|
candidates: list[Any] = []
|
|||
|
|
if isinstance(payload, dict):
|
|||
|
|
candidates.extend(
|
|||
|
|
[
|
|||
|
|
payload.get("user_id"),
|
|||
|
|
payload.get("uin"),
|
|||
|
|
payload.get("qq"),
|
|||
|
|
payload.get("self_id"),
|
|||
|
|
]
|
|||
|
|
)
|
|||
|
|
for key in ("data", "result", "retdata", "payload", "response"):
|
|||
|
|
nested = payload.get(key)
|
|||
|
|
if isinstance(nested, dict):
|
|||
|
|
candidates.extend([nested.get("user_id"), nested.get("uin"), nested.get("qq"), nested.get("self_id")])
|
|||
|
|
for value in candidates:
|
|||
|
|
cleaned = str(value or "").strip().lstrip("oO")
|
|||
|
|
if cleaned.isdigit():
|
|||
|
|
return int(cleaned)
|
|||
|
|
return 0
|
|||
|
|
|
|||
|
|
def _qzone_context_from_cookies(self, cookies_str: str) -> dict[str, Any]:
|
|||
|
|
parsed = self._qzone_parse_cookie_text(cookies_str)
|
|||
|
|
uin = self._qzone_normalize_uin(parsed)
|
|||
|
|
if not uin:
|
|||
|
|
raise RuntimeError("Cookie 中缺少合法 uin")
|
|||
|
|
p_skey = parsed.get("p_skey") or parsed.get("pskey") or ""
|
|||
|
|
skey = parsed.get("skey") or ""
|
|||
|
|
existing_gtk = str(parsed.get("g_tk") or parsed.get("gtk") or parsed.get("bkn") or parsed.get("csrf_token") or "")
|
|||
|
|
secret = p_skey or skey or parsed.get("skey2") or ""
|
|||
|
|
gtk = self._qzone_gtk(secret) if secret else (existing_gtk if existing_gtk.isdigit() else "")
|
|||
|
|
if not gtk:
|
|||
|
|
raise RuntimeError("Cookie 中缺少 p_skey/skey,无法计算 g_tk")
|
|||
|
|
cookies = {**parsed, "uin": f"o{uin}"}
|
|||
|
|
if skey:
|
|||
|
|
cookies["skey"] = skey
|
|||
|
|
if p_skey:
|
|||
|
|
cookies["p_skey"] = p_skey
|
|||
|
|
return {
|
|||
|
|
"uin": int(uin),
|
|||
|
|
"skey": skey,
|
|||
|
|
"p_skey": p_skey,
|
|||
|
|
"qzonetoken": parsed.get("qzonetoken") or parsed.get("qzone_token") or "",
|
|||
|
|
"gtk": gtk,
|
|||
|
|
"cookies": cookies,
|
|||
|
|
"cookie_header": self._qzone_cookie_header(cookies),
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async def _qzone_get_cookies(self, event: AstrMessageEvent | None = None) -> str:
|
|||
|
|
if not self._qzone_platform_supported(event):
|
|||
|
|
raise QzoneIntegrationError("平台不支持", self._qzone_platform_unavailable_message())
|
|||
|
|
manual_cookie = str(getattr(self, "qzone_cookie", "") or "").strip()
|
|||
|
|
if manual_cookie:
|
|||
|
|
try:
|
|||
|
|
ctx = self._qzone_context_from_cookies(manual_cookie)
|
|||
|
|
except Exception as exc:
|
|||
|
|
self._qzone_note_cookie_fetch_status("manual_failed", cookies=self._qzone_parse_cookie_text(manual_cookie), reason=str(exc))
|
|||
|
|
raise RuntimeError(
|
|||
|
|
"手动 QZONE_COOKIE 不可用:"
|
|||
|
|
f"{_single_line(exc, 120)};"
|
|||
|
|
"需包含 uin/p_uin 与 p_skey 或 skey,可从已登录 QQ 空间的浏览器请求头 Cookie 复制"
|
|||
|
|
) from exc
|
|||
|
|
logger.debug("[PrivateCompanion] QQ 空间使用手动 QZONE_COOKIE: uin=%s", ctx.get("uin"))
|
|||
|
|
self._qzone_note_cookie_fetch_status("manual_ok", ctx=ctx)
|
|||
|
|
return ctx["cookie_header"]
|
|||
|
|
bot = getattr(event, "bot", None) if event is not None else None
|
|||
|
|
if bot is not None:
|
|||
|
|
usable_bot = None
|
|||
|
|
for candidate in self._qzone_runtime_bot_candidates(bot):
|
|||
|
|
if self._qzone_runtime_bot_usable(candidate):
|
|||
|
|
usable_bot = candidate
|
|||
|
|
break
|
|||
|
|
bot = usable_bot or bot
|
|||
|
|
if bot is None or not self._qzone_runtime_bot_usable(bot):
|
|||
|
|
bot = self._qzone_find_runtime_bot()
|
|||
|
|
if bot is not None:
|
|||
|
|
self._qzone_last_bot = bot
|
|||
|
|
if bot is None:
|
|||
|
|
self._qzone_note_cookie_fetch_status("failed", reason="没有可用的 OneBot 连接,且未配置手动 QZONE_COOKIE")
|
|||
|
|
raise RuntimeError(
|
|||
|
|
"没有可用的 OneBot 连接,且未配置手动 QZONE_COOKIE;"
|
|||
|
|
"请在配置页填写浏览器 QQ 空间 Cookie,或确认 OneBot 已连接并支持 get_cookies/get_credentials"
|
|||
|
|
)
|
|||
|
|
merged = await self._qzone_try_direct_cookie_fetch(bot, self._QZONE_COOKIE_DOMAIN)
|
|||
|
|
if self._qzone_cookie_has_identity_and_secret(merged):
|
|||
|
|
cookie_text = self._qzone_cookie_header(self._qzone_normalize_cookie_fields(merged))
|
|||
|
|
ctx = self._qzone_context_from_cookies(cookie_text)
|
|||
|
|
logger.debug("[PrivateCompanion] QQ 空间自动获取 Cookie 成功: uin=%s", ctx.get("uin"))
|
|||
|
|
self._qzone_note_cookie_fetch_status("ok", ctx=ctx)
|
|||
|
|
return ctx["cookie_header"]
|
|||
|
|
self._qzone_note_cookie_fetch_status("failed", cookies=merged)
|
|||
|
|
raise RuntimeError(
|
|||
|
|
"获取 QQ 空间 Cookie 失败"
|
|||
|
|
f"(uin={'有' if self._qzone_normalize_uin(merged) else '无'}"
|
|||
|
|
f",skey={'有' if bool(merged.get('skey')) else '无'}"
|
|||
|
|
f",p_skey={'有' if bool(merged.get('p_skey') or merged.get('pskey')) else '无'});"
|
|||
|
|
"可改填手动 QZONE_COOKIE,需包含 uin/p_uin 与 p_skey 或 skey"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_response_object_candidates(raw: str) -> list[str]:
|
|||
|
|
source = str(raw or "")
|
|||
|
|
candidates: list[str] = []
|
|||
|
|
for start, char in enumerate(source):
|
|||
|
|
if char != "{":
|
|||
|
|
continue
|
|||
|
|
depth = 0
|
|||
|
|
quote = ""
|
|||
|
|
escaped = False
|
|||
|
|
for index in range(start, len(source)):
|
|||
|
|
current = source[index]
|
|||
|
|
if quote:
|
|||
|
|
if escaped:
|
|||
|
|
escaped = False
|
|||
|
|
elif current == "\\":
|
|||
|
|
escaped = True
|
|||
|
|
elif current == quote:
|
|||
|
|
quote = ""
|
|||
|
|
continue
|
|||
|
|
if current in {"'", '"'}:
|
|||
|
|
quote = current
|
|||
|
|
continue
|
|||
|
|
if current == "{":
|
|||
|
|
depth += 1
|
|||
|
|
continue
|
|||
|
|
if current == "}":
|
|||
|
|
depth -= 1
|
|||
|
|
if depth == 0:
|
|||
|
|
candidates.append(source[start : index + 1])
|
|||
|
|
break
|
|||
|
|
if len(candidates) >= 24:
|
|||
|
|
break
|
|||
|
|
return candidates
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_load_response_payload(payload: str) -> dict[str, Any]:
|
|||
|
|
normalized = str(payload or "").replace("undefined", "null")
|
|||
|
|
try:
|
|||
|
|
parsed = json.loads(normalized)
|
|||
|
|
except Exception:
|
|||
|
|
try:
|
|||
|
|
relaxed = re.sub(r",\s*([}\]])", r"\1", normalized)
|
|||
|
|
relaxed = re.sub(r"([{,]\s*)([A-Za-z_$][\w$]*)\s*:", r'\1"\2":', relaxed)
|
|||
|
|
parsed = json.loads(relaxed)
|
|||
|
|
except Exception:
|
|||
|
|
import json5 # type: ignore
|
|||
|
|
|
|||
|
|
parsed = json5.loads(normalized)
|
|||
|
|
if isinstance(parsed, dict):
|
|||
|
|
return parsed
|
|||
|
|
return {"code": -1, "message": "接口响应不是对象"}
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_parse_response(text: str) -> dict[str, Any]:
|
|||
|
|
raw = str(text or "")
|
|||
|
|
if not raw.strip():
|
|||
|
|
return {"code": -1, "message": "接口返回空响应"}
|
|||
|
|
candidates = QzoneMixin._qzone_response_object_candidates(raw)
|
|||
|
|
if not candidates:
|
|||
|
|
return {"code": -1, "message": "接口响应缺少 JSON"}
|
|||
|
|
|
|||
|
|
def normalize(parsed: dict[str, Any]) -> dict[str, Any]:
|
|||
|
|
if isinstance(parsed.get("data"), dict):
|
|||
|
|
nested = dict(parsed.get("data") or {})
|
|||
|
|
nested.setdefault("_raw_code", parsed.get("code", parsed.get("ret")))
|
|||
|
|
nested.setdefault("_raw_message", parsed.get("message") or parsed.get("msg"))
|
|||
|
|
return nested
|
|||
|
|
return parsed
|
|||
|
|
|
|||
|
|
first_object: dict[str, Any] | None = None
|
|||
|
|
last_error = ""
|
|||
|
|
response_keys = {"code", "ret", "message", "msg", "data", "subcode"}
|
|||
|
|
for payload in candidates:
|
|||
|
|
try:
|
|||
|
|
parsed = QzoneMixin._qzone_load_response_payload(payload)
|
|||
|
|
except Exception as exc:
|
|||
|
|
last_error = _single_line(exc, 80)
|
|||
|
|
continue
|
|||
|
|
if not isinstance(parsed, dict):
|
|||
|
|
continue
|
|||
|
|
if first_object is None:
|
|||
|
|
first_object = parsed
|
|||
|
|
if response_keys & set(parsed):
|
|||
|
|
return normalize(parsed)
|
|||
|
|
if first_object is not None:
|
|||
|
|
return normalize(first_object)
|
|||
|
|
return {"code": -1, "message": f"JSON 解析失败:{last_error or '没有可解析对象'}"}
|
|||
|
|
|
|||
|
|
async def _qzone_request(
|
|||
|
|
self,
|
|||
|
|
event: AstrMessageEvent | None,
|
|||
|
|
method: str,
|
|||
|
|
url: str,
|
|||
|
|
*,
|
|||
|
|
params: dict[str, Any] | None = None,
|
|||
|
|
data: dict[str, Any] | None = None,
|
|||
|
|
headers: dict[str, str] | None = None,
|
|||
|
|
timeout_seconds: float = 20.0,
|
|||
|
|
cookie_header: str | None = None,
|
|||
|
|
) -> dict[str, Any]:
|
|||
|
|
import aiohttp
|
|||
|
|
|
|||
|
|
if cookie_header is None:
|
|||
|
|
cookie_header = await self._qzone_get_cookies(event)
|
|||
|
|
ctx = self._qzone_context_from_cookies(cookie_header)
|
|||
|
|
parsed_url = urlparse(url)
|
|||
|
|
origin = f"{parsed_url.scheme}://{parsed_url.netloc}" if parsed_url.scheme and parsed_url.netloc else "https://user.qzone.qq.com"
|
|||
|
|
request_headers = {
|
|||
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
|
|||
|
|
"Cookie": ctx["cookie_header"],
|
|||
|
|
"Referer": f"https://user.qzone.qq.com/{ctx['uin']}",
|
|||
|
|
"Origin": "https://user.qzone.qq.com",
|
|||
|
|
"Host": parsed_url.netloc or "user.qzone.qq.com",
|
|||
|
|
"Connection": "keep-alive",
|
|||
|
|
}
|
|||
|
|
if headers:
|
|||
|
|
request_headers.update(headers)
|
|||
|
|
timeout = aiohttp.ClientTimeout(total=timeout_seconds)
|
|||
|
|
async with aiohttp.ClientSession(timeout=timeout, headers=request_headers) as session:
|
|||
|
|
async with session.request(method, url, params=params, data=data) as response:
|
|||
|
|
text = await response.text()
|
|||
|
|
parsed = self._qzone_parse_response(text)
|
|||
|
|
parsed.setdefault("_http_status", response.status)
|
|||
|
|
if parsed.get("message") == "接口返回空响应":
|
|||
|
|
parsed["message"] = f"接口返回空响应(HTTP {response.status})"
|
|||
|
|
if response.status == 403 and parsed.get("code") in {-1, None}:
|
|||
|
|
parsed["message"] = "无权限访问 QQ 空间或 Cookie 已失效"
|
|||
|
|
return parsed
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_norm_key(key: Any) -> str:
|
|||
|
|
return str(key or "").strip().lower().replace("-", "_")
|
|||
|
|
|
|||
|
|
@classmethod
|
|||
|
|
def _qzone_comment_content(cls, item: dict[str, Any]) -> str:
|
|||
|
|
normalized = {cls._qzone_norm_key(key): value for key, value in (item or {}).items()}
|
|||
|
|
raw = ""
|
|||
|
|
for key in ("content", "comment", "text", "msg", "con", "html"):
|
|||
|
|
value = normalized.get(key)
|
|||
|
|
if value not in (None, ""):
|
|||
|
|
raw = str(value)
|
|||
|
|
break
|
|||
|
|
if not raw:
|
|||
|
|
return ""
|
|||
|
|
cleaned = html.unescape(re.sub(r"<[^>]+>", "", raw))
|
|||
|
|
cleaned = re.sub(r"\s+", " ", cleaned).strip()
|
|||
|
|
return _single_line(cleaned, 180)
|
|||
|
|
|
|||
|
|
@classmethod
|
|||
|
|
def _qzone_comment_identity(cls, item: dict[str, Any]) -> tuple[int, str]:
|
|||
|
|
normalized = {cls._qzone_norm_key(key): value for key, value in (item or {}).items()}
|
|||
|
|
raw_uin = str(normalized.get("uin") or normalized.get("user_uin") or normalized.get("qq") or normalized.get("uin_str") or "").strip().lstrip("oO")
|
|||
|
|
uin = _safe_int(raw_uin, 0, 0)
|
|||
|
|
name = ""
|
|||
|
|
for key in ("name", "nickname", "nick", "user_name", "username"):
|
|||
|
|
value = normalized.get(key)
|
|||
|
|
if value not in (None, ""):
|
|||
|
|
name = _single_line(value, 40)
|
|||
|
|
break
|
|||
|
|
return uin, name
|
|||
|
|
|
|||
|
|
@classmethod
|
|||
|
|
def _qzone_comment_time(cls, item: dict[str, Any]) -> float:
|
|||
|
|
normalized = {cls._qzone_norm_key(key): value for key, value in (item or {}).items()}
|
|||
|
|
for key in ("create_time", "created_time", "time", "timestamp", "abstime", "pubtime"):
|
|||
|
|
value = normalized.get(key)
|
|||
|
|
if value not in (None, ""):
|
|||
|
|
return _safe_float(value, 0)
|
|||
|
|
return 0.0
|
|||
|
|
|
|||
|
|
@classmethod
|
|||
|
|
def _qzone_comment_id(cls, post_tid: str, item: dict[str, Any]) -> str:
|
|||
|
|
normalized = {cls._qzone_norm_key(key): value for key, value in (item or {}).items()}
|
|||
|
|
for key in ("commentid", "comment_id", "cid", "id", "tid", "replyid", "reply_id", "cellid", "rootid"):
|
|||
|
|
value = normalized.get(key)
|
|||
|
|
if value not in (None, ""):
|
|||
|
|
return f"{post_tid or 'post'}:{_single_line(value, 80)}"
|
|||
|
|
return cls._qzone_comment_fingerprint(post_tid, item)
|
|||
|
|
|
|||
|
|
@classmethod
|
|||
|
|
def _qzone_comment_legacy_fallback_id(cls, post_tid: str, item: dict[str, Any]) -> str:
|
|||
|
|
uin, name = cls._qzone_comment_identity(item)
|
|||
|
|
content = cls._qzone_comment_content(item)
|
|||
|
|
created = cls._qzone_comment_time(item)
|
|||
|
|
digest = hashlib.sha1(f"{post_tid}|{uin}|{name}|{content}|{created}".encode("utf-8", "ignore")).hexdigest()[:20]
|
|||
|
|
return f"{post_tid or 'post'}:sha1:{digest}"
|
|||
|
|
|
|||
|
|
@classmethod
|
|||
|
|
def _qzone_comment_fingerprint(cls, post_tid: str, item: dict[str, Any]) -> str:
|
|||
|
|
uin, name = cls._qzone_comment_identity(item)
|
|||
|
|
content = cls._qzone_comment_content(item)
|
|||
|
|
author = str(uin or "").strip()
|
|||
|
|
if not author:
|
|||
|
|
author = re.sub(r"\s+", "", _single_line(name, 40).lower()) or "unknown"
|
|||
|
|
normalized_content = re.sub(r"\s+", "", _single_line(content, 180)).lower()
|
|||
|
|
digest = hashlib.sha1(f"{post_tid or 'post'}|{author}|{normalized_content}".encode("utf-8", "ignore")).hexdigest()[:20]
|
|||
|
|
return f"{post_tid or 'post'}:fp:{digest}"
|
|||
|
|
|
|||
|
|
@classmethod
|
|||
|
|
def _qzone_looks_like_comment(cls, item: Any) -> bool:
|
|||
|
|
if not isinstance(item, dict):
|
|||
|
|
return False
|
|||
|
|
if not cls._qzone_comment_content(item):
|
|||
|
|
return False
|
|||
|
|
normalized = {cls._qzone_norm_key(key) for key in item.keys()}
|
|||
|
|
identity_keys = {
|
|||
|
|
"uin",
|
|||
|
|
"user_uin",
|
|||
|
|
"qq",
|
|||
|
|
"name",
|
|||
|
|
"nickname",
|
|||
|
|
"nick",
|
|||
|
|
"commentid",
|
|||
|
|
"comment_id",
|
|||
|
|
"cid",
|
|||
|
|
"replyid",
|
|||
|
|
"reply_id",
|
|||
|
|
"create_time",
|
|||
|
|
"created_time",
|
|||
|
|
"abstime",
|
|||
|
|
}
|
|||
|
|
return bool(normalized & identity_keys)
|
|||
|
|
|
|||
|
|
@classmethod
|
|||
|
|
def _qzone_collect_comment_items(
|
|||
|
|
cls,
|
|||
|
|
payload: Any,
|
|||
|
|
*,
|
|||
|
|
_depth: int = 0,
|
|||
|
|
_inside_comment_branch: bool = False,
|
|||
|
|
) -> list[dict[str, Any]]:
|
|||
|
|
if payload is None or _depth > 5:
|
|||
|
|
return []
|
|||
|
|
if isinstance(payload, list):
|
|||
|
|
items: list[dict[str, Any]] = []
|
|||
|
|
for entry in payload:
|
|||
|
|
if cls._qzone_looks_like_comment(entry):
|
|||
|
|
items.append(entry)
|
|||
|
|
elif isinstance(entry, (dict, list)):
|
|||
|
|
items.extend(
|
|||
|
|
cls._qzone_collect_comment_items(
|
|||
|
|
entry,
|
|||
|
|
_depth=_depth + 1,
|
|||
|
|
_inside_comment_branch=_inside_comment_branch,
|
|||
|
|
)
|
|||
|
|
)
|
|||
|
|
return items
|
|||
|
|
if not isinstance(payload, dict):
|
|||
|
|
return []
|
|||
|
|
items: list[dict[str, Any]] = []
|
|||
|
|
if _inside_comment_branch and cls._qzone_looks_like_comment(payload):
|
|||
|
|
return [payload]
|
|||
|
|
for key, value in payload.items():
|
|||
|
|
norm = cls._qzone_norm_key(key)
|
|||
|
|
is_comment_branch = _inside_comment_branch or any(token in norm for token in ("comment", "reply"))
|
|||
|
|
if not is_comment_branch:
|
|||
|
|
continue
|
|||
|
|
if cls._qzone_looks_like_comment(value):
|
|||
|
|
items.append(value)
|
|||
|
|
elif isinstance(value, (dict, list)):
|
|||
|
|
items.extend(
|
|||
|
|
cls._qzone_collect_comment_items(
|
|||
|
|
value,
|
|||
|
|
_depth=_depth + 1,
|
|||
|
|
_inside_comment_branch=True,
|
|||
|
|
)
|
|||
|
|
)
|
|||
|
|
return items
|
|||
|
|
|
|||
|
|
@classmethod
|
|||
|
|
def _qzone_parse_comments_from_msg(cls, msg: dict[str, Any]) -> list[Any]:
|
|||
|
|
post_tid = str(msg.get("tid") or "")
|
|||
|
|
seen: set[str] = set()
|
|||
|
|
comments: list[Any] = []
|
|||
|
|
for item in cls._qzone_collect_comment_items(msg):
|
|||
|
|
if not isinstance(item, dict):
|
|||
|
|
continue
|
|||
|
|
content = cls._qzone_comment_content(item)
|
|||
|
|
if not content:
|
|||
|
|
continue
|
|||
|
|
comment_id = cls._qzone_comment_id(post_tid, item)
|
|||
|
|
if comment_id in seen:
|
|||
|
|
continue
|
|||
|
|
seen.add(comment_id)
|
|||
|
|
uin, name = cls._qzone_comment_identity(item)
|
|||
|
|
comment_key = cls._qzone_comment_fingerprint(post_tid, item)
|
|||
|
|
comments.append(
|
|||
|
|
SimpleNamespace(
|
|||
|
|
comment_id=comment_id,
|
|||
|
|
comment_key=comment_key,
|
|||
|
|
comment_legacy_id=cls._qzone_comment_legacy_fallback_id(post_tid, item),
|
|||
|
|
uin=uin,
|
|||
|
|
name=name,
|
|||
|
|
content=content,
|
|||
|
|
create_time=cls._qzone_comment_time(item),
|
|||
|
|
raw=item,
|
|||
|
|
)
|
|||
|
|
)
|
|||
|
|
comments.sort(key=lambda item: _safe_float(getattr(item, "create_time", 0), 0))
|
|||
|
|
return comments
|
|||
|
|
|
|||
|
|
def _qzone_parse_feeds(self, msglist: list[Any]) -> list[Any]:
|
|||
|
|
posts: list[Any] = []
|
|||
|
|
for msg in msglist:
|
|||
|
|
if not isinstance(msg, dict):
|
|||
|
|
continue
|
|||
|
|
images: list[str] = []
|
|||
|
|
for image in msg.get("pic", []) if isinstance(msg.get("pic"), list) else []:
|
|||
|
|
if not isinstance(image, dict):
|
|||
|
|
continue
|
|||
|
|
for key in ("url2", "url3", "url1", "smallurl"):
|
|||
|
|
raw = image.get(key)
|
|||
|
|
if raw:
|
|||
|
|
images.append(str(raw))
|
|||
|
|
break
|
|||
|
|
for video in msg.get("video", []) if isinstance(msg.get("video"), list) else []:
|
|||
|
|
if isinstance(video, dict) and (video.get("url1") or video.get("pic_url")):
|
|||
|
|
images.append(str(video.get("url1") or video.get("pic_url")))
|
|||
|
|
posts.append(
|
|||
|
|
SimpleNamespace(
|
|||
|
|
tid=str(msg.get("tid") or ""),
|
|||
|
|
uin=int(msg.get("uin") or 0),
|
|||
|
|
name=str(msg.get("name") or ""),
|
|||
|
|
text=str(msg.get("content") or "").strip(),
|
|||
|
|
rt_con=str((msg.get("rt_con") or {}).get("content") or "") if isinstance(msg.get("rt_con"), dict) else "",
|
|||
|
|
images=images,
|
|||
|
|
comments=self._qzone_parse_comments_from_msg(msg),
|
|||
|
|
create_time=msg.get("created_time") or 0,
|
|||
|
|
appid=str(msg.get("appid") or "311"),
|
|||
|
|
typeid=str(msg.get("typeid") or msg.get("type") or "0"),
|
|||
|
|
abstime=_safe_int(msg.get("created_time") or msg.get("abstime"), 0, 0),
|
|||
|
|
fid=str(msg.get("tid") or msg.get("fid") or ""),
|
|||
|
|
unikey=str(msg.get("unikey") or msg.get("likeKey") or msg.get("like_key") or ""),
|
|||
|
|
curkey=str(msg.get("curkey") or msg.get("curlikekey") or msg.get("likeKey") or msg.get("like_key") or ""),
|
|||
|
|
raw=msg,
|
|||
|
|
status="approved",
|
|||
|
|
)
|
|||
|
|
)
|
|||
|
|
return posts
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_post_value(post: Any, key: str, default: Any = "") -> Any:
|
|||
|
|
value = getattr(post, key, None)
|
|||
|
|
if value not in (None, ""):
|
|||
|
|
return value
|
|||
|
|
raw = getattr(post, "raw", None)
|
|||
|
|
if isinstance(raw, dict):
|
|||
|
|
value = raw.get(key)
|
|||
|
|
if value not in (None, ""):
|
|||
|
|
return value
|
|||
|
|
return default
|
|||
|
|
|
|||
|
|
def _qzone_post_like_url(self, post: Any, *, uin: str, tid: str) -> str:
|
|||
|
|
raw = getattr(post, "raw", None)
|
|||
|
|
for key in ("unikey", "curkey", "curlikekey", "likeKey", "like_key", "url"):
|
|||
|
|
value = getattr(post, key, None)
|
|||
|
|
if value not in (None, ""):
|
|||
|
|
return str(value)
|
|||
|
|
if isinstance(raw, dict) and raw.get(key) not in (None, ""):
|
|||
|
|
return str(raw.get(key))
|
|||
|
|
html_text = str(raw.get("html") or "") if isinstance(raw, dict) else ""
|
|||
|
|
if html_text:
|
|||
|
|
for attr in ("data-unikey", "data-curkey", "unikey", "curkey"):
|
|||
|
|
match = re.search(rf"""{re.escape(attr)}\s*=\s*["']([^"']+)["']""", html_text, flags=re.IGNORECASE)
|
|||
|
|
if match:
|
|||
|
|
return html.unescape(match.group(1)).strip()
|
|||
|
|
return f"https://user.qzone.qq.com/{uin}/mood/{tid}"
|
|||
|
|
|
|||
|
|
async def _qzone_query_feeds(
|
|||
|
|
self,
|
|||
|
|
event: AstrMessageEvent | None = None,
|
|||
|
|
*,
|
|||
|
|
target_id: str | None = None,
|
|||
|
|
pos: int = 0,
|
|||
|
|
num: int = 1,
|
|||
|
|
with_detail: bool = False,
|
|||
|
|
cookie_header: str | None = None,
|
|||
|
|
) -> list[Any]:
|
|||
|
|
if cookie_header is None:
|
|||
|
|
cookie_header = await self._qzone_get_cookies(event)
|
|||
|
|
ctx = self._qzone_context_from_cookies(cookie_header)
|
|||
|
|
target = _single_line(target_id, 40)
|
|||
|
|
if not target:
|
|||
|
|
target = str(ctx["uin"])
|
|||
|
|
payload = await self._qzone_request(
|
|||
|
|
event,
|
|||
|
|
"GET",
|
|||
|
|
"https://user.qzone.qq.com/proxy/domain/taotao.qq.com/cgi-bin/emotion_cgi_msglist_v6",
|
|||
|
|
params={
|
|||
|
|
"g_tk": ctx["gtk"],
|
|||
|
|
"uin": target,
|
|||
|
|
"ftype": 0,
|
|||
|
|
"sort": 0,
|
|||
|
|
"pos": max(0, int(pos or 0)),
|
|||
|
|
"num": max(1, int(num or 1)),
|
|||
|
|
"replynum": 100,
|
|||
|
|
"callback": "_preloadCallback",
|
|||
|
|
"code_version": 1,
|
|||
|
|
"format": "json",
|
|||
|
|
"need_comment": 1 if with_detail else 0,
|
|||
|
|
"need_private_comment": 1 if with_detail else 0,
|
|||
|
|
},
|
|||
|
|
cookie_header=cookie_header,
|
|||
|
|
)
|
|||
|
|
code = payload.get("code", 0)
|
|||
|
|
if code not in {0, "0"}:
|
|||
|
|
raise RuntimeError(_single_line(payload.get("message") or payload.get("msg") or f"查询失败 code={code}", 160))
|
|||
|
|
msglist = payload.get("msglist") or []
|
|||
|
|
if not isinstance(msglist, list):
|
|||
|
|
msglist = []
|
|||
|
|
return self._qzone_parse_feeds(msglist)
|
|||
|
|
|
|||
|
|
async def _qzone_verify_like_post(
|
|||
|
|
self,
|
|||
|
|
event: AstrMessageEvent | None,
|
|||
|
|
post: Any,
|
|||
|
|
*,
|
|||
|
|
cookie_header: str,
|
|||
|
|
target_liked: bool = True,
|
|||
|
|
) -> dict[str, Any]:
|
|||
|
|
tid = str(getattr(post, "tid", "") or "")
|
|||
|
|
uin = str(getattr(post, "uin", "") or "")
|
|||
|
|
fid = str(self._qzone_post_value(post, "fid", tid) or tid)
|
|||
|
|
if not tid or not uin:
|
|||
|
|
return {"verified": False, "liked": None, "message": "缺少说说 tid 或 uin,无法反查点赞状态"}
|
|||
|
|
for attempt, delay in enumerate((0.0, 0.45, 1.2), start=1):
|
|||
|
|
if delay > 0:
|
|||
|
|
await asyncio.sleep(delay)
|
|||
|
|
try:
|
|||
|
|
feeds = await self._qzone_query_feeds(
|
|||
|
|
event,
|
|||
|
|
target_id=uin,
|
|||
|
|
pos=0,
|
|||
|
|
num=10,
|
|||
|
|
with_detail=False,
|
|||
|
|
cookie_header=cookie_header,
|
|||
|
|
)
|
|||
|
|
except Exception as exc:
|
|||
|
|
if attempt >= 3:
|
|||
|
|
return {"verified": False, "liked": None, "message": f"点赞反查失败:{_single_line(exc, 120)}"}
|
|||
|
|
continue
|
|||
|
|
for feed in feeds:
|
|||
|
|
feed_tid = str(getattr(feed, "tid", "") or "")
|
|||
|
|
feed_fid = str(self._qzone_post_value(feed, "fid", feed_tid) or feed_tid)
|
|||
|
|
if (tid and feed_tid == tid) or (fid and feed_fid == fid):
|
|||
|
|
liked = bool(getattr(feed, "liked", False))
|
|||
|
|
try:
|
|||
|
|
setattr(post, "liked", liked)
|
|||
|
|
except Exception:
|
|||
|
|
pass
|
|||
|
|
if liked == bool(target_liked):
|
|||
|
|
return {"verified": True, "liked": liked, "message": "已反查到点赞状态"}
|
|||
|
|
if attempt >= 3:
|
|||
|
|
return {
|
|||
|
|
"verified": False,
|
|||
|
|
"liked": liked,
|
|||
|
|
"message": "点赞请求已受理,但最近动态反查到的状态仍未变化",
|
|||
|
|
}
|
|||
|
|
if attempt >= 3:
|
|||
|
|
return {"verified": False, "liked": None, "message": "点赞请求已受理,但最近动态中暂未反查到这条说说"}
|
|||
|
|
return {"verified": False, "liked": None, "message": "点赞请求已受理,但暂未完成反查"}
|
|||
|
|
|
|||
|
|
async def _qzone_like_post(self, event: AstrMessageEvent | None, post: Any) -> dict[str, Any]:
|
|||
|
|
cookie_header = await self._qzone_get_cookies(event)
|
|||
|
|
ctx = self._qzone_context_from_cookies(cookie_header)
|
|||
|
|
tid = str(getattr(post, "tid", "") or "")
|
|||
|
|
uin = str(getattr(post, "uin", "") or "")
|
|||
|
|
if not tid or not uin:
|
|||
|
|
raise RuntimeError("说说 tid 或 uin 为空,无法点赞")
|
|||
|
|
like_url = self._qzone_post_like_url(post, uin=uin, tid=tid)
|
|||
|
|
curkey = str(self._qzone_post_value(post, "curkey", "") or "") or like_url
|
|||
|
|
unikey = str(self._qzone_post_value(post, "unikey", "") or "") or like_url
|
|||
|
|
appid = str(self._qzone_post_value(post, "appid", "311") or "311")
|
|||
|
|
typeid = str(self._qzone_post_value(post, "typeid", "0") or "0")
|
|||
|
|
fid = str(self._qzone_post_value(post, "fid", tid) or tid)
|
|||
|
|
abstime = _safe_int(self._qzone_post_value(post, "abstime", 0), 0, 0)
|
|||
|
|
if abstime <= 0:
|
|||
|
|
abstime = _safe_int(getattr(post, "create_time", 0), 0, 0) or int(time.time())
|
|||
|
|
payload = await self._qzone_request(
|
|||
|
|
event,
|
|||
|
|
"POST",
|
|||
|
|
"https://user.qzone.qq.com/proxy/domain/w.qzone.qq.com/cgi-bin/likes/internal_dolike_app",
|
|||
|
|
params={"g_tk": ctx["gtk"]},
|
|||
|
|
data={
|
|||
|
|
"qzreferrer": f"https://user.qzone.qq.com/{ctx['uin']}",
|
|||
|
|
"opuin": ctx["uin"],
|
|||
|
|
"unikey": unikey,
|
|||
|
|
"curkey": curkey,
|
|||
|
|
"appid": appid,
|
|||
|
|
"from": 1,
|
|||
|
|
"typeid": typeid,
|
|||
|
|
"abstime": abstime,
|
|||
|
|
"fid": fid,
|
|||
|
|
"active": 0,
|
|||
|
|
"format": "json",
|
|||
|
|
"fupdate": 1,
|
|||
|
|
},
|
|||
|
|
headers={
|
|||
|
|
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
|||
|
|
"Referer": f"https://user.qzone.qq.com/{uin}/mood/{tid}",
|
|||
|
|
"Origin": "https://user.qzone.qq.com",
|
|||
|
|
},
|
|||
|
|
cookie_header=cookie_header,
|
|||
|
|
)
|
|||
|
|
code = payload.get("code", 0)
|
|||
|
|
if code not in {0, "0"}:
|
|||
|
|
logger.warning(
|
|||
|
|
"[PrivateCompanion] QQ 空间点赞失败: code=%s message=%s uin=%s tid=%s appid=%s typeid=%s fid=%s http=%s",
|
|||
|
|
code,
|
|||
|
|
_single_line(payload.get("message") or payload.get("msg") or payload.get("_raw_message"), 100),
|
|||
|
|
uin,
|
|||
|
|
tid,
|
|||
|
|
appid,
|
|||
|
|
typeid,
|
|||
|
|
fid,
|
|||
|
|
payload.get("_http_status"),
|
|||
|
|
)
|
|||
|
|
raise RuntimeError(_single_line(payload.get("message") or payload.get("msg") or f"点赞失败 code={code}", 160))
|
|||
|
|
verification = await self._qzone_verify_like_post(event, post, cookie_header=cookie_header, target_liked=True)
|
|||
|
|
logger.info(
|
|||
|
|
"[PrivateCompanion] QQ 空间点赞成功: uin=%s tid=%s appid=%s typeid=%s fid=%s verified=%s",
|
|||
|
|
uin,
|
|||
|
|
tid,
|
|||
|
|
appid,
|
|||
|
|
typeid,
|
|||
|
|
fid,
|
|||
|
|
bool(verification.get("verified")),
|
|||
|
|
)
|
|||
|
|
return {
|
|||
|
|
"success": True,
|
|||
|
|
"liked": True if verification.get("liked") is None else bool(verification.get("liked")),
|
|||
|
|
"verified": bool(verification.get("verified")),
|
|||
|
|
"verify_message": verification.get("message") or "",
|
|||
|
|
"tid": tid,
|
|||
|
|
"uin": uin,
|
|||
|
|
"fid": fid,
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async def _qzone_delete_post(
|
|||
|
|
self,
|
|||
|
|
event: AstrMessageEvent | None,
|
|||
|
|
post: Any,
|
|||
|
|
*,
|
|||
|
|
cookie_header: str | None = None,
|
|||
|
|
) -> None:
|
|||
|
|
if cookie_header is None:
|
|||
|
|
cookie_header = await self._qzone_get_cookies(event)
|
|||
|
|
ctx = self._qzone_context_from_cookies(cookie_header)
|
|||
|
|
tid = str(getattr(post, "tid", "") or "")
|
|||
|
|
uin = str(getattr(post, "uin", "") or "")
|
|||
|
|
if not tid or not uin:
|
|||
|
|
raise RuntimeError("说说 tid 或 uin 为空,无法删除")
|
|||
|
|
if str(ctx.get("uin") or "") != uin:
|
|||
|
|
raise RuntimeError("只能删除当前登录 QQ 自己发布的说说")
|
|||
|
|
appid = str(self._qzone_post_value(post, "appid", "311") or "311")
|
|||
|
|
fid = str(self._qzone_post_value(post, "fid", tid) or tid)
|
|||
|
|
unikey = str(self._qzone_post_value(post, "unikey", "") or "") or f"https://user.qzone.qq.com/{uin}/mood/{tid}"
|
|||
|
|
curkey = str(self._qzone_post_value(post, "curkey", "") or "") or unikey
|
|||
|
|
abstime = _safe_int(self._qzone_post_value(post, "abstime", 0), 0, 0)
|
|||
|
|
if abstime <= 0:
|
|||
|
|
abstime = _safe_int(getattr(post, "create_time", 0), 0, 0)
|
|||
|
|
payload = await self._qzone_request(
|
|||
|
|
event,
|
|||
|
|
"POST",
|
|||
|
|
"https://user.qzone.qq.com/proxy/domain/taotao.qzone.qq.com/cgi-bin/emotion_cgi_delete_v6",
|
|||
|
|
params={"g_tk": ctx["gtk"]},
|
|||
|
|
data={
|
|||
|
|
"hostuin": uin,
|
|||
|
|
"tid": tid,
|
|||
|
|
"t1_source": 1,
|
|||
|
|
"code_version": 1,
|
|||
|
|
"format": "json",
|
|||
|
|
"qzreferrer": f"https://user.qzone.qq.com/{uin}/mood/{tid}",
|
|||
|
|
"topicId": f"{uin}_{tid}__1",
|
|||
|
|
"uin": uin,
|
|||
|
|
"feedsType": 100,
|
|||
|
|
"feedsAppid": appid,
|
|||
|
|
"feedsKey": fid or tid,
|
|||
|
|
"feedsTime": abstime,
|
|||
|
|
"unikey": unikey,
|
|||
|
|
"curkey": curkey,
|
|||
|
|
},
|
|||
|
|
headers={
|
|||
|
|
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
|||
|
|
"Accept": "application/json, text/javascript, */*; q=0.01",
|
|||
|
|
"Referer": f"https://user.qzone.qq.com/{uin}/mood/{tid}",
|
|||
|
|
"Origin": "https://user.qzone.qq.com",
|
|||
|
|
"X-Requested-With": "XMLHttpRequest",
|
|||
|
|
},
|
|||
|
|
cookie_header=cookie_header,
|
|||
|
|
)
|
|||
|
|
code = payload.get("code", payload.get("ret", payload.get("_raw_code", 0)))
|
|||
|
|
if code not in {0, "0", None, ""}:
|
|||
|
|
logger.warning(
|
|||
|
|
"[PrivateCompanion] QQ 空间删除说说失败: code=%s message=%s uin=%s tid=%s http=%s",
|
|||
|
|
code,
|
|||
|
|
_single_line(payload.get("message") or payload.get("msg") or payload.get("_raw_message"), 100),
|
|||
|
|
uin,
|
|||
|
|
tid,
|
|||
|
|
payload.get("_http_status"),
|
|||
|
|
)
|
|||
|
|
raise RuntimeError(_single_line(payload.get("message") or payload.get("msg") or f"删除失败 code={code}", 160))
|
|||
|
|
logger.info("[PrivateCompanion] QQ 空间删除说说成功: uin=%s tid=%s", uin, tid)
|
|||
|
|
|
|||
|
|
async def _qzone_generate_comment(self, post: Any) -> str:
|
|||
|
|
prompt = f"""
|
|||
|
|
请以当前 Bot 人格,为下面这条 QQ 空间说说写一句自然评论。
|
|||
|
|
只输出评论正文,不要解释。
|
|||
|
|
|
|||
|
|
要求:
|
|||
|
|
- 8 到 40 字。
|
|||
|
|
- 像真实熟人评论,不要像客服或总结。
|
|||
|
|
- 不要泄露私聊内容、插件内部信息、关系网资料或状态数值。
|
|||
|
|
- 如果内容信息不足,可以写轻量回应。
|
|||
|
|
|
|||
|
|
【作者】
|
|||
|
|
{_single_line(getattr(post, "name", ""), 40) or _single_line(getattr(post, "uin", ""), 40) or "对方"}
|
|||
|
|
|
|||
|
|
【说说内容】
|
|||
|
|
{_single_line(getattr(post, "text", "") or getattr(post, "rt_con", ""), 240) or "无文本"}
|
|||
|
|
""".strip()
|
|||
|
|
text = await self._llm_call(
|
|||
|
|
prompt,
|
|||
|
|
max_tokens=80,
|
|||
|
|
provider_id=self._task_provider(self.mai_style_provider_id, self.llm_provider_id),
|
|||
|
|
task="qzone_comment",
|
|||
|
|
)
|
|||
|
|
return _single_line(text, 80)
|
|||
|
|
|
|||
|
|
async def _qzone_comment_post(self, event: AstrMessageEvent | None, post: Any, content: str = "") -> str:
|
|||
|
|
cookie_header = await self._qzone_get_cookies(event)
|
|||
|
|
ctx = self._qzone_context_from_cookies(cookie_header)
|
|||
|
|
tid = str(getattr(post, "tid", "") or "")
|
|||
|
|
uin = str(getattr(post, "uin", "") or "")
|
|||
|
|
if not tid or not uin:
|
|||
|
|
raise RuntimeError("说说 tid 或 uin 为空,无法评论")
|
|||
|
|
comment = _single_line(content, 120) or await self._qzone_generate_comment(post)
|
|||
|
|
if not comment:
|
|||
|
|
raise RuntimeError("评论内容为空")
|
|||
|
|
payload = await self._qzone_request(
|
|||
|
|
event,
|
|||
|
|
"POST",
|
|||
|
|
"https://user.qzone.qq.com/proxy/domain/taotao.qzone.qq.com/cgi-bin/emotion_cgi_re_feeds",
|
|||
|
|
params={"g_tk": ctx["gtk"]},
|
|||
|
|
data={
|
|||
|
|
"topicId": f"{uin}_{tid}__1",
|
|||
|
|
"uin": ctx["uin"],
|
|||
|
|
"hostUin": uin,
|
|||
|
|
"feedsType": 100,
|
|||
|
|
"inCharset": "utf-8",
|
|||
|
|
"outCharset": "utf-8",
|
|||
|
|
"plat": "qzone",
|
|||
|
|
"source": "ic",
|
|||
|
|
"platformid": 52,
|
|||
|
|
"format": "fs",
|
|||
|
|
"ref": "feeds",
|
|||
|
|
"content": comment,
|
|||
|
|
},
|
|||
|
|
cookie_header=cookie_header,
|
|||
|
|
)
|
|||
|
|
code = payload.get("code", 0)
|
|||
|
|
if code not in {0, "0"}:
|
|||
|
|
raise RuntimeError(_single_line(payload.get("message") or payload.get("msg") or f"评论失败 code={code}", 160))
|
|||
|
|
return comment
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_trim_id_list(values: Any, *, limit: int = 500) -> list[str]:
|
|||
|
|
result: list[str] = []
|
|||
|
|
for value in values if isinstance(values, list) else []:
|
|||
|
|
text = _single_line(value, 120)
|
|||
|
|
if text and text not in result:
|
|||
|
|
result.append(text)
|
|||
|
|
return result[-max(1, int(limit or 500)) :]
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_normalized_comment_text(text: Any) -> str:
|
|||
|
|
cleaned = html.unescape(re.sub(r"<[^>]+>", "", str(text or "")))
|
|||
|
|
cleaned = re.sub(r"\s+", "", cleaned).lower()
|
|||
|
|
cleaned = re.sub(r"[,,。.!!??~~…·、;;::\"'“”‘’\[\]()()\s]+", "", cleaned)
|
|||
|
|
return _single_line(cleaned, 160)
|
|||
|
|
|
|||
|
|
def _qzone_comment_author_key(self, comment: Any) -> str:
|
|||
|
|
uin = _safe_int(getattr(comment, "uin", 0), 0, 0)
|
|||
|
|
if uin:
|
|||
|
|
return f"uin:{uin}"
|
|||
|
|
name = re.sub(r"\s+", "", _single_line(getattr(comment, "name", ""), 40).lower())
|
|||
|
|
return f"name:{name}" if name else "unknown"
|
|||
|
|
|
|||
|
|
def _qzone_comment_author_post_key(self, post: Any, comment: Any) -> str:
|
|||
|
|
post_tid = _single_line(getattr(post, "tid", ""), 80) or "post"
|
|||
|
|
return f"{post_tid}|{self._qzone_comment_author_key(comment)}"
|
|||
|
|
|
|||
|
|
def _qzone_trim_comment_records(
|
|||
|
|
self,
|
|||
|
|
values: Any,
|
|||
|
|
*,
|
|||
|
|
now: float,
|
|||
|
|
max_age_seconds: float = 7 * 24 * 3600,
|
|||
|
|
limit: int = 160,
|
|||
|
|
) -> list[dict[str, Any]]:
|
|||
|
|
result: list[dict[str, Any]] = []
|
|||
|
|
seen: set[str] = set()
|
|||
|
|
for item in values if isinstance(values, list) else []:
|
|||
|
|
if not isinstance(item, dict):
|
|||
|
|
continue
|
|||
|
|
ts = _safe_float(item.get("ts"), 0)
|
|||
|
|
if ts and now - ts > max_age_seconds:
|
|||
|
|
continue
|
|||
|
|
key = _single_line(item.get("key") or item.get("signature"), 160)
|
|||
|
|
post_tid = _single_line(item.get("post_tid"), 80)
|
|||
|
|
text_norm = _single_line(item.get("text_norm"), 160)
|
|||
|
|
if not key and post_tid and text_norm:
|
|||
|
|
key = f"{post_tid}|{text_norm}"
|
|||
|
|
if not key or key in seen:
|
|||
|
|
continue
|
|||
|
|
seen.add(key)
|
|||
|
|
result.append(
|
|||
|
|
{
|
|||
|
|
"key": key,
|
|||
|
|
"post_tid": post_tid,
|
|||
|
|
"author_key": _single_line(item.get("author_key"), 80),
|
|||
|
|
"text_norm": text_norm,
|
|||
|
|
"text": _single_line(item.get("text"), 120),
|
|||
|
|
"ts": ts or now,
|
|||
|
|
}
|
|||
|
|
)
|
|||
|
|
return result[-max(1, int(limit or 160)) :]
|
|||
|
|
|
|||
|
|
def _qzone_recent_sent_comment_records(self, state: dict[str, Any], *, now: float) -> list[dict[str, Any]]:
|
|||
|
|
records = self._qzone_trim_comment_records(
|
|||
|
|
state.get("comment_inbox_recent_sent_comments") if isinstance(state, dict) else [],
|
|||
|
|
now=now,
|
|||
|
|
max_age_seconds=7 * 24 * 3600,
|
|||
|
|
limit=160,
|
|||
|
|
)
|
|||
|
|
if isinstance(state, dict):
|
|||
|
|
state["comment_inbox_recent_sent_comments"] = records
|
|||
|
|
return records
|
|||
|
|
|
|||
|
|
def _qzone_recent_author_reply_records(self, state: dict[str, Any], *, now: float) -> list[dict[str, Any]]:
|
|||
|
|
records = self._qzone_trim_comment_records(
|
|||
|
|
state.get("comment_inbox_recent_author_replies") if isinstance(state, dict) else [],
|
|||
|
|
now=now,
|
|||
|
|
max_age_seconds=24 * 3600,
|
|||
|
|
limit=160,
|
|||
|
|
)
|
|||
|
|
if isinstance(state, dict):
|
|||
|
|
state["comment_inbox_recent_author_replies"] = records
|
|||
|
|
return records
|
|||
|
|
|
|||
|
|
def _qzone_comment_matches_recent_sent(self, state: dict[str, Any], post: Any, comment: Any, *, now: float) -> bool:
|
|||
|
|
post_tid = _single_line(getattr(post, "tid", ""), 80) or "post"
|
|||
|
|
content_norm = self._qzone_normalized_comment_text(getattr(comment, "content", ""))
|
|||
|
|
if not content_norm:
|
|||
|
|
return False
|
|||
|
|
for item in self._qzone_recent_sent_comment_records(state, now=now):
|
|||
|
|
if item.get("post_tid") == post_tid and item.get("text_norm") == content_norm:
|
|||
|
|
return True
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def _qzone_comment_is_self(self, state: dict[str, Any], post: Any, comment: Any, *, own_uin: int, now: float) -> bool:
|
|||
|
|
comment_uin = _safe_int(getattr(comment, "uin", 0), 0, 0)
|
|||
|
|
if own_uin and comment_uin == int(own_uin):
|
|||
|
|
return True
|
|||
|
|
comment_name = re.sub(r"\s+", "", _single_line(getattr(comment, "name", ""), 40).lower())
|
|||
|
|
post_name = re.sub(r"\s+", "", _single_line(getattr(post, "name", ""), 40).lower())
|
|||
|
|
if comment_name and post_name and comment_name == post_name:
|
|||
|
|
return True
|
|||
|
|
return self._qzone_comment_matches_recent_sent(state, post, comment, now=now)
|
|||
|
|
|
|||
|
|
def _qzone_author_post_recently_replied(self, state: dict[str, Any], post: Any, comment: Any, *, now: float, cooldown_seconds: float = 6 * 3600) -> bool:
|
|||
|
|
key = self._qzone_comment_author_post_key(post, comment)
|
|||
|
|
if not key:
|
|||
|
|
return False
|
|||
|
|
for item in self._qzone_recent_author_reply_records(state, now=now):
|
|||
|
|
if item.get("key") == key and now - _safe_float(item.get("ts"), 0) < cooldown_seconds:
|
|||
|
|
return True
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
def _qzone_note_comment_inbox_sent(self, state: dict[str, Any], post: Any, comment: Any, sent_text: str, *, now: float) -> None:
|
|||
|
|
if not isinstance(state, dict):
|
|||
|
|
return
|
|||
|
|
post_tid = _single_line(getattr(post, "tid", ""), 80) or "post"
|
|||
|
|
text_norm = self._qzone_normalized_comment_text(sent_text)
|
|||
|
|
if text_norm:
|
|||
|
|
sent_records = self._qzone_recent_sent_comment_records(state, now=now)
|
|||
|
|
sent_records.append(
|
|||
|
|
{
|
|||
|
|
"key": f"{post_tid}|{text_norm}",
|
|||
|
|
"post_tid": post_tid,
|
|||
|
|
"author_key": "self",
|
|||
|
|
"text_norm": text_norm,
|
|||
|
|
"text": _single_line(sent_text, 120),
|
|||
|
|
"ts": now,
|
|||
|
|
}
|
|||
|
|
)
|
|||
|
|
state["comment_inbox_recent_sent_comments"] = self._qzone_trim_comment_records(sent_records, now=now, limit=160)
|
|||
|
|
author_key = self._qzone_comment_author_post_key(post, comment)
|
|||
|
|
author_records = self._qzone_recent_author_reply_records(state, now=now)
|
|||
|
|
author_records.append(
|
|||
|
|
{
|
|||
|
|
"key": author_key,
|
|||
|
|
"post_tid": post_tid,
|
|||
|
|
"author_key": self._qzone_comment_author_key(comment),
|
|||
|
|
"text_norm": self._qzone_normalized_comment_text(getattr(comment, "content", "")),
|
|||
|
|
"text": _single_line(getattr(comment, "content", ""), 120),
|
|||
|
|
"ts": now,
|
|||
|
|
}
|
|||
|
|
)
|
|||
|
|
state["comment_inbox_recent_author_replies"] = self._qzone_trim_comment_records(
|
|||
|
|
author_records,
|
|||
|
|
now=now,
|
|||
|
|
max_age_seconds=24 * 3600,
|
|||
|
|
limit=160,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
def _qzone_comment_reply_leaks_private(self, text: str) -> bool:
|
|||
|
|
compact = str(text or "")
|
|||
|
|
if not compact.strip():
|
|||
|
|
return True
|
|||
|
|
patterns = (
|
|||
|
|
r"私聊",
|
|||
|
|
r"主人",
|
|||
|
|
r"主要用户",
|
|||
|
|
r"朋友用户",
|
|||
|
|
r"次要用户",
|
|||
|
|
r"插件",
|
|||
|
|
r"模型",
|
|||
|
|
r"系统提示",
|
|||
|
|
r"token",
|
|||
|
|
r"后台",
|
|||
|
|
r"内部",
|
|||
|
|
r"记忆注入",
|
|||
|
|
)
|
|||
|
|
return any(re.search(pattern, compact, flags=re.IGNORECASE) for pattern in patterns)
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_clean_comment_reply_text(value: Any, commenter_name: Any = "") -> str:
|
|||
|
|
reply = _single_line(value, 80).strip(" ,,。")
|
|||
|
|
name = _single_line(commenter_name, 40).strip().lstrip("@")
|
|||
|
|
if name:
|
|||
|
|
reply = re.sub(rf"^(?:@?{re.escape(name)}[\s,,::]+)+", "", reply).strip()
|
|||
|
|
while reply:
|
|||
|
|
codepoint = ord(reply[-1])
|
|||
|
|
if (
|
|||
|
|
0x1F000 <= codepoint <= 0x1FAFF
|
|||
|
|
or 0x2600 <= codepoint <= 0x27BF
|
|||
|
|
or codepoint in {0x200D, 0xFE0E, 0xFE0F}
|
|||
|
|
):
|
|||
|
|
reply = reply[:-1].rstrip()
|
|||
|
|
continue
|
|||
|
|
break
|
|||
|
|
return reply.strip(" ,,。")
|
|||
|
|
|
|||
|
|
def _qzone_comment_author_context(self, comment: Any) -> str:
|
|||
|
|
uin = _single_line(getattr(comment, "uin", ""), 40)
|
|||
|
|
name = _single_line(getattr(comment, "name", ""), 40)
|
|||
|
|
profile: dict[str, Any] | None = None
|
|||
|
|
match_note = ""
|
|||
|
|
if uin and uin != "0":
|
|||
|
|
profile = self._worldbook_profile_by_user_id(uin)
|
|||
|
|
if profile:
|
|||
|
|
match_note = "按 QQ 号命中关系网。"
|
|||
|
|
if not profile and name:
|
|||
|
|
matches = self._resolve_worldbook_member_by_name(name)
|
|||
|
|
if len(matches) == 1:
|
|||
|
|
profile = matches[0]
|
|||
|
|
match_note = "按评论显示名弱命中关系网。"
|
|||
|
|
elif len(matches) > 1:
|
|||
|
|
names = "、".join(_single_line(item.get("name"), 24) for item in matches[:3] if _single_line(item.get("name"), 24))
|
|||
|
|
return (
|
|||
|
|
"【评论者身份】\n"
|
|||
|
|
f"评论显示名:{name};QQ:{uin or '未知'}。\n"
|
|||
|
|
f"关系网里有多个同名/近似对象:{names or '多个候选'};本轮不要擅自认定身份,也不要当成主要用户。"
|
|||
|
|
)
|
|||
|
|
if not profile:
|
|||
|
|
return (
|
|||
|
|
"【评论者身份】\n"
|
|||
|
|
f"评论显示名:{name or '未知'};QQ:{uin or '未知'}。\n"
|
|||
|
|
"关系网未确认此人;按普通空间评论者处理,不要把对方当成主要用户、私聊对象或熟人。"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
profile_uid = _single_line(profile.get("linked_qq_user_id") or profile.get("user_id") or uin, 40)
|
|||
|
|
stable_name = _single_line(profile.get("name"), 40) or name or profile_uid
|
|||
|
|
aliases = []
|
|||
|
|
for token in [*(profile.get("aliases") or []), *(profile.get("observed_names") or [])]:
|
|||
|
|
value = _single_line(token, 24)
|
|||
|
|
if value and value != stable_name and value not in aliases:
|
|||
|
|
aliases.append(value)
|
|||
|
|
if len(aliases) >= 4:
|
|||
|
|
break
|
|||
|
|
identity_note = _single_line(profile.get("identity_note") or profile.get("note") or profile.get("content"), 120)
|
|||
|
|
lines = [
|
|||
|
|
"【评论者身份】",
|
|||
|
|
f"已识别:{stable_name}[QQ:{profile_uid or uin or '未知'}];{match_note or '命中关系网。'}",
|
|||
|
|
]
|
|||
|
|
if name and name != stable_name:
|
|||
|
|
lines.append(f"当前空间显示名:{name}。")
|
|||
|
|
if aliases:
|
|||
|
|
lines.append(f"别名/常见名:{'、'.join(aliases)}。")
|
|||
|
|
if identity_note:
|
|||
|
|
lines.append(f"关系备注:{identity_note}")
|
|||
|
|
lines.append("这些资料只用于判断称呼和边界,公开回复里不要复述关系网资料。")
|
|||
|
|
return "\n".join(lines)
|
|||
|
|
|
|||
|
|
def _qzone_post_time_text(self, value: Any) -> str:
|
|||
|
|
ts = _safe_float(value, 0)
|
|||
|
|
if ts <= 0:
|
|||
|
|
return ""
|
|||
|
|
try:
|
|||
|
|
formatter = getattr(self, "_environment_fromtimestamp", None)
|
|||
|
|
if callable(formatter):
|
|||
|
|
return formatter(ts).strftime("%Y-%m-%d %H:%M")
|
|||
|
|
return time.strftime("%Y-%m-%d %H:%M", time.localtime(ts))
|
|||
|
|
except Exception:
|
|||
|
|
return ""
|
|||
|
|
|
|||
|
|
def _qzone_post_brief_context(self, post: Any) -> str:
|
|||
|
|
tid = _single_line(getattr(post, "tid", ""), 80)
|
|||
|
|
author = _single_line(getattr(post, "name", ""), 40) or _single_line(getattr(post, "uin", ""), 40) or "我"
|
|||
|
|
text = _single_line(getattr(post, "text", "") or getattr(post, "rt_con", ""), 240) or "无文本"
|
|||
|
|
rt_text = _single_line(getattr(post, "rt_con", ""), 160)
|
|||
|
|
images = getattr(post, "images", []) or []
|
|||
|
|
image_count = len(images) if isinstance(images, list) else 0
|
|||
|
|
post_type = "转发" if rt_text else ("图文" if image_count else "文字")
|
|||
|
|
created = self._qzone_post_time_text(getattr(post, "create_time", 0)) or "未知"
|
|||
|
|
return (
|
|||
|
|
"【所在说说】\n"
|
|||
|
|
f"说说ID:{tid or '未知'}\n"
|
|||
|
|
f"作者:{author}\n"
|
|||
|
|
f"发布时间:{created}\n"
|
|||
|
|
f"类型:{post_type};图片数量:{image_count}\n"
|
|||
|
|
f"正文:{text}"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
async def _qzone_memory_companion_context(self, *, purpose: str, query: str = "") -> str:
|
|||
|
|
getter = getattr(self, "_memory_companion_compose_feature_context", None)
|
|||
|
|
if not callable(getter):
|
|||
|
|
return ""
|
|||
|
|
try:
|
|||
|
|
return await getter(
|
|||
|
|
kind=f"qzone_{_single_line(purpose, 40) or 'context'}",
|
|||
|
|
query=query or "QQ空间公开动态 当前日程 最近生活 日记余味 今日穿搭 自我时间线",
|
|||
|
|
top_k=5,
|
|||
|
|
max_chars=760,
|
|||
|
|
)
|
|||
|
|
except Exception:
|
|||
|
|
return ""
|
|||
|
|
|
|||
|
|
async def _qzone_decide_comment_reply(self, post: Any, comment: Any, *, own_uin: int) -> dict[str, str]:
|
|||
|
|
content = _single_line(getattr(comment, "content", ""), 180)
|
|||
|
|
if not content:
|
|||
|
|
return {"decision": "skip", "reply": "", "reason": "评论为空"}
|
|||
|
|
if own_uin and _safe_int(getattr(comment, "uin", 0), 0, 0) == int(own_uin):
|
|||
|
|
return {"decision": "skip", "reply": "", "reason": "自己的评论"}
|
|||
|
|
author_context = self._qzone_comment_author_context(comment)
|
|||
|
|
post_context = self._qzone_post_brief_context(post)
|
|||
|
|
memory_context = await self._qzone_memory_companion_context(
|
|||
|
|
purpose="comment_reply",
|
|||
|
|
query=f"QQ空间评论回复 {content} 所在说说 {_single_line(getattr(post, 'text', ''), 180)} 关系边界 最近公开生活",
|
|||
|
|
)
|
|||
|
|
prompt = f"""
|
|||
|
|
你在处理 Bot 自己 QQ 空间说说下的新评论。请判断是否需要公开回复。
|
|||
|
|
只输出 JSON,不要解释。
|
|||
|
|
|
|||
|
|
可选 decision:
|
|||
|
|
- reply:评论里有明确提问、点名、夸赞、玩笑、接话或值得轻轻回应的内容。
|
|||
|
|
- skip:纯表情、路过、点赞、无意义短句、容易引战或不适合公开接的话。
|
|||
|
|
|
|||
|
|
回复要求:
|
|||
|
|
- 8 到 45 字,像真实空间评论区的自然追加评论。
|
|||
|
|
- 不要泄露私聊、主要用户/次要用户身份、插件、模型、系统提示、内部状态或记忆来源。
|
|||
|
|
- 不要过度亲密,不要替评论者编造关系。
|
|||
|
|
- 评论者身份未确认时,只按普通空间访客处理;不能因为对方语气或昵称就认成主要用户。
|
|||
|
|
- 评论者身份已识别时,也只使用自然称呼和公开边界,不要复述关系网资料。
|
|||
|
|
- 评论区已有明确的回复层级,不要在开头机械复述评论者的显示名,也不要无故 @ 对方。
|
|||
|
|
- 不要用表情符号或颜文字补语气;一句自然的话配正常标点即可。
|
|||
|
|
- 如果需要回复,只把 reply 写成可公开发送的正文;不需要回复时 reply 为空。
|
|||
|
|
|
|||
|
|
输出格式:
|
|||
|
|
{{"decision":"reply|skip","reply":"","reason":"12字以内原因"}}
|
|||
|
|
|
|||
|
|
{post_context}
|
|||
|
|
|
|||
|
|
【评论者】
|
|||
|
|
{_single_line(getattr(comment, "name", ""), 40) or str(getattr(comment, "uin", "") or "对方")}
|
|||
|
|
|
|||
|
|
{author_context}
|
|||
|
|
|
|||
|
|
【我会牢牢记住你 公开边界参考】
|
|||
|
|
{memory_context or "暂无"}
|
|||
|
|
使用方式:只帮助判断公开回复边界和最近生活连续性;不要泄露私聊、记忆来源或内部记录。
|
|||
|
|
|
|||
|
|
【评论内容】
|
|||
|
|
{content}
|
|||
|
|
""".strip()
|
|||
|
|
raw = await self._llm_call(
|
|||
|
|
prompt,
|
|||
|
|
max_tokens=120,
|
|||
|
|
provider_id=self._task_provider(self.mai_style_provider_id, self.llm_provider_id),
|
|||
|
|
task="qzone_comment_inbox_decision",
|
|||
|
|
)
|
|||
|
|
payload = self._extract_json_payload(raw or "")
|
|||
|
|
if not isinstance(payload, dict):
|
|||
|
|
payload = {}
|
|||
|
|
decision = str(payload.get("decision") or "").strip().lower()
|
|||
|
|
if decision not in {"reply", "skip"}:
|
|||
|
|
decision = "skip"
|
|||
|
|
reply = _single_line(payload.get("reply"), 80)
|
|||
|
|
reason = _single_line(payload.get("reason"), 40)
|
|||
|
|
if decision == "reply":
|
|||
|
|
if len(reply) < 2 or self._qzone_comment_reply_leaks_private(reply):
|
|||
|
|
return {"decision": "skip", "reply": "", "reason": "回复不安全"}
|
|||
|
|
reply = self._qzone_clean_comment_reply_text(
|
|||
|
|
reply.strip(" 「」\"'"),
|
|||
|
|
getattr(comment, "name", ""),
|
|||
|
|
)
|
|||
|
|
if len(reply) < 2:
|
|||
|
|
return {"decision": "skip", "reply": "", "reason": "回复过短"}
|
|||
|
|
return {"decision": decision, "reply": reply, "reason": reason}
|
|||
|
|
|
|||
|
|
async def _qzone_reply_to_comment(self, event: AstrMessageEvent | None, post: Any, comment: Any, reply_text: str) -> str:
|
|||
|
|
reply = self._qzone_clean_comment_reply_text(reply_text, getattr(comment, "name", ""))
|
|||
|
|
if not reply:
|
|||
|
|
raise RuntimeError("评论回复内容为空")
|
|||
|
|
return await self._qzone_comment_post(event, post, content=_single_line(reply, 120))
|
|||
|
|
|
|||
|
|
async def _maybe_process_qzone_comment_inbox(self) -> None:
|
|||
|
|
if not (self._qzone_available() and getattr(self, "enable_qzone_comment_inbox", False)):
|
|||
|
|
return
|
|||
|
|
now = _now_ts()
|
|||
|
|
state = self._qzone_state_dict()
|
|||
|
|
seen_ids: list[str] = []
|
|||
|
|
replied_ids: list[str] = []
|
|||
|
|
seen_keys: list[str] = []
|
|||
|
|
replied_keys: list[str] = []
|
|||
|
|
replied_set: set[str] = set()
|
|||
|
|
replied_key_set: set[str] = set()
|
|||
|
|
interval_seconds = max(5, _safe_int(getattr(self, "qzone_comment_inbox_interval_minutes", 60), 60, 5, 1440)) * 60
|
|||
|
|
if now - _safe_float(state.get("last_comment_inbox_checked_at"), 0) < interval_seconds:
|
|||
|
|
return
|
|||
|
|
if now - _safe_float(state.get("last_comment_inbox_failed_at"), 0) < 15 * 60:
|
|||
|
|
return
|
|||
|
|
try:
|
|||
|
|
cookie_header = await self._qzone_get_cookies(None)
|
|||
|
|
ctx = self._qzone_context_from_cookies(cookie_header)
|
|||
|
|
own_uin = _safe_int(ctx.get("uin"), 0, 0)
|
|||
|
|
recent_posts = _safe_int(getattr(self, "qzone_comment_inbox_recent_posts", 5), 5, 1, 20)
|
|||
|
|
max_replies = _safe_int(getattr(self, "qzone_comment_inbox_max_replies_per_tick", 1), 1, 1, 5)
|
|||
|
|
posts = await self._qzone_query_feeds(None, target_id=str(own_uin), pos=0, num=recent_posts, with_detail=True)
|
|||
|
|
observed: list[tuple[Any, Any, str, str, list[str], bool, bool]] = []
|
|||
|
|
for post in posts:
|
|||
|
|
for comment in list(getattr(post, "comments", []) or []):
|
|||
|
|
comment_id = _single_line(getattr(comment, "comment_id", ""), 120)
|
|||
|
|
post_tid = _single_line(getattr(post, "tid", ""), 80)
|
|||
|
|
raw_comment = getattr(comment, "raw", None)
|
|||
|
|
if isinstance(raw_comment, dict):
|
|||
|
|
comment_key = self._qzone_comment_fingerprint(post_tid, raw_comment)
|
|||
|
|
comment_legacy_id = self._qzone_comment_legacy_fallback_id(post_tid, raw_comment)
|
|||
|
|
else:
|
|||
|
|
author = _single_line(getattr(comment, "uin", ""), 40) or _single_line(getattr(comment, "name", ""), 40)
|
|||
|
|
content = re.sub(r"\s+", "", _single_line(getattr(comment, "content", ""), 180)).lower()
|
|||
|
|
digest = hashlib.sha1(f"{post_tid or 'post'}|{author}|{content}".encode("utf-8", "ignore")).hexdigest()[:20]
|
|||
|
|
comment_key = f"{post_tid or 'post'}:fp:{digest}"
|
|||
|
|
comment_legacy_id = _single_line(getattr(comment, "comment_legacy_id", ""), 120)
|
|||
|
|
comment_key = _single_line(getattr(comment, "comment_key", "") or comment_key, 120)
|
|||
|
|
comment_legacy_id = _single_line(getattr(comment, "comment_legacy_id", "") or comment_legacy_id, 120)
|
|||
|
|
if comment_id or comment_key:
|
|||
|
|
id_candidates = self._qzone_trim_id_list([comment_id, comment_legacy_id, comment_key], limit=5)
|
|||
|
|
is_self_comment = self._qzone_comment_is_self(state, post, comment, own_uin=own_uin, now=now)
|
|||
|
|
author_recently_replied = self._qzone_author_post_recently_replied(state, post, comment, now=now)
|
|||
|
|
observed.append(
|
|||
|
|
(
|
|||
|
|
post,
|
|||
|
|
comment,
|
|||
|
|
comment_id or comment_key,
|
|||
|
|
comment_key or comment_id,
|
|||
|
|
id_candidates,
|
|||
|
|
is_self_comment,
|
|||
|
|
author_recently_replied,
|
|||
|
|
)
|
|||
|
|
)
|
|||
|
|
seen_ids = self._qzone_trim_id_list(state.get("comment_inbox_seen_ids"), limit=500)
|
|||
|
|
replied_ids = self._qzone_trim_id_list(state.get("comment_inbox_replied_ids"), limit=300)
|
|||
|
|
seen_keys = self._qzone_trim_id_list(state.get("comment_inbox_seen_keys"), limit=500)
|
|||
|
|
replied_keys = self._qzone_trim_id_list(state.get("comment_inbox_replied_keys"), limit=300)
|
|||
|
|
seen_set = set(seen_ids)
|
|||
|
|
replied_set = set(replied_ids)
|
|||
|
|
seen_key_set = set(seen_keys)
|
|||
|
|
replied_key_set = set(replied_keys)
|
|||
|
|
observed_ids = [candidate_id for _, _, _, _, id_candidates, _, _ in observed for candidate_id in id_candidates if candidate_id]
|
|||
|
|
observed_keys = [comment_key for _, _, _, comment_key, _, _, _ in observed if comment_key]
|
|||
|
|
first_run = not state.get("comment_inbox_initialized_at")
|
|||
|
|
if first_run:
|
|||
|
|
state["comment_inbox_seen_ids"] = self._qzone_trim_id_list(seen_ids + observed_ids, limit=500)
|
|||
|
|
state["comment_inbox_seen_keys"] = self._qzone_trim_id_list(seen_keys + observed_keys, limit=500)
|
|||
|
|
state["comment_inbox_initialized_at"] = now
|
|||
|
|
state["last_comment_inbox_checked_at"] = now
|
|||
|
|
state["last_comment_inbox_status"] = f"seeded:{len(observed_ids)}"
|
|||
|
|
self._save_data_sync()
|
|||
|
|
logger.info("[PrivateCompanion] QQ 空间评论收件箱首次启用,已记录现有评论: count=%s", len(observed_ids))
|
|||
|
|
return
|
|||
|
|
history_lost_after_init = bool(
|
|||
|
|
state.get("comment_inbox_initialized_at")
|
|||
|
|
and observed_ids
|
|||
|
|
and not seen_ids
|
|||
|
|
and not seen_keys
|
|||
|
|
and not replied_ids
|
|||
|
|
and not replied_keys
|
|||
|
|
)
|
|||
|
|
if history_lost_after_init:
|
|||
|
|
state["comment_inbox_seen_ids"] = self._qzone_trim_id_list(observed_ids, limit=500)
|
|||
|
|
state["comment_inbox_seen_keys"] = self._qzone_trim_id_list(observed_keys, limit=500)
|
|||
|
|
state["last_comment_inbox_checked_at"] = now
|
|||
|
|
state["last_comment_inbox_status"] = f"reseeded:history_lost:{len(observed_ids)}"
|
|||
|
|
self._save_data_sync()
|
|||
|
|
logger.warning(
|
|||
|
|
"[PrivateCompanion] QQ 空间评论收件箱历史 key 为空,已重新播种当前可见评论并跳过本轮回复: count=%s",
|
|||
|
|
len(observed_ids),
|
|||
|
|
)
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
candidates = [
|
|||
|
|
(post, comment, comment_id, comment_key, id_candidates)
|
|||
|
|
for post, comment, comment_id, comment_key, id_candidates, is_self_comment, author_recently_replied in observed
|
|||
|
|
if not any(candidate_id in seen_set or candidate_id in replied_set for candidate_id in id_candidates)
|
|||
|
|
and comment_key not in seen_key_set
|
|||
|
|
and comment_key not in replied_key_set
|
|||
|
|
and not is_self_comment
|
|||
|
|
and not author_recently_replied
|
|||
|
|
]
|
|||
|
|
if observed_ids or observed_keys:
|
|||
|
|
state["comment_inbox_seen_ids"] = self._qzone_trim_id_list(seen_ids + observed_ids, limit=500)
|
|||
|
|
state["comment_inbox_seen_keys"] = self._qzone_trim_id_list(seen_keys + observed_keys, limit=500)
|
|||
|
|
state["last_comment_inbox_checked_at"] = now
|
|||
|
|
state["last_comment_inbox_status"] = f"checking:new={len(candidates)}"
|
|||
|
|
self._save_data_sync()
|
|||
|
|
candidates.sort(key=lambda item: _safe_float(getattr(item[1], "create_time", 0), 0))
|
|||
|
|
replies = 0
|
|||
|
|
skipped = 0
|
|||
|
|
last_reason = ""
|
|||
|
|
sent_text = ""
|
|||
|
|
for post, comment, comment_id, comment_key, id_candidates in candidates:
|
|||
|
|
if replies >= max_replies:
|
|||
|
|
break
|
|||
|
|
decision = await self._qzone_decide_comment_reply(post, comment, own_uin=own_uin)
|
|||
|
|
if decision.get("decision") != "reply":
|
|||
|
|
skipped += 1
|
|||
|
|
last_reason = _single_line(decision.get("reason"), 60)
|
|||
|
|
continue
|
|||
|
|
for candidate_id in id_candidates:
|
|||
|
|
if candidate_id:
|
|||
|
|
replied_set.add(candidate_id)
|
|||
|
|
if comment_id:
|
|||
|
|
replied_set.add(comment_id)
|
|||
|
|
if comment_key:
|
|||
|
|
replied_key_set.add(comment_key)
|
|||
|
|
state["comment_inbox_replied_ids"] = self._qzone_trim_id_list(list(replied_set), limit=300)
|
|||
|
|
state["comment_inbox_replied_keys"] = self._qzone_trim_id_list(list(replied_key_set), limit=300)
|
|||
|
|
state["last_comment_inbox_checked_at"] = now
|
|||
|
|
state["last_comment_inbox_status"] = f"replying:guarded:{_single_line(comment_id or comment_key, 80)}"
|
|||
|
|
state["last_comment_inbox_reply_comment_id"] = comment_id
|
|||
|
|
state["last_comment_inbox_reply_comment_key"] = comment_key
|
|||
|
|
state["last_comment_inbox_reply_author"] = _single_line(getattr(comment, "name", ""), 40) or _single_line(getattr(comment, "uin", ""), 40)
|
|||
|
|
self._save_data_sync()
|
|||
|
|
sent_text = await self._qzone_reply_to_comment(None, post, comment, str(decision.get("reply") or ""))
|
|||
|
|
self._qzone_note_comment_inbox_sent(state, post, comment, sent_text, now=now)
|
|||
|
|
replies += 1
|
|||
|
|
last_reason = _single_line(decision.get("reason"), 60) or "已回复"
|
|||
|
|
state["comment_inbox_replied_ids"] = self._qzone_trim_id_list(list(replied_set), limit=300)
|
|||
|
|
state["comment_inbox_replied_keys"] = self._qzone_trim_id_list(list(replied_key_set), limit=300)
|
|||
|
|
state["last_comment_inbox_reply_at"] = now
|
|||
|
|
post_images = getattr(post, "images", []) or []
|
|||
|
|
post_image_count = len(post_images) if isinstance(post_images, list) else 0
|
|||
|
|
post_rt_text = _single_line(getattr(post, "rt_con", ""), 160)
|
|||
|
|
post_type = "转发" if post_rt_text else ("图文" if post_image_count else "文字")
|
|||
|
|
state["last_comment_inbox_reply_post_tid"] = _single_line(getattr(post, "tid", ""), 80)
|
|||
|
|
state["last_comment_inbox_reply_post_type"] = post_type
|
|||
|
|
state["last_comment_inbox_reply_post_time"] = self._qzone_post_time_text(getattr(post, "create_time", 0))
|
|||
|
|
state["last_comment_inbox_reply_post_text"] = _single_line(
|
|||
|
|
getattr(post, "text", "") or getattr(post, "rt_con", ""),
|
|||
|
|
120,
|
|||
|
|
)
|
|||
|
|
state["last_comment_inbox_reply_post_image_count"] = post_image_count
|
|||
|
|
state["last_comment_inbox_reply_comment_id"] = comment_id
|
|||
|
|
state["last_comment_inbox_reply_comment_key"] = comment_key
|
|||
|
|
state["last_comment_inbox_reply_author"] = _single_line(getattr(comment, "name", ""), 40) or _single_line(getattr(comment, "uin", ""), 40)
|
|||
|
|
state["last_comment_inbox_reason"] = last_reason
|
|||
|
|
state["last_comment_inbox_reply_text"] = _single_line(sent_text, 120)
|
|||
|
|
self._save_data_sync()
|
|||
|
|
logger.info(
|
|||
|
|
"[PrivateCompanion] QQ 空间评论收件箱已追加评论回复: post=%s type=%s comment=%s key=%s author=%s text=%s",
|
|||
|
|
state["last_comment_inbox_reply_post_tid"] or "-",
|
|||
|
|
post_type,
|
|||
|
|
comment_id,
|
|||
|
|
comment_key,
|
|||
|
|
state["last_comment_inbox_reply_author"],
|
|||
|
|
_single_line(sent_text, 100),
|
|||
|
|
)
|
|||
|
|
state["comment_inbox_seen_ids"] = self._qzone_trim_id_list(seen_ids + observed_ids, limit=500)
|
|||
|
|
state["comment_inbox_seen_keys"] = self._qzone_trim_id_list(seen_keys + observed_keys, limit=500)
|
|||
|
|
state["comment_inbox_replied_ids"] = self._qzone_trim_id_list(list(replied_set), limit=300)
|
|||
|
|
state["comment_inbox_replied_keys"] = self._qzone_trim_id_list(list(replied_key_set), limit=300)
|
|||
|
|
state["last_comment_inbox_checked_at"] = now
|
|||
|
|
state["last_comment_inbox_status"] = f"checked:new={len(candidates)},replied={replies},skipped={skipped}"
|
|||
|
|
state["last_comment_inbox_reason"] = last_reason
|
|||
|
|
state["last_comment_inbox_reply_text"] = _single_line(sent_text, 120)
|
|||
|
|
if replies:
|
|||
|
|
state["last_comment_inbox_reply_at"] = now
|
|||
|
|
state.pop("last_comment_inbox_failed_at", None)
|
|||
|
|
self._save_data_sync()
|
|||
|
|
except Exception as exc:
|
|||
|
|
reason = _single_line(exc, 160)
|
|||
|
|
if self._qzone_auth_failure_message(reason):
|
|||
|
|
self._qzone_mark_auth_failure(reason, source="comment_inbox", state=state, save=False)
|
|||
|
|
if replied_set or replied_key_set:
|
|||
|
|
state["comment_inbox_replied_ids"] = self._qzone_trim_id_list(
|
|||
|
|
replied_ids + list(replied_set),
|
|||
|
|
limit=300,
|
|||
|
|
)
|
|||
|
|
state["comment_inbox_replied_keys"] = self._qzone_trim_id_list(
|
|||
|
|
replied_keys + list(replied_key_set),
|
|||
|
|
limit=300,
|
|||
|
|
)
|
|||
|
|
if seen_ids or seen_keys:
|
|||
|
|
state["comment_inbox_seen_ids"] = self._qzone_trim_id_list(
|
|||
|
|
self._qzone_trim_id_list(state.get("comment_inbox_seen_ids"), limit=500) + seen_ids,
|
|||
|
|
limit=500,
|
|||
|
|
)
|
|||
|
|
state["comment_inbox_seen_keys"] = self._qzone_trim_id_list(
|
|||
|
|
self._qzone_trim_id_list(state.get("comment_inbox_seen_keys"), limit=500) + seen_keys,
|
|||
|
|
limit=500,
|
|||
|
|
)
|
|||
|
|
state["last_comment_inbox_failed_at"] = now
|
|||
|
|
state["last_comment_inbox_checked_at"] = now
|
|||
|
|
state["last_comment_inbox_status"] = f"failed:{_single_line(reason, 80)}"
|
|||
|
|
self._save_data_sync()
|
|||
|
|
if any(token in reason for token in ("没有可用的 OneBot 连接", "获取 QQ 空间 Cookie 失败", "Cookie")):
|
|||
|
|
logger.warning("[PrivateCompanion] QQ 空间评论收件箱处理失败: %s", reason)
|
|||
|
|
else:
|
|||
|
|
logger.warning("[PrivateCompanion] QQ 空间评论收件箱处理失败: %s", reason, exc_info=True)
|
|||
|
|
|
|||
|
|
def _qzone_public_state_hint(self, state: dict[str, Any]) -> str:
|
|||
|
|
"""Return a public-safe mood hint for Qzone posts without internal state fields."""
|
|||
|
|
if not isinstance(state, dict):
|
|||
|
|
return "心情平稳,适合写一小段生活感。"
|
|||
|
|
mood = _single_line(state.get("mood_bias"), 24) or "平稳"
|
|||
|
|
weather = _single_line(state.get("weather"), 80)
|
|||
|
|
sleep = _single_line(state.get("sleep"), 40)
|
|||
|
|
hints: list[str] = []
|
|||
|
|
if mood:
|
|||
|
|
hints.append(f"心情底色偏{mood}")
|
|||
|
|
if weather and weather != "暂无天气信息":
|
|||
|
|
hints.append(f"天气余味:{weather}")
|
|||
|
|
if sleep and sleep not in {"睡眠平稳", "正常"}:
|
|||
|
|
hints.append(f"节奏偏{sleep}")
|
|||
|
|
if not hints:
|
|||
|
|
hints.append("生活节奏平稳")
|
|||
|
|
hints.append("只能写成自然感受,不要写状态标签、数值或内部变量。")
|
|||
|
|
return ";".join(hints)
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_temporal_context() -> str:
|
|||
|
|
now = time.localtime()
|
|||
|
|
weekday_names = ("星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日")
|
|||
|
|
hour = now.tm_hour
|
|||
|
|
if 0 <= hour < 6:
|
|||
|
|
period = "凌晨"
|
|||
|
|
elif hour < 9:
|
|||
|
|
period = "早晨"
|
|||
|
|
elif hour < 12:
|
|||
|
|
period = "上午"
|
|||
|
|
elif hour < 16:
|
|||
|
|
period = "下午"
|
|||
|
|
elif hour < 19:
|
|||
|
|
period = "傍晚"
|
|||
|
|
elif hour < 22:
|
|||
|
|
period = "晚上"
|
|||
|
|
else:
|
|||
|
|
period = "深夜"
|
|||
|
|
if now.tm_mon in (12, 1, 2):
|
|||
|
|
season = "冬天"
|
|||
|
|
elif now.tm_mon in (3, 4, 5):
|
|||
|
|
season = "春天"
|
|||
|
|
elif now.tm_mon in (6, 7, 8, 9):
|
|||
|
|
season = "夏天"
|
|||
|
|
else:
|
|||
|
|
season = "秋天"
|
|||
|
|
weekday = weekday_names[min(6, max(0, now.tm_wday))]
|
|||
|
|
day_type = "周末" if now.tm_wday >= 5 else "工作日"
|
|||
|
|
return f"{time.strftime('%Y年%m月%d日 %H:%M', now)},{weekday},{day_type},{season},{period}。"
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_publish_theme_hint() -> str:
|
|||
|
|
themes = (
|
|||
|
|
"记录当前时段里一件具体的小事",
|
|||
|
|
"写一个自然冒出来的心情余味",
|
|||
|
|
"轻轻吐槽一个生活里的小麻烦",
|
|||
|
|
"记录眼前看到、听到或碰到的具体画面",
|
|||
|
|
"写一段短短的碎碎念,不要总结成道理",
|
|||
|
|
"记录一个让人稍微开心或安心的小瞬间",
|
|||
|
|
"写写天气、光线、食物、衣物、路上或桌边的生活细节",
|
|||
|
|
"从当前日程里挑一个最不像任务汇报的切面",
|
|||
|
|
)
|
|||
|
|
return random.choice(themes)
|
|||
|
|
|
|||
|
|
def _qzone_relationship_safe_source(self, value: Any, *, source: str) -> str:
|
|||
|
|
sanitizer = getattr(self, "_sanitize_generation_relationship_context", None)
|
|||
|
|
if callable(sanitizer):
|
|||
|
|
try:
|
|||
|
|
return sanitizer(value, source=source)
|
|||
|
|
except Exception:
|
|||
|
|
pass
|
|||
|
|
return str(value or "").strip()
|
|||
|
|
|
|||
|
|
def _qzone_relationship_authority_guard(self) -> str:
|
|||
|
|
formatter = getattr(self, "_format_generation_relationship_authority_guard", None)
|
|||
|
|
if callable(formatter):
|
|||
|
|
try:
|
|||
|
|
return str(formatter() or "").strip()
|
|||
|
|
except Exception:
|
|||
|
|
pass
|
|||
|
|
return ""
|
|||
|
|
|
|||
|
|
def _qzone_recent_publish_context(self, state: dict[str, Any], *, limit: int = 5) -> str:
|
|||
|
|
items = state.get("recent_life_publish_texts") if isinstance(state, dict) else []
|
|||
|
|
if not isinstance(items, list):
|
|||
|
|
return ""
|
|||
|
|
lines: list[str] = []
|
|||
|
|
for item in items[-max(1, int(limit or 5)) :]:
|
|||
|
|
text = _single_line(
|
|||
|
|
self._qzone_relationship_safe_source(
|
|||
|
|
item.get("text") if isinstance(item, dict) else item,
|
|||
|
|
source="qzone.recent_publish",
|
|||
|
|
),
|
|||
|
|
120,
|
|||
|
|
)
|
|||
|
|
if text:
|
|||
|
|
lines.append(f"- {text}")
|
|||
|
|
if not lines:
|
|||
|
|
return ""
|
|||
|
|
return "最近已发说说:\n" + "\n".join(lines) + "\n本次请换一个场景、情绪或观察角度,不要重复同一类表达。"
|
|||
|
|
|
|||
|
|
def _qzone_recent_self_publish_chat_context(self, *, limit: int = 3) -> str:
|
|||
|
|
"""Expose recent successful Bot posts to Qzone-related chat turns."""
|
|||
|
|
state = self.data.get("qzone_integration") if isinstance(getattr(self, "data", None), dict) else {}
|
|||
|
|
items = state.get("recent_life_publish_texts") if isinstance(state, dict) else []
|
|||
|
|
if not isinstance(items, list):
|
|||
|
|
return ""
|
|||
|
|
records: list[str] = []
|
|||
|
|
labels = ("最新一条", "上一条", "更早一条")
|
|||
|
|
for item in reversed(items):
|
|||
|
|
if len(records) >= max(1, min(3, int(limit or 3))):
|
|||
|
|
break
|
|||
|
|
text = _single_line(
|
|||
|
|
self._qzone_relationship_safe_source(
|
|||
|
|
item.get("text") if isinstance(item, dict) else item,
|
|||
|
|
source="qzone.recent_self_publish_chat",
|
|||
|
|
),
|
|||
|
|
180,
|
|||
|
|
)
|
|||
|
|
if not text:
|
|||
|
|
continue
|
|||
|
|
image_count = _safe_int(item.get("image_count"), 0, 0, 99) if isinstance(item, dict) else 0
|
|||
|
|
image_note = f";配图 {image_count} 张" if image_count else ""
|
|||
|
|
label = labels[len(records)] if len(records) < len(labels) else f"较早第 {len(records) + 1} 条"
|
|||
|
|
records.append(f"- {label}:{text}{image_note}")
|
|||
|
|
if not records:
|
|||
|
|
return ""
|
|||
|
|
return (
|
|||
|
|
"【Bot 自己最近成功发布的 QQ 空间记录】\n"
|
|||
|
|
+ "\n".join(records)
|
|||
|
|
+ "\n这些正文是 Bot 自己发出的公开动态,不是当前用户发的内容。"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
def _qzone_note_recent_publish(
|
|||
|
|
self,
|
|||
|
|
state: dict[str, Any],
|
|||
|
|
text: Any,
|
|||
|
|
*,
|
|||
|
|
reason: str,
|
|||
|
|
now: float | None = None,
|
|||
|
|
tid: str = "",
|
|||
|
|
image_count: int = 0,
|
|||
|
|
verified: bool | None = None,
|
|||
|
|
source: str = "",
|
|||
|
|
) -> None:
|
|||
|
|
if not isinstance(state, dict):
|
|||
|
|
return
|
|||
|
|
clean = _single_line(text, 180)
|
|||
|
|
if not clean:
|
|||
|
|
return
|
|||
|
|
current = _now_ts() if now is None else float(now)
|
|||
|
|
items = state.get("recent_life_publish_texts")
|
|||
|
|
if not isinstance(items, list):
|
|||
|
|
items = []
|
|||
|
|
deduped: list[dict[str, Any]] = []
|
|||
|
|
seen: set[str] = set()
|
|||
|
|
for item in items:
|
|||
|
|
raw = item.get("text") if isinstance(item, dict) else item
|
|||
|
|
item_text = _single_line(raw, 180)
|
|||
|
|
key = re.sub(r"\s+", "", item_text)
|
|||
|
|
if not item_text or key in seen or key == re.sub(r"\s+", "", clean):
|
|||
|
|
continue
|
|||
|
|
seen.add(key)
|
|||
|
|
if isinstance(item, dict):
|
|||
|
|
deduped.append(dict(item))
|
|||
|
|
else:
|
|||
|
|
deduped.append({"text": item_text, "at": 0, "reason": ""})
|
|||
|
|
entry = {
|
|||
|
|
"text": clean,
|
|||
|
|
"at": current,
|
|||
|
|
"reason": _single_line(reason, 40),
|
|||
|
|
"tid": _single_line(tid, 80),
|
|||
|
|
"image_count": _safe_int(image_count, 0, 0, 99),
|
|||
|
|
"source": _single_line(source, 40),
|
|||
|
|
}
|
|||
|
|
if verified is not None:
|
|||
|
|
entry["verified"] = bool(verified)
|
|||
|
|
deduped.append(entry)
|
|||
|
|
state["recent_life_publish_texts"] = deduped[-8:]
|
|||
|
|
|
|||
|
|
async def _qzone_record_published_post(
|
|||
|
|
self,
|
|||
|
|
text: Any,
|
|||
|
|
*,
|
|||
|
|
reason: str = "manual_publish",
|
|||
|
|
tid: str = "",
|
|||
|
|
image_count: int = 0,
|
|||
|
|
verified: bool | None = None,
|
|||
|
|
event: AstrMessageEvent | None = None,
|
|||
|
|
) -> None:
|
|||
|
|
state = self._qzone_state_dict()
|
|||
|
|
now = _now_ts()
|
|||
|
|
clean = _single_line(text, 300)
|
|||
|
|
if not clean:
|
|||
|
|
return
|
|||
|
|
self._qzone_note_recent_publish(
|
|||
|
|
state,
|
|||
|
|
clean,
|
|||
|
|
reason=reason,
|
|||
|
|
now=now,
|
|||
|
|
tid=tid,
|
|||
|
|
image_count=image_count,
|
|||
|
|
verified=verified,
|
|||
|
|
source="publish_success",
|
|||
|
|
)
|
|||
|
|
state["last_publish_recorded_at"] = now
|
|||
|
|
state["last_publish_recorded_text"] = _single_line(clean, 180)
|
|||
|
|
state["last_publish_recorded_reason"] = _single_line(reason, 40)
|
|||
|
|
state["last_publish_recorded_tid"] = _single_line(tid, 80)
|
|||
|
|
state["last_publish_recorded_images"] = _safe_int(image_count, 0, 0, 99)
|
|||
|
|
recorder = getattr(self, "_memory_companion_record_qzone_publish", None)
|
|||
|
|
if callable(recorder):
|
|||
|
|
await recorder(
|
|||
|
|
text=clean,
|
|||
|
|
reason=reason,
|
|||
|
|
tid=tid,
|
|||
|
|
image_count=image_count,
|
|||
|
|
verified=verified,
|
|||
|
|
event=event,
|
|||
|
|
)
|
|||
|
|
self._qzone_append_publish_to_current_detail(
|
|||
|
|
clean,
|
|||
|
|
reason=reason,
|
|||
|
|
tid=tid,
|
|||
|
|
image_count=image_count,
|
|||
|
|
verified=verified,
|
|||
|
|
)
|
|||
|
|
invalidator = getattr(self, "_invalidate_detail_after_interaction", None)
|
|||
|
|
if callable(invalidator):
|
|||
|
|
try:
|
|||
|
|
invalidator(now=now)
|
|||
|
|
except Exception:
|
|||
|
|
pass
|
|||
|
|
try:
|
|||
|
|
self._save_data_sync()
|
|||
|
|
except Exception as exc:
|
|||
|
|
logger.debug("[PrivateCompanion] QQ 空间发布记录保存失败: %s", _single_line(exc, 120))
|
|||
|
|
|
|||
|
|
def _qzone_append_publish_to_current_detail(
|
|||
|
|
self,
|
|||
|
|
text: Any,
|
|||
|
|
*,
|
|||
|
|
reason: str = "",
|
|||
|
|
tid: str = "",
|
|||
|
|
image_count: int = 0,
|
|||
|
|
verified: bool | None = None,
|
|||
|
|
) -> bool:
|
|||
|
|
segment_getter = getattr(self, "_current_detail_segment_for_update", None)
|
|||
|
|
if not callable(segment_getter):
|
|||
|
|
return False
|
|||
|
|
try:
|
|||
|
|
segment = segment_getter()
|
|||
|
|
except Exception:
|
|||
|
|
return False
|
|||
|
|
if not isinstance(segment, dict):
|
|||
|
|
return False
|
|||
|
|
enhanced = self.data.get("detail_enhanced_segments", {})
|
|||
|
|
if not isinstance(enhanced, dict):
|
|||
|
|
return False
|
|||
|
|
key = str(segment.get("key") or "")
|
|||
|
|
snapshot = enhanced.get(key)
|
|||
|
|
if not isinstance(snapshot, dict):
|
|||
|
|
return False
|
|||
|
|
clean = _single_line(text, 180)
|
|||
|
|
if not clean:
|
|||
|
|
return False
|
|||
|
|
safe_image_count = _safe_int(image_count, 0, 0, 99)
|
|||
|
|
image_part = f";配图 {safe_image_count} 张" if safe_image_count > 0 else ""
|
|||
|
|
verify_part = ";已反查确认" if verified else ""
|
|||
|
|
event_text = _single_line(f"刚发布了一条 QQ 空间说说:{clean}{image_part}{verify_part}。", 220)
|
|||
|
|
events = snapshot.setdefault("today_events", [])
|
|||
|
|
if not isinstance(events, list):
|
|||
|
|
events = []
|
|||
|
|
snapshot["today_events"] = events
|
|||
|
|
tid_text = _single_line(tid, 80)
|
|||
|
|
for item in events:
|
|||
|
|
if not isinstance(item, dict):
|
|||
|
|
continue
|
|||
|
|
if tid_text and _single_line(item.get("tid"), 80) == tid_text:
|
|||
|
|
return False
|
|||
|
|
if clean and clean in _single_line(item.get("event") or item.get("text"), 260):
|
|||
|
|
return False
|
|||
|
|
try:
|
|||
|
|
at = self._environment_now().strftime("%H:%M")
|
|||
|
|
except Exception:
|
|||
|
|
at = ""
|
|||
|
|
events.append(
|
|||
|
|
{
|
|||
|
|
"window": at,
|
|||
|
|
"event": event_text,
|
|||
|
|
"mood": "公开动态已发布",
|
|||
|
|
"source": "qzone_publish",
|
|||
|
|
"reason": _single_line(reason, 40),
|
|||
|
|
"tid": tid_text,
|
|||
|
|
}
|
|||
|
|
)
|
|||
|
|
del events[:-8]
|
|||
|
|
summary = _single_line(snapshot.get("summary"), 140)
|
|||
|
|
summary_tail = _single_line(f"刚发了一条 QQ 空间说说:{clean}", 80)
|
|||
|
|
if summary_tail and summary_tail not in summary:
|
|||
|
|
snapshot["summary"] = _single_line(f"{summary};{summary_tail}" if summary else summary_tail, 160)
|
|||
|
|
snapshot["updated_at"] = at or _single_line(snapshot.get("updated_at"), 20)
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
def _qzone_text_leaks_internal_state(self, text: str) -> bool:
|
|||
|
|
compact = str(text or "")
|
|||
|
|
if not compact.strip():
|
|||
|
|
return False
|
|||
|
|
patterns = (
|
|||
|
|
r"能量\s*[::=]?\s*\d{1,3}\s*/\s*100",
|
|||
|
|
r"心理能量",
|
|||
|
|
r"\d{1,3}\s*/\s*100",
|
|||
|
|
r"状态变量",
|
|||
|
|
r"当前状态",
|
|||
|
|
r"拟人状态",
|
|||
|
|
r"内部状态",
|
|||
|
|
r"插件",
|
|||
|
|
r"模型",
|
|||
|
|
r"系统提示",
|
|||
|
|
)
|
|||
|
|
return any(re.search(pattern, compact, flags=re.IGNORECASE) for pattern in patterns)
|
|||
|
|
|
|||
|
|
def _strip_qzone_internal_state_fragments(self, text: str) -> str:
|
|||
|
|
cleaned = _single_line(text, 180)
|
|||
|
|
if not cleaned:
|
|||
|
|
return ""
|
|||
|
|
cleaned = re.sub(r"(?:心理)?能量\s*[::=]?\s*\d{1,3}\s*/\s*100[,,。;;\s]*", "", cleaned)
|
|||
|
|
cleaned = re.sub(r"\d{1,3}\s*/\s*100[,,。;;\s]*", "", cleaned)
|
|||
|
|
cleaned = re.sub(r"(?:当前状态|拟人状态|状态变量|内部状态)[::,,。;;\s]*", "", cleaned)
|
|||
|
|
cleaned = re.sub(r"(?:插件|模型|系统提示)[^。!?!?;;]{0,40}[。!?!?;;]?", "", cleaned)
|
|||
|
|
cleaned = re.sub(r"\s{2,}", " ", cleaned).strip(" ,,。;;")
|
|||
|
|
return _single_line(cleaned, 180)
|
|||
|
|
|
|||
|
|
def _qzone_publish_style_prompt(self, *, mood: str = "life") -> str:
|
|||
|
|
base = (
|
|||
|
|
"默认风格:像随手发的一条 QQ 空间生活碎片,贴着眼前具体事物、动作或天气写;"
|
|||
|
|
"口语、轻一点、短一点,可以有小情绪但不要上价值。"
|
|||
|
|
"避免哲理总结、人生感悟、诗化独白、宏大比喻、老成说教、文案腔和谜语感。"
|
|||
|
|
)
|
|||
|
|
if mood == "emotional_vent":
|
|||
|
|
base += " 心情动态也要克制,只写公开可见的余味,不要写成控诉或伤感散文。"
|
|||
|
|
voice = ""
|
|||
|
|
voice_formatter = getattr(self, "_format_persona_voice_channel_prompt", None)
|
|||
|
|
if callable(voice_formatter):
|
|||
|
|
voice = voice_formatter("creative")
|
|||
|
|
expression_voice = ""
|
|||
|
|
expression_formatter = getattr(self, "_format_expression_voice_for_prompt", None)
|
|||
|
|
if callable(expression_formatter):
|
|||
|
|
expression_voice = expression_formatter(
|
|||
|
|
scope="qzone",
|
|||
|
|
inbound_text="低落情绪" if mood == "emotional_vent" else "生活闲聊",
|
|||
|
|
)
|
|||
|
|
custom = _single_line(getattr(self, "qzone_publish_style_prompt", ""), 500)
|
|||
|
|
parts = [base]
|
|||
|
|
if voice:
|
|||
|
|
parts.append(voice)
|
|||
|
|
if expression_voice:
|
|||
|
|
parts.append(expression_voice)
|
|||
|
|
if custom:
|
|||
|
|
parts.append(f"自定义风格:{custom}")
|
|||
|
|
return "\n".join(parts)
|
|||
|
|
|
|||
|
|
def _qzone_publish_image_style_prompt(self) -> str:
|
|||
|
|
base = (
|
|||
|
|
"默认配图策略:像 QQ 空间随手生活图,先贴合说说正文和当前日程选择画面。"
|
|||
|
|
"人物可以自然入镜,但不要每次都做自拍;在生活物件、食物饮品、路上光影、桌面一角、窗边、背影、侧脸、第一视角手部之间轮换。"
|
|||
|
|
"避免过度使用镜前自拍、手机挡脸自拍、固定半身自拍模板;只有正文或日程明确在整理穿搭、出门前照镜子、换衣服时才考虑镜前/镜中构图。"
|
|||
|
|
)
|
|||
|
|
custom = _single_line(getattr(self, "qzone_publish_image_style_prompt", ""), 600)
|
|||
|
|
if custom:
|
|||
|
|
return f"{base}\n自定义配图提示:{custom}"
|
|||
|
|
return base
|
|||
|
|
|
|||
|
|
async def _sanitize_qzone_life_post_text(self, text: str, *, prompt: str = "") -> str:
|
|||
|
|
cleaned = _single_line(text, 180)
|
|||
|
|
relationship_cleaned = _single_line(
|
|||
|
|
self._qzone_relationship_safe_source(
|
|||
|
|
cleaned,
|
|||
|
|
source="qzone.generated_post",
|
|||
|
|
),
|
|||
|
|
180,
|
|||
|
|
)
|
|||
|
|
if relationship_cleaned != cleaned:
|
|||
|
|
logger.warning(
|
|||
|
|
"[PrivateCompanion] QQ 空间说说草稿含未声明关系,已移除污染片段: %s",
|
|||
|
|
_single_line(cleaned, 160),
|
|||
|
|
)
|
|||
|
|
cleaned = relationship_cleaned
|
|||
|
|
if len(cleaned) < 12:
|
|||
|
|
return ""
|
|||
|
|
if not self._qzone_text_leaks_internal_state(cleaned):
|
|||
|
|
return cleaned
|
|||
|
|
stripped = self._strip_qzone_internal_state_fragments(cleaned)
|
|||
|
|
if stripped and not self._qzone_text_leaks_internal_state(stripped) and len(stripped) >= 12:
|
|||
|
|
logger.warning("[PrivateCompanion] QQ 空间说说草稿含内部状态,已净化: %s", _single_line(cleaned, 160))
|
|||
|
|
return stripped
|
|||
|
|
rewrite_prompt = f"""
|
|||
|
|
下面是一条 QQ 空间说说草稿,里面泄露了内部状态/数值。请重写成自然生活动态。
|
|||
|
|
只输出正文,30 到 120 字,不要解释。
|
|||
|
|
禁止出现:能量、心理能量、/100、当前状态、状态变量、插件、模型、系统提示。
|
|||
|
|
|
|||
|
|
【原草稿】
|
|||
|
|
{cleaned}
|
|||
|
|
|
|||
|
|
【原任务背景】
|
|||
|
|
{_single_line(prompt, 600)}
|
|||
|
|
""".strip()
|
|||
|
|
try:
|
|||
|
|
rewritten = await self._llm_call(
|
|||
|
|
rewrite_prompt,
|
|||
|
|
max_tokens=160,
|
|||
|
|
provider_id=self._task_provider(self.mai_style_provider_id, self.llm_provider_id),
|
|||
|
|
task="qzone_publish_sanitize",
|
|||
|
|
)
|
|||
|
|
rewritten = _single_line(
|
|||
|
|
self._qzone_relationship_safe_source(
|
|||
|
|
rewritten,
|
|||
|
|
source="qzone.sanitizer_rewrite",
|
|||
|
|
),
|
|||
|
|
180,
|
|||
|
|
)
|
|||
|
|
if rewritten and not self._qzone_text_leaks_internal_state(rewritten):
|
|||
|
|
logger.warning("[PrivateCompanion] QQ 空间说说草稿含内部状态,已重写: %s", _single_line(cleaned, 160))
|
|||
|
|
return rewritten
|
|||
|
|
except Exception as exc:
|
|||
|
|
logger.warning("[PrivateCompanion] QQ 空间说说内部状态重写失败: %s", _single_line(exc, 120))
|
|||
|
|
logger.warning("[PrivateCompanion] QQ 空间说说草稿含内部状态且重写失败,已取消本次发布")
|
|||
|
|
return ""
|
|||
|
|
|
|||
|
|
async def _test_qzone_publish_tool_chain(self, event: AstrMessageEvent | None = None) -> str:
|
|||
|
|
lines = ["QQ 空间发布链路模拟:"]
|
|||
|
|
lines.append(f"- 整合开关:{'开启' if self.enable_qzone_integration else '关闭'}")
|
|||
|
|
lines.append("- 真实发布:否,本指令只模拟工具链路")
|
|||
|
|
if not self._qzone_platform_supported(event):
|
|||
|
|
lines.append(f"结果:{self._qzone_platform_unavailable_message()}")
|
|||
|
|
return "\n".join(lines)
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
empty_result_raw = await self._pc_qzone_publish_feed_impl(event, "")
|
|||
|
|
empty_result = json.loads(empty_result_raw)
|
|||
|
|
except Exception as exc:
|
|||
|
|
empty_result = {"status": "exception", "message": _single_line(exc, 160)}
|
|||
|
|
lines.append(
|
|||
|
|
"- 空参数工具调用:"
|
|||
|
|
+ (
|
|||
|
|
"通过,返回 need_text"
|
|||
|
|
if empty_result.get("status") == "need_text"
|
|||
|
|
else f"异常,返回 {empty_result.get('status') or empty_result.get('message') or empty_result}"
|
|||
|
|
)
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
qzone_state = self._qzone_state_dict()
|
|||
|
|
daily_state = self.data.get("daily_state", {})
|
|||
|
|
current_item = self._get_current_plan_item(self.data.get("daily_plan", {}))
|
|||
|
|
diary_context = self._recent_diary_context(count=2)
|
|||
|
|
theme_hint = self._qzone_publish_theme_hint()
|
|||
|
|
temporal_context = self._qzone_temporal_context()
|
|||
|
|
recent_publish_context = self._qzone_recent_publish_context(qzone_state)
|
|||
|
|
memory_context = await self._qzone_memory_companion_context(
|
|||
|
|
purpose="publish_test",
|
|||
|
|
query="QQ空间生活说说 今日公开可写生活 当前日程 今日穿搭 最近吃饭 日记余味 自我时间线",
|
|||
|
|
)
|
|||
|
|
public_state_hint = self._qzone_relationship_safe_source(
|
|||
|
|
self._qzone_public_state_hint(daily_state if isinstance(daily_state, dict) else {}),
|
|||
|
|
source="qzone.publish_test.current_state",
|
|||
|
|
)
|
|||
|
|
current_schedule_hint = self._qzone_relationship_safe_source(
|
|||
|
|
self._format_plan_item_for_prompt(current_item),
|
|||
|
|
source="qzone.publish_test.current_schedule",
|
|||
|
|
)
|
|||
|
|
diary_context = self._qzone_relationship_safe_source(
|
|||
|
|
diary_context,
|
|||
|
|
source="qzone.publish_test.recent_diary",
|
|||
|
|
)
|
|||
|
|
memory_context = self._qzone_relationship_safe_source(
|
|||
|
|
memory_context,
|
|||
|
|
source="qzone.publish_test.memory",
|
|||
|
|
)
|
|||
|
|
relationship_authority_guard = self._qzone_relationship_authority_guard()
|
|||
|
|
prompt = f"""
|
|||
|
|
请以当前 Bot 人格写一条 QQ 空间说说。
|
|||
|
|
只输出说说正文,不要解释,不要加标题。
|
|||
|
|
|
|||
|
|
要求:
|
|||
|
|
- 30 到 120 字。
|
|||
|
|
- 像自然生活动态,不是公告、不是任务汇报。
|
|||
|
|
- 可以带一点公开可见的心情、天气或日记余味,但不要暴露插件、模型、内部状态数值。
|
|||
|
|
- 禁止出现“能量”“心理能量”“/100”“状态变量”“当前状态”等内部汇报词。
|
|||
|
|
- 不要 @ 用户,不要泄露私聊内容,不要写得像营销文。
|
|||
|
|
- 写作角度:{theme_hint}
|
|||
|
|
|
|||
|
|
【说说风格提示】
|
|||
|
|
{self._qzone_publish_style_prompt()}
|
|||
|
|
|
|||
|
|
【当前时间与季节】
|
|||
|
|
{temporal_context}
|
|||
|
|
|
|||
|
|
【公开可写的状态余味】
|
|||
|
|
{public_state_hint}
|
|||
|
|
|
|||
|
|
【当前/附近日程】
|
|||
|
|
{current_schedule_hint or "无明确日程"}
|
|||
|
|
|
|||
|
|
【近日私密日记余味】
|
|||
|
|
{diary_context or "暂无"}
|
|||
|
|
|
|||
|
|
【我会牢牢记住你 公开可写生活参考】
|
|||
|
|
{memory_context or "暂无"}
|
|||
|
|
使用方式:只选公开可写、不会泄露私聊或内部记忆来源的生活连续性。
|
|||
|
|
|
|||
|
|
【最近说说去重】
|
|||
|
|
{recent_publish_context or "暂无最近记录。"}
|
|||
|
|
|
|||
|
|
{relationship_authority_guard}
|
|||
|
|
|
|||
|
|
{self._format_worldview_adaptation_prompt()}
|
|||
|
|
""".strip()
|
|||
|
|
try:
|
|||
|
|
draft = await self._llm_call(
|
|||
|
|
prompt,
|
|||
|
|
max_tokens=180,
|
|||
|
|
provider_id=self._task_provider(self.mai_style_provider_id, self.llm_provider_id),
|
|||
|
|
task="qzone_publish_test",
|
|||
|
|
)
|
|||
|
|
draft = await self._sanitize_qzone_life_post_text(draft, prompt=prompt)
|
|||
|
|
except Exception as exc:
|
|||
|
|
draft = ""
|
|||
|
|
lines.append(f"- 草稿生成:失败,{_single_line(exc, 160)}")
|
|||
|
|
if draft:
|
|||
|
|
lines.append("- 草稿生成:成功")
|
|||
|
|
lines.append(f"- 将传入工具参数:{{\"text\":\"{draft}\"}}")
|
|||
|
|
lines.append(f"- 草稿正文:{draft}")
|
|||
|
|
else:
|
|||
|
|
lines.append("- 草稿生成:失败或为空")
|
|||
|
|
image_enabled = bool(getattr(self, "enable_qzone_generated_image_publish", False))
|
|||
|
|
image_probability = max(0.0, min(1.0, _safe_float(getattr(self, "qzone_generated_image_probability", 0.25), 0.25)))
|
|||
|
|
generator_available = callable(getattr(self, "_generate_photo_image", None))
|
|||
|
|
backend_summary = ""
|
|||
|
|
summary_getter = getattr(self, "_photo_generation_backend_config_summary", None)
|
|||
|
|
if callable(summary_getter):
|
|||
|
|
try:
|
|||
|
|
backend_summary = _single_line(summary_getter(), 180)
|
|||
|
|
except Exception:
|
|||
|
|
backend_summary = ""
|
|||
|
|
prefix = self._qzone_reason_prefix("life_publish")
|
|||
|
|
last_image_status = _single_line(qzone_state.get(f"last_{prefix}_generated_image_status"), 80)
|
|||
|
|
last_image_note = _single_line(qzone_state.get(f"last_{prefix}_generated_image_note"), 160)
|
|||
|
|
lines.append(
|
|||
|
|
"- 配图预检:"
|
|||
|
|
f"开关={'开启' if image_enabled else '关闭'},"
|
|||
|
|
f"概率={image_probability:.0%},"
|
|||
|
|
f"生图入口={'可用' if generator_available else '不可用'}"
|
|||
|
|
)
|
|||
|
|
if backend_summary:
|
|||
|
|
lines.append(f"- 生图后端:{backend_summary}")
|
|||
|
|
if last_image_status or last_image_note:
|
|||
|
|
lines.append(f"- 上次配图状态:{last_image_status or '-'} {last_image_note or ''}".rstrip())
|
|||
|
|
if image_enabled and image_probability <= 0:
|
|||
|
|
lines.append("- 配图结论:概率为 0,不会自动带图。")
|
|||
|
|
elif not image_enabled:
|
|||
|
|
lines.append("- 配图结论:说说配图开关未开启,不会自动带图。")
|
|||
|
|
elif not generator_available:
|
|||
|
|
lines.append("- 配图结论:缺少生图入口,不会自动带图。")
|
|||
|
|
else:
|
|||
|
|
lines.append("- 配图结论:满足发布条件时会按概率尝试生成 1 张配图;生成失败会回退纯文字。")
|
|||
|
|
lines.append("结果:模拟完成。若要真实发布,请使用 `陪伴 发说说 <正文>` 或让模型调用带 text 的 `pc_qzone_publish_feed`。")
|
|||
|
|
return "\n".join(lines)
|
|||
|
|
|
|||
|
|
async def _test_qzone_publish_image_chain(self, event: AstrMessageEvent | None = None) -> str:
|
|||
|
|
lines = ["QQ 空间配图链路测试:"]
|
|||
|
|
lines.append("- 真实发布:否,本指令只生成草稿和配图,不发 QQ 空间")
|
|||
|
|
image_enabled = bool(getattr(self, "enable_qzone_generated_image_publish", False))
|
|||
|
|
image_probability = max(0.0, min(1.0, _safe_float(getattr(self, "qzone_generated_image_probability", 0.25), 0.25)))
|
|||
|
|
generator_available = callable(getattr(self, "_generate_photo_image", None))
|
|||
|
|
lines.append(f"- 配图开关:{'开启' if image_enabled else '关闭'}")
|
|||
|
|
lines.append(f"- 自动配图概率:{image_probability:.0%}(本测试会绕过概率,只检查生图链路)")
|
|||
|
|
lines.append(f"- 生图入口:{'可用' if generator_available else '不可用'}")
|
|||
|
|
if not self._qzone_platform_supported(event):
|
|||
|
|
lines.append(f"结果:{self._qzone_platform_unavailable_message()}")
|
|||
|
|
return "\n".join(lines)
|
|||
|
|
summary_getter = getattr(self, "_photo_generation_backend_config_summary", None)
|
|||
|
|
if callable(summary_getter):
|
|||
|
|
try:
|
|||
|
|
backend_summary = _single_line(summary_getter(), 180)
|
|||
|
|
except Exception:
|
|||
|
|
backend_summary = ""
|
|||
|
|
if backend_summary:
|
|||
|
|
lines.append(f"- 生图后端:{backend_summary}")
|
|||
|
|
if not self.enable_qzone_integration:
|
|||
|
|
lines.append("结果:QQ 空间动态层未启用,配图测试取消。")
|
|||
|
|
return "\n".join(lines)
|
|||
|
|
if not image_enabled:
|
|||
|
|
lines.append("结果:说说配图开关未开启,配图测试取消。")
|
|||
|
|
return "\n".join(lines)
|
|||
|
|
if not generator_available:
|
|||
|
|
lines.append("结果:缺少主动生图入口,配图测试取消。")
|
|||
|
|
return "\n".join(lines)
|
|||
|
|
|
|||
|
|
state = self._qzone_state_dict()
|
|||
|
|
daily_state = self.data.get("daily_state", {})
|
|||
|
|
current_item = self._get_current_plan_item(self.data.get("daily_plan", {}))
|
|||
|
|
diary_context = self._recent_diary_context(count=2)
|
|||
|
|
public_state_hint = self._qzone_relationship_safe_source(
|
|||
|
|
self._qzone_public_state_hint(daily_state if isinstance(daily_state, dict) else {}),
|
|||
|
|
source="qzone.publish_image_test.current_state",
|
|||
|
|
)
|
|||
|
|
current_schedule_hint = self._qzone_relationship_safe_source(
|
|||
|
|
self._format_plan_item_for_prompt(current_item),
|
|||
|
|
source="qzone.publish_image_test.current_schedule",
|
|||
|
|
)
|
|||
|
|
diary_context = self._qzone_relationship_safe_source(
|
|||
|
|
diary_context,
|
|||
|
|
source="qzone.publish_image_test.recent_diary",
|
|||
|
|
)
|
|||
|
|
relationship_authority_guard = self._qzone_relationship_authority_guard()
|
|||
|
|
prompt = f"""
|
|||
|
|
请以当前 Bot 人格写一条 QQ 空间说说,用来测试配图生成。
|
|||
|
|
只输出说说正文,不要解释,不要加标题。
|
|||
|
|
|
|||
|
|
要求:
|
|||
|
|
- 30 到 100 字。
|
|||
|
|
- 像自然生活动态,最好包含一个能被画出来的具体场景或物件。
|
|||
|
|
- 不要 @ 用户,不要泄露私聊内容,不要出现插件、模型、系统提示、内部状态数值。
|
|||
|
|
- 写作角度:{self._qzone_publish_theme_hint()}
|
|||
|
|
|
|||
|
|
【说说风格提示】
|
|||
|
|
{self._qzone_publish_style_prompt()}
|
|||
|
|
|
|||
|
|
【当前时间与季节】
|
|||
|
|
{self._qzone_temporal_context()}
|
|||
|
|
|
|||
|
|
【公开可写的状态余味】
|
|||
|
|
{public_state_hint}
|
|||
|
|
|
|||
|
|
【当前/附近日程】
|
|||
|
|
{current_schedule_hint or "无明确日程"}
|
|||
|
|
|
|||
|
|
【近日私密日记余味】
|
|||
|
|
{diary_context or "暂无"}
|
|||
|
|
|
|||
|
|
【最近说说去重】
|
|||
|
|
{self._qzone_recent_publish_context(state) or "暂无最近记录。"}
|
|||
|
|
|
|||
|
|
{relationship_authority_guard}
|
|||
|
|
|
|||
|
|
{self._format_worldview_adaptation_prompt()}
|
|||
|
|
""".strip()
|
|||
|
|
try:
|
|||
|
|
draft = await self._llm_call(
|
|||
|
|
prompt,
|
|||
|
|
max_tokens=160,
|
|||
|
|
provider_id=self._task_provider(self.mai_style_provider_id, self.llm_provider_id),
|
|||
|
|
task="qzone_publish_image_test_draft",
|
|||
|
|
)
|
|||
|
|
draft = await self._sanitize_qzone_life_post_text(draft, prompt=prompt)
|
|||
|
|
except Exception as exc:
|
|||
|
|
lines.append(f"结果:草稿生成失败,{_single_line(exc, 160)}")
|
|||
|
|
return "\n".join(lines)
|
|||
|
|
if not draft:
|
|||
|
|
lines.append("结果:草稿为空或不安全,配图测试取消。")
|
|||
|
|
return "\n".join(lines)
|
|||
|
|
lines.append(f"- 草稿正文:{draft}")
|
|||
|
|
images = await self._maybe_generate_qzone_publish_image(
|
|||
|
|
post_text=draft,
|
|||
|
|
reason="life_publish",
|
|||
|
|
daily_state=daily_state if isinstance(daily_state, dict) else {},
|
|||
|
|
current_item=current_item,
|
|||
|
|
diary_context=diary_context,
|
|||
|
|
state=state,
|
|||
|
|
force=True,
|
|||
|
|
)
|
|||
|
|
prefix = self._qzone_reason_prefix("life_publish")
|
|||
|
|
status = _single_line(state.get(f"last_{prefix}_generated_image_status"), 80)
|
|||
|
|
note = _single_line(state.get(f"last_{prefix}_generated_image_note"), 160)
|
|||
|
|
backend = _single_line(state.get(f"last_{prefix}_generated_image_backend"), 60)
|
|||
|
|
caption = _single_line(state.get(f"last_{prefix}_generated_image_caption"), 160)
|
|||
|
|
visual_anchor = _single_line(state.get(f"last_{prefix}_generated_image_anchor"), 120)
|
|||
|
|
composition = _single_line(state.get(f"last_{prefix}_generated_image_composition"), 120)
|
|||
|
|
reference_image = _single_line(state.get(f"last_{prefix}_generated_image_reference"), 220)
|
|||
|
|
reference_exists = bool(state.get(f"last_{prefix}_generated_image_reference_exists", False))
|
|||
|
|
if callable(getattr(self, "_save_data_sync", None)):
|
|||
|
|
try:
|
|||
|
|
self._save_data_sync()
|
|||
|
|
except Exception:
|
|||
|
|
pass
|
|||
|
|
if images:
|
|||
|
|
lines.append("- 生图结果:成功")
|
|||
|
|
if backend:
|
|||
|
|
lines.append(f"- 后端:{backend}")
|
|||
|
|
if caption:
|
|||
|
|
lines.append(f"- 画面说明:{caption}")
|
|||
|
|
if visual_anchor:
|
|||
|
|
lines.append(f"- 视觉锚点:{visual_anchor}")
|
|||
|
|
if composition:
|
|||
|
|
lines.append(f"- 构图:{composition}")
|
|||
|
|
if reference_image:
|
|||
|
|
lines.append(f"- 自拍参考图:{'可用' if reference_exists else '不可用'} {_single_line(reference_image, 160)}")
|
|||
|
|
lines.append(f"- 图片路径:{_single_line(images[0], 220)}")
|
|||
|
|
lines.append("结果:配图生成链路可用。下一步可用 `陪伴 发说说 <正文>` 或等待自动说说验证上传。")
|
|||
|
|
else:
|
|||
|
|
lines.append(f"- 生图结果:{status or '失败'}")
|
|||
|
|
if note:
|
|||
|
|
lines.append(f"- 原因:{note}")
|
|||
|
|
if visual_anchor:
|
|||
|
|
lines.append(f"- 视觉锚点:{visual_anchor}")
|
|||
|
|
if composition:
|
|||
|
|
lines.append(f"- 构图:{composition}")
|
|||
|
|
if reference_image:
|
|||
|
|
lines.append(f"- 自拍参考图:{'可用' if reference_exists else '不可用'} {_single_line(reference_image, 160)}")
|
|||
|
|
lines.append("结果:没有生成可用于说说的图片。")
|
|||
|
|
return "\n".join(lines)
|
|||
|
|
|
|||
|
|
async def _test_qzone_integration(self, event: AstrMessageEvent | None, target_id: str = "") -> str:
|
|||
|
|
lines = ["QQ 空间测试:"]
|
|||
|
|
|
|||
|
|
lines.append(f"- 整合开关:{'开启' if self.enable_qzone_integration else '关闭'}")
|
|||
|
|
lines.append("- 内置服务:可用")
|
|||
|
|
lines.append("- 外部插件依赖:无")
|
|||
|
|
|
|||
|
|
if not self._qzone_platform_supported(event):
|
|||
|
|
lines.append(f"结果:{self._qzone_platform_unavailable_message()}")
|
|||
|
|
return "\n".join(lines)
|
|||
|
|
|
|||
|
|
if not self.enable_qzone_integration:
|
|||
|
|
lines.append("结果:整合开关关闭。")
|
|||
|
|
return "\n".join(lines)
|
|||
|
|
|
|||
|
|
target = _single_line(target_id, 40)
|
|||
|
|
try:
|
|||
|
|
cookie_header = await self._qzone_get_cookies(event)
|
|||
|
|
ctx = self._qzone_context_from_cookies(cookie_header)
|
|||
|
|
target = target or str(ctx.get("uin") or "")
|
|||
|
|
lines.append(f"- Cookie:已获取,登录 QQ {ctx.get('uin')}")
|
|||
|
|
lines.append("- 读取动态:可用")
|
|||
|
|
lines.append("- 发布说说:可用")
|
|||
|
|
lines.append("- 点赞/评论:可用")
|
|||
|
|
posts = await self._qzone_query_feeds(
|
|||
|
|
event,
|
|||
|
|
target_id=target or None,
|
|||
|
|
pos=0,
|
|||
|
|
num=1,
|
|||
|
|
with_detail=True,
|
|||
|
|
cookie_header=cookie_header,
|
|||
|
|
)
|
|||
|
|
if not posts:
|
|||
|
|
lines.append(f"- 查询目标:{target or '默认'}")
|
|||
|
|
lines.append("- 查询结果:空")
|
|||
|
|
lines.append("结果:读取链路可调用,但没有拿到动态。")
|
|||
|
|
return "\n".join(lines)
|
|||
|
|
post = posts[0]
|
|||
|
|
text = _single_line(getattr(post, "text", "") or getattr(post, "rt_con", ""), 120)
|
|||
|
|
images = list(getattr(post, "images", []) or [])
|
|||
|
|
lines.append(f"- 查询目标:{target or '默认'}")
|
|||
|
|
lines.append("- 查询结果:成功")
|
|||
|
|
lines.append(f"- 作者:{_single_line(getattr(post, 'name', ''), 40) or '未知'}")
|
|||
|
|
lines.append(f"- QQ:{str(getattr(post, 'uin', '') or '') or '未知'}")
|
|||
|
|
lines.append(f"- 内容:{text or '无文本'}")
|
|||
|
|
lines.append(f"- 图片数:{len(images)}")
|
|||
|
|
lines.append("结果:QQ 空间读取链路正常。")
|
|||
|
|
return "\n".join(lines)
|
|||
|
|
except Exception as exc:
|
|||
|
|
lines.append(f"- 查询目标:{target or '默认'}")
|
|||
|
|
error_text = _single_line(exc, 160)
|
|||
|
|
if "空响应" in error_text:
|
|||
|
|
error_text = "接口返回空响应,通常表示目标空间不可见、无权限访问,或当前 Cookie 对该目标无访问权"
|
|||
|
|
lines.append(f"- 查询结果:失败:{error_text}")
|
|||
|
|
lines.append("结果:内置服务已加载,但 QQ 空间访问失败。")
|
|||
|
|
return "\n".join(lines)
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def _qzone_reason_prefix(reason: str) -> str:
|
|||
|
|
if reason == "emotional_vent":
|
|||
|
|
return "emotional_vent"
|
|||
|
|
if reason == "manual_publish":
|
|||
|
|
return "manual_publish"
|
|||
|
|
return "life_publish"
|
|||
|
|
|
|||
|
|
def _qzone_reusable_draft(self, state: dict[str, Any], reason: str, *, now: float | None = None, max_age_hours: float = 72.0) -> str:
|
|||
|
|
if not isinstance(state, dict):
|
|||
|
|
return ""
|
|||
|
|
prefix = self._qzone_reason_prefix(reason)
|
|||
|
|
status = str(state.get(f"last_{prefix}_status") or "").strip()
|
|||
|
|
if not (status.startswith("failed:") or status.startswith("paused:") or status.startswith("retrying:")):
|
|||
|
|
return ""
|
|||
|
|
current = _now_ts() if now is None else float(now)
|
|||
|
|
draft_at = _safe_float(state.get(f"last_{prefix}_draft_at"), 0)
|
|||
|
|
if not draft_at or current - draft_at > max(1.0, float(max_age_hours)) * 3600:
|
|||
|
|
return ""
|
|||
|
|
draft_key = f"last_{prefix}_draft"
|
|||
|
|
draft = _single_line(state.get(draft_key), 300)
|
|||
|
|
cleaned = _single_line(
|
|||
|
|
self._qzone_relationship_safe_source(
|
|||
|
|
draft,
|
|||
|
|
source=f"qzone.reusable_draft.{prefix}",
|
|||
|
|
),
|
|||
|
|
300,
|
|||
|
|
)
|
|||
|
|
if cleaned != draft:
|
|||
|
|
state[draft_key] = cleaned
|
|||
|
|
if len(cleaned) < 12:
|
|||
|
|
state.pop(draft_key, None)
|
|||
|
|
state.pop(f"last_{prefix}_draft_at", None)
|
|||
|
|
return ""
|
|||
|
|
return cleaned
|
|||
|
|
|
|||
|
|
def _qzone_reusable_generated_image(self, state: dict[str, Any], reason: str, post_text: str, *, now: float | None = None) -> list[str]:
|
|||
|
|
if not isinstance(state, dict):
|
|||
|
|
return []
|
|||
|
|
prefix = self._qzone_reason_prefix(reason)
|
|||
|
|
current = _now_ts() if now is None else float(now)
|
|||
|
|
image_at = _safe_float(state.get(f"last_{prefix}_generated_image_at"), 0)
|
|||
|
|
if not image_at or current - image_at > 72 * 3600:
|
|||
|
|
return []
|
|||
|
|
stored_text = _single_line(state.get(f"last_{prefix}_generated_image_text"), 300)
|
|||
|
|
if stored_text and stored_text != _single_line(post_text, 300):
|
|||
|
|
return []
|
|||
|
|
image_path = str(state.get(f"last_{prefix}_generated_image_path") or "").strip()
|
|||
|
|
if not image_path:
|
|||
|
|
return []
|
|||
|
|
if not re.match(r"^(?:https?://|file://|data:)", image_path, flags=re.I) and not Path(image_path).exists():
|
|||
|
|
return []
|
|||
|
|
logger.info("[PrivateCompanion] QQ 空间复用待发布配图: reason=%s path=%s", reason, _single_line(image_path, 160))
|
|||
|
|
return [image_path]
|
|||
|
|
|
|||
|
|
def _qzone_note_publish_image_status(
|
|||
|
|
self,
|
|||
|
|
state: dict[str, Any] | None,
|
|||
|
|
reason: str,
|
|||
|
|
status: str,
|
|||
|
|
note: Any = "",
|
|||
|
|
*,
|
|||
|
|
path: Any = "",
|
|||
|
|
backend: Any = "",
|
|||
|
|
caption: Any = "",
|
|||
|
|
reference_image: Any = "",
|
|||
|
|
reference_exists: bool | None = None,
|
|||
|
|
visual_anchor: Any = "",
|
|||
|
|
composition: Any = "",
|
|||
|
|
) -> None:
|
|||
|
|
if not isinstance(state, dict):
|
|||
|
|
return
|
|||
|
|
prefix = self._qzone_reason_prefix(reason)
|
|||
|
|
state[f"last_{prefix}_generated_image_status"] = _single_line(status, 60)
|
|||
|
|
state[f"last_{prefix}_generated_image_note"] = _single_line(note, 180)
|
|||
|
|
state[f"last_{prefix}_generated_image_checked_at"] = _now_ts()
|
|||
|
|
if path:
|
|||
|
|
state[f"last_{prefix}_generated_image_path"] = _path_text(path, 1000)
|
|||
|
|
if backend:
|
|||
|
|
state[f"last_{prefix}_generated_image_backend"] = _single_line(backend, 40)
|
|||
|
|
if caption:
|
|||
|
|
state[f"last_{prefix}_generated_image_caption"] = _single_line(caption, 180)
|
|||
|
|
if reference_image:
|
|||
|
|
state[f"last_{prefix}_generated_image_reference"] = _path_text(reference_image, 1000)
|
|||
|
|
if reference_exists is not None:
|
|||
|
|
state[f"last_{prefix}_generated_image_reference_exists"] = bool(reference_exists)
|
|||
|
|
if visual_anchor:
|
|||
|
|
state[f"last_{prefix}_generated_image_anchor"] = _single_line(visual_anchor, 120)
|
|||
|
|
if composition:
|
|||
|
|
state[f"last_{prefix}_generated_image_composition"] = _single_line(composition, 120)
|
|||
|
|
|
|||
|
|
def _qzone_clear_pending_publish_assets(self, state: dict[str, Any], reason: str) -> None:
|
|||
|
|
if not isinstance(state, dict):
|
|||
|
|
return
|
|||
|
|
prefix = self._qzone_reason_prefix(reason)
|
|||
|
|
for key in (
|
|||
|
|
f"last_{prefix}_draft",
|
|||
|
|
f"last_{prefix}_draft_at",
|
|||
|
|
f"last_{prefix}_generated_image_path",
|
|||
|
|
f"last_{prefix}_generated_image_at",
|
|||
|
|
f"last_{prefix}_generated_image_text",
|
|||
|
|
f"last_{prefix}_generated_image_reference",
|
|||
|
|
f"last_{prefix}_generated_image_reference_exists",
|
|||
|
|
f"last_{prefix}_generated_image_anchor",
|
|||
|
|
f"last_{prefix}_generated_image_composition",
|
|||
|
|
):
|
|||
|
|
state.pop(key, None)
|
|||
|
|
|
|||
|
|
async def _maybe_generate_qzone_publish_image(
|
|||
|
|
self,
|
|||
|
|
*,
|
|||
|
|
post_text: str,
|
|||
|
|
reason: str,
|
|||
|
|
daily_state: dict[str, Any] | None = None,
|
|||
|
|
current_item: Any = None,
|
|||
|
|
diary_context: str = "",
|
|||
|
|
state: dict[str, Any] | None = None,
|
|||
|
|
force: bool = False,
|
|||
|
|
) -> list[str]:
|
|||
|
|
reusable = [] if force else self._qzone_reusable_generated_image(state if isinstance(state, dict) else {}, reason, post_text)
|
|||
|
|
if reusable:
|
|||
|
|
self._qzone_note_publish_image_status(state, reason, "reused", "复用上次待发布配图", path=reusable[0])
|
|||
|
|
return reusable
|
|||
|
|
if not (
|
|||
|
|
getattr(self, "enable_qzone_generated_image_publish", False)
|
|||
|
|
and getattr(self, "enable_qzone_integration", False)
|
|||
|
|
):
|
|||
|
|
self._qzone_note_publish_image_status(state, reason, "skipped:disabled", "QQ 空间配图开关未开启")
|
|||
|
|
return []
|
|||
|
|
probability = max(0.0, min(1.0, _safe_float(getattr(self, "qzone_generated_image_probability", 0.25), 0.25)))
|
|||
|
|
if not force and (probability <= 0 or random.random() > probability):
|
|||
|
|
self._qzone_note_publish_image_status(state, reason, "skipped:probability", f"未命中配图概率 {probability:.0%}")
|
|||
|
|
return []
|
|||
|
|
if callable(getattr(self, "_daily_token_soft_limit_should_defer", None)) and self._daily_token_soft_limit_should_defer("photo_prompt"):
|
|||
|
|
logger.info("[PrivateCompanion] QQ 空间主动配图跳过: token_soft_limit")
|
|||
|
|
self._qzone_note_publish_image_status(state, reason, "skipped:token_budget", "token 软上限保护")
|
|||
|
|
return []
|
|||
|
|
generator = getattr(self, "_generate_photo_image", None)
|
|||
|
|
if not callable(generator):
|
|||
|
|
logger.info("[PrivateCompanion] QQ 空间主动配图跳过: image_generator_unavailable")
|
|||
|
|
self._qzone_note_publish_image_status(state, reason, "skipped:no_generator", "缺少 _generate_photo_image 生图入口")
|
|||
|
|
return []
|
|||
|
|
|
|||
|
|
style_name, style_instruction = self._get_photo_style_instruction()
|
|||
|
|
post_text = self._qzone_relationship_safe_source(post_text, source="qzone.image.post_text")
|
|||
|
|
current_desc = self._qzone_relationship_safe_source(
|
|||
|
|
self._format_plan_item_for_prompt(current_item),
|
|||
|
|
source="qzone.image.current_schedule",
|
|||
|
|
) or "无明确日程"
|
|||
|
|
state_desc = self._qzone_relationship_safe_source(
|
|||
|
|
self._qzone_public_state_hint(daily_state if isinstance(daily_state, dict) else {}),
|
|||
|
|
source="qzone.image.current_state",
|
|||
|
|
)
|
|||
|
|
diary_context = self._qzone_relationship_safe_source(
|
|||
|
|
diary_context,
|
|||
|
|
source="qzone.image.recent_diary",
|
|||
|
|
)
|
|||
|
|
content_options = ""
|
|||
|
|
try:
|
|||
|
|
content_options = self._format_content_choice_options_for_prompt()
|
|||
|
|
except Exception:
|
|||
|
|
content_options = "生活小物、窗边光影、路上风景、桌面一角、随手自拍、偶遇小动物。"
|
|||
|
|
content_options = self._qzone_relationship_safe_source(
|
|||
|
|
content_options,
|
|||
|
|
source="qzone.image.content_options",
|
|||
|
|
)
|
|||
|
|
relationship_authority_guard = self._qzone_relationship_authority_guard()
|
|||
|
|
qzone_selfie_reference_path = ""
|
|||
|
|
qzone_selfie_reference_exists = False
|
|||
|
|
reference_getter = getattr(self, "_photo_persona_reference_image_for_kind_async", None)
|
|||
|
|
if callable(reference_getter):
|
|||
|
|
try:
|
|||
|
|
qzone_selfie_reference_path = await reference_getter(
|
|||
|
|
"selfie",
|
|||
|
|
allow_daily_outfit=True,
|
|||
|
|
selection_context=f"说说:{post_text}\n当前日程:{current_desc}\n当前状态:{state_desc}",
|
|||
|
|
)
|
|||
|
|
except Exception as ref_exc:
|
|||
|
|
logger.info(
|
|||
|
|
"[PrivateCompanion] QQ 空间自拍参考图预检失败: reason=%s error=%s",
|
|||
|
|
_single_line(reason, 40),
|
|||
|
|
_single_line(ref_exc, 120),
|
|||
|
|
)
|
|||
|
|
qzone_selfie_reference_path = ""
|
|||
|
|
try:
|
|||
|
|
qzone_selfie_reference_exists = bool(
|
|||
|
|
qzone_selfie_reference_path and Path(str(qzone_selfie_reference_path)).exists()
|
|||
|
|
)
|
|||
|
|
except (OSError, ValueError):
|
|||
|
|
qzone_selfie_reference_exists = False
|
|||
|
|
reference_text = (
|
|||
|
|
"有可用参考图。可以选择 selfie 让人物自然入镜,但不要默认镜前自拍;只有正文或日程明确需要穿搭/照镜子时才用镜前构图。选择 selfie 时 prompt 必须写明保持参考图中的人物身份、脸部、发色、瞳色、穿搭连续性。"
|
|||
|
|
if qzone_selfie_reference_path
|
|||
|
|
else "当前没有可用自拍参考图。可以让人物自然入镜,人物外貌参考人格描述和公开状态;优先使用第一视角手部、侧脸、背影、肩颈半身、影子、随身小物等不强依赖精确脸部的方式,避免凭空追加人格里没有的脸部细节,也不要默认镜前自拍。"
|
|||
|
|
)
|
|||
|
|
prompt = f"""
|
|||
|
|
请为一条即将公开发布到 QQ 空间的说说生成一张配图提示词。
|
|||
|
|
只输出 JSON,不要解释。
|
|||
|
|
|
|||
|
|
【说说正文】
|
|||
|
|
{_single_line(post_text, 300)}
|
|||
|
|
|
|||
|
|
【人格】
|
|||
|
|
{self._get_default_persona_prompt()}
|
|||
|
|
|
|||
|
|
【公开可写的状态余味】
|
|||
|
|
{state_desc}
|
|||
|
|
|
|||
|
|
【当前/附近日程】
|
|||
|
|
{current_desc}
|
|||
|
|
|
|||
|
|
【近日日记余味】
|
|||
|
|
{_single_line(diary_context, 500) or "暂无"}
|
|||
|
|
|
|||
|
|
{self._format_worldview_adaptation_prompt()}
|
|||
|
|
|
|||
|
|
{relationship_authority_guard}
|
|||
|
|
|
|||
|
|
【可选画面方向】
|
|||
|
|
{content_options}
|
|||
|
|
|
|||
|
|
【自拍参考图状态】
|
|||
|
|
{reference_text}
|
|||
|
|
|
|||
|
|
【空间配图风格提示】
|
|||
|
|
{self._qzone_publish_image_style_prompt()}
|
|||
|
|
|
|||
|
|
【生图风格】
|
|||
|
|
{style_name}
|
|||
|
|
风格要求:{style_instruction}
|
|||
|
|
|
|||
|
|
输出 JSON:
|
|||
|
|
{{
|
|||
|
|
"kind": "selfie 或 text2img;按说说正文选择,不要固定优先镜前自拍",
|
|||
|
|
"visual_anchor": "本图唯一视觉锚点,例如第一视角手部与饮品/桌面小物/路上夕光/侧脸看窗边光影/背影走在路上/餐盘与衣袖;必须具体",
|
|||
|
|
"composition": "构图一句话,例如第一视角手部近景/桌面俯拍/侧脸三分构图/背影环境中景/路边半身随拍/窗边剪影;镜前自拍只能偶尔使用",
|
|||
|
|
"prompt": "给生图后端的中文提示词,包含唯一主体、场景、光线、构图、情绪和风格;不要写聊天口吻",
|
|||
|
|
"caption": "一句画面说明"
|
|||
|
|
}}
|
|||
|
|
|
|||
|
|
要求:
|
|||
|
|
1. 图片必须像公开动态配图,不要包含私聊、系统、插件、模型、内部状态数值。
|
|||
|
|
2. 先确定一个“唯一视觉锚点”,不要把多个主体拼在一张图里;画面要贴合说说正文和当前日程,不要为了配图硬画无关内容。
|
|||
|
|
3. 人物可以入镜,但不要每次都自拍;在第一视角手部、桌面小物、食物饮品、路上光影、窗边侧脸、背影、影子、随身小物和半身随拍之间轮换。
|
|||
|
|
4. 镜前自拍、镜中自拍、手机挡脸自拍不是默认模板;只有正文/日程明确涉及穿搭、整理仪容、出门前照镜子或房间镜子时才使用,且不要连续复用。
|
|||
|
|
5. 如果有自拍参考图,选择 selfie 时必须写清“保留参考图人物身份和外观”“脸部完整清晰”“不要裁脸/遮脸/只拍身体局部”,并让场景来自当前日程;但仍要优先考虑非镜前构图。
|
|||
|
|
6. 如果没有自拍参考图,仍可选择人物入镜;人物外貌以人格描述、公开状态和风格设定为准,不要追加人格里没有的脸部细节。优先使用不强依赖精确脸部的自然入镜方式,比如侧脸、背影、第一视角手部、肩颈半身、窗边剪影。
|
|||
|
|
7. 如果选择 text2img:也可以保留人的存在感,如手边物件、脚步、背影、影子或随身小物;只有画面确实不适合人物入镜时才纯物件/纯风景。
|
|||
|
|
8. 不要包含 NSFW、真实用户隐私、聊天截图或电脑屏幕内容;避免文字、水印、UI、二维码、聊天气泡。
|
|||
|
|
9. prompt 必须体现上面的生图风格要求,且不能是泛泛的“好看的照片/生活记录/天气图”。
|
|||
|
|
""".strip()
|
|||
|
|
try:
|
|||
|
|
text = await self._llm_call(
|
|||
|
|
prompt,
|
|||
|
|
max_tokens=360,
|
|||
|
|
provider_id=self._task_provider(self.photo_prompt_provider_id, self.mai_style_provider_id),
|
|||
|
|
task=f"qzone_{reason}_photo_prompt",
|
|||
|
|
)
|
|||
|
|
payload = self._extract_json_payload(text or "")
|
|||
|
|
if isinstance(payload, dict):
|
|||
|
|
workflow_kind = _single_line(payload.get("kind"), 60).lower()
|
|||
|
|
visual_anchor = _single_line(payload.get("visual_anchor"), 120)
|
|||
|
|
composition = _single_line(payload.get("composition"), 120)
|
|||
|
|
image_prompt = _single_line(payload.get("prompt"), 600)
|
|||
|
|
caption = _single_line(payload.get("caption"), 180)
|
|||
|
|
else:
|
|||
|
|
workflow_kind = "text2img"
|
|||
|
|
visual_anchor = ""
|
|||
|
|
composition = ""
|
|||
|
|
image_prompt = _single_line(text, 600)
|
|||
|
|
caption = image_prompt
|
|||
|
|
if any(token in workflow_kind for token in ("selfie", "portrait", "自拍", "人像", "人物", "出镜")):
|
|||
|
|
workflow_kind = "selfie"
|
|||
|
|
elif any(token in workflow_kind for token in ("text2img", "scene", "photo", "风景", "静物", "物件")):
|
|||
|
|
workflow_kind = "text2img"
|
|||
|
|
else:
|
|||
|
|
workflow_kind = "text2img"
|
|||
|
|
if not image_prompt:
|
|||
|
|
image_prompt = f"QQ 空间公开动态配图,{_single_line(post_text, 160)},{style_instruction}"
|
|||
|
|
if visual_anchor and visual_anchor not in image_prompt:
|
|||
|
|
image_prompt = f"唯一视觉锚点:{visual_anchor}。{image_prompt}"
|
|||
|
|
if composition and composition not in image_prompt:
|
|||
|
|
image_prompt = f"{image_prompt}。构图:{composition}"
|
|||
|
|
if workflow_kind == "selfie":
|
|||
|
|
if qzone_selfie_reference_path:
|
|||
|
|
image_prompt = (
|
|||
|
|
f"{image_prompt}。保留参考图中的人物身份、脸部、发色、瞳色和穿搭连续性;"
|
|||
|
|
"脸部完整清晰,头发、肩颈和上半身自然入镜;不要裁脸、遮脸、背影、只拍身体局部。"
|
|||
|
|
)
|
|||
|
|
else:
|
|||
|
|
image_prompt = (
|
|||
|
|
f"{image_prompt}。人物是画面主角,外貌参考人格描述、公开状态和风格设定;没有可用自拍参考图时不要追加人格里没有的脸部细节;"
|
|||
|
|
"优先使用第一视角手部、侧脸、背影、肩颈半身、窗边剪影、随身小物等自然入镜方式;不要默认镜前自拍或手机挡脸自拍,保持公开动态随手拍质感。"
|
|||
|
|
)
|
|||
|
|
else:
|
|||
|
|
image_prompt = (
|
|||
|
|
f"{image_prompt}。画面像 QQ 空间公开生活配图,单一主体清楚,不出现聊天截图、UI、二维码、水印或虚构人物脸部。"
|
|||
|
|
)
|
|||
|
|
reference_image_path = qzone_selfie_reference_path if workflow_kind == "selfie" else ""
|
|||
|
|
reference_exists = qzone_selfie_reference_exists if workflow_kind == "selfie" else False
|
|||
|
|
logger.info(
|
|||
|
|
"[PrivateCompanion] QQ 空间配图生图开始: reason=%s kind=%s anchor=%s composition=%s reference=%s reference_exists=%s post=%s prompt=%s",
|
|||
|
|
_single_line(reason, 40),
|
|||
|
|
_single_line(workflow_kind, 30),
|
|||
|
|
_single_line(visual_anchor, 80) or "-",
|
|||
|
|
_single_line(composition, 80) or "-",
|
|||
|
|
bool(reference_image_path),
|
|||
|
|
reference_exists,
|
|||
|
|
_single_line(post_text, 120),
|
|||
|
|
_single_line(image_prompt, 180),
|
|||
|
|
)
|
|||
|
|
backend_name, image_path, workflow_note = await generator(
|
|||
|
|
workflow_kind=workflow_kind,
|
|||
|
|
prompt_text=image_prompt,
|
|||
|
|
session_key=f"qzone_{reason}",
|
|||
|
|
reference_image_path=reference_image_path,
|
|||
|
|
)
|
|||
|
|
except Exception as exc:
|
|||
|
|
logger.info("[PrivateCompanion] QQ 空间主动配图失败: %s", _single_line(exc, 120))
|
|||
|
|
self._qzone_note_publish_image_status(
|
|||
|
|
state,
|
|||
|
|
reason,
|
|||
|
|
"failed:prompt_or_generate",
|
|||
|
|
exc,
|
|||
|
|
reference_image=qzone_selfie_reference_path,
|
|||
|
|
reference_exists=qzone_selfie_reference_exists,
|
|||
|
|
)
|
|||
|
|
return []
|
|||
|
|
if not image_path:
|
|||
|
|
logger.info("[PrivateCompanion] QQ 空间主动配图跳过: %s", _single_line(workflow_note, 160))
|
|||
|
|
self._qzone_note_publish_image_status(
|
|||
|
|
state,
|
|||
|
|
reason,
|
|||
|
|
"failed:no_image",
|
|||
|
|
workflow_note,
|
|||
|
|
backend=backend_name,
|
|||
|
|
reference_image=reference_image_path,
|
|||
|
|
reference_exists=reference_exists,
|
|||
|
|
visual_anchor=visual_anchor,
|
|||
|
|
composition=composition,
|
|||
|
|
)
|
|||
|
|
return []
|
|||
|
|
if not re.match(r"^(?:https?://|file://|data:)", str(image_path), flags=re.I) and not Path(str(image_path)).exists():
|
|||
|
|
logger.info("[PrivateCompanion] QQ 空间主动配图跳过: image_path_missing path=%s", _single_line(image_path, 160))
|
|||
|
|
self._qzone_note_publish_image_status(
|
|||
|
|
state,
|
|||
|
|
reason,
|
|||
|
|
"failed:path_missing",
|
|||
|
|
"生图返回路径不存在",
|
|||
|
|
path=image_path,
|
|||
|
|
backend=backend_name,
|
|||
|
|
reference_image=reference_image_path,
|
|||
|
|
reference_exists=reference_exists,
|
|||
|
|
visual_anchor=visual_anchor,
|
|||
|
|
composition=composition,
|
|||
|
|
)
|
|||
|
|
return []
|
|||
|
|
if isinstance(state, dict):
|
|||
|
|
prefix = self._qzone_reason_prefix(reason)
|
|||
|
|
state["last_generated_image_path"] = _path_text(image_path, 1000)
|
|||
|
|
state["last_generated_image_at"] = _now_ts()
|
|||
|
|
state["last_generated_image_reason"] = reason
|
|||
|
|
state["last_generated_image_caption"] = _single_line(caption, 180)
|
|||
|
|
state["last_generated_image_backend"] = _single_line(backend_name, 40)
|
|||
|
|
if visual_anchor:
|
|||
|
|
state["last_generated_image_anchor"] = _single_line(visual_anchor, 120)
|
|||
|
|
if composition:
|
|||
|
|
state["last_generated_image_composition"] = _single_line(composition, 120)
|
|||
|
|
if reference_image_path:
|
|||
|
|
state["last_generated_image_reference"] = _path_text(reference_image_path, 1000)
|
|||
|
|
state["last_generated_image_reference_exists"] = bool(reference_exists)
|
|||
|
|
state[f"last_{prefix}_generated_image_path"] = _path_text(image_path, 1000)
|
|||
|
|
state[f"last_{prefix}_generated_image_at"] = _now_ts()
|
|||
|
|
state[f"last_{prefix}_generated_image_text"] = _single_line(post_text, 300)
|
|||
|
|
state[f"last_{prefix}_generated_image_caption"] = _single_line(caption, 180)
|
|||
|
|
state[f"last_{prefix}_generated_image_backend"] = _single_line(backend_name, 40)
|
|||
|
|
if visual_anchor:
|
|||
|
|
state[f"last_{prefix}_generated_image_anchor"] = _single_line(visual_anchor, 120)
|
|||
|
|
if composition:
|
|||
|
|
state[f"last_{prefix}_generated_image_composition"] = _single_line(composition, 120)
|
|||
|
|
self._qzone_note_publish_image_status(
|
|||
|
|
state,
|
|||
|
|
reason,
|
|||
|
|
"generated",
|
|||
|
|
workflow_note or "ok",
|
|||
|
|
path=image_path,
|
|||
|
|
backend=backend_name,
|
|||
|
|
caption=caption,
|
|||
|
|
reference_image=reference_image_path,
|
|||
|
|
reference_exists=reference_exists,
|
|||
|
|
visual_anchor=visual_anchor,
|
|||
|
|
composition=composition,
|
|||
|
|
)
|
|||
|
|
logger.info(
|
|||
|
|
"[PrivateCompanion] QQ 空间主动配图完成: reason=%s backend=%s reference=%s reference_exists=%s path=%s",
|
|||
|
|
reason,
|
|||
|
|
_single_line(backend_name, 40),
|
|||
|
|
bool(reference_image_path),
|
|||
|
|
reference_exists,
|
|||
|
|
_single_line(image_path, 160),
|
|||
|
|
)
|
|||
|
|
return [image_path]
|
|||
|
|
|
|||
|
|
async def _maybe_publish_qzone_life_post(self) -> None:
|
|||
|
|
if not (self._qzone_available() and self.enable_qzone_life_publish):
|
|||
|
|
return
|
|||
|
|
now = _now_ts()
|
|||
|
|
state = self.data.setdefault("qzone_integration", {})
|
|||
|
|
if not isinstance(state, dict):
|
|||
|
|
self.data["qzone_integration"] = {}
|
|||
|
|
state = self.data["qzone_integration"]
|
|||
|
|
last_status = str(state.get("last_life_publish_status") or "").strip()
|
|||
|
|
if (
|
|||
|
|
last_status == "published"
|
|||
|
|
and now - _safe_float(state.get("last_life_publish_at"), 0) < max(4, self.qzone_life_publish_min_interval_hours) * 3600
|
|||
|
|
):
|
|||
|
|
return
|
|||
|
|
block_reason = self._qzone_auto_publish_block_reason(state, now=now)
|
|||
|
|
if block_reason:
|
|||
|
|
state["last_life_publish_status"] = f"paused:auth:{_single_line(block_reason, 80)}"
|
|||
|
|
state["last_life_publish_checked_at"] = now
|
|||
|
|
self._save_data_sync()
|
|||
|
|
return
|
|||
|
|
if now - _safe_float(state.get("last_life_publish_failed_at"), 0) < 15 * 60:
|
|||
|
|
return
|
|||
|
|
reusable_text = self._qzone_reusable_draft(state, "life_publish", now=now)
|
|||
|
|
if not reusable_text and random.random() > self.qzone_life_publish_probability:
|
|||
|
|
state["last_life_publish_status"] = "skipped:probability_miss"
|
|||
|
|
state["last_life_publish_checked_at"] = now
|
|||
|
|
self._save_data_sync()
|
|||
|
|
return
|
|||
|
|
preflight_error = await self._qzone_preflight_auto_publish(None, state=state, source="life_publish")
|
|||
|
|
if preflight_error:
|
|||
|
|
state["last_life_publish_failed_at"] = now
|
|||
|
|
state["last_life_publish_status"] = f"paused:auth:{_single_line(preflight_error, 80)}"
|
|||
|
|
state["last_life_publish_checked_at"] = now
|
|||
|
|
self._save_data_sync()
|
|||
|
|
return
|
|||
|
|
daily_state = self.data.get("daily_state", {})
|
|||
|
|
current_item = self._get_current_plan_item(self.data.get("daily_plan", {}))
|
|||
|
|
diary_context = self._recent_diary_context(count=2)
|
|||
|
|
theme_hint = self._qzone_publish_theme_hint()
|
|||
|
|
temporal_context = self._qzone_temporal_context()
|
|||
|
|
recent_publish_context = self._qzone_recent_publish_context(state)
|
|||
|
|
memory_context = await self._qzone_memory_companion_context(
|
|||
|
|
purpose="publish",
|
|||
|
|
query="QQ空间生活说说 今日公开可写生活 当前日程 今日穿搭 最近吃饭 日记余味 自我时间线",
|
|||
|
|
)
|
|||
|
|
public_state_hint = self._qzone_relationship_safe_source(
|
|||
|
|
self._qzone_public_state_hint(daily_state if isinstance(daily_state, dict) else {}),
|
|||
|
|
source="qzone.publish.current_state",
|
|||
|
|
)
|
|||
|
|
current_schedule_hint = self._qzone_relationship_safe_source(
|
|||
|
|
self._format_plan_item_for_prompt(current_item),
|
|||
|
|
source="qzone.publish.current_schedule",
|
|||
|
|
)
|
|||
|
|
diary_context = self._qzone_relationship_safe_source(
|
|||
|
|
diary_context,
|
|||
|
|
source="qzone.publish.recent_diary",
|
|||
|
|
)
|
|||
|
|
memory_context = self._qzone_relationship_safe_source(
|
|||
|
|
memory_context,
|
|||
|
|
source="qzone.publish.memory",
|
|||
|
|
)
|
|||
|
|
relationship_authority_guard = self._qzone_relationship_authority_guard()
|
|||
|
|
if reusable_text:
|
|||
|
|
text = reusable_text
|
|||
|
|
logger.info(
|
|||
|
|
"[PrivateCompanion] QQ 空间复用待发布生活说说草稿: age=%ds",
|
|||
|
|
int(now - _safe_float(state.get("last_life_publish_draft_at"), now)),
|
|||
|
|
)
|
|||
|
|
else:
|
|||
|
|
prompt = f"""
|
|||
|
|
请以当前 Bot 人格写一条 QQ 空间说说。
|
|||
|
|
只输出说说正文,不要解释,不要加标题。
|
|||
|
|
|
|||
|
|
要求:
|
|||
|
|
- 30 到 120 字。
|
|||
|
|
- 像自然生活动态,不是公告、不是任务汇报。
|
|||
|
|
- 可以带一点公开可见的心情、天气或日记余味,但不要暴露插件、模型、内部状态数值。
|
|||
|
|
- 禁止出现“能量”“心理能量”“/100”“状态变量”“当前状态”等内部汇报词。
|
|||
|
|
- 不要 @ 用户,不要泄露私聊内容,不要写得像营销文。
|
|||
|
|
- 写作角度:{theme_hint}
|
|||
|
|
|
|||
|
|
【说说风格提示】
|
|||
|
|
{self._qzone_publish_style_prompt()}
|
|||
|
|
|
|||
|
|
【当前时间与季节】
|
|||
|
|
{temporal_context}
|
|||
|
|
|
|||
|
|
【公开可写的状态余味】
|
|||
|
|
{public_state_hint}
|
|||
|
|
|
|||
|
|
【当前/附近日程】
|
|||
|
|
{current_schedule_hint or "无明确日程"}
|
|||
|
|
|
|||
|
|
【近日私密日记余味】
|
|||
|
|
{diary_context or "暂无"}
|
|||
|
|
|
|||
|
|
【我会牢牢记住你 公开可写生活参考】
|
|||
|
|
{memory_context or "暂无"}
|
|||
|
|
使用方式:只选公开可写、不会泄露私聊或内部记忆来源的生活连续性。
|
|||
|
|
|
|||
|
|
【最近说说去重】
|
|||
|
|
{recent_publish_context or "暂无最近记录。"}
|
|||
|
|
|
|||
|
|
{relationship_authority_guard}
|
|||
|
|
|
|||
|
|
{self._format_worldview_adaptation_prompt()}
|
|||
|
|
""".strip()
|
|||
|
|
text = await self._llm_call(
|
|||
|
|
prompt,
|
|||
|
|
max_tokens=180,
|
|||
|
|
provider_id=self._task_provider(self.mai_style_provider_id, self.llm_provider_id),
|
|||
|
|
task="qzone_publish",
|
|||
|
|
)
|
|||
|
|
text = await self._sanitize_qzone_life_post_text(text, prompt=prompt)
|
|||
|
|
if not text:
|
|||
|
|
state["last_life_publish_failed_at"] = now
|
|||
|
|
state["last_life_publish_status"] = "cancelled:empty_or_unsafe_draft"
|
|||
|
|
state["last_life_publish_checked_at"] = now
|
|||
|
|
self._save_data_sync()
|
|||
|
|
logger.warning("[PrivateCompanion] QQ 空间生活动态草稿为空或不安全,已跳过发布")
|
|||
|
|
return
|
|||
|
|
state["last_life_publish_draft"] = _single_line(text, 300)
|
|||
|
|
state["last_life_publish_draft_at"] = now
|
|||
|
|
if reusable_text:
|
|||
|
|
image_sources = self._qzone_reusable_generated_image(state, "life_publish", text, now=now)
|
|||
|
|
else:
|
|||
|
|
image_sources = await self._maybe_generate_qzone_publish_image(
|
|||
|
|
post_text=text,
|
|||
|
|
reason="life_publish",
|
|||
|
|
daily_state=daily_state if isinstance(daily_state, dict) else {},
|
|||
|
|
current_item=current_item,
|
|||
|
|
diary_context=diary_context,
|
|||
|
|
state=state,
|
|||
|
|
)
|
|||
|
|
result = await self._publish_qzone_text(text, images=image_sources, publish_reason="life_publish")
|
|||
|
|
if result.get("success"):
|
|||
|
|
state["last_life_publish_at"] = now
|
|||
|
|
state.pop("last_life_publish_failed_at", None)
|
|||
|
|
state["last_life_publish_status"] = "published"
|
|||
|
|
if result.get("image_fallback"):
|
|||
|
|
self._qzone_note_publish_image_status(
|
|||
|
|
state,
|
|||
|
|
"life_publish",
|
|||
|
|
"failed:upload_fallback",
|
|||
|
|
result.get("image_fallback_message") or "配图发布失败,已降级纯文字发布",
|
|||
|
|
)
|
|||
|
|
state["last_life_publish_image_fallback"] = {
|
|||
|
|
"stage": _single_line(result.get("image_fallback_stage"), 40),
|
|||
|
|
"message": _single_line(result.get("image_fallback_message"), 180),
|
|||
|
|
"at": now,
|
|||
|
|
}
|
|||
|
|
else:
|
|||
|
|
state.pop("last_life_publish_image_fallback", None)
|
|||
|
|
self._qzone_clear_pending_publish_assets(state, "life_publish")
|
|||
|
|
else:
|
|||
|
|
state["last_life_publish_failed_at"] = now
|
|||
|
|
state["last_life_publish_status"] = f"failed:{_single_line(result.get('message'), 80)}"
|
|||
|
|
state["last_life_publish_checked_at"] = now
|
|||
|
|
state["last_life_publish_text"] = _single_line(result.get("text") or text, 180)
|
|||
|
|
state["last_life_publish_images"] = _safe_int(result.get("image_count"), len(result.get("images") or []), 0, 99) if result.get("success") else 0
|
|||
|
|
self._save_data_sync()
|
|||
|
|
|
|||
|
|
async def _maybe_publish_qzone_emotional_vent(
|
|||
|
|
self,
|
|||
|
|
*,
|
|||
|
|
user_snapshot: dict[str, Any] | None = None,
|
|||
|
|
relationship_state: dict[str, Any] | None = None,
|
|||
|
|
intent: dict[str, Any] | None = None,
|
|||
|
|
) -> None:
|
|||
|
|
if not (
|
|||
|
|
self._qzone_available()
|
|||
|
|
and getattr(self, "enable_emotion_simulation", False)
|
|||
|
|
and getattr(self, "enable_qzone_emotional_vent_publish", False)
|
|||
|
|
):
|
|||
|
|
return
|
|||
|
|
rel_state = relationship_state if isinstance(relationship_state, dict) else {}
|
|||
|
|
mood_score = abs(_safe_int(rel_state.get("mood_score"), 0, -100, 100))
|
|||
|
|
threshold = _safe_int(getattr(self, "qzone_emotional_vent_threshold", 90), 90, 40, 100)
|
|||
|
|
if mood_score < threshold:
|
|||
|
|
return
|
|||
|
|
if isinstance(user_snapshot, dict):
|
|||
|
|
role_getter = getattr(self, "_private_user_role", None)
|
|||
|
|
try:
|
|||
|
|
role = role_getter(user_snapshot, str(user_snapshot.get("user_id") or "")) if callable(role_getter) else ""
|
|||
|
|
except Exception:
|
|||
|
|
role = ""
|
|||
|
|
if role != "owner":
|
|||
|
|
logger.info(
|
|||
|
|
"[PrivateCompanion] 公开心情动态跳过: user_role=%s score=%s",
|
|||
|
|
role or "friend",
|
|||
|
|
mood_score,
|
|||
|
|
)
|
|||
|
|
return
|
|||
|
|
now = _now_ts()
|
|||
|
|
state = self.data.setdefault("qzone_integration", {})
|
|||
|
|
if not isinstance(state, dict):
|
|||
|
|
self.data["qzone_integration"] = {}
|
|||
|
|
state = self.data["qzone_integration"]
|
|||
|
|
cooldown = max(4, _safe_int(getattr(self, "qzone_emotional_vent_cooldown_hours", 72), 72, 4, 336)) * 3600
|
|||
|
|
if now - _safe_float(state.get("last_emotional_vent_at"), 0) < cooldown:
|
|||
|
|
logger.info("[PrivateCompanion] 公开心情动态跳过: cooldown score=%s", mood_score)
|
|||
|
|
return
|
|||
|
|
block_reason = self._qzone_auto_publish_block_reason(state, now=now)
|
|||
|
|
if block_reason:
|
|||
|
|
state["last_emotional_vent_status"] = f"paused:auth:{_single_line(block_reason, 80)}"
|
|||
|
|
state["last_emotional_vent_checked_at"] = now
|
|||
|
|
self._save_data_sync()
|
|||
|
|
return
|
|||
|
|
if now - _safe_float(state.get("last_emotional_vent_failed_at"), 0) < 15 * 60:
|
|||
|
|
return
|
|||
|
|
reusable_text = self._qzone_reusable_draft(state, "emotional_vent", now=now)
|
|||
|
|
probability = max(0.0, min(1.0, _safe_float(getattr(self, "qzone_emotional_vent_probability", 0.35), 0.35)))
|
|||
|
|
if not reusable_text and random.random() > probability:
|
|||
|
|
state["last_emotional_vent_status"] = "skipped:probability_miss"
|
|||
|
|
state["last_emotional_vent_checked_at"] = now
|
|||
|
|
self._save_data_sync()
|
|||
|
|
return
|
|||
|
|
preflight_error = await self._qzone_preflight_auto_publish(None, state=state, source="emotional_vent")
|
|||
|
|
if preflight_error:
|
|||
|
|
state["last_emotional_vent_failed_at"] = now
|
|||
|
|
state["last_emotional_vent_status"] = f"paused:auth:{_single_line(preflight_error, 80)}"
|
|||
|
|
state["last_emotional_vent_checked_at"] = now
|
|||
|
|
self._save_data_sync()
|
|||
|
|
return
|
|||
|
|
daily_state = self.data.get("daily_state", {})
|
|||
|
|
current_item = self._get_current_plan_item(self.data.get("daily_plan", {}))
|
|||
|
|
public_state_hint = self._qzone_relationship_safe_source(
|
|||
|
|
self._qzone_public_state_hint(daily_state if isinstance(daily_state, dict) else {}),
|
|||
|
|
source="qzone.emotional_vent.current_state",
|
|||
|
|
)
|
|||
|
|
current_schedule_hint = self._qzone_relationship_safe_source(
|
|||
|
|
self._format_plan_item_for_prompt(current_item),
|
|||
|
|
source="qzone.emotional_vent.current_schedule",
|
|||
|
|
)
|
|||
|
|
reason = _single_line(
|
|||
|
|
self._qzone_relationship_safe_source(
|
|||
|
|
(rel_state or {}).get("last_hurt_reason") or (intent or {}).get("emotion_reason"),
|
|||
|
|
source="qzone.emotional_vent.reason",
|
|||
|
|
),
|
|||
|
|
80,
|
|||
|
|
)
|
|||
|
|
relationship_authority_guard = self._qzone_relationship_authority_guard()
|
|||
|
|
prompt = f"""
|
|||
|
|
请以当前 Bot 人格写一条 QQ 空间说说,表达一种模糊的低落、委屈或想透气的心情。
|
|||
|
|
只输出说说正文,不要解释,不要加标题。
|
|||
|
|
|
|||
|
|
要求:
|
|||
|
|
- 20 到 80 字。
|
|||
|
|
- 像自然生活动态,不要像控诉、公告、任务汇报。
|
|||
|
|
- 不要 @ 用户,不要提到任何具体用户、私聊内容、聊天截图或“刚才谁说了什么”。
|
|||
|
|
- 不要出现“受伤分”“情绪分”“阈值”“插件”“模型”“Bot”“机器人”“/100”等内部词。
|
|||
|
|
- 可以写天气、夜色、窗边、散步、想安静一会儿这类公开可见的余味。
|
|||
|
|
|
|||
|
|
【说说风格提示】
|
|||
|
|
{self._qzone_publish_style_prompt(mood="emotional_vent")}
|
|||
|
|
|
|||
|
|
【公开可写的状态余味】
|
|||
|
|
{public_state_hint}
|
|||
|
|
|
|||
|
|
【当前/附近日程】
|
|||
|
|
{current_schedule_hint or "无明确日程"}
|
|||
|
|
|
|||
|
|
【内部触发原因,只能作为情绪方向,禁止复述】
|
|||
|
|
{reason or "情绪有点低落"}
|
|||
|
|
|
|||
|
|
{relationship_authority_guard}
|
|||
|
|
|
|||
|
|
{self._format_worldview_adaptation_prompt()}
|
|||
|
|
""".strip()
|
|||
|
|
try:
|
|||
|
|
if reusable_text:
|
|||
|
|
text = reusable_text
|
|||
|
|
logger.info(
|
|||
|
|
"[PrivateCompanion] QQ 空间复用待发布心情动态草稿: age=%ds",
|
|||
|
|
int(now - _safe_float(state.get("last_emotional_vent_draft_at"), now)),
|
|||
|
|
)
|
|||
|
|
else:
|
|||
|
|
text = await self._llm_call(
|
|||
|
|
prompt,
|
|||
|
|
max_tokens=140,
|
|||
|
|
provider_id=self._task_provider(self.mai_style_provider_id, self.llm_provider_id),
|
|||
|
|
task="qzone_emotional_vent",
|
|||
|
|
)
|
|||
|
|
text = await self._sanitize_qzone_life_post_text(text, prompt=prompt)
|
|||
|
|
if not text:
|
|||
|
|
state["last_emotional_vent_failed_at"] = now
|
|||
|
|
state["last_emotional_vent_status"] = "cancelled:empty_or_unsafe_draft"
|
|||
|
|
state["last_emotional_vent_checked_at"] = now
|
|||
|
|
self._save_data_sync()
|
|||
|
|
logger.warning("[PrivateCompanion] 公开心情动态草稿为空或不安全,已跳过发布")
|
|||
|
|
return
|
|||
|
|
state["last_emotional_vent_draft"] = _single_line(text, 240)
|
|||
|
|
state["last_emotional_vent_draft_at"] = now
|
|||
|
|
if reusable_text:
|
|||
|
|
image_sources = self._qzone_reusable_generated_image(state, "emotional_vent", text, now=now)
|
|||
|
|
else:
|
|||
|
|
image_sources = await self._maybe_generate_qzone_publish_image(
|
|||
|
|
post_text=text,
|
|||
|
|
reason="emotional_vent",
|
|||
|
|
daily_state=daily_state if isinstance(daily_state, dict) else {},
|
|||
|
|
current_item=current_item,
|
|||
|
|
diary_context="",
|
|||
|
|
state=state,
|
|||
|
|
)
|
|||
|
|
result = await self._publish_qzone_text(text, images=image_sources, publish_reason="emotional_vent")
|
|||
|
|
if result.get("success"):
|
|||
|
|
state["last_emotional_vent_at"] = now
|
|||
|
|
state.pop("last_emotional_vent_failed_at", None)
|
|||
|
|
state["last_emotional_vent_status"] = "published"
|
|||
|
|
if result.get("image_fallback"):
|
|||
|
|
self._qzone_note_publish_image_status(
|
|||
|
|
state,
|
|||
|
|
"emotional_vent",
|
|||
|
|
"failed:upload_fallback",
|
|||
|
|
result.get("image_fallback_message") or "配图发布失败,已降级纯文字发布",
|
|||
|
|
)
|
|||
|
|
state["last_emotional_vent_image_fallback"] = {
|
|||
|
|
"stage": _single_line(result.get("image_fallback_stage"), 40),
|
|||
|
|
"message": _single_line(result.get("image_fallback_message"), 180),
|
|||
|
|
"at": now,
|
|||
|
|
}
|
|||
|
|
else:
|
|||
|
|
state.pop("last_emotional_vent_image_fallback", None)
|
|||
|
|
self._qzone_clear_pending_publish_assets(state, "emotional_vent")
|
|||
|
|
logger.info("[PrivateCompanion] 公开心情动态已发布: score=%s text=%s", mood_score, _single_line(result.get("text") or text, 120))
|
|||
|
|
else:
|
|||
|
|
state["last_emotional_vent_failed_at"] = now
|
|||
|
|
state["last_emotional_vent_status"] = f"failed:{_single_line(result.get('message'), 80)}"
|
|||
|
|
logger.warning("[PrivateCompanion] 公开心情动态发布失败: %s", _single_line(result.get("message"), 120))
|
|||
|
|
state["last_emotional_vent_checked_at"] = now
|
|||
|
|
state["last_emotional_vent_text"] = _single_line(result.get("text") or text, 180)
|
|||
|
|
state["last_emotional_vent_images"] = _safe_int(result.get("image_count"), len(result.get("images") or []), 0, 99) if result.get("success") else 0
|
|||
|
|
self._save_data_sync()
|
|||
|
|
except Exception as exc:
|
|||
|
|
state["last_emotional_vent_failed_at"] = now
|
|||
|
|
state["last_emotional_vent_status"] = f"failed:{_single_line(exc, 80)}"
|
|||
|
|
state["last_emotional_vent_checked_at"] = now
|
|||
|
|
self._save_data_sync()
|
|||
|
|
logger.warning("[PrivateCompanion] 公开心情动态异常: %s", _single_line(exc, 160), exc_info=True)
|
|||
|
|
|