feat(*): 分镜创作体系 - 重构画风、提示词模板与视频生成链路
This commit is contained in:
1 parent
98382ba976
commit
d4018123e1
46 files changed
+2666
-571
No files matched your search
@@ -238,7 +238,8 @@ func migrations() []tableMigration {
|
||||
migrateTable[model.Asset]("assets"),
|
||||
migrateTable[model.CanvasProject]("canvas_projects"),
|
||||
migrateTable[model.CanvasShare]("canvas_shares"),
|
||||
migrateTable[model.StoryboardPromptTemplate]("storyboard_prompt_templates"),
|
||||
migrateTable[model.PromptTemplate]("prompt_templates"),
|
||||
migrateTable[model.UserPromptCustomization]("user_prompt_customizations"),
|
||||
migrateTable[model.Announcement]("announcements"),
|
||||
migrateTable[model.UserAnnouncementRead]("user_announcement_reads"),
|
||||
migrateTable[model.Task]("tasks"),
|
||||
|
||||
@@ -45,7 +45,7 @@ func main() {
|
||||
if err := svc.EnsureSystemChannelModels(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := svc.EnsureDefaultStoryboardPromptTemplate(); err != nil {
|
||||
if err := svc.EnsureDefaultPromptTemplates(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := svc.EnsureBuiltinProjectWorkflowTemplate(); err != nil {
|
||||
|
||||
@@ -49,7 +49,8 @@ func Models() []any {
|
||||
&model.WorkflowStepTask{},
|
||||
&model.CanvasProject{},
|
||||
&model.CanvasShare{},
|
||||
&model.StoryboardPromptTemplate{},
|
||||
&model.PromptTemplate{},
|
||||
&model.UserPromptCustomization{},
|
||||
&model.Announcement{},
|
||||
&model.UserAnnouncementRead{},
|
||||
&model.Task{},
|
||||
|
||||
@@ -381,62 +381,64 @@ func RegisterAdminRoutes(r *gin.RouterGroup, svc *service.Service) {
|
||||
}
|
||||
ok(c, gin.H{"ok": true})
|
||||
})
|
||||
r.GET("/admin/storyboard-prompts", func(c *gin.Context) {
|
||||
r.GET("/admin/prompt-templates", func(c *gin.Context) {
|
||||
user, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
templates, variables, err := svc.AdminStoryboardPromptTemplates(user)
|
||||
templates, definitions, err := svc.AdminPromptTemplates(user)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
ok(c, gin.H{"templates": templates, "variables": variables})
|
||||
ok(c, gin.H{"templates": templates, "definitions": definitions})
|
||||
})
|
||||
r.POST("/admin/storyboard-prompts", func(c *gin.Context) {
|
||||
r.POST("/admin/prompt-templates", func(c *gin.Context) {
|
||||
user, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
var req service.StoryboardPromptTemplateRequest
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 64<<10)
|
||||
var req service.PromptTemplateRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
fail(c, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
template, err := svc.CreateStoryboardPromptTemplate(user, req)
|
||||
template, err := svc.CreatePromptTemplate(user, req)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
ok(c, gin.H{"template": template})
|
||||
})
|
||||
r.PATCH("/admin/storyboard-prompts/:id", func(c *gin.Context) {
|
||||
r.PATCH("/admin/prompt-templates/:id", func(c *gin.Context) {
|
||||
user, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
var req service.StoryboardPromptTemplateRequest
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 64<<10)
|
||||
var req service.PromptTemplateRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
fail(c, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
template, err := svc.UpdateStoryboardPromptTemplate(user, c.Param("id"), req)
|
||||
template, err := svc.UpdatePromptTemplate(user, c.Param("id"), req)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
ok(c, gin.H{"template": template})
|
||||
})
|
||||
r.DELETE("/admin/storyboard-prompts/:id", func(c *gin.Context) {
|
||||
r.DELETE("/admin/prompt-templates/:id", func(c *gin.Context) {
|
||||
user, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
if err := svc.DeleteStoryboardPromptTemplate(user, c.Param("id")); err != nil {
|
||||
if err := svc.DeletePromptTemplate(user, c.Param("id")); err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -16,6 +16,50 @@ import (
|
||||
)
|
||||
|
||||
func RegisterUserDataRoutes(r *gin.RouterGroup, svc *service.Service) {
|
||||
r.GET("/settings/prompt-templates", func(c *gin.Context) {
|
||||
user, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
preferences, err := svc.UserPromptPreferences(user)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
ok(c, gin.H{"preferences": preferences})
|
||||
})
|
||||
r.PATCH("/settings/prompt-templates/:operation", 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, 64<<10)
|
||||
var req service.UserPromptCustomizationRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
fail(c, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
customization, err := svc.UpdateUserPromptCustomization(user, c.Param("operation"), req)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
ok(c, gin.H{"customization": customization})
|
||||
})
|
||||
r.DELETE("/settings/prompt-templates/:operation", func(c *gin.Context) {
|
||||
user, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
if err := svc.ResetUserPromptCustomization(user, c.Param("operation")); err != nil {
|
||||
failService(c, err)
|
||||
return
|
||||
}
|
||||
ok(c, gin.H{"ok": true})
|
||||
})
|
||||
r.GET("/settings/oss", func(c *gin.Context) {
|
||||
user, err := currentUser(c, svc)
|
||||
if err != nil {
|
||||
|
||||
@@ -688,14 +688,29 @@ type CanvasShare struct {
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
|
||||
type StoryboardPromptTemplate struct {
|
||||
ID string `json:"id" gorm:"primaryKey;size:36"`
|
||||
Name string `json:"name" gorm:"size:120"`
|
||||
Content string `json:"content" gorm:"type:text"`
|
||||
Enabled bool `json:"enabled" gorm:"index"`
|
||||
CreatedBy string `json:"createdBy" gorm:"index;size:36"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
type PromptTemplate struct {
|
||||
ID string `json:"id" gorm:"primaryKey;size:36"`
|
||||
Operation string `json:"operation" gorm:"size:64;index;uniqueIndex:idx_prompt_template_operation_version,priority:1"`
|
||||
Name string `json:"name" gorm:"size:120"`
|
||||
Version int `json:"version" gorm:"uniqueIndex:idx_prompt_template_operation_version,priority:2"`
|
||||
Content string `json:"content" gorm:"type:text"`
|
||||
OutputType string `json:"outputType" gorm:"size:24"`
|
||||
Enabled bool `json:"enabled" gorm:"index;index:idx_prompt_template_active,priority:2"`
|
||||
CreatedBy string `json:"createdBy" gorm:"index;size:36"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
|
||||
// UserPromptCustomization 只保存用户的创作策略层,动态上下文和输出契约始终由服务端编译器注入。
|
||||
type UserPromptCustomization struct {
|
||||
ID string `json:"id" gorm:"primaryKey;size:36"`
|
||||
UserID string `json:"userId" gorm:"size:36;index;uniqueIndex:idx_user_prompt_operation,priority:1"`
|
||||
Operation string `json:"operation" gorm:"size:64;index;uniqueIndex:idx_user_prompt_operation,priority:2"`
|
||||
Mode string `json:"mode" gorm:"size:24"`
|
||||
Content string `json:"content" gorm:"type:text"`
|
||||
BaseTemplateID string `json:"baseTemplateId" gorm:"size:36;index"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
|
||||
type Announcement struct {
|
||||
|
||||
@@ -1436,38 +1436,44 @@ func (r *Repository) ReplaceCanvasProjects(userID string, projects []model.Canva
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Repository) StoryboardPromptTemplates() ([]model.StoryboardPromptTemplate, error) {
|
||||
var templates []model.StoryboardPromptTemplate
|
||||
err := r.db.Order("updated_at desc").Find(&templates).Error
|
||||
func (r *Repository) PromptTemplates() ([]model.PromptTemplate, error) {
|
||||
var templates []model.PromptTemplate
|
||||
err := r.db.Order("operation asc, version desc").Find(&templates).Error
|
||||
return templates, err
|
||||
}
|
||||
|
||||
func (r *Repository) StoryboardPromptTemplate(id string) (*model.StoryboardPromptTemplate, error) {
|
||||
var template model.StoryboardPromptTemplate
|
||||
func (r *Repository) PromptTemplate(id string) (*model.PromptTemplate, error) {
|
||||
var template model.PromptTemplate
|
||||
if err := r.db.First(&template, "id = ?", id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &template, nil
|
||||
}
|
||||
|
||||
func (r *Repository) ActiveStoryboardPromptTemplate() (*model.StoryboardPromptTemplate, error) {
|
||||
var template model.StoryboardPromptTemplate
|
||||
if err := r.db.Order("updated_at desc").First(&template, "enabled = ?", true).Error; err != nil {
|
||||
func (r *Repository) ActivePromptTemplate(operation string) (*model.PromptTemplate, error) {
|
||||
var template model.PromptTemplate
|
||||
if err := r.db.Order("version desc").First(&template, "operation = ? AND enabled = ?", operation, true).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &template, nil
|
||||
}
|
||||
|
||||
func (r *Repository) StoryboardPromptTemplateCount() (int64, error) {
|
||||
func (r *Repository) PromptTemplateCount(operation string) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.Model(&model.StoryboardPromptTemplate{}).Count(&count).Error
|
||||
err := r.db.Model(&model.PromptTemplate{}).Where("operation = ?", operation).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
func (r *Repository) SaveStoryboardPromptTemplate(template *model.StoryboardPromptTemplate) error {
|
||||
func (r *Repository) NextPromptTemplateVersion(operation string) (int, error) {
|
||||
var version int
|
||||
err := r.db.Model(&model.PromptTemplate{}).Where("operation = ?", operation).Select("COALESCE(MAX(version), 0)").Scan(&version).Error
|
||||
return version + 1, err
|
||||
}
|
||||
|
||||
func (r *Repository) SavePromptTemplate(template *model.PromptTemplate) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
if template.Enabled {
|
||||
if err := tx.Model(&model.StoryboardPromptTemplate{}).Where("id <> ?", template.ID).Update("enabled", false).Error; err != nil {
|
||||
if err := tx.Model(&model.PromptTemplate{}).Where("operation = ? AND id <> ?", template.Operation, template.ID).Update("enabled", false).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -1475,6 +1481,28 @@ func (r *Repository) SaveStoryboardPromptTemplate(template *model.StoryboardProm
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Repository) DeleteStoryboardPromptTemplate(id string) error {
|
||||
return r.db.Delete(&model.StoryboardPromptTemplate{}, "id = ?", id).Error
|
||||
func (r *Repository) DeletePromptTemplate(id string) error {
|
||||
return r.db.Delete(&model.PromptTemplate{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
func (r *Repository) UserPromptCustomizations(userID string) ([]model.UserPromptCustomization, error) {
|
||||
var customizations []model.UserPromptCustomization
|
||||
err := r.db.Where("user_id = ?", userID).Order("operation asc").Find(&customizations).Error
|
||||
return customizations, err
|
||||
}
|
||||
|
||||
func (r *Repository) UserPromptCustomization(userID string, operation string) (*model.UserPromptCustomization, error) {
|
||||
var customization model.UserPromptCustomization
|
||||
if err := r.db.First(&customization, "user_id = ? AND operation = ?", userID, operation).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &customization, nil
|
||||
}
|
||||
|
||||
func (r *Repository) SaveUserPromptCustomization(customization *model.UserPromptCustomization) error {
|
||||
return r.db.Save(customization).Error
|
||||
}
|
||||
|
||||
func (r *Repository) DeleteUserPromptCustomization(userID string, operation string) error {
|
||||
return r.db.Delete(&model.UserPromptCustomization{}, "user_id = ? AND operation = ?", userID, operation).Error
|
||||
}
|
||||
@@ -0,0 +1,405 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
promptCustomizationInherit = "inherit"
|
||||
promptCustomizationAppend = "append"
|
||||
promptCustomizationRewrite = "rewrite"
|
||||
)
|
||||
|
||||
var promptPlaceholderPattern = regexp.MustCompile(`\{\{[^{}]+\}\}`)
|
||||
|
||||
type PromptTemplateVariable struct {
|
||||
Label string `json:"label"`
|
||||
Placeholder string `json:"placeholder"`
|
||||
}
|
||||
|
||||
type PromptOperationDefinition struct {
|
||||
Operation string `json:"operation"`
|
||||
Label string `json:"label"`
|
||||
Category string `json:"category"`
|
||||
Description string `json:"description"`
|
||||
OutputType string `json:"outputType"`
|
||||
SchemaKey string `json:"schemaKey,omitempty"`
|
||||
Variables []PromptTemplateVariable `json:"variables"`
|
||||
OutputContract string `json:"outputContract"`
|
||||
DefaultContent string `json:"-"`
|
||||
}
|
||||
|
||||
type PromptTemplateRequest struct {
|
||||
Operation string `json:"operation"`
|
||||
Name string `json:"name"`
|
||||
Content string `json:"content"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
}
|
||||
|
||||
type UserPromptCustomizationRequest struct {
|
||||
Mode string `json:"mode"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type UserPromptPreference struct {
|
||||
Definition PromptOperationDefinition `json:"definition"`
|
||||
Template *model.PromptTemplate `json:"template"`
|
||||
Customization *model.UserPromptCustomization `json:"customization,omitempty"`
|
||||
Outdated bool `json:"outdated"`
|
||||
}
|
||||
|
||||
type CompiledPrompt struct {
|
||||
Content string
|
||||
TemplateID string
|
||||
TemplateVersion int
|
||||
CustomizationID string
|
||||
CustomizationUpdated string
|
||||
}
|
||||
|
||||
func promptDefinitions() []PromptOperationDefinition {
|
||||
definitions := defaultPromptDefinitions()
|
||||
for index := range definitions {
|
||||
definitions[index].OutputContract = promptOutputContract(definitions[index].Operation)
|
||||
}
|
||||
return definitions
|
||||
}
|
||||
|
||||
func promptDefinition(operation string) (PromptOperationDefinition, bool) {
|
||||
for _, definition := range promptDefinitions() {
|
||||
if definition.Operation == operation {
|
||||
return definition, true
|
||||
}
|
||||
}
|
||||
return PromptOperationDefinition{}, false
|
||||
}
|
||||
|
||||
func (s *Service) EnsureDefaultPromptTemplates() error {
|
||||
for _, definition := range promptDefinitions() {
|
||||
count, err := s.repo.PromptTemplateCount(definition.Operation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
continue
|
||||
}
|
||||
if err := s.repo.SavePromptTemplate(&model.PromptTemplate{
|
||||
ID: newID(), Operation: definition.Operation, Name: "默认" + definition.Label + "模板", Version: 1,
|
||||
Content: definition.DefaultContent, OutputType: definition.OutputType, Enabled: true,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) AdminPromptTemplates(actor *model.User) ([]model.PromptTemplate, []PromptOperationDefinition, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
templates, err := s.repo.PromptTemplates()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return templates, promptDefinitions(), nil
|
||||
}
|
||||
|
||||
func (s *Service) CreatePromptTemplate(actor *model.User, req PromptTemplateRequest) (*model.PromptTemplate, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
definition, ok := promptDefinition(strings.TrimSpace(req.Operation))
|
||||
if !ok {
|
||||
return nil, BadAuthRequest("不支持的提示词模板类型")
|
||||
}
|
||||
name, content, err := validatePromptTemplateContent(definition, req.Name, req.Content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
version, err := s.repo.NextPromptTemplateVersion(definition.Operation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
template := &model.PromptTemplate{
|
||||
ID: newID(), Operation: definition.Operation, Name: name, Version: version, Content: content,
|
||||
OutputType: definition.OutputType, Enabled: req.Enabled != nil && *req.Enabled, CreatedBy: actor.ID,
|
||||
}
|
||||
if err := s.repo.SavePromptTemplate(template); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.appendAdminAudit(actor, "prompt_template.create", "prompt_template", template.ID, "创建提示词模板版本", map[string]any{"operation": template.Operation, "version": template.Version, "enabled": template.Enabled}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return template, nil
|
||||
}
|
||||
|
||||
func (s *Service) UpdatePromptTemplate(actor *model.User, id string, req PromptTemplateRequest) (*model.PromptTemplate, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
template, err := s.repo.PromptTemplate(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
definition, ok := promptDefinition(template.Operation)
|
||||
if !ok {
|
||||
return nil, BadAuthRequest("提示词模板类型已经失效")
|
||||
}
|
||||
if template.Enabled && (strings.TrimSpace(req.Content) != strings.TrimSpace(template.Content) || strings.TrimSpace(req.Name) != template.Name) {
|
||||
return nil, BadAuthRequest("启用中的版本不可直接修改,请基于它新建版本")
|
||||
}
|
||||
if req.Enabled != nil && !*req.Enabled && template.Enabled {
|
||||
return nil, BadAuthRequest("启用中的版本不能直接停用,请先启用同类型的其他版本")
|
||||
}
|
||||
name, content, err := validatePromptTemplateContent(definition, req.Name, req.Content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
template.Name = name
|
||||
template.Content = content
|
||||
if req.Enabled != nil {
|
||||
template.Enabled = *req.Enabled
|
||||
}
|
||||
if err := s.repo.SavePromptTemplate(template); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.appendAdminAudit(actor, "prompt_template.update", "prompt_template", template.ID, "更新提示词模板版本", map[string]any{"operation": template.Operation, "version": template.Version, "enabled": template.Enabled}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return template, nil
|
||||
}
|
||||
|
||||
func (s *Service) DeletePromptTemplate(actor *model.User, id string) error {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return err
|
||||
}
|
||||
template, err := s.repo.PromptTemplate(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if template.Enabled {
|
||||
return BadAuthRequest("启用中的版本不能删除,请先启用同类型的其他版本")
|
||||
}
|
||||
if err := s.repo.DeletePromptTemplate(id); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.appendAdminAudit(actor, "prompt_template.delete", "prompt_template", template.ID, "删除提示词模板版本", map[string]any{"operation": template.Operation, "version": template.Version})
|
||||
}
|
||||
|
||||
func (s *Service) UserPromptPreferences(user *model.User) ([]UserPromptPreference, error) {
|
||||
if user == nil || user.ID == "" {
|
||||
return nil, BadAuthRequest("请先登录")
|
||||
}
|
||||
customizations, err := s.repo.UserPromptCustomizations(user.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
customizationByOperation := make(map[string]*model.UserPromptCustomization, len(customizations))
|
||||
for index := range customizations {
|
||||
customization := &customizations[index]
|
||||
customizationByOperation[customization.Operation] = customization
|
||||
}
|
||||
preferences := make([]UserPromptPreference, 0)
|
||||
for _, definition := range promptDefinitions() {
|
||||
template, err := s.repo.ActivePromptTemplate(definition.Operation)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
customization := customizationByOperation[definition.Operation]
|
||||
preference := UserPromptPreference{Definition: definition, Template: template, Customization: customization}
|
||||
preference.Outdated = customization != nil && customization.Mode == promptCustomizationRewrite && template != nil && customization.BaseTemplateID != template.ID
|
||||
preferences = append(preferences, preference)
|
||||
}
|
||||
return preferences, nil
|
||||
}
|
||||
|
||||
func (s *Service) UpdateUserPromptCustomization(user *model.User, operation string, req UserPromptCustomizationRequest) (*model.UserPromptCustomization, error) {
|
||||
if user == nil || user.ID == "" {
|
||||
return nil, BadAuthRequest("请先登录")
|
||||
}
|
||||
definition, ok := promptDefinition(strings.TrimSpace(operation))
|
||||
if !ok {
|
||||
return nil, BadAuthRequest("不支持的提示词模板类型")
|
||||
}
|
||||
mode := strings.TrimSpace(req.Mode)
|
||||
if mode != promptCustomizationInherit && mode != promptCustomizationAppend && mode != promptCustomizationRewrite {
|
||||
return nil, BadAuthRequest("不支持的提示词定制方式")
|
||||
}
|
||||
content := strings.TrimSpace(req.Content)
|
||||
if mode == promptCustomizationInherit {
|
||||
content = ""
|
||||
}
|
||||
if mode != promptCustomizationInherit && content == "" {
|
||||
return nil, BadAuthRequest("请填写个人提示词要求")
|
||||
}
|
||||
if len([]rune(content)) > 12_000 {
|
||||
return nil, BadAuthRequest("个人提示词最多 12000 个字符")
|
||||
}
|
||||
if err := validatePromptPlaceholders(definition, content); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
active, err := s.repo.ActivePromptTemplate(definition.Operation)
|
||||
if err != nil {
|
||||
return nil, errors.New("当前模板类型没有启用版本")
|
||||
}
|
||||
customization, err := s.repo.UserPromptCustomization(user.ID, definition.Operation)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
customization = &model.UserPromptCustomization{ID: newID(), UserID: user.ID, Operation: definition.Operation}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
customization.Mode = mode
|
||||
customization.Content = content
|
||||
customization.BaseTemplateID = active.ID
|
||||
if err := s.repo.SaveUserPromptCustomization(customization); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return customization, nil
|
||||
}
|
||||
|
||||
func (s *Service) ResetUserPromptCustomization(user *model.User, operation string) error {
|
||||
if user == nil || user.ID == "" {
|
||||
return BadAuthRequest("请先登录")
|
||||
}
|
||||
if _, ok := promptDefinition(strings.TrimSpace(operation)); !ok {
|
||||
return BadAuthRequest("不支持的提示词模板类型")
|
||||
}
|
||||
return s.repo.DeleteUserPromptCustomization(user.ID, operation)
|
||||
}
|
||||
|
||||
func (s *Service) compilePrompt(userID string, operation string, values map[string]string) (CompiledPrompt, error) {
|
||||
definition, ok := promptDefinition(operation)
|
||||
if !ok {
|
||||
return CompiledPrompt{}, fmt.Errorf("不支持的提示词模板类型:%s", operation)
|
||||
}
|
||||
template, err := s.repo.ActivePromptTemplate(operation)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
template = &model.PromptTemplate{Operation: operation, Name: "内置默认", Version: 1, Content: definition.DefaultContent, OutputType: definition.OutputType}
|
||||
} else if err != nil {
|
||||
return CompiledPrompt{}, err
|
||||
}
|
||||
creative := template.Content
|
||||
compiled := CompiledPrompt{TemplateID: template.ID, TemplateVersion: template.Version}
|
||||
customization, customizationErr := s.repo.UserPromptCustomization(userID, operation)
|
||||
if customizationErr == nil {
|
||||
compiled.CustomizationID = customization.ID
|
||||
compiled.CustomizationUpdated = customization.UpdatedAt.Format("2006-01-02T15:04:05Z07:00")
|
||||
switch customization.Mode {
|
||||
case promptCustomizationAppend:
|
||||
creative += "\n\n【用户个性化创作要求】\n" + customization.Content
|
||||
case promptCustomizationRewrite:
|
||||
creative = customization.Content
|
||||
}
|
||||
} else if !errors.Is(customizationErr, gorm.ErrRecordNotFound) {
|
||||
return CompiledPrompt{}, customizationErr
|
||||
}
|
||||
rendered, err := renderPromptTemplate(definition, creative, values)
|
||||
if err != nil {
|
||||
return CompiledPrompt{}, err
|
||||
}
|
||||
parts := []string{strings.TrimSpace(rendered)}
|
||||
if protected := strings.TrimSpace(protectedPromptContext(operation, values)); protected != "" {
|
||||
parts = append(parts, protected)
|
||||
}
|
||||
compiled.Content = strings.Join(parts, "\n\n")
|
||||
return compiled, nil
|
||||
}
|
||||
|
||||
func validatePromptTemplateContent(definition PromptOperationDefinition, name string, content string) (string, string, error) {
|
||||
name = strings.TrimSpace(name)
|
||||
content = strings.TrimSpace(content)
|
||||
if name == "" {
|
||||
return "", "", BadAuthRequest("请填写版本名称")
|
||||
}
|
||||
if content == "" {
|
||||
return "", "", BadAuthRequest("请填写提示词模板")
|
||||
}
|
||||
if len([]rune(content)) > 30_000 {
|
||||
return "", "", BadAuthRequest("提示词模板最多 30000 个字符")
|
||||
}
|
||||
if err := validatePromptPlaceholders(definition, content); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return name, content, nil
|
||||
}
|
||||
|
||||
func validatePromptPlaceholders(definition PromptOperationDefinition, content string) error {
|
||||
allowed := make(map[string]bool, len(definition.Variables))
|
||||
for _, variable := range definition.Variables {
|
||||
allowed[variable.Placeholder] = true
|
||||
}
|
||||
unknown := make([]string, 0)
|
||||
for _, placeholder := range promptPlaceholderPattern.FindAllString(content, -1) {
|
||||
if !allowed[placeholder] {
|
||||
unknown = append(unknown, placeholder)
|
||||
}
|
||||
}
|
||||
if len(unknown) == 0 {
|
||||
return nil
|
||||
}
|
||||
sort.Strings(unknown)
|
||||
return BadAuthRequest("模板包含不支持的变量:" + strings.Join(uniqueStrings(unknown), "、"))
|
||||
}
|
||||
|
||||
func renderPromptTemplate(definition PromptOperationDefinition, content string, values map[string]string) (string, error) {
|
||||
if err := validatePromptPlaceholders(definition, content); err != nil {
|
||||
return "", err
|
||||
}
|
||||
rendered := content
|
||||
for _, variable := range definition.Variables {
|
||||
key := strings.TrimSuffix(strings.TrimPrefix(variable.Placeholder, "{{"), "}}")
|
||||
rendered = strings.ReplaceAll(rendered, variable.Placeholder, strings.TrimSpace(values[key]))
|
||||
}
|
||||
return strings.TrimSpace(rendered), nil
|
||||
}
|
||||
|
||||
func validatePromptTemplateResult(operation string, result map[string]interface{}) error {
|
||||
definition, ok := promptDefinition(operation)
|
||||
if !ok || definition.OutputType != "json" {
|
||||
return nil
|
||||
}
|
||||
text, _ := result["text"].(string)
|
||||
jsonText, err := extractJSONText(text)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s 返回内容不符合受保护 JSON 契约:%w", definition.Label, err)
|
||||
}
|
||||
if operation != promptOperationCharacterExtract {
|
||||
return nil
|
||||
}
|
||||
var payload struct {
|
||||
Characters *[]map[string]interface{} `json:"characters"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(jsonText), &payload); err != nil {
|
||||
return fmt.Errorf("角色卡提取返回的 JSON 无法解析:%w", err)
|
||||
}
|
||||
required := []string{"name", "aliases", "role", "appearance", "clothing", "physique", "personality", "props", "consistencyPrompt", "multiViewPrompt", "voiceLanguage", "voiceAge", "voiceTimbre"}
|
||||
if payload.Characters == nil {
|
||||
return errors.New("角色卡提取结果缺少 characters 数组")
|
||||
}
|
||||
for index, character := range *payload.Characters {
|
||||
for _, field := range required {
|
||||
if _, exists := character[field]; !exists {
|
||||
return fmt.Errorf("角色卡提取结果中第 %d 个角色缺少字段 %s", index+1, field)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func uniqueStrings(values []string) []string {
|
||||
result := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
if len(result) == 0 || result[len(result)-1] != value {
|
||||
result = append(result, value)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
promptOperationStoryboardPlan = "storyboard_plan"
|
||||
promptOperationStoryboardRepair = "storyboard_repair"
|
||||
promptOperationStoryboardFirstFrame = "storyboard_first_frame"
|
||||
promptOperationStoryboardVideo = "storyboard_video"
|
||||
promptOperationCharacterExtract = "character_extract"
|
||||
promptOperationCharacterTurnaround = "character_turnaround"
|
||||
)
|
||||
|
||||
func defaultPromptDefinitions() []PromptOperationDefinition {
|
||||
return []PromptOperationDefinition{
|
||||
{
|
||||
Operation: promptOperationStoryboardPlan, Label: "分镜规划", Category: "分镜", OutputType: "json", SchemaKey: "storyboard-plan/v3",
|
||||
Description: "把剧情、项目画风和当前角色版本规划为可执行镜头。",
|
||||
Variables: []PromptTemplateVariable{{Label: "项目名称", Placeholder: "{{项目名称}}"}, {Label: "项目画风", Placeholder: "{{项目画风}}"}, {Label: "用户要求", Placeholder: "{{用户要求}}"}},
|
||||
DefaultContent: defaultStoryboardPromptTemplate(),
|
||||
},
|
||||
{
|
||||
Operation: promptOperationStoryboardRepair, Label: "分镜修复", Category: "分镜", OutputType: "json", SchemaKey: "storyboard-plan/v3",
|
||||
Description: "修复模型返回的分镜结构、字段和镜头复杂度。",
|
||||
Variables: []PromptTemplateVariable{{Label: "校验错误", Placeholder: "{{校验错误}}"}, {Label: "项目画风", Placeholder: "{{项目画风}}"}},
|
||||
DefaultContent: `你是影视分镜 JSON 修复导演。修复结构时必须保留剧情信息,通过拆镜或重新分配内容解决复杂度超限,不得用删除关键剧情掩盖错误。保持原项目的视觉媒介、角色身份、服装、道具和连续性,不要擅自改写画风。只修复校验错误和由此引发的镜头组织问题。`,
|
||||
},
|
||||
{
|
||||
Operation: promptOperationStoryboardFirstFrame, Label: "分镜首帧", Category: "生成", OutputType: "text",
|
||||
Description: "把单镜头结构转换为图片模型使用的首帧提示词。",
|
||||
Variables: []PromptTemplateVariable{{Label: "项目视觉", Placeholder: "{{项目视觉}}"}, {Label: "首帧构图", Placeholder: "{{首帧构图}}"}, {Label: "表演起始状态", Placeholder: "{{表演起始状态}}"}, {Label: "负面要求", Placeholder: "{{负面要求}}"}},
|
||||
DefaultContent: `生成单一、可执行的分镜首帧。严格继承项目视觉媒介和角色资产;画面明确主体左右位置、视线、前中后景、遮挡、视觉焦点、可信光源与材质。只描述静止首帧,不提前写后续运动,不添加画外人物、无来源光线、文字或水印。
|
||||
|
||||
【项目视觉】
|
||||
{{项目视觉}}
|
||||
|
||||
【首帧构图】
|
||||
{{首帧构图}}
|
||||
|
||||
【表演起始状态】
|
||||
{{表演起始状态}}
|
||||
|
||||
【负面要求】
|
||||
{{负面要求}}`,
|
||||
},
|
||||
{
|
||||
Operation: promptOperationStoryboardVideo, Label: "分镜视频", Category: "生成", OutputType: "text",
|
||||
Description: "把单镜头结构转换为视频模型使用的紧凑执行提示词。",
|
||||
Variables: []PromptTemplateVariable{{Label: "项目视觉", Placeholder: "{{项目视觉}}"}, {Label: "镜头意图", Placeholder: "{{镜头意图}}"}, {Label: "首帧构图", Placeholder: "{{首帧构图}}"}, {Label: "表演与调度", Placeholder: "{{表演与调度}}"}, {Label: "摄影机", Placeholder: "{{摄影机}}"}, {Label: "时间节拍", Placeholder: "{{时间节拍}}"}, {Label: "运动与结尾", Placeholder: "{{运动与结尾}}"}, {Label: "声音", Placeholder: "{{声音}}"}, {Label: "执行优先级", Placeholder: "{{执行优先级}}"}, {Label: "负面要求", Placeholder: "{{负面要求}}"}},
|
||||
DefaultContent: `生成单一连续镜头的视频执行提示词。一个镜头只保留一个叙事目标、一个主运镜和一条主要动作链;摄影机运动必须有起点、动机和停止点。优先保证角色身份、表演、关键动作和连续性,次要环境效果可以简化。
|
||||
|
||||
【项目视觉】
|
||||
{{项目视觉}}
|
||||
|
||||
【镜头意图】
|
||||
{{镜头意图}}
|
||||
|
||||
【首帧构图】
|
||||
{{首帧构图}}
|
||||
|
||||
【表演与调度】
|
||||
{{表演与调度}}
|
||||
|
||||
【摄影机】
|
||||
{{摄影机}}
|
||||
|
||||
【时间节拍】
|
||||
{{时间节拍}}
|
||||
|
||||
【运动与结尾】
|
||||
{{运动与结尾}}
|
||||
|
||||
【台词与声音】
|
||||
{{声音}}
|
||||
|
||||
【执行优先级】
|
||||
{{执行优先级}}
|
||||
|
||||
【负面要求】
|
||||
{{负面要求}}`,
|
||||
},
|
||||
{
|
||||
Operation: promptOperationCharacterExtract, Label: "角色卡提取", Category: "角色", OutputType: "json", SchemaKey: "character-breakdown/v1",
|
||||
Description: "从章节正文提取需要跨镜头保持一致的角色资产。",
|
||||
Variables: []PromptTemplateVariable{{Label: "项目名称", Placeholder: "{{项目名称}}"}, {Label: "章节名称", Placeholder: "{{章节名称}}"}, {Label: "项目画风", Placeholder: "{{项目画风}}"}},
|
||||
DefaultContent: `你是短剧角色资产导演。只提取章节中实际出场、发言或对剧情产生明确作用,并且后续制作需要保持视觉或声音一致的角色。忽略系统播报、纯物件、无身份群众和没有持续角色价值的一次性路人;合并同一角色的姓名、专属称谓和别名。正文未明确的信息必须写“正文未明确”,不得自行改变人物关系、时代背景或编造编号式姓名。角色设定要具体、稳定、可用于后续三视图和视频一致性控制。`,
|
||||
},
|
||||
{
|
||||
Operation: promptOperationCharacterTurnaround, Label: "角色三视图", Category: "角色", OutputType: "text",
|
||||
Description: "按当前角色版本和项目画风生成正、侧、背三视图。",
|
||||
Variables: []PromptTemplateVariable{{Label: "角色名称", Placeholder: "{{角色名称}}"}, {Label: "项目画风", Placeholder: "{{项目画风}}"}, {Label: "角色设定", Placeholder: "{{角色设定}}"}},
|
||||
DefaultContent: `制作专业人物三视图设定表。画面严格分成三个等宽竖向区域,从左到右依次为正面全身、右侧面全身、背面全身。三个视角必须是同一角色、同一服装、同一发型、同一体型和同一比例,采用站立中性姿势,完整显示头顶到脚底。背景使用纯净中性浅色和均匀设定稿光线,只负责分离轮廓,不得改变项目画风的绘画或渲染媒介。禁止文字、边框、道具说明、表情变化和额外人物。`,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func protectedPromptContext(operation string, values map[string]string) string {
|
||||
switch operation {
|
||||
case promptOperationStoryboardPlan:
|
||||
return storyboardProtectedContext(values)
|
||||
case promptOperationStoryboardRepair:
|
||||
return storyboardRepairProtectedContext(values)
|
||||
case promptOperationCharacterExtract:
|
||||
return characterExtractProtectedContext(values)
|
||||
case promptOperationCharacterTurnaround:
|
||||
return strings.Join([]string{"【角色名称】\n" + values["角色名称"], "【项目画风】\n" + values["项目画风"], "【角色设定】\n" + values["角色设定"]}, "\n\n")
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func promptOutputContract(operation string) string {
|
||||
switch operation {
|
||||
case promptOperationStoryboardPlan, promptOperationStoryboardRepair:
|
||||
return "服务端固定 JSON Schema storyboard-plan/v3(不可由运营模板或用户定制覆盖):\n" + storyboardPlanJSONSchema
|
||||
case promptOperationCharacterExtract:
|
||||
return "服务端固定 JSON Schema character-breakdown/v1(不可由运营模板或用户定制覆盖):\n" + characterBreakdownJSONSchema
|
||||
default:
|
||||
return "当前操作输出普通文本提示词,没有 JSON Schema。"
|
||||
}
|
||||
}
|
||||
|
||||
const storyboardPlanJSONSchema = `{
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["title", "logline", "styleGuide", "characters", "locations", "shots"],
|
||||
"properties": {
|
||||
"title": {"type": "string"},
|
||||
"logline": {"type": "string"},
|
||||
"styleGuide": {"type": "string", "maxLength": 120},
|
||||
"characters": {"type": "array", "items": {"type": "string"}},
|
||||
"locations": {"type": "array", "items": {"type": "string"}},
|
||||
"shots": {
|
||||
"type": "array",
|
||||
"minItems": 1,
|
||||
"items": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["title", "description", "durationSeconds", "dialogue", "characterIds", "narrativeIntent", "viewerPOV", "performanceBlocking", "shotSize", "emotion", "lightingAndAtmosphere", "audioEffects", "visualPrompt", "videoPrompt", "camera", "motion", "timeBeats", "mustHave", "optionalDetails", "continuityOut", "negativePrompt", "assetTags"],
|
||||
"properties": {
|
||||
"title": {"type": "string"},
|
||||
"description": {"type": "string"},
|
||||
"durationSeconds": {"type": "integer", "minimum": 1, "maximum": 60},
|
||||
"dialogue": {"type": "string"},
|
||||
"characterIds": {"type": "array", "items": {"type": "string"}},
|
||||
"narrativeIntent": {"type": "string"},
|
||||
"viewerPOV": {"type": "string"},
|
||||
"performanceBlocking": {"type": "string"},
|
||||
"shotSize": {"type": "string"},
|
||||
"emotion": {"type": "string"},
|
||||
"lightingAndAtmosphere": {"type": "string"},
|
||||
"audioEffects": {"type": "string"},
|
||||
"visualPrompt": {"type": "string"},
|
||||
"videoPrompt": {"type": "string"},
|
||||
"camera": {"type": "string"},
|
||||
"motion": {"type": "string"},
|
||||
"timeBeats": {"type": "string"},
|
||||
"mustHave": {"type": "array", "maxItems": 3, "items": {"type": "string"}},
|
||||
"optionalDetails": {"type": "array", "items": {"type": "string"}},
|
||||
"continuityOut": {"type": "string"},
|
||||
"negativePrompt": {"type": "string"},
|
||||
"assetTags": {"type": "array", "items": {"type": "string"}}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
const characterBreakdownJSONSchema = `{
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["characters"],
|
||||
"properties": {
|
||||
"characters": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["name", "aliases", "role", "appearance", "clothing", "physique", "personality", "props", "consistencyPrompt", "multiViewPrompt", "voiceLanguage", "voiceAge", "voiceTimbre"],
|
||||
"properties": {
|
||||
"name": {"type": "string"},
|
||||
"aliases": {"type": "array", "items": {"type": "string"}},
|
||||
"role": {"type": "string"},
|
||||
"appearance": {"type": "string"},
|
||||
"clothing": {"type": "string"},
|
||||
"physique": {"type": "string"},
|
||||
"personality": {"type": "string"},
|
||||
"props": {"type": "string"},
|
||||
"consistencyPrompt": {"type": "string"},
|
||||
"multiViewPrompt": {"type": "string"},
|
||||
"voiceLanguage": {"type": "string"},
|
||||
"voiceAge": {"type": "string"},
|
||||
"voiceTimbre": {"type": "string"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
func storyboardProtectedContext(values map[string]string) string {
|
||||
return strings.Join([]string{
|
||||
"【剧情】\n" + values["剧情"],
|
||||
"【用户本次要求】\n" + values["用户要求"],
|
||||
"【当前画布资产】\n" + values["画布资产"],
|
||||
"【当前项目画风】\n" + values["项目画风"],
|
||||
"【当前角色版本】\n" + values["角色版本"],
|
||||
storyboardExecutionContract(values["单镜头时长规则"], values["镜头数量规则"]),
|
||||
}, "\n\n")
|
||||
}
|
||||
|
||||
func storyboardRepairProtectedContext(values map[string]string) string {
|
||||
return strings.Join([]string{
|
||||
"【原始校验错误】\n" + values["校验错误"],
|
||||
"【当前项目画风】\n" + values["项目画风"],
|
||||
"【当前角色版本】\n" + values["角色版本"],
|
||||
storyboardExecutionContract(values["单镜头时长规则"], values["镜头数量规则"]),
|
||||
"【需要修复的原始输出】\n" + values["原始输出"],
|
||||
}, "\n\n")
|
||||
}
|
||||
|
||||
func storyboardExecutionContract(durationRule string, countRule string) string {
|
||||
return `【受保护执行契约】
|
||||
- ` + durationRule + `
|
||||
- ` + countRule + `
|
||||
- 单镜头最多 2 名主要角色、1 个主运镜、1 条主要动作链、3 个 timeBeats 和 3 个 mustHave;超限必须拆镜或在固定镜头数内重新分配。
|
||||
- characterIds 只能引用当前角色版本中的 assetId;没有角色时返回空数组。
|
||||
- styleGuide 最多 120 个中文字符;visualPrompt 只描述首帧,videoPrompt 只描述运动和结尾状态。
|
||||
- 画幅比例由视频节点参数控制,提示词不得写入具体比例,也不要讨论画幅配置。
|
||||
- 只返回完整 JSON,不要 Markdown 或解释。
|
||||
- ` + promptOutputContract(promptOperationStoryboardPlan)
|
||||
}
|
||||
|
||||
func characterExtractProtectedContext(values map[string]string) string {
|
||||
return strings.Join([]string{
|
||||
fmt.Sprintf("【任务】\n从短剧项目《%s》的章节“%s”提取角色。", values["项目名称"], values["章节名称"]),
|
||||
"【项目画风】\n" + values["项目画风"],
|
||||
"【章节正文】\n" + values["章节正文"],
|
||||
"【受保护输出契约】\n" + promptOutputContract(promptOperationCharacterExtract) + "\n严格 JSON 示例:{\"characters\":[{\"name\":\"角色名\",\"aliases\":[],\"role\":\"剧情定位与人物关系\",\"appearance\":\"稳定外貌\",\"clothing\":\"固定服装\",\"physique\":\"体型体态\",\"personality\":\"表演基线\",\"props\":\"\",\"consistencyPrompt\":\"跨镜头一致性约束\",\"multiViewPrompt\":\"三视图结构重点\",\"voiceLanguage\":\"语言口音\",\"voiceAge\":\"声音年龄感\",\"voiceTimbre\":\"音色语速力度\"}]}",
|
||||
}, "\n\n")
|
||||
}
|
||||
@@ -152,6 +152,15 @@ func (s *Service) processCanvasGenerationTask(ctx context.Context, userID string
|
||||
if strings.TrimSpace(input.Prompt) == "" {
|
||||
input.Prompt = fallbackPrompt
|
||||
}
|
||||
promptTemplateOperation := metadataString(input.Metadata, "promptTemplateOperation")
|
||||
if promptTemplateOperation != "" {
|
||||
values := metadataStringValues(input.Metadata["promptTemplateVariables"])
|
||||
compiled, compileErr := s.compilePrompt(userID, promptTemplateOperation, values)
|
||||
if compileErr != nil {
|
||||
return nil, fmt.Errorf("编译用户提示词失败:%w", compileErr)
|
||||
}
|
||||
input.Prompt = compiled.Content
|
||||
}
|
||||
if strings.TrimSpace(input.Prompt) == "" {
|
||||
return nil, errors.New("prompt is required")
|
||||
}
|
||||
@@ -185,7 +194,11 @@ func (s *Service) processCanvasGenerationTask(ctx context.Context, userID string
|
||||
case "image":
|
||||
return runImageTask(ctx, input)
|
||||
case "text":
|
||||
return runTextTask(ctx, input)
|
||||
result, taskErr := runTextTask(ctx, input)
|
||||
if taskErr == nil && promptTemplateOperation != "" {
|
||||
taskErr = validatePromptTemplateResult(promptTemplateOperation, result)
|
||||
}
|
||||
return result, taskErr
|
||||
case "video":
|
||||
return runVideoTask(ctx, input)
|
||||
case "audio":
|
||||
@@ -195,6 +208,18 @@ func (s *Service) processCanvasGenerationTask(ctx context.Context, userID string
|
||||
}
|
||||
}
|
||||
|
||||
func metadataStringValues(value any) map[string]string {
|
||||
values := map[string]string{}
|
||||
raw, ok := value.(map[string]interface{})
|
||||
if !ok {
|
||||
return values
|
||||
}
|
||||
for key, item := range raw {
|
||||
values[key] = strings.TrimSpace(fmt.Sprint(item))
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func (s *Service) hydrateGenerationMedia(userID string, input *canvasGenerationInput, requirePublicURL bool) error {
|
||||
groups := [][]providerMedia{input.ReferenceImages, input.ReferenceVideos, input.ReferenceAudios}
|
||||
for _, group := range groups {
|
||||
|
||||
+415
-110
@@ -11,6 +11,7 @@ import (
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -40,13 +41,15 @@ const taskWorkerConcurrency = 3
|
||||
const taskLogPayloadLimit = 4000
|
||||
|
||||
type CreateSessionRequest struct {
|
||||
ProjectID string `json:"projectId"`
|
||||
Prompt string `json:"prompt"`
|
||||
CanvasSnapshot map[string]any `json:"canvasSnapshot"`
|
||||
References []string `json:"references"`
|
||||
Requirements string `json:"requirements"`
|
||||
CanvasAssets []storyboardAsset `json:"canvasAssets"`
|
||||
Config providerConfig `json:"config"`
|
||||
ProjectID string `json:"projectId"`
|
||||
Prompt string `json:"prompt"`
|
||||
CanvasSnapshot map[string]any `json:"canvasSnapshot"`
|
||||
References []string `json:"references"`
|
||||
Requirements string `json:"requirements"`
|
||||
CanvasAssets []storyboardAsset `json:"canvasAssets"`
|
||||
ProjectStyle storyboardProjectStyle `json:"projectStyle"`
|
||||
Characters []storyboardCharacterCard `json:"characters"`
|
||||
Config providerConfig `json:"config"`
|
||||
}
|
||||
|
||||
type CreateTaskRequest struct {
|
||||
@@ -111,21 +114,38 @@ type TaskListOptions struct {
|
||||
}
|
||||
|
||||
type agentStoryboardInput struct {
|
||||
References []string `json:"references"`
|
||||
CanvasSnapshot map[string]any `json:"canvasSnapshot"`
|
||||
Requirements string `json:"requirements"`
|
||||
CanvasAssets []storyboardAsset `json:"canvasAssets"`
|
||||
Config providerConfig `json:"config"`
|
||||
ShotDuration int `json:"shotDurationSeconds"`
|
||||
ShotCount int `json:"shotCount"`
|
||||
References []string `json:"references"`
|
||||
CanvasSnapshot map[string]any `json:"canvasSnapshot"`
|
||||
Requirements string `json:"requirements"`
|
||||
CanvasAssets []storyboardAsset `json:"canvasAssets"`
|
||||
ProjectStyle storyboardProjectStyle `json:"projectStyle"`
|
||||
Characters []storyboardCharacterCard `json:"characters"`
|
||||
Config providerConfig `json:"config"`
|
||||
ShotDuration int `json:"shotDurationSeconds"`
|
||||
ShotCount int `json:"shotCount"`
|
||||
}
|
||||
|
||||
type storyboardProjectStyle struct {
|
||||
PresetID string `json:"presetId"`
|
||||
Title string `json:"title"`
|
||||
Prompt string `json:"prompt"`
|
||||
}
|
||||
|
||||
type storyboardCharacterCard struct {
|
||||
AssetID string `json:"assetId"`
|
||||
VersionID string `json:"versionId"`
|
||||
Name string `json:"name"`
|
||||
Definition map[string]any `json:"definition"`
|
||||
}
|
||||
|
||||
type storyboardAsset struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Type string `json:"type"`
|
||||
Tags []string `json:"tags"`
|
||||
Prompt string `json:"prompt"`
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Type string `json:"type"`
|
||||
Tags []string `json:"tags"`
|
||||
Prompt string `json:"prompt"`
|
||||
CharacterAssetID string `json:"characterAssetId,omitempty"`
|
||||
CharacterVersionID string `json:"characterVersionId,omitempty"`
|
||||
}
|
||||
|
||||
type agentStoryboardPlan struct {
|
||||
@@ -153,7 +173,14 @@ type agentStoryboardShot struct {
|
||||
Motion string `json:"motion"`
|
||||
TimeBeats string `json:"timeBeats"`
|
||||
Negative string `json:"negativePrompt"`
|
||||
AssetTags []string `json:"assetTags"`
|
||||
AssetTags []string `json:"assetTags"`
|
||||
CharacterIDs []string `json:"characterIds"`
|
||||
Intent string `json:"narrativeIntent"`
|
||||
ViewerPOV string `json:"viewerPOV"`
|
||||
Performance string `json:"performanceBlocking"`
|
||||
MustHave []string `json:"mustHave"`
|
||||
Optional []string `json:"optionalDetails"`
|
||||
ContinuityOut string `json:"continuityOut"`
|
||||
}
|
||||
|
||||
func New(repo *repository.Repository, dataDir string) *Service {
|
||||
@@ -202,6 +229,9 @@ func (s *Service) CreateSession(userID string, req CreateSessionRequest) (*Sessi
|
||||
if prompt == "" {
|
||||
return nil, errors.New("prompt is required")
|
||||
}
|
||||
if err := validateStoryboardContext(req.ProjectStyle, req.Characters); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
compactedSnapshot := compactPersistedValue(req.CanvasSnapshot)
|
||||
snapshotJSON, _ := json.Marshal(compactedSnapshot)
|
||||
session := model.Session{ID: newID(), UserID: userID, ProjectID: req.ProjectID, Status: model.SessionStatusActive, Prompt: prompt, CanvasSnapshotJSON: string(snapshotJSON)}
|
||||
@@ -233,7 +263,7 @@ func (s *Service) CreateSession(userID string, req CreateSessionRequest) (*Sessi
|
||||
return nil, err
|
||||
}
|
||||
s.storageMu.Unlock()
|
||||
taskReq := CreateTaskRequest{SessionID: session.ID, ProjectID: req.ProjectID, Type: "agent_storyboard", Operation: "storyboard", Prompt: prompt, Provider: "openai-compatible", Model: req.Config.Model, Input: map[string]any{"references": req.References, "canvasSnapshot": compactedSnapshot, "requirements": req.Requirements, "canvasAssets": req.CanvasAssets, "config": req.Config}}
|
||||
taskReq := CreateTaskRequest{SessionID: session.ID, ProjectID: req.ProjectID, Type: "agent_storyboard", Operation: "storyboard", Prompt: prompt, Provider: "openai-compatible", Model: req.Config.Model, Input: map[string]any{"references": req.References, "canvasSnapshot": compactedSnapshot, "requirements": req.Requirements, "canvasAssets": req.CanvasAssets, "projectStyle": req.ProjectStyle, "characters": req.Characters, "config": req.Config}}
|
||||
if _, err := s.CreateTask(userID, taskReq); err != nil {
|
||||
s.storageMu.Lock()
|
||||
cleanupErr := s.repo.DeleteSessionDraft(userID, session.ID)
|
||||
@@ -1023,28 +1053,53 @@ func (s *Service) processAgentStoryboardTask(ctx context.Context, task model.Tas
|
||||
return nil, nil, fmt.Errorf("Agent 会话输入解析失败:%w", err)
|
||||
}
|
||||
}
|
||||
if !providerConfigReady(input.Config) {
|
||||
return nil, nil, errors.New("请先配置可用的文本模型")
|
||||
}
|
||||
if err := validateStoryboardContext(input.ProjectStyle, input.Characters); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
assets := input.CanvasAssets
|
||||
if len(assets) == 0 {
|
||||
assets = extractStoryboardAssets(input.CanvasSnapshot)
|
||||
}
|
||||
plan := fallbackAgentStoryboardPlan(task.Prompt)
|
||||
if providerConfigReady(input.Config) {
|
||||
config, err := s.resolveProviderConfig(input.Config)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
result, err := runTextTask(ctx, canvasGenerationInput{Mode: "text", Prompt: s.buildAgentStoryboardPlannerPrompt(task.Prompt, input.Requirements, assets, 0, 0), Config: config})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
text, _ := result["text"].(string)
|
||||
nextPlan, err := parseAgentStoryboardPlan(text)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
plan = nextPlan
|
||||
config, err := s.resolveProviderConfig(input.Config)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return buildAgentStoryboardResult(task, plan, assets)
|
||||
plannerPrompt, err := s.buildAgentStoryboardPlannerPrompt(task.UserID, task.Prompt, input.Requirements, assets, input.ProjectStyle, input.Characters, 0, 0)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
result, err := runTextTask(ctx, canvasGenerationInput{Mode: "text", Prompt: plannerPrompt, Config: config})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
text, _ := result["text"].(string)
|
||||
plan, err := parseAgentStoryboardPlan(text)
|
||||
if err == nil {
|
||||
err = validateStoryboardPlan(plan, 0, 0, input.Characters)
|
||||
}
|
||||
if err != nil {
|
||||
_ = s.repo.UpdateTaskProgress(task.ID, "修复分镜结构", 55)
|
||||
repairPrompt, promptErr := s.buildStoryboardRepairPrompt(task.UserID, err, input, text)
|
||||
if promptErr != nil {
|
||||
return nil, nil, promptErr
|
||||
}
|
||||
repaired, repairErr := runTextTask(withProviderRequestKind(ctx, "repair"), canvasGenerationInput{Mode: "text", Prompt: repairPrompt, Config: config})
|
||||
if repairErr != nil {
|
||||
return nil, nil, fmt.Errorf("分镜结构修复失败:%w", repairErr)
|
||||
}
|
||||
repairedText, _ := repaired["text"].(string)
|
||||
plan, err = parseAgentStoryboardPlan(repairedText)
|
||||
if err == nil {
|
||||
err = validateStoryboardPlan(plan, 0, 0, input.Characters)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("分镜模型结构修复后仍不合法:%w", err)
|
||||
}
|
||||
}
|
||||
return s.buildAgentStoryboardResult(task, plan, assets, input.ProjectStyle)
|
||||
}
|
||||
|
||||
func (s *Service) processStoryboardRowsTask(ctx context.Context, task model.Task) (map[string]interface{}, []map[string]interface{}, error) {
|
||||
@@ -1057,6 +1112,9 @@ func (s *Service) processStoryboardRowsTask(ctx context.Context, task model.Task
|
||||
if !providerConfigReady(input.Config) {
|
||||
return nil, nil, errors.New("请先配置可用的文本模型")
|
||||
}
|
||||
if err := validateStoryboardContext(input.ProjectStyle, input.Characters); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
assets := input.CanvasAssets
|
||||
if len(assets) == 0 {
|
||||
assets = extractStoryboardAssets(input.CanvasSnapshot)
|
||||
@@ -1065,21 +1123,25 @@ func (s *Service) processStoryboardRowsTask(ctx context.Context, task model.Task
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
result, err := runTextTask(ctx, canvasGenerationInput{Mode: "text", Prompt: s.buildAgentStoryboardPlannerPrompt(task.Prompt, input.Requirements, assets, input.ShotDuration, input.ShotCount), Config: config})
|
||||
plannerPrompt, err := s.buildAgentStoryboardPlannerPrompt(task.UserID, task.Prompt, input.Requirements, assets, input.ProjectStyle, input.Characters, input.ShotDuration, input.ShotCount)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
result, err := runTextTask(ctx, canvasGenerationInput{Mode: "text", Prompt: plannerPrompt, Config: config})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
text, _ := result["text"].(string)
|
||||
plan, err := parseAgentStoryboardPlan(text)
|
||||
if err == nil {
|
||||
err = validateStoryboardShotDuration(plan, input.ShotDuration)
|
||||
}
|
||||
if err == nil {
|
||||
err = validateStoryboardShotCount(plan, input.ShotCount)
|
||||
err = validateStoryboardPlan(plan, input.ShotDuration, input.ShotCount, input.Characters)
|
||||
}
|
||||
if err != nil {
|
||||
_ = s.repo.UpdateTaskProgress(task.ID, "修复分镜结构", 55)
|
||||
repairPrompt := fmt.Sprintf("请修复下面的分镜 JSON。原始校验错误:%s。必须保持原有剧情和镜头内容,补齐缺失字段并修复非法字段值;durationSeconds 必须是 1 到 60 的整数。\n\n%s\n\n只返回完整 JSON,不要 markdown 或解释。\n\n原始输出:\n%s", err.Error(), storyboardCinematicQualityContract(input.ShotDuration, input.ShotCount), text)
|
||||
repairPrompt, promptErr := s.buildStoryboardRepairPrompt(task.UserID, err, input, text)
|
||||
if promptErr != nil {
|
||||
return nil, nil, promptErr
|
||||
}
|
||||
repaired, repairErr := runTextTask(withProviderRequestKind(ctx, "repair"), canvasGenerationInput{Mode: "text", Prompt: repairPrompt, Config: config})
|
||||
if repairErr != nil {
|
||||
return nil, nil, fmt.Errorf("分镜结构修复失败:%w", repairErr)
|
||||
@@ -1087,10 +1149,7 @@ func (s *Service) processStoryboardRowsTask(ctx context.Context, task model.Task
|
||||
repairedText, _ := repaired["text"].(string)
|
||||
plan, err = parseAgentStoryboardPlan(repairedText)
|
||||
if err == nil {
|
||||
err = validateStoryboardShotDuration(plan, input.ShotDuration)
|
||||
}
|
||||
if err == nil {
|
||||
err = validateStoryboardShotCount(plan, input.ShotCount)
|
||||
err = validateStoryboardPlan(plan, input.ShotDuration, input.ShotCount, input.Characters)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("分镜模型结构修复后仍不合法:%w", err)
|
||||
@@ -1098,6 +1157,16 @@ func (s *Service) processStoryboardRowsTask(ctx context.Context, task model.Task
|
||||
}
|
||||
rows := make([]map[string]any, 0, len(plan.Shots))
|
||||
for index, shot := range plan.Shots {
|
||||
imagePromptVariables := storyboardImagePromptValues(input.ProjectStyle.Prompt, plan.StyleGuide, shot)
|
||||
videoPromptVariables := storyboardVideoPromptValues(input.ProjectStyle.Prompt, plan.StyleGuide, shot)
|
||||
imagePrompt, promptErr := s.compileStoryboardImagePrompt(task.UserID, input.ProjectStyle.Prompt, plan.StyleGuide, shot)
|
||||
if promptErr != nil {
|
||||
return nil, nil, promptErr
|
||||
}
|
||||
videoPrompt, promptErr := s.compileStoryboardVideoPrompt(task.UserID, input.ProjectStyle.Prompt, plan.StyleGuide, shot)
|
||||
if promptErr != nil {
|
||||
return nil, nil, promptErr
|
||||
}
|
||||
matchedAssets := matchStoryboardAssets(assets, shot.AssetTags)
|
||||
referenceNodeIDs := make([]string, 0, len(matchedAssets))
|
||||
for _, asset := range matchedAssets {
|
||||
@@ -1105,10 +1174,13 @@ func (s *Service) processStoryboardRowsTask(ctx context.Context, task model.Task
|
||||
}
|
||||
rows = append(rows, map[string]any{
|
||||
"shotNumber": index + 1, "durationSeconds": shot.Duration, "plotDescription": shot.Description,
|
||||
"dialogue": shot.Dialogue, "characters": []any{}, "shotSize": shot.ShotSize, "emotion": shot.Emotion,
|
||||
"dialogue": shot.Dialogue, "characters": storyboardRowCharacters(shot, input.Characters), "shotSize": shot.ShotSize, "emotion": shot.Emotion,
|
||||
"lightingAndAtmosphere": shot.Lighting, "audioEffects": shot.AudioEffects,
|
||||
"imageGenerationPrompt": shot.VisualPrompt, "videoMotionPrompt": buildStoryboardVideoPrompt(plan.StyleGuide, shot),
|
||||
"imageGenerationPrompt": imagePrompt, "videoMotionPrompt": videoPrompt,
|
||||
"imagePromptTemplateVariables": imagePromptVariables, "videoPromptTemplateVariables": videoPromptVariables,
|
||||
"camera": shot.Camera, "motion": shot.Motion, "timeBeats": shot.TimeBeats, "negativePrompt": shot.Negative,
|
||||
"narrativeIntent": shot.Intent, "viewerPOV": shot.ViewerPOV, "performanceBlocking": shot.Performance,
|
||||
"mustHave": shot.MustHave, "optionalDetails": shot.Optional, "continuityOut": shot.ContinuityOut,
|
||||
"referenceNodeIds": referenceNodeIDs, "assetTags": shot.AssetTags,
|
||||
})
|
||||
}
|
||||
@@ -1124,23 +1196,36 @@ func parseAgentStoryboardPlan(raw string) (agentStoryboardPlan, error) {
|
||||
if err != nil {
|
||||
return agentStoryboardPlan{}, err
|
||||
}
|
||||
if err := validateStoryboardJSONFields(jsonText); err != nil {
|
||||
return agentStoryboardPlan{}, err
|
||||
}
|
||||
var plan agentStoryboardPlan
|
||||
if err := json.Unmarshal([]byte(jsonText), &plan); err != nil {
|
||||
return agentStoryboardPlan{}, fmt.Errorf("分镜 JSON 解析失败:%w", err)
|
||||
}
|
||||
plan.Title = defaultString(strings.TrimSpace(plan.Title), "影视分镜")
|
||||
plan.Logline = defaultString(strings.TrimSpace(plan.Logline), "根据剧情生成的分镜方案")
|
||||
plan.StyleGuide = defaultString(strings.TrimSpace(plan.StyleGuide), "真实电影机拍摄,保持角色、空间、道具、色彩和镜头语言一致。")
|
||||
plan.StyleGuide = defaultString(strings.TrimSpace(plan.StyleGuide), "严格沿用当前项目画风,保持角色、空间、道具、色彩和视觉媒介一致。")
|
||||
if len(plan.Shots) == 0 {
|
||||
return agentStoryboardPlan{}, errors.New("分镜模型没有返回 shots")
|
||||
}
|
||||
if len(plan.Shots) > 12 {
|
||||
plan.Shots = plan.Shots[:12]
|
||||
return agentStoryboardPlan{}, fmt.Errorf("分镜数量最多 12 个,实际返回 %d 个", len(plan.Shots))
|
||||
}
|
||||
for i := range plan.Shots {
|
||||
if strings.TrimSpace(plan.Shots[i].Title) == "" {
|
||||
plan.Shots[i].Title = fmt.Sprintf("镜头 %d", i+1)
|
||||
}
|
||||
plan.Shots[i].CharacterIDs = nonNilStrings(plan.Shots[i].CharacterIDs)
|
||||
plan.Shots[i].AssetTags = nonNilStrings(plan.Shots[i].AssetTags)
|
||||
plan.Shots[i].Optional = nonNilStrings(plan.Shots[i].Optional)
|
||||
plan.Shots[i].Intent = defaultString(strings.TrimSpace(plan.Shots[i].Intent), strings.TrimSpace(plan.Shots[i].Description))
|
||||
plan.Shots[i].ViewerPOV = defaultString(strings.TrimSpace(plan.Shots[i].ViewerPOV), "客观观察当前主要角色与事件")
|
||||
plan.Shots[i].Performance = defaultString(strings.TrimSpace(plan.Shots[i].Performance), strings.TrimSpace(plan.Shots[i].Description))
|
||||
plan.Shots[i].ContinuityOut = defaultString(strings.TrimSpace(plan.Shots[i].ContinuityOut), "保持本镜头结尾的人物位置、动作状态、道具和光线方向进入下一镜")
|
||||
if len(plan.Shots[i].MustHave) == 0 {
|
||||
plan.Shots[i].MustHave = []string{"主要角色身份与当前版本稳定", "主要动作完成并有清晰落点", "结尾状态可供下一镜继承"}
|
||||
}
|
||||
if strings.TrimSpace(plan.Shots[i].VideoPrompt) == "" {
|
||||
plan.Shots[i].VideoPrompt = defaultString(plan.Shots[i].VisualPrompt, plan.Shots[i].Description)
|
||||
}
|
||||
@@ -1157,6 +1242,50 @@ func parseAgentStoryboardPlan(raw string) (agentStoryboardPlan, error) {
|
||||
return plan, nil
|
||||
}
|
||||
|
||||
func validateStoryboardJSONFields(jsonText string) error {
|
||||
var root map[string]json.RawMessage
|
||||
if err := json.Unmarshal([]byte(jsonText), &root); err != nil {
|
||||
return fmt.Errorf("分镜 JSON 解析失败:%w", err)
|
||||
}
|
||||
for _, field := range []string{"title", "logline", "styleGuide", "characters", "locations", "shots"} {
|
||||
if _, ok := root[field]; !ok {
|
||||
return fmt.Errorf("分镜 JSON 缺少受保护字段 %s", field)
|
||||
}
|
||||
}
|
||||
var shots []map[string]json.RawMessage
|
||||
if err := json.Unmarshal(root["shots"], &shots); err != nil {
|
||||
return errors.New("分镜 JSON 的 shots 必须是数组")
|
||||
}
|
||||
required := []string{"title", "description", "durationSeconds", "dialogue", "characterIds", "narrativeIntent", "viewerPOV", "performanceBlocking", "shotSize", "emotion", "lightingAndAtmosphere", "audioEffects", "visualPrompt", "videoPrompt", "camera", "motion", "timeBeats", "mustHave", "optionalDetails", "continuityOut", "negativePrompt", "assetTags"}
|
||||
for index, shot := range shots {
|
||||
for _, field := range required {
|
||||
if _, ok := shot[field]; !ok {
|
||||
return fmt.Errorf("镜头 %d 缺少受保护字段 %s", index+1, field)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func nonNilStrings(values []string) []string {
|
||||
if values == nil {
|
||||
return []string{}
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func validateStoryboardContext(projectStyle storyboardProjectStyle, characters []storyboardCharacterCard) error {
|
||||
if strings.TrimSpace(projectStyle.PresetID) == "" || strings.TrimSpace(projectStyle.Title) == "" || strings.TrimSpace(projectStyle.Prompt) == "" {
|
||||
return errors.New("请先设置项目画风,再生成分镜")
|
||||
}
|
||||
for _, character := range characters {
|
||||
if strings.TrimSpace(character.AssetID) == "" || strings.TrimSpace(character.VersionID) == "" || strings.TrimSpace(character.Name) == "" {
|
||||
return errors.New("角色卡缺少当前资产版本,请刷新角色资产后再生成分镜")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateStoryboardShotDuration(plan agentStoryboardPlan, target int) error {
|
||||
if target == 0 {
|
||||
return nil
|
||||
@@ -1172,6 +1301,22 @@ func validateStoryboardShotDuration(plan agentStoryboardPlan, target int) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateStoryboardPlan(plan agentStoryboardPlan, shotDuration int, shotCount int, characters []storyboardCharacterCard) error {
|
||||
if utf8.RuneCountInString(strings.TrimSpace(plan.StyleGuide)) > 120 {
|
||||
return errors.New("styleGuide 最多 120 个中文字符")
|
||||
}
|
||||
if err := validateStoryboardShotDuration(plan, shotDuration); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateStoryboardShotCount(plan, shotCount); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateStoryboardCharacterIDs(plan, characters); err != nil {
|
||||
return err
|
||||
}
|
||||
return validateStoryboardComplexity(plan)
|
||||
}
|
||||
|
||||
func validateStoryboardShotCount(plan agentStoryboardPlan, target int) error {
|
||||
if target == 0 {
|
||||
return nil
|
||||
@@ -1185,6 +1330,93 @@ func validateStoryboardShotCount(plan agentStoryboardPlan, target int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateStoryboardComplexity(plan agentStoryboardPlan) error {
|
||||
issues := make([]string, 0)
|
||||
for index, shot := range plan.Shots {
|
||||
shotNumber := index + 1
|
||||
if len(shot.CharacterIDs) > 2 {
|
||||
issues = append(issues, fmt.Sprintf("镜头 %d 有 %d 名主要角色,最多 2 名", shotNumber, len(shot.CharacterIDs)))
|
||||
}
|
||||
if len(shot.MustHave) > 3 {
|
||||
issues = append(issues, fmt.Sprintf("镜头 %d 有 %d 个必须完成项,最多 3 个", shotNumber, len(shot.MustHave)))
|
||||
}
|
||||
if beats := storyboardBeatCount(shot.TimeBeats); beats > 3 {
|
||||
issues = append(issues, fmt.Sprintf("镜头 %d 有 %d 个时间节拍,最多 3 个", shotNumber, beats))
|
||||
}
|
||||
if movements := storyboardCameraMovementCount(shot.Motion); movements > 1 {
|
||||
issues = append(issues, fmt.Sprintf("镜头 %d 包含 %d 种主运镜,最多 1 种", shotNumber, movements))
|
||||
}
|
||||
dialogueLimit := max(24, shot.Duration*5)
|
||||
if dialogueLength := utf8.RuneCountInString(strings.TrimSpace(shot.Dialogue)); dialogueLength > dialogueLimit {
|
||||
issues = append(issues, fmt.Sprintf("镜头 %d 台词 %d 字,%d 秒镜头最多约 %d 字", shotNumber, dialogueLength, shot.Duration, dialogueLimit))
|
||||
}
|
||||
}
|
||||
if len(issues) == 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("镜头复杂度超限:%s", strings.Join(issues, ";"))
|
||||
}
|
||||
|
||||
func validateStoryboardCharacterIDs(plan agentStoryboardPlan, characters []storyboardCharacterCard) error {
|
||||
allowed := make(map[string]bool, len(characters))
|
||||
for _, character := range characters {
|
||||
allowed[character.AssetID] = true
|
||||
}
|
||||
for index, shot := range plan.Shots {
|
||||
for _, assetID := range shot.CharacterIDs {
|
||||
if !allowed[assetID] {
|
||||
return fmt.Errorf("镜头 %d 引用了不存在或非当前版本的角色 assetId:%s", index+1, assetID)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func storyboardRowCharacters(shot agentStoryboardShot, characters []storyboardCharacterCard) []map[string]any {
|
||||
byID := make(map[string]storyboardCharacterCard, len(characters))
|
||||
for _, character := range characters {
|
||||
byID[character.AssetID] = character
|
||||
}
|
||||
result := make([]map[string]any, 0, len(shot.CharacterIDs))
|
||||
for _, assetID := range shot.CharacterIDs {
|
||||
character, ok := byID[assetID]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
result = append(result, map[string]any{
|
||||
"characterName": character.Name,
|
||||
"characterAssetId": character.AssetID,
|
||||
"characterVersionId": character.VersionID,
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func storyboardBeatCount(value string) int {
|
||||
parts := strings.FieldsFunc(value, func(r rune) bool { return r == ';' || r == ';' || r == '\n' })
|
||||
count := 0
|
||||
for _, part := range parts {
|
||||
if strings.TrimSpace(part) != "" {
|
||||
count++
|
||||
}
|
||||
}
|
||||
timecodeCount := len(storyboardTimecodePattern.FindAllString(value, -1))
|
||||
return max(count, timecodeCount)
|
||||
}
|
||||
|
||||
var storyboardTimecodePattern = regexp.MustCompile(`\d+(?:\.\d+)?\s*[-~—至到]\s*\d+(?:\.\d+)?\s*秒`)
|
||||
|
||||
func storyboardCameraMovementCount(value string) int {
|
||||
movements := []string{"推进", "推近", "拉远", "摇摄", "横移", "侧移", "跟拍", "跟随", "升降", "上升", "下降", "环绕", "俯冲", "变焦", "甩镜", "穿越"}
|
||||
count := 0
|
||||
for _, movement := range movements {
|
||||
if strings.Contains(value, movement) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func extractJSONText(raw string) (string, error) {
|
||||
trimmed := strings.TrimSpace(raw)
|
||||
trimmed = strings.TrimPrefix(trimmed, "```json")
|
||||
@@ -1193,49 +1425,12 @@ func extractJSONText(raw string) (string, error) {
|
||||
start := strings.Index(trimmed, "{")
|
||||
end := strings.LastIndex(trimmed, "}")
|
||||
if start < 0 || end < start {
|
||||
return "", errors.New("分镜模型返回的不是 JSON")
|
||||
return "", errors.New("模型返回的不是 JSON")
|
||||
}
|
||||
return trimmed[start : end+1], nil
|
||||
}
|
||||
|
||||
func fallbackAgentStoryboardPlan(prompt string) agentStoryboardPlan {
|
||||
title := shortTitle(prompt, 18)
|
||||
return agentStoryboardPlan{
|
||||
Title: title,
|
||||
Logline: "围绕用户 brief 拆解的影视短片工作流。",
|
||||
StyleGuide: "真实电影机拍摄,自然曝光,低饱和色彩,保持角色、空间、道具和镜头语言一致。",
|
||||
Characters: []string{"主角:根据 brief 保持服装、动作动机和情绪连续。"},
|
||||
Locations: []string{"主场景:根据 brief 建立前景、中景、远景和可信光源。"},
|
||||
Shots: []agentStoryboardShot{
|
||||
{
|
||||
Title: "开场建立",
|
||||
Description: "建立故事空间、主角状态和情绪基调。",
|
||||
VisualPrompt: "以真实电影机语言建立主要空间和角色状态,前景、中景、远景层次清晰。",
|
||||
VideoPrompt: "8 秒连续镜头,从故事空间中的人类尺度前景开始,摄影机缓慢前推,先展示环境细节和主角状态,中段让关键冲突迹象进入画面,结尾停在主角反应。自然曝光,真实高光滚降,空气介质和轻微胶片颗粒,避免廉价特效感、均匀平光和无尺度参照。",
|
||||
Camera: "中景,平视到轻微低机位,中等焦段",
|
||||
Motion: "缓慢前推,结尾停住",
|
||||
},
|
||||
{
|
||||
Title: "冲突推进",
|
||||
Description: "推进动作、关系变化和核心冲突。",
|
||||
VisualPrompt: "主体动作、道具和环境反馈同时出现,空间调度明确。",
|
||||
VideoPrompt: "10 秒连续镜头,摄影机从主角侧后方跟随移动,开始画面聚焦人物动作和关键道具,中段冲突升级,环境中的灯光、尘埃、水汽或人群反应随动作发生变化,结尾用中近景压住情绪。真实电影机拍摄,受控冷暖对比,运动处保留自然模糊,避免过度锐化、过亮轮廓和塑料表面。",
|
||||
Camera: "中近景,侧后方跟拍,中长焦压缩空间",
|
||||
Motion: "跟拍加轻微抬镜",
|
||||
},
|
||||
{
|
||||
Title: "结果与钩子",
|
||||
Description: "交代结果并留下下一段钩子。",
|
||||
VisualPrompt: "主角反应、环境后果和悬念信息同框。",
|
||||
VideoPrompt: "8 秒连续镜头,从冲突后的环境细节开始,摄影机缓慢横移揭示结果,中段主角进入画面并完成关键反应,结尾停在一个可延续的悬念物或空间方向。真实电影摄影质感,暗部保留层次,高光不过曝,低饱和色彩,避免海报式摆拍、干净空白背景和主体完整居中平铺。",
|
||||
Camera: "中景到近景,横移构图",
|
||||
Motion: "缓慢横移,结尾定格",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func buildAgentStoryboardResult(task model.Task, plan agentStoryboardPlan, assets []storyboardAsset) (map[string]interface{}, []map[string]interface{}, error) {
|
||||
func (s *Service) buildAgentStoryboardResult(task model.Task, plan agentStoryboardPlan, assets []storyboardAsset, projectStyle storyboardProjectStyle) (map[string]interface{}, []map[string]interface{}, error) {
|
||||
prefix := "agent-" + task.ID
|
||||
scriptID := prefix + "-script"
|
||||
sceneID := prefix + "-scenes"
|
||||
@@ -1247,15 +1442,19 @@ func buildAgentStoryboardResult(task model.Task, plan agentStoryboardPlan, asset
|
||||
ops := []map[string]any{
|
||||
nodeOpWithMetadata(scriptID, "text", "剧本 · "+shortTitle(plan.Title, 24), 0, 0, map[string]any{"workflowKind": "script", "workflowTitle": "剧本", "status": "success", "content": strings.Join([]string{plan.Title, "", plan.Logline, "", task.Prompt}, "\n")}),
|
||||
nodeOpWithMetadata(sceneID, "text", "场景设定", sceneX, 0, map[string]any{"workflowKind": "scene", "workflowTitle": "场景", "status": "success", "content": listContent("场景", plan.Locations)}),
|
||||
nodeOpWithMetadata(styleID, "text", "风格板", styleX, 0, map[string]any{"workflowKind": "styleboard", "workflowTitle": "风格板", "status": "success", "content": plan.StyleGuide}),
|
||||
nodeOpWithMetadata(styleID, "text", "项目画风 · "+shortTitle(projectStyle.Title, 24), styleX, 0, map[string]any{"workflowKind": "styleboard", "workflowTitle": "项目画风", "workflowDescription": plan.StyleGuide, "stylePresetId": projectStyle.PresetID, "status": "success", "content": projectStyle.Prompt, "prompt": projectStyle.Prompt}),
|
||||
nodeOpWithMetadata(referenceID, "text", "参考素材组", 0, 270, map[string]any{"workflowKind": "reference_set", "workflowTitle": "参考素材组", "status": "success", "content": storyboardAssetsContent(assets)}),
|
||||
nodeOpWithMetadata(finalID, "video", "成片 · 待生成", styleX, 270, map[string]any{"workflowKind": "final", "workflowTitle": "成片", "status": "idle"}),
|
||||
connectOp(scriptID, sceneID),
|
||||
}
|
||||
resultShots := make([]map[string]any, 0, len(plan.Shots))
|
||||
for index, shot := range plan.Shots {
|
||||
videoPrompt, err := s.compileStoryboardVideoPrompt(task.UserID, projectStyle.Prompt, plan.StyleGuide, shot)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
shotID := fmt.Sprintf("%s-shot-%d", prefix, index+1)
|
||||
matchedAssets := matchStoryboardAssets(assets, shot.AssetTags)
|
||||
matchedAssets := matchStoryboardAssetsForShot(assets, shot)
|
||||
assetIDs := make([]string, 0, len(matchedAssets))
|
||||
for _, asset := range matchedAssets {
|
||||
assetIDs = append(assetIDs, asset.ID)
|
||||
@@ -1267,9 +1466,9 @@ func buildAgentStoryboardResult(task model.Task, plan agentStoryboardPlan, asset
|
||||
"workflowDescription": shotDescription(shot),
|
||||
"shotIndex": index + 1,
|
||||
"generationMode": "video",
|
||||
"prompt": buildStoryboardVideoPrompt(plan.StyleGuide, shot),
|
||||
"composerContent": shotComposerContent(buildStoryboardVideoPrompt(plan.StyleGuide, shot), matchedAssets),
|
||||
"videoEditOperation": "image_to_video",
|
||||
"prompt": videoPrompt,
|
||||
"composerContent": shotComposerContent(videoPrompt, matchedAssets),
|
||||
"videoEditOperation": "text_to_video",
|
||||
"assetTags": shot.AssetTags,
|
||||
"referenceAssetNodeIds": assetIDs,
|
||||
"status": "idle",
|
||||
@@ -1303,13 +1502,18 @@ func extractStoryboardAssets(snapshot map[string]any) []storyboardAsset {
|
||||
assets := make([]storyboardAsset, 0, len(rawNodes))
|
||||
for _, raw := range rawNodes {
|
||||
node, _ := raw.(map[string]interface{})
|
||||
if node == nil || fmt.Sprint(node["type"]) != "image" {
|
||||
if node == nil {
|
||||
continue
|
||||
}
|
||||
metadata, _ := node["metadata"].(map[string]interface{})
|
||||
if metadata == nil {
|
||||
metadata = map[string]interface{}{}
|
||||
}
|
||||
nodeType := stringValue(node["type"])
|
||||
isCharacterCard := stringValue(metadata["workflowKind"]) == "character" && stringValue(metadata["characterAssetId"]) != "" && stringValue(metadata["characterVersionId"]) != ""
|
||||
if nodeType != "image" && !isCharacterCard {
|
||||
continue
|
||||
}
|
||||
id := stringValue(node["id"])
|
||||
if id == "" {
|
||||
continue
|
||||
@@ -1317,10 +1521,18 @@ func extractStoryboardAssets(snapshot map[string]any) []storyboardAsset {
|
||||
tags := stringSlice(metadata["assetTags"])
|
||||
prompt := stringValue(metadata["prompt"])
|
||||
content := stringValue(metadata["content"])
|
||||
if len(tags) == 0 && prompt == "" && content == "" {
|
||||
if len(tags) == 0 && prompt == "" && content == "" && !isCharacterCard {
|
||||
continue
|
||||
}
|
||||
assets = append(assets, storyboardAsset{ID: id, Title: defaultString(stringValue(node["title"]), "未命名图片"), Type: "image", Tags: tags, Prompt: prompt})
|
||||
assets = append(assets, storyboardAsset{
|
||||
ID: id,
|
||||
Title: defaultString(stringValue(node["title"]), "未命名图片"),
|
||||
Type: defaultString(nodeType, "reference"),
|
||||
Tags: tags,
|
||||
Prompt: prompt,
|
||||
CharacterAssetID: stringValue(metadata["characterAssetId"]),
|
||||
CharacterVersionID: stringValue(metadata["characterVersionId"]),
|
||||
})
|
||||
if len(assets) >= 30 {
|
||||
break
|
||||
}
|
||||
@@ -1359,6 +1571,34 @@ func matchStoryboardAssets(assets []storyboardAsset, shotTags []string) []storyb
|
||||
return matched
|
||||
}
|
||||
|
||||
func matchStoryboardAssetsForShot(assets []storyboardAsset, shot agentStoryboardShot) []storyboardAsset {
|
||||
matched := make([]storyboardAsset, 0, 6)
|
||||
seen := make(map[string]bool, 6)
|
||||
wantedCharacters := make(map[string]bool, len(shot.CharacterIDs))
|
||||
for _, assetID := range shot.CharacterIDs {
|
||||
wantedCharacters[assetID] = true
|
||||
}
|
||||
for _, asset := range assets {
|
||||
if len(matched) >= 6 {
|
||||
break
|
||||
}
|
||||
if !seen[asset.ID] && wantedCharacters[asset.CharacterAssetID] {
|
||||
matched = append(matched, asset)
|
||||
seen[asset.ID] = true
|
||||
}
|
||||
}
|
||||
for _, asset := range matchStoryboardAssets(assets, shot.AssetTags) {
|
||||
if len(matched) >= 6 {
|
||||
break
|
||||
}
|
||||
if !seen[asset.ID] {
|
||||
matched = append(matched, asset)
|
||||
seen[asset.ID] = true
|
||||
}
|
||||
}
|
||||
return matched
|
||||
}
|
||||
|
||||
func storyboardTokensMatch(wanted map[string]bool, tokens map[string]bool) bool {
|
||||
for want := range wanted {
|
||||
if tokens[want] {
|
||||
@@ -1458,24 +1698,89 @@ func shotDescription(shot agentStoryboardShot) string {
|
||||
return strings.Join(filtered, "\n\n")
|
||||
}
|
||||
|
||||
func buildStoryboardVideoPrompt(styleGuide string, shot agentStoryboardShot) string {
|
||||
func storyboardImagePromptValues(projectStyle string, styleGuide string, shot agentStoryboardShot) map[string]string {
|
||||
negative := defaultString(strings.TrimSpace(shot.Negative), "禁止换脸、服装变化、手部畸形、乱码、风格突变和塑料材质")
|
||||
return map[string]string{
|
||||
"项目视觉": storyboardProjectVisualSummary(projectStyle, styleGuide),
|
||||
"首帧构图": compactPromptText(shot.VisualPrompt+";光影:"+shot.Lighting, 360),
|
||||
"表演起始状态": compactPromptText(shot.Performance, 180),
|
||||
"负面要求": compactPromptText(negative, 140),
|
||||
}
|
||||
}
|
||||
|
||||
func buildStoryboardImagePrompt(projectStyle string, styleGuide string, shot agentStoryboardShot) string {
|
||||
definition, _ := promptDefinition(promptOperationStoryboardFirstFrame)
|
||||
prompt, _ := renderPromptTemplate(definition, definition.DefaultContent, storyboardImagePromptValues(projectStyle, styleGuide, shot))
|
||||
return prompt
|
||||
}
|
||||
|
||||
func (s *Service) compileStoryboardImagePrompt(userID string, projectStyle string, styleGuide string, shot agentStoryboardShot) (string, error) {
|
||||
compiled, err := s.compilePrompt(userID, promptOperationStoryboardFirstFrame, storyboardImagePromptValues(projectStyle, styleGuide, shot))
|
||||
return compiled.Content, err
|
||||
}
|
||||
|
||||
func storyboardVideoPromptValues(projectStyle string, styleGuide string, shot agentStoryboardShot) map[string]string {
|
||||
camera := defaultString(strings.TrimSpace(shot.Camera), strings.TrimSpace(shot.ShotSize)+",平视机位,中等焦段,主体与环境保持空间层次")
|
||||
motion := defaultString(strings.TrimSpace(shot.Motion), "固定机位,主体在画面内完成动作")
|
||||
timeBeats := defaultString(strings.TrimSpace(shot.TimeBeats), fmt.Sprintf("0-%d秒:%s", shot.Duration, strings.TrimSpace(shot.Description)))
|
||||
negative := defaultString(strings.TrimSpace(shot.Negative), "禁止换脸、服装变化、手部畸形、乱码、闪烁、风格突变和动作僵硬")
|
||||
parts := []string{
|
||||
"【氛围与画质】\n" + strings.TrimSpace(styleGuide),
|
||||
"【镜头设计】\n" + strings.TrimSpace(shot.ShotSize) + ";" + camera + ";运镜:" + motion,
|
||||
"【画面内容】\n" + timeBeats,
|
||||
values := map[string]string{
|
||||
"项目视觉": storyboardProjectVisualSummary(projectStyle, styleGuide),
|
||||
"镜头意图": compactPromptText(shot.Intent+";观众视点:"+shot.ViewerPOV+";情绪:"+shot.Emotion, 150),
|
||||
"首帧构图": compactPromptText(shot.VisualPrompt+";光影:"+shot.Lighting, 280),
|
||||
"表演与调度": compactPromptText(shot.Performance, 180),
|
||||
"摄影机": compactPromptText(strings.TrimSpace(shot.ShotSize)+";"+camera+";主运镜:"+motion, 220),
|
||||
"时间节拍": compactPromptText(timeBeats, 240),
|
||||
"运动与结尾": compactPromptText(shot.VideoPrompt+";连续性结尾:"+shot.ContinuityOut, 240),
|
||||
"声音": compactPromptText(strings.TrimSpace(shot.Dialogue)+";音效:"+strings.TrimSpace(shot.AudioEffects), 160),
|
||||
"负面要求": compactPromptText(negative, 160),
|
||||
}
|
||||
if strings.TrimSpace(shot.Dialogue) != "" || strings.TrimSpace(shot.AudioEffects) != "" {
|
||||
parts = append(parts, "【台词/声音】\n"+strings.TrimSpace(shot.Dialogue)+";音效:"+strings.TrimSpace(shot.AudioEffects))
|
||||
if len(shot.MustHave) > 0 {
|
||||
priority := "必须完成:" + strings.Join(shot.MustHave, ";")
|
||||
if len(shot.Optional) > 0 {
|
||||
priority += "。可以简化:" + strings.Join(shot.Optional, ";")
|
||||
}
|
||||
values["执行优先级"] = compactPromptText(priority, 140)
|
||||
}
|
||||
if strings.TrimSpace(shot.VideoPrompt) != "" {
|
||||
parts = append(parts, "【执行约束】\n"+strings.TrimSpace(shot.VideoPrompt))
|
||||
return values
|
||||
}
|
||||
|
||||
func buildStoryboardVideoPrompt(projectStyle string, styleGuide string, shot agentStoryboardShot) string {
|
||||
definition, _ := promptDefinition(promptOperationStoryboardVideo)
|
||||
prompt, _ := renderPromptTemplate(definition, definition.DefaultContent, storyboardVideoPromptValues(projectStyle, styleGuide, shot))
|
||||
return prompt
|
||||
}
|
||||
|
||||
func (s *Service) compileStoryboardVideoPrompt(userID string, projectStyle string, styleGuide string, shot agentStoryboardShot) (string, error) {
|
||||
compiled, err := s.compilePrompt(userID, promptOperationStoryboardVideo, storyboardVideoPromptValues(projectStyle, styleGuide, shot))
|
||||
return compiled.Content, err
|
||||
}
|
||||
|
||||
func storyboardProjectVisualSummary(projectStyle string, styleGuide string) string {
|
||||
identity := ""
|
||||
for _, line := range strings.Split(projectStyle, "\n") {
|
||||
if strings.TrimSpace(line) != "" {
|
||||
identity = strings.TrimSpace(line)
|
||||
break
|
||||
}
|
||||
}
|
||||
parts = append(parts, "【负面要求】\n"+negative)
|
||||
return strings.Join(parts, "\n\n")
|
||||
parts := make([]string, 0, 2)
|
||||
if identity != "" {
|
||||
parts = append(parts, identity)
|
||||
}
|
||||
if strings.TrimSpace(styleGuide) != "" {
|
||||
parts = append(parts, strings.TrimSpace(styleGuide))
|
||||
}
|
||||
return compactPromptText(strings.Join(parts, ";"), 180)
|
||||
}
|
||||
|
||||
func compactPromptText(value string, limit int) string {
|
||||
text := strings.TrimSpace(value)
|
||||
if utf8.RuneCountInString(text) <= limit {
|
||||
return text
|
||||
}
|
||||
runes := []rune(text)
|
||||
return strings.TrimSpace(string(runes[:limit])) + "。"
|
||||
}
|
||||
|
||||
func shotComposerContent(prompt string, assets []storyboardAsset) string {
|
||||
|
||||
@@ -4,224 +4,81 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
)
|
||||
|
||||
type StoryboardPromptTemplateRequest struct {
|
||||
Name string `json:"name"`
|
||||
Content string `json:"content"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
}
|
||||
|
||||
type StoryboardPromptVariable struct {
|
||||
Label string `json:"label"`
|
||||
Placeholder string `json:"placeholder"`
|
||||
}
|
||||
|
||||
var storyboardPromptVariables = []StoryboardPromptVariable{
|
||||
{Label: "剧情", Placeholder: "{{剧情}}"},
|
||||
{Label: "用户要求", Placeholder: "{{用户要求}}"},
|
||||
{Label: "画布资产", Placeholder: "{{画布资产}}"},
|
||||
}
|
||||
|
||||
func (s *Service) EnsureDefaultStoryboardPromptTemplate() error {
|
||||
count, err := s.repo.StoryboardPromptTemplateCount()
|
||||
func (s *Service) buildAgentStoryboardPlannerPrompt(userID string, brief string, requirements string, assets []storyboardAsset, projectStyle storyboardProjectStyle, characters []storyboardCharacterCard, shotDuration int, shotCount int) (string, error) {
|
||||
values := storyboardPromptValues(brief, requirements, assets, projectStyle, characters, shotDuration, shotCount)
|
||||
compiled, err := s.compilePrompt(userID, promptOperationStoryboardPlan, values)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
if count > 0 {
|
||||
// 只升级仍保留旧占位符的内置模板,避免覆盖管理员创建或修改的版本。
|
||||
templates, err := s.repo.StoryboardPromptTemplates()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for index := range templates {
|
||||
template := &templates[index]
|
||||
if template.Name == "默认影视分镜提示词" && strings.Contains(template.Content, "\n....\n") {
|
||||
template.Content = defaultStoryboardPromptTemplate()
|
||||
return s.repo.SaveStoryboardPromptTemplate(template)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return s.repo.SaveStoryboardPromptTemplate(&model.StoryboardPromptTemplate{
|
||||
ID: newID(),
|
||||
Name: "默认影视分镜提示词",
|
||||
Content: defaultStoryboardPromptTemplate(),
|
||||
Enabled: true,
|
||||
})
|
||||
return removeFixedMediaRestrictions(compiled.Content), nil
|
||||
}
|
||||
|
||||
func (s *Service) AdminStoryboardPromptTemplates(actor *model.User) ([]model.StoryboardPromptTemplate, []StoryboardPromptVariable, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
templates, err := s.repo.StoryboardPromptTemplates()
|
||||
func (s *Service) buildStoryboardRepairPrompt(userID string, validationErr error, input agentStoryboardInput, original string) (string, error) {
|
||||
values := storyboardPromptValues("", input.Requirements, input.CanvasAssets, input.ProjectStyle, input.Characters, input.ShotDuration, input.ShotCount)
|
||||
values["校验错误"] = validationErr.Error()
|
||||
values["原始输出"] = original
|
||||
compiled, err := s.compilePrompt(userID, promptOperationStoryboardRepair, values)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return "", err
|
||||
}
|
||||
return templates, storyboardPromptVariables, nil
|
||||
return removeFixedMediaRestrictions(compiled.Content), nil
|
||||
}
|
||||
|
||||
func (s *Service) CreateStoryboardPromptTemplate(actor *model.User, req StoryboardPromptTemplateRequest) (*model.StoryboardPromptTemplate, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, err
|
||||
func storyboardPromptValues(brief string, requirements string, assets []storyboardAsset, projectStyle storyboardProjectStyle, characters []storyboardCharacterCard, shotDuration int, shotCount int) map[string]string {
|
||||
assetJSON, _ := json.MarshalIndent(assets, "", " ")
|
||||
characterJSON, _ := json.MarshalIndent(characters, "", " ")
|
||||
styleText := strings.TrimSpace(projectStyle.Prompt)
|
||||
if styleText == "" {
|
||||
styleText = "项目尚未设置画风;仅允许生成中性分镜草案,不得擅自指定视觉媒介。"
|
||||
}
|
||||
template, err := storyboardPromptTemplateFromRequest(req, model.StoryboardPromptTemplate{ID: newID(), CreatedBy: actor.ID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if req.Enabled == nil {
|
||||
template.Enabled = false
|
||||
}
|
||||
if err := s.repo.SaveStoryboardPromptTemplate(&template); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &template, nil
|
||||
}
|
||||
|
||||
func (s *Service) UpdateStoryboardPromptTemplate(actor *model.User, id string, req StoryboardPromptTemplateRequest) (*model.StoryboardPromptTemplate, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
template, err := s.repo.StoryboardPromptTemplate(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if template.Enabled && req.Enabled != nil && !*req.Enabled {
|
||||
return nil, BadAuthRequest("至少需要保留一个启用的分镜提示词")
|
||||
}
|
||||
next, err := storyboardPromptTemplateFromRequest(req, *template)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.repo.SaveStoryboardPromptTemplate(&next); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &next, nil
|
||||
}
|
||||
|
||||
func (s *Service) DeleteStoryboardPromptTemplate(actor *model.User, id string) error {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return err
|
||||
}
|
||||
template, err := s.repo.StoryboardPromptTemplate(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if template.Enabled {
|
||||
return BadAuthRequest("启用中的分镜提示词不能删除,请先启用其他版本")
|
||||
}
|
||||
return s.repo.DeleteStoryboardPromptTemplate(id)
|
||||
}
|
||||
|
||||
func (s *Service) buildAgentStoryboardPlannerPrompt(brief string, requirements string, assets []storyboardAsset, shotDuration int, shotCount int) string {
|
||||
template := defaultStoryboardPromptTemplate()
|
||||
if active, err := s.repo.ActiveStoryboardPromptTemplate(); err == nil && strings.TrimSpace(active.Content) != "" {
|
||||
template = active.Content
|
||||
}
|
||||
return renderStoryboardPromptTemplate(template, brief, requirements, assets) + "\n\n" + storyboardCinematicQualityContract(shotDuration, shotCount)
|
||||
}
|
||||
|
||||
func storyboardCinematicQualityContract(shotDuration int, shotCount int) string {
|
||||
durationRule := "单个镜头时长由剧情节奏决定,必须是 1 到 60 秒的整数。"
|
||||
if shotDuration == 5 || shotDuration == 10 || shotDuration == 15 || shotDuration == 30 {
|
||||
durationRule = fmt.Sprintf("本次生成单个镜头时长必须严格等于 %d 秒;模型只决定镜头数量和总时长,不得修改单镜头时长。", shotDuration)
|
||||
durationRule = fmt.Sprintf("本次生成单个镜头时长必须严格等于 %d 秒。", shotDuration)
|
||||
}
|
||||
countRule := "镜头数量由模型按剧情节奏自动决定。"
|
||||
if shotCount >= 1 && shotCount <= 10 {
|
||||
countRule = fmt.Sprintf("shots 数组必须严格输出 %d 个镜头,不得多于或少于 %d 个;该规则覆盖模板中的默认镜头数量范围。", shotCount, shotCount)
|
||||
countRule = fmt.Sprintf("shots 数组必须严格输出 %d 个镜头。", shotCount)
|
||||
}
|
||||
return map[string]string{
|
||||
"项目名称": projectStyle.Title, "剧情": strings.TrimSpace(brief), "用户要求": strings.TrimSpace(requirements),
|
||||
"画布资产": string(assetJSON), "项目画风": styleText, "角色版本": string(characterJSON),
|
||||
"单镜头时长规则": durationRule, "镜头数量规则": countRule,
|
||||
}
|
||||
contract := `强制电影化质量契约(优先级高于用户 brief 中的泛化叙述):
|
||||
- ` + durationRule + `
|
||||
- ` + countRule + `
|
||||
- 不要把剧情段落直接改写成镜头摘要。每一行必须是一个可执行、可拍摄的连续镜头,必须写清主体在画面中的具体动作、空间关系、视觉焦点和可见结果。
|
||||
- 每个镜头必须有摄影机设计:shotSize、camera(机位高度/角度/焦段/景别/构图)、motion(推拉摇移跟/环绕/升降/手持及起止方向)。镜头之间要有远近景、角度或运动变化,避免同质中景。
|
||||
- timeBeats 必须按时间码拆解该镜头内部动作,例如“0-1.2秒:...;1.2-4.5秒:...;4.5-8秒:...”,覆盖完整 durationSeconds,包含开始画面、中段变化和结尾停点。
|
||||
- description 只写可见画面与动作,不使用“意识到、回忆起、感到、关系建立、命运转折”等不可直接拍摄的剧情总结;这些内容必须转译成眼神、停顿、手部动作、走位、道具反应或环境变化。
|
||||
- visualPrompt 必须包含主体、前中后景、构图、光线、材质、色彩和真实摄影质感;videoPrompt 必须补充主体运动、环境运动、连续性、时长和转场逻辑。
|
||||
- styleGuide、visualPrompt 和 videoPrompt 禁止写入 2.39:1、16:9 等具体画幅比例,也不要讨论画幅配置。
|
||||
- negativePrompt 必须针对本镜头列出换脸、服装变化、手部错误、乱码、闪烁、风格突变、动作僵硬等风险,不要只写“无”。
|
||||
- 只返回 JSON。shots 中每项必须包含:title、description、durationSeconds、dialogue、shotSize、emotion、lightingAndAtmosphere、audioEffects、visualPrompt、videoPrompt、camera、motion、timeBeats、negativePrompt、assetTags。`
|
||||
|
||||
return contract + "\n\n" + storyboardCameraLanguageGuide()
|
||||
}
|
||||
|
||||
func storyboardCameraLanguageGuide() string {
|
||||
return `分镜镜头语言规则(必须融入每个镜头的设计,服务叙事而非炫技):
|
||||
- 景别必须从以下六类中选择并写入 shotSize:S01 大远景 ELS(环境/宏大空间)、S02 远景 LS(人物全身与处境)、S03 全景 FS(完整动作)、S04 中景 MS(自然叙事/对话首选)、S05 近景 CU(表情与反应)、S06 特写 ECU(眼睛、手或关键细节)。同一场景按叙事需要渐进变化,避免无逻辑跳跃。
|
||||
- 机位角度按叙事需要选择:A01 平视 Eye Level(中性客观)、A02 仰角 Low Angle(强大/压迫)、A03 俯角 High Angle(弱小/脆弱)、A04 鸟瞰 Bird's Eye/Top Down(全知/构图)、A05 倾斜角 Dutch Angle(失衡/不安)、A06 蜗牛视角 Worm's Eye(极度崇高或压迫)。无动机不得突变视角。
|
||||
- 运镜从以下体系中按情绪和空间目的选择,并在 motion 写清起止状态:M01 推、M02 拉、M03 横移、M04 水平摇、M05 垂直倾斜、M06 升降、M07 环绕、M08 甩镜、M09 手持、M10 稳定器跟拍、M11 主观跟进、M12 航拍推进、M13 航拍拉升、M14 变焦、M15 希区柯克变焦、M16 过肩。运动镜头必须有动机;以静制动,固定镜头与运动镜头要形成对比。
|
||||
- 构图优先从以下方法选择并写入 camera 或 visualPrompt:C01 三分法、C02 框中框、C03 引导线、C04 对称构图、C05 负空间、C06 前景叠层。必须交代主体、前景/中景/远景和视觉焦点。
|
||||
- 叙事手法按需要使用:N01 主观镜头、N02 反应镜头、N03 空镜/插入、N04 匹配剪辑、N05 交叉剪辑、N06 跳切。对话优先 MS + OTS(过肩)+ 反应镜头,不要用复杂运动掩盖表演。
|
||||
- 节奏约束:推/拉/横移/升降等运动镜头通常保持 5-8 秒以上;甩镜和希区柯克变焦全片各不超过 1-2 次;手持只用于纪实、紧迫或混乱等明确情绪;航拍主要用于开篇或高潮;ECU 每场景最多 2 个。
|
||||
- 禁止每 3-5 秒更换一次运镜,禁止同类技法连续重复,禁止无叙事动机的角度突变;希区柯克变焦仅用于顿悟、恐惧或心理扭转等关键时刻。`
|
||||
}
|
||||
|
||||
func storyboardPromptTemplateFromRequest(req StoryboardPromptTemplateRequest, template model.StoryboardPromptTemplate) (model.StoryboardPromptTemplate, error) {
|
||||
name := strings.TrimSpace(req.Name)
|
||||
content := strings.TrimSpace(req.Content)
|
||||
if name == "" {
|
||||
return template, BadAuthRequest("请填写提示词名称")
|
||||
// 兼容历史模板中强制真人媒介的冲突规则;项目画风才是视觉媒介的唯一来源。
|
||||
func removeFixedMediaRestrictions(template string) string {
|
||||
lines := strings.Split(template, "\n")
|
||||
filtered := lines[:0]
|
||||
for _, line := range lines {
|
||||
mentionsPrompt := strings.Contains(line, "visualPrompt") || strings.Contains(line, "videoPrompt")
|
||||
forbidsNonLiveAction := strings.Contains(line, "不要") || strings.Contains(line, "禁止") || strings.Contains(line, "不得")
|
||||
mentionsAnimation := strings.Contains(line, "3D") || strings.Contains(line, "三维") || strings.Contains(line, "动画") || strings.Contains(line, "卡通") || strings.Contains(line, "游戏CG")
|
||||
forcesLiveAction := strings.Contains(line, "默认使用真实电影") || strings.Contains(line, "统一使用真实电影") || strings.Contains(line, "必须使用真实电影")
|
||||
if (mentionsPrompt && forbidsNonLiveAction && mentionsAnimation) || forcesLiveAction {
|
||||
continue
|
||||
}
|
||||
filtered = append(filtered, line)
|
||||
}
|
||||
if content == "" {
|
||||
return template, BadAuthRequest("请填写提示词内容")
|
||||
}
|
||||
template.Name = name
|
||||
template.Content = content
|
||||
if req.Enabled != nil {
|
||||
template.Enabled = *req.Enabled
|
||||
}
|
||||
return template, nil
|
||||
}
|
||||
|
||||
func renderStoryboardPromptTemplate(template string, brief string, requirements string, assets []storyboardAsset) string {
|
||||
assetJSON, _ := json.MarshalIndent(assets, "", " ")
|
||||
replacer := strings.NewReplacer(
|
||||
"{{剧情}}", strings.TrimSpace(brief),
|
||||
"{{用户brief}}", strings.TrimSpace(brief),
|
||||
"{{用户要求}}", strings.TrimSpace(requirements),
|
||||
"{{画布资产}}", string(assetJSON),
|
||||
"{{获取当前画布资产}}", string(assetJSON),
|
||||
)
|
||||
return replacer.Replace(template)
|
||||
return strings.Join(filtered, "\n")
|
||||
}
|
||||
|
||||
func defaultStoryboardPromptTemplate() string {
|
||||
codeFence := "```"
|
||||
return `你是影视分镜导演和 AI 视频提示词专家。
|
||||
请根据用户剧情 brief 和当前画布资产标签,生成可直接用于 AI 视频生成的分镜 JSON。
|
||||
return `你是影视分镜导演和 AI 视频提示词专家。先理解故事目标、人物动机、冲突、情绪曲线和结尾,再把剧情转译为可执行、可拍摄的连续镜头,不要把剧情段落直接改写成镜头摘要。
|
||||
|
||||
创作方法参考成熟分镜提示词实践:
|
||||
- 先理解故事目标、人物动机、冲突、情绪曲线和结尾。
|
||||
- 每个镜头都要明确主体、动作、场景、景别、构图、镜头焦段、机位、运镜、光线、色彩、质感、时长感和转场。
|
||||
- durationSeconds 必须是 1 到 60 的整数;没有台词、旁白或音效时返回空字符串。
|
||||
- 需要保持角色、服装、道具、空间和风格一致;能复用画布资产时,在 assetTags 中引用对应标签。
|
||||
- characters 必须逐角色输出,格式固定为“角色名:剧情定位、外貌、服装、体态、道具、性格动机和跨镜头一致性约束”,不要把多个角色合并成一项。
|
||||
- videoPrompt 必须是完整中文视频生成提示词,不要只写关键词。
|
||||
- videoPrompt 必须包含镜头时长、开始画面、中段变化、结尾画面、摄影机运动、主体运动、环境运动、可信光源、色彩基调、真实摄影质感和负面视觉约束。
|
||||
- 优先使用真实电影机语言:低机位/平视/俯拍、焦段、景深、自然曝光、空气介质、胶片颗粒、真实高光滚降。
|
||||
- 不要把主体完整居中平铺;需要有前景/中景/远景和人、物、空间尺度参照。
|
||||
- 不要在 visualPrompt 或 videoPrompt 中使用 3D动漫、动画、二次元、游戏CG、游戏截图、角色原画、概念设计图、手办感、卡通渲染、插画风等媒介限定词。
|
||||
- assetTags 只能引用当前画布资产里已有的标签或标签值;没有可复用资产时返回空数组。
|
||||
每个镜头先确定 narrativeIntent、viewerPOV 和 performanceBlocking,再选择景别、机位、焦段、构图和运镜。摄影机设计必须说明叙事动机,不得用术语数量代替导演判断;一个镜头只保留一个主运镜,必须有起点、动机和停止点,并为人物表演让出注意力。
|
||||
|
||||
用户brief:
|
||||
` + codeFence + `
|
||||
{{剧情}}
|
||||
` + codeFence + `
|
||||
大远景只承担空间、规模和处境建立;人物对白与喜剧反应优先使用中景、近景、过肩或独立反应镜头。按叙事需要使用反应、停顿、空镜、插入和匹配剪辑,避免每镜都推进、环绕、航拍或慢动作。
|
||||
|
||||
用户要求:
|
||||
` + codeFence + `
|
||||
{{用户要求}}
|
||||
` + codeFence + `
|
||||
description 只写可见画面与动作;把“意识到、回忆起、感到”等不可见信息转译成眼神、停顿、手部动作、走位、道具反应或环境变化。visualPrompt 明确人物左右位置、视线、前中后景、遮挡、视觉焦点、可信光源、材质和色彩;videoPrompt 只补充主体运动、环境运动和结尾状态。
|
||||
|
||||
当前画布资产:{{画布资产}}
|
||||
保持角色五官、年龄、发型、服装、道具、损伤位置、空间关系和项目媒介一致。视觉媒介、光线和材质严格服从项目画风,不得默认改写为真人摄影,也不得把 3D、动画或卡通项目强制改成写实。负面要求必须针对当前镜头的换脸、服装变化、手部错误、乱码、闪烁、穿模、风格突变和动作僵硬风险。`
|
||||
}
|
||||
|
||||
格式:
|
||||
{"title":"项目标题","logline":"一句话故事","styleGuide":"整体摄影、光线、色彩、质感和一致性规则","characters":["张三:男主角,28岁,短黑发,深灰夹克,清瘦挺拔,随身旧怀表,克制果断;所有镜头保持五官、发型、服装和怀表一致"],"locations":["场景与空间描述"],"shots":[{"title":"镜头标题","description":"可拍摄的主体动作、空间关系和可见结果","durationSeconds":8,"dialogue":"台词或旁白","shotSize":"中近景","emotion":"克制紧张","lightingAndAtmosphere":"侧逆光与薄雾","audioEffects":"环境风声","visualPrompt":"包含前中后景、构图、光线、材质和真实摄影质感的图片提示词","videoPrompt":"补充主体运动、环境运动、连续性、时长和转场逻辑","camera":"平视略低机位,50mm,中近景,前景遮挡,三分法构图","motion":"0-1秒固定,1-5秒缓慢推近,5-8秒轻微横移后停住","timeBeats":"0-1秒:建立画面;1-5秒:主体完成关键动作;5-8秒:反应和结尾停点","negativePrompt":"禁止换脸、服装变化、手部畸形、乱码、闪烁、风格突变、动作僵硬","assetTags":["角色:张三"]}]}
|
||||
|
||||
特别注意:
|
||||
- 只返回 JSON,不要 markdown,不要解释。
|
||||
- shots 输出 3 到 8 个;如果后续强制电影化质量契约指定了镜头数量,以契约为准。`
|
||||
// 保留为纯函数,供分镜校验测试验证受保护契约不会被模板或用户定制覆盖。
|
||||
func storyboardCinematicQualityContract(shotDuration int, shotCount int) string {
|
||||
values := storyboardPromptValues("", "", nil, storyboardProjectStyle{}, nil, shotDuration, shotCount)
|
||||
return defaultStoryboardPromptTemplate() + "\n\n" + storyboardExecutionContract(values["单镜头时长规则"], values["镜头数量规则"])
|
||||
}
|
||||
@@ -17,7 +17,7 @@ func TestStoryboardCinematicQualityContractIncludesRequestedCountAndDuration(t *
|
||||
|
||||
func TestStoryboardCinematicQualityContractIncludesCameraLanguageGuide(t *testing.T) {
|
||||
contract := storyboardCinematicQualityContract(0, 0)
|
||||
for _, term := range []string{"S01 大远景 ELS", "A05 倾斜角 Dutch Angle", "M15 希区柯克变焦", "C06 前景叠层", "N02 反应镜头", "禁止每 3-5 秒更换一次运镜"} {
|
||||
for _, term := range []string{"先确定观众此刻跟随谁", "大远景只承担空间", "一个镜头只保留一个主运镜", "明确人物左右位置", "避免每镜都推进"} {
|
||||
if !strings.Contains(contract, term) {
|
||||
t.Fatalf("camera language guide is missing %q: %s", term, contract)
|
||||
}
|
||||
@@ -32,9 +32,22 @@ func TestStoryboardPromptsLeaveAspectRatioToVideoNode(t *testing.T) {
|
||||
if strings.Contains(defaultStoryboardPromptTemplate(), "2.39:1") {
|
||||
t.Fatal("default storyboard prompt still hard-codes 2.39:1")
|
||||
}
|
||||
plan := fallbackAgentStoryboardPlan("测试故事")
|
||||
if strings.Contains(plan.StyleGuide+plan.Shots[0].VideoPrompt, "2.39:1") || strings.Contains(plan.StyleGuide+plan.Shots[0].VideoPrompt, "画幅") {
|
||||
t.Fatal("fallback storyboard still mentions output aspect ratio")
|
||||
prompt := buildStoryboardVideoPrompt("【风格组合】仙侠·宏大叙事·3D 动漫", "高清、明亮、空间层次清楚", agentStoryboardShot{
|
||||
Duration: 8, Description: "剑仙踏云进入画面", VisualPrompt: "剑仙位于画面左侧", VideoPrompt: "向前踏出一步后停住", Camera: "平视中景", Motion: "缓慢推近后停住", TimeBeats: "0-4秒:进入;4-8秒:停住",
|
||||
})
|
||||
if strings.Contains(prompt, "2.39:1") || strings.Contains(prompt, "画幅") {
|
||||
t.Fatal("compiled storyboard video prompt still mentions output aspect ratio")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveFixedMediaRestrictionsKeepsCurrentProjectMedium(t *testing.T) {
|
||||
template := "保留这一行\n- 不要在 visualPrompt 或 videoPrompt 中使用 3D动漫、动画、二次元、游戏CG、卡通渲染。\n继续保留"
|
||||
filtered := removeFixedMediaRestrictions(template)
|
||||
if strings.Contains(filtered, "不要在 visualPrompt") {
|
||||
t.Fatal("fixed 3D/cartoon restriction was not removed")
|
||||
}
|
||||
if !strings.Contains(filtered, "保留这一行") || !strings.Contains(filtered, "继续保留") {
|
||||
t.Fatal("unrelated template content was removed")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,3 +63,32 @@ func TestValidateStoryboardShotCount(t *testing.T) {
|
||||
t.Fatalf("expected automatic shot count to pass: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateStoryboardComplexityRejectsOverloadedShot(t *testing.T) {
|
||||
plan := agentStoryboardPlan{Shots: []agentStoryboardShot{{
|
||||
Duration: 5, CharacterIDs: []string{"a", "b", "c"}, MustHave: []string{"一", "二", "三", "四"}, TimeBeats: "0-1秒:起,1-2秒:承,2-4秒:转,4-5秒:合", Motion: "航拍推进并升降",
|
||||
}}}
|
||||
if err := validateStoryboardComplexity(plan); err == nil {
|
||||
t.Fatal("expected overloaded shot to fail complexity validation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateStoryboardComplexityAcceptsSingleDirectedMove(t *testing.T) {
|
||||
plan := agentStoryboardPlan{Shots: []agentStoryboardShot{{
|
||||
Duration: 8, CharacterIDs: []string{"a", "b"}, MustHave: []string{"身份稳定", "动作落点", "结尾状态"}, TimeBeats: "0-2秒:建立;2-6秒:动作;6-8秒:反应", Motion: "缓慢推近后停住", Dialogue: "你终于来了。",
|
||||
}}}
|
||||
if err := validateStoryboardComplexity(plan); err != nil {
|
||||
t.Fatalf("expected focused shot to pass complexity validation: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchStoryboardAssetsForShotPrioritizesCurrentCharacterCards(t *testing.T) {
|
||||
assets := []storyboardAsset{
|
||||
{ID: "environment", Title: "天宫", Tags: []string{"场景:天宫"}},
|
||||
{ID: "character-node", Title: "青莲剑仙", CharacterAssetID: "character-1", CharacterVersionID: "version-3"},
|
||||
}
|
||||
matched := matchStoryboardAssetsForShot(assets, agentStoryboardShot{CharacterIDs: []string{"character-1"}, AssetTags: []string{"场景:天宫"}})
|
||||
if len(matched) != 2 || matched[0].ID != "character-node" {
|
||||
t.Fatalf("expected current character card before optional tagged assets: %#v", matched)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user