2026-08-21 00:57:23 +08:00
|
|
|
package aiagent
|
2026-08-21 00:41:07 +08:00
|
|
|
|
|
|
|
|
import (
|
2026-08-28 22:23:13 +08:00
|
|
|
"context"
|
2026-08-21 00:41:07 +08:00
|
|
|
"errors"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2026-08-28 22:23:13 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/contract"
|
2026-08-21 00:41:07 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/identity"
|
2026-08-28 22:23:13 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/ai"
|
2026-08-21 00:41:07 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/bootstrap"
|
2026-08-28 22:23:13 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/models"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/httpx"
|
2026-08-21 00:41:07 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/services"
|
2026-08-28 22:23:13 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/services/storage"
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
2026-08-21 00:41:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Options struct {
|
2026-08-28 22:23:13 +08:00
|
|
|
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...)
|
2026-08-21 00:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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) {
|
2026-08-28 22:23:13 +08:00
|
|
|
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")
|
|
|
|
|
}
|
2026-08-21 00:41:07 +08:00
|
|
|
if options.QuerySubjects == nil {
|
2026-08-21 00:57:23 +08:00
|
|
|
return nil, errors.New("ai-agent: QuerySubjects is required")
|
2026-08-21 00:41:07 +08:00
|
|
|
}
|
|
|
|
|
if options.Authorize == nil {
|
2026-08-21 00:57:23 +08:00
|
|
|
return nil, errors.New("ai-agent: Authorize is required")
|
2026-08-21 00:41:07 +08:00
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
if options.TablePrefix == "" {
|
|
|
|
|
options.TablePrefix = "ai_"
|
2026-08-21 00:41:07 +08:00
|
|
|
}
|
|
|
|
|
services.SetQuerySubjects(options.QuerySubjects)
|
|
|
|
|
services.SetAuthorize(options.Authorize)
|
2026-08-28 22:23:13 +08:00
|
|
|
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 {
|
2026-08-21 00:41:07 +08:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
httpx.SetResponseWriter(options.ResponseWriter)
|
2026-08-21 00:41:07 +08:00
|
|
|
return bootstrap.NewServer()
|
|
|
|
|
}
|