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:
1 parent
3a65fd2ee1
commit
a8ea4e0656
15 files changed
+430
-215
No files matched your search
@@ -21,9 +21,6 @@ type inboundMedia struct {
|
||||
// note is a bracketed, self-describing placeholder such as "[photo]"
|
||||
// or "[document: report.pdf]".
|
||||
note string
|
||||
// transcribe marks audio a configured Transcriber should turn into text
|
||||
// (a Telegram voice note). False leaves the note as-is.
|
||||
transcribe bool
|
||||
}
|
||||
|
||||
// extractInboundMedia maps a Telegram message onto the attachment it
|
||||
@@ -73,12 +70,12 @@ func extractInboundMedia(msg *models.Message) (inboundMedia, bool) {
|
||||
|
||||
case msg.Voice != nil:
|
||||
return inboundMedia{attachment: messaging.MediaAttachment{
|
||||
Type: "audio",
|
||||
Type: messaging.MediaTypeVoice,
|
||||
FileID: msg.Voice.FileID,
|
||||
MIMEType: msg.Voice.MimeType,
|
||||
FileSize: nilIfZero(msg.Voice.FileSize),
|
||||
Duration: &msg.Voice.Duration,
|
||||
}, note: "[voice message]", transcribe: true}, true
|
||||
}, note: "[voice message]"}, true
|
||||
}
|
||||
return inboundMedia{}, false
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ func TestExtractInboundMediaMapsTelegramKinds(t *testing.T) {
|
||||
msg: &models.Message{
|
||||
Voice: &models.Voice{FileID: "voice1", MimeType: "audio/ogg", Duration: 8},
|
||||
},
|
||||
wantType: "audio",
|
||||
wantType: messaging.MediaTypeVoice,
|
||||
wantFileID: "voice1",
|
||||
wantMIME: "audio/ogg",
|
||||
wantNote: "[voice message]",
|
||||
@@ -333,24 +333,6 @@ func TestDefaultHandlerSilentForNonContentMessages(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// fakeTranscriber records the audio it was handed and returns a fixed
|
||||
// transcript, so a test can assert both that transcription ran on the
|
||||
// downloaded bytes and what the turn then carried.
|
||||
type fakeTranscriber struct {
|
||||
text string
|
||||
err error
|
||||
calls int
|
||||
audio []byte
|
||||
}
|
||||
|
||||
var _ messaging.Transcriber = (*fakeTranscriber)(nil)
|
||||
|
||||
func (f *fakeTranscriber) Transcribe(_ context.Context, audio []byte) (string, error) {
|
||||
f.calls++
|
||||
f.audio = append([]byte(nil), audio...)
|
||||
return f.text, f.err
|
||||
}
|
||||
|
||||
// voiceUpdate is a Telegram voice note from the authorized sender.
|
||||
func voiceUpdate(fileID string) *models.Message {
|
||||
return &models.Message{
|
||||
@@ -362,11 +344,12 @@ func voiceUpdate(fileID string) *models.Message {
|
||||
}
|
||||
}
|
||||
|
||||
// TestDefaultHandlerTranscribesVoiceNoteIntoText drives the behaviour this
|
||||
// feature exists for: a configured Transcriber turns the downloaded voice
|
||||
// note into the turn's text, behind a provenance marker so the agent knows
|
||||
// it is reading a machine transcript.
|
||||
func TestDefaultHandlerTranscribesVoiceNoteIntoText(t *testing.T) {
|
||||
// TestDefaultHandlerMarksVoiceNoteAsSpeech drives the frontend half of the
|
||||
// boundary: the messaging process downloads the note, classifies it as speech
|
||||
// (messaging.MediaTypeVoice), and carries the bytes and the note to the turn.
|
||||
// Turning those bytes into text is the model-owning process's job -- this
|
||||
// process holds no provider credential -- so no transcription happens here.
|
||||
func TestDefaultHandlerMarksVoiceNoteAsSpeech(t *testing.T) {
|
||||
voiceBytes := []byte("ogg-voice-bytes")
|
||||
b, _ := newRecordedAPI(t, func(w http.ResponseWriter, r *http.Request) bool {
|
||||
if strings.HasPrefix(r.URL.Path, "/file/bot1:test/") {
|
||||
@@ -386,72 +369,30 @@ func TestDefaultHandlerTranscribesVoiceNoteIntoText(t *testing.T) {
|
||||
return ch, nil
|
||||
},
|
||||
}
|
||||
transcriber := &fakeTranscriber{text: "turn the lights on"}
|
||||
g := testMediaGateway(b)
|
||||
g.Transcriber = transcriber
|
||||
|
||||
g.defaultHandler(client)(context.Background(), b, &models.Update{Message: voiceUpdate("voice1")})
|
||||
|
||||
select {
|
||||
case in := <-inboundCh:
|
||||
want := "[voice transcription]\nturn the lights on"
|
||||
if in.Message.Text != want {
|
||||
t.Errorf("Text = %q, want %q", in.Message.Text, want)
|
||||
if len(in.Media) != 1 || in.Media[0].Type != messaging.MediaTypeVoice {
|
||||
t.Fatalf("inbound media = %#v, want the voice attachment marked as speech", in.Media)
|
||||
}
|
||||
if len(in.Media) != 1 || in.Media[0].Type != "audio" {
|
||||
t.Fatalf("inbound media = %#v, want the voice attachment", in.Media)
|
||||
if string(in.Media[0].Data) != string(voiceBytes) {
|
||||
t.Errorf("inbound media bytes = %q, want the downloaded voice bytes", in.Media[0].Data)
|
||||
}
|
||||
if transcriber.calls != 1 {
|
||||
t.Fatalf("Transcribe calls = %d, want 1", transcriber.calls)
|
||||
}
|
||||
if string(transcriber.audio) != string(voiceBytes) {
|
||||
t.Errorf("Transcribe received %q, want the downloaded voice bytes", transcriber.audio)
|
||||
if in.Message.Text != "[voice message]" {
|
||||
t.Errorf("Text = %q, want the media note preserved for the model-owning side", in.Message.Text)
|
||||
}
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("voice note never reached the chat turn")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDefaultHandlerKeepsVoiceNoteWhenTranscriptionUnavailable pins the
|
||||
// degradation the config contract requires: with no Transcriber the voice
|
||||
// note still becomes a turn, carrying today's "[voice message]" note rather
|
||||
// than failing or going silent.
|
||||
func TestDefaultHandlerKeepsVoiceNoteWhenTranscriptionUnavailable(t *testing.T) {
|
||||
b, _ := newRecordedAPI(t, func(w http.ResponseWriter, r *http.Request) bool {
|
||||
if strings.HasPrefix(r.URL.Path, "/file/bot1:test/") {
|
||||
_, _ = w.Write([]byte("ogg-voice-bytes"))
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
inboundCh := make(chan messaging.Inbound, 1)
|
||||
client := &fakeChatContract{
|
||||
streamFunc: func(_ context.Context, in messaging.Inbound) (<-chan messaging.ChatEvent, error) {
|
||||
inboundCh <- in
|
||||
ch := make(chan messaging.ChatEvent, 1)
|
||||
ch <- messaging.ChatEvent{Kind: "done", Text: "done", SessionID: "s1"}
|
||||
close(ch)
|
||||
return ch, nil
|
||||
},
|
||||
}
|
||||
g := testMediaGateway(b) // Transcriber left nil on purpose
|
||||
|
||||
g.defaultHandler(client)(context.Background(), b, &models.Update{Message: voiceUpdate("voice1")})
|
||||
|
||||
select {
|
||||
case in := <-inboundCh:
|
||||
if in.Message.Text != "[voice message]" {
|
||||
t.Errorf("Text = %q, want the media note preserved", in.Message.Text)
|
||||
}
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("voice note was dropped when transcription was unavailable")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDefaultHandlerDoesNotTranscribePlainAudio keeps the flag scoped to
|
||||
// voice notes: a forwarded music file must not spend a transcription call.
|
||||
func TestDefaultHandlerDoesNotTranscribePlainAudio(t *testing.T) {
|
||||
// TestDefaultHandlerMarksPlainAudioAsAudio keeps the speech distinction: a
|
||||
// forwarded music file is ordinary audio, so the model-owning side knows not
|
||||
// to spend a transcription call on it.
|
||||
func TestDefaultHandlerMarksPlainAudioAsAudio(t *testing.T) {
|
||||
b, _ := newRecordedAPI(t, func(w http.ResponseWriter, r *http.Request) bool {
|
||||
if strings.HasPrefix(r.URL.Path, "/file/bot1:test/") {
|
||||
_, _ = w.Write([]byte("mp3-bytes"))
|
||||
@@ -470,9 +411,7 @@ func TestDefaultHandlerDoesNotTranscribePlainAudio(t *testing.T) {
|
||||
return ch, nil
|
||||
},
|
||||
}
|
||||
transcriber := &fakeTranscriber{text: "should not be used"}
|
||||
g := testMediaGateway(b)
|
||||
g.Transcriber = transcriber
|
||||
|
||||
g.defaultHandler(client)(context.Background(), b, &models.Update{Message: &models.Message{
|
||||
ID: 12,
|
||||
@@ -484,12 +423,12 @@ func TestDefaultHandlerDoesNotTranscribePlainAudio(t *testing.T) {
|
||||
|
||||
select {
|
||||
case in := <-inboundCh:
|
||||
if len(in.Media) != 1 || in.Media[0].Type != messaging.MediaTypeAudio {
|
||||
t.Fatalf("inbound media = %#v, want the plain audio attachment", in.Media)
|
||||
}
|
||||
if in.Message.Text != "[audio]" {
|
||||
t.Errorf("Text = %q, want the plain audio note", in.Message.Text)
|
||||
}
|
||||
if transcriber.calls != 0 {
|
||||
t.Errorf("Transcribe calls = %d, want 0 for non-voice audio", transcriber.calls)
|
||||
}
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("audio update never reached the chat turn")
|
||||
}
|
||||
|
||||
@@ -50,12 +50,6 @@ type Gateway struct {
|
||||
Updates UpdateService
|
||||
// Settings executes control-plane commands in this authenticated adapter.
|
||||
Settings *messaging.SettingsCommand
|
||||
// Transcriber turns voice notes into text before their turn is dispatched.
|
||||
// Nil means the capability is not configured (or its provider did not
|
||||
// resolve): a voice note then keeps its "[voice message]" note, exactly
|
||||
// the behaviour before transcription existed. The composition root owns
|
||||
// construction, so this adapter never holds a provider credential itself.
|
||||
Transcriber messaging.Transcriber
|
||||
// ResolveActor maps a channel-native sender to an authenticated Archie
|
||||
// identity. Nil deliberately permits reads but makes writes fail closed.
|
||||
ResolveActor func(int64) (string, bool)
|
||||
@@ -655,7 +649,6 @@ func (g *Gateway) submitTurn(ctx context.Context, b *bot.Bot, msg *models.Messag
|
||||
if !isTurn {
|
||||
return
|
||||
}
|
||||
transcribeVoice := hasMedia && media.transcribe
|
||||
|
||||
// The lane key must be the session, so that /stop -- which resolves
|
||||
// the same key -- reaches the turn the sender is actually watching.
|
||||
@@ -665,7 +658,6 @@ func (g *Gateway) submitTurn(ctx context.Context, b *bot.Bot, msg *models.Messag
|
||||
if !g.fetchTurnMedia(turnCtx, b, &gm, chatID, threadID) {
|
||||
return
|
||||
}
|
||||
g.transcribeVoiceNote(turnCtx, &gm, msg.Caption, transcribeVoice)
|
||||
|
||||
// If the turn invokes a tool that requires human approval,
|
||||
// the dispatch layer blocks on this approver. Nil is fine
|
||||
@@ -776,27 +768,6 @@ func (g *Gateway) fetchTurnMedia(ctx context.Context, b *bot.Bot, gm *messaging.
|
||||
return true
|
||||
}
|
||||
|
||||
// transcribeVoiceNote replaces a voice note's stored placeholder with the
|
||||
// transcript, so the agent reads what was said rather than "[voice message]".
|
||||
// It degrades: a nil Transcriber, an error, or an empty transcript leaves the
|
||||
// placeholder in place -- a turn must never fail because transcription was
|
||||
// unavailable.
|
||||
func (g *Gateway) transcribeVoiceNote(ctx context.Context, gm *messaging.Inbound, caption string, wanted bool) {
|
||||
if !wanted || g.Transcriber == nil || len(gm.Media) == 0 {
|
||||
return
|
||||
}
|
||||
transcript, err := g.Transcriber.Transcribe(ctx, gm.Media[0].Data)
|
||||
if err != nil {
|
||||
g.log.Warn("voice transcription failed; keeping the media note", "error", err)
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(transcript) == "" {
|
||||
g.log.Info("voice transcription produced no text; keeping the media note")
|
||||
return
|
||||
}
|
||||
gm.Message.Text = messaging.TranscribedText(transcript, caption)
|
||||
}
|
||||
|
||||
// ── helpers ──────────────────────────────────────────────────────
|
||||
|
||||
// conversationID addresses the chat a Telegram message arrived in: the
|
||||
|
||||
Reference in new issue
Block a user