refactor: 将客服后端重构为宿主可嵌入模块
- 注入数据库、运行时配置、统一响应、文件存储和平台 AI 能力,补充业务读写工具与客户快捷操作契约。 - 移除模块内重复的组织、客户、工单、标签、技能、旧工作流、MCP 和迁移实现,将身份权限与业务主体交由宿主管理。 - 使用 libSQL 重构向量存储,并完善图片消息、访客身份、排队调度、企业微信和支持聊天页面。 - 统一 HTTP、DTO 与 WebSocket 的 snake_case 协议,补齐模块初始化、业务动作和公共载荷等回归测试。
This commit is contained in:
+29
-45
@@ -4,18 +4,13 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"code.tczkiot.com/wlw/ai-agent/internal/pkg/config"
|
||||
|
||||
"github.com/mlogclub/simple/sqls"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/postgres"
|
||||
|
||||
// "gorm.io/driver/sqlite" // Sqlite driver based on CGO
|
||||
"github.com/glebarez/sqlite" // Pure go SQLite driver, checkout https://github.com/glebarez/sqlite for details
|
||||
"gorm.io/gorm"
|
||||
|
||||
// "gorm.io/gorm/logger"
|
||||
@@ -69,50 +64,39 @@ func InitDB(cfg config.DBConfig) (*gorm.DB, error) {
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// UseDatabase scopes the host application's connection to AI Agent tables.
|
||||
// The connection pool is shared, while the naming strategy is copied so the
|
||||
// host database naming rules remain untouched.
|
||||
func UseDatabase(database *gorm.DB, tablePrefix string) error {
|
||||
moduleDB, err := ScopedDatabase(database, tablePrefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sqls.SetDB(moduleDB)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ScopedDatabase shares the host connection pool while applying the AI Agent
|
||||
// table prefix to an isolated GORM session.
|
||||
func ScopedDatabase(database *gorm.DB, tablePrefix string) (*gorm.DB, error) {
|
||||
if database == nil {
|
||||
return nil, fmt.Errorf("database is required")
|
||||
}
|
||||
moduleDB := database.Session(&gorm.Session{NewDB: true})
|
||||
moduleConfig := *moduleDB.Config
|
||||
moduleConfig.NamingStrategy = schema.NamingStrategy{
|
||||
TablePrefix: tablePrefix,
|
||||
SingularTable: true,
|
||||
}
|
||||
moduleDB.Config = &moduleConfig
|
||||
return moduleDB, nil
|
||||
}
|
||||
|
||||
func newDialector(cfg config.DBConfig) (gorm.Dialector, error) {
|
||||
switch strings.ToLower(strings.TrimSpace(cfg.Type)) {
|
||||
case "sqlite":
|
||||
if err := ensureSQLiteDir(cfg.DSN); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sqlite.Open(cfg.DSN), nil
|
||||
case "mysql":
|
||||
return mysql.Open(cfg.DSN), nil
|
||||
case "postgres", "postgresql":
|
||||
return postgres.Open(cfg.DSN), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported db type: %s", cfg.Type)
|
||||
return nil, fmt.Errorf("unsupported db type %q: only postgres is supported", cfg.Type)
|
||||
}
|
||||
}
|
||||
|
||||
func ensureSQLiteDir(dsn string) error {
|
||||
dbPath := sqliteFilePath(dsn)
|
||||
if dbPath == "" {
|
||||
return nil
|
||||
}
|
||||
dir := filepath.Dir(dbPath)
|
||||
if dir == "." || dir == "" {
|
||||
return nil
|
||||
}
|
||||
return os.MkdirAll(dir, 0o755)
|
||||
}
|
||||
|
||||
func sqliteFilePath(dsn string) string {
|
||||
if dsn == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
path := dsn
|
||||
if after, ok := strings.CutPrefix(path, "file:"); ok {
|
||||
path = after
|
||||
}
|
||||
if idx := strings.Index(path, "?"); idx >= 0 {
|
||||
path = path[:idx]
|
||||
}
|
||||
|
||||
normalized := strings.TrimSpace(path)
|
||||
if normalized == "" || normalized == ":memory:" || strings.Contains(normalized, "mode=memory") {
|
||||
return ""
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user