mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-05 00:45:29 +08:00
- 引入 parseTextKVToolCalls 解析器以处理混杂文本或带历史记录套壳(如 [TOOL_CALL_HISTORY])输出的函数调用提取。 - 将其作为 JSON 和 XML 的 fallback 解析手段集成到主流程。 - 添加单元测试用例且更新相关语义说明文档。
56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package util
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var textKVNamePattern = regexp.MustCompile(`(?is)function\.name:\s*([a-zA-Z0-9_\-.]+)`)
|
|
|
|
func parseTextKVToolCalls(text string) []ParsedToolCall {
|
|
var out []ParsedToolCall
|
|
matches := textKVNamePattern.FindAllStringSubmatchIndex(text, -1)
|
|
if len(matches) == 0 {
|
|
return nil
|
|
}
|
|
|
|
for i, match := range matches {
|
|
name := text[match[2]:match[3]]
|
|
|
|
offset := match[1]
|
|
endSearch := len(text)
|
|
if i+1 < len(matches) {
|
|
endSearch = matches[i+1][0]
|
|
}
|
|
|
|
searchArea := text[offset:endSearch]
|
|
argIdx := strings.Index(searchArea, "function.arguments:")
|
|
if argIdx < 0 {
|
|
continue
|
|
}
|
|
|
|
startIdx := offset + argIdx + len("function.arguments:")
|
|
braceIdx := strings.IndexByte(text[startIdx:endSearch], '{')
|
|
if braceIdx < 0 {
|
|
continue
|
|
}
|
|
|
|
actualStart := startIdx + braceIdx
|
|
objJson, _, ok := extractJSONObject(text, actualStart)
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
input := parseToolCallInput(objJson)
|
|
out = append(out, ParsedToolCall{
|
|
Name: name,
|
|
Input: input,
|
|
})
|
|
}
|
|
|
|
if len(out) == 0 {
|
|
return nil
|
|
}
|
|
return out
|
|
}
|