Files
magpie/internal/gateway/audio_input_test.go
T
yetone ea4ab91906 gateway: a recording a tool returns reaches Gemini as inlineData (Parlevent_ on X)
A Chat tool message's input_audio part was dropped: toolOutput read a
tool result's text and images only, so after 22f8db6c9 carried a user's
audio, a tool's audio still never reached a Gemini model (Parlevent_ on
X, who found the cause). toolOutput now reads input_audio, from any
inbound API's tool result (Chat's tool message; a Responses
function_call_output that holds one), into the ToolResult's new Audio
parts, with audioType's MIME type.

- Gemini (Gemini API, Vertex AI, Code Assist, Antigravity's Gemini
  models): each recording follows the functionResponse as inlineData, as
  the tool's images do, and moves to the user turn after it on Vertex AI
  as they do.
- Every other upstream (Chat, Responses, Messages, the Claude bridge,
  Antigravity's Claude models): the result's text ends with
  "[attachment audio/wav]", as a user's audio is told of; the bytes are
  never sent as an image, which those APIs would turn away with a 400.
- Anthropic's tool_result has no audio block, so nothing changes for it.
- tool_loop's resultSum counts the recordings, so two results that
  differ only in their audio aren't taken as a repeat.

Not changed: a file a tool returns (an Anthropic document block, a
Responses input_file) is still dropped from its result: #6703.

Run (mica, mica-run; mote's load was over its cores):
- TestChatToolAudioReachesGemini, TestToolAudioToldElsewhere: green.
  Broken by hand: with toolOutput's input_audio case off, both red;
  with the Gemini emitter's audio loop off, TestChatToolAudioReachesGemini
  red (no inlineData after the functionResponse).
- go test ./internal/gateway -count=1 -timeout 45m: ok (862s), at base 9afec644c
- rebased on ec97a4d7b (4 commits, none in internal/gateway): go vet
  ./internal/gateway and its Audio|ToolResult|Gemini tests: ok
- go vet ./internal/gateway on darwin, GOOS=linux and GOOS=windows: ok
- gofmt -l: clean

Not tried: a real Gemini or Vertex AI request with a tool's audio.
2026-11-15 03:09:03 +08:00

89 lines
4.0 KiB
Go

package gateway
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A Chat input_audio part reaches a Gemini upstream as inlineData with its
// format's MIME type. It was dropped on the way in, so the model answered
// as if no audio had been sent (Parlevent_ on X, Gemini on Vertex AI).
func TestChatAudioReachesGemini(t *testing.T) {
fresh(t)
noVision(t)
gem, gemUp := videoUp(t)
saveVideoProvider(t, provider.Provider{ID: "gem", Name: "Gem", Gemini: gemUp.URL + "/v1beta", Key: "k", Models: []string{"v", "t", "u"}}, gemUp.URL+"/v1beta")
s := New()
const audio = "UklGRiQAAABXQVZFZm10IBAAAAABAAEAQB8AAIA+AAACABAAZGF0YQAAAAA="
for _, c := range []struct{ format, mime string }{{"wav", "audio/wav"}, {"mp3", "audio/mp3"}} {
gem.reset()
rec := serveVideo(s, "/v1/chat/completions", `{"model":"gem/u","messages":[{"role":"user","content":[{"type":"text","text":"这段音频说了什么?"},{"type":"input_audio","input_audio":{"data":"`+audio+`","format":"`+c.format+`"}}]}]}`)
bodies, _ := gem.sent()
if rec.Code != 200 || len(bodies) != 1 || !strings.Contains(bodies[0], `"mimeType":"`+c.mime+`"`) || !strings.Contains(bodies[0], audio) {
t.Errorf("%s audio: %d %s; upstream %v", c.format, rec.Code, rec.Body.String(), bodies)
}
}
}
func TestAudioType(t *testing.T) {
for in, want := range map[string]string{"": "audio/wav", "wav": "audio/wav", "MP3": "audio/mp3", "mpeg": "audio/mp3", "flac": "audio/flac", "audio/ogg": "audio/ogg"} {
if got := audioType(in); got != want {
t.Errorf("audioType(%q) = %q, want %q", in, got, want)
}
}
}
// toolAudioChat is a Chat conversation whose tool message returns a
// recording, as Parlevent_'s client sends one: text and input_audio parts.
func toolAudioChat(model, audio string) string {
return `{"model":"` + model + `","messages":[` +
`{"role":"user","content":"听一下录音"},` +
`{"role":"assistant","content":null,"tool_calls":[{"id":"call_1","type":"function","function":{"name":"record","arguments":"{}"}}]},` +
`{"role":"tool","tool_call_id":"call_1","content":[{"type":"text","text":"recorded"},{"type":"input_audio","input_audio":{"data":"` + audio + `","format":"wav"}}]}]}`
}
// A recording a tool returned in a Chat tool message reaches a Gemini
// upstream as inlineData after the functionResponse, as its images do.
// toolOutput read only text and images, so it was dropped (Parlevent_ on
// X, after TestChatAudioReachesGemini's fix for a user's audio).
func TestChatToolAudioReachesGemini(t *testing.T) {
fresh(t)
noVision(t)
gem, gemUp := videoUp(t)
saveVideoProvider(t, provider.Provider{ID: "gem", Name: "Gem", Gemini: gemUp.URL + "/v1beta", Key: "k", Models: []string{"v", "t", "u"}}, gemUp.URL+"/v1beta")
s := New()
const audio = "UklGRiQAAABXQVZFZm10IBAAAAABAAEAQB8AAIA+AAACABAAZGF0YQAAAAA="
rec := serveVideo(s, "/v1/chat/completions", toolAudioChat("gem/u", audio))
bodies, _ := gem.sent()
if rec.Code != 200 || len(bodies) != 1 {
t.Fatalf("%d %s; upstream %v", rec.Code, rec.Body.String(), bodies)
}
b := bodies[0]
resp, inline := strings.Index(b, `"functionResponse"`), strings.Index(b, `{"inlineData":{"data":"`+audio+`","mimeType":"audio/wav"}}`)
if resp < 0 || inline < resp {
t.Errorf("the tool's audio isn't inlineData after its response: %s", b)
}
}
// Every other upstream is told of the recording in the result's text and
// is never handed it as an image, which Chat, Responses and Messages would
// turn away with a 400.
func TestToolAudioToldElsewhere(t *testing.T) {
const audio = "UklGRiQAAABXQVZFZm10IBAAAAABAAEAQB8AAIA+AAACABAAZGF0YQAAAAA="
r, err := parseChat([]byte(toolAudioChat("m", audio)))
if err != nil {
t.Fatal(err)
}
for name, body := range map[string][]byte{
"chat": buildChat(r, "m", "api.example.com", false),
"responses": buildResponses(r, "m", "api.example.com", false),
"messages": buildAnthropic(r, "m"),
} {
if s := string(body); strings.Contains(s, audio) || !strings.Contains(s, `recorded\n[attachment audio/wav]`) {
t.Errorf("%s: %s", name, s)
}
}
}