Files
ssw-ontology/server/models.go
T
admin da73b1091f feat(governance): restore_object 恢复关系边 + 数据分级通用能力 (issue #4 #5)
- #4 restore_object 默认连同关系边恢复:删除级联存边快照(links_snapshot),回滚重建两端在的边、跳过悬空引用,分级从快照物化
- #5 数据分级(不硬编码字段名/级别名):
  - 类型级 classification_property/levels/default 配置 + objects.classification 物化列+索引
  - 读路径按 principal_clearances 下推过滤,越级默认屏蔽且不泄漏存在性(fail-closed)
  - 字段级 properties.sensitivity 越级脱敏为 ***
  - 枚举写入校验(修复变量遮蔽导致的永不拦截 bug):越界值 400 enum_violation
  - 批量重算 recompute(dry_run 默认/apply 落库) + KM 源文件级分级传导
  - MCP +3 工具(get_classification_config/recompute_classification/set_km_classification)
- 分级路由改挂 /ontology 避免与 /objects/:type/:pk 参数路由冲突
- 更新 CHANGELOG 与 INSTANCE_GOVERNANCE.md §15-§16
- 验证:localhost:8081 真实库跑通删→恢复边、分级过滤、枚举拦截、字段脱敏、重算落库
2026-09-07 09:45:02 +08:00

198 lines
8.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import "time"
// ============ 本体语义层模型(参考 Palantir Ontology) ============
// ObjectType 对象类型:现实世界实体/事件的 Schema 定义。
type ObjectType struct {
ID int64 `json:"id"`
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
Icon string `json:"icon"`
Group string `json:"group"`
TitleProperty string `json:"title_property"` // 主标题对应 property.api_name
SubtitleProperty string `json:"subtitle_property"` // 副标题对应 property.api_name
Color string `json:"color"` // UI 标识色
PrimaryKey string `json:"primary_key"` // 主键字段名,默认 'pk'
// 数据分级(2026-09-07):Property 为空 = 该类型不启用分级
ClassificationProperty string `json:"classification_property,omitempty"`
ClassificationLevels []string `json:"classification_levels,omitempty"`
ClassificationDefault string `json:"classification_default,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Properties []Property `json:"properties,omitempty"`
}
// Property 属性:对象类型的特征字段。
type Property struct {
ID int64 `json:"id"`
ObjectID int64 `json:"object_type_id"`
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
DataType string `json:"data_type"` // string/integer/number/boolean/date/datetime/enum/json/text
Required bool `json:"required"`
EnumValues []string `json:"enum_values,omitempty"`
Description string `json:"description"`
Format string `json:"format"` // 日期格式 / 数字精度 / 字符串正则
DefaultValue interface{} `json:"default_value"` // 默认值(任意 JSON)
Indexable bool `json:"indexable"` // 关键字检索
VectorIndex bool `json:"vector_index"` // 向量索引(语义检索)
BackingColumn string `json:"backing_column"` // 物化层用
// 字段级敏感度(2026-09-07):值取自所属类型的 classification_levels 词汇,
// 高于主体可见上限时该字段对主体 redact(与 FieldPolicy 并行生效)
Sensitivity string `json:"sensitivity,omitempty"`
}
// LinkType 链接类型:两个对象类型之间的关系边。
type LinkType struct {
ID int64 `json:"id"`
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
SourceType string `json:"source_type"`
TargetType string `json:"target_type"`
Cardinality string `json:"cardinality"` // one_to_many / many_to_many / one_to_one
Description string `json:"description"`
Directional bool `json:"directional"` // 是否单向(默认 false = 双向)
SourceProperty string `json:"source_property"` // 源端 key 字段(默认 pk)
TargetProperty string `json:"target_property"` // 目标端 key 字段(默认 pk)
}
// ActionType 动作类型(动力层):对对象/属性/链接进行修改的预定义操作。
// 完整契约(Palantir Ontology 对标 2026-08-24):
// - Params: form schema 数组,每条 {name, type, required, default, enum, validation, description}
// - SubmissionCriteria: 执行前校验(object_status_in / required_grant 等)
// - SideEffects: 执行后触发(webhooks / cascade_actions / notifications)
// - ImplFunctionID: FBA —— 非空时整条 Action 由 Function 实现
// - ReturnType: 返回类型(string / object_ref / array 等)
type ActionType struct {
ID int64 `json:"id"`
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
Params string `json:"params"` // form schema(JSON 字符串)
SubmissionCriteria string `json:"submission_criteria"` // JSON 字符串
SideEffects string `json:"side_effects"` // JSON 字符串
ImplFunctionID *int64 `json:"impl_function_id"` // FBA;可空
ImplFunctionName string `json:"impl_function_name"` // JOIN 出来,便于 UI 显示
ReturnType string `json:"return_type"`
WhenToSuggest []string `json:"when_to_suggest"`
LLMToolDescription string `json:"llm_tool_description"`
}
// Object 对象实例:对象类型的一个具体实体(数字孪生中的一个节点)。
type Object struct {
ID int64 `json:"id"`
ObjectID int64 `json:"object_type_id"`
TypeAPIName string `json:"type"` // 冗余存储,便于连接器按类型查询
PK string `json:"pk"` // 该实例在所属类型下的唯一主键
Properties map[string]interface{} `json:"properties"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// 实例治理(2026-09-05):软删除墓碑 + 别名归并
Deleted bool `json:"deleted"`
SameAs string `json:"same_as"` // 非空 = 该实例是重复实体,canonical 指向同类型另一个 pk
// 生效分级(2026-09-07):空 = 未分级(视为最低,对任何主体可见)
Classification string `json:"classification,omitempty"`
}
// Link 链接实例:两个对象实例之间的具体关系。
type Link struct {
ID int64 `json:"id"`
LinkType string `json:"type"` // link type api_name
SourceType string `json:"source_type"`
SourcePK string `json:"source_pk"`
TargetType string `json:"target_type"`
TargetPK string `json:"target_pk"`
Properties map[string]interface{} `json:"properties,omitempty"` // 边属性(任职起止时间等)
CreatedAt time.Time `json:"created_at"`
}
// ============ AI 摄取相关 ============
// Ingestion AI 摄取任务记录。
type Ingestion struct {
ID int64 `json:"id"`
Title string `json:"title"`
RawText string `json:"raw_text"`
Directive string `json:"directive"` // 用户的更新要求
Mode string `json:"mode"` // auto(直接应用) / review(仅建议)
Status string `json:"status"` // pending / applied / failed / reviewed
Provider string `json:"provider"` // openai / heuristic
ResultJSON string `json:"result_json"`
Summary string `json:"summary"`
CreatedAt time.Time `json:"created_at"`
AppliedAt *time.Time `json:"applied_at"`
}
// AISuggestion AI 抽取结果结构(与后端抽取提示词对齐)。
type AISuggestion struct {
ObjectTypes []AISuggestedType `json:"object_types"`
LinkTypes []AISuggestedLink `json:"link_types"`
ActionTypes []AISuggestedAction `json:"action_types"`
Objects []AISuggestedObject `json:"objects"`
Links []AISuggestedLinkInst `json:"links"`
Summary string `json:"summary"`
}
type AISuggestedType struct {
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
Group string `json:"group"`
Icon string `json:"icon"`
Properties []AISuggestedProp `json:"properties"`
}
type AISuggestedProp struct {
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
DataType string `json:"data_type"`
Required bool `json:"required"`
EnumValues []string `json:"enum_values"`
Description string `json:"description"`
}
type AISuggestedLink struct {
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
SourceType string `json:"source_type"`
TargetType string `json:"target_type"`
Cardinality string `json:"cardinality"`
Description string `json:"description"`
}
type AISuggestedAction struct {
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
Params string `json:"params"`
}
type AISuggestedObject struct {
Type string `json:"type"`
PK string `json:"pk"`
Properties map[string]interface{} `json:"properties"`
}
type AISuggestedLinkInst struct {
Type string `json:"type"`
SourceType string `json:"source_type"`
SourcePK string `json:"source_pk"`
TargetType string `json:"target_type"`
TargetPK string `json:"target_pk"`
}
// ============ 连接器 / MCP ============
// ConnectorObjectType 连接器精简视图:应用只关心类型名+属性。
type ConnectorObjectType struct {
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
Properties []Property `json:"properties"`
}
// MCPTool MCP 工具清单中的单个工具描述(供 WorkBuddy 等接入)。
type MCPTool struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema map[string]interface{} `json:"inputSchema"`
}