Files
archie/internal/domain/messaging/transcribe.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

84 lines
3.6 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")
}
// TranscribedMessageText renders the text a transcribed speech attachment
// contributes to a message, preserving any caption the sender attached.
//
// messageText is the inbound text a channel frontend rendered for an
// attachment: the media note alone, or the note followed by the sender's
// caption on the next line. The note is a bracketed, self-describing
// placeholder (see internal/channels/telegram's turnMessageText); it is
// replaced by the provenance marker and transcript, and anything after it
// survives as the caption. Text that carries no recognisable note is treated
// as a caption in full, so nothing the sender wrote is dropped.
//
// The transcription happens on the model-owning side of the Messaging
// boundary, which receives the audio bytes but not the caption as a separate
// field; this is the function that turns the frontend's rendered text into
// the transcript the turn is recorded with.
func TranscribedMessageText(messageText, transcript string) string {
note, caption, _ := strings.Cut(messageText, "\n")
if !isMediaNote(note) {
return TranscribedText(transcript, messageText)
}
return TranscribedText(transcript, caption)
}
// isMediaNote reports whether s is a channel frontend's bracketed media
// placeholder such as "[voice message]".
func isMediaNote(s string) bool {
s = strings.TrimSpace(s)
return len(s) >= 2 && s[0] == '[' && s[len(s)-1] == ']'
}