fix(codex): only override instructions in responses for OpenCode UA

This commit is contained in:
hkfires
2026-01-11 15:19:37 +08:00
parent ac626111ac
commit 70a82d80ac
3 changed files with 19 additions and 5 deletions

View File

@@ -73,6 +73,10 @@ func useOpenCodeInstructions(userAgent string) bool {
return strings.Contains(strings.ToLower(userAgent), userAgentOpenAISDK) return strings.Contains(strings.ToLower(userAgent), userAgentOpenAISDK)
} }
func IsOpenCodeUserAgent(userAgent string) bool {
return useOpenCodeInstructions(userAgent)
}
func codexInstructionsForCodex(modelName, systemInstructions string) (bool, string) { func codexInstructionsForCodex(modelName, systemInstructions string) (bool, string) {
entries, _ := codexInstructionsDir.ReadDir("codex_instructions") entries, _ := codexInstructionsDir.ReadDir("codex_instructions")
@@ -120,7 +124,7 @@ func codexInstructionsForCodex(modelName, systemInstructions string) (bool, stri
} }
func CodexInstructionsForModel(modelName, systemInstructions, userAgent string) (bool, string) { func CodexInstructionsForModel(modelName, systemInstructions, userAgent string) (bool, string) {
if useOpenCodeInstructions(userAgent) { if IsOpenCodeUserAgent(userAgent) {
return codexInstructionsForOpenCode(systemInstructions) return codexInstructionsForOpenCode(systemInstructions)
} }
return codexInstructionsForCodex(modelName, systemInstructions) return codexInstructionsForCodex(modelName, systemInstructions)

View File

@@ -172,7 +172,7 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re
} }
var param any var param any
out := sdktranslator.TranslateNonStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, line, &param) out := sdktranslator.TranslateNonStream(ctx, to, from, req.Model, bytes.Clone(originalPayload), body, line, &param)
resp = cliproxyexecutor.Response{Payload: []byte(out)} resp = cliproxyexecutor.Response{Payload: []byte(out)}
return resp, nil 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), &param) chunks := sdktranslator.TranslateStream(ctx, to, from, req.Model, bytes.Clone(originalPayload), body, bytes.Clone(line), &param)
for i := range chunks { for i := range chunks {
out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunks[i])} out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunks[i])}
} }

View File

@@ -5,6 +5,7 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/router-for-me/CLIProxyAPI/v6/internal/misc"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"github.com/tidwall/sjson" "github.com/tidwall/sjson"
) )
@@ -18,7 +19,8 @@ func ConvertCodexResponseToOpenAIResponses(ctx context.Context, modelName string
if typeResult := gjson.GetBytes(rawJSON, "type"); typeResult.Exists() { if typeResult := gjson.GetBytes(rawJSON, "type"); typeResult.Exists() {
typeStr := typeResult.String() typeStr := typeResult.String()
if typeStr == "response.created" || typeStr == "response.in_progress" || typeStr == "response.completed" { 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)) out := fmt.Sprintf("data: %s", string(rawJSON))
@@ -37,6 +39,14 @@ func ConvertCodexResponseToOpenAIResponsesNonStream(_ context.Context, modelName
} }
responseResult := rootResult.Get("response") responseResult := rootResult.Get("response")
template := responseResult.Raw template := responseResult.Raw
template, _ = sjson.Set(template, "instructions", gjson.GetBytes(requestRawJSON, "instructions").String()) template, _ = sjson.Set(template, "instructions", selectInstructions(originalRequestRawJSON, requestRawJSON))
return template 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()
}