- 将 shangshu-ontology/ 子目录内容移至仓库根一级 - 修复 run.sh 的 REPO_ROOT 计算(扁平化后不再需要 cd ..),ARCHIVE_DIR 正确指向仓库根/archive - 文档(README/PLAN/USAGE/IMPORT/product-intro)移至 docs/,同步更新结构树与导入命令中的旧路径 - 新增根 README.md 索引指向 docs/ - 新增 .gitignore:忽略 bin/、web/node_modules/、web/dist/、server/webroot/、.env、.workbuddy/ 等噪音 - main.go 的 ARCHIVE_DIR fallback 基于可执行文件相邻上级,扁平化后仍正确,无需改动
344 lines
12 KiB
Go
344 lines
12 KiB
Go
package main
|
||
|
||
// Function 元原语 REST API
|
||
// GET /api/v1/ontology/function-types 列出全部
|
||
// GET /api/v1/ontology/function-types/:name 单个
|
||
// POST /api/v1/ontology/function-types 新建(自定义 script/external_api)
|
||
// DELETE /api/v1/ontology/function-types/:name 删除
|
||
// POST /api/v1/ontology/function-types/:name/invoke 按 api_name 调用(manager 可调)
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"log"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// FunctionType 是返回结构(inputs/outputs 解码成 map)
|
||
type FunctionTypeOut struct {
|
||
ID int64 `json:"id"`
|
||
APIName string `json:"api_name"`
|
||
DisplayName string `json:"display_name"`
|
||
Description string `json:"description"`
|
||
Inputs interface{} `json:"inputs"`
|
||
Outputs interface{} `json:"outputs"`
|
||
ImplKind string `json:"impl_kind"`
|
||
ImplRef string `json:"impl_ref"`
|
||
IsPure bool `json:"is_pure"`
|
||
BuiltinFn string `json:"builtin_fn"`
|
||
}
|
||
|
||
func scanFunctionRow(scan func(...any) error) (FunctionTypeOut, error) {
|
||
var f FunctionTypeOut
|
||
var inputs, outputs []byte
|
||
if err := scan(&f.ID, &f.APIName, &f.DisplayName, &f.Description, &inputs, &outputs,
|
||
&f.ImplKind, &f.ImplRef, &f.IsPure, &f.BuiltinFn); err != nil {
|
||
return f, err
|
||
}
|
||
f.Inputs = jsonRaw(inputs)
|
||
f.Outputs = jsonRaw(outputs)
|
||
return f, nil
|
||
}
|
||
|
||
func (a *app) listFunctionTypes(c *gin.Context) {
|
||
rows, err := a.pool.Query(c,
|
||
`SELECT id, api_name, display_name, description, inputs, outputs, impl_kind, impl_ref, is_pure, builtin_fn
|
||
FROM function_types ORDER BY id`)
|
||
if err != nil {
|
||
c.JSON(500, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
defer rows.Close()
|
||
var out []FunctionTypeOut
|
||
for rows.Next() {
|
||
f, err := scanFunctionRow(rows.Scan)
|
||
if err == nil {
|
||
out = append(out, f)
|
||
}
|
||
}
|
||
c.JSON(200, gin.H{"function_types": out, "count": len(out)})
|
||
}
|
||
|
||
func (a *app) getFunctionType(c *gin.Context) {
|
||
name := c.Param("name")
|
||
f, err := scanFunctionRow(func(args ...any) error {
|
||
return a.pool.QueryRow(c,
|
||
`SELECT id, api_name, display_name, description, inputs, outputs, impl_kind, impl_ref, is_pure, builtin_fn
|
||
FROM function_types WHERE api_name=$1`, name).Scan(args...)
|
||
})
|
||
if err != nil {
|
||
c.JSON(404, gin.H{"error": "function_type 不存在"})
|
||
return
|
||
}
|
||
c.JSON(200, f)
|
||
}
|
||
|
||
type functionTypeInput struct {
|
||
APIName string `json:"api_name"`
|
||
DisplayName string `json:"display_name"`
|
||
Description string `json:"description"`
|
||
Inputs string `json:"inputs"`
|
||
Outputs string `json:"outputs"`
|
||
ImplKind string `json:"impl_kind"`
|
||
ImplRef string `json:"impl_ref"`
|
||
IsPure bool `json:"is_pure"`
|
||
}
|
||
|
||
func (a *app) createFunctionType(c *gin.Context) {
|
||
var in functionTypeInput
|
||
if err := c.ShouldBindJSON(&in); err != nil {
|
||
c.JSON(400, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
if in.APIName == "" || in.DisplayName == "" {
|
||
c.JSON(400, gin.H{"error": "api_name/display_name 必填"})
|
||
return
|
||
}
|
||
if in.Inputs == "" {
|
||
in.Inputs = "[]"
|
||
}
|
||
if in.Outputs == "" {
|
||
in.Outputs = "[]"
|
||
}
|
||
if in.ImplKind == "" {
|
||
in.ImplKind = "script"
|
||
}
|
||
if in.ImplKind == "builtin" {
|
||
// builtin 只能由系统注册(builtin_fn 必填 = api_name)
|
||
c.JSON(400, gin.H{"error": "builtin 类型不可手动新建,请改名为 script/external_api"})
|
||
return
|
||
}
|
||
_, err := a.pool.Exec(c,
|
||
`INSERT INTO function_types(api_name,display_name,description,inputs,outputs,impl_kind,impl_ref,is_pure)
|
||
VALUES($1,$2,$3,$4::jsonb,$5::jsonb,$6,$7,$8)
|
||
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,
|
||
updated_at=now()`,
|
||
in.APIName, in.DisplayName, in.Description, in.Inputs, in.Outputs, in.ImplKind, in.ImplRef, in.IsPure)
|
||
if err != nil {
|
||
c.JSON(409, gin.H{"error": "创建失败: " + err.Error()})
|
||
return
|
||
}
|
||
c.JSON(201, gin.H{"ok": true, "api_name": in.APIName})
|
||
}
|
||
|
||
func (a *app) deleteFunctionType(c *gin.Context) {
|
||
name := c.Param("name")
|
||
tag, err := a.pool.Exec(c, `DELETE FROM function_types WHERE api_name=$1 AND impl_kind<>'builtin'`, name)
|
||
if err != nil {
|
||
c.JSON(500, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
if tag.RowsAffected() == 0 {
|
||
c.JSON(404, gin.H{"error": "function_type 不存在或为内置(不可删)"})
|
||
return
|
||
}
|
||
c.JSON(200, gin.H{"ok": true})
|
||
}
|
||
|
||
func (a *app) invokeFunctionType(c *gin.Context) {
|
||
name := c.Param("name")
|
||
var args map[string]interface{}
|
||
if err := c.ShouldBindJSON(&args); err != nil {
|
||
c.JSON(400, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
out, err := invokeFunction(c, a, name, args)
|
||
if err != nil {
|
||
c.JSON(500, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
c.JSON(200, gin.H{"ok": true, "function": name, "result": out})
|
||
}
|
||
|
||
// ============ Interface 元原语(⑦Interface)============
|
||
|
||
type InterfaceTypeOut struct {
|
||
ID int64 `json:"id"`
|
||
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 interface{} `json:"inputs"`
|
||
Outputs interface{} `json:"outputs"`
|
||
RequiresAuth bool `json:"requires_auth"`
|
||
ExposeToMCP bool `json:"expose_to_mcp"`
|
||
BackingRef string `json:"backing_ref"`
|
||
Builtin bool `json:"builtin"`
|
||
}
|
||
|
||
func scanInterfaceRow(scan func(...any) error) (InterfaceTypeOut, error) {
|
||
var it InterfaceTypeOut
|
||
var inputs, outputs []byte
|
||
if err := scan(&it.ID, &it.APIName, &it.DisplayName, &it.Description, &it.Protocol, &it.Method, &it.Path,
|
||
&inputs, &outputs, &it.RequiresAuth, &it.ExposeToMCP, &it.BackingRef, &it.Builtin); err != nil {
|
||
return it, err
|
||
}
|
||
it.Inputs = jsonRaw(inputs)
|
||
it.Outputs = jsonRaw(outputs)
|
||
return it, nil
|
||
}
|
||
|
||
func (a *app) listInterfaceTypes(c *gin.Context) {
|
||
rows, err := a.pool.Query(c,
|
||
`SELECT id, api_name, display_name, description, protocol, method, path, inputs, outputs,
|
||
requires_auth, expose_to_mcp, backing_ref, builtin
|
||
FROM interface_types ORDER BY protocol, method, api_name`)
|
||
if err != nil {
|
||
c.JSON(500, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
defer rows.Close()
|
||
var out []InterfaceTypeOut
|
||
for rows.Next() {
|
||
it, err := scanInterfaceRow(rows.Scan)
|
||
if err == nil {
|
||
out = append(out, it)
|
||
}
|
||
}
|
||
c.JSON(200, gin.H{"interface_types": out, "count": len(out)})
|
||
}
|
||
|
||
func (a *app) getInterfaceType(c *gin.Context) {
|
||
name := c.Param("name")
|
||
it, err := scanInterfaceRow(func(args ...any) error {
|
||
return a.pool.QueryRow(c,
|
||
`SELECT id, api_name, display_name, description, protocol, method, path, inputs, outputs,
|
||
requires_auth, expose_to_mcp, backing_ref, builtin
|
||
FROM interface_types WHERE api_name=$1`, name).Scan(args...)
|
||
})
|
||
if err != nil {
|
||
c.JSON(404, gin.H{"error": "interface_type 不存在"})
|
||
return
|
||
}
|
||
c.JSON(200, it)
|
||
}
|
||
|
||
type interfaceTypeInput struct {
|
||
APIName string `json:"api_name"`
|
||
DisplayName string `json:"display_name"`
|
||
Description string `json:"description"`
|
||
Protocol string `json:"protocol"`
|
||
Method string `json:"method"`
|
||
Path string `json:"path"`
|
||
Inputs string `json:"inputs"`
|
||
Outputs string `json:"outputs"`
|
||
RequiresAuth bool `json:"requires_auth"`
|
||
ExposeToMCP bool `json:"expose_to_mcp"`
|
||
BackingRef string `json:"backing_ref"`
|
||
}
|
||
|
||
func (a *app) createInterfaceType(c *gin.Context) {
|
||
var in interfaceTypeInput
|
||
if err := c.ShouldBindJSON(&in); err != nil {
|
||
c.JSON(400, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
if in.APIName == "" || in.DisplayName == "" {
|
||
c.JSON(400, gin.H{"error": "api_name/display_name 必填"})
|
||
return
|
||
}
|
||
if in.Inputs == "" {
|
||
in.Inputs = "[]"
|
||
}
|
||
if in.Outputs == "" {
|
||
in.Outputs = "{}"
|
||
}
|
||
if in.Protocol == "" {
|
||
in.Protocol = "rest"
|
||
}
|
||
_, err := a.pool.Exec(c,
|
||
`INSERT INTO interface_types(api_name,display_name,description,protocol,method,path,inputs,outputs,requires_auth,expose_to_mcp,backing_ref)
|
||
VALUES($1,$2,$3,$4,$5,$6,$7::jsonb,$8::jsonb,$9,$10,$11)
|
||
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,
|
||
updated_at=now()`,
|
||
in.APIName, in.DisplayName, in.Description, in.Protocol, in.Method, in.Path, in.Inputs, in.Outputs,
|
||
in.RequiresAuth, in.ExposeToMCP, in.BackingRef)
|
||
if err != nil {
|
||
c.JSON(409, gin.H{"error": "创建失败: " + err.Error()})
|
||
return
|
||
}
|
||
c.JSON(201, gin.H{"ok": true, "api_name": in.APIName})
|
||
}
|
||
|
||
func (a *app) deleteInterfaceType(c *gin.Context) {
|
||
name := c.Param("name")
|
||
tag, err := a.pool.Exec(c, `DELETE FROM interface_types WHERE api_name=$1 AND builtin=false`, name)
|
||
if err != nil {
|
||
c.JSON(500, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
if tag.RowsAffected() == 0 {
|
||
c.JSON(404, gin.H{"error": "interface_type 不存在或为内置(不可删)"})
|
||
return
|
||
}
|
||
c.JSON(200, gin.H{"ok": true})
|
||
}
|
||
|
||
// ensureBuiltinInterfaces 启动时把 6 个 connector REST 端点注册到 interface_types
|
||
func ensureBuiltinInterfaces(ctx context.Context, a *app) error {
|
||
ifs := []struct {
|
||
APIName string
|
||
DisplayName string
|
||
Description string
|
||
Method string
|
||
Path string
|
||
Inputs string
|
||
Outputs string
|
||
}{
|
||
{"list_object_types", "列出对象类型", "获取尚数网本体的全部对象类型(含属性定义)", "GET", "/api/v1/connector/object-types", "[]",
|
||
`{"object_types":"[ObjectType]","count":"int"}`},
|
||
{"list_objects", "列出某类实例", "按对象类型列出全部实例", "GET", "/api/v1/connector/objects/:type",
|
||
`[{"name":"type","type":"string","required":true},{"name":"limit","type":"int","required":false}]`,
|
||
`{"type":"string","items":"Object[]","count":"int"}`},
|
||
{"get_object", "获取单个实例", "按类型与主键获取单个本体对象", "GET", "/api/v1/connector/object/:type/:pk",
|
||
`[{"name":"type","type":"string","required":true},{"name":"pk","type":"string","required":true}]`,
|
||
`{"object":"Object"}`},
|
||
{"search_ontology", "精准检索", "按名称命中优先排序;可选 type/field/limit", "GET", "/api/v1/connector/search",
|
||
`[{"name":"q","type":"string","required":true},{"name":"type","type":"string","required":false},{"name":"field","type":"string","required":false},{"name":"limit","type":"int","required":false}]`,
|
||
`{"query":"string","items":"Object[]","count":"int"}`},
|
||
{"get_facets", "获取维度取值", "返回各类型可过滤的维度取值", "GET", "/api/v1/connector/facets",
|
||
`[{"name":"type","type":"string","required":false}]`,
|
||
`{"facets":"{type:{facet:[values]}}"}`},
|
||
{"query_related", "图式查询", "按类型+属性过滤,并可沿链接类型展开邻居", "POST", "/api/v1/connector/query",
|
||
`[{"name":"type","type":"string","required":true},{"name":"filters","type":"object","required":false},{"name":"expand","type":"string","required":false},{"name":"limit","type":"int","required":false}]`,
|
||
`{"type":"string","items":"Object[]","neighbors":"Object[]","neighbor_count":"int"}`},
|
||
}
|
||
for _, x := range ifs {
|
||
_, err := a.pool.Exec(ctx,
|
||
`INSERT INTO interface_types(api_name,display_name,description,protocol,method,path,inputs,outputs,requires_auth,expose_to_mcp,backing_ref,builtin)
|
||
VALUES($1,$2,$3,'rest',$4,$5,$6::jsonb,$7::jsonb,true,true,$1,true)
|
||
ON CONFLICT(api_name) DO UPDATE SET
|
||
display_name=EXCLUDED.display_name,
|
||
description=EXCLUDED.description,
|
||
method=EXCLUDED.method,
|
||
path=EXCLUDED.path,
|
||
expose_to_mcp=true,
|
||
updated_at=now()`,
|
||
x.APIName, x.DisplayName, x.Description, x.Method, x.Path, x.Inputs, x.Outputs)
|
||
if err != nil {
|
||
return fmt.Errorf("register builtin interface %s: %w", x.APIName, err)
|
||
}
|
||
}
|
||
log.Printf("[interface] 内置接口就绪: %d 个 connector REST + MCP 暴露", len(ifs))
|
||
return nil
|
||
} |