refactor: rename AI expert item model
This commit is contained in:
@@ -14,8 +14,8 @@ type ExpertFilter struct {
|
|||||||
Query string
|
Query string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SessionService) ListExperts(filter ExpertFilter) ([]models.SenlinAgentAIExpert, error) {
|
func (s *SessionService) ListExperts(filter ExpertFilter) ([]models.SenlinAgentAIExpertItem, error) {
|
||||||
query := models.DBService.Model(&models.SenlinAgentAIExpert{}).Where("enabled = ?", true)
|
query := models.DBService.Model(&models.SenlinAgentAIExpertItem{}).Where("enabled = ?", true)
|
||||||
if category := strings.TrimSpace(filter.Category); category != "" {
|
if category := strings.TrimSpace(filter.Category); category != "" {
|
||||||
query = query.Where("category = ?", category)
|
query = query.Where("category = ?", category)
|
||||||
}
|
}
|
||||||
@@ -26,18 +26,18 @@ func (s *SessionService) ListExperts(filter ExpertFilter) ([]models.SenlinAgentA
|
|||||||
like, like, like,
|
like, like, like,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
var experts []models.SenlinAgentAIExpert
|
var experts []models.SenlinAgentAIExpertItem
|
||||||
if err := query.Order("category asc, id asc").Find(&experts).Error; err != nil {
|
if err := query.Order("category asc, id asc").Find(&experts).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if experts == nil {
|
if experts == nil {
|
||||||
experts = []models.SenlinAgentAIExpert{}
|
experts = []models.SenlinAgentAIExpertItem{}
|
||||||
}
|
}
|
||||||
return experts, nil
|
return experts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SessionService) GetExpert(identity string) (*models.SenlinAgentAIExpert, error) {
|
func (s *SessionService) GetExpert(identity string) (*models.SenlinAgentAIExpertItem, error) {
|
||||||
var expert models.SenlinAgentAIExpert
|
var expert models.SenlinAgentAIExpertItem
|
||||||
if err := models.DBService.Where("identity = ? AND enabled = ?", strings.TrimSpace(identity), true).First(&expert).Error; err != nil {
|
if err := models.DBService.Where("identity = ? AND enabled = ?", strings.TrimSpace(identity), true).First(&expert).Error; err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return nil, ErrExpertNotFound
|
return nil, ErrExpertNotFound
|
||||||
@@ -47,11 +47,11 @@ func (s *SessionService) GetExpert(identity string) (*models.SenlinAgentAIExpert
|
|||||||
return &expert, nil
|
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) == "" {
|
if strings.TrimSpace(identity) == "" {
|
||||||
return nil, nil
|
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 err := tx.Where("identity = ? AND enabled = ?", identity, true).First(&expert).Error; err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return nil, ErrExpertNotFound
|
return nil, ErrExpertNotFound
|
||||||
|
|||||||
@@ -158,12 +158,12 @@ func sessionDTO(session models.SenlinAgentAISession) SessionDTO {
|
|||||||
return dto
|
return dto
|
||||||
}
|
}
|
||||||
|
|
||||||
func expertSummaryPointer(expert models.SenlinAgentAIExpert) *ExpertSummaryDTO {
|
func expertSummaryPointer(expert models.SenlinAgentAIExpertItem) *ExpertSummaryDTO {
|
||||||
dto := expertSummaryDTO(expert)
|
dto := expertSummaryDTO(expert)
|
||||||
return &dto
|
return &dto
|
||||||
}
|
}
|
||||||
|
|
||||||
func expertSummaryDTO(expert models.SenlinAgentAIExpert) ExpertSummaryDTO {
|
func expertSummaryDTO(expert models.SenlinAgentAIExpertItem) ExpertSummaryDTO {
|
||||||
return ExpertSummaryDTO{
|
return ExpertSummaryDTO{
|
||||||
ID: expert.Identity, Slug: expert.Slug, Category: expert.Category,
|
ID: expert.Identity, Slug: expert.Slug, Category: expert.Category,
|
||||||
CategoryName: expert.CategoryName, Name: expert.Name, Description: expert.Description,
|
CategoryName: expert.CategoryName, Name: expert.Name, Description: expert.Description,
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ func TestAIExpertLibraryListsDetailsAndCreatesAssociatedSession(t *testing.T) {
|
|||||||
database := newAIHandlerTestDB(t)
|
database := newAIHandlerTestDB(t)
|
||||||
owner := createAIHandlerUser(t, database, "expert-owner@example.com")
|
owner := createAIHandlerUser(t, database, "expert-owner@example.com")
|
||||||
project := createAIHandlerProject(t, database, owner.ID, "EXPERTS")
|
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)
|
require.NoError(t, database.Order("id asc").First(&expert).Error)
|
||||||
router := aiHandlerTestRouter(owner.ID, NewGatewayWithSecret("system-key", "test-encryption-secret"))
|
router := aiHandlerTestRouter(owner.ID, NewGatewayWithSecret("system-key", "test-encryption-secret"))
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ package models
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
// SenlinAgentAIExpert 是本地专家库中的可选 AI 角色。
|
// SenlinAgentAIExpertItem 是本地专家库中的可选 AI 角色。
|
||||||
type SenlinAgentAIExpert struct {
|
type SenlinAgentAIExpertItem struct {
|
||||||
ID uint `gorm:"primaryKey"`
|
ID uint `gorm:"primaryKey"`
|
||||||
Identity string `gorm:"type:char(36);uniqueIndex"`
|
Identity string `gorm:"type:char(36);uniqueIndex"`
|
||||||
Slug string `gorm:"size:160;uniqueIndex;not null"`
|
Slug string `gorm:"size:160;uniqueIndex;not null"`
|
||||||
@@ -25,6 +25,6 @@ type SenlinAgentAIExpert struct {
|
|||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (SenlinAgentAIExpert) TableName() string {
|
func (SenlinAgentAIExpertItem) TableName() string {
|
||||||
return "senlin_agent_ai_experts"
|
return "senlin_agent_ai_experts"
|
||||||
}
|
}
|
||||||
@@ -3,18 +3,18 @@ package models
|
|||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type SenlinAgentAISession struct {
|
type SenlinAgentAISession struct {
|
||||||
ID uint `gorm:"primaryKey"`
|
ID uint `gorm:"primaryKey"`
|
||||||
Identity string `gorm:"type:char(36);uniqueIndex"`
|
Identity string `gorm:"type:char(36);uniqueIndex"`
|
||||||
ProjectID uint `gorm:"index;not null"`
|
ProjectID uint `gorm:"index;not null"`
|
||||||
ProjectIdentity string `gorm:"type:char(36);index"`
|
ProjectIdentity string `gorm:"type:char(36);index"`
|
||||||
CreatedBy uint `gorm:"index;not null"`
|
CreatedBy uint `gorm:"index;not null"`
|
||||||
CreatedByIdentity string `gorm:"type:char(36);index"`
|
CreatedByIdentity string `gorm:"type:char(36);index"`
|
||||||
ExpertID *uint `gorm:"index"`
|
ExpertID *uint `gorm:"index"`
|
||||||
ExpertIdentity *string `gorm:"type:char(36);index"`
|
ExpertIdentity *string `gorm:"type:char(36);index"`
|
||||||
Expert *SenlinAgentAIExpert `gorm:"foreignKey:ExpertID"`
|
Expert *SenlinAgentAIExpertItem `gorm:"foreignKey:ExpertID"`
|
||||||
Title string `gorm:"not null"`
|
Title string `gorm:"not null"`
|
||||||
Context string `gorm:"type:text"`
|
Context string `gorm:"type:text"`
|
||||||
Status string `gorm:"not null;default:ready"`
|
Status string `gorm:"not null;default:ready"`
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,14 +93,14 @@ func (m *SenlinAgentAISession) BeforeCreate(tx *gorm.DB) error {
|
|||||||
if err := resolveIdentity(tx, &SenlinAgentUser{}, m.CreatedBy, &m.CreatedByIdentity); err != nil {
|
if err := resolveIdentity(tx, &SenlinAgentUser{}, m.CreatedBy, &m.CreatedByIdentity); err != nil {
|
||||||
return err
|
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 {
|
func (m *SenlinAgentAIExpertCategory) BeforeCreate(_ *gorm.DB) error {
|
||||||
return ensureIdentity(&m.Identity)
|
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 {
|
if err := ensureIdentity(&m.Identity); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,10 +92,10 @@ func seedAIExperts(tx *gorm.DB) (map[string]int, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
records := make([]SenlinAgentAIExpert, 0, len(catalog.Experts))
|
records := make([]SenlinAgentAIExpertItem, 0, len(catalog.Experts))
|
||||||
for _, expert := range catalog.Experts {
|
for _, expert := range catalog.Experts {
|
||||||
category := categories[expert.Category]
|
category := categories[expert.Category]
|
||||||
records = append(records, SenlinAgentAIExpert{
|
records = append(records, SenlinAgentAIExpertItem{
|
||||||
Slug: expert.Slug, CategoryID: category.ID, CategoryIdentity: category.Identity,
|
Slug: expert.Slug, CategoryID: category.ID, CategoryIdentity: category.Identity,
|
||||||
Category: expert.Category, CategoryName: expert.CategoryName,
|
Category: expert.Category, CategoryName: expert.CategoryName,
|
||||||
Name: expert.Name, Description: expert.Description, Emoji: expert.Emoji,
|
Name: expert.Name, Description: expert.Description, Emoji: expert.Emoji,
|
||||||
@@ -120,7 +120,7 @@ func normalizeAIExpertCategories(tx *gorm.DB) (map[string]int, error) {
|
|||||||
linked := 0
|
linked := 0
|
||||||
for _, expert := range catalog.Experts {
|
for _, expert := range catalog.Experts {
|
||||||
category := categories[expert.Category]
|
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_id": category.ID,
|
||||||
"category_identity": category.Identity,
|
"category_identity": category.Identity,
|
||||||
"reference": catalog.Reference,
|
"reference": catalog.Reference,
|
||||||
|
|||||||
@@ -69,9 +69,9 @@ func TestAutoMigrateUpgradesLegacyProjectsAndTagsWithoutLosingAssociations(t *te
|
|||||||
require.NoError(t, database.Model(&SenlinAgentAIExpertCategory{}).Count(&categoryCount).Error)
|
require.NoError(t, database.Model(&SenlinAgentAIExpertCategory{}).Count(&categoryCount).Error)
|
||||||
require.Equal(t, int64(19), categoryCount)
|
require.Equal(t, int64(19), categoryCount)
|
||||||
var expertCount int64
|
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)
|
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.NoError(t, database.Preload("ExpertCategory").Where("slug = ?", "academic-geographer").First(&expert).Error)
|
||||||
require.Equal(t, "academic", expert.ExpertCategory.Slug)
|
require.Equal(t, "academic", expert.ExpertCategory.Slug)
|
||||||
require.Equal(t, "学术研究", expert.ExpertCategory.Name)
|
require.Equal(t, "学术研究", expert.ExpertCategory.Name)
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func AutoMigrate(database *gorm.DB) error {
|
|||||||
&SenlinAgentNote{},
|
&SenlinAgentNote{},
|
||||||
&SenlinAgentSource{},
|
&SenlinAgentSource{},
|
||||||
&SenlinAgentAIExpertCategory{},
|
&SenlinAgentAIExpertCategory{},
|
||||||
&SenlinAgentAIExpert{},
|
&SenlinAgentAIExpertItem{},
|
||||||
&SenlinAgentAISession{},
|
&SenlinAgentAISession{},
|
||||||
&SenlinAgentTag{},
|
&SenlinAgentTag{},
|
||||||
&SenlinAgentProjectChannel{},
|
&SenlinAgentProjectChannel{},
|
||||||
|
|||||||
Reference in New Issue
Block a user