feat: implement model-specific Codex instructions for GPT-5

- Added `CodexInstructions(modelName string)` function to dynamically select instructions based on the model (e.g., GPT-5 Codex).
- Introduced `gpt_5_instructions.txt` and `gpt_5_codex_instructions.txt` for respective model configurations.
- Updated translators to pass `modelName` and use the new instruction logic.
This commit is contained in:
Luis Pater
2025-09-19 08:47:54 +08:00
parent df66046b14
commit 869a3dfbb4
8 changed files with 21 additions and 10 deletions

View File

@@ -9,5 +9,15 @@ import _ "embed"
// which is embedded into the application binary at compile time. This variable // which is embedded into the application binary at compile time. This variable
// contains instructional text used for Codex-related operations and model guidance. // contains instructional text used for Codex-related operations and model guidance.
// //
//go:embed codex_instructions.txt //go:embed gpt_5_instructions.txt
var CodexInstructions string var GPT5Instructions string
//go:embed gpt_5_codex_instructions.txt
var GPT5CodexInstructions string
func CodexInstructions(modelName string) string {
if modelName == "gpt-5-codex" {
return GPT5CodexInstructions
}
return GPT5Instructions
}

File diff suppressed because one or more lines are too long

View File

@@ -39,7 +39,7 @@ func ConvertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
template := `{"model":"","instructions":"","input":[]}` template := `{"model":"","instructions":"","input":[]}`
instructions := misc.CodexInstructions instructions := misc.CodexInstructions(modelName)
template, _ = sjson.SetRaw(template, "instructions", instructions) template, _ = sjson.SetRaw(template, "instructions", instructions)
rootResult := gjson.ParseBytes(rawJSON) rootResult := gjson.ParseBytes(rawJSON)

View File

@@ -42,7 +42,7 @@ func ConvertGeminiRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
out := `{"model":"","instructions":"","input":[]}` out := `{"model":"","instructions":"","input":[]}`
// Inject standard Codex instructions // Inject standard Codex instructions
instructions := misc.CodexInstructions instructions := misc.CodexInstructions(modelName)
out, _ = sjson.SetRaw(out, "instructions", instructions) out, _ = sjson.SetRaw(out, "instructions", instructions)
root := gjson.ParseBytes(rawJSON) root := gjson.ParseBytes(rawJSON)

View File

@@ -97,7 +97,7 @@ 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.CodexInstructions instructions := misc.CodexInstructions(modelName)
out, _ = sjson.SetRaw(out, "instructions", instructions) out, _ = sjson.SetRaw(out, "instructions", instructions)
// if messages.IsArray() { // if messages.IsArray() {
// arr := messages.Array() // arr := messages.Array()

View File

@@ -8,7 +8,7 @@ import (
"github.com/tidwall/sjson" "github.com/tidwall/sjson"
) )
func ConvertOpenAIResponsesRequestToCodex(_ string, inputRawJSON []byte, _ bool) []byte { func ConvertOpenAIResponsesRequestToCodex(modelName string, inputRawJSON []byte, _ bool) []byte {
rawJSON := bytes.Clone(inputRawJSON) rawJSON := bytes.Clone(inputRawJSON)
rawJSON, _ = sjson.SetBytes(rawJSON, "stream", true) rawJSON, _ = sjson.SetBytes(rawJSON, "stream", true)
@@ -16,7 +16,7 @@ func ConvertOpenAIResponsesRequestToCodex(_ string, inputRawJSON []byte, _ bool)
rawJSON, _ = sjson.SetBytes(rawJSON, "parallel_tool_calls", true) rawJSON, _ = sjson.SetBytes(rawJSON, "parallel_tool_calls", true)
rawJSON, _ = sjson.SetBytes(rawJSON, "include", []string{"reasoning.encrypted_content"}) rawJSON, _ = sjson.SetBytes(rawJSON, "include", []string{"reasoning.encrypted_content"})
instructions := misc.CodexInstructions instructions := misc.CodexInstructions(modelName)
originalInstructions := "" originalInstructions := ""
originalInstructionsResult := gjson.GetBytes(rawJSON, "instructions") originalInstructionsResult := gjson.GetBytes(rawJSON, "instructions")

View File

@@ -19,7 +19,7 @@ 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" {
instructions := misc.CodexInstructions instructions := misc.CodexInstructions(modelName)
instructionsResult := gjson.GetBytes(rawJSON, "response.instructions") instructionsResult := gjson.GetBytes(rawJSON, "response.instructions")
if instructionsResult.Raw == instructions { if instructionsResult.Raw == instructions {
rawJSON, _ = sjson.SetBytes(rawJSON, "response.instructions", gjson.GetBytes(originalRequestRawJSON, "instructions").String()) rawJSON, _ = sjson.SetBytes(rawJSON, "response.instructions", gjson.GetBytes(originalRequestRawJSON, "instructions").String())
@@ -33,7 +33,7 @@ func ConvertCodexResponseToOpenAIResponses(ctx context.Context, modelName string
// ConvertCodexResponseToOpenAIResponsesNonStream builds a single Responses JSON // ConvertCodexResponseToOpenAIResponsesNonStream builds a single Responses JSON
// from a non-streaming OpenAI Chat Completions response. // from a non-streaming OpenAI Chat Completions response.
func ConvertCodexResponseToOpenAIResponsesNonStream(_ context.Context, _ string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, _ *any) string { func ConvertCodexResponseToOpenAIResponsesNonStream(_ context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, _ *any) string {
scanner := bufio.NewScanner(bytes.NewReader(rawJSON)) scanner := bufio.NewScanner(bytes.NewReader(rawJSON))
buffer := make([]byte, 10240*1024) buffer := make([]byte, 10240*1024)
scanner.Buffer(buffer, 10240*1024) scanner.Buffer(buffer, 10240*1024)
@@ -54,7 +54,7 @@ func ConvertCodexResponseToOpenAIResponsesNonStream(_ context.Context, _ string,
responseResult := rootResult.Get("response") responseResult := rootResult.Get("response")
template := responseResult.Raw template := responseResult.Raw
instructions := misc.CodexInstructions instructions := misc.CodexInstructions(modelName)
instructionsResult := gjson.Get(template, "instructions") instructionsResult := gjson.Get(template, "instructions")
if instructionsResult.Raw == instructions { if instructionsResult.Raw == instructions {
template, _ = sjson.Set(template, "instructions", gjson.GetBytes(originalRequestRawJSON, "instructions").String()) template, _ = sjson.Set(template, "instructions", gjson.GetBytes(originalRequestRawJSON, "instructions").String())