Fix/claude thinking tool call conversion (#3756)

Signed-off-by: wydream <yaodiwu618@gmail.com>
This commit is contained in:
woody
2026-05-20 11:34:07 +08:00
committed by GitHub
parent f1dfc8f3d2
commit e1e631263c
12 changed files with 1113 additions and 58 deletions

View File

@@ -174,7 +174,15 @@ func mergeConsecutiveMessages(body []byte) ([]byte, error) {
result[len(result)-1].Role == msg.Role &&
(msg.Role == roleUser || msg.Role == roleAssistant) {
last := &result[len(result)-1]
if msg.Role == roleAssistant &&
(len(last.ToolCalls) > 0 || len(msg.ToolCalls) > 0 || last.FunctionCall != nil || msg.FunctionCall != nil) {
// Assistant tool-calling turns are structurally sensitive. Keep them split
// rather than trying to merge content/reasoning and risking dropped calls.
result = append(result, msg)
continue
}
last.Content = mergeMessageContent(last.Content, msg.Content)
last.ReasoningContent = mergeReasoningContent(last.ReasoningContent, msg.ReasoningContent)
merged = true
continue
}
@@ -202,6 +210,17 @@ func mergeMessageContent(prev, curr any) any {
return append(prevParts, currParts...)
}
func mergeReasoningContent(prev, curr string) string {
switch {
case prev == "":
return curr
case curr == "":
return prev
default:
return prev + "\n\n" + curr
}
}
func ReplaceResponseBody(body []byte) error {
log.Debugf("response body: %s", string(body))
err := proxywasm.ReplaceHttpResponseBody(body)