sync update from LAN repo (source ji @ 85078a31)
This commit is contained in:
1 parent
5d6627af50
commit
e602d88695
864 files changed
+99150
-6346
No files matched your search
@@ -1,10 +1,11 @@
|
||||
"""Fulfill a durable plan_request: catalog → select → assemble → validate → persist.
|
||||
|
||||
Does not start a run. Rule-based until the BE-7 model adapter is wired.
|
||||
Does not start a run. P1.2 起优先由规划师 LLM 生成定制策略,模板为回退。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
@@ -12,8 +13,9 @@ from app.report_collaboration.contracts.requirements import ReportRequirementSna
|
||||
from app.report_collaboration.planning.assembler import PlanAssembler
|
||||
from app.report_collaboration.planning.catalog import project_agent_catalog
|
||||
from app.report_collaboration.planning.controller import PlanController
|
||||
from app.report_collaboration.planning.planner_agent import LlmFactory
|
||||
from app.report_collaboration.planning.validator import apply_validation, validate_assembled_plan
|
||||
from app.report_collaboration.requirement.defaults import COORDINATOR_NAME, DEFAULT_TITLE
|
||||
from app.report_collaboration.requirement.defaults import DEFAULT_TITLE
|
||||
from deerflow.config.app_config import get_app_config
|
||||
from deerflow.config.report_collaboration_config import ReportCollaborationConfig
|
||||
from deerflow.persistence.report_collaboration import (
|
||||
@@ -22,6 +24,8 @@ from deerflow.persistence.report_collaboration import (
|
||||
ReportCollaborationValidationError,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_BUSY = frozenset({"running", "awaiting_input", "reviewing", "completed", "failed", "cancelled"})
|
||||
|
||||
|
||||
@@ -51,17 +55,31 @@ class PlanProposalService:
|
||||
*,
|
||||
agent_store: Any | None = None,
|
||||
config: ReportCollaborationConfig | None = None,
|
||||
llm_factory: LlmFactory | None = None,
|
||||
) -> None:
|
||||
self._store = store
|
||||
self._agent_store = agent_store
|
||||
self._config = config
|
||||
self._controller = PlanController()
|
||||
self._assembler = PlanAssembler()
|
||||
self._llm_factory = llm_factory
|
||||
self._llm: Any | None = None
|
||||
self._llm_tried = False
|
||||
|
||||
def _cfg(self) -> ReportCollaborationConfig:
|
||||
return self._config or get_app_config().report_collaboration
|
||||
|
||||
async def fulfill(self, session_id: str, *, user_id: str, command_id: str, idempotency_key: str) -> list[dict[str, Any]]:
|
||||
async def fulfill(
|
||||
self,
|
||||
session_id: str,
|
||||
*,
|
||||
user_id: str,
|
||||
command_id: str,
|
||||
idempotency_key: str,
|
||||
adjustment_text: str | None = None,
|
||||
precomputed_design: tuple[list[Any], str] | None = None,
|
||||
planner_reasoning: str | None = None,
|
||||
planner_steps: list[dict[str, Any]] | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
record = await self._store.get_session_record(session_id)
|
||||
if record is None:
|
||||
raise ReportCollaborationValidationError("NOT_FOUND", "会话不存在")
|
||||
@@ -71,12 +89,28 @@ class PlanProposalService:
|
||||
raise ReportCollaborationConflictError("SESSION_BUSY", "当前会话状态不允许生成方案", current_revision=int(record.get("requirement_revision") or 0))
|
||||
|
||||
try:
|
||||
return await self._build(session_id, user_id=user_id, command_id=command_id, idempotency_key=idempotency_key, record=record)
|
||||
requirement_state = record.get("requirement_json")
|
||||
if not isinstance(requirement_state, dict) or requirement_state.get("ready_to_plan") is not True:
|
||||
raise ReportCollaborationValidationError(
|
||||
"REQUIREMENT_NOT_READY",
|
||||
"规划师尚未确认完整意图,请先继续回答澄清问题",
|
||||
)
|
||||
return await self._build(
|
||||
session_id,
|
||||
user_id=user_id,
|
||||
command_id=command_id,
|
||||
idempotency_key=idempotency_key,
|
||||
record=record,
|
||||
adjustment_text=adjustment_text,
|
||||
precomputed_design=precomputed_design,
|
||||
planner_reasoning=planner_reasoning,
|
||||
planner_steps=planner_steps,
|
||||
)
|
||||
except Exception as exc:
|
||||
await self._fail_command(session_id, command_id, exc)
|
||||
await self.fail_command(session_id, command_id, exc)
|
||||
raise
|
||||
|
||||
async def _fail_command(self, session_id: str, command_id: str, exc: Exception) -> None:
|
||||
async def fail_command(self, session_id: str, command_id: str, exc: Exception) -> None:
|
||||
try:
|
||||
await self._store.update_command_status(command_id, status="failed", error=str(exc)[:500])
|
||||
record = await self._store.get_session_record(session_id)
|
||||
@@ -106,16 +140,60 @@ class PlanProposalService:
|
||||
command_id: str,
|
||||
idempotency_key: str,
|
||||
record: dict[str, Any],
|
||||
adjustment_text: str | None,
|
||||
precomputed_design: tuple[list[Any], str] | None,
|
||||
planner_reasoning: str | None,
|
||||
planner_steps: list[dict[str, Any]] | None,
|
||||
) -> list[dict[str, Any]]:
|
||||
requirement = requirement_from_session(record)
|
||||
cfg = self._cfg()
|
||||
catalog = await project_agent_catalog(user_id=user_id, config=cfg, agent_store=self._agent_store)
|
||||
strategies = self._controller.propose(
|
||||
requirement,
|
||||
max_parallel_tasks=cfg.max_parallel_tasks,
|
||||
max_team_members=cfg.max_team_members,
|
||||
)
|
||||
group_id = f"rpg_{uuid4().hex[:12]}"
|
||||
|
||||
# 规划师智能体带着完整对话、当前候选和真实智能体目录设计候选工作流。
|
||||
# 确定性策略仅用于补足候选数量,最终仍会绑定真实目录中的智能体。
|
||||
from app.report_collaboration.planning.planner_agent import PlannerAgent
|
||||
|
||||
history = [
|
||||
{"role": "human" if item.get("role") == "human" else "ai", "content": str(item.get("content") or "")}
|
||||
for item in await self._store.list_messages(session_id)
|
||||
]
|
||||
current_plans = [
|
||||
item
|
||||
for item in await self._store.list_plans(session_id)
|
||||
if item.get("status") in {"proposed", "selected"}
|
||||
]
|
||||
designed = precomputed_design
|
||||
if designed is None:
|
||||
designed = await PlannerAgent(self.planner_llm()).design_workflows(
|
||||
requirement=requirement,
|
||||
history=history,
|
||||
catalog=catalog,
|
||||
current_plans=current_plans,
|
||||
adjustment_text=adjustment_text,
|
||||
max_team_members=cfg.max_team_members,
|
||||
)
|
||||
if designed is None:
|
||||
raise ReportCollaborationValidationError(
|
||||
"PLANNER_UNAVAILABLE", "规划师智能体未能生成有效工作流,请重试或换个说法再试"
|
||||
)
|
||||
strategies: list[Any] = list(designed[0])
|
||||
planner_reply: str | None = designed[1]
|
||||
if len(strategies) < 2:
|
||||
existing = {item.strategy for item in strategies}
|
||||
for fallback in PlanController().propose(
|
||||
requirement,
|
||||
max_parallel_tasks=cfg.max_parallel_tasks,
|
||||
max_team_members=cfg.max_team_members,
|
||||
):
|
||||
if fallback.strategy in existing:
|
||||
continue
|
||||
fallback.recommended = not strategies
|
||||
strategies.append(fallback)
|
||||
existing.add(fallback.strategy)
|
||||
if len(strategies) >= 3:
|
||||
break
|
||||
|
||||
assembled_list = [
|
||||
self._assembler.assemble(
|
||||
strategy,
|
||||
@@ -140,13 +218,50 @@ class PlanProposalService:
|
||||
for candidate in persisted:
|
||||
saved.append(await self._store.insert_plan(session_id, candidate.model_dump()))
|
||||
await self._store.update_command_status(command_id, status="completed")
|
||||
content = planner_reply or "候选工作流已规划完成,你可以选择一个方案,也可以继续通过对话调整智能体结构。"
|
||||
await self._store.create_message(
|
||||
session_id,
|
||||
role="ai",
|
||||
content="已根据当前需求生成 3 个候选协作方案。请选择其中一个后再开始执行;选择方案不会自动开跑。",
|
||||
content=content,
|
||||
idempotency_key=f"{idempotency_key}:plans",
|
||||
name=COORDINATOR_NAME,
|
||||
metadata={"plan_request_command_id": command_id, "proposal_group_id": group_id},
|
||||
name="工作流规划师",
|
||||
metadata={
|
||||
"plan_request_command_id": command_id,
|
||||
"proposal_group_id": group_id,
|
||||
"plan_source": "planner_agent",
|
||||
**({"reasoning_content": planner_reasoning} if planner_reasoning else {}),
|
||||
},
|
||||
tool_calls=planner_steps or [],
|
||||
completed=True,
|
||||
)
|
||||
return saved
|
||||
|
||||
def planner_llm(self) -> Any:
|
||||
"""规划师模型:注入优先(测试);否则按 planner_llm_enabled 懒构建。不可用即抛错。"""
|
||||
|
||||
if self._llm_factory is not None:
|
||||
llm = self._llm_factory()
|
||||
if llm is None:
|
||||
raise ReportCollaborationValidationError("PLANNER_UNAVAILABLE", "规划师智能体不可用,请检查模型配置")
|
||||
return llm
|
||||
try:
|
||||
if not (self._config or get_app_config().report_collaboration).planner_llm_enabled:
|
||||
raise ReportCollaborationValidationError(
|
||||
"PLANNER_UNAVAILABLE", "规划师智能体未启用(report_collaboration.planner_llm_enabled)"
|
||||
)
|
||||
except ReportCollaborationValidationError:
|
||||
raise
|
||||
except Exception as exc:
|
||||
raise ReportCollaborationValidationError("PLANNER_UNAVAILABLE", f"规划师智能体配置不可读:{exc}") from None
|
||||
if not self._llm_tried:
|
||||
self._llm_tried = True
|
||||
try:
|
||||
from app.report_collaboration.planning.planner_agent import build_planner_llm
|
||||
|
||||
self._llm = build_planner_llm()
|
||||
except Exception as exc:
|
||||
logger.warning("rc planner agent llm build failed: %s", exc)
|
||||
self._llm = None
|
||||
if self._llm is None:
|
||||
raise ReportCollaborationValidationError("PLANNER_UNAVAILABLE", "规划师智能体不可用,请检查模型配置")
|
||||
return self._llm
|
||||
Reference in new issue
Block a user