fix: Enhance Kol Variable Rendering and Management

- Updated the regex for placeholder matching to support more flexible key formats.
- Refactored variable storage to allow both ID and key lookups in rendering.
- Improved schema validation to check for duplicate keys and enforce non-empty keys.
- Added new utility functions for variable value lookup and display name generation.
- Enhanced tests to cover new key-based variable scenarios.
- Refactored KolPrompt repository to simplify prompt storage and management, including the removal of obsolete revision handling.
- Introduced new API endpoints for saving, activating, and archiving prompts.
- Added new utility functions for handling Kol placeholders and platform options in the admin web.
- Implemented database migrations to simplify prompt storage structure and ensure data integrity.
This commit is contained in:
2026-04-18 00:47:57 +08:00
parent 614ca4a2ea
commit 3ef0807456
32 changed files with 2414 additions and 3518 deletions
@@ -99,16 +99,16 @@ func (s *KolGenerationService) WithCache(c sharedcache.Cache) *KolGenerationServ
}
func (s *KolGenerationService) GetSchema(ctx context.Context, actor auth.Actor, subPromptID int64) (*KolSubscriptionPromptSchemaResponse, error) {
row, revision, err := s.loadAuthorizedRevision(ctx, actor, subPromptID)
row, prompt, err := s.loadAuthorizedPrompt(ctx, actor, subPromptID)
if err != nil {
return nil, err
}
schema, err := decodeKolSchema(revision.SchemaJSON)
schema, err := decodeKolSchema(prompt.SchemaJSON)
if err != nil {
return nil, response.ErrInternal(50096, "kol_generation_schema_decode_failed", err.Error())
}
cardConfig, err := decodeKolCardConfig(revision.CardConfigJSON)
cardConfig, err := decodeKolCardConfig(prompt.CardConfigJSON)
if err != nil {
return nil, response.ErrInternal(50097, "kol_generation_card_config_decode_failed", err.Error())
}
@@ -126,7 +126,7 @@ func (s *KolGenerationService) GetSchema(ctx context.Context, actor auth.Actor,
}
func (s *KolGenerationService) Submit(ctx context.Context, actor auth.Actor, subPromptID int64, variables map[string]any) (*KolGenerationSubmitResponse, error) {
row, revision, err := s.loadAuthorizedRevision(ctx, actor, subPromptID)
row, prompt, err := s.loadAuthorizedPrompt(ctx, actor, subPromptID)
if err != nil {
return nil, err
}
@@ -135,12 +135,16 @@ func (s *KolGenerationService) Submit(ctx context.Context, actor auth.Actor, sub
variables = map[string]any{}
}
schema, err := decodeKolSchema(revision.SchemaJSON)
schema, err := decodeKolSchema(prompt.SchemaJSON)
if err != nil {
return nil, response.ErrInternal(50096, "kol_generation_schema_decode_failed", err.Error())
}
content, err := s.asset.Get(ctx, revision.PromptAssetKey)
if prompt.PromptAssetKey == nil || *prompt.PromptAssetKey == "" {
return nil, response.ErrInternal(50098, "kol_generation_prompt_asset_missing", "published prompt content not found")
}
content, err := s.asset.Get(ctx, *prompt.PromptAssetKey)
if err != nil {
return nil, response.ErrInternal(50098, "kol_generation_prompt_asset_load_failed", err.Error())
}
@@ -150,6 +154,10 @@ func (s *KolGenerationService) Submit(ctx context.Context, actor auth.Actor, sub
return nil, response.ErrBadRequest(40071, "kol_generation_variables_invalid", err.Error())
}
renderedPromptHash := HashPrompt(renderedPrompt)
promptRevisionNo := 1
if row.PublishedRevisionNo != nil && *row.PublishedRevisionNo > 0 {
promptRevisionNo = *row.PublishedRevisionNo
}
balance, err := s.quotaRepo.GetCurrentBalance(ctx, actor.TenantID, "article_generation")
if err != nil || balance < 1 {
@@ -161,10 +169,10 @@ func (s *KolGenerationService) Submit(ctx context.Context, actor auth.Actor, sub
SubscriptionID: row.SubscriptionID,
PackageID: row.PackageID,
PromptID: row.PromptID,
PromptRevisionNo: *row.PublishedRevisionNo,
PromptRevisionNo: promptRevisionNo,
Variables: variables,
SchemaSnapshot: json.RawMessage(revision.SchemaJSON),
CardConfigSnapshot: json.RawMessage(revision.CardConfigJSON),
SchemaSnapshot: json.RawMessage(prompt.SchemaJSON),
CardConfigSnapshot: json.RawMessage(prompt.CardConfigJSON),
RenderedPromptHash: renderedPromptHash,
RenderedPrompt: renderedPrompt,
})
@@ -259,10 +267,10 @@ func (s *KolGenerationService) Submit(ctx context.Context, actor auth.Actor, sub
SubscriptionPromptID: &row.ID,
PackageID: row.PackageID,
PromptID: row.PromptID,
PromptRevisionNo: *row.PublishedRevisionNo,
PromptRevisionNo: promptRevisionNo,
GenerationTaskID: &taskID,
VariablesSnapshot: variablesJSON,
SchemaSnapshot: revision.SchemaJSON,
SchemaSnapshot: prompt.SchemaJSON,
RenderedPromptHash: &renderedPromptHash,
})
if err != nil {
@@ -300,7 +308,7 @@ func (s *KolGenerationService) Submit(ctx context.Context, actor auth.Actor, sub
}, nil
}
func (s *KolGenerationService) loadAuthorizedRevision(ctx context.Context, actor auth.Actor, subPromptID int64) (*repository.KolSubscriptionPrompt, *repository.KolPromptRevision, error) {
func (s *KolGenerationService) loadAuthorizedPrompt(ctx context.Context, actor auth.Actor, subPromptID int64) (*repository.KolSubscriptionPrompt, *repository.KolPrompt, error) {
row, err := s.subRepo.GetSubscriptionPromptByID(ctx, actor.TenantID, subPromptID)
if err != nil {
return nil, nil, response.ErrInternal(50109, "kol_subscription_prompt_query_failed", err.Error())
@@ -319,19 +327,19 @@ func (s *KolGenerationService) loadAuthorizedRevision(ctx context.Context, actor
return nil, nil, ErrKolGenerationForbidden
case row.PackageStatus != "published":
return nil, nil, ErrKolGenerationForbidden
case row.PromptStatus != "active" || row.PublishedRevisionNo == nil:
case row.PromptStatus != "active":
return nil, nil, ErrKolGenerationForbidden
}
revision, err := s.promptRepo.GetRevision(ctx, row.CreatorTenantID, row.PromptID, *row.PublishedRevisionNo)
prompt, err := s.promptRepo.GetByID(ctx, row.CreatorTenantID, row.PromptID)
if err != nil {
return nil, nil, response.ErrInternal(50110, "kol_prompt_revision_query_failed", err.Error())
return nil, nil, response.ErrInternal(50110, "kol_prompt_query_failed", err.Error())
}
if revision == nil {
return nil, nil, response.ErrInternal(50111, "kol_prompt_revision_missing", "published prompt revision not found")
if prompt == nil || prompt.PromptAssetKey == nil || *prompt.PromptAssetKey == "" {
return nil, nil, response.ErrInternal(50111, "kol_prompt_content_missing", "published prompt content not found")
}
return row, revision, nil
return row, prompt, nil
}
func HandleKolGenerationTaskFailure(
+212 -103
View File
@@ -15,11 +15,11 @@ import (
)
var (
errKolPackageNotFound = response.ErrNotFound(40461, "kol_package_not_found", "package not found")
ErrPackageNotOwned = response.ErrForbidden(40361, "kol_package_not_owned", "package is not owned by current KOL")
ErrPromptNotFound = response.ErrNotFound(40462, "kol_prompt_not_found", "prompt not found")
ErrPromptNotOwned = response.ErrForbidden(40362, "kol_prompt_not_owned", "prompt is not owned by current KOL")
errKolPromptDraftEmpty = response.ErrConflict(40961, "kol_prompt_draft_missing", "no draft revision to publish")
errKolPackageNotFound = response.ErrNotFound(40461, "kol_package_not_found", "package not found")
ErrPackageNotOwned = response.ErrForbidden(40361, "kol_package_not_owned", "package is not owned by current KOL")
ErrPromptNotFound = response.ErrNotFound(40462, "kol_prompt_not_found", "prompt not found")
ErrPromptNotOwned = response.ErrForbidden(40362, "kol_prompt_not_owned", "prompt is not owned by current KOL")
errKolPromptContentEmpty = response.ErrConflict(40961, "kol_prompt_content_missing", "prompt content is required before publish or activate")
)
type CreatePromptInput struct {
@@ -34,11 +34,13 @@ type UpdateKolPromptInput struct {
SortOrder *int `json:"sort_order"`
}
type SaveDraftInput struct {
PromptID int64 `json:"-"`
Content string `json:"content" binding:"required"`
Schema KolSchemaJSON `json:"schema" binding:"required"`
CardConfig map[string]interface{} `json:"card_config"`
type PublishPromptInput struct {
Name string `json:"name" binding:"required"`
PlatformHint *string `json:"platform_hint"`
SortOrder *int `json:"sort_order"`
Content string `json:"content" binding:"required"`
Schema KolSchemaJSON `json:"schema" binding:"required"`
CardConfig map[string]interface{} `json:"card_config"`
}
type KolPromptDetailResponse struct {
@@ -57,13 +59,6 @@ type KolPromptDetailResponse struct {
UpdatedAt time.Time `json:"updated_at"`
}
type KolPromptRevisionResponse struct {
ID int64 `json:"id"`
PromptID int64 `json:"prompt_id"`
RevisionNo int `json:"revision_no"`
CreatedAt time.Time `json:"created_at"`
}
type KolPromptService struct {
pool *pgxpool.Pool
profileSvc *KolProfileService
@@ -184,7 +179,16 @@ func (s *KolPromptService) UpdateMetadata(ctx context.Context, actor auth.Actor,
return s.buildPromptDetail(ctx, updated, false)
}
func (s *KolPromptService) SaveDraft(ctx context.Context, actor auth.Actor, input SaveDraftInput) (*KolPromptRevisionResponse, error) {
func (s *KolPromptService) Save(ctx context.Context, actor auth.Actor, promptID int64, input PublishPromptInput) (*KolPromptDetailResponse, error) {
_, prompt, _, err := s.loadOwnedPrompt(ctx, actor, promptID)
if err != nil {
return nil, err
}
name := strings.TrimSpace(input.Name)
if name == "" {
return nil, response.ErrBadRequest(40063, "kol_prompt_name_required", "prompt name is required")
}
if strings.TrimSpace(input.Content) == "" {
return nil, response.ErrBadRequest(40064, "kol_prompt_content_required", "prompt content is required")
}
@@ -192,17 +196,86 @@ func (s *KolPromptService) SaveDraft(ctx context.Context, actor auth.Actor, inpu
return nil, response.ErrBadRequest(40065, "kol_prompt_schema_invalid", err.Error())
}
_, prompt, _, err := s.loadOwnedPrompt(ctx, actor, input.PromptID)
sortOrder := prompt.SortOrder
if input.SortOrder != nil {
sortOrder = *input.SortOrder
}
const currentRevisionNo = 1
assetKey, err := s.asset.Put(ctx, actor.TenantID, promptID, currentRevisionNo, input.Content)
if err != nil {
return nil, response.ErrInternal(50074, "kol_prompt_asset_store_failed", err.Error())
}
schemaJSON, err := JSONEncodeSchema(input.Schema)
if err != nil {
return nil, response.ErrBadRequest(40065, "kol_prompt_schema_invalid", err.Error())
}
cardConfigJSON, err := json.Marshal(normalizeKolCardConfig(input.CardConfig))
if err != nil {
return nil, response.ErrBadRequest(40066, "kol_prompt_card_config_invalid", "card_config must be valid json")
}
publishedRevisionNo := prompt.PublishedRevisionNo
if prompt.Status == "active" {
revNo := currentRevisionNo
publishedRevisionNo = &revNo
}
if err := s.promptRepo.Save(ctx, repository.SaveKolPromptInput{
ID: promptID,
TenantID: actor.TenantID,
Name: name,
PlatformHint: normalizeKolOptionalString(input.PlatformHint),
SortOrder: sortOrder,
PromptAssetKey: assetKey,
SchemaJSON: schemaJSON,
CardConfigJSON: cardConfigJSON,
Status: prompt.Status,
PublishedRevisionNo: publishedRevisionNo,
UpdatedBy: actor.UserID,
}); err != nil {
return nil, response.ErrInternal(50072, "kol_prompt_update_failed", err.Error())
}
updated, err := s.promptRepo.GetByID(ctx, actor.TenantID, promptID)
if err != nil {
return nil, response.ErrInternal(50071, "kol_prompt_query_failed", err.Error())
}
if updated == nil {
return nil, ErrPromptNotFound
}
return s.buildPromptDetail(ctx, updated, true)
}
func (s *KolPromptService) Publish(ctx context.Context, actor auth.Actor, promptID int64, input PublishPromptInput) (*KolPromptDetailResponse, error) {
_, prompt, pkg, err := s.loadOwnedPrompt(ctx, actor, promptID)
if err != nil {
return nil, err
}
nextRevisionNo, err := s.promptRepo.NextRevisionNo(ctx, actor.TenantID, input.PromptID)
if err != nil {
return nil, response.ErrInternal(50073, "kol_prompt_revision_query_failed", err.Error())
name := strings.TrimSpace(input.Name)
if name == "" {
return nil, response.ErrBadRequest(40063, "kol_prompt_name_required", "prompt name is required")
}
if strings.TrimSpace(input.Content) == "" {
return nil, response.ErrBadRequest(40064, "kol_prompt_content_required", "prompt content is required")
}
if err := ValidateSchema(input.Schema); err != nil {
return nil, response.ErrBadRequest(40065, "kol_prompt_schema_invalid", err.Error())
}
assetKey, err := s.asset.Put(ctx, actor.TenantID, input.PromptID, nextRevisionNo, input.Content)
sortOrder := prompt.SortOrder
if input.SortOrder != nil {
sortOrder = *input.SortOrder
}
const publishedRevisionNo = 1
assetKey, err := s.asset.Put(ctx, actor.TenantID, promptID, publishedRevisionNo, input.Content)
if err != nil {
return nil, response.ErrInternal(50074, "kol_prompt_asset_store_failed", err.Error())
}
@@ -219,82 +292,108 @@ func (s *KolPromptService) SaveDraft(ctx context.Context, actor auth.Actor, inpu
tx, err := s.pool.Begin(ctx)
if err != nil {
return nil, response.ErrInternal(50075, "kol_prompt_draft_tx_failed", "failed to begin prompt draft transaction")
}
defer tx.Rollback(ctx)
promptTx := repository.NewKolPromptRepository(tx)
revision, err := promptTx.CreateRevision(ctx, repository.CreateKolPromptRevisionInput{
TenantID: actor.TenantID,
PromptID: prompt.ID,
RevisionNo: nextRevisionNo,
PromptAssetKey: assetKey,
SchemaJSON: schemaJSON,
CardConfigJSON: cardConfigJSON,
CreatedBy: actor.UserID,
})
if err != nil {
return nil, response.ErrInternal(50076, "kol_prompt_draft_create_failed", err.Error())
}
if err := promptTx.SetDraftRevision(ctx, actor.TenantID, prompt.ID, nextRevisionNo, actor.UserID); err != nil {
return nil, response.ErrInternal(50077, "kol_prompt_draft_update_failed", err.Error())
}
if err := tx.Commit(ctx); err != nil {
return nil, response.ErrInternal(50078, "kol_prompt_draft_commit_failed", err.Error())
}
return &KolPromptRevisionResponse{
ID: revision.ID,
PromptID: revision.PromptID,
RevisionNo: revision.RevisionNo,
CreatedAt: revision.CreatedAt,
}, nil
}
func (s *KolPromptService) Publish(ctx context.Context, actor auth.Actor, promptID int64) error {
_, prompt, pkg, err := s.loadOwnedPrompt(ctx, actor, promptID)
if err != nil {
return err
}
if prompt.DraftRevisionNo == nil {
return errKolPromptDraftEmpty
}
tx, err := s.pool.Begin(ctx)
if err != nil {
return response.ErrInternal(50079, "kol_prompt_publish_tx_failed", "failed to begin prompt publish transaction")
return nil, response.ErrInternal(50079, "kol_prompt_publish_tx_failed", "failed to begin prompt publish transaction")
}
defer tx.Rollback(ctx)
promptTx := repository.NewKolPromptRepository(tx)
subTx := repository.NewKolSubscriptionRepository(tx)
if err := promptTx.SetPublishedRevision(ctx, actor.TenantID, promptID, *prompt.DraftRevisionNo, actor.UserID); err != nil {
return response.ErrInternal(50080, "kol_prompt_publish_failed", err.Error())
if err := promptTx.Publish(ctx, repository.PublishKolPromptInput{
ID: promptID,
TenantID: actor.TenantID,
Name: name,
PlatformHint: normalizeKolOptionalString(input.PlatformHint),
SortOrder: sortOrder,
PromptAssetKey: assetKey,
SchemaJSON: schemaJSON,
CardConfigJSON: cardConfigJSON,
PublishedRevisionNo: publishedRevisionNo,
UpdatedBy: actor.UserID,
}); err != nil {
return nil, response.ErrInternal(50080, "kol_prompt_publish_failed", err.Error())
}
subscribers, err := subTx.ListActiveSubscribersForPackage(ctx, pkg.ID)
if err != nil {
return response.ErrInternal(50083, "kol_subscription_query_failed", err.Error())
}
for _, sub := range subscribers {
if err := subTx.CreateAccessRow(ctx, repository.CreateAccessRowInput{
TenantID: sub.TenantID,
SubscriptionID: sub.ID,
PackageID: pkg.ID,
PromptID: promptID,
}); err != nil {
return response.ErrInternal(50084, "kol_subscription_access_create_failed", err.Error())
}
if err := s.grantPromptToActiveSubscribers(ctx, subTx, pkg.ID, promptID); err != nil {
return nil, err
}
if err := tx.Commit(ctx); err != nil {
return response.ErrInternal(50081, "kol_prompt_publish_commit_failed", err.Error())
return nil, response.ErrInternal(50081, "kol_prompt_publish_commit_failed", err.Error())
}
return nil
updated, err := s.promptRepo.GetByID(ctx, actor.TenantID, promptID)
if err != nil {
return nil, response.ErrInternal(50071, "kol_prompt_query_failed", err.Error())
}
if updated == nil {
return nil, ErrPromptNotFound
}
return s.buildPromptDetail(ctx, updated, true)
}
func (s *KolPromptService) Activate(ctx context.Context, actor auth.Actor, promptID int64) (*KolPromptDetailResponse, error) {
_, prompt, pkg, err := s.loadOwnedPrompt(ctx, actor, promptID)
if err != nil {
return nil, err
}
tx, err := s.pool.Begin(ctx)
if err != nil {
return nil, response.ErrInternal(50088, "kol_prompt_activate_tx_failed", "failed to begin prompt activate transaction")
}
defer tx.Rollback(ctx)
promptTx := repository.NewKolPromptRepository(tx)
subTx := repository.NewKolSubscriptionRepository(tx)
switch {
case prompt.PromptAssetKey != nil && strings.TrimSpace(*prompt.PromptAssetKey) != "":
if err := promptTx.SetPublishedRevision(ctx, actor.TenantID, promptID, 1, actor.UserID); err != nil {
return nil, response.ErrInternal(50090, "kol_prompt_activate_failed", err.Error())
}
default:
return nil, errKolPromptContentEmpty
}
if err := s.grantPromptToActiveSubscribers(ctx, subTx, pkg.ID, promptID); err != nil {
return nil, err
}
if err := tx.Commit(ctx); err != nil {
return nil, response.ErrInternal(50091, "kol_prompt_activate_commit_failed", "failed to commit prompt activate transaction")
}
updated, err := s.promptRepo.GetByID(ctx, actor.TenantID, promptID)
if err != nil {
return nil, response.ErrInternal(50071, "kol_prompt_query_failed", err.Error())
}
if updated == nil {
return nil, ErrPromptNotFound
}
return s.buildPromptDetail(ctx, updated, false)
}
func (s *KolPromptService) Archive(ctx context.Context, actor auth.Actor, promptID int64) (*KolPromptDetailResponse, error) {
if _, _, _, err := s.loadOwnedPrompt(ctx, actor, promptID); err != nil {
return nil, err
}
if err := s.promptRepo.UpdateStatus(ctx, actor.TenantID, promptID, "archived", actor.UserID); err != nil {
return nil, response.ErrInternal(50092, "kol_prompt_archive_failed", err.Error())
}
updated, err := s.promptRepo.GetByID(ctx, actor.TenantID, promptID)
if err != nil {
return nil, response.ErrInternal(50071, "kol_prompt_query_failed", err.Error())
}
if updated == nil {
return nil, ErrPromptNotFound
}
return s.buildPromptDetail(ctx, updated, false)
}
func (s *KolPromptService) Delete(ctx context.Context, actor auth.Actor, promptID int64) error {
@@ -371,27 +470,15 @@ func (s *KolPromptService) buildPromptDetail(ctx context.Context, prompt *reposi
UpdatedAt: prompt.UpdatedAt,
}
revisionNo := prompt.DraftRevisionNo
if revisionNo == nil {
revisionNo = prompt.PublishedRevisionNo
}
if revisionNo == nil {
if len(prompt.SchemaJSON) == 0 && len(prompt.CardConfigJSON) == 0 && (prompt.PromptAssetKey == nil || strings.TrimSpace(*prompt.PromptAssetKey) == "") {
return detail, nil
}
revision, err := s.promptRepo.GetRevision(ctx, prompt.TenantID, prompt.ID, *revisionNo)
if err != nil {
return nil, response.ErrInternal(50083, "kol_prompt_revision_query_failed", err.Error())
}
if revision == nil {
return nil, response.ErrInternal(50084, "kol_prompt_revision_not_found", "prompt revision not found")
}
schema, err := decodeKolSchema(revision.SchemaJSON)
schema, err := decodeKolSchema(prompt.SchemaJSON)
if err != nil {
return nil, response.ErrInternal(50085, "kol_prompt_schema_decode_failed", err.Error())
}
cardConfig, err := decodeKolCardConfig(revision.CardConfigJSON)
cardConfig, err := decodeKolCardConfig(prompt.CardConfigJSON)
if err != nil {
return nil, response.ErrInternal(50086, "kol_prompt_card_config_decode_failed", err.Error())
}
@@ -399,8 +486,8 @@ func (s *KolPromptService) buildPromptDetail(ctx context.Context, prompt *reposi
detail.LatestSchemaJSON = schema
detail.LatestCardConfigJSON = cardConfig
if includeContent {
content, err := s.asset.Get(ctx, revision.PromptAssetKey)
if includeContent && prompt.PromptAssetKey != nil && strings.TrimSpace(*prompt.PromptAssetKey) != "" {
content, err := s.asset.Get(ctx, *prompt.PromptAssetKey)
if err != nil {
return nil, response.ErrInternal(50087, "kol_prompt_asset_load_failed", err.Error())
}
@@ -410,6 +497,28 @@ func (s *KolPromptService) buildPromptDetail(ctx context.Context, prompt *reposi
return detail, nil
}
func (s *KolPromptService) grantPromptToActiveSubscribers(
ctx context.Context,
subRepo repository.KolSubscriptionRepository,
packageID, promptID int64,
) error {
subscribers, err := subRepo.ListActiveSubscribersForPackage(ctx, packageID)
if err != nil {
return response.ErrInternal(50083, "kol_subscription_query_failed", err.Error())
}
for _, sub := range subscribers {
if err := subRepo.CreateAccessRow(ctx, repository.CreateAccessRowInput{
TenantID: sub.TenantID,
SubscriptionID: sub.ID,
PackageID: packageID,
PromptID: promptID,
}); err != nil {
return response.ErrInternal(50084, "kol_subscription_access_create_failed", err.Error())
}
}
return nil
}
func normalizeKolCardConfig(cardConfig map[string]interface{}) map[string]interface{} {
if cardConfig == nil {
return map[string]interface{}{}
@@ -33,14 +33,21 @@ type KolSchemaJSON struct {
Variables []KolVariableDefinition `json:"variables"`
}
var kolPlaceholderRE = regexp.MustCompile(`\{\{\s*([^}\s]+)\s*\}\}`)
var kolPlaceholderRE = regexp.MustCompile(`\{\{\s*([^{}]+?)\s*\}\}`)
func RenderPrompt(content string, schema KolSchemaJSON, values map[string]any) (string, error) {
schema = normalizeKolSchema(schema)
byID := make(map[string]KolVariableDefinition, len(schema.Variables))
byToken := make(map[string]KolVariableDefinition, len(schema.Variables)*2)
for _, variable := range schema.Variables {
byID[variable.ID] = variable
id := strings.TrimSpace(variable.ID)
key := strings.TrimSpace(variable.Key)
if id != "" {
byToken[id] = variable
}
if key != "" {
byToken[key] = variable
}
}
missingSet := make(map[string]struct{})
@@ -62,19 +69,19 @@ func RenderPrompt(content string, schema KolSchemaJSON, values map[string]any) (
return match
}
id := submatches[1]
variable, exists := byID[id]
token := strings.TrimSpace(submatches[1])
variable, exists := byToken[token]
if !exists {
addMissing(id)
addMissing(token)
return match
}
value, hasValue := values[id]
value, hasValue := lookupKolVariableValue(values, variable, token)
if hasValue {
return fmt.Sprint(value)
}
if variable.Required {
addMissing(id)
addMissing(kolVariableDisplayName(variable))
}
return ""
})
@@ -94,16 +101,26 @@ func HashPrompt(rendered string) string {
func ValidateSchema(schema KolSchemaJSON) error {
schema = normalizeKolSchema(schema)
seen := make(map[string]struct{}, len(schema.Variables))
seenIDs := make(map[string]struct{}, len(schema.Variables))
seenKeys := make(map[string]struct{}, len(schema.Variables))
for _, variable := range schema.Variables {
id := strings.TrimSpace(variable.ID)
if id == "" || !strings.HasPrefix(id, "var_") {
return fmt.Errorf("variable id must be non-empty and start with var_")
}
if _, exists := seen[id]; exists {
if _, exists := seenIDs[id]; exists {
return fmt.Errorf("duplicate variable id: %s", id)
}
seen[id] = struct{}{}
seenIDs[id] = struct{}{}
key := strings.TrimSpace(variable.Key)
if key == "" {
return fmt.Errorf("variable key must be non-empty")
}
if _, exists := seenKeys[key]; exists {
return fmt.Errorf("duplicate variable key: %s", key)
}
seenKeys[key] = struct{}{}
switch KolVariableType(strings.TrimSpace(string(variable.Type))) {
case KolVariableTypeInput, KolVariableTypeTextarea, KolVariableTypeSelect, KolVariableTypeNumber, KolVariableTypeCheckbox:
@@ -125,3 +142,38 @@ func normalizeKolSchema(schema KolSchemaJSON) KolSchemaJSON {
}
return schema
}
func lookupKolVariableValue(values map[string]any, variable KolVariableDefinition, token string) (any, bool) {
candidates := []string{
strings.TrimSpace(token),
strings.TrimSpace(variable.Key),
strings.TrimSpace(variable.ID),
}
seen := make(map[string]struct{}, len(candidates))
for _, candidate := range candidates {
if candidate == "" {
continue
}
if _, exists := seen[candidate]; exists {
continue
}
seen[candidate] = struct{}{}
if value, ok := values[candidate]; ok {
return value, true
}
}
return nil, false
}
func kolVariableDisplayName(variable KolVariableDefinition) string {
if label := strings.TrimSpace(variable.Label); label != "" {
return label
}
if key := strings.TrimSpace(variable.Key); key != "" {
return key
}
return strings.TrimSpace(variable.ID)
}
@@ -22,8 +22,8 @@ func TestKolVariableRenderPrompt(t *testing.T) {
content: "Hello {{var_a}} from {{var_b}}",
schema: KolSchemaJSON{
Variables: []KolVariableDefinition{
{ID: "var_a", Type: KolVariableTypeInput, Required: true},
{ID: "var_b", Type: KolVariableTypeSelect},
{ID: "var_a", Key: "brand", Type: KolVariableTypeInput, Required: true},
{ID: "var_b", Key: "industry", Type: KolVariableTypeSelect},
},
},
values: map[string]any{
@@ -32,12 +32,38 @@ func TestKolVariableRenderPrompt(t *testing.T) {
},
want: "Hello 宜家 from 家具",
},
{
name: "supports key placeholders and key values",
content: "围绕 {{目标词}} 写一篇文章,并突出 {{目标词}} 的优势",
schema: KolSchemaJSON{
Variables: []KolVariableDefinition{
{ID: "var_a", Key: "目标词", Label: "目标词", Type: KolVariableTypeInput, Required: true},
},
},
values: map[string]any{
"目标词": "北京装修公司",
},
want: "围绕 北京装修公司 写一篇文章,并突出 北京装修公司 的优势",
},
{
name: "supports key placeholders with id values",
content: "Hello {{brand}}",
schema: KolSchemaJSON{
Variables: []KolVariableDefinition{
{ID: "var_a", Key: "brand", Label: "Brand", Type: KolVariableTypeInput, Required: true},
},
},
values: map[string]any{
"var_a": "宜家",
},
want: "Hello 宜家",
},
{
name: "missing required",
content: "Hello {{var_required}}",
schema: KolSchemaJSON{
Variables: []KolVariableDefinition{
{ID: "var_required", Type: KolVariableTypeInput, Required: true},
{ID: "var_required", Key: "required", Label: "Required", Type: KolVariableTypeInput, Required: true},
},
},
values: map[string]any{},
@@ -75,8 +101,8 @@ func TestKolVariableValidateSchemaRejectsDuplicateID(t *testing.T) {
err := ValidateSchema(KolSchemaJSON{
Variables: []KolVariableDefinition{
{ID: "var_a", Type: KolVariableTypeInput},
{ID: "var_a", Type: KolVariableTypeSelect},
{ID: "var_a", Key: "first", Type: KolVariableTypeInput},
{ID: "var_a", Key: "second", Type: KolVariableTypeSelect},
},
})
@@ -89,10 +115,24 @@ func TestKolVariableValidateSchemaRejectsInvalidID(t *testing.T) {
err := ValidateSchema(KolSchemaJSON{
Variables: []KolVariableDefinition{
{ID: "bad_id", Type: KolVariableTypeInput},
{ID: "bad_id", Key: "bad", Type: KolVariableTypeInput},
},
})
require.Error(t, err)
require.Contains(t, err.Error(), "start with var_")
}
func TestKolVariableValidateSchemaRejectsDuplicateKey(t *testing.T) {
t.Parallel()
err := ValidateSchema(KolSchemaJSON{
Variables: []KolVariableDefinition{
{ID: "var_a", Key: "topic", Type: KolVariableTypeInput},
{ID: "var_b", Key: "topic", Type: KolVariableTypeSelect},
},
})
require.Error(t, err)
require.Contains(t, err.Error(), "duplicate variable key")
}