Files
TradarrV2/backend/internal/models/generations.go
Blomios 989996fbd6 feat(ai): add personal summary models and retire Q&A
Add an admin-managed provider model catalog with per-user summary selection, migration backfill, and generation snapshots. Retire report creation and messaging while preserving owner-scoped history.

Refs #15 #16

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-21 20:45:21 +01:00

60 lines
2.3 KiB
Go

package models
import (
"context"
"database/sql"
"encoding/json"
"time"
)
type GenerationHistory struct {
ID string `json:"id"`
SummaryID *string `json:"summary_id"`
Outcome json.RawMessage `json:"outcome"`
CreatedAt time.Time `json:"created_at"`
}
// SaveGeneration deliberately accepts no article data and commits both rows together.
func (r *Repository) SaveGeneration(ctx context.Context, userID, id string, summary *Summary, outcome json.RawMessage, createdAt time.Time) error {
tx, err := r.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
var summaryID *string
if summary != nil {
if _, err = tx.ExecContext(ctx, `INSERT INTO summaries (id,user_id,content,ai_provider_id,generated_at,summary_model_id,provider_name,model_name) VALUES ($1,$2,$3,$4,$5,$6,$7,$8)`, summary.ID, userID, summary.Content, summary.AIProviderID, summary.GeneratedAt, summary.SummaryModelID, summary.ProviderName, summary.ModelName); err != nil {
return err
}
summaryID = &summary.ID
}
if _, err = tx.ExecContext(ctx, `INSERT INTO summary_generations (id,user_id,summary_id,outcome,created_at) VALUES ($1,$2,$3,$4,$5)`, id, userID, summaryID, string(outcome), createdAt); err != nil {
return err
}
return tx.Commit()
}
func (r *Repository) ListGenerations(ctx context.Context, userID string, limit int) ([]GenerationHistory, error) {
rows, err := r.db.QueryContext(ctx, `SELECT id,summary_id,outcome,created_at FROM summary_generations WHERE user_id=$1 ORDER BY created_at DESC,id DESC LIMIT $2`, userID, limit)
if err != nil {
return nil, err
}
defer rows.Close()
result := []GenerationHistory{}
for rows.Next() {
var g GenerationHistory
if err = rows.Scan(&g.ID, &g.SummaryID, &g.Outcome, &g.CreatedAt); err != nil {
return nil, err
}
result = append(result, g)
}
return result, rows.Err()
}
func (r *Repository) GetOwnedSummary(id, userID string) (*Summary, error) {
s := &Summary{}
err := r.db.QueryRow(`SELECT id,user_id,content,ai_provider_id,generated_at,summary_model_id,provider_name,model_name FROM summaries WHERE id=$1 AND user_id=$2`, id, userID).Scan(&s.ID, &s.UserID, &s.Content, &s.AIProviderID, &s.GeneratedAt, &s.SummaryModelID, &s.ProviderName, &s.ModelName)
if err == sql.ErrNoRows {
return nil, nil
}
return s, err
}