Files
archie/internal/domain/messaging/transcribe.go
T
sam 2c5dfe51a7 feat(messaging): transcribe Telegram voice notes
A Telegram voice note was stored as the placeholder "[voice message]" and
nothing turned its audio into text, so the agent could not read it. Add a
channel-neutral Transcriber capability to the messaging media layer
(internal/domain/messaging), an ai-sdk Whisper implementation behind it
(internal/infrastructure/transcription), and wire it for Telegram.

The capability is config-driven like the other model roles:
models.transcription names a provider/model, the matching [providers.*]
entry carries the credential (api_key_env or an api_key secret ref), and
a missing role, provider, class or credential degrades to today's
behaviour with a log -- never an error that fails the turn.

A transcribed note replaces its placeholder with "[voice transcription]"
followed by the transcript, preserving any caption, so stored history
shows the agent it is reading a machine transcript.
2026-10-02 09:36:48 +10:00

54 lines
2.2 KiB
Go

package messaging
import (
"context"
"errors"
"strings"
)
// ErrTranscriptionUnavailable reports that the transcription capability
// cannot serve right now: no role configured, an unknown or unsupported
// provider, or a credential that could not be resolved. Infrastructure
// implementations wrap it so a caller can degrade -- keep the media note --
// instead of failing the turn.
var ErrTranscriptionUnavailable = errors.New("transcription capability unavailable")
// Transcriber turns recorded audio into text. It is a channel-neutral,
// call-scoped capability: the messaging media layer defines what a
// transcription is, and each frontend supplies the bytes its platform
// produced. Implementations live in internal/infrastructure/transcription;
// the domain never imports one.
//
// The same shape as image.Provider and embedding.Client: no Lifecycle,
// because "can the capability serve right now" is exactly what a returned
// error already communicates, and a missing capability must degrade rather
// than fail the turn it arrived on.
type Transcriber interface {
// Transcribe returns the transcript of audio, which must be non-empty.
// A failure is an error, never a panic; any error means the caller keeps
// the media note rather than failing the turn.
Transcribe(ctx context.Context, audio []byte) (string, error)
}
// TranscriptionProvenance prefixes text a Transcriber produced. Stored
// history is text-only, so without this marker a later reader (the model in
// a following turn, the dashboard transcript) cannot tell a machine
// transcript from something the sender typed.
const TranscriptionProvenance = "[voice transcription]"
// TranscribedText renders a transcript as the text an inbound voice note
// contributes to the turn, keeping any caption the sender attached after the
// transcript. An empty transcript still yields the provenance marker, so the
// agent knows audio arrived rather than silently treating the message as
// empty.
func TranscribedText(transcript, caption string) string {
parts := []string{TranscriptionProvenance}
if t := strings.TrimSpace(transcript); t != "" {
parts = append(parts, t)
}
if c := strings.TrimSpace(caption); c != "" {
parts = append(parts, c)
}
return strings.Join(parts, "\n")
}