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>
This commit is contained in:
2026-11-21 20:45:21 +01:00
parent ac17d27f51
commit 989996fbd6
53 changed files with 2228 additions and 979 deletions

View File

@ -0,0 +1,94 @@
package models
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
"github.com/lib/pq"
)
var ErrSummaryModelUnavailable = errors.New("summary_model_unavailable")
var ErrCatalogConflict = errors.New("summary_model_conflict")
type SummaryModel struct {
ID string `json:"id"`
ProviderID string `json:"provider_id"`
Provider string `json:"provider"`
Model string `json:"model"`
Enabled bool `json:"enabled"`
Label string `json:"label"`
}
type ResolvedSummaryModel struct {
ID, ProviderID, Provider, Model, Endpoint, APIKeyEncrypted string
}
func (r *Repository) ListSummaryModels(enabledOnly bool) ([]SummaryModel, error) {
rows, err := r.db.Query(`SELECT m.id,m.provider_id,p.name,m.model,m.enabled FROM ai_provider_models m JOIN ai_providers p ON p.id=m.provider_id WHERE NOT $1 OR m.enabled ORDER BY p.name,m.model,m.id`, enabledOnly)
if err != nil {
return nil, err
}
defer rows.Close()
result := []SummaryModel{}
for rows.Next() {
var m SummaryModel
if err = rows.Scan(&m.ID, &m.ProviderID, &m.Provider, &m.Model, &m.Enabled); err != nil {
return nil, err
}
m.Label = m.Provider + " / " + m.Model
result = append(result, m)
}
return result, rows.Err()
}
func catalogError(err error) error {
var pg *pq.Error
if errors.As(err, &pg) {
switch pg.Code {
case "23503", "23505":
return ErrCatalogConflict
}
}
return err
}
func (r *Repository) CreateSummaryModel(providerID, model string, enabled bool) (*SummaryModel, error) {
model = strings.TrimSpace(model)
if model == "" {
return nil, ErrSummaryModelUnavailable
}
m := &SummaryModel{ProviderID: providerID, Model: model, Enabled: enabled}
err := r.db.QueryRow(`WITH inserted AS (INSERT INTO ai_provider_models(id,provider_id,model,enabled) VALUES(gen_random_uuid(),$1,$2,$3) RETURNING id,provider_id,model,enabled) SELECT i.id,p.name FROM inserted i JOIN ai_providers p ON p.id=i.provider_id`, providerID, model, enabled).Scan(&m.ID, &m.Provider)
m.Label = m.Provider + " / " + m.Model
return m, catalogError(err)
}
func (r *Repository) UpdateSummaryModel(id string, enabled bool) error {
var found string
err := r.db.QueryRow(`UPDATE ai_provider_models SET enabled=$1 WHERE id=$2 RETURNING id`, enabled, id).Scan(&found)
return err
}
func (r *Repository) DeleteSummaryModel(id string) error {
var found string
err := r.db.QueryRow(`DELETE FROM ai_provider_models WHERE id=$1 RETURNING id`, id).Scan(&found)
return catalogError(err)
}
// Resolution is independent from active-provider/global-role state. The user
// and enabled catalogue row must agree at the moment the generation starts.
func (r *Repository) ResolveSummaryModel(ctx context.Context, userID string, id *string) (ResolvedSummaryModel, error) {
if id == nil {
return ResolvedSummaryModel{}, ErrSummaryModelUnavailable
}
var m ResolvedSummaryModel
err := r.db.QueryRowContext(ctx, `SELECT m.id,p.id,p.name,m.model,p.endpoint,p.api_key_encrypted FROM users u JOIN ai_provider_models m ON m.id=u.summary_model_id JOIN ai_providers p ON p.id=m.provider_id WHERE u.id=$1 AND m.id=$2 AND m.enabled`, userID, *id).Scan(&m.ID, &m.ProviderID, &m.Provider, &m.Model, &m.Endpoint, &m.APIKeyEncrypted)
if err == sql.ErrNoRows {
return m, ErrSummaryModelUnavailable
}
if err != nil {
return m, fmt.Errorf("resolve summary model: %w", err)
}
if strings.TrimSpace(m.Model) == "" {
return m, ErrSummaryModelUnavailable
}
return m, nil
}