astrbot_plugin_private_comp.../activity_capture.py
agent-bot 85d5069bbd feat: complete M6.1.3-R2 through M6.1.7 bot personal archive contract
M6.1.3-R2 非五类兼容(阻断修复):DTO date 缺失时从 occurred_at 派生,
刻意不做时区换算以与 _infer_window 同一时钟;calendar_event 取 start_at、
creative_progress 取 project.updated_at;未知 kind 不再默认 bot_observed_activity;
capture_hard_fact L4→L3;TYPE_CONTRACTS 覆盖全部 12 类,修复非五类 source_kind
兜底成 observed 污染本地记录;proactive_message 的 image_path 改为 has_image+
image_name(原样透传会被自身隐私校验整条静默丢弃);snapshot/daily_plan 透传 version。

M6.1.4 创作/媒体/主观分域:creative_progress 补 chunk 形参(生产调用点一直 TypeError
导致创作推进整步被阻断)并只留里程碑摘要;photo_generation 改为媒体索引
(原 payload 带 prompt 键与绝对路径,100% 静默丢弃);幂等键纳入隐私校验;
禁用键清单收窄,放行媒体标识键;两条 Bridge 路径统一送 body payload。

M6.1.5 Bot 专用检索 Profiles:按 Profile 分槽注入,固定槽位与条数、跨槽去重、
声明当前以本地运行态为准;旧版记忆插件回退通用检索。

M6.1.6 分层只读 API:新增 build_agenda_runtime_view 与 GET /agenda/runtime,
记忆插件停用时仍完整返回并标降级。

M6.1.7 隐私与残余收口:DTO 新增 contexts 禁用键;已删除娱乐功能(qzone/news/
web_exploration)的残余配置入口从展示面隐藏,旧值保留不删。

测试:174 项通过(pytest 与 unittest 双口径)。联合 E2E 135 项全部通过。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 01:37:40 +08:00

111 行
4.4 KiB
Python

# -*- coding: utf-8 -*-
"""Low-noise local activity capture for the operations companion."""
from __future__ import annotations
from datetime import datetime, timedelta
from typing import Any
try:
from .agenda_contracts import normalize_observed_activity, stable_id
except ImportError:
from agenda_contracts import normalize_observed_activity, stable_id
COLLABORATION_HINTS = {
"代码", "插件", "开发", "排障", "测试", "部署", "运维", "NAS", "VM", "项目", "创作", "一起", "继续"
}
def classify_interaction(text: str, *, message_count: int = 1) -> str:
content = str(text or "").strip()
if not content:
return "ordinary"
if any(token in content for token in COLLABORATION_HINTS) and len(content) >= 8:
return "sustained" if message_count >= 2 else "candidate"
if message_count >= 4 and len(content) >= 12:
return "sustained"
if len(content) <= 12:
return "ordinary"
return "short"
class ActivityCapture:
def __init__(self, *, window_minutes: int = 30, min_sustained_messages: int = 3):
self.window_minutes = max(5, int(window_minutes))
self.min_sustained_messages = max(2, int(min_sustained_messages))
def capture_message(
self,
*,
text: str,
event_time: datetime,
source_ref: str,
conversation_id: str,
participant: str = "user",
message_count: int = 1,
topic: str = "",
) -> dict[str, Any] | None:
interaction = classify_interaction(text, message_count=message_count)
if interaction not in {"sustained", "candidate"}:
return None
if interaction == "candidate" and message_count < self.min_sustained_messages and not any(
token in str(text or "") for token in COLLABORATION_HINTS
):
return None
bucket_minutes = max(5, self.window_minutes)
bucket_start = event_time.replace(second=0, microsecond=0) - timedelta(minutes=event_time.minute % bucket_minutes)
start = min(bucket_start, event_time - timedelta(minutes=min(self.window_minutes, max(1, message_count - 1) * 3)))
title = str(topic or text or "持续互动").strip().replace("\n", " ")[:120]
return normalize_observed_activity(
{
"activity_id": stable_id("activity", conversation_id, topic or "conversation", bucket_start.date().isoformat(), bucket_start.hour, bucket_start.minute),
"title": title,
"kind": "user_collaboration" if interaction == "sustained" else "conversation_candidate",
"start_at": start.isoformat(),
"end_at": event_time.isoformat(),
"source_kind": "real",
"source": "conversation",
"source_refs": [source_ref],
"participants": [participant, "bot"],
"evidence_level": "L2" if interaction == "sustained" else "L1",
"certainty": "high" if interaction == "sustained" else "low",
"status": "active" if interaction == "sustained" else "unknown",
},
now=event_time,
)
@staticmethod
def split_for_storage(activity: dict[str, Any]) -> dict[str, Any]:
try:
from .bot_self_store import split_shared_activity
except ImportError:
from bot_self_store import split_shared_activity
return split_shared_activity(activity)
def capture_hard_fact(
self,
*,
title: str,
start_at: datetime,
end_at: datetime | None,
source: str,
source_refs: list[str],
participants: list[str] | None = None,
kind: str = "tool_activity",
) -> dict[str, Any]:
return normalize_observed_activity(
{
"activity_id": stable_id("activity", source, title, start_at.isoformat(), *source_refs),
"title": title,
"kind": kind,
"start_at": start_at.isoformat(),
"end_at": end_at.isoformat() if end_at else "",
"source_kind": "real",
"source": source,
"source_refs": source_refs,
"participants": participants or ["bot"],
"evidence_level": "L3",
"certainty": "high",
"status": "completed" if end_at else "active",
},
now=start_at,
)