Files
archie/internal/app/archiemessaging/config.go
T
sam a8ea4e0656 fix(messaging): transcribe voice notes on the model-owning side of the boundary
The Messaging Service built the voice-transcription client in-process
from [models].transcription and [providers.*], which the messaging
boundary PRD reserves to the process that owns the model runtime and its
provider credentials. The deletion gate only measured linked packages,
so the PRD violation shipped green: config reads are not imports.

Move the capability across the boundary:

- archiemessaging no longer reads [models] or [providers], builds no
  transcription client, and drops the ResolvedConfig.Transcriber field.
- telegram classifies a voice note as messaging.MediaTypeVoice and stops
  transcribing; it carries the attachment bytes and its note to the turn,
  which is all a frontend without the credential can do.
- archied builds the transcriber beside the chat runtime from its own
  model-role configuration (setupTranscriber) and gives it to every
  gateway TurnRunner.
- the turn runner transcribes a speech attachment before recording the
  inbound message, so stored history and the model context carry the
  transcript; a nil/failed/empty transcription keeps the frontend note.
- messaging.TranscribedMessageText renders the transcript into the text a
  frontend built from its media note, preserving the sender's caption.

Red first: TestTurnRunnerTranscribesSpeechOnTheModelOwningSide failed at
'Transcribe calls = 0, want 1' with the transcription call disabled.
2026-10-02 11:12:59 +10:00

168 lines
4.9 KiB
Go

package archiemessaging
import (
"errors"
"fmt"
"io/fs"
"log/slog"
"os"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
"github.com/samcharles93/archie-core/internal/secret"
)
// ResolvedConfig contains the resolved inputs needed by the Messaging Service.
type ResolvedConfig struct {
Options Options
TelegramToken string
Telegram config.TelegramConfig
Email config.EmailConfig
Webhook config.WebhookRoute
WebhookSecret string
WebhookAddr string
// WorkDir, BotUser and HealthURL are not channel transport settings. They
// locate this identity's release-announcement and update-report state
// files, and give the update installer the daemon health endpoint to poll
// after it applies one (docs/architecture/migration-decisions.md, "Telegram
// operator surface after extraction").
WorkDir string
BotUser string
HealthURL string
ShowToolCalls bool
}
type projection struct {
gateway ServiceTarget
stateStore ServiceTarget
telegram config.TelegramConfig
email config.EmailConfig
webhook config.WebhookRoute
webhookAddr string
workDir string
botUser string
healthURL string
showToolCalls bool
}
// Resolve loads the configuration, extracts the messaging projection, resolves
// credentials, and applies defaults.
func Resolve(o Options, log *slog.Logger) (ResolvedConfig, error) {
var proj projection
if o.Config != "" {
p, found, err := readProjection(o.Config, o.Overlay, log)
if err != nil {
return ResolvedConfig{}, err
}
if found {
proj = p
o = merge(o, proj)
}
}
o = withDefaults(o)
o = withEnvTokens(o)
if err := o.validate(); err != nil {
return ResolvedConfig{}, err
}
secrets := secret.NewRegistry()
tgToken, err := resolveTelegramToken(proj.telegram, secrets)
if err != nil {
return ResolvedConfig{}, fmt.Errorf("resolve telegram token: %w", err)
}
// An unresolvable webhook secret is not fatal: the route still serves,
// with signature validation off, which is what it did before a secret
// was ever configurable. It is logged so the degradation is visible.
whSecret, err := resolveWebhookSecret(proj.webhook, secrets)
if err != nil {
log.Error("webhook secret unresolvable; starting with signature validation disabled",
"engine", proj.webhook.Secret.Engine, "key", proj.webhook.Secret.Key, "err", err)
}
return ResolvedConfig{
Options: o,
TelegramToken: tgToken,
Telegram: proj.telegram,
Email: proj.email,
Webhook: proj.webhook,
WebhookSecret: whSecret,
WebhookAddr: proj.webhookAddr,
WorkDir: proj.workDir,
BotUser: proj.botUser,
HealthURL: proj.healthURL,
ShowToolCalls: proj.showToolCalls,
}, nil
}
func readProjection(path, overlay string, log *slog.Logger) (projection, bool, error) {
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
return projection{}, false, nil
}
doc, err := configuration.New(log).Resolve(path, overlay)
if err != nil {
return projection{}, false, fmt.Errorf("read messaging configuration: %w", err)
}
return project(doc.Config), true, nil
}
func project(cfg config.Config) projection {
return projection{
gateway: ServiceTarget{
Target: cfg.Services.Get(config.ServiceNameGateway).Target,
Token: cfg.Services.Get(config.ServiceNameGateway).TargetToken,
},
stateStore: ServiceTarget{Target: cfg.Services.Get(config.ServiceNameState).Target, Token: cfg.Services.Get(config.ServiceNameState).TargetToken},
telegram: cfg.Chat.Telegram,
email: cfg.Chat.Email,
webhook: cfg.Chat.Webhook,
webhookAddr: cfg.Chat.WebhookAddr,
workDir: cfg.WorkDir,
botUser: cfg.BotUser,
healthURL: cfg.Health.URL(),
showToolCalls: cfg.Chat.ShowToolCalls,
}
}
func withEnvTokens(o Options) Options {
if o.Gateway.Token == "" {
o.Gateway.Token = os.Getenv("GATEWAY_TOKEN")
}
if o.StateStore.Token == "" {
o.StateStore.Token = os.Getenv("STATE_STORE_TOKEN")
}
return o
}
func merge(o Options, p projection) Options {
if o.Gateway.Target == "" {
o.Gateway.Target = p.gateway.Target
}
if o.Gateway.Token == "" {
o.Gateway.Token = p.gateway.Token
}
if o.StateStore.Target == "" {
o.StateStore.Target = p.stateStore.Target
}
if o.StateStore.Token == "" {
o.StateStore.Token = p.stateStore.Token
}
return o
}
// resolveTelegramToken resolves the configured bot token; an unset
// reference means the channel is off.
func resolveTelegramToken(cfg config.TelegramConfig, secrets *secret.Registry) (string, error) {
if cfg.Token == (secret.SecretRef{}) {
return "", nil
}
return secrets.Resolve(cfg.Token)
}
func resolveWebhookSecret(route config.WebhookRoute, secrets *secret.Registry) (string, error) {
if route.Secret != (secret.SecretRef{}) {
return secrets.Resolve(route.Secret)
}
return "", nil
}