fix(merge): 本地整合 - 保留主分支交互与样式并排除不安全用户删除
This commit is contained in:
commit
14adaa1e99
100 files changed
+4845
-865
No files matched your search
@@ -120,6 +120,7 @@ func run(ctx context.Context) error {
|
||||
handler.RegisterAdminAnalyticsRoutes(api, svc)
|
||||
handler.RegisterAdminStorageRoutes(api, svc)
|
||||
handler.RegisterAdminUpdateRoutes(api, svc)
|
||||
handler.RegisterAdminSystemPerformanceRoutes(api, svc)
|
||||
handler.RegisterAnnouncementRoutes(api, svc)
|
||||
handler.RegisterFinanceRoutes(api, svc)
|
||||
handler.RegisterPaymentRoutes(api, svc)
|
||||
|
||||
@@ -178,6 +178,13 @@ func migrateSchemaV6(tx *gorm.DB) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func migrateSchemaV7(tx *gorm.DB) error {
|
||||
if err := tx.AutoMigrate(&model.Asset{}, &model.AssetFolder{}); err != nil {
|
||||
return fmt.Errorf("创建个人素材分类并扩展素材目录字段:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func migrateSchemaV8(tx *gorm.DB) error {
|
||||
if !tx.Migrator().HasTable(&model.LogicalModel{}) {
|
||||
return nil
|
||||
@@ -191,12 +198,6 @@ func migrateSchemaV8(tx *gorm.DB) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func migrateSchemaV7(tx *gorm.DB) error {
|
||||
if err := tx.AutoMigrate(&model.Asset{}, &model.AssetFolder{}); err != nil {
|
||||
return fmt.Errorf("创建个人素材分类并扩展素材目录字段:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func MigrateSchema(db *gorm.DB) error {
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
if tx.Dialector.Name() == "postgres" {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"infinite-canvas/backend/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterAdminSystemPerformanceRoutes(r *gin.RouterGroup, svc *service.Service) {
|
||||
r.GET("/admin/system-performance", func(c *gin.Context) {
|
||||
actor, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
result, err := svc.AdminSystemPerformance(c.Request.Context(), actor)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
ok(c, result)
|
||||
})
|
||||
|
||||
r.POST("/admin/system-performance/cache/clear", func(c *gin.Context) {
|
||||
actor, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 4<<10)
|
||||
var request service.AdminCacheClearRequest
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
failService(c, service.BadAuthRequest("缓存清理请求无效"))
|
||||
return
|
||||
}
|
||||
result, err := svc.ClearAdminRuntimeCache(c.Request.Context(), actor, request)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
ok(c, result)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"infinite-canvas/backend/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestAdminSystemPerformanceRoutesAreRegistered(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
router := gin.New()
|
||||
RegisterAdminSystemPerformanceRoutes(router.Group("/api"), &service.Service{})
|
||||
wanted := map[string]bool{
|
||||
"GET /api/admin/system-performance": false,
|
||||
"POST /api/admin/system-performance/cache/clear": false,
|
||||
}
|
||||
for _, route := range router.Routes() {
|
||||
key := route.Method + " " + route.Path
|
||||
if _, exists := wanted[key]; exists {
|
||||
wanted[key] = true
|
||||
}
|
||||
}
|
||||
for route, found := range wanted {
|
||||
if !found {
|
||||
t.Errorf("route %s is not registered", route)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -375,18 +375,6 @@ func RegisterAdminRoutes(r *gin.RouterGroup, svc *service.Service) {
|
||||
}
|
||||
ok(c, gin.H{"ok": true})
|
||||
})
|
||||
r.DELETE("/admin/users/:id/permanent", func(c *gin.Context) {
|
||||
user, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
if err := svc.PurgeUser(user, c.Param("id")); err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
ok(c, gin.H{"ok": true})
|
||||
})
|
||||
r.GET("/admin/channels", func(c *gin.Context) {
|
||||
user, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
|
||||
@@ -269,6 +269,27 @@ func RegisterFinanceRoutes(r *gin.RouterGroup, svc *service.Service) {
|
||||
r.POST("/admin/channels/:id/models", func(c *gin.Context) {
|
||||
saveChannelModel(c, svc, "")
|
||||
})
|
||||
r.POST("/admin/channels/:id/models/batch-delete", func(c *gin.Context) {
|
||||
user, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 32<<10)
|
||||
var req struct {
|
||||
ModelIDs []string `json:"modelIds"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
fail(c, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
deleted, err := svc.DeleteAdminChannelModels(user, c.Param("id"), req.ModelIDs)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
ok(c, gin.H{"deleted": deleted})
|
||||
})
|
||||
r.PATCH("/admin/channels/:id/models/:modelId", func(c *gin.Context) {
|
||||
saveChannelModel(c, svc, c.Param("modelId"))
|
||||
})
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"infinite-canvas/backend/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestFinanceRoutesExposeChannelModelBatchDelete(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
router := gin.New()
|
||||
RegisterFinanceRoutes(router.Group("/api"), &service.Service{})
|
||||
|
||||
const wanted = "POST /api/admin/channels/:id/models/batch-delete"
|
||||
for _, route := range router.Routes() {
|
||||
if route.Method+" "+route.Path == wanted {
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatalf("route %s is not registered", wanted)
|
||||
}
|
||||
@@ -377,6 +377,55 @@ func TestOfficialRollDekWanVideoUsesVideosLifecycle(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewAPIVideoGenerationsParsesNestedTaskIDs(t *testing.T) {
|
||||
adapter := officialPackageAdapter(t, "newapi-video-generations-v1.yingce-plugin", "newapi-channel-2")
|
||||
tests := []struct {
|
||||
name string
|
||||
payload string
|
||||
wantID string
|
||||
}{
|
||||
{name: "snake case", payload: `{"data":{"task_id":"task-snake","status":"queued"}}`, wantID: "task-snake"},
|
||||
{name: "camel case", payload: `{"data":{"taskId":"task-camel","status":"queued"}}`, wantID: "task-camel"},
|
||||
{name: "nested upstream id wins over wrapper id", payload: `{"id":"49137","data":{"task_id":"task-upstream","status":"queued"}}`, wantID: "task-upstream"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
created, err := adapter.ParseCreate(context.Background(), []byte(test.payload))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if created.TaskID != test.wantID || created.Status != StatusPending {
|
||||
t.Fatalf("created = %#v, want task ID %q and pending status", created, test.wantID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewAPIVideoGenerationsParsesNestedVideoResults(t *testing.T) {
|
||||
adapter := officialPackageAdapter(t, "newapi-video-generations-v1.yingce-plugin", "newapi-channel-2")
|
||||
tests := []struct {
|
||||
name string
|
||||
payload string
|
||||
}{
|
||||
{name: "channel result URL", payload: `{"code":"success","data":{"task_id":"task-upstream","status":"SUCCESS","result_url":"https://cdn.example/channel-result.mp4"}}`},
|
||||
{name: "provider nested video URL", payload: `{"code":"success","data":{"task_id":"task-upstream","status":"SUCCESS","data":{"status":"completed","video_url":"https://cdn.example/provider-result.mp4"}}}`},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
state, err := adapter.ParsePoll(context.Background(), PollContext{TaskID: "task-upstream"}, []byte(test.payload))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if state.Status != StatusSucceeded || state.Result == nil || len(state.Result.Videos) != 1 {
|
||||
t.Fatalf("state = %#v, want one completed video", state)
|
||||
}
|
||||
if state.Result.Videos[0].URL == "" {
|
||||
t.Fatalf("video = %#v, want a result URL", state.Result.Videos[0])
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOfficialOpenAIVideosDeclaresAuthenticatedResultDownload(t *testing.T) {
|
||||
adapter := officialPackageAdapter(t, "openai-videos.yingce-plugin", "newapi")
|
||||
capability, ok := adapter.(ResultCapability)
|
||||
|
||||
@@ -3,6 +3,7 @@ package repository
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -230,12 +231,43 @@ func (r *Repository) PopulateChannelModelPriceTier(item *model.ChannelModel) err
|
||||
return r.attachChannelModelPriceTiers([]*model.ChannelModel{item})
|
||||
}
|
||||
|
||||
func (r *Repository) DeleteChannelModel(channelID string, id string, modelsJSON string, now time.Time) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
func (r *Repository) DeleteChannelModel(channelID string, id string, now time.Time) error {
|
||||
deleted, err := r.DeleteChannelModels(channelID, []string{id}, now)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if deleted != 1 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteChannelModels atomically removes a selection and refreshes the channel's
|
||||
// compatibility model list. Any active route or task reference aborts the whole
|
||||
// transaction so a bulk action cannot leave the administrator with a partial result.
|
||||
func (r *Repository) DeleteChannelModels(channelID string, ids []string, now time.Time) (int64, error) {
|
||||
ids = uniqueStrings(ids)
|
||||
if len(ids) == 0 {
|
||||
return 0, gorm.ErrRecordNotFound
|
||||
}
|
||||
var deleted int64
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := r.lockSystemChannelForModelMutation(tx, channelID); err != nil {
|
||||
return err
|
||||
}
|
||||
var existing int64
|
||||
if err := tx.Model(&model.ChannelModel{}).
|
||||
Where("channel_id = ? AND id IN ?", channelID, ids).
|
||||
Count(&existing).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if existing != int64(len(ids)) {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
var activeReferences int64
|
||||
if err := tx.Table("logical_model_routes AS route").
|
||||
Joins("JOIN logical_models AS logical_model ON logical_model.active_revision_id = route.logical_model_revision_id").
|
||||
Where("route.channel_model_id = ?", id).
|
||||
Where("route.channel_model_id IN ?", ids).
|
||||
Count(&activeReferences).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -243,7 +275,7 @@ func (r *Repository) DeleteChannelModel(channelID string, id string, modelsJSON
|
||||
return ErrChannelModelInUse
|
||||
}
|
||||
if err := tx.Model(&model.Task{}).
|
||||
Where("channel_model_id = ? AND status IN ?", id, []model.TaskStatus{model.TaskStatusQueued, model.TaskStatusRunning}).
|
||||
Where("channel_model_id IN ? AND status IN ?", ids, []model.TaskStatus{model.TaskStatusQueued, model.TaskStatusRunning}).
|
||||
Count(&activeReferences).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -251,28 +283,63 @@ func (r *Repository) DeleteChannelModel(channelID string, id string, modelsJSON
|
||||
return ErrChannelModelInUse
|
||||
}
|
||||
result := tx.Model(&model.ChannelModel{}).
|
||||
Where("id = ? AND channel_id = ?", id, channelID).
|
||||
Where("id IN ? AND channel_id = ?", ids, channelID).
|
||||
Updates(map[string]any{"enabled": false, "price_version": gorm.Expr("price_version + 1"), "updated_at": now})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected != 1 {
|
||||
if result.RowsAffected != int64(len(ids)) {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
if err := tx.Where("id = ? AND channel_id = ?", id, channelID).Delete(&model.ChannelModel{}).Error; err != nil {
|
||||
if err := tx.Where("id IN ? AND channel_id = ?", ids, channelID).Delete(&model.ChannelModel{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
channelResult := tx.Model(&model.ModelChannel{}).
|
||||
Where("id = ? AND scope = ?", channelID, model.ChannelScopeSystem).
|
||||
Updates(map[string]any{"models_json": modelsJSON, "updated_at": now})
|
||||
if channelResult.Error != nil {
|
||||
return channelResult.Error
|
||||
}
|
||||
if channelResult.RowsAffected != 1 {
|
||||
return gorm.ErrRecordNotFound
|
||||
if err := refreshChannelModelNames(tx, channelID, now); err != nil {
|
||||
return err
|
||||
}
|
||||
deleted = result.RowsAffected
|
||||
return nil
|
||||
})
|
||||
return deleted, err
|
||||
}
|
||||
|
||||
// SyncChannelModelNames serializes compatibility-list refreshes for a system
|
||||
// channel and derives the list from the committed channel_models rows. Callers
|
||||
// must not pass a previously-read snapshot because concurrent administrator
|
||||
// writes could otherwise overwrite a newer catalog.
|
||||
func (r *Repository) SyncChannelModelNames(channelID string, now time.Time) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := r.lockSystemChannelForModelMutation(tx, channelID); err != nil {
|
||||
return err
|
||||
}
|
||||
return refreshChannelModelNames(tx, channelID, now)
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Repository) lockSystemChannelForModelMutation(tx *gorm.DB, channelID string) error {
|
||||
query := tx.Select("id").Where("id = ? AND scope = ?", channelID, model.ChannelScopeSystem)
|
||||
if r.Dialect() == "postgres" {
|
||||
query = query.Clauses(clause.Locking{Strength: "UPDATE"})
|
||||
}
|
||||
var channel model.ModelChannel
|
||||
return query.First(&channel).Error
|
||||
}
|
||||
|
||||
func refreshChannelModelNames(tx *gorm.DB, channelID string, now time.Time) error {
|
||||
var names []string
|
||||
if err := tx.Model(&model.ChannelModel{}).
|
||||
Where("channel_id = ? AND enabled = ?", channelID, true).
|
||||
Order("created_at asc").
|
||||
Pluck("model_key", &names).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
encoded, err := json.Marshal(names)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Model(&model.ModelChannel{}).
|
||||
Where("id = ? AND scope = ?", channelID, model.ChannelScopeSystem).
|
||||
Updates(map[string]any{"models_json": string(encoded), "updated_at": now}).Error
|
||||
}
|
||||
|
||||
func (r *Repository) CreateMissingChannelModels(items []model.ChannelModel) (int64, error) {
|
||||
|
||||
@@ -285,20 +285,6 @@ func (r *Repository) DeleteUserAuthSessions(userID string) error {
|
||||
return r.db.Delete(&model.AuthSession{}, "user_id = ?", userID).Error
|
||||
}
|
||||
|
||||
// CountUserRowsByTable 统计目标用户在给定表(均须含 user_id 列)中的行数,供删除前守卫使用。
|
||||
func (r *Repository) CountUserRowsByTable(userID string, tables []string) (map[string]int64, error) {
|
||||
counts := make(map[string]int64, len(tables))
|
||||
for _, table := range tables {
|
||||
var n int64
|
||||
if err := r.db.Table(table).Where("user_id = ?", userID).Count(&n).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if n > 0 {
|
||||
counts[table] = n
|
||||
}
|
||||
}
|
||||
return counts, nil
|
||||
}
|
||||
func (r *Repository) LatestEmailVerificationCode(email string, purpose string) (*model.EmailVerificationCode, error) {
|
||||
var code model.EmailVerificationCode
|
||||
if err := r.db.Where("email = ? AND purpose = ? AND used_at IS NULL", email, purpose).Order("created_at desc").First(&code).Error; err != nil {
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"infinite-canvas/backend/internal/database"
|
||||
)
|
||||
|
||||
type DatabasePoolStats struct {
|
||||
MaxOpenConnections int `json:"maxOpenConnections"`
|
||||
OpenConnections int `json:"openConnections"`
|
||||
InUse int `json:"inUse"`
|
||||
Idle int `json:"idle"`
|
||||
WaitCount int64 `json:"waitCount"`
|
||||
WaitDuration time.Duration `json:"-"`
|
||||
WaitDurationMs int64 `json:"waitDurationMs"`
|
||||
MaxIdleClosed int64 `json:"maxIdleClosed"`
|
||||
MaxLifetimeClosed int64 `json:"maxLifetimeClosed"`
|
||||
}
|
||||
|
||||
type PostgresRuntimeStats struct {
|
||||
ServerVersion string `json:"serverVersion,omitempty"`
|
||||
DatabaseBytes int64 `json:"databaseBytes,omitempty"`
|
||||
Connections int64 `json:"connections,omitempty"`
|
||||
MaxConnections int64 `json:"maxConnections,omitempty"`
|
||||
Transactions int64 `json:"transactions,omitempty"`
|
||||
Rollbacks int64 `json:"rollbacks,omitempty"`
|
||||
CacheHitRate float64 `json:"cacheHitRate,omitempty"`
|
||||
TempFiles int64 `json:"tempFiles,omitempty"`
|
||||
TempBytes int64 `json:"tempBytes,omitempty"`
|
||||
Deadlocks int64 `json:"deadlocks,omitempty"`
|
||||
}
|
||||
|
||||
type DatabaseRuntimeStats struct {
|
||||
Driver string `json:"driver"`
|
||||
Connected bool `json:"connected"`
|
||||
LatencyMs int64 `json:"latencyMs"`
|
||||
DatabaseBytes int64 `json:"databaseBytes,omitempty"`
|
||||
Schema database.SchemaStatus `json:"schema"`
|
||||
Pool DatabasePoolStats `json:"pool"`
|
||||
Postgres *PostgresRuntimeStats `json:"postgres,omitempty"`
|
||||
}
|
||||
|
||||
func (r *Repository) DatabaseRuntimeStats(ctx context.Context) (DatabaseRuntimeStats, error) {
|
||||
result := DatabaseRuntimeStats{Driver: r.Dialect(), Schema: database.SchemaStatus{Expected: database.CurrentSchemaVersion}}
|
||||
sqlDB, err := r.db.DB()
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
started := time.Now()
|
||||
if err := sqlDB.PingContext(ctx); err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Connected = true
|
||||
result.LatencyMs = max(1, time.Since(started).Milliseconds())
|
||||
result.Pool = databasePoolStats(sqlDB.Stats())
|
||||
if schema, schemaErr := database.ReadSchemaStatus(r.db.WithContext(ctx)); schemaErr == nil {
|
||||
result.Schema = schema
|
||||
}
|
||||
if result.Driver == "postgres" {
|
||||
result.Postgres, err = r.postgresRuntimeStats(ctx)
|
||||
if result.Postgres != nil {
|
||||
result.DatabaseBytes = result.Postgres.DatabaseBytes
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
if result.Driver == "sqlite" {
|
||||
var pageCount, pageSize int64
|
||||
if r.db.WithContext(ctx).Raw("PRAGMA page_count").Scan(&pageCount).Error == nil && r.db.WithContext(ctx).Raw("PRAGMA page_size").Scan(&pageSize).Error == nil {
|
||||
result.DatabaseBytes = pageCount * pageSize
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func databasePoolStats(stats sql.DBStats) DatabasePoolStats {
|
||||
return DatabasePoolStats{
|
||||
MaxOpenConnections: stats.MaxOpenConnections, OpenConnections: stats.OpenConnections, InUse: stats.InUse, Idle: stats.Idle,
|
||||
WaitCount: stats.WaitCount, WaitDuration: stats.WaitDuration, WaitDurationMs: stats.WaitDuration.Milliseconds(),
|
||||
MaxIdleClosed: stats.MaxIdleClosed, MaxLifetimeClosed: stats.MaxLifetimeClosed,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Repository) postgresRuntimeStats(ctx context.Context) (*PostgresRuntimeStats, error) {
|
||||
stats := &PostgresRuntimeStats{}
|
||||
if err := r.db.WithContext(ctx).Raw("SHOW server_version").Scan(&stats.ServerVersion).Error; err != nil {
|
||||
return stats, err
|
||||
}
|
||||
var maxConnections string
|
||||
if err := r.db.WithContext(ctx).Raw("SHOW max_connections").Scan(&maxConnections).Error; err == nil {
|
||||
stats.MaxConnections, _ = strconv.ParseInt(maxConnections, 10, 64)
|
||||
}
|
||||
var blocksRead, blocksHit int64
|
||||
err := r.db.WithContext(ctx).Raw(`
|
||||
SELECT pg_database_size(current_database()) AS database_bytes,
|
||||
numbackends AS connections, xact_commit AS transactions, xact_rollback AS rollbacks,
|
||||
blks_read, blks_hit, temp_files, temp_bytes, deadlocks
|
||||
FROM pg_stat_database WHERE datname = current_database()
|
||||
`).Row().Scan(&stats.DatabaseBytes, &stats.Connections, &stats.Transactions, &stats.Rollbacks, &blocksRead, &blocksHit, &stats.TempFiles, &stats.TempBytes, &stats.Deadlocks)
|
||||
if blocksRead+blocksHit > 0 {
|
||||
stats.CacheHitRate = float64(blocksHit) / float64(blocksRead+blocksHit) * 100
|
||||
}
|
||||
return stats, err
|
||||
}
|
||||
@@ -1,213 +0,0 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"infinite-canvas/backend/internal/database"
|
||||
"infinite-canvas/backend/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// UserPurgeResult 汇总彻底删除用户时清掉的行数(按表)。
|
||||
type UserPurgeResult struct {
|
||||
DeletedByTable map[string]int64 `json:"deletedByTable"`
|
||||
DeletedRowsTotal int64 `json:"deletedRowsTotal"`
|
||||
}
|
||||
|
||||
type purgeSchema struct {
|
||||
model any
|
||||
table string
|
||||
dbNameOfGo map[string]string // Go 字段名 → 真实列名(尊重 gorm tag / 命名策略)
|
||||
}
|
||||
|
||||
func (p purgeSchema) column(goName string) string {
|
||||
return p.dbNameOfGo[goName]
|
||||
}
|
||||
|
||||
// purgeFKTemplates 描述“指向用户子数据的列”如何回溯到用户。
|
||||
// 统一以 user_id = ? 收口;模板中 %s 为列名占位,? 为用户 ID 参数。
|
||||
// 由于所有模板都回溯到“带 user_id 的根表”,而根表行只在第二阶段删除,
|
||||
// 第一阶段内部任意表间顺序都不会破坏子查询可见性。
|
||||
var purgeFKTemplates = map[string]string{
|
||||
"TaskID": "%s IN (SELECT id FROM tasks WHERE user_id = ?)",
|
||||
"SessionID": "%s IN (SELECT id FROM sessions WHERE user_id = ?)",
|
||||
"ProjectID": "%s IN (SELECT id FROM projects WHERE user_id = ?)",
|
||||
"AssetID": "%s IN (SELECT id FROM assets WHERE user_id = ?)",
|
||||
"CanvasID": "%s IN (SELECT id FROM canvas_projects WHERE user_id = ?)",
|
||||
"ResourceID": "%s IN (SELECT id FROM resources WHERE user_id = ?)",
|
||||
"StorageSettingID": "%s IN (SELECT id FROM user_oss_settings WHERE user_id = ?)",
|
||||
"BillingOrderID": "%s IN (SELECT id FROM billing_orders WHERE user_id = ?)",
|
||||
"PaymentOrderID": "%s IN (SELECT id FROM payment_orders WHERE user_id = ?)",
|
||||
"AssetVersionID": "%s IN (SELECT id FROM asset_versions WHERE asset_id IN (SELECT id FROM assets WHERE user_id = ?))",
|
||||
"ShotID": "%s IN (SELECT id FROM shots WHERE project_id IN (SELECT id FROM projects WHERE user_id = ?))",
|
||||
"UnitID": "%s IN (SELECT id FROM project_units WHERE project_id IN (SELECT id FROM projects WHERE user_id = ?))",
|
||||
"WorkflowInstanceID": "%s IN (SELECT id FROM workflow_instances WHERE project_id IN (SELECT id FROM projects WHERE user_id = ?))",
|
||||
"WorkflowStepID": "%s IN (SELECT id FROM workflow_step_instances WHERE workflow_instance_id IN (SELECT id FROM workflow_instances WHERE project_id IN (SELECT id FROM projects WHERE user_id = ?)))",
|
||||
"RevisionID": "%s IN (SELECT id FROM shot_revisions WHERE shot_id IN (SELECT id FROM shots WHERE project_id IN (SELECT id FROM projects WHERE user_id = ?)))",
|
||||
"FolderID": "%s IN (SELECT id FROM project_asset_folders WHERE project_id IN (SELECT id FROM projects WHERE user_id = ?))",
|
||||
"VoiceProfileID": "%s IN (SELECT id FROM voice_profiles WHERE user_id = ?)",
|
||||
}
|
||||
|
||||
// purgeOwnColumns 第二阶段直接归属用户的字段(Go 字段名)。
|
||||
var purgeOwnColumns = []string{"UserID", "OwnerID", "RedeemedBy"}
|
||||
|
||||
// PurgeUserData 在单个事务内彻底删除一个用户及其名下业务数据,并(可选)
|
||||
// 在事务尾部原子写入一条管理员审计记录(如 user.purge)。
|
||||
// 阶段一:删除所有能通过 FK 回溯到该用户的子数据(根表行仍在,子查询始终可见);
|
||||
// 阶段二:删除直接归属该用户的行(user_id / owner_id / redeemed_by);
|
||||
// 阶段三:审计记录(保留本次传入的审计)、邮箱验证码与用户主行。
|
||||
// 用户主行最后删,保证事务内全程可回溯。
|
||||
func (r *Repository) PurgeUserData(user *model.User, keepAudit *model.AdminAuditEvent) (*UserPurgeResult, error) {
|
||||
userID := strings.TrimSpace(user.ID)
|
||||
if userID == "" {
|
||||
return nil, fmt.Errorf("缺少用户 ID")
|
||||
}
|
||||
email := strings.TrimSpace(user.Email)
|
||||
|
||||
result := &UserPurgeResult{DeletedByTable: map[string]int64{}}
|
||||
keepAction := ""
|
||||
if keepAudit != nil {
|
||||
keepAction = strings.TrimSpace(keepAudit.Action)
|
||||
}
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
schemas, err := parsePurgeSchemas(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 阶段一:子表级联(不含用户主行,也不含 user_id 直属行)
|
||||
for _, schema := range schemas {
|
||||
if _, isUser := schema.model.(*model.User); isUser {
|
||||
continue
|
||||
}
|
||||
conds, args := buildFKPurgeConds(schema, userID)
|
||||
if len(conds) == 0 {
|
||||
continue
|
||||
}
|
||||
if err := deletePurgedRows(tx, schema, conds, args, result); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// 阶段二:直属行
|
||||
for _, schema := range schemas {
|
||||
if _, isUser := schema.model.(*model.User); isUser {
|
||||
continue
|
||||
}
|
||||
conds := make([]string, 0, 1)
|
||||
args := make([]any, 0, 1)
|
||||
for _, goName := range purgeOwnColumns {
|
||||
if column := schema.column(goName); column != "" {
|
||||
conds = append(conds, column+" = ?")
|
||||
args = append(args, userID)
|
||||
}
|
||||
}
|
||||
if len(conds) == 0 {
|
||||
continue
|
||||
}
|
||||
if err := deletePurgedRows(tx, schema, conds, args, result); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// 阶段三:审计(该用户作操作者/被操作对象)、邮箱验证码、用户主行
|
||||
for _, schema := range schemas {
|
||||
if _, ok := schema.model.(*model.AdminAuditEvent); ok {
|
||||
if err := deleteAdminAuditRows(tx, schema, userID, keepAction, result); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, ok := schema.model.(*model.EmailVerificationCode); ok && email != "" {
|
||||
if column := schema.column("Email"); column != "" {
|
||||
if err := deletePurgedWhere(tx, schema, column+" = ?", []any{email}, result); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, schema := range schemas {
|
||||
if _, isUser := schema.model.(*model.User); isUser {
|
||||
if err := deletePurgedWhere(tx, schema, "id = ?", []any{userID}, result); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
// 本次删除动作的审计记录在用户主行删除后写入,避免被上面的清理误删。
|
||||
if keepAudit != nil {
|
||||
if err := tx.Create(keepAudit).Error; err != nil {
|
||||
return fmt.Errorf("写入管理员审计失败:%w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func parsePurgeSchemas(db *gorm.DB) ([]purgeSchema, error) {
|
||||
models := database.Models()
|
||||
schemas := make([]purgeSchema, 0, len(models))
|
||||
for _, dst := range models {
|
||||
stmt := &gorm.Statement{DB: db, Dest: dst}
|
||||
if err := stmt.Parse(dst); err != nil {
|
||||
return nil, fmt.Errorf("解析表结构失败(%T):%w", dst, err)
|
||||
}
|
||||
names := make(map[string]string, len(stmt.Schema.Fields))
|
||||
for _, field := range stmt.Schema.Fields {
|
||||
names[field.Name] = field.DBName
|
||||
}
|
||||
schemas = append(schemas, purgeSchema{model: dst, table: stmt.Schema.Table, dbNameOfGo: names})
|
||||
}
|
||||
return schemas, nil
|
||||
}
|
||||
|
||||
// buildFKPurgeConds 汇总一张表中命中“用户子数据 FK”的删除条件。
|
||||
func buildFKPurgeConds(schema purgeSchema, userID string) ([]string, []any) {
|
||||
conds := make([]string, 0, 2)
|
||||
args := make([]any, 0, 2)
|
||||
for goName, template := range purgeFKTemplates {
|
||||
column := schema.column(goName)
|
||||
if column == "" {
|
||||
continue
|
||||
}
|
||||
conds = append(conds, fmt.Sprintf(template, column))
|
||||
args = append(args, userID)
|
||||
}
|
||||
return conds, args
|
||||
}
|
||||
|
||||
func deletePurgedRows(tx *gorm.DB, schema purgeSchema, conds []string, args []any, result *UserPurgeResult) error {
|
||||
combined := "(" + strings.Join(conds, " OR ") + ")"
|
||||
return deletePurgedWhere(tx, schema, combined, args, result)
|
||||
}
|
||||
|
||||
func deletePurgedWhere(tx *gorm.DB, schema purgeSchema, where string, args []any, result *UserPurgeResult) error {
|
||||
db := tx.Table(schema.table).Where(where, args...).Delete(schema.model)
|
||||
if db.Error != nil {
|
||||
return fmt.Errorf("清理 %s 失败:%w", schema.table, db.Error)
|
||||
}
|
||||
if db.RowsAffected > 0 {
|
||||
result.DeletedByTable[schema.table] += db.RowsAffected
|
||||
result.DeletedRowsTotal += db.RowsAffected
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func deleteAdminAuditRows(tx *gorm.DB, schema purgeSchema, userID string, keepAction string, result *UserPurgeResult) error {
|
||||
actorColumn := schema.column("ActorUserID")
|
||||
targetTypeColumn := schema.column("TargetType")
|
||||
targetIDColumn := schema.column("TargetID")
|
||||
actionColumn := schema.column("Action")
|
||||
if actorColumn == "" || targetTypeColumn == "" || targetIDColumn == "" || actionColumn == "" {
|
||||
return nil
|
||||
}
|
||||
where := "(" + actorColumn + " = ? OR (" + targetTypeColumn + " = 'user' AND " + targetIDColumn + " = ?))"
|
||||
args := []any{userID, userID}
|
||||
if keepAction != "" {
|
||||
where += " AND " + actionColumn + " <> ?"
|
||||
args = append(args, keepAction)
|
||||
}
|
||||
return deletePurgedWhere(tx, schema, where, args, result)
|
||||
}
|
||||
@@ -389,47 +389,6 @@ func (s *Service) DeleteUser(actor *model.User, userID string) error {
|
||||
return s.appendAdminAudit(actor, "user.disable", "user", user.ID, "停用用户并清除登录态", nil)
|
||||
}
|
||||
|
||||
// purgeFinanceTables 存在即拒绝彻底删除的账务数据:真实资金流水/订单必须长期保留。
|
||||
var purgeFinanceTables = []string{"billing_orders", "payment_orders"}
|
||||
|
||||
// PurgeUser 彻底删除(硬删)用户:账号与全部关联数据在单个事务内级联物理移除,不可恢复。
|
||||
// 守卫:仅管理员、不能删自己、管理员需保底一个;存在资金流水/账单等需保留的账务数据时拒绝,提示改用停用。
|
||||
// 业务内容(任务/项目/素材等)由 repo.PurgeUserData 按 FK 链在事务内级联删除,删除动作以 user.purge 审计落库。
|
||||
func (s *Service) PurgeUser(actor *model.User, userID string) error {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return err
|
||||
}
|
||||
if actor.ID == userID {
|
||||
return BadAuthRequest("不能删除当前登录的管理员账号")
|
||||
}
|
||||
user, err := s.repo.User(userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if user.Role == model.UserRoleAdmin {
|
||||
count, err := s.repo.ActiveAdminCountExcluding(user.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count == 0 {
|
||||
return BadAuthRequest("至少需要保留一个管理员")
|
||||
}
|
||||
}
|
||||
financeCounts, err := s.repo.CountUserRowsByTable(user.ID, purgeFinanceTables)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(financeCounts) > 0 {
|
||||
return BadAuthRequest("该用户存在资金流水/账单数据,无法彻底删除,请改用停用")
|
||||
}
|
||||
audit, err := newAdminAuditEvent(actor, "user.purge", "user", user.ID, "彻底删除用户账号及全部关联数据", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.repo.PurgeUserData(user, audit)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Service) BulkDisableUsers(actor *model.User, req BulkDisableUsersRequest) (*BulkDisableUsersResult, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
@@ -275,6 +276,109 @@ func TestSaveAdminChannelModelRejectsActiveDuplicateKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteAdminChannelModelsDeletesSelectionAtomically(t *testing.T) {
|
||||
svc, db := newChannelModelTestService(t)
|
||||
admin := &model.User{ID: "admin", Role: model.UserRoleAdmin}
|
||||
channel := model.ModelChannel{ID: "channel-1", UserID: admin.ID, Scope: model.ChannelScopeSystem, Enabled: true, Name: "Test", BaseURL: "https://example.com/v1", APIKey: "key", APIFormat: "openai", ModelsJSON: `["model-a","model-b","model-c"]`}
|
||||
items := []model.ChannelModel{
|
||||
{ID: "model-a", ChannelID: channel.ID, ModelKey: "model-a", DisplayName: "Model A", Enabled: true, PriceVersion: 1},
|
||||
{ID: "model-b", ChannelID: channel.ID, ModelKey: "model-b", DisplayName: "Model B", Enabled: true, PriceVersion: 1},
|
||||
{ID: "model-c", ChannelID: channel.ID, ModelKey: "model-c", DisplayName: "Model C", Enabled: true, PriceVersion: 1},
|
||||
}
|
||||
if err := db.Create(&channel).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&items).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
deleted, err := svc.DeleteAdminChannelModels(admin, channel.ID, []string{" model-a ", "model-b", "model-a"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if deleted != 2 {
|
||||
t.Fatalf("deleted = %d, want 2", deleted)
|
||||
}
|
||||
var storedChannel model.ModelChannel
|
||||
if err := db.First(&storedChannel, "id = ?", channel.ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if storedChannel.ModelsJSON != `["model-c"]` {
|
||||
t.Fatalf("ModelsJSON = %s, want model-c only", storedChannel.ModelsJSON)
|
||||
}
|
||||
var active []model.ChannelModel
|
||||
if err := db.Where("channel_id = ?", channel.ID).Find(&active).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(active) != 1 || active[0].ID != "model-c" {
|
||||
t.Fatalf("active models = %#v, want model-c", active)
|
||||
}
|
||||
var removed []model.ChannelModel
|
||||
if err := db.Unscoped().Where("channel_id = ? AND id IN ?", channel.ID, []string{"model-a", "model-b"}).Find(&removed).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(removed) != 2 {
|
||||
t.Fatalf("removed models = %#v, want two", removed)
|
||||
}
|
||||
for _, item := range removed {
|
||||
if item.Enabled || item.PriceVersion != 2 || !item.DeletedAt.Valid {
|
||||
t.Fatalf("removed model state = %#v", item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteAdminChannelModelsRejectsWholeSelectionWhenOneModelIsInUse(t *testing.T) {
|
||||
svc, db := newChannelModelTestService(t)
|
||||
admin := &model.User{ID: "admin", Role: model.UserRoleAdmin}
|
||||
channel := model.ModelChannel{ID: "channel-1", UserID: admin.ID, Scope: model.ChannelScopeSystem, Enabled: true, Name: "Test", BaseURL: "https://example.com/v1", APIKey: "key", APIFormat: "openai", ModelsJSON: `["model-a","model-b"]`}
|
||||
items := []model.ChannelModel{
|
||||
{ID: "model-a", ChannelID: channel.ID, ModelKey: "model-a", DisplayName: "Model A", Enabled: true, PriceVersion: 1},
|
||||
{ID: "model-b", ChannelID: channel.ID, ModelKey: "model-b", DisplayName: "Model B", Enabled: true, PriceVersion: 1},
|
||||
}
|
||||
if err := db.Create(&channel).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&items).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.Task{ID: "task-running", ChannelModelID: "model-b", Status: model.TaskStatusRunning}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
deleted, err := svc.DeleteAdminChannelModels(admin, channel.ID, []string{"model-a", "model-b"})
|
||||
var authErr *AuthError
|
||||
if !errors.As(err, &authErr) || authErr.Message != "所选渠道模型中有模型仍被前台模型供应线路或进行中任务使用,本次未删除任何模型" {
|
||||
t.Fatalf("DeleteAdminChannelModels() deleted = %d, error = %#v", deleted, err)
|
||||
}
|
||||
var active int64
|
||||
if err := db.Model(&model.ChannelModel{}).Where("channel_id = ?", channel.ID).Count(&active).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if active != 2 {
|
||||
t.Fatalf("active models = %d, want 2 after atomic rejection", active)
|
||||
}
|
||||
var storedChannel model.ModelChannel
|
||||
if err := db.First(&storedChannel, "id = ?", channel.ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if storedChannel.ModelsJSON != channel.ModelsJSON {
|
||||
t.Fatalf("ModelsJSON changed after rejected batch: %s", storedChannel.ModelsJSON)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeAdminChannelModelDeleteIDsRequiresBoundedSelection(t *testing.T) {
|
||||
if _, err := normalizeAdminChannelModelDeleteIDs([]string{"", " "}); err == nil {
|
||||
t.Fatal("empty selection should be rejected")
|
||||
}
|
||||
values := make([]string, 101)
|
||||
for index := range values {
|
||||
values[index] = "model-" + strconv.Itoa(index)
|
||||
}
|
||||
if _, err := normalizeAdminChannelModelDeleteIDs(values); err == nil {
|
||||
t.Fatal("selection above 100 models should be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveProviderConfigMapsSKUToProviderModel(t *testing.T) {
|
||||
svc, db := newChannelModelTestService(t)
|
||||
svc.dataDir = t.TempDir()
|
||||
@@ -344,7 +448,7 @@ func newChannelModelTestService(t *testing.T) (*Service, *gorm.DB) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.ModelChannel{}, &model.ChannelModel{}, &model.ChannelModelPriceTier{}, &model.IDSequence{}); err != nil {
|
||||
if err := db.AutoMigrate(&model.ModelChannel{}, &model.ChannelModel{}, &model.ChannelModelPriceTier{}, &model.LogicalModel{}, &model.LogicalModelRevision{}, &model.LogicalModelRoute{}, &model.Task{}, &model.IDSequence{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return &Service{repo: repository.New(db)}, db
|
||||
|
||||
@@ -0,0 +1,346 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"infinite-canvas/backend/internal/buildinfo"
|
||||
"infinite-canvas/backend/internal/model"
|
||||
"infinite-canvas/backend/internal/repository"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var systemProcessStartedAt = time.Now()
|
||||
|
||||
const systemPerformanceTimeout = 3 * time.Second
|
||||
|
||||
type AdminSystemPerformance struct {
|
||||
CollectedAt time.Time `json:"collectedAt"`
|
||||
Status string `json:"status"`
|
||||
Host SystemPerformanceHost `json:"host"`
|
||||
Memory SystemPerformanceMemory `json:"memory"`
|
||||
Disk SystemPerformanceDisk `json:"disk"`
|
||||
Database repository.DatabaseRuntimeStats `json:"database"`
|
||||
Redis SystemPerformanceRedis `json:"redis"`
|
||||
Build buildinfo.Info `json:"build"`
|
||||
}
|
||||
|
||||
type SystemPerformanceHost struct {
|
||||
Hostname string `json:"hostname"`
|
||||
OS string `json:"os"`
|
||||
Arch string `json:"arch"`
|
||||
CPUCores int `json:"cpuCores"`
|
||||
GOMAXPROCS int `json:"gomaxprocs"`
|
||||
ProcessID int `json:"processId"`
|
||||
UptimeSeconds int64 `json:"uptimeSeconds"`
|
||||
Goroutines int `json:"goroutines"`
|
||||
ActiveWorkerTasks int64 `json:"activeWorkerTasks"`
|
||||
LoadAverage [3]float64 `json:"loadAverage"`
|
||||
LoadAverageAvailable bool `json:"loadAverageAvailable"`
|
||||
}
|
||||
|
||||
type SystemPerformanceMemory struct {
|
||||
SystemAvailable bool `json:"systemAvailable"`
|
||||
TotalBytes uint64 `json:"totalBytes"`
|
||||
UsedBytes uint64 `json:"usedBytes"`
|
||||
AvailableBytes uint64 `json:"availableBytes"`
|
||||
UsagePercent float64 `json:"usagePercent"`
|
||||
HeapAllocBytes uint64 `json:"heapAllocBytes"`
|
||||
HeapSysBytes uint64 `json:"heapSysBytes"`
|
||||
SysBytes uint64 `json:"sysBytes"`
|
||||
HeapObjects uint64 `json:"heapObjects"`
|
||||
GCCount uint32 `json:"gcCount"`
|
||||
LastGCAt *time.Time `json:"lastGCAt,omitempty"`
|
||||
}
|
||||
|
||||
type SystemPerformanceDisk struct {
|
||||
Available bool `json:"available"`
|
||||
Writable bool `json:"writable"`
|
||||
TotalBytes uint64 `json:"totalBytes"`
|
||||
UsedBytes uint64 `json:"usedBytes"`
|
||||
FreeBytes uint64 `json:"freeBytes"`
|
||||
UsagePercent float64 `json:"usagePercent"`
|
||||
}
|
||||
|
||||
type SystemPerformanceRedisPool struct {
|
||||
Hits uint32 `json:"hits"`
|
||||
Misses uint32 `json:"misses"`
|
||||
Timeouts uint32 `json:"timeouts"`
|
||||
TotalConnections uint32 `json:"totalConnections"`
|
||||
IdleConnections uint32 `json:"idleConnections"`
|
||||
StaleConnections uint32 `json:"staleConnections"`
|
||||
PendingRequests uint32 `json:"pendingRequests"`
|
||||
}
|
||||
|
||||
type SystemPerformanceCacheGroup struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
Keys int64 `json:"keys"`
|
||||
Clearable bool `json:"clearable"`
|
||||
}
|
||||
|
||||
type SystemPerformanceRedis struct {
|
||||
Configured bool `json:"configured"`
|
||||
Connected bool `json:"connected"`
|
||||
Mode string `json:"mode"`
|
||||
LatencyMs int64 `json:"latencyMs,omitempty"`
|
||||
Version string `json:"version,omitempty"`
|
||||
UptimeSeconds int64 `json:"uptimeSeconds,omitempty"`
|
||||
UsedMemoryBytes int64 `json:"usedMemoryBytes,omitempty"`
|
||||
PeakMemoryBytes int64 `json:"peakMemoryBytes,omitempty"`
|
||||
MaxMemoryBytes int64 `json:"maxMemoryBytes,omitempty"`
|
||||
MaxMemoryPolicy string `json:"maxMemoryPolicy,omitempty"`
|
||||
Keys int64 `json:"keys,omitempty"`
|
||||
Clients int64 `json:"clients,omitempty"`
|
||||
BlockedClients int64 `json:"blockedClients,omitempty"`
|
||||
OpsPerSecond int64 `json:"opsPerSecond,omitempty"`
|
||||
HitRate float64 `json:"hitRate,omitempty"`
|
||||
ExpiredKeys int64 `json:"expiredKeys,omitempty"`
|
||||
EvictedKeys int64 `json:"evictedKeys,omitempty"`
|
||||
Pool SystemPerformanceRedisPool `json:"pool"`
|
||||
CacheGroups []SystemPerformanceCacheGroup `json:"cacheGroups"`
|
||||
StatusMessage string `json:"statusMessage,omitempty"`
|
||||
}
|
||||
|
||||
type AdminCacheClearRequest struct {
|
||||
Scope string `json:"scope"`
|
||||
}
|
||||
|
||||
type AdminCacheClearGroupResult struct {
|
||||
ID string `json:"id"`
|
||||
DeletedKeys int64 `json:"deletedKeys"`
|
||||
}
|
||||
|
||||
type AdminCacheClearResult struct {
|
||||
Scope string `json:"scope"`
|
||||
DeletedKeys int64 `json:"deletedKeys"`
|
||||
MemoryEntries int `json:"memoryEntries"`
|
||||
Groups []AdminCacheClearGroupResult `json:"groups"`
|
||||
ClearedAt time.Time `json:"clearedAt"`
|
||||
}
|
||||
|
||||
type runtimeCacheDefinition struct {
|
||||
id, label, pattern string
|
||||
}
|
||||
|
||||
var runtimeCacheDefinitions = []runtimeCacheDefinition{
|
||||
{id: "rateLimits", label: "请求频控", pattern: "canvas:rate:*"},
|
||||
{id: "circuitFailures", label: "渠道失败计数", pattern: "canvas:circuit:failures:*"},
|
||||
{id: "circuitOpen", label: "渠道熔断状态", pattern: "canvas:circuit:open:*"},
|
||||
{id: "routeHealth", label: "线路临时禁用", pattern: "canvas:logical-route-health:*"},
|
||||
}
|
||||
|
||||
func (s *Service) AdminSystemPerformance(ctx context.Context, actor *model.User) (*AdminSystemPerformance, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, systemPerformanceTimeout)
|
||||
defer cancel()
|
||||
|
||||
hostname, _ := os.Hostname()
|
||||
load, loadAvailable := platformLoadAverage()
|
||||
memory := collectSystemMemory()
|
||||
disk := collectSystemDisk(s.dataDir)
|
||||
databaseStats, databaseErr := s.repo.DatabaseRuntimeStats(ctx)
|
||||
redisStats := s.collectRedisPerformance(ctx)
|
||||
status := "healthy"
|
||||
if databaseErr != nil || !databaseStats.Connected || !disk.Available || !disk.Writable || s.ValidateRuntime() != nil || (redisStats.Configured && !redisStats.Connected) {
|
||||
status = "degraded"
|
||||
}
|
||||
return &AdminSystemPerformance{
|
||||
CollectedAt: time.Now(), Status: status,
|
||||
Host: SystemPerformanceHost{Hostname: hostname, OS: runtime.GOOS, Arch: runtime.GOARCH, CPUCores: runtime.NumCPU(), GOMAXPROCS: runtime.GOMAXPROCS(0), ProcessID: os.Getpid(), UptimeSeconds: int64(time.Since(systemProcessStartedAt).Seconds()), Goroutines: runtime.NumGoroutine(), ActiveWorkerTasks: s.ActiveWorkerTasks(), LoadAverage: load, LoadAverageAvailable: loadAvailable},
|
||||
Memory: memory, Disk: disk, Database: databaseStats, Redis: redisStats, Build: buildinfo.Current(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func collectSystemMemory() SystemPerformanceMemory {
|
||||
var stats runtime.MemStats
|
||||
runtime.ReadMemStats(&stats)
|
||||
total, available, ok := platformMemory()
|
||||
used := uint64(0)
|
||||
percentage := float64(0)
|
||||
if ok && total >= available {
|
||||
used = total - available
|
||||
if total > 0 {
|
||||
percentage = float64(used) / float64(total) * 100
|
||||
}
|
||||
}
|
||||
result := SystemPerformanceMemory{SystemAvailable: ok, TotalBytes: total, UsedBytes: used, AvailableBytes: available, UsagePercent: percentage, HeapAllocBytes: stats.HeapAlloc, HeapSysBytes: stats.HeapSys, SysBytes: stats.Sys, HeapObjects: stats.HeapObjects, GCCount: stats.NumGC}
|
||||
if stats.LastGC > 0 {
|
||||
last := time.Unix(0, int64(stats.LastGC))
|
||||
result.LastGCAt = &last
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *Service) collectRedisPerformance(ctx context.Context) SystemPerformanceRedis {
|
||||
result := SystemPerformanceRedis{Mode: "local", StatusMessage: "未配置 Redis,当前使用单实例本地协调", CacheGroups: make([]SystemPerformanceCacheGroup, 0, len(runtimeCacheDefinitions))}
|
||||
if s.coordinator == nil {
|
||||
result.CacheGroups = append(result.CacheGroups, SystemPerformanceCacheGroup{ID: "rateLimits", Label: "本地请求频控", Clearable: true})
|
||||
return result
|
||||
}
|
||||
if s.coordinator.redis == nil {
|
||||
s.coordinator.localMu.Lock()
|
||||
localRateKeys := int64(len(s.coordinator.localRate))
|
||||
s.coordinator.localMu.Unlock()
|
||||
result.CacheGroups = append(result.CacheGroups, SystemPerformanceCacheGroup{ID: "rateLimits", Label: "本地请求频控", Keys: localRateKeys, Clearable: true})
|
||||
return result
|
||||
}
|
||||
result.Configured, result.Mode = true, "redis"
|
||||
started := time.Now()
|
||||
if err := s.coordinator.redis.Ping(ctx).Err(); err != nil {
|
||||
result.StatusMessage = "Redis 暂时不可用"
|
||||
return result
|
||||
}
|
||||
result.Connected = true
|
||||
result.LatencyMs = max(1, time.Since(started).Milliseconds())
|
||||
result.StatusMessage = "Redis 连接正常"
|
||||
if size, err := s.coordinator.redis.DBSize(ctx).Result(); err == nil {
|
||||
result.Keys = size
|
||||
}
|
||||
if info, err := s.coordinator.redis.Info(ctx, "server", "clients", "memory", "stats").Result(); err == nil {
|
||||
values := parseRedisInfo(info)
|
||||
result.Version = values["redis_version"]
|
||||
result.UptimeSeconds = redisInfoInt(values, "uptime_in_seconds")
|
||||
result.UsedMemoryBytes = redisInfoInt(values, "used_memory")
|
||||
result.PeakMemoryBytes = redisInfoInt(values, "used_memory_peak")
|
||||
result.MaxMemoryBytes = redisInfoInt(values, "maxmemory")
|
||||
result.MaxMemoryPolicy = values["maxmemory_policy"]
|
||||
result.Clients = redisInfoInt(values, "connected_clients")
|
||||
result.BlockedClients = redisInfoInt(values, "blocked_clients")
|
||||
result.OpsPerSecond = redisInfoInt(values, "instantaneous_ops_per_sec")
|
||||
result.ExpiredKeys = redisInfoInt(values, "expired_keys")
|
||||
result.EvictedKeys = redisInfoInt(values, "evicted_keys")
|
||||
hits, misses := redisInfoInt(values, "keyspace_hits"), redisInfoInt(values, "keyspace_misses")
|
||||
if hits+misses > 0 {
|
||||
result.HitRate = float64(hits) / float64(hits+misses) * 100
|
||||
}
|
||||
}
|
||||
pool := s.coordinator.redis.PoolStats()
|
||||
result.Pool = SystemPerformanceRedisPool{Hits: pool.Hits, Misses: pool.Misses, Timeouts: pool.Timeouts, TotalConnections: pool.TotalConns, IdleConnections: pool.IdleConns, StaleConnections: pool.StaleConns, PendingRequests: pool.PendingRequests}
|
||||
for _, definition := range runtimeCacheDefinitions {
|
||||
count, _ := scanRedisKeyCount(ctx, s.coordinator.redis, definition.pattern)
|
||||
result.CacheGroups = append(result.CacheGroups, SystemPerformanceCacheGroup{ID: definition.id, Label: definition.label, Keys: count, Clearable: true})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *Service) ClearAdminRuntimeCache(ctx context.Context, actor *model.User, request AdminCacheClearRequest) (*AdminCacheClearResult, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if strings.TrimSpace(request.Scope) != "runtime" {
|
||||
return nil, BadAuthRequest("仅支持清理运行时缓存")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
result := &AdminCacheClearResult{Scope: "runtime", ClearedAt: time.Now(), Groups: make([]AdminCacheClearGroupResult, 0, len(runtimeCacheDefinitions))}
|
||||
if s.coordinator != nil {
|
||||
if s.coordinator.redis != nil {
|
||||
for _, definition := range runtimeCacheDefinitions {
|
||||
deleted, err := deleteRedisKeysByPattern(ctx, s.coordinator.redis, definition.pattern)
|
||||
if err != nil {
|
||||
return nil, errors.New("清理 Redis 运行时缓存失败")
|
||||
}
|
||||
result.Groups = append(result.Groups, AdminCacheClearGroupResult{ID: definition.id, DeletedKeys: deleted})
|
||||
result.DeletedKeys += deleted
|
||||
}
|
||||
} else {
|
||||
s.coordinator.localMu.Lock()
|
||||
count := len(s.coordinator.localRate)
|
||||
s.coordinator.localRate = map[string]localRateEntry{}
|
||||
s.coordinator.localMu.Unlock()
|
||||
result.DeletedKeys += int64(count)
|
||||
result.Groups = append(result.Groups, AdminCacheClearGroupResult{ID: "rateLimits", DeletedKeys: int64(count)})
|
||||
}
|
||||
}
|
||||
s.initReadCaches()
|
||||
result.MemoryEntries += s.concurrencyReadCache.clearAndCount()
|
||||
result.MemoryEntries += s.textReplayReadCache.clearAndCount()
|
||||
result.MemoryEntries += s.routeVersionReadCache.clearAndCount()
|
||||
s.routeHealthMu.Lock()
|
||||
result.MemoryEntries += len(s.routeHealthBlocked)
|
||||
s.routeHealthBlocked = make(map[string]time.Time)
|
||||
s.routeHealthMu.Unlock()
|
||||
// 与路由目录刷新保持同一加锁顺序,避免清理动作和回源刷新并发写入重试状态。
|
||||
s.routeCatalogRefreshMu.Lock()
|
||||
s.routeCatalogMu.Lock()
|
||||
if s.routeCatalog != nil {
|
||||
result.MemoryEntries++
|
||||
}
|
||||
s.routeCatalog = nil
|
||||
s.routeCatalogVersion = 0
|
||||
s.routeCatalogRetryAt = time.Time{}
|
||||
s.routeCatalogRefreshError = nil
|
||||
s.routeCatalogMu.Unlock()
|
||||
s.routeCatalogRefreshMu.Unlock()
|
||||
if err := s.appendAdminAudit(actor, "system.runtime_cache.clear", "system", "runtime-cache", "清理安全运行时缓存", map[string]any{"deletedKeys": result.DeletedKeys, "memoryEntries": result.MemoryEntries}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func parseRedisInfo(info string) map[string]string {
|
||||
values := make(map[string]string)
|
||||
for _, line := range strings.Split(info, "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
key, value, found := strings.Cut(line, ":")
|
||||
if found {
|
||||
values[key] = strings.TrimSpace(value)
|
||||
}
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func redisInfoInt(values map[string]string, key string) int64 {
|
||||
value, _ := strconv.ParseInt(values[key], 10, 64)
|
||||
return value
|
||||
}
|
||||
|
||||
func scanRedisKeyCount(ctx context.Context, client *redis.Client, pattern string) (int64, error) {
|
||||
var cursor uint64
|
||||
var total int64
|
||||
for {
|
||||
keys, next, err := client.Scan(ctx, cursor, pattern, 200).Result()
|
||||
if err != nil {
|
||||
return total, err
|
||||
}
|
||||
total += int64(len(keys))
|
||||
cursor = next
|
||||
if cursor == 0 {
|
||||
return total, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func deleteRedisKeysByPattern(ctx context.Context, client *redis.Client, pattern string) (int64, error) {
|
||||
var cursor uint64
|
||||
var deleted int64
|
||||
for {
|
||||
keys, next, err := client.Scan(ctx, cursor, pattern, 200).Result()
|
||||
if err != nil {
|
||||
return deleted, err
|
||||
}
|
||||
if len(keys) > 0 {
|
||||
count, err := client.Unlink(ctx, keys...).Result()
|
||||
if err != nil {
|
||||
return deleted, err
|
||||
}
|
||||
deleted += count
|
||||
}
|
||||
cursor = next
|
||||
if cursor == 0 {
|
||||
return deleted, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"infinite-canvas/backend/internal/database"
|
||||
"infinite-canvas/backend/internal/model"
|
||||
"infinite-canvas/backend/internal/repository"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestAdminSystemPerformanceRequiresAdmin(t *testing.T) {
|
||||
svc := &Service{}
|
||||
user := &model.User{ID: "user-1", Role: model.UserRoleUser}
|
||||
|
||||
if _, err := svc.AdminSystemPerformance(context.Background(), user); authStatus(err) != 403 {
|
||||
t.Fatalf("AdminSystemPerformance() error = %#v, want 403", err)
|
||||
}
|
||||
if _, err := svc.ClearAdminRuntimeCache(context.Background(), user, AdminCacheClearRequest{Scope: "runtime"}); authStatus(err) != 403 {
|
||||
t.Fatalf("ClearAdminRuntimeCache() error = %#v, want 403", err)
|
||||
}
|
||||
if _, err := svc.AdminSystemPerformance(context.Background(), nil); authStatus(err) != 401 {
|
||||
t.Fatalf("AdminSystemPerformance(nil) error = %#v, want 401", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminSystemPerformanceReportsSQLiteWithoutRedis(t *testing.T) {
|
||||
svc, db := newSystemPerformanceTestService(t)
|
||||
admin := &model.User{ID: "admin-1", Role: model.UserRoleAdmin}
|
||||
|
||||
result, err := svc.AdminSystemPerformance(context.Background(), admin)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Status != "healthy" {
|
||||
t.Fatalf("status = %q, want healthy", result.Status)
|
||||
}
|
||||
if result.Database.Driver != "sqlite" || !result.Database.Connected || result.Database.Postgres != nil {
|
||||
t.Fatalf("database = %#v", result.Database)
|
||||
}
|
||||
if result.Database.DatabaseBytes <= 0 {
|
||||
t.Fatalf("database bytes = %d, want > 0", result.Database.DatabaseBytes)
|
||||
}
|
||||
if result.Database.Schema.Expected != database.CurrentSchemaVersion {
|
||||
t.Fatalf("schema = %#v", result.Database.Schema)
|
||||
}
|
||||
if result.Redis.Configured || result.Redis.Connected || result.Redis.Mode != "local" {
|
||||
t.Fatalf("redis = %#v", result.Redis)
|
||||
}
|
||||
if len(result.Redis.CacheGroups) != 1 || result.Redis.CacheGroups[0].ID != "rateLimits" {
|
||||
t.Fatalf("cache groups = %#v", result.Redis.CacheGroups)
|
||||
}
|
||||
if !result.Disk.Available || !result.Disk.Writable {
|
||||
t.Fatalf("disk = %#v", result.Disk)
|
||||
}
|
||||
if result.Host.CPUCores < 1 || result.Host.GOMAXPROCS < 1 || result.Host.ProcessID < 1 {
|
||||
t.Fatalf("host = %#v", result.Host)
|
||||
}
|
||||
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stats := sqlDB.Stats(); result.Database.Pool.OpenConnections != stats.OpenConnections {
|
||||
t.Fatalf("pool open connections = %d, want %d", result.Database.Pool.OpenConnections, stats.OpenConnections)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClearAdminRuntimeCacheRejectsUnknownScopeAndAuditsLocalClear(t *testing.T) {
|
||||
svc, db := newSystemPerformanceTestService(t)
|
||||
admin := &model.User{ID: "admin-1", Role: model.UserRoleAdmin}
|
||||
svc.coordinator.localRate["user-1"] = localRateEntry{started: time.Now(), count: 2}
|
||||
|
||||
if _, err := svc.ClearAdminRuntimeCache(context.Background(), admin, AdminCacheClearRequest{Scope: "all"}); authStatus(err) != 400 {
|
||||
t.Fatalf("unknown scope error = %#v, want 400", err)
|
||||
}
|
||||
if len(svc.coordinator.localRate) != 1 {
|
||||
t.Fatal("invalid scope changed local runtime cache")
|
||||
}
|
||||
|
||||
svc.initReadCaches()
|
||||
if _, err := svc.routeVersionReadCache.get(context.Background(), "route-version", func(context.Context) (int64, int, error) {
|
||||
return 7, 256, nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
svc.routeHealthBlocked["route-1"] = time.Now().Add(time.Minute)
|
||||
svc.routeCatalog = &routeCatalogSnapshot{}
|
||||
svc.routeCatalogRetryAt = time.Now().Add(time.Minute)
|
||||
svc.routeCatalogRefreshError = errors.New("cached refresh failure")
|
||||
|
||||
result, err := svc.ClearAdminRuntimeCache(context.Background(), admin, AdminCacheClearRequest{Scope: "runtime"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.DeletedKeys != 1 || len(svc.coordinator.localRate) != 0 {
|
||||
t.Fatalf("local clear result = %#v, remaining = %d", result, len(svc.coordinator.localRate))
|
||||
}
|
||||
if result.MemoryEntries != 3 {
|
||||
t.Fatalf("memory entries = %d, want 3", result.MemoryEntries)
|
||||
}
|
||||
if svc.routeCatalog != nil || !svc.routeCatalogRetryAt.IsZero() || svc.routeCatalogRefreshError != nil || len(svc.routeHealthBlocked) != 0 {
|
||||
t.Fatalf("route caches were not reset")
|
||||
}
|
||||
var audit model.AdminAuditEvent
|
||||
if err := db.Where("action = ?", "system.runtime_cache.clear").First(&audit).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if audit.ActorUserID != admin.ID || audit.TargetID != "runtime-cache" {
|
||||
t.Fatalf("audit = %#v", audit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClearAdminRuntimeCacheUsesRedisWhitelist(t *testing.T) {
|
||||
client := startSystemPerformanceRedis(t)
|
||||
svc, db := newSystemPerformanceTestService(t)
|
||||
svc.coordinator = &runtimeCoordinator{redis: client, instanceID: "system-performance-test", localRate: map[string]localRateEntry{}, localSlots: map[string]map[string]time.Time{}}
|
||||
admin := &model.User{ID: "admin-redis", Role: model.UserRoleAdmin}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
clearable := []string{
|
||||
"canvas:rate:user-1",
|
||||
"canvas:circuit:failures:channel-1",
|
||||
"canvas:circuit:open:channel-1",
|
||||
"canvas:logical-route-health:route-1",
|
||||
}
|
||||
preserved := []string{
|
||||
"canvas:slots:channel:channel-1",
|
||||
"canvas:logical-model-route-catalog:version",
|
||||
"canvas:task:task-1",
|
||||
}
|
||||
for _, key := range append(append([]string{}, clearable...), preserved...) {
|
||||
if err := client.Set(ctx, key, "1", 0).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
before := svc.collectRedisPerformance(ctx)
|
||||
if !before.Configured || !before.Connected || before.Mode != "redis" || before.Keys != int64(len(clearable)+len(preserved)) {
|
||||
t.Fatalf("redis performance = %#v", before)
|
||||
}
|
||||
|
||||
result, err := svc.ClearAdminRuntimeCache(ctx, admin, AdminCacheClearRequest{Scope: "runtime"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.DeletedKeys != int64(len(clearable)) || len(result.Groups) != len(runtimeCacheDefinitions) {
|
||||
t.Fatalf("clear result = %#v", result)
|
||||
}
|
||||
if remaining, err := client.Exists(ctx, clearable...).Result(); err != nil || remaining != 0 {
|
||||
t.Fatalf("clearable keys remaining = %d, err = %v", remaining, err)
|
||||
}
|
||||
if remaining, err := client.Exists(ctx, preserved...).Result(); err != nil || remaining != int64(len(preserved)) {
|
||||
t.Fatalf("preserved keys remaining = %d, err = %v", remaining, err)
|
||||
}
|
||||
var auditCount int64
|
||||
if err := db.Model(&model.AdminAuditEvent{}).Where("action = ?", "system.runtime_cache.clear").Count(&auditCount).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if auditCount != 1 {
|
||||
t.Fatalf("audit count = %d, want 1", auditCount)
|
||||
}
|
||||
}
|
||||
|
||||
func newSystemPerformanceTestService(t *testing.T) (*Service, *gorm.DB) {
|
||||
t.Helper()
|
||||
dataDir := t.TempDir()
|
||||
db, err := gorm.Open(sqlite.Open(filepath.Join(dataDir, "system-performance.db")), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.AdminAuditEvent{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sqlDB, err := db.DB(); err == nil {
|
||||
sqlDB.SetMaxOpenConns(1)
|
||||
t.Cleanup(func() { _ = sqlDB.Close() })
|
||||
}
|
||||
return &Service{
|
||||
repo: repository.New(db),
|
||||
dataDir: dataDir,
|
||||
coordinator: &runtimeCoordinator{instanceID: "local-test", localRate: map[string]localRateEntry{}, localSlots: map[string]map[string]time.Time{}},
|
||||
routeCatalogTTL: 30 * time.Second,
|
||||
routeCatalogMaxStale: 5 * time.Minute,
|
||||
routeHealthBlocked: map[string]time.Time{},
|
||||
}, db
|
||||
}
|
||||
|
||||
func startSystemPerformanceRedis(t *testing.T) *redis.Client {
|
||||
t.Helper()
|
||||
binary, err := exec.LookPath("redis-server")
|
||||
if err != nil {
|
||||
t.Skip("redis-server unavailable; Redis cache whitelist integration not run")
|
||||
}
|
||||
dir, err := os.MkdirTemp("", "canvas-performance-redis-")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.RemoveAll(dir) })
|
||||
socket := filepath.Join(dir, "r.sock")
|
||||
cmd := exec.Command(binary, "--port", "0", "--unixsocket", socket, "--unixsocketperm", "700", "--save", "", "--appendonly", "no", "--dir", dir)
|
||||
if err := cmd.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = cmd.Process.Kill()
|
||||
_ = cmd.Wait()
|
||||
})
|
||||
client := redis.NewClient(&redis.Options{Network: "unix", Addr: socket, MaxRetries: -1, ContextTimeoutEnabled: true})
|
||||
t.Cleanup(func() { _ = client.Close() })
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
for client.Ping(ctx).Err() != nil {
|
||||
if ctx.Err() != nil {
|
||||
t.Fatal("temporary Redis did not start")
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
func authStatus(err error) int {
|
||||
var authErr *AuthError
|
||||
if errors.As(err, &authErr) {
|
||||
return authErr.Status
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -1000,7 +1000,11 @@ func (s *Service) enrichAPICallLogFailureSummary(log *model.ApiCallLog, response
|
||||
}
|
||||
|
||||
func (s *Service) enrichAPICallLogPayload(log *model.ApiCallLog, payload map[string]any) {
|
||||
nestedTaskID := ""
|
||||
if data, ok := payload["data"].(map[string]any); ok {
|
||||
if log.Capability == "video" && strings.Contains(log.Path, "/v1/video/generations") {
|
||||
nestedTaskID = firstNonEmpty(stringField(data, "task_id"), stringField(data, "taskId"))
|
||||
}
|
||||
for key, value := range data {
|
||||
if _, exists := payload[key]; !exists {
|
||||
payload[key] = value
|
||||
@@ -1064,7 +1068,7 @@ func (s *Service) enrichAPICallLogPayload(log *model.ApiCallLog, payload map[str
|
||||
}
|
||||
log.CachedTokens = firstInt64(usageMetadata, "cachedContentTokenCount")
|
||||
}
|
||||
log.ProviderRequestID = firstNonEmpty(stringField(payload, "task_id"), stringField(payload, "id"), stringField(payload, "request_id"), stringField(payload, "name"), log.ProviderRequestID)
|
||||
log.ProviderRequestID = firstNonEmpty(nestedTaskID, stringField(payload, "task_id"), stringField(payload, "id"), stringField(payload, "request_id"), stringField(payload, "name"), log.ProviderRequestID)
|
||||
log.ProviderStatus = strings.ToLower(firstNonEmpty(stringField(payload, "status"), log.ProviderStatus))
|
||||
if log.ProviderStatus == "failed" || log.ProviderStatus == "cancelled" || log.ProviderStatus == "expired" {
|
||||
log.Status = model.ApiCallStatusFailed
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -29,7 +31,7 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
appearanceSchemaVersion = 6
|
||||
appearanceSchemaVersion = 7
|
||||
appearanceLogoMaxBytes int64 = 5 << 20
|
||||
appearancePosterMaxBytes int64 = 10 << 20
|
||||
appearanceVideoMaxBytes int64 = 256 << 20
|
||||
@@ -56,6 +58,7 @@ type AppearanceSetting struct {
|
||||
LogoFrameEnabled bool `json:"logoFrameEnabled"`
|
||||
AuthVideoResourceID string `json:"authVideoResourceId"`
|
||||
AuthVideoPosterResourceID string `json:"authVideoPosterResourceId"`
|
||||
AuthVideoAutoplay bool `json:"authVideoAutoplay"`
|
||||
SkinID string `json:"skinId"`
|
||||
SkinThemes []AppearanceSkinTheme `json:"skinThemes"`
|
||||
SEOTitle string `json:"seoTitle"`
|
||||
@@ -77,6 +80,7 @@ type PublicAppearanceSetting struct {
|
||||
LogoFrameEnabled bool `json:"logoFrameEnabled"`
|
||||
AuthVideoURL string `json:"authVideoUrl"`
|
||||
AuthVideoPosterURL string `json:"authVideoPosterUrl"`
|
||||
AuthVideoAutoplay bool `json:"authVideoAutoplay"`
|
||||
SkinID string `json:"skinId"`
|
||||
ActiveSkin AppearanceSkinTheme `json:"activeSkin"`
|
||||
SEOTitle string `json:"seoTitle"`
|
||||
@@ -105,13 +109,14 @@ type AdminAppearanceSetting struct {
|
||||
|
||||
func defaultAppearanceSetting() AppearanceSetting {
|
||||
return AppearanceSetting{
|
||||
SchemaVersion: appearanceSchemaVersion,
|
||||
BrandName: defaultAppearanceBrandName,
|
||||
BrandSlug: defaultAppearanceBrandSlug,
|
||||
AuthHeroTitle: defaultAppearanceHeroTitle,
|
||||
LogoFrameEnabled: true,
|
||||
SkinID: defaultAppearanceSkinID,
|
||||
SkinThemes: defaultAppearanceSkinThemes(),
|
||||
SchemaVersion: appearanceSchemaVersion,
|
||||
BrandName: defaultAppearanceBrandName,
|
||||
BrandSlug: defaultAppearanceBrandSlug,
|
||||
AuthHeroTitle: defaultAppearanceHeroTitle,
|
||||
AuthVideoAutoplay: true,
|
||||
LogoFrameEnabled: true,
|
||||
SkinID: defaultAppearanceSkinID,
|
||||
SkinThemes: defaultAppearanceSkinThemes(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,6 +138,7 @@ func (s *Service) Appearance() (*PublicAppearanceSetting, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value = s.resolveAvailableAppearanceAssets(value)
|
||||
return publicAppearanceSetting(setting, value), nil
|
||||
}
|
||||
|
||||
@@ -144,6 +150,7 @@ func (s *Service) AdminAppearance(actor *model.User) (*AdminAppearanceSetting, e
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value = s.resolveAvailableAppearanceAssets(value)
|
||||
result := &AdminAppearanceSetting{
|
||||
AppearanceSetting: value,
|
||||
Public: *publicAppearanceSetting(setting, value),
|
||||
@@ -257,7 +264,15 @@ func (s *Service) UploadAppearanceAsset(actor *model.User, slot string, header *
|
||||
if slot == AppearanceAssetVideo {
|
||||
kind = "video"
|
||||
}
|
||||
resource, err := s.UploadResource(actor.ID, header, kind, 0, 0, 0)
|
||||
var resource *model.Resource
|
||||
// Logos are tiny, installation-owned assets. Keep them in the server's
|
||||
// persistent resource directory so their availability and cost do not
|
||||
// depend on the administrator's currently selected object storage.
|
||||
if slot == AppearanceAssetLogo || slot == AppearanceAssetDarkLogo {
|
||||
resource, err = s.uploadLocalResource(actor.ID, header, kind, 0, 0, 0)
|
||||
} else {
|
||||
resource, err = s.UploadResource(actor.ID, header, kind, 0, 0, 0)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -364,6 +379,42 @@ func (s *Service) readAppearance() (*model.SystemSetting, AppearanceSetting, err
|
||||
return setting, value, nil
|
||||
}
|
||||
|
||||
// resolveAvailableAppearanceAssets prevents stale resource references from
|
||||
// being advertised to anonymous clients or echoed back into the admin editor.
|
||||
// Remote objects are not probed on every appearance request; the frontend has
|
||||
// its own load-error fallback for objects that disappear outside the system.
|
||||
func (s *Service) resolveAvailableAppearanceAssets(value AppearanceSetting) AppearanceSetting {
|
||||
for _, slot := range []string{AppearanceAssetLogo, AppearanceAssetDarkLogo, AppearanceAssetVideo, AppearanceAssetPoster} {
|
||||
resourceID := appearanceResourceID(value, slot)
|
||||
if resourceID == "" || s.appearanceAssetAvailable(slot, resourceID) {
|
||||
continue
|
||||
}
|
||||
switch slot {
|
||||
case AppearanceAssetLogo:
|
||||
value.LogoResourceID = ""
|
||||
case AppearanceAssetDarkLogo:
|
||||
value.DarkLogoResourceID = ""
|
||||
case AppearanceAssetVideo:
|
||||
value.AuthVideoResourceID = ""
|
||||
case AppearanceAssetPoster:
|
||||
value.AuthVideoPosterResourceID = ""
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func (s *Service) appearanceAssetAvailable(slot string, resourceID string) bool {
|
||||
resource, err := s.repo.Resource(resourceID)
|
||||
if err != nil || validateAppearanceResourceType(slot, resource) != nil {
|
||||
return false
|
||||
}
|
||||
if resource.Provider != "local" {
|
||||
return true
|
||||
}
|
||||
info, err := os.Stat(filepath.Join(s.dataDir, "resources", filepath.FromSlash(resource.ObjectKey)))
|
||||
return err == nil && !info.IsDir()
|
||||
}
|
||||
|
||||
func validateAppearanceSetting(value AppearanceSetting) error {
|
||||
if value.BrandName == "" || utf8.RuneCountInString(value.BrandName) > 40 {
|
||||
return BadAuthRequest("品牌名称必须为 1 到 40 个字符")
|
||||
@@ -571,6 +622,7 @@ func publicAppearanceSetting(setting *model.SystemSetting, value AppearanceSetti
|
||||
LogoFrameEnabled: value.LogoFrameEnabled,
|
||||
AuthVideoURL: defaultAppearanceVideoURL,
|
||||
AuthVideoPosterURL: defaultAppearancePosterURL,
|
||||
AuthVideoAutoplay: value.AuthVideoAutoplay,
|
||||
SkinID: value.SkinID,
|
||||
ActiveSkin: activeAppearanceSkin(value.SkinThemes, value.SkinID),
|
||||
SEOTitle: effectiveAppearanceSEOTitle(value),
|
||||
|
||||
@@ -3,12 +3,14 @@ package service
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"math"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/textproto"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -27,7 +29,7 @@ func TestAppearanceDefaultsPreserveBuiltInBrand(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if appearance.Configured || appearance.SchemaVersion != appearanceSchemaVersion || appearance.BrandName != defaultAppearanceBrandName || appearance.BrandSlug != defaultAppearanceBrandSlug || appearance.AuthHeroTitle != defaultAppearanceHeroTitle || appearance.AuthHeroDescription != "" || appearance.LogoURL != defaultAppearanceLogoURL || appearance.DarkLogoURL != defaultAppearanceLogoURL || !appearance.LogoFrameEnabled || appearance.AuthVideoURL != defaultAppearanceVideoURL || appearance.AuthVideoPosterURL != defaultAppearancePosterURL || appearance.SkinID != defaultAppearanceSkinID || appearance.SEOTitle != defaultAppearanceBrandName || !strings.Contains(appearance.SEODescription, defaultAppearanceBrandName) || !strings.Contains(appearance.FooterCopyright, defaultAppearanceBrandName) || appearance.ICPFilingEnabled {
|
||||
if appearance.Configured || appearance.SchemaVersion != appearanceSchemaVersion || appearance.BrandName != defaultAppearanceBrandName || appearance.BrandSlug != defaultAppearanceBrandSlug || appearance.AuthHeroTitle != defaultAppearanceHeroTitle || appearance.AuthHeroDescription != "" || appearance.LogoURL != defaultAppearanceLogoURL || appearance.DarkLogoURL != defaultAppearanceLogoURL || !appearance.LogoFrameEnabled || appearance.AuthVideoURL != defaultAppearanceVideoURL || appearance.AuthVideoPosterURL != defaultAppearancePosterURL || !appearance.AuthVideoAutoplay || appearance.SkinID != defaultAppearanceSkinID || appearance.SEOTitle != defaultAppearanceBrandName || !strings.Contains(appearance.SEODescription, defaultAppearanceBrandName) || !strings.Contains(appearance.FooterCopyright, defaultAppearanceBrandName) || appearance.ICPFilingEnabled {
|
||||
t.Fatalf("Appearance() = %#v", appearance)
|
||||
}
|
||||
if appearance.LogoConfigured || appearance.DarkLogoConfigured || appearance.AuthVideoConfigured || appearance.AuthVideoPosterConfigured || appearance.Revision != "builtin" {
|
||||
@@ -48,7 +50,53 @@ func TestAppearanceDefaultsPreserveBuiltInBrand(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppearanceBackfillsVersionSixFieldsForExistingSetting(t *testing.T) {
|
||||
func TestBuiltInAppearanceSkinTooltipPairsMeetContrast(t *testing.T) {
|
||||
for _, skin := range defaultAppearanceSkinThemes() {
|
||||
modes := []struct {
|
||||
name string
|
||||
tokens AppearanceSkinModeTokens
|
||||
}{
|
||||
{name: "light", tokens: skin.Tokens.Light},
|
||||
{name: "dark", tokens: skin.Tokens.Dark},
|
||||
}
|
||||
for _, mode := range modes {
|
||||
ratio := appearanceColorContrastRatio(t, mode.tokens.Overlay, mode.tokens.Text)
|
||||
if ratio < 4.5 {
|
||||
t.Errorf("skin %s %s tooltip contrast = %.2f, want at least 4.5", skin.ID, mode.name, ratio)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func appearanceColorContrastRatio(t *testing.T, foreground, background string) float64 {
|
||||
t.Helper()
|
||||
contrastLuminance := func(color string) float64 {
|
||||
if len(color) != 7 || color[0] != '#' {
|
||||
t.Fatalf("unsupported test color %q", color)
|
||||
}
|
||||
channels := make([]float64, 3)
|
||||
for index := range channels {
|
||||
value, err := strconv.ParseUint(color[1+index*2:3+index*2], 16, 8)
|
||||
if err != nil {
|
||||
t.Fatalf("parse test color %q: %v", color, err)
|
||||
}
|
||||
srgb := float64(value) / 255
|
||||
if srgb <= 0.04045 {
|
||||
channels[index] = srgb / 12.92
|
||||
} else {
|
||||
channels[index] = math.Pow((srgb+0.055)/1.055, 2.4)
|
||||
}
|
||||
}
|
||||
return 0.2126*channels[0] + 0.7152*channels[1] + 0.0722*channels[2]
|
||||
}
|
||||
foregroundLuminance := contrastLuminance(foreground)
|
||||
backgroundLuminance := contrastLuminance(background)
|
||||
lighter := math.Max(foregroundLuminance, backgroundLuminance)
|
||||
darker := math.Min(foregroundLuminance, backgroundLuminance)
|
||||
return (lighter + 0.05) / (darker + 0.05)
|
||||
}
|
||||
|
||||
func TestAppearanceBackfillsVersionSevenFieldsForExistingSetting(t *testing.T) {
|
||||
svc, db, _, _ := newAppearanceTestService(t)
|
||||
legacy := model.SystemSetting{Key: appearanceSettingKey, ValueJSON: `{"schemaVersion":1,"brandName":"旧品牌","skinId":"classic"}`}
|
||||
if err := db.Create(&legacy).Error; err != nil {
|
||||
@@ -59,13 +107,13 @@ func TestAppearanceBackfillsVersionSixFieldsForExistingSetting(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if appearance.SchemaVersion != appearanceSchemaVersion || appearance.BrandName != "旧品牌" || appearance.BrandSlug != defaultAppearanceBrandSlug || appearance.AuthHeroTitle != defaultAppearanceHeroTitle || appearance.AuthHeroDescription != "" || appearance.DarkLogoURL != defaultAppearanceLogoURL || !appearance.LogoFrameEnabled || appearance.SEOTitle != "旧品牌" || !strings.Contains(appearance.SEODescription, "旧品牌") || !strings.Contains(appearance.FooterCopyright, "旧品牌") || appearance.ActiveSkin.ID != "classic" {
|
||||
if appearance.SchemaVersion != appearanceSchemaVersion || appearance.BrandName != "旧品牌" || appearance.BrandSlug != defaultAppearanceBrandSlug || appearance.AuthHeroTitle != defaultAppearanceHeroTitle || appearance.AuthHeroDescription != "" || appearance.DarkLogoURL != defaultAppearanceLogoURL || !appearance.LogoFrameEnabled || !appearance.AuthVideoAutoplay || appearance.SEOTitle != "旧品牌" || !strings.Contains(appearance.SEODescription, "旧品牌") || !strings.Contains(appearance.FooterCopyright, "旧品牌") || appearance.ActiveSkin.ID != "classic" {
|
||||
t.Fatalf("legacy appearance = %#v", appearance)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateAppearancePersistsAuditsAndUsesVersionedPublicAssets(t *testing.T) {
|
||||
svc, db, _, admin := newAppearanceTestService(t)
|
||||
svc, db, dataDir, admin := newAppearanceTestService(t)
|
||||
resources := []model.Resource{
|
||||
{ID: "brand-logo", UserID: admin.ID, Kind: "image", Status: model.ResourceStatusReady, Provider: "local", ObjectKey: "brand/logo.png", MimeType: "image/png"},
|
||||
{ID: "brand-logo-dark", UserID: admin.ID, Kind: "image", Status: model.ResourceStatusReady, Provider: "local", ObjectKey: "brand/logo-dark.png", MimeType: "image/png"},
|
||||
@@ -75,6 +123,7 @@ func TestUpdateAppearancePersistsAuditsAndUsesVersionedPublicAssets(t *testing.T
|
||||
if err := db.Create(&resources).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeAppearanceResourceFixtures(t, dataDir, resources)
|
||||
|
||||
updated, err := svc.UpdateAppearance(admin, AppearanceSetting{
|
||||
BrandName: "HIMA Studio",
|
||||
@@ -86,6 +135,7 @@ func TestUpdateAppearancePersistsAuditsAndUsesVersionedPublicAssets(t *testing.T
|
||||
LogoFrameEnabled: false,
|
||||
AuthVideoResourceID: "brand-video",
|
||||
AuthVideoPosterResourceID: "brand-poster",
|
||||
AuthVideoAutoplay: false,
|
||||
SkinID: "studio-indigo",
|
||||
SEOTitle: "HIMA Studio - AI 影视工作台",
|
||||
SEODescription: "面向 Agent、图片、视频、画布与短剧生产的一体化 AI 创作工作台。",
|
||||
@@ -97,7 +147,7 @@ func TestUpdateAppearancePersistsAuditsAndUsesVersionedPublicAssets(t *testing.T
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !updated.Configured || updated.BrandName != "HIMA Studio" || updated.BrandSlug != "hima-studio" || updated.Public.BrandSlug != "hima-studio" || updated.AuthHeroTitle != "把灵感,\n变成可见的故事。" || updated.Public.AuthHeroDescription != "从画布开始,持续推进你的创作。" || !updated.Public.LogoConfigured || !updated.Public.DarkLogoConfigured || updated.Public.LogoFrameEnabled || !updated.Public.AuthVideoConfigured || !updated.Public.AuthVideoPosterConfigured || updated.Public.SkinID != "studio-indigo" || updated.Public.SEOTitle != "HIMA Studio - AI 影视工作台" || updated.Public.SEOKeywords != "AI 影视,短剧,画布" || !updated.Public.ICPFilingEnabled || updated.Public.ICPFilingNumber != "蜀ICP备2026000000号-1" {
|
||||
if !updated.Configured || updated.BrandName != "HIMA Studio" || updated.BrandSlug != "hima-studio" || updated.Public.BrandSlug != "hima-studio" || updated.AuthHeroTitle != "把灵感,\n变成可见的故事。" || updated.Public.AuthHeroDescription != "从画布开始,持续推进你的创作。" || !updated.Public.LogoConfigured || !updated.Public.DarkLogoConfigured || updated.Public.LogoFrameEnabled || !updated.Public.AuthVideoConfigured || !updated.Public.AuthVideoPosterConfigured || updated.Public.AuthVideoAutoplay || updated.Public.SkinID != "studio-indigo" || updated.Public.SEOTitle != "HIMA Studio - AI 影视工作台" || updated.Public.SEOKeywords != "AI 影视,短剧,画布" || !updated.Public.ICPFilingEnabled || updated.Public.ICPFilingNumber != "蜀ICP备2026000000号-1" {
|
||||
t.Fatalf("UpdateAppearance() = %#v", updated)
|
||||
}
|
||||
for _, assetURL := range []string{updated.Public.LogoURL, updated.Public.DarkLogoURL, updated.Public.AuthVideoURL, updated.Public.AuthVideoPosterURL} {
|
||||
@@ -125,7 +175,7 @@ func TestUpdateAppearancePersistsAuditsAndUsesVersionedPublicAssets(t *testing.T
|
||||
}
|
||||
|
||||
func TestAppearanceReusesSingleLogoAcrossThemes(t *testing.T) {
|
||||
svc, db, _, admin := newAppearanceTestService(t)
|
||||
svc, db, dataDir, admin := newAppearanceTestService(t)
|
||||
resources := []model.Resource{
|
||||
{ID: "only-light", UserID: admin.ID, Kind: "image", Status: model.ResourceStatusReady, Provider: "local", ObjectKey: "brand/light.png", MimeType: "image/png"},
|
||||
{ID: "only-dark", UserID: admin.ID, Kind: "image", Status: model.ResourceStatusReady, Provider: "local", ObjectKey: "brand/dark.png", MimeType: "image/png"},
|
||||
@@ -133,6 +183,7 @@ func TestAppearanceReusesSingleLogoAcrossThemes(t *testing.T) {
|
||||
if err := db.Create(&resources).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeAppearanceResourceFixtures(t, dataDir, resources)
|
||||
|
||||
lightOnly, err := svc.UpdateAppearance(admin, AppearanceSetting{BrandName: "HIMA Studio", BrandSlug: "hima-studio", AuthHeroTitle: defaultAppearanceHeroTitle, LogoResourceID: "only-light", LogoFrameEnabled: true, SkinID: defaultAppearanceSkinID})
|
||||
if err != nil {
|
||||
@@ -233,6 +284,89 @@ func TestOpenAppearanceAssetOnlyServesConfiguredSlot(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppearanceFallsBackWhenConfiguredLogoResourceIsMissing(t *testing.T) {
|
||||
svc, db, _, admin := newAppearanceTestService(t)
|
||||
value := defaultAppearanceSetting()
|
||||
value.BrandName = "HIMA Studio"
|
||||
value.BrandSlug = "hima-studio"
|
||||
value.LogoResourceID = "missing-logo"
|
||||
encoded, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.SystemSetting{Key: appearanceSettingKey, ValueJSON: string(encoded)}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
appearance, err := svc.Appearance()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if appearance.LogoConfigured || appearance.LogoURL != defaultAppearanceLogoURL || appearance.DarkLogoURL != defaultAppearanceLogoURL {
|
||||
t.Fatalf("missing logo public appearance = %#v", appearance)
|
||||
}
|
||||
adminAppearance, err := svc.AdminAppearance(admin)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if adminAppearance.LogoResourceID != "" || adminAppearance.Public.LogoConfigured {
|
||||
t.Fatalf("missing logo admin appearance = %#v", adminAppearance)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppearanceFallsBackWhenLocalLogoFileIsMissing(t *testing.T) {
|
||||
svc, db, _, _ := newAppearanceTestService(t)
|
||||
resource := model.Resource{ID: "missing-local-logo", UserID: "appearance-admin", Kind: "image", Status: model.ResourceStatusReady, Provider: "local", ObjectKey: "brand/missing.png", MimeType: "image/png"}
|
||||
if err := db.Create(&resource).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value := defaultAppearanceSetting()
|
||||
value.LogoResourceID = resource.ID
|
||||
encoded, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.SystemSetting{Key: appearanceSettingKey, ValueJSON: string(encoded)}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
appearance, err := svc.Appearance()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if appearance.LogoConfigured || appearance.LogoURL != defaultAppearanceLogoURL {
|
||||
t.Fatalf("missing local logo public appearance = %#v", appearance)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadAppearanceLogoAlwaysUsesServerLocalStorage(t *testing.T) {
|
||||
svc, db, dataDir, admin := newAppearanceTestService(t)
|
||||
requests := make(chan struct{}, 2)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
requests <- struct{}{}
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}))
|
||||
defer server.Close()
|
||||
seedOSSEnabled(t, db, admin.ID, server.URL)
|
||||
pngBytes := append([]byte{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a}, bytes.Repeat([]byte{0}, 32)...)
|
||||
|
||||
for _, slot := range []string{AppearanceAssetLogo, AppearanceAssetDarkLogo} {
|
||||
resource, err := svc.UploadAppearanceAsset(admin, slot, multipartFileHeader(t, slot+".png", "image/png", pngBytes))
|
||||
if err != nil {
|
||||
t.Fatalf("UploadAppearanceAsset(%s): %v", slot, err)
|
||||
}
|
||||
if resource.Provider != "local" || resource.Endpoint != "" || resource.Bucket != "" || resource.StorageSettingID != "" {
|
||||
t.Fatalf("UploadAppearanceAsset(%s) storage = %#v", slot, resource)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dataDir, "resources", filepath.FromSlash(resource.ObjectKey))); err != nil {
|
||||
t.Fatalf("UploadAppearanceAsset(%s) local file: %v", slot, err)
|
||||
}
|
||||
}
|
||||
if len(requests) != 0 {
|
||||
t.Fatalf("logo upload unexpectedly contacted object storage %d time(s)", len(requests))
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateAppearanceValidatesLoginCopy(t *testing.T) {
|
||||
svc, _, _, admin := newAppearanceTestService(t)
|
||||
|
||||
@@ -367,6 +501,22 @@ func multipartFileHeader(t *testing.T, name string, contentType string, data []b
|
||||
return req.MultipartForm.File["file"][0]
|
||||
}
|
||||
|
||||
func writeAppearanceResourceFixtures(t *testing.T, dataDir string, resources []model.Resource) {
|
||||
t.Helper()
|
||||
for _, resource := range resources {
|
||||
if resource.Provider != "local" {
|
||||
continue
|
||||
}
|
||||
filePath := filepath.Join(dataDir, "resources", filepath.FromSlash(resource.ObjectKey))
|
||||
if err := os.MkdirAll(filepath.Dir(filePath), 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filePath, []byte(resource.ID), 0o640); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newAppearanceTestService(t *testing.T) (*Service, *gorm.DB, string, *model.User) {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open("file:"+newID()+"?mode=memory&cache=shared"), &gorm.Config{})
|
||||
|
||||
@@ -35,6 +35,8 @@ type ChannelModelRequest struct {
|
||||
PriceTiers []ChannelModelPriceTierRequest `json:"priceTiers"`
|
||||
}
|
||||
|
||||
const maxAdminChannelModelBatchDeleteCount = 100
|
||||
|
||||
// ChannelModelPriceTierRequest 是系统渠道内某个规格的上游 SKU 与结算价格。
|
||||
// Resolution="*"、VideoSeconds=0 分别表示任意分辨率和任意时长。
|
||||
type ChannelModelPriceTierRequest struct {
|
||||
@@ -826,47 +828,76 @@ func normalizeChannelModelContractWithRegistry(registry *protocol.Registry, chan
|
||||
}
|
||||
|
||||
func (s *Service) DeleteAdminChannelModel(actor *model.User, channelID string, id string) error {
|
||||
_, err := s.DeleteAdminChannelModels(actor, channelID, []string{id})
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteAdminChannelModels validates the complete selection before asking the
|
||||
// repository to remove it atomically. This deliberately rejects partial success:
|
||||
// administrators can safely correct an in-use model and retry the same selection.
|
||||
func (s *Service) DeleteAdminChannelModels(actor *model.User, channelID string, ids []string) (int64, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return err
|
||||
return 0, err
|
||||
}
|
||||
if _, err := s.repo.AdminSystemChannel(channelID); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return BadAuthRequest("系统渠道不存在或已删除")
|
||||
return 0, BadAuthRequest("系统渠道不存在或已删除")
|
||||
}
|
||||
return err
|
||||
return 0, err
|
||||
}
|
||||
if _, err := s.repo.ChannelModelByID(channelID, id); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return BadAuthRequest("渠道模型不存在或已删除")
|
||||
}
|
||||
return err
|
||||
}
|
||||
items, err := s.repo.ChannelModels(channelID, false)
|
||||
modelIDs, err := normalizeAdminChannelModelDeleteIDs(ids)
|
||||
if err != nil {
|
||||
return err
|
||||
return 0, err
|
||||
}
|
||||
names := make([]string, 0, len(items))
|
||||
items, err := s.repo.ChannelModels(channelID, true)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
selected := make(map[string]bool, len(modelIDs))
|
||||
for _, id := range modelIDs {
|
||||
selected[id] = true
|
||||
}
|
||||
found := 0
|
||||
for _, item := range items {
|
||||
if item.ID != id {
|
||||
names = append(names, item.ModelKey)
|
||||
if selected[item.ID] {
|
||||
found++
|
||||
}
|
||||
}
|
||||
encoded, err := json.Marshal(names)
|
||||
if err != nil {
|
||||
return err
|
||||
if found != len(modelIDs) {
|
||||
return 0, BadAuthRequest("所选渠道模型中存在已删除或不属于当前渠道的记录,请刷新后重试")
|
||||
}
|
||||
// 删除模型与渠道的兼容模型清单必须同事务提交,避免接口报错但列表已部分变化。
|
||||
err = s.repo.DeleteChannelModel(channelID, id, string(encoded), time.Now())
|
||||
deleted, err := s.repo.DeleteChannelModels(channelID, modelIDs, time.Now())
|
||||
if errors.Is(err, repository.ErrChannelModelInUse) {
|
||||
return BadAuthRequest("渠道模型仍被前台模型供应线路或进行中任务使用,请先移除线路并等待任务结束")
|
||||
return 0, BadAuthRequest("所选渠道模型中有模型仍被前台模型供应线路或进行中任务使用,本次未删除任何模型")
|
||||
}
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return BadAuthRequest("渠道模型不存在或已删除")
|
||||
return 0, BadAuthRequest("所选渠道模型中存在已删除或不属于当前渠道的记录,请刷新后重试")
|
||||
}
|
||||
if err == nil {
|
||||
s.invalidateRouteCatalog()
|
||||
}
|
||||
return err
|
||||
return deleted, err
|
||||
}
|
||||
|
||||
func normalizeAdminChannelModelDeleteIDs(values []string) ([]string, error) {
|
||||
result := make([]string, 0, len(values))
|
||||
seen := make(map[string]bool, len(values))
|
||||
for _, value := range values {
|
||||
id := strings.TrimSpace(value)
|
||||
if id == "" || seen[id] {
|
||||
continue
|
||||
}
|
||||
seen[id] = true
|
||||
result = append(result, id)
|
||||
}
|
||||
if len(result) == 0 {
|
||||
return nil, BadAuthRequest("请至少选择一个要删除的渠道模型")
|
||||
}
|
||||
if len(result) > maxAdminChannelModelBatchDeleteCount {
|
||||
return nil, BadAuthRequest("单次最多删除 100 个渠道模型")
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *Service) syncInitialChannelModels(channel *model.ModelChannel, names []string) error {
|
||||
@@ -945,20 +976,7 @@ func (s *Service) ensureChannelModels(channelID string, includeDisabled bool) ([
|
||||
}
|
||||
|
||||
func (s *Service) syncChannelModelNames(channel *model.ModelChannel) error {
|
||||
items, err := s.repo.ChannelModels(channel.ID, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
names := make([]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
names = append(names, item.ModelKey)
|
||||
}
|
||||
encoded, err := json.Marshal(names)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
channel.ModelsJSON = string(encoded)
|
||||
return s.repo.Save(channel)
|
||||
return s.repo.SyncChannelModelNames(channel.ID, time.Now())
|
||||
}
|
||||
|
||||
func (s *Service) capabilityForProtocol(protocol model.ChannelInterfaceType) string {
|
||||
|
||||
@@ -86,6 +86,25 @@ func TestEnrichAPICallLogReadsArkVideoUsage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnrichAPICallLogPrefersNewAPIChannel2NestedTaskID(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
payload string
|
||||
}{
|
||||
{name: "snake case", payload: `{"id":"wrapper-id","data":{"task_id":"task-snake"}}`},
|
||||
{name: "camel case", payload: `{"id":"wrapper-id","data":{"taskId":"task-camel"}}`},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
log := &model.ApiCallLog{Capability: "video", Path: "/v1/video/generations"}
|
||||
(&Service{}).EnrichAPICallLog(log, []byte(test.payload))
|
||||
if !strings.HasPrefix(log.ProviderRequestID, "task-") {
|
||||
t.Fatalf("ProviderRequestID = %q, want nested upstream task ID", log.ProviderRequestID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnrichAPICallLogKeepsUserFacingAndUpstreamFailureDetails(t *testing.T) {
|
||||
log := &model.ApiCallLog{
|
||||
Status: model.ApiCallStatusFailed,
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestImageSizePresetsValidationAndRoundTrip(t *testing.T) {
|
||||
profile := DefaultImageCapabilityConfig("openai-image", "test")
|
||||
profile.Size = ImageSizeConfig{Parameter: "size", Values: []string{"1824x1024", "1920x1080"}, Default: "1824x1024", Presets: []ImageSizePreset{
|
||||
{Tier: "1k", Ratio: "16:9", Size: "1824x1024", Width: 1824, Height: 1024},
|
||||
{Tier: "2k", Ratio: "16:9", Size: "1920x1080", Width: 1920, Height: 1080},
|
||||
}}
|
||||
if err := validateImageCapabilityConfig(profile); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := json.Marshal(profile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var restored ImageCapabilityConfig
|
||||
if err := json.Unmarshal(data, &restored); err != nil || len(restored.Size.Presets) != 2 || restored.Size.Presets[1].Size != "1920x1080" {
|
||||
t.Fatalf("presets did not survive JSON round trip: %v", err)
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
edit func(*ImageCapabilityConfig)
|
||||
want string
|
||||
}{
|
||||
{"invalid tier", func(p *ImageCapabilityConfig) { p.Size.Presets[0].Tier = "8k" }, "档位"},
|
||||
{"zero ratio", func(p *ImageCapabilityConfig) { p.Size.Presets[0].Ratio = "0:9" }, "比例"},
|
||||
{"mismatched ratio", func(p *ImageCapabilityConfig) { p.Size.Presets[0].Ratio = "9:16" }, "不一致"},
|
||||
{"tiny size", func(p *ImageCapabilityConfig) {
|
||||
p.Size.Presets[0].Width = 16
|
||||
p.Size.Presets[0].Height = 16
|
||||
p.Size.Presets[0].Size = "16x16"
|
||||
}, "像素"},
|
||||
{"size mismatch", func(p *ImageCapabilityConfig) { p.Size.Presets[0].Size = "1024x1024" }, "像素"},
|
||||
{"not supported", func(p *ImageCapabilityConfig) { p.Size.Values = []string{"1824x1024"} }, "支持值"},
|
||||
{"duplicate equivalent ratio", func(p *ImageCapabilityConfig) { p.Size.Presets[1].Tier = "1k"; p.Size.Presets[1].Ratio = "32:18" }, "重复"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var p ImageCapabilityConfig
|
||||
if err := json.Unmarshal(data, &p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tc.edit(&p)
|
||||
if err := validateImageCapabilityConfig(&p); err == nil || !strings.Contains(err.Error(), tc.want) {
|
||||
t.Fatalf("expected %s, got %v", tc.want, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageSizePresetsEnforceResolutionRatioPair(t *testing.T) {
|
||||
profile := DefaultImageCapabilityConfig("grok-image", "grok-imagine-image")
|
||||
profile.Quality.Default = "1k"
|
||||
profile.Size = ImageSizeConfig{Parameter: "aspect_ratio", Values: []string{"1:1", "16:9"}, Default: "1:1", Presets: []ImageSizePreset{
|
||||
{Tier: "1k", Ratio: "1:1", Size: "1024x1024", Width: 1024, Height: 1024},
|
||||
{Tier: "2k", Ratio: "16:9", Size: "2752x1536", Width: 2752, Height: 1536},
|
||||
}}
|
||||
if err := validateImageCapabilityConfig(profile); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
profile.Quality.Default = "2k"
|
||||
if err := validateImageCapabilityConfig(profile); err == nil || !strings.Contains(err.Error(), "默认图片分辨率") {
|
||||
t.Fatalf("expected invalid default pair error, got %v", err)
|
||||
}
|
||||
profile.Quality.Default = "1k"
|
||||
input := canvasGenerationInput{Config: providerConfig{Size: "1:1", Quality: "2k"}}
|
||||
if err := validateImageTask(profile, input); err == nil || !strings.Contains(err.Error(), "当前分辨率") {
|
||||
t.Fatalf("expected pair validation error, got %v", err)
|
||||
}
|
||||
input.Config.Size = "16:9"
|
||||
if err := validateImageTask(profile, input); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
profile.Size.AllowCustom = true
|
||||
input.Config.Size = "1:1"
|
||||
if err := validateImageTask(profile, input); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -65,6 +65,41 @@ func TestMatchCapabilityTreatsVideoResolutionSuffixAsEquivalent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestVideoResolutionAliasesAcrossProductAndPricedRoutes(t *testing.T) {
|
||||
product := CapabilitySpec{
|
||||
Version: 1, Capability: "video",
|
||||
Options: map[string]OptionConstraint{"vquality": {Values: []any{"768P", "2K"}}},
|
||||
}
|
||||
route := capabilitySpecWithPriceTiers(product, model.ChannelModel{
|
||||
PriceTiers: []model.ChannelModelPriceTier{
|
||||
{Resolution: "768P", Enabled: true, PriceConfigured: true},
|
||||
{Resolution: "2K", Enabled: true, PriceConfigured: true},
|
||||
},
|
||||
})
|
||||
if err := validateProductSpecWithinRoutes(product, []CapabilitySpec{route}); err != nil {
|
||||
t.Fatalf("2K product rejected against priced route: %v", err)
|
||||
}
|
||||
for _, resolution := range []string{"2K", "2k", "1440P", "1440"} {
|
||||
intent := ModelRequestIntent{Capability: "video", Options: map[string]any{
|
||||
"vquality": normalizeModelRequestOption("vquality", resolution),
|
||||
}}
|
||||
for name, spec := range map[string]CapabilitySpec{"product": product, "route": route} {
|
||||
if match := MatchCapability(spec, intent); !match.Matched {
|
||||
t.Errorf("%s rejected %s: %#v", name, resolution, match)
|
||||
}
|
||||
}
|
||||
}
|
||||
unsupported := CapabilitySpec{Version: 1, Capability: "video", Options: map[string]OptionConstraint{
|
||||
"vquality": {Values: []any{"4K"}},
|
||||
}}
|
||||
if err := validateProductSpecWithinRoutes(unsupported, []CapabilitySpec{route}); err == nil {
|
||||
t.Fatal("2K route accepted unsupported 4K product")
|
||||
}
|
||||
if capabilityOptionValuesEqual("quality", "2K", "1440p") {
|
||||
t.Fatal("video aliases must not affect image quality")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateProductSpecWithinRoutesRejectsUnsupportedCapabilityValue(t *testing.T) {
|
||||
routes := []CapabilitySpec{
|
||||
{
|
||||
|
||||
@@ -50,10 +50,19 @@ type ImageReferenceConfig struct {
|
||||
}
|
||||
|
||||
type ImageSizeConfig struct {
|
||||
Parameter string `json:"parameter"`
|
||||
Values []string `json:"values"`
|
||||
Default string `json:"default"`
|
||||
AllowCustom bool `json:"allowCustom"`
|
||||
Parameter string `json:"parameter"`
|
||||
Values []string `json:"values"`
|
||||
Default string `json:"default"`
|
||||
AllowCustom bool `json:"allowCustom"`
|
||||
Presets []ImageSizePreset `json:"presets,omitempty"`
|
||||
}
|
||||
|
||||
type ImageSizePreset struct {
|
||||
Tier string `json:"tier"`
|
||||
Ratio string `json:"ratio"`
|
||||
Size string `json:"size"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
}
|
||||
|
||||
type ImageQualityConfig struct {
|
||||
@@ -494,6 +503,7 @@ func validateImageCapabilityConfig(value *ImageCapabilityConfig) error {
|
||||
switch value.Size.Parameter {
|
||||
case "none":
|
||||
value.Size.Values = []string{}
|
||||
value.Size.Presets = nil
|
||||
value.Size.Default = "auto"
|
||||
value.Size.AllowCustom = false
|
||||
case "size", "aspect_ratio":
|
||||
@@ -506,6 +516,48 @@ func validateImageCapabilityConfig(value *ImageCapabilityConfig) error {
|
||||
default:
|
||||
return BadAuthRequest("尺寸参数仅支持不发送、size 或 aspect_ratio")
|
||||
}
|
||||
seenPresets := make(map[string]bool)
|
||||
for _, preset := range value.Size.Presets {
|
||||
if preset.Tier != "1k" && preset.Tier != "2k" && preset.Tier != "4k" {
|
||||
return BadAuthRequest("图片分辨率档位仅支持 1K、2K、4K")
|
||||
}
|
||||
parts := strings.Split(preset.Ratio, ":")
|
||||
if len(parts) != 2 {
|
||||
return BadAuthRequest("图片预设比例格式无效")
|
||||
}
|
||||
w, ew := strconv.Atoi(parts[0])
|
||||
h, eh := strconv.Atoi(parts[1])
|
||||
if ew != nil || eh != nil || w <= 0 || h <= 0 || w > 100000 || h > 100000 || max(w, h) > min(w, h)*3 {
|
||||
return BadAuthRequest("图片预设比例无效")
|
||||
}
|
||||
if preset.Width <= 0 || preset.Height <= 0 || max(preset.Width, preset.Height) > 3840 || max(preset.Width, preset.Height) > min(preset.Width, preset.Height)*3 || preset.Width*preset.Height < 655360 || preset.Width*preset.Height > 8294400 || preset.Size != fmt.Sprintf("%dx%d", preset.Width, preset.Height) {
|
||||
return BadAuthRequest("图片预设像素尺寸无效")
|
||||
}
|
||||
// 容许像素取整误差,但不能将横屏尺寸标记成竖屏或其他比例。
|
||||
difference := preset.Width*h - preset.Height*w
|
||||
if difference < 0 {
|
||||
difference = -difference
|
||||
}
|
||||
if difference*1000 > preset.Height*w*25 {
|
||||
return BadAuthRequest("图片预设像素尺寸与宽高比不一致")
|
||||
}
|
||||
a, b := w, h
|
||||
for b != 0 {
|
||||
a, b = b, a%b
|
||||
}
|
||||
key := fmt.Sprintf("%s:%d:%d", preset.Tier, w/a, h/a)
|
||||
if seenPresets[key] {
|
||||
return BadAuthRequest("图片尺寸预设重复")
|
||||
}
|
||||
seenPresets[key] = true
|
||||
requestValue := preset.Size
|
||||
if value.Size.Parameter == "aspect_ratio" {
|
||||
requestValue = preset.Ratio
|
||||
}
|
||||
if !containsCapabilityString(value.Size.Values, requestValue) {
|
||||
return BadAuthRequest("图片预设必须包含在尺寸支持值中")
|
||||
}
|
||||
}
|
||||
if value.Quality.Supported {
|
||||
if len(value.Quality.Values) == 0 || strings.TrimSpace(value.Quality.Default) == "" || !containsCapabilityString(value.Quality.Values, value.Quality.Default) {
|
||||
return BadAuthRequest("请配置图片质量支持值和默认值")
|
||||
@@ -514,6 +566,9 @@ func validateImageCapabilityConfig(value *ImageCapabilityConfig) error {
|
||||
value.Quality.Values = []string{}
|
||||
value.Quality.Default = "auto"
|
||||
}
|
||||
if err := validateImagePresetSelection(value, value.Quality.Default, value.Size.Default); err != nil {
|
||||
return BadAuthRequest("默认图片分辨率与宽高比不在已配置的组合中")
|
||||
}
|
||||
if !value.TransparentBackground.Supported {
|
||||
value.TransparentBackground.Default = false
|
||||
}
|
||||
@@ -770,6 +825,9 @@ func validateImageTask(profile *ImageCapabilityConfig, input canvasGenerationInp
|
||||
if profile.Quality.Supported && strings.TrimSpace(input.Config.Quality) != "" && !containsCapabilityString(profile.Quality.Values, input.Config.Quality) {
|
||||
return BadAuthRequest("图片质量不在当前模型支持范围内")
|
||||
}
|
||||
if err := validateImagePresetSelection(profile, firstNonEmpty(input.Config.Quality, profile.Quality.Default), firstNonEmpty(input.Config.Size, profile.Size.Default)); err != nil {
|
||||
return err
|
||||
}
|
||||
count, err := strconv.Atoi(strings.TrimSpace(input.Config.Count))
|
||||
if err == nil && count > profile.MaxOutputs {
|
||||
return BadAuthRequest(fmt.Sprintf("当前图片模型单次最多生成 %d 张", profile.MaxOutputs))
|
||||
@@ -777,6 +835,35 @@ func validateImageTask(profile *ImageCapabilityConfig, input canvasGenerationInp
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateImagePresetSelection(profile *ImageCapabilityConfig, quality, ratio string) error {
|
||||
if profile.Size.Parameter != "aspect_ratio" || profile.Size.AllowCustom || len(profile.Size.Presets) == 0 || ratio == "auto" {
|
||||
return nil
|
||||
}
|
||||
tier := imageResolutionTier(quality)
|
||||
if tier == "" {
|
||||
return nil
|
||||
}
|
||||
for _, preset := range profile.Size.Presets {
|
||||
if preset.Tier == tier && preset.Ratio == ratio {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return BadAuthRequest("当前分辨率不支持所选图片宽高比")
|
||||
}
|
||||
|
||||
func imageResolutionTier(quality string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(quality)) {
|
||||
case "1k", "low":
|
||||
return "1k"
|
||||
case "2k", "medium":
|
||||
return "2k"
|
||||
case "4k", "high":
|
||||
return "4k"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func validateWorkflowProviderPromptLength(input canvasGenerationInput) error {
|
||||
profile := input.Config.CapabilityConfig
|
||||
if input.Mode != "video" || profile == nil || profile.Video == nil {
|
||||
|
||||
@@ -358,8 +358,9 @@ func capabilityOptionValuesEqual(name string, candidate any, value any) bool {
|
||||
left := normalizedScalar(candidate)
|
||||
right := normalizedScalar(value)
|
||||
if canonicalCapabilityOptionName(name) == "vquality" {
|
||||
left = strings.TrimSuffix(left, "p")
|
||||
right = strings.TrimSuffix(right, "p")
|
||||
// Compare using the same aliases as request intents and price tiers.
|
||||
left = strings.TrimSuffix(normalizedScalar(normalizeModelRequestOption(name, left)), "p")
|
||||
right = strings.TrimSuffix(normalizedScalar(normalizeModelRequestOption(name, right)), "p")
|
||||
}
|
||||
return left == right
|
||||
}
|
||||
|
||||
@@ -5,12 +5,14 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"infinite-canvas/backend/internal/protocol"
|
||||
)
|
||||
@@ -342,6 +344,129 @@ func TestDeclarativeProtocolRuntimeExecutesCreatePollAndDownload(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeclarativeNewAPIChannel2TaskNotExistRetry(t *testing.T) {
|
||||
adapter := newDeclarativeNewAPIChannel2TestAdapter(t)
|
||||
createCalls := 0
|
||||
pollCalls := 0
|
||||
var server *httptest.Server
|
||||
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/v1/video/generations":
|
||||
createCalls++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":"provider-task-1","status":"pending"}`))
|
||||
case "/v1/video/generations/provider-task-1":
|
||||
pollCalls++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if pollCalls <= 2 {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_, _ = w.Write([]byte(`{"code":"task_not_exist","data":null,"message":"task_not_exist"}`))
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"id":"provider-task-1","status":"succeeded","video_url":"` + server.URL + `/media.mp4"}`))
|
||||
case "/media.mp4":
|
||||
w.Header().Set("Content-Type", "video/mp4")
|
||||
_, _ = w.Write([]byte("video"))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
config := providerConfig{BaseURL: server.URL + "/v1", APIKey: "key", Model: "video-model", InterfaceType: "newapi-channel-2", AllowLocalChannel: true}
|
||||
ctx := withProviderOutboundPolicy(context.Background(), config)
|
||||
result, err := runProtocolAdapterTaskWithTiming(ctx, canvasGenerationInput{Mode: "video", Prompt: "a clip", Config: config}, adapter, protocolPollTiming{
|
||||
InitialDelay: time.Millisecond,
|
||||
PollInterval: time.Millisecond,
|
||||
TaskNotExistWindow: time.Second,
|
||||
TaskNotExistRetryDelays: []time.Duration{time.Millisecond, time.Millisecond, time.Millisecond, time.Millisecond},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result["mode"] != "video" || createCalls != 1 || pollCalls != 3 {
|
||||
t.Fatalf("result = %#v, create calls = %d, poll calls = %d", result, createCalls, pollCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeclarativeNewAPIChannel2TaskNotExistExhaustion(t *testing.T) {
|
||||
adapter := newDeclarativeNewAPIChannel2TestAdapter(t)
|
||||
createCalls := 0
|
||||
pollCalls := 0
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/v1/video/generations":
|
||||
createCalls++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":"provider-task-1","status":"pending"}`))
|
||||
case "/v1/video/generations/provider-task-1":
|
||||
pollCalls++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_, _ = w.Write([]byte(`{"code":"task_not_exist","message":"task_not_exist"}`))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
config := providerConfig{BaseURL: server.URL + "/v1", APIKey: "key", Model: "video-model", InterfaceType: "newapi-channel-2", AllowLocalChannel: true}
|
||||
ctx := withProviderOutboundPolicy(context.Background(), config)
|
||||
_, err := runProtocolAdapterTaskWithTiming(ctx, canvasGenerationInput{Mode: "video", Prompt: "a clip", Config: config}, adapter, protocolPollTiming{
|
||||
InitialDelay: time.Millisecond,
|
||||
PollInterval: time.Millisecond,
|
||||
TaskNotExistWindow: time.Second,
|
||||
TaskNotExistRetryDelays: []time.Duration{time.Millisecond, time.Millisecond, time.Millisecond, time.Millisecond},
|
||||
})
|
||||
var pendingErr providerStatePendingError
|
||||
var httpErr providerHTTPError
|
||||
if !errors.As(err, &pendingErr) || pendingErr.TaskID != "provider-task-1" || !errors.As(err, &httpErr) {
|
||||
t.Fatalf("error = %#v, want typed provider pending error wrapping HTTP error", err)
|
||||
}
|
||||
if createCalls != 1 || pollCalls != 5 {
|
||||
t.Fatalf("create calls = %d, poll calls = %d, want 1 and 5", createCalls, pollCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeclarativeNewAPIChannel2TaskNotExistStrictClassification(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
interfaceType string
|
||||
taskID string
|
||||
err error
|
||||
want bool
|
||||
}{
|
||||
{name: "error code", interfaceType: "newapi-channel-2", taskID: "task-1", err: providerHTTPError{StatusCode: 400, Body: `{"code":"task_not_exist"}`}, want: true},
|
||||
{name: "error message", interfaceType: "newapi-channel-2", taskID: "task-1", err: providerHTTPError{StatusCode: 400, Body: `{"message":"task_not_exist"}`}, want: true},
|
||||
{name: "other interface", interfaceType: "newapi-channel-1", taskID: "task-1", err: providerHTTPError{StatusCode: 400, Body: `{"code":"task_not_exist"}`}},
|
||||
{name: "missing task id", interfaceType: "newapi-channel-2", err: providerHTTPError{StatusCode: 400, Body: `{"code":"task_not_exist"}`}},
|
||||
{name: "other status", interfaceType: "newapi-channel-2", taskID: "task-1", err: providerHTTPError{StatusCode: 404, Body: `{"code":"task_not_exist"}`}},
|
||||
{name: "unstructured body", interfaceType: "newapi-channel-2", taskID: "task-1", err: providerHTTPError{StatusCode: 400, Body: "task_not_exist"}},
|
||||
{name: "partial match", interfaceType: "newapi-channel-2", taskID: "task-1", err: providerHTTPError{StatusCode: 400, Body: `{"code":"task_not_exist_later"}`}},
|
||||
{name: "other provider error", interfaceType: "newapi-channel-2", taskID: "task-1", err: providerHTTPError{StatusCode: 400, Body: `{"code":"invalid_parameter"}`}},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if got := isNewAPIChannel2TaskNotReady(test.interfaceType, test.taskID, test.err); got != test.want {
|
||||
t.Fatalf("isNewAPIChannel2TaskNotReady() = %v, want %v", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func newDeclarativeNewAPIChannel2TestAdapter(t *testing.T) protocol.Adapter {
|
||||
t.Helper()
|
||||
adapter, err := protocol.LoadManifest([]byte(`{
|
||||
"apiVersion":"yingce.plugin/v1",
|
||||
"id":"newapi-channel-2","version":"1.0.0","name":"NewAPI Channel 2","author":"Test","documentation":"# Test",
|
||||
"contributes":{"providers":[{"id":"newapi-channel-2","label":"NewAPI Channel 2","capabilities":["video"],"scopes":["canvas"],"create":{"method":"POST","path":"/video/generations","fields":{"model":"request.model"}},"poll":{"method":"GET","path":"/video/generations/{{taskId}}"},"response":{"taskIdPaths":["id"],"statusPaths":["status"],"resultPaths":["video_url"],"resultKind":"video"}}]}
|
||||
}`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return adapter
|
||||
}
|
||||
|
||||
func TestDeclarativeProtocolRecoveryQueriesExistingTaskWithoutCreating(t *testing.T) {
|
||||
manifest := []byte(`{
|
||||
"apiVersion":"yingce.plugin/v2",
|
||||
|
||||
@@ -156,6 +156,17 @@ type providerHTTPError struct {
|
||||
RetryAfter time.Duration
|
||||
}
|
||||
|
||||
type providerStatePendingError struct {
|
||||
TaskID string
|
||||
Cause error
|
||||
}
|
||||
|
||||
func (e providerStatePendingError) Error() string {
|
||||
return fmt.Sprintf("上游任务状态尚未同步,将继续查询原任务(任务 %s)", e.TaskID)
|
||||
}
|
||||
|
||||
func (e providerStatePendingError) Unwrap() error { return e.Cause }
|
||||
|
||||
type providerAnalyticsKey struct{}
|
||||
type providerOutboundPolicyKey struct{}
|
||||
|
||||
@@ -368,7 +379,7 @@ func (s *Service) processCanvasGenerationTask(ctx context.Context, userID string
|
||||
}
|
||||
// 工作流参数由工作流字段定义校验,普通模型能力配置不能覆盖它们。
|
||||
if resumedProviderRequestID(ctx) == "" {
|
||||
if err := s.hydrateGenerationMedia(userID, &input, false); err != nil {
|
||||
if err := s.hydrateGenerationMedia(userID, &input, providerMediaHydrationPolicy{}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -405,11 +416,8 @@ func (s *Service) processCanvasGenerationTask(ctx context.Context, userID string
|
||||
}
|
||||
}
|
||||
if resumedProviderRequestID(ctx) == "" {
|
||||
requirePublicURL := input.Config.InterfaceType == "newapi-channel-1" || input.Config.InterfaceType == "newapi-channel-2" || input.Config.InterfaceType == string(model.ChannelInterfaceVolcengineArkVideo) || input.Config.InterfaceType == string(model.ChannelInterfaceMiniMaxVideo)
|
||||
if adapter, ok := protocolAdapterForContext(ctx, input.Config.InterfaceType); ok {
|
||||
requirePublicURL = requirePublicURL || adapter.Metadata().RequiresPublicMediaURLs
|
||||
}
|
||||
if err := s.hydrateGenerationMedia(userID, &input, requirePublicURL); err != nil {
|
||||
mediaPolicy := providerMediaHydrationPolicyFor(ctx, input)
|
||||
if err := s.hydrateGenerationMedia(userID, &input, mediaPolicy); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.prepareArkPrivateAssetReferences(ctx, userID, &input); err != nil {
|
||||
@@ -442,6 +450,51 @@ func (s *Service) processCanvasGenerationTask(ctx context.Context, userID string
|
||||
}
|
||||
}
|
||||
|
||||
type providerMediaHydrationPolicy struct {
|
||||
requireURL bool
|
||||
preferURL bool
|
||||
}
|
||||
|
||||
func providerMediaHydrationPolicyFor(ctx context.Context, input canvasGenerationInput) providerMediaHydrationPolicy {
|
||||
policy := providerMediaHydrationPolicy{preferURL: providerPrefersMediaURLs(input.Config.InterfaceType, input)}
|
||||
switch strings.TrimSpace(input.Config.InterfaceType) {
|
||||
case string(model.ChannelInterfaceNewAPIVideo), string(model.ChannelInterfaceNewAPIChannel1), string(model.ChannelInterfaceNewAPIChannel2), string(model.ChannelInterfaceVolcengineArkVideo), string(model.ChannelInterfaceMiniMaxVideo):
|
||||
policy.requireURL = true
|
||||
policy.preferURL = true
|
||||
}
|
||||
if adapter, ok := protocolAdapterForContext(ctx, input.Config.InterfaceType); ok && adapter.Metadata().RequiresPublicMediaURLs {
|
||||
policy.requireURL = true
|
||||
policy.preferURL = true
|
||||
}
|
||||
if input.Mask != nil {
|
||||
policy.requireURL = false
|
||||
policy.preferURL = false
|
||||
}
|
||||
return policy
|
||||
}
|
||||
|
||||
// providerPrefersMediaURLs lists protocols whose media fields accept a remote
|
||||
// URL. Byte-oriented protocols deliberately remain on the existing data path.
|
||||
func providerPrefersMediaURLs(interfaceType string, input canvasGenerationInput) bool {
|
||||
if input.Mask != nil {
|
||||
// OpenAI image edits and similar multipart requests require file bytes.
|
||||
return false
|
||||
}
|
||||
switch strings.TrimSpace(interfaceType) {
|
||||
case string(model.ChannelInterfaceChatCompletion), string(model.ChannelInterfaceOpenAIResponse), string(model.ChannelInterfaceClaudeAPI),
|
||||
string(model.ChannelInterfaceGrokImage), string(model.ChannelInterfaceVolcengineArkImage),
|
||||
string(model.ChannelInterfaceXAIVideo), string(model.ChannelInterfaceNovitaVideo),
|
||||
string(model.ChannelInterfaceMiniMaxVideo), string(model.ChannelInterfaceNewAPIVideo),
|
||||
string(model.ChannelInterfaceNewAPIChannel1), string(model.ChannelInterfaceNewAPIChannel2),
|
||||
string(model.ChannelInterfaceVolcengineArkVideo):
|
||||
return true
|
||||
}
|
||||
if isGrokVideoConfig(input.Config) || isSeedanceVideoConfig(input.Config) || isArkPlanVideoConfig(input.Config) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func runAgentToolTask(ctx context.Context, input canvasGenerationInput) (map[string]interface{}, error) {
|
||||
if adapter, ok := agentProtocolAdapterForContext(ctx, input.Config.InterfaceType); ok {
|
||||
return runDeclarativeAgentTask(ctx, input, adapter)
|
||||
@@ -1317,40 +1370,41 @@ func metadataStringValues(value any) map[string]string {
|
||||
return values
|
||||
}
|
||||
|
||||
func (s *Service) hydrateGenerationMedia(userID string, input *canvasGenerationInput, requirePublicURL bool) error {
|
||||
func (s *Service) hydrateGenerationMedia(userID string, input *canvasGenerationInput, policy providerMediaHydrationPolicy) error {
|
||||
groups := [][]providerMedia{input.ReferenceImages, input.ReferenceVideos, input.ReferenceAudios}
|
||||
for _, group := range groups {
|
||||
for index := range group {
|
||||
if err := s.hydrateProviderMedia(userID, &group[index], requirePublicURL); err != nil {
|
||||
if err := s.hydrateProviderMedia(userID, &group[index], policy); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if input.Mask != nil {
|
||||
return s.hydrateProviderMedia(userID, input.Mask, requirePublicURL)
|
||||
return s.hydrateProviderMedia(userID, input.Mask, policy)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) hydrateProviderMedia(userID string, media *providerMedia, requirePublicURL bool) error {
|
||||
func (s *Service) hydrateProviderMedia(userID string, media *providerMedia, policy providerMediaHydrationPolicy) error {
|
||||
if !strings.HasPrefix(media.StorageKey, "resource:") {
|
||||
if requirePublicURL && strings.HasPrefix(strings.TrimSpace(media.DataURL), "data:") {
|
||||
if policy.requireURL && strings.HasPrefix(strings.TrimSpace(media.DataURL), "data:") {
|
||||
return errors.New("当前 JSON 视频协议的参考素材不能使用内嵌数据,请先上传到对象存储或提供公网素材地址")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
resourceID := strings.TrimPrefix(media.StorageKey, "resource:")
|
||||
if requirePublicURL {
|
||||
resource, err := s.repo.ResourceForUser(userID, resourceID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取任务参考资源失败:%w", err)
|
||||
}
|
||||
if resource.Status != "ready" {
|
||||
return errors.New("任务参考资源尚未上传完成")
|
||||
}
|
||||
resource, err := s.repo.ResourceForUser(userID, resourceID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取任务参考资源失败:%w", err)
|
||||
}
|
||||
if resource.Status != "ready" {
|
||||
return errors.New("任务参考资源尚未上传完成")
|
||||
}
|
||||
useObjectURL := policy.requireURL || (policy.preferURL && resourceUsesObjectStorage(resource))
|
||||
if useObjectURL {
|
||||
signedURL, err := s.directResourceURL(resource, time.Now().Add(providerResourceURLTTL))
|
||||
if err != nil {
|
||||
return fmt.Errorf("生成 JSON 视频协议参考素材地址失败:%w", err)
|
||||
return fmt.Errorf("生成参考素材地址失败:%w", err)
|
||||
}
|
||||
media.URL = signedURL
|
||||
media.DataURL = ""
|
||||
@@ -1369,17 +1423,17 @@ func (s *Service) hydrateProviderMedia(userID string, media *providerMedia, requ
|
||||
return fmt.Errorf("读取任务参考资源失败:%w", err)
|
||||
}
|
||||
defer body.Close()
|
||||
policy, err := s.RuntimePolicy()
|
||||
runtimePolicy, err := s.RuntimePolicy()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resourceLimit := megabytes(policy.Resource.ResourceUploadMB)
|
||||
resourceLimit := megabytes(runtimePolicy.Resource.ResourceUploadMB)
|
||||
data, err := io.ReadAll(io.LimitReader(body, resourceLimit+1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if int64(len(data)) > resourceLimit {
|
||||
return fmt.Errorf("任务参考资源超过 %dMB", policy.Resource.ResourceUploadMB)
|
||||
return fmt.Errorf("任务参考资源超过 %dMB", runtimePolicy.Resource.ResourceUploadMB)
|
||||
}
|
||||
mimeType := normalizedMediaMimeType(firstNonEmpty(media.MimeType, resource.MimeType), data)
|
||||
media.DataURL = dataURL(mimeType, data)
|
||||
@@ -1391,6 +1445,14 @@ func (s *Service) hydrateProviderMedia(userID string, media *providerMedia, requ
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceUsesObjectStorage(resource *model.Resource) bool {
|
||||
if resource == nil {
|
||||
return false
|
||||
}
|
||||
provider := strings.ToLower(strings.TrimSpace(resource.Provider))
|
||||
return provider != "" && provider != "local"
|
||||
}
|
||||
|
||||
func normalizedMediaMimeType(declared string, data []byte) string {
|
||||
declared = strings.TrimSpace(strings.Split(declared, ";")[0])
|
||||
if declared != "" && declared != "application/octet-stream" {
|
||||
@@ -2356,10 +2418,32 @@ func runDeclarativeProtocolTask(ctx context.Context, input canvasGenerationInput
|
||||
return runProtocolAdapterTask(ctx, input, adapter)
|
||||
}
|
||||
|
||||
type protocolPollTiming struct {
|
||||
InitialDelay time.Duration
|
||||
PollInterval time.Duration
|
||||
TaskNotExistWindow time.Duration
|
||||
TaskNotExistMaxMisses int
|
||||
TaskNotExistRetryDelays []time.Duration
|
||||
}
|
||||
|
||||
var defaultProtocolPollTiming = protocolPollTiming{
|
||||
InitialDelay: 2 * time.Second,
|
||||
PollInterval: 2500 * time.Millisecond,
|
||||
TaskNotExistWindow: 15 * time.Second,
|
||||
TaskNotExistMaxMisses: 5,
|
||||
TaskNotExistRetryDelays: []time.Duration{2 * time.Second, 3 * time.Second, 5 * time.Second, 5 * time.Second},
|
||||
}
|
||||
|
||||
func runProtocolAdapterTask(ctx context.Context, input canvasGenerationInput, adapter protocol.Adapter) (map[string]interface{}, error) {
|
||||
return runProtocolAdapterTaskWithTiming(ctx, input, adapter, defaultProtocolPollTiming)
|
||||
}
|
||||
|
||||
func runProtocolAdapterTaskWithTiming(ctx context.Context, input canvasGenerationInput, adapter protocol.Adapter, timing protocolPollTiming) (map[string]interface{}, error) {
|
||||
request := protocolRequestFromInput(input)
|
||||
taskID := resumedProviderRequestID(ctx)
|
||||
var created protocol.CreateResult
|
||||
createdProviderTask := false
|
||||
syncWindowStartedAt := time.Now()
|
||||
if taskID == "" {
|
||||
spec, err := adapter.BuildCreate(ctx, protocol.RequestContext{BaseURL: input.Config.BaseURL, Request: request})
|
||||
if err != nil {
|
||||
@@ -2383,8 +2467,16 @@ func runProtocolAdapterTask(ctx context.Context, input canvasGenerationInput, ad
|
||||
if taskID == "" {
|
||||
return nil, errors.New("声明式协议创建请求没有返回任务 ID")
|
||||
}
|
||||
createdProviderTask = true
|
||||
syncWindowStartedAt = time.Now()
|
||||
}
|
||||
if createdProviderTask && input.Config.InterfaceType == string(model.ChannelInterfaceNewAPIChannel2) {
|
||||
if err := sleepContext(ctx, timing.InitialDelay); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
taskNotExistMisses := 0
|
||||
for deadline := providerPollingDeadline(ctx); time.Now().Before(deadline); {
|
||||
spec, err := adapter.BuildPoll(ctx, protocol.PollContext{BaseURL: input.Config.BaseURL, Model: request.Model, Request: request, TaskID: taskID})
|
||||
if err != nil {
|
||||
@@ -2392,8 +2484,26 @@ func runProtocolAdapterTask(ctx context.Context, input canvasGenerationInput, ad
|
||||
}
|
||||
body, err := executeProtocolRequest(withProviderRequestKind(ctx, "poll"), input.Config, spec)
|
||||
if err != nil {
|
||||
if isNewAPIChannel2TaskNotReady(input.Config.InterfaceType, taskID, err) {
|
||||
taskNotExistMisses++
|
||||
maxMisses := timing.TaskNotExistMaxMisses
|
||||
if maxMisses <= 0 {
|
||||
maxMisses = len(timing.TaskNotExistRetryDelays) + 1
|
||||
}
|
||||
if time.Since(syncWindowStartedAt) >= timing.TaskNotExistWindow || taskNotExistMisses >= maxMisses {
|
||||
return nil, providerStatePendingError{TaskID: taskID, Cause: err}
|
||||
}
|
||||
delayIndex := min(taskNotExistMisses-1, len(timing.TaskNotExistRetryDelays)-1)
|
||||
if delayIndex >= 0 {
|
||||
if sleepErr := sleepContext(ctx, timing.TaskNotExistRetryDelays[delayIndex]); sleepErr != nil {
|
||||
return nil, sleepErr
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
taskNotExistMisses = 0
|
||||
state, err := adapter.ParsePoll(ctx, protocol.PollContext{BaseURL: input.Config.BaseURL, Model: request.Model, Request: request, TaskID: taskID}, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -2407,13 +2517,29 @@ func runProtocolAdapterTask(ctx context.Context, input canvasGenerationInput, ad
|
||||
case protocol.StatusFailed, protocol.StatusCancelled:
|
||||
return nil, protocolResultError(state.Message, taskID)
|
||||
}
|
||||
if err := sleepContext(ctx, 2500*time.Millisecond); err != nil {
|
||||
if err := sleepContext(ctx, timing.PollInterval); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("声明式协议任务超时(任务 %s)", taskID)
|
||||
}
|
||||
|
||||
func isNewAPIChannel2TaskNotReady(interfaceType string, taskID string, err error) bool {
|
||||
if strings.TrimSpace(interfaceType) != string(model.ChannelInterfaceNewAPIChannel2) || strings.TrimSpace(taskID) == "" {
|
||||
return false
|
||||
}
|
||||
var httpErr providerHTTPError
|
||||
if !errors.As(err, &httpErr) || httpErr.StatusCode != http.StatusBadRequest {
|
||||
return false
|
||||
}
|
||||
var payload map[string]any
|
||||
if json.Unmarshal([]byte(httpErr.Body), &payload) != nil {
|
||||
return false
|
||||
}
|
||||
code, message := providerFailureDetails(payload)
|
||||
return strings.EqualFold(strings.TrimSpace(code), "task_not_exist") || strings.EqualFold(strings.TrimSpace(message), "task_not_exist")
|
||||
}
|
||||
|
||||
// queryProtocolAdapterVideoTask performs exactly one read of an existing
|
||||
// declarative provider task. Manual recovery uses this path so it can never
|
||||
// create a second billable generation while checking a failed local task.
|
||||
|
||||
@@ -23,6 +23,34 @@ import (
|
||||
const testReferenceImageDataURL = "data:image/png;base64,aGVsbG8="
|
||||
const testGeminiReferenceImageDataURL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
|
||||
|
||||
func TestProviderMediaHydrationPolicyPrefersObjectURLs(t *testing.T) {
|
||||
prefer := providerMediaHydrationPolicyFor(context.Background(), canvasGenerationInput{Config: providerConfig{InterfaceType: string(model.ChannelInterfaceGrokImage)}})
|
||||
if prefer.requireURL || !prefer.preferURL {
|
||||
t.Fatalf("grok image policy = %#v", prefer)
|
||||
}
|
||||
required := providerMediaHydrationPolicyFor(context.Background(), canvasGenerationInput{Config: providerConfig{InterfaceType: string(model.ChannelInterfaceMiniMaxVideo)}})
|
||||
if !required.requireURL || !required.preferURL {
|
||||
t.Fatalf("minimax policy = %#v", required)
|
||||
}
|
||||
bytesOnly := providerMediaHydrationPolicyFor(context.Background(), canvasGenerationInput{Config: providerConfig{InterfaceType: string(model.ChannelInterfaceGeminiImage)}})
|
||||
if bytesOnly.requireURL || bytesOnly.preferURL {
|
||||
t.Fatalf("gemini image policy = %#v", bytesOnly)
|
||||
}
|
||||
masked := providerMediaHydrationPolicyFor(context.Background(), canvasGenerationInput{Config: providerConfig{InterfaceType: string(model.ChannelInterfaceOpenAIImage)}, Mask: &providerMedia{ID: "mask"}})
|
||||
if masked.requireURL || masked.preferURL {
|
||||
t.Fatalf("openai mask policy = %#v", masked)
|
||||
}
|
||||
if resourceUsesObjectStorage(&model.Resource{Provider: "aliyun"}) != true {
|
||||
t.Fatal("aliyun should use object storage URLs")
|
||||
}
|
||||
if resourceUsesObjectStorage(&model.Resource{Provider: "tencent"}) != true || resourceUsesObjectStorage(&model.Resource{Provider: "qiniu"}) != true || resourceUsesObjectStorage(&model.Resource{Provider: "s3"}) != true {
|
||||
t.Fatal("cos/kodo/s3 should use object storage URLs")
|
||||
}
|
||||
if resourceUsesObjectStorage(&model.Resource{Provider: "local"}) {
|
||||
t.Fatal("local resources should fall back to protocol-compatible bytes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProviderRequestErrorDetails(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -2714,6 +2742,39 @@ func TestRunMiniMaxVideoTaskCreatesPollsAndDownloads(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunMiniMaxVideoTaskSendsOfficial2KResolution(t *testing.T) {
|
||||
t.Setenv("CANVAS_ALLOWED_PRIVATE_UPSTREAM_HOSTS", "127.0.0.1")
|
||||
for _, quality := range []string{"2K", "1440p"} {
|
||||
t.Run(quality, func(t *testing.T) {
|
||||
requested := false
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost || r.URL.Path != "/v2/video_generation" {
|
||||
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
|
||||
}
|
||||
requested = true
|
||||
var body miniMaxVideoRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Errorf("decode request: %v", err)
|
||||
}
|
||||
if body.Resolution != "2K" {
|
||||
t.Errorf("resolution = %q, want official API value 2K", body.Resolution)
|
||||
}
|
||||
// End at submission: this test inspects the wire payload without polling.
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
_, err := runMiniMaxVideoTask(context.Background(), canvasGenerationInput{
|
||||
Mode: "video", Prompt: "make it move",
|
||||
Config: providerConfig{BaseURL: server.URL, APIKey: "test-key", Model: "MiniMax-H3", VQuality: quality},
|
||||
})
|
||||
if !requested || err == nil || !strings.Contains(err.Error(), "没有返回任务 ID") {
|
||||
t.Fatalf("expected submission without task ID, requested=%v err=%v", requested, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunMiniMaxVideoTaskUsesExplicitReferenceRoles(t *testing.T) {
|
||||
t.Setenv("CANVAS_ALLOW_PRIVATE_UPSTREAMS", "true")
|
||||
var server *httptest.Server
|
||||
|
||||
@@ -139,9 +139,15 @@ func (c *boundedReadCache[K, V]) remove(e *readCacheEntry[K, V]) {
|
||||
}
|
||||
|
||||
func (c *boundedReadCache[K, V]) clear() {
|
||||
c.clearAndCount()
|
||||
}
|
||||
|
||||
func (c *boundedReadCache[K, V]) clearAndCount() int {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
count := len(c.entries)
|
||||
c.entries = make(map[K]*readCacheEntry[K, V])
|
||||
c.lru.Init()
|
||||
c.bytes = 0
|
||||
return count
|
||||
}
|
||||
@@ -269,6 +269,17 @@ func validatePublicResourceBaseURL(raw string) (*url.URL, error) {
|
||||
}
|
||||
|
||||
func (s *Service) UploadResource(userID string, header *multipart.FileHeader, kind string, width int, height int, durationMs int64, uploadIdentity ...string) (*model.Resource, error) {
|
||||
return s.uploadResource(userID, header, kind, width, height, durationMs, false, uploadIdentity...)
|
||||
}
|
||||
|
||||
// uploadLocalResource keeps small system-owned assets on the server even when
|
||||
// the uploader or platform has enabled object storage. The resource still uses
|
||||
// the normal database record and persistent data directory lifecycle.
|
||||
func (s *Service) uploadLocalResource(userID string, header *multipart.FileHeader, kind string, width int, height int, durationMs int64, uploadIdentity ...string) (*model.Resource, error) {
|
||||
return s.uploadResource(userID, header, kind, width, height, durationMs, true, uploadIdentity...)
|
||||
}
|
||||
|
||||
func (s *Service) uploadResource(userID string, header *multipart.FileHeader, kind string, width int, height int, durationMs int64, forceLocal bool, uploadIdentity ...string) (*model.Resource, error) {
|
||||
if header == nil {
|
||||
return nil, BadAuthRequest("请选择要上传的文件")
|
||||
}
|
||||
@@ -298,7 +309,7 @@ func (s *Service) UploadResource(userID string, header *multipart.FileHeader, ki
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resource, stored, err := s.storeResource(userID, kind, header.Filename, mimeType, header.Size, width, height, durationMs, file, uploadKey)
|
||||
resource, stored, err := s.storeResource(userID, kind, header.Filename, mimeType, header.Size, width, height, durationMs, file, uploadKey, forceLocal)
|
||||
if err != nil {
|
||||
s.releaseUserUploadQuota(userID, day, header.Size)
|
||||
} else if stored {
|
||||
@@ -334,7 +345,7 @@ func (s *Service) UploadResourceFile(userID string, fileName string, size int64,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resource, stored, err := s.storeResource(userID, kind, fileName, mimeType, size, width, height, durationMs, file, uploadKey)
|
||||
resource, stored, err := s.storeResource(userID, kind, fileName, mimeType, size, width, height, durationMs, file, uploadKey, false)
|
||||
if err != nil {
|
||||
s.releaseUserUploadQuota(userID, day, size)
|
||||
} else if stored {
|
||||
@@ -397,7 +408,7 @@ func (s *Service) ImportResourceURL(userID string, rawURL string, kind string, w
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resource, stored, err := s.storeResource(userID, kind, payload.fileName, payload.mimeType, size, width, height, durationMs, bytes.NewReader(payload.data), uploadKey)
|
||||
resource, stored, err := s.storeResource(userID, kind, payload.fileName, payload.mimeType, size, width, height, durationMs, bytes.NewReader(payload.data), uploadKey, false)
|
||||
if err != nil {
|
||||
s.releaseUserUploadQuota(userID, day, size)
|
||||
} else if stored {
|
||||
@@ -492,7 +503,7 @@ func (s *Service) openResourceRange(userID string, resource *model.Resource, ran
|
||||
return &ResourceStream{Resource: resource, Body: stream.body, StatusCode: stream.statusCode, ContentLength: stream.contentLength, ContentRange: stream.contentRange, AcceptRanges: stream.acceptRanges}, nil
|
||||
}
|
||||
|
||||
func (s *Service) storeResource(userID string, kind string, fileName string, mimeType string, size int64, width int, height int, durationMs int64, body io.Reader, uploadKey *string) (*model.Resource, bool, error) {
|
||||
func (s *Service) storeResource(userID string, kind string, fileName string, mimeType string, size int64, width int, height int, durationMs int64, body io.Reader, uploadKey *string, forceLocal bool) (*model.Resource, bool, error) {
|
||||
if existing, err := s.resourceForUploadKey(userID, uploadKey); err != nil {
|
||||
return nil, false, err
|
||||
} else if existing != nil {
|
||||
@@ -503,9 +514,15 @@ func (s *Service) storeResource(userID string, kind string, fileName string, mim
|
||||
}
|
||||
now := time.Now()
|
||||
kind = normalizeResourceKind(kind, mimeType)
|
||||
setting, storageSettingID, useOSS, err := s.activeResourceOSSSetting(userID)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
var setting ossSettingValue
|
||||
var storageSettingID string
|
||||
var useOSS bool
|
||||
var err error
|
||||
if !forceLocal {
|
||||
setting, storageSettingID, useOSS, err = s.activeResourceOSSSetting(userID)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
}
|
||||
provider := "local"
|
||||
objectKey := localObjectKey(userID, kind, fileName, mimeType, now)
|
||||
@@ -752,7 +769,7 @@ func (s *Service) persistGeneratedMediaValueMode(userID string, value interface{
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
resource, _, err := s.storeResource(userID, kind, "generated."+extensionFromMimeType(mimeType), mimeType, int64(len(data)), width, height, int64(intValue(item["durationMs"])), bytes.NewReader(data), nil)
|
||||
resource, _, err := s.storeResource(userID, kind, "generated."+extensionFromMimeType(mimeType), mimeType, int64(len(data)), width, height, int64(intValue(item["durationMs"])), bytes.NewReader(data), nil, false)
|
||||
if err != nil {
|
||||
if enforceQuota {
|
||||
s.releaseUserUploadQuota(userID, quotaDay, int64(len(data)))
|
||||
|
||||
@@ -40,7 +40,7 @@ func TestStoreResourceDegradesToLocalWhenOSSUnavailable(t *testing.T) {
|
||||
|
||||
resource, created, err := service.storeResource(
|
||||
"user-1", "video", "intro.mp4", "video/mp4", 1024,
|
||||
1920, 1080, 0, bytes.NewReader([]byte("fake-mp4-bytes")), nil,
|
||||
1920, 1080, 0, bytes.NewReader([]byte("fake-mp4-bytes")), nil, false,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("storeResource: %v", err)
|
||||
@@ -74,7 +74,7 @@ func TestStoreResourceLocalPathUnaffectedByOSS(t *testing.T) {
|
||||
|
||||
resource, created, err := service.storeResource(
|
||||
"user-2", "video", "local.mp4", "video/mp4", 512,
|
||||
1280, 720, 0, bytes.NewReader([]byte("local-bytes")), nil,
|
||||
1280, 720, 0, bytes.NewReader([]byte("local-bytes")), nil, false,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("storeResource: %v", err)
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"net/url"
|
||||
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -735,13 +736,13 @@ func TestHydrateNewAPIChannel1ResourceUsesSignedOSSURL(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
media := providerMedia{StorageKey: "resource:resource-1", DataURL: "data:image/png;base64,old"}
|
||||
if err := svc.hydrateProviderMedia("user-1", &media, true); err != nil {
|
||||
if err := svc.hydrateProviderMedia("user-1", &media, providerMediaHydrationPolicy{requireURL: true}); err != nil {
|
||||
t.Fatalf("hydrateProviderMedia() error = %v", err)
|
||||
}
|
||||
if !strings.HasPrefix(media.URL, "https://private-bucket.oss-cn-test.aliyuncs.com/") || media.DataURL != "" || !strings.Contains(media.URL, "Signature=") {
|
||||
t.Fatalf("media = %#v", media)
|
||||
}
|
||||
if err := svc.hydrateProviderMedia("other-user", &providerMedia{StorageKey: "resource:resource-1"}, true); err == nil {
|
||||
if err := svc.hydrateProviderMedia("other-user", &providerMedia{StorageKey: "resource:resource-1"}, providerMediaHydrationPolicy{requireURL: true}); err == nil {
|
||||
t.Fatal("hydrateProviderMedia() allowed another user's resource")
|
||||
}
|
||||
}
|
||||
@@ -760,7 +761,7 @@ func TestHydrateNewAPIChannel1ResourceUsesSignedLocalURL(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
media := providerMedia{StorageKey: "resource:resource-local"}
|
||||
if err := svc.hydrateProviderMedia("user-1", &media, true); err != nil {
|
||||
if err := svc.hydrateProviderMedia("user-1", &media, providerMediaHydrationPolicy{requireURL: true}); err != nil {
|
||||
t.Fatalf("hydrateProviderMedia() error = %v", err)
|
||||
}
|
||||
if !strings.HasPrefix(media.URL, server.URL+"/api/public/resources/resource-local/file/resource-local.png?") || !strings.Contains(media.URL, "signature=") || media.DataURL != "" {
|
||||
@@ -772,6 +773,51 @@ func TestHydrateNewAPIChannel1ResourceUsesSignedLocalURL(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHydratePreferredURLUsesObjectStorageAndFallsBackLocal(t *testing.T) {
|
||||
svc := newResourceTestService(t)
|
||||
settingJSON, _ := json.Marshal(ossSettingValue{
|
||||
Enabled: true, Provider: "aliyun", Endpoint: "https://oss-cn-test.aliyuncs.com", Bucket: "private-bucket",
|
||||
AccessKeyID: "access-id", AccessKeySecret: "secret-value",
|
||||
})
|
||||
if err := svc.repo.SaveSystemSetting(&model.SystemSetting{Key: ossSettingKey, ValueJSON: string(settingJSON)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
objectResource := model.Resource{
|
||||
ID: "resource-oss", UserID: "user-1", Kind: "image", Status: model.ResourceStatusReady,
|
||||
Provider: "aliyun", Endpoint: "https://oss-cn-test.aliyuncs.com", Bucket: "private-bucket",
|
||||
ObjectKey: "users/user-1/image/prefer.png", MimeType: "image/png",
|
||||
}
|
||||
if err := svc.repo.CreateResource(&objectResource); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
media := providerMedia{StorageKey: "resource:resource-oss", DataURL: "data:image/png;base64,old"}
|
||||
if err := svc.hydrateProviderMedia("user-1", &media, providerMediaHydrationPolicy{preferURL: true}); err != nil {
|
||||
t.Fatalf("hydrateProviderMedia(prefer object) error = %v", err)
|
||||
}
|
||||
if !strings.HasPrefix(media.URL, "https://private-bucket.oss-cn-test.aliyuncs.com/") || media.DataURL != "" || !strings.Contains(media.URL, "Signature=") {
|
||||
t.Fatalf("object media = %#v", media)
|
||||
}
|
||||
|
||||
localDir := filepath.Join(svc.dataDir, "resources", "users", "user-1", "image")
|
||||
if err := os.MkdirAll(localDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(localDir, "local.png"), []byte("png-bytes"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
localResource := model.Resource{ID: "resource-local-bytes", UserID: "user-1", Kind: "image", Status: model.ResourceStatusReady, Provider: "local", ObjectKey: "users/user-1/image/local.png", MimeType: "image/png"}
|
||||
if err := svc.repo.CreateResource(&localResource); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
localMedia := providerMedia{StorageKey: "resource:resource-local-bytes"}
|
||||
if err := svc.hydrateProviderMedia("user-1", &localMedia, providerMediaHydrationPolicy{preferURL: true}); err != nil {
|
||||
t.Fatalf("hydrateProviderMedia(prefer local) error = %v", err)
|
||||
}
|
||||
if localMedia.URL != "" || !strings.HasPrefix(localMedia.DataURL, "data:image/png;base64,") {
|
||||
t.Fatalf("local media = %#v", localMedia)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicResourceSignatureRejectsExpiredAndAlteredLinks(t *testing.T) {
|
||||
svc := newResourceTestService(t)
|
||||
expires := strconv.FormatInt(time.Now().Add(time.Minute).Unix(), 10)
|
||||
@@ -879,14 +925,14 @@ func newResourceTestService(t *testing.T) *Service {
|
||||
func TestStoreResourceReusesReadyUploadIdentity(t *testing.T) {
|
||||
svc := newResourceTestService(t)
|
||||
uploadKey := normalizedResourceUploadKey([]string{"image:user-1:logical-upload"})
|
||||
first, stored, err := svc.storeResource("user-1", "image", "first.png", "image/png", 7, 1, 1, 0, bytes.NewReader([]byte("payload")), uploadKey)
|
||||
first, stored, err := svc.storeResource("user-1", "image", "first.png", "image/png", 7, 1, 1, 0, bytes.NewReader([]byte("payload")), uploadKey, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !stored {
|
||||
t.Fatal("first upload was not stored")
|
||||
}
|
||||
second, stored, err := svc.storeResource("user-1", "image", "second.png", "image/png", 7, 1, 1, 0, bytes.NewReader([]byte("payload")), uploadKey)
|
||||
second, stored, err := svc.storeResource("user-1", "image", "second.png", "image/png", 7, 1, 1, 0, bytes.NewReader([]byte("payload")), uploadKey, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -31,16 +31,38 @@ func TestOnlyResumableNewAPIChannel2VideoDeadlinesStayRunning(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
base := model.Task{ID: "task-1", Type: "canvas_video", ProviderRequestID: "provider-task-1"}
|
||||
startedAt := time.Now()
|
||||
base := model.Task{ID: "task-1", Type: "canvas_video", ProviderRequestID: "provider-task-1", StartedAt: &startedAt}
|
||||
if !svc.shouldDeferVideoProviderTask(base, string(input), context.DeadlineExceeded) {
|
||||
t.Fatal("resumable NewAPI Channel 2 deadline should remain running")
|
||||
}
|
||||
pendingErr := providerStatePendingError{TaskID: base.ProviderRequestID, Cause: providerHTTPError{StatusCode: 400, Body: `{"code":"task_not_exist"}`}}
|
||||
if !svc.shouldDeferVideoProviderTask(base, string(input), pendingErr) {
|
||||
t.Fatal("resumable NewAPI Channel 2 provider sync delay should remain running")
|
||||
}
|
||||
expiredStartedAt := startedAt.Add(-newAPIChannel2TaskSyncMaxAge - time.Second)
|
||||
expiredTask := model.Task{ID: base.ID, Type: base.Type, ProviderRequestID: base.ProviderRequestID, StartedAt: &expiredStartedAt}
|
||||
if svc.shouldDeferVideoProviderTask(expiredTask, string(input), pendingErr) {
|
||||
t.Fatal("provider sync delay must stop after the bounded synchronization window")
|
||||
}
|
||||
if !svc.shouldDeferVideoProviderTask(expiredTask, string(input), context.DeadlineExceeded) {
|
||||
t.Fatal("a normal long-running provider task must remain resumable after the synchronization window")
|
||||
}
|
||||
if svc.shouldDeferVideoProviderTask(model.Task{ID: base.ID, Type: base.Type, ProviderRequestID: base.ProviderRequestID}, string(input), pendingErr) {
|
||||
t.Fatal("provider sync delay without a persisted start time must fail closed")
|
||||
}
|
||||
if svc.shouldDeferVideoProviderTask(model.Task{ID: "task-2", Type: "canvas_video"}, string(input), context.DeadlineExceeded) {
|
||||
t.Fatal("missing provider task id must not be deferred")
|
||||
}
|
||||
if svc.shouldDeferVideoProviderTask(model.Task{ID: "task-2", Type: "canvas_video"}, string(input), pendingErr) {
|
||||
t.Fatal("provider sync delay without persisted provider task id must not be deferred")
|
||||
}
|
||||
if svc.shouldDeferVideoProviderTask(base, string(input), context.Canceled) {
|
||||
t.Fatal("explicit cancellation must not be deferred")
|
||||
}
|
||||
if svc.shouldDeferVideoProviderTask(base, string(input), providerHTTPError{StatusCode: 400, Body: `{"code":"task_not_exist"}`}) {
|
||||
t.Fatal("untyped provider error must not be deferred")
|
||||
}
|
||||
other, err := json.Marshal(canvasGenerationInput{Mode: "video", Config: providerConfig{BaseURL: "https://example.com", InterfaceType: string(model.ChannelInterfaceNewAPIVideo)}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
//go:build darwin
|
||||
|
||||
package service
|
||||
|
||||
import "golang.org/x/sys/unix"
|
||||
|
||||
func platformMemory() (uint64, uint64, bool) {
|
||||
total, err := unix.SysctlUint64("hw.memsize")
|
||||
if err != nil || total == 0 {
|
||||
return 0, 0, false
|
||||
}
|
||||
// hw.memsize 只能稳定提供物理内存总量;没有可靠可用量时不要把 available=0
|
||||
// 误算成 100% 占用,页面会明确降级展示 Go 进程堆内存。
|
||||
return total, 0, false
|
||||
}
|
||||
|
||||
func platformLoadAverage() ([3]float64, bool) {
|
||||
return [3]float64{}, false
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
//go:build linux
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func platformMemory() (uint64, uint64, bool) {
|
||||
data, err := os.ReadFile("/proc/meminfo")
|
||||
if err != nil {
|
||||
return 0, 0, false
|
||||
}
|
||||
values := map[string]uint64{}
|
||||
for _, line := range strings.Split(string(data), "\n") {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) < 2 {
|
||||
continue
|
||||
}
|
||||
value, _ := strconv.ParseUint(fields[1], 10, 64)
|
||||
values[strings.TrimSuffix(fields[0], ":")] = value * 1024
|
||||
}
|
||||
total, available := values["MemTotal"], values["MemAvailable"]
|
||||
return total, available, total > 0
|
||||
}
|
||||
|
||||
func platformLoadAverage() ([3]float64, bool) {
|
||||
var result [3]float64
|
||||
data, err := os.ReadFile("/proc/loadavg")
|
||||
if err != nil {
|
||||
return result, false
|
||||
}
|
||||
fields := strings.Fields(string(data))
|
||||
if len(fields) < 3 {
|
||||
return result, false
|
||||
}
|
||||
for index := range 3 {
|
||||
result[index], _ = strconv.ParseFloat(fields[index], 64)
|
||||
}
|
||||
return result, true
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
//go:build !linux && !darwin
|
||||
|
||||
package service
|
||||
|
||||
func platformMemory() (uint64, uint64, bool) { return 0, 0, false }
|
||||
func platformLoadAverage() ([3]float64, bool) { return [3]float64{}, false }
|
||||
func collectSystemDisk(string) SystemPerformanceDisk { return SystemPerformanceDisk{} }
|
||||
@@ -0,0 +1,36 @@
|
||||
//go:build linux || darwin
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func collectSystemDisk(path string) SystemPerformanceDisk {
|
||||
if path == "" {
|
||||
path = "."
|
||||
}
|
||||
path, _ = filepath.Abs(path)
|
||||
var stats syscall.Statfs_t
|
||||
if err := syscall.Statfs(path, &stats); err != nil {
|
||||
return SystemPerformanceDisk{}
|
||||
}
|
||||
blockSize := uint64(stats.Bsize)
|
||||
total := stats.Blocks * blockSize
|
||||
free := stats.Bavail * blockSize
|
||||
used := total - stats.Bfree*blockSize
|
||||
usage := float64(0)
|
||||
if total > 0 {
|
||||
usage = float64(used) / float64(total) * 100
|
||||
}
|
||||
writable := false
|
||||
if file, err := os.CreateTemp(path, ".system-performance-write-check-"); err == nil {
|
||||
writable = true
|
||||
name := file.Name()
|
||||
_ = file.Close()
|
||||
_ = os.Remove(name)
|
||||
}
|
||||
return SystemPerformanceDisk{Available: true, Writable: writable, TotalBytes: total, UsedBytes: used, FreeBytes: free, UsagePercent: usage}
|
||||
}
|
||||
@@ -88,7 +88,7 @@ func (w *taskWorkerCoordinator) processTimelineRender(task *model.Task, ctx cont
|
||||
}
|
||||
durationMs := planDurationMs(plan)
|
||||
fileName := fmt.Sprintf("timeline-render-%s.mp4", time.Now().Format("20060102-150405"))
|
||||
resource, _, err := s.storeResource(task.UserID, "media", fileName, "video/mp4", stat.Size(), renderWidth, renderHeight, durationMs, file, nil)
|
||||
resource, _, err := s.storeResource(task.UserID, "media", fileName, "video/mp4", stat.Size(), renderWidth, renderHeight, durationMs, file, nil, false)
|
||||
if err != nil || resource == nil {
|
||||
return w.failTimelineTask(task, "渲染失败", "保存渲染产物失败")
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ import (
|
||||
"infinite-canvas/backend/internal/model"
|
||||
)
|
||||
|
||||
const newAPIChannel2TaskSyncMaxAge = 5 * time.Minute
|
||||
|
||||
// taskWorkerCoordinator 收敛任务领取、租约维护和执行结果落库,避免 Service 同时承担 worker 生命周期与业务命令。
|
||||
type taskWorkerCoordinator struct {
|
||||
service *Service
|
||||
@@ -211,15 +213,25 @@ func (w *taskWorkerCoordinator) processClaimedTask(task *model.Task, globalSlot
|
||||
return leaseErr
|
||||
default:
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
decryptedInput, decryptErr := s.decryptTaskInputJSON(task.InputJSON)
|
||||
if decryptErr == nil && s.shouldDeferVideoProviderTask(*task, decryptedInput, err) {
|
||||
if deferErr := s.repo.DeferRunningTaskForProviderPoll(task.ID, task.LeaseOwner, "后台仍在生成", 15*time.Second); deferErr != nil {
|
||||
return deferErr
|
||||
}
|
||||
_ = s.log(task.UserID, task.ID, "info", "前台等待结束,上游视频仍在生成,将继续回查原任务", task.PollStage)
|
||||
return nil
|
||||
decryptedInput, decryptErr := s.decryptTaskInputJSON(task.InputJSON)
|
||||
if decryptErr == nil && s.shouldDeferVideoProviderTask(*task, decryptedInput, err) {
|
||||
stage := "后台仍在生成"
|
||||
message := "前台等待结束,上游视频仍在生成,将继续回查原任务"
|
||||
var pendingErr providerStatePendingError
|
||||
if errors.As(err, &pendingErr) {
|
||||
stage = "等待上游任务同步"
|
||||
message = "上游任务状态暂未同步,将继续回查原任务"
|
||||
}
|
||||
if deferErr := s.repo.DeferRunningTaskForProviderPoll(task.ID, task.LeaseOwner, stage, 15*time.Second); deferErr != nil {
|
||||
return deferErr
|
||||
}
|
||||
_ = s.log(task.UserID, task.ID, "info", message, task.PollStage)
|
||||
return nil
|
||||
}
|
||||
if newAPIChannel2TaskSyncExpired(*task, err, time.Now()) {
|
||||
err = errors.New("上游任务长时间未同步,已停止自动查询,请确认渠道任务状态后重试。")
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
err = errors.New(taskTimeoutMessage(task.Type))
|
||||
}
|
||||
return terminal.handleExecutionFailure(task, err, providerSucceeded, channelSlotFailedBeforeRequest)
|
||||
@@ -283,7 +295,16 @@ func taskExecutionTimeoutWithPolicy(taskType string, policy RuntimeTaskPolicy) t
|
||||
}
|
||||
|
||||
func (s *Service) shouldDeferVideoProviderTask(task model.Task, decryptedInput string, err error) bool {
|
||||
if !errors.Is(err, context.DeadlineExceeded) || strings.TrimSpace(task.ProviderRequestID) == "" || (!strings.HasPrefix(task.Type, "canvas_video") && !strings.HasPrefix(task.Type, "video_")) {
|
||||
providerRequestID := strings.TrimSpace(task.ProviderRequestID)
|
||||
if providerRequestID == "" || (!strings.HasPrefix(task.Type, "canvas_video") && !strings.HasPrefix(task.Type, "video_")) {
|
||||
return false
|
||||
}
|
||||
deferSignal := errors.Is(err, context.DeadlineExceeded)
|
||||
var pendingErr providerStatePendingError
|
||||
if errors.As(err, &pendingErr) {
|
||||
deferSignal = strings.TrimSpace(pendingErr.TaskID) == providerRequestID && !newAPIChannel2TaskSyncExpired(task, err, time.Now())
|
||||
}
|
||||
if !deferSignal {
|
||||
return false
|
||||
}
|
||||
var input canvasGenerationInput
|
||||
@@ -294,6 +315,17 @@ func (s *Service) shouldDeferVideoProviderTask(task model.Task, decryptedInput s
|
||||
return resolveErr == nil && resolved.InterfaceType == string(model.ChannelInterfaceNewAPIChannel2)
|
||||
}
|
||||
|
||||
func newAPIChannel2TaskSyncExpired(task model.Task, err error, now time.Time) bool {
|
||||
var pendingErr providerStatePendingError
|
||||
if !errors.As(err, &pendingErr) || strings.TrimSpace(pendingErr.TaskID) == "" || strings.TrimSpace(pendingErr.TaskID) != strings.TrimSpace(task.ProviderRequestID) {
|
||||
return false
|
||||
}
|
||||
if task.StartedAt == nil {
|
||||
return true
|
||||
}
|
||||
return !now.Before(task.StartedAt.Add(newAPIChannel2TaskSyncMaxAge))
|
||||
}
|
||||
|
||||
func taskTimeoutMessage(taskType string) string {
|
||||
if strings.HasPrefix(taskType, "canvas_video") || strings.HasPrefix(taskType, "video_") {
|
||||
return "视频生成等待超时,请稍后到任务中心查看或重试。"
|
||||
|
||||
Reference in new issue
Block a user