"backend/vscode:/vscode.git/clone" did not exist on "a8c3dfb0c17d6b490a0043b08b60ef26fd34c408"
Commit ebd5253e authored by shaw's avatar shaw
Browse files

fix: /response端点移除强制注入大量instructions内容

parent 6411645f
package service package service
import ( import (
_ "embed"
"strings" "strings"
) )
//go:embed prompts/codex_cli_instructions.md
var codexCLIInstructions string
var codexModelMap = map[string]string{ var codexModelMap = map[string]string{
"gpt-5.4": "gpt-5.4", "gpt-5.4": "gpt-5.4",
"gpt-5.4-none": "gpt-5.4", "gpt-5.4-none": "gpt-5.4",
...@@ -230,72 +226,13 @@ func getNormalizedCodexModel(modelID string) string { ...@@ -230,72 +226,13 @@ func getNormalizedCodexModel(modelID string) string {
return "" return ""
} }
func getOpenCodeCodexHeader() string { // applyInstructions 处理 instructions 字段:仅在 instructions 为空时填充默认值。
// 兼容保留:历史上这里会从 opencode 仓库拉取 codex_header.txt。
// 现在我们与 Codex CLI 一致,直接使用仓库内置的 instructions,避免读写缓存与外网依赖。
return getCodexCLIInstructions()
}
func getCodexCLIInstructions() string {
return codexCLIInstructions
}
func GetOpenCodeInstructions() string {
return getOpenCodeCodexHeader()
}
// GetCodexCLIInstructions 返回内置的 Codex CLI 指令内容。
func GetCodexCLIInstructions() string {
return getCodexCLIInstructions()
}
// applyInstructions 处理 instructions 字段
// isCodexCLI=true: 仅补充缺失的 instructions(使用内置 Codex CLI 指令)
// isCodexCLI=false: 优先使用内置 Codex CLI 指令覆盖
func applyInstructions(reqBody map[string]any, isCodexCLI bool) bool { func applyInstructions(reqBody map[string]any, isCodexCLI bool) bool {
if isCodexCLI {
return applyCodexCLIInstructions(reqBody)
}
return applyOpenCodeInstructions(reqBody)
}
// applyCodexCLIInstructions 为 Codex CLI 请求补充缺失的 instructions
// 仅在 instructions 为空时添加内置 Codex CLI 指令(不依赖 opencode 缓存/回源)
func applyCodexCLIInstructions(reqBody map[string]any) bool {
if !isInstructionsEmpty(reqBody) { if !isInstructionsEmpty(reqBody) {
return false // 已有有效 instructions,不修改
}
instructions := strings.TrimSpace(getCodexCLIInstructions())
if instructions != "" {
reqBody["instructions"] = instructions
return true
}
return false return false
}
// applyOpenCodeInstructions 为非 Codex CLI 请求应用内置 Codex CLI 指令(兼容历史函数名)
// 优先使用内置 Codex CLI 指令覆盖
func applyOpenCodeInstructions(reqBody map[string]any) bool {
instructions := strings.TrimSpace(getOpenCodeCodexHeader())
existingInstructions, _ := reqBody["instructions"].(string)
existingInstructions = strings.TrimSpace(existingInstructions)
if instructions != "" {
if existingInstructions != instructions {
reqBody["instructions"] = instructions
return true
} }
} else if existingInstructions == "" { reqBody["instructions"] = "You are a helpful coding assistant."
codexInstructions := strings.TrimSpace(getCodexCLIInstructions())
if codexInstructions != "" {
reqBody["instructions"] = codexInstructions
return true return true
}
}
return false
} }
// isInstructionsEmpty 检查 instructions 字段是否为空 // isInstructionsEmpty 检查 instructions 字段是否为空
......
...@@ -234,20 +234,19 @@ func TestApplyCodexOAuthTransform_CodexCLI_SuppliesDefaultWhenEmpty(t *testing.T ...@@ -234,20 +234,19 @@ func TestApplyCodexOAuthTransform_CodexCLI_SuppliesDefaultWhenEmpty(t *testing.T
require.True(t, result.Modified) require.True(t, result.Modified)
} }
func TestApplyCodexOAuthTransform_NonCodexCLI_OverridesInstructions(t *testing.T) { func TestApplyCodexOAuthTransform_NonCodexCLI_PreservesExistingInstructions(t *testing.T) {
// 非 Codex CLI 场景:使用内置 Codex CLI 指令覆盖 // 非 Codex CLI 场景:已有 instructions 时保留客户端的值,不再覆盖
reqBody := map[string]any{ reqBody := map[string]any{
"model": "gpt-5.1", "model": "gpt-5.1",
"instructions": "old instructions", "instructions": "old instructions",
} }
result := applyCodexOAuthTransform(reqBody, false, false) // isCodexCLI=false applyCodexOAuthTransform(reqBody, false, false) // isCodexCLI=false
instructions, ok := reqBody["instructions"].(string) instructions, ok := reqBody["instructions"].(string)
require.True(t, ok) require.True(t, ok)
require.NotEqual(t, "old instructions", instructions) require.Equal(t, "old instructions", instructions)
require.True(t, result.Modified)
} }
func TestIsInstructionsEmpty(t *testing.T) { func TestIsInstructionsEmpty(t *testing.T) {
......
...@@ -1597,13 +1597,11 @@ func (s *OpenAIGatewayService) Forward(ctx context.Context, c *gin.Context, acco ...@@ -1597,13 +1597,11 @@ func (s *OpenAIGatewayService) Forward(ctx context.Context, c *gin.Context, acco
patchDisabled = true patchDisabled = true
} }
// 非透传模式下,保持历史行为:非 Codex CLI 请求在 instructions 为空时注入默认指令。 // 非透传模式下,instructions 为空时注入默认指令。
if !isCodexCLI && isInstructionsEmpty(reqBody) { if isInstructionsEmpty(reqBody) {
if instructions := strings.TrimSpace(GetOpenCodeInstructions()); instructions != "" { reqBody["instructions"] = "You are a helpful coding assistant."
reqBody["instructions"] = instructions
bodyModified = true bodyModified = true
markPatchSet("instructions", instructions) markPatchSet("instructions", "You are a helpful coding assistant.")
}
} }
// 对所有请求执行模型映射(包含 Codex CLI)。 // 对所有请求执行模型映射(包含 Codex CLI)。
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment