fix(dify): initialize file pointer before remote-image field assignment - #5134
Conversation
`requestOpenAI2Dify()` declared `var file *DifyFile` and then, in the
`media.IsRemoteImage()` branch, wrote directly to `file.Type`,
`file.TransferMode`, and `file.URL` without ever pointing `file` at a
real value. Any chat completion sent through a Dify channel that
contained an `image_url` whose URL starts with `http` therefore
triggered:
runtime error: invalid memory address or nil pointer dereference
and was returned to the caller as:
{"error":{"message":"Panic detected, ...","type":"new_api_error"}}
The fix mirrors the upload path (which already returns a `*DifyFile`
via `uploadDifyFile`): construct the value explicitly with a
composite literal so the pointer is non-nil before the field writes.
Fixes QuantumNous#2083
Co-authored-by: Cursor <cursoragent@cursor.com>
|
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 (1)
WalkthroughThe pull request fixes a runtime panic in the Dify relay handler. When processing remote images in OpenAI-format requests, the code now properly initializes the ChangesRemote Image Handling in Dify Relay
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
panjinhe
left a comment
There was a problem hiding this comment.
Reviewed by Codex: small focused fix, checks passing.
|
Please disregard my previous approval on this PR. It was submitted by mistake while I was checking the wrong repository. |
… id 等) 主要变更(QuantumNous/new-api → adc390c): - feat: 渠道被禁用后可配置是否清空渠道粘性 (QuantumNous#5306) - feat(web): profile 页显示 user id (QuantumNous#5317) - fix: 复用 channel handler 的 stream scanner buffer (QuantumNous#5225) - fix: 收窄 OpenAI o 系列模型适配范围 (QuantumNous#5293) - fix(relay): GLM Anthropic 兼容避免 chunked encoding (QuantumNous#5307) - fix: 新增 relay idle 连接超时配置 (QuantumNous#5309) - fix: 限制匿名请求体大小 (QuantumNous#5244) - fix(distributor): 修复 video generations task_id 模型解析 (QuantumNous#5133) - fix(dify): 远程图片字段赋值前初始化 file pointer (QuantumNous#5134) - fix(i18n): 优化 thinking adapter 文案 (QuantumNous#5242) 去合规一致性:上游未触及任何合规文件,合并完整保留本地去合规状态 (payment_setting.go / payment-settings-section.tsx / recharge-form-card.tsx / risk-acknowledgement-dialog.tsx 均未被改动)。 i18n:web/default 6 语言(en/zh/fr/ja/ru/vi)key 并集三路合并 + i18n:sync 规范化,4584 keys/语言,missing/extras/untranslated 全为 0。 验证:go build ./... ✅ + bun run typecheck ✅
📝 变更描述 / Description
relay/channel/dify/relay-dify.go的requestOpenAI2Dify()在第 160 行声明了var file *DifyFile(值为nil),随后在media.IsRemoteImage()分支里没有初始化file就直接写入字段:任何包含
image_url且 URL 以http开头的消息经过 Dify 渠道时都会触发:并被全局 panic recover 包装成 500 返回客户端。
修复方法是把字段赋值改成 composite literal,让指针在写入前指向一个真实对象,与
else分支里uploadDifyFile返回&DifyFile{...}的方式一致。🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
#2083—— 无任何 open / merged PR 在修。transfer_mode=remote_url + url=...形式被加入difyReq.Files)。go build ./relay/channel/dify/✅;gofmt -l干净;go vet输出与main分支一致(adaptor.go 那 2 条unreachable codewarning 是先前就存在的,本 PR 未引入新 warning)。📸 运行证明 / Proof of Work
Bug 现场(来自 #2083)
请求 body:
{ "model": "your-dify-model", "messages": [ { "role": "user", "content": [ {"type": "text", "text": "describe this image"}, {"type": "image_url", "image_url": {"url": "https://example.com/cat.png"}} ] } ] }修复前响应(500):
{ "error": { "message": "Panic detected, error: runtime error: invalid memory address or nil pointer dereference", "type": "new_api_error" } }修复点
relay/channel/dify/relay-dify.go:边界行为
http:///https://开头)transfer_mode=remote_url透传给 DifyuploadDifyFile)files,行为不变本地验证
备注
dify包内目前没有单元测试基础设施(无*_test.go),且requestOpenAI2Dify依赖gin.Context与relaycommon.RelayInfo,单测改造成本超出本 PR 的范围。如果维护者希望补回归测试,我可以另起 PR 引入 httptest + mock。Made with Cursor
Summary by CodeRabbit