refactor(messaging): store Telegram's connection in extension-settings (#1802)
deploy / plan (push) Successful in 11s
deploy / archied-image (push) Canceled after 0s
deploy / agent-image (push) Canceled after 0s
deploy / dist-zip (push) Canceled after 0s
deploy / gate (push) Canceled after 7m26s

This commit is contained in:
sam committed 2026-11-08 17:12:59 +11:00
1 parent 617a312ed5
commit af7577625f
19 files changed
+305 -232

No files matched your search

+1 -2
View File
@@ -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))
+1 -8
View File
@@ -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},
}
@@ -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")
}
}
@@ -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 {
+2 -2
View File
@@ -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
+2 -4
View File
@@ -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)
}
+3 -5
View File
@@ -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}
}
-21
View File
@@ -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{}),
},
}
}
@@ -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,
}
@@ -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
@@ -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
}
@@ -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 {
@@ -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":""}}`,
}
@@ -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.
@@ -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
}
@@ -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
}
@@ -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)
}
})
}
}
+3 -7
View File
@@ -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));
}
</script>
+3 -80
View File
@@ -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<ChannelStatus[]>([]);
@@ -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[]) {
</AlertDescription></Alert>
<template v-if="chat">
<Section v-for="channel in channels" :key="channel.id" :title="channel.title" :class="!shown(channel.id) && 'gap-0'">
<template #action>
<div class="flex items-center gap-2">
<Badge v-if="configured[channel.id] && statusOf(channel.id)" :variant="statusOf(channel.id)!.tone">{{
statusOf(channel.id)!.label
}}</Badge>
<Button v-if="!configured[channel.id] && !opened[channel.id]" variant="outline" size="sm" @click="opened[channel.id] = true">Set up</Button>
</div>
</template>
<Alert v-if="statusFor(channel.id)?.state !== 'running' && statusFor(channel.id)?.detail" variant="destructive"><AlertDescription>{{ statusFor(channel.id)!.detail }}</AlertDescription></Alert>
<FieldGroup v-if="form && shown(channel.id) && channel.id === 'telegram'" class="@container/field-group">
<form.Field v-slot="{ field }" name="telegram.token_ref">
<Field orientation="responsive">
<FieldContent>
<FieldLabel for="tg-token-field">Bot token</FieldLabel>
<FieldDescription>Empty disables Telegram.</FieldDescription>
</FieldContent>
<div class="flex min-w-0 flex-wrap items-center gap-3 @md/field-group:flex-1">
<div id="tg-token-field" role="group" aria-label="Bot token" class="contents"><SecretRefField :model-value="field.state.value" id-prefix="tg-token" @update:model-value="field.handleChange" /></div>
</div>
</Field>
</form.Field>
<Field orientation="responsive">
<FieldContent>
<FieldLabel id="channel-allowed-label">Allowed users</FieldLabel>
<FieldDescription>Everyone else is ignored.</FieldDescription>
</FieldContent>
<div class="flex min-w-0 flex-wrap items-center gap-3 @md/field-group:flex-1">
<div role="group" aria-labelledby="channel-allowed-label" class="contents"><TagsInput :model-value="allowedUsers" class="font-mono" @update:model-value="(ids) => setAllowedUsers(ids.map(String))">
<TagsInputItem v-for="id in allowedUsers" :key="id" :value="id">
<TagsInputItemText />
<TagsInputItemDelete />
</TagsInputItem>
<TagsInputInput placeholder="Add user ID" aria-label="Allowed users" />
</TagsInput></div>
</div>
</Field>
</FieldGroup>
</Section>
<ExtensionChannelSettings v-for="extension in extensionChannels" :key="extension.name" :extension="extension" :status="statusFor(extension.name)" />
<Section title="Chat session defaults">