feat: 完善策略优选项目工作流

This commit is contained in:
Administrator committed 2026-09-09 20:49:59 +08:00
1 parent a26fd32eba
commit 5d6627af50
45 files changed
+7235 -768

No files matched your search

@@ -40,11 +40,12 @@ from fastapi.responses import StreamingResponse
from pydantic import BaseModel, Field
from app.gateway.roundtable_model_fallback import is_llm_error_message, reason_text
from app.gateway.routers import agents as agents_router
from app.gateway.routers._strategy_selection_seed import STRATEGY_AGENT_ID_PREFIX
from app.gateway.routers.multi_agent import (
_ROUNDTABLE_THREAD_METADATA,
_auth_headers,
_create_thread,
_flatten_content,
_loopback_base,
_make_loopback_client,
_parse_last_messages,
@@ -54,6 +55,7 @@ from app.gateway.routers.multi_agent import (
)
from deerflow.agents.roundtable_orchestrator.delivery import visible_seat_text
from deerflow.tools.builtins.clarification_utils import resolve_allow_multiple
from deerflow.utils.stream_text import InlineThinkTagFilter
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api/parallel-agents", tags=["parallel-agents"])
@@ -72,6 +74,7 @@ def _has_substantive_content(content: str) -> bool:
visible = visible_seat_text(content)
return sum(character.isalnum() for character in visible) >= MIN_MEANINGFUL_CONTENT_CHARS
# 禁两类工具:①**写文件**(write_file / str_replace)—— 并行席位互相写文件会打架;
# ②**派活 / 脚本逃逸**(agent_orchestration / bash)—— 本面板里每个智能体只做自己那
# 份分析交付,绝不该扮演「总控」去调度别人。弱模型(deepseek 等)拿到带「总控 / 业务
@@ -80,10 +83,10 @@ def _has_substantive_content(content: str) -> bool:
# glob / bash 翻找派活脚本)。物理禁掉这两类即可根治。其余只读工具(ls / read_file /
# 检索)与 ask_clarification(协作卡片)保留:既不互相打架,又能正常出步骤条 / 要澄清。
_EXCLUDED_TOOLS: list[str] = [
"write_file", # 并行不互相写文件打架
"write_file", # 并行不互相写文件打架
"str_replace",
"agent_orchestration", # 子智能体不派活 / 不调度(防模仿总控)
"bash", # 禁跑脚本(防绕道脚本调内部接口模仿派活)
"bash", # 禁跑脚本(防绕道脚本调内部接口模仿派活)
]
@@ -119,25 +122,126 @@ async def issue_token(body: _TokenRequest) -> _TokenResponse:
# ── frame normalisation (mirrors multi-agent.ts) ─────────────────────────────
def _extract_ai_text(chunk: dict[str, Any]) -> str:
"""AI text delta from a ``messages-tuple`` chunk obj; "" when not AI text."""
def _extract_ai_stream_parts(chunk: dict[str, Any]) -> tuple[str, str]:
"""Return ``(answer, reasoning)`` deltas from one AI message chunk.
OpenAI-compatible providers do not agree on where displayable reasoning
lives. Most put it in ``additional_kwargs.reasoning_content`` (or the
legacy ``reasoning`` key), while a few return typed content blocks. Keep
that channel separate from the answer so the strategy workbench can show
progress without mixing internal trace text into the generated strategy.
Literal ``<think>`` blocks in the normal text channel are handled by the
per-agent :class:`InlineThinkTagFilter` in ``_consume_agent`` because tags
may be split across arbitrary SSE chunks.
"""
type_ = str(chunk.get("type") or "").lower()
if type_ not in ("ai", "aimessagechunk", "aimessage"):
return ""
return "", ""
content = chunk.get("content")
answer_parts: list[str] = []
reasoning_parts: list[str] = []
if isinstance(content, str):
return content
if isinstance(content, list):
out: list[str] = []
answer_parts.append(content)
elif isinstance(content, list):
for item in content:
if isinstance(item, str):
out.append(item)
answer_parts.append(item)
elif isinstance(item, dict):
t = item.get("text")
if isinstance(t, str):
out.append(t)
return "".join(out)
return ""
block_type = str(item.get("type") or "").lower()
value = item.get("thinking")
if not isinstance(value, str):
value = item.get("text")
if not isinstance(value, str):
value = item.get("content")
if not isinstance(value, str):
continue
if block_type in {
"thinking",
"thinking_delta",
"reasoning",
"reasoning_delta",
}:
reasoning_parts.append(value)
else:
answer_parts.append(value)
additional_kwargs = chunk.get("additional_kwargs")
direct_reasoning = chunk.get("reasoning_content") or chunk.get("reasoning")
nested_reasoning: object = None
if isinstance(additional_kwargs, dict):
nested_reasoning = additional_kwargs.get("reasoning_content") or additional_kwargs.get("reasoning")
raw_reasoning = nested_reasoning or direct_reasoning
if isinstance(raw_reasoning, str):
reasoning_parts.append(raw_reasoning)
return "".join(answer_parts), "".join(reasoning_parts)
def _extract_ai_text(chunk: dict[str, Any]) -> str:
"""Compatibility helper returning only the ordinary AI text delta."""
return _extract_ai_stream_parts(chunk)[0]
def _split_final_inline_reasoning(content: str) -> tuple[str, str]:
"""Split complete inline thinking, including template-injected close tags.
Some reasoning chat templates insert ``<think>`` before generation, so the
provider returns only ``reasoning</think>answer``. The streaming filter
cannot classify that prefix before the closing marker arrives, but the
terminal values frame can. Returning the recovered trace here also lets
the client correct any transient streaming classification.
"""
if not content:
return "", ""
working = content
prefix_reasoning = ""
lowered = working.lower()
first_open = lowered.find("<think>")
first_close = lowered.find("</think>")
if first_close >= 0 and (first_open < 0 or first_close < first_open):
prefix_reasoning = working[:first_close]
working = working[first_close + len("</think>") :]
text_filter = InlineThinkTagFilter()
visible, reasoning = text_filter.push_parts(working)
visible_tail, reasoning_tail = text_filter.finish_parts()
final_reasoning = "".join(part for part in (prefix_reasoning, reasoning, reasoning_tail) if part)
# ``visible_seat_text`` remains the canonical complete-output cleanup for
# malformed/multiple tags; the filter-derived visible value is only a
# fallback for an otherwise empty canonical result.
final_visible = visible_seat_text(content) or f"{visible}{visible_tail}".strip()
return final_visible, final_reasoning.strip()
def _should_probe_implicit_reasoning(
request: Request,
agent_id: str,
requested_model: str,
) -> bool:
"""Enable close-only probing only for a thinking-capable effective model.
Ordinary models must stream untagged answer chunks immediately. The
stricter prelude buffer is reserved for providers that advertise thinking
support and may use templates where ``<think>`` is injected outside the
returned content while ``</think>`` remains in-band.
"""
config = getattr(request.app.state, "config", None)
models = list(getattr(config, "models", None) or [])
effective_name = requested_model.strip()
if not effective_name:
try:
effective_name = str(agents_router._load_config_extras(agent_id).get("model") or "").strip()
except Exception:
effective_name = ""
if not effective_name and models:
effective_name = str(getattr(models[0], "name", "") or "")
selected = next(
(entry for entry in models if str(getattr(entry, "name", "") or "") == effective_name),
None,
)
return bool(selected and getattr(selected, "supports_thinking", False))
def _ingest_tool_chunks(chunks: Any, acc: dict[int, dict[str, Any]]) -> None:
@@ -248,7 +352,9 @@ async def _run_one_agent(
failure_reason = is_llm_error_message(last_ai)
if failure_reason:
raise RuntimeError(f"模型调用失败:{reason_text(failure_reason)}")
content = _flatten_content(last_ai.get("content", ""))
answer_channel, structured_reasoning = _extract_ai_stream_parts(last_ai)
content, inline_reasoning = _split_final_inline_reasoning(answer_channel)
reasoning = "\n\n".join(part.strip() for part in (structured_reasoning, inline_reasoning) if part.strip())
# 收集**所有** AI 消息的 tool_calls(全量 args,按顺序)—— 用于在收口时补发所有步骤
# (含 ls / skill_list 这类无参/中间步骤),保证步骤条不丢步、且 args 完整。
final_tool_calls: list[dict[str, Any]] = []
@@ -263,6 +369,7 @@ async def _run_one_agent(
yield {
"type": "clarification",
"content": content,
"reasoning": reasoning,
"clarification": clarification,
"tool_calls": final_tool_calls,
}
@@ -272,6 +379,7 @@ async def _run_one_agent(
yield {
"status": f"子智能体 {agent_id} 完成",
"content": content,
"reasoning": reasoning,
"agent_name": agent_id,
"tool_calls": final_tool_calls,
}
@@ -283,10 +391,7 @@ def _build_task_message(task_name: str, brief: str) -> str:
parts.append(f"任务名称:{task_name.strip()}")
if brief.strip():
parts.append(f"任务简介:\n{brief.strip()}")
parts.append(
"请你从你的专业职责出发,针对以上任务给出你的分析与交付。"
"本轮为**并行**研讨:请把你的产出**直接写在回复正文里**(本轮禁止写文件)。"
)
parts.append("请你从你的专业职责出发,针对以上任务给出你的分析与交付。本轮为**并行**研讨:请把你的产出**直接写在回复正文里**(本轮禁止写文件)。")
# 角色边界护栏:本面板里每个智能体都是**执行席位**,不是总控 / 指挥。即便任务里带有
# 「总控 / 业务链条 / 调度 / 派活」字样,也只完成属于自己的那份交付,严禁模仿总控派活。
parts.append(
@@ -329,6 +434,41 @@ async def run_parallel_agents_stream(request: Request, payload: dict = Body(...)
status_code=400,
detail=f"并行智能体数量上限 {MAX_PARALLEL_AGENTS}(当前 {len(agent_ids)})",
)
role_store = getattr(request.app.state, "strategy_agent_role_store", None)
agent_store = getattr(request.app.state, "agent_store", None)
strategy_ids = [agent_id for agent_id in agent_ids if agent_id.casefold().startswith(STRATEGY_AGENT_ID_PREFIX.casefold())]
if strategy_ids and role_store is not None and agent_store is not None:
# Every worker owns an independent memory store. Synchronize from
# durable folders/sidecars immediately before execution so a request
# routed to another worker cannot see random 404s or revived angles.
from app.gateway.routers.strategy_agents import _ensure_registry
await _ensure_registry(agent_store, role_store)
for agent_id in agent_ids:
if not agent_id.casefold().startswith(STRATEGY_AGENT_ID_PREFIX.casefold()):
continue
if role_store is None or agent_store is None:
raise HTTPException(
status_code=503,
detail="策略智能体分类或配置服务不可用",
)
role = await role_store.get_role(agent_id)
if role is None:
raise HTTPException(
status_code=400,
detail=f"未注册的策略智能体:{agent_id}",
)
if not bool(role.get("enabled")):
raise HTTPException(
status_code=409,
detail=f"策略智能体已停用:{agent_id}",
)
record = await agent_store.get_any(agent_id)
if record is None or str(record.get("id") or "") != agent_id or record.get("user_id") is not None:
raise HTTPException(
status_code=409,
detail=f"策略智能体必须由系统全局持有:{agent_id}",
)
if not task_name and not brief:
raise HTTPException(status_code=400, detail="任务名称与简介至少填写其一")
@@ -346,8 +486,54 @@ async def run_parallel_agents_stream(request: Request, payload: dict = Body(...)
emitted_tools: set[str] = set() # 已发过 step 的 tool_call id(去重)
tool_acc: dict[int, dict[str, Any]] = {} # tool_call_chunks 累积
buffer: list[str] = []
raw_answer_buffer: list[str] = []
probe_implicit_close = await asyncio.to_thread(
_should_probe_implicit_reasoning,
request,
agent_id,
model_name,
)
inline_thinking = InlineThinkTagFilter(allow_implicit_close=probe_implicit_close)
reasoning_emitted = False
done_sent = False
async def _emit_text_parts(text: str, provider_reasoning: str = "") -> None:
nonlocal reasoning_emitted
if text:
raw_answer_buffer.append(text)
visible_prelude = inline_thinking.confirm_visible_channel() if provider_reasoning else ""
visible, inline_reasoning = inline_thinking.push_parts(text)
reasoning = f"{provider_reasoning}{inline_reasoning}"
if reasoning:
reasoning_emitted = True
await queue.put(
{
"type": "reasoning_delta",
"agent_id": agent_id,
"text": reasoning,
}
)
confirmed_visible = f"{visible_prelude}{visible}"
if confirmed_visible:
buffer.append(confirmed_visible)
await queue.put({"type": "delta", "agent_id": agent_id, "text": confirmed_visible})
async def _flush_text_filter() -> None:
nonlocal reasoning_emitted
visible, reasoning = inline_thinking.finish_parts()
if reasoning:
reasoning_emitted = True
await queue.put(
{
"type": "reasoning_delta",
"agent_id": agent_id,
"text": reasoning,
}
)
if visible:
buffer.append(visible)
await queue.put({"type": "delta", "agent_id": agent_id, "text": visible})
async def _emit_tool(tc: Any, allow_empty: bool = False) -> None:
"""一个 tool_call(含完整 args)→ 一条 step 事件;按 id 去重。
@@ -369,9 +555,7 @@ async def run_parallel_agents_stream(request: Request, payload: dict = Body(...)
if key_id in emitted_tools:
return
emitted_tools.add(key_id)
await queue.put(
{"type": "step", "agent_id": agent_id, "key": f"tool_calling:{name}", "label": name, "args": args}
)
await queue.put({"type": "step", "agent_id": agent_id, "key": f"tool_calling:{name}", "label": name, "args": args})
try:
thread_id = await _create_thread(client, base, headers, metadata=_ROUNDTABLE_THREAD_METADATA)
@@ -381,22 +565,32 @@ async def run_parallel_agents_stream(request: Request, payload: dict = Body(...)
async for chunk in gen:
if isinstance(chunk, dict):
# 终态:先用最终 tool_calls(全量 args)补发漏掉的 step,再发收口/澄清。
await _flush_text_filter()
for tc in chunk.get("tool_calls") or []:
await _emit_tool(tc, allow_empty=True)
final_content = visible_seat_text(str(chunk.get("content") or ""))
final_reasoning = str(chunk.get("reasoning") or "")
if final_reasoning and not reasoning_emitted:
reasoning_emitted = True
await queue.put(
{
"type": "reasoning_delta",
"agent_id": agent_id,
"text": final_reasoning,
}
)
if chunk.get("type") == "clarification":
clar = chunk.get("clarification") or {}
await queue.put(
{
"type": "agent_clarification",
"agent_id": agent_id,
"content": str(chunk.get("content") or ""),
"content": final_content,
**clar,
}
)
else:
await queue.put(
{"type": "agent_done", "agent_id": agent_id, "content": str(chunk.get("content") or "")}
)
await queue.put({"type": "agent_done", "agent_id": agent_id, "content": final_content})
done_sent = True
continue
line = chunk.strip() if isinstance(chunk, str) else ""
@@ -415,10 +609,9 @@ async def run_parallel_agents_stream(request: Request, payload: dict = Body(...)
if last_event == "messages" and isinstance(parsed, list) and parsed:
obj = parsed[0]
if isinstance(obj, dict):
text = _extract_ai_text(obj)
if text:
buffer.append(text)
await queue.put({"type": "delta", "agent_id": agent_id, "text": text})
text, reasoning = _extract_ai_stream_parts(obj)
if text or reasoning:
await _emit_text_parts(text, reasoning)
# 完整 tool_calls(部分模型直接给)+ 累积 chunks 后的合成调用,都尝试发 step。
for tc in obj.get("tool_calls") or []:
await _emit_tool(tc)
@@ -427,18 +620,24 @@ async def run_parallel_agents_stream(request: Request, payload: dict = Body(...)
await _emit_tool(tc)
if not done_sent:
# 流被抢占/截断,没收到终态帧 —— 用已累积正文兜底收口本席位。
buffered_content = "".join(buffer)
failure_reason = is_llm_error_message(
{"type": "ai", "content": buffered_content}
)
await _flush_text_filter()
buffered_content, recovered_reasoning = _split_final_inline_reasoning("".join(raw_answer_buffer))
if not buffered_content:
buffered_content = "".join(buffer)
if recovered_reasoning and not reasoning_emitted:
reasoning_emitted = True
await queue.put(
{
"type": "reasoning_delta",
"agent_id": agent_id,
"text": recovered_reasoning,
}
)
failure_reason = is_llm_error_message({"type": "ai", "content": buffered_content})
if failure_reason:
raise RuntimeError(
f"模型调用失败:{reason_text(failure_reason)}"
)
raise RuntimeError(f"模型调用失败:{reason_text(failure_reason)}")
if not _has_substantive_content(buffered_content):
raise RuntimeError(
"智能体未生成有效正文,请重试或更换全局模型"
)
raise RuntimeError("智能体未生成有效正文,请重试或更换全局模型")
await queue.put(
{
"type": "agent_done",