OpenAI compat: preserve stream options

This commit is contained in:
Badgerbees 2026-04-14 20:33:06 +07:00
parent fad2cc7b65
commit 41c7ad9a48
2 changed files with 29 additions and 9 deletions

View file

@ -258,7 +258,12 @@ func (p *Provider) ChatStream(
requestBody := p.buildRequestBody(messages, tools, model, options) requestBody := p.buildRequestBody(messages, tools, model, options)
requestBody["stream"] = true requestBody["stream"] = true
if supportsStreamingUsage(p.apiBase) { if supportsStreamingUsage(p.apiBase) {
requestBody["stream_options"] = map[string]any{"include_usage": true} streamOptions := map[string]any{}
if existing, ok := requestBody["stream_options"].(map[string]any); ok {
streamOptions = maps.Clone(existing)
}
streamOptions["include_usage"] = true
requestBody["stream_options"] = streamOptions
} }
jsonData, err := json.Marshal(requestBody) jsonData, err := json.Marshal(requestBody)
@ -474,12 +479,7 @@ func (p *Provider) SupportsNativeSearch() bool {
} }
func isNativeSearchHost(apiBase string) bool { func isNativeSearchHost(apiBase string) bool {
u, err := url.Parse(apiBase) return isOpenAINativeBaseURL(apiBase)
if err != nil {
return false
}
host := u.Hostname()
return host == "api.openai.com" || strings.HasSuffix(host, ".openai.azure.com")
} }
// isOpenAINativeBaseURL reports whether the given API base is the OpenAI or // isOpenAINativeBaseURL reports whether the given API base is the OpenAI or

View file

@ -991,7 +991,7 @@ func chatWithCacheKey(t *testing.T, apiBase string) map[string]any {
return requestBody return requestBody
} }
func chatStreamWithRequestBody(t *testing.T, apiBase string) map[string]any { func chatStreamWithRequestBody(t *testing.T, apiBase string, opts ...Option) map[string]any {
t.Helper() t.Helper()
var requestBody map[string]any var requestBody map[string]any
@ -1016,7 +1016,7 @@ func chatStreamWithRequestBody(t *testing.T, apiBase string) map[string]any {
})) }))
defer server.Close() defer server.Close()
p := NewProvider("key", server.URL, "") p := NewProvider("key", server.URL, "", opts...)
p.apiBase = apiBase p.apiBase = apiBase
p.httpClient = &http.Client{ p.httpClient = &http.Client{
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) { Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
@ -1057,6 +1057,26 @@ func TestProviderChatStream_IncludesUsageForOpenAI(t *testing.T) {
} }
} }
func TestProviderChatStream_PreservesExistingStreamOptions(t *testing.T) {
body := chatStreamWithRequestBody(
t,
"https://api.openai.com/v1",
WithExtraBody(map[string]any{
"stream_options": map[string]any{"custom_flag": true},
}),
)
streamOptions, ok := body["stream_options"].(map[string]any)
if !ok {
t.Fatalf("stream_options = %T, want map[string]any", body["stream_options"])
}
if got := streamOptions["custom_flag"]; got != true {
t.Fatalf("stream_options.custom_flag = %v, want true", got)
}
if got := streamOptions["include_usage"]; got != true {
t.Fatalf("stream_options.include_usage = %v, want true", got)
}
}
func TestProviderChatStream_OmitsUsageForNonOpenAI(t *testing.T) { func TestProviderChatStream_OmitsUsageForNonOpenAI(t *testing.T) {
tests := []struct { tests := []struct {
name string name string