mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-28 03:07:08 +08:00
fix(gemini): retain complete string argument snapshots
This commit is contained in:
1 parent
ddb7eac633
commit
ce026a2cbf
4 files changed
+22
-17
No files matched your search
@@ -764,7 +764,7 @@ func (s *GeminiMessagesCompatService) handleChatCompletionsStreamingResponseFrom
|
||||
}
|
||||
|
||||
delta := seenToolJSON.next(argsJSONText)
|
||||
toolBoundary.observe(fc, seenToolJSON.seen.String())
|
||||
toolBoundary.observe(fc)
|
||||
if delta != "" {
|
||||
if emitAnthropicEvent(&apicompat.AnthropicStreamEvent{
|
||||
Type: "content_block_delta",
|
||||
|
||||
@@ -2428,7 +2428,7 @@ func (s *GeminiMessagesCompatService) handleStreamingResponse(c *gin.Context, re
|
||||
}
|
||||
|
||||
delta := seenToolJSON.next(argsJSONText)
|
||||
toolBoundary.observe(fc, seenToolJSON.seen.String())
|
||||
toolBoundary.observe(fc)
|
||||
if delta != "" {
|
||||
writeSSE(c.Writer, "content_block_delta", map[string]any{
|
||||
"type": "content_block_delta",
|
||||
|
||||
@@ -37,6 +37,14 @@ func TestGeminiParallelToolStreams(t *testing.T) {
|
||||
{"late_id_then_distinct_same_arguments_call", [][]string{{`{"name":"read_file","args":"{\"path\":"}`}, {`{"name":"read_file","args":"\"a\"}","id":"a"}`}, {`{"name":"read_file","args":{"path":"a"},"id":"b"}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}},
|
||||
{"anonymous_cumulative_final_string_then_snapshot", [][]string{{`{"name":"read_file","args":"{\"path\":"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":{"path":"a"}}`}}, []string{`{"path":"a"}`}},
|
||||
{"explicit_id_change_overrides_fragment_snapshot", [][]string{{`{"name":"read_file","args":"{\"path\":","id":"a"}`}, {`{"name":"read_file","args":"\"a\"}","id":"a"}`}, {`{"name":"read_file","args":{"path":"a"},"id":"b"}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}},
|
||||
{"complete_string_duplicate_no_ids", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}"}`}}, []string{`{"path":"a"}`}},
|
||||
{"complete_string_duplicate_first_id_only", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}"}`}}, []string{`{"path":"a"}`}},
|
||||
{"complete_string_duplicate_final_object_same_id", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":{"path":"a"},"id":"a"}`}}, []string{`{"path":"a"}`}},
|
||||
{"complete_string_final_object_no_ids", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":{"path":"a"}}`}}, []string{`{"path":"a"}`}},
|
||||
{"complete_strings_stable_id", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":{"path":"a"},"id":"a"}`}}, []string{`{"path":"a"}`}},
|
||||
{"complete_strings_distinct_ids", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}","id":"b"}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}},
|
||||
{"complete_string_snapshot_then_identical_native_call", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":{"path":"a"}}`}, {`{"name":"read_file","args":{"path":"a"}}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}},
|
||||
{"complete_string_late_id_then_new_id", [][]string{{`{"name":"read_file","args":"{\"path\":\"a\"}"}`}, {`{"name":"read_file","args":"{\"path\":\"a\"}","id":"a"}`}, {`{"name":"read_file","args":{"path":"a"},"id":"b"}`}}, []string{`{"path":"a"}`, `{"path":"a"}`}},
|
||||
{"different_names", [][]string{{`{"name":"read_file","args":{}}`, `{"name":"read_other","args":{}}`}}, []string{`{}`, `{}`}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
||||
@@ -7,13 +7,13 @@ import (
|
||||
|
||||
// geminiToolCallBoundary tracks identity separately from the function name.
|
||||
// Native object-valued args without IDs represent independent calls, including
|
||||
// identical calls. String-valued fragments may end with a cumulative snapshot;
|
||||
// preserve that snapshot's identity until its object-valued args arrive.
|
||||
// Without IDs, a terminal snapshot and a new identical call immediately after
|
||||
// fragments are indistinguishable; prefer the snapshot in that specific case.
|
||||
// identical calls. String-valued arguments use stream mode even when the first
|
||||
// chunk is already complete JSON; retain duplicate strings and the terminal
|
||||
// object snapshot in that call. Without IDs, a terminal snapshot and a new
|
||||
// identical call after strings are indistinguishable; prefer the snapshot.
|
||||
type geminiToolCallBoundary struct {
|
||||
upstreamID string
|
||||
fragmented bool
|
||||
stringArgs bool
|
||||
}
|
||||
|
||||
func (b *geminiToolCallBoundary) startsNewBlock(openName, seenJSON, argsJSON string, fc map[string]any) bool {
|
||||
@@ -32,24 +32,21 @@ func (b *geminiToolCallBoundary) startsNewBlock(openName, seenJSON, argsJSON str
|
||||
return false
|
||||
}
|
||||
// Keep the existing delta accumulator's duplicate/rewind behavior for a
|
||||
// fragmented call. Do not deduplicate separate complete native calls.
|
||||
if b.fragmented && strings.HasPrefix(seenJSON, strings.TrimSuffix(argsJSON, "\u0000")) {
|
||||
// string-argument call. Do not deduplicate separate complete native calls.
|
||||
if b.stringArgs && strings.HasPrefix(seenJSON, strings.TrimSuffix(argsJSON, "\u0000")) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (b *geminiToolCallBoundary) observe(fc map[string]any, seenJSON string) {
|
||||
func (b *geminiToolCallBoundary) observe(fc map[string]any) {
|
||||
if b.upstreamID == "" {
|
||||
b.upstreamID, _ = fc["id"].(string)
|
||||
}
|
||||
if _, isString := fc["args"].(string); isString {
|
||||
b.fragmented = b.fragmented || !json.Valid([]byte(seenJSON))
|
||||
} else {
|
||||
// The terminal object snapshot has been consumed. A following
|
||||
// anonymous complete object can now be another identical call.
|
||||
b.fragmented = false
|
||||
}
|
||||
// The representation determines the mode, not whether the first string
|
||||
// happens to contain complete JSON. An object consumes the final snapshot,
|
||||
// allowing the next anonymous native object to be an independent call.
|
||||
_, b.stringArgs = fc["args"].(string)
|
||||
}
|
||||
|
||||
func geminiToolCallArgsJSON(args any) string {
|
||||
|
||||
Reference in new issue
Block a user