sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)
This commit is contained in:
1 parent
4f7f07504a
commit
8bc8b0e082
1651 files changed
+278599
-11416
No files matched your search
@@ -20,7 +20,7 @@ import uuid
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query, Request
|
||||
from fastapi import APIRouter, HTTPException, Query, Request, Response
|
||||
from langgraph.checkpoint.base import empty_checkpoint, uuid6
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
from sqlalchemy import select
|
||||
@@ -28,15 +28,16 @@ from sqlalchemy import select
|
||||
from app.gateway.authz import require_permission
|
||||
from app.gateway.deps import get_checkpointer
|
||||
from app.gateway.llmwiki_deposit import delete_conversation_deposit_documents
|
||||
from app.gateway.operation_timing import OperationTiming
|
||||
from app.gateway.utils import sanitize_log_param
|
||||
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.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.runtime import serialize_channel_values
|
||||
from deerflow.runtime.references import (
|
||||
adapt_reference_batches_with_displays,
|
||||
@@ -152,6 +153,12 @@ class ThreadDeleteResponse(BaseModel):
|
||||
message: str
|
||||
|
||||
|
||||
class ThreadClearResponse(BaseModel):
|
||||
"""Response model for clearing the current user's conversations."""
|
||||
|
||||
deleted: int = Field(description="Number of conversations removed")
|
||||
|
||||
|
||||
class ThreadResponse(BaseModel):
|
||||
"""Response model for a single thread."""
|
||||
|
||||
@@ -272,6 +279,7 @@ class ReferenceBatchResponse(BaseModel):
|
||||
|
||||
class ReferenceBatchesResponse(BaseModel):
|
||||
batches: list[ReferenceBatchResponse] = Field(default_factory=list)
|
||||
retrieval_traces: list[dict[str, Any]] = Field(default_factory=list)
|
||||
|
||||
|
||||
class ReferenceDebugLogRequest(BaseModel):
|
||||
@@ -280,6 +288,150 @@ class ReferenceDebugLogRequest(BaseModel):
|
||||
payload: dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
|
||||
def _trace_message_extra(message: Any) -> dict[str, Any]:
|
||||
if isinstance(message, dict):
|
||||
value = message.get("additional_kwargs") or message.get("additionalKwargs")
|
||||
else:
|
||||
value = getattr(message, "additional_kwargs", None)
|
||||
return value if isinstance(value, dict) else {}
|
||||
|
||||
|
||||
def _trace_message_value(message: Any, key: str) -> Any:
|
||||
if isinstance(message, dict):
|
||||
return message.get(key)
|
||||
return getattr(message, key, None)
|
||||
|
||||
|
||||
def _tool_trace_from_message(message: Any) -> dict[str, Any] | None:
|
||||
"""Build a bounded trace for a model-driven ``llmwiki_search`` call."""
|
||||
|
||||
message_type = str(_trace_message_value(message, "type") or "").lower()
|
||||
name = str(_trace_message_value(message, "name") or "")
|
||||
if message_type not in {"tool", "toolmessage"} or name != "llmwiki_search":
|
||||
return None
|
||||
content = _trace_message_value(message, "content")
|
||||
if not isinstance(content, str):
|
||||
return None
|
||||
try:
|
||||
payload = json.loads(content)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
if not isinstance(payload, dict) or not isinstance(payload.get("results"), list):
|
||||
return None
|
||||
results: list[dict[str, Any]] = []
|
||||
raw_results = payload["results"]
|
||||
for rank, item in enumerate(raw_results[:8], start=1):
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
sections = [
|
||||
section
|
||||
for section in item.get("matched_sections") or []
|
||||
if isinstance(section, dict)
|
||||
]
|
||||
matched_section = item.get("matched_section")
|
||||
if isinstance(matched_section, dict):
|
||||
sections.append(matched_section)
|
||||
section = sections[0] if sections else {}
|
||||
content_text = str(
|
||||
section.get("content")
|
||||
or item.get("content")
|
||||
or item.get("content_markdown")
|
||||
or item.get("summary")
|
||||
or ""
|
||||
)
|
||||
retrieval_mode = str(item.get("retrieval_mode") or "unknown")
|
||||
explicit_vector_score = item.get("vector_score")
|
||||
vector_scores = [
|
||||
float(candidate["score"])
|
||||
for candidate in sections
|
||||
if isinstance(candidate.get("score"), (int, float))
|
||||
]
|
||||
results.append(
|
||||
{
|
||||
"rank": rank,
|
||||
"id": item.get("id") or item.get("wiki_page_id") or item.get("slug"),
|
||||
"kind": "assistant" if item.get("type") == "assistant_knowledge" else "llmwiki",
|
||||
"knowledge_base_id": item.get("knowledge_base_id") or item.get("base_id"),
|
||||
"knowledge_base_name": item.get("knowledge_base_name") or item.get("source"),
|
||||
"title": item.get("title"),
|
||||
"wiki_slug": item.get("wiki_slug") or item.get("slug"),
|
||||
"retrieval_mode": retrieval_mode,
|
||||
"rank_score": item.get("score"),
|
||||
"channel_score": item.get("channel_score"),
|
||||
"vector_score": (
|
||||
explicit_vector_score
|
||||
if isinstance(explicit_vector_score, (int, float))
|
||||
else max(vector_scores)
|
||||
if retrieval_mode == "vector" and vector_scores
|
||||
else None
|
||||
),
|
||||
"content": content_text[:1400],
|
||||
"url": item.get("url") or "",
|
||||
}
|
||||
)
|
||||
tool_call_id = str(_trace_message_value(message, "tool_call_id") or uuid.uuid4())
|
||||
ordinary_ids = [str(item) for item in payload.get("selection") or []]
|
||||
assistant_ids = [str(item) for item in payload.get("assistant_selection") or []]
|
||||
diagnostics = payload.get("diagnostics") if isinstance(payload.get("diagnostics"), dict) else {}
|
||||
selection_count = payload.get("selection_count")
|
||||
if not isinstance(selection_count, int) or isinstance(selection_count, bool):
|
||||
selection_count = len(ordinary_ids) + len(assistant_ids)
|
||||
authorized_space_count = payload.get("authorized_space_count")
|
||||
if not isinstance(authorized_space_count, int) or isinstance(authorized_space_count, bool):
|
||||
# Legacy tool traces only exposed ``knowledge_base_count``. Preserve
|
||||
# those historical records while preferring the exact new field.
|
||||
legacy_count = payload.get("knowledge_base_count")
|
||||
authorized_space_count = legacy_count if isinstance(legacy_count, int) and not isinstance(legacy_count, bool) else 0
|
||||
return {
|
||||
"retrieval_trace_id": str(payload.get("retrieval_trace_id") or f"tool:{tool_call_id}"),
|
||||
"trigger": "tool",
|
||||
"query": str(payload.get("query") or ""),
|
||||
"query_variants": [str(payload.get("query") or "")],
|
||||
"selected_spaces": [
|
||||
*({"kind": "llmwiki", "id": item} for item in ordinary_ids),
|
||||
*({"kind": "assistant", "id": item} for item in assistant_ids),
|
||||
],
|
||||
"selection_count": max(0, selection_count),
|
||||
"authorized_space_count": max(0, authorized_space_count),
|
||||
"mode": str(payload.get("mode") or "empty"),
|
||||
"source_count": int(payload.get("total_result_count") or len(raw_results)),
|
||||
"results_truncated": bool(payload.get("results_truncated")) or len(raw_results) > len(results),
|
||||
"partial": bool(payload.get("partial")),
|
||||
"warnings": [str(item) for item in payload.get("warnings") or []],
|
||||
"fallback_reason": diagnostics.get("fallback_reason"),
|
||||
"fallback_knowledge_base_ids": [
|
||||
str(item) for item in payload.get("fallback_knowledge_base_ids") or []
|
||||
],
|
||||
"diagnostics": diagnostics,
|
||||
"timings_ms": payload.get("timings_ms") if isinstance(payload.get("timings_ms"), dict) else {},
|
||||
"results": results,
|
||||
"zero_result_reason": None if results else payload.get("zero_result_reason"),
|
||||
}
|
||||
|
||||
|
||||
def _extract_knowledge_retrieval_traces(messages: list[Any]) -> list[dict[str, Any]]:
|
||||
"""Extract pre-retrieval and tool traces from persisted message state."""
|
||||
|
||||
traces: list[dict[str, Any]] = []
|
||||
seen: set[str] = set()
|
||||
for message in messages:
|
||||
trace = _trace_message_extra(message).get("knowledge_retrieval_trace")
|
||||
if not isinstance(trace, dict):
|
||||
trace = _tool_trace_from_message(message)
|
||||
if not isinstance(trace, dict):
|
||||
continue
|
||||
item = dict(trace)
|
||||
trace_id = str(item.get("retrieval_trace_id") or "")
|
||||
if not trace_id or trace_id in seen:
|
||||
continue
|
||||
seen.add(trace_id)
|
||||
message_id = _trace_message_value(message, "id")
|
||||
if message_id:
|
||||
item.setdefault("message_id", str(message_id))
|
||||
traces.append(item)
|
||||
return traces
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -581,6 +733,63 @@ def _build_reference_batches(messages: list[Any]) -> list[ReferenceBatchResponse
|
||||
return batches
|
||||
|
||||
|
||||
async def _invalidate_project_memory_source_before_mutation(
|
||||
request: Request,
|
||||
*,
|
||||
thread_id: str,
|
||||
source_message_ids: list[str] | None,
|
||||
) -> dict[str, Any] | None:
|
||||
"""Reliably fence stale project memory before a transcript mutation."""
|
||||
|
||||
from app.gateway.analysis_project_memory import (
|
||||
invalidate_project_memory_before_thread_mutation,
|
||||
)
|
||||
|
||||
try:
|
||||
return await invalidate_project_memory_before_thread_mutation(
|
||||
app=request.app,
|
||||
thread_id=thread_id,
|
||||
source_message_ids=source_message_ids,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - mutation must not outrun invalidation
|
||||
logger.exception(
|
||||
"Could not fence project memory before mutating thread %s",
|
||||
sanitize_log_param(thread_id),
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail="项目记忆暂时无法同步,本次对话修改尚未执行,请稍后重试",
|
||||
) from exc
|
||||
|
||||
|
||||
async def _finalize_project_memory_source_mutation(
|
||||
request: Request,
|
||||
*,
|
||||
project: dict[str, Any] | None,
|
||||
thread_id: str,
|
||||
thread_deleted: bool = False,
|
||||
) -> None:
|
||||
"""Best-effort rebuild; invalidation already makes any failure fail-closed."""
|
||||
|
||||
from app.gateway.analysis_project_memory import (
|
||||
finalize_project_memory_after_thread_mutation,
|
||||
)
|
||||
|
||||
try:
|
||||
await finalize_project_memory_after_thread_mutation(
|
||||
app=request.app,
|
||||
project=project,
|
||||
thread_id=thread_id,
|
||||
thread_deleted=thread_deleted,
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning(
|
||||
"Project memory remains fail-closed after mutating thread %s; rebuild pending",
|
||||
sanitize_log_param(thread_id),
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Endpoints
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -597,6 +806,12 @@ async def delete_thread_data(thread_id: str, request: Request) -> ThreadDeleteRe
|
||||
"""
|
||||
from app.gateway.deps import get_thread_store
|
||||
|
||||
memory_project = await _invalidate_project_memory_source_before_mutation(
|
||||
request,
|
||||
thread_id=thread_id,
|
||||
source_message_ids=None,
|
||||
)
|
||||
|
||||
# Clean local filesystem
|
||||
response = _delete_thread_data(thread_id, user_id=get_effective_user_id())
|
||||
|
||||
@@ -609,6 +824,15 @@ async def delete_thread_data(thread_id: str, request: Request) -> ThreadDeleteRe
|
||||
except Exception:
|
||||
logger.debug("Could not delete checkpoints for thread %s (not critical)", sanitize_log_param(thread_id))
|
||||
|
||||
# Invalidation committed before deletion. Remove the immutable binding and
|
||||
# rebuild the remaining project history; a rebuild failure stays fail-closed.
|
||||
await _finalize_project_memory_source_mutation(
|
||||
request,
|
||||
project=memory_project,
|
||||
thread_id=thread_id,
|
||||
thread_deleted=True,
|
||||
)
|
||||
|
||||
# Remove thread_meta row (best-effort) — required for sqlite backend
|
||||
# so the deleted thread no longer appears in /threads/search.
|
||||
try:
|
||||
@@ -633,7 +857,7 @@ async def delete_thread_data(thread_id: str, request: Request) -> ThreadDeleteRe
|
||||
|
||||
|
||||
@router.post("", response_model=ThreadResponse)
|
||||
async def create_thread(body: ThreadCreateRequest, request: Request) -> ThreadResponse:
|
||||
async def create_thread(body: ThreadCreateRequest, request: Request, response: Response = None) -> ThreadResponse: # type: ignore[assignment]
|
||||
"""Create a new thread.
|
||||
|
||||
Writes a thread_meta record (so the thread appears in /threads/search)
|
||||
@@ -649,56 +873,100 @@ async def create_thread(body: ThreadCreateRequest, request: Request) -> ThreadRe
|
||||
# ``body.metadata`` is already stripped of server-reserved keys by
|
||||
# ``ThreadCreateRequest._strip_reserved`` — see the model definition.
|
||||
|
||||
# Idempotency: return existing record when already present
|
||||
existing_record = await thread_store.get(thread_id)
|
||||
if existing_record is not None:
|
||||
with OperationTiming(
|
||||
response,
|
||||
logger,
|
||||
"thread_create",
|
||||
thread_id=sanitize_log_param(thread_id),
|
||||
metadata_store=type(thread_store).__name__,
|
||||
checkpointer=type(checkpointer).__name__,
|
||||
) as timing:
|
||||
# Idempotency: return existing record when already present
|
||||
with timing.phase("metadata_lookup"):
|
||||
existing_record = await thread_store.get(thread_id)
|
||||
if existing_record is not None:
|
||||
return ThreadResponse(
|
||||
thread_id=thread_id,
|
||||
status=existing_record.get("status", "idle"),
|
||||
created_at=coerce_iso(existing_record.get("created_at", "")),
|
||||
updated_at=coerce_iso(existing_record.get("updated_at", "")),
|
||||
metadata=existing_record.get("metadata", {}),
|
||||
)
|
||||
|
||||
# Write thread_meta so the thread appears in /threads/search immediately
|
||||
try:
|
||||
with timing.phase("metadata_create"):
|
||||
await thread_store.create(
|
||||
thread_id,
|
||||
assistant_id=getattr(body, "assistant_id", None),
|
||||
metadata=body.metadata,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Failed to write thread_meta for %s", sanitize_log_param(thread_id))
|
||||
raise HTTPException(status_code=500, detail="Failed to create thread")
|
||||
|
||||
# Keep the initial checkpoint: state endpoints must work immediately.
|
||||
# Its timing includes saver lock/pool waits as well as the DB write.
|
||||
config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}}
|
||||
try:
|
||||
ckpt_metadata = {
|
||||
"step": -1,
|
||||
"source": "input",
|
||||
"writes": None,
|
||||
"parents": {},
|
||||
**body.metadata,
|
||||
"created_at": now,
|
||||
}
|
||||
with timing.phase("checkpoint_create"):
|
||||
await checkpointer.aput(config, empty_checkpoint(), ckpt_metadata, {})
|
||||
except Exception:
|
||||
logger.exception("Failed to create checkpoint for thread %s", sanitize_log_param(thread_id))
|
||||
raise HTTPException(status_code=500, detail="Failed to create thread")
|
||||
|
||||
logger.info("Thread created: %s", sanitize_log_param(thread_id))
|
||||
return ThreadResponse(
|
||||
thread_id=thread_id,
|
||||
status=existing_record.get("status", "idle"),
|
||||
created_at=coerce_iso(existing_record.get("created_at", "")),
|
||||
updated_at=coerce_iso(existing_record.get("updated_at", "")),
|
||||
metadata=existing_record.get("metadata", {}),
|
||||
)
|
||||
|
||||
# Write thread_meta so the thread appears in /threads/search immediately
|
||||
try:
|
||||
await thread_store.create(
|
||||
thread_id,
|
||||
assistant_id=getattr(body, "assistant_id", None),
|
||||
status="idle",
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
metadata=body.metadata,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Failed to write thread_meta for %s", sanitize_log_param(thread_id))
|
||||
raise HTTPException(status_code=500, detail="Failed to create thread")
|
||||
|
||||
# Write an empty checkpoint so state endpoints work immediately
|
||||
config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}}
|
||||
try:
|
||||
ckpt_metadata = {
|
||||
"step": -1,
|
||||
"source": "input",
|
||||
"writes": None,
|
||||
"parents": {},
|
||||
**body.metadata,
|
||||
"created_at": now,
|
||||
}
|
||||
await checkpointer.aput(config, empty_checkpoint(), ckpt_metadata, {})
|
||||
except Exception:
|
||||
logger.exception("Failed to create checkpoint for thread %s", sanitize_log_param(thread_id))
|
||||
raise HTTPException(status_code=500, detail="Failed to create thread")
|
||||
|
||||
logger.info("Thread created: %s", sanitize_log_param(thread_id))
|
||||
return ThreadResponse(
|
||||
thread_id=thread_id,
|
||||
status="idle",
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
metadata=body.metadata,
|
||||
def _clear_request_user_id(request: Request) -> str:
|
||||
auth = getattr(request.state, "auth", None)
|
||||
user = getattr(auth, "user", None)
|
||||
user_id = getattr(user, "id", None)
|
||||
if user_id:
|
||||
return str(user_id)
|
||||
return get_effective_user_id()
|
||||
|
||||
|
||||
@router.post("/clear", response_model=ThreadClearResponse)
|
||||
@require_permission("threads", "delete")
|
||||
async def clear_user_chat_threads(request: Request) -> ThreadClearResponse:
|
||||
"""Delete the current user's ordinary chats and every agent chat they own.
|
||||
|
||||
System threads (roundtable, scheduler, notebooks, writing setup, deep
|
||||
research) are left in place. One call replaces per-thread delete loops.
|
||||
"""
|
||||
from app.gateway.deps import get_thread_store
|
||||
from app.gateway.thread_clear import clear_owned_chat_threads
|
||||
|
||||
user_id = _clear_request_user_id(request)
|
||||
deleted = await clear_owned_chat_threads(
|
||||
store=get_thread_store(request),
|
||||
user_id=user_id,
|
||||
checkpointer=getattr(request.app.state, "checkpointer", None),
|
||||
session_factory=get_session_factory(),
|
||||
paths=get_paths(),
|
||||
app=request.app,
|
||||
)
|
||||
return ThreadClearResponse(deleted=deleted)
|
||||
|
||||
|
||||
@router.post("/search", response_model=list[ThreadResponse])
|
||||
async def search_threads(body: ThreadSearchRequest, request: Request) -> list[ThreadResponse]:
|
||||
async def search_threads(body: ThreadSearchRequest, request: Request, response: Response = None) -> list[ThreadResponse]: # type: ignore[assignment]
|
||||
"""Search and list threads.
|
||||
|
||||
Delegates to the configured ThreadMetaStore implementation
|
||||
@@ -711,32 +979,36 @@ async def search_threads(body: ThreadSearchRequest, request: Request) -> list[Th
|
||||
# writing setup wizards, …) carry ``metadata.system=true`` and/or a known
|
||||
# ``thread_type`` — hide them from the default conversation list so they
|
||||
# don't crowd out real chats. A caller that explicitly filters on
|
||||
# ``thread_type``/``system`` (e.g. the studio notebook list) opts out of
|
||||
# the exclusion.
|
||||
explicit_system_filter = bool(body.metadata) and ("thread_type" in body.metadata or "system" in body.metadata)
|
||||
rows = await repo.search(
|
||||
metadata=body.metadata or None,
|
||||
status=body.status,
|
||||
limit=body.limit,
|
||||
offset=body.offset,
|
||||
exclude_system=not explicit_system_filter,
|
||||
query=body.query,
|
||||
# ``thread_type``/``system`` (e.g. the studio notebook list), or explicitly
|
||||
# filters one task via ``taskId``, opts out of the exclusion.
|
||||
explicit_system_filter = bool(body.metadata) and any(
|
||||
key in body.metadata for key in ("thread_type", "system", "taskId")
|
||||
)
|
||||
return [
|
||||
ThreadResponse(
|
||||
thread_id=r["thread_id"],
|
||||
status=r.get("status", "idle"),
|
||||
# ``coerce_iso`` heals legacy unix-second values that
|
||||
# ``MemoryThreadMetaStore`` historically wrote with ``time.time()``;
|
||||
# SQL-backed rows already arrive as ISO strings and pass through.
|
||||
created_at=coerce_iso(r.get("created_at", "")),
|
||||
updated_at=coerce_iso(r.get("updated_at", "")),
|
||||
metadata=r.get("metadata", {}),
|
||||
values={"title": r["display_name"]} if r.get("display_name") else {},
|
||||
interrupts={},
|
||||
)
|
||||
for r in rows
|
||||
]
|
||||
with OperationTiming(response, logger, "thread_search", metadata_store=type(repo).__name__) as timing:
|
||||
with timing.phase("metadata_search"):
|
||||
rows = await repo.search(
|
||||
metadata=body.metadata or None,
|
||||
status=body.status,
|
||||
limit=body.limit,
|
||||
offset=body.offset,
|
||||
exclude_system=not explicit_system_filter,
|
||||
query=body.query,
|
||||
)
|
||||
return [
|
||||
ThreadResponse(
|
||||
thread_id=r["thread_id"],
|
||||
status=r.get("status", "idle"),
|
||||
# ``coerce_iso`` heals legacy unix-second values that
|
||||
# ``MemoryThreadMetaStore`` historically wrote with ``time.time()``;
|
||||
# SQL-backed rows already arrive as ISO strings and pass through.
|
||||
created_at=coerce_iso(r.get("created_at", "")),
|
||||
updated_at=coerce_iso(r.get("updated_at", "")),
|
||||
metadata=r.get("metadata", {}),
|
||||
values={"title": r["display_name"]} if r.get("display_name") else {},
|
||||
interrupts={},
|
||||
)
|
||||
for r in rows
|
||||
]
|
||||
|
||||
|
||||
class ThreadCountResponse(BaseModel):
|
||||
@@ -755,7 +1027,9 @@ async def count_threads(body: ThreadSearchRequest, request: Request) -> ThreadCo
|
||||
from app.gateway.deps import get_thread_store
|
||||
|
||||
repo = get_thread_store(request)
|
||||
explicit_system_filter = bool(body.metadata) and ("thread_type" in body.metadata or "system" in body.metadata)
|
||||
explicit_system_filter = bool(body.metadata) and any(
|
||||
key in body.metadata for key in ("thread_type", "system", "taskId")
|
||||
)
|
||||
total = await repo.count(
|
||||
metadata=body.metadata or None,
|
||||
status=body.status,
|
||||
@@ -929,6 +1203,7 @@ async def get_thread_reference_batches(thread_id: str, request: Request) -> Refe
|
||||
_rag_debug_file_path(),
|
||||
)
|
||||
return ReferenceBatchesResponse(batches=[])
|
||||
retrieval_traces = _extract_knowledge_retrieval_traces(messages)
|
||||
batches_before_display_adapt = build_runtime_reference_batches(messages)
|
||||
raw_batches = batches_before_display_adapt
|
||||
persisted_displays = await _load_persisted_skill_displays()
|
||||
@@ -938,7 +1213,7 @@ async def get_thread_reference_batches(thread_id: str, request: Request) -> Refe
|
||||
_log_rag_debug(
|
||||
"backend reference-batches thread=%s messages=%s turns=%s tool_messages=%s raw_items=%s "
|
||||
"normalized_items=%s kept_items=%s batches=%s batch_sources=%s batch_ids=%s "
|
||||
"display_adapters=%s turns_detail=%s log_file=%s",
|
||||
"display_adapters=%s retrieval_traces=%s turns_detail=%s log_file=%s",
|
||||
sanitize_log_param(thread_id),
|
||||
debug_summary["messages"],
|
||||
debug_summary["turns"],
|
||||
@@ -950,6 +1225,7 @@ async def get_thread_reference_batches(thread_id: str, request: Request) -> Refe
|
||||
",".join(str(batch.get("source_count", 0)) for batch in debug_summary["batches"]),
|
||||
",".join(str(batch.get("id") or "") for batch in debug_summary["batches"]),
|
||||
len(persisted_displays),
|
||||
len(retrieval_traces),
|
||||
json.dumps(debug_summary["turns_detail"], ensure_ascii=False),
|
||||
_rag_debug_file_path(),
|
||||
)
|
||||
@@ -984,7 +1260,8 @@ async def get_thread_reference_batches(thread_id: str, request: Request) -> Refe
|
||||
],
|
||||
)
|
||||
for batch in raw_batches
|
||||
]
|
||||
],
|
||||
retrieval_traces=retrieval_traces,
|
||||
)
|
||||
|
||||
|
||||
@@ -1042,6 +1319,13 @@ async def update_thread_state(thread_id: str, body: ThreadStateUpdateRequest, re
|
||||
metadata: dict[str, Any] = dict(getattr(checkpoint_tuple, "metadata", {}) or {})
|
||||
channel_values: dict[str, Any] = dict(checkpoint.get("channel_values", {}))
|
||||
|
||||
memory_project = None
|
||||
if body.values and "messages" in body.values:
|
||||
memory_project = await _invalidate_project_memory_source_before_mutation(
|
||||
request,
|
||||
thread_id=thread_id,
|
||||
source_message_ids=None,
|
||||
)
|
||||
if body.values:
|
||||
channel_values.update(body.values)
|
||||
|
||||
@@ -1065,9 +1349,20 @@ async def update_thread_state(thread_id: str, body: ThreadStateUpdateRequest, re
|
||||
try:
|
||||
new_config = await checkpointer.aput(write_config, checkpoint, metadata, {})
|
||||
except Exception:
|
||||
await _finalize_project_memory_source_mutation(
|
||||
request,
|
||||
project=memory_project,
|
||||
thread_id=thread_id,
|
||||
)
|
||||
logger.exception("Failed to update state for thread %s", sanitize_log_param(thread_id))
|
||||
raise HTTPException(status_code=500, detail="Failed to update thread state")
|
||||
|
||||
await _finalize_project_memory_source_mutation(
|
||||
request,
|
||||
project=memory_project,
|
||||
thread_id=thread_id,
|
||||
)
|
||||
|
||||
new_checkpoint_id: str | None = None
|
||||
if isinstance(new_config, dict):
|
||||
new_checkpoint_id = new_config.get("configurable", {}).get("checkpoint_id")
|
||||
@@ -1343,6 +1638,11 @@ async def clear_thread_messages(thread_id: str, request: Request) -> dict[str, A
|
||||
messages = []
|
||||
|
||||
deleted_ids = [mid for m in messages if (mid := _message_id(m))]
|
||||
memory_project = await _invalidate_project_memory_source_before_mutation(
|
||||
request,
|
||||
thread_id=thread_id,
|
||||
source_message_ids=None,
|
||||
)
|
||||
channel_values["messages"] = []
|
||||
checkpoint["channel_values"] = channel_values
|
||||
checkpoint["id"] = str(uuid6(clock_seq=-2))
|
||||
@@ -1356,6 +1656,11 @@ async def clear_thread_messages(thread_id: str, request: Request) -> dict[str, A
|
||||
try:
|
||||
await checkpointer.aput(config, checkpoint, metadata, {"messages": new_ver})
|
||||
except Exception:
|
||||
await _finalize_project_memory_source_mutation(
|
||||
request,
|
||||
project=memory_project,
|
||||
thread_id=thread_id,
|
||||
)
|
||||
logger.exception("Failed to clear messages from thread %s", sanitize_log_param(thread_id))
|
||||
raise HTTPException(status_code=500, detail="Failed to clear thread messages")
|
||||
|
||||
@@ -1371,6 +1676,11 @@ async def clear_thread_messages(thread_id: str, request: Request) -> dict[str, A
|
||||
message_ids=None,
|
||||
)
|
||||
)
|
||||
await _finalize_project_memory_source_mutation(
|
||||
request,
|
||||
project=memory_project,
|
||||
thread_id=thread_id,
|
||||
)
|
||||
return {
|
||||
"ok": True,
|
||||
"deleted_ids": deleted_ids,
|
||||
@@ -1438,6 +1748,11 @@ async def delete_thread_message(thread_id: str, message_id: str, request: Reques
|
||||
deleted_ids = [
|
||||
mid for m in messages[target_index:end] if (mid := _message_id(m))
|
||||
]
|
||||
memory_project = await _invalidate_project_memory_source_before_mutation(
|
||||
request,
|
||||
thread_id=thread_id,
|
||||
source_message_ids=deleted_ids,
|
||||
)
|
||||
filtered = messages[:target_index] + messages[end:]
|
||||
|
||||
channel_values["messages"] = filtered
|
||||
@@ -1459,6 +1774,11 @@ async def delete_thread_message(thread_id: str, message_id: str, request: Reques
|
||||
try:
|
||||
await checkpointer.aput(write_config, checkpoint, metadata, {"messages": new_ver})
|
||||
except Exception:
|
||||
await _finalize_project_memory_source_mutation(
|
||||
request,
|
||||
project=memory_project,
|
||||
thread_id=thread_id,
|
||||
)
|
||||
logger.exception(
|
||||
"Failed to delete message %s from thread %s",
|
||||
sanitize_log_param(message_id),
|
||||
@@ -1479,6 +1799,11 @@ async def delete_thread_message(thread_id: str, message_id: str, request: Reques
|
||||
message_ids=deleted_ids,
|
||||
)
|
||||
)
|
||||
await _finalize_project_memory_source_mutation(
|
||||
request,
|
||||
project=memory_project,
|
||||
thread_id=thread_id,
|
||||
)
|
||||
return {"ok": True, "deleted_ids": deleted_ids, "remaining": len(filtered)}
|
||||
|
||||
|
||||
@@ -1527,9 +1852,22 @@ async def edit_thread_message(
|
||||
if target_index is None:
|
||||
raise HTTPException(status_code=404, detail=f"Message {message_id} not found")
|
||||
|
||||
memory_project = await _invalidate_project_memory_source_before_mutation(
|
||||
request,
|
||||
thread_id=thread_id,
|
||||
source_message_ids=[message_id],
|
||||
)
|
||||
try:
|
||||
_set_message_content(messages[target_index], body.content)
|
||||
except TypeError as exc:
|
||||
# Invalidation is durable and intentionally happens first. If this
|
||||
# provider-specific message object cannot be edited, rebuild the
|
||||
# unchanged checkpoint instead of leaving memory fail-closed forever.
|
||||
await _finalize_project_memory_source_mutation(
|
||||
request,
|
||||
project=memory_project,
|
||||
thread_id=thread_id,
|
||||
)
|
||||
raise HTTPException(status_code=422, detail=str(exc)) from exc
|
||||
|
||||
channel_values["messages"] = messages
|
||||
@@ -1545,6 +1883,11 @@ async def edit_thread_message(
|
||||
try:
|
||||
await checkpointer.aput(config, checkpoint, metadata, {"messages": new_ver})
|
||||
except Exception:
|
||||
await _finalize_project_memory_source_mutation(
|
||||
request,
|
||||
project=memory_project,
|
||||
thread_id=thread_id,
|
||||
)
|
||||
logger.exception(
|
||||
"Failed to edit message %s from thread %s",
|
||||
sanitize_log_param(message_id),
|
||||
@@ -1558,6 +1901,11 @@ async def edit_thread_message(
|
||||
sanitize_log_param(message_id),
|
||||
sanitize_log_param(thread_id),
|
||||
)
|
||||
await _finalize_project_memory_source_mutation(
|
||||
request,
|
||||
project=memory_project,
|
||||
thread_id=thread_id,
|
||||
)
|
||||
return {
|
||||
"ok": True,
|
||||
"message": serialized_messages[0] if serialized_messages else {"id": message_id, "content": body.content},
|
||||
|
||||
Reference in new issue
Block a user