Skip to content

Conversation

@daixijun
Copy link
Contributor

Ⅰ. Describe what this PR did

  1. 添加对 OpenAI responses 接口 Body 的处理
  2. 添加火山方舟大模型 responses 接口支持

Ⅱ. Does this pull request fix one issue?

Ⅲ. Why don't you add test cases (unit test/integration test)?

Ⅳ. Describe how to verify it

Ⅴ. Special notes for reviews

@lingma-agents
Copy link

lingma-agents bot commented Jun 27, 2025

新增火山方舟大模型Responses接口支持

变更概述

新功能

  • 新增API端点支持
    • main.go中添加对/v1/responses路径的识别,映射为provider.ApiNameResponses枚举值,扩展了AI代理对火山方舟大模型接口的支持范围。
    • doubao.go中:
      • 新增常量doubaoResponsesPath/api/v3/responses)和对应API名称ApiNameResponses,完成路径与接口类型的绑定。
      • DefaultCapabilities()方法中注册Responses接口路径,使其纳入火山方舟服务支持列表。
  • 请求体参数适配
    • TransformRequestBody()方法中新增ApiNameResponses分支:
      • 通过sjson.DeleteBytes删除火山方舟不支持的参数parallel_tool_callstool_choicestream_options(第85-90行)。
      • 使用日志模块记录参数删除失败的警告信息(第87-88行)。
  • 路径解析增强
    GetApiName()方法中添加对doubaoResponsesPath的匹配逻辑,确保请求路径正确映射到ApiNameResponses

重构

  • 代码结构优化
    doubao.go中新增对gjsonsjson库的导入(第9-10行),为JSON操作提供统一工具支持,提升代码可维护性。
变更文件
文件路径 变更说明
plugins/​wasm-go/​extensions/​ai-proxy/​main.​go 新增对/v1/responses路径的API名称识别逻辑
plugins/​wasm-go/​extensions/​ai-proxy/​provider/​doubao.​go 新增Responses接口路径映射、请求体参数清理逻辑及日志记录
时序图
sequenceDiagram
    participant HC as HttpContext
    participant DP as doubaoProvider
    HC->>DP: HandleRequest("/v1/responses")
    DP->>DP: GetApiName() → ApiNameResponses
    DP->>DP: TransformRequestBody()
        DP-->>DP: 删除不支持参数
        DP-->>DP: 记录删除失败日志
    DP-->>HC: 返回处理后的请求体
Loading

💡 小贴士

与 lingma-agents 交流的方式

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

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

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

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

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

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

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

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

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

--- ### Added new Volcano Ark Mockup Responses interface support
Change Overview

New Features

  • Added API endpoint support
    • Add the recognition of the /v1/responses path in main.go, mapped to the provider.ApiNameResponses enumeration value, extending the AI ​​proxy's support range for the volcanic ark big model interface.
    • In doubao.go:
      • Added the constant doubaoResponsesPath (/api/v3/responses) and the corresponding API name ApiNameResponses to complete the binding of the path and interface type.
      • Register the Responses interface path in the DefaultCapabilities() method to include it in the Volcano Ark service support list.
  • Request body parameter adaptation
    • Add ApiNameResponses branch to the TransformRequestBody() method:
      • Remove parameters parallel_tool_calls, tool_choice, stream_options that are not supported by sjson.DeleteBytes (lines 85-90).
      • Use the log module to record the warning message for failed parameters to delete (lines 87-88).
  • Path resolution enhancement
    Add matching logic to doubaoResponsesPath in the GetApiName() method to ensure that the request path is correctly mapped to ApiNameResponses.

Refactor

  • Code structure optimization
    Added new imports of gjson and sjson libraries (lines 9-10) to doubao.go to provide unified tool support for JSON operations and improve code maintainability.
Change file
File path Change instructions
plugins/​wasm-go/​extensions/​ai-proxy/​main.​go Added API name recognition logic for /v1/responses path
plugins/​wasm-go/​extensions/​ai-proxy/​provider/​doubao.​go Added Responses interface path mapping, request body parameter cleaning logic and logging
Sequence chart
sequenceDiagram
    participant HC as HttpContext
    participant DP as doubaoProvider
    HC->>DP: HandleRequest("/v1/responses")
    DP->>DP: GetApiName() → ApiNameResponses
    DP->>DP: TransformRequestBody()
        DP-->>DP: Delete parameters not supported
        DP-->>DP: Log deletion failed log
    DP-->>HC: Returns the processed request body
Loading

💡 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 46.04%. Comparing base (ef31e09) to head (777646f).
Report is 589 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #2509       +/-   ##
===========================================
+ Coverage   35.91%   46.04%   +10.13%     
===========================================
  Files          69       81       +12     
  Lines       11576    13020     +1444     
===========================================
+ Hits         4157     5995     +1838     
+ Misses       7104     6680      -424     
- Partials      315      345       +30     

see 78 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.

Copy link

@lingma-agents lingma-agents bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔎 代码评审报告

🎯 评审意见概览
严重度 数量 说明
🔴 Blocker 0 阻断性问题,需立即修复。例如:系统崩溃、关键功能不可用或严重安全漏洞。
🟠 Critical 2 严重问题,高优先级修复。例如:核心功能异常或性能瓶颈影响用户体验。
🟡 Major 1 主要问题,建议修复。例如:非核心功能缺陷或代码维护性较差。
🟢 Minor 0 次要问题,酬情优化。例如:代码格式不规范或注释缺失。

总计: 3 个问题

📋 评审意见详情
💡 代码实现建议
以下是文件级别的代码建议,聚焦于代码的可读性、可维护性和潜在问题。
🔹 plugins/wasm-go/extensions/ai-proxy/main.go (1 💬)
🔹 plugins/wasm-go/extensions/ai-proxy/provider/doubao.go (2 💬)
🚀 架构设计建议
以下是对代码架构和设计的综合分析,聚焦于跨文件交互、系统一致性和潜在优化空间。
🔍1. 路径版本号不一致导致API名称识别风险

在main.go中使用'/v1/responses'路径检查,而doubao.go中定义的常量为'/api/v3/responses',路径版本号不一致可能导致API名称无法正确匹配。跨文件的路径定义需统一版本规范以确保系统一致性。

📌 关键代码

+	if strings.HasSuffix(path, "/v1/responses") {
+	doubaoResponsesPath       = "/api/v3/responses"

⚠️ 潜在风险

路径版本不一致将导致API路由逻辑失效,请求无法正确分发到目标接口,影响功能可用性。

🔍2. 路径匹配方式存在误判风险

在doubao.go的GetApiName方法中,使用strings.Contains检查路径是否包含doubaoResponsesPath,可能导致路径误匹配(如'/api/v3/responses/test'会被错误识别)。应采用精确路径解析或正则表达式增强匹配准确性。

📌 关键代码

+	if strings.Contains(path, doubaoResponsesPath) {

⚠️ 潜在风险

误匹配将导致请求被错误路由,可能引发安全漏洞或功能异常。

🔍3. JSON操作技术债务问题

在TransformRequestBody方法中直接使用sjson操作原始字节流处理JSON参数,未采用结构体解析方式。此方式易引发字段误操作,且难以维护。建议引入DTO对象进行类型安全操作。

📌 关键代码

+	body, err = sjson.DeleteBytes(body, param)

⚠️ 潜在风险

字符串操作易受JSON格式变化影响,增加未来维护成本并可能引入隐蔽错误。

🔍4. 跨功能代码重复问题

TransformRequestBody中对不同API的请求体修改逻辑(如参数删除/添加)采用分散实现,未提取公共方法。建议将JSON操作封装为复用函数以减少冗余代码。

📌 关键代码

+	for _, param := range []string{"parallel_tool_calls", ... } { body, err = sjson.DeleteBytes(body, param) ... }
+	body, err = sjson.SetBytes(body, "watermark", false)

⚠️ 潜在风险

重复代码将导致维护时需要同步修改多个位置,增加人为错误风险。

审查详情
📒 文件清单 (2 个文件)
📝 变更: 2 个文件

📝 变更文件:

  • plugins/wasm-go/extensions/ai-proxy/main.go
  • plugins/wasm-go/extensions/ai-proxy/provider/doubao.go

💡 小贴士

与 lingma-agents 交流的方式

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

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

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

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

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

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

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

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

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

Comment on lines +409 to +411
if strings.HasSuffix(path, "/v1/responses") {
return provider.ApiNameResponses
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

路径中的版本号与常量定义不一致,导致API名称无法正确识别

🟠 Critical | 🐞 Bugs

📋 问题详情

在getApiName函数中新增的条件检查路径是否以“/v1/responses”结尾,而doubao.go中定义的doubaoResponsesPath是“/api/v3/responses”。这两个路径的版本号(v1 vs v3)和结构不一致,导致条件判断失败,无法正确返回ApiNameResponses。需统一路径版本和结构。

💡 解决方案

将路径检查中的“/v1/responses”改为与doubao.go中定义的doubaoResponsesPath一致的“/api/v3/responses”:

-if strings.HasSuffix(path, "/v1/responses") {
+if strings.HasSuffix(path, "/api/v3/responses") {

您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

Comment on lines +85 to +90
for _, param := range []string{"parallel_tool_calls", "tool_choice", "stream_options"} {
body, err = sjson.DeleteBytes(body, param)
if err != nil {
log.Warnf("[doubao] failed to delete %s in request body, err: %v", param, err)
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参数删除错误处理不充分,导致错误被忽略

🟠 Critical | 🐞 Bugs

📋 问题详情

在TransformRequestBody函数中,当sjson.DeleteBytes发生错误时仅记录警告并继续处理,但未返回错误可能导致后续逻辑异常。建议在首次错误时立即返回错误。

💡 解决方案

在参数删除失败时立即返回错误:

@@ -88,6 +88,7 @@
     log.Warnf("[doubao] failed to delete %s in request body, err: %v", param, err)
+    return nil, err
     }
 }

您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

Comment on lines +114 to +116
if strings.Contains(path, doubaoResponsesPath) {
return ApiNameResponses
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用Contains检查路径可能导致误匹配,应改为更精确的检查方式

🟡 Major | 🐞 Bugs

📋 问题详情

在GetApiName函数中,使用strings.Contains检查路径是否包含doubaoResponsesPath(“/api/v3/responses”)可能导致误匹配,例如路径“/api/v3/responsesomething”也会触发该条件。建议改为使用strings.HasSuffix确保路径结尾精确匹配。

💡 解决方案

将Contains改为HasSuffix确保路径精确匹配:

-if strings.Contains(path, doubaoResponsesPath) {
+if strings.HasSuffix(path, doubaoResponsesPath) {

您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

Copy link
Collaborator

@CH3CHO CH3CHO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@CH3CHO CH3CHO merged commit bb786c9 into alibaba:main Jun 28, 2025
14 checks passed
@daixijun daixijun deleted the feat/add-doubao-responses branch June 28, 2025 06:49
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.

3 participants