feat(roundtable): 第三步「数据入库」由结构化输出智能体自行调用技能
- 入库改为让 roundtable-structure 走完整 agent 运行时(ingest run policy) 自行挑选/读取/调用入库技能并传入 taskId,不再前端代调演示 - 新增 /api/flow-extract/ingest-eligibility(智能体判断当前业务场景是否有 合适入库技能,决定入库按钮是否显示)+ /api/multi-agent/structure/init - 入库前判断是否已生成流程图:无则发起「是否先生成流程图」用户协助 - 「知识库入库」更名为「数据入库」 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
a85d2480b4
commit
03c155f8a7
10 files changed
+507
-80
No files matched your search
@@ -35,8 +35,10 @@ from app.gateway.routers._roundtable_seed import (
|
||||
STRUCTURE_AGENT_ID,
|
||||
ensure_roundtable_functional_agents,
|
||||
)
|
||||
from deerflow.config import get_app_config
|
||||
from deerflow.config.agents_config import load_agent_config, load_agent_soul
|
||||
from deerflow.models.factory import create_chat_model
|
||||
from deerflow.skills.storage import get_or_new_skill_storage
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter(prefix="/api/flow-extract", tags=["flow-extract"])
|
||||
@@ -762,3 +764,114 @@ async def classify_ingest_intent(request: Request, payload: IngestIntentRequest
|
||||
except Exception: # noqa: BLE001 — 分类失败不致命,回退协助让用户自己定
|
||||
logger.warning("ingest intent classify failed", exc_info=True)
|
||||
return {"intent": "unclear", "reason": "意图识别失败"}
|
||||
|
||||
|
||||
# ── 入库适用性判定(结构化抽取智能体「自己看技能是否适合本场景」)──────────────────
|
||||
#
|
||||
# 需求:入库不再由前端硬编码「有没有含『入库』关键字的技能」判定,而是让**结构化输出
|
||||
# 智能体**读它**挂载的入库技能**(roundtable-structure 的 config.yaml ``skills``)+ 当前
|
||||
# 业务场景(如 rwfx-A=六步法),**自行判断**是否有适合本场景的入库技能。判定为可入库才
|
||||
# 由前端显示「入库」按钮。真正调用技能在 ``POST /api/multi-agent/run/stream``(role=ingest)。
|
||||
|
||||
|
||||
class IngestEligibilityRequest(BaseModel):
|
||||
"""入库适用性判定入参:业务场景 + taskId,供智能体对照挂载的入库技能判断。"""
|
||||
|
||||
task_id: str | None = Field(default=None)
|
||||
business_code: str | None = Field(default=None, description="业务码,如 6BF/3Q/7BF 或 rwfx-A")
|
||||
chain_name: str | None = Field(default=None, description="业务链条名称")
|
||||
coordinator_prompt: str | None = Field(default=None, description="业务链总控编排提示(含场景线索,如六步法)")
|
||||
report_title: str | None = Field(default=None)
|
||||
model: str | None = Field(default=None)
|
||||
|
||||
|
||||
def _structure_skill_candidates() -> list[dict[str, str]]:
|
||||
"""读取挂在 roundtable-structure 上、且**已启用**的技能(候选入库技能)。
|
||||
|
||||
返回 ``[{name, description}]``。失败 / 未配置 → 空列表(= 不可入库)。
|
||||
技能元数据取自磁盘 SKILL.md(name + description),足够智能体判断适用性。
|
||||
"""
|
||||
try:
|
||||
cfg = load_agent_config(STRUCTURE_AGENT_ID)
|
||||
except Exception: # noqa: BLE001
|
||||
return []
|
||||
attached = [s for s in (getattr(cfg, "skills", None) or []) if isinstance(s, str) and s.strip()]
|
||||
if not attached:
|
||||
return []
|
||||
attached_set = {s.strip().lower() for s in attached}
|
||||
try:
|
||||
storage = get_or_new_skill_storage(app_config=get_app_config())
|
||||
skills = storage.load_skills(enabled_only=True)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning("ingest eligibility: load_skills failed", exc_info=True)
|
||||
return []
|
||||
out: list[dict[str, str]] = []
|
||||
for sk in skills:
|
||||
if (sk.name or "").strip().lower() in attached_set:
|
||||
out.append({"name": sk.name, "description": sk.description or ""})
|
||||
return out
|
||||
|
||||
|
||||
_INGEST_ELIGIBILITY_SYS = (
|
||||
"你是圆桌「方案总结」第三步的**结构化抽取智能体**,正在判断「本次会商成果**能否入库**」。"
|
||||
"下面给你两样东西:① 当前业务场景(业务码 / 业务链名称 / 总控编排提示等线索);"
|
||||
"② 你**挂载的候选入库技能**清单(每个有 name 与 description)。\n"
|
||||
"请判断:这些候选技能里,**是否有适合把当前这种业务场景的会商成果入库的技能**。判断依据:\n"
|
||||
"- 技能名称 / 描述是否表明它就是做「入库 / 沉淀 / 写入知识库」的;\n"
|
||||
"- 它是否对应当前业务场景——例如六步法类业务(如 rwfx-A)的入库技能,其名称通常**含「六步法」**字样;\n"
|
||||
"- 若某技能明显是为本场景设计的入库技能 → 可入库;若候选里压根没有入库类技能、或都与本场景不符 → 不可入库。\n"
|
||||
"只输出一个 JSON 对象(不要解释、不要 markdown 代码块、用英文双引号):\n"
|
||||
'{"can_ingest":<true|false>,"skill":"<选中的技能 name,不可入库时为空字符串>","reason":"<一句话依据>"}'
|
||||
)
|
||||
|
||||
|
||||
@router.post("/ingest-eligibility")
|
||||
async def classify_ingest_eligibility(request: Request, payload: IngestEligibilityRequest = Body(...)) -> dict[str, Any]:
|
||||
"""让结构化抽取智能体判断当前业务场景**是否有合适的入库技能**(决定前端是否显示入库按钮)。
|
||||
|
||||
候选 = 挂在 roundtable-structure 上且已启用的技能。无候选 → 直接不可入库(不调模型)。
|
||||
有候选 → 一次模型调用判断适用性,返回 ``{can_ingest, skill, reason}``。失败 → 不可入库(安全回退)。
|
||||
"""
|
||||
ensure_roundtable_functional_agents() # 自愈:确保 roundtable-structure 存在
|
||||
candidates = _structure_skill_candidates()
|
||||
if not candidates:
|
||||
return {"can_ingest": False, "skill": "", "reason": "结构化抽取智能体未挂载入库技能"}
|
||||
|
||||
scenario_lines = []
|
||||
if (payload.business_code or "").strip():
|
||||
scenario_lines.append(f"业务码:{payload.business_code.strip()}")
|
||||
if (payload.chain_name or "").strip():
|
||||
scenario_lines.append(f"业务链名称:{payload.chain_name.strip()}")
|
||||
if (payload.report_title or "").strip():
|
||||
scenario_lines.append(f"方案标题:{payload.report_title.strip()}")
|
||||
if (payload.coordinator_prompt or "").strip():
|
||||
scenario_lines.append("总控编排提示(节选):" + payload.coordinator_prompt.strip()[:1200])
|
||||
scenario = "\n".join(scenario_lines) or "(未提供额外业务场景线索)"
|
||||
skills_block = "\n".join(f"- name: {c['name']}\n description: {c['description']}" for c in candidates)
|
||||
valid_names = {c["name"] for c in candidates}
|
||||
|
||||
user = (
|
||||
"【当前业务场景】\n" + scenario + "\n\n【候选入库技能】\n" + skills_block + "\n\n请输出 JSON:"
|
||||
)
|
||||
try:
|
||||
model = create_chat_model(name=_resolve_model(payload.model), thinking_enabled=False)
|
||||
res = await model.ainvoke([SystemMessage(content=_INGEST_ELIGIBILITY_SYS), HumanMessage(content=user)])
|
||||
text = res.content if isinstance(res.content, str) else _as_str(getattr(res, "text", ""))
|
||||
obj = _parse_intent_json(text) or {}
|
||||
skill = _as_str(obj.get("skill"))
|
||||
can = bool(obj.get("can_ingest")) and skill in valid_names
|
||||
# 模型说可入库但没给/给错技能名 → 单候选时回退到唯一候选,否则判不可入库(避免乱选)。
|
||||
if bool(obj.get("can_ingest")) and skill not in valid_names:
|
||||
if len(candidates) == 1:
|
||||
skill = candidates[0]["name"]
|
||||
can = True
|
||||
else:
|
||||
can = False
|
||||
return {
|
||||
"can_ingest": can,
|
||||
"skill": skill if can else "",
|
||||
"reason": _as_str(obj.get("reason")),
|
||||
}
|
||||
except Exception: # noqa: BLE001 — 判定失败不致命,安全回退为不可入库
|
||||
logger.warning("ingest eligibility classify failed", exc_info=True)
|
||||
return {"can_ingest": False, "skill": "", "reason": "入库适用性判定失败"}
|
||||
Reference in new issue
Block a user