refactor: rename AI expert item model

This commit is contained in:
2026-07-23 14:11:17 +08:00
parent 6844af321e
commit f248cfd79e
9 changed files with 34 additions and 34 deletions

View File

@@ -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

View File

@@ -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,

View File

@@ -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"))

View File

@@ -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"
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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,

View File

@@ -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)

View File

@@ -33,7 +33,7 @@ func AutoMigrate(database *gorm.DB) error {
&SenlinAgentNote{},
&SenlinAgentSource{},
&SenlinAgentAIExpertCategory{},
&SenlinAgentAIExpert{},
&SenlinAgentAIExpertItem{},
&SenlinAgentAISession{},
&SenlinAgentTag{},
&SenlinAgentProjectChannel{},