sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)

This commit is contained in:
gozeros committed 2026-10-21 14:51:31 +08:00
1 parent 4f7f07504a
commit 8bc8b0e082
1651 files changed
+278599 -11416

No files matched your search

@@ -31,18 +31,22 @@ from __future__ import annotations
import asyncio
import json
import logging
from typing import Any, AsyncIterator
import time
from collections.abc import AsyncIterator
from typing import Any
import httpx
from fastapi import APIRouter, Body, HTTPException, Request
from fastapi import APIRouter, Body, HTTPException, Query, Request, Response
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,
@@ -50,7 +54,9 @@ from app.gateway.routers.multi_agent import (
_stream_upstream,
_validate_agent_id,
)
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"])
@@ -58,6 +64,64 @@ router = APIRouter(prefix="/api/parallel-agents", tags=["parallel-agents"])
# Soft cap on parallel degree — protects remote model concurrency + sandbox.
MAX_PARALLEL_AGENTS = 8
# Strategy roles are expected to return a substantive task/strategy/evaluation,
# not merely punctuation or a broken provider placeholder. Counting Unicode
# alphanumerics keeps the check language-neutral while rejecting outputs such as
# a long run of "!!!", which otherwise look non-empty to the UI.
MIN_MEANINGFUL_CONTENT_CHARS = 20
# Long-running read-only skills can be silent while they wait on an upstream
# service. Emit user-visible progress often enough to keep proxies and the UI
# alive, and fail one seat instead of leaving the whole strategy page pending
# forever when the upstream stream has truly stalled.
PROGRESS_HEARTBEAT_SECONDS = 5.0
AGENT_STREAM_IDLE_TIMEOUT_SECONDS = 180.0
_TOOL_PROGRESS_LABELS: dict[str, str] = {
"search_skills": "正在搜索匹配技能",
"skill_list": "正在查找可用技能",
"skill_view": "正在读取技能说明",
"llmwiki_search": "正在检索知识库",
"web_search": "正在检索外部信息",
"web_fetch": "正在读取外部资料",
"knowledge_search": "正在检索知识资料",
"read_file": "正在读取资料",
"read_multiple_files": "正在读取多份资料",
"ls": "正在查看资料目录",
"glob": "正在查找相关资料",
"grep": "正在筛选相关内容",
}
def _tool_progress_message(name: str, args: dict[str, Any]) -> str:
base = _TOOL_PROGRESS_LABELS.get(name, f"正在执行 {name}" if name else "正在执行策略分析步骤")
detail = next(
(
str(value).strip()
for value in (
args.get("query"),
args.get("skill_name"),
args.get("skill"),
args.get("name"),
args.get("path"),
)
if value is not None and str(value).strip()
),
"",
)
if not detail:
return base
compact = " ".join(detail.split())
if len(compact) > 64:
compact = f"{compact[:64]}…"
return f"{base}:{compact}"
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 等)拿到带「总控 / 业务
@@ -66,18 +130,14 @@ MAX_PARALLEL_AGENTS = 8
# glob / bash 翻找派活脚本)。物理禁掉这两类即可根治。其余只读工具(ls / read_file /
# 检索)与 ask_clarification(协作卡片)保留:既不互相打架,又能正常出步骤条 / 要澄清。
_EXCLUDED_TOOLS: list[str] = [
"write_file", # 并行不互相写文件打架
"write_file", # 并行不互相写文件打架
"str_replace",
"agent_orchestration", # 子智能体不派活 / 不调度(防模仿总控)
"bash", # 禁跑脚本(防绕道脚本调内部接口模仿派活)
"bash", # 禁跑脚本(防绕道脚本调内部接口模仿派活)
]
# ── 专用「绕过常规登录」取 token ──────────────────────────────────────────────
# 内网部署对标准 /api/v1/auth/login/username 有额外操作限制(口令门 / 网关裁剪等),
# 嵌入方拿不到 token。这里给并行多智能体面板单开一个**公共**端点:传用户名直接签发 token
# (账号不存在则自动注册,与 username 登录同一套逻辑)。已在 auth_middleware / csrf_middleware
# 的白名单里放行(公共 + CSRF 豁免)。⚠️ 这是有意的鉴权旁路,仅供内网部署的本功能使用。
# ── 并行面板兼容登录:复用单点开关、密码、限速与封禁校验 ────────────────────
class _TokenRequest(BaseModel):
@@ -91,39 +151,137 @@ class _TokenResponse(BaseModel):
@router.post("/auth/token", response_model=_TokenResponse)
async def issue_token(body: _TokenRequest) -> _TokenResponse:
"""传用户名 → 直接签发 access_token(绕过常规登录,公共端点)。"""
# 懒加载,避免与 auth 路由的模块级循环依赖。
from app.gateway.auth import create_access_token
from app.gateway.routers.auth import _get_or_create_user_by_username
async def issue_token(body: _TokenRequest, request: Request, response: Response, password: str | None = Query(default=None)) -> _TokenResponse:
"""Keep the panel response contract while enforcing standard login policy."""
from app.gateway.routers.auth import UsernameLoginRequest, login_by_username
user, _created = await _get_or_create_user_by_username(body.username.strip())
token = create_access_token(str(user.id), token_version=user.token_version)
return _TokenResponse(access_token=token, token_type="bearer", user_id=str(user.id))
result = await login_by_username(request, response, UsernameLoginRequest(username=body.username.strip()), password=password)
return _TokenResponse(access_token=result.access_token, token_type="bearer", user_id=result.user_id)
# ── 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:
@@ -231,7 +389,12 @@ async def _run_one_agent(
if isinstance(msg, dict) and (msg.get("type") or "").lower() in ("ai", "aimessage", "aimessagechunk"):
last_ai = msg
break
content = _flatten_content(last_ai.get("content", ""))
failure_reason = is_llm_error_message(last_ai)
if failure_reason:
raise RuntimeError(f"模型调用失败:{reason_text(failure_reason)}")
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]] = []
@@ -246,13 +409,17 @@ async def _run_one_agent(
yield {
"type": "clarification",
"content": content,
"reasoning": reasoning,
"clarification": clarification,
"tool_calls": final_tool_calls,
}
else:
if not _has_substantive_content(content):
raise RuntimeError("智能体未生成有效正文,请重试或更换全局模型")
yield {
"status": f"子智能体 {agent_id} 完成",
"content": content,
"reasoning": reasoning,
"agent_name": agent_id,
"tool_calls": final_tool_calls,
}
@@ -264,10 +431,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(
@@ -310,6 +474,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="任务名称与简介至少填写其一")
@@ -327,7 +526,74 @@ 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
started_at = time.monotonic()
progress_message = "智能体正在分析任务"
async def _emit_progress_heartbeats() -> None:
while not done_sent:
await asyncio.sleep(PROGRESS_HEARTBEAT_SECONDS)
# A terminal event may have been queued while this task was
# sleeping. Never let a late heartbeat move a completed card
# back into the running state on the frontend.
if done_sent:
return
await queue.put(
{
"type": "agent_progress",
"agent_id": agent_id,
"message": progress_message,
"elapsed_seconds": max(0, round(time.monotonic() - started_at)),
}
)
async def _emit_text_parts(text: str, provider_reasoning: str = "") -> None:
nonlocal progress_message, 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:
progress_message = "正在分析并组织策略"
reasoning_emitted = True
await queue.put(
{
"type": "reasoning_delta",
"agent_id": agent_id,
"text": reasoning,
}
)
confirmed_visible = f"{visible_prelude}{visible}"
if confirmed_visible:
progress_message = "正在生成策略正文"
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 去重。
@@ -336,6 +602,7 @@ async def run_parallel_agents_stream(request: Request, payload: dict = Body(...)
空 ``{}`` 占位、真实 args 走 tool_call_chunks 后到,过早发会把空 args 钉死。等累积出
完整 args(synthetic)或终态全量 tool_calls(``allow_empty=True``)再发。
"""
nonlocal progress_message
if not isinstance(tc, dict):
return
name = tc.get("name")
@@ -350,35 +617,56 @@ 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}
)
progress_message = _tool_progress_message(name, args)
await queue.put({"type": "step", "agent_id": agent_id, "key": f"tool_calling:{name}", "label": name, "args": args})
heartbeat_task = asyncio.create_task(_emit_progress_heartbeats())
try:
thread_id = await _create_thread(client, base, headers, metadata=_ROUNDTABLE_THREAD_METADATA)
gen = _run_one_agent( # 只禁文件写入,其余工具放开(保留步骤条)
client, base, headers, agent_id, thread_id, new_message, model_name
)
async for chunk in gen:
).__aiter__()
while True:
try:
chunk = await asyncio.wait_for(
anext(gen),
timeout=AGENT_STREAM_IDLE_TIMEOUT_SECONDS,
)
except StopAsyncIteration:
break
except TimeoutError as exc:
await gen.aclose()
raise RuntimeError(f"智能体连续 {int(AGENT_STREAM_IDLE_TIMEOUT_SECONDS)} 秒没有返回进度,已停止本次执行,请重试") from exc
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":
done_sent = True
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 "")}
)
done_sent = True
done_sent = True
await queue.put({"type": "agent_done", "agent_id": agent_id, "content": final_content})
continue
line = chunk.strip() if isinstance(chunk, str) else ""
if line.startswith("event:"):
@@ -396,10 +684,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)
@@ -408,13 +695,38 @@ async def run_parallel_agents_stream(request: Request, payload: dict = Body(...)
await _emit_tool(tc)
if not done_sent:
# 流被抢占/截断,没收到终态帧 —— 用已累积正文兜底收口本席位。
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)}")
if not _has_substantive_content(buffered_content):
raise RuntimeError("智能体未生成有效正文,请重试或更换全局模型")
done_sent = True
await queue.put(
{"type": "agent_done", "agent_id": agent_id, "content": "".join(buffer)}
{
"type": "agent_done",
"agent_id": agent_id,
"content": buffered_content,
}
)
except Exception as exc: # noqa: BLE001 — 单个 agent 失败不拖垮其它
logger.warning("[parallel-agents] %s failed: %r", agent_id, exc)
await queue.put({"type": "agent_error", "agent_id": agent_id, "message": str(exc)})
finally:
heartbeat_task.cancel()
await asyncio.gather(heartbeat_task, return_exceptions=True)
await queue.put({"type": "__agent_end__", "agent_id": agent_id})
async def stream_generator() -> AsyncIterator[str]: