From f248cfd79e82d9f647dacfccd6739bcc982da03c Mon Sep 17 00:00:00 2001 From: yanweidong Date: Thu, 23 Jul 2026 14:11:17 +0800 Subject: [PATCH] refactor: rename AI expert item model --- backend/internal/logic/ai/experts.go | 16 ++++++------- backend/internal/logic/ai/handlers.go | 4 ++-- backend/internal/logic/ai/handlers_test.go | 2 +- .../{ai_expert.go => ai_expert_items.go} | 6 ++--- backend/internal/models/ai_session.go | 24 +++++++++---------- backend/internal/models/identity.go | 4 ++-- backend/internal/models/migrations.go | 6 ++--- backend/internal/models/migrations_test.go | 4 ++-- backend/internal/models/new.go | 2 +- 9 files changed, 34 insertions(+), 34 deletions(-) rename backend/internal/models/{ai_expert.go => ai_expert_items.go} (89%) diff --git a/backend/internal/logic/ai/experts.go b/backend/internal/logic/ai/experts.go index 4df137c..2438a28 100644 --- a/backend/internal/logic/ai/experts.go +++ b/backend/internal/logic/ai/experts.go @@ -14,8 +14,8 @@ type ExpertFilter struct { Query string } -func (s *SessionService) ListExperts(filter ExpertFilter) ([]models.SenlinAgentAIExpert, error) { - query := models.DBService.Model(&models.SenlinAgentAIExpert{}).Where("enabled = ?", true) +func (s *SessionService) ListExperts(filter ExpertFilter) ([]models.SenlinAgentAIExpertItem, error) { + query := models.DBService.Model(&models.SenlinAgentAIExpertItem{}).Where("enabled = ?", true) if category := strings.TrimSpace(filter.Category); category != "" { query = query.Where("category = ?", category) } @@ -26,18 +26,18 @@ func (s *SessionService) ListExperts(filter ExpertFilter) ([]models.SenlinAgentA like, like, like, ) } - var experts []models.SenlinAgentAIExpert + var experts []models.SenlinAgentAIExpertItem if err := query.Order("category asc, id asc").Find(&experts).Error; err != nil { return nil, err } if experts == nil { - experts = []models.SenlinAgentAIExpert{} + experts = []models.SenlinAgentAIExpertItem{} } return experts, nil } -func (s *SessionService) GetExpert(identity string) (*models.SenlinAgentAIExpert, error) { - var expert models.SenlinAgentAIExpert +func (s *SessionService) GetExpert(identity string) (*models.SenlinAgentAIExpertItem, error) { + var expert models.SenlinAgentAIExpertItem if err := models.DBService.Where("identity = ? AND enabled = ?", strings.TrimSpace(identity), true).First(&expert).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, ErrExpertNotFound @@ -47,11 +47,11 @@ func (s *SessionService) GetExpert(identity string) (*models.SenlinAgentAIExpert return &expert, nil } -func findExpertByIdentity(tx *gorm.DB, identity string) (*models.SenlinAgentAIExpert, error) { +func findExpertByIdentity(tx *gorm.DB, identity string) (*models.SenlinAgentAIExpertItem, error) { if strings.TrimSpace(identity) == "" { return nil, nil } - var expert models.SenlinAgentAIExpert + var expert models.SenlinAgentAIExpertItem if err := tx.Where("identity = ? AND enabled = ?", identity, true).First(&expert).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, ErrExpertNotFound diff --git a/backend/internal/logic/ai/handlers.go b/backend/internal/logic/ai/handlers.go index 706d2c9..ef59833 100644 --- a/backend/internal/logic/ai/handlers.go +++ b/backend/internal/logic/ai/handlers.go @@ -158,12 +158,12 @@ func sessionDTO(session models.SenlinAgentAISession) SessionDTO { return dto } -func expertSummaryPointer(expert models.SenlinAgentAIExpert) *ExpertSummaryDTO { +func expertSummaryPointer(expert models.SenlinAgentAIExpertItem) *ExpertSummaryDTO { dto := expertSummaryDTO(expert) return &dto } -func expertSummaryDTO(expert models.SenlinAgentAIExpert) ExpertSummaryDTO { +func expertSummaryDTO(expert models.SenlinAgentAIExpertItem) ExpertSummaryDTO { return ExpertSummaryDTO{ ID: expert.Identity, Slug: expert.Slug, Category: expert.Category, CategoryName: expert.CategoryName, Name: expert.Name, Description: expert.Description, diff --git a/backend/internal/logic/ai/handlers_test.go b/backend/internal/logic/ai/handlers_test.go index 947b318..79587e3 100644 --- a/backend/internal/logic/ai/handlers_test.go +++ b/backend/internal/logic/ai/handlers_test.go @@ -54,7 +54,7 @@ func TestAIExpertLibraryListsDetailsAndCreatesAssociatedSession(t *testing.T) { database := newAIHandlerTestDB(t) owner := createAIHandlerUser(t, database, "expert-owner@example.com") project := createAIHandlerProject(t, database, owner.ID, "EXPERTS") - var expert models.SenlinAgentAIExpert + var expert models.SenlinAgentAIExpertItem require.NoError(t, database.Order("id asc").First(&expert).Error) router := aiHandlerTestRouter(owner.ID, NewGatewayWithSecret("system-key", "test-encryption-secret")) diff --git a/backend/internal/models/ai_expert.go b/backend/internal/models/ai_expert_items.go similarity index 89% rename from backend/internal/models/ai_expert.go rename to backend/internal/models/ai_expert_items.go index 2184a44..6c91ecf 100644 --- a/backend/internal/models/ai_expert.go +++ b/backend/internal/models/ai_expert_items.go @@ -2,8 +2,8 @@ package models import "time" -// SenlinAgentAIExpert 是本地专家库中的可选 AI 角色。 -type SenlinAgentAIExpert struct { +// SenlinAgentAIExpertItem 是本地专家库中的可选 AI 角色。 +type SenlinAgentAIExpertItem struct { ID uint `gorm:"primaryKey"` Identity string `gorm:"type:char(36);uniqueIndex"` Slug string `gorm:"size:160;uniqueIndex;not null"` @@ -25,6 +25,6 @@ type SenlinAgentAIExpert struct { UpdatedAt time.Time } -func (SenlinAgentAIExpert) TableName() string { +func (SenlinAgentAIExpertItem) TableName() string { return "senlin_agent_ai_experts" } diff --git a/backend/internal/models/ai_session.go b/backend/internal/models/ai_session.go index e690629..182495c 100644 --- a/backend/internal/models/ai_session.go +++ b/backend/internal/models/ai_session.go @@ -3,18 +3,18 @@ package models import "time" type SenlinAgentAISession struct { - ID uint `gorm:"primaryKey"` - Identity string `gorm:"type:char(36);uniqueIndex"` - ProjectID uint `gorm:"index;not null"` - ProjectIdentity string `gorm:"type:char(36);index"` - CreatedBy uint `gorm:"index;not null"` - CreatedByIdentity string `gorm:"type:char(36);index"` - ExpertID *uint `gorm:"index"` - ExpertIdentity *string `gorm:"type:char(36);index"` - Expert *SenlinAgentAIExpert `gorm:"foreignKey:ExpertID"` - Title string `gorm:"not null"` - Context string `gorm:"type:text"` - Status string `gorm:"not null;default:ready"` + ID uint `gorm:"primaryKey"` + Identity string `gorm:"type:char(36);uniqueIndex"` + ProjectID uint `gorm:"index;not null"` + ProjectIdentity string `gorm:"type:char(36);index"` + CreatedBy uint `gorm:"index;not null"` + CreatedByIdentity string `gorm:"type:char(36);index"` + ExpertID *uint `gorm:"index"` + ExpertIdentity *string `gorm:"type:char(36);index"` + Expert *SenlinAgentAIExpertItem `gorm:"foreignKey:ExpertID"` + Title string `gorm:"not null"` + Context string `gorm:"type:text"` + Status string `gorm:"not null;default:ready"` CreatedAt time.Time UpdatedAt time.Time } diff --git a/backend/internal/models/identity.go b/backend/internal/models/identity.go index e4f3e46..33e66fa 100644 --- a/backend/internal/models/identity.go +++ b/backend/internal/models/identity.go @@ -93,14 +93,14 @@ func (m *SenlinAgentAISession) BeforeCreate(tx *gorm.DB) error { if err := resolveIdentity(tx, &SenlinAgentUser{}, m.CreatedBy, &m.CreatedByIdentity); err != nil { return err } - return resolveOptionalIdentity(tx, &SenlinAgentAIExpert{}, m.ExpertID, &m.ExpertIdentity) + return resolveOptionalIdentity(tx, &SenlinAgentAIExpertItem{}, m.ExpertID, &m.ExpertIdentity) } func (m *SenlinAgentAIExpertCategory) BeforeCreate(_ *gorm.DB) error { return ensureIdentity(&m.Identity) } -func (m *SenlinAgentAIExpert) BeforeCreate(tx *gorm.DB) error { +func (m *SenlinAgentAIExpertItem) BeforeCreate(tx *gorm.DB) error { if err := ensureIdentity(&m.Identity); err != nil { return err } diff --git a/backend/internal/models/migrations.go b/backend/internal/models/migrations.go index fdccbbb..3f0e1bb 100644 --- a/backend/internal/models/migrations.go +++ b/backend/internal/models/migrations.go @@ -92,10 +92,10 @@ func seedAIExperts(tx *gorm.DB) (map[string]int, error) { if err != nil { return nil, err } - records := make([]SenlinAgentAIExpert, 0, len(catalog.Experts)) + records := make([]SenlinAgentAIExpertItem, 0, len(catalog.Experts)) for _, expert := range catalog.Experts { category := categories[expert.Category] - records = append(records, SenlinAgentAIExpert{ + records = append(records, SenlinAgentAIExpertItem{ Slug: expert.Slug, CategoryID: category.ID, CategoryIdentity: category.Identity, Category: expert.Category, CategoryName: expert.CategoryName, Name: expert.Name, Description: expert.Description, Emoji: expert.Emoji, @@ -120,7 +120,7 @@ func normalizeAIExpertCategories(tx *gorm.DB) (map[string]int, error) { linked := 0 for _, expert := range catalog.Experts { category := categories[expert.Category] - result := tx.Model(&SenlinAgentAIExpert{}).Where("slug = ?", expert.Slug).Updates(map[string]any{ + result := tx.Model(&SenlinAgentAIExpertItem{}).Where("slug = ?", expert.Slug).Updates(map[string]any{ "category_id": category.ID, "category_identity": category.Identity, "reference": catalog.Reference, diff --git a/backend/internal/models/migrations_test.go b/backend/internal/models/migrations_test.go index 4c00d33..e1305e0 100644 --- a/backend/internal/models/migrations_test.go +++ b/backend/internal/models/migrations_test.go @@ -69,9 +69,9 @@ func TestAutoMigrateUpgradesLegacyProjectsAndTagsWithoutLosingAssociations(t *te require.NoError(t, database.Model(&SenlinAgentAIExpertCategory{}).Count(&categoryCount).Error) require.Equal(t, int64(19), categoryCount) var expertCount int64 - require.NoError(t, database.Model(&SenlinAgentAIExpert{}).Count(&expertCount).Error) + require.NoError(t, database.Model(&SenlinAgentAIExpertItem{}).Count(&expertCount).Error) require.Equal(t, int64(267), expertCount) - var expert SenlinAgentAIExpert + var expert SenlinAgentAIExpertItem require.NoError(t, database.Preload("ExpertCategory").Where("slug = ?", "academic-geographer").First(&expert).Error) require.Equal(t, "academic", expert.ExpertCategory.Slug) require.Equal(t, "学术研究", expert.ExpertCategory.Name) diff --git a/backend/internal/models/new.go b/backend/internal/models/new.go index be5f89c..09cd170 100644 --- a/backend/internal/models/new.go +++ b/backend/internal/models/new.go @@ -33,7 +33,7 @@ func AutoMigrate(database *gorm.DB) error { &SenlinAgentNote{}, &SenlinAgentSource{}, &SenlinAgentAIExpertCategory{}, - &SenlinAgentAIExpert{}, + &SenlinAgentAIExpertItem{}, &SenlinAgentAISession{}, &SenlinAgentTag{}, &SenlinAgentProjectChannel{},