feat: support OpenRouter image generation endpoint - #5964
Conversation
OpenRouter's image generation API lives at POST {base}/v1/images
(https://openrouter.ai/docs/features/multimodal/image-generation-api),
not the OpenAI-style /v1/images/generations. Route image generation
requests on OpenRouter channels to that path so downstream
/v1/images/generations requests relay correctly. The existing OpenAI
image handler already parses OpenRouter's response shape
(created/data[].b64_json/usage).
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
WalkthroughThis PR adds OpenRouter-specific image-generation URL routing and image-body merging. OpenRouter image requests now use ChangesOpenRouter image handling
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@relay/channel/openai/openrouter_image_test.go`:
- Around line 14-62: The new tests in TestGetRequestURLOpenRouterImageGeneration
and TestGetRequestURLOpenRouterChatUnchanged should use testify instead of raw
t.Fatalf. Update the openrouter_image_test.go test setup to import
github.com/stretchr/testify/require and use require.NoError for GetRequestURL
errors, then use assert.Equal for the returned URL comparisons. Keep the
existing test names and Adaptor/GetRequestURL usage, but replace manual fatal
checks with the appropriate require/assert helpers throughout.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: dc97ac6c-a51f-4c6e-adbe-765e46fce95e
📒 Files selected for processing (2)
relay/channel/openai/adaptor.gorelay/channel/openai/openrouter_image_test.go
|
Addressed the review comment — tests now use |
|
这里把 OpenRouter 图片生成请求改到 建议补充 OpenRouter 图片 API 需要的显式字段,或增加 OpenRouter 专用 image DTO/转换路径,并加一个回归测试验证至少一个 OpenRouter 专属参数(如 参考:https://openrouter.ai/docs/guides/overview/multimodal/image-generation.md |
|
@Calcium-Ion @t0ng7u @seefs001 would appreciate a review! |
The generic dto.ImageRequest serialization drops unknown fields (aspect_ratio, resolution, seed, input_references, provider) that OpenRouter's /v1/images endpoint accepts. Merge Extra back into the outbound body for OpenRouter image generation requests only, leaving all other channels' serialization unchanged.
|
感谢审阅——您说得对,仅修复 URL 还不够。通用的 实现方式: 我注意到 测试: 已用真实 API 调用完成端到端验证: 本地运行 new-api 实例并配置 OpenRouter 渠道,向 这种方式能通用覆盖 OpenRouter 图片生成文档 中的所有参数——OpenRouter 未来新增的参数也无需再改动即可透传。如果您更倾向于显式的类型化字段,我也很乐意调整。 |
📝 变更描述 / Description
OpenRouter 现已提供图像生成 API,端点为
POST {base}/v1/images(官方文档),与 OpenAI 风格的/v1/images/generations路径不同。目前 OpenRouter 渠道复用 OpenAI adaptor,图像请求会被转发到https://openrouter.ai/api/v1/images/generations,上游返回 404。本 PR 在
openai.Adaptor.GetRequestURL中为 OpenRouter 渠道的RelayModeImagesGenerations增加特判,改为请求{base}/v1/images。请求体(model/prompt/n/size)和响应体(created/data[].b64_json/usage)均为 OpenAI Images 兼容格式,OpenRouter 会忽略未知字段,因此现有的ImageRequest转换和OpenaiImageHandler(含 usage 计费解析)无需改动。OpenRouter now offers an image generation API at
POST {base}/v1/images(docs) — a flat path, unlike OpenAI's/v1/images/generations. Since the OpenRouter channel reuses the OpenAI adaptor, image requests currently go tohttps://openrouter.ai/api/v1/images/generationsand get a 404 upstream. This PR special-casesRelayModeImagesGenerationsfor OpenRouter channels inGetRequestURL. Request and response shapes are OpenAI-Images-compatible (OpenRouter silently ignores unknown fields, responses carrycreated/data[].b64_json/usage), so the existingConvertImageRequestpassthrough andOpenaiImageHandlerbilling path work unchanged.🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
📸 运行证明 / Proof of Work
1. 路径验证 / Endpoint verification — OpenRouter 上游确认扁平路径存在,
/generations不存在:2. 单元测试 / Unit tests(新增
openrouter_image_test.go,覆盖图像 URL 特判 + chat 路径不受影响):3. 端到端验证 / E2E — 本地启动 new-api(SQLite),配置 type=20 OpenRouter 渠道指向一个记录请求路径的 mock 上游,然后调用 new-api 的
/v1/images/generations:Summary by CodeRabbit
/v1/imagesendpoint.