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