feat(prompts): add prompts loader and configuration management

- Implemented a new prompts loader in `loader.go` to manage prompt templates and configurations.
- Introduced caching mechanism for prompt configurations to optimize loading.
- Added functions to apply platform-specific prompt template overrides.
- Created unit tests in `loader_test.go` to validate prompt configuration loading and reloading behavior.
- Ensured that the last valid configuration is retained in case of errors during reload.
This commit is contained in:
2026-04-13 16:08:12 +08:00
parent 6066f43a7d
commit 1b01caac0f
21 changed files with 1630 additions and 392 deletions
@@ -26,6 +26,9 @@ func (r *cachedTemplateRepository) ListTemplates(ctx context.Context, tenantID i
if data, err := r.cache.Get(ctx, key); err == nil {
var records []TemplateRecord
if err := json.Unmarshal(data, &records); err == nil {
for i := range records {
applyPlatformTemplatePromptOverrides(&records[i])
}
return records, nil
}
}
@@ -38,6 +41,9 @@ func (r *cachedTemplateRepository) ListTemplates(ctx context.Context, tenantID i
if data, err := json.Marshal(records); err == nil {
_ = r.cache.Set(ctx, key, data, templateCacheTTL)
}
for i := range records {
applyPlatformTemplatePromptOverrides(&records[i])
}
return records, nil
}
@@ -47,6 +53,7 @@ func (r *cachedTemplateRepository) GetTemplateByID(ctx context.Context, id, tena
if data, err := r.cache.Get(ctx, key); err == nil {
var record TemplateRecord
if err := json.Unmarshal(data, &record); err == nil {
applyPlatformTemplatePromptOverrides(&record)
return &record, nil
}
}
@@ -59,5 +66,6 @@ func (r *cachedTemplateRepository) GetTemplateByID(ctx context.Context, id, tena
if data, err := json.Marshal(record); err == nil {
_ = r.cache.Set(ctx, key, data, templateCacheTTL)
}
applyPlatformTemplatePromptOverrides(record)
return record, nil
}
@@ -4,6 +4,7 @@ import (
"context"
"time"
"github.com/geo-platform/tenant-api/internal/tenant/prompts"
"github.com/geo-platform/tenant-api/internal/tenant/repository/generated"
)
@@ -60,6 +61,7 @@ func (r *templateRepository) ListTemplates(ctx context.Context, tenantID int64)
CreatedAt: timeFromTimestamp(row.CreatedAt),
UpdatedAt: timeFromTimestamp(row.UpdatedAt),
})
applyPlatformTemplatePromptOverrides(&templates[len(templates)-1])
}
return templates, nil
@@ -74,7 +76,7 @@ func (r *templateRepository) GetTemplateByID(ctx context.Context, id, tenantID i
return nil, err
}
return &TemplateRecord{
record := &TemplateRecord{
ID: row.ID,
Scope: row.Scope,
TenantID: nullableInt64(row.TenantID),
@@ -89,5 +91,18 @@ func (r *templateRepository) GetTemplateByID(ctx context.Context, id, tenantID i
VersionNo: int(row.VersionNo),
CreatedAt: timeFromTimestamp(row.CreatedAt),
UpdatedAt: timeFromTimestamp(row.UpdatedAt),
}, nil
}
applyPlatformTemplatePromptOverrides(record)
return record, nil
}
func applyPlatformTemplatePromptOverrides(record *TemplateRecord) {
if record == nil || record.Scope != "platform" {
return
}
record.PromptTemplate, record.CardConfigJSON = prompts.ApplyPlatformTemplatePromptOverrides(
record.TemplateKey,
record.PromptTemplate,
record.CardConfigJSON,
)
}