mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-21 08:27:42 +08:00
refactor: 统一内容归一化逻辑并补充 nil 回归测试
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"ds2api/internal/config"
|
"ds2api/internal/config"
|
||||||
|
"ds2api/internal/prompt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func normalizeOpenAIMessagesForPrompt(raw []any, traceID string) []map[string]any {
|
func normalizeOpenAIMessagesForPrompt(raw []any, traceID string) []map[string]any {
|
||||||
@@ -132,35 +133,7 @@ func formatToolResultForPrompt(msg map[string]any) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func normalizeOpenAIContentForPrompt(v any) string {
|
func normalizeOpenAIContentForPrompt(v any) string {
|
||||||
if v == nil {
|
return prompt.NormalizeContent(v)
|
||||||
return ""
|
|
||||||
}
|
|
||||||
switch x := v.(type) {
|
|
||||||
case string:
|
|
||||||
return x
|
|
||||||
case []any:
|
|
||||||
parts := make([]string, 0, len(x))
|
|
||||||
for _, item := range x {
|
|
||||||
m, ok := item.(map[string]any)
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
t := strings.ToLower(strings.TrimSpace(asString(m["type"])))
|
|
||||||
if t != "text" && t != "output_text" && t != "input_text" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if text := asString(m["text"]); text != "" {
|
|
||||||
parts = append(parts, text)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if text := asString(m["content"]); text != "" {
|
|
||||||
parts = append(parts, text)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return strings.Join(parts, "\n")
|
|
||||||
default:
|
|
||||||
return marshalToPromptString(v)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeOpenAIArgumentsForPrompt(v any) string {
|
func normalizeOpenAIArgumentsForPrompt(v any) string {
|
||||||
|
|||||||
@@ -51,6 +51,9 @@ func MessagesPrepare(messages []map[string]any) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NormalizeContent(v any) string {
|
func NormalizeContent(v any) string {
|
||||||
|
if v == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
switch x := v.(type) {
|
switch x := v.(type) {
|
||||||
case string:
|
case string:
|
||||||
return x
|
return x
|
||||||
|
|||||||
23
internal/prompt/messages_test.go
Normal file
23
internal/prompt/messages_test.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package prompt
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestNormalizeContentNilReturnsEmpty(t *testing.T) {
|
||||||
|
if got := NormalizeContent(nil); got != "" {
|
||||||
|
t.Fatalf("expected empty string for nil content, got %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMessagesPrepareNilContentNoNullLiteral(t *testing.T) {
|
||||||
|
messages := []map[string]any{
|
||||||
|
{"role": "assistant", "content": nil},
|
||||||
|
{"role": "user", "content": "ok"},
|
||||||
|
}
|
||||||
|
got := MessagesPrepare(messages)
|
||||||
|
if got == "" {
|
||||||
|
t.Fatalf("expected non-empty output")
|
||||||
|
}
|
||||||
|
if got == "null" {
|
||||||
|
t.Fatalf("expected no null literal output, got %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user