diff --git a/internal/app/archiegateway/chat_models.go b/internal/app/archiegateway/chat_models.go index c1983bd28..f888743e9 100644 --- a/internal/app/archiegateway/chat_models.go +++ b/internal/app/archiegateway/chat_models.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "slices" + "strings" "sync" "github.com/samcharles93/archie-core/internal/config" @@ -12,9 +13,9 @@ import ( "github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog" ) -// chatModelManager owns the process-local model alias selected for -// interactive chat. Switching it does not rewrite configuration or affect -// workflow steps. +// 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 @@ -33,15 +34,12 @@ func newChatModelManager(aliases map[string]string) *chatModelManager { } // SetConfigured replaces the alias table after a live change to model-aliases. -// The active alias survives when it still exists; otherwise chat returns to -// the default alias. +// 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 - if !slices.Contains(config.ChatAliases(aliases), m.active) { - m.active = config.PurposeChat.DefaultAlias() - } + m.keepActiveLocked() } // SetModelCatalog replaces the provider display names and per-model details @@ -64,6 +62,14 @@ func (m *chatModelManager) SetModelCatalog(snapshot modelcatalog.Snapshot) { } } } + m.keepActiveLocked() +} + +// keepActiveLocked retains an alias or an available model across live updates. +func (m *chatModelManager) keepActiveLocked() { + if !slices.Contains(config.ChatAliases(m.aliases), m.active) && !slices.Contains(chatModelRefs(m.aliases, m.details), m.active) { + m.active = config.PurposeChat.DefaultAlias() + } } func (m *chatModelManager) ProviderDisplayName(provider string) string { @@ -79,16 +85,19 @@ func (m *chatModelManager) ModelDetails(ref string) (gateway.ModelDetails, bool) return details, ok } -// Models returns the aliases chat may switch between. +// Models returns the configured and catalog model references chat may select. func (m *chatModelManager) Models() []string { m.mu.RLock() defer m.mu.RUnlock() - return config.ChatAliases(m.aliases) + return chatModelRefs(m.aliases, m.details) } func (m *chatModelManager) ActiveAlias() string { m.mu.RLock() defer m.mu.RUnlock() + if strings.Contains(m.active, "/") { + return "" + } return m.active } @@ -96,20 +105,27 @@ func (m *chatModelManager) ActiveAlias() string { func (m *chatModelManager) ActiveModel() string { m.mu.RLock() defer m.mu.RUnlock() + if strings.Contains(m.active, "/") { + return m.active + } ref, _ := config.ResolveModel(m.aliases, m.active, config.PurposeChat) return ref } -func (m *chatModelManager) SetActiveModel(ctx context.Context, alias string) error { +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 + } + if !slices.Contains(config.ChatAliases(aliases), selection) && !slices.Contains(m.modelRefsFor(ctx, aliases), selection) { + return fmt.Errorf("model or alias %q is not available", selection) + } m.mu.Lock() defer m.mu.Unlock() - if !slices.Contains(config.ChatAliases(m.aliases), alias) { - return fmt.Errorf("model alias %q is not configured", alias) - } - m.active = alias + m.active = selection return nil } @@ -124,21 +140,59 @@ func (m *chatModelManager) aliasesFor(ctx context.Context) (map[string]string, e return m.orgAliases(ctx) } -// ModelsFor returns the aliases ctx's org may switch between. +// 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 } - return config.ChatAliases(aliases), nil + return m.modelRefsFor(ctx, aliases), nil } -// ModelFor resolves the active alias in ctx's org. An org that cannot use it -// gets an error, never another model. +// 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 } - return config.ResolveModel(aliases, m.ActiveAlias(), config.PurposeChat) + m.mu.RLock() + selection := m.active + m.mu.RUnlock() + if strings.Contains(selection, "/") { + if !slices.Contains(m.modelRefsFor(ctx, aliases), selection) { + return "", fmt.Errorf("model %q is not available in this org", selection) + } + return selection, nil + } + return config.ResolveModel(aliases, selection, config.PurposeChat) +} + +// modelRefsFor keeps tenant choices within their assigned aliases; the +// system org can also choose from its credential-filtered provider catalog. +func (m *chatModelManager) modelRefsFor(ctx context.Context, aliases map[string]string) []string { + if org.OrgFromContext(ctx) != org.DefaultOrgID { + return chatModelRefs(aliases, nil) + } + m.mu.RLock() + defer m.mu.RUnlock() + 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 } diff --git a/internal/app/archiegateway/chat_models_test.go b/internal/app/archiegateway/chat_models_test.go new file mode 100644 index 000000000..d4e129b2f --- /dev/null +++ b/internal/app/archiegateway/chat_models_test.go @@ -0,0 +1,101 @@ +package archiegateway + +import ( + "context" + "slices" + "strings" + "testing" + + "github.com/samcharles93/archie-core/internal/domain/messaging" + "github.com/samcharles93/archie-core/internal/domain/org" + "github.com/samcharles93/archie-core/internal/gateway" + "github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog" +) + +type modelSelectionSessions struct{ gateway.SessionStore } + +func (modelSelectionSessions) List(context.Context) ([]gateway.SessionContext, error) { + return nil, nil +} + +func TestChatModelSelection(t *testing.T) { + for _, tc := range []struct { + name string + org org.OrgID + selection string + want string + denied bool + }{ + {"catalog model", org.DefaultOrgID, "openai/new", "openai/new", false}, + {"configured alias", org.DefaultOrgID, "fast", "openai/small", false}, + {"unknown model", org.DefaultOrgID, "openai/unknown", "openai/old", true}, + {"org model", "tenant", "anthropic/sonnet", "anthropic/sonnet", false}, + {"org alias", "tenant", "tenant-chat", "anthropic/sonnet", false}, + {"other org model", "tenant", "openai/new", "anthropic/sonnet", true}, + {"other org alias", "tenant", "fast", "anthropic/sonnet", true}, + } { + t.Run(tc.name, func(t *testing.T) { + manager := newChatModelManager(map[string]string{"default": "openai/old", "fast": "openai/small"}) + manager.SetModelCatalog(modelcatalog.Snapshot{Providers: []modelcatalog.Provider{ + {ID: "openai", Models: []modelcatalog.Model{{ID: "old"}, {ID: "small"}, {ID: "new"}}}, + {ID: "anthropic", Models: []modelcatalog.Model{{ID: "sonnet"}}}, + }}) + manager.orgAliases = func(context.Context) (map[string]string, error) { + return map[string]string{"default": "anthropic/sonnet", "tenant-chat": "anthropic/sonnet"}, nil + } + ctx := org.WithOrg(t.Context(), tc.org) + router := gateway.NewRouter(nil, nil, "telegram") + router.Models = manager + reply, err := router.Route(ctx, gateway.Inbound{Message: messaging.Message{Text: "/model " + tc.selection}}) + if err != nil { + t.Fatal(err) + } + if denied := strings.HasPrefix(reply, "Cannot switch"); denied != tc.denied { + t.Fatalf("reply = %q, denied = %v", reply, tc.denied) + } + got, err := manager.ModelFor(ctx) + if err != nil || got != tc.want { + t.Fatalf("runtime model = %q, %v; want %q", got, err, tc.want) + } + adapter := &gateway.LocalChatAdapter{Router: router, Sessions: modelSelectionSessions{}, Models: manager} + snapshot, err := adapter.Snapshot(ctx) + if err != nil { + t.Fatal(err) + } + provider, _, _ := strings.Cut(tc.want, "/") + if snapshot.ActiveModel != tc.want || snapshot.ActiveProvider != provider || !slices.Contains(snapshot.ModelsByProvider[provider], tc.want) || !slices.Contains(snapshot.Providers, provider) { + t.Fatalf("selector lost model/provider: %+v", snapshot) + } + if tc.org != org.DefaultOrgID && slices.Contains(snapshot.Models, "openai/new") { + t.Fatal("selector exposed another org's model") + } + profile, err := router.Route(ctx, gateway.Inbound{Message: messaging.Message{Text: "/profile"}}) + if err != nil || !strings.Contains(profile, tc.want) { + t.Fatalf("profile without task identity = %q, %v", profile, err) + } + }) + } +} + +func TestChatModelPickerAfterOrgChange(t *testing.T) { + manager := newChatModelManager(map[string]string{"default": "openai/old"}) + manager.SetModelCatalog(modelcatalog.Snapshot{Providers: []modelcatalog.Provider{{ID: "openai", Models: []modelcatalog.Model{{ID: "new"}}}}}) + manager.orgAliases = func(context.Context) (map[string]string, error) { + return map[string]string{"default": "anthropic/sonnet"}, nil + } + if err := manager.SetActiveModel(t.Context(), "openai/new"); err != nil { + t.Fatal(err) + } + ctx := org.WithOrg(t.Context(), "tenant") + if _, err := manager.ModelFor(ctx); err == nil { + t.Fatal("a tenant could execute the system org's model") + } + adapter := &gateway.LocalChatAdapter{Router: gateway.NewRouter(nil, nil, "telegram"), Sessions: modelSelectionSessions{}, Models: manager} + snapshot, err := adapter.Snapshot(ctx) + if err != nil { + t.Fatal(err) + } + if snapshot.ActiveModel != "" || !slices.Contains(snapshot.ModelsByProvider["anthropic"], "anthropic/sonnet") { + t.Fatalf("picker cannot recover from revoked model: %+v", snapshot) + } +} diff --git a/internal/gateway/chat_local.go b/internal/gateway/chat_local.go index 9077ab23f..e28170ced 100644 --- a/internal/gateway/chat_local.go +++ b/internal/gateway/chat_local.go @@ -52,8 +52,25 @@ func (a *LocalChatAdapter) Snapshot(ctx context.Context) (ChatSnapshot, error) { return ChatSnapshot{}, err } } - s.ActiveModel = a.Models.ActiveAlias() + s.ActiveModel = a.Models.ActiveModel() + if manager, ok := a.Models.(OrgModelManager); ok { + // Keep the picker available when a previous selection was revoked. + s.ActiveModel, _ = manager.ModelFor(ctx) + } + } + s.ActiveProvider, _, _ = strings.Cut(s.ActiveModel, "/") + for _, model := range s.Models { + provider, _, ok := strings.Cut(model, "/") + if !ok { + continue + } + s.ModelsByProvider[provider] = append(s.ModelsByProvider[provider], model) + } + for provider := range s.ModelsByProvider { + s.Providers = append(s.Providers, provider) + } + slices.Sort(s.Providers) if a.Personas != nil { s.Personas = slices.Clone(a.Personas.List()) for _, session := range sessions { diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 3429c6451..88e84e5f8 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -55,19 +55,17 @@ type LLMStreamResponder func(ctx context.Context, in Inbound, stream TurnStream) // runtime provider catalog. When nil on a Router, /model returns a // "not configured" message. type ModelManager interface { - // Models returns the model aliases chat may switch between. + // Models returns the model references chat may switch between. Models() []string - // ActiveAlias returns the active model alias. + // ActiveAlias returns the selected alias, or empty for a direct model choice. ActiveAlias() string - // ActiveModel returns the active alias's "provider/model" ref, or "" - // when the alias is not configured. + // ActiveModel returns the selected "provider/model" reference. ActiveModel() string - // SetActiveModel switches the active alias. Returns an error if the - // alias is unknown. + // SetActiveModel selects an available model reference or a named alias. SetActiveModel(ctx context.Context, alias string) error } -// OrgModelManager is the optional org-aware extension: the aliases and the +// OrgModelManager is the optional org-aware extension: the choices and the // active model for the org of the principal ctx carries. type OrgModelManager interface { ModelManager @@ -354,7 +352,7 @@ func (r *Router) dispatchLocal(ctx context.Context, msg messaging.Message, platf reply, err := r.handleWhoami() return reply, true, err case "/profile": - reply, err := r.handleProfile() + reply, err := r.handleProfile(ctx) return reply, true, err } return r.dispatchLocalMisc(ctx, msg, platform, cmd, rest) @@ -494,11 +492,11 @@ func (r *Router) handleModel(ctx context.Context, arg string) (string, error) { return fmt.Sprintf("Cannot list models: %v", err), nil } if len(models) == 0 { - return "No model aliases configured.\nUsage: /model ", nil + return "No models configured.\nUsage: /model ", nil } var b strings.Builder - b.WriteString("Usage: /model \nAvailable models:\n") - active := r.Models.ActiveAlias() + b.WriteString("Usage: /model \nAvailable models:\n") + active := r.Models.ActiveModel() for _, m := range models { if m == active { fmt.Fprintf(&b, " %s (active)\n", m) @@ -514,7 +512,7 @@ func (r *Router) handleModel(ctx context.Context, arg string) (string, error) { return fmt.Sprintf("Active model set to %s.", arg), nil } -// chatModels lists the aliases the caller's org may switch between. +// chatModels lists the model references the caller's org may switch between. func (r *Router) chatModels(ctx context.Context) ([]string, error) { if manager, ok := r.Models.(OrgModelManager); ok { return manager.ModelsFor(ctx) @@ -908,14 +906,23 @@ func (r *Router) handleWhoami() (string, error) { return b.String(), nil } -func (r *Router) handleProfile() (string, error) { - if r.Identity == "" { - return "Profile is not configured (no identity set).", nil - } +func (r *Router) handleProfile(ctx context.Context) (string, error) { var b strings.Builder - fmt.Fprintf(&b, "Identity: %s\n", r.Identity) + identity := r.Identity + if identity == "" { + identity = "not configured" + } + fmt.Fprintf(&b, "Identity: %s\n", identity) if r.Models != nil { - fmt.Fprintf(&b, "Model: %s\n", r.Models.ActiveModel()) + model := r.Models.ActiveModel() + if manager, ok := r.Models.(OrgModelManager); ok { + var err error + model, err = manager.ModelFor(ctx) + if err != nil { + return "", err + } + } + fmt.Fprintf(&b, "Model: %s\n", model) } return strings.TrimSpace(b.String()), nil }