diff --git a/internal/app/archiemessaging/compose.go b/internal/app/archiemessaging/compose.go index 79c453788..dfbff7519 100644 --- a/internal/app/archiemessaging/compose.go +++ b/internal/app/archiemessaging/compose.go @@ -171,8 +171,7 @@ func compose(ctx context.Context, d deps) (*Service, error) { } // composeChannels builds the configured channel set: the installed channel -// extensions that extension settings (or a [chat.telegram] file -// configuration) enable. Each instance carries the factory a restart uses to +// extensions that extension settings enable. Each instance carries the factory a restart uses to // build a replacement from a new resolved configuration. func composeChannels(ctx context.Context, d deps) ([]*channelInstance, error) { instances := make([]*channelInstance, 0, len(d.ExtensionChannels)) diff --git a/internal/app/archiemessaging/config.go b/internal/app/archiemessaging/config.go index 880e61129..81363b700 100644 --- a/internal/app/archiemessaging/config.go +++ b/internal/app/archiemessaging/config.go @@ -17,11 +17,7 @@ import ( type ResolvedConfig struct { Options Options // Identity is this process's certificate; nil is plaintext, loopback only. - Identity *rpctls.Identity - // Telegram is the [chat.telegram] configuration. It does not compose a - // channel in this process: it enables the telegram channel extension, with - // the same keys, through openExtensionChannels. - Telegram config.TelegramConfig + Identity *rpctls.Identity ShowToolCalls bool Logging config.Config } @@ -31,7 +27,6 @@ type projection struct { stateStore ServiceTarget messaging config.ServiceConnection tls config.TLS - telegram config.TelegramConfig showToolCalls bool logging config.Config } @@ -64,7 +59,6 @@ func Resolve(o Options, log *slog.Logger) (ResolvedConfig, error) { return ResolvedConfig{ Options: o, Identity: id, - Telegram: proj.telegram, ShowToolCalls: proj.showToolCalls, Logging: proj.logging, }, nil @@ -87,7 +81,6 @@ func project(cfg config.Config) projection { stateStore: ServiceTarget{Target: cfg.Services.Get(config.ServiceNameState).Target}, messaging: cfg.Services.Get(config.ServiceNameMessaging), tls: cfg.TLS, - telegram: cfg.Chat.Telegram, showToolCalls: cfg.Chat.ShowToolCalls, logging: config.Config{StateDir: cfg.StateDir, Log: cfg.Log, Telemetry: cfg.Telemetry}, } diff --git a/internal/app/archiemessaging/enablement_test.go b/internal/app/archiemessaging/enablement_test.go index dfd771cea..9980bc9bb 100644 --- a/internal/app/archiemessaging/enablement_test.go +++ b/internal/app/archiemessaging/enablement_test.go @@ -12,7 +12,6 @@ import ( "github.com/samcharles93/archie-core/internal/channels/status" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/messaging" - "github.com/samcharles93/archie-core/internal/secret" ) // servingChannel reports itself running once it starts, which is what an @@ -214,34 +213,3 @@ func TestReconcileAppliesShowToolCalls(t *testing.T) { t.Fatalf("reconcile stale: %v", err) } } - -// TestTelegramFileSetting pins the mapping that keeps an existing -// [chat.telegram] file configuration working through the channel extension: -// the token stays a secret reference for the host to resolve, the allowlist -// joins the same way the extension parses it, and no token means no setting. -func TestTelegramFileSetting(t *testing.T) { - setting, ok := telegramFileSetting(config.TelegramConfig{ - Token: secret.SecretRef{Engine: "env", Key: "ARCHIE_TELEGRAM_TOKEN"}, - AllowedUserIDs: []int64{111, 222}, - }, true) - if !ok { - t.Fatal("setting not produced for a configured token") - } - if setting.Name != "telegram" || !setting.Enabled { - t.Fatalf("setting = %+v, want name telegram enabled", setting) - } - want := map[string]string{ - "token_ref": "env:ARCHIE_TELEGRAM_TOKEN", - "allowed_user_ids": "111,222", - "show_tool_calls": "true", - } - for key, value := range want { - if setting.Settings[key] != value { - t.Errorf("settings[%q] = %q, want %q", key, setting.Settings[key], value) - } - } - - if _, ok := telegramFileSetting(config.TelegramConfig{}, false); ok { - t.Fatal("empty token produced a setting, want none") - } -} diff --git a/internal/app/archiemessaging/extension_channels.go b/internal/app/archiemessaging/extension_channels.go index c2c4dfd00..df199a914 100644 --- a/internal/app/archiemessaging/extension_channels.go +++ b/internal/app/archiemessaging/extension_channels.go @@ -13,11 +13,9 @@ import ( "github.com/samcharles93/archie-core/internal/app/servicekit" "github.com/samcharles93/archie-core/internal/channels" - "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/storepkg" "github.com/samcharles93/archie-core/internal/infrastructure/channelext" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" - "github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc" "github.com/samcharles93/archie-core/internal/infrastructure/extension" "github.com/samcharles93/archie-core/internal/secret" ) @@ -26,7 +24,7 @@ import ( const refSuffix = "_ref" // telegramChannel is the package name the Telegram channel extension installs -// under, and the config keys [chat.telegram] that enable it. +// under. const telegramChannel = "telegram" // channelFactory builds extension channel instances on demand, so channels @@ -72,12 +70,11 @@ func (f *channelFactory) channelPackage(ctx context.Context, name string) (store } // openExtensionChannels builds a channel for each installed channel package -// whose authority is accepted and that extension-settings enables. A -// [chat.telegram] file configuration enables the telegram package the same -// way, with the same config keys, so an existing config keeps working without -// a server login. Changing the set takes a restart, as every channel binding -// does. Running extensions stop with ctx. -func openExtensionChannels(ctx context.Context, source extension.Source, secrets *secret.Registry, log *slog.Logger, telegram config.TelegramConfig, showToolCalls bool) ([]*channelInstance, *channelFactory) { +// whose authority is accepted and that extension-settings enables. The +// host passes the telegram channel the show_tool_calls session default. +// Changing the set takes a restart, as every channel binding does. Running +// extensions stop with ctx. +func openExtensionChannels(ctx context.Context, source extension.Source, secrets *secret.Registry, log *slog.Logger, showToolCalls bool) ([]*channelInstance, *channelFactory) { cache, err := configuration.DefaultCacheDir() if err != nil { log.WarnContext(ctx, "channel extensions unavailable: no cache directory", "err", err) @@ -93,15 +90,6 @@ func openExtensionChannels(ctx context.Context, source extension.Source, secrets log.WarnContext(ctx, "channel extensions unavailable: installed packages", "err", err) return nil, nil } - if file, ok := telegramFileSetting(telegram, showToolCalls); ok { - stored := settings[telegramChannel] - if stored.Settings == nil { - stored.Settings = map[string]string{} - } - maps.Copy(stored.Settings, file.Settings) - stored.Name, stored.Enabled = telegramChannel, true - settings[telegramChannel] = stored - } host := extension.NewHost(log) factory := &channelFactory{host: host, source: source, cacheDir: filepath.Join(cache, "extensions", "channels"), secrets: secrets} var instances []*channelInstance @@ -126,7 +114,15 @@ func openExtensionChannels(ctx context.Context, source extension.Source, secrets built[pkg.Name] = true continue } - ch, err := factory.build(ctx, pkg, pkg.Descriptor.Contributes.Extensions[index].Path, pkg.Name, setting.Settings) + values := setting.Settings + if pkg.Name == telegramChannel { + values = maps.Clone(values) + if values == nil { + values = map[string]string{} + } + values["show_tool_calls"] = strconv.FormatBool(showToolCalls) + } + ch, err := factory.build(ctx, pkg, pkg.Descriptor.Contributes.Extensions[index].Path, pkg.Name, values) if err != nil { log.WarnContext(ctx, "channel extension not started", "channel", pkg.Name, "err", err) continue @@ -138,11 +134,11 @@ func openExtensionChannels(ctx context.Context, source extension.Source, secrets rebuild: func(ResolvedConfig) (channels.Channel, error) { return ch, nil }, }) } - if _, ok := telegramFileSetting(telegram, showToolCalls); ok && !built[telegramChannel] { - // Report it as a channel, so the dashboard shows why the configured + if setting := settings[telegramChannel]; setting.Enabled && !slices.ContainsFunc(installed, func(pkg storepkg.Installed) bool { return pkg.Name == telegramChannel }) { + // Report it as a channel, so the dashboard shows why the enabled // bot is not running instead of showing nothing. const needs = "The Telegram channel extension is not installed" - log.WarnContext(ctx, "telegram is configured but its channel extension is not installed") + log.WarnContext(ctx, "telegram is enabled but its channel extension is not installed") instances = append(instances, &channelInstance{ name: telegramChannel, needs: needs, @@ -158,29 +154,6 @@ func openExtensionChannels(ctx context.Context, source extension.Source, secrets return instances, factory } -// telegramFileSetting renders [chat.telegram] as the extension setting that -// enables the telegram channel package. The token stays a reference: the host -// resolves token_ref through the secret registry, so this process never holds -// the bot credential. -func telegramFileSetting(cfg config.TelegramConfig, showToolCalls bool) (controlplanerpc.ExtensionSetting, bool) { - if cfg.Token == (secret.SecretRef{}) { - return controlplanerpc.ExtensionSetting{}, false - } - ids := make([]string, 0, len(cfg.AllowedUserIDs)) - for _, id := range cfg.AllowedUserIDs { - ids = append(ids, strconv.FormatInt(id, 10)) - } - return controlplanerpc.ExtensionSetting{ - Name: telegramChannel, - Enabled: true, - Settings: map[string]string{ - "token_ref": cfg.Token.Engine + ":" + cfg.Token.Key, - "allowed_user_ids": strings.Join(ids, ","), - "show_tool_calls": strconv.FormatBool(showToolCalls), - }, - }, true -} - func buildExtensionChannel(ctx context.Context, host *extension.Host, source extension.Source, cacheDir string, pkg storepkg.Installed, file, name string, settings map[string]string, secrets *secret.Registry) (channels.Channel, error) { binary, sum, err := extension.Materialize(ctx, source.Packages, cacheDir, pkg, file) if err != nil { diff --git a/internal/app/archiemessaging/org_channels.go b/internal/app/archiemessaging/org_channels.go index d68838e4c..fe56fcb64 100644 --- a/internal/app/archiemessaging/org_channels.go +++ b/internal/app/archiemessaging/org_channels.go @@ -118,7 +118,7 @@ func (s *Service) planOrgChannel(ctx context.Context, entry controlplanerpc.OrgC switch { case settings.Agent == "": return orgChannelPlan{}, errors.New("no agent named") - case len(settings.Telegram.AllowedUserIDs) == 0: + case len(settings.AllowedUserIDs) == 0: return orgChannelPlan{}, errors.New("no allowed telegram users") case binding == (secret.SecretRef{}): return orgChannelPlan{}, fmt.Errorf("no credential binding %q", telegramCredential) @@ -130,7 +130,7 @@ func (s *Service) planOrgChannel(ctx context.Context, entry controlplanerpc.OrgC if string(principal.Org) != entry.Org || !principal.Kind.Agent() { return orgChannelPlan{}, errors.New("agent is not an agent of this org") } - return orgChannelPlan{org: entry.Org, binding: binding, allowed: settings.Telegram.AllowedUserIDs, show: settings.ShowToolCalls, agent: principal}, nil + return orgChannelPlan{org: entry.Org, binding: binding, allowed: settings.AllowedUserIDs, show: settings.ShowToolCalls, agent: principal}, nil } // telegramBindings maps each org to the secret its Telegram bot token is bound diff --git a/internal/app/archiemessaging/reconcile.go b/internal/app/archiemessaging/reconcile.go index fa207c5ad..61de9e42d 100644 --- a/internal/app/archiemessaging/reconcile.go +++ b/internal/app/archiemessaging/reconcile.go @@ -41,9 +41,7 @@ func (s *Service) reconcileOnce(ctx context.Context) error { if err != nil { return err } - layered, version, err := s.settingsSource.RuntimeChatConfig(ctx, config.ChatConfig{ - Telegram: base.Telegram, ShowToolCalls: base.ShowToolCalls, - }) + layered, version, err := s.settingsSource.RuntimeChatConfig(ctx, config.ChatConfig{ShowToolCalls: base.ShowToolCalls}) if err != nil { return err } @@ -51,7 +49,7 @@ func (s *Service) reconcileOnce(ctx context.Context) error { return nil } next := base - next.Telegram, next.ShowToolCalls = layered.Telegram, layered.ShowToolCalls + next.ShowToolCalls = layered.ShowToolCalls if next.ShowToolCalls != base.ShowToolCalls { s.applyShowToolCalls(next.ShowToolCalls) } diff --git a/internal/app/archiemessaging/run.go b/internal/app/archiemessaging/run.go index 73383d346..4e66c4e8f 100644 --- a/internal/app/archiemessaging/run.go +++ b/internal/app/archiemessaging/run.go @@ -77,17 +77,15 @@ func Run(ctx context.Context, o Options) error { reporter := applystatus.New(applystatus.Messaging, stateStore, log) extensions := extension.Source{Query: controlPlaneClient, Packages: stateStore} defer superviseSecretEngines(ctx, extensions, secrets, reporter, log)() - chatSettings, channelVersion, settingsErr := controlPlaneClient.RuntimeChatConfig(ctx, config.ChatConfig{ - Telegram: cfg.Telegram, ShowToolCalls: cfg.ShowToolCalls, - }) + chatSettings, channelVersion, settingsErr := controlPlaneClient.RuntimeChatConfig(ctx, config.ChatConfig{ShowToolCalls: cfg.ShowToolCalls}) if settingsErr != nil { return fmt.Errorf("load channel settings: %w", settingsErr) } reporter.Report(ctx, controlplanerpc.ChannelSettingsKind, channelVersion, nil) go reporter.Run(ctx) go presence.Run(ctx, presence.Messaging, servicekit.Build(), stateStore, health, log) - cfg.Telegram, cfg.ShowToolCalls = chatSettings.Telegram, chatSettings.ShowToolCalls - extensionChannels, extensionFactory = openExtensionChannels(ctx, extensions, secrets, log, cfg.Telegram, cfg.ShowToolCalls) + cfg.ShowToolCalls = chatSettings.ShowToolCalls + extensionChannels, extensionFactory = openExtensionChannels(ctx, extensions, secrets, log, cfg.ShowToolCalls) channelStatusStore = stateStore settingsSource, applyReporter, appliedVersion, orgChannels = controlPlaneClient, reporter, channelVersion, &orgChannelDeps{Source: controlPlaneClient, Principals: stateStore} } diff --git a/internal/config/chat_frontends.go b/internal/config/chat_frontends.go deleted file mode 100644 index 745431356..000000000 --- a/internal/config/chat_frontends.go +++ /dev/null @@ -1,21 +0,0 @@ -package config - -// ChatFrontEnd is one conversational front-end [chat] can enable. ID is its -// stable identifier. -type ChatFrontEnd struct { - ID string - Name string - Configured bool -} - -// FrontEnds returns every chat front-end this configuration enables, in -// dashboard order. Whitespace-only values do not count as configured. -func (c ChatConfig) FrontEnds() []ChatFrontEnd { - return []ChatFrontEnd{ - { - ID: "telegram", - Name: "Telegram", - Configured: c.Telegram.Token != (SecretRef{}), - }, - } -} diff --git a/internal/infrastructure/controlplanerpc/client.go b/internal/infrastructure/controlplanerpc/client.go index f93b268e0..088fc6c85 100644 --- a/internal/infrastructure/controlplanerpc/client.go +++ b/internal/infrastructure/controlplanerpc/client.go @@ -77,7 +77,6 @@ func RuntimeChatConfigFrom(ctx context.Context, reader ResourceReader, base conf } out = config.ChatConfig{ Operator: settings.Operator, ShowToolCalls: settings.ShowToolCalls, MaxSteps: settings.MaxSteps, - Telegram: config.TelegramConfig{AllowedUserIDs: settings.Telegram.AllowedUserIDs, Token: settings.Telegram.Token}, RateLimit: config.RateLimitConfig{Window: time.Duration(settings.RateLimit.Window), MaxRequests: settings.RateLimit.MaxRequests}, UnrestrictedFilesystem: settings.UnrestrictedFilesystem, Workspace: settings.Workspace, } diff --git a/internal/infrastructure/controlplanerpc/vocabulary.go b/internal/infrastructure/controlplanerpc/vocabulary.go index d4c1dc0c3..68be986be 100644 --- a/internal/infrastructure/controlplanerpc/vocabulary.go +++ b/internal/infrastructure/controlplanerpc/vocabulary.go @@ -7,7 +7,6 @@ import ( "fmt" "time" - "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/distribution" "github.com/samcharles93/archie-core/internal/installtype" "github.com/samcharles93/archie-core/internal/releaseupdate" @@ -19,19 +18,16 @@ type ChannelSettings struct { Operator string `json:"operator"` ShowToolCalls bool `json:"show_tool_calls"` MaxSteps int `json:"max_steps"` - Telegram TelegramSettings `json:"telegram"` RateLimit RateLimitSettings `json:"rate_limit"` UnrestrictedFilesystem bool `json:"unrestricted_filesystem"` Workspace string `json:"workspace"` // Agent is the identity an org's channel chats as. The system org's // channel acts for no one and leaves it empty. Agent string `json:"agent"` -} - -type TelegramSettings struct { - AllowedUserIDs []int64 `json:"allowed_user_ids"` - Token config.SecretRef `json:"token_ref"` - CredentialConfigured bool `json:"credential_configured"` + // AllowedUserIDs lists the Telegram users an org's bot answers. The + // instance's bot takes its users and token from the telegram package's + // extension settings. + AllowedUserIDs []int64 `json:"allowed_user_ids"` } // RateLimitSettings is the inbound rate-limit object, and it reads the Go-cased diff --git a/internal/infrastructure/controlplanestore/channel_settings.go b/internal/infrastructure/controlplanestore/channel_settings.go index cec65d409..0d28ce5d6 100644 --- a/internal/infrastructure/controlplanestore/channel_settings.go +++ b/internal/infrastructure/controlplanestore/channel_settings.go @@ -12,7 +12,6 @@ import ( // Aliases for the channel-settings document defined in controlplanerpc. type ( channelSettings = controlplanerpc.ChannelSettings - telegramSettings = controlplanerpc.TelegramSettings rateLimitSettings = controlplanerpc.RateLimitSettings channelDuration = controlplanerpc.ChannelDuration ) @@ -25,7 +24,6 @@ func seedChannels(cfg config.Config) any { chat := cfg.Chat return channelSettings{ Operator: chat.Operator, Workspace: chat.Workspace, UnrestrictedFilesystem: chat.UnrestrictedFilesystem, ShowToolCalls: chat.ShowToolCalls, MaxSteps: chat.MaxSteps, - Telegram: telegramSettings{AllowedUserIDs: chat.Telegram.AllowedUserIDs, Token: chat.Telegram.Token, CredentialConfigured: chat.Telegram.Token != (config.SecretRef{})}, RateLimit: rateLimitSettings{Window: channelDuration(chat.RateLimit.Window), MaxRequests: chat.RateLimit.MaxRequests}, } } @@ -50,24 +48,24 @@ func normalizeChannels(input []byte) ([]byte, error) { // checkChannelWrite keeps an org to the settings its own channel consumes. // The workspace, filesystem, step, rate-limit and operator settings configure -// the instance's gateway, and the bot token resolves against the instance's -// secret engines, so an org's bot takes its token from the org's credential -// binding named telegram instead. The system org's channel acts for no one, so -// it names no agent. +// the instance's gateway, and an org's bot takes its token from the org's +// credential binding named telegram. The system org's channel acts for no one, +// so it names no agent and no bot users: the instance's bot is the telegram +// package, configured in its extension settings. func checkChannelWrite(orgID string, value []byte) error { var doc channelSettings if err := json.Unmarshal(value, &doc); err != nil { return err } if orgID == storecontract.DefaultOrgID { - if doc.Agent != "" { - return fmt.Errorf("%w: the instance channel acts for no agent", ErrValidation) + if doc.Agent != "" || len(doc.AllowedUserIDs) > 0 { + return fmt.Errorf("%w: the instance channel acts for no agent and its Telegram users are the telegram extension's settings", ErrValidation) } return nil } if doc.Operator != "" || doc.Workspace != "" || doc.UnrestrictedFilesystem || doc.MaxSteps != 0 || - doc.RateLimit != (rateLimitSettings{}) || doc.Telegram.Token != (config.SecretRef{}) { - return fmt.Errorf("%w: operator, workspace, filesystem, step, rate-limit and token settings are the instance's; an org sets its Telegram users, tool-call display and agent, and binds its bot token as the credential telegram", ErrValidation) + doc.RateLimit != (rateLimitSettings{}) { + return fmt.Errorf("%w: operator, workspace, filesystem, step and rate-limit settings are the instance's; an org sets its Telegram users, tool-call display and agent, and binds its bot token as the credential telegram", ErrValidation) } return nil } diff --git a/internal/infrastructure/controlplanestore/channel_settings_test.go b/internal/infrastructure/controlplanestore/channel_settings_test.go index 1f4ae1d5a..7f2d68782 100644 --- a/internal/infrastructure/controlplanestore/channel_settings_test.go +++ b/internal/infrastructure/controlplanestore/channel_settings_test.go @@ -19,12 +19,12 @@ func TestChannelSettingsWritesStayWithinTheirOrg(t *testing.T) { name, org, value string refused bool }{ - {"system org sets the instance channel", storecontract.DefaultOrgID, `{"operator":"sam","workspace":"/srv","telegram":{"allowed_user_ids":[1]}}`, false}, + {"system org sets the instance channel", storecontract.DefaultOrgID, `{"operator":"sam","workspace":"/srv"}`, false}, + {"system org names bot users", storecontract.DefaultOrgID, `{"allowed_user_ids":[1]}`, true}, {"system org names an agent", storecontract.DefaultOrgID, `{"agent":"a1"}`, true}, - {"another org sets its users and agent", "acme", `{"agent":"a1","show_tool_calls":true,"telegram":{"allowed_user_ids":[7]}}`, false}, + {"another org sets its users and agent", "acme", `{"agent":"a1","show_tool_calls":true,"allowed_user_ids":[7]}`, false}, {"another org sets the host workspace", "acme", `{"workspace":"/srv"}`, true}, {"another org lifts the filesystem limit", "acme", `{"unrestricted_filesystem":true}`, true}, - {"another org names a token secret", "acme", `{"telegram":{"token_ref":{"engine":"env","key":"INSTANCE_BOT"}}}`, true}, {"another org sets the instance rate limit", "acme", `{"rate_limit":{"max_requests":5}}`, true}, } for _, tt := range tests { diff --git a/internal/infrastructure/controlplanestore/controlplane.go b/internal/infrastructure/controlplanestore/controlplane.go index 897487f67..32fe1e687 100644 --- a/internal/infrastructure/controlplanestore/controlplane.go +++ b/internal/infrastructure/controlplanestore/controlplane.go @@ -202,7 +202,7 @@ var orgSeededKinds = map[string]bool{ // instance's, so nothing is stored for it and the instance's values are not // copied in. var orgEmptyDocuments = map[string]string{ - ChannelSettingsKind: `{"operator":"","show_tool_calls":false,"max_steps":0,"telegram":{"allowed_user_ids":[],"token_ref":{},"credential_configured":false},"rate_limit":{"window":0,"max_requests":0},"unrestricted_filesystem":false,"workspace":"","agent":""}`, + ChannelSettingsKind: `{"operator":"","show_tool_calls":false,"max_steps":0,"allowed_user_ids":[],"rate_limit":{"window":0,"max_requests":0},"unrestricted_filesystem":false,"workspace":"","agent":""}`, TaskPolicyKind: `{"notify_webhook":"","notify_min_severity":"","notify_secret":{}}`, SchedulingPolicyKind: `{"poll_interval":"","max_retries":0,"dispatch":{"trigger":"","ack_reaction":""}}`, } diff --git a/internal/infrastructure/controlplanestore/operational_settings.go b/internal/infrastructure/controlplanestore/operational_settings.go index fabfd431e..03ad268ac 100644 --- a/internal/infrastructure/controlplanestore/operational_settings.go +++ b/internal/infrastructure/controlplanestore/operational_settings.go @@ -60,9 +60,7 @@ func operationalDefinitions() []Definition { }, Validate: validatePluginSettings}, // Live: each process that supervises extensions re-reads this on its sync // tick, then starts, restarts or stops what changed. - {Kind: ExtensionSettingsKind, Title: "Extension settings", ApplyMode: "live", Document: controlplanerpc.ExtensionSettings{}, Seed: func(cfg config.Config) any { - return controlplanerpc.ExtensionSettings{CatalogueSource: cfg.Distribution.Catalogue, CatalogueKey: cfg.Distribution.CatalogueKey} - }, Validate: validateExtensions}, + {Kind: ExtensionSettingsKind, Title: "Extension settings", ApplyMode: "live", Document: controlplanerpc.ExtensionSettings{}, Seed: seedExtensionSettings, Validate: validateExtensions}, // Live: the container pool reads these on every acquire and the dispatcher // is resized on publish. // Live: every check and install reads the channel afresh. diff --git a/internal/infrastructure/controlplanestore/registry.go b/internal/infrastructure/controlplanestore/registry.go index 10e98b4b8..e2571121a 100644 --- a/internal/infrastructure/controlplanestore/registry.go +++ b/internal/infrastructure/controlplanestore/registry.go @@ -126,6 +126,9 @@ func (s *Server) ImportConfig(ctx context.Context, cfg config.Config) (map[strin skipped = append(skipped, SeedSkip{Kind: resource.Kind, Removed: removed}) } } + if err := s.migrateTelegramConnection(ctx); err != nil { + return nil, nil, err + } return versions, skipped, nil } diff --git a/internal/infrastructure/controlplanestore/telegram_connection.go b/internal/infrastructure/controlplanestore/telegram_connection.go new file mode 100644 index 000000000..9274ac941 --- /dev/null +++ b/internal/infrastructure/controlplanestore/telegram_connection.go @@ -0,0 +1,184 @@ +package controlplanestore + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "maps" + "strconv" + "strings" + "time" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/storecontract" + "github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc" +) + +// telegramPackage is the channel package whose extension settings hold the +// instance's Telegram connection. +const telegramPackage = "telegram" + +// telegramConnection is the instance bot's token reference and allowlist. +type telegramConnection struct { + Token config.SecretRef + Users []int64 +} + +// extensionSetting renders the connection as the telegram package's settings, +// in the encoding the package parses. +func (c telegramConnection) extensionSetting() controlplanerpc.ExtensionSetting { + settings := map[string]string{} + if c.Token != (config.SecretRef{}) { + settings["token_ref"] = c.Token.Engine + ":" + c.Token.Key + } + if len(c.Users) > 0 { + ids := make([]string, len(c.Users)) + for i, id := range c.Users { + ids[i] = strconv.FormatInt(id, 10) + } + settings["allowed_user_ids"] = strings.Join(ids, ",") + } + return controlplanerpc.ExtensionSetting{Name: telegramPackage, Enabled: true, Settings: settings} +} + +// seedExtensionSettings is the extension-settings seed: the catalogue sources +// and, when the file's [chat.telegram] names a token, the telegram package +// enabled with that connection. +func seedExtensionSettings(cfg config.Config) any { + doc := controlplanerpc.ExtensionSettings{CatalogueSource: cfg.Distribution.Catalogue, CatalogueKey: cfg.Distribution.CatalogueKey} + if file := cfg.Chat.Telegram; file.Token != (config.SecretRef{}) { + doc.Extensions = append(doc.Extensions, telegramConnection{Token: file.Token, Users: file.AllowedUserIDs}.extensionSetting()) + } + return doc +} + +// withTelegramConnection adds the connection to the telegram package's +// setting. A value the operator already set there wins, and a package entry +// that exists keeps its enabled state. +func withTelegramConnection(doc controlplanerpc.ExtensionSettings, c telegramConnection) controlplanerpc.ExtensionSettings { + moved := c.extensionSetting() + out := doc + out.Extensions = append([]controlplanerpc.ExtensionSetting(nil), doc.Extensions...) + for i, setting := range out.Extensions { + if setting.Name != telegramPackage { + continue + } + merged := make(map[string]string, len(setting.Settings)+len(moved.Settings)) + maps.Copy(merged, moved.Settings) + for key, value := range setting.Settings { + if value != "" { + merged[key] = value + } + } + setting.Settings = merged + out.Extensions[i] = setting + return out + } + out.Extensions = append(out.Extensions, moved) + return out +} + +// splitLegacyChannelSettings removes the nested telegram object a +// channel-settings document used to carry. The instance's token and users are +// returned for extension-settings; an org keeps its users as the flat +// allowed_user_ids. changed is false for a document with no such object. +func splitLegacyChannelSettings(orgID string, stored []byte) (rewritten []byte, moved telegramConnection, changed bool, err error) { + var doc map[string]json.RawMessage + if err := json.Unmarshal(stored, &doc); err != nil { + return nil, telegramConnection{}, false, err + } + raw, ok := doc["telegram"] + if !ok { + return nil, telegramConnection{}, false, nil + } + var legacy struct { + AllowedUserIDs []int64 `json:"allowed_user_ids"` + TokenRef config.SecretRef `json:"token_ref"` + } + if err := json.Unmarshal(raw, &legacy); err != nil { + return nil, telegramConnection{}, false, fmt.Errorf("decode telegram: %w", err) + } + delete(doc, "telegram") + if orgID == storecontract.DefaultOrgID { + moved = telegramConnection{Token: legacy.TokenRef, Users: legacy.AllowedUserIDs} + } else if len(legacy.AllowedUserIDs) > 0 { + users, err := json.Marshal(legacy.AllowedUserIDs) + if err != nil { + return nil, telegramConnection{}, false, err + } + doc["allowed_user_ids"] = users + } + rewritten, err = json.Marshal(doc) + return rewritten, moved, true, err +} + +// migrateTelegramConnection moves the Telegram connection out of every +// channel-settings document. The instance's token reference and users go to +// the telegram package's extension settings first, so a failure between the two +// writes leaves the legacy object in place to be moved again on the next start. +func (s *Server) migrateTelegramConnection(ctx context.Context) error { + resources, err := s.store.ListResources(ctx) + if err != nil { + return err + } + for _, resource := range resources { + if resource.Kind != ChannelSettingsKind { + continue + } + rewritten, moved, changed, err := splitLegacyChannelSettings(resource.OrgID, resource.Value) + if err != nil { + return fmt.Errorf("migrate %s of org %q: %w", ChannelSettingsKind, resource.OrgID, err) + } + if !changed { + continue + } + if moved.Token != (config.SecretRef{}) { + if err := s.addTelegramConnection(ctx, moved); err != nil { + return err + } + } + value, err := s.definitions[ChannelSettingsKind].Decode(rewritten) + if err != nil { + return fmt.Errorf("migrate %s of org %q: %w", ChannelSettingsKind, resource.OrgID, err) + } + if err := s.putMigrated(ctx, resource.OrgID, ChannelSettingsKind, value, resource.Version); err != nil { + return err + } + } + return nil +} + +func (s *Server) addTelegramConnection(ctx context.Context, c telegramConnection) error { + stored, err := s.store.Resource(ctx, storecontract.DefaultOrgID, ExtensionSettingsKind) + absent := errors.Is(err, storecontract.ErrResourceNotFound) + if err != nil && !absent { + return err + } + var doc controlplanerpc.ExtensionSettings + if !absent { + if err := json.Unmarshal(stored.Value, &doc); err != nil { + return fmt.Errorf("decode %s: %w", ExtensionSettingsKind, err) + } + } + encoded, err := json.Marshal(withTelegramConnection(doc, c)) + if err != nil { + return err + } + value, err := s.definitions[ExtensionSettingsKind].Decode(encoded) + if err != nil { + return fmt.Errorf("migrate telegram connection: %w", err) + } + return s.putMigrated(ctx, storecontract.DefaultOrgID, ExtensionSettingsKind, value, expectedVersion(stored, absent)) +} + +func (s *Server) putMigrated(ctx context.Context, orgID, kind string, value []byte, expected int64) error { + _, err := s.store.PutResource(ctx, storecontract.ResourceWrite{ + OrgID: orgID, Kind: kind, Value: value, Actor: seedActor, Source: seedSource, + RequestID: "migrate:" + orgID + ":" + seedRequestID(kind, value), ExpectedVersion: expected, At: time.Now().UTC(), + }) + if err != nil { + return fmt.Errorf("migrate %s of org %q: %w", kind, orgID, err) + } + return nil +} diff --git a/internal/infrastructure/controlplanestore/telegram_connection_test.go b/internal/infrastructure/controlplanestore/telegram_connection_test.go new file mode 100644 index 000000000..c757b68bf --- /dev/null +++ b/internal/infrastructure/controlplanestore/telegram_connection_test.go @@ -0,0 +1,68 @@ +package controlplanestore + +import ( + "encoding/json" + "reflect" + "testing" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/storecontract" + "github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc" +) + +func TestSplitLegacyChannelSettings(t *testing.T) { + legacy := `{"operator":"sam","telegram":{"allowed_user_ids":[1,2],"token_ref":{"engine":"env","key":"BOT"},"credential_configured":true}}` + tests := []struct { + name, org, stored string + wantChanged bool + wantMoved telegramConnection + wantDoc string + }{ + { + "instance connection moves out", storecontract.DefaultOrgID, legacy, true, + telegramConnection{Token: config.SecretRef{Engine: "env", Key: "BOT"}, Users: []int64{1, 2}}, + `{"operator":"sam"}`, + }, + {"org keeps its users flat and moves nothing", "acme", legacy, true, telegramConnection{}, `{"allowed_user_ids":[1,2],"operator":"sam"}`}, + {"migrated document is left alone", storecontract.DefaultOrgID, `{"operator":"sam"}`, false, telegramConnection{}, ""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + doc, moved, changed, err := splitLegacyChannelSettings(tt.org, []byte(tt.stored)) + if err != nil { + t.Fatal(err) + } + if changed != tt.wantChanged || !reflect.DeepEqual(moved, tt.wantMoved) || string(doc) != tt.wantDoc { + t.Fatalf("got (%s, %+v, %v), want (%s, %+v, %v)", doc, moved, changed, tt.wantDoc, tt.wantMoved, tt.wantChanged) + } + }) + } +} + +func TestWithTelegramConnection(t *testing.T) { + moved := telegramConnection{Token: config.SecretRef{Engine: "env", Key: "BOT"}, Users: []int64{1, 2}} + tests := []struct { + name string + have []controlplanerpc.ExtensionSetting + want controlplanerpc.ExtensionSetting + }{ + { + "enabled when the package has no entry", nil, + controlplanerpc.ExtensionSetting{Name: "telegram", Enabled: true, Settings: map[string]string{"token_ref": "env:BOT", "allowed_user_ids": "1,2"}}, + }, + { + "values already set win and enabled state is kept", + []controlplanerpc.ExtensionSetting{{Name: "telegram", Settings: map[string]string{"token_ref": "bws:OTHER"}}}, + controlplanerpc.ExtensionSetting{Name: "telegram", Settings: map[string]string{"token_ref": "bws:OTHER", "allowed_user_ids": "1,2"}}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := withTelegramConnection(controlplanerpc.ExtensionSettings{Extensions: tt.have}, moved) + if len(got.Extensions) != 1 || !reflect.DeepEqual(got.Extensions[0], tt.want) { + encoded, _ := json.Marshal(got) + t.Fatalf("got %s, want %+v", encoded, tt.want) + } + }) + } +} diff --git a/ui/src/channels/ChannelsPage.vue b/ui/src/channels/ChannelsPage.vue index 84aef58a2..edcd31ebd 100644 --- a/ui/src/channels/ChannelsPage.vue +++ b/ui/src/channels/ChannelsPage.vue @@ -27,16 +27,12 @@ import { useLiveResource, useSessionStore } from "@/stores/session"; const KIND = "channel-settings"; -interface SecretRef { - engine: string; - key: string; -} interface ChannelSettings { operator: string; show_tool_calls: boolean; max_steps: number; models: string[] | null; - telegram: { allowed_user_ids: number[] | null; token_ref: SecretRef; credential_configured: boolean }; + allowed_user_ids: number[] | null; rate_limit: { window: string; max_requests: number }; unrestricted_filesystem: boolean; workspace: string; @@ -134,12 +130,12 @@ const channels = [ { id: "telegram", title: "Telegram" }, ] as const; -const allowedUsers = computed(() => (chat.value?.telegram.allowed_user_ids ?? []).map(String)); +const allowedUsers = computed(() => (chat.value?.allowed_user_ids ?? []).map(String)); // Tag lists are edited in script, so they go through the form's API rather // than a field's handleChange. function setAllowedUsers(ids: string[]) { - form.value?.api.setFieldValue("telegram.allowed_user_ids", ids.filter((id) => /^\d+$/.test(id)).map(Number)); + form.value?.api.setFieldValue("allowed_user_ids", ids.filter((id) => /^\d+$/.test(id)).map(Number)); } diff --git a/ui/src/settings/ChannelSettingsPage.vue b/ui/src/settings/ChannelSettingsPage.vue index 895d7dbbb..758e6f81a 100644 --- a/ui/src/settings/ChannelSettingsPage.vue +++ b/ui/src/settings/ChannelSettingsPage.vue @@ -3,7 +3,7 @@ import { Alert, AlertDescription } from "@/components/ui/alert"; import ApplyStatusRows from "@/settings/ApplyStatusRows.vue"; import KindGate from "@/settings/KindGate.vue"; import HistoryLink from "@/settings/HistoryLink.vue"; -import { computed, onMounted, reactive, ref } from "vue"; +import { computed, onMounted, ref } from "vue"; import ExtensionChannelSettings from "@/settings/ExtensionChannelSettings.vue"; import { useExtensionsStore } from "@/stores/extensions"; import { storeToRefs } from "pinia"; @@ -20,7 +20,6 @@ import { NumberFieldIncrement, NumberFieldInput, } from "@/components/ui/number-field"; -import { Badge } from "@/components/ui/badge"; import { Switch } from "@/components/ui/switch"; import { TagsInput, @@ -31,22 +30,16 @@ import { } from "@/components/ui/tags-input"; import { api } from "@/lib/api"; import { Section } from "@/components/ui/section"; -import SecretRefField from "@/settings/SecretRefField.vue"; import { resourcesForPage, useSettingsStore } from "@/stores/settings"; import { useLiveResource } from "@/stores/session"; const KIND = "channel-settings"; -interface SecretRef { - engine: string; - key: string; -} interface ChannelSettings { operator: string; show_tool_calls: boolean; max_steps: number; models: string[] | null; - telegram: { allowed_user_ids: number[] | null; token_ref: SecretRef; credential_configured: boolean }; rate_limit: { window: string; max_requests: number }; unrestricted_filesystem: boolean; workspace: string; @@ -67,11 +60,10 @@ const form = computed(() => store.formFor(KIND)); const chat = computed(() => store.draftValue(KIND) as ChannelSettings | undefined); const error = computed(() => store.pageErrorFor(KIND)); -// Telegram's connection is the core channel-settings resource; every other -// channel package declares its settings, and the form is built from them. +// Channel packages declare their settings, and each form is built from them. const extensions = useExtensionsStore(); const extensionChannels = computed(() => - extensions.extensions.filter((e) => e.surfaces.includes("channel") && e.name !== "telegram" && e.settings_schema.length), + extensions.extensions.filter((e) => e.surfaces.includes("channel") && e.settings_schema.length), ); const statuses = ref([]); @@ -93,37 +85,8 @@ async function loadStatus() { useLiveResource(null, () => void loadStatus()); onMounted(() => Promise.all([loadStatus(), store.load(), extensions.loadInstalled()])); -// Running state comes only from the messaging service's report; a channel it -// does not mention shows none rather than one guessed from its settings. -function statusOf(id: string) { - const state = statuses.value.find((s) => s.id === id)?.state; - if (!state) return undefined; - if (state === "running") return undefined; - if (state === "failed") return { label: "Failed", tone: "danger" } as const; - if (state === "degraded") return { label: "Degraded", tone: "warn" } as const; - return { label: state[0]!.toUpperCase() + state.slice(1), tone: "idle" } as const; -} - -// A channel is set up once its enabling field is filled in, the rule the -// daemon applies when it decides what to start. -const configured = computed(() => ({ - telegram: !!chat.value?.telegram.token_ref.key, -})); -const opened = reactive({ telegram: false }); -const shown = (id: keyof typeof opened) => configured.value[id] || opened[id]; - -const channels = [ - { id: "telegram", title: "Telegram" }, -] as const; - -const allowedUsers = computed(() => (chat.value?.telegram.allowed_user_ids ?? []).map(String)); const models = computed(() => chat.value?.models ?? []); -// Tag lists are edited in script, so they go through the form's API rather -// than a field's handleChange. -function setAllowedUsers(ids: string[]) { - form.value?.api.setFieldValue("telegram.allowed_user_ids", ids.filter((id) => /^\d+$/.test(id)).map(Number)); -} function statusFor(id: string) { return statuses.value.find((s) => s.id === id); } @@ -152,46 +115,6 @@ function setModels(list: string[]) {