Skip to content

Conversation

@johnlanni
Copy link
Collaborator

@johnlanni johnlanni commented Aug 21, 2025

Overview

This PR implements automatic protocol compatibility that enables seamless support for both OpenAI and Claude API protocols without requiring user configuration. The plugin now intelligently detects the incoming request protocol and automatically handles protocol conversion when needed.

Key Features

✨ Auto Protocol Detection

  • OpenAI Protocol: Automatically detected via /v1/chat/completions path
  • Claude Protocol: Automatically detected via /v1/messages path

🧠 Intelligent Protocol Conversion

  • Provider Capability Detection: Automatically checks if target provider natively supports Claude protocol
  • Smart Conversion: Only converts when necessary (provider doesn't support Claude natively)
  • Bidirectional Support: Handles both request and response conversions
  • Streaming Compatible: Supports both streaming and non-streaming responses

🔄 Backward Compatibility

  • Existing Configurations: All existing configurations continue to work
  • Protocol Field: The protocol field is now optional and ignored for auto-detection

Technical Implementation

Core Components

  1. Auto Detection Logic (main.go): Path-based protocol detection in onHttpRequestHeader
  2. Protocol Converter (provider/claude_to_openai.go): Bidirectional conversion between Claude and OpenAI formats
  3. Provider Capability Check (provider/provider.go): IsSupportedAPI method for checking native protocol support
  4. Context Management: Uses needClaudeResponseConversion to track conversion state across request lifecycle

Request Flow

Incoming Request → Protocol Detection → Provider Capability Check → Smart Conversion → Target Provider

Response Conversion ← Protocol Detection ← Provider Response ← Target Provider

Usage Examples

provider:
  type: qwen  # Auto-detects and converts when needed
  apiTokens: ["token"]

Supported Request Formats

# OpenAI Protocol
curl http://your-domain/v1/chat/completions -d '{...}'

# Claude Protocol (auto-converted if provider doesn't support natively)
curl http://your-domain/v1/messages -d '{...}'

Code Changes

New Files

  • provider/claude_to_openai.go: Protocol conversion logic
  • Updated documentation and examples

Modified Files

  • main.go: Auto-detection logic and response conversion
  • provider/provider.go: Removed unused Claude protocol configuration, added capability checks

Removed Code

  • protocolClaude constant and related configuration logic
  • IsClaudeProtocol() method
  • Manual protocol validation for Claude

Testing

  • ✅ OpenAI protocol requests work as before
  • ✅ Claude protocol requests automatically convert when needed
  • ✅ Native Claude providers (like claude type) work without conversion
  • ✅ Non-native providers (like qwen type) auto-convert Claude requests
  • ✅ Streaming responses work for both protocols
  • ✅ Backward compatibility maintained

Benefits

  1. Simplified User Experience: No need to understand or configure protocol types
  2. Universal Compatibility: Any provider can now handle any protocol format
  3. Intelligent Optimization: Only converts when necessary, preserving native performance
  4. Future-Proof: Easy to add support for additional protocols

Migration Guide

No action required - existing configurations will continue to work. Users can optionally remove the protocol field from their configurations as it's no longer needed.


This change transforms the AI proxy plugin into a truly "protocol-agnostic" gateway, dramatically simplifying the user experience while maintaining full backward compatibility.

@lingma-agents
Copy link

lingma-agents bot commented Aug 21, 2025

AI代理插件新增Claude协议自动兼容功能

变更概述
  • 新功能

    • 实现了OpenAI和Claude协议的自动检测与智能转换功能,用户无需手动配置即可使用两种协议
    • 添加了基于请求路径的协议自动识别逻辑,支持/v1/chat/completions(OpenAI)和/v1/messages(Claude)路径
    • 引入了Claude到OpenAI协议双向转换器,支持请求和响应的格式转换
    • 增加了供应商原生API支持检测机制,仅在必要时进行协议转换以优化性能
    • 支持流式和非流式响应的协议转换
  • 重构

    • 移除了原有的手动protocol配置字段和相关验证逻辑,改为自动检测机制
    • 将协议转换逻辑从主流程中分离到独立的转换器组件中
  • 文档

    • 更新了中英文README文档,添加了自动协议兼容功能的详细说明和使用示例
    • 增加了配置示例和请求/响应格式说明,涵盖原生支持和需要转换的场景
    • 更新了配置参数说明,标记protocol字段为可选并说明自动检测机制
  • 测试更新

    • 添加了完整的协议转换测试用例,覆盖OpenAI和Claude协议的请求处理
    • 验证了原生支持和需要转换的供应商场景
    • 测试了流式响应和非流式响应的协议转换功能
变更文件
文件路径 变更说明
plugins/​wasm-go/​extensions/​ai-proxy/​README.​md 添加了自动协议兼容功能的中文文档说明,包括功能介绍、使用示例和配置说明。
plugins/​wasm-go/​extensions/​ai-proxy/​README_​EN.​md 添加了自动协议兼容功能的英文文档说明,包括功能介绍、使用示例和配置说明。
plugins/​wasm-go/​extensions/​ai-proxy/​main.​go 实现了基于请求路径的协议自动检测逻辑,在请求头处理阶段进行路径转换和标记,并在响应处理阶段进行协议格式转换。
plugins/​wasm-go/​extensions/​ai-proxy/​provider/​claude_​to_​openai.​go 新增Claude到OpenAI协议转换器,实现了请求和响应的双向转换功能,支持文本、多模态内容、工具调用等复杂场景。
plugins/​wasm-go/​extensions/​ai-proxy/​provider/​provider.​go 添加了API支持检测的公共接口,实现了请求体处理阶段的协议转换逻辑,并优化了配置验证逻辑。

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @lingma-agents 分析这个方法的性能瓶颈并提供优化建议。

  • @lingma-agents 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @lingma-agents 请总结上述讨论并提出解决方案。

  • @lingma-agents 请根据讨论内容生成优化代码。

--- ### AI Agent Plugin adds automatic compatibility function of Claude protocol
Change Overview
  • New Features

    • Implements automatic detection and intelligent conversion functions of OpenAI and Claude protocols, and users can use both protocols without manual configuration.
    • Added protocol automatic identification logic based on request paths, supporting /v1/chat/completes(OpenAI) and /v1/messages(Claude) paths
    • Introduced Claude to OpenAI protocol bidirectional converter, supporting format conversion of requests and responses
    • Added vendor native API support detection mechanism, only protocol conversion is performed when necessary to optimize performance
    • Supports streaming and non-streaming response protocol conversion
  • Refactor

    • Removed the original manual protocol configuration field and related verification logic, and changed to an automatic detection mechanism
    • Separate protocol conversion logic from the main process into independent converter components
  • document

    • Updated Chinese and English README documentation, added detailed descriptions and usage examples of automatic protocol compatibility
    • Added configuration examples and request/response format descriptions to cover native support and scenarios that require conversion
    • Updated configuration parameter description, marked protocol field as optional and explained the automatic detection mechanism

Test Update

  • Added a complete protocol conversion test case to cover request processing of OpenAI and Claude protocols
  • Verified native support and supplier scenarios that require conversion
  • Tested protocol conversion functions of streaming and non-streaming response
Change file
File path Change instructions
plugins/​wasm-go/​extensions/​ai-proxy/​README.​md Added Chinese documentation descriptions for automatic protocol compatibility, including feature introduction, usage examples, and configuration instructions.
plugins/​wasm-go/​extensions/​ai-proxy/​README_​EN.​md Added English documentation descriptions for automatic protocol compatibility, including feature introduction, usage examples, and configuration instructions.
plugins/​wasm-go/​extensions/​ai-proxy/​main.​go implements protocol automatic detection logic based on request paths, path conversion and tagging are performed in the request header processing stage, and protocol format conversion is performed in the response processing stage.
plugins/​wasm-go/​extensions/​ai-proxy/​provider/​claude_​to_​openai.​go A new Claude to OpenAI protocol converter is added, which realizes the bidirectional conversion function of requests and responses, and supports complex scenarios such as text, multimodal content, tool calls, etc.
plugins/​wasm-go/​extensions/​ai-proxy/​provider/​provider.​go Added a public interface for API support detection, implementing protocol conversion logic in the request body processing stage, and optimizing configuration verification logic.

💡 Tips

How to communicate with lingma-agents

📜 Reply to comments directly
Reply to this comment directly and lingma-agents will automatically process your request. For example:

  • _Add detailed comment description in the current code. _

  • _Please introduce the LRU transformation plan you mentioned in detail and explain it using pseudo-code. _

**📜 Mark ** at line of code
Create comments at a specific location in the file and @lingma-agents. For example:

  • _@lingma-agents Analyze the performance bottlenecks of this method and provide optimization suggestions. _

  • _@lingma-agents Generate optimization code for this method. _

📜 Ask a question during discussion
In any discussion @lingma-agents to get help. For example:

  • _@lingma-agents Please summarize the above discussion and propose solutions. _

  • _@lingma-agents Please generate optimization code based on the discussion content. _

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 45.31%. Comparing base (ef31e09) to head (446c4dd).
⚠️ Report is 669 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2810      +/-   ##
==========================================
+ Coverage   35.91%   45.31%   +9.40%     
==========================================
  Files          69       82      +13     
  Lines       11576    13224    +1648     
==========================================
+ Hits         4157     5992    +1835     
+ Misses       7104     6886     -218     
- Partials      315      346      +31     

see 79 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@johnlanni johnlanni merged commit 16a18c6 into alibaba:main Aug 25, 2025
16 checks passed
ink-hz pushed a commit to ink-hz/higress-ai-capability-auth that referenced this pull request Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants