1b01caac0f
- 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.
109 lines
3.3 KiB
Go
109 lines
3.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/geo-platform/tenant-api/internal/tenant/prompts"
|
|
"github.com/geo-platform/tenant-api/internal/tenant/repository/generated"
|
|
)
|
|
|
|
type TemplateRecord struct {
|
|
ID int64
|
|
Scope string
|
|
TenantID *int64
|
|
OriginType string
|
|
TemplateKey string
|
|
TemplateName string
|
|
PromptTemplate *string
|
|
PromptVisibility string
|
|
ProtectedPromptAssetKey *string
|
|
CardConfigJSON []byte
|
|
Status string
|
|
VersionNo int
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
type TemplateRepository interface {
|
|
ListTemplates(ctx context.Context, tenantID int64) ([]TemplateRecord, error)
|
|
GetTemplateByID(ctx context.Context, id, tenantID int64) (*TemplateRecord, error)
|
|
}
|
|
|
|
type templateRepository struct {
|
|
q generated.Querier
|
|
}
|
|
|
|
func NewTemplateRepository(db generated.DBTX) TemplateRepository {
|
|
return &templateRepository{q: newQuerier(db)}
|
|
}
|
|
|
|
func (r *templateRepository) ListTemplates(ctx context.Context, tenantID int64) ([]TemplateRecord, error) {
|
|
rows, err := r.q.ListTemplates(ctx, tenantID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
templates := make([]TemplateRecord, 0, len(rows))
|
|
for _, row := range rows {
|
|
templates = append(templates, TemplateRecord{
|
|
ID: row.ID,
|
|
Scope: row.Scope,
|
|
TenantID: nullableInt64(row.TenantID),
|
|
OriginType: row.OriginType,
|
|
TemplateKey: row.TemplateKey,
|
|
TemplateName: row.TemplateName,
|
|
PromptTemplate: nullableText(row.PromptTemplate),
|
|
PromptVisibility: row.PromptVisibility,
|
|
CardConfigJSON: row.CardConfigJson,
|
|
Status: row.Status,
|
|
VersionNo: int(row.VersionNo),
|
|
CreatedAt: timeFromTimestamp(row.CreatedAt),
|
|
UpdatedAt: timeFromTimestamp(row.UpdatedAt),
|
|
})
|
|
applyPlatformTemplatePromptOverrides(&templates[len(templates)-1])
|
|
}
|
|
|
|
return templates, nil
|
|
}
|
|
|
|
func (r *templateRepository) GetTemplateByID(ctx context.Context, id, tenantID int64) (*TemplateRecord, error) {
|
|
row, err := r.q.GetTemplateByID(ctx, generated.GetTemplateByIDParams{
|
|
ID: id,
|
|
TenantID: tenantID,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
record := &TemplateRecord{
|
|
ID: row.ID,
|
|
Scope: row.Scope,
|
|
TenantID: nullableInt64(row.TenantID),
|
|
OriginType: row.OriginType,
|
|
TemplateKey: row.TemplateKey,
|
|
TemplateName: row.TemplateName,
|
|
PromptTemplate: nullableText(row.PromptTemplate),
|
|
PromptVisibility: row.PromptVisibility,
|
|
ProtectedPromptAssetKey: nullableText(row.ProtectedPromptAssetKey),
|
|
CardConfigJSON: row.CardConfigJson,
|
|
Status: row.Status,
|
|
VersionNo: int(row.VersionNo),
|
|
CreatedAt: timeFromTimestamp(row.CreatedAt),
|
|
UpdatedAt: timeFromTimestamp(row.UpdatedAt),
|
|
}
|
|
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,
|
|
)
|
|
}
|