fix(toolcall): fix deepseek function calling bug and add json repair

- Fix: Expand stream sieve keywords to support function.name: and [TOOL_CALL_HISTORY]

- Fix: Add repairInvalidJSONBackslashes to handle unescaped backslashes in Windows paths

- Sync: Update JS stream sieve to match Go implementation

- Test: Add unit tests for backslash repair and deepseek format parsing

- Tool: Move repair json test tool to tests/repair_json_tool.go
This commit is contained in:
huangxun
2026-03-13 13:47:40 +08:00
parent f2674487c7
commit 7318d1f4a8
5 changed files with 211 additions and 7 deletions

View File

@@ -167,13 +167,28 @@ func findToolSegmentStart(s string) int {
return -1
}
lower := strings.ToLower(s)
keywords := []string{"tool_calls", "function.name:", "[tool_call_history]"}
offset := 0
for {
keyRel := strings.Index(lower[offset:], "tool_calls")
if keyRel < 0 {
bestKeyIdx := -1
matchedKeyword := ""
for _, kw := range keywords {
idx := strings.Index(lower[offset:], kw)
if idx >= 0 {
absIdx := offset + idx
if bestKeyIdx < 0 || absIdx < bestKeyIdx {
bestKeyIdx = absIdx
matchedKeyword = kw
}
}
}
if bestKeyIdx < 0 {
return -1
}
keyIdx := offset + keyRel
keyIdx := bestKeyIdx
start := strings.LastIndex(s[:keyIdx], "{")
if start < 0 {
start = keyIdx
@@ -181,7 +196,7 @@ func findToolSegmentStart(s string) int {
if !insideCodeFence(s[:start]) {
return start
}
offset = keyIdx + len("tool_calls")
offset = keyIdx + len(matchedKeyword)
}
}