perf(roundtable): 轻量 thread 接口降低广播与席位状态核对开销
新增 messages/append 与 last-ai-message 接口,圆桌广播和总控派活轮改走服务端轻量读写,避免 loopback 传输整份 state。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
924c8f2fac
commit
1ee2e6ca0a
2 files changed
+191
-50
No files matched your search
@@ -20,7 +20,7 @@ import uuid
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from langgraph.checkpoint.base import empty_checkpoint
|
||||
from langgraph.checkpoint.base import empty_checkpoint, uuid6
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
from sqlalchemy import select
|
||||
|
||||
@@ -1009,6 +1009,138 @@ async def update_thread_state(thread_id: str, body: ThreadStateUpdateRequest, re
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Lightweight message append / last-AI read
|
||||
#
|
||||
# 圆桌广播与「席位交付状态」核对原本都走 GET /state + POST /state,要把动辄几万字的整份
|
||||
# thread 状态经 loopback 传一两遍 —— 实测让总控每轮 payload_built 飙到 ~11s、广播也很慢。
|
||||
# 下面两个轻量接口把重活留在服务端,loopback 只传一条小消息 / 截断后的摘要:
|
||||
# • POST /{id}/messages/append —— 只传 1 条消息,服务端读 checkpoint+追加+写回;
|
||||
# • GET /{id}/last-ai-message —— 只回最后一条 AI 交付(可 max_chars 截断),核对交付状态用。
|
||||
# 注意:Postgres checkpoint 的 aput/aget 本身仍是 O(state) 的(快照模型),这里省掉的是
|
||||
# 「整份 state 的 JSON 序列化 + loopback 往返」,这是可避免的那部分开销。
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class AppendMessageRequest(BaseModel):
|
||||
"""Request body for the lightweight single-message append."""
|
||||
|
||||
content: str = Field(..., description="Message text to append")
|
||||
type: str = Field(default="human", description="Message role: human/ai/system")
|
||||
|
||||
|
||||
def _flatten_msg_content(content: Any) -> str:
|
||||
"""把 message.content(str / 结构化 block 列表)拍平成纯文本。"""
|
||||
if isinstance(content, str):
|
||||
return content
|
||||
if isinstance(content, list):
|
||||
parts: list[str] = []
|
||||
for item in content:
|
||||
if isinstance(item, str):
|
||||
parts.append(item)
|
||||
elif isinstance(item, dict):
|
||||
parts.append(str(item.get("text", "")))
|
||||
return "".join(parts)
|
||||
return str(content) if content else ""
|
||||
|
||||
|
||||
def _extract_last_ai_text(messages: list[Any]) -> str:
|
||||
"""返回最后一条 AI 消息的纯文本;没有则空串。兼容 message 对象与序列化 dict 两种存储形态。"""
|
||||
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 ""
|
||||
|
||||
|
||||
async def _append_message_to_checkpoint(checkpointer: Any, thread_id: str, content: str, type_: str) -> int | None:
|
||||
"""把一条消息追加进 thread 最新 checkpoint 的 ``messages`` 通道,返回新消息总数。
|
||||
|
||||
``None`` 表示线程不存在(无 checkpoint)。纯粹围绕 checkpointer 的 aget/aput,便于单测。
|
||||
语义与 ``update_thread_state`` 一致:仅替换 ``messages`` 通道为追加后的列表,其它通道原样保留。
|
||||
"""
|
||||
config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}}
|
||||
ckpt_tuple = await checkpointer.aget_tuple(config)
|
||||
if ckpt_tuple is None:
|
||||
return None
|
||||
checkpoint: dict[str, Any] = dict(getattr(ckpt_tuple, "checkpoint", {}) or {})
|
||||
metadata: dict[str, Any] = dict(getattr(ckpt_tuple, "metadata", {}) or {})
|
||||
channel_values: dict[str, Any] = dict(checkpoint.get("channel_values", {}))
|
||||
messages = list(channel_values.get("messages") or [])
|
||||
messages.append({"type": type_, "content": content})
|
||||
channel_values["messages"] = messages
|
||||
checkpoint["channel_values"] = channel_values
|
||||
# 生成**新的** checkpoint 快照 id + ts —— 每次追加都落成一个新快照(uuid6 时间可排序,新 id
|
||||
# 天然成为最新),而不是复用旧 id 原地覆盖(后端语义不一致,会让连续追加互相丢失)。
|
||||
checkpoint["id"] = str(uuid6(clock_seq=-2))
|
||||
checkpoint["ts"] = now_iso()
|
||||
# **关键**:checkpointer 用「版本化 blob」存 channel —— 必须 bump ``messages`` 的版本并把它
|
||||
# 放进 aput 的 new_versions,aput 才会真正写入该 channel 的新内容、aget 才能读回。否则(像原
|
||||
# GET/POST /state 那样只改 channel_values、new_versions 传 {})对没有现成 messages 版本的线程
|
||||
# 不生效。
|
||||
versions = dict(checkpoint.get("channel_versions", {}))
|
||||
new_ver = checkpointer.get_next_version(versions.get("messages"), None)
|
||||
versions["messages"] = new_ver
|
||||
checkpoint["channel_versions"] = versions
|
||||
metadata["updated_at"] = now_iso()
|
||||
await checkpointer.aput(config, checkpoint, metadata, {"messages": new_ver})
|
||||
return len(messages)
|
||||
|
||||
|
||||
@router.post("/{thread_id}/messages/append", response_model=None)
|
||||
@require_permission("threads", "write", owner_check=True, require_existing=True)
|
||||
async def append_thread_message(thread_id: str, body: AppendMessageRequest, request: Request) -> dict[str, Any]:
|
||||
"""轻量追加一条消息到 thread checkpoint —— 调用方只传一条消息,不必 GET+POST 整份 state。
|
||||
|
||||
语义与 ``update_thread_state`` 的 messages 写入一致:直接追加到 ``messages`` 通道,下次图
|
||||
运行时 ``add_messages`` 做规范化/按 id 去重。失败抛 5xx 由调用方按 best-effort 处理。
|
||||
"""
|
||||
checkpointer = get_checkpointer(request)
|
||||
try:
|
||||
count = await _append_message_to_checkpoint(checkpointer, thread_id, body.content, body.type)
|
||||
except Exception:
|
||||
logger.exception("Failed to append message on thread %s", sanitize_log_param(thread_id))
|
||||
raise HTTPException(status_code=500, detail="Failed to append thread message")
|
||||
if count is None:
|
||||
raise HTTPException(status_code=404, detail=f"Thread {thread_id} not found")
|
||||
return {"ok": True, "message_count": count}
|
||||
|
||||
|
||||
@router.get("/{thread_id}/last-ai-message", response_model=None)
|
||||
@require_permission("threads", "read", owner_check=True)
|
||||
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=False``(而非 404),方便调用方把"还没建好/没交付"统一当未交付。
|
||||
"""
|
||||
checkpointer = get_checkpointer(request)
|
||||
config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}}
|
||||
try:
|
||||
ckpt_tuple = await checkpointer.aget_tuple(config)
|
||||
except Exception:
|
||||
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}
|
||||
|
||||
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 [])
|
||||
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}
|
||||
|
||||
|
||||
@router.delete("/{thread_id}/messages/{message_id}")
|
||||
@require_permission("threads", "write", owner_check=True, require_existing=True)
|
||||
async def delete_thread_message(thread_id: str, message_id: str, request: Request) -> dict[str, Any]:
|
||||
|
||||
Reference in new issue
Block a user