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.
This commit is contained in:
sam committed 2026-10-02 11:12:59 +10:00
1 parent 3a65fd2ee1
commit a8ea4e0656
15 files changed
+430 -215

No files matched your search

+16 -1
View File
@@ -9,6 +9,21 @@ package messaging
// attachment is accepted rather than rejected as an oversized message.
const MaxInboundAttachmentBytes = 20 << 20
// Type values. MediaTypeVoice is speech the sender recorded -- a Telegram
// voice note -- as distinct from MediaTypeAudio, a music or generic audio
// file. The distinction is what tells the model-owning turn runner to
// transcribe a speech attachment before the turn: a voice note's bytes reach
// the Gateway, which owns the provider credential, but the Gateway must not
// spend a transcription call on a forwarded song. The remaining values are
// what they say.
const (
MediaTypeImage = "image"
MediaTypeVideo = "video"
MediaTypeAudio = "audio"
MediaTypeDocument = "document"
MediaTypeVoice = "voice"
)
// MediaAttachment describes a file attached to a message. Platform-agnostic;
// platform-specific fields live in Raw.
//
@@ -16,7 +31,7 @@ const MaxInboundAttachmentBytes = 20 << 20
// "unknown" across a JSON round-trip: a 0-byte file is valid and
// different from a file whose size was never set.
type MediaAttachment struct {
// Type is "image", "video", "audio", or "document".
// Type is "image", "video", "audio", "voice", or "document".
Type string `json:"type"`
// FileID is the platform-assigned identifier for download APIs.
+30
View File
@@ -51,3 +51,33 @@ func TranscribedText(transcript, caption string) string {
}
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] == ']'
}
@@ -2,6 +2,45 @@ package messaging
import "testing"
// TestTranscribedMessageTextReplacesTheMediaNote pins the boundary handoff:
// the frontend renders a voice note as its bracketed note (plus any caption),
// and the model-owning side only has that text when it transcribes. The note
// must be replaced by the transcript; the caption must survive.
func TestTranscribedMessageTextReplacesTheMediaNote(t *testing.T) {
cases := []struct {
name string
messageText string
transcript string
want string
}{
{
name: "note alone is replaced",
messageText: "[voice message]",
transcript: "hello there",
want: "[voice transcription]\nhello there",
},
{
name: "caption after the note survives",
messageText: "[voice message]\nignore the background noise",
transcript: "what time is it",
want: "[voice transcription]\nwhat time is it\nignore the background noise",
},
{
name: "text with no note is kept as the caption",
messageText: "typed it by hand",
transcript: "spoken words",
want: "[voice transcription]\nspoken words\ntyped it by hand",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := TranscribedMessageText(tc.messageText, tc.transcript); got != tc.want {
t.Errorf("TranscribedMessageText(%q, %q) = %q, want %q", tc.messageText, tc.transcript, got, tc.want)
}
})
}
}
// TestTranscribedTextMarksProvenanceAndKeepsCaption pins the stored shape of
// a transcribed voice note: history is text-only and the original bytes are
// gone, so the marker is the only thing that tells a later reader the words