mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-21 06:11:59 +00:00
业务本体与标准实体:跨源键值对齐、对账共变算子、健康证据链与实体助手
- 新增标准业务实体(CanonicalEntity)模型、确定性跨源编码翻译、后台候选实体扫描与文档驱动实体智能助手 - 新增指标本体图谱(factor / reconciles_with / upstream_driver)、DAG 防环校验以及 ontology_neighbors / source_health 内置工具 - 新增跨源对账(analysis_reconcile)与滞后共变(analysis_cochange)分析算子,打通 [[h:H1]] 数据源健康证据链、报告导出与本体消融评测 - 数据目录新增「标准业务实体」视图与候选审阅工作台,口径页集成指标本体拓扑面板与端到端浏览器检查
This commit is contained in:
@@ -855,8 +855,8 @@ EVALUATION_NOT_FOUND = "eval_evaluation_not_found"
|
||||
PAIR_NOT_FOUND = "eval_pair_not_found"
|
||||
RESULT_NOT_FOUND = "eval_result_not_found"
|
||||
CLEANED = "eval_details_cleaned"
|
||||
#: 预设对照:目录贡献(当前目录 vs 关,可加只用已核实的项)、分析工具贡献(开 vs 关)
|
||||
PRESETS = ("catalog", "catalog_reviewed", "analysis_tools")
|
||||
#: 预设对照:目录贡献(当前目录 vs 关,可加只用已核实的项)、分析工具贡献(开 vs 关)、本体穿透贡献(开 vs 关)
|
||||
PRESETS = ("catalog", "catalog_reviewed", "analysis_tools", "ontology_tools")
|
||||
|
||||
|
||||
def _refused(e: CaseInvalid) -> CodedHTTPException:
|
||||
@@ -865,8 +865,8 @@ def _refused(e: CaseInvalid) -> CodedHTTPException:
|
||||
|
||||
async def _arms(session: AsyncSession, suite: EvalSuite, body: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
"""请求体 → 规范化的几臂变体:{variant}(一臂,不写用题集的缺省变体)、{arms: [变体…]}(A/B)或 {preset: catalog |
|
||||
catalog_reviewed | analysis_tools}(目录贡献对照:当前目录 vs 关,可加只用已核实的项;分析工具贡献对照:开 vs 关;别的项
|
||||
照 variant 或题集的缺省)。A/B 缺省每题 3 次。"""
|
||||
catalog_reviewed | analysis_tools | ontology_tools}(目录贡献对照:当前目录 vs 关,可加只用已核实的项;分析工具贡献对照:
|
||||
开 vs 关;本体穿透贡献对照:开 vs 关;别的项照 variant 或题集的缺省)。A/B 缺省每题 3 次。"""
|
||||
from app.eval import variants as variant_rules
|
||||
|
||||
judge = bool((await _site(session))["judge"])
|
||||
@@ -880,6 +880,8 @@ async def _arms(session: AsyncSession, suite: EvalSuite, body: dict[str, Any]) -
|
||||
judge_default=judge, repeats_default=variant_rules.PAIR_REPEATS)
|
||||
if preset == "analysis_tools":
|
||||
return variant_rules.analysis_tools_preset(base)
|
||||
if preset == "ontology_tools":
|
||||
return variant_rules.ontology_tools_preset(base)
|
||||
return variant_rules.catalog_preset(base, reviewed=preset == "catalog_reviewed")
|
||||
if raw_arms is not None:
|
||||
if not isinstance(raw_arms, list) or not 2 <= len(raw_arms) <= variant_rules.ARMS_MAX:
|
||||
|
||||
@@ -72,6 +72,7 @@ from sqlalchemy import select, update
|
||||
from sqlalchemy.exc import StatementError
|
||||
|
||||
from app.analysis.contract import CATALOG_PREFIX, LABELS as ANALYSIS_LABELS, LEDGER_KIND, is_result, method_label
|
||||
from app.data.source_health import LEDGER_KIND as HEALTH_KIND, is_health_result
|
||||
from app.api.coded import CodedHTTPException
|
||||
from app.core.artifact_store import load
|
||||
from app.core.events import EventType
|
||||
@@ -411,9 +412,25 @@ def _graph_of(report: _Report, sealed: _Sealed, inputs: dict[str, Any]) -> tuple
|
||||
# 输入快照的 Q…,下钻的上一层 A…),图上看得出 A3 是从 Q2 算出来的
|
||||
item.update({k: entry[k] for k in ("op", "mode", "status", "verdict", "item", "call_id")
|
||||
if entry.get(k) is not None})
|
||||
targets = [t for t in dict.fromkeys(_catalog_alias_of(catalog, a) for a in entry.get("inputs") or [])
|
||||
if t and t != alias]
|
||||
input_sources = {
|
||||
str(catalog[t].get("source"))
|
||||
for t in targets
|
||||
if isinstance(catalog.get(t), dict) and catalog[t].get("source")
|
||||
}
|
||||
if len(input_sources) > 1 or entry.get("op") in ("reconcile", "cochange"):
|
||||
item["cross_source"] = True
|
||||
edges.extend({"from": alias, "to": target, "rel": "derived_from", "report": report.node_id}
|
||||
for target in dict.fromkeys(_catalog_alias_of(catalog, a) for a in entry.get("inputs") or [])
|
||||
if target and target != alias)
|
||||
for target in targets)
|
||||
elif entry.get("kind") == HEALTH_KIND:
|
||||
item.update({k: entry[k] for k in ("verdict", "sources", "issue_count", "call_id")
|
||||
if entry.get(k) is not None})
|
||||
checked_sources = {str(s) for s in entry.get("sources") or [] if s}
|
||||
edges.extend({"from": alias, "to": q_alias, "rel": "health_of", "report": report.node_id}
|
||||
for q_alias, q_entry in catalog.items()
|
||||
if isinstance(q_entry, dict) and q_entry.get("kind") == "query"
|
||||
and str(q_entry.get("source") or "") in checked_sources)
|
||||
elif entry.get("kind") in ENTITY_KINDS:
|
||||
# 表和字段:叫什么、出现在哪几次查询里、来历(表结构快照 / SQL / 结果列)各自封存了没有
|
||||
item.update({k: entry[k] for k in ("name", "table", "tables", "qualified", "source") if entry.get(k)})
|
||||
@@ -845,6 +862,8 @@ async def _chain(seg: dict[str, Any], doc: dict[str, Any], sealed: _Sealed,
|
||||
if cite.get("kind") == LEDGER_KIND or (cite.get("kind") == "cell" and entry.get("kind") == LEDGER_KIND):
|
||||
# 分析结论:判定语、具名标量、结果表的一格都接同一条链(判定 → 方法 → 计算 → 输入 → 复算 → 封存)
|
||||
return await _analysis_chain(seg, cite, entry, doc, sealed, masks)
|
||||
if cite.get("kind") == HEALTH_KIND:
|
||||
return _health_chain(seg, cite, entry, sealed)
|
||||
if cite.get("kind") == "cell":
|
||||
locator = cite.get("locator") or {}
|
||||
# 只有直接引用的单元格才可能推断来源(P4-SPEC 2.8.2)。指标链里的查询步骤不带:指标片段的推断来源只会
|
||||
@@ -890,10 +909,13 @@ _OP_SETTINGS: dict[str, tuple[str, ...]] = {
|
||||
"trend": ("trend_min_points", "trend_alpha", "min_history", "max_history"),
|
||||
"differ": ("differ_alpha", "min_n", "min_expected"),
|
||||
"comparable": (),
|
||||
"reconcile": ("reconcile_rel_tol", "reconcile_abs_tol"),
|
||||
"cochange": ("k_warn", "k_alert", "min_history", "max_history", "cochange_max_lag"),
|
||||
}
|
||||
#: 输入的角色(analysis.contract.AnalysisInput.role)给人看的叫法;比率拆解的 headline 是整体比率,不是总数
|
||||
_INPUT_ROLES = {"data": "数据", "base": "基期", "target": "本期", "headline_base": "基期总数",
|
||||
"headline_target": "本期总数", "parent": "上一层拆解"}
|
||||
"headline_target": "本期总数", "parent": "上一层拆解",
|
||||
"primary": "主侧数据", "counterpart": "对侧数据", "driver": "上游驱动"}
|
||||
_RATIO_ROLES = {"headline_base": "基期整体比率", "headline_target": "本期整体比率"}
|
||||
#: 闭合核对:headline 的那一格对着结论里的哪个数(细案 01 §2.1、§2.2)。可加拆解对各段之和(base_total / target_total),
|
||||
#: 比率拆解对整体比率,即各段分子之和 ÷ 分母之和(r_base_total / r_target_total)
|
||||
@@ -911,6 +933,64 @@ def _analysis_of(artifact: str) -> tuple[dict[str, Any] | None, bool | None]:
|
||||
return (content, ok) if ok is True and is_result(content) else (None, False if ok is False else None)
|
||||
|
||||
|
||||
def _health_of(artifact: str) -> tuple[dict[str, Any] | None, bool | None]:
|
||||
"""取回一份数据源健康结论:(结论, hash_ok)。"""
|
||||
content, ok = _fetch(artifact) if artifact else (None, None)
|
||||
return (content, ok) if ok is True and is_health_result(content) else (None, False if ok is False else None)
|
||||
|
||||
|
||||
def _health_chain(seg: dict[str, Any], cite: dict[str, Any], entry: dict[str, Any],
|
||||
sealed: _Sealed) -> list[dict[str, Any]]:
|
||||
"""数据源健康结论的出处链:健康核查判定 → 封存。"""
|
||||
alias = str(cite.get("alias") or entry.get("alias") or "")
|
||||
artifact = str(entry.get("artifact") or "")
|
||||
result, hash_ok = _health_of(artifact)
|
||||
info = sealed.source_healths.get(artifact)
|
||||
in_ledger = sealed.trusted and (str(entry.get("node_id") or ""), artifact) in sealed.ledger
|
||||
eid = make_eid(HEALTH_KIND, artifact or None, {})
|
||||
phrase = result.get("phrase") if isinstance(result, dict) else None
|
||||
rendered = None
|
||||
if isinstance(phrase, str):
|
||||
with contextlib.suppress(RenderError):
|
||||
rendered = render_cell(phrase, kind="text")
|
||||
judged: dict[str, Any] = {
|
||||
"step": HEALTH_KIND,
|
||||
"alias": alias,
|
||||
"node_id": entry.get("node_id"),
|
||||
"label": _node_label(sealed, entry.get("node_id")) or None,
|
||||
**({k: entry[k] for k in ("call_id",) if entry.get(k)}),
|
||||
"artifact": artifact or None,
|
||||
"eid": entry.get("eid"),
|
||||
"eid_ok": entry.get("eid") == eid,
|
||||
"rendered": seg.get("text"),
|
||||
"render_ok": rendered is not None and rendered == seg.get("text"),
|
||||
"hash_ok": hash_ok,
|
||||
}
|
||||
seal = {
|
||||
"step": "health_seal",
|
||||
"alias": alias,
|
||||
"node_id": entry.get("node_id"),
|
||||
"artifact": artifact or None,
|
||||
"ledger": info is not None,
|
||||
"hash_ok": hash_ok,
|
||||
"eid_ok": judged["eid_ok"],
|
||||
"sealed": in_ledger and hash_ok is True and judged["eid_ok"],
|
||||
}
|
||||
if info is None:
|
||||
seal["note"] = "这份数据源健康核查不属于封存范围内的任何事件,不能作为证据展示"
|
||||
if result is None:
|
||||
judged["note"] = "数据源健康核查与哈希不一致,疑似被修改" if hash_ok is False else "无法读取数据源健康核查"
|
||||
return [judged, seal]
|
||||
judged.update(
|
||||
verdict=result.get("verdict"),
|
||||
phrase=result.get("phrase"),
|
||||
sources=list(result.get("sources") or []),
|
||||
issues=list(result.get("issues") or []),
|
||||
tables=list(result.get("tables") or []),
|
||||
)
|
||||
return [judged, seal]
|
||||
|
||||
|
||||
def _catalog_alias_of(catalog: dict[str, Any], artifact: Any) -> str | None:
|
||||
"""这份工件在报告目录里的编号:查询快照是 Q…,分析结论是 A…。"""
|
||||
if not artifact:
|
||||
@@ -3202,7 +3282,7 @@ async def evidence_provenance(run_id: str, segment_id: str, report: str | None =
|
||||
def _export_cite_key(seg: dict[str, Any]) -> tuple | None:
|
||||
"""数字 / 取值 / 判定语片段的引用键:同一处引用(同一个指标、同一格、同一条分析结论的同一项、同样的换算)共用一条
|
||||
出处链。别的片段(表名、引文)的链跟着片段走,不归并。具名标量的键另接在后面:没有它的引用键和以前一模一样。"""
|
||||
if seg.get("kind") not in ("number", "value", LEDGER_KIND):
|
||||
if seg.get("kind") not in ("number", "value", LEDGER_KIND, HEALTH_KIND):
|
||||
return None
|
||||
cite = seg.get("cite")
|
||||
if not isinstance(cite, dict):
|
||||
@@ -3996,7 +4076,7 @@ def _scope_catalog(sealed: _Sealed, report_node: str) -> dict[str, Any]:
|
||||
from app.engine.nodes.report import report_catalog
|
||||
from app.engine.schema import GraphSpec
|
||||
|
||||
entries = [e for e in sealed.entries if e.get("kind") != LEDGER_KIND]
|
||||
entries = [e for e in sealed.entries if e.get("kind") not in (LEDGER_KIND, HEALTH_KIND)]
|
||||
try:
|
||||
spec = GraphSpec.model_validate(sealed.graph)
|
||||
node = spec.node_map().get(report_node)
|
||||
@@ -4088,6 +4168,8 @@ def _note(seg: dict[str, Any], unit: dict[str, Any]) -> str:
|
||||
"仅核对它存在且本次用到,不核对其业务含义")
|
||||
if seg.get("state") == "deterministic" and seg.get("kind") == "quote":
|
||||
return "这段原话在知识库检索命中的片段中逐字出现(空白归一化后比对),已由系统核对"
|
||||
if seg.get("state") == "deterministic" and cite.get("kind") == HEALTH_KIND:
|
||||
return "判定语由系统按数据源健康核查规则算出、按固定模板写成,未经模型转写;各项核查与封存见出处链"
|
||||
if seg.get("state") == "deterministic" and cite.get("kind") == LEDGER_KIND:
|
||||
if (cite.get("locator") or {}).get("key"):
|
||||
return "这个数取自分析结论的具名的数,由系统按固定规则显示,未经模型转写"
|
||||
@@ -4666,7 +4748,7 @@ SEAL_NONE = "未封存:运行尚未结束,或正停在人工审批"
|
||||
_CSV_LABELS: dict[str, dict[Any, str]] = {
|
||||
"group": dict(GROUPS),
|
||||
"kind": {"number": "数字", "value": "值", "entity": "表名、字段名", "quote": "引文", "claim": "结论句",
|
||||
"violation": "违规", LEDGER_KIND: "分析判定"},
|
||||
"violation": "违规", LEDGER_KIND: "分析判定", HEALTH_KIND: "数据源健康"},
|
||||
"state": {"deterministic": "有出处", "none": "无证据", "candidate": "候选"},
|
||||
"issue": {"uncited_number": "没有出处的数字", "unresolved_ref": "引用无法解析", "unknown_entity": "疑似不存在的名称",
|
||||
"unverified_entity": "无法核实", "uncited_claim": "结论句未附依据", "no_candidate": "未找到候选",
|
||||
@@ -4676,7 +4758,7 @@ _CSV_LABELS: dict[str, dict[Any, str]] = {
|
||||
"causal_unsupported": "因果说法没有依据",
|
||||
**{code: "文档不一致" for code in _INTEGRITY}},
|
||||
"evidence_kind": {"metric": "口径卡指标", "cell": "查询单元格", "input": "运行输入", "table": "表", "column": "字段",
|
||||
"quote": "知识库原话", "query": "查询结果", LEDGER_KIND: "分析结论"},
|
||||
"quote": "知识库原话", "query": "查询结果", LEDGER_KIND: "分析结论", HEALTH_KIND: "数据源健康"},
|
||||
"sealed": {True: "是", False: "否", None: "—"},
|
||||
}
|
||||
#: 电子表格会把以这些字符开头的格子当公式算:报告、答案里的字来自模型,照原样写进去就能被人利用
|
||||
|
||||
@@ -0,0 +1,354 @@
|
||||
"""标准业务实体与指标本体图谱接口:/api/ontology。
|
||||
|
||||
- GET /api/ontology/entities 列出标准业务实体(可按数据源过滤)
|
||||
- POST /api/ontology/entities 新建标准业务实体
|
||||
- GET /api/ontology/entities/{entity_id} 读取单个标准业务实体
|
||||
- PATCH /api/ontology/entities/{entity_id} 更新标准业务实体(支持 version 乐观锁)
|
||||
- DELETE /api/ontology/entities/{entity_id} 删除标准业务实体
|
||||
- POST /api/ontology/entities/{entity_id}/review 审阅单条跨源表列绑定或口径参数映射(确认 / 驳回)
|
||||
- GET /api/ontology/graph 汇总已发布口径的指标关系网、反向表血缘与标准实体跨源桥
|
||||
- GET /api/ontology/workflows/{workflow_id} 读取单个工作流(当前草稿或指定版本)的指标关系与反向表血缘
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Literal
|
||||
|
||||
from fastapi import APIRouter, BackgroundTasks, Body, Depends, File, Form, Header, HTTPException, Query, UploadFile
|
||||
from fastapi.responses import StreamingResponse
|
||||
from pydantic import BaseModel, Field
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.api.coded import CATALOG_DRAFT_MODEL_UNAVAILABLE, CodedHTTPException
|
||||
from app.api.deps import actor_of
|
||||
from app.data import catalog, entity_assistant, entity_discovery, entity_docs, entity_patch
|
||||
from app.data.entities import (
|
||||
EntityConflict,
|
||||
EntityInvalid,
|
||||
EntityNotFound,
|
||||
create_entity,
|
||||
delete_entity,
|
||||
entity_to_dict,
|
||||
get_entity,
|
||||
list_entities,
|
||||
review_entity_item,
|
||||
update_entity,
|
||||
)
|
||||
from app.db.base import get_session
|
||||
from app.db.models import Workflow, WorkflowVersion
|
||||
from app.engine.schema import GraphSpec
|
||||
from app.ledger.ontology import build_published_ontology_graph, ontology_publish_issues, workflow_ontology_index
|
||||
|
||||
router = APIRouter(prefix="/api/ontology", tags=["ontology"])
|
||||
|
||||
|
||||
class EntityReviewIn(BaseModel):
|
||||
target_kind: Literal["binding", "caliber_param"]
|
||||
item_id: str
|
||||
status: Literal["proposed", "verified", "confirmed", "rejected"]
|
||||
reason: str = ""
|
||||
version: int | None = None
|
||||
|
||||
|
||||
class EntityAssistantMessageIn(BaseModel):
|
||||
role: Literal["user", "assistant"]
|
||||
content: str = Field(max_length=entity_assistant.MESSAGE_MAX)
|
||||
|
||||
|
||||
class EntityAssistantIn(BaseModel):
|
||||
messages: list[EntityAssistantMessageIn] = Field(default_factory=list, max_length=entity_assistant.MAX_MESSAGES)
|
||||
source_ids: list[str] | None = Field(default=None, max_length=50)
|
||||
focus_entity_key: str | None = None
|
||||
attachment_ids: list[str] = Field(default_factory=list, max_length=20)
|
||||
document_ids: list[str] = Field(default_factory=list, max_length=30)
|
||||
pending_ops: list[dict[str, Any]] = Field(default_factory=list)
|
||||
|
||||
|
||||
class EntityApplyPatchIn(BaseModel):
|
||||
operations: list[dict[str, Any]] = Field(min_length=1, max_length=200)
|
||||
expected_versions: dict[str, int] | None = None
|
||||
|
||||
|
||||
class EntityDiscoveryJobIn(BaseModel):
|
||||
source_ids: list[str] | None = None
|
||||
document_ids: list[str] | None = None
|
||||
attachment_ids: list[str] | None = None
|
||||
note: str | None = None
|
||||
|
||||
|
||||
@router.get("/entities")
|
||||
async def api_list_entities(
|
||||
source_name: str | None = Query(default=None),
|
||||
only_active: bool = Query(default=False),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> dict[str, Any]:
|
||||
rows = await list_entities(session, source_name=source_name, only_active_bindings=only_active)
|
||||
return {"items": [entity_to_dict(r) for r in rows]}
|
||||
|
||||
|
||||
@router.post("/entities", status_code=201)
|
||||
async def api_create_entity(
|
||||
payload: dict[str, Any] = Body(...),
|
||||
x_actor: str | None = Header(default=None, alias="X-Actor"),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> dict[str, Any]:
|
||||
try:
|
||||
row = await create_entity(session, payload, actor=actor_of(x_actor))
|
||||
await session.commit()
|
||||
except EntityInvalid as exc:
|
||||
raise HTTPException(status_code=422, detail={"message": exc.message, "field": exc.field}) from None
|
||||
except EntityConflict as exc:
|
||||
raise HTTPException(status_code=409, detail={"message": exc.message, "field": exc.field}) from None
|
||||
return entity_to_dict(row)
|
||||
|
||||
|
||||
@router.post("/entities/attachments", status_code=201)
|
||||
async def api_upload_entity_attachment(
|
||||
background: BackgroundTasks,
|
||||
file: UploadFile = File(...),
|
||||
save_to_kb: bool = Form(default=False),
|
||||
collection: str = Form(default="default"),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> dict[str, Any]:
|
||||
"""解析业务文档或主数据编码对照表附件,支持勾选同步存入知识库复用。"""
|
||||
try:
|
||||
raw_bytes = await entity_docs.read_upload_limited(file)
|
||||
return await entity_docs.ingest_entity_attachment(
|
||||
session,
|
||||
raw_bytes=raw_bytes,
|
||||
filename=file.filename or "上传文档",
|
||||
mime=file.content_type or "text/plain",
|
||||
save_to_kb=save_to_kb,
|
||||
collection=collection,
|
||||
background=background,
|
||||
)
|
||||
except entity_docs.AttachmentTooLarge as exc:
|
||||
raise HTTPException(status_code=413, detail=str(exc)) from None
|
||||
except entity_docs.AttachmentInvalid as exc:
|
||||
raise HTTPException(status_code=415, detail=str(exc)) from None
|
||||
|
||||
|
||||
@router.post("/entities/assistant")
|
||||
async def api_entity_assistant(
|
||||
payload: EntityAssistantIn,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> Any:
|
||||
"""交互式实体助手(SSE):跨数据源与业务文档对话对齐业务实体,只产出提案不直接写库。"""
|
||||
messages = [m.model_dump() for m in payload.messages]
|
||||
if problem := entity_assistant.conversation_problem(messages):
|
||||
raise HTTPException(status_code=422, detail=problem)
|
||||
try:
|
||||
model, model_id = await entity_assistant.resolve_model(session)
|
||||
except catalog.CatalogModelUnavailable as exc:
|
||||
raise CodedHTTPException(409, str(exc), CATALOG_DRAFT_MODEL_UNAVAILABLE) from exc
|
||||
ctx = await entity_assistant.prepare(
|
||||
session,
|
||||
source_ids=payload.source_ids,
|
||||
focus_entity_key=payload.focus_entity_key,
|
||||
attachment_ids=payload.attachment_ids,
|
||||
document_ids=payload.document_ids,
|
||||
pending_ops=payload.pending_ops,
|
||||
)
|
||||
return StreamingResponse(
|
||||
entity_assistant.stream(model, model_id, ctx, messages),
|
||||
media_type="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/entities/apply-patch")
|
||||
async def api_apply_entity_patch(
|
||||
body: EntityApplyPatchIn,
|
||||
x_actor: str | None = Header(default=None, alias="X-Actor"),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> dict[str, Any]:
|
||||
"""批量应用人工在实体助手审阅确认的修改提案(校验乐观锁版本)。"""
|
||||
try:
|
||||
result = await entity_patch.apply_entity_patch(
|
||||
session,
|
||||
body.operations,
|
||||
mode="confirmed",
|
||||
actor=actor_of(x_actor),
|
||||
expected_versions=body.expected_versions,
|
||||
)
|
||||
await session.commit()
|
||||
return result
|
||||
except EntityInvalid as exc:
|
||||
raise HTTPException(status_code=422, detail={"message": exc.message, "field": exc.field}) from None
|
||||
except EntityConflict as exc:
|
||||
raise HTTPException(status_code=409, detail={"message": exc.message, "field": exc.field}) from None
|
||||
|
||||
|
||||
@router.post("/discovery-jobs", status_code=202)
|
||||
async def api_create_discovery_job(
|
||||
body: EntityDiscoveryJobIn,
|
||||
x_actor: str | None = Header(default=None, alias="X-Actor"),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> dict[str, Any]:
|
||||
"""创建后台扫描发现候选标准业务实体的任务(结果一律落为 proposed)。"""
|
||||
try:
|
||||
job = await entity_discovery.create_job(
|
||||
session,
|
||||
source_ids=body.source_ids,
|
||||
document_ids=body.document_ids,
|
||||
attachment_ids=body.attachment_ids,
|
||||
note=body.note,
|
||||
actor=actor_of(x_actor),
|
||||
)
|
||||
return entity_discovery.job_to_dict(job)
|
||||
except catalog.CatalogModelUnavailable as exc:
|
||||
raise CodedHTTPException(409, str(exc), CATALOG_DRAFT_MODEL_UNAVAILABLE) from exc
|
||||
except entity_discovery.DiscoveryBusy as exc:
|
||||
raise CodedHTTPException(409, str(exc), entity_discovery.DISCOVERY_BUSY_CODE) from exc
|
||||
except (entity_discovery.DiscoveryEmptyScope, ValueError) as exc:
|
||||
raise HTTPException(status_code=422, detail=str(exc)) from None
|
||||
|
||||
|
||||
@router.get("/discovery-jobs/active")
|
||||
async def api_get_active_discovery_job(
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> dict[str, Any]:
|
||||
return await entity_discovery.get_active_or_latest_job(session)
|
||||
|
||||
|
||||
@router.get("/discovery-jobs/{job_id}")
|
||||
async def api_get_discovery_job(
|
||||
job_id: str,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> dict[str, Any]:
|
||||
row = await entity_discovery.get_job(session, job_id)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="候选实体扫描任务不存在")
|
||||
return entity_discovery.job_to_dict(row)
|
||||
|
||||
|
||||
@router.post("/discovery-jobs/{job_id}/cancel")
|
||||
async def api_cancel_discovery_job(
|
||||
job_id: str,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> dict[str, Any]:
|
||||
try:
|
||||
row = await entity_discovery.cancel_job(session, job_id)
|
||||
return entity_discovery.job_to_dict(row)
|
||||
except LookupError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc)) from None
|
||||
except entity_discovery.DiscoveryFinished as exc:
|
||||
raise CodedHTTPException(409, str(exc), entity_discovery.DISCOVERY_FINISHED_CODE) from exc
|
||||
|
||||
|
||||
@router.get("/entities/{entity_id}")
|
||||
async def api_get_entity(
|
||||
entity_id: str,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> dict[str, Any]:
|
||||
row = await get_entity(session, entity_id)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="标准业务实体不存在")
|
||||
return entity_to_dict(row)
|
||||
|
||||
|
||||
@router.patch("/entities/{entity_id}")
|
||||
async def api_update_entity(
|
||||
entity_id: str,
|
||||
payload: dict[str, Any] = Body(...),
|
||||
x_actor: str | None = Header(default=None, alias="X-Actor"),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> dict[str, Any]:
|
||||
try:
|
||||
row = await update_entity(session, entity_id, payload, actor=actor_of(x_actor))
|
||||
await session.commit()
|
||||
except EntityNotFound as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc)) from None
|
||||
except EntityInvalid as exc:
|
||||
raise HTTPException(status_code=422, detail={"message": exc.message, "field": exc.field}) from None
|
||||
except EntityConflict as exc:
|
||||
raise HTTPException(status_code=409, detail={"message": exc.message, "field": exc.field}) from None
|
||||
return entity_to_dict(row)
|
||||
|
||||
|
||||
@router.post("/entities/{entity_id}/review")
|
||||
async def api_review_entity_item(
|
||||
entity_id: str,
|
||||
body: EntityReviewIn,
|
||||
x_actor: str | None = Header(default=None, alias="X-Actor"),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> dict[str, Any]:
|
||||
try:
|
||||
row = await review_entity_item(
|
||||
session,
|
||||
entity_id,
|
||||
target_kind=body.target_kind,
|
||||
item_id=body.item_id,
|
||||
status=body.status,
|
||||
reason=body.reason,
|
||||
actor=actor_of(x_actor),
|
||||
expected_version=body.version,
|
||||
)
|
||||
await session.commit()
|
||||
except EntityNotFound as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc)) from None
|
||||
except EntityInvalid as exc:
|
||||
raise HTTPException(status_code=422, detail={"message": exc.message, "field": exc.field}) from None
|
||||
except EntityConflict as exc:
|
||||
raise HTTPException(status_code=409, detail={"message": exc.message, "field": exc.field}) from None
|
||||
return entity_to_dict(row)
|
||||
|
||||
|
||||
@router.delete("/entities/{entity_id}")
|
||||
async def api_delete_entity(
|
||||
entity_id: str,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> dict[str, Any]:
|
||||
deleted = await delete_entity(session, entity_id)
|
||||
if not deleted:
|
||||
raise HTTPException(status_code=404, detail="标准业务实体不存在")
|
||||
await session.commit()
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.get("/graph")
|
||||
async def api_get_ontology_graph(
|
||||
include_proposed: bool = Query(default=True),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> dict[str, Any]:
|
||||
return await build_published_ontology_graph(session, include_proposed=include_proposed)
|
||||
|
||||
|
||||
@router.get("/workflows/{workflow_id}")
|
||||
async def api_get_workflow_ontology(
|
||||
workflow_id: str,
|
||||
version: int | None = Query(default=None),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> dict[str, Any]:
|
||||
workflow = await session.get(Workflow, workflow_id)
|
||||
if workflow is None:
|
||||
raise HTTPException(status_code=404, detail="工作流不存在")
|
||||
if version is not None:
|
||||
snap = (
|
||||
await session.execute(
|
||||
select(WorkflowVersion).where(
|
||||
WorkflowVersion.workflow_id == workflow_id,
|
||||
WorkflowVersion.version == version,
|
||||
)
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
if snap is None:
|
||||
raise HTTPException(status_code=404, detail=f"工作流不存在 v{version} 版本")
|
||||
graph = snap.graph or {}
|
||||
ver = int(snap.version)
|
||||
else:
|
||||
graph = workflow.graph or {}
|
||||
ver = int(workflow.version or 1)
|
||||
index = workflow_ontology_index(
|
||||
workflow_id=workflow.id,
|
||||
version=ver,
|
||||
graph=graph,
|
||||
workflow_name=workflow.name or workflow.id,
|
||||
)
|
||||
issues: list[dict[str, Any]] = []
|
||||
try:
|
||||
spec = GraphSpec.model_validate(graph)
|
||||
issues = await ontology_publish_issues(session, spec, workflow_id=workflow.id)
|
||||
except Exception: # noqa: BLE001 - 草稿图结构未成形时照常返回可提取的指标清单
|
||||
issues = []
|
||||
return {**index, "issues": issues}
|
||||
+19
-7
@@ -102,10 +102,16 @@ class _Projection:
|
||||
return event
|
||||
node_id = str(event.get("node_id") or "")
|
||||
failure = str(event.get("type") or "") in FAILURE_EVENTS
|
||||
if node_id in self.nodes or str(data.get("tool") or "") == CALIBER_QUERY \
|
||||
tool_name = str(data.get("tool") or "")
|
||||
if node_id in self.nodes or tool_name in (CALIBER_QUERY, "ontology_neighbors", "source_health") \
|
||||
or str(data.get("node_id") or "") in self.nodes \
|
||||
or (failure and safe_failure(data.get("error")) is not None):
|
||||
safe = {k: v for k, v in data.items() if k in SAFE_EVENT_KEYS}
|
||||
if isinstance(data.get("health_artifact"), Mapping):
|
||||
safe["health_artifact"] = {
|
||||
k: v for k, v in data["health_artifact"].items()
|
||||
if k in ("ref", "verdict", "phrase")
|
||||
}
|
||||
result = result_summary(data.get("artifact")) or safe_result(data.get("preview"))
|
||||
if result is not None:
|
||||
safe["result"] = result
|
||||
@@ -138,9 +144,12 @@ def _caliber_nodes(graph: Any) -> set[str]:
|
||||
|
||||
|
||||
def _touches_caliber(config: Mapping[str, Any]) -> bool:
|
||||
"""这个节点的配置里有没有口径的身份:绑了口径查询,或者是一张转抄卡。"""
|
||||
bound = config.get("tools") if isinstance(config.get("tools"), list) else []
|
||||
if config.get("tool") == CALIBER_QUERY or CALIBER_QUERY in [t for t in bound if isinstance(t, str)]:
|
||||
"""这个节点的配置里有没有口径的身份:绑了口径查询/本体邻居,或者是一张转抄卡。"""
|
||||
bound = [t for t in (config.get("tools") if isinstance(config.get("tools"), list) else []) if isinstance(t, str)]
|
||||
caliber_tools = {CALIBER_QUERY, "ontology_neighbors"}
|
||||
if str(config.get("tool") or "") in caliber_tools or any(t in caliber_tools for t in bound):
|
||||
return True
|
||||
if config.get("tool") == "source_health" and isinstance(config.get("args"), Mapping) and config["args"].get("workflow"):
|
||||
return True
|
||||
return config.get("caliber_from") not in (None, "", {})
|
||||
|
||||
@@ -151,10 +160,13 @@ def _safe_config(config: Mapping[str, Any]) -> dict[str, Any]:
|
||||
别的键(节点自己的说明、inputs 的值、工具清单)一个字不动——「使用者的图长什么样」照旧看得见,看不见的只是
|
||||
「调的是哪一份工作流的哪一版」。"""
|
||||
out = dict(config)
|
||||
if out.get("tool") == CALIBER_QUERY and "args" in out:
|
||||
caliber_tools = {CALIBER_QUERY, "ontology_neighbors"}
|
||||
if (str(out.get("tool") or "") in caliber_tools or (
|
||||
out.get("tool") == "source_health" and isinstance(out.get("args"), Mapping) and out["args"].get("workflow")
|
||||
)) and "args" in out:
|
||||
out["args"] = dict(CALIBER_ARGS_NOTE)
|
||||
bound = out.get("tools") if isinstance(out.get("tools"), list) else []
|
||||
if out.get("tool") == CALIBER_QUERY or CALIBER_QUERY in [t for t in bound if isinstance(t, str)]:
|
||||
bound = [t for t in (out.get("tools") if isinstance(out.get("tools"), list) else []) if isinstance(t, str)]
|
||||
if str(out.get("tool") or "") in caliber_tools or any(t in caliber_tools for t in bound):
|
||||
for key in ("prompt", "instructions"):
|
||||
if key in out:
|
||||
out[key] = safe_call_text(out[key])
|
||||
|
||||
@@ -988,6 +988,8 @@ DEFAULT_SETTINGS: dict[str, Any] = {
|
||||
"agent_max_steps": 100,
|
||||
"agent_budget_tokens": 2_000_000,
|
||||
"agent_budget_usd": None,
|
||||
# 问数简单查询撞上硬异常信号时的处置策略(P1):prompt(默认,正常交付并提示一键本体排查)/ auto(自动升级最多 1 轮)
|
||||
"anomaly_escalation_mode": "prompt",
|
||||
},
|
||||
"limits": {
|
||||
"max_graph_steps": app_settings.max_graph_steps,
|
||||
@@ -1018,6 +1020,9 @@ DEFAULT_SETTINGS: dict[str, Any] = {
|
||||
}
|
||||
|
||||
|
||||
ANOMALY_ESCALATION_MODES = ("prompt", "auto")
|
||||
|
||||
|
||||
async def run_defaults(session: AsyncSession) -> dict[str, Any]:
|
||||
"""设置里 run 这一组,没存过的键补内置默认。
|
||||
|
||||
@@ -1028,6 +1033,12 @@ async def run_defaults(session: AsyncSession) -> dict[str, Any]:
|
||||
return {**DEFAULT_SETTINGS["run"], **((row.value if row else None) or {})}
|
||||
|
||||
|
||||
async def anomaly_escalation_mode(session: AsyncSession) -> str:
|
||||
"""问数简单查数命中硬异常时的本体排查升级策略:默认 prompt,明确设为 auto 时自动发起最多 1 轮诊断升级。"""
|
||||
raw = (await run_defaults(session)).get("anomaly_escalation_mode")
|
||||
return str(raw) if raw in ANOMALY_ESCALATION_MODES else "prompt"
|
||||
|
||||
|
||||
def tool_approval_default(run: dict[str, Any]) -> str:
|
||||
"""「危险工具默认需要人工确认」换算成节点 approval 的取值,节点自己配了的不看它。
|
||||
|
||||
@@ -1163,6 +1174,14 @@ async def put_settings(
|
||||
from app.tools.trust import SETTING_KEY as TRUST_KEY
|
||||
|
||||
# 先查后写:一组里有一项不对就整个请求不落库,免得存下半组
|
||||
if "run" in payload.values and isinstance(payload.values["run"], dict):
|
||||
if "anomaly_escalation_mode" in payload.values["run"]:
|
||||
mode = payload.values["run"]["anomaly_escalation_mode"]
|
||||
if mode is not None and mode not in ANOMALY_ESCALATION_MODES:
|
||||
raise HTTPException(
|
||||
422,
|
||||
f"异常升级策略(run.anomaly_escalation_mode)只能是 {' 或 '.join(ANOMALY_ESCALATION_MODES)},当前为「{mode}」",
|
||||
)
|
||||
if JUDGE_GROUP in payload.values and (problem := judge_problem(payload.values[JUDGE_GROUP])):
|
||||
raise HTTPException(422, problem)
|
||||
if VERIFY_GROUP in payload.values and (problem := verify_problem(payload.values[VERIFY_GROUP])):
|
||||
|
||||
Reference in New Issue
Block a user