122 lines
3.4 KiB
Go
122 lines
3.4 KiB
Go
// Rewrite repeated heading/paragraph instructions in A1 Slovenija prompts,
|
|
// keeping the section count and intended product-name inclusion rules.
|
|
// Runs only for the A1 tenant and never rewrites title or meta instructions.
|
|
// Dry run by default; pass -apply to write changes atomically.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/descrybe/descrybe-v2/apps/api/internal/aiprompts"
|
|
"github.com/descrybe/descrybe-v2/apps/api/internal/config"
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
const a1ID = "604f23a8-b66e-4b21-8b45-0d72b68f4790"
|
|
|
|
func normalizeSections(raw string) (string, int, error) {
|
|
return aiprompts.NormalizeA1DescriptionTemplate(raw)
|
|
}
|
|
|
|
func main() {
|
|
config.LoadDotEnv()
|
|
databaseURL := flag.String("postgres", os.Getenv("DATABASE_URL"), "Postgres connection URL")
|
|
apply := flag.Bool("apply", false, "commit changes (default: dry run)")
|
|
flag.Parse()
|
|
if strings.TrimSpace(*databaseURL) == "" {
|
|
log.Fatal("DATABASE_URL is required")
|
|
}
|
|
ctx := context.Background()
|
|
db, err := pgxpool.New(ctx, *databaseURL)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
rows, err := db.Query(ctx, `SELECT c.id, COALESCE(c.prompt, '{}'::jsonb)::text,
|
|
COALESCE(c.description_template::text, '')
|
|
FROM categories c JOIN companies co ON co.id = c.company_id
|
|
WHERE (co.id = $1 OR LOWER(BTRIM(co.name)) = 'a1 slovenija')
|
|
ORDER BY c.name`, uuid.MustParse(a1ID))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
type candidate struct {
|
|
id uuid.UUID
|
|
prompt, desc string
|
|
}
|
|
candidates := make([]candidate, 0)
|
|
for rows.Next() {
|
|
var c candidate
|
|
if err := rows.Scan(&c.id, &c.prompt, &c.desc); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
candidates = append(candidates, c)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
rows.Close()
|
|
if len(candidates) == 0 {
|
|
log.Fatal("no A1 Slovenija categories found; no changes made")
|
|
}
|
|
updated, rewrittenPairs := 0, 0
|
|
for _, c := range candidates {
|
|
var prompts map[string]string
|
|
if err := json.Unmarshal([]byte(c.prompt), &prompts); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if prompts == nil {
|
|
prompts = map[string]string{}
|
|
}
|
|
changed, count := false, 0
|
|
for language, text := range prompts {
|
|
cleaned, n := aiprompts.NormalizeA1RepeatedBenefits(text)
|
|
if n != 0 {
|
|
prompts[language] = cleaned
|
|
changed = true
|
|
count += n
|
|
}
|
|
}
|
|
desc, n, err := normalizeSections(c.desc)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if n != 0 {
|
|
count += n
|
|
changed = true
|
|
}
|
|
if !changed {
|
|
continue
|
|
}
|
|
updated++
|
|
rewrittenPairs += count
|
|
log.Printf("category=%s repeated_pairs_rewritten=%d", c.id, count)
|
|
if !*apply {
|
|
continue
|
|
}
|
|
encoded, err := json.Marshal(prompts)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
tag, err := db.Exec(ctx, `UPDATE categories SET prompt=$2::jsonb,
|
|
description_template=NULLIF($3, '')::jsonb, updated_at=now()
|
|
WHERE id=$1 AND COALESCE(prompt, '{}'::jsonb)::text=$4
|
|
AND COALESCE(description_template::text, '')=$5`,
|
|
c.id, string(encoded), desc, c.prompt, c.desc)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if tag.RowsAffected() != 1 {
|
|
log.Fatal(fmt.Errorf("category %s changed concurrently; rerun dry-run", c.id))
|
|
}
|
|
}
|
|
log.Printf("A1 cleanup: categories_scanned=%d categories_requiring_cleanup=%d rewritten_pairs=%d applied=%v", len(candidates), updated, rewrittenPairs, *apply)
|
|
}
|