Files
descrybe/apps/api/internal/aiprompts/a1_meta.go
T

124 lines
4.2 KiB
Go
Raw Normal View History

2026-10-14 20:28:38 +02:00
package aiprompts
import (
"encoding/json"
"fmt"
"strings"
)
// isObsoleteA1Meta recognizes only known, previously shipped generic meta
// instructions. A category's genuinely custom SEO copy must not be overwritten.
func isObsoleteA1Meta(value string) bool {
value = strings.TrimSpace(value)
if value == "" || value == "{}" || value == "null" {
return true
}
lower := strings.ToLower(value)
// Platform-generated English SEO instructions in an A1 Slovenian category
// are also defaults, not custom authoring. Upgrade those to Slovenian.
if enTitle, enDesc, err := DefaultSEOMetaInstructions("en"); err == nil && (value == enTitle || value == enDesc) {
return true
}
return (strings.Contains(lower, "novo_ime_izdelka") && strings.Contains(lower, "140 znakov")) ||
(strings.Contains(lower, "novo ime izdelka") && strings.Contains(lower, "50-60 znakov")) ||
(strings.Contains(lower, "new product name") && strings.Contains(lower, "140 characters")) ||
value == DefaultLegacyMetaTitleRule || value == DefaultMetaTitleRuleEN
}
// NormalizeA1MetaRole updates ONLY a missing or recognized obsolete Meta role.
// Title and Description roles, and user-authored meta rules, remain byte-identical.
// The selected language determines which bundled translated rule is inserted.
func NormalizeA1MetaRole(source, lang string) (string, bool) {
if !CategoryEnhanceHasRoleSections(source) {
return source, false
}
old := ExtractEnhanceSectionBody(source, SectionMetaStart, SectionMetaEnd)
// Imported A1 combined formulas may already have good meta rules but lack
// the {} instruction wrappers. Preserve those words and normalize markup.
if title, description, ok := splitLocalizedMetaRules(old); ok {
canonical := "{meta_title: " + title + "}\n{meta_description: " + description + "}"
if strings.TrimSpace(old) == canonical {
return source, false
}
return replaceA1MetaBody(source, canonical)
}
if !isObsoleteA1Meta(old) {
return source, false
}
localized, err := OfflineDefaultPrompt(lang)
if err != nil {
return source, false
}
replacement := ExtractEnhanceSectionBody(localized, SectionMetaStart, SectionMetaEnd)
if replacement == "" || strings.TrimSpace(old) == replacement {
return source, false
}
return replaceA1MetaBody(source, replacement)
}
func replaceA1MetaBody(source, replacement string) (string, bool) {
// Locate the exact Meta role without touching the other two roles.
normal := strings.ToLower(source)
start := strings.Index(normal, strings.ToLower(SectionMetaStart))
if start < 0 {
return source, false
}
offset := start + len(SectionMetaStart)
end := strings.Index(normal[offset:], strings.ToLower(SectionMetaEnd))
if end < 0 {
return source, false
}
return source[:offset] + "\n" + replacement + "\n" + source[offset+end:], true
}
// NormalizeA1MetaTemplate adds/repairs metaTitle and metaDescription on the
// A1 JSON description_template. Existing HTML sections and custom metadata are
// preserved. An empty template is valid and is initialized with metadata only.
func NormalizeA1MetaTemplate(raw, lang string) (string, bool, error) {
trimmed := strings.TrimSpace(raw)
if trimmed == "null" {
trimmed = ""
}
obj := make(map[string]json.RawMessage)
if trimmed != "" {
if err := json.Unmarshal([]byte(trimmed), &obj); err != nil {
return raw, false, fmt.Errorf("decode A1 description_template: %w", err)
}
}
if obj == nil {
obj = make(map[string]json.RawMessage)
}
title, desc, err := DefaultSEOMetaInstructions(lang)
if err != nil {
return raw, false, err
}
changed := false
for key, replacement := range map[string]string{"metaTitle": title, "metaDescription": desc} {
var existing string
if value := obj[key]; len(value) > 0 && string(value) != "null" {
if err := json.Unmarshal(value, &existing); err != nil {
return raw, false, fmt.Errorf("decode A1 %s: %w", key, err)
}
}
if !isObsoleteA1Meta(existing) {
continue
}
if existing == replacement {
continue
}
encoded, err := json.Marshal(replacement)
if err != nil {
return raw, false, err
}
obj[key] = encoded
changed = true
}
if !changed {
return raw, false, nil
}
encoded, err := json.Marshal(obj)
if err != nil {
return raw, false, err
}
return string(encoded), true, nil
}