This commit is contained in:
2026-08-23 16:34:50 +02:00
parent 815e26d359
commit a30b78c3b6
8 changed files with 178 additions and 33 deletions
@@ -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