Files
digital-psychology/apps/api/internal/repository/ai_config_repo.go
T
jackyu66gitandCursor bf1ae8bc53 feat(ECR-023): AICoreConfig KnowledgeSource 只读并 Closed
运营可观测知识源目录(knowledge_sources + admin /ai),不含 Chunk/Embedding。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-08 01:49:58 +08:00

119 lines
3.4 KiB
Go

package repository
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
)
// SystemPromptRow is AICoreConfig SystemPrompt catalog row.
type SystemPromptRow struct {
ID uuid.UUID `json:"id"`
Code string `json:"code"`
Title string `json:"title"`
Scene *string `json:"scene,omitempty"`
Body string `json:"body"`
Version int `json:"version"`
Active bool `json:"active"`
System bool `json:"system"`
UpdatedAt time.Time `json:"updated_at"`
}
// ListSystemPrompts returns prompt catalog (body included for ops read).
func (r *AdminRepo) ListSystemPrompts(ctx context.Context) ([]SystemPromptRow, error) {
rows, err := r.Pool.Query(ctx, `
SELECT id, code, title, scene, body, version, active, system, updated_at
FROM system_prompts
ORDER BY active DESC, code ASC`)
if err != nil {
return nil, err
}
defer rows.Close()
var out []SystemPromptRow
for rows.Next() {
var p SystemPromptRow
if err := rows.Scan(
&p.ID, &p.Code, &p.Title, &p.Scene, &p.Body, &p.Version, &p.Active, &p.System, &p.UpdatedAt,
); err != nil {
return nil, err
}
out = append(out, p)
}
return out, rows.Err()
}
// GetSystemPrompt loads one prompt by id.
func (r *AdminRepo) GetSystemPrompt(ctx context.Context, id uuid.UUID) (*SystemPromptRow, error) {
var p SystemPromptRow
err := r.Pool.QueryRow(ctx, `
SELECT id, code, title, scene, body, version, active, system, updated_at
FROM system_prompts WHERE id=$1`, id,
).Scan(&p.ID, &p.Code, &p.Title, &p.Scene, &p.Body, &p.Version, &p.Active, &p.System, &p.UpdatedAt)
if errors.Is(err, pgx.ErrNoRows) {
return nil, err
}
if err != nil {
return nil, err
}
return &p, nil
}
// KnowledgeSourceRow is AICoreConfig KnowledgeSource catalog row.
type KnowledgeSourceRow struct {
ID uuid.UUID `json:"id"`
Code string `json:"code"`
Title string `json:"title"`
Description *string `json:"description,omitempty"`
SourceKind string `json:"source_kind"`
Version int `json:"version"`
Active bool `json:"active"`
System bool `json:"system"`
UpdatedAt time.Time `json:"updated_at"`
}
// ListKnowledgeSources returns knowledge source catalog.
func (r *AdminRepo) ListKnowledgeSources(ctx context.Context) ([]KnowledgeSourceRow, error) {
rows, err := r.Pool.Query(ctx, `
SELECT id, code, title, description, source_kind, version, active, system, updated_at
FROM knowledge_sources
ORDER BY active DESC, code ASC`)
if err != nil {
return nil, err
}
defer rows.Close()
var out []KnowledgeSourceRow
for rows.Next() {
var k KnowledgeSourceRow
if err := rows.Scan(
&k.ID, &k.Code, &k.Title, &k.Description, &k.SourceKind,
&k.Version, &k.Active, &k.System, &k.UpdatedAt,
); err != nil {
return nil, err
}
out = append(out, k)
}
return out, rows.Err()
}
// GetKnowledgeSource loads one source by id.
func (r *AdminRepo) GetKnowledgeSource(ctx context.Context, id uuid.UUID) (*KnowledgeSourceRow, error) {
var k KnowledgeSourceRow
err := r.Pool.QueryRow(ctx, `
SELECT id, code, title, description, source_kind, version, active, system, updated_at
FROM knowledge_sources WHERE id=$1`, id,
).Scan(
&k.ID, &k.Code, &k.Title, &k.Description, &k.SourceKind,
&k.Version, &k.Active, &k.System, &k.UpdatedAt,
)
if errors.Is(err, pgx.ErrNoRows) {
return nil, err
}
if err != nil {
return nil, err
}
return &k, nil
}