mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-04 16:35:27 +08:00
fix: prioritize quoted functionCall keys in tool sieve
This commit is contained in:
@@ -5,18 +5,18 @@ import "strings"
|
||||
func findQuotedFunctionCallKeyStart(s string) int {
|
||||
lower := strings.ToLower(s)
|
||||
quotedIdx := findFunctionCallKeyStart(lower, `"functioncall"`)
|
||||
baretIdx := findFunctionCallKeyStart(lower, "functioncall")
|
||||
bareIdx := findFunctionCallKeyStart(lower, "functioncall")
|
||||
|
||||
switch {
|
||||
case quotedIdx < 0:
|
||||
return baretIdx
|
||||
case baretIdx < 0:
|
||||
// Prefer quoted JSON keys when present. Bare-key detection is a fallback
|
||||
// for loose payloads like {functionCall:{...}}.
|
||||
//
|
||||
// This avoids anchoring on earlier prose such as:
|
||||
// "... {note} functionCall: ... {\"functionCall\":{...}}"
|
||||
// where choosing the earliest bare match can hide the real tool payload.
|
||||
if quotedIdx >= 0 {
|
||||
return quotedIdx
|
||||
case quotedIdx < baretIdx:
|
||||
return quotedIdx
|
||||
default:
|
||||
return baretIdx
|
||||
}
|
||||
return bareIdx
|
||||
}
|
||||
|
||||
func findFunctionCallKeyStart(lower, key string) int {
|
||||
|
||||
@@ -143,6 +143,14 @@ func TestFindToolSegmentStartDetectsLooseFunctionCallKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindToolSegmentStartPrefersQuotedFunctionCallOverEarlierBareProse(t *testing.T) {
|
||||
input := `prefix {note} functionCall: docs hint {"functionCall":{"name":"search_web","args":{"query":"x"}}}`
|
||||
want := strings.Index(input, `{"functionCall"`)
|
||||
if got := findToolSegmentStart(input); got != want {
|
||||
t.Fatalf("expected quoted functionCall JSON start %d, got %d", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindToolSegmentStartIgnoresLooseFunctionCallProse(t *testing.T) {
|
||||
input := "Please explain why functionCall: is used in documentation examples."
|
||||
if got := findToolSegmentStart(input); got != -1 {
|
||||
|
||||
Reference in New Issue
Block a user