update
This commit is contained in:
1 parent
3bfeb6b61e
commit
7537a41955
30 files changed
+1441
-85
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/company"
|
||||
)
|
||||
|
||||
// DefaultPromptHash versions platform translations by the exact English formula.
|
||||
// Editing a built-in formula therefore cannot accidentally apply stale translations.
|
||||
func DefaultPromptHash(source string) string {
|
||||
sum := sha256.Sum256([]byte(source))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// IsUntouchedDefaultPrompt ensures tenant-authored prompts are NEVER translated,
|
||||
// modified, or mistaken for the platform's default. A language override is
|
||||
// eligible only when it is an exact copy of a previously seeded default.
|
||||
func IsUntouchedDefaultPrompt(prompts company.LangPromptMap, source string, seeded company.LangPromptMap) bool {
|
||||
if strings.TrimSpace(source) == "" {
|
||||
return false
|
||||
}
|
||||
for code, value := range prompts {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
continue
|
||||
}
|
||||
if code == "*" || code == "en" {
|
||||
if value != source {
|
||||
return false
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !company.IsAllowedLanguage(code) || strings.TrimSpace(seeded[code]) != value {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/aiprompts"
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/company"
|
||||
)
|
||||
|
||||
func TestIsUntouchedDefaultPrompt(t *testing.T) {
|
||||
source := aiprompts.GenericCategoryFormula().Overlay
|
||||
seeded := company.LangPromptMap{"de": "Vorgabe", "sl": "Predloga"}
|
||||
for name, prompts := range map[string]company.LangPromptMap{
|
||||
"no stored prompt": {},
|
||||
"shared default": {"*": source},
|
||||
"existing translated default": {"*": source, "de": "Vorgabe", "sl": "Predloga"},
|
||||
"english default": {"en": source},
|
||||
} {
|
||||
if !IsUntouchedDefaultPrompt(prompts, source, seeded) {
|
||||
t.Errorf("%s should be default", name)
|
||||
}
|
||||
}
|
||||
for name, prompts := range map[string]company.LangPromptMap{
|
||||
"custom shared": {"*": "My shop-specific instructions"},
|
||||
"custom english": {"en": "Custom"},
|
||||
"custom german override": {"*": source, "de": "Our custom German copy"},
|
||||
"custom extra language": {"*": source, "fr": "custom"},
|
||||
"unsupported override": {"*": source, "xx": "custom"},
|
||||
} {
|
||||
if IsUntouchedDefaultPrompt(prompts, source, seeded) {
|
||||
t.Errorf("%s must stay custom", name)
|
||||
}
|
||||
}
|
||||
if DefaultPromptHash(source) == DefaultPromptHash("changed "+source) {
|
||||
t.Fatal("hash not versioned by source")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultPromptSourcesCoverGenericAndTaxonomy(t *testing.T) {
|
||||
all := aiprompts.AllDefaultCategoryPrompts()
|
||||
if len(all) < 100 {
|
||||
t.Fatalf("expected embedded taxonomy + generic, got %d", len(all))
|
||||
}
|
||||
generic := aiprompts.GenericCategoryFormula().Overlay
|
||||
found := false
|
||||
for _, s := range all {
|
||||
if s == generic {
|
||||
found = true
|
||||
}
|
||||
if s == "" {
|
||||
t.Fatal("empty default")
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("generic default not included")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user