feat: 新增报告协作页,并强化 Deep Research 运行诊断与报告兜底

支持浏览器侧素材快照撰写、拒绝报告读时修复、Mermaid PNG 导出与 Word 导出加固。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
董宏凯andCursor committed 2026-09-03 11:16:50 +08:00
1 parent 65d6b61d5d
commit f109a95535
65 files changed
+10342 -245

No files matched your search

@@ -13,8 +13,11 @@ don't carry those ids around.
from __future__ import annotations
import json
import logging
from collections.abc import Awaitable, Callable
from datetime import date, datetime
from enum import Enum
from typing import TYPE_CHECKING, Any
from deerflow.agents.deep_research.events import (
@@ -29,6 +32,26 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
def _json_default(value: Any) -> Any:
if isinstance(value, (datetime, date)):
return value.isoformat()
if isinstance(value, Enum):
return value.value
if hasattr(value, "model_dump"):
return value.model_dump(mode="json")
return str(value)
def _json_safe_payload(payload: dict[str, Any]) -> dict[str, Any]:
"""Normalize event payloads before they reach any event repository.
This protects deployments that still use a strict ``json.dumps`` inside
the SQL repository and keeps native datetime values in source metadata
from degrading every ``source_added`` event.
"""
return json.loads(json.dumps(payload, ensure_ascii=False, default=_json_default))
class PersistingEventSink:
"""EventSink implementation backed by the deep-research event repository.
@@ -48,6 +71,7 @@ class PersistingEventSink:
self._session_id = session_id
self._job_id = job_id
self._on_event = on_event
self.diagnostics: list[dict[str, Any]] = []
# A durable event row normally owns the sequence. When the event
# database is temporarily unavailable, use distinct negative sequence
# numbers for the live-only path so clients can render every milestone
@@ -61,7 +85,8 @@ class PersistingEventSink:
phase: ResearchPhase = "initializing",
payload: dict | None = None,
) -> DeepResearchEvent:
event_payload = payload or {}
event_payload = _json_safe_payload(payload or {})
persistence_error: Exception | None = None
try:
row = await self._repo.append(
session_id=self._session_id,
@@ -79,17 +104,29 @@ class PersistingEventSink:
phase=row.get("phase", phase),
payload=row.get("payload") or event_payload,
)
except Exception: # noqa: BLE001
except Exception as exc: # noqa: BLE001
# Progress persistence must not turn a recoverable database blip
# into a failed report. The job/status stores still record terminal
# state; this live-only frame lets an attached UI stay responsive
# until durable replay becomes available again after reconnect.
self._fallback_seq -= 1
logger.exception(
"deep-research event persistence failed; continuing job with a live-only event (job=%s, type=%s, phase=%s)",
persistence_error = exc
self.diagnostics.append(
{
"code": "event_persistence_degraded",
"stage": phase,
"eventType": type,
"errorType": exc.__class__.__name__,
"detail": str(exc).split("[SQL:", 1)[0].strip()[:600] or exc.__class__.__name__,
}
)
logger.warning(
"deep-research event persistence degraded; continuing job with a live-only event "
"(job=%s, type=%s, phase=%s, error=%s)",
self._job_id,
type,
phase,
persistence_error.__class__.__name__,
)
event = DeepResearchEvent(
seq=self._fallback_seq,
@@ -100,6 +137,32 @@ class PersistingEventSink:
payload={**event_payload, "eventPersistenceDegraded": True},
)
await self._publish_event(event)
if persistence_error is not None:
# Keep the original source/progress event live, and additionally
# expose one copyable diagnostic frame. This never re-enters the
# failing repository and therefore cannot interrupt the job.
self._fallback_seq -= 1
detail = str(persistence_error).split("[SQL:", 1)[0].strip()[:600]
await self._publish_event(
DeepResearchEvent(
seq=self._fallback_seq,
session_id=self._session_id,
job_id=self._job_id,
type="warning",
phase=phase,
payload={
"code": "event_persistence_degraded",
"message": "研究过程事件暂未写入数据库,报告任务已继续运行",
"recoverable": True,
"diagnostic": {
"stage": phase,
"eventType": type,
"errorType": persistence_error.__class__.__name__,
"detail": detail or persistence_error.__class__.__name__,
},
},
)
)
return event
async def _publish_event(self, event: DeepResearchEvent) -> None:
@@ -427,6 +427,54 @@ class DeerFlowCompletionBackend:
await self._record_usage(model_name, operation, usage)
return CompletionResult(text="".join(text_parts), model=model_name, usage=usage)
async def complete_prose(
self,
*,
model_role: ModelRole = "smart",
messages: list[dict[str, str]],
temperature: float | None = None,
max_tokens: int | None = None,
operation: str = "unknown",
) -> CompletionResult:
"""Generate long prose without the provider's inter-chunk timeout.
This is the report retry path. Some reasoning models pause for longer
than the streaming client's 120-second chunk deadline after already
producing several chunks. A normal complete response can still finish
successfully, so the retry deliberately waits for that full response.
"""
if self._cancel is not None:
self._cancel.raise_if_cancelled()
model = self._get_model(model_role)
model_name = self._resolve_model_name(model_role) or "default"
kwargs: dict[str, Any] = {}
if temperature is not None:
kwargs["temperature"] = temperature
if max_tokens is not None:
from deerflow.models.openai_codex_provider import CodexChatModel
if not isinstance(model, CodexChatModel):
kwargs["max_tokens"] = max_tokens
isolated_config = _parent_isolated_run_config(f"deep_research:{operation}")
reset_parent_config = _clear_inherited_runnable_config(isolated_config)
try:
response = await model.ainvoke(
to_langchain_messages(messages),
config=isolated_config,
**kwargs,
)
finally:
reset_parent_config()
if self._cancel is not None:
self._cancel.raise_if_cancelled()
text = _content_to_text(response)
usage = _extract_usage(response)
await self._record_usage(model_name, operation, usage)
return CompletionResult(text=text, model=model_name, usage=usage)
# ── usage accounting ────────────────────────────────────────────────────
async def _record_usage(self, model_name: str, operation: str, usage: dict[str, Any]) -> None: