feat(tenant): 新空间开箱即用:自动初始化默认模型配置

- 新增 TenantBootstrapService:建空间后自动创建「新手入门」知识库
  (绑定内置默认对话/向量化模型)、预置欢迎文档(走标准上传管道)、
  配置租户级检索重排模型;整体 fail-open 且幂等,不阻断主流程
- 挂钩 Register(普通注册与 OIDC 首登自动开户均覆盖)与手动创建
  工作区接口,内置模型宿主租户(10000)跳过
- CreateKnowledgeBase / CreateAgent 空的必填模型字段自动落系统默认
  (is_default 优先,否则该类型第一个 active),API 直调路径同样兜底
- 自动化编辑器模型选择预填系统默认对话模型
This commit is contained in:
runny committed 2026-11-19 22:33:21 +08:00
1 parent 4b899a4647
commit 3a578153bb
11 files changed
+344 -13

No files matched your search

+1 -1
View File
@@ -68,7 +68,7 @@ func TestCustomAgentAvatarAndErrorResponses(t *testing.T) {
agent: &types.CustomAgent{ID: "custom-1", Name: "old", Avatar: "🤖", TenantID: 1},
err: tc.repoErr,
}
svc := service.NewCustomAgentService(repo, nil, nil, nil, nil, nil, nil, nil)
svc := service.NewCustomAgentService(repo, nil, nil, nil, nil, nil, nil, nil, nil)
h := &CustomAgentHandler{service: svc}
router := gin.New()
router.Use(middleware.ErrorHandler())
+1 -1
View File
@@ -50,7 +50,7 @@ func servePatchWith(
) *httptest.ResponseRecorder {
t.Helper()
gin.SetMode(gin.TestMode)
h.service = service.NewCustomAgentService(repo, nil, nil, nil, nil, nil, nil, nil)
h.service = service.NewCustomAgentService(repo, nil, nil, nil, nil, nil, nil, nil, nil)
router := gin.New()
router.Use(middleware.ErrorHandler())
router.PATCH("/agents/:id", h.PatchAgent)
+1 -1
View File
@@ -90,7 +90,7 @@ func TestSetUserStatus_NotFound404(t *testing.T) {
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, sqlDB.Close()) })
require.NoError(t, db.AutoMigrate(&types.User{}))
svc := service.NewUserService(nil, repository.NewUserRepository(db), nil, nil, nil, nil, nil)
svc := service.NewUserService(nil, repository.NewUserRepository(db), nil, nil, nil, nil, nil, nil)
h := &SystemHandler{userSvc: svc}
r := statusRouter(h, "admin-1")
body, _ := json.Marshal(map[string]any{"user_id": "ghost", "is_active": true})
+13 -3
View File
@@ -36,6 +36,10 @@ type TenantHandler struct {
// in-code default, so a SystemAdmin's UI override applies on the
// very next CreateTenant call.
systemSettingSvc interfaces.SystemSettingService
// bootstrap seeds out-of-the-box defaults (starter KB bound to the
// built-in default models, welcome doc, tenant rerank) into freshly
// created workspaces. Optional; fail-open by contract.
bootstrap interfaces.TenantBootstrapService
}
// NewTenantHandler creates a new tenant handler instance with the provided service
@@ -63,6 +67,7 @@ func NewTenantHandler(
kbService interfaces.KnowledgeBaseService,
config *config.Config,
systemSettingSvc interfaces.SystemSettingService,
bootstrap interfaces.TenantBootstrapService,
) *TenantHandler {
return &TenantHandler{
service: service,
@@ -72,6 +77,7 @@ func NewTenantHandler(
kbService: kbService,
config: config,
systemSettingSvc: systemSettingSvc,
bootstrap: bootstrap,
}
}
@@ -445,12 +451,16 @@ func (h *TenantHandler) CreateTenant(c *gin.Context) {
}
}
// 开箱即用:为新建空间 seed 默认模型配置(入门知识库 + 欢迎文档 +
// 租户级重排模型)。整体 fail-open,任何失败不影响建租户结果。
if h.bootstrap != nil {
h.bootstrap.SeedTenantDefaults(ctx, createdTenant, caller.ID)
}
logger.Infof(
ctx,
"Tenant created successfully, ID: %d, name: %s",
createdTenant.ID,
secutils.SanitizeForLog(createdTenant.Name),
)
createdTenant.ID, secutils.SanitizeForLog(createdTenant.Name))
// data carries the created tenant. When the legacy auto-create-key
// behaviour is enabled we embed the plaintext token as data.api_key so