From 6c39c8e191e169ca5ba99b88d3cb51855b42cac4 Mon Sep 17 00:00:00 2001 From: "CJACK." Date: Fri, 6 Mar 2026 21:24:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20text=20=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA=E6=97=B6=20content=20=E5=9B=9E=E9=80=80=E4=B8=A2?= =?UTF-8?q?=E5=A4=B1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../adapter/openai/message_normalize_test.go | 20 +++++++++++++++++++ internal/prompt/messages.go | 4 ++-- internal/prompt/messages_test.go | 9 +++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/internal/adapter/openai/message_normalize_test.go b/internal/adapter/openai/message_normalize_test.go index a4e1843..ecb3bbd 100644 --- a/internal/adapter/openai/message_normalize_test.go +++ b/internal/adapter/openai/message_normalize_test.go @@ -237,3 +237,23 @@ func TestNormalizeOpenAIMessagesForPrompt_DeveloperRoleMapsToSystem(t *testing.T t.Fatalf("expected developer role converted to system, got %#v", normalized[0]["role"]) } } + +func TestNormalizeOpenAIMessagesForPrompt_AssistantArrayContentFallbackWhenTextEmpty(t *testing.T) { + raw := []any{ + map[string]any{ + "role": "assistant", + "content": []any{ + map[string]any{"type": "text", "text": "", "content": "工具说明文本"}, + }, + }, + } + + normalized := normalizeOpenAIMessagesForPrompt(raw, "") + if len(normalized) != 1 { + t.Fatalf("expected one normalized message, got %d", len(normalized)) + } + content, _ := normalized[0]["content"].(string) + if content != "工具说明文本" { + t.Fatalf("expected content fallback text preserved, got %q", content) + } +} diff --git a/internal/prompt/messages.go b/internal/prompt/messages.go index 5e124b4..e86c391 100644 --- a/internal/prompt/messages.go +++ b/internal/prompt/messages.go @@ -67,11 +67,11 @@ func NormalizeContent(v any) string { typeStr, _ := m["type"].(string) typeStr = strings.ToLower(strings.TrimSpace(typeStr)) if typeStr == "text" || typeStr == "output_text" || typeStr == "input_text" { - if txt, ok := m["text"].(string); ok { + if txt, ok := m["text"].(string); ok && txt != "" { parts = append(parts, txt) continue } - if txt, ok := m["content"].(string); ok { + if txt, ok := m["content"].(string); ok && txt != "" { parts = append(parts, txt) } } diff --git a/internal/prompt/messages_test.go b/internal/prompt/messages_test.go index 0407552..9114d39 100644 --- a/internal/prompt/messages_test.go +++ b/internal/prompt/messages_test.go @@ -21,3 +21,12 @@ func TestMessagesPrepareNilContentNoNullLiteral(t *testing.T) { t.Fatalf("expected no null literal output, got %q", got) } } + +func TestNormalizeContentArrayFallsBackToContentWhenTextEmpty(t *testing.T) { + got := NormalizeContent([]any{ + map[string]any{"type": "text", "text": "", "content": "from-content"}, + }) + if got != "from-content" { + t.Fatalf("expected fallback to content when text is empty, got %q", got) + } +}