feat: 报告结构支持检索方向/来源与智能新增,并修复协助卡停轮与宿主跳转
为深度研究报告结构增加检索方向与技能来源、对话式智能新增页与详情排版;澄清中间件在 after_model 可靠结束本轮,避免弱模型出卡后继续生成;任务按钮补充 host 跳转与相关深链能力。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
a5a9c952e8
commit
51afebb394
67 files changed
+4511
-699
No files matched your search
@@ -20,7 +20,7 @@ import uuid
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from fastapi import APIRouter, HTTPException, Query, Request
|
||||
from langgraph.checkpoint.base import empty_checkpoint, uuid6
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
from sqlalchemy import select
|
||||
@@ -193,6 +193,18 @@ class ThreadStateResponse(BaseModel):
|
||||
tasks: list[dict[str, Any]] = Field(default_factory=list, description="Interrupted task details")
|
||||
|
||||
|
||||
class ThreadMessagesResponse(BaseModel):
|
||||
"""A page of the latest thread message state.
|
||||
|
||||
``messages`` deliberately has no narrower response model: LangChain message
|
||||
fields such as ``additional_kwargs`` and provider-specific tool-call payloads
|
||||
must reach integration clients unchanged.
|
||||
"""
|
||||
|
||||
total: int = Field(description="Total number of messages in the thread")
|
||||
messages: list[Any] = Field(default_factory=list, description="Messages in chronological order")
|
||||
|
||||
|
||||
class ThreadPatchRequest(BaseModel):
|
||||
"""Request body for patching thread metadata."""
|
||||
|
||||
@@ -321,6 +333,20 @@ def _message_id(message: Any) -> str | None:
|
||||
return str(value) if value else None
|
||||
|
||||
|
||||
def _serialize_thread_messages(messages: Any) -> list[Any]:
|
||||
"""Return checkpoint messages in the same wire form as stream/state APIs.
|
||||
|
||||
The latest checkpoint is the canonical conversation transcript. Reusing the
|
||||
runtime serializer keeps every standard and provider-specific message field
|
||||
(including ``additional_kwargs``, ``tool_calls`` and hidden context flags)
|
||||
while converting LangChain message objects into JSON-safe values.
|
||||
"""
|
||||
if not isinstance(messages, list):
|
||||
return []
|
||||
serialized = serialize_channel_values({"messages": messages}).get("messages", [])
|
||||
return serialized if isinstance(serialized, list) else []
|
||||
|
||||
|
||||
def _message_content(message: Any) -> Any:
|
||||
if isinstance(message, dict):
|
||||
return message.get("content")
|
||||
@@ -587,6 +613,18 @@ async def delete_thread_data(thread_id: str, request: Request) -> ThreadDeleteRe
|
||||
except Exception:
|
||||
logger.debug("Could not delete thread_meta for %s (not critical)", sanitize_log_param(thread_id))
|
||||
|
||||
# Conversation deposits are an auxiliary artifact index. Remove their
|
||||
# records/documents as well so a whole-thread delete cannot leave a stale
|
||||
# searchable copy behind. The helper is intentionally best-effort (the
|
||||
# primary thread state is already deleted above).
|
||||
asyncio.create_task(
|
||||
delete_conversation_deposit_documents(
|
||||
request.app,
|
||||
thread_id=thread_id,
|
||||
message_ids=None,
|
||||
)
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
@@ -1204,6 +1242,38 @@ async def get_thread_last_ai_message(thread_id: str, request: Request, max_chars
|
||||
return {"delivered": full_len > 0, "content": text, "length": full_len}
|
||||
|
||||
|
||||
@router.get("/{thread_id}/messages", response_model=ThreadMessagesResponse)
|
||||
@require_permission("threads", "read", owner_check=True)
|
||||
async def get_thread_messages(
|
||||
thread_id: str,
|
||||
request: Request,
|
||||
limit: int = Query(default=200, ge=1, le=1000, description="Maximum messages to return"),
|
||||
offset: int = Query(default=0, ge=0, description="Number of leading messages to skip"),
|
||||
) -> ThreadMessagesResponse:
|
||||
"""Return a chronological, paginated view of a thread's message transcript.
|
||||
|
||||
This reads the latest checkpoint rather than a derived display transcript, so
|
||||
hidden context messages and all original message fields are retained. The
|
||||
response serialization is shared with ``GET /state`` and streaming events.
|
||||
"""
|
||||
checkpointer = get_checkpointer(request)
|
||||
config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}}
|
||||
try:
|
||||
checkpoint_tuple = await checkpointer.aget_tuple(config)
|
||||
except Exception:
|
||||
logger.exception("Failed to get messages for thread %s", sanitize_log_param(thread_id))
|
||||
raise HTTPException(status_code=500, detail="Failed to get thread messages")
|
||||
|
||||
if checkpoint_tuple is None:
|
||||
raise HTTPException(status_code=404, detail=f"Thread {thread_id} not found")
|
||||
|
||||
checkpoint = getattr(checkpoint_tuple, "checkpoint", {}) or {}
|
||||
channel_values = checkpoint.get("channel_values", {}) or {}
|
||||
messages = _serialize_thread_messages(channel_values.get("messages"))
|
||||
total = len(messages)
|
||||
return ThreadMessagesResponse(total=total, messages=messages[offset : offset + limit])
|
||||
|
||||
|
||||
@router.delete("/{thread_id}/messages")
|
||||
@require_permission("threads", "write", owner_check=True, require_existing=True)
|
||||
async def clear_thread_messages(thread_id: str, request: Request) -> dict[str, Any]:
|
||||
@@ -1332,6 +1402,12 @@ async def delete_thread_message(thread_id: str, message_id: str, request: Reques
|
||||
|
||||
channel_values["messages"] = filtered
|
||||
checkpoint["channel_values"] = channel_values
|
||||
checkpoint["id"] = str(uuid6(clock_seq=-2))
|
||||
checkpoint["ts"] = now_iso()
|
||||
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()
|
||||
|
||||
write_config: dict[str, Any] = {
|
||||
@@ -1341,7 +1417,7 @@ async def delete_thread_message(thread_id: str, message_id: str, request: Reques
|
||||
}
|
||||
}
|
||||
try:
|
||||
await checkpointer.aput(write_config, checkpoint, metadata, {})
|
||||
await checkpointer.aput(write_config, checkpoint, metadata, {"messages": new_ver})
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Failed to delete message %s from thread %s",
|
||||
|
||||
Reference in new issue
Block a user