2026-03-26 18:04:53 +11:00
|
|
|
package pubsub
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ChatConfigEventChannel is the pubsub channel for chat config
|
2026-04-30 15:07:33 +02:00
|
|
|
// changes (providers, model configs, user prompts, advisor config).
|
|
|
|
|
// All replicas subscribe to this channel to invalidate their local
|
|
|
|
|
// caches.
|
2026-03-26 18:04:53 +11:00
|
|
|
const ChatConfigEventChannel = "chat:config_change"
|
|
|
|
|
|
|
|
|
|
// HandleChatConfigEvent wraps a typed callback for ChatConfigEvent
|
2026-04-10 10:47:30 +01:00
|
|
|
// messages, following the same pattern as HandleChatWatchEvent.
|
2026-03-26 18:04:53 +11:00
|
|
|
func HandleChatConfigEvent(cb func(ctx context.Context, payload ChatConfigEvent, err error)) func(ctx context.Context, message []byte, err error) {
|
|
|
|
|
return func(ctx context.Context, message []byte, err error) {
|
|
|
|
|
if err != nil {
|
|
|
|
|
cb(ctx, ChatConfigEvent{}, xerrors.Errorf("chat config event pubsub: %w", err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var payload ChatConfigEvent
|
|
|
|
|
if err := json.Unmarshal(message, &payload); err != nil {
|
|
|
|
|
cb(ctx, ChatConfigEvent{}, xerrors.Errorf("unmarshal chat config event: %w", err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cb(ctx, payload, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ChatConfigEvent is published when chat configuration changes
|
2026-04-30 15:07:33 +02:00
|
|
|
// (provider CRUD, model config CRUD, user prompt updates, or advisor
|
|
|
|
|
// config updates). Subscribers use this to invalidate their local
|
|
|
|
|
// caches.
|
2026-03-26 18:04:53 +11:00
|
|
|
type ChatConfigEvent struct {
|
|
|
|
|
Kind ChatConfigEventKind `json:"kind"`
|
|
|
|
|
// EntityID carries context for the invalidation:
|
|
|
|
|
// - For providers: uuid.Nil (all providers are invalidated).
|
|
|
|
|
// - For model configs: the specific config ID.
|
|
|
|
|
// - For user prompts: the user ID.
|
2026-04-30 15:07:33 +02:00
|
|
|
// - For advisor config: uuid.Nil (singleton site-config row).
|
2026-03-26 18:04:53 +11:00
|
|
|
EntityID uuid.UUID `json:"entity_id"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ChatConfigEventKind string
|
|
|
|
|
|
|
|
|
|
const (
|
2026-04-30 15:07:33 +02:00
|
|
|
ChatConfigEventProviders ChatConfigEventKind = "providers"
|
|
|
|
|
ChatConfigEventModelConfig ChatConfigEventKind = "model_config"
|
|
|
|
|
ChatConfigEventUserPrompt ChatConfigEventKind = "user_prompt"
|
|
|
|
|
ChatConfigEventAdvisorConfig ChatConfigEventKind = "advisor_config"
|
2026-03-26 18:04:53 +11:00
|
|
|
)
|