This commit is contained in:
greeneclipse committed 2026-10-14 18:18:54 +02:00
1 parent 3bfeb6b61e
commit 7537a41955
30 files changed
+1441 -85

No files matched your search

+12 -7
View File
@@ -30,7 +30,7 @@ const applyDefaultCategoryFormulasSQL = `
UPDATE categories AS c SET
prompt = CASE
WHEN c.prompt IS NULL OR c.prompt = '{}'::jsonb
THEN v.prompt::jsonb ELSE c.prompt END,
THEN v.prompt::jsonb || v.translations ELSE c.prompt END,
title_template = CASE
WHEN c.title_template IS NULL OR c.title_template::text IN ('null', '{}')
THEN NULLIF(v.title_template, '')::jsonb ELSE c.title_template END,
@@ -39,13 +39,16 @@ const applyDefaultCategoryFormulasSQL = `
THEN NULLIF(v.description_template, '')::jsonb ELSE c.description_template END,
updated_at = now()
FROM (
SELECT * FROM unnest($2::uuid[], $3::text[], $4::text[], $5::text[])
AS t(id, prompt, title_template, description_template)
SELECT t.id, t.prompt, t.title_template, t.description_template,
COALESCE(cache.translations, '{}'::jsonb) AS translations
FROM unnest($2::uuid[], $3::text[], $4::text[], $5::text[], $6::text[])
AS t(id, prompt, title_template, description_template, source_hash)
LEFT JOIN default_category_prompt_translations cache ON cache.source_hash = t.source_hash
) AS v
WHERE c.id = v.id AND c.company_id = $1`
// ApplyDefaultCategoryFormulas gives newly created categories the platform-default
// formulas (English, same style as the A1 legacy templates) when they have none.
// formulas and any seeded translations when they have none.
//
// New categories get a working title/description/meta formula out of the box
// instead of falling through to generic "1-3 paragraphs" copy. Existing rows are
@@ -56,6 +59,7 @@ func ApplyDefaultCategoryFormulas(ctx context.Context, db pgxExecer, companyID u
return nil
}
ids := make([]uuid.UUID, 0, len(targets))
hashes := make([]string, 0, len(targets))
prompts := make([]string, 0, len(targets))
titles := make([]string, 0, len(targets))
descs := make([]string, 0, len(targets))
@@ -67,12 +71,13 @@ func ApplyDefaultCategoryFormulas(ctx context.Context, db pgxExecer, companyID u
if def.Overlay == "" {
continue
}
// "*" applies to every content language: the formula text is English while
// {{language}} still selects the output language at render time.
// Keep the English wildcard for an explicit fallback if a translation
// is missing; the seed's language variants take precedence.
promptJSON, err := company.EncodeLangPromptMap(company.LangPromptMap{"*": def.Overlay})
if err != nil {
return err
}
hashes = append(hashes, DefaultPromptHash(def.Overlay))
ids = append(ids, t.ID)
prompts = append(prompts, string(promptJSON))
titles = append(titles, def.TitleTemplateJSON)
@@ -81,6 +86,6 @@ func ApplyDefaultCategoryFormulas(ctx context.Context, db pgxExecer, companyID u
if len(ids) == 0 {
return nil
}
_, err := db.Exec(ctx, applyDefaultCategoryFormulasSQL, companyID, ids, prompts, titles, descs)
_, err := db.Exec(ctx, applyDefaultCategoryFormulasSQL, companyID, ids, prompts, titles, descs, hashes)
return err
}