Files
descrybe/apps/api/internal/aiprompts/offline_translations.go
T
2026-10-14 19:23:16 +02:00

61 lines
2.1 KiB
Go

package aiprompts
import (
_ "embed"
"encoding/json"
"fmt"
"strings"
"sync"
)
// Shipped, human-readable platform instructions for every supported content
// language. This is offline reference data, not a request to an LLM.
//
//go:embed defaults/prompt-rules-25.json
var offlinePromptRulesJSON []byte
type offlineRules struct {
Title string `json:"title"`
Description string `json:"description"`
Meta string `json:"meta"`
}
var offlinePromptOnce sync.Once
var offlinePrompts map[string]offlineRules
var offlinePromptErr error
// OfflineDefaultPrompt returns a fully localized, A1-style platform template.
// Original category-specific English title instructions continue to live in
// the English wildcard and the structured title_template; this shared default
// uses the standard brand/type/model/color formula for non-English languages.
// Returns an error, rather than silently falling back to English, if a locale
// has no bundled translation.
func OfflineDefaultPrompt(lang string) (string, error) {
offlinePromptOnce.Do(func() {
offlinePromptErr = json.Unmarshal(offlinePromptRulesJSON, &offlinePrompts)
})
if offlinePromptErr != nil {
return "", fmt.Errorf("load bundled prompts: %w", offlinePromptErr)
}
rule, ok := offlinePrompts[lang]
if !ok || strings.TrimSpace(rule.Title) == "" || strings.TrimSpace(rule.Description) == "" || strings.TrimSpace(rule.Meta) == "" {
return "", fmt.Errorf("no bundled category formula for language %q", lang)
}
// The editor and processing pipeline interpret each role section using
// the exact marker syntax below. Body structure is defined in the
// localized Description HTML instructions, not a repetitive fixed-length article.
return "--- Title ---\n" + rule.Title +
"\n--- End Title ---\n\n--- Description ---\n" + rule.Description +
"\n--- End Description ---\n\n--- Meta ---\n" + rule.Meta +
"\n--- End Meta ---", nil
}
func OfflinePromptLanguages() []string {
offlinePromptOnce.Do(func() { offlinePromptErr = json.Unmarshal(offlinePromptRulesJSON, &offlinePrompts) })
out := make([]string, 0, len(offlinePrompts))
for lang := range offlinePrompts {
out = append(out, lang)
}
return out
}