2026-08-17 00:39:25 +02:00
|
|
|
package aiprompts
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2026-10-14 20:28:38 +02:00
|
|
|
"fmt"
|
2026-08-17 00:39:25 +02:00
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
reLegacyHTMLBlock = regexp.MustCompile(`(?is)<\s*(h[1-4]|p|ul|ol|li|b|strong)\s*>\s*\{?\s*(.*?)\s*\}?\s*<\s*/\s*(?:h[1-4]|p|ul|ol|li|b|strong)\s*>`)
|
2026-10-14 19:23:16 +02:00
|
|
|
reLegacyList = regexp.MustCompile(`(?is)<\s*ul\s*>\s*<\s*li\s*>\s*\{?\s*(.*?)\s*\}?\s*<\s*/\s*li\s*>\s*<\s*/\s*ul\s*>`)
|
2026-08-17 00:39:25 +02:00
|
|
|
reLegacyBoldUL = regexp.MustCompile(`(?is)<\s*b\s*>\s*\{?\s*(.*?)\s*\}?\s*<\s*/\s*b\s*>\s*<\s*ul\s*>\s*<\s*li\s*>\s*\{?\s*(.*?)\s*\}?\s*<\s*/\s*li\s*>\s*<\s*/\s*ul\s*>`)
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-23 18:58:03 +02:00
|
|
|
// LegacyHTMLFormulaPresent reports whether s contains A1-style HTML formula blocks
|
|
|
|
|
// (<H2>{…}</H2>, <p>{…}</p>, …) used in category Description GPT predloga.
|
|
|
|
|
func LegacyHTMLFormulaPresent(s string) bool {
|
|
|
|
|
return reLegacyHTMLBlock.MatchString(s)
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 00:39:25 +02:00
|
|
|
// DescriptionFormulaSection is one ordered block in categories.description_template.
|
|
|
|
|
type DescriptionFormulaSection struct {
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
Instructions string `json:"instructions"`
|
2026-10-14 19:23:16 +02:00
|
|
|
Optional bool `json:"optional,omitempty"`
|
2026-08-17 00:39:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DescriptionFormula is the JSON shape stored in categories.description_template
|
|
|
|
|
// (sections + optional SEO meta instruction strings).
|
|
|
|
|
type DescriptionFormula struct {
|
|
|
|
|
Sections []DescriptionFormulaSection `json:"sections,omitempty"`
|
|
|
|
|
MetaTitle string `json:"metaTitle,omitempty"`
|
|
|
|
|
MetaDescription string `json:"metaDescription,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DescriptionTemplateNeedsRepair is true when template is missing or has no sections
|
|
|
|
|
// (and no usable meta instructions).
|
|
|
|
|
func DescriptionTemplateNeedsRepair(template any) bool {
|
|
|
|
|
f, ok := parseDescriptionFormula(template)
|
|
|
|
|
if !ok {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
if len(f.Sections) > 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return strings.TrimSpace(f.MetaTitle) == "" && strings.TrimSpace(f.MetaDescription) == ""
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-18 00:13:44 +02:00
|
|
|
// DefaultLegacyMetaTitleRule fills description_template.metaTitle for legacy A1
|
|
|
|
|
// prompts, which only carried a <metaDescription> instruction. Mirrors the legacy
|
|
|
|
|
// name-first Slovenian instruction style with the SEO title length bound.
|
|
|
|
|
const DefaultLegacyMetaTitleRule = "Najprej napiši Novo ime izdelka in dodaj glavno prednost. Dolžina 50-60 znakov vključno s presledki."
|
|
|
|
|
|
2026-08-23 20:49:40 +02:00
|
|
|
// DefaultMetaTitleRuleEN is the same instruction for the English platform-default
|
|
|
|
|
// formulas. Legacy dumps carry only a <metaDescription> rule, so the meta title
|
|
|
|
|
// instruction is supplied here in the language of the formula set.
|
|
|
|
|
const DefaultMetaTitleRuleEN = "Start with the New product name and add the main benefit. Length 50-60 characters including spaces."
|
|
|
|
|
|
2026-08-17 00:39:25 +02:00
|
|
|
// DeriveDescriptionFormulaFromLegacyParts builds description_template from split
|
2026-08-18 00:13:44 +02:00
|
|
|
// legacy DescriptionRules HTML + MetaRules. Legacy dumps never carried a meta
|
|
|
|
|
// title rule, so meta intent (MetaRules present) also seeds a default metaTitle
|
|
|
|
|
// instruction — the category UI then shows all four prompt areas filled.
|
2026-08-17 00:39:25 +02:00
|
|
|
func DeriveDescriptionFormulaFromLegacyParts(parts LegacyEnhanceParts) DescriptionFormula {
|
2026-10-14 19:23:16 +02:00
|
|
|
out := DescriptionFormula{MetaDescription: strings.TrimSpace(parts.MetaRules)}
|
2026-08-18 00:13:44 +02:00
|
|
|
if out.MetaDescription != "" {
|
2026-10-14 20:28:38 +02:00
|
|
|
if mt, md, ok := splitLocalizedMetaRules(out.MetaDescription); ok {
|
|
|
|
|
out.MetaTitle, out.MetaDescription = mt, md
|
|
|
|
|
} else {
|
|
|
|
|
out.MetaTitle = DefaultLegacyMetaTitleRule
|
2026-10-14 19:23:16 +02:00
|
|
|
}
|
2026-08-18 00:13:44 +02:00
|
|
|
}
|
2026-10-14 20:28:38 +02:00
|
|
|
|
2026-08-17 00:39:25 +02:00
|
|
|
body := strings.TrimSpace(parts.DescriptionRules)
|
|
|
|
|
if body == "" {
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Walk left-to-right so section order matches the legacy HTML template.
|
|
|
|
|
remaining := body
|
|
|
|
|
for remaining != "" {
|
|
|
|
|
boldUL := reLegacyBoldUL.FindStringSubmatchIndex(remaining)
|
2026-10-14 19:23:16 +02:00
|
|
|
list := reLegacyList.FindStringSubmatchIndex(remaining)
|
2026-08-17 00:39:25 +02:00
|
|
|
block := reLegacyHTMLBlock.FindStringSubmatchIndex(remaining)
|
2026-10-14 19:23:16 +02:00
|
|
|
if list != nil && (block == nil || list[0] <= block[0]) && (boldUL == nil || list[0] < boldUL[0]) {
|
|
|
|
|
m := reLegacyList.FindStringSubmatch(remaining[list[0]:list[1]])
|
|
|
|
|
if len(m) > 1 {
|
|
|
|
|
if instr := cleanLegacyInstruction(m[1]); instr != "" {
|
|
|
|
|
out.Sections = append(out.Sections, DescriptionFormulaSection{Type: "ul", Instructions: instr, Optional: true})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
remaining = remaining[list[1]:]
|
|
|
|
|
continue
|
|
|
|
|
}
|
2026-08-17 00:39:25 +02:00
|
|
|
switch {
|
|
|
|
|
case boldUL != nil && (block == nil || boldUL[0] <= block[0]):
|
|
|
|
|
m := reLegacyBoldUL.FindStringSubmatch(remaining[boldUL[0]:boldUL[1]])
|
|
|
|
|
heading, instr := "", ""
|
|
|
|
|
if len(m) >= 3 {
|
|
|
|
|
heading = cleanLegacyInstruction(m[1])
|
|
|
|
|
instr = cleanLegacyInstruction(m[2])
|
|
|
|
|
}
|
|
|
|
|
if heading != "" && instr != "" && !strings.Contains(strings.ToLower(instr), strings.ToLower(heading)) {
|
|
|
|
|
instr = heading + ": " + instr
|
|
|
|
|
} else if instr == "" {
|
|
|
|
|
instr = heading
|
|
|
|
|
}
|
|
|
|
|
if strings.TrimSpace(instr) != "" {
|
2026-10-14 19:23:16 +02:00
|
|
|
out.Sections = append(out.Sections, DescriptionFormulaSection{Type: "ul", Instructions: instr, Optional: true})
|
2026-08-17 00:39:25 +02:00
|
|
|
}
|
|
|
|
|
remaining = remaining[boldUL[1]:]
|
|
|
|
|
case block != nil:
|
|
|
|
|
m := reLegacyHTMLBlock.FindStringSubmatch(remaining[block[0]:block[1]])
|
|
|
|
|
if len(m) >= 3 {
|
|
|
|
|
typ := strings.ToLower(strings.TrimSpace(m[1]))
|
|
|
|
|
instr := cleanLegacyInstruction(m[2])
|
|
|
|
|
if instr != "" {
|
|
|
|
|
switch typ {
|
|
|
|
|
case "b", "strong":
|
|
|
|
|
typ = "h2"
|
|
|
|
|
case "li", "ol":
|
|
|
|
|
typ = "ul"
|
|
|
|
|
}
|
|
|
|
|
out.Sections = append(out.Sections, DescriptionFormulaSection{Type: typ, Instructions: instr})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
remaining = remaining[block[1]:]
|
|
|
|
|
default:
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-14 19:23:16 +02:00
|
|
|
// The modern product-page formula has an introduction and optional
|
|
|
|
|
// feature/specification blocks. If evidence is sparse, do not require the
|
|
|
|
|
// model to hallucinate headings or empty paragraphs.
|
|
|
|
|
if len(out.Sections) == 5 && out.Sections[0].Type == "p" &&
|
|
|
|
|
out.Sections[1].Type == "h2" && out.Sections[2].Type == "p" &&
|
|
|
|
|
out.Sections[3].Type == "h2" && out.Sections[4].Type == "ul" {
|
|
|
|
|
out.Sections[1].Optional = true
|
|
|
|
|
out.Sections[2].Optional = true
|
|
|
|
|
}
|
|
|
|
|
for i := 0; i+1 < len(out.Sections); i++ {
|
|
|
|
|
if out.Sections[i].Type == "h2" && out.Sections[i+1].Type == "ul" {
|
|
|
|
|
out.Sections[i].Optional = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-17 00:39:25 +02:00
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeriveDescriptionTemplateJSON encodes DeriveDescriptionFormulaFromLegacyParts for DB write.
|
|
|
|
|
func DeriveDescriptionTemplateJSON(parts LegacyEnhanceParts) string {
|
|
|
|
|
f := DeriveDescriptionFormulaFromLegacyParts(parts)
|
|
|
|
|
if len(f.Sections) == 0 && strings.TrimSpace(f.MetaTitle) == "" && strings.TrimSpace(f.MetaDescription) == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
b, err := json.Marshal(f)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return string(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseDescriptionFormula(template any) (DescriptionFormula, bool) {
|
|
|
|
|
if template == nil {
|
|
|
|
|
return DescriptionFormula{}, false
|
|
|
|
|
}
|
|
|
|
|
switch t := template.(type) {
|
|
|
|
|
case DescriptionFormula:
|
|
|
|
|
return t, true
|
|
|
|
|
case *DescriptionFormula:
|
|
|
|
|
if t == nil {
|
|
|
|
|
return DescriptionFormula{}, false
|
|
|
|
|
}
|
|
|
|
|
return *t, true
|
|
|
|
|
case []byte:
|
|
|
|
|
if len(t) == 0 {
|
|
|
|
|
return DescriptionFormula{}, false
|
|
|
|
|
}
|
|
|
|
|
var f DescriptionFormula
|
|
|
|
|
if err := json.Unmarshal(t, &f); err != nil {
|
|
|
|
|
return DescriptionFormula{}, false
|
|
|
|
|
}
|
|
|
|
|
return f, true
|
|
|
|
|
case string:
|
|
|
|
|
s := strings.TrimSpace(t)
|
|
|
|
|
if s == "" || s == "null" || s == "{}" {
|
|
|
|
|
return DescriptionFormula{}, false
|
|
|
|
|
}
|
|
|
|
|
var f DescriptionFormula
|
|
|
|
|
if err := json.Unmarshal([]byte(s), &f); err != nil {
|
|
|
|
|
return DescriptionFormula{}, false
|
|
|
|
|
}
|
|
|
|
|
return f, true
|
|
|
|
|
default:
|
|
|
|
|
b, err := json.Marshal(t)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return DescriptionFormula{}, false
|
|
|
|
|
}
|
|
|
|
|
var f DescriptionFormula
|
|
|
|
|
if err := json.Unmarshal(b, &f); err != nil {
|
|
|
|
|
return DescriptionFormula{}, false
|
|
|
|
|
}
|
|
|
|
|
return f, true
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-23 13:39:56 +02:00
|
|
|
|
2026-08-23 16:34:50 +02:00
|
|
|
// EffectiveDescriptionFormula merges structured description_template with the
|
|
|
|
|
// category enhance prompt Title/Description/Meta role sections.
|
|
|
|
|
//
|
|
|
|
|
// Precedence for HTML body sections:
|
|
|
|
|
// 1. Prompt Description HTML overlay when it yields more sections than stored
|
|
|
|
|
// (tenants author structure in /categories/.../prompt Section instructions).
|
|
|
|
|
// 2. Otherwise stored description_template.sections.
|
|
|
|
|
//
|
|
|
|
|
// Meta instructions: stored metaTitle/metaDescription win; else --- Meta --- body.
|
2026-08-23 13:39:56 +02:00
|
|
|
func EffectiveDescriptionFormula(stored any, categoryPrompt string) DescriptionFormula {
|
2026-08-23 16:34:50 +02:00
|
|
|
storedF, storedOK := parseDescriptionFormula(stored)
|
|
|
|
|
out := DescriptionFormula{}
|
|
|
|
|
if storedOK {
|
|
|
|
|
out = storedF
|
2026-08-23 13:39:56 +02:00
|
|
|
}
|
2026-08-23 16:34:50 +02:00
|
|
|
|
|
|
|
|
descBody := ExtractEnhanceSectionBody(categoryPrompt, SectionDescriptionStart, SectionDescriptionEnd)
|
|
|
|
|
if descBody == "" && !promptHasRoleMarkers(categoryPrompt) {
|
|
|
|
|
// Unsectioned prompt may still be a legacy HTML blob.
|
|
|
|
|
descBody = strings.TrimSpace(categoryPrompt)
|
|
|
|
|
}
|
2026-08-23 18:58:03 +02:00
|
|
|
if isBuiltInDescriptionRoleBoilerplate(descBody) {
|
|
|
|
|
descBody = ""
|
|
|
|
|
}
|
2026-08-23 16:34:50 +02:00
|
|
|
var derived DescriptionFormula
|
|
|
|
|
if descBody != "" && reLegacyHTMLBlock.MatchString(descBody) {
|
|
|
|
|
derived = DeriveDescriptionFormulaFromLegacyParts(LegacyEnhanceParts{
|
|
|
|
|
DescriptionRules: descBody,
|
|
|
|
|
WasLegacy: true,
|
|
|
|
|
})
|
2026-08-23 13:39:56 +02:00
|
|
|
}
|
2026-08-23 20:49:40 +02:00
|
|
|
// A legacy HTML formula in the category prompt is the tenant's own authored
|
|
|
|
|
// structure, so it wins outright — it is also what buildA1StyleEnhanceUserTemplate
|
|
|
|
|
// puts in the GPT predloga. Preferring it only when it has MORE sections let a
|
|
|
|
|
// stale stored template (e.g. seven English boilerplate sections from an old
|
|
|
|
|
// migration) validate against a five-section prompt: the model obeyed the prompt,
|
|
|
|
|
// the gate rejected the reply as formula-mismatch, and enhance fell back to synth
|
|
|
|
|
// or supplier copy. Instruction and gate must read the same formula.
|
|
|
|
|
if len(derived.Sections) > 0 {
|
2026-08-23 16:34:50 +02:00
|
|
|
out.Sections = derived.Sections
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metaBody := ExtractEnhanceSectionBody(categoryPrompt, SectionMetaStart, SectionMetaEnd)
|
2026-08-23 18:58:03 +02:00
|
|
|
if isBuiltInMetaRoleBoilerplate(metaBody) {
|
|
|
|
|
metaBody = ""
|
|
|
|
|
}
|
2026-10-14 20:02:42 +02:00
|
|
|
// Explicit language-scoped Meta instructions belong to the current language.
|
|
|
|
|
// A shared JSON description_template may hold English default meta rules,
|
|
|
|
|
// but must not override the translated --- Meta --- section in the API.
|
|
|
|
|
if mt, md, ok := splitLocalizedMetaRules(metaBody); ok {
|
|
|
|
|
out.MetaTitle, out.MetaDescription = mt, md
|
|
|
|
|
} else if strings.TrimSpace(out.MetaTitle) == "" && strings.TrimSpace(out.MetaDescription) == "" && strings.TrimSpace(metaBody) != "" {
|
2026-08-23 16:34:50 +02:00
|
|
|
out.MetaDescription = strings.TrimSpace(metaBody)
|
|
|
|
|
out.MetaTitle = DefaultLegacyMetaTitleRule
|
|
|
|
|
} else {
|
|
|
|
|
if strings.TrimSpace(out.MetaTitle) == "" && strings.TrimSpace(derived.MetaTitle) != "" {
|
|
|
|
|
out.MetaTitle = derived.MetaTitle
|
|
|
|
|
}
|
|
|
|
|
if strings.TrimSpace(out.MetaDescription) == "" && strings.TrimSpace(derived.MetaDescription) != "" {
|
|
|
|
|
out.MetaDescription = derived.MetaDescription
|
2026-08-23 13:39:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-08-23 16:34:50 +02:00
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-14 20:02:42 +02:00
|
|
|
// splitLocalizedMetaRules parses the two fixed schema keys while retaining the
|
|
|
|
|
// instructions verbatim in the category's selected content language. The braces
|
|
|
|
|
// are editor instruction delimiters, not text for SEO output.
|
|
|
|
|
func splitLocalizedMetaRules(body string) (title, description string, ok bool) {
|
|
|
|
|
body = strings.TrimSpace(body)
|
2026-10-14 20:28:38 +02:00
|
|
|
if strings.HasPrefix(body, "{") && strings.HasSuffix(body, "}") {
|
2026-10-14 20:02:42 +02:00
|
|
|
body = strings.TrimSpace(body[1 : len(body)-1])
|
|
|
|
|
}
|
|
|
|
|
lower := strings.ToLower(body)
|
|
|
|
|
if !strings.HasPrefix(lower, "meta_title:") {
|
|
|
|
|
return "", "", false
|
|
|
|
|
}
|
|
|
|
|
idx := strings.Index(lower, "meta_description:")
|
|
|
|
|
if idx < 0 {
|
|
|
|
|
return "", "", false
|
|
|
|
|
}
|
2026-10-14 20:28:38 +02:00
|
|
|
// Both legacy {meta_title: ...\nmeta_description: ...} and the newer
|
|
|
|
|
// {meta_title: ...}\n{meta_description: ...} are valid. Store plain
|
|
|
|
|
// instructions in description_template, keeping {} only in the editor.
|
|
|
|
|
title = strings.TrimSpace(strings.TrimSpace(body[len("meta_title:"):idx]))
|
|
|
|
|
title = strings.TrimSpace(title)
|
|
|
|
|
// The two-brace form leaves the separator "}\n{" between the roles
|
|
|
|
|
// after the outer braces were removed. Strip ONLY those delimiters, never
|
|
|
|
|
// user variables such as {{name}} at the end of an instruction.
|
|
|
|
|
if strings.HasSuffix(title, "{") {
|
|
|
|
|
title = strings.TrimSpace(strings.TrimSuffix(title, "{"))
|
|
|
|
|
title = strings.TrimSpace(strings.TrimSuffix(title, "}"))
|
|
|
|
|
}
|
2026-10-14 20:02:42 +02:00
|
|
|
description = strings.TrimSpace(body[idx+len("meta_description:"):])
|
|
|
|
|
return title, description, title != "" && description != ""
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-14 20:28:38 +02:00
|
|
|
// DefaultSEOMetaInstructions returns the two translated prompt rules for a
|
|
|
|
|
// language as plain text (without the editor's instruction braces).
|
|
|
|
|
func DefaultSEOMetaInstructions(lang string) (title, description string, err error) {
|
|
|
|
|
prompt, err := OfflineDefaultPrompt(lang)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
|
|
|
|
title, description, ok := splitLocalizedMetaRules(ExtractEnhanceSectionBody(prompt, SectionMetaStart, SectionMetaEnd))
|
|
|
|
|
if !ok {
|
|
|
|
|
return "", "", fmt.Errorf("invalid meta rules for %q", lang)
|
|
|
|
|
}
|
|
|
|
|
return title, description, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-23 18:58:03 +02:00
|
|
|
func isBuiltInMetaRoleBoilerplate(body string) bool {
|
|
|
|
|
lower := strings.ToLower(strings.TrimSpace(body))
|
|
|
|
|
return strings.HasPrefix(lower, "role: meta")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isBuiltInDescriptionRoleBoilerplate(body string) bool {
|
|
|
|
|
lower := strings.ToLower(strings.TrimSpace(body))
|
|
|
|
|
return strings.HasPrefix(lower, "role: description")
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-23 16:34:50 +02:00
|
|
|
func promptHasRoleMarkers(prompt string) bool {
|
|
|
|
|
lower := strings.ToLower(prompt)
|
|
|
|
|
return strings.Contains(lower, strings.ToLower(SectionTitleStart)) ||
|
|
|
|
|
strings.Contains(lower, strings.ToLower(SectionDescriptionStart)) ||
|
|
|
|
|
strings.Contains(lower, strings.ToLower(SectionMetaStart))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TitleSectionInstructions returns the --- Title --- body from a category prompt.
|
|
|
|
|
func TitleSectionInstructions(categoryPrompt string) string {
|
|
|
|
|
return ExtractEnhanceSectionBody(categoryPrompt, SectionTitleStart, SectionTitleEnd)
|
2026-08-23 13:39:56 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 18:58:03 +02:00
|
|
|
// DescriptionSectionInstructions returns the --- Description --- body, or empty
|
|
|
|
|
// when it is only the built-in Role: description boilerplate.
|
|
|
|
|
func DescriptionSectionInstructions(categoryPrompt string) string {
|
|
|
|
|
body := ExtractEnhanceSectionBody(categoryPrompt, SectionDescriptionStart, SectionDescriptionEnd)
|
|
|
|
|
if isBuiltInDescriptionRoleBoilerplate(body) {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return body
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MetaSectionInstructions returns the --- Meta --- body, or empty when it is only
|
|
|
|
|
// the built-in Role: meta boilerplate.
|
|
|
|
|
func MetaSectionInstructions(categoryPrompt string) string {
|
|
|
|
|
body := ExtractEnhanceSectionBody(categoryPrompt, SectionMetaStart, SectionMetaEnd)
|
|
|
|
|
if isBuiltInMetaRoleBoilerplate(body) {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return body
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-23 13:39:56 +02:00
|
|
|
// EffectiveDescriptionTemplateAny is EffectiveDescriptionFormula as a JSON object
|
|
|
|
|
// for processing.ProductInput.DescriptionTemplate (nil when empty).
|
|
|
|
|
func EffectiveDescriptionTemplateAny(stored any, categoryPrompt string) any {
|
|
|
|
|
f := EffectiveDescriptionFormula(stored, categoryPrompt)
|
|
|
|
|
if len(f.Sections) == 0 && strings.TrimSpace(f.MetaTitle) == "" && strings.TrimSpace(f.MetaDescription) == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
b, err := json.Marshal(f)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
var out map[string]any
|
|
|
|
|
if err := json.Unmarshal(b, &out); err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CategoryTitlePromptRequiresRewrite reports whether the Title role instructs a
|
|
|
|
|
// new retail name (not keep/echo supplier title). Used to reject "Matched" copy-paste.
|
|
|
|
|
func CategoryTitlePromptRequiresRewrite(categoryPrompt string) bool {
|
2026-08-23 16:34:50 +02:00
|
|
|
body := TitleSectionInstructions(categoryPrompt)
|
2026-08-23 13:39:56 +02:00
|
|
|
if body == "" {
|
|
|
|
|
body = strings.TrimSpace(categoryPrompt)
|
|
|
|
|
}
|
|
|
|
|
if body == "" {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2026-08-23 16:34:50 +02:00
|
|
|
// Built-in CategoryEnhanceUserTemplate Title role mentions "Title formula"
|
|
|
|
|
// generically — that is not a tenant rewrite formula.
|
|
|
|
|
if strings.HasPrefix(strings.ToLower(strings.TrimSpace(body)), "role: title") {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2026-08-23 13:39:56 +02:00
|
|
|
lower := strings.ToLower(body)
|
|
|
|
|
cues := []string{
|
|
|
|
|
"napiši novo ime",
|
|
|
|
|
"napisi novo ime",
|
|
|
|
|
"novo ime izdelka",
|
|
|
|
|
"po formuli",
|
|
|
|
|
"write a new",
|
|
|
|
|
"new product name",
|
|
|
|
|
"rename",
|
|
|
|
|
"sentence case",
|
2026-08-23 16:34:50 +02:00
|
|
|
"tip izdelka",
|
|
|
|
|
"product type",
|
2026-08-23 13:39:56 +02:00
|
|
|
}
|
|
|
|
|
for _, c := range cues {
|
|
|
|
|
if strings.Contains(lower, c) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|