feat(codex): enable instruction toggling and update role terminology

- Added conditional logic for Codex instruction injection based on configuration.
- Updated role terminology from "user" to "developer" for better alignment with context.
This commit is contained in:
Luis Pater
2026-01-17 04:12:29 +08:00
parent 6600d58ba2
commit 65b4e1ec6c
3 changed files with 21 additions and 17 deletions

View File

@@ -52,7 +52,7 @@ func ConvertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
systemsResult := rootResult.Get("system") systemsResult := rootResult.Get("system")
if systemsResult.IsArray() { if systemsResult.IsArray() {
systemResults := systemsResult.Array() systemResults := systemsResult.Array()
message := `{"type":"message","role":"user","content":[]}` message := `{"type":"message","role":"developer","content":[]}`
for i := 0; i < len(systemResults); i++ { for i := 0; i < len(systemResults); i++ {
systemResult := systemResults[i] systemResult := systemResults[i]
systemTypeResult := systemResult.Get("type") systemTypeResult := systemResult.Get("type")
@@ -245,21 +245,23 @@ func ConvertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
template, _ = sjson.Set(template, "include", []string{"reasoning.encrypted_content"}) template, _ = sjson.Set(template, "include", []string{"reasoning.encrypted_content"})
// Add a first message to ignore system instructions and ensure proper execution. // Add a first message to ignore system instructions and ensure proper execution.
inputResult := gjson.Get(template, "input") if misc.GetCodexInstructionsEnabled() {
if inputResult.Exists() && inputResult.IsArray() { inputResult := gjson.Get(template, "input")
inputResults := inputResult.Array() if inputResult.Exists() && inputResult.IsArray() {
newInput := "[]" inputResults := inputResult.Array()
for i := 0; i < len(inputResults); i++ { newInput := "[]"
if i == 0 { for i := 0; i < len(inputResults); i++ {
firstText := inputResults[i].Get("content.0.text") if i == 0 {
firstInstructions := "EXECUTE ACCORDING TO THE FOLLOWING INSTRUCTIONS!!!" firstText := inputResults[i].Get("content.0.text")
if firstText.Exists() && firstText.String() != firstInstructions { firstInstructions := "EXECUTE ACCORDING TO THE FOLLOWING INSTRUCTIONS!!!"
newInput, _ = sjson.SetRaw(newInput, "-1", `{"type":"message","role":"user","content":[{"type":"input_text","text":"EXECUTE ACCORDING TO THE FOLLOWING INSTRUCTIONS!!!"}]}`) if firstText.Exists() && firstText.String() != firstInstructions {
newInput, _ = sjson.SetRaw(newInput, "-1", `{"type":"message","role":"user","content":[{"type":"input_text","text":"EXECUTE ACCORDING TO THE FOLLOWING INSTRUCTIONS!!!"}]}`)
}
} }
newInput, _ = sjson.SetRaw(newInput, "-1", inputResults[i].Raw)
} }
newInput, _ = sjson.SetRaw(newInput, "-1", inputResults[i].Raw) template, _ = sjson.SetRaw(template, "input", newInput)
} }
template, _ = sjson.SetRaw(template, "input", newInput)
} }
return []byte(template) return []byte(template)

View File

@@ -95,7 +95,7 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
// System instruction -> as a user message with input_text parts // System instruction -> as a user message with input_text parts
sysParts := root.Get("system_instruction.parts") sysParts := root.Get("system_instruction.parts")
if sysParts.IsArray() { if sysParts.IsArray() {
msg := `{"type":"message","role":"user","content":[]}` msg := `{"type":"message","role":"developer","content":[]}`
arr := sysParts.Array() arr := sysParts.Array()
for i := 0; i < len(arr); i++ { for i := 0; i < len(arr); i++ {
p := arr[i] p := arr[i]

View File

@@ -33,7 +33,7 @@ func ConvertOpenAIRequestToCodex(modelName string, inputRawJSON []byte, stream b
rawJSON := bytes.Clone(inputRawJSON) rawJSON := bytes.Clone(inputRawJSON)
userAgent := misc.ExtractCodexUserAgent(rawJSON) userAgent := misc.ExtractCodexUserAgent(rawJSON)
// Start with empty JSON object // Start with empty JSON object
out := `{}` out := `{"instructions":""}`
// Stream must be set to true // Stream must be set to true
out, _ = sjson.Set(out, "stream", stream) out, _ = sjson.Set(out, "stream", stream)
@@ -98,7 +98,9 @@ func ConvertOpenAIRequestToCodex(modelName string, inputRawJSON []byte, stream b
// Extract system instructions from first system message (string or text object) // Extract system instructions from first system message (string or text object)
messages := gjson.GetBytes(rawJSON, "messages") messages := gjson.GetBytes(rawJSON, "messages")
_, instructions := misc.CodexInstructionsForModel(modelName, "", userAgent) _, instructions := misc.CodexInstructionsForModel(modelName, "", userAgent)
out, _ = sjson.Set(out, "instructions", instructions) if misc.GetCodexInstructionsEnabled() {
out, _ = sjson.Set(out, "instructions", instructions)
}
// if messages.IsArray() { // if messages.IsArray() {
// arr := messages.Array() // arr := messages.Array()
// for i := 0; i < len(arr); i++ { // for i := 0; i < len(arr); i++ {
@@ -141,7 +143,7 @@ func ConvertOpenAIRequestToCodex(modelName string, inputRawJSON []byte, stream b
msg := `{}` msg := `{}`
msg, _ = sjson.Set(msg, "type", "message") msg, _ = sjson.Set(msg, "type", "message")
if role == "system" { if role == "system" {
msg, _ = sjson.Set(msg, "role", "user") msg, _ = sjson.Set(msg, "role", "developer")
} else { } else {
msg, _ = sjson.Set(msg, "role", role) msg, _ = sjson.Set(msg, "role", role)
} }