「让客户端敢操作」的关键能力:写前/写后打一份快照 → 任意 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]。
64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package ai
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func stripHTML(s string) string {
|
|
scriptRe := regexp.MustCompile(`(?is)<(script|style)[^>]*>.*?</(script|style)>`)
|
|
htmlTagRe := regexp.MustCompile(`(?s)<[^>]+>`)
|
|
s = scriptRe.ReplaceAllString(s, " ")
|
|
s = htmlTagRe.ReplaceAllString(s, " ")
|
|
s = strings.Join(strings.Fields(s), " ")
|
|
return s
|
|
}
|
|
|
|
func TestHeuristicA5(t *testing.T) {
|
|
h := &heuristicProvider{}
|
|
raw, err := os.ReadFile("/Users/xs/Desktop/know/ssw-ontology/archive/展厅设备介绍-A5.html")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
text := stripHTML(string(raw))
|
|
fmt.Println("[stripped text len]", len(text))
|
|
fmt.Println("[stripped first 600]", text[:min(600, len(text))])
|
|
fmt.Println("---")
|
|
|
|
s, err := h.Suggest(context.Background(), text, "提取所有提到的设备和概念", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
fmt.Println(strings.Repeat("=", 60))
|
|
fmt.Println("[bracketRe matches]")
|
|
bm := bracketRe.FindAllStringSubmatch(text, -1)
|
|
for _, m := range bm {
|
|
fmt.Printf(" - %q\n", m[1])
|
|
}
|
|
fmt.Println(strings.Repeat("=", 60))
|
|
fmt.Println("[keywordMap first index]")
|
|
for _, km := range keywordMap {
|
|
idx := strings.Index(text, km.kw)
|
|
fmt.Printf(" %-12s @%-5d -> %s\n", km.kw, idx, km.apiName)
|
|
}
|
|
|
|
fmt.Println(strings.Repeat("=", 60))
|
|
b, _ := json.MarshalIndent(s, "", " ")
|
|
fmt.Println(string(b))
|
|
fmt.Println(strings.Repeat("=", 60))
|
|
fmt.Printf("\nSummary: %s\n", s.Summary)
|
|
}
|
|
|
|
func min(a, b int) int {
|
|
if a < b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|