refactor: initialize expert catalog from initdb
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1,7 +1,6 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -32,8 +31,6 @@ func runVersionedMigrations(database *gorm.DB) error {
|
||||
migrations := []versionedMigration{
|
||||
{version: 1, name: "normalize_project_identifiers", run: normalizeLegacyProjectIdentifiers},
|
||||
{version: 2, name: "deduplicate_project_tags", run: deduplicateLegacyProjectTags},
|
||||
{version: 3, name: "seed_ai_experts", run: seedAIExperts},
|
||||
{version: 4, name: "normalize_ai_expert_categories", run: normalizeAIExpertCategories},
|
||||
}
|
||||
for _, migration := range migrations {
|
||||
// Connection callbacks can provide an initialized Gorm session. Start each
|
||||
@@ -66,108 +63,6 @@ func runVersionedMigrations(database *gorm.DB) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//go:embed experts_catalog.json
|
||||
var expertCatalogJSON []byte
|
||||
|
||||
type embeddedExpertCatalog struct {
|
||||
Source string `json:"source"`
|
||||
Reference string `json:"reference"`
|
||||
License string `json:"license"`
|
||||
Experts []embeddedExpertCatalogItem `json:"experts"`
|
||||
}
|
||||
|
||||
type embeddedExpertCatalogItem struct {
|
||||
Slug string `json:"slug"`
|
||||
Category string `json:"category"`
|
||||
CategoryName string `json:"categoryName"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Emoji string `json:"emoji"`
|
||||
Color string `json:"color"`
|
||||
SystemPrompt string `json:"systemPrompt"`
|
||||
}
|
||||
|
||||
func seedAIExperts(tx *gorm.DB) (map[string]int, error) {
|
||||
catalog, categories, err := loadAIExpertCatalog(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
records := make([]SenlinAgentAIExpertItem, 0, len(catalog.Experts))
|
||||
for _, expert := range catalog.Experts {
|
||||
category := categories[expert.Category]
|
||||
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,
|
||||
Color: expert.Color, SystemPrompt: expert.SystemPrompt, Source: catalog.Source,
|
||||
Reference: catalog.Reference, SourceLicense: catalog.License, Enabled: true,
|
||||
})
|
||||
}
|
||||
if len(records) > 0 {
|
||||
if err := tx.CreateInBatches(&records, 25).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return map[string]int{"inserted": len(records)}, nil
|
||||
}
|
||||
|
||||
func normalizeAIExpertCategories(tx *gorm.DB) (map[string]int, error) {
|
||||
catalog, categories, err := loadAIExpertCatalog(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
linked := 0
|
||||
for _, expert := range catalog.Experts {
|
||||
category := categories[expert.Category]
|
||||
result := tx.Model(&SenlinAgentAIExpertItem{}).Where("slug = ?", expert.Slug).Updates(map[string]any{
|
||||
"category_id": category.ID,
|
||||
"category_identity": category.Identity,
|
||||
"reference": catalog.Reference,
|
||||
})
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
linked += int(result.RowsAffected)
|
||||
}
|
||||
return map[string]int{"categories": len(categories), "linked": linked}, nil
|
||||
}
|
||||
|
||||
func loadAIExpertCatalog(tx *gorm.DB) (embeddedExpertCatalog, map[string]SenlinAgentAIExpertCategory, error) {
|
||||
var catalog embeddedExpertCatalog
|
||||
if err := json.Unmarshal(expertCatalogJSON, &catalog); err != nil {
|
||||
return catalog, nil, fmt.Errorf("decode embedded AI expert catalog: %w", err)
|
||||
}
|
||||
|
||||
categoryNames := make(map[string]string)
|
||||
categoryOrder := make([]string, 0)
|
||||
for _, expert := range catalog.Experts {
|
||||
if _, exists := categoryNames[expert.Category]; exists {
|
||||
continue
|
||||
}
|
||||
categoryNames[expert.Category] = expert.CategoryName
|
||||
categoryOrder = append(categoryOrder, expert.Category)
|
||||
}
|
||||
|
||||
categories := make(map[string]SenlinAgentAIExpertCategory, len(categoryOrder))
|
||||
for _, slug := range categoryOrder {
|
||||
category := SenlinAgentAIExpertCategory{Slug: slug}
|
||||
if err := tx.Where("slug = ?", slug).
|
||||
Attrs(SenlinAgentAIExpertCategory{Name: categoryNames[slug]}).
|
||||
FirstOrCreate(&category).Error; err != nil {
|
||||
return catalog, nil, err
|
||||
}
|
||||
if category.Name != categoryNames[slug] {
|
||||
category.Name = categoryNames[slug]
|
||||
if err := tx.Model(&category).Update("name", category.Name).Error; err != nil {
|
||||
return catalog, nil, err
|
||||
}
|
||||
}
|
||||
categories[slug] = category
|
||||
}
|
||||
return catalog, categories, nil
|
||||
}
|
||||
|
||||
func normalizeLegacyProjectIdentifiers(tx *gorm.DB) (map[string]int, error) {
|
||||
var projects []SenlinAgentProject
|
||||
if err := tx.Model(&SenlinAgentProject{}).Select("id", "owner_id", "identifier").Order("owner_id asc, id asc").Find(&projects).Error; err != nil {
|
||||
|
||||
@@ -57,28 +57,14 @@ func TestAutoMigrateUpgradesLegacyProjectsAndTagsWithoutLosingAssociations(t *te
|
||||
|
||||
var migrationCount int64
|
||||
require.NoError(t, database.Table("senlin_agent_schema_migrations").Count(&migrationCount).Error)
|
||||
require.Equal(t, int64(4), migrationCount)
|
||||
require.Equal(t, int64(2), migrationCount)
|
||||
var auditDetails []string
|
||||
require.NoError(t, database.Table("senlin_agent_schema_migrations").Order("version asc").Pluck("details", &auditDetails).Error)
|
||||
require.Contains(t, auditDetails[0], `"updated":3`)
|
||||
require.Contains(t, auditDetails[1], `"deduplicated":1`)
|
||||
require.Contains(t, auditDetails[2], `"inserted":267`)
|
||||
require.Contains(t, auditDetails[3], `"categories":19`)
|
||||
require.Contains(t, auditDetails[3], `"linked":267`)
|
||||
var categoryCount int64
|
||||
require.NoError(t, database.Model(&SenlinAgentAIExpertCategory{}).Count(&categoryCount).Error)
|
||||
require.Equal(t, int64(19), categoryCount)
|
||||
var expertCount int64
|
||||
require.NoError(t, database.Model(&SenlinAgentAIExpertItem{}).Count(&expertCount).Error)
|
||||
require.Equal(t, int64(267), expertCount)
|
||||
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)
|
||||
require.Equal(t, "https://ao.aiolaola.com/experts", expert.Reference)
|
||||
require.NoError(t, AutoMigrate(database), "versioned migrations must be safe to run again")
|
||||
require.NoError(t, database.Table("senlin_agent_schema_migrations").Count(&migrationCount).Error)
|
||||
require.Equal(t, int64(4), migrationCount)
|
||||
require.Equal(t, int64(2), migrationCount)
|
||||
|
||||
require.Error(t, database.Exec(`INSERT INTO senlin_agent_projects (owner_id, name, identifier) VALUES (7, 'still duplicate', 'DUP')`).Error)
|
||||
require.Error(t, database.Exec(`INSERT INTO senlin_agent_tags (project_id, name) VALUES (1, 'UI')`).Error)
|
||||
|
||||
Reference in New Issue
Block a user