Files
ssw-ontology/web/src/api.js
T
admin f6ae259574 feat(ontology): 对象类型梳理 - 健康度扫描 + AI 同义判定 + 合并向导
让平台自己就能诊断同义/零实例/孤立/低区分度 4 类问题, 由 manager 审核后执行合并.

后端:
- 新建 server/handlers_ontology_cleanup.go, 4 个接口 (analyze / merge-preview /
  merge-execute / delete-zero-inst), 均走 managerAuth 仅 manager 可调.
- 新表 ontology_audit_log (actor / action / target_type / details / affected_rows),
  analyze / merge / delete-zero-inst 都写审计.
- ai.Provider 接口加 Complete(ctx, prompt) 轻量补全 (anthropic / openai / heuristic 实现),
  ExtractJSON 改为导出.
- db.go DDL 幂等 CREATE TABLE / INDEX.

前端:
- api.js 4 个新 wrapper.
- OntologyView 第 6 个 Tab「类型梳理」: 一键扫描 + 4 类问题清单 +
  同义组展开 (radio 选 target) + 合并预览 dialog + 一键删除按钮.

合并事务要点:
- 先 pk 加 _m<src_id> 后缀再 UPDATE object_type_id, 避免 UNIQUE(object_type_id,pk).
- 同时 UPDATE object_type_id + type_api_name, 保持两字段一致 (否则连接器查不到).
- link.source_type / target_type / link_types.source_type / link_types.target_type 同步改写.
- action_types 没有 target_type 字段 (在 params jsonb), 跳过.

健康度判定:
- 启发式候选: 名称规范化后相同 / Levenshtein ≤2 / HasPrefix 双向 (含
  TrustedCryptoCard vs TrustedCryptographyCard) / 描述 Jaccard ≥ 0.6.
- AI 用 anthropic MiniMax-M3 复核, 给 confidence + reason + 建议 target.
- AI 未明确表态的启发式候选不强行过滤, 标 conf=0.5 让 manager 自己决定.

辅助:
- 提供 docker-compose.ontology.yml 片段给 workspace compose 挂载使用
  (二进制 + archive + env + healthcheck + autoheal 自愈).

测试: 54→39 个对象类型, 13 个零实例一键删 + 2 组同义合并
(FunctionNode→FunctionalNode, TrustedCryptoCard→TrustedCryptographyCard),
审计日志 12 条, AI 实际接入 anthropic:MiniMax-M3.
2026-08-07 19:16:13 +08:00

129 lines
6.8 KiB
JavaScript

// 与尚数网本体平台后端交互的轻量封装(使用 fetch,无需额外依赖)。
const BASE = '/api/v1'
async function request(method, path, body) {
const opts = {
method,
headers: { 'Content-Type': 'application/json' }
}
if (body !== undefined) {
opts.body = JSON.stringify(body)
}
const resp = await fetch(BASE + path, opts)
const text = await resp.text()
let data = null
try { data = text ? JSON.parse(text) : null } catch (e) { data = text }
if (!resp.ok) {
const msg = (data && data.error) ? data.error : `请求失败(${resp.status})`
throw new Error(msg)
}
return data
}
export const api = {
// 本体
listObjectTypes: () => request('GET', '/ontology/object-types'),
// 类型梳理 (analyze / merge / delete-zero-inst) — 仅 manager
analyzeTypes: () => request('POST', '/ontology/types/analyze'),
mergePreview: (body) => request('POST', '/ontology/types/merge-preview', body),
mergeExecute: (body) => request('POST', '/ontology/types/merge-execute', body),
deleteZeroInstTypes: () => request('POST', '/ontology/types/delete-zero-inst'),
getObjectType: (name) => request('GET', `/ontology/object-types/${name}`),
createObjectType: (body) => request('POST', '/ontology/object-types', body),
updateObjectType: (name, body) => request('PUT', `/ontology/object-types/${name}`, body),
deleteObjectType: (name) => request('DELETE', `/ontology/object-types/${name}`),
addProperty: (name, body) => request('POST', `/ontology/object-types/${name}/properties`, body),
updateProperty: (id, body) => request('PUT', `/ontology/properties/${id}`, body),
deleteProperty: (id) => request('DELETE', `/ontology/properties/${id}`),
listLinkTypes: () => request('GET', '/ontology/link-types'),
createLinkType: (body) => request('POST', '/ontology/link-types', body),
deleteLinkType: (name) => request('DELETE', `/ontology/link-types/${name}`),
listActionTypes: () => request('GET', '/ontology/action-types'),
createActionType: (body) => request('POST', '/ontology/action-types', body),
// Function 元原语(Kinetic ⑤)
listFunctionTypes: () => request('GET', '/ontology/function-types'),
getFunctionType: (name) => request('GET', `/ontology/function-types/${name}`),
createFunctionType: (body) => request('POST', '/ontology/function-types', body),
deleteFunctionType: (name) => request('DELETE', `/ontology/function-types/${name}`),
invokeFunction: (name, body) => request('POST', `/ontology/function-types/${name}/invoke`, body),
// Interface 元原语(Semantic ⑦)
listInterfaceTypes: () => request('GET', '/ontology/interface-types'),
getInterfaceType: (name) => request('GET', `/ontology/interface-types/${name}`),
createInterfaceType: (body) => request('POST', '/ontology/interface-types', body),
deleteInterfaceType: (name) => request('DELETE', `/ontology/interface-types/${name}`),
getGraph: () => request('GET', '/ontology/graph'),
// 实例
listObjects: (params = {}) => {
const q = new URLSearchParams(params).toString()
return request('GET', '/objects' + (q ? '?' + q : ''))
},
getObject: (type, pk) => request('GET', `/objects/${type}/${encodeURIComponent(pk)}`),
createObject: (body) => request('POST', '/objects', body),
updateObject: (type, pk, body) => request('PUT', `/objects/${type}/${encodeURIComponent(pk)}`, body),
deleteObject: (type, pk) => request('DELETE', `/objects/${type}/${encodeURIComponent(pk)}`),
createLink: (body) => request('POST', '/links', body),
deleteLink: (id) => request('DELETE', `/links/${id}`),
// AI 摄取
aiIngest: (body) => request('POST', '/ai/ingest', body),
listIngestions: () => request('GET', '/ai/ingest'),
getIngestion: (id) => request('GET', `/ai/ingest/${id}`),
// 连接器
connObjectTypes: () => request('GET', '/connector/object-types'),
connObjects: (type) => request('GET', `/connector/objects/${type}`),
connSearch: (q) => request('GET', `/connector/search?q=${encodeURIComponent(q)}`),
connQuery: (body) => request('POST', '/connector/query', body),
mcpManifest: () => request('GET', '/mcp/manifest'),
// 知识管理
kmTree: () => request('GET', '/km/tree'),
kmListFiles: (folder = '') => request('GET', '/km/files?folder=' + encodeURIComponent(folder)),
kmCreateFolder: (body) => request('POST', '/km/folders', body),
kmUpload: (folder, files) => {
const fd = new FormData()
for (const f of files) fd.append('files', f)
return fetch(BASE + '/km/upload?folder=' + encodeURIComponent(folder), { method: 'POST', body: fd })
.then(r => r.json())
},
kmFileMeta: (rel) => request('GET', '/km/file?rel=' + encodeURIComponent(rel)),
kmExtract: (body) => request('POST', '/km/extract', body),
kmDeleteFiles: (body) => request('DELETE', '/km/files', body),
kmListConnections: () => request('GET', '/km/connections'),
kmCreateConnection: (body) => request('POST', '/km/connections', body),
kmConnectionDetail: (id) => request('GET', `/km/connections/${id}`),
kmDeleteConnection: (id) => request('DELETE', `/km/connections/${id}`),
kmImportConnection: (id) => request('POST', `/km/connections/${id}/import`),
kmExtractConnections: (body) => request('POST', '/km/connections/extract', body),
// 访问控制
whoami: () => request('GET', '/access/whoami'),
listPrincipals: () => request('GET', '/access/principals'),
getPrincipal: (name) => request('GET', `/access/principals/${name}`),
createPrincipal: (body) => request('POST', '/access/principals', body),
updatePrincipal: (name, body) => request('PUT', `/access/principals/${name}`, body),
deletePrincipal: (name) => request('DELETE', `/access/principals/${name}`),
getClearances: (name) => request('GET', `/access/principals/${name}/clearances`),
putClearances: (name, body) => request('PUT', `/access/principals/${name}/clearances`, body),
listGrants: () => request('GET', '/access/grants'),
createGrant: (body) => request('POST', '/access/grants', body),
deleteGrant: (id) => request('DELETE', `/access/grants/${id}`),
listMarkings: () => request('GET', '/access/markings'),
getObjectMarkings: (type, pk) => request('GET', `/access/objects/${type}/${encodeURIComponent(pk)}/markings`),
putObjectMarkings: (type, pk, body) => request('PUT', `/access/objects/${type}/${encodeURIComponent(pk)}/markings`, body),
listFieldPolicies: () => request('GET', '/access/field-policies'),
createFieldPolicy: (body) => request('POST', '/access/field-policies', body),
deleteFieldPolicy: (id) => request('DELETE', `/access/field-policies/${id}`),
previewAsPrincipal: (principal) => request('GET', `/access/preview?principal=${encodeURIComponent(principal)}`),
listAccessLog: (params = {}) => {
const q = new URLSearchParams(params).toString()
return request('GET', '/access/log' + (q ? '?' + q : ''))
},
accessLogStats: () => request('GET', '/access/log/stats')
}