RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix: upload Aliyun ASR audio to DashScope storage instead of sending a data URL (#21441)
CCLUCKYaf22a0c55e1f
diff --git a/internal/entity/models/aliyun.go b/internal/entity/models/aliyun.goindex 8aa3ff4b4c..ebe8ef06ba 100644--- a/internal/entity/models/aliyun.go+++ b/internal/entity/models/aliyun.go@@ -19,10 +19,10 @@ package modelsimport ("bytes""context"- "encoding/base64""encoding/json""fmt""io"+ "mime/multipart""net/http""net/url""os"@@ -30,6 +30,8 @@ import ("strings""sync"+ "github.com/google/uuid"+"ragflow/internal/common")@@ -603,8 +605,10 @@ type aliyunASRResponse struct {// TranscribeAudio transcribes an audio file with a DashScope ASR model// (qwen-asr-flash / qwen-audio-asr). It calls the same synchronized-// multimodal-generation endpoint used for TTS, passing the audio as a base64-// data URL, matching the legacy dashscope.MultiModalConversation path.+// multimodal-generation endpoint used for TTS, but the audio field must be a+// URL DashScope can read: a public http(s) URL or an oss:// object in+// DashScope's temporary storage. Base64 data URLs are rejected with+// "InvalidParameter: url error", so a local file is uploaded first.func (a *AliyunModel) TranscribeAudio(ctx context.Context, modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig, modelUsage *common.ModelUsage) (*ASRResponse, error) {if err := a.baseModel.APIConfigCheck(apiConfig); err != nil {return nil, err@@ -629,11 +633,24 @@ func (a *AliyunModel) TranscribeAudio(ctx context.Context, modelName *string, fi}contextText, options := aliyunASROptions(asrConfig)+ asrEndpoint, err := a.baseModel.EndpointURL(apiConfig, a.baseModel.URLSuffix.ASR)+ if err != nil {+ return nil, err+ }++ ctx, cancel := context.WithTimeout(ctx, longOpCallTimeout)+ defer cancel()++ audioURL, err := a.uploadAudio(ctx, asrEndpoint, *apiConfig.ApiKey, strings.TrimSpace(*modelName), *file, audio)+ if err != nil {+ return nil, err+ }+reqBody := aliyunASRRequest{Model: strings.TrimSpace(*modelName),Input: aliyunASRInput{Messages: []aliyunASRMessage{{Role: "system", Content: []aliyunASRContentPart{{Text: contextText}}},- {Role: "user", Content: []aliyunASRContentPart{{Audio: aliyunAudioDataURL(*file, audio)}}},+ {Role: "user", Content: []aliyunASRContentPart{{Audio: audioURL}}},}},Parameters: &aliyunASRParameters{ResultFormat: "message", ASROptions: options},}@@ -643,21 +660,16 @@ func (a *AliyunModel) TranscribeAudio(ctx context.Context, modelName *string, fireturn nil, fmt.Errorf("failed to marshal request: %w", err)}- url, err := a.baseModel.EndpointURL(apiConfig, a.baseModel.URLSuffix.ASR)- if err != nil {- return nil, err- }-- ctx, cancel := context.WithTimeout(ctx, longOpCallTimeout)- defer cancel()-- req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))+ req, err := http.NewRequestWithContext(ctx, "POST", asrEndpoint, bytes.NewBuffer(jsonData))if err != nil {return nil, fmt.Errorf("failed to create request: %w", err)}req.Header.Set("Content-Type", "application/json")req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))req.Header.Set("X-DashScope-SSE", "disable")+ // DashScope only resolves oss:// objects from its temporary upload space+ // when this header is present; without it the request fails as a URL error.+ req.Header.Set("X-DashScope-OssResourceResolve", "enable")resp, err := a.baseModel.httpClient.Do(req)if err != nil {@@ -733,35 +745,160 @@ func aliyunASROptions(asrConfig *ASRConfig) (string, map[string]interface{}) {return contextText, options}-// aliyunAudioDataURL encodes audio bytes as the DashScope data URL-// "data:<mime>;base64,<data>" accepted by the ASR endpoint.-func aliyunAudioDataURL(filename string, audio []byte) string {- return "data:" + aliyunAudioMIME(filename) + ";base64," + base64.StdEncoding.EncodeToString(audio)+// aliyunUploadsPath is the DashScope endpoint handing out a temporary OSS+// upload policy. The ASR endpoint cannot read a local file, so the audio is+// uploaded here first and referenced as oss://<upload_dir>/<file>.+const aliyunUploadsPath = "/api/v1/uploads"++// aliyunUploadPolicy is the credential set DashScope returns for one upload.+type aliyunUploadPolicy struct {+ Policy string `json:"policy"`+ Signature string `json:"signature"`+ UploadDir string `json:"upload_dir"`+ UploadHost string `json:"upload_host"`+ OSSAccessKeyID string `json:"oss_access_key_id"`+ XOSSObjectACL string `json:"x_oss_object_acl"`+ XOSSForbidOverwrite string `json:"x_oss_forbid_overwrite"`}-// aliyunAudioMIME maps an audio file extension to the MIME type DashScope-// expects; WAV is the default because it is the most common RAGFlow output.-func aliyunAudioMIME(filename string) string {- switch strings.ToLower(filepath.Ext(filename)) {- case ".mp3":- return "audio/mpeg"- case ".m4a":- return "audio/mp4"- case ".aac":- return "audio/aac"- case ".flac":- return "audio/flac"- case ".ogg":- return "audio/ogg"- case ".opus":- return "audio/opus"- case ".amr":- return "audio/amr"- case ".wma":- return "audio/x-ms-wma"- default:- return "audio/wav"+// aliyunUploadPolicyResponse is the getPolicy envelope; DashScope may report+// failures in Code/Message with an HTTP 200 status.+type aliyunUploadPolicyResponse struct {+ RequestID string `json:"request_id"`+ Data aliyunUploadPolicy `json:"data"`+ Code string `json:"code"`+ Message string `json:"message"`+}++// aliyunAudioExt lower-cases the filename extension for the OSS object key+// and keeps it URL-safe: a dot followed by alphanumerics only. Anything else+// ("clip.wav#1" -> ".wav#1", bare ".", no extension) falls back to ".wav".+func aliyunAudioExt(filename string) string {+ ext := strings.ToLower(filepath.Ext(filename))+ if len(ext) > 1 && strings.TrimFunc(ext[1:], func(r rune) bool {+ return (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9')+ }) == "" {+ return ext+ }+ return ".wav"+}++// uploadAudio stores audio in DashScope's temporary OSS space and returns the+// oss:// URL to send as the ASR audio field. The policy endpoint is derived+// from the ASR endpoint's origin, so the configured region is preserved.+func (a *AliyunModel) uploadAudio(ctx context.Context, asrEndpoint, apiKey, model, filename string, audio []byte) (string, error) {+ endpoint, err := url.Parse(asrEndpoint)+ if err != nil {+ return "", fmt.Errorf("parse aliyun ASR endpoint: %w", err)+ }+ if endpoint.Scheme == "" || endpoint.Host == "" {+ return "", fmt.Errorf("aliyun ASR endpoint %q is not an absolute URL", asrEndpoint)+ }+ policyURL := url.URL{+ Scheme: endpoint.Scheme,+ Host: endpoint.Host,+ Path: aliyunUploadsPath,+ RawQuery: url.Values{"action": {"getPolicy"}, "model": {model}}.Encode(),+ }+ policyReq, err := http.NewRequestWithContext(ctx, http.MethodGet, policyURL.String(), nil)+ if err != nil {+ return "", fmt.Errorf("create aliyun upload policy request: %w", err)+ }+ policyReq.Header.Set("Authorization", "Bearer "+apiKey)+ // The getPolicy endpoint documents this header even though the request has+ // no body.+ policyReq.Header.Set("Content-Type", "application/json")+ policyResp, err := a.baseModel.httpClient.Do(policyReq)+ if err != nil {+ return "", fmt.Errorf("request aliyun upload policy: %w", err)+ }+ defer policyResp.Body.Close()+ if policyResp.StatusCode != http.StatusOK {+ body, readErr := readModelErrorBody(policyResp.Body)+ if readErr != nil {+ return "", fmt.Errorf("aliyun upload policy failed with status %d; failed to read error response: %w", policyResp.StatusCode, readErr)+ }+ return "", &APIStatusError{Status: policyResp.StatusCode, Body: string(body)}+ }+ body, err := readModelResponseBody(policyResp.Body)+ if err != nil {+ return "", fmt.Errorf("read aliyun upload policy: %w", err)+ }+ var policy aliyunUploadPolicyResponse+ if err := json.Unmarshal(body, &policy); err != nil {+ return "", fmt.Errorf("decode aliyun upload policy: %w", err)+ }+ if policy.Code != "" {+ return "", &APIStatusError{Status: policyResp.StatusCode, Body: string(body)}+ }+ data := policy.Data+ if data.UploadHost == "" || data.UploadDir == "" || data.OSSAccessKeyID == "" || data.Signature == "" || data.Policy == "" {+ return "", fmt.Errorf("aliyun upload policy response is incomplete")+ }++ // The object name lands inside an oss:// URL the provider later resolves,+ // so it must stay URL-safe. A generated name keeps a normalised extension,+ // which is what the provider reads for the audio format, and drops+ // everything else a caller-supplied filename may carry (spaces, '#',+ // non-ASCII).+ objectName := uuid.NewString() + aliyunAudioExt(filename)+ objectKey := strings.Trim(data.UploadDir, "/") + "/" + objectName+ fields := [][2]string{+ {"OSSAccessKeyId", data.OSSAccessKeyID},+ {"policy", data.Policy},+ {"Signature", data.Signature},+ {"key", objectKey},+ {"success_action_status", "200"},+ }+ // The policy decides which optional form fields are signed; sending an+ // empty value for one OSS did not sign makes the upload fail.+ if data.XOSSObjectACL != "" {+ fields = append(fields, [2]string{"x-oss-object-acl", data.XOSSObjectACL})+ }+ if data.XOSSForbidOverwrite != "" {+ fields = append(fields, [2]string{"x-oss-forbid-overwrite", data.XOSSForbidOverwrite})+ }++ var form bytes.Buffer+ writer := multipart.NewWriter(&form)+ for _, field := range fields {+ if err := writer.WriteField(field[0], field[1]); err != nil {+ return "", fmt.Errorf("write aliyun upload form field %s: %w", field[0], err)+ }+ }+ part, err := writer.CreateFormFile("file", objectName)+ if err != nil {+ return "", fmt.Errorf("create aliyun upload file part: %w", err)+ }+ if _, err := part.Write(audio); err != nil {+ return "", fmt.Errorf("write aliyun upload audio: %w", err)+ }+ if err := writer.Close(); err != nil {+ return "", fmt.Errorf("finish aliyun upload form: %w", err)+ }++ uploadHost := data.UploadHost+ if !strings.Contains(uploadHost, "://") {+ uploadHost = "https://" + uploadHost+ }+ uploadReq, err := http.NewRequestWithContext(ctx, http.MethodPost, uploadHost, &form)+ if err != nil {+ return "", fmt.Errorf("create aliyun upload request to %s: %w", uploadHost, err)+ }+ uploadReq.Header.Set("Content-Type", writer.FormDataContentType())+ uploadResp, err := a.baseModel.httpClient.Do(uploadReq)+ if err != nil {+ return "", fmt.Errorf("upload audio to aliyun at %s: %w", uploadHost, err)+ }+ defer uploadResp.Body.Close()+ if uploadResp.StatusCode < 200 || uploadResp.StatusCode >= 300 {+ body, readErr := readModelErrorBody(uploadResp.Body)+ if readErr != nil {+ return "", fmt.Errorf("aliyun audio upload failed with status %d; failed to read error response: %w", uploadResp.StatusCode, readErr)+ }+ return "", &APIStatusError{Status: uploadResp.StatusCode, Body: string(body)}}+ return "oss://" + objectKey, nil}// aliyunTTSDefaultVoice is used when the caller does not specify a voice;diff --git a/internal/entity/models/aliyun_test.go b/internal/entity/models/aliyun_test.goindex b22c1644c2..f1fce0e4b3 100644--- a/internal/entity/models/aliyun_test.go+++ b/internal/entity/models/aliyun_test.go@@ -17,16 +17,19 @@package modelsimport (- "encoding/base64"+ "bytes""encoding/json""errors""fmt"+ "io""net/http""net/http/httptest""os""path/filepath""strings""testing"++ "github.com/google/uuid")func TestAliyunChatWithMessagesSupportsToolCalls(t *testing.T) {@@ -939,35 +942,114 @@ func TestAliyunEmbedNativeRejectsTooNegativeIndex(t *testing.T) {}}-const aliyunASRTestSuffix = "api/v1/services/aigc/multimodal-generation/generation"+const (+ aliyunASRTestSuffix = "api/v1/services/aigc/multimodal-generation/generation"+ aliyunUploadTestDir = "dashscope-tmp"+)++// aliyunUploadCapture is one multipart upload the ASR flow sent to the stubbed+// OSS host.+type aliyunUploadCapture struct {+ Fields map[string]string+ Name string+ Data []byte+}++// aliyunASRTestStub stubs the three DashScope calls an ASR request makes: the+// upload policy, the OSS multipart upload, and the multimodal-generation call+// carrying the transcription. Channels are buffered so a test may ignore them.+type aliyunASRTestStub struct {+ Server *httptest.Server+ Requests chan map[string]interface{}+ Paths chan string+ Uploads chan aliyunUploadCapture+}-// newAliyunASRTestServer stubs the DashScope multimodal-generation endpoint for-// ASR: POST returns one message choice whose content carries the transcription.-func newAliyunASRTestServer(t *testing.T, transcription string, requestBody chan<- map[string]interface{}, requestPath chan<- string) *httptest.Server {+func newAliyunASRTestServer(t *testing.T, transcription string) *aliyunASRTestStub {t.Helper()- server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- if r.Method != http.MethodPost {- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)- return- }- var body map[string]interface{}- if err := json.NewDecoder(r.Body).Decode(&body); err != nil {- http.Error(w, err.Error(), http.StatusBadRequest)- return- }- if requestPath != nil {- requestPath <- r.URL.Path- }- if requestBody != nil {- requestBody <- body- }- w.Header().Set("Content-Type", "application/json")- if _, err := fmt.Fprintf(w, `{"output":{"choices":[{"finish_reason":"stop","message":{"role":"assistant","content":[{"text":%q}]}}]},"request_id":"req-1"}`, transcription); err != nil {- t.Errorf("failed to write ASR response: %v", err)+ return newAliyunASRTestServerWithASRResponse(t, "test-key", http.StatusOK, fmt.Sprintf(`{"output":{"choices":[{"finish_reason":"stop","message":{"role":"assistant","content":[{"text":%q}]}}]},"request_id":"req-1"}`, transcription))+}++// newAliyunASRTestServerWithASRResponse stubs a successful policy and upload+// and replies to the transcription call with the given status and body.+func newAliyunASRTestServerWithASRResponse(t *testing.T, apiKey string, asrStatus int, asrBody string) *aliyunASRTestStub {+ t.Helper()+ stub := &aliyunASRTestStub{+ Requests: make(chan map[string]interface{}, 4),+ Paths: make(chan string, 4),+ Uploads: make(chan aliyunUploadCapture, 4),+ }+ var server *httptest.Server+ server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ switch {+ case r.Method == http.MethodGet && r.URL.Path == aliyunUploadsPath:+ if got := r.URL.Query().Get("action"); got != "getPolicy" {+ http.Error(w, "action = "+got, http.StatusBadRequest)+ return+ }+ if got := r.URL.Query().Get("model"); got == "" {+ http.Error(w, "model query is missing", http.StatusBadRequest)+ return+ }+ if got := r.Header.Get("Authorization"); got != "Bearer "+apiKey {+ http.Error(w, "authorization = "+got, http.StatusUnauthorized)+ return+ }+ if got := r.Header.Get("Content-Type"); got != "application/json" {+ http.Error(w, "content-type = "+got, http.StatusBadRequest)+ return+ }+ w.Header().Set("Content-Type", "application/json")+ if _, err := fmt.Fprintf(w, `{"request_id":"req-policy","data":{"policy":"policy-value","signature":"signature-value","upload_dir":%q,"upload_host":%q,"oss_access_key_id":"access-key","x_oss_object_acl":"private","x_oss_forbid_overwrite":"true"}}`,+ aliyunUploadTestDir, server.URL+"/oss-upload"); err != nil {+ t.Errorf("failed to write upload policy: %v", err)+ }+ case r.Method == http.MethodPost && r.URL.Path == "/oss-upload":+ if err := r.ParseMultipartForm(1 << 20); err != nil {+ http.Error(w, err.Error(), http.StatusBadRequest)+ return+ }+ file, header, err := r.FormFile("file")+ if err != nil {+ http.Error(w, err.Error(), http.StatusBadRequest)+ return+ }+ defer file.Close()+ data, err := io.ReadAll(file)+ if err != nil {+ http.Error(w, err.Error(), http.StatusBadRequest)+ return+ }+ capture := aliyunUploadCapture{Fields: map[string]string{}, Name: header.Filename, Data: data}+ for key := range r.MultipartForm.Value {+ capture.Fields[key] = r.FormValue(key)+ }+ stub.Uploads <- capture+ w.WriteHeader(http.StatusOK)+ case r.Method == http.MethodPost && r.URL.Path == "/"+aliyunASRTestSuffix:+ if got := r.Header.Get("X-DashScope-OssResourceResolve"); got != "enable" {+ http.Error(w, "X-DashScope-OssResourceResolve = "+got, http.StatusBadRequest)+ return+ }+ var body map[string]interface{}+ if err := json.NewDecoder(r.Body).Decode(&body); err != nil {+ http.Error(w, err.Error(), http.StatusBadRequest)+ return+ }+ stub.Paths <- r.URL.Path+ stub.Requests <- body+ w.Header().Set("Content-Type", "application/json")+ w.WriteHeader(asrStatus)+ if _, err := w.Write([]byte(asrBody)); err != nil {+ t.Errorf("failed to write ASR response: %v", err)+ }+ default:+ http.Error(w, "unexpected "+r.Method+" "+r.URL.Path, http.StatusNotFound)}}))+ stub.Server = servert.Cleanup(server.Close)- return server+ return stub}func writeAliyunASRTestFile(t *testing.T, name string, content []byte) string {@@ -1014,15 +1096,13 @@ func aliyunASRContentString(t *testing.T, message map[string]interface{}, key streturn value}-func TestAliyunTranscribeAudioSendsDataURLAndParsesText(t *testing.T) {+func TestAliyunTranscribeAudioUploadsFileAndSendsOSSURL(t *testing.T) {withSSRFBypass(t)- requestBody := make(chan map[string]interface{}, 1)- requestPath := make(chan string, 1)- server := newAliyunASRTestServer(t, "hello world", requestBody, requestPath)+ stub := newAliyunASRTestServer(t, "hello world")ctx := t.Context()model := NewAliyunModel(- map[string]string{"default": server.URL},+ map[string]string{"default": stub.Server.URL},URLSuffix{ASR: aliyunASRTestSuffix},)apiKey := "test-key"@@ -1038,10 +1118,10 @@ func TestAliyunTranscribeAudioSendsDataURLAndParsesText(t *testing.T) {t.Errorf("text = %q, want %q", response.Text, "hello world")}- if got := <-requestPath; got != "/"+aliyunASRTestSuffix {+ if got := <-stub.Paths; got != "/"+aliyunASRTestSuffix {t.Errorf("request path = %q, want /%s", got, aliyunASRTestSuffix)}- body := <-requestBody+ body := <-stub.Requestsif body["model"] != "qwen-asr-flash" {t.Errorf("model = %v, want qwen-asr-flash", body["model"])}@@ -1055,9 +1135,38 @@ func TestAliyunTranscribeAudioSendsDataURLAndParsesText(t *testing.T) {if messages[1]["role"] != "user" {t.Errorf("messages[1].role = %v, want user", messages[1]["role"])}- wantURL := "data:audio/wav;base64," + base64.StdEncoding.EncodeToString(audio)- if got := aliyunASRContentString(t, messages[1], "audio"); got != wantURL {- t.Errorf("audio = %q, want %q", got, wantURL)+ audioURL := aliyunASRContentString(t, messages[1], "audio")+ wantPrefix := "oss://" + aliyunUploadTestDir + "/"+ if !strings.HasPrefix(audioURL, wantPrefix) || !strings.HasSuffix(audioURL, ".wav") {+ t.Errorf("audio = %q, want %q<uuid>.wav", audioURL, wantPrefix)+ }++ upload := <-stub.Uploads+ key := upload.Fields["key"]+ objectName := strings.TrimPrefix(key, aliyunUploadTestDir+"/")+ if _, err := uuid.Parse(strings.TrimSuffix(objectName, filepath.Ext(objectName))); err != nil {+ t.Errorf("uploaded object name = %q, want <uuid>.wav: %v", objectName, err)+ }+ if upload.Name != objectName {+ t.Errorf("uploaded file name = %q, want %q", upload.Name, objectName)+ }+ if "oss://"+key != audioURL {+ t.Errorf("audio url %q does not reference uploaded key %q", audioURL, key)+ }+ if !bytes.Equal(upload.Data, audio) {+ t.Errorf("uploaded data = %q, want %q", upload.Data, audio)+ }+ for field, want := range map[string]string{+ "OSSAccessKeyId": "access-key",+ "policy": "policy-value",+ "Signature": "signature-value",+ "x-oss-object-acl": "private",+ "x-oss-forbid-overwrite": "true",+ "success_action_status": "200",+ } {+ if got := upload.Fields[field]; got != want {+ t.Errorf("upload field %s = %q, want %q", field, got, want)+ }}params, ok := body["parameters"].(map[string]interface{})@@ -1078,17 +1187,16 @@ func TestAliyunTranscribeAudioSendsDataURLAndParsesText(t *testing.T) {func TestAliyunTranscribeAudioHonorsContextAndASROptions(t *testing.T) {withSSRFBypass(t)- requestBody := make(chan map[string]interface{}, 1)- server := newAliyunASRTestServer(t, "ok", requestBody, nil)+ stub := newAliyunASRTestServer(t, "ok")ctx := t.Context()model := NewAliyunModel(- map[string]string{"default": server.URL},+ map[string]string{"default": stub.Server.URL},URLSuffix{ASR: aliyunASRTestSuffix},)apiKey := "test-key"modelName := "qwen-asr-flash"- file := writeAliyunASRTestFile(t, "clip.mp3", []byte("ID3...."))+ file := writeAliyunASRTestFile(t, "clip.MP3", []byte("ID3...."))_, err := model.TranscribeAudio(ctx, &modelName, &file, &APIConfig{ApiKey: &apiKey}, &ASRConfig{Params: map[string]interface{}{"context": "NAVI vs Faze",@@ -1098,13 +1206,13 @@ func TestAliyunTranscribeAudioHonorsContextAndASROptions(t *testing.T) {if err != nil {t.Fatalf("TranscribeAudio: %v", err)}- body := <-requestBody+ body := <-stub.Requestsmessages := aliyunASRMessages(t, body)if got := aliyunASRContentString(t, messages[0], "text"); got != "NAVI vs Faze" {t.Errorf("system text = %q, want NAVI vs Faze", got)}- if got := aliyunASRContentString(t, messages[1], "audio"); !strings.HasPrefix(got, "data:audio/mpeg;base64,") {- t.Errorf("audio data URL = %q, want mp3 MIME prefix", got)+ if got := aliyunASRContentString(t, messages[1], "audio"); !strings.HasPrefix(got, "oss://"+aliyunUploadTestDir+"/") || !strings.HasSuffix(got, ".mp3") {+ t.Errorf("audio = %q, want oss://%s/<uuid>.mp3", got, aliyunUploadTestDir)}params, ok := body["parameters"].(map[string]interface{})if !ok {@@ -1126,11 +1234,59 @@ func TestAliyunTranscribeAudioHonorsContextAndASROptions(t *testing.T) {}func TestAliyunTranscribeAudioSurfacesAPIError(t *testing.T) {+ withSSRFBypass(t)+ stub := newAliyunASRTestServerWithASRResponse(t, "bad-key", http.StatusUnauthorized, `{"code":"InvalidApiKey","message":"Invalid API-key provided."}`)++ model := NewAliyunModel(+ map[string]string{"default": stub.Server.URL},+ URLSuffix{ASR: aliyunASRTestSuffix},+ )+ apiKey := "bad-key"+ modelName := "qwen-asr-flash"+ file := writeAliyunASRTestFile(t, "clip.wav", []byte("data"))++ _, err := model.TranscribeAudio(t.Context(), &modelName, &file, &APIConfig{ApiKey: &apiKey}, nil, nil)+ var statusErr *APIStatusError+ if !errors.As(err, &statusErr) || statusErr.Status != http.StatusUnauthorized {+ t.Fatalf("error = %v, want APIStatusError with status 401", err)+ }+ if !strings.Contains(statusErr.Body, "InvalidApiKey") {+ t.Fatalf("error body = %q, want to include InvalidApiKey", statusErr.Body)+ }+}++func TestAliyunTranscribeAudioSurfacesUploadPolicyError(t *testing.T) {+ withSSRFBypass(t)+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ w.WriteHeader(http.StatusForbidden)+ _, _ = w.Write([]byte(`{"code":"AccessDenied","message":"upload policy denied"}`))+ }))+ defer server.Close()++ model := NewAliyunModel(+ map[string]string{"default": server.URL},+ URLSuffix{ASR: aliyunASRTestSuffix},+ )+ apiKey := "test-key"+ modelName := "fun-asr"+ file := writeAliyunASRTestFile(t, "clip.wav", []byte("data"))++ _, err := model.TranscribeAudio(t.Context(), &modelName, &file, &APIConfig{ApiKey: &apiKey}, nil, nil)+ var statusErr *APIStatusError+ if !errors.As(err, &statusErr) || statusErr.Status != http.StatusForbidden {+ t.Fatalf("error = %v, want APIStatusError with status 403", err)+ }+ if !strings.Contains(statusErr.Body, "upload policy denied") {+ t.Fatalf("error body = %q, want to include upload policy denied", statusErr.Body)+ }+}++func TestAliyunTranscribeAudioSurfacesProviderCodedPolicyError(t *testing.T) {withSSRFBypass(t)server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {w.Header().Set("Content-Type", "application/json")- w.WriteHeader(http.StatusUnauthorized)- _, _ = w.Write([]byte(`{"code":"InvalidApiKey","message":"Invalid API-key provided."}`))+ _, _ = w.Write([]byte(`{"request_id":"req-policy","code":"InvalidApiKey","message":"invalid key"}`))}))defer server.Close()@@ -1144,14 +1300,49 @@ func TestAliyunTranscribeAudioSurfacesAPIError(t *testing.T) {_, err := model.TranscribeAudio(t.Context(), &modelName, &file, &APIConfig{ApiKey: &apiKey}, nil, nil)var statusErr *APIStatusError- if !errors.As(err, &statusErr) || statusErr.Status != http.StatusUnauthorized {- t.Fatalf("error = %v, want APIStatusError with status 401", err)+ if !errors.As(err, &statusErr) || statusErr.Status != http.StatusOK {+ t.Fatalf("error = %v, want APIStatusError with the policy status", err)}if !strings.Contains(statusErr.Body, "InvalidApiKey") {t.Fatalf("error body = %q, want to include InvalidApiKey", statusErr.Body)}}+func TestAliyunTranscribeAudioSurfacesUploadError(t *testing.T) {+ withSSRFBypass(t)+ var server *httptest.Server+ server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.Method == http.MethodGet && r.URL.Path == aliyunUploadsPath {+ w.Header().Set("Content-Type", "application/json")+ if _, err := fmt.Fprintf(w, `{"request_id":"req-policy","data":{"policy":"policy-value","signature":"signature-value","upload_dir":%q,"upload_host":%q,"oss_access_key_id":"access-key","x_oss_object_acl":"private","x_oss_forbid_overwrite":"true"}}`,+ aliyunUploadTestDir, server.URL+"/oss-upload"); err != nil {+ t.Errorf("failed to write upload policy: %v", err)+ }+ return+ }+ w.WriteHeader(http.StatusInternalServerError)+ _, _ = w.Write([]byte("oss exploded"))+ }))+ defer server.Close()++ model := NewAliyunModel(+ map[string]string{"default": server.URL},+ URLSuffix{ASR: aliyunASRTestSuffix},+ )+ apiKey := "test-key"+ modelName := "qwen-asr-flash"+ file := writeAliyunASRTestFile(t, "clip.wav", []byte("data"))++ _, err := model.TranscribeAudio(t.Context(), &modelName, &file, &APIConfig{ApiKey: &apiKey}, nil, nil)+ var statusErr *APIStatusError+ if !errors.As(err, &statusErr) || statusErr.Status != http.StatusInternalServerError {+ t.Fatalf("error = %v, want APIStatusError with status 500", err)+ }+ if !strings.Contains(statusErr.Body, "oss exploded") {+ t.Fatalf("error body = %q, want to include oss exploded", statusErr.Body)+ }+}+func TestAliyunTranscribeAudioRejectsEmptyFile(t *testing.T) {withSSRFBypass(t)model := NewAliyunModel(@@ -1186,9 +1377,9 @@ func TestAliyunTranscribeAudioRequiresASRSuffix(t *testing.T) {func TestAliyunTranscribeAudioWithSenderSendsSingleChunk(t *testing.T) {withSSRFBypass(t)- server := newAliyunASRTestServer(t, "transcribed", nil, nil)+ stub := newAliyunASRTestServer(t, "transcribed")model := NewAliyunModel(- map[string]string{"default": server.URL},+ map[string]string{"default": stub.Server.URL},URLSuffix{ASR: aliyunASRTestSuffix},)apiKey := "test-key"@@ -1209,3 +1400,85 @@ func TestAliyunTranscribeAudioWithSenderSendsSingleChunk(t *testing.T) {t.Fatalf("chunks = %v, want [transcribed]", chunks)}}++// TestAliyunUploadAudioNormalisesSchemelessUploadHost pins the upload-host+// repair: a policy response whose upload_host lacks a scheme must be retried+// against an explicit https:// target instead of failing with an opaque+// "unsupported protocol scheme" from the HTTP client.+func TestAliyunUploadAudioNormalisesSchemelessUploadHost(t *testing.T) {+ withSSRFBypass(t)+ var server *httptest.Server+ server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.Method == http.MethodGet && r.URL.Path == aliyunUploadsPath {+ w.Header().Set("Content-Type", "application/json")+ schemeless := strings.TrimPrefix(server.URL, "http://")+ if _, err := fmt.Fprintf(w, `{"request_id":"req-policy","data":{"policy":"policy-value","signature":"signature-value","upload_dir":"dashscope-tmp","upload_host":%q,"oss_access_key_id":"access-key"}}`, schemeless+"/oss-upload"); err != nil {+ t.Errorf("failed to write policy: %v", err)+ }+ return+ }+ http.Error(w, "unexpected "+r.Method+" "+r.URL.Path, http.StatusNotFound)+ }))+ defer server.Close()+ model := NewAliyunModel(+ map[string]string{"default": server.URL},+ URLSuffix{ASR: aliyunASRTestSuffix},+ )+ _, err := model.uploadAudio(t.Context(), server.URL+"/"+aliyunASRTestSuffix, "test-key", "qwen-asr-flash", "clip.wav", []byte("data"))+ if err == nil {+ t.Fatal("upload must fail: the test server speaks plain http, not https")+ }+ if !strings.Contains(err.Error(), "https://"+strings.TrimPrefix(server.URL, "http://")) {+ t.Errorf("error = %q, want it to name the normalised https:// upload target", err)+ }+}++// TestAliyunAudioExt pins the URL-safe extension normalization: only a+// dot followed by alphanumerics survives, everything URL-sensitive falls+// back to ".wav".+func TestAliyunAudioExt(t *testing.T) {+ for filename, want := range map[string]string{+ "clip.wav": ".wav",+ "CLIP.MP3": ".mp3",+ "clip.wav#1": ".wav",+ "clip.tar.gz": ".gz",+ "clip.wav ": ".wav",+ "clip.wav%23": ".wav",+ "clip.音频": ".wav",+ "no-ext": ".wav",+ ".": ".wav",+ "": ".wav",+ "clip.": ".wav",+ "dir.d/clip": ".wav",+ } {+ if got := aliyunAudioExt(filename); got != want {+ t.Errorf("aliyunAudioExt(%q) = %q, want %q", filename, got, want)+ }+ }+}++// TestAliyunUploadAudioSanitisesFragmentFilename pins that a filename whose+// extension carries a URL fragment marker never reaches the returned oss://+// URL: "clip.wav#1" must upload as <uuid>.wav, not <uuid>.wav#1.+func TestAliyunUploadAudioSanitisesFragmentFilename(t *testing.T) {+ withSSRFBypass(t)+ stub := newAliyunASRTestServer(t, "hello")+ model := NewAliyunModel(+ map[string]string{"default": stub.Server.URL},+ URLSuffix{ASR: aliyunASRTestSuffix},+ )+ audioURL, err := model.uploadAudio(t.Context(), stub.Server.URL+"/"+aliyunASRTestSuffix, "test-key", "qwen-asr-flash", "clip.wav#1", []byte("data"))+ if err != nil {+ t.Fatalf("uploadAudio: %v", err)+ }+ if strings.Contains(audioURL, "#") {+ t.Errorf("audio url = %q, want no fragment character", audioURL)+ }+ if !strings.HasPrefix(audioURL, "oss://"+aliyunUploadTestDir+"/") || !strings.HasSuffix(audioURL, ".wav") {+ t.Errorf("audio url = %q, want oss://%s/<uuid>.wav", audioURL, aliyunUploadTestDir)+ }+ upload := <-stub.Uploads+ if upload.Fields["key"] != strings.TrimPrefix(audioURL, "oss://") {+ t.Errorf("uploaded key = %q, want %q", upload.Fields["key"], strings.TrimPrefix(audioURL, "oss://"))+ }+}