Skip to content

Conversation

@xiaomakuaiz
Copy link
Collaborator

现有的 OpenAI API 兼容接口不支持标准的 OpenAI messages 格式,特别是当 content 字段为数组格式时会解析失败。

  1. 扩展 MessageContent 类型:实现自定义的 JSON 序列化/反序列化,支持 content 既可以是字符串,也可以是包含 text/type 的对象数组
  2. 添加 stream_options 支持:支持 OpenAI 标准的 stream_options 参数(如 include_usage)
  3. 更新响应格式:在流式响应中添加 usage 字段支持,符合 OpenAI 标准
  • domain/openai.go:

    • 新增 MessageContent 类型及其 JSON 序列化方法
    • 新增 OpenAIStreamOptions 结构体
    • 更新 OpenAIMessage.Content 类型从 string 改为 *MessageContent
    • 在流式响应中添加 usage 字段
  • handler/share/chat.go:

    • 更新消息内容提取逻辑,使用 MessageContent.String() 方法
    • 修复流式和非流式响应中的 content 序列化
  • 已通过单元测试验证 MessageContent 可以正确解析字符串和数组格式

  • 编译通过,无语法错误

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings November 12, 2025 11:53
Copilot finished reviewing on behalf of xiaomakuaiz November 12, 2025 11:56
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes OpenAI API compatibility issues in the /share/v1/chat/completions endpoint by implementing support for the standard OpenAI message format where content can be either a string or an array of content parts.

  • Introduces a custom MessageContent type with JSON marshaling/unmarshaling to handle both string and array content formats
  • Adds stream_options support for OpenAI streaming parameters
  • Updates message handling throughout the chat completion flow to work with the new MessageContent type

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.

File Description
backend/domain/openai.go Adds MessageContent type with custom JSON serialization, OpenAIStreamOptions struct, and updates OpenAIMessage.Content from string to *MessageContent; adds usage field to stream response
backend/handler/share/chat.go Updates message extraction logic to use MessageContent.String() method and modifies response construction to work with new MessageContent type in both streaming and non-streaming flows

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings November 14, 2025 10:42
@xiaomakuaiz xiaomakuaiz force-pushed the fix/openai-api-compatibility branch from cfc53da to ed8f3f5 Compare November 14, 2025 10:42
Copilot finished reviewing on behalf of xiaomakuaiz November 14, 2025 10:44
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

xiaomakuaiz and others added 4 commits November 14, 2025 19:26
现有的 OpenAI API 兼容接口不支持标准的 OpenAI messages 格式,特别是当 content 字段为数组格式时会解析失败。

1. **扩展 MessageContent 类型**:实现自定义的 JSON 序列化/反序列化,支持 content 既可以是字符串,也可以是包含 text/type 的对象数组
2. **添加 stream_options 支持**:支持 OpenAI 标准的 stream_options 参数(如 include_usage)
3. **更新响应格式**:在流式响应中添加 usage 字段支持,符合 OpenAI 标准

- `domain/openai.go`:
  - 新增 `MessageContent` 类型及其 JSON 序列化方法
  - 新增 `OpenAIStreamOptions` 结构体
  - 更新 `OpenAIMessage.Content` 类型从 string 改为 *MessageContent
  - 在流式响应中添加 usage 字段

- `handler/share/chat.go`:
  - 更新消息内容提取逻辑,使用 MessageContent.String() 方法
  - 修复流式和非流式响应中的 content 序列化

- 已通过单元测试验证 MessageContent 可以正确解析字符串和数组格式
- 编译通过,无语法错误

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Co-authored-by: monkeycode-ai <[email protected]>

Co-authored-by: monkeycode-ai <[email protected]>
Co-authored-by: MonkeyCode-AI <[email protected]>
Co-authored-by: MonkeyCode-AI <[email protected]>
Co-authored-by: MonkeyCode-AI <[email protected]>
根据 PR chaitin#1512 代码审查意见,进行以下优化:

**安全性修复 (P0)**
- 修复 JSON 注入安全漏洞:移除不安全的字符串拼接构造 JSON 的方式
- 添加 NewStringContent 和 NewArrayContent 构造函数,直接构造对象而非通过 JSON 序列化

**性能优化**
- String() 方法使用 strings.Builder 替代字符串拼接,提升性能
- 多个 text 部分之间添加空格分隔符,避免语义错误

**测试覆盖**
- 添加完整的单元测试覆盖 MessageContent 类型
- 测试字符串格式解析(包括特殊字符、Unicode、换行符等)
- 测试数组格式解析(单个/多个 text 部分、混合类型)
- 测试无效输入处理
- 测试序列化/反序列化往返

**代码改进**
- 更新 handler/share/chat.go 使用安全的构造函数
- 所有测试通过验证

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Co-authored-by: MonkeyCode-AI <[email protected]>

Co-authored-by: MonkeyCode-AI <[email protected]>
Co-authored-by: MonkeyCode-AI <[email protected]>
Co-authored-by: MonkeyCode-AI <[email protected]>
Co-authored-by: MonkeyCode-AI <[email protected]>
Co-authored-by: MonkeyCode-AI <[email protected]>
Co-authored-by: MonkeyCode-AI <[email protected]>
Co-authored-by: MonkeyCode-AI <[email protected]>
Co-authored-by: MonkeyCode-AI <[email protected]>
- 添加 ImageURL 字段支持 image_url 类型的内容部分
- 新增 OpenAIContentPartURL 结构体处理嵌套的 URL 对象
- 修复 MarshalJSON 使用指针接收器以保持与 UnmarshalJSON 的一致性
- 修复 String 方法的空格逻辑,使用 builder.Len() 替代索引判断

修复了测试用例中 image_url 数据被静默忽略的问题,确保正确解析包含图片 URL 的混合内容。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Co-authored-by: MonkeyCode-AI <[email protected]>
Co-authored-by: MonkeyCode-AI <[email protected]>
Co-authored-by: MonkeyCode-AI <[email protected]>
Co-authored-by: MonkeyCode-AI <[email protected]>
Copilot AI review requested due to automatic review settings November 14, 2025 11:42
@xiaomakuaiz xiaomakuaiz force-pushed the fix/openai-api-compatibility branch from 9debddd to b8f2b95 Compare November 14, 2025 11:42
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@xiaomakuaiz xiaomakuaiz merged commit e9d30eb into chaitin:main Nov 14, 2025
9 of 10 checks passed
@xiaomakuaiz xiaomakuaiz deleted the fix/openai-api-compatibility branch November 14, 2025 12:56
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.

1 participant