mirror of
https://github.com/chenyme/grok2api.git
synced 2026-12-05 16:02:12 +00:00
Merge pull request #673 from Hhy812/fix/codex-modelinput-history-compat
修复 Codex 多轮历史导致 Grok Build ModelInput 422
This commit is contained in:
6 files changed
+713
-54
No files matched your search
@@ -90,7 +90,7 @@ func TestForwardResponseMatchesGrokBuildHeadersAndPreservesReasoning(t *testing.
|
||||
}
|
||||
_ = response.Body.Close()
|
||||
input := captured["input"].([]any)
|
||||
if captured["model"] != "grok-4.5" || captured["prompt_cache_key"] != "isolated-key" || len(input) != 1 || input[0].(map[string]any)["encrypted_content"] != "cipher" {
|
||||
if captured["model"] != "grok-4.5" || captured["prompt_cache_key"] != "isolated-key" || len(input) != 1 || input[0].(map[string]any)["type"] != "reasoning" || input[0].(map[string]any)["encrypted_content"] != "cipher" {
|
||||
t.Fatalf("captured = %#v", captured)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func TestNormalizeResponsesRequest(t *testing.T) {
|
||||
t.Fatalf("模型或缓存键未正确改写: %#v", payload)
|
||||
}
|
||||
input := payload["input"].([]any)
|
||||
if len(input) != 2 || input[0].(map[string]any)["encrypted_content"] != "cipher" {
|
||||
if len(input) != 2 || input[0].(map[string]any)["type"] != "reasoning" || input[0].(map[string]any)["encrypted_content"] != "cipher" {
|
||||
t.Fatalf("reasoning 回放项未保留: %#v", input)
|
||||
}
|
||||
reasoningContent := input[0].(map[string]any)["content"].([]any)[0].(map[string]any)
|
||||
|
||||
@@ -84,17 +84,11 @@ func (c *responsesToolCompatibility) normalizeApplyPatchCallInput(item map[strin
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
converted := map[string]any{
|
||||
return map[string]any{
|
||||
"type": "function_call", "call_id": callID,
|
||||
"name": c.alias(responsesToolIdentity{Kind: responsesApplyPatchTool, Name: "apply_patch"}),
|
||||
"arguments": string(arguments),
|
||||
}
|
||||
for _, key := range []string{"id", "status"} {
|
||||
if value, exists := item[key]; exists {
|
||||
converted[key] = cloneJSONValue(value)
|
||||
}
|
||||
}
|
||||
return converted, nil
|
||||
}, nil
|
||||
}
|
||||
|
||||
func normalizeApplyPatchOutputInput(item map[string]any, param string) (map[string]any, error) {
|
||||
@@ -276,12 +270,127 @@ func normalizeLegacyLocalShellOutputInput(item map[string]any, param string) (ma
|
||||
return nil, &responsesRequestError{Message: "local_shell_call_output.output 必须是字符串或数组", Param: param + ".output", Code: "invalid_parameter"}
|
||||
}
|
||||
converted := map[string]any{"type": "shell_call_output", "call_id": callID, "output": output}
|
||||
if value, exists := item["max_output_length"]; exists {
|
||||
if value, exists := item["max_output_length"]; exists && value != nil {
|
||||
converted["max_output_length"] = cloneJSONValue(value)
|
||||
}
|
||||
return converted, nil
|
||||
}
|
||||
|
||||
func normalizeShellCallOutputInput(item map[string]any, param string) (map[string]any, error) {
|
||||
callID := strings.TrimSpace(stringField(item, "call_id"))
|
||||
if callID == "" {
|
||||
return nil, &responsesRequestError{Message: param + ".call_id 不能为空", Param: param + ".call_id", Code: "invalid_parameter"}
|
||||
}
|
||||
output, err := normalizeShellOutputBlocks(item["output"], item["status"], param+".output")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
converted := map[string]any{"type": "shell_call_output", "call_id": callID, "output": output}
|
||||
if value, exists := item["max_output_length"]; exists && value != nil {
|
||||
converted["max_output_length"] = cloneJSONValue(value)
|
||||
}
|
||||
return converted, nil
|
||||
}
|
||||
|
||||
func normalizeFunctionCallOutputInput(item map[string]any, param string) (map[string]any, error) {
|
||||
callID := strings.TrimSpace(stringField(item, "call_id"))
|
||||
if callID == "" {
|
||||
return nil, &responsesRequestError{Message: param + ".call_id 不能为空", Param: param + ".call_id", Code: "invalid_parameter"}
|
||||
}
|
||||
output, err := encodeToolOutput(item["output"], param+".output")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 按官方 Build 回放结构重建,不携带输出态 id/status。
|
||||
return map[string]any{"type": "function_call_output", "call_id": callID, "output": output}, nil
|
||||
}
|
||||
|
||||
func encodeToolOutput(value any, param string) (string, error) {
|
||||
switch typed := value.(type) {
|
||||
case nil:
|
||||
return "", nil
|
||||
case string:
|
||||
return typed, nil
|
||||
default:
|
||||
encoded, err := json.Marshal(typed)
|
||||
if err != nil {
|
||||
return "", &responsesRequestError{Message: param + " 无法编码", Param: param, Code: "invalid_parameter"}
|
||||
}
|
||||
return string(encoded), nil
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeShellOutputBlocks(value, status any, param string) ([]any, error) {
|
||||
switch typed := value.(type) {
|
||||
case []any:
|
||||
output := make([]any, 0, len(typed))
|
||||
for index, raw := range typed {
|
||||
block, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
return nil, &responsesRequestError{Message: fmt.Sprintf("%s[%d] 必须是对象", param, index), Param: fmt.Sprintf("%s[%d]", param, index), Code: "invalid_parameter"}
|
||||
}
|
||||
normalized, err := normalizeShellOutputBlock(block, fmt.Sprintf("%s[%d]", param, index))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
output = append(output, normalized)
|
||||
}
|
||||
return output, nil
|
||||
case string:
|
||||
exitCode := 0
|
||||
if strings.EqualFold(fmt.Sprint(status), "failed") {
|
||||
exitCode = 1
|
||||
}
|
||||
return []any{map[string]any{
|
||||
"stdout": typed, "stderr": "",
|
||||
"outcome": map[string]any{"type": "exit", "exit_code": exitCode},
|
||||
}}, nil
|
||||
default:
|
||||
return nil, &responsesRequestError{Message: param + " 必须是字符串或数组", Param: param, Code: "invalid_parameter"}
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeShellOutputBlock(block map[string]any, param string) (map[string]any, error) {
|
||||
stdout, _ := block["stdout"].(string)
|
||||
stderr, _ := block["stderr"].(string)
|
||||
outcome, ok := block["outcome"].(map[string]any)
|
||||
if !ok {
|
||||
return nil, &responsesRequestError{Message: param + ".outcome 必须是对象", Param: param + ".outcome", Code: "invalid_parameter"}
|
||||
}
|
||||
normalizedOutcome := map[string]any{"type": strings.TrimSpace(stringField(outcome, "type"))}
|
||||
switch normalizedOutcome["type"] {
|
||||
case "exit":
|
||||
exitCode, ok := shellExitCode(outcome["exit_code"])
|
||||
if !ok {
|
||||
exitCode, ok = shellExitCode(outcome["exitCode"])
|
||||
}
|
||||
if !ok {
|
||||
return nil, &responsesRequestError{Message: param + ".outcome.exit_code 必须是数字", Param: param + ".outcome.exit_code", Code: "invalid_parameter"}
|
||||
}
|
||||
normalizedOutcome["exit_code"] = exitCode
|
||||
case "timeout":
|
||||
default:
|
||||
return nil, &responsesRequestError{Message: param + ".outcome.type 无效", Param: param + ".outcome.type", Code: "invalid_parameter"}
|
||||
}
|
||||
return map[string]any{"stdout": stdout, "stderr": stderr, "outcome": normalizedOutcome}, nil
|
||||
}
|
||||
|
||||
func shellExitCode(value any) (int, bool) {
|
||||
switch typed := value.(type) {
|
||||
case float64:
|
||||
return int(typed), true
|
||||
case int:
|
||||
return typed, true
|
||||
case int64:
|
||||
return int(typed), true
|
||||
case json.Number:
|
||||
parsed, err := typed.Int64()
|
||||
return int(parsed), err == nil
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
func rewriteLegacyShellAction(value any) map[string]any {
|
||||
action, _ := value.(map[string]any)
|
||||
commands, _ := action["commands"].([]any)
|
||||
|
||||
@@ -10,13 +10,13 @@ import (
|
||||
func TestLegacyLocalShellUsesNativeLocalShellAndRestoresJSON(t *testing.T) {
|
||||
normalized, compatibility, err := normalizeResponsesRequest([]byte(`{
|
||||
"model":"public","input":"show cwd",
|
||||
"tools":[{"type":"local_shell","timeout_ms":30000}],
|
||||
"tools":[{"type":"local_shell"}],
|
||||
"tool_choice":{"type":"local_shell"}
|
||||
}`), "grok-4.5")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if compatibility == nil || !compatibility.legacyLocalShell || !strings.Contains(compatibility.warningHeader(), "legacy_local_shell_controls_ignored") {
|
||||
if compatibility == nil || !compatibility.legacyLocalShell {
|
||||
t.Fatal("legacy local_shell 未启用兼容层")
|
||||
}
|
||||
var request map[string]any
|
||||
@@ -78,6 +78,257 @@ func TestLegacyLocalShellHistoryBecomesStructuredShellHistory(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeShellOutputHistoryIsSanitizedForBuild(t *testing.T) {
|
||||
normalized, _, err := normalizeResponsesRequest([]byte(`{
|
||||
"model":"public","tools":[{"type":"shell","environment":{"type":"local"}}],"input":[
|
||||
{"type":"shell_call_output","call_id":"call_1","status":"completed","output":[
|
||||
{"command":"pwd","stdout":"/workspace\n","stderr":"","outcome":{"type":"exit","exitCode":0}}
|
||||
],"max_output_length":2048}
|
||||
]
|
||||
}`), "grok-4.5")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var request map[string]any
|
||||
if err := json.Unmarshal(normalized, &request); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
item := request["input"].([]any)[0].(map[string]any)
|
||||
block := item["output"].([]any)[0].(map[string]any)
|
||||
outcome := block["outcome"].(map[string]any)
|
||||
if item["type"] != "shell_call_output" || block["command"] != nil || outcome["exit_code"] != float64(0) || outcome["exitCode"] != nil {
|
||||
t.Fatalf("shell output history = %#v", item)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFunctionCallOutputHistoryEncodesStructuredOutput(t *testing.T) {
|
||||
normalized, _, err := normalizeResponsesRequest([]byte(`{
|
||||
"model":"public","tools":[{"type":"function","name":"shell_command","parameters":{"type":"object"}}],"input":[
|
||||
{"type":"function_call_output","call_id":"call_1","status":"completed","output":{"exit_code":0,"stdout":"ok","stderr":""}}
|
||||
]
|
||||
}`), "grok-4.5")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var request map[string]any
|
||||
if err := json.Unmarshal(normalized, &request); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
item := request["input"].([]any)[0].(map[string]any)
|
||||
output, ok := item["output"].(string)
|
||||
if item["type"] != "function_call_output" || !ok || !strings.Contains(output, `"stdout":"ok"`) {
|
||||
t.Fatalf("function output history = %#v", item)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssistantOutputMessageHistoryUsesEasyMessageText(t *testing.T) {
|
||||
normalized, _, err := normalizeResponsesRequest([]byte(`{
|
||||
"model":"public","input":[
|
||||
{"type":"message","role":"system","content":"system instruction"},
|
||||
{"id":"msg_1","type":"message","status":"completed","role":"assistant","content":[
|
||||
{"type":"output_text","text":"Hi there","annotations":[]}
|
||||
]},
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"继续"}]}
|
||||
]
|
||||
}`), "grok-4.5")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var request map[string]any
|
||||
if err := json.Unmarshal(normalized, &request); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := request["input"].([]any)
|
||||
system := items[0].(map[string]any)
|
||||
assistant := items[1].(map[string]any)
|
||||
user := items[2].(map[string]any)
|
||||
userContent := user["content"].([]any)[0].(map[string]any)
|
||||
if system["role"] != "system" || system["content"] != "system instruction" {
|
||||
t.Fatalf("system message history = %#v", system)
|
||||
}
|
||||
if assistant["id"] != nil || assistant["status"] != nil || assistant["content"] != "Hi there" {
|
||||
t.Fatalf("assistant message history = %#v", assistant)
|
||||
}
|
||||
if userContent["type"] != "input_text" {
|
||||
t.Fatalf("user message history = %#v", user)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoleMissingTypeBecomesMessageAndFunctionCallIsAllowlisted(t *testing.T) {
|
||||
normalized, _, err := normalizeResponsesRequest([]byte(`{
|
||||
"model":"public","input":[
|
||||
{"role":"user","content":"hello"},
|
||||
{"type":"function_call","id":"fc_1","status":"completed","call_id":"call_1","name":"shell_command","arguments":"{}","namespace":"","internal_chat_message_metadata_passthrough":{"turn_id":"t1"}},
|
||||
{"type":"function_call_output","id":"fco_1","status":"completed","call_id":"call_1","output":"ok"}
|
||||
]
|
||||
}`), "grok-4.5")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var request map[string]any
|
||||
if err := json.Unmarshal(normalized, &request); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := request["input"].([]any)
|
||||
user := items[0].(map[string]any)
|
||||
call := items[1].(map[string]any)
|
||||
output := items[2].(map[string]any)
|
||||
if user["type"] != "message" || user["content"] != "hello" {
|
||||
t.Fatalf("user history = %#v", user)
|
||||
}
|
||||
if call["type"] != "function_call" || call["id"] != nil || call["status"] != nil || call["internal_chat_message_metadata_passthrough"] != nil {
|
||||
t.Fatalf("function_call history = %#v", call)
|
||||
}
|
||||
if output["type"] != "function_call_output" || output["id"] != nil || output["status"] != nil {
|
||||
t.Fatalf("function_call_output history = %#v", output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeBuildHistoryItemsArePreservedAndSanitized(t *testing.T) {
|
||||
normalized, _, err := normalizeResponsesRequest([]byte(`{
|
||||
"model":"public","input":[
|
||||
{"type":"web_search_call","id":"ws_1","status":"completed","action":{"type":"search","query":"Grok Build","sources":null},"phase":"commentary"},
|
||||
{"type":"shell_call","id":null,"call_id":"call_1","status":"completed","action":{"commands":["pwd"],"timeout_ms":null,"internal_chat_message_metadata_passthrough":{"turn_id":"t1"}}},
|
||||
{"type":"shell_call_output","call_id":"call_1","output":[{"stdout":"/workspace\n","stderr":"","outcome":{"type":"exit","exit_code":0}}]},
|
||||
{"type":"code_interpreter_call","id":"ci_1","container_id":"container_1","status":"completed","code":"print(1)","outputs":null},
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"continue"}]}
|
||||
]
|
||||
}`), "grok-4.5")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var request map[string]any
|
||||
if err := json.Unmarshal(normalized, &request); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := request["input"].([]any)
|
||||
webSearch := items[0].(map[string]any)
|
||||
searchAction := webSearch["action"].(map[string]any)
|
||||
if webSearch["type"] != "web_search_call" || webSearch["phase"] != nil || searchAction["sources"] != nil {
|
||||
t.Fatalf("web search history = %#v", webSearch)
|
||||
}
|
||||
shellCall := items[1].(map[string]any)
|
||||
shellAction := shellCall["action"].(map[string]any)
|
||||
if shellCall["type"] != "shell_call" || shellCall["id"] != nil || shellAction["timeout_ms"] != nil || shellAction["internal_chat_message_metadata_passthrough"] != nil {
|
||||
t.Fatalf("shell call history = %#v", shellCall)
|
||||
}
|
||||
if items[2].(map[string]any)["type"] != "shell_call_output" || items[3].(map[string]any)["type"] != "code_interpreter_call" || items[3].(map[string]any)["outputs"] != nil {
|
||||
t.Fatalf("native tool history = %#v", items[2:4])
|
||||
}
|
||||
if items[4].(map[string]any)["role"] != "user" {
|
||||
t.Fatalf("user message changed unexpectedly: %#v", items[4])
|
||||
}
|
||||
}
|
||||
|
||||
func TestReasoningWithoutEncryptedContentRemainsNative(t *testing.T) {
|
||||
normalized, _, err := normalizeResponsesRequest([]byte(`{
|
||||
"model":"public","input":[
|
||||
{"type":"reasoning","id":"rs_1","status":"completed","summary":[{"type":"summary_text","text":"who am I"}],"content":null,"encrypted_content":null,"internal_chat_message_metadata_passthrough":{"turn_id":"t1"}},
|
||||
{"type":"message","role":"assistant","content":[{"type":"output_text","text":"hi"}]},
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"嗯"}]}
|
||||
]
|
||||
}`), "grok-4.5")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var request map[string]any
|
||||
if err := json.Unmarshal(normalized, &request); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := request["input"].([]any)
|
||||
first := items[0].(map[string]any)
|
||||
if first["type"] != "reasoning" || first["status"] != nil || first["encrypted_content"] != nil || first["content"] != nil {
|
||||
t.Fatalf("reasoning history = %#v", first)
|
||||
}
|
||||
summary := first["summary"].([]any)[0].(map[string]any)
|
||||
if summary["text"] != "who am I" {
|
||||
t.Fatalf("reasoning summary = %#v", first["summary"])
|
||||
}
|
||||
if items[1].(map[string]any)["content"] != "hi" {
|
||||
t.Fatalf("assistant history = %#v", items[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnsupportedResponsesHistoryItemBecomesBoundary(t *testing.T) {
|
||||
normalized, _, err := normalizeResponsesRequest([]byte(`{
|
||||
"model":"public","input":[
|
||||
{"type":"future_codex_item","id":"future_1","status":"completed"},
|
||||
{"type":"message","role":"user","content":"continue"}
|
||||
]
|
||||
}`), "grok-4.5")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var request map[string]any
|
||||
if err := json.Unmarshal(normalized, &request); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := request["input"].([]any)
|
||||
boundary := items[0].(map[string]any)
|
||||
text := boundary["content"].([]any)[0].(map[string]any)["text"].(string)
|
||||
if boundary["role"] != "developer" || !strings.Contains(text, "future_codex_item") || items[1].(map[string]any)["role"] != "user" {
|
||||
t.Fatalf("normalized history = %#v", items)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageImageAndFileNullFieldsAreRemoved(t *testing.T) {
|
||||
normalized, _, err := normalizeResponsesRequest([]byte(`{
|
||||
"model":"public","input":[{"type":"message","role":"user","content":[
|
||||
{"type":"input_image","image_url":"https://example.com/image.png","detail":null,"file_id":null},
|
||||
{"type":"input_file","file_id":"file_1","file_url":null,"filename":null}
|
||||
]}]
|
||||
}`), "grok-4.5")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var request map[string]any
|
||||
if err := json.Unmarshal(normalized, &request); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
content := request["input"].([]any)[0].(map[string]any)["content"].([]any)
|
||||
image := content[0].(map[string]any)
|
||||
file := content[1].(map[string]any)
|
||||
if image["image_url"] == nil || image["detail"] != nil || image["file_id"] != nil || file["file_id"] != "file_1" || file["file_url"] != nil || file["filename"] != nil {
|
||||
t.Fatalf("message content = %#v", content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodexPrivateMetadataIsStrippedFromHistory(t *testing.T) {
|
||||
normalized, _, err := normalizeResponsesRequest([]byte(`{
|
||||
"model":"public","input":[
|
||||
{"type":"reasoning","id":"rs_1","status":"completed","summary":[{"type":"summary_text","text":"plan"}],"encrypted_content":"cipher","internal_chat_message_metadata_passthrough":{"turn_id":"t1"}},
|
||||
{"type":"function_call","id":"fc_1","call_id":"call_1","name":"shell_command","arguments":"{}","internal_chat_message_metadata_passthrough":{"turn_id":"t1"}},
|
||||
{"type":"function_call_output","id":"fco_1","call_id":"call_1","output":"ok","internal_chat_message_metadata_passthrough":{"turn_id":"t1"}},
|
||||
{"type":"custom_tool_call","id":"ctc_1","call_id":"call_2","name":"apply_patch","input":"patch","status":"completed","internal_chat_message_metadata_passthrough":{"turn_id":"t1"}},
|
||||
{"type":"custom_tool_call_output","id":"ctco_1","call_id":"call_2","output":"done","internal_chat_message_metadata_passthrough":{"turn_id":"t1"}},
|
||||
{"type":"message","role":"assistant","phase":"final_answer","content":[{"type":"output_text","text":"hi"}],"internal_chat_message_metadata_passthrough":{"turn_id":"t1"}}
|
||||
]
|
||||
}`), "grok-4.5")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var request map[string]any
|
||||
if err := json.Unmarshal(normalized, &request); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for index, raw := range request["input"].([]any) {
|
||||
item := raw.(map[string]any)
|
||||
if item["internal_chat_message_metadata_passthrough"] != nil || item["phase"] != nil {
|
||||
t.Fatalf("input[%d] still has private fields: %#v", index, item)
|
||||
}
|
||||
}
|
||||
items := request["input"].([]any)
|
||||
if items[0].(map[string]any)["type"] != "reasoning" || items[0].(map[string]any)["encrypted_content"] != "cipher" || items[0].(map[string]any)["status"] != nil {
|
||||
t.Fatalf("reasoning history = %#v", items[0])
|
||||
}
|
||||
if items[1].(map[string]any)["type"] != "function_call" || items[3].(map[string]any)["type"] != "function_call" {
|
||||
t.Fatalf("tool call history = %#v", items)
|
||||
}
|
||||
if items[5].(map[string]any)["role"] != "assistant" {
|
||||
t.Fatalf("assistant message history = %#v", items[5])
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestRejectsAmbiguousShellDeclarations(t *testing.T) {
|
||||
_, _, err := normalizeResponsesRequest([]byte(`{
|
||||
"model":"public","input":"hello",
|
||||
@@ -89,16 +340,43 @@ func TestRequestRejectsAmbiguousShellDeclarations(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestOptionalCompatibilityControlsAreIgnored(t *testing.T) {
|
||||
normalized, compatibility, err := normalizeResponsesRequest([]byte(`{
|
||||
"model":"public",
|
||||
"tools":[{"type":"local_shell","display_width":120},{"type":"apply_patch","mode":"auto"}],
|
||||
"input":[{"type":"additional_tools","role":"user","tools":[{"type":"function","name":"lookup","parameters":{"type":"object"}}]}]
|
||||
}`), "grok-4.5")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if compatibility == nil {
|
||||
t.Fatal("兼容字段未触发归一化")
|
||||
}
|
||||
warnings := compatibility.warningHeader()
|
||||
for _, expected := range []string{"legacy_local_shell_controls_ignored", "apply_patch_controls_ignored", "additional_tools_role_approximated"} {
|
||||
if !strings.Contains(warnings, expected) {
|
||||
t.Fatalf("warnings = %q", warnings)
|
||||
}
|
||||
}
|
||||
var request map[string]any
|
||||
if err := json.Unmarshal(normalized, &request); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(request["tools"].([]any)) != 3 {
|
||||
t.Fatalf("tools = %#v", request["tools"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyPatchToolRequestHistoryAndJSONResponse(t *testing.T) {
|
||||
normalized, compatibility, err := normalizeResponsesRequest([]byte(`{
|
||||
"model":"public","input":"edit file",
|
||||
"tools":[{"type":"apply_patch","strict":false}],
|
||||
"tools":[{"type":"apply_patch"}],
|
||||
"tool_choice":{"type":"apply_patch"}
|
||||
}`), "grok-4.5")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if compatibility == nil || !strings.Contains(compatibility.warningHeader(), "apply_patch_controls_ignored") {
|
||||
if compatibility == nil {
|
||||
t.Fatal("apply_patch 未启用兼容层")
|
||||
}
|
||||
var request map[string]any
|
||||
@@ -145,7 +423,7 @@ func TestApplyPatchToolRequestHistoryAndJSONResponse(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := request["input"].([]any)
|
||||
if items[0].(map[string]any)["type"] != "function_call" || !strings.Contains(items[0].(map[string]any)["arguments"].(string), `"delete_file"`) {
|
||||
if items[0].(map[string]any)["type"] != "function_call" || items[0].(map[string]any)["id"] != nil || items[0].(map[string]any)["status"] != nil || !strings.Contains(items[0].(map[string]any)["arguments"].(string), `"delete_file"`) {
|
||||
t.Fatalf("apply patch call history = %#v", items[0])
|
||||
}
|
||||
if items[1].(map[string]any)["type"] != "function_call_output" || !strings.Contains(items[1].(map[string]any)["output"].(string), "failed") {
|
||||
@@ -194,20 +472,17 @@ func TestApplyPatchStreamBuffersFunctionProtocolAndRestoresItems(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAdditionalToolsAndCompactionBoundaryRemainVisible(t *testing.T) {
|
||||
normalized, compatibility, err := normalizeResponsesRequest([]byte(`{
|
||||
normalized, _, err := normalizeResponsesRequest([]byte(`{
|
||||
"model":"public","tools":[{"type":"function","name":"lookup","description":"old","parameters":{"type":"object"}}],
|
||||
"input":[
|
||||
{"type":"compaction_trigger"},
|
||||
{"type":"additional_tools","role":"user","tools":[{"type":"function","name":"lookup","description":"new","parameters":{"type":"object"}},{"type":"apply_patch"}]},
|
||||
{"type":"additional_tools","role":"developer","tools":[{"type":"function","name":"lookup","description":"new","parameters":{"type":"object"}},{"type":"apply_patch"}]},
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"continue"}]}
|
||||
]
|
||||
}`), "grok-4.5")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if compatibility == nil || !strings.Contains(compatibility.warningHeader(), "additional_tools_role_approximated") {
|
||||
t.Fatalf("compatibility = %#v", compatibility)
|
||||
}
|
||||
var request map[string]any
|
||||
if err := json.Unmarshal(normalized, &request); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -19,20 +19,43 @@ func (c *responsesToolCompatibility) normalizeInputItems(items []any) ([]any, []
|
||||
continue
|
||||
}
|
||||
param := fmt.Sprintf("input[%d]", index)
|
||||
switch stringField(item, "type") {
|
||||
itemType := strings.TrimSpace(stringField(item, "type"))
|
||||
// Codex/OpenAI 可能省略带 role 消息的 type;在重建前补成明确消息,
|
||||
// 避免无类型对象直接进入 ModelInput。
|
||||
if itemType == "" && strings.TrimSpace(stringField(item, "role")) != "" {
|
||||
itemType = "message"
|
||||
}
|
||||
switch itemType {
|
||||
case "message":
|
||||
converted, err := normalizeMessageInput(item, param)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
c.changed = true
|
||||
rewritten = append(rewritten, converted)
|
||||
case "function_call":
|
||||
namespace := strings.TrimSpace(stringField(item, "namespace"))
|
||||
if namespace == "" {
|
||||
rewritten = append(rewritten, cloneJSONValue(item))
|
||||
continue
|
||||
converted, err := c.normalizeFunctionCallInput(item, param)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
name := strings.TrimSpace(stringField(item, "name"))
|
||||
if name == "" {
|
||||
return nil, nil, nil, &responsesRequestError{Message: param + ".name 不能为空", Param: param + ".name", Code: "invalid_parameter"}
|
||||
c.changed = true
|
||||
rewritten = append(rewritten, converted)
|
||||
case "function_call_output":
|
||||
converted, err := normalizeFunctionCallOutputInput(item, param)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
converted := cloneJSONObject(item)
|
||||
converted["name"] = c.alias(responsesToolIdentity{Kind: responsesFunctionTool, Namespace: namespace, Name: name})
|
||||
delete(converted, "namespace")
|
||||
c.changed = true
|
||||
rewritten = append(rewritten, converted)
|
||||
case "reasoning":
|
||||
converted := sanitizeReasoningInput(item)
|
||||
c.changed = true
|
||||
rewritten = append(rewritten, converted)
|
||||
case "file_search_call", "web_search_call", "image_generation_call", "code_interpreter_call",
|
||||
"shell_call", "mcp_list_tools", "mcp_approval_request", "mcp_approval_response", "mcp_call", "compaction":
|
||||
// 这些类型已进入 Grok Build 0.2.101 的 Responses InputItem 契约。
|
||||
// 仅清理 Codex 私有字段和 null,不能把原生调用降级成文本边界。
|
||||
converted := sanitizeNativeHistoryInput(item, itemType)
|
||||
c.changed = true
|
||||
rewritten = append(rewritten, converted)
|
||||
case "tool_search_call":
|
||||
@@ -49,7 +72,7 @@ func (c *responsesToolCompatibility) normalizeInputItems(items []any) ([]any, []
|
||||
continue
|
||||
}
|
||||
if execution != "client" {
|
||||
return nil, nil, nil, &responsesRequestError{Message: "tool_search_call.execution 必须是 client 或 server", Param: param + ".execution", Code: "invalid_parameter"}
|
||||
return nil, nil, nil, &responsesRequestError{Message: "tool_search_call.execution 只支持 client 或 server", Param: param + ".execution", Code: "invalid_parameter"}
|
||||
}
|
||||
arguments, err := encodeFunctionArguments(item["arguments"])
|
||||
if err != nil {
|
||||
@@ -63,7 +86,7 @@ func (c *responsesToolCompatibility) normalizeInputItems(items []any) ([]any, []
|
||||
case "tool_search_output":
|
||||
execution := strings.ToLower(strings.TrimSpace(stringField(item, "execution")))
|
||||
if execution != "" && execution != "client" && execution != "server" {
|
||||
return nil, nil, nil, &responsesRequestError{Message: "tool_search_output.execution 必须是 client 或 server", Param: param + ".execution", Code: "invalid_parameter"}
|
||||
return nil, nil, nil, &responsesRequestError{Message: "tool_search_output.execution 只支持 client 或 server", Param: param + ".execution", Code: "invalid_parameter"}
|
||||
}
|
||||
callID := strings.TrimSpace(stringField(item, "call_id"))
|
||||
if callID == "" {
|
||||
@@ -91,32 +114,17 @@ func (c *responsesToolCompatibility) normalizeInputItems(items []any) ([]any, []
|
||||
rewritten = append(rewritten, compatibilityBoundaryMessage(message))
|
||||
}
|
||||
case "custom_tool_call":
|
||||
name := strings.TrimSpace(stringField(item, "name"))
|
||||
if name == "" {
|
||||
return nil, nil, nil, &responsesRequestError{Message: param + ".name 不能为空", Param: param + ".name", Code: "invalid_parameter"}
|
||||
}
|
||||
input, ok := item["input"].(string)
|
||||
if !ok {
|
||||
return nil, nil, nil, &responsesRequestError{Message: param + ".input 必须是字符串", Param: param + ".input", Code: "invalid_parameter"}
|
||||
}
|
||||
arguments, err := encodeCustomToolArguments(input)
|
||||
converted, err := c.normalizeCustomToolCallInput(item, param)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
namespace := strings.TrimSpace(stringField(item, "namespace"))
|
||||
converted := cloneJSONObject(item)
|
||||
converted["type"] = "function_call"
|
||||
converted["name"] = c.alias(responsesToolIdentity{Kind: responsesCustomTool, Namespace: namespace, Name: name})
|
||||
converted["arguments"] = arguments
|
||||
delete(converted, "input")
|
||||
delete(converted, "namespace")
|
||||
c.changed = true
|
||||
rewritten = append(rewritten, converted)
|
||||
case "custom_tool_call_output":
|
||||
converted := cloneJSONObject(item)
|
||||
converted["type"] = "function_call_output"
|
||||
delete(converted, "name")
|
||||
delete(converted, "namespace")
|
||||
converted, err := normalizeFunctionCallOutputInput(item, param)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
c.changed = true
|
||||
rewritten = append(rewritten, converted)
|
||||
case "apply_patch_call":
|
||||
@@ -157,6 +165,13 @@ func (c *responsesToolCompatibility) normalizeInputItems(items []any) ([]any, []
|
||||
}
|
||||
c.changed = true
|
||||
rewritten = append(rewritten, converted)
|
||||
case "shell_call_output":
|
||||
converted, err := normalizeShellCallOutputInput(item, param)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
c.changed = true
|
||||
rewritten = append(rewritten, converted)
|
||||
case "mcp_tool_call_output":
|
||||
converted, err := normalizeMCPOutputInput(item, param)
|
||||
if err != nil {
|
||||
@@ -178,12 +193,167 @@ func (c *responsesToolCompatibility) normalizeInputItems(items []any) ([]any, []
|
||||
c.changed = true
|
||||
rewritten = append(rewritten, marker)
|
||||
default:
|
||||
if kind := strings.TrimSpace(stringField(item, "type")); kind != "" {
|
||||
c.changed = true
|
||||
c.addWarning("unsupported_input_history_omitted")
|
||||
rewritten = append(rewritten, unsupportedInputHistoryBoundary(item, kind))
|
||||
continue
|
||||
}
|
||||
rewritten = append(rewritten, cloneJSONValue(item))
|
||||
}
|
||||
}
|
||||
return rewritten, loadedTools, visibleTools, nil
|
||||
}
|
||||
|
||||
func (c *responsesToolCompatibility) normalizeFunctionCallInput(item map[string]any, param string) (map[string]any, error) {
|
||||
name := strings.TrimSpace(stringField(item, "name"))
|
||||
if name == "" {
|
||||
return nil, &responsesRequestError{Message: param + ".name 不能为空", Param: param + ".name", Code: "invalid_parameter"}
|
||||
}
|
||||
callID := strings.TrimSpace(stringField(item, "call_id"))
|
||||
if callID == "" {
|
||||
return nil, &responsesRequestError{Message: param + ".call_id 不能为空", Param: param + ".call_id", Code: "invalid_parameter"}
|
||||
}
|
||||
arguments, err := encodeFunctionArguments(item["arguments"])
|
||||
if err != nil {
|
||||
return nil, &responsesRequestError{Message: param + ".arguments 无法编码", Param: param + ".arguments", Code: "invalid_parameter"}
|
||||
}
|
||||
namespace := strings.TrimSpace(stringField(item, "namespace"))
|
||||
if namespace != "" {
|
||||
name = c.alias(responsesToolIdentity{Kind: responsesFunctionTool, Namespace: namespace, Name: name})
|
||||
}
|
||||
// 按官方 Build 回放结构仅保留四个输入字段,避免输出态字段和私有元数据
|
||||
// 干扰 Grok 的 untagged ModelInput 反序列化。
|
||||
return map[string]any{"type": "function_call", "call_id": callID, "name": name, "arguments": arguments}, nil
|
||||
}
|
||||
|
||||
func (c *responsesToolCompatibility) normalizeCustomToolCallInput(item map[string]any, param string) (map[string]any, error) {
|
||||
name := strings.TrimSpace(stringField(item, "name"))
|
||||
if name == "" {
|
||||
return nil, &responsesRequestError{Message: param + ".name 不能为空", Param: param + ".name", Code: "invalid_parameter"}
|
||||
}
|
||||
input, ok := item["input"].(string)
|
||||
if !ok {
|
||||
return nil, &responsesRequestError{Message: param + ".input 必须是字符串", Param: param + ".input", Code: "invalid_parameter"}
|
||||
}
|
||||
arguments, err := encodeCustomToolArguments(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
callID := strings.TrimSpace(stringField(item, "call_id"))
|
||||
if callID == "" {
|
||||
return nil, &responsesRequestError{Message: param + ".call_id 不能为空", Param: param + ".call_id", Code: "invalid_parameter"}
|
||||
}
|
||||
namespace := strings.TrimSpace(stringField(item, "namespace"))
|
||||
return map[string]any{
|
||||
"type": "function_call", "call_id": callID,
|
||||
"name": c.alias(responsesToolIdentity{Kind: responsesCustomTool, Namespace: namespace, Name: name}),
|
||||
"arguments": arguments,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func sanitizeReasoningInput(item map[string]any) map[string]any {
|
||||
// 官方 Grok Build 回放 reasoning 时会删除 output-only status,但会保留
|
||||
// id、summary、content 和可选 encrypted_content。密文不是回放的前置条件。
|
||||
converted := copyNonNullHistoryFields(item, "id", "summary", "content", "encrypted_content")
|
||||
converted["type"] = "reasoning"
|
||||
if !hasPortableReasoningContent(converted) {
|
||||
return compatibilityBoundaryMessage("A prior model reasoning item was omitted because it has no portable content for Grok Build.")
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
func hasPortableReasoningContent(item map[string]any) bool {
|
||||
if encrypted, ok := item["encrypted_content"].(string); ok && strings.TrimSpace(encrypted) != "" {
|
||||
return true
|
||||
}
|
||||
for _, key := range []string{"summary", "content"} {
|
||||
if values, ok := item[key].([]any); ok && len(values) > 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// sanitizeNativeHistoryInput 按 Grok Build 0.2.101 的原生 InputItem 字段重建历史,
|
||||
// 避免 Codex 扩展元数据干扰 Rust untagged enum 的反序列化。
|
||||
func sanitizeNativeHistoryInput(item map[string]any, itemType string) map[string]any {
|
||||
var fields []string
|
||||
switch itemType {
|
||||
case "file_search_call":
|
||||
fields = []string{"id", "queries", "status", "results"}
|
||||
case "web_search_call":
|
||||
fields = []string{"action", "id", "status"}
|
||||
case "image_generation_call":
|
||||
fields = []string{"id", "result", "status"}
|
||||
case "code_interpreter_call":
|
||||
fields = []string{"code", "container_id", "id", "outputs", "status"}
|
||||
case "shell_call":
|
||||
fields = []string{"id", "call_id", "action", "status", "environment"}
|
||||
case "mcp_list_tools":
|
||||
fields = []string{"id", "server_label", "tools", "error"}
|
||||
case "mcp_approval_request":
|
||||
fields = []string{"arguments", "id", "name", "server_label"}
|
||||
case "mcp_approval_response":
|
||||
fields = []string{"approval_request_id", "approve", "id", "reason"}
|
||||
case "mcp_call":
|
||||
fields = []string{"arguments", "id", "name", "server_label", "approval_request_id", "error", "output", "status"}
|
||||
case "compaction":
|
||||
fields = []string{"id", "encrypted_content"}
|
||||
}
|
||||
converted := copyNonNullHistoryFields(item, fields...)
|
||||
converted["type"] = itemType
|
||||
return converted
|
||||
}
|
||||
|
||||
func copyNonNullHistoryFields(item map[string]any, fields ...string) map[string]any {
|
||||
converted := make(map[string]any, len(fields)+1)
|
||||
for _, key := range fields {
|
||||
if value, ok := sanitizeHistoryJSONValue(item[key]); ok {
|
||||
converted[key] = value
|
||||
}
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
func sanitizeHistoryJSONValue(value any) (any, bool) {
|
||||
switch typed := value.(type) {
|
||||
case nil:
|
||||
return nil, false
|
||||
case map[string]any:
|
||||
cleaned := make(map[string]any, len(typed))
|
||||
for key, nested := range typed {
|
||||
if key == "phase" || key == "internal_chat_message_metadata_passthrough" {
|
||||
continue
|
||||
}
|
||||
if normalized, ok := sanitizeHistoryJSONValue(nested); ok {
|
||||
cleaned[key] = normalized
|
||||
}
|
||||
}
|
||||
return cleaned, true
|
||||
case []any:
|
||||
cleaned := make([]any, 0, len(typed))
|
||||
for _, nested := range typed {
|
||||
if normalized, ok := sanitizeHistoryJSONValue(nested); ok {
|
||||
cleaned = append(cleaned, normalized)
|
||||
}
|
||||
}
|
||||
return cleaned, true
|
||||
default:
|
||||
return cloneJSONValue(value), true
|
||||
}
|
||||
}
|
||||
|
||||
func unsupportedInputHistoryBoundary(item map[string]any, kind string) map[string]any {
|
||||
parts := []string{"A prior Responses history item was omitted because Grok Build cannot deserialize this Codex item type.", "Type: " + kind}
|
||||
for _, key := range []string{"id", "call_id", "name", "status"} {
|
||||
if value := strings.TrimSpace(stringField(item, key)); value != "" {
|
||||
parts = append(parts, strings.ReplaceAll(key, "_", " ")+": "+value)
|
||||
}
|
||||
}
|
||||
return compatibilityBoundaryMessage(strings.Join(parts, "\n"))
|
||||
}
|
||||
|
||||
func encodeFunctionArguments(value any) (string, error) {
|
||||
if text, ok := value.(string); ok {
|
||||
return text, nil
|
||||
|
||||
@@ -2,6 +2,7 @@ package cli
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -57,6 +58,110 @@ func normalizeMCPOutputInput(item map[string]any, param string) (map[string]any,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func normalizeMessageInput(item map[string]any, param string) (map[string]any, error) {
|
||||
role := strings.TrimSpace(stringField(item, "role"))
|
||||
if role == "" {
|
||||
role = "assistant"
|
||||
}
|
||||
if role == "model" {
|
||||
role = "assistant"
|
||||
}
|
||||
content, err := normalizeMessageContent(item["content"], role, param+".content")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 仅按白名单重建,避免 Codex 的 phase、metadata、status、id 等
|
||||
// 非输入字段进入 Grok ModelInput。
|
||||
return map[string]any{"type": "message", "role": role, "content": content}, nil
|
||||
}
|
||||
|
||||
func normalizeMessageContent(value any, role, param string) (any, error) {
|
||||
if text, ok := value.(string); ok {
|
||||
return text, nil
|
||||
}
|
||||
items, ok := value.([]any)
|
||||
if !ok {
|
||||
return nil, &responsesRequestError{Message: param + " 必须是字符串或数组", Param: param, Code: "invalid_parameter"}
|
||||
}
|
||||
// 官方 Grok CLI 会把 assistant 的多个输出文本合并为 EasyInputMessage
|
||||
// 字符串,既保留语义,也绕开 OutputMessage 对 id/status 的输入限制。
|
||||
if role == "assistant" {
|
||||
texts := make([]string, 0, len(items))
|
||||
for _, raw := range items {
|
||||
item, isObject := raw.(map[string]any)
|
||||
if !isObject {
|
||||
texts = nil
|
||||
break
|
||||
}
|
||||
switch stringField(item, "type") {
|
||||
case "text", "input_text", "output_text":
|
||||
texts = append(texts, stringField(item, "text"))
|
||||
case "refusal":
|
||||
texts = append(texts, stringField(item, "refusal"))
|
||||
default:
|
||||
texts = nil
|
||||
}
|
||||
if texts == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if texts != nil {
|
||||
return strings.Join(texts, "\n"), nil
|
||||
}
|
||||
}
|
||||
|
||||
textPartType := "input_text"
|
||||
if role == "assistant" {
|
||||
textPartType = "output_text"
|
||||
}
|
||||
normalized := make([]any, 0, len(items))
|
||||
for index, raw := range items {
|
||||
item, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
return nil, &responsesRequestError{Message: param + "[] 必须是对象", Param: fmt.Sprintf("%s[%d]", param, index), Code: "invalid_parameter"}
|
||||
}
|
||||
switch stringField(item, "type") {
|
||||
case "text", "input_text", "output_text":
|
||||
// 按角色重建文本类型,不能把 assistant 的 output_text 改成 input_text。
|
||||
normalized = append(normalized, map[string]any{"type": textPartType, "text": stringField(item, "text")})
|
||||
case "refusal":
|
||||
normalized = append(normalized, map[string]any{"type": textPartType, "text": stringField(item, "refusal")})
|
||||
case "input_image":
|
||||
normalized = append(normalized, normalizeInputImagePart(item))
|
||||
case "input_file":
|
||||
normalized = append(normalized, normalizeInputFilePart(item))
|
||||
default:
|
||||
return nil, &responsesRequestError{Message: "Grok Build 0.2.101 不支持该 message.content 类型", Param: fmt.Sprintf("%s[%d].type", param, index), Code: "unsupported_parameter"}
|
||||
}
|
||||
}
|
||||
return normalized, nil
|
||||
}
|
||||
|
||||
func normalizeInputImagePart(item map[string]any) map[string]any {
|
||||
converted := map[string]any{"type": "input_image"}
|
||||
if value, exists := item["image_url"]; exists && value != nil {
|
||||
converted["image_url"] = cloneJSONValue(value)
|
||||
} else if value, exists := item["url"]; exists && value != nil {
|
||||
converted["image_url"] = cloneJSONValue(value)
|
||||
}
|
||||
for _, key := range []string{"detail", "file_id"} {
|
||||
if value, exists := item[key]; exists && value != nil {
|
||||
converted[key] = cloneJSONValue(value)
|
||||
}
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
func normalizeInputFilePart(item map[string]any) map[string]any {
|
||||
converted := map[string]any{"type": "input_file"}
|
||||
for _, key := range []string{"file_data", "file_id", "filename", "file_url"} {
|
||||
if value, exists := item[key]; exists && value != nil {
|
||||
converted[key] = cloneJSONValue(value)
|
||||
}
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
func textInputContent(raw any) (string, bool) {
|
||||
if text, ok := raw.(string); ok {
|
||||
return text, true
|
||||
|
||||
Reference in new issue
Block a user