Repository navigation
fix(codex): inline remote Responses input files - #6522
builderHelen wants to merge 1 commit into
Conversation
WalkthroughCodex request conversion now recursively rewrites remote ChangesCodex input file conversion
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant ConvertOpenAIResponsesRequest
participant rewriteRemoteInputFiles
participant service.LoadFileSource
Client->>ConvertOpenAIResponsesRequest: submit input with file_url
ConvertOpenAIResponsesRequest->>rewriteRemoteInputFiles: rewrite request.Input
rewriteRemoteInputFiles->>service.LoadFileSource: load remote file
service.LoadFileSource-->>rewriteRemoteInputFiles: file content and MIME type
rewriteRemoteInputFiles-->>ConvertOpenAIResponsesRequest: rewritten input
ConvertOpenAIResponsesRequest-->>Client: converted request
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches 💡 1⚔️ Resolve merge conflicts 💡
🧪 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 |
|
Closing because the deployment owner requires this compatibility fix to remain outside NewAPI. The implementation will move to the caller service. |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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/codex/input_file_test.go`:
- Around line 1-59: Update TestRewriteRemoteInputFiles and
TestRewriteRemoteInputFilesPreservesInlineAndPropagatesLoadErrors to use
testify/require for setup and fatal assertions instead of t.Fatalf, including
unmarshalling and error checks. Add the required testify/require import while
preserving the existing test coverage and non-fatal assertion behavior.
In `@relay/channel/codex/input_file.go`:
- Around line 3-9: Update rewriteRemoteInputFiles to use the repository’s
common.Marshal and common.Unmarshal wrappers for both serialization operations,
matching the convention in adaptor.go. Remove the direct encoding/json import
and add the appropriate common package reference while preserving the existing
input-rewrite behavior.
🪄 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 Plus
Run ID: c98c06e9-0bbf-4f46-a10d-3612701986c1
📒 Files selected for processing (3)
relay/channel/codex/adaptor.gorelay/channel/codex/input_file.gorelay/channel/codex/input_file_test.go
| package codex | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestRewriteRemoteInputFiles(t *testing.T) { | ||
| input := json.RawMessage(`[{"role":"user","content":[{"type":"input_text","text":"summarize"},{"type":"input_file","file_url":"https://files.example.com/attachments/47/file.pdf?verify=secret"}]}]`) | ||
|
|
||
| rewritten, err := rewriteRemoteInputFiles(input, func(fileURL string) (string, error) { | ||
| if fileURL != "https://files.example.com/attachments/47/file.pdf?verify=secret" { | ||
| t.Fatalf("loader URL = %q", fileURL) | ||
| } | ||
| return "data:application/pdf;base64,JVBERi0=", nil | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("rewriteRemoteInputFiles() error = %v", err) | ||
| } | ||
|
|
||
| var decoded []map[string]any | ||
| if err := json.Unmarshal(rewritten, &decoded); err != nil { | ||
| t.Fatalf("unmarshal rewritten input: %v", err) | ||
| } | ||
| content := decoded[0]["content"].([]any) | ||
| file := content[1].(map[string]any) | ||
| if _, exists := file["file_url"]; exists { | ||
| t.Fatalf("rewritten input still contains file_url: %#v", file) | ||
| } | ||
| if file["file_data"] != "data:application/pdf;base64,JVBERi0=" { | ||
| t.Fatalf("file_data = %#v", file["file_data"]) | ||
| } | ||
| if file["filename"] != "file.pdf" { | ||
| t.Fatalf("filename = %#v, want URL basename", file["filename"]) | ||
| } | ||
| } | ||
|
|
||
| func TestRewriteRemoteInputFilesPreservesInlineAndPropagatesLoadErrors(t *testing.T) { | ||
| inline := json.RawMessage(`[{"role":"user","content":[{"type":"input_file","filename":"report.pdf","file_data":"data:application/pdf;base64,JVBERi0="}]}]`) | ||
| rewritten, err := rewriteRemoteInputFiles(inline, func(string) (string, error) { | ||
| t.Fatal("inline file must not invoke loader") | ||
| return "", nil | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("inline rewrite error = %v", err) | ||
| } | ||
| if string(rewritten) != string(inline) { | ||
| t.Fatalf("inline input changed: %s", rewritten) | ||
| } | ||
|
|
||
| wantErr := errors.New("download failed") | ||
| _, err = rewriteRemoteInputFiles(json.RawMessage(`[{"type":"input_file","file_url":"https://files.example.com/report.pdf"}]`), func(string) (string, error) { | ||
| return "", wantErr | ||
| }) | ||
| if !errors.Is(err, wantErr) { | ||
| t.Fatalf("rewrite error = %v, want %v", err, wantErr) | ||
| } | ||
| } |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Use testify/require for fatal assertions in this new test file.
All assertions here use t.Fatalf; per repo guidelines, new backend tests must use testify/require for setup/fatal checks (and testify/assert for non-fatal ones).
♻️ Proposed fix (representative diff)
import (
"encoding/json"
"errors"
"testing"
+
+ "github.com/stretchr/testify/require"
)
func TestRewriteRemoteInputFiles(t *testing.T) {
input := json.RawMessage(`...`)
rewritten, err := rewriteRemoteInputFiles(input, func(fileURL string) (string, error) {
- if fileURL != "https://files.example.com/attachments/47/file.pdf?verify=secret" {
- t.Fatalf("loader URL = %q", fileURL)
- }
+ require.Equal(t, "https://files.example.com/attachments/47/file.pdf?verify=secret", fileURL)
return "data:application/pdf;base64,JVBERi0=", nil
})
- if err != nil {
- t.Fatalf("rewriteRemoteInputFiles() error = %v", err)
- }
+ require.NoError(t, err)
var decoded []map[string]any
- if err := json.Unmarshal(rewritten, &decoded); err != nil {
- t.Fatalf("unmarshal rewritten input: %v", err)
- }
+ require.NoError(t, json.Unmarshal(rewritten, &decoded))
content := decoded[0]["content"].([]any)
file := content[1].(map[string]any)
- if _, exists := file["file_url"]; exists {
- t.Fatalf("rewritten input still contains file_url: %#v", file)
- }
- if file["file_data"] != "data:application/pdf;base64,JVBERi0=" {
- t.Fatalf("file_data = %#v", file["file_data"])
- }
- if file["filename"] != "file.pdf" {
- t.Fatalf("filename = %#v, want URL basename", file["filename"])
- }
+ _, hasFileURL := file["file_url"]
+ require.False(t, hasFileURL, "rewritten input still contains file_url")
+ require.Equal(t, "data:application/pdf;base64,JVBERi0=", file["file_data"])
+ require.Equal(t, "file.pdf", file["filename"])
}Apply the same pattern to TestRewriteRemoteInputFilesPreservesInlineAndPropagatesLoadErrors.
As per coding guidelines, "New or substantially rewritten Go backend tests must use testify/require for setup and fatal assertions and testify/assert for non-fatal checks."
🤖 Prompt for 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.
In `@relay/channel/codex/input_file_test.go` around lines 1 - 59, Update
TestRewriteRemoteInputFiles and
TestRewriteRemoteInputFilesPreservesInlineAndPropagatesLoadErrors to use
testify/require for setup and fatal assertions instead of t.Fatalf, including
unmarshalling and error checks. Add the required testify/require import while
preserving the existing test coverage and non-fatal assertion behavior.
Source: Coding guidelines
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/url" | ||
| "path" | ||
| "strings" | ||
| ) |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Use common/json.go wrappers instead of encoding/json directly.
rewriteRemoteInputFiles calls json.Unmarshal (line 19) and json.Marshal (line 30) directly. The repo's adaptor.go already uses common.Marshal/common.Unmarshal for this exact kind of business-logic serialization; this new file should follow the same convention.
♻️ Proposed fix
import (
- "encoding/json"
"fmt"
"net/url"
"path"
"strings"
+
+ "github.com/QuantumNous/new-api/common"
)
@@
- var value any
- if err := json.Unmarshal(input, &value); err != nil {
+ var value any
+ if err := common.Unmarshal(input, &value); err != nil {
return nil, fmt.Errorf("decode responses input: %w", err)
}
@@
- rewritten, err := json.Marshal(value)
+ rewritten, err := common.Marshal(value)
if err != nil {
return nil, fmt.Errorf("encode responses input: %w", err)
}As per coding guidelines, "All JSON marshal and unmarshal operations in business code must use the wrappers in common/json.go; do not directly call encoding/json operations."
Also applies to: 13-35
🤖 Prompt for 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.
In `@relay/channel/codex/input_file.go` around lines 3 - 9, Update
rewriteRemoteInputFiles to use the repository’s common.Marshal and
common.Unmarshal wrappers for both serialization operations, matching the
convention in adaptor.go. Remove the direct encoding/json import and add the
appropriate common package reference while preserving the existing input-rewrite
behavior.
Source: Coding guidelines
📝 变更描述 / Description
Codex Responses can reject remote
input_file.file_urlvalues when the URL requires signed access or cannot be fetched by the upstream. The Codex adaptor now loads remote input files through the existing file-source cache and sends them asfile_datadata URIs. Inline files and image inputs are unchanged.🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
📸 运行证明 / Proof of Work
go test ./relay/channel/codexpasses.file_datacompletes successfully through the Codex channel.Summary by CodeRabbit
New Features
Bug Fixes
Tests