feat(deep-research): 强化任务链路可靠性并接入 agentfx 报告助手种子
功能说明:\n- 重构 Deep Research 调度与执行链,补齐 dispatcher、executor、live hub 与依赖注入入口,提升任务下发、执行、回调的稳定性\n- 增强运行时持久化与恢复能力,接入业务映射、任务来源与任务事件日志模型,覆盖持久化与执行可靠性关键路径测试\n- 新增 agentfx 报告助手 seed 与任务报告导入脚本配置,支持离线环境快速初始化与任务流模板回填\n- 完善 deep-research 与 thread_runs 路由参数编排,兼容不同业务映射与运行入口的前后端联动\n- 补充前端 agentfx deep-link 解析,保证 deep-research 场景下的登录跳转与任务上下文一致
This commit is contained in:
1 parent
438772f0b7
commit
5c75e1af16
36 files changed
+3163
-312
No files matched your search
+108
@@ -16,8 +16,10 @@ from __future__ import annotations
|
||||
import json
|
||||
import logging
|
||||
from collections.abc import Awaitable, Callable
|
||||
from contextlib import suppress
|
||||
from datetime import date, datetime
|
||||
from enum import Enum
|
||||
from time import monotonic
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from deerflow.agents.deep_research.events import (
|
||||
@@ -42,6 +44,20 @@ def _json_default(value: Any) -> Any:
|
||||
return str(value)
|
||||
|
||||
|
||||
# Live-only frame types that have no durable counterpart at all. Report and
|
||||
# summary text are already checkpointed (``report_chunk`` / ``summary_completed``),
|
||||
# so only the reasoning channel needs a cross-worker mirror.
|
||||
_MIRRORED_LIVE_TYPES = frozenset({"report_thinking", "plan_reasoning", "summary_thinking"})
|
||||
# Coalescing bounds for that mirror: one bounded row per ~600 characters or
|
||||
# ~1.5 seconds, never one row per provider token.
|
||||
_MIRROR_MIN_CHARS = 600
|
||||
_MIRROR_MAX_DELAY_S = 1.5
|
||||
# Floor for the time-based flush: a slowly trickling reasoning stream must not
|
||||
# degrade into one durable row per token. Anything shorter is carried until the
|
||||
# next durable milestone flushes it.
|
||||
_MIRROR_MIN_FLUSH_CHARS = 40
|
||||
|
||||
|
||||
def _json_safe_payload(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Normalize event payloads before they reach any event repository.
|
||||
|
||||
@@ -66,11 +82,18 @@ class PersistingEventSink:
|
||||
session_id: str,
|
||||
job_id: str,
|
||||
on_event: Callable[[dict[str, Any]], Awaitable[None]] | None = None,
|
||||
live_subscriber_probe: Callable[[str], bool] | None = None,
|
||||
) -> None:
|
||||
self._repo = repo
|
||||
self._session_id = session_id
|
||||
self._job_id = job_id
|
||||
self._on_event = on_event
|
||||
# Reports whether this worker holds an SSE connection for the job.
|
||||
# Absent probe → assume the live path reaches the viewer (single-worker
|
||||
# deployments and tests keep today's zero-extra-write behaviour).
|
||||
self._live_subscriber_probe = live_subscriber_probe
|
||||
self._mirror_buffers: dict[str, dict[str, Any]] = {}
|
||||
self._mirroring = False
|
||||
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
|
||||
@@ -87,6 +110,10 @@ class PersistingEventSink:
|
||||
) -> DeepResearchEvent:
|
||||
event_payload = _json_safe_payload(payload or {})
|
||||
persistence_error: Exception | None = None
|
||||
if not self._mirroring:
|
||||
# Order any coalesced reasoning tail before the next milestone
|
||||
# instead of leaving it buffered until the job ends.
|
||||
await self._flush_live_mirrors()
|
||||
try:
|
||||
row = await self._repo.append(
|
||||
session_id=self._session_id,
|
||||
@@ -211,6 +238,87 @@ class PersistingEventSink:
|
||||
type,
|
||||
exc_info=True,
|
||||
)
|
||||
with suppress(Exception):
|
||||
await self._mirror_live_frame(type, phase=phase, payload=payload or {})
|
||||
|
||||
# ── cross-worker mirror for live-only reasoning frames ──────────────────
|
||||
|
||||
async def _mirror_live_frame(
|
||||
self,
|
||||
type: str, # noqa: A002
|
||||
*,
|
||||
phase: ResearchPhase,
|
||||
payload: dict[str, Any],
|
||||
) -> None:
|
||||
"""Buffer a live-only reasoning delta when no local viewer can see it.
|
||||
|
||||
The live hub is in-process. A job runs on the worker that won its
|
||||
durable lease, while the SSE connection is served by whichever worker
|
||||
accepted it, so on a multi-worker deployment the model's thinking is
|
||||
usually published into a hub with no subscribers and is then lost —
|
||||
there is no durable row to replay it from. When that is the case, the
|
||||
reasoning is coalesced here and persisted as a bounded durable event
|
||||
so the polling stream on the other worker still renders it.
|
||||
"""
|
||||
if type not in _MIRRORED_LIVE_TYPES or self._live_subscriber_probe is None:
|
||||
return
|
||||
delta = payload.get("delta")
|
||||
if not isinstance(delta, str) or not delta:
|
||||
return
|
||||
if self._live_subscriber_probe(self._job_id):
|
||||
# A viewer is attached here and has already received every frame
|
||||
# live. Drop the buffer rather than flushing it, so that viewer
|
||||
# does not also receive the same text as a durable replay.
|
||||
self._mirror_buffers.pop(self._mirror_key(type, payload), None)
|
||||
return
|
||||
key = self._mirror_key(type, payload)
|
||||
buffered = self._mirror_buffers.get(key)
|
||||
if buffered is None:
|
||||
buffered = {
|
||||
"type": type,
|
||||
"phase": phase,
|
||||
"target": payload.get("target"),
|
||||
"reportVersion": payload.get("reportVersion"),
|
||||
"text": "",
|
||||
"since": monotonic(),
|
||||
}
|
||||
self._mirror_buffers[key] = buffered
|
||||
buffered["text"] += delta
|
||||
buffered["phase"] = phase
|
||||
buffered_chars = len(buffered["text"])
|
||||
aged_out = (
|
||||
buffered_chars >= _MIRROR_MIN_FLUSH_CHARS
|
||||
and monotonic() - float(buffered["since"]) >= _MIRROR_MAX_DELAY_S
|
||||
)
|
||||
if buffered_chars >= _MIRROR_MIN_CHARS or aged_out:
|
||||
await self._emit_mirror(key)
|
||||
|
||||
@staticmethod
|
||||
def _mirror_key(type: str, payload: dict[str, Any]) -> str: # noqa: A002
|
||||
return f"{type}::{payload.get('target') or 'report'}"
|
||||
|
||||
async def _flush_live_mirrors(self) -> None:
|
||||
for key in tuple(self._mirror_buffers):
|
||||
with suppress(Exception):
|
||||
await self._emit_mirror(key)
|
||||
|
||||
async def _emit_mirror(self, key: str) -> None:
|
||||
buffered = self._mirror_buffers.pop(key, None)
|
||||
if buffered is None or not buffered["text"]:
|
||||
return
|
||||
payload: dict[str, Any] = {
|
||||
"delta": buffered["text"],
|
||||
"liveMirror": True,
|
||||
}
|
||||
if buffered["target"] is not None:
|
||||
payload["target"] = buffered["target"]
|
||||
if buffered["reportVersion"] is not None:
|
||||
payload["reportVersion"] = buffered["reportVersion"]
|
||||
self._mirroring = True
|
||||
try:
|
||||
await self.emit(buffered["type"], phase=buffered["phase"], payload=payload)
|
||||
finally:
|
||||
self._mirroring = False
|
||||
|
||||
|
||||
class NullEventSink:
|
||||
|
||||
+55
-26
@@ -33,7 +33,7 @@ from deerflow.agents.deep_research.cancellation import CancellationToken
|
||||
from deerflow.agents.deep_research.config import DeepResearchConfig, ModelRole
|
||||
from deerflow.agents.deep_research.events import EventSink
|
||||
from deerflow.agents.deep_research.types import CompletionResult
|
||||
from deerflow.utils.stream_text import InlineThinkTagFilter
|
||||
from deerflow.utils.stream_text import InlineThinkTagFilter, strip_inline_think_tags
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from langchain.chat_models import BaseChatModel
|
||||
@@ -388,31 +388,57 @@ class DeerFlowCompletionBackend:
|
||||
# Custom ``report_delta`` events remain the only live preview channel.
|
||||
isolated_config = _parent_isolated_run_config(f"deep_research:{operation}")
|
||||
reset_parent_config = _clear_inherited_runnable_config(isolated_config)
|
||||
attempts = 2 if operation.startswith("write_report") else 1
|
||||
last_error: Exception | None = None
|
||||
try:
|
||||
async for chunk in model.astream(
|
||||
to_langchain_messages(messages),
|
||||
config=isolated_config,
|
||||
**kwargs,
|
||||
):
|
||||
if self._cancel is not None:
|
||||
self._cancel.raise_if_cancelled()
|
||||
reasoning = _reasoning_delta(chunk)
|
||||
if reasoning and on_reasoning is not None:
|
||||
await on_reasoning(reasoning)
|
||||
# A few OpenAI-compatible models put their reasoning in the
|
||||
# ordinary content channel instead of ``reasoning_content``.
|
||||
# Split it from Markdown even when tags span tokens: callers may
|
||||
# display a live thought trace, but it never joins ``text``.
|
||||
delta, inline_reasoning = inline_think_filter.push_parts(_content_to_text(chunk))
|
||||
if inline_reasoning and on_reasoning is not None:
|
||||
await on_reasoning(inline_reasoning)
|
||||
if delta:
|
||||
text_parts.append(delta)
|
||||
if on_delta is not None:
|
||||
await on_delta(delta)
|
||||
chunk_usage = _extract_usage(chunk)
|
||||
if any(chunk_usage.values()):
|
||||
usage = chunk_usage
|
||||
for attempt in range(attempts):
|
||||
text_parts.clear()
|
||||
inline_think_filter = InlineThinkTagFilter()
|
||||
try:
|
||||
async for chunk in model.astream(
|
||||
to_langchain_messages(messages),
|
||||
config=isolated_config,
|
||||
**kwargs,
|
||||
):
|
||||
if self._cancel is not None:
|
||||
self._cancel.raise_if_cancelled()
|
||||
reasoning = _reasoning_delta(chunk)
|
||||
if reasoning and on_reasoning is not None:
|
||||
await on_reasoning(reasoning)
|
||||
# A few OpenAI-compatible models put their reasoning in the
|
||||
# ordinary content channel instead of ``reasoning_content``.
|
||||
# Split it from Markdown even when tags span tokens: callers may
|
||||
# display a live thought trace, but it never joins ``text``.
|
||||
delta, inline_reasoning = inline_think_filter.push_parts(_content_to_text(chunk))
|
||||
if inline_reasoning and on_reasoning is not None:
|
||||
await on_reasoning(inline_reasoning)
|
||||
if delta:
|
||||
text_parts.append(delta)
|
||||
if on_delta is not None:
|
||||
await on_delta(delta)
|
||||
chunk_usage = _extract_usage(chunk)
|
||||
if any(chunk_usage.values()):
|
||||
usage = chunk_usage
|
||||
last_error = None
|
||||
break
|
||||
except Exception as exc: # noqa: BLE001 - report writing waits like ordinary chat
|
||||
from deerflow.agents.deep_research.cancellation import ResearchCancelled
|
||||
|
||||
if isinstance(exc, (ResearchCancelled, asyncio.CancelledError)):
|
||||
raise
|
||||
last_error = exc
|
||||
# Only replay a clean connection; a half-written stream is
|
||||
# reset by the runner, not by appending a second attempt.
|
||||
if text_parts or attempt + 1 >= attempts:
|
||||
raise
|
||||
logger.warning(
|
||||
"deep_research llm: %s stream interrupted before the first token (%s); retrying once",
|
||||
operation,
|
||||
type(exc).__name__,
|
||||
)
|
||||
await asyncio.sleep(1.5)
|
||||
if last_error is not None:
|
||||
raise last_error
|
||||
finally:
|
||||
reset_parent_config()
|
||||
|
||||
@@ -470,7 +496,10 @@ class DeerFlowCompletionBackend:
|
||||
|
||||
if self._cancel is not None:
|
||||
self._cancel.raise_if_cancelled()
|
||||
text = _content_to_text(response)
|
||||
# ``ainvoke`` returns raw content, including the inline ``<think>``
|
||||
# block some OpenAI-compatible deployments write there. The streamed
|
||||
# path filters it out, so this retry path must not reintroduce it.
|
||||
text = strip_inline_think_tags(_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)
|
||||
|
||||
Reference in new issue
Block a user