functions.py
2.21 KB
"""把 SOP 暴露成只读函数,让问答能「按流程回答怎么做」「按剧本做诊断」。"""
from __future__ import annotations
from ..capability.registry import Emit, function
from . import engine, service
@function("sop.find", "查流程", "按关键词查已发布的 SOP,返回结构化的步骤、角色与参数。回答「X 怎么做 / 流程是什么」时用。",
{"type": "object", "properties": {"q": {"type": "string", "description": "流程关键词"}},
"required": []}, category="process")
def sop_find(args: dict, emit: Emit | None) -> dict:
items = service.find_published(str(args.get("q") or ""))
return {"items": items, "summary": f"找到 {len(items)} 个流程"}
@function("sop.playbook", "按流程诊断", "以只读方式跑一遍已发布 SOP 的取数/分析/判断部分(遇到人工或动作就停),"
"返回每一步的结果与建议的下一步。适合「为什么/怎么处理」这类需要按标准流程排查的问题。",
{"type": "object", "properties": {
"sop": {"type": "string", "description": "流程卡", "x-ref": "sop", "x-primary": True},
"params": {"type": "object", "description": "流程参数"}},
"required": ["sop"]}, category="process")
def sop_playbook(args: dict, emit: Emit | None) -> dict:
target = service.require_published(str(args["sop"]))
run = engine.start(target["id"], args.get("params") or {}, mode="ai_readonly",
trigger="playbook", wait=True)
steps = [{"title": s["title"], "type": s["step_type"], "status": s["status"],
"note": s["note"], "error": s["error"]} for s in run["steps"] or []]
suggested = [s for s in run["steps"] or [] if s["status"] == "suggested"]
return {"sop": {"id": target["id"], "title": target["title"]}, "run_id": run["id"],
"status": run["status"], "outcome": run["outcome"] or run["error"], "steps": steps,
"suggested": [{"title": s["title"], "type": s["step_type"], "note": s["note"],
"preview": (s["output"] or {}).get("preview")} for s in suggested],
"summary": f"按「{target['title']}」排查:{run['outcome'] or run['error']}"}