18c9354095
- 注入数据库、运行时配置、统一响应、文件存储和平台 AI 能力,补充业务读写工具与客户快捷操作契约。 - 移除模块内重复的组织、客户、工单、标签、技能、旧工作流、MCP 和迁移实现,将身份权限与业务主体交由宿主管理。 - 使用 libSQL 重构向量存储,并完善图片消息、访客身份、排队调度、企业微信和支持聊天页面。 - 统一 HTTP、DTO 与 WebSocket 的 snake_case 协议,补齐模块初始化、业务动作和公共载荷等回归测试。
85 lines
2.8 KiB
Go
85 lines
2.8 KiB
Go
package aiagent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/contract"
|
|
"code.tczkiot.com/wlw/ai-agent/identity"
|
|
"code.tczkiot.com/wlw/ai-agent/internal/ai"
|
|
"code.tczkiot.com/wlw/ai-agent/internal/bootstrap"
|
|
"code.tczkiot.com/wlw/ai-agent/internal/models"
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/httpx"
|
|
"code.tczkiot.com/wlw/ai-agent/internal/services"
|
|
"code.tczkiot.com/wlw/ai-agent/internal/services/storage"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Options struct {
|
|
Database *gorm.DB
|
|
TablePrefix string
|
|
LoadSettings func(ctx context.Context, prefix string) (map[string]string, error)
|
|
QuerySubjects identity.QuerySubjectsFunc
|
|
Authorize identity.AuthorizeFunc
|
|
ResponseWriter contract.ResponseWriter
|
|
FileStorage contract.FileStorage
|
|
BusinessReadTools []contract.BusinessReadTool
|
|
BusinessActionTools []contract.BusinessActionTool
|
|
CustomerQuickActions []contract.CustomerQuickAction
|
|
PlatformAI contract.PlatformAIProvider
|
|
}
|
|
|
|
// SyncSchema explicitly creates or updates AI Agent tables. It is intended
|
|
// for the host application's maintenance command and is never called by New.
|
|
func SyncSchema(database *gorm.DB, tablePrefix string) error {
|
|
if tablePrefix == "" {
|
|
tablePrefix = "ai_"
|
|
}
|
|
moduleDB, err := bootstrap.ScopedDatabase(database, tablePrefix)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return moduleDB.AutoMigrate(models.Models...)
|
|
}
|
|
|
|
// New initializes the customer-service business module. Authentication and
|
|
// authorization must already have been completed by the host system.
|
|
func New(options Options) (http.Handler, error) {
|
|
if options.Database == nil {
|
|
return nil, errors.New("ai-agent: Database is required")
|
|
}
|
|
if options.LoadSettings == nil {
|
|
return nil, errors.New("ai-agent: LoadSettings is required")
|
|
}
|
|
if options.QuerySubjects == nil {
|
|
return nil, errors.New("ai-agent: QuerySubjects is required")
|
|
}
|
|
if options.Authorize == nil {
|
|
return nil, errors.New("ai-agent: Authorize is required")
|
|
}
|
|
if options.TablePrefix == "" {
|
|
options.TablePrefix = "ai_"
|
|
}
|
|
services.SetQuerySubjects(options.QuerySubjects)
|
|
services.SetAuthorize(options.Authorize)
|
|
services.SetPlatformAIProvider(options.PlatformAI)
|
|
ai.SetPlatformAIProvider(options.PlatformAI)
|
|
if err := services.SetBusinessReadTools(options.BusinessReadTools); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := services.SetBusinessActionTools(options.BusinessActionTools); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := services.SetCustomerQuickActions(options.CustomerQuickActions); err != nil {
|
|
return nil, err
|
|
}
|
|
storage.SetHostStorage(options.FileStorage)
|
|
if err := bootstrap.InitModule(options.Database, options.TablePrefix, options.LoadSettings); err != nil {
|
|
return nil, err
|
|
}
|
|
httpx.SetResponseWriter(options.ResponseWriter)
|
|
return bootstrap.NewServer()
|
|
}
|