# -*- coding: utf-8 -*- r"""本体视图适配(P12):把 ontology MCP 的返回值包成**与 Python 详情层相同**的响应形状。 用途:Java 业务端点只需代理本模块的输出(再按对外口径脱敏),一致性由裁判逐值验收。 形状来源:`app_backEnd/app_backEnd_guanlan/serve.py` 的 `/api/ont_list`、`/api/ont_obj` 分支。 """ from __future__ import annotations from typing import Any def _om(): from src.ontology import mcp_server as om return om def ont_list_rows(type: str = '', t: str = '', limit: int = 60) -> dict[str, Any]: """`/api/ont_list`:`rows = om.ont_query(type=…, turbine=…, limit=…).get('data', [])`""" got = _om().ont_query(type=type or '', turbine=t or '', limit=int(limit or 60)) return dict(rows=(got or {}).get('data', [])) def ont_obj_bundle(id: str = '') -> dict[str, Any]: """`/api/ont_obj`:`{obj: ont_get(id).data, links: ont_links(id).data, err: ont_get(id).error}`""" om = _om() r = om.ont_get(id=id) or {} lk = (om.ont_links(id=id) or {}).get('data', {}) return dict(obj=r.get('data'), links=lk, err=r.get('error')) def ont_chain(kind: str = 'fault', code: str = '', system: str = '', top: int = 10, keyword: str = '', dkind: str = '') -> dict[str, Any]: """`/api/ont_chain`:**逐字照抄**详情层的五路分派与错误形状(含"未知链"与异常包装)。""" om = _om() try: fn = {'fault': lambda: om.ont_chain_fault(code=code or ''), 'prev': lambda: om.ont_chain_preventive(system=system or ''), 'tree': lambda: om.ont_fault_tree(system=system or ''), 'plan': lambda: om.ont_maint_plan(top=int(top or 10)), 'doc': lambda: om.ont_doc_search(keyword=keyword or '', kind=dkind or '')} return fn[kind]() if kind in fn else dict(text='未知链 %s' % kind, error=True) except Exception as e: # noqa: BLE001 return dict(text=('本体查询失败: %s' % e)[:200], error=True)