代码提交

This commit is contained in:
董宏凯 committed 2026-08-24 20:47:31 +08:00
1 parent 007ed5a7de
commit babc5e95c6
21 files changed
+1170 -145

No files matched your search

@@ -33,6 +33,10 @@ from deerflow.config.extensions_config import SkillDisplayConfig
from deerflow.config.paths import Paths, get_paths
from deerflow.persistence.engine import get_engine, get_session_factory
from deerflow.persistence.skills.model import SkillDisplayAdapterRow
from deerflow.agents.roundtable_orchestrator.delivery import (
classify_seat_delivery,
visible_seat_text,
)
from deerflow.runtime import serialize_channel_values
from deerflow.runtime.references import (
adapt_reference_batches_with_displays,
@@ -1146,20 +1150,48 @@ def _set_message_content(message: Any, content: str) -> None:
raise TypeError("Message content cannot be edited") from exc
def _extract_last_ai_text(messages: list[Any]) -> str:
"""返回最后一条 AI 消息的纯文本;没有则空串。兼容 message 对象与序列化 dict 两种存储形态。"""
def _msg_type_and_content(msg: Any) -> tuple[str, Any]:
if isinstance(msg, dict):
return str(msg.get("type") or "").lower(), msg.get("content", "")
return str(getattr(msg, "type", "") or "").lower(), getattr(msg, "content", "")
def _is_broadcast_human(text: str) -> bool:
"""圆桌后广播进席位 thread 的 human 消息,不算新一轮任务边界。"""
t = (text or "").strip()
return t.startswith("总控智能体收到") or t.startswith("子智能体")
def _extract_seat_delivery(messages: list[Any]) -> tuple[str, str]:
"""核对席位本轮是否产出**可见正文**。
返回 ``(visible_text, invalid_reason)``。``invalid_reason`` 为空表示已交付;
``thinking_only`` = 最新 AI 只有思考;``empty`` = 本轮尚无可见正文。
从后往前扫:跳过后广播 human、跳过空壳 AI(纯 tool_call);一旦碰到思考-only 的
最新 AI 就停(不继承更早一轮的旧交付);碰到真实派活 human 也停。
"""
for msg in reversed(messages or []):
if isinstance(msg, dict):
mtype = str(msg.get("type") or "").lower()
content = msg.get("content", "")
else:
mtype = str(getattr(msg, "type", "") or "").lower()
content = getattr(msg, "content", "")
if mtype in ("ai", "aimessage", "aimessagechunk"):
text = _flatten_msg_content(content)
if text.strip():
return text
return ""
mtype, content = _msg_type_and_content(msg)
if mtype in ("human", "user"):
if _is_broadcast_human(_flatten_msg_content(content)):
continue
return "", "empty"
if mtype not in ("ai", "aimessage", "aimessagechunk"):
continue
raw = _flatten_msg_content(content)
kind = classify_seat_delivery(raw)
if kind == "valid":
return visible_seat_text(raw), ""
if kind == "thinking_only":
return "", "thinking_only"
return "", "empty"
def _extract_last_ai_text(messages: list[Any]) -> str:
"""返回席位最后一条**可见**交付正文;思考-only / 空串都不算。"""
visible, _reason = _extract_seat_delivery(messages)
return visible
async def _append_message_to_checkpoint(checkpointer: Any, thread_id: str, content: str, type_: str) -> int | None:
@@ -1220,8 +1252,10 @@ async def append_thread_message(thread_id: str, body: AppendMessageRequest, requ
async def get_thread_last_ai_message(thread_id: str, request: Request, max_chars: int = 0) -> dict[str, Any]:
"""轻量读取 thread 最后一条 AI 消息(可截断),供圆桌核对席位交付状态。
返回 ``{delivered, content, length}``:``delivered`` = 是否存在非空 AI 交付;``max_chars>0``
时 ``content`` 截断到该长度(派活轮取摘要),``<=0`` 取全文(综合轮)。``length`` 是全文长度。
返回 ``{delivered, content, length, invalid_reason}``:``delivered`` = 本轮是否存在
**可见正文**(剥掉 ``<think>`` 之后非空;不要求 md 文件);``max_chars>0`` 时 ``content``
截断到该长度(派活轮取摘要),``<=0`` 取全文(综合轮)。``length`` 是可见正文全文长度。
``invalid_reason`` 在未交付时为 ``thinking_only`` / ``empty``,已交付时为空串。
线程不存在时返回 ``delivered=False``(而非 404),方便调用方把"还没建好/没交付"统一当未交付。
"""
checkpointer = get_checkpointer(request)
@@ -1232,15 +1266,20 @@ async def get_thread_last_ai_message(thread_id: str, request: Request, max_chars
logger.exception("Failed to read last-ai-message on thread %s", sanitize_log_param(thread_id))
raise HTTPException(status_code=500, detail="Failed to get thread state")
if ckpt_tuple is None:
return {"delivered": False, "content": "", "length": 0}
return {"delivered": False, "content": "", "length": 0, "invalid_reason": "empty"}
channel_values = (getattr(ckpt_tuple, "checkpoint", {}) or {}).get("channel_values", {}) or {}
messages = channel_values.get("messages") or []
text = _extract_last_ai_text(messages if isinstance(messages, list) else [])
text, invalid_reason = _extract_seat_delivery(messages if isinstance(messages, list) else [])
full_len = len(text)
if max_chars and max_chars > 0:
text = text[:max_chars]
return {"delivered": full_len > 0, "content": text, "length": full_len}
return {
"delivered": full_len > 0,
"content": text,
"length": full_len,
"invalid_reason": invalid_reason,
}
@router.get("/{thread_id}/messages", response_model=ThreadMessagesResponse)