package archiegateway import ( "context" "fmt" "slices" "strings" "sync" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/gateway" "github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog" ) // chatModelManager owns the model selection for interactive chat. Named // aliases remain defaults; choosing a catalog model does not rewrite them // or affect workflow steps. type chatModelManager struct { mu sync.RWMutex aliases map[string]string active map[org.OrgID]string details map[string]gateway.ModelDetails providerNames map[string]string // orgAliases answers the aliases the principal ctx carries may use. Nil // serves every caller the instance aliases. orgAliases func(ctx context.Context) (map[string]string, error) } func newChatModelManager(aliases map[string]string) *chatModelManager { m := &chatModelManager{active: make(map[org.OrgID]string), details: make(map[string]gateway.ModelDetails), providerNames: make(map[string]string)} m.SetConfigured(aliases) return m } // SetConfigured replaces the alias table after a live change to model-aliases. // An available selection survives; otherwise chat returns to the default alias. func (m *chatModelManager) SetConfigured(aliases map[string]string) { m.mu.Lock() defer m.mu.Unlock() m.aliases = aliases m.keepActiveLocked() } // SetModelCatalog replaces the provider display names and per-model details // the catalog publishes. func (m *chatModelManager) SetModelCatalog(snapshot modelcatalog.Snapshot) { m.mu.Lock() defer m.mu.Unlock() m.details = make(map[string]gateway.ModelDetails) m.providerNames = make(map[string]string) for _, provider := range snapshot.Providers { m.providerNames[provider.ID] = provider.Name for _, model := range provider.Models { ref := provider.ID + "/" + model.ID m.details[ref] = gateway.ModelDetails{ Ref: ref, Name: model.Name, ContextWindow: model.ContextWindow, MaxOutputTokens: model.MaxOutputTokens, Reasoning: model.Reasoning, Tools: true, Attachment: model.Attachment, Structured: model.Structured, InputModalities: slices.Clone(model.InputModalities), } } } m.keepActiveLocked() } // keepActiveLocked retains an alias or an available model across live updates. func (m *chatModelManager) keepActiveLocked() { selection := m.selectionLocked(org.DefaultOrgID) if !slices.Contains(config.ChatAliases(m.aliases), selection) && !slices.Contains(chatModelRefs(m.aliases, m.details), selection) { m.active[org.DefaultOrgID] = config.PurposeChat.DefaultAlias() } } func (m *chatModelManager) ProviderDisplayName(provider string) string { m.mu.RLock() defer m.mu.RUnlock() return m.providerNames[provider] } func (m *chatModelManager) ModelDetails(ref string) (gateway.ModelDetails, bool) { m.mu.RLock() defer m.mu.RUnlock() details, ok := m.details[ref] return details, ok } func (m *chatModelManager) selectionLocked(id org.OrgID) string { if selection := m.active[id]; selection != "" { return selection } return config.PurposeChat.DefaultAlias() } func (m *chatModelManager) ActiveAlias(ctx context.Context) string { m.mu.RLock() defer m.mu.RUnlock() selection := m.selectionLocked(org.OrgFromContext(ctx)) if strings.Contains(selection, "/") { return "" } return selection } func (m *chatModelManager) SetActiveModel(ctx context.Context, selection string) error { if err := ctx.Err(); err != nil { return err } aliases, err := m.aliasesFor(ctx) if err != nil { return err } m.mu.Lock() defer m.mu.Unlock() id := org.OrgFromContext(ctx) if id == org.DefaultOrgID || m.orgAliases == nil { aliases = m.aliases } if !slices.Contains(config.ChatAliases(aliases), selection) && !slices.Contains(m.refsLocked(id, aliases), selection) { return fmt.Errorf("model or alias %q is not available", selection) } m.active[id] = selection return nil } // aliasesFor is the alias table ctx's org resolves against: the instance's // for the system org, the org's own answer otherwise. func (m *chatModelManager) aliasesFor(ctx context.Context) (map[string]string, error) { if m.orgAliases == nil || org.OrgFromContext(ctx) == org.DefaultOrgID { m.mu.RLock() defer m.mu.RUnlock() return m.aliases, nil } return m.orgAliases(ctx) } // ModelsFor returns the model references ctx's org may select. func (m *chatModelManager) ModelsFor(ctx context.Context) ([]string, error) { aliases, err := m.aliasesFor(ctx) if err != nil { return nil, err } m.mu.RLock() defer m.mu.RUnlock() id := org.OrgFromContext(ctx) if id == org.DefaultOrgID || m.orgAliases == nil { aliases = m.aliases } return m.refsLocked(id, aliases), nil } // ModelFor resolves the current selection in the caller's org. A direct // selection is checked again so revoked models stop serving turns. func (m *chatModelManager) ModelFor(ctx context.Context) (string, error) { aliases, err := m.aliasesFor(ctx) if err != nil { return "", err } m.mu.RLock() defer m.mu.RUnlock() id := org.OrgFromContext(ctx) if id == org.DefaultOrgID || m.orgAliases == nil { aliases = m.aliases } selection := m.selectionLocked(id) if strings.Contains(selection, "/") { if !slices.Contains(m.refsLocked(id, aliases), selection) { return "", fmt.Errorf("model %q is not available in this org", selection) } return selection, nil } return config.ResolveModel(aliases, selection, config.PurposeChat) } // refsLocked keeps tenant choices within their assigned aliases; the system // org can also select from its credential-filtered provider catalog. func (m *chatModelManager) refsLocked(id org.OrgID, aliases map[string]string) []string { if id != org.DefaultOrgID { return chatModelRefs(aliases, nil) } return chatModelRefs(aliases, m.details) } func chatModelRefs(aliases map[string]string, catalog map[string]gateway.ModelDetails) []string { refs := make(map[string]struct{}, len(catalog)+len(aliases)) for ref := range catalog { refs[ref] = struct{}{} } for _, alias := range config.ChatAliases(aliases) { if ref := strings.TrimSpace(aliases[alias]); ref != "" { refs[ref] = struct{}{} } } out := make([]string, 0, len(refs)) for ref := range refs { out = append(out, ref) } slices.Sort(out) return out }