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:
@@ -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{}{}
|
||||
|
||||
Reference in New Issue
Block a user