diff --git a/internal/misc/codex_instructions.go b/internal/misc/codex_instructions.go index 04876ea4..9d0971c5 100644 --- a/internal/misc/codex_instructions.go +++ b/internal/misc/codex_instructions.go @@ -73,6 +73,10 @@ func useOpenCodeInstructions(userAgent string) bool { return strings.Contains(strings.ToLower(userAgent), userAgentOpenAISDK) } +func IsOpenCodeUserAgent(userAgent string) bool { + return useOpenCodeInstructions(userAgent) +} + func codexInstructionsForCodex(modelName, systemInstructions string) (bool, string) { entries, _ := codexInstructionsDir.ReadDir("codex_instructions") @@ -120,7 +124,7 @@ func codexInstructionsForCodex(modelName, systemInstructions string) (bool, stri } func CodexInstructionsForModel(modelName, systemInstructions, userAgent string) (bool, string) { - if useOpenCodeInstructions(userAgent) { + if IsOpenCodeUserAgent(userAgent) { return codexInstructionsForOpenCode(systemInstructions) } return codexInstructionsForCodex(modelName, systemInstructions) diff --git a/internal/runtime/executor/codex_executor.go b/internal/runtime/executor/codex_executor.go index a36b8eea..e8dc3f8b 100644 --- a/internal/runtime/executor/codex_executor.go +++ b/internal/runtime/executor/codex_executor.go @@ -172,7 +172,7 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re } var param any - out := sdktranslator.TranslateNonStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, line, ¶m) + out := sdktranslator.TranslateNonStream(ctx, to, from, req.Model, bytes.Clone(originalPayload), body, line, ¶m) resp = cliproxyexecutor.Response{Payload: []byte(out)} return resp, nil } @@ -286,7 +286,7 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au } } - chunks := sdktranslator.TranslateStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, bytes.Clone(line), ¶m) + chunks := sdktranslator.TranslateStream(ctx, to, from, req.Model, bytes.Clone(originalPayload), body, bytes.Clone(line), ¶m) for i := range chunks { out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunks[i])} } diff --git a/internal/translator/codex/openai/responses/codex_openai-responses_response.go b/internal/translator/codex/openai/responses/codex_openai-responses_response.go index 02054afb..95d771d5 100644 --- a/internal/translator/codex/openai/responses/codex_openai-responses_response.go +++ b/internal/translator/codex/openai/responses/codex_openai-responses_response.go @@ -5,6 +5,7 @@ import ( "context" "fmt" + "github.com/router-for-me/CLIProxyAPI/v6/internal/misc" "github.com/tidwall/gjson" "github.com/tidwall/sjson" ) @@ -18,7 +19,8 @@ func ConvertCodexResponseToOpenAIResponses(ctx context.Context, modelName string if typeResult := gjson.GetBytes(rawJSON, "type"); typeResult.Exists() { typeStr := typeResult.String() if typeStr == "response.created" || typeStr == "response.in_progress" || typeStr == "response.completed" { - rawJSON, _ = sjson.SetBytes(rawJSON, "response.instructions", gjson.GetBytes(requestRawJSON, "instructions").String()) + instructions := selectInstructions(originalRequestRawJSON, requestRawJSON) + rawJSON, _ = sjson.SetBytes(rawJSON, "response.instructions", instructions) } } out := fmt.Sprintf("data: %s", string(rawJSON)) @@ -37,6 +39,14 @@ func ConvertCodexResponseToOpenAIResponsesNonStream(_ context.Context, modelName } responseResult := rootResult.Get("response") template := responseResult.Raw - template, _ = sjson.Set(template, "instructions", gjson.GetBytes(requestRawJSON, "instructions").String()) + template, _ = sjson.Set(template, "instructions", selectInstructions(originalRequestRawJSON, requestRawJSON)) return template } + +func selectInstructions(originalRequestRawJSON, requestRawJSON []byte) string { + userAgent := misc.ExtractCodexUserAgent(originalRequestRawJSON) + if misc.IsOpenCodeUserAgent(userAgent) { + return gjson.GetBytes(requestRawJSON, "instructions").String() + } + return gjson.GetBytes(originalRequestRawJSON, "instructions").String() +}