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
@@ -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,
)
}