Files
ssw-ontology/server/handlers_objects.go
T
admin 86730bd52d feat: 对标 Palantir Ontology + P0/P1/P2 全落地 + xeo 端到端导入
## Palantir Ontology 对标(10 项)

P0 核心契约:
- when_to_suggest JSONB + llm_tool_description TEXT 加到 function_types/action_types
- Action 完整契约: submission_criteria / side_effects / impl_function_id / return_type
- FBA (Function-backed Action) 执行链: /api/v1/objects/:type/:pk/actions/:action
- /api/v1/ontology/suggestions 提示调度端点

P1 元原语补全:
- Object Type UX: title_property / subtitle_property / color / primary_key
- Property 字段元数据: format / default_value / indexable / vector_index / backing_column
- Link Type: directional + 独立 link_properties 子表
- Interface Type: shared_properties + parent_interface_id 多态
- Property extended types: geoshape / geopoint / media / timeseries / timestamp

P2 数据管道层:
- materializations / materialization_rows 物化层 + 60s ticker scheduler
- backing_sources 同步源元数据 + 投影字段映射

AI 提示词升级:
- ai/prompt.go 抽出 250+ 行静态 systemPrompt
- 动态上下文注入: 每次 Suggest 前查 db 拉已注册类型清单
- Suggestion struct 扩展 22 字段

## ON CONFLICT 漏 source 修复(4 处)
- handlers_objects createObject/updateObject: COALESCE(NULLIF(EXCLUDED.source,''), objects.source)
- handlers_km upsertObject + ai.ApplySuggestion: SET source=EXCLUDED.source
- 表名必须限定 objects.source, 否则报 ambiguous

## xeo mobileApi 端到端导入
- /api/v1/ai/ingest raw_text 改用 json.dumps 转义, 修复 ingestion_id=56 failed
- 1179 实例 + 1672 links + 1806 物化行
- 占位: 5 TrustedDataSpace + 158 MemberOrg
- 5 类 Materialization 配好(mat_xeo_dataproduct 等)

## 文档
- docs/update/2026-08-24.md: 10 节迁移 + 增量 11 节
- .gitignore: 加 web/vite.config.js.timestamp-*.mjs

Refs: docs/update/2026-08-24.md
2026-08-24 13:54:49 +08:00

259 lines
7.5 KiB
Go

package main
import (
"strconv"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgxpool"
)
// ---------- 对象实例 ----------
func (a *app) listObjects(c *gin.Context) {
typ := c.Query("type")
q := c.Query("q")
limit, _ := strconv.Atoi(c.Query("limit"))
if limit <= 0 || limit > 500 {
limit = 100
}
sqlStr := `SELECT id, object_type_id, type_api_name, pk, properties_json, created_at, updated_at FROM objects WHERE 1=1`
args := []interface{}{}
if typ != "" {
args = append(args, typ)
sqlStr += ` AND type_api_name=$` + strconv.Itoa(len(args))
}
if q != "" {
args = append(args, "%"+q+"%")
sqlStr += ` AND properties_json::text ILIKE $` + strconv.Itoa(len(args))
}
args = append(args, limit)
sqlStr += ` ORDER BY id DESC LIMIT $` + strconv.Itoa(len(args))
rows, err := a.pool.Query(c, sqlStr, args...)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
defer rows.Close()
authz, principal := a.authzOf(c)
var out []Object
for rows.Next() {
var o Object
var props []byte
if err := rows.Scan(&o.ID, &o.ObjectID, &o.TypeAPIName, &o.PK, &props, &o.CreatedAt, &o.UpdatedAt); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
mks := a.loadObjectMarkings(c, o.TypeAPIName, o.PK)
if !authz.CanRead(o.TypeAPIName, o.PK, mks) {
continue
}
o.Properties = decodeProps(props)
authz.ApplyFieldPolicy(o.TypeAPIName, o.Properties)
out = append(out, o)
}
c.JSON(200, gin.H{"items": out, "count": len(out), "principal": principal})
}
func (a *app) getObject(c *gin.Context) {
typ := c.Param("type")
pk := c.Param("pk")
authz, principal := a.authzOf(c)
o, err := loadObject(c, a.pool, typ, pk)
if err != nil {
c.JSON(404, gin.H{"error": "对象实例不存在"})
return
}
mks := a.loadObjectMarkings(c, typ, pk)
if !authz.CanRead(typ, pk, mks) {
c.JSON(404, gin.H{"error": "对象实例不存在", "principal": principal})
return
}
if o.Properties != nil {
authz.ApplyFieldPolicy(typ, o.Properties)
}
c.JSON(200, o)
}
type objectInput struct {
Type string `json:"type"`
PK string `json:"pk"`
Properties map[string]interface{} `json:"properties"`
Source string `json:"source"` // 溯源标签(可选);空时保留旧值
}
func (a *app) createObject(c *gin.Context) {
var in objectInput
if err := c.ShouldBindJSON(&in); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if in.Type == "" || in.PK == "" {
c.JSON(400, gin.H{"error": "type 与 pk 必填"})
return
}
authz, _ := a.authzOf(c)
if !authz.CanWrite("write", in.Type, in.PK) {
a.abortForbidden(c, "无 create 权限")
return
}
var oid int64
if err := a.pool.QueryRow(c, `SELECT id FROM object_types WHERE api_name=$1`, in.Type).Scan(&oid); err != nil {
c.JSON(400, gin.H{"error": "对象类型不存在: " + in.Type})
return
}
props := "{}"
if len(in.Properties) > 0 {
props = mustJSON(in.Properties)
}
nameVal, _ := in.Properties["name"].(string)
_, err := a.pool.Exec(c,
`INSERT INTO objects(object_type_id,type_api_name,pk,name,properties_json,source) VALUES($1,$2,$3,$4,$5::jsonb,$6)
ON CONFLICT(object_type_id,pk) DO UPDATE SET
name=EXCLUDED.name,
properties_json=EXCLUDED.properties_json,
source=COALESCE(NULLIF(EXCLUDED.source,''), objects.source),
updated_at=now()`,
oid, in.Type, in.PK, nameVal, props, in.Source)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.JSON(201, gin.H{"ok": true, "type": in.Type, "pk": in.PK})
}
func (a *app) updateObject(c *gin.Context) {
typ := c.Param("type")
pk := c.Param("pk")
var in objectInput
if err := c.ShouldBindJSON(&in); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
authz, _ := a.authzOf(c)
if !authz.CanWrite("write", typ, pk) {
a.abortForbidden(c, "无 write 权限")
return
}
props := "{}"
if len(in.Properties) > 0 {
props = mustJSON(in.Properties)
}
nameVal, _ := in.Properties["name"].(string)
// updateObject 走 UPDATE 路径:source 用 COALESCE 语义,显式非空才覆盖,空保留旧值
tag, err := a.pool.Exec(c,
`UPDATE objects SET name=$1, properties_json=$2::jsonb,
source=COALESCE(NULLIF($3,''), source),
updated_at=now()
WHERE type_api_name=$4 AND pk=$5`,
nameVal, props, in.Source, typ, pk)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
if tag.RowsAffected() == 0 {
c.JSON(404, gin.H{"error": "对象实例不存在"})
return
}
c.JSON(200, gin.H{"ok": true})
}
func (a *app) deleteObject(c *gin.Context) {
typ := c.Param("type")
pk := c.Param("pk")
authz, _ := a.authzOf(c)
if !authz.CanWrite("delete", typ, pk) {
a.abortForbidden(c, "无 delete 权限")
return
}
tag, err := a.pool.Exec(c, `DELETE FROM objects WHERE type_api_name=$1 AND pk=$2`, typ, pk)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
if tag.RowsAffected() == 0 {
c.JSON(404, gin.H{"error": "对象实例不存在"})
return
}
c.JSON(200, gin.H{"ok": true})
}
func loadObject(c *gin.Context, p *pgxpool.Pool, typ, pk string) (*Object, error) {
var o Object
var props []byte
err := p.QueryRow(c,
`SELECT id, object_type_id, type_api_name, pk, properties_json, created_at, updated_at
FROM objects WHERE type_api_name=$1 AND pk=$2`, typ, pk).
Scan(&o.ID, &o.ObjectID, &o.TypeAPIName, &o.PK, &props, &o.CreatedAt, &o.UpdatedAt)
if err != nil {
return nil, err
}
o.Properties = decodeProps(props)
return &o, nil
}
// ---------- 链接实例 ----------
type linkInput 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"`
}
func (a *app) createLink(c *gin.Context) {
var in linkInput
if err := c.ShouldBindJSON(&in); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if in.Type == "" || in.SourceType == "" || in.SourcePK == "" || in.TargetType == "" || in.TargetPK == "" {
c.JSON(400, gin.H{"error": "type/source/target 必填"})
return
}
authz, _ := a.authzOf(c)
if !authz.CanWrite("write", in.SourceType, in.SourcePK) || !authz.CanWrite("write", in.TargetType, in.TargetPK) {
a.abortForbidden(c, "无 write 权限创建此链接")
return
}
// 校验类型存在
var cnt int
if err := a.pool.QueryRow(c, `SELECT count(*) FROM link_types WHERE api_name=$1`, in.Type).Scan(&cnt); err != nil || cnt == 0 {
c.JSON(400, gin.H{"error": "链接类型不存在: " + in.Type})
return
}
_, err := a.pool.Exec(c,
`INSERT INTO links(link_type,source_type,source_pk,target_type,target_pk) VALUES($1,$2,$3,$4,$5)
ON CONFLICT DO NOTHING`, in.Type, in.SourceType, in.SourcePK, in.TargetType, in.TargetPK)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.JSON(201, gin.H{"ok": true})
}
func (a *app) deleteLink(c *gin.Context) {
authz, _ := a.authzOf(c)
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
// 先查链接属于哪些对象
var st, spk, tt, tpk string
if err := a.pool.QueryRow(c, `SELECT source_type, source_pk, target_type, target_pk FROM links WHERE id=$1`, id).Scan(&st, &spk, &tt, &tpk); err == nil {
if !authz.CanWrite("delete", st, spk) || !authz.CanWrite("delete", tt, tpk) {
a.abortForbidden(c, "无 delete 权限")
return
}
}
tag, err := a.pool.Exec(c, `DELETE FROM links WHERE id=$1`, id)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
if tag.RowsAffected() == 0 {
c.JSON(404, gin.H{"error": "链接不存在"})
return
}
c.JSON(200, gin.H{"ok": true})
}