mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-16 22:57:53 +08:00
- Replace tidwall/gjson and tidwall/sjson with github.com/MoeForge/moego/ojson and encoding/base64 with github.com/MoeForge/moego/base64 across the main module and examples - Introduce cross-method *ojson.Doc request rewriting pipelines across thinking, signature, client/codex, runtime/executor/helps, pluginhost, and all provider executors with FinalizePayloadDoc as the final semantic barrier - Convert all consecutive JSON mutations (>= 2 Set/SetRaw/Delete operations) to batch edits on ojson.Doc - Upgrade read-only byte paths to ojson.GetBytesNoCopy, ojson.GetManyBytesNoCopy, and util.ParseGJSONBytesNoCopy with explicit strings.Clone ownership protection across streaming and persistent state - Use base64.StdEncoding.ValidateString for zero-allocation signature checks and base64.EncodeDataURL for single-allocation data URL construction
78 lines
2.3 KiB
Go
78 lines
2.3 KiB
Go
package helps
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/MoeForge/moego/ojson"
|
|
translatorcommon "github.com/router-for-me/CLIProxyAPI/v8/internal/translator/common"
|
|
)
|
|
|
|
type geminiFunctionCallCounter struct {
|
|
activeByCandidate map[int64]*translatorcommon.VertexStreamingToolCall
|
|
seenCallIDs map[string]struct{}
|
|
}
|
|
|
|
// observePart counts logical Gemini/Vertex function calls, deduplicating streamed
|
|
// partialArgs continuations, repeated call IDs, and empty separator objects.
|
|
func (c *geminiFunctionCallCounter) observePart(candidateIdx int64, call ojson.Result) int64 {
|
|
if c == nil || !call.Exists() || !call.IsObject() {
|
|
return 0
|
|
}
|
|
rawName := strings.TrimSpace(call.Get("name").String())
|
|
upstreamID := strings.TrimSpace(call.Get("id").String())
|
|
|
|
if active := c.activeByCandidate[candidateIdx]; active != nil {
|
|
ownedCall := ojson.Parse(strings.Clone(call.Raw))
|
|
if active.CanContinueWith(ownedCall) {
|
|
_, done := active.Assembler.PushFunctionCall(ownedCall)
|
|
if active.Assembler.Err() != nil || done {
|
|
delete(c.activeByCandidate, candidateIdx)
|
|
}
|
|
return 0
|
|
}
|
|
}
|
|
|
|
delete(c.activeByCandidate, candidateIdx)
|
|
|
|
if translatorcommon.IsVertexStreamingFunctionCall(call) {
|
|
if rawName == "" {
|
|
return 0
|
|
}
|
|
ownCall := ojson.Parse(strings.Clone(call.Raw))
|
|
clonedName := strings.Clone(rawName)
|
|
clonedID := strings.Clone(upstreamID)
|
|
active := translatorcommon.NewVertexStreamingToolCall(clonedName, clonedName, clonedID, clonedID, 0)
|
|
_, done := active.Assembler.PushFunctionCall(ownCall)
|
|
if active.Assembler.Err() != nil {
|
|
return 0
|
|
}
|
|
if !done {
|
|
if c.activeByCandidate == nil {
|
|
c.activeByCandidate = make(map[int64]*translatorcommon.VertexStreamingToolCall)
|
|
}
|
|
c.activeByCandidate[candidateIdx] = active
|
|
}
|
|
return c.countLogicalCall(candidateIdx, clonedID)
|
|
}
|
|
|
|
if rawName == "" && upstreamID == "" && !call.Get("args").Exists() {
|
|
return 0
|
|
}
|
|
return c.countLogicalCall(candidateIdx, strings.Clone(upstreamID))
|
|
}
|
|
|
|
func (c *geminiFunctionCallCounter) countLogicalCall(candidateIdx int64, clonedID string) int64 {
|
|
if clonedID != "" {
|
|
key := fmt.Sprintf("%d:%s", candidateIdx, clonedID)
|
|
if c.seenCallIDs == nil {
|
|
c.seenCallIDs = make(map[string]struct{})
|
|
}
|
|
if _, exists := c.seenCallIDs[key]; exists {
|
|
return 0
|
|
}
|
|
c.seenCallIDs[key] = struct{}{}
|
|
}
|
|
return 1
|
|
}
|