2026-08-17 00:39:25 +02:00
|
|
|
package aiprompts
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
reLegacyNameTag = regexp.MustCompile(`(?is)<\s*name\s*>\s*\{?\s*(.*?)\s*\}?\s*<\s*/\s*name\s*>`)
|
|
|
|
|
reLegacyMetaTag = regexp.MustCompile(`(?is)<\s*metaDescription\s*>\s*\{?\s*(.*?)\s*\}?\s*<\s*/\s*metaDescription\s*>`)
|
2026-08-23 20:49:40 +02:00
|
|
|
reLegacyDescPH = regexp.MustCompile(`(?i)\{\s*""?\s*(?:OPIS\s+IZDELKA|PRODUCT\s+DESCRIPTION)\s*""?\s*\}`)
|
|
|
|
|
reLegacyNamePH = regexp.MustCompile(`(?i)\{\s*""?\s*(?:STARO\s+IME\s+IZDELKA|OLD\s+PRODUCT\s+NAME)\s*""?\s*\}`)
|
2026-08-17 00:39:25 +02:00
|
|
|
reDoubledQuotes = regexp.MustCompile(`"{2,}`)
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-23 20:49:40 +02:00
|
|
|
// legacyTemplateMarkers introduce the formula body in a combined prompt. The
|
|
|
|
|
// Slovenian marker is the legacy A1 wording; the English one is used by the
|
|
|
|
|
// platform-default seed (scripts/seed/default-category-prompts.json), which is the
|
|
|
|
|
// same template style translated.
|
|
|
|
|
var legacyTemplateMarkers = []string{"gpt predloga:", "product template:"}
|
|
|
|
|
|
|
|
|
|
// legacyIntroLinePrefixes are scaffolding lines above the template body ("here are
|
|
|
|
|
// the variables", "follow the template step by step") in either language. They are
|
|
|
|
|
// not body structure and must not become description sections.
|
|
|
|
|
var legacyIntroLinePrefixes = []string{
|
|
|
|
|
"ustvari nov opis",
|
|
|
|
|
"star_opis_izdelka",
|
|
|
|
|
"staro_ime_izdelka",
|
|
|
|
|
"uporabi spodnjo gpt",
|
|
|
|
|
"sledi tej gpt",
|
|
|
|
|
"create a new product description",
|
|
|
|
|
"old_product_description",
|
|
|
|
|
"old_product_name",
|
|
|
|
|
"use the product template",
|
|
|
|
|
"follow this product template",
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 00:39:25 +02:00
|
|
|
// LegacyEnhanceParts holds extracted role content from a combined PHP/A1 category Prompt.
|
|
|
|
|
type LegacyEnhanceParts struct {
|
|
|
|
|
TitleRules string
|
|
|
|
|
DescriptionRules string
|
|
|
|
|
MetaRules string
|
|
|
|
|
WasLegacy bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsLegacyCombinedEnhancePrompt reports whether prompt looks like the old A1/PHP
|
2026-08-23 20:49:40 +02:00
|
|
|
// combined name+description(+meta) marketing blob (<name>/<metaDescription>/GPT
|
|
|
|
|
// predloga), or its English platform-default translation (Product template:).
|
2026-08-17 00:39:25 +02:00
|
|
|
func IsLegacyCombinedEnhancePrompt(prompt string) bool {
|
|
|
|
|
p := strings.TrimSpace(prompt)
|
|
|
|
|
if p == "" {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
// Require real tag wrappers — not instructional mentions like
|
|
|
|
|
// `do NOT … <name>/<metaDescription> tags` in CategoryEnhanceUserTemplate.
|
|
|
|
|
hasNameTag := reLegacyNameTag.MatchString(p)
|
|
|
|
|
hasMetaTag := reLegacyMetaTag.MatchString(p)
|
2026-08-23 20:49:40 +02:00
|
|
|
hasTemplateMarker := hasAnyLegacyTemplateMarker(p)
|
|
|
|
|
hasPlaceholders := reLegacyNamePH.MatchString(p) || reLegacyDescPH.MatchString(p)
|
|
|
|
|
if hasNameTag && (hasMetaTag || hasTemplateMarker || hasPlaceholders) {
|
2026-08-17 00:39:25 +02:00
|
|
|
return true
|
|
|
|
|
}
|
2026-08-23 20:49:40 +02:00
|
|
|
return hasPlaceholders && hasTemplateMarker
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func hasAnyLegacyTemplateMarker(prompt string) bool {
|
|
|
|
|
lower := strings.ToLower(prompt)
|
|
|
|
|
for _, marker := range legacyTemplateMarkers {
|
|
|
|
|
if strings.Contains(lower, marker) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2026-08-17 00:39:25 +02:00
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ParseLegacyCombinedEnhancePrompt extracts title / description / meta instruction
|
|
|
|
|
// blobs from a legacy combined prompt. Placeholders are modernized to {{name}} /
|
|
|
|
|
// {{description}}. WasLegacy is false when no combined markers are found.
|
|
|
|
|
func ParseLegacyCombinedEnhancePrompt(prompt string) LegacyEnhanceParts {
|
|
|
|
|
raw := strings.TrimSpace(prompt)
|
|
|
|
|
out := LegacyEnhanceParts{}
|
|
|
|
|
if raw == "" || !IsLegacyCombinedEnhancePrompt(raw) {
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
out.WasLegacy = true
|
|
|
|
|
|
|
|
|
|
if m := reLegacyNameTag.FindStringSubmatch(raw); len(m) == 2 {
|
|
|
|
|
out.TitleRules = cleanLegacyInstruction(m[1])
|
|
|
|
|
}
|
|
|
|
|
if m := reLegacyMetaTag.FindStringSubmatch(raw); len(m) == 2 {
|
|
|
|
|
out.MetaRules = cleanLegacyInstruction(m[1])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rest := reLegacyNameTag.ReplaceAllString(raw, "")
|
|
|
|
|
rest = reLegacyMetaTag.ReplaceAllString(rest, "")
|
2026-08-23 20:49:40 +02:00
|
|
|
for _, marker := range legacyTemplateMarkers {
|
|
|
|
|
if idx := strings.Index(strings.ToLower(rest), marker); idx >= 0 {
|
|
|
|
|
rest = rest[idx+len(marker):]
|
|
|
|
|
break
|
|
|
|
|
}
|
2026-08-17 00:39:25 +02:00
|
|
|
}
|
|
|
|
|
// Drop Slovenian boilerplate intro lines that are not body structure.
|
|
|
|
|
rest = stripLegacyIntroNoise(rest)
|
|
|
|
|
out.DescriptionRules = cleanLegacyInstruction(rest)
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SplitLegacyCombinedEnhancePrompt rewrites a legacy combined name+description(+meta)
|
2026-08-18 00:53:26 +02:00
|
|
|
// prompt into role sections carrying ONLY the original per-category text — the
|
|
|
|
|
// Slovenian naming formula under Title, the HTML body structure under Description,
|
|
|
|
|
// the SEO instruction under Meta. No schema/role boilerplate is stored: JSON
|
|
|
|
|
// schema, role framing, and {{name}}/{{description}}/{{category}}/{{attrs}}
|
|
|
|
|
// product context are all supplied at render time (system template +
|
|
|
|
|
// processing.ensureCategoryEnhanceUserContext). Non-legacy input returns "" —
|
|
|
|
|
// callers leave the category without an override (company default applies).
|
2026-08-17 00:39:25 +02:00
|
|
|
func SplitLegacyCombinedEnhancePrompt(prompt string) string {
|
|
|
|
|
parts := ParseLegacyCombinedEnhancePrompt(prompt)
|
|
|
|
|
if !parts.WasLegacy {
|
2026-08-18 00:53:26 +02:00
|
|
|
return ""
|
2026-08-17 00:39:25 +02:00
|
|
|
}
|
|
|
|
|
return BuildCategoryEnhanceOverlay(parts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BuildCategoryEnhanceOverlay composes a role-sectioned enhance USER overlay from
|
2026-08-18 00:53:26 +02:00
|
|
|
// extracted legacy parts — the stored text is exactly the split legacy content,
|
|
|
|
|
// matching what a user would type into the Title/Description/Meta prompt areas.
|
2026-08-17 00:39:25 +02:00
|
|
|
func BuildCategoryEnhanceOverlay(parts LegacyEnhanceParts) string {
|
|
|
|
|
var b strings.Builder
|
2026-08-18 00:53:26 +02:00
|
|
|
section := func(start, end, body string) {
|
|
|
|
|
b.WriteString(start)
|
2026-08-17 00:39:25 +02:00
|
|
|
b.WriteByte('\n')
|
2026-08-18 00:53:26 +02:00
|
|
|
if body = strings.TrimSpace(body); body != "" {
|
|
|
|
|
b.WriteString(body)
|
|
|
|
|
b.WriteByte('\n')
|
|
|
|
|
}
|
|
|
|
|
b.WriteString(end)
|
|
|
|
|
b.WriteString("\n\n")
|
2026-08-17 00:39:25 +02:00
|
|
|
}
|
2026-08-18 00:53:26 +02:00
|
|
|
section(SectionTitleStart, SectionTitleEnd, parts.TitleRules)
|
|
|
|
|
section(SectionDescriptionStart, SectionDescriptionEnd, parts.DescriptionRules)
|
|
|
|
|
section(SectionMetaStart, SectionMetaEnd, parts.MetaRules)
|
2026-08-17 00:39:25 +02:00
|
|
|
return strings.TrimSpace(b.String())
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-23 13:39:56 +02:00
|
|
|
// ExtractEnhanceSectionBody returns the text between start/end markers
|
|
|
|
|
// (case-insensitive markers, body preserved). Empty when the start marker is missing.
|
|
|
|
|
func ExtractEnhanceSectionBody(prompt, start, end string) string {
|
|
|
|
|
raw := strings.ReplaceAll(strings.ReplaceAll(prompt, "\r\n", "\n"), "\r", "\n")
|
|
|
|
|
lower := strings.ToLower(raw)
|
|
|
|
|
startL := strings.ToLower(strings.TrimSpace(start))
|
|
|
|
|
endL := strings.ToLower(strings.TrimSpace(end))
|
|
|
|
|
if startL == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
startIdx := strings.Index(lower, startL)
|
|
|
|
|
if startIdx < 0 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
bodyStart := startIdx + len(startL)
|
|
|
|
|
rest := raw[bodyStart:]
|
|
|
|
|
restLower := lower[bodyStart:]
|
|
|
|
|
if endL != "" {
|
|
|
|
|
if endIdx := strings.Index(restLower, endL); endIdx >= 0 {
|
|
|
|
|
rest = rest[:endIdx]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return strings.TrimSpace(strings.TrimPrefix(strings.TrimSuffix(rest, "\n"), "\n"))
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-23 20:49:40 +02:00
|
|
|
func hasAnyPrefix(s string, prefixes []string) bool {
|
|
|
|
|
for _, p := range prefixes {
|
|
|
|
|
if strings.HasPrefix(s, p) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-17 00:39:25 +02:00
|
|
|
func cleanLegacyInstruction(s string) string {
|
|
|
|
|
s = modernizeLegacyPlaceholders(s)
|
|
|
|
|
s = reDoubledQuotes.ReplaceAllString(s, `"`)
|
|
|
|
|
s = strings.ReplaceAll(s, "\r\n", "\n")
|
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
// Collapse excessive blank lines.
|
|
|
|
|
for strings.Contains(s, "\n\n\n") {
|
|
|
|
|
s = strings.ReplaceAll(s, "\n\n\n", "\n\n")
|
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func modernizeLegacyPlaceholders(prompt string) string {
|
|
|
|
|
prompt = reLegacyDescPH.ReplaceAllString(prompt, "{{description}}")
|
|
|
|
|
prompt = reLegacyNamePH.ReplaceAllString(prompt, "{{name}}")
|
|
|
|
|
return prompt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func stripLegacyIntroNoise(s string) string {
|
|
|
|
|
lines := strings.Split(s, "\n")
|
|
|
|
|
out := make([]string, 0, len(lines))
|
|
|
|
|
for _, line := range lines {
|
|
|
|
|
trim := strings.TrimSpace(line)
|
|
|
|
|
lower := strings.ToLower(trim)
|
|
|
|
|
switch {
|
|
|
|
|
case trim == "":
|
|
|
|
|
if len(out) > 0 {
|
|
|
|
|
out = append(out, "")
|
|
|
|
|
}
|
2026-08-23 20:49:40 +02:00
|
|
|
case hasAnyPrefix(lower, legacyIntroLinePrefixes):
|
2026-08-17 00:39:25 +02:00
|
|
|
continue
|
|
|
|
|
default:
|
|
|
|
|
out = append(out, trim)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return strings.TrimSpace(strings.Join(out, "\n"))
|
|
|
|
|
}
|