Skip to content

feat(bridge): support image input (multimodal) - #1

Merged
Zhengyuuuui merged 1 commit into
Zhengyuuuui:mainfrom
LeonardW-sl:feat/image-input
Oct 7, 2026
Merged

Zhengyuuuui merged 1 commit into
Zhengyuuuui:mainfrom
LeonardW-sl:feat/image-input

Conversation

@LeonardW-sl

Copy link
Copy Markdown
Contributor

背景

当前 bridge 只处理文本与 tool calls:ConvertIncomingMessage 用 NormalizeMessageContent 提取文本后,content 数组里的图片块会被静默丢弃(且纯图片消息因 text == "" 直接返回 nil 被整条丢掉)。本 PR 让 qoder2api 支持图片输入(多模态)。

改动

internal/bridge/messages.go:

  • ConvertIncomingMessage 新增 images := ExtractMessageImages(msg);丢弃条件由 text == "" 改为 text == "" && len(images) == 0(纯图片消息不再被丢);user 分支改用 BuildUserMessageWithImages(text, images)。
  • 新增 BuildUserMessageWithImages:按 Qoder 协议构造 contents —— 图片在前 {type:image_url, image_url:{url}},文本在后 {type:text, text}(与官方客户端一致)。
  • 新增 ExtractMessageImages,覆盖三种形态:
    • OpenAI Chat Completions:{type:image_url, image_url:{url}}
    • OpenAI Responses:{type:input_image, image_url:"..."}
    • Claude Messages:{type:image, source:{type:base64|url, ...}}(转成 data/http URL)
    • 另有 contents 数组回退(图片可能直接放在 contents 里)。
  • 新增 imageURLFromPart / claudeImageToURL / looksLikeImageURL:只放行 data: 与 http(s),其余丢弃。
  • BuildUserMessage(text) 保留为薄封装,向后兼容。

因为 chat.go / claude.go / codex.go 三个入口都走 BuildQoderMessages → ConvertIncomingMessage,OpenAI / Claude / Codex 三条协议一次性覆盖。

测试

新增 image_test.go(7 个单元用例)与 image_integration_test.go(1 个端到端用例:起 mock 上游,走完整 BuildQoderMessages → CallQoderWithOpts → cosy.Encode,把上游收到的 body 解码回来断言含 image part)。

$ gofmt -l internal/bridge/     # 无输出
$ go build ./...                # ok
$ go vet ./internal/bridge/     # ok
$ go test ./...                 # ok

真机验证

导入真实 Qoder 账号(OAuth)后实测:

  • 1x1 图片 → 上游返回 The image length and width do not meet the model restrictions. [height:1 or width:1 must be larger than 10],证明图片 part 已被上游当作图片解析;
  • 32x32 纯红 PNG → 模型回答「红色」,HTTP 200。

兼容性

  • 纯文本请求行为不变(有单测断言);
  • 无新增依赖;BuildUserMessage 签名不变。

- ConvertIncomingMessage extracts image parts and builds Qoder contents
  with {type:image_url, image_url:{url}} before the text part
- image-only user messages are no longer silently dropped
- ExtractMessageImages handles OpenAI image_url, Responses input_image and
  Claude image blocks (base64 + url sources), with a contents[] fallback
- reject non data:/http(s) image URLs
- add unit tests and an end-to-end test asserting the encoded upstream body
  carries the image part
Copilot AI balanced review requested due to automatic review settings October 3, 2026 08:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Zhengyuuuui
Zhengyuuuui merged commit be38b8f into Zhengyuuuui:main Oct 7, 2026
@Zhengyuuuui

Copy link
Copy Markdown
Owner

牛谢谢好兄弟维护

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