Files
archie/internal/infrastructure/configuration/setup/step_chat.go
T
sam 0ddc43081e feat(setup): implement the interactive setup steps (archie-core-cbk)
internal/infrastructure/configuration/setup.Run drives operator
identity, LLM provider, models, forge, and Telegram chat channel
prompts and returns TOML edits -- not a config.Config. Building a
parallel in-memory representation to validate against was rejected
during design review: it's a second source of truth about the exact
thing (text vs. struct disagreeing) this whole feature exists to stop.
Tests instead render the edits against the real embedded template and
load the result through a real configuration.Loader, proving the
actual defaults/decode/validate path accepts it.

Two boot-time landmines in config.example.toml surfaced by that
approach, both fixed here:

- [providers.openai] ships active with api_key pointed at the bws
  engine. cmd/archied/provider_secrets.go resolves every entry in
  cfg.Providers at startup, not just the ones [models] references, and
  refuses to start the daemon if any fails -- so picking any other
  provider (or picking openai but skipping the key) left a config that
  wouldn't boot on a machine without the bws CLI installed (the common
  case). stepProvider now neutralises providers.openai.api_key to the
  zero-value secret ref whenever openai wasn't the actual choice.
- [[repos]] shipped active with a placeholder owner/name that
  validates cleanly (owner/name are only checked non-empty) and would
  have archied silently polling a repo that doesn't exist. Commented
  out in the template with an explanation; setup does not write this
  section since tomlwrite cannot target array-of-tables.
  TestExampleConfigLoads updated to expect zero repos.

Also: forge and provider secret env-var names are each defined in one
table (forgeToken, cloudProviders) read by both the config edit and
the secret sink, so the two can't name a secret differently -- the
exact bug class this feature exists to close for the installer, tested
directly (TestRun_ForgeTokenKeyMatchesStoredSecretKey compares the
decoded key against the key the secret was actually stored under, not
a hardcoded literal on both sides). Telegram writes chat.telegram.
token_env, not a secret.SecretRef -- TelegramConfig has no such field.
Telegram also requires at least one allowed_user_id: AllowedUserIDs is
deny-by-default, so a bot token with none configured answers nobody,
which reads as broken rather than as the safe default it is.

SecretSink is a caller-supplied sink (Put buffers, Commit persists),
not a return value: Run never hands back plaintext secrets, and the
caller is expected to call Commit only after proving the generated
config loads -- so a validation failure never leaves a secret written
without the config that references it.

Refs archie-core-cbk
2026-08-05 12:58:53 +10:00

89 lines
2.7 KiB
Go

package setup
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite"
)
// telegramTokenEnv is the env var Telegram's bot token is stored under.
// TelegramConfig has no secret.SecretRef field -- unlike the forge and
// provider steps, the config edit is a plain token_env string, not a
// {engine,key} inline table.
const telegramTokenEnv = "ARCHIE_TELEGRAM_TOKEN"
func stepChat(ctx context.Context, p Prompter, secrets SecretSink) (tableEdits, error) {
wantTelegram, err := p.Confirm(ctx, "Configure a Telegram chat channel?", false)
if err != nil {
return nil, fmt.Errorf("setup: telegram: %w", err)
}
if !wantTelegram {
return nil, nil
}
token, err := p.ReadSecret(ctx, "Telegram bot token (from @BotFather): ")
if err != nil {
return nil, fmt.Errorf("setup: telegram token: %w", err)
}
if strings.TrimSpace(token) == "" {
return nil, nil
}
// TelegramConfig.AllowedUserIDs is deny-by-default: a bot token with no
// allowlist answers nobody, which reads as broken rather than as the
// safe default it actually is. Require at least one ID rather than
// shipping a channel that silently does nothing.
idsLine, err := p.ReadLine(ctx, "Allowed Telegram user IDs (comma-separated; required -- the bot answers nobody without this): ", "")
if err != nil {
return nil, fmt.Errorf("setup: telegram allowed user ids: %w", err)
}
ids, err := parseUserIDs(idsLine)
if err != nil {
return nil, fmt.Errorf("setup: telegram allowed user ids: %w", err)
}
if len(ids) == 0 {
return nil, fmt.Errorf("setup: at least one allowed Telegram user ID is required, or the bot will answer nobody")
}
if err := secrets.Put("env", telegramTokenEnv, token); err != nil {
return nil, fmt.Errorf("setup: store telegram token: %w", err)
}
return tableEdits{
"chat.telegram": {
"token_env": tomlwrite.String(telegramTokenEnv),
"allowed_user_ids": intArrayLiteral(ids),
},
}, nil
}
func parseUserIDs(line string) ([]int64, error) {
var ids []int64
for part := range strings.SplitSeq(line, ",") {
part = strings.TrimSpace(part)
if part == "" {
continue
}
id, err := strconv.ParseInt(part, 10, 64)
if err != nil {
return nil, fmt.Errorf("%q is not a valid Telegram user ID: %w", part, err)
}
ids = append(ids, id)
}
return ids, nil
}
// intArrayLiteral renders a single-line TOML integer array. tomlwrite only
// understands single-line values; a Telegram allowlist is short enough
// that this never needs to wrap.
func intArrayLiteral(ids []int64) string {
parts := make([]string, len(ids))
for i, id := range ids {
parts[i] = strconv.FormatInt(id, 10)
}
return "[" + strings.Join(parts, ", ") + "]"
}