Store category prompts as EXACT split legacy text, no boilerplate

The per-category prompt editor showed English machine boilerplate
("Role: description. Build JSON …", schema intro, {{var}} context lines)
because seed/sync baked the render framing into categories.prompt. Now the
stored overlay is exactly the legacy wp_product_categories.sql content,
split into the three sections a user would type themselves:

  --- Title ---        Slovenian naming formula
  --- Description ---  legacy <H2>/<p>/<ul> body structure
  --- Meta ---         legacy metaDescription instruction

Schema, role framing, and Name/Description/Category/Attrs product context
stay render-time only (system template + ensureCategoryEnhanceUserContext),
where they already existed.

- SplitLegacyCombinedEnhancePrompt: non-legacy input now returns "" —
  categories without seed/legacy content get their override CLEARED
  (company default) instead of being stuffed with the canonical template
  (e.g. parent categories like "Bela tehnika" absent from the SQL).
- CategoryEnhancePromptNeedsRepair flags stored boilerplate ("parsed as
  JSON", "Build JSON", retired Attributes section) so Sync rewrites old
  data to the clean shape; repair supports clearing (prompt = '{}').
- seed-a1 apply-category-prompts skips instead of writing template text.
- Web editor compose stores only the user's section text: empty sections
  keep bare markers, default bodies and the default preamble are never
  persisted (DEFAULT_SECTION_BODIES removed).
- Verified locally: apply rewrote 238 A1+Demo categories (216 exact
  splits, 22 cleared), zero boilerplate matches in DB, idempotent re-run
  (would_update=0).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-18 00:53:26 +02:00
co-authored by Claude Fable 5
parent 0eef202f56
commit 9a839d6d13
13 changed files with 182 additions and 157 deletions
+7 -3
View File
@@ -188,7 +188,8 @@ func applyCategoryPrompts(ctx context.Context, pg *pgxpool.Pool, companyID uuid.
byNorm := make(map[string]string, len(file.Entries)) byNorm := make(map[string]string, len(file.Entries))
byUnique := make(map[string]string, len(file.Entries)) byUnique := make(map[string]string, len(file.Entries))
// Overlay: split each legacy combined Name+Description prompt into role-sectioned // Overlay: split each legacy combined Name+Description prompt into role-sectioned
// enhance USER text (Title / Description / Meta / Attributes). Unique title / // enhance USER text (Title / Description / Meta) holding exactly the legacy
// per-category text — no schema/role boilerplate. Unique title /
// description / meta formulas stay in title_template / description_template. // description / meta formulas stay in title_template / description_template.
// Stored under "sl" + "*" so prompt->>'sl' and LangPromptAny both resolve; // Stored under "sl" + "*" so prompt->>'sl' and LangPromptAny both resolve;
// language stays via {{language}}. // language stays via {{language}}.
@@ -207,10 +208,13 @@ func applyCategoryPrompts(ctx context.Context, pg *pgxpool.Pool, companyID uuid.
} }
if aiprompts.IsLegacyCombinedEnhancePrompt(raw) { if aiprompts.IsLegacyCombinedEnhancePrompt(raw) {
canonical = prepareCategoryPrompt(aiprompts.SplitLegacyCombinedEnhancePrompt(raw)) canonical = prepareCategoryPrompt(aiprompts.SplitLegacyCombinedEnhancePrompt(raw))
} else if aiprompts.CategoryEnhanceHasRoleSections(raw) { } else if aiprompts.CategoryEnhanceHasRoleSections(raw) && !aiprompts.CategoryEnhancePromptNeedsRepair(raw) {
canonical = prepareCategoryPrompt(raw) canonical = prepareCategoryPrompt(raw)
} else { } else {
canonical = prepareCategoryPrompt(aiprompts.CategoryEnhanceUserTemplate) // No usable per-category content — never store template boilerplate;
// the category keeps the company default.
out.Skipped++
continue
} }
if canonical == "" { if canonical == "" {
out.Skipped++ out.Skipped++
+12 -7
View File
@@ -84,12 +84,12 @@ Category: {{category}}
Attrs: {{attrs}}` Attrs: {{attrs}}`
// CategoryEnhancePromptNeedsRepair reports whether a stored categories.prompt value // CategoryEnhancePromptNeedsRepair reports whether a stored categories.prompt value
// should be rewritten into role-sectioned enhance overlay form. Empty prompts are // should be rewritten into clean role-sectioned overlay form (or cleared). Empty
// left alone. Canonical CategoryEnhanceUserTemplate and per-category overlays that // prompts are left alone. A clean overlay carries Title/Description/Meta markers
// already carry Title/Description/Meta markers are OK — equality with the shared // with ONLY the category's own instruction text — schema/role machine boilerplate
// template is not required (legacy splits keep Slovenian rules). Prompts still // (old seed versions stored the canonical template text, "Build JSON …" role
// carrying a retired "--- Attributes ---" role section are rewritten so attribute // instructions, and a retired "--- Attributes ---" section) marks a prompt for
// prompting stays out of category prompts. // repair so the prompt editor shows exactly the per-category rules.
func CategoryEnhancePromptNeedsRepair(prompt string) bool { func CategoryEnhancePromptNeedsRepair(prompt string) bool {
p := strings.TrimSpace(prompt) p := strings.TrimSpace(prompt)
if p == "" { if p == "" {
@@ -101,7 +101,12 @@ func CategoryEnhancePromptNeedsRepair(prompt string) bool {
if !CategoryEnhanceHasRoleSections(p) { if !CategoryEnhanceHasRoleSections(p) {
return true return true
} }
return strings.Contains(strings.ToLower(p), strings.ToLower(SectionAttributesStart)) lower := strings.ToLower(p)
if strings.Contains(lower, strings.ToLower(SectionAttributesStart)) {
return true
}
return strings.Contains(lower, "your reply is parsed as json") ||
strings.Contains(lower, `build json "`)
} }
// BuiltInDefaults match the previous hardcoded system prompts, with structured user templates. // BuiltInDefaults match the previous hardcoded system prompts, with structured user templates.
+8 -6
View File
@@ -43,17 +43,16 @@ func TestCategoryEnhancePromptNeedsRepair(t *testing.T) {
if CategoryEnhancePromptNeedsRepair("") { if CategoryEnhancePromptNeedsRepair("") {
t.Fatal("empty should not need repair") t.Fatal("empty should not need repair")
} }
if CategoryEnhancePromptNeedsRepair(CategoryEnhanceUserTemplate) { // A STORED copy of the built-in template is boilerplate pollution — repair
t.Fatal("canonical template should be idempotent") // clears it so the category falls back to the company default.
} if !CategoryEnhancePromptNeedsRepair(CategoryEnhanceUserTemplate) {
if CategoryEnhancePromptNeedsRepair(" " + CategoryEnhanceUserTemplate + "\n") { t.Fatal("stored canonical template text should need repair (cleared to default)")
t.Fatal("whitespace-trimmed canonical should be idempotent")
} }
legacy := `Ustvari nov opis\n<H2>foo</H2>\nAttrs missing` legacy := `Ustvari nov opis\n<H2>foo</H2>\nAttrs missing`
if !CategoryEnhancePromptNeedsRepair(legacy) { if !CategoryEnhancePromptNeedsRepair(legacy) {
t.Fatal("legacy HTML prompt should need repair") t.Fatal("legacy HTML prompt should need repair")
} }
// Per-category sectioned overlay (Slovenian rules) must not force re-repair. // Per-category sectioned overlay (pure Slovenian rules) must not force re-repair.
split := SplitLegacyCombinedEnhancePrompt(`GPT predloga: split := SplitLegacyCombinedEnhancePrompt(`GPT predloga:
<name>{Napiši tip izdelka}</name> <name>{Napiši tip izdelka}</name>
<metaDescription>{140 znakov}</metaDescription> <metaDescription>{140 znakov}</metaDescription>
@@ -61,6 +60,9 @@ func TestCategoryEnhancePromptNeedsRepair(t *testing.T) {
if CategoryEnhancePromptNeedsRepair(split) { if CategoryEnhancePromptNeedsRepair(split) {
t.Fatal("sectioned split overlay should be idempotent") t.Fatal("sectioned split overlay should be idempotent")
} }
if CategoryEnhancePromptNeedsRepair(" " + split + "\n") {
t.Fatal("whitespace-trimmed split overlay should be idempotent")
}
} }
func TestBuiltInProductEnhanceUsesSharedUserTemplate(t *testing.T) { func TestBuiltInProductEnhanceUsesSharedUserTemplate(t *testing.T) {
+21 -43
View File
@@ -74,61 +74,39 @@ func ParseLegacyCombinedEnhancePrompt(prompt string) LegacyEnhanceParts {
} }
// SplitLegacyCombinedEnhancePrompt rewrites a legacy combined name+description(+meta) // SplitLegacyCombinedEnhancePrompt rewrites a legacy combined name+description(+meta)
// prompt into CategoryEnhanceUserTemplate role sections, preserving Slovenian // prompt into role sections carrying ONLY the original per-category text — the
// naming / HTML / meta intent as category-specific rules under Title / Description / Meta. // Slovenian naming formula under Title, the HTML body structure under Description,
// Non-legacy input returns CategoryEnhanceUserTemplate (canonical shared overlay). // 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).
func SplitLegacyCombinedEnhancePrompt(prompt string) string { func SplitLegacyCombinedEnhancePrompt(prompt string) string {
parts := ParseLegacyCombinedEnhancePrompt(prompt) parts := ParseLegacyCombinedEnhancePrompt(prompt)
if !parts.WasLegacy { if !parts.WasLegacy {
return strings.TrimSpace(CategoryEnhanceUserTemplate) return ""
} }
return BuildCategoryEnhanceOverlay(parts) return BuildCategoryEnhanceOverlay(parts)
} }
// BuildCategoryEnhanceOverlay composes a role-sectioned enhance USER overlay from // BuildCategoryEnhanceOverlay composes a role-sectioned enhance USER overlay from
// extracted legacy parts (or empty parts → canonical template text with no extra rules). // 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.
func BuildCategoryEnhanceOverlay(parts LegacyEnhanceParts) string { func BuildCategoryEnhanceOverlay(parts LegacyEnhanceParts) string {
var b strings.Builder var b strings.Builder
b.WriteString(`Your reply is parsed as JSON {"name":"string","description":"string","meta_title":"string","meta_description":"string","attrs":{}} only (system schema). Write all string fields in {{language}} (do not hardcode a language).`) section := func(start, end, body string) {
b.WriteString("\n\n") b.WriteString(start)
b.WriteString(SectionTitleStart)
b.WriteByte('\n')
b.WriteString(TitleRoleInstruction)
b.WriteByte('\n')
if rules := strings.TrimSpace(parts.TitleRules); rules != "" {
b.WriteString("Category naming rules (preserve intent): ")
b.WriteString(rules)
b.WriteByte('\n') b.WriteByte('\n')
if body = strings.TrimSpace(body); body != "" {
b.WriteString(body)
b.WriteByte('\n')
}
b.WriteString(end)
b.WriteString("\n\n")
} }
b.WriteString("Name: {{name}}\n") section(SectionTitleStart, SectionTitleEnd, parts.TitleRules)
b.WriteString(SectionTitleEnd) section(SectionDescriptionStart, SectionDescriptionEnd, parts.DescriptionRules)
b.WriteString("\n\n") section(SectionMetaStart, SectionMetaEnd, parts.MetaRules)
b.WriteString(SectionDescriptionStart)
b.WriteString("\nRole: description. Build JSON \"description\": product body HTML only (not SEO meta). When a Description formula follows, emit ONE HTML string covering each section in order (tags matching type: h1/h2/h3/h4, p, ul); otherwise prefer 1-3 factual paragraphs as ONE string with limited HTML (<h2><p><ul><li>) — do NOT emit a competing full HTML document or wrap the whole reply in <name>/<metaDescription> tags.\n")
if rules := strings.TrimSpace(parts.DescriptionRules); rules != "" {
b.WriteString("Category HTML structure (preserve intent; emit as ONE description HTML string, not tagged name/meta blocks):\n")
b.WriteString(rules)
b.WriteByte('\n')
}
b.WriteString("Description: {{description}}\n")
b.WriteString(SectionDescriptionEnd)
b.WriteString("\n\n")
b.WriteString(SectionMetaStart)
b.WriteString("\nRole: meta. Build JSON \"meta_title\" and \"meta_description\" as plain SEO text (never HTML). meta_title: 50-60 chars; meta_description: 120-155 chars; follow any SEO meta formula that follows; never copy the full description HTML into meta_description.\n")
if rules := strings.TrimSpace(parts.MetaRules); rules != "" {
b.WriteString("Category SEO rules (preserve intent; plain text only): ")
b.WriteString(rules)
b.WriteByte('\n')
}
b.WriteString(SectionMetaEnd)
b.WriteString("\n\n")
b.WriteString("Product context:\n")
b.WriteString("Category: {{category}}\n")
b.WriteString("Attrs: {{attrs}}")
return strings.TrimSpace(b.String()) return strings.TrimSpace(b.String())
} }
@@ -44,19 +44,25 @@ GPT predloga:
if !strings.Contains(got, "Tehnične specifikacije") && !strings.Contains(got, "tehničnih specifikacij") { if !strings.Contains(got, "Tehnične specifikacije") && !strings.Contains(got, "tehničnih specifikacij") {
t.Fatal("description section must preserve HTML body intent") t.Fatal("description section must preserve HTML body intent")
} }
if !strings.Contains(got, "{{attrs}}") || !strings.Contains(got, "{{language}}") { // The stored overlay is EXACTLY the split legacy text — no schema/role
t.Fatal("overlay must keep {{attrs}} and {{language}}") // boilerplate, no {{var}} placeholders (product context is render-time).
lower := strings.ToLower(got)
for _, junk := range []string{"your reply is parsed as json", "role: title", "role: description", "role: meta", "{{attrs}}", "{{name}}", "{{language}}", "product context"} {
if strings.Contains(lower, junk) {
t.Fatalf("overlay must not carry boilerplate %q:\n%s", junk, got)
}
} }
if strings.Contains(got, "OPIS IZDELKA") || strings.Contains(got, "STARO IME IZDELKA") { if strings.Contains(got, "OPIS IZDELKA") || strings.Contains(got, "STARO IME IZDELKA") {
t.Fatal("legacy placeholders must be modernized") t.Fatal("legacy placeholders must be modernized")
} }
} }
func TestSplitLegacyNonLegacyFallsBackToTemplate(t *testing.T) { func TestSplitLegacyNonLegacyReturnsEmpty(t *testing.T) {
t.Parallel() t.Parallel()
got := SplitLegacyCombinedEnhancePrompt("short retail overlay") // Non-legacy input has no per-category content to preserve — callers must
if strings.TrimSpace(got) != strings.TrimSpace(CategoryEnhanceUserTemplate) { // leave the category on the company default instead of storing boilerplate.
t.Fatalf("non-legacy should fall back to canonical template") if got := SplitLegacyCombinedEnhancePrompt("short retail overlay"); got != "" {
t.Fatalf("non-legacy should return empty, got %q", got)
} }
} }
+4 -2
View File
@@ -25,8 +25,10 @@ func TestCategoryEnhanceHasRoleSections(t *testing.T) {
if !CategoryEnhancePromptNeedsRepair(old) { if !CategoryEnhancePromptNeedsRepair(old) {
t.Fatal("old attributes-bearing prompt must need repair (attributes section retired)") t.Fatal("old attributes-bearing prompt must need repair (attributes section retired)")
} }
if CategoryEnhancePromptNeedsRepair(CategoryEnhanceUserTemplate) { // The built-in template is render-time only; a STORED copy is boilerplate
t.Fatal("canonical template must not need repair") // pollution and must be repaired (cleared to company default).
if !CategoryEnhancePromptNeedsRepair(CategoryEnhanceUserTemplate) {
t.Fatal("stored canonical template text must need repair")
} }
} }
@@ -86,12 +86,17 @@ func TestTitleRoleInstructionNeverBrandOnly(t *testing.T) {
if !strings.Contains(CategoryEnhanceUserTemplate, TitleRoleInstruction) { if !strings.Contains(CategoryEnhanceUserTemplate, TitleRoleInstruction) {
t.Fatal("CategoryEnhanceUserTemplate must embed TitleRoleInstruction") t.Fatal("CategoryEnhanceUserTemplate must embed TitleRoleInstruction")
} }
// Split overlays carry ONLY the legacy per-category text; the never-brand-only
// rule lives in the render-time role framing, not the stored prompt.
split := SplitLegacyCombinedEnhancePrompt(`GPT predloga: split := SplitLegacyCombinedEnhancePrompt(`GPT predloga:
<name>{Napiši tip izdelka, znamka, poln model}</name> <name>{Napiši tip izdelka, znamka, poln model}</name>
<metaDescription>{140 znakov}</metaDescription> <metaDescription>{140 znakov}</metaDescription>
<H2>{benefit}</H2>`) <H2>{benefit}</H2>`)
if !strings.Contains(split, "never brand-only") { if strings.Contains(split, "never brand-only") {
t.Fatalf("split Title section missing never brand-only: %s", split) t.Fatalf("split overlay must not embed role boilerplate: %s", split)
}
if !strings.Contains(split, "Napiši tip izdelka, znamka, poln model") {
t.Fatalf("split Title section must keep the legacy naming text: %s", split)
} }
} }
+5 -18
View File
@@ -15,13 +15,15 @@ func TestCategoryEnhanceTemplateHasAttrs(t *testing.T) {
t.Fatalf("template missing {{attrs}}: %q", tpl) t.Fatalf("template missing {{attrs}}: %q", tpl)
} }
if !aiprompts.CategoryEnhanceHasRoleSections(tpl) { if !aiprompts.CategoryEnhanceHasRoleSections(tpl) {
t.Fatal("repaired template must include title/description/meta/attributes sections") t.Fatal("built-in template must include title/description/meta sections")
} }
if !aiprompts.CategoryEnhancePromptNeedsRepair("legacy HTML prompt without attrs") { if !aiprompts.CategoryEnhancePromptNeedsRepair("legacy HTML prompt without attrs") {
t.Fatal("expected legacy prompt to need repair") t.Fatal("expected legacy prompt to need repair")
} }
if aiprompts.CategoryEnhancePromptNeedsRepair(tpl) { // The built-in template is render-time only; STORED copies of it are
t.Fatal("canonical template should not need repair") // boilerplate pollution and must be repaired (cleared).
if !aiprompts.CategoryEnhancePromptNeedsRepair(tpl) {
t.Fatal("stored template text should need repair")
} }
} }
@@ -32,21 +34,6 @@ func TestPlatformDemoNameConstant(t *testing.T) {
} }
} }
func TestRepairedCategoryEnhancePromptMapMatchesA1Demo(t *testing.T) {
t.Parallel()
want, err := repairedCategoryEnhancePromptMap()
if err != nil {
t.Fatal(err)
}
tpl := strings.TrimSpace(aiprompts.CategoryEnhanceUserTemplate)
if want["sl"] != tpl || want[company.LangPromptAny] != tpl {
t.Fatalf("want sl+* template, got %#v", want)
}
if !strings.Contains(tpl, "{{attrs}}") {
t.Fatal("template must include {{attrs}}")
}
}
func TestCategoryEnhancePromptMapOKAcceptsSplitOverlay(t *testing.T) { func TestCategoryEnhancePromptMapOKAcceptsSplitOverlay(t *testing.T) {
t.Parallel() t.Parallel()
legacy := `GPT predloga: legacy := `GPT predloga:
+32 -24
View File
@@ -62,10 +62,12 @@ type RepairA1DemoOptions struct {
// RepairA1DemoCategoryEnhancePrompts replaces non-sectioned / legacy combined // RepairA1DemoCategoryEnhancePrompts replaces non-sectioned / legacy combined
// categories.prompt values for A1 Slovenija + Platform Demo with role-sectioned // categories.prompt values for A1 Slovenija + Platform Demo with role-sectioned
// overlays (Title / Description / Meta). Prefers wp_product_categories.sql // overlays (Title / Description / Meta) carrying exactly the split legacy text.
// (SEED_A1_WP_CATEGORIES / scripts/seed) as source of truth, then a1-category-prompts.json, // Prefers wp_product_categories.sql (SEED_A1_WP_CATEGORIES / scripts/seed) as
// splitting combined Name+Description prompts so naming rules land under Title and // source of truth, then a1-category-prompts.json, splitting combined
// HTML body under Description; otherwise fall back to CategoryEnhanceUserTemplate. // Name+Description prompts so naming rules land under Title and HTML body under
// Description; categories with no seed/legacy content have their override
// cleared (company default) — template boilerplate is never stored.
// //
// Also repairs empty or brand-only title_template and empty description_template // Also repairs empty or brand-only title_template and empty description_template
// from legacy <name> / HTML / meta blocks so enhance uses real A1 formulas. // from legacy <name> / HTML / meta blocks so enhance uses real A1 formulas.
@@ -337,19 +339,6 @@ func foldSlovenePromptRune(r rune) rune {
} }
} }
// repairedCategoryEnhancePromptMap is the shared fallback overlay (no per-category
// Slovenian rules): "sl" + LangPromptAny ("*").
func repairedCategoryEnhancePromptMap() (company.LangPromptMap, error) {
tpl := strings.TrimSpace(aiprompts.CategoryEnhanceUserTemplate)
if tpl == "" {
return nil, fmt.Errorf("CategoryEnhanceUserTemplate is empty")
}
return company.LangPromptMap{
"sl": tpl,
company.LangPromptAny: tpl,
}, nil
}
func categoryEnhancePromptValueOK(p string) bool { func categoryEnhancePromptValueOK(p string) bool {
p = strings.TrimSpace(p) p = strings.TrimSpace(p)
if p == "" { if p == "" {
@@ -406,6 +395,10 @@ func resolveRepairedEnhancePrompt(current company.LangPromptMap, seedLegacy stri
return prompt return prompt
} }
// computeRepairedEnhancePrompt returns the clean role-sectioned overlay for a
// category — exactly the split per-category text. "" means the override should
// be CLEARED (no legacy/seed content to preserve → company default applies);
// fallback reports that clear case.
func computeRepairedEnhancePrompt(current company.LangPromptMap, seedLegacy string, preferSeed bool) (prompt string, fromSeed, fromLegacy, fallback bool) { func computeRepairedEnhancePrompt(current company.LangPromptMap, seedLegacy string, preferSeed bool) (prompt string, fromSeed, fromLegacy, fallback bool) {
trySeed := func(raw string) (string, bool, bool) { trySeed := func(raw string) (string, bool, bool) {
raw = strings.TrimSpace(raw) raw = strings.TrimSpace(raw)
@@ -415,10 +408,10 @@ func computeRepairedEnhancePrompt(current company.LangPromptMap, seedLegacy stri
if aiprompts.IsLegacyCombinedEnhancePrompt(raw) { if aiprompts.IsLegacyCombinedEnhancePrompt(raw) {
return aiprompts.SplitLegacyCombinedEnhancePrompt(raw), true, false return aiprompts.SplitLegacyCombinedEnhancePrompt(raw), true, false
} }
if aiprompts.CategoryEnhanceHasRoleSections(raw) { if aiprompts.CategoryEnhanceHasRoleSections(raw) && !aiprompts.CategoryEnhancePromptNeedsRepair(raw) {
return security.SanitizePrompt(raw, MaxCategoryPromptRunes), false, false return security.SanitizePrompt(raw, MaxCategoryPromptRunes), false, false
} }
return strings.TrimSpace(aiprompts.CategoryEnhanceUserTemplate), false, true return "", false, true
} }
if preferSeed && seedLegacy != "" { if preferSeed && seedLegacy != "" {
@@ -439,7 +432,7 @@ func computeRepairedEnhancePrompt(current company.LangPromptMap, seedLegacy stri
p, leg, fb := trySeed(seedLegacy) p, leg, fb := trySeed(seedLegacy)
return p, true, leg, fb return p, true, leg, fb
} }
return strings.TrimSpace(aiprompts.CategoryEnhanceUserTemplate), false, false, true return "", false, false, true
} }
func repairCompanyCategoryEnhancePrompts( func repairCompanyCategoryEnhancePrompts(
@@ -485,8 +478,9 @@ func repairCompanyCategoryEnhancePrompts(
seedLegacy := pickSeedPrompt(uniqueID, name, seedByNorm, seedByUID) seedLegacy := pickSeedPrompt(uniqueID, name, seedByNorm, seedByUID)
needPrompt := company.HasAnyPrompt(m) && !categoryEnhancePromptMapOK(m) needPrompt := company.HasAnyPrompt(m) && !categoryEnhancePromptMapOK(m)
// Empty prompt → write sectioned overlay (seed split when available, else shared template). // Empty prompt: only seed-matched categories get an overlay written;
if !company.HasAnyPrompt(m) { // categories without seed content stay on the company default.
if !company.HasAnyPrompt(m) && seedLegacy != "" {
needPrompt = true needPrompt = true
} }
@@ -539,11 +533,14 @@ func repairCompanyCategoryEnhancePrompts(
continue continue
} }
// clearPrompt: repair resolved to "no content" → drop the override so the
// category falls back to the company default (never store template boilerplate).
clearPrompt := false
if needPrompt && wantPrompt == "" { if needPrompt && wantPrompt == "" {
wantPrompt = resolveRepairedEnhancePrompt(m, seedLegacy, forceFromSeed && seedLegacy != "", out) wantPrompt = resolveRepairedEnhancePrompt(m, seedLegacy, forceFromSeed && seedLegacy != "", out)
wantPrompt = security.SanitizePrompt(wantPrompt, MaxCategoryPromptRunes) wantPrompt = security.SanitizePrompt(wantPrompt, MaxCategoryPromptRunes)
if wantPrompt == "" { if wantPrompt == "" {
return updatedHere, fmt.Errorf("repaired prompt empty category=%s", id) clearPrompt = true
} }
} else if needPrompt { } else if needPrompt {
// Record seed/split stats for the prompt we already computed. // Record seed/split stats for the prompt we already computed.
@@ -571,7 +568,18 @@ func repairCompanyCategoryEnhancePrompts(
continue continue
} }
if needPrompt { if needPrompt && clearPrompt {
ct, err := pool.Exec(ctx, `
UPDATE categories
SET prompt = '{}'::jsonb, updated_at = now()
WHERE id = $1 AND company_id = $2`, id, companyID)
if err != nil {
return updatedHere, fmt.Errorf("clear prompt category=%s: %w", id, err)
}
if ct.RowsAffected() == 0 {
return updatedHere, fmt.Errorf("category %s not updated (company mismatch?)", id)
}
} else if needPrompt {
want := company.LangPromptMap{ want := company.LangPromptMap{
"sl": wantPrompt, "sl": wantPrompt,
company.LangPromptAny: wantPrompt, company.LangPromptAny: wantPrompt,
+40 -18
View File
@@ -8,33 +8,33 @@ import (
"github.com/descrybe/descrybe-v2/apps/api/internal/company" "github.com/descrybe/descrybe-v2/apps/api/internal/company"
) )
func TestRepairedCategoryEnhancePromptMap(t *testing.T) { func TestCategoryEnhancePromptMapRepairStates(t *testing.T) {
t.Parallel() t.Parallel()
want, err := repairedCategoryEnhancePromptMap() // Clean split overlay (pure legacy text) is OK under sl and *.
if err != nil { split := aiprompts.SplitLegacyCombinedEnhancePrompt(`GPT predloga:
t.Fatal(err) <name>{Napiši tip izdelka}</name>
<metaDescription>{140 znakov}</metaDescription>
<H2>{benefit}</H2>`)
if split == "" {
t.Fatal("expected split overlay")
} }
tpl := strings.TrimSpace(aiprompts.CategoryEnhanceUserTemplate) if !categoryEnhancePromptMapOK(company.LangPromptMap{"sl": split, company.LangPromptAny: split}) {
if want["sl"] != tpl { t.Fatal("split overlay map should be OK")
t.Fatalf("want prompt->>'sl' = shared template")
} }
if want[company.LangPromptAny] != tpl { if !categoryEnhancePromptMapOK(company.LangPromptMap{"sl": split}) {
t.Fatalf("want * = shared template") t.Fatal("sl-only split map should be OK (idempotent)")
} }
if !categoryEnhancePromptMapOK(want) { if !categoryEnhancePromptMapOK(company.LangPromptMap{company.LangPromptAny: split}) {
t.Fatal("canonical map should be OK") t.Fatal("*-only split map should be OK (idempotent)")
} }
legacy := company.LangPromptMap{"sl": "<H2>legacy HTML marketing</H2>"} legacy := company.LangPromptMap{"sl": "<H2>legacy HTML marketing</H2>"}
if categoryEnhancePromptMapOK(legacy) { if categoryEnhancePromptMapOK(legacy) {
t.Fatal("legacy HTML must need repair") t.Fatal("legacy HTML must need repair")
} }
slOnly := company.LangPromptMap{"sl": tpl} // Stored template boilerplate (old seed versions) must need repair → cleared.
if !categoryEnhancePromptMapOK(slOnly) { tpl := strings.TrimSpace(aiprompts.CategoryEnhanceUserTemplate)
t.Fatal("sl-only repaired map should be OK (idempotent)") if categoryEnhancePromptMapOK(company.LangPromptMap{"sl": tpl, company.LangPromptAny: tpl}) {
} t.Fatal("stored template boilerplate must need repair")
starOnly := company.LangPromptMap{company.LangPromptAny: tpl}
if !categoryEnhancePromptMapOK(starOnly) {
t.Fatal("*-only repaired map should be OK (idempotent)")
} }
} }
@@ -54,6 +54,28 @@ func TestRepairTargetsUseSharedTemplate(t *testing.T) {
} }
} }
func TestComputeRepairedEnhancePromptClears(t *testing.T) {
t.Parallel()
// No seed + no legacy content → clear (fallback), never template boilerplate.
prompt, fromSeed, fromLegacy, fallback := computeRepairedEnhancePrompt(
company.LangPromptMap{"sl": strings.TrimSpace(aiprompts.CategoryEnhanceUserTemplate)}, "", false)
if prompt != "" || fromSeed || fromLegacy || !fallback {
t.Fatalf("want clear fallback, got prompt=%q seed=%v legacy=%v fallback=%v", prompt, fromSeed, fromLegacy, fallback)
}
// Seed legacy → split overlay with the exact Slovenian text.
seed := `GPT predloga:
<name>{Napiši tip izdelka}</name>
<metaDescription>{140 znakov}</metaDescription>
<H2>{benefit}</H2>`
prompt, fromSeed, fromLegacy, fallback = computeRepairedEnhancePrompt(nil, seed, true)
if prompt == "" || !fromSeed || !fromLegacy || fallback {
t.Fatalf("want seed split, got prompt=%q seed=%v legacy=%v fallback=%v", prompt, fromSeed, fromLegacy, fallback)
}
if !strings.Contains(prompt, "140 znakov") || strings.Contains(strings.ToLower(prompt), "build json") {
t.Fatalf("split must keep legacy text without boilerplate: %q", prompt)
}
}
func TestLegacyTitleRulesFromSeedShape(t *testing.T) { func TestLegacyTitleRulesFromSeedShape(t *testing.T) {
t.Parallel() t.Parallel()
seed := `Ustvari nov opis seed := `Ustvari nov opis
+20 -16
View File
@@ -34,19 +34,14 @@ export const LEGACY_ATTRIBUTES_MARKERS = {
end: "--- End Attributes ---" end: "--- End Attributes ---"
} as const; } as const;
/** Canonical shared intro (same idea as CategoryEnhanceUserTemplate preamble). */ /**
* UI-only default intro shown in the editor for context. NEVER stored: the
* stored prompt is exactly the user's section text — schema/role framing and
* product context are supplied by the pipeline at render time.
*/
export const DEFAULT_ENHANCE_PREAMBLE = export const DEFAULT_ENHANCE_PREAMBLE =
"Write all product text in {{language}} (do not hardcode a language). Keep the title, description, and SEO meta consistent with the product evidence below."; "Write all product text in {{language}} (do not hardcode a language). Keep the title, description, and SEO meta consistent with the product evidence below.";
/** Minimal role bodies used when a section is empty on compose (keeps markers valid). */
export const DEFAULT_SECTION_BODIES: Record<EnhancePromptSectionId, string> = {
title:
"Write a short retail product title from the title formula and attributes — never brand-only. Include product type and full model when evidence exists; follow any title formula constraints that follow.\nName: {{name}}",
description:
"Write the product description as HTML (not SEO meta). When a description formula follows, cover each section in order as one HTML string; otherwise prefer 13 factual paragraphs with simple HTML (<h2><p><ul><li>).\nDescription: {{description}}",
meta: "Write the SEO title (5060 characters) and SEO description (120155 characters) as plain text, never HTML. Follow any SEO meta formula that follows; do not copy the full description into the SEO description."
};
export type SectionSchemaHint = { export type SectionSchemaHint = {
/** JSON keys this section is responsible for in the one-shot enhance reply. */ /** JSON keys this section is responsible for in the one-shot enhance reply. */
jsonKeys: string[]; jsonKeys: string[];
@@ -170,20 +165,29 @@ export function parseEnhancePrompt(prompt: string): ParsedEnhancePrompt {
} }
/** /**
* Compose preamble + section bodies back into the stored enhance USER template shape. * Compose section bodies back into the stored enhance USER template shape.
* Empty section bodies fall back to DEFAULT_SECTION_BODIES so markers stay valid. * Stores ONLY the user's text inside the markers — empty sections keep bare
* markers, and the default preamble is never persisted (a custom, non-default
* preamble from an existing prompt is kept).
*/ */
export function composeEnhancePrompt( export function composeEnhancePrompt(
preamble: string, preamble: string,
sections: Partial<Record<EnhancePromptSectionId, string>> sections: Partial<Record<EnhancePromptSectionId, string>>
): string { ): string {
const intro = (preamble.trim() || DEFAULT_ENHANCE_PREAMBLE).trim(); const intro = preamble.trim();
const parts: string[] = [intro, ""]; const parts: string[] = [];
if (intro && intro !== DEFAULT_ENHANCE_PREAMBLE.trim()) {
parts.push(intro, "");
}
for (const id of ENHANCE_PROMPT_SECTIONS) { for (const id of ENHANCE_PROMPT_SECTIONS) {
const { start, end } = SECTION_MARKERS[id]; const { start, end } = SECTION_MARKERS[id];
const body = (sections[id] ?? "").trim() || DEFAULT_SECTION_BODIES[id]; const body = (sections[id] ?? "").trim();
parts.push(start, body, end, ""); if (body) {
parts.push(start, body, end, "");
} else {
parts.push(start, end, "");
}
} }
return parts.join("\n").trim(); return parts.join("\n").trim();
@@ -75,16 +75,22 @@ describe("category prompt sections", () => {
assert.equal(parsed.sections.title, ""); assert.equal(parsed.sections.title, "");
}); });
it("compose fills empty sections with defaults so markers remain", () => { it("compose stores ONLY the user's text — no boilerplate, no preamble", () => {
const out = composeEnhancePrompt(DEFAULT_ENHANCE_PREAMBLE, { const out = composeEnhancePrompt(DEFAULT_ENHANCE_PREAMBLE, {
title: "Custom title rules", title: "Napiši novo ime izdelka po formuli",
description: "", description: "",
meta: "" meta: ""
}); });
assert.equal(hasEnhanceRoleSections(out), true); assert.equal(hasEnhanceRoleSections(out), true);
assert.match(out, /Custom title rules/); assert.match(out, /Napiši novo ime izdelka po formuli/);
assert.match(out, /product description as HTML/); // Empty sections keep bare markers; nothing is injected.
assert.equal(out.includes(DEFAULT_ENHANCE_PREAMBLE), false);
assert.equal(out.includes(LEGACY_ATTRIBUTES_MARKERS.start), false); assert.equal(out.includes(LEGACY_ATTRIBUTES_MARKERS.start), false);
assert.equal(out.toLowerCase().includes("write the"), false);
const reparsed = parseEnhancePrompt(out);
assert.equal(reparsed.sections.title, "Napiši novo ime izdelka po formuli");
assert.equal(reparsed.sections.description, "");
assert.equal(reparsed.sections.meta, "");
}); });
it("isEnhancePromptEmpty ignores default preamble", () => { it("isEnhancePromptEmpty ignores default preamble", () => {
+4 -8
View File
@@ -8,10 +8,7 @@ import { describe, it } from "node:test";
import { UI_LOCALES } from "./i18n/locales.ts"; import { UI_LOCALES } from "./i18n/locales.ts";
import { en } from "./i18n/messages/en.ts"; import { en } from "./i18n/messages/en.ts";
import { loadAllMessages, messagesFor } from "./i18n/messages/catalog.ts"; import { loadAllMessages, messagesFor } from "./i18n/messages/catalog.ts";
import { import { DEFAULT_ENHANCE_PREAMBLE } from "./categories/prompt-sections.ts";
DEFAULT_ENHANCE_PREAMBLE,
DEFAULT_SECTION_BODIES
} from "./categories/prompt-sections.ts";
const CUSTOMER_DEMO_EMPTY_KEYS = ["dashboard.demoEmptyHint", "dashboard.demoEmptyMessage"] as const; const CUSTOMER_DEMO_EMPTY_KEYS = ["dashboard.demoEmptyHint", "dashboard.demoEmptyMessage"] as const;
const CUSTOMER_ETL_KEYS = [ const CUSTOMER_ETL_KEYS = [
@@ -72,9 +69,8 @@ describe("customer-visible copy leaks", () => {
}); });
it("category prompt defaults hide JSON schema jargon from customer text", () => { it("category prompt defaults hide JSON schema jargon from customer text", () => {
const blob = [DEFAULT_ENHANCE_PREAMBLE, ...Object.values(DEFAULT_SECTION_BODIES)].join("\n"); assert.doesNotMatch(DEFAULT_ENHANCE_PREAMBLE, /system schema/i);
assert.doesNotMatch(blob, /system schema/i); assert.doesNotMatch(DEFAULT_ENHANCE_PREAMBLE, /your reply is parsed as json/i);
assert.doesNotMatch(blob, /your reply is parsed as json/i); assert.doesNotMatch(DEFAULT_ENHANCE_PREAMBLE, /build json/i);
assert.doesNotMatch(blob, /build json/i);
}); });
}); });