Changeset 0.28.0.4 (#176)

This commit is contained in:
2026-03-12 12:12:17 +00:00
parent f5171d3bd3
commit 52bd36ba48
16 changed files with 520 additions and 292 deletions

View File

@@ -29,25 +29,28 @@ func (s *AuditStore) Log(ctx context.Context, entry *models.AuditEntry) error {
}
func (s *AuditStore) List(ctx context.Context, opts store.AuditListOptions) ([]models.AuditEntry, int, error) {
b := NewSelect("id, actor_id, action, resource_type, resource_id, metadata, ip_address, user_agent, created_at", "audit_log")
b := NewSelect(`al.id, al.actor_id, COALESCE(u.username, '') AS actor_name,
al.action, al.resource_type, al.resource_id, al.metadata,
al.ip_address, al.user_agent, al.created_at`, "audit_log al")
b.Join("LEFT JOIN users u ON al.actor_id = u.id")
if opts.ActorID != "" {
b.Where("actor_id = ?", opts.ActorID)
b.Where("al.actor_id = ?", opts.ActorID)
}
if opts.Action != "" {
b.Where("action = ?", opts.Action)
b.Where("al.action = ?", opts.Action)
}
if opts.ResourceType != "" {
b.Where("resource_type = ?", opts.ResourceType)
b.Where("al.resource_type = ?", opts.ResourceType)
}
if opts.ResourceID != "" {
b.Where("resource_id = ?", opts.ResourceID)
b.Where("al.resource_id = ?", opts.ResourceID)
}
if opts.Since != nil {
b.Where("created_at >= ?", *opts.Since)
b.Where("al.created_at >= ?", *opts.Since)
}
if opts.Until != nil {
b.Where("created_at <= ?", *opts.Until)
b.Where("al.created_at <= ?", *opts.Until)
}
countQ, countArgs := b.CountBuild()
@@ -55,7 +58,7 @@ func (s *AuditStore) List(ctx context.Context, opts store.AuditListOptions) ([]m
DB.QueryRowContext(ctx, countQ, countArgs...).Scan(&total)
if opts.Sort == "" {
b.OrderBy("created_at", "DESC")
b.OrderBy("al.created_at", "DESC")
}
b.Paginate(opts.ListOptions)
@@ -69,16 +72,40 @@ func (s *AuditStore) List(ctx context.Context, opts store.AuditListOptions) ([]m
var result []models.AuditEntry
for rows.Next() {
var e models.AuditEntry
var actorID sql.NullString
var actorID, actorName sql.NullString
var metadataJSON string
err := rows.Scan(&e.ID, &actorID, &e.Action, &e.ResourceType, &e.ResourceID,
err := rows.Scan(&e.ID, &actorID, &actorName, &e.Action, &e.ResourceType, &e.ResourceID,
&metadataJSON, &e.IPAddress, &e.UserAgent, st(&e.CreatedAt))
if err != nil {
return nil, 0, err
}
e.ActorID = NullableStringPtr(actorID)
if actorName.Valid && actorName.String != "" {
e.ActorName = &actorName.String
}
json.Unmarshal([]byte(metadataJSON), &e.Metadata)
result = append(result, e)
}
return result, total, rows.Err()
}
func (s *AuditStore) ListActions(ctx context.Context) ([]string, error) {
rows, err := DB.QueryContext(ctx,
`SELECT DISTINCT action FROM audit_log ORDER BY action ASC`)
if err != nil {
return nil, err
}
defer rows.Close()
var actions []string
for rows.Next() {
var a string
if rows.Scan(&a) == nil {
actions = append(actions, a)
}
}
if actions == nil {
actions = []string{}
}
return actions, rows.Err()
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/json"
"fmt"
"strings"
"time"
@@ -419,3 +420,55 @@ func (s *ChannelStore) DeleteModelByPersona(ctx context.Context, channelID, pers
channelID, personaID)
return err
}
// ── Admin: Archived Channel Management ──────
func (s *ChannelStore) ListArchived(ctx context.Context, opts store.ListOptions) ([]store.ArchivedChannel, int, error) {
// Count
var total int
DB.QueryRowContext(ctx,
`SELECT COUNT(*) FROM channels WHERE is_archived = 1`).Scan(&total)
rows, err := DB.QueryContext(ctx, `
SELECT c.id, c.title, c.type, COALESCE(u.username, '') AS owner_name,
c.updated_at,
(SELECT COUNT(*) FROM messages WHERE channel_id = c.id) AS message_count
FROM channels c
LEFT JOIN users u ON u.id = c.user_id
WHERE c.is_archived = 1
ORDER BY c.updated_at DESC
LIMIT ? OFFSET ?`, opts.Limit, opts.Offset)
if err != nil {
return nil, 0, err
}
defer rows.Close()
var channels []store.ArchivedChannel
for rows.Next() {
var ch store.ArchivedChannel
if rows.Scan(&ch.ID, &ch.Title, &ch.Type, &ch.OwnerName, &ch.UpdatedAt, &ch.MessageCount) == nil {
channels = append(channels, ch)
}
}
if channels == nil {
channels = []store.ArchivedChannel{}
}
return channels, total, rows.Err()
}
func (s *ChannelStore) Purge(ctx context.Context, id string) error {
// Verify the channel is actually archived
var isArchived int
err := DB.QueryRowContext(ctx,
`SELECT is_archived FROM channels WHERE id = ?`, id).Scan(&isArchived)
if err != nil {
return fmt.Errorf("channel not found")
}
if isArchived == 0 {
return fmt.Errorf("channel must be archived before purging")
}
// Hard delete (CASCADE removes messages, participants, models)
_, err = DB.ExecContext(ctx, `DELETE FROM channels WHERE id = ?`, id)
return err
}

View File

@@ -40,6 +40,7 @@ func NewStores(db *sql.DB) store.Stores {
CapOverrides: NewCapOverrideStore(),
RoutingPolicies: NewRoutingPolicyStore(),
Sessions: NewSessionStore(),
Surfaces: NewSurfaceRegistryStore(),
Workflows: NewWorkflowStore(),
Tasks: NewTaskStore(),
}

View File

@@ -0,0 +1,125 @@
package sqlite
import (
"context"
"database/sql"
"encoding/json"
"git.gobha.me/xcaliber/chat-switchboard/store"
)
type SurfaceRegistryStore struct{}
func NewSurfaceRegistryStore() *SurfaceRegistryStore { return &SurfaceRegistryStore{} }
// Seed upserts a core surface. Called at startup by the page engine
// to ensure core surfaces exist in the registry. Does NOT overwrite
// the enabled state if the row already exists (admin toggle survives restart).
func (s *SurfaceRegistryStore) Seed(ctx context.Context, id, title, source string, manifest map[string]any) error {
manifestJSON := ToJSON(manifest)
_, err := DB.ExecContext(ctx, `
INSERT INTO surface_registry (id, title, source, manifest, enabled, installed_at, updated_at)
VALUES (?, ?, ?, ?, 1, datetime('now'), datetime('now'))
ON CONFLICT (id) DO UPDATE SET
title = excluded.title,
manifest = excluded.manifest,
source = excluded.source,
updated_at = datetime('now')`,
// Note: ON CONFLICT does NOT update 'enabled' — preserves admin toggle
id, title, source, manifestJSON)
return err
}
func (s *SurfaceRegistryStore) List(ctx context.Context) ([]store.SurfaceRegistration, error) {
rows, err := DB.QueryContext(ctx,
`SELECT id, title, manifest, enabled, source, installed_at, updated_at
FROM surface_registry ORDER BY source, title`)
if err != nil {
return nil, err
}
defer rows.Close()
var surfaces []store.SurfaceRegistration
for rows.Next() {
var sr store.SurfaceRegistration
var manifestJSON string
var enabledInt int
if err := rows.Scan(&sr.ID, &sr.Title, &manifestJSON, &enabledInt, &sr.Source, &sr.InstalledAt, &sr.UpdatedAt); err != nil {
continue
}
sr.Enabled = enabledInt != 0
json.Unmarshal([]byte(manifestJSON), &sr.Manifest)
surfaces = append(surfaces, sr)
}
return surfaces, rows.Err()
}
func (s *SurfaceRegistryStore) Get(ctx context.Context, id string) (*store.SurfaceRegistration, error) {
var sr store.SurfaceRegistration
var manifestJSON string
var enabledInt int
err := DB.QueryRowContext(ctx,
`SELECT id, title, manifest, enabled, source, installed_at, updated_at
FROM surface_registry WHERE id = ?`, id).
Scan(&sr.ID, &sr.Title, &manifestJSON, &enabledInt, &sr.Source, &sr.InstalledAt, &sr.UpdatedAt)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
sr.Enabled = enabledInt != 0
json.Unmarshal([]byte(manifestJSON), &sr.Manifest)
return &sr, nil
}
func (s *SurfaceRegistryStore) SetEnabled(ctx context.Context, id string, enabled bool) error {
enabledInt := 0
if enabled {
enabledInt = 1
}
result, err := DB.ExecContext(ctx,
`UPDATE surface_registry SET enabled = ?, updated_at = datetime('now') WHERE id = ?`,
enabledInt, id)
if err != nil {
return err
}
affected, _ := result.RowsAffected()
if affected == 0 {
return sql.ErrNoRows
}
return nil
}
func (s *SurfaceRegistryStore) Delete(ctx context.Context, id string) error {
// Only allow deleting extension surfaces, not core
result, err := DB.ExecContext(ctx,
`DELETE FROM surface_registry WHERE id = ? AND source = 'extension'`, id)
if err != nil {
return err
}
affected, _ := result.RowsAffected()
if affected == 0 {
return sql.ErrNoRows
}
return nil
}
func (s *SurfaceRegistryStore) ListEnabled(ctx context.Context) ([]string, error) {
rows, err := DB.QueryContext(ctx,
`SELECT id FROM surface_registry WHERE enabled = 1 ORDER BY title`)
if err != nil {
return nil, err
}
defer rows.Close()
var ids []string
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
continue
}
ids = append(ids, id)
}
return ids, rows.Err()
}