feat(dingtalk): enable secure multimodal image QA

This commit is contained in:
姚章浩
2026-07-16 00:17:16 +08:00
parent 4022040e98
commit c9c4f0aaa5
59 changed files with 3279 additions and 429 deletions
+56
View File
@@ -3,7 +3,11 @@ package chat
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync/atomic"
"testing"
"time"
@@ -13,6 +17,17 @@ import (
"github.com/stretchr/testify/require"
)
func TestRemoteAPIStatusErrorDoesNotExposeResponseBody(t *testing.T) {
secret := "data:image/png;base64,TOPSECRET"
err := remoteAPIStatusError(http.StatusBadRequest, []byte(secret))
if strings.Contains(err.Error(), secret) || strings.Contains(err.Error(), "TOPSECRET") {
t.Fatalf("remote API error leaked response body: %v", err)
}
if !strings.Contains(err.Error(), "400") {
t.Fatalf("remote API error = %v, want status code", err)
}
}
func newTestRemoteChat(t *testing.T) *RemoteAPIChat {
t.Helper()
@@ -533,3 +548,44 @@ func TestTokenUsage_CachedTokensJSONOmitempty(t *testing.T) {
assert.Contains(t, string(b), `"cached_tokens":7`)
})
}
func newMultimodalUnsupportedTestChat(t *testing.T, calls *atomic.Int32) *RemoteAPIChat {
t.Helper()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
calls.Add(1)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"error":{"message":"image input is not supported","type":"invalid_request_error","code":"unsupported"}}`))
}))
t.Cleanup(server.Close)
chat := newTestRemoteChat(t)
cfg := openai.DefaultConfig("test-key")
cfg.BaseURL = server.URL
chat.client = openai.NewClientWithConfig(cfg)
return chat
}
func TestRemoteAPIChat_MultimodalUnsupportedDoesNotStripImages(t *testing.T) {
messages := []Message{{
Role: "user",
Content: "read this image",
Images: []string{"data:image/png;base64,iVBORw0KGgo="},
}}
t.Run("non-streaming", func(t *testing.T) {
var calls atomic.Int32
chat := newMultimodalUnsupportedTestChat(t, &calls)
_, err := chat.Chat(context.Background(), messages, &ChatOptions{})
require.Error(t, err)
assert.EqualValues(t, 1, calls.Load(), "image input must not be removed and retried as text")
})
t.Run("streaming", func(t *testing.T) {
var calls atomic.Int32
chat := newMultimodalUnsupportedTestChat(t, &calls)
_, err := chat.ChatStream(context.Background(), messages, &ChatOptions{})
require.Error(t, err)
assert.EqualValues(t, 1, calls.Load(), "image input must not be removed and retried as text")
})
}