fix
This commit is contained in:
@@ -47,6 +47,46 @@ func AppendFormulaConstraints(userTpl string, titleTemplate, descriptionTemplate
|
||||
return userTpl + "\n\n" + joined.String()
|
||||
}
|
||||
|
||||
// FormatTitleSectionConstraint turns free-form --- Title --- section instructions
|
||||
// into a required enhance constraint when no structured title_template elements exist.
|
||||
func FormatTitleSectionConstraint(categoryPrompt string) string {
|
||||
body := strings.TrimSpace(aiprompts.TitleSectionInstructions(categoryPrompt))
|
||||
if body == "" || isBuiltInTitleRoleBoilerplate(body) {
|
||||
return ""
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("Title instructions (REQUIRED — overrides any shorter \"short retail title\" rule):\n")
|
||||
b.WriteString(body)
|
||||
b.WriteString("\nBuild JSON \"name\" exactly per these instructions in {{language}}.")
|
||||
b.WriteString(" Never copy the supplier Name unchanged when instructions ask for a new name or formula.")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func isBuiltInTitleRoleBoilerplate(body string) bool {
|
||||
lower := strings.ToLower(strings.TrimSpace(body))
|
||||
return strings.HasPrefix(lower, "role: title")
|
||||
}
|
||||
|
||||
// AppendTitleSectionConstraint appends FormatTitleSectionConstraint when the
|
||||
// category Title role has text and structured title_template is empty/unusable.
|
||||
func AppendTitleSectionConstraint(userTpl, categoryPrompt string, titleTemplate any) string {
|
||||
if FormatTitleFormulaConstraint(titleTemplate) != "" {
|
||||
return strings.TrimSpace(userTpl)
|
||||
}
|
||||
block := FormatTitleSectionConstraint(categoryPrompt)
|
||||
if block == "" {
|
||||
return strings.TrimSpace(userTpl)
|
||||
}
|
||||
userTpl = strings.TrimSpace(userTpl)
|
||||
if strings.Contains(userTpl, "Title instructions (REQUIRED") {
|
||||
return userTpl
|
||||
}
|
||||
if userTpl == "" {
|
||||
return block
|
||||
}
|
||||
return userTpl + "\n\n" + block
|
||||
}
|
||||
|
||||
// MaxAttrAllowlistPromptKeys caps Allowed attribute keys listed in enhance prompts.
|
||||
const MaxAttrAllowlistPromptKeys = 40
|
||||
|
||||
|
||||
@@ -29,6 +29,8 @@ func resolveProductPromptTemplates(in ProductInput) (systemTpl, userTpl string)
|
||||
descTpl := aiprompts.EffectiveDescriptionTemplateAny(in.DescriptionTemplate, catPrompt)
|
||||
// Category formulas are language-agnostic; inject once into the shared user skeleton.
|
||||
userTpl = AppendFormulaConstraints(userTpl, in.TitleTemplate, descTpl, in.OmitSEOMeta)
|
||||
// Free-form --- Title --- instructions when structured title_template is empty.
|
||||
userTpl = AppendTitleSectionConstraint(userTpl, catPrompt, in.TitleTemplate)
|
||||
// Category attribute allowlist + title-formula keys guide JSON "attrs" extraction.
|
||||
allowed := enhanceAllowedAttrKeys(in, in.CategoryUniqueID)
|
||||
userTpl = AppendAttributeConstraints(userTpl, allowed, in.TitleTemplate)
|
||||
|
||||
@@ -113,6 +113,8 @@ func TestResolveProductPromptTemplates_derivesFormulaFromPromptHTML(t *testing.T
|
||||
aiprompts.SectionDescriptionStart + "\n" +
|
||||
`<H2>{Napiši Novo ime izdelka in izpostavi en benefit}</H2>` +
|
||||
`<p>{Napiši odstavek, ki je dolg 100 besed.}</p>` +
|
||||
`<H2>{Izpostavi en benefit}</H2>` +
|
||||
`<p>{Napiši odstavek z tipom izdelka lowercase.}</p>` +
|
||||
`<b>{Tehnične specifikacije}</b><ul><li>{Napiši 3-10 tehničnih specifikacij}</li></ul>` + "\n" +
|
||||
aiprompts.SectionDescriptionEnd
|
||||
sys, user := resolveProductPromptTemplates(ProductInput{
|
||||
@@ -133,9 +135,22 @@ func TestResolveProductPromptTemplates_derivesFormulaFromPromptHTML(t *testing.T
|
||||
if !strings.Contains(sys, `"name" MUST be rewritten`) {
|
||||
t.Fatalf("missing title rewrite system override:\n%s", sys)
|
||||
}
|
||||
if descriptionSatisfiesFormula("plain Slovenian prose without tags", aiprompts.EffectiveDescriptionTemplateAny(nil, prompt)) {
|
||||
if !strings.Contains(user, "Title instructions (REQUIRED") {
|
||||
t.Fatalf("missing Title section constraint:\n%s", user)
|
||||
}
|
||||
effective := aiprompts.EffectiveDescriptionTemplateAny(nil, prompt)
|
||||
if descriptionSatisfiesFormula("plain Slovenian prose without tags", effective) {
|
||||
t.Fatal("plain prose must fail derived formula gate")
|
||||
}
|
||||
// One h2 is not enough when formula asks for two.
|
||||
thin := "<h2>A</h2><p>One</p><p>Two</p><ul><li>x</li></ul>"
|
||||
if descriptionSatisfiesFormula(thin, effective) {
|
||||
t.Fatal("under-counted h2 must fail formula gate")
|
||||
}
|
||||
ok := "<h2>A</h2><p>One</p><h2>B</h2><p>Two</p><ul><li>x</li></ul>"
|
||||
if !descriptionSatisfiesFormula(ok, effective) {
|
||||
t.Fatal("full section count must pass formula gate")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveProductPromptTemplates_fallsBackToCompany(t *testing.T) {
|
||||
|
||||
@@ -278,6 +278,9 @@ func (e *Engine) RunSteps(ctx context.Context, companyID string, in ProductInput
|
||||
catPrompt = categoryEnhancePromptFor(in.CategoryPromptsByLang, out.Category, lang, primary)
|
||||
}
|
||||
titleTpl, descTpl := categoryFormulasFor(in, out.Category)
|
||||
if effective := aiprompts.EffectiveDescriptionTemplateAny(descTpl, catPrompt); effective != nil {
|
||||
descTpl = effective
|
||||
}
|
||||
priorFields := company.FieldsForLanguage(in.PriorLocalized, lang)
|
||||
priorHash := priorFields.EnhanceInputHash
|
||||
priorName := priorFields.ProcessedName
|
||||
|
||||
Reference in New Issue
Block a user