From 6c1fe57034a2aa259ab3ecc75ed7c8cfc8556d68 Mon Sep 17 00:00:00 2001 From: johnlanni Date: Wed, 10 Dec 2025 18:50:23 +0800 Subject: [PATCH] fix(ai-proxy): only perform protocol conversion for non-original protocols Change-Id: Ib8ae3ebf6b47284108663c97777032d6282bb53c --- plugins/wasm-go/extensions/ai-proxy/main.go | 35 +++++++++++---------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/plugins/wasm-go/extensions/ai-proxy/main.go b/plugins/wasm-go/extensions/ai-proxy/main.go index 3384bfea4..9dd05be7d 100644 --- a/plugins/wasm-go/extensions/ai-proxy/main.go +++ b/plugins/wasm-go/extensions/ai-proxy/main.go @@ -205,23 +205,24 @@ func onHttpRequestHeader(ctx wrapper.HttpContext, pluginConfig config.PluginConf if handler, ok := activeProvider.(provider.ApiNameHandler); ok { apiName = handler.GetApiName(path.Path) } - } - - // Auto-detect protocol based on request path and handle conversion if needed - // If request is Claude format (/v1/messages) but provider doesn't support it natively, - // convert to OpenAI format (/v1/chat/completions) - if apiName == provider.ApiNameAnthropicMessages && !providerConfig.IsSupportedAPI(provider.ApiNameAnthropicMessages) { - // Provider doesn't support Claude protocol natively, convert to OpenAI format - newPath := strings.Replace(path.Path, provider.PathAnthropicMessages, provider.PathOpenAIChatCompletions, 1) - _ = proxywasm.ReplaceHttpRequestHeader(":path", newPath) - // Update apiName to match the new path - apiName = provider.ApiNameChatCompletion - // Mark that we need to convert response back to Claude format - ctx.SetContext("needClaudeResponseConversion", true) - log.Debugf("[Auto Protocol] Claude request detected, provider doesn't support natively, converted path from %s to %s, apiName: %s", path.Path, newPath, apiName) - } else if apiName == provider.ApiNameAnthropicMessages { - // Provider supports Claude protocol natively, no conversion needed - log.Debugf("[Auto Protocol] Claude request detected, provider supports natively, keeping original path: %s, apiName: %s", path.Path, apiName) + } else { + // Only perform protocol conversion for non-original protocols. + // Auto-detect protocol based on request path and handle conversion if needed + // If request is Claude format (/v1/messages) but provider doesn't support it natively, + // convert to OpenAI format (/v1/chat/completions) + if apiName == provider.ApiNameAnthropicMessages && !providerConfig.IsSupportedAPI(provider.ApiNameAnthropicMessages) { + // Provider doesn't support Claude protocol natively, convert to OpenAI format + newPath := strings.Replace(path.Path, provider.PathAnthropicMessages, provider.PathOpenAIChatCompletions, 1) + _ = proxywasm.ReplaceHttpRequestHeader(":path", newPath) + // Update apiName to match the new path + apiName = provider.ApiNameChatCompletion + // Mark that we need to convert response back to Claude format + ctx.SetContext("needClaudeResponseConversion", true) + log.Debugf("[Auto Protocol] Claude request detected, provider doesn't support natively, converted path from %s to %s, apiName: %s", path.Path, newPath, apiName) + } else if apiName == provider.ApiNameAnthropicMessages { + // Provider supports Claude protocol natively, no conversion needed + log.Debugf("[Auto Protocol] Claude request detected, provider supports natively, keeping original path: %s, apiName: %s", path.Path, apiName) + } } if contentType, _ := proxywasm.GetHttpRequestHeader(util.HeaderContentType); contentType != "" && !strings.Contains(contentType, util.MimeTypeApplicationJson) {