mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-19 07:27:43 +08:00
fix: align tool call protocol and thinking controls
This commit is contained in:
@@ -56,6 +56,21 @@ func TestCollectStreamThinkingAndText(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectStreamDropsThinkingWhenDisabled(t *testing.T) {
|
||||
resp := makeHTTPResponse(
|
||||
"data: {\"p\":\"response/thinking_content\",\"v\":\"Thinking...\"}\n" +
|
||||
"data: {\"p\":\"response/content\",\"v\":\"Answer\"}\n" +
|
||||
"data: [DONE]\n",
|
||||
)
|
||||
result := CollectStream(resp, false, true)
|
||||
if result.Thinking != "" {
|
||||
t.Fatalf("expected disabled thinking to be dropped, got %q", result.Thinking)
|
||||
}
|
||||
if result.Text != "Answer" {
|
||||
t.Fatalf("expected only visible answer, got %q", result.Text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectStreamOnlyThinking(t *testing.T) {
|
||||
resp := makeHTTPResponse(
|
||||
"data: {\"p\":\"response/thinking_content\",\"v\":\"Only thinking\"}\n" +
|
||||
|
||||
@@ -99,6 +99,10 @@ func ParseSSEChunkForContent(chunk map[string]any, thinkingEnabled bool, current
|
||||
if transitioned {
|
||||
newType = "text"
|
||||
}
|
||||
if !thinkingEnabled {
|
||||
parts = dropThinkingParts(parts)
|
||||
newType = "text"
|
||||
}
|
||||
return parts, false, newType
|
||||
}
|
||||
|
||||
@@ -172,6 +176,9 @@ func updateTypeFromNestedResponse(path string, v any, newType *string) {
|
||||
func resolvePartType(path string, thinkingEnabled bool, newType string) string {
|
||||
switch {
|
||||
case path == "response/thinking_content":
|
||||
if !thinkingEnabled {
|
||||
return "thinking"
|
||||
}
|
||||
if newType == "text" {
|
||||
return "text"
|
||||
}
|
||||
@@ -187,6 +194,20 @@ func resolvePartType(path string, thinkingEnabled bool, newType string) string {
|
||||
}
|
||||
}
|
||||
|
||||
func dropThinkingParts(parts []ContentPart) []ContentPart {
|
||||
if len(parts) == 0 {
|
||||
return parts
|
||||
}
|
||||
out := parts[:0]
|
||||
for _, p := range parts {
|
||||
if p.Type == "thinking" {
|
||||
continue
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func appendChunkValueContent(v any, partType string, newType *string, parts *[]ContentPart, path string) bool {
|
||||
switch val := v.(type) {
|
||||
case string:
|
||||
|
||||
Reference in New Issue
Block a user