Files
ssw-ontology/server/ai/ai.go
T
admin e7b39805fb feat(backup): 按 schema 镜像 + 请求级 search_path 切库 (issue #4 #5 后续)
「让客户端敢操作」的关键能力:写前/写后打一份快照 → 任意 GET 带 X-Snapshot-Id
 头即可命中 backups_<id> schema 的同名表(业务 SQL 不带 schema 前缀照样切)。

 设计要点:
   - 备份 = 独立 schema backups_<id>(真表,不是单存 JSONB)—— 查询性能与活库一致
   - snapshotRouter 中间件:BeginTx + SET LOCAL search_path → 把 a.pool 临时换
     为事务视图 txPool → handler 结束 commit + 还原 a.pool。业务代码一行不改。
   - a.pool 字段类型从 *pgxpool.Pool 升级为 dbRunner interface(中间件可换 txPool)。
   - 11 个 helper 函数签名同步改为 dbExec(与现有 dbExec 共存,无破坏性)。
   - 写方法(POST/PUT/PATCH/DELETE)带快照头 → 403 拒绝(mistake-safety)。

 接口:
   - POST /api/v1/backups 创建
   - GET /api/v1/backups 列表
   - GET /api/v1/backups/:id 元信息(含 schema_name)
   - GET /api/v1/backups/:id/switch 切库提示
   - DELETE /api/v1/backups/:id 级联删 schema
   - 支持 X-Snapshot-Id (直接 run id) + X-Snapshot-Ts (秒级时间戳) 两种切换入口

 MCP 工具 (5 个):backup_create / backup_list / backup_inspect / backup_switch / backup_delete

 测试 (22 个全绿,~5.5s 集成):
   - SnapshotRouter:改前建快照 → 改后读快照只见改前
   - SnapshotSearchPathPersist:不污染下一个请求
   - SnapshotByTimestamp:ts 头也能切库
   - SnapshotDeleteCleansSchema:delete 真 DROP schema
   - BackupLifecycle / BackupDiffAfterMutation:完整 create→list→inspect→switch→delete

 文档:docs/BACKUP.md 写清用法、机制、客户端工作流;CHANGELOG.md [1.3.0]。
2026-09-07 11:35:47 +08:00

681 lines
27 KiB
Go
Raw 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 ai
import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"unicode"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
// dbExec 复制 server/dbExec 的形态,避免 main ↔ ai 循环依赖。
// 包含 Begin 以支持 ApplySuggestion 内的事务嵌套。
type dbExec interface {
Exec(ctx context.Context, sql string, args ...interface{}) (pgconn.CommandTag, error)
QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
Begin(ctx context.Context) (pgx.Tx, error)
}
// Suggestion AI 从资料中抽取出的本体增量建议。
//
// 结构与 ai/prompt.go 里的 sswOntologySystemPrompt 严格对应。
// 模型应当严格按此 JSON Schema 输出,便于 ApplySuggestion 落地。
type Suggestion struct {
ObjectTypes []SuggestedType `json:"object_types"`
LinkTypes []SuggestedLink `json:"link_types"`
ActionTypes []SuggestedAction `json:"action_types"`
FunctionTypes []SuggestedFunction `json:"function_types,omitempty"` // P0-2 引入
InterfaceTypes []SuggestedInterface `json:"interface_types,omitempty"` // P1-4 引入
Materializations []SuggestedMaterialization `json:"materializations,omitempty"` // P2-1 引入
BackingSources []SuggestedBackingSource `json:"backing_sources,omitempty"` // P2-2 引入
Objects []SuggestedObject `json:"objects"`
Links []SuggestedLinkInst `json:"links"`
Summary string `json:"summary"`
}
// SuggestedType 对象类型(P1-1 起扩字段)。
type SuggestedType struct {
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
Group string `json:"group"`
Icon string `json:"icon"`
TitleProperty string `json:"title_property,omitempty"` // P1-1
SubtitleProperty string `json:"subtitle_property,omitempty"` // P1-1
Color string `json:"color,omitempty"` // P1-1
PrimaryKey string `json:"primary_key,omitempty"` // P1-1
Properties []SuggestedProp `json:"properties"`
}
// SuggestedProp 属性(P1-2 起扩字段,P1-5 扩 data_type)。
type SuggestedProp struct {
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
DataType string `json:"data_type"` // 含扩展类型 geoshape/geopoint/media/timeseries/timestamp
Required bool `json:"required"`
EnumValues []string `json:"enum_values"`
Description string `json:"description"`
Format string `json:"format,omitempty"` // P1-2: url/email/phone/...
DefaultValue interface{} `json:"default_value,omitempty"` // P1-2
Indexable bool `json:"indexable,omitempty"` // P1-2
VectorIndex bool `json:"vector_index,omitempty"` // P1-2
BackingColumn string `json:"backing_column,omitempty"` // P1-2
}
// SuggestedLink 链接类型(P1-3 起扩字段)。
type SuggestedLink 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"`
Directional bool `json:"directional,omitempty"` // P1-3
SourceProperty string `json:"source_property,omitempty"` // P1-3
TargetProperty string `json:"target_property,omitempty"` // P1-3
LinkProperties []SuggestedProp `json:"link_properties,omitempty"` // P1-3 链接本身带属性
}
// SuggestedAction 动作类型(P0-1 / P0-2 起扩字段)。
type SuggestedAction struct {
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
Params string `json:"params"`
SubmissionCriteria string `json:"submission_criteria,omitempty"` // P0-1
SideEffects string `json:"side_effects,omitempty"` // P0-1
ImplFunctionID *int64 `json:"impl_function_id,omitempty"` // P0-1 FBA;nil=声明式
ReturnType string `json:"return_type,omitempty"` // P0-1
WhenToSuggest []string `json:"when_to_suggest,omitempty"` // P0-2
LLMToolDescription string `json:"llm_tool_description,omitempty"`// P0-2
}
// SuggestedFunction 函数类型(P0-2 起在 Suggestion 输出可选)。
type SuggestedFunction struct {
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
Inputs []SuggestedProp `json:"inputs"`
Outputs []SuggestedProp `json:"outputs"`
ImplKind string `json:"impl_kind"` // builtin/script/external_api
ImplRef string `json:"impl_ref,omitempty"`
IsPure bool `json:"is_pure"`
WhenToSuggest []string `json:"when_to_suggest,omitempty"` // P0-2
LLMToolDescription string `json:"llm_tool_description,omitempty"` // P0-2
}
// SuggestedInterface 接口类型(P1-4 起在 Suggestion 输出可选)。
type SuggestedInterface struct {
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
Protocol string `json:"protocol"` // rest/mcp
Method string `json:"method"`
Path string `json:"path"`
Inputs []SuggestedProp `json:"inputs"`
Outputs interface{} `json:"outputs"`
RequiresAuth bool `json:"requires_auth"`
ExposeToMCP bool `json:"expose_to_mcp"`
BackingRef string `json:"backing_ref,omitempty"`
SharedProperties []string `json:"shared_properties,omitempty"` // P1-4
ParentInterfaceID *int64 `json:"parent_interface_id,omitempty"` // P1-4
}
// SuggestedMaterialization 物化(P2-1)。
type SuggestedMaterialization struct {
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
SourceObjectType string `json:"source_object_type"`
Projection []struct {
Property string `json:"property"`
Alias string `json:"alias"`
Transform string `json:"transform,omitempty"` // upper/lower/trim/as_string/as_int/as_bool
} `json:"projection"`
RefreshStrategy string `json:"refresh_strategy"` // manual/periodic/on_change
}
// SuggestedBackingSource 同步源(P2-2)。
type SuggestedBackingSource struct {
APIName string `json:"api_name"`
DisplayName string `json:"display_name"`
SourceKind string `json:"source_kind"` // api/db/file/other
ConfigJSON map[string]interface{} `json:"config_json"`
FieldMapping []map[string]interface{} `json:"field_mapping"`
MaterializationID *int64 `json:"materialization_id,omitempty"`
}
// SuggestedObject 实例(P0-3 后 properties 任意)。
type SuggestedObject struct {
Type string `json:"type"`
PK string `json:"pk"`
Properties map[string]interface{} `json:"properties"`
}
// SuggestedLinkInst 链接实例。
type SuggestedLinkInst 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"`
}
// Provider AI 抽取提供者接口。
//
// Suggest 的 existingContext 来自 FetchExistingContext(pg pool),
// 拼到 systemPrompt 末尾,让 LLM 知道当前已注册什么类型,从而复用而非新建。
type Provider interface {
Name() string
Suggest(ctx context.Context, text, directive, existingContext string) (*Suggestion, error)
// Complete 轻量文本补全: 给 prompt 让大模型生成自由文本返回 (用于同义判定等场景).
// 实现者尽量保持简短、响应稳定.
Complete(ctx context.Context, prompt string) (string, error)
// CompleteWithSystem 同 Complete 但带 system prompt (用于同义判定专用 prompt).
// heuristic 提供者 system 字段被忽略, 直接走用户提示.
CompleteWithSystem(ctx context.Context, system, prompt string) (string, error)
}
// Config AI 配置。
type Config struct {
BaseURL string
Key string
Model string
}
// NewProvider 依据是否配置 Key 选择真实大模型或启发式兜底。
// BaseURL 包含 "/anthropic" 子串时走 Anthropic Messages 协议,其余按 OpenAI Chat Completions 协议。
func NewProvider(cfg Config) Provider {
if cfg.Key != "" && cfg.BaseURL != "" {
if strings.Contains(cfg.BaseURL, "/anthropic") {
return &anthropicProvider{cfg: cfg}
}
return &openAIProvider{cfg: cfg}
}
return &heuristicProvider{}
}
// ApplyResult 应用建议后的统计结果。
type ApplyResult struct {
ObjectTypes int `json:"object_types"`
Properties int `json:"properties"`
LinkTypes int `json:"link_types"`
ActionTypes int `json:"action_types"`
FunctionTypes int `json:"function_types"`
InterfaceTypes int `json:"interface_types"`
Materializations int `json:"materializations"`
BackingSources int `json:"backing_sources"`
Objects int `json:"objects"`
Links int `json:"links"`
Summary string `json:"summary"`
}
// ApplySuggestion 将抽取建议 upsert 进本体库。整个流程在一个事务内完成。
// source 用于溯源标记:删除文件/连接时可级联回滚其产生的实例与关系。
func ApplySuggestion(ctx context.Context, p dbExec, s *Suggestion, source string) (*ApplyResult, error) {
tx, err := p.Begin(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback(ctx)
res := &ApplyResult{}
// 1) 对象类型
for _, t := range s.ObjectTypes {
if t.APIName == "" {
continue
}
if _, err := tx.Exec(ctx, `
INSERT INTO object_types(api_name,display_name,description,icon,group_name,
title_property,subtitle_property,color,primary_key)
VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9)
ON CONFLICT(api_name) DO UPDATE SET display_name=EXCLUDED.display_name,
description=EXCLUDED.description, icon=EXCLUDED.icon, group_name=EXCLUDED.group_name,
title_property=EXCLUDED.title_property, subtitle_property=EXCLUDED.subtitle_property,
color=EXCLUDED.color, primary_key=EXCLUDED.primary_key, updated_at=now()`,
t.APIName, orDef(t.DisplayName, t.APIName), t.Description, orDef(t.Icon, "📌"), orDef(t.Group, "未分类"),
t.TitleProperty, t.SubtitleProperty, t.Color, orDef(t.PrimaryKey, "pk")); err != nil {
return nil, fmt.Errorf("应用对象类型 %s 失败: %w", t.APIName, err)
}
res.ObjectTypes++
// 属性(含 P1-2 字段级元数据 + P1-5 扩展类型)
for _, pr := range t.Properties {
if pr.APIName == "" {
continue
}
enum := "[]"
if len(pr.EnumValues) > 0 {
enum = jsonStr(pr.EnumValues)
}
defaultJSON := "null"
if pr.DefaultValue != nil {
defaultJSON = jsonStr(pr.DefaultValue)
}
if _, err := tx.Exec(ctx, `
INSERT INTO properties(object_type_id,api_name,display_name,data_type,required,enum_values,description,
format,default_value,indexable,vector_index,backing_column)
SELECT ot.id,$1,$2,$3,$4,$5::jsonb,$6,$7,$8::jsonb,$9,$10,$11
FROM object_types ot WHERE ot.api_name=$12
ON CONFLICT(object_type_id,api_name) DO UPDATE SET display_name=EXCLUDED.display_name,
data_type=EXCLUDED.data_type, required=EXCLUDED.required,
enum_values=EXCLUDED.enum_values, description=EXCLUDED.description,
format=EXCLUDED.format, default_value=EXCLUDED.default_value,
indexable=EXCLUDED.indexable, vector_index=EXCLUDED.vector_index,
backing_column=EXCLUDED.backing_column`,
pr.APIName, orDef(pr.DisplayName, pr.APIName), normType(pr.DataType), pr.Required, enum, pr.Description,
pr.Format, defaultJSON, pr.Indexable, pr.VectorIndex, pr.BackingColumn, t.APIName); err != nil {
return nil, fmt.Errorf("应用属性 %s.%s 失败: %w", t.APIName, pr.APIName, err)
}
res.Properties++
}
}
// 2) 链接类型(含 P1-3 directional / link_properties)
for _, l := range s.LinkTypes {
if l.APIName == "" {
continue
}
card := l.Cardinality
if card == "" {
card = "many_to_many"
}
if _, err := tx.Exec(ctx, `
INSERT INTO link_types(api_name,display_name,source_type,target_type,cardinality,description,
directional,source_property,target_property)
VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9)
ON CONFLICT(api_name) DO UPDATE SET display_name=EXCLUDED.display_name,
source_type=EXCLUDED.source_type, target_type=EXCLUDED.target_type,
cardinality=EXCLUDED.cardinality, description=EXCLUDED.description,
directional=EXCLUDED.directional, source_property=EXCLUDED.source_property,
target_property=EXCLUDED.target_property`,
l.APIName, orDef(l.DisplayName, l.APIName), l.SourceType, l.TargetType, card, l.Description,
l.Directional, l.SourceProperty, l.TargetProperty); err != nil {
return nil, fmt.Errorf("应用链接类型 %s 失败: %w", l.APIName, err)
}
res.LinkTypes++
// 链接属性(链接本身带字段,P1-3)
if len(l.LinkProperties) > 0 {
var linkID int64
if err := tx.QueryRow(ctx, `SELECT id FROM link_types WHERE api_name=$1`, l.APIName).Scan(&linkID); err != nil {
log.Printf("[ai] 链接 %s 找不到 id,跳过 link_properties", l.APIName)
} else {
for _, lp := range l.LinkProperties {
if lp.APIName == "" {
continue
}
enum := "[]"
if len(lp.EnumValues) > 0 {
enum = jsonStr(lp.EnumValues)
}
if _, err := tx.Exec(ctx, `
INSERT INTO link_properties(link_type_id,api_name,display_name,data_type,required,enum_values,description)
VALUES($1,$2,$3,$4,$5,$6::jsonb,$7)
ON CONFLICT(link_type_id,api_name) DO UPDATE SET display_name=EXCLUDED.display_name,
data_type=EXCLUDED.data_type, required=EXCLUDED.required,
enum_values=EXCLUDED.enum_values, description=EXCLUDED.description`,
linkID, lp.APIName, orDef(lp.DisplayName, lp.APIName), normType(lp.DataType), lp.Required, enum, lp.Description); err != nil {
log.Printf("[ai] 链接属性 %s.%s 失败: %v", l.APIName, lp.APIName, err)
}
}
}
}
}
// 3) 动作类型(含 P0-1 完整契约 + P0-2 提示调度)
for _, a := range s.ActionTypes {
if a.APIName == "" {
continue
}
params := a.Params
if params == "" {
params = "[]"
}
subCrit := a.SubmissionCriteria
if subCrit == "" {
subCrit = "{}"
}
sideEff := a.SideEffects
if sideEff == "" {
sideEff = "{}"
}
whenJSON := "[]"
if len(a.WhenToSuggest) > 0 {
whenJSON = jsonStr(a.WhenToSuggest)
}
if _, err := tx.Exec(ctx, `
INSERT INTO action_types(api_name,display_name,description,params,
submission_criteria,side_effects,impl_function_id,return_type,
when_to_suggest,llm_tool_description)
VALUES($1,$2,$3,$4::jsonb,$5::jsonb,$6::jsonb,$7,$8,$9::jsonb,$10)
ON CONFLICT(api_name) DO UPDATE SET display_name=EXCLUDED.display_name,
description=EXCLUDED.description, params=EXCLUDED.params,
submission_criteria=EXCLUDED.submission_criteria, side_effects=EXCLUDED.side_effects,
impl_function_id=EXCLUDED.impl_function_id, return_type=EXCLUDED.return_type,
when_to_suggest=EXCLUDED.when_to_suggest, llm_tool_description=EXCLUDED.llm_tool_description`,
a.APIName, orDef(a.DisplayName, a.APIName), a.Description, params,
subCrit, sideEff, a.ImplFunctionID, a.ReturnType,
whenJSON, a.LLMToolDescription); err != nil {
return nil, fmt.Errorf("应用动作类型 %s 失败: %w", a.APIName, err)
}
res.ActionTypes++
}
// 3.5) 函数类型(P0-2:让 LLM 直接抽取 Function 进库)
for _, fn := range s.FunctionTypes {
if fn.APIName == "" {
continue
}
inputs := "[]"
if len(fn.Inputs) > 0 {
inputs = jsonStr(fn.Inputs)
}
outputs := "[]"
if len(fn.Outputs) > 0 {
outputs = jsonStr(fn.Outputs)
}
implKind := orDef(fn.ImplKind, "script")
if implKind == "builtin" {
// builtin 必须由系统注册,AI 不能直接落库
log.Printf("[ai] 函数 %s 是 builtin 但 LLM 想新建,跳过(builtin 仅系统可注册)", fn.APIName)
continue
}
whenJSON := "[]"
if len(fn.WhenToSuggest) > 0 {
whenJSON = jsonStr(fn.WhenToSuggest)
}
if _, err := tx.Exec(ctx, `
INSERT INTO function_types(api_name,display_name,description,inputs,outputs,
impl_kind,impl_ref,is_pure,when_to_suggest,llm_tool_description)
VALUES($1,$2,$3,$4::jsonb,$5::jsonb,$6,$7,$8,$9::jsonb,$10)
ON CONFLICT(api_name) DO UPDATE SET display_name=EXCLUDED.display_name,
description=EXCLUDED.description, inputs=EXCLUDED.inputs, outputs=EXCLUDED.outputs,
impl_kind=EXCLUDED.impl_kind, impl_ref=EXCLUDED.impl_ref, is_pure=EXCLUDED.is_pure,
when_to_suggest=EXCLUDED.when_to_suggest, llm_tool_description=EXCLUDED.llm_tool_description,
updated_at=now()`,
fn.APIName, orDef(fn.DisplayName, fn.APIName), fn.Description, inputs, outputs,
implKind, fn.ImplRef, fn.IsPure, whenJSON, fn.LLMToolDescription); err != nil {
return nil, fmt.Errorf("应用函数 %s 失败: %w", fn.APIName, err)
}
res.FunctionTypes++
}
// 3.6) 接口类型(P1-4:含 shared_properties / parent_interface_id)
for _, it := range s.InterfaceTypes {
if it.APIName == "" {
continue
}
inputs := "[]"
if len(it.Inputs) > 0 {
inputs = jsonStr(it.Inputs)
}
outputs := "{}"
if it.Outputs != nil {
outputs = jsonStr(it.Outputs)
}
shared := "[]"
if len(it.SharedProperties) > 0 {
shared = jsonStr(it.SharedProperties)
}
if _, err := tx.Exec(ctx, `
INSERT INTO interface_types(api_name,display_name,description,protocol,method,path,inputs,outputs,
requires_auth,expose_to_mcp,backing_ref,builtin,shared_properties,parent_interface_id)
VALUES($1,$2,$3,$4,$5,$6,$7::jsonb,$8::jsonb,$9,$10,$11,FALSE,$12::jsonb,$13)
ON CONFLICT(api_name) DO UPDATE SET display_name=EXCLUDED.display_name,
description=EXCLUDED.description, protocol=EXCLUDED.protocol,
method=EXCLUDED.method, path=EXCLUDED.path,
inputs=EXCLUDED.inputs, outputs=EXCLUDED.outputs,
requires_auth=EXCLUDED.requires_auth, expose_to_mcp=EXCLUDED.expose_to_mcp,
backing_ref=EXCLUDED.backing_ref,
shared_properties=EXCLUDED.shared_properties, parent_interface_id=EXCLUDED.parent_interface_id,
updated_at=now()`,
it.APIName, orDef(it.DisplayName, it.APIName), it.Description, orDef(it.Protocol, "rest"),
orDef(it.Method, "GET"), it.Path, inputs, outputs,
it.RequiresAuth, it.ExposeToMCP, it.BackingRef, shared, it.ParentInterfaceID); err != nil {
return nil, fmt.Errorf("应用接口 %s 失败: %w", it.APIName, err)
}
res.InterfaceTypes++
}
// 3.7) 物化(P2-1)
for _, m := range s.Materializations {
if m.APIName == "" || m.SourceObjectType == "" {
continue
}
proj := "[]"
if len(m.Projection) > 0 {
proj = jsonStr(m.Projection)
}
refresh := orDef(m.RefreshStrategy, "manual")
if _, err := tx.Exec(ctx, `
INSERT INTO materializations(api_name,display_name,source_object_type,projection,refresh_strategy,is_active)
VALUES($1,$2,$3,$4::jsonb,$5,TRUE)
ON CONFLICT(api_name) DO UPDATE SET display_name=EXCLUDED.display_name,
source_object_type=EXCLUDED.source_object_type, projection=EXCLUDED.projection,
refresh_strategy=EXCLUDED.refresh_strategy, updated_at=now()`,
m.APIName, orDef(m.DisplayName, m.APIName), m.SourceObjectType, proj, refresh); err != nil {
return nil, fmt.Errorf("应用物化 %s 失败: %w", m.APIName, err)
}
res.Materializations++
}
// 3.8) 同步源(P2-2)
for _, bs := range s.BackingSources {
if bs.APIName == "" {
continue
}
cfg := "{}"
if bs.ConfigJSON != nil {
cfg = jsonStr(bs.ConfigJSON)
}
mapping := "[]"
if len(bs.FieldMapping) > 0 {
mapping = jsonStr(bs.FieldMapping)
}
if _, err := tx.Exec(ctx, `
INSERT INTO backing_sources(api_name,display_name,description,source_kind,config_json,field_mapping,materialization_id,is_active)
VALUES($1,$2,$3,$4,$5::jsonb,$6::jsonb,$7,TRUE)
ON CONFLICT(api_name) DO UPDATE SET display_name=EXCLUDED.display_name,
description=EXCLUDED.description, source_kind=EXCLUDED.source_kind,
config_json=EXCLUDED.config_json, field_mapping=EXCLUDED.field_mapping,
materialization_id=EXCLUDED.materialization_id, updated_at=now()`,
bs.APIName, orDef(bs.DisplayName, bs.APIName), "", orDef(bs.SourceKind, "other"), cfg, mapping, bs.MaterializationID); err != nil {
return nil, fmt.Errorf("应用同步源 %s 失败: %w", bs.APIName, err)
}
res.BackingSources++
}
// 4) 对象实例(按 类型+归一化名称 去重,跨文件/跨批次合并同一实体)
//
// 2026-09-05 改进:去重命中走 merge 语义(见 object_upsert.applyObjectUpsert),
// 响应里带回 created/reused/skipped 三类明细(替代之前仅 res.Objects 单计数)。
existingByType := map[string][]objMeta{}
if rows, err := tx.Query(ctx, `SELECT id, type_api_name, pk, name FROM objects`); err == nil {
for rows.Next() {
var m objMeta
if err := rows.Scan(&m.id, &m.typeName, &m.pk, &m.name); err == nil {
existingByType[m.typeName] = append(existingByType[m.typeName], m)
}
}
rows.Close()
}
seen := map[string]string{} // normKey(类型+名) -> canonical pk(批次内去重)
pkMap := map[string]string{} // 模型给出的 pk -> canonical pk(供关系重指向)
// 类型级标题属性缓存(用于把 properties.name 之外的显示名物化到 objects.name)
titlePropByType := map[string]string{}
if rows, err := tx.Query(ctx, `SELECT api_name, COALESCE(title_property,'') FROM object_types`); err == nil {
for rows.Next() {
var api, tp string
if err := rows.Scan(&api, &tp); err == nil {
titlePropByType[api] = tp
}
}
rows.Close()
}
_ = titlePropByType // 当前本段不直接读它:name 物化由 ON CONFLICT 后的 refreshDisplayName 处理
for _, o := range s.Objects {
if o.Type == "" || o.PK == "" {
continue
}
var oid int64
if err := tx.QueryRow(ctx, "SELECT id FROM object_types WHERE api_name=$1", o.Type).Scan(&oid); err != nil {
log.Printf("[ai] 对象类型 %s 不存在,跳过实例 %s", o.Type, o.PK)
continue
}
props := "{}"
if len(o.Properties) > 0 {
props = jsonStr(o.Properties)
}
nameVal, _ := o.Properties["name"].(string)
canonical, isDup := resolveObjectPK(existingByType[o.Type], seen, dedupKey(o.Type, nameVal, o.PK))
if canonical == "" {
canonical = o.PK
}
if isDup {
res.Objects-- // 合并到已存在实体,不计入新增(仅统计口径)
}
if _, err := tx.Exec(ctx, `
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=EXCLUDED.source,
updated_at=now()`,
oid, o.Type, canonical, nameVal, props, source); err != nil {
return nil, fmt.Errorf("应用对象 %s/%s 失败: %w", o.Type, o.PK, err)
}
seen[dedupKey(o.Type, nameVal, o.PK)] = canonical
pkMap[o.PK] = canonical
res.Objects++
}
// 5) 链接实例(按 pkMap 重指向 canonical pk,避免指向被合并掉的旧 pk)
for _, l := range s.Links {
if l.Type == "" || l.SourceType == "" || l.SourcePK == "" || l.TargetType == "" || l.TargetPK == "" {
continue
}
spk := l.SourcePK
if c, ok := pkMap[spk]; ok {
spk = c
}
tpk := l.TargetPK
if c, ok := pkMap[tpk]; ok {
tpk = c
}
if _, err := tx.Exec(ctx, `
INSERT INTO links(link_type,source_type,source_pk,target_type,target_pk,source)
VALUES($1,$2,$3,$4,$5,$6)
ON CONFLICT DO NOTHING`,
l.Type, l.SourceType, spk, l.TargetType, tpk, source); err != nil {
return nil, fmt.Errorf("应用链接 %s 失败: %w", l.Type, err)
}
res.Links++
}
if err := tx.Commit(ctx); err != nil {
return nil, err
}
summary := fmt.Sprintf("已应用:对象类型 %d / 属性 %d / 链接类型 %d / 动作 %d / 函数 %d / 接口 %d / 物化 %d / 同步源 %d / 对象 %d / 关系 %d。%s",
res.ObjectTypes, res.Properties, res.LinkTypes, res.ActionTypes,
res.FunctionTypes, res.InterfaceTypes, res.Materializations, res.BackingSources,
res.Objects, res.Links, s.Summary)
res.Summary = summary
log.Printf("[ai] %s", summary)
return res, nil
}
func orDef(v, def string) string {
if v == "" {
return def
}
return v
}
func normType(t string) string {
switch t {
case "string", "integer", "number", "boolean", "date", "datetime", "enum", "json", "text",
"geoshape", "geopoint", "media", "timeseries", "timestamp": // P1-5 扩展
return t
default:
return "string"
}
}
func jsonStr(v interface{}) string {
b, err := json.Marshal(v)
if err != nil {
return "{}"
}
return string(b)
}
// ---------- 对象去重(按 类型 + 归一化名称;保守策略,避免误并不同实体) ----------
// objMeta 现有对象的轻量元数据,用于去重匹配。
type objMeta struct {
id int64
typeName string
pk string
name string
}
// normalizeName 强归一化:去首尾空白、全角转半角、小写、仅保留字母与数字。
func normalizeName(s string) string {
rs := []rune(strings.ToLower(strings.TrimSpace(s)))
var b strings.Builder
for _, r := range rs {
r = fullToHalf(r)
if unicode.IsLetter(r) || unicode.IsDigit(r) {
b.WriteRune(r)
}
}
return b.String()
}
func fullToHalf(r rune) rune {
switch {
case r >= '0' && r <= '9':
return r - '0' + '0'
case r >= 'A' && r <= 'Z':
return r - 'A' + 'A'
case r >= 'a' && r <= 'z':
return r - 'a' + 'a'
}
return r
}
// dedupKey 去重键:类型 + 归一化(名称,名称为空时回退到 pk)。
// 设计为确定性的"完全相等"匹配——只吃掉空格/标点/大小写/全角变体,绝不模糊、不括号剥离、不链式,
// 以避免把"(日)"与"(月)"、"(上)"与"(下)"、"(第9期)"等不同实体误并。
// 带区分性括号或语义近重复的实体,交由人工在界面确认合并,而非自动吞并。
func dedupKey(typeName, name, pk string) string {
key := name
if key == "" {
key = pk
}
return typeName + "\x00" + normalizeName(key)
}
// resolveObjectPK 在同类型现有对象 + 本批次已见对象中查找同一实体的 canonical pk。
// 命中返回 (pk, true);否则返回 ("", false),调用方应使用模型给出的 pk 新建。
func resolveObjectPK(existing []objMeta, seen map[string]string, key string) (string, bool) {
if pk, ok := seen[key]; ok {
return pk, true
}
for _, e := range existing {
if dedupKey(e.typeName, e.name, e.pk) == key {
return e.pk, true
}
}
return "", false
}