mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-19 00:13:11 +08:00
feat(gemini): support asymmetric thoughtSignature replay KV across Gemini executors
This commit is contained in:
@@ -186,8 +186,14 @@ func (e *AIStudioExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth,
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, wsResp.Body)
|
||||
}
|
||||
if wsResp.Status < 200 || wsResp.Status >= 300 {
|
||||
if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, body.replayScope, wsResp.Status, wsResp.Body); errClear != nil {
|
||||
logAntigravityReasoningReplayDegraded(body.replayScope, "invalidate", errClear)
|
||||
}
|
||||
return resp, statusErr{code: wsResp.Status, msg: string(wsResp.Body)}
|
||||
}
|
||||
if body.action != "countTokens" {
|
||||
cacheAntigravityReasoningReplayFromResponse(ctx, body.replayScope, body.replayRequestPayload, wsResp.Body)
|
||||
}
|
||||
reporter.ObserveResponseModel(wsResp.Body)
|
||||
responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts)
|
||||
var param any
|
||||
@@ -284,20 +290,23 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth
|
||||
reporter.StartResponseTTFT()
|
||||
metadataLogged = true
|
||||
}
|
||||
var body bytes.Buffer
|
||||
var errBody bytes.Buffer
|
||||
if len(firstEvent.Payload) > 0 {
|
||||
reporter.MarkFirstResponseByte()
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, firstEvent.Payload)
|
||||
body.Write(firstEvent.Payload)
|
||||
errBody.Write(firstEvent.Payload)
|
||||
}
|
||||
if firstEvent.Type == wsrelay.MessageTypeStreamEnd {
|
||||
return nil, statusErr{code: firstEvent.Status, msg: body.String()}
|
||||
if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, body.replayScope, firstEvent.Status, errBody.Bytes()); errClear != nil {
|
||||
logAntigravityReasoningReplayDegraded(body.replayScope, "invalidate", errClear)
|
||||
}
|
||||
return nil, statusErr{code: firstEvent.Status, msg: errBody.String()}
|
||||
}
|
||||
for event := range wsStream {
|
||||
if event.Err != nil {
|
||||
helps.RecordAPIResponseError(ctx, e.cfg, event.Err)
|
||||
if body.Len() == 0 {
|
||||
body.WriteString(event.Err.Error())
|
||||
if errBody.Len() == 0 {
|
||||
errBody.WriteString(event.Err.Error())
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -309,14 +318,18 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth
|
||||
if len(event.Payload) > 0 {
|
||||
reporter.MarkFirstResponseByte()
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, event.Payload)
|
||||
body.Write(event.Payload)
|
||||
errBody.Write(event.Payload)
|
||||
}
|
||||
if event.Type == wsrelay.MessageTypeStreamEnd {
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil, statusErr{code: firstEvent.Status, msg: body.String()}
|
||||
if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, body.replayScope, firstEvent.Status, errBody.Bytes()); errClear != nil {
|
||||
logAntigravityReasoningReplayDegraded(body.replayScope, "invalidate", errClear)
|
||||
}
|
||||
return nil, statusErr{code: firstEvent.Status, msg: errBody.String()}
|
||||
}
|
||||
replayAccumulator := newAntigravityReasoningReplayAccumulator(body.replayScope, body.replayRequestPayload)
|
||||
out := make(chan cliproxyexecutor.StreamChunk, 1)
|
||||
go func(first wsrelay.StreamEvent) {
|
||||
defer close(out)
|
||||
@@ -336,12 +349,22 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth
|
||||
_ = helps.StopTranslatedClaudeStream(ctx, &streamErr, reporter, &streamUsage, out)
|
||||
}()
|
||||
var param any
|
||||
var replayCommitted bool
|
||||
commitReplay := func() {
|
||||
if !replayCommitted && replayAccumulator != nil && replayAccumulator.terminal && streamErr == nil && helps.ApplyPatchTranslationError(param) == nil {
|
||||
replayAccumulator.Commit(ctx)
|
||||
replayCommitted = true
|
||||
}
|
||||
}
|
||||
helps.InitializeApplyPatchStream(ctx, body.toFormat, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), translatedReq, ¶m)
|
||||
metadataLogged := false
|
||||
var relayFrames helps.RelaySSEBuffer
|
||||
processRelayFrames := func(payloads [][]byte) bool {
|
||||
for _, payload := range payloads {
|
||||
for _, frame := range splitRelayFrames(payload) {
|
||||
if replayAccumulator != nil {
|
||||
replayAccumulator.ObserveSSELine(frame)
|
||||
}
|
||||
reporter.ObserveResponseModel(frame)
|
||||
streamUsage.ObserveGeminiStream(frame)
|
||||
filtered := helps.FilterSSEUsageMetadata(frame)
|
||||
@@ -350,6 +373,9 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth
|
||||
helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error {
|
||||
return statusErr{code: http.StatusBadGateway, msg: msg}
|
||||
})
|
||||
if replayAccumulator != nil && replayAccumulator.terminal && !replayCommitted && antigravityStreamChunksHaveTerminalCommitEvent(lines) {
|
||||
commitReplay()
|
||||
}
|
||||
for i := range lines {
|
||||
select {
|
||||
case out <- cliproxyexecutor.StreamChunk{Payload: ensureColonSpacedJSON(lines[i])}:
|
||||
@@ -382,6 +408,7 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth
|
||||
helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error {
|
||||
return statusErr{code: http.StatusBadGateway, msg: msg}
|
||||
})
|
||||
commitReplay()
|
||||
for _, line := range lines {
|
||||
select {
|
||||
case out <- cliproxyexecutor.StreamChunk{Payload: ensureColonSpacedJSON(line)}:
|
||||
@@ -435,6 +462,9 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, event.Payload)
|
||||
}
|
||||
if event.Status < http.StatusOK || event.Status >= http.StatusMultipleChoices {
|
||||
if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, body.replayScope, event.Status, event.Payload); errClear != nil {
|
||||
logAntigravityReasoningReplayDegraded(body.replayScope, "invalidate", errClear)
|
||||
}
|
||||
errResponse := statusErr{code: event.Status, msg: string(event.Payload)}
|
||||
helps.RecordAPIResponseError(ctx, e.cfg, errResponse)
|
||||
reporter.PublishFailure(ctx, errResponse)
|
||||
@@ -444,6 +474,12 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth
|
||||
}
|
||||
return false
|
||||
}
|
||||
if replayAccumulator != nil {
|
||||
replayAccumulator.observeResponsePayload(event.Payload)
|
||||
if !replayAccumulator.terminal && (ojson.GetBytesNoCopy(event.Payload, "response.candidates.0").Exists() || ojson.GetBytesNoCopy(event.Payload, "candidates.0").Exists()) {
|
||||
replayAccumulator.terminal = true
|
||||
}
|
||||
}
|
||||
reporter.ObserveResponseModel(event.Payload)
|
||||
streamUsage.Observe(helps.ParseGeminiUsage(event.Payload), true)
|
||||
lines := helps.TranslateStreamWithClaudeInputTokens(ctx, body.toFormat, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), translatedReq, event.Payload, ¶m, claudeInputTokens)
|
||||
@@ -451,6 +487,7 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth
|
||||
helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error {
|
||||
return statusErr{code: http.StatusBadGateway, msg: msg}
|
||||
})
|
||||
commitReplay()
|
||||
for i := range lines {
|
||||
select {
|
||||
case out <- cliproxyexecutor.StreamChunk{Payload: ensureColonSpacedJSON(lines[i])}:
|
||||
@@ -561,9 +598,11 @@ func (e *AIStudioExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth)
|
||||
}
|
||||
|
||||
type translatedPayload struct {
|
||||
payload []byte
|
||||
action string
|
||||
toFormat sdktranslator.Format
|
||||
payload []byte
|
||||
action string
|
||||
toFormat sdktranslator.Format
|
||||
replayScope antigravityReasoningReplayScope
|
||||
replayRequestPayload []byte
|
||||
}
|
||||
|
||||
func (e *AIStudioExecutor) translateRequest(ctx context.Context, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, stream bool) ([]byte, translatedPayload, error) {
|
||||
@@ -608,6 +647,19 @@ func (e *AIStudioExecutor) translateRequest(ctx context.Context, req cliproxyexe
|
||||
}
|
||||
normalizeAIStudioThinkingLevelDoc(doc)
|
||||
internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
||||
replayScope := antigravityReasoningReplayScope{}
|
||||
var replayRequestPayload []byte
|
||||
if antigravityUsesReasoningReplayCache(baseModel) {
|
||||
var changedReplay bool
|
||||
var errReplay error
|
||||
replayScope, replayRequestPayload, changedReplay, errReplay = prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc)
|
||||
if errReplay != nil {
|
||||
return nil, translatedPayload{}, errReplay
|
||||
}
|
||||
if changedReplay {
|
||||
internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
||||
}
|
||||
}
|
||||
if action == "countTokens" {
|
||||
_ = doc.Delete("generationConfig")
|
||||
_ = doc.Delete("tools")
|
||||
@@ -615,7 +667,13 @@ func (e *AIStudioExecutor) translateRequest(ctx context.Context, req cliproxyexe
|
||||
}
|
||||
helps.ApplyPayloadConfigWithRequestDoc(e.cfg, baseModel, to.String(), from.String(), "", doc, originalTranslated, requestedModel, requestPath, opts.Headers)
|
||||
payload = doc.Bytes()
|
||||
return payload, translatedPayload{payload: payload, action: action, toFormat: to}, nil
|
||||
return payload, translatedPayload{
|
||||
payload: payload,
|
||||
action: action,
|
||||
toFormat: to,
|
||||
replayScope: replayScope,
|
||||
replayRequestPayload: replayRequestPayload,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// normalizeAIStudioThinkingLevel normalizes thinking levels to Google's canonical uppercase enum values.
|
||||
|
||||
@@ -782,14 +782,22 @@ func generateSessionID() string {
|
||||
}
|
||||
|
||||
func generateStableSessionID(payload []byte) string {
|
||||
return generateStableSessionIDFromContents(util.GetGJSONBytesNoCopy(payload, "request.contents"))
|
||||
contents := util.GetGJSONBytesNoCopy(payload, "request.contents")
|
||||
if !contents.IsArray() {
|
||||
contents = util.GetGJSONBytesNoCopy(payload, "contents")
|
||||
}
|
||||
return generateStableSessionIDFromContents(contents)
|
||||
}
|
||||
|
||||
func generateStableSessionIDDoc(doc *ojson.Doc) string {
|
||||
if doc == nil {
|
||||
return generateSessionID()
|
||||
}
|
||||
return generateStableSessionIDFromContents(doc.Get("request.contents"))
|
||||
contents := doc.Get("request.contents")
|
||||
if !contents.IsArray() {
|
||||
contents = doc.Get("contents")
|
||||
}
|
||||
return generateStableSessionIDFromContents(contents)
|
||||
}
|
||||
|
||||
func generateStableSessionIDFromContents(contents ojson.Result) string {
|
||||
|
||||
@@ -358,14 +358,8 @@ func (e *AntigravityExecutor) ExecuteStream(ctx context.Context, auth *cliproxya
|
||||
}
|
||||
}
|
||||
}
|
||||
if replayAccumulator != nil && replayAccumulator.terminal && !replayCommitted && responseFormat == sdktranslator.FormatOpenAIResponse {
|
||||
for _, chunk := range chunks {
|
||||
for _, line := range bytes.Split(chunk, []byte("\n")) {
|
||||
if ojson.GetBytesNoCopy(helps.JSONPayload(line), "type").String() == "response.completed" {
|
||||
commitReplay()
|
||||
}
|
||||
}
|
||||
}
|
||||
if replayAccumulator != nil && replayAccumulator.terminal && !replayCommitted && antigravityStreamChunksHaveTerminalCommitEvent(chunks) {
|
||||
commitReplay()
|
||||
}
|
||||
for i := range chunks {
|
||||
select {
|
||||
|
||||
@@ -66,7 +66,7 @@ func (e *AntigravityExecutor) CountTokens(ctx context.Context, auth *cliproxyaut
|
||||
e.obfuscateSensitiveWordsDoc(doc)
|
||||
helps.PrepareAntigravityClaudeStructuredOutputDoc(baseModel, doc)
|
||||
sanitizeAntigravityGeminiRequestSignaturesDoc(baseModel, doc)
|
||||
if _, _, errReplay := prepareAntigravityGeminiReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc); errReplay != nil {
|
||||
if _, errReplay := prepareAntigravityGeminiCountTokensReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc); errReplay != nil {
|
||||
return cliproxyexecutor.Response{}, errReplay
|
||||
}
|
||||
ensureAntigravityGeminiLeadingUserContentDoc(baseModel, doc)
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
homekv "github.com/router-for-me/CLIProxyAPI/v8/internal/home"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps"
|
||||
internalsignature "github.com/router-for-me/CLIProxyAPI/v8/internal/signature"
|
||||
translatorcommon "github.com/router-for-me/CLIProxyAPI/v8/internal/translator/common"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/util"
|
||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/session"
|
||||
@@ -40,14 +41,22 @@ func antigravityReplayLogKey(value string) string {
|
||||
// antigravityCountClaudeToolProvenanceIDs reports how many reserved
|
||||
// Claude-facing provenance IDs are still present in a Gemini-shaped payload.
|
||||
func antigravityCountClaudeToolProvenanceIDs(payload []byte) int {
|
||||
return antigravityCountClaudeToolProvenanceIDsResult(util.GetGJSONBytesNoCopy(payload, "request.contents"))
|
||||
contents := util.GetGJSONBytesNoCopy(payload, "request.contents")
|
||||
if !contents.IsArray() {
|
||||
contents = util.GetGJSONBytesNoCopy(payload, "contents")
|
||||
}
|
||||
return antigravityCountClaudeToolProvenanceIDsResult(contents)
|
||||
}
|
||||
|
||||
func antigravityCountClaudeToolProvenanceIDsDoc(doc *ojson.Doc) int {
|
||||
if doc == nil {
|
||||
return 0
|
||||
}
|
||||
return antigravityCountClaudeToolProvenanceIDsResult(doc.Get("request.contents"))
|
||||
contents := doc.Get("request.contents")
|
||||
if !contents.IsArray() {
|
||||
contents = doc.Get("contents")
|
||||
}
|
||||
return antigravityCountClaudeToolProvenanceIDsResult(contents)
|
||||
}
|
||||
|
||||
func antigravityCountClaudeToolProvenanceIDsResult(contents ojson.Result) int {
|
||||
@@ -379,12 +388,117 @@ func prepareAntigravityGeminiReasoningReplayPayload(ctx context.Context, modelNa
|
||||
return doc.Bytes(), scope, nil
|
||||
}
|
||||
|
||||
// antigravityStreamChunksHaveTerminalCommitEvent reports whether translated SSE
|
||||
// chunks carry a client-visible terminal completion/truncation event before
|
||||
// which accumulated reasoning replay items must be committed to KV.
|
||||
func antigravityStreamChunksHaveTerminalCommitEvent(chunks [][]byte) bool {
|
||||
for _, chunk := range chunks {
|
||||
for _, line := range bytes.Split(chunk, []byte("\n")) {
|
||||
payload := helps.JSONPayload(line)
|
||||
if len(payload) == 0 {
|
||||
continue
|
||||
}
|
||||
switch ojson.GetBytesNoCopy(payload, "type").String() {
|
||||
case "response.completed", "response.incomplete", "response.done", "message_stop":
|
||||
return true
|
||||
}
|
||||
if ojson.GetBytesNoCopy(payload, "event_type").String() == "interaction.completed" {
|
||||
return true
|
||||
}
|
||||
for _, path := range []string{
|
||||
"choices.0.finish_reason",
|
||||
"response.choices.0.finish_reason",
|
||||
"candidates.0.finishReason",
|
||||
"response.candidates.0.finishReason",
|
||||
} {
|
||||
if finishReason := ojson.GetBytesNoCopy(payload, path); finishReason.Exists() && strings.TrimSpace(finishReason.String()) != "" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isGeminiCountTokensAction(req cliproxyexecutor.Request, opts cliproxyexecutor.Options) bool {
|
||||
if req.Metadata != nil {
|
||||
if action, _ := req.Metadata["action"].(string); strings.EqualFold(strings.TrimSpace(action), "countTokens") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if opts.Metadata != nil {
|
||||
if action, _ := opts.Metadata["action"].(string); strings.EqualFold(strings.TrimSpace(action), "countTokens") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc) (antigravityReasoningReplayScope, []byte, bool, error) {
|
||||
return prepareGeminiTopLevelReasoningReplayPayloadDocWithMode(ctx, modelName, req, opts, doc, !isGeminiCountTokensAction(req, opts))
|
||||
}
|
||||
|
||||
func prepareGeminiTopLevelCountTokensReasoningReplayPayloadDoc(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc) (bool, error) {
|
||||
_, _, changed, err := prepareGeminiTopLevelReasoningReplayPayloadDocWithMode(ctx, modelName, req, opts, doc, false)
|
||||
return changed, err
|
||||
}
|
||||
|
||||
func prepareGeminiTopLevelReasoningReplayPayloadDocWithMode(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc, reserveOnMiss bool) (antigravityReasoningReplayScope, []byte, bool, error) {
|
||||
if doc == nil || !antigravityUsesReasoningReplayCache(modelName) {
|
||||
return antigravityReasoningReplayScope{}, nil, false, nil
|
||||
}
|
||||
if doc.Get("request.contents").Exists() {
|
||||
scope, changed, err := prepareAntigravityGeminiReasoningReplayPayloadDocWithOptions(ctx, modelName, req, opts, doc, true, reserveOnMiss)
|
||||
var replayRequestPayload []byte
|
||||
if reserveOnMiss && scope.valid() {
|
||||
replayRequestPayload, _ = antigravityReplayRequestEnvelopeFromDoc(doc)
|
||||
}
|
||||
return scope, replayRequestPayload, changed, err
|
||||
}
|
||||
if !doc.Get("contents").Exists() {
|
||||
return antigravityReasoningReplayScope{}, nil, false, nil
|
||||
}
|
||||
wrappedBytes, ok := antigravityReplayRequestEnvelopeFromDoc(doc)
|
||||
if !ok {
|
||||
return antigravityReasoningReplayScope{}, nil, false, nil
|
||||
}
|
||||
wrappedDoc := ojson.NewDoc(wrappedBytes)
|
||||
for _, key := range []string{"sessionId", "session_id"} {
|
||||
if val := doc.Get(key); val.Exists() {
|
||||
_ = wrappedDoc.SetRawBytes(key, []byte(val.Raw))
|
||||
}
|
||||
}
|
||||
scope, changed, err := prepareAntigravityGeminiReasoningReplayPayloadDocWithOptions(ctx, modelName, req, opts, wrappedDoc, false, reserveOnMiss)
|
||||
if err != nil {
|
||||
return scope, nil, false, err
|
||||
}
|
||||
if changed {
|
||||
if updatedContents := wrappedDoc.Get("request.contents"); updatedContents.Exists() {
|
||||
_ = doc.SetRawBytes("contents", []byte(updatedContents.Raw))
|
||||
}
|
||||
}
|
||||
var replayRequestPayload []byte
|
||||
if reserveOnMiss && scope.valid() {
|
||||
replayRequestPayload, _ = antigravityReplayRequestEnvelopeFromDoc(doc)
|
||||
}
|
||||
return scope, replayRequestPayload, changed, nil
|
||||
}
|
||||
|
||||
func prepareAntigravityGeminiReasoningReplayPayloadDoc(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc) (antigravityReasoningReplayScope, bool, error) {
|
||||
return prepareAntigravityGeminiReasoningReplayPayloadDocWithOptions(ctx, modelName, req, opts, doc, true, !isGeminiCountTokensAction(req, opts))
|
||||
}
|
||||
|
||||
func prepareAntigravityGeminiCountTokensReasoningReplayPayloadDoc(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc) (bool, error) {
|
||||
_, changed, err := prepareAntigravityGeminiReasoningReplayPayloadDocWithOptions(ctx, modelName, req, opts, doc, true, false)
|
||||
return changed, err
|
||||
}
|
||||
|
||||
func prepareAntigravityGeminiReasoningReplayPayloadDocWithOptions(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc, normalizeFunctionResponseRoles, reserveOnMiss bool) (antigravityReasoningReplayScope, bool, error) {
|
||||
if doc == nil || !antigravityUsesReasoningReplayCache(modelName) {
|
||||
return antigravityReasoningReplayScope{}, false, nil
|
||||
}
|
||||
originalContentsRaw := doc.Get("request.contents").Raw
|
||||
scope, replayApplied, errReplay := applyAntigravityReasoningReplayCacheDoc(ctx, modelName, req, opts, doc)
|
||||
scope, replayApplied, errReplay := applyAntigravityReasoningReplayCacheDocWithMode(ctx, modelName, req, opts, doc, reserveOnMiss)
|
||||
changed := replayApplied
|
||||
if errReplay != nil {
|
||||
logAntigravityReasoningReplayDegraded(scope, "read", errReplay)
|
||||
@@ -394,7 +508,7 @@ func prepareAntigravityGeminiReasoningReplayPayloadDoc(ctx context.Context, mode
|
||||
changed = false
|
||||
replayApplied = false
|
||||
}
|
||||
if normalizeAntigravityGeminiFunctionResponseRolesDoc(doc) {
|
||||
if normalizeFunctionResponseRoles && normalizeAntigravityGeminiFunctionResponseRolesDoc(doc) {
|
||||
changed = true
|
||||
}
|
||||
if antigravityDocHasClaudeToolProvenanceID(doc) {
|
||||
@@ -409,7 +523,7 @@ func prepareAntigravityGeminiReasoningReplayPayloadDoc(ctx context.Context, mode
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
if normalizeAntigravityGeminiFunctionResponseRolesDoc(doc) {
|
||||
if normalizeFunctionResponseRoles && normalizeAntigravityGeminiFunctionResponseRolesDoc(doc) {
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
@@ -428,7 +542,7 @@ func prepareAntigravityGeminiReasoningReplayPayloadDoc(ctx context.Context, mode
|
||||
origWrapped = []byte(`{"request":{"contents":` + originalContentsRaw + `}}`)
|
||||
}
|
||||
originalPairingValid := internalsignature.ValidateGeminiFunctionCallPairing(origWrapped) == nil
|
||||
if replayApplied && originalPairingValid && scope.valid() {
|
||||
if replayApplied && originalPairingValid && scope.valid() && reserveOnMiss {
|
||||
if _, errDelete := internalcache.DeleteAntigravityReasoningReplayItemsIfUnchanged(ctx, scope.modelName, scope.sessionKey, scope.cacheSnapshot); errDelete != nil {
|
||||
logAntigravityReasoningReplayDegraded(scope, "invalidate", errDelete)
|
||||
}
|
||||
@@ -471,6 +585,10 @@ func applyAntigravityReasoningReplayCache(ctx context.Context, modelName string,
|
||||
}
|
||||
|
||||
func applyAntigravityReasoningReplayCacheDoc(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc) (antigravityReasoningReplayScope, bool, error) {
|
||||
return applyAntigravityReasoningReplayCacheDocWithMode(ctx, modelName, req, opts, doc, true)
|
||||
}
|
||||
|
||||
func applyAntigravityReasoningReplayCacheDocWithMode(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc, reserveOnMiss bool) (antigravityReasoningReplayScope, bool, error) {
|
||||
if doc == nil {
|
||||
return antigravityReasoningReplayScope{}, false, nil
|
||||
}
|
||||
@@ -478,7 +596,17 @@ func applyAntigravityReasoningReplayCacheDoc(ctx context.Context, modelName stri
|
||||
if !scope.valid() {
|
||||
return scope, false, nil
|
||||
}
|
||||
items, snapshot, ok, err := internalcache.GetAntigravityReasoningReplayItemsWithSnapshotRequired(ctx, scope.modelName, scope.sessionKey)
|
||||
var (
|
||||
items [][]byte
|
||||
snapshot internalcache.AntigravityReasoningReplaySnapshot
|
||||
ok bool
|
||||
err error
|
||||
)
|
||||
if reserveOnMiss {
|
||||
items, snapshot, ok, err = internalcache.GetAntigravityReasoningReplayItemsWithSnapshotRequired(ctx, scope.modelName, scope.sessionKey)
|
||||
} else {
|
||||
items, snapshot, ok, err = internalcache.PeekAntigravityReasoningReplayItemsWithSnapshotRequired(ctx, scope.modelName, scope.sessionKey)
|
||||
}
|
||||
scope.cacheSnapshot = snapshot
|
||||
reservedBefore := antigravityCountClaudeToolProvenanceIDsDoc(doc)
|
||||
if err != nil || !ok || len(items) == 0 {
|
||||
@@ -518,9 +646,14 @@ func antigravityReplayRequestEnvelopeFromDoc(doc *ojson.Doc) ([]byte, bool) {
|
||||
if requestNode := doc.Get("request"); requestNode.IsObject() {
|
||||
return []byte(`{"request":` + requestNode.Raw + `}`), true
|
||||
}
|
||||
prefix := "request."
|
||||
contents := doc.Get("request.contents")
|
||||
if !contents.Exists() {
|
||||
return nil, false
|
||||
prefix = ""
|
||||
contents = doc.Get("contents")
|
||||
if !contents.Exists() {
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString(`{"request":{`)
|
||||
@@ -529,10 +662,10 @@ func antigravityReplayRequestEnvelopeFromDoc(doc *ojson.Doc) ([]byte, bool) {
|
||||
key string
|
||||
path string
|
||||
}{
|
||||
{key: "systemInstruction", path: "request.systemInstruction"},
|
||||
{key: "tools", path: "request.tools"},
|
||||
{key: "toolConfig", path: "request.toolConfig"},
|
||||
{key: "contents", path: "request.contents"},
|
||||
{key: "systemInstruction", path: prefix + "systemInstruction"},
|
||||
{key: "tools", path: prefix + "tools"},
|
||||
{key: "toolConfig", path: prefix + "toolConfig"},
|
||||
{key: "contents", path: prefix + "contents"},
|
||||
} {
|
||||
val := doc.Get(field.path)
|
||||
if !val.Exists() {
|
||||
@@ -2000,14 +2133,22 @@ func antigravityFunctionCallMatchesReplayItem(functionCall, itemResult ojson.Res
|
||||
}
|
||||
|
||||
func antigravityPayloadHasClaudeToolProvenanceID(payload []byte) bool {
|
||||
return antigravityContentsHasClaudeToolProvenanceID(util.GetGJSONBytesNoCopy(payload, "request.contents"))
|
||||
contents := util.GetGJSONBytesNoCopy(payload, "request.contents")
|
||||
if !contents.IsArray() {
|
||||
contents = util.GetGJSONBytesNoCopy(payload, "contents")
|
||||
}
|
||||
return antigravityContentsHasClaudeToolProvenanceID(contents)
|
||||
}
|
||||
|
||||
func antigravityDocHasClaudeToolProvenanceID(doc *ojson.Doc) bool {
|
||||
if doc == nil {
|
||||
return false
|
||||
}
|
||||
return antigravityContentsHasClaudeToolProvenanceID(doc.Get("request.contents"))
|
||||
contents := doc.Get("request.contents")
|
||||
if !contents.IsArray() {
|
||||
contents = doc.Get("contents")
|
||||
}
|
||||
return antigravityContentsHasClaudeToolProvenanceID(contents)
|
||||
}
|
||||
|
||||
func antigravityContentsHasClaudeToolProvenanceID(contents ojson.Result) bool {
|
||||
@@ -2556,6 +2697,12 @@ type antigravityPendingThoughtSignature struct {
|
||||
targetKind string
|
||||
}
|
||||
|
||||
type antigravityStreamingReplayToolCall struct {
|
||||
call *translatorcommon.VertexStreamingToolCall
|
||||
partIndex int
|
||||
signature string
|
||||
}
|
||||
|
||||
type antigravityReasoningReplayAccumulator struct {
|
||||
scope antigravityReasoningReplayScope
|
||||
responseContextHash string
|
||||
@@ -2572,6 +2719,8 @@ type antigravityReasoningReplayAccumulator struct {
|
||||
thoughtPartIndex int
|
||||
lastResponseKind string
|
||||
pendingSignatures []antigravityPendingThoughtSignature
|
||||
activeStreamingFC *antigravityStreamingReplayToolCall
|
||||
hasIncompleteFC bool
|
||||
itemBytes int
|
||||
overflow bool
|
||||
terminal bool
|
||||
@@ -2702,9 +2851,20 @@ func (a *antigravityReasoningReplayAccumulator) appendItem(item []byte) {
|
||||
a.itemBytes += len(item)
|
||||
}
|
||||
|
||||
func (a *antigravityReasoningReplayAccumulator) attachDetachedSignatureToLastFunctionCall(signature string) {
|
||||
func (a *antigravityReasoningReplayAccumulator) attachDetachedSignatureToLastFunctionCall(signature string) bool {
|
||||
if a == nil || signature == "" {
|
||||
return
|
||||
return false
|
||||
}
|
||||
if a.activeStreamingFC != nil {
|
||||
if a.activeStreamingFC.signature != "" {
|
||||
return false
|
||||
}
|
||||
if a.itemBytes+len(signature) > internalcache.AntigravityReasoningReplayCacheMaxBytesPerEntry {
|
||||
a.overflow = true
|
||||
return false
|
||||
}
|
||||
a.activeStreamingFC.signature = signature
|
||||
return true
|
||||
}
|
||||
for itemIndex := len(a.items) - 1; itemIndex >= 0; itemIndex-- {
|
||||
item := util.ParseGJSONBytesNoCopy(a.items[itemIndex])
|
||||
@@ -2712,21 +2872,22 @@ func (a *antigravityReasoningReplayAccumulator) attachDetachedSignatureToLastFun
|
||||
continue
|
||||
}
|
||||
if strings.TrimSpace(item.Get("thoughtSignature").String()) != "" {
|
||||
return
|
||||
return false
|
||||
}
|
||||
updated, errSet := ojson.SetBytes(a.items[itemIndex], "thoughtSignature", signature)
|
||||
if errSet != nil {
|
||||
return
|
||||
return false
|
||||
}
|
||||
delta := len(updated) - len(a.items[itemIndex])
|
||||
if a.itemBytes+delta > internalcache.AntigravityReasoningReplayCacheMaxBytesPerEntry {
|
||||
a.overflow = true
|
||||
return
|
||||
return false
|
||||
}
|
||||
a.items[itemIndex] = updated
|
||||
a.itemBytes += delta
|
||||
return
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (a *antigravityReasoningReplayAccumulator) ObserveSSELine(line []byte) {
|
||||
@@ -2787,56 +2948,17 @@ func (a *antigravityReasoningReplayAccumulator) observeResponsePayload(payload [
|
||||
signature = ""
|
||||
}
|
||||
if fc := part.Get("functionCall"); fc.Exists() {
|
||||
if a.lastResponseKind == "text" || a.lastResponseKind == "thought" {
|
||||
a.flushPendingThoughtSignaturesForKind(a.lastResponseKind)
|
||||
}
|
||||
if signature != "" {
|
||||
remainingPending := a.pendingSignatures[:0]
|
||||
for _, pending := range a.pendingSignatures {
|
||||
if pending.targetKind != "" {
|
||||
remainingPending = append(remainingPending, pending)
|
||||
}
|
||||
}
|
||||
a.pendingSignatures = remainingPending
|
||||
}
|
||||
if signature == "" {
|
||||
for pendingIndex := len(a.pendingSignatures) - 1; pendingIndex >= 0; pendingIndex-- {
|
||||
if a.pendingSignatures[pendingIndex].targetKind == "" {
|
||||
signature = a.pendingSignatures[pendingIndex].signature
|
||||
a.pendingSignatures = append(a.pendingSignatures[:pendingIndex], a.pendingSignatures[pendingIndex+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if signature == "" {
|
||||
signature = a.claimPrecedingTurnThoughtSignature()
|
||||
}
|
||||
keys := antigravityReplayToolCallKeysFromPart(fc)
|
||||
for _, key := range keys {
|
||||
dedupeKey := key + "\x00" + signature
|
||||
if signature == "" {
|
||||
dedupeKey = fmt.Sprintf("%s\x00part:%d", key, pi)
|
||||
}
|
||||
if a.seenFC[dedupeKey] {
|
||||
return true
|
||||
}
|
||||
a.seenFC[dedupeKey] = true
|
||||
}
|
||||
occurrenceKey := antigravityFunctionCallKey(fc.Get("name").String(), fc.Get("args").Raw, "")
|
||||
occurrence := a.functionCallOccurrences[occurrenceKey]
|
||||
if occurrenceKey != "" {
|
||||
a.functionCallOccurrences[occurrenceKey] = occurrence + 1
|
||||
}
|
||||
item := buildAntigravityFunctionCallPartItem(a.contentIndex, pi, occurrence, fc, signature)
|
||||
if len(item) > 0 {
|
||||
a.appendItem(antigravitySetReplayItemContextHashValue(item, a.responseContextHash))
|
||||
if signature != "" {
|
||||
a.seenSignatures[signature] = true
|
||||
}
|
||||
}
|
||||
a.lastResponseKind = "function_call"
|
||||
a.observeFunctionCallPart(pi, fc, signature)
|
||||
return true
|
||||
}
|
||||
if a.activeStreamingFC != nil {
|
||||
text := part.Get("text")
|
||||
hasSemanticText := text.Exists() && text.String() != ""
|
||||
if hasSemanticText {
|
||||
a.hasIncompleteFC = true
|
||||
a.activeStreamingFC = nil
|
||||
}
|
||||
}
|
||||
|
||||
targetKind := ""
|
||||
if part.Get("thought").Bool() {
|
||||
@@ -2846,8 +2968,7 @@ func (a *antigravityReasoningReplayAccumulator) observeResponsePayload(payload [
|
||||
hasSemanticText := text.Exists() && text.String() != ""
|
||||
signatureOnly := signature != "" && !hasSemanticText
|
||||
if signatureOnly && a.lastResponseKind == "function_call" {
|
||||
if !a.seenSignatures[signature] {
|
||||
a.attachDetachedSignatureToLastFunctionCall(signature)
|
||||
if !a.seenSignatures[signature] && a.attachDetachedSignatureToLastFunctionCall(signature) {
|
||||
a.seenSignatures[signature] = true
|
||||
}
|
||||
return true
|
||||
@@ -3035,15 +3156,163 @@ func (a *antigravityReasoningReplayAccumulator) appendPendingThoughtSignatures()
|
||||
a.pendingSignatures = nil
|
||||
}
|
||||
|
||||
func (a *antigravityReasoningReplayAccumulator) resolveFunctionCallSignature(signature string) string {
|
||||
if a.lastResponseKind == "text" || a.lastResponseKind == "thought" {
|
||||
a.flushPendingThoughtSignaturesForKind(a.lastResponseKind)
|
||||
}
|
||||
if signature != "" {
|
||||
remainingPending := a.pendingSignatures[:0]
|
||||
for _, pending := range a.pendingSignatures {
|
||||
if pending.targetKind != "" {
|
||||
remainingPending = append(remainingPending, pending)
|
||||
}
|
||||
}
|
||||
a.pendingSignatures = remainingPending
|
||||
}
|
||||
if signature == "" {
|
||||
for pendingIndex := len(a.pendingSignatures) - 1; pendingIndex >= 0; pendingIndex-- {
|
||||
if a.pendingSignatures[pendingIndex].targetKind == "" {
|
||||
signature = a.pendingSignatures[pendingIndex].signature
|
||||
a.pendingSignatures = append(a.pendingSignatures[:pendingIndex], a.pendingSignatures[pendingIndex+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if signature == "" {
|
||||
signature = a.claimPrecedingTurnThoughtSignature()
|
||||
}
|
||||
return signature
|
||||
}
|
||||
|
||||
func (a *antigravityReasoningReplayAccumulator) recordCompletedFunctionCall(pi int, fc ojson.Result, signature string) {
|
||||
keys := antigravityReplayToolCallKeysFromPart(fc)
|
||||
for _, key := range keys {
|
||||
dedupeKey := key + "\x00" + signature
|
||||
if signature == "" {
|
||||
dedupeKey = fmt.Sprintf("%s\x00part:%d", key, pi)
|
||||
}
|
||||
if a.seenFC[dedupeKey] {
|
||||
return
|
||||
}
|
||||
a.seenFC[dedupeKey] = true
|
||||
}
|
||||
occurrenceKey := antigravityFunctionCallKey(fc.Get("name").String(), fc.Get("args").Raw, "")
|
||||
occurrence := a.functionCallOccurrences[occurrenceKey]
|
||||
if occurrenceKey != "" {
|
||||
a.functionCallOccurrences[occurrenceKey] = occurrence + 1
|
||||
}
|
||||
item := buildAntigravityFunctionCallPartItem(a.contentIndex, pi, occurrence, fc, signature)
|
||||
if len(item) > 0 {
|
||||
a.appendItem(antigravitySetReplayItemContextHashValue(item, a.responseContextHash))
|
||||
if signature != "" {
|
||||
a.seenSignatures[signature] = true
|
||||
}
|
||||
}
|
||||
a.lastResponseKind = "function_call"
|
||||
}
|
||||
|
||||
func (a *antigravityReasoningReplayAccumulator) flushCompletedStreamingFunctionCall(active *antigravityStreamingReplayToolCall) {
|
||||
if a == nil || active == nil || active.call == nil || active.call.Assembler == nil {
|
||||
return
|
||||
}
|
||||
if active.call.Assembler.Err() != nil {
|
||||
a.hasIncompleteFC = true
|
||||
return
|
||||
}
|
||||
argsRaw := strings.TrimSpace(active.call.Assembler.FullArguments())
|
||||
if argsRaw == "" {
|
||||
argsRaw = "{}"
|
||||
}
|
||||
if !ojson.Valid(argsRaw) {
|
||||
a.hasIncompleteFC = true
|
||||
return
|
||||
}
|
||||
fcDoc := ojson.NewDocString(`{"name":""}`)
|
||||
_ = fcDoc.Set("name", active.call.RawName)
|
||||
if active.call.UpstreamID != "" {
|
||||
_ = fcDoc.Set("id", active.call.UpstreamID)
|
||||
}
|
||||
_ = fcDoc.SetRaw("args", argsRaw)
|
||||
a.recordCompletedFunctionCall(active.partIndex, ojson.ParseBytes(fcDoc.Bytes()), active.signature)
|
||||
}
|
||||
|
||||
func (a *antigravityReasoningReplayAccumulator) observeFunctionCallPart(pi int, fc ojson.Result, signature string) {
|
||||
if a == nil {
|
||||
return
|
||||
}
|
||||
if active := a.activeStreamingFC; active != nil && active.call != nil && active.call.CanContinueWith(fc) {
|
||||
if signature != "" && active.signature == "" {
|
||||
active.signature = signature
|
||||
}
|
||||
if upstreamID := strings.TrimSpace(fc.Get("id").String()); upstreamID != "" && active.call.UpstreamID == "" {
|
||||
active.call.UpstreamID = strings.Clone(upstreamID)
|
||||
}
|
||||
_, done := active.call.Assembler.PushFunctionCall(fc)
|
||||
if active.call.Assembler.Err() != nil {
|
||||
a.hasIncompleteFC = true
|
||||
a.activeStreamingFC = nil
|
||||
return
|
||||
}
|
||||
if done {
|
||||
a.activeStreamingFC = nil
|
||||
a.flushCompletedStreamingFunctionCall(active)
|
||||
}
|
||||
a.lastResponseKind = "function_call"
|
||||
return
|
||||
}
|
||||
if translatorcommon.IsVertexStreamingFunctionCall(fc) {
|
||||
if a.activeStreamingFC != nil {
|
||||
a.hasIncompleteFC = true
|
||||
a.activeStreamingFC = nil
|
||||
}
|
||||
rawName := strings.Clone(strings.TrimSpace(fc.Get("name").String()))
|
||||
if rawName == "" {
|
||||
a.hasIncompleteFC = true
|
||||
return
|
||||
}
|
||||
upstreamID := strings.Clone(strings.TrimSpace(fc.Get("id").String()))
|
||||
resolvedSig := a.resolveFunctionCallSignature(signature)
|
||||
call := translatorcommon.NewVertexStreamingToolCall(rawName, rawName, upstreamID, upstreamID, 0)
|
||||
_, done := call.Assembler.PushFunctionCall(fc)
|
||||
if call.Assembler.Err() != nil {
|
||||
a.hasIncompleteFC = true
|
||||
return
|
||||
}
|
||||
active := &antigravityStreamingReplayToolCall{
|
||||
call: call,
|
||||
partIndex: pi,
|
||||
signature: resolvedSig,
|
||||
}
|
||||
if done {
|
||||
a.flushCompletedStreamingFunctionCall(active)
|
||||
} else {
|
||||
a.activeStreamingFC = active
|
||||
}
|
||||
a.lastResponseKind = "function_call"
|
||||
return
|
||||
}
|
||||
if a.activeStreamingFC != nil {
|
||||
a.hasIncompleteFC = true
|
||||
a.activeStreamingFC = nil
|
||||
}
|
||||
rawName := strings.TrimSpace(fc.Get("name").String())
|
||||
if rawName == "" && !fc.Get("args").Exists() {
|
||||
return
|
||||
}
|
||||
resolvedSig := a.resolveFunctionCallSignature(signature)
|
||||
a.recordCompletedFunctionCall(pi, fc, resolvedSig)
|
||||
}
|
||||
|
||||
func (a *antigravityReasoningReplayAccumulator) Commit(ctx context.Context) {
|
||||
if a == nil || !a.scope.valid() {
|
||||
return
|
||||
}
|
||||
log.Debugf("antigravity replay: accumulator commit terminal=%t overflow=%t items=%d (session=%s)",
|
||||
a.terminal, a.overflow, len(a.items), antigravityReplayLogKey(a.scope.sessionKey))
|
||||
if !a.terminal {
|
||||
// No terminal finishReason means the stream never completed, so this turn
|
||||
// contributes nothing to the ledger and its tool IDs become unresolvable.
|
||||
if !a.terminal || a.activeStreamingFC != nil || a.hasIncompleteFC {
|
||||
// No terminal finishReason (or an unclosed/corrupted streaming tool call)
|
||||
// means the stream never completed cleanly, so this turn contributes
|
||||
// nothing to the ledger and its tool IDs become unresolvable.
|
||||
return
|
||||
}
|
||||
if a.overflow {
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
internalcache "github.com/router-for-me/CLIProxyAPI/v8/internal/cache"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
|
||||
homekv "github.com/router-for-me/CLIProxyAPI/v8/internal/home"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps"
|
||||
internalsignature "github.com/router-for-me/CLIProxyAPI/v8/internal/signature"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/util"
|
||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
@@ -2015,6 +2016,93 @@ func TestAntigravityReasoningReplaySkipsSymmetricGeminiAndInteractionsButKeepsAs
|
||||
t.Fatalf("expected asymmetric replay items to be persisted in KV, ok=%v len=%d", ok, len(items))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("top_level_gemini_doc_replays_asymmetric_and_skips_symmetric", func(t *testing.T) {
|
||||
internalcache.ClearAntigravityReasoningReplayCache()
|
||||
topLevelTurn1 := []byte(`{"contents":[{"role":"user","parts":[{"text":"Call get_weather for Beijing"}]}]}`)
|
||||
topLevelTurn2 := []byte(`{"contents":[{"role":"user","parts":[{"text":"Call get_weather for Beijing"}]},{"role":"model","parts":[{"thoughtSignature":"skip_thought_signature_validator","functionCall":{"name":"get_weather","args":{"city":"Beijing"},"id":"call_1"}}]},{"role":"user","parts":[{"functionResponse":{"name":"get_weather","id":"call_1","response":{"result":"sunny"}}}]}]}`)
|
||||
topLevelResponse := []byte(`{"candidates":[{"content":{"role":"model","parts":[{"thought":true,"text":""},{"thoughtSignature":"EmcKZQFpFH0THU+YNRY5uVwt/0bo2hFo1RmXsMhlX9FCg2n6GssBe5sMV37KB6OrgeKKEP9fxN261QX","functionCall":{"name":"get_weather","args":{"city":"Beijing"},"id":"call_1"}}]},"finishReason":"STOP"}]}`)
|
||||
|
||||
symDoc := ojson.NewDoc(topLevelTurn1)
|
||||
symScope, _, _, errSym := prepareGeminiTopLevelReasoningReplayPayloadDoc(
|
||||
context.Background(),
|
||||
model,
|
||||
cliproxyexecutor.Request{Model: model, Payload: topLevelTurn1},
|
||||
cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatGemini, ResponseFormat: sdktranslator.FormatGemini},
|
||||
symDoc,
|
||||
)
|
||||
if errSym != nil {
|
||||
t.Fatalf("symmetric top-level prepare error: %v", errSym)
|
||||
}
|
||||
if symScope.valid() {
|
||||
t.Fatalf("symmetric top-level Gemini must not allocate replay scope, got %+v", symScope)
|
||||
}
|
||||
|
||||
headers := make(http.Header)
|
||||
headers.Set("Session-Id", "sess-non-ag-gemini")
|
||||
asymOpts := cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FormatOpenAIResponse,
|
||||
ResponseFormat: sdktranslator.FormatOpenAIResponse,
|
||||
Headers: headers,
|
||||
}
|
||||
doc1 := ojson.NewDoc(topLevelTurn1)
|
||||
scope1, replayReq1, _, err1 := prepareGeminiTopLevelReasoningReplayPayloadDoc(
|
||||
context.Background(),
|
||||
model,
|
||||
cliproxyexecutor.Request{Model: model, Payload: topLevelTurn1},
|
||||
asymOpts,
|
||||
doc1,
|
||||
)
|
||||
if err1 != nil || !scope1.valid() {
|
||||
t.Fatalf("asymmetric top-level prepare turn 1: scope=%+v err=%v", scope1, err1)
|
||||
}
|
||||
cacheAntigravityReasoningReplayFromResponse(context.Background(), scope1, replayReq1, topLevelResponse)
|
||||
|
||||
doc2 := ojson.NewDoc(topLevelTurn2)
|
||||
scope2, replayReq2, changed2, err2 := prepareGeminiTopLevelReasoningReplayPayloadDoc(
|
||||
context.Background(),
|
||||
model,
|
||||
cliproxyexecutor.Request{Model: model, Payload: topLevelTurn2},
|
||||
asymOpts,
|
||||
doc2,
|
||||
)
|
||||
if err2 != nil || !changed2 {
|
||||
t.Fatalf("asymmetric top-level prepare turn 2: changed=%v err=%v", changed2, err2)
|
||||
}
|
||||
if gotSig := doc2.Get("contents.1.parts.0.thoughtSignature").String(); gotSig != "EmcKZQFpFH0THU+YNRY5uVwt/0bo2hFo1RmXsMhlX9FCg2n6GssBe5sMV37KB6OrgeKKEP9fxN261QX" {
|
||||
t.Fatalf("replayed signature = %q, want native thoughtSignature", gotSig)
|
||||
}
|
||||
if gotRole := doc2.Get("contents.2.role").String(); gotRole != "user" {
|
||||
t.Fatalf("non-Antigravity functionResponse role = %q, want user", gotRole)
|
||||
}
|
||||
|
||||
// Turn 2 simulates Vertex stripping wire-only functionCall.id/functionResponse.id
|
||||
// after prepareGeminiTopLevelReasoningReplayPayloadDoc; the captured replayRequestPayload
|
||||
// from prepareGeminiTopLevelReasoningReplayPayloadDoc must still keep canonical call_id
|
||||
// context so Turn 3 matches both call_1 and call_2 signatures.
|
||||
helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc2, asymOpts.SourceFormat.String())
|
||||
topLevelResponse2 := []byte(`{"candidates":[{"content":{"role":"model","parts":[{"thought":true,"text":""},{"thoughtSignature":"EsMTCsATARFNMg/XNVix5lDpkKaHR7Xg","functionCall":{"name":"get_weather","args":{"city":"Shanghai"},"id":"call_2"}}]},"finishReason":"STOP"}]}`)
|
||||
cacheAntigravityReasoningReplayFromResponse(context.Background(), scope2, replayReq2, topLevelResponse2)
|
||||
|
||||
topLevelTurn3 := []byte(`{"contents":[{"role":"user","parts":[{"text":"Call get_weather for Beijing"}]},{"role":"model","parts":[{"thoughtSignature":"skip_thought_signature_validator","functionCall":{"name":"get_weather","args":{"city":"Beijing"},"id":"call_1"}}]},{"role":"user","parts":[{"functionResponse":{"name":"get_weather","id":"call_1","response":{"result":"sunny"}}}]},{"role":"model","parts":[{"thoughtSignature":"skip_thought_signature_validator","functionCall":{"name":"get_weather","args":{"city":"Shanghai"},"id":"call_2"}}]},{"role":"user","parts":[{"functionResponse":{"name":"get_weather","id":"call_2","response":{"result":"cloudy"}}}]}]}`)
|
||||
doc3 := ojson.NewDoc(topLevelTurn3)
|
||||
_, _, changed3, err3 := prepareGeminiTopLevelReasoningReplayPayloadDoc(
|
||||
context.Background(),
|
||||
model,
|
||||
cliproxyexecutor.Request{Model: model, Payload: topLevelTurn3},
|
||||
asymOpts,
|
||||
doc3,
|
||||
)
|
||||
if err3 != nil || !changed3 {
|
||||
t.Fatalf("asymmetric top-level prepare turn 3: changed=%v err=%v", changed3, err3)
|
||||
}
|
||||
if gotSig1 := doc3.Get("contents.1.parts.0.thoughtSignature").String(); gotSig1 != "EmcKZQFpFH0THU+YNRY5uVwt/0bo2hFo1RmXsMhlX9FCg2n6GssBe5sMV37KB6OrgeKKEP9fxN261QX" {
|
||||
t.Fatalf("turn 3 call_1 signature = %q, want preserved signature", gotSig1)
|
||||
}
|
||||
if gotSig2 := doc3.Get("contents.3.parts.0.thoughtSignature").String(); gotSig2 != "EsMTCsATARFNMg/XNVix5lDpkKaHR7Xg" {
|
||||
t.Fatalf("turn 3 call_2 signature = %q, want second signature", gotSig2)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestPrepareAntigravityGeminiReasoningReplayDegradesWhenReplayBreaksPairing(t *testing.T) {
|
||||
@@ -2044,3 +2132,174 @@ func TestPrepareAntigravityGeminiReasoningReplayDegradesWhenReplayBreaksPairing(
|
||||
t.Fatalf("out = %s, want original payload %s", out, payload)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAntigravityStreamChunksHaveTerminalCommitEvent(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
chunks [][]byte
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "openai responses completed",
|
||||
chunks: [][]byte{[]byte("event: response.completed\ndata: {\"type\":\"response.completed\"}\n\n")},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "openai responses incomplete",
|
||||
chunks: [][]byte{[]byte("event: response.incomplete\ndata: {\"type\":\"response.incomplete\",\"response\":{\"incomplete_details\":{\"reason\":\"max_output_tokens\"}}}\n\n")},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "openai chat finish_reason stop",
|
||||
chunks: [][]byte{[]byte("data: {\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n")},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "openai chat null finish_reason",
|
||||
chunks: [][]byte{[]byte("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hi\"},\"finish_reason\":null}]}\n\n")},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "claude message_stop",
|
||||
chunks: [][]byte{[]byte("event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n")},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "bare done frame only",
|
||||
chunks: [][]byte{[]byte("data: [DONE]\n\n")},
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := antigravityStreamChunksHaveTerminalCommitEvent(tc.chunks); got != tc.want {
|
||||
t.Fatalf("antigravityStreamChunksHaveTerminalCommitEvent() = %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAntigravityReasoningReplayAccumulatorAssemblesVertexStreamingPartialArgs(t *testing.T) {
|
||||
internalcache.ClearAntigravityReasoningReplayCache()
|
||||
t.Cleanup(internalcache.ClearAntigravityReasoningReplayCache)
|
||||
|
||||
const model = "gemini-3.8-flash"
|
||||
asymOpts := cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FormatOpenAIResponse,
|
||||
ResponseFormat: sdktranslator.FormatOpenAIResponse,
|
||||
Headers: http.Header{"X-Session-Id": []string{"sess-vertex-partial-args"}},
|
||||
}
|
||||
|
||||
turn1 := []byte(`{"contents":[{"role":"user","parts":[{"text":"Weather in New York?"}]}]}`)
|
||||
doc1 := ojson.NewDoc(turn1)
|
||||
scope1, replayReq1, _, err1 := prepareGeminiTopLevelReasoningReplayPayloadDoc(
|
||||
context.Background(),
|
||||
model,
|
||||
cliproxyexecutor.Request{Model: model, Payload: turn1},
|
||||
asymOpts,
|
||||
doc1,
|
||||
)
|
||||
if err1 != nil || !scope1.valid() {
|
||||
t.Fatalf("prepare turn 1: scope=%+v err=%v", scope1, err1)
|
||||
}
|
||||
|
||||
acc := newAntigravityReasoningReplayAccumulator(scope1, replayReq1)
|
||||
acc.ObserveSSELine([]byte(`data: {"candidates":[{"index":0,"content":{"role":"model","parts":[{"functionCall":{"id":"call_ny","name":"get_weather","partialArgs":[{"jsonPath":"$.city","stringValue":"New ","willContinue":true}],"willContinue":true}}]}}]}`))
|
||||
acc.ObserveSSELine([]byte(`data: {"candidates":[{"index":0,"content":{"role":"model","parts":[{"text":"","thoughtSignature":"EmcKZQFpFH0THU+YNRY5uVwt/0bo2hFo1RmXsMhlX9FCg2n6GssBe5sMV37KB6OrgeKKEP9fxN261QX"}]}}]}`))
|
||||
acc.ObserveSSELine([]byte(`data: {"candidates":[{"index":0,"content":{"role":"model","parts":[{"functionCall":{"partialArgs":[{"jsonPath":"$.city","stringValue":"York"}],"willContinue":false}}]},"finishReason":"STOP"}]}`))
|
||||
acc.Commit(context.Background())
|
||||
|
||||
turn2 := []byte(`{"contents":[{"role":"user","parts":[{"text":"Weather in New York?"}]},{"role":"model","parts":[{"thoughtSignature":"skip_thought_signature_validator","functionCall":{"name":"get_weather","args":{"city":"New York"},"id":"call_ny"}}]},{"role":"user","parts":[{"functionResponse":{"name":"get_weather","id":"call_ny","response":{"result":"sunny"}}}]}]}`)
|
||||
doc2 := ojson.NewDoc(turn2)
|
||||
_, _, changed2, err2 := prepareGeminiTopLevelReasoningReplayPayloadDoc(
|
||||
context.Background(),
|
||||
model,
|
||||
cliproxyexecutor.Request{Model: model, Payload: turn2},
|
||||
asymOpts,
|
||||
doc2,
|
||||
)
|
||||
if err2 != nil || !changed2 {
|
||||
t.Fatalf("prepare turn 2 after partialArgs stream: changed=%v err=%v", changed2, err2)
|
||||
}
|
||||
if gotSig := doc2.Get("contents.1.parts.0.thoughtSignature").String(); gotSig != "EmcKZQFpFH0THU+YNRY5uVwt/0bo2hFo1RmXsMhlX9FCg2n6GssBe5sMV37KB6OrgeKKEP9fxN261QX" {
|
||||
t.Fatalf("replayed signature = %q, want native signature from partialArgs stream", gotSig)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNonVertexSanitizesReplayedVertexBareTinkSignature(t *testing.T) {
|
||||
// Verify that a Vertex bare-Tink thoughtSignature replayed from shared KV is
|
||||
// downgraded to skip_thought_signature_validator when post-replay non-Vertex
|
||||
// sanitization runs (as in GeminiExecutor and AIStudioExecutor).
|
||||
const vertexBareTinkSig = "AY89a18f0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
internalcache.ClearAntigravityReasoningReplayCache()
|
||||
const model = "gemini-3.8-flash"
|
||||
asymOpts := cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FormatOpenAIResponse,
|
||||
ResponseFormat: sdktranslator.FormatOpenAIResponse,
|
||||
Headers: http.Header{"X-Session-Id": []string{"sess-vertex-to-gemini"}},
|
||||
}
|
||||
turn1 := []byte(`{"contents":[{"role":"user","parts":[{"text":"Call get_weather"}]}]}`)
|
||||
doc1 := ojson.NewDoc(turn1)
|
||||
scope1, replayReq1, _, err1 := prepareGeminiTopLevelReasoningReplayPayloadDoc(
|
||||
context.Background(),
|
||||
model,
|
||||
cliproxyexecutor.Request{Model: model, Payload: turn1},
|
||||
asymOpts,
|
||||
doc1,
|
||||
)
|
||||
if err1 != nil || !scope1.valid() {
|
||||
t.Fatalf("prepare turn 1: scope=%+v err=%v", scope1, err1)
|
||||
}
|
||||
vertexResp := []byte(`{"candidates":[{"content":{"role":"model","parts":[{"thoughtSignature":"` + vertexBareTinkSig + `","functionCall":{"name":"get_weather","args":{"city":"Tokyo"},"id":"call_tink"}}]},"finishReason":"STOP"}]}`)
|
||||
cacheAntigravityReasoningReplayFromResponse(context.Background(), scope1, replayReq1, vertexResp)
|
||||
|
||||
turn2 := []byte(`{"contents":[{"role":"user","parts":[{"text":"Call get_weather"}]},{"role":"model","parts":[{"thoughtSignature":"skip_thought_signature_validator","functionCall":{"name":"get_weather","args":{"city":"Tokyo"},"id":"call_tink"}}]},{"role":"user","parts":[{"functionResponse":{"name":"get_weather","id":"call_tink","response":{"result":"clear"}}}]}]}`)
|
||||
nonVertexDoc := ojson.NewDoc(turn2)
|
||||
_, _, changed2, err2 := prepareGeminiTopLevelReasoningReplayPayloadDoc(
|
||||
context.Background(),
|
||||
model,
|
||||
cliproxyexecutor.Request{Model: model, Payload: turn2},
|
||||
asymOpts,
|
||||
nonVertexDoc,
|
||||
)
|
||||
if err2 != nil || !changed2 {
|
||||
t.Fatalf("prepare turn 2: changed=%v err=%v", changed2, err2)
|
||||
}
|
||||
internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(nonVertexDoc, "contents")
|
||||
if got := nonVertexDoc.Get("contents.1.parts.0.thoughtSignature").String(); got != "skip_thought_signature_validator" {
|
||||
t.Fatalf("non-Vertex post-replay sanitized signature = %q, want skip_thought_signature_validator", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCountTokensReasoningReplayPeeksWithoutReservingTombstone(t *testing.T) {
|
||||
internalcache.ClearAntigravityReasoningReplayCache()
|
||||
t.Cleanup(internalcache.ClearAntigravityReasoningReplayCache)
|
||||
|
||||
const model = "gemini-3.8-flash"
|
||||
opts := cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FormatOpenAIResponse,
|
||||
ResponseFormat: sdktranslator.FormatOpenAIResponse,
|
||||
Headers: http.Header{"X-Session-Id": []string{"sess-count-tokens-no-tombstone"}},
|
||||
}
|
||||
payload := []byte(`{"contents":[{"role":"user","parts":[{"text":"Count tokens only"}]}]}`)
|
||||
doc := ojson.NewDoc(payload)
|
||||
req := cliproxyexecutor.Request{
|
||||
Model: model,
|
||||
Payload: payload,
|
||||
Metadata: map[string]any{"action": "countTokens"},
|
||||
}
|
||||
scope, replayReq, changed, err := prepareGeminiTopLevelReasoningReplayPayloadDoc(context.Background(), model, req, opts, doc)
|
||||
if err != nil || changed || len(replayReq) != 0 {
|
||||
t.Fatalf("countTokens prepare = scope:%+v len(replayReq):%d changed:%v err:%v", scope, len(replayReq), changed, err)
|
||||
}
|
||||
// A subsequent real generation turn must still be able to cache best-effort
|
||||
// without being blocked by an unclosed countTokens reservation.
|
||||
item := []byte(`{"type":"thought_signature","contentIndex":0,"partIndex":0,"targetOccurrence":0,"thoughtSignature":"EmcKZQFpFH0THU+YNRY5uVwt/0bo2hFo1RmXsMhlX9FCg2n6GssBe5sMV37KB6OrgeKKEP9fxN261QX"}`)
|
||||
if !internalcache.CacheAntigravityReasoningReplayItems(model, scope.sessionKey, [][]byte{item}) {
|
||||
t.Fatal("failed to cache replay item after countTokens peek")
|
||||
}
|
||||
got, ok := internalcache.GetAntigravityReasoningReplayItems(model, scope.sessionKey)
|
||||
if !ok || len(got) != 1 {
|
||||
t.Fatalf("GetAntigravityReasoningReplayItems after countTokens peek = %v, ok=%v", got, ok)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,17 +33,17 @@ func claudeReplayTestAuth(baseURL string) *cliproxyauth.Auth {
|
||||
|
||||
func claudeReplayTestRequest(payload []byte, sessionID string, isCompat bool, source sdktranslator.Format) (cliproxyexecutor.Request, cliproxyexecutor.Options) {
|
||||
return cliproxyexecutor.Request{
|
||||
Model: "claude-synthetic-4772",
|
||||
Payload: payload,
|
||||
Metadata: map[string]any{
|
||||
claudeReplayResolvedModelInfoKey: ®istry.ModelInfo{IsCompat: isCompat},
|
||||
},
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: source,
|
||||
Metadata: map[string]any{
|
||||
cliproxyexecutor.ExecutionSessionMetadataKey: sessionID,
|
||||
},
|
||||
}
|
||||
Model: "claude-synthetic-4772",
|
||||
Payload: payload,
|
||||
Metadata: map[string]any{
|
||||
claudeReplayResolvedModelInfoKey: ®istry.ModelInfo{IsCompat: isCompat},
|
||||
},
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: source,
|
||||
Metadata: map[string]any{
|
||||
cliproxyexecutor.ExecutionSessionMetadataKey: sessionID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThinkingReplayEnabledRequiresCompatClaudeAPIKey(t *testing.T) {
|
||||
|
||||
@@ -46,12 +46,12 @@ func codexTestAuth(baseURL string) *cliproxyauth.Auth {
|
||||
|
||||
func codexTestRequest() (cliproxyexecutor.Request, cliproxyexecutor.Options) {
|
||||
return cliproxyexecutor.Request{
|
||||
Model: "gpt-5.6-terra",
|
||||
Payload: []byte(`{"model":"gpt-5.6-terra","input":"hello"}`),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FromString("openai-response"),
|
||||
Stream: true,
|
||||
}
|
||||
Model: "gpt-5.6-terra",
|
||||
Payload: []byte(`{"model":"gpt-5.6-terra","input":"hello"}`),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FromString("openai-response"),
|
||||
Stream: true,
|
||||
}
|
||||
}
|
||||
|
||||
// codexSSEServer streams the supplied event payloads as an HTTP 200 SSE response.
|
||||
@@ -92,11 +92,11 @@ func codexWebsocketServer(t *testing.T, frames ...string) *httptest.Server {
|
||||
|
||||
func codexWebsocketRequest() (cliproxyexecutor.Request, cliproxyexecutor.Options) {
|
||||
return cliproxyexecutor.Request{
|
||||
Model: "gpt-5.6-terra",
|
||||
Payload: []byte(`{"model":"gpt-5.6-terra","input":[{"type":"message","role":"user","content":"hello"}]}`),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FromString("openai-response"),
|
||||
}
|
||||
Model: "gpt-5.6-terra",
|
||||
Payload: []byte(`{"model":"gpt-5.6-terra","input":[{"type":"message","role":"user","content":"hello"}]}`),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FromString("openai-response"),
|
||||
}
|
||||
}
|
||||
|
||||
// drainChunks collects every payload and the first error from a stream result.
|
||||
|
||||
@@ -176,6 +176,19 @@ func (e *GeminiExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
|
||||
if action != "countTokens" {
|
||||
helps.EnsureGeminiTrailingUserContentDoc(doc, "contents")
|
||||
}
|
||||
replayScope := antigravityReasoningReplayScope{}
|
||||
var replayRequestPayload []byte
|
||||
if antigravityUsesReasoningReplayCache(baseModel) {
|
||||
var changedReplay bool
|
||||
var errReplay error
|
||||
replayScope, replayRequestPayload, changedReplay, errReplay = prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc)
|
||||
if errReplay != nil {
|
||||
return resp, errReplay
|
||||
}
|
||||
if changedReplay {
|
||||
internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
||||
}
|
||||
}
|
||||
baseURL := resolveGeminiBaseURL(auth)
|
||||
url := fmt.Sprintf("%s/%s/models/%s:%s", baseURL, glAPIVersion, baseModel, action)
|
||||
if opts.Alt != "" && action != "countTokens" {
|
||||
@@ -231,6 +244,9 @@ func (e *GeminiExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
|
||||
b, _ := io.ReadAll(httpResp.Body)
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, b)
|
||||
helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), b))
|
||||
if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, replayScope, httpResp.StatusCode, b); errClear != nil {
|
||||
logAntigravityReasoningReplayDegraded(replayScope, "invalidate", errClear)
|
||||
}
|
||||
err = statusErr{code: httpResp.StatusCode, msg: string(b)}
|
||||
return resp, err
|
||||
}
|
||||
@@ -240,6 +256,9 @@ func (e *GeminiExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
|
||||
return resp, err
|
||||
}
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, data)
|
||||
if action != "countTokens" {
|
||||
cacheAntigravityReasoningReplayFromResponse(ctx, replayScope, replayRequestPayload, data)
|
||||
}
|
||||
reporter.ObserveResponseModel(data)
|
||||
if blockMsg, blocked := geminiPromptBlockErrorMessage(data); blocked {
|
||||
err = statusErr{code: http.StatusBadGateway, msg: blockMsg}
|
||||
@@ -309,6 +328,19 @@ func (e *GeminiExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
|
||||
capGeminiMaxOutputTokensDoc(doc, baseModel)
|
||||
internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
||||
helps.EnsureGeminiBoundaryUserContentDoc(doc, "contents")
|
||||
replayScope := antigravityReasoningReplayScope{}
|
||||
var replayRequestPayload []byte
|
||||
if antigravityUsesReasoningReplayCache(baseModel) {
|
||||
var changedReplay bool
|
||||
var errReplay error
|
||||
replayScope, replayRequestPayload, changedReplay, errReplay = prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc)
|
||||
if errReplay != nil {
|
||||
return nil, errReplay
|
||||
}
|
||||
if changedReplay {
|
||||
internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
||||
}
|
||||
}
|
||||
|
||||
baseURL := resolveGeminiBaseURL(auth)
|
||||
url := fmt.Sprintf("%s/%s/models/%s:%s", baseURL, glAPIVersion, baseModel, "streamGenerateContent")
|
||||
@@ -365,9 +397,13 @@ func (e *GeminiExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
|
||||
if errClose := httpResp.Body.Close(); errClose != nil {
|
||||
log.Errorf("gemini executor: close response body error: %v", errClose)
|
||||
}
|
||||
if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, replayScope, httpResp.StatusCode, b); errClear != nil {
|
||||
logAntigravityReasoningReplayDegraded(replayScope, "invalidate", errClear)
|
||||
}
|
||||
err = statusErr{code: httpResp.StatusCode, msg: string(b)}
|
||||
return nil, err
|
||||
}
|
||||
replayAccumulator := newAntigravityReasoningReplayAccumulator(replayScope, replayRequestPayload)
|
||||
out := make(chan cliproxyexecutor.StreamChunk, 1)
|
||||
go func() {
|
||||
defer close(out)
|
||||
@@ -396,6 +432,13 @@ func (e *GeminiExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
|
||||
}()
|
||||
var param any
|
||||
var sawFinishReason bool
|
||||
var replayCommitted bool
|
||||
commitReplay := func() {
|
||||
if !replayCommitted && replayAccumulator != nil && replayAccumulator.terminal && streamErr == nil && helps.ApplyPatchTranslationError(param) == nil {
|
||||
replayAccumulator.Commit(ctx)
|
||||
replayCommitted = true
|
||||
}
|
||||
}
|
||||
var pendingFilterPayload []byte
|
||||
var pendingFilterMsg string
|
||||
helps.InitializeApplyPatchStream(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, ¶m)
|
||||
@@ -405,6 +448,9 @@ func (e *GeminiExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
|
||||
helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error {
|
||||
return statusErr{code: http.StatusBadGateway, msg: msg}
|
||||
})
|
||||
if replayAccumulator != nil && replayAccumulator.terminal && !replayCommitted && antigravityStreamChunksHaveTerminalCommitEvent(lines) {
|
||||
commitReplay()
|
||||
}
|
||||
for i := range lines {
|
||||
select {
|
||||
case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}:
|
||||
@@ -420,6 +466,9 @@ func (e *GeminiExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
|
||||
for scanner.Scan() {
|
||||
line := scanner.Bytes()
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, line)
|
||||
if replayAccumulator != nil {
|
||||
replayAccumulator.ObserveSSELine(line)
|
||||
}
|
||||
reporter.ObserveResponseModel(line)
|
||||
rawPayload := helps.JSONPayload(line)
|
||||
if len(rawPayload) > 0 && len(pendingFilterPayload) > 0 {
|
||||
@@ -517,6 +566,7 @@ func (e *GeminiExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
|
||||
helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error {
|
||||
return statusErr{code: http.StatusBadGateway, msg: msg}
|
||||
})
|
||||
commitReplay()
|
||||
for i := range lines {
|
||||
select {
|
||||
case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}:
|
||||
@@ -879,6 +929,15 @@ func (e *GeminiExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Aut
|
||||
helps.SetStringIfDifferentDoc(doc, "model", baseModel)
|
||||
internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
||||
helps.EnsureGeminiLeadingUserContentDoc(doc, "contents")
|
||||
if antigravityUsesReasoningReplayCache(baseModel) {
|
||||
changedReplay, errReplay := prepareGeminiTopLevelCountTokensReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc)
|
||||
if errReplay != nil {
|
||||
return cliproxyexecutor.Response{}, errReplay
|
||||
}
|
||||
if changedReplay {
|
||||
internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
||||
}
|
||||
}
|
||||
|
||||
baseURL := resolveGeminiBaseURL(auth)
|
||||
url := fmt.Sprintf("%s/%s/models/%s:%s", baseURL, glAPIVersion, baseModel, "countTokens")
|
||||
|
||||
@@ -456,7 +456,6 @@ func (e *GeminiVertexExecutor) executeWithServiceAccount(ctx context.Context, au
|
||||
|
||||
fixGeminiImageAspectRatioDoc(baseModel, doc)
|
||||
helps.SetStringIfDifferentDoc(doc, "model", baseModel)
|
||||
helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String())
|
||||
internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
||||
}
|
||||
|
||||
@@ -471,6 +470,18 @@ func (e *GeminiVertexExecutor) executeWithServiceAccount(ctx context.Context, au
|
||||
helps.DropGeminiTrailingThoughtOnlyContentDoc(doc, "contents")
|
||||
helps.EnsureGeminiTrailingUserContentDoc(doc, "contents")
|
||||
}
|
||||
replayScope := antigravityReasoningReplayScope{}
|
||||
var replayRequestPayload []byte
|
||||
if !isImagenModel(baseModel) && antigravityUsesReasoningReplayCache(baseModel) {
|
||||
var errReplay error
|
||||
replayScope, replayRequestPayload, _, errReplay = prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc)
|
||||
if errReplay != nil {
|
||||
return resp, errReplay
|
||||
}
|
||||
}
|
||||
if !isImagenModel(baseModel) {
|
||||
helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, opts.SourceFormat.String())
|
||||
}
|
||||
baseURL := vertexBaseURL(location)
|
||||
url := fmt.Sprintf("%s/%s/projects/%s/locations/%s/publishers/google/models/%s:%s", baseURL, vertexAPIVersion, projectID, location, baseModel, action)
|
||||
if opts.Alt != "" && action != "countTokens" {
|
||||
@@ -534,6 +545,9 @@ func (e *GeminiVertexExecutor) executeWithServiceAccount(ctx context.Context, au
|
||||
b, _ := io.ReadAll(httpResp.Body)
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, b)
|
||||
helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), b))
|
||||
if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, replayScope, httpResp.StatusCode, b); errClear != nil {
|
||||
logAntigravityReasoningReplayDegraded(replayScope, "invalidate", errClear)
|
||||
}
|
||||
err = statusErr{code: httpResp.StatusCode, msg: string(b)}
|
||||
return resp, err
|
||||
}
|
||||
@@ -543,6 +557,9 @@ func (e *GeminiVertexExecutor) executeWithServiceAccount(ctx context.Context, au
|
||||
return resp, errRead
|
||||
}
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, data)
|
||||
if action != "countTokens" && !isImagenModel(baseModel) {
|
||||
cacheAntigravityReasoningReplayFromResponse(ctx, replayScope, replayRequestPayload, data)
|
||||
}
|
||||
reporter.ObserveResponseModel(data)
|
||||
|
||||
// For Imagen models, convert response to Gemini format before translation
|
||||
@@ -612,7 +629,6 @@ func (e *GeminiVertexExecutor) executeWithAPIKey(ctx context.Context, auth *clip
|
||||
requestedModel := helps.PayloadRequestedModel(opts, req.Model)
|
||||
requestPath := helps.PayloadRequestPath(opts)
|
||||
helps.SetStringIfDifferentDoc(doc, "model", baseModel)
|
||||
helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String())
|
||||
internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
||||
|
||||
action := getVertexAction(baseModel, false)
|
||||
@@ -626,6 +642,16 @@ func (e *GeminiVertexExecutor) executeWithAPIKey(ctx context.Context, auth *clip
|
||||
helps.DropGeminiTrailingThoughtOnlyContentDoc(doc, "contents")
|
||||
helps.EnsureGeminiTrailingUserContentDoc(doc, "contents")
|
||||
}
|
||||
replayScope := antigravityReasoningReplayScope{}
|
||||
var replayRequestPayload []byte
|
||||
if !isImagenModel(baseModel) && antigravityUsesReasoningReplayCache(baseModel) {
|
||||
var errReplay error
|
||||
replayScope, replayRequestPayload, _, errReplay = prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc)
|
||||
if errReplay != nil {
|
||||
return resp, errReplay
|
||||
}
|
||||
}
|
||||
helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String())
|
||||
|
||||
// For API key auth, use simpler URL format without project/location.
|
||||
// The helper honors base URLs that already encode "/publishers/..." so
|
||||
@@ -690,6 +716,9 @@ func (e *GeminiVertexExecutor) executeWithAPIKey(ctx context.Context, auth *clip
|
||||
b, _ := io.ReadAll(httpResp.Body)
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, b)
|
||||
helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), b))
|
||||
if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, replayScope, httpResp.StatusCode, b); errClear != nil {
|
||||
logAntigravityReasoningReplayDegraded(replayScope, "invalidate", errClear)
|
||||
}
|
||||
err = statusErr{code: httpResp.StatusCode, msg: string(b)}
|
||||
return resp, err
|
||||
}
|
||||
@@ -699,6 +728,9 @@ func (e *GeminiVertexExecutor) executeWithAPIKey(ctx context.Context, auth *clip
|
||||
return resp, errRead
|
||||
}
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, data)
|
||||
if action != "countTokens" && !isImagenModel(baseModel) {
|
||||
cacheAntigravityReasoningReplayFromResponse(ctx, replayScope, replayRequestPayload, data)
|
||||
}
|
||||
reporter.ObserveResponseModel(data)
|
||||
if blockMsg, blocked := geminiPromptBlockErrorMessage(data); blocked {
|
||||
err = statusErr{code: http.StatusBadGateway, msg: blockMsg}
|
||||
@@ -757,12 +789,21 @@ func (e *GeminiVertexExecutor) executeStreamWithServiceAccount(ctx context.Conte
|
||||
requestedModel := helps.PayloadRequestedModel(opts, req.Model)
|
||||
requestPath := helps.PayloadRequestPath(opts)
|
||||
helps.SetStringIfDifferentDoc(doc, "model", baseModel)
|
||||
helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String())
|
||||
internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
||||
|
||||
action := getVertexAction(baseModel, true)
|
||||
helps.DropGeminiTrailingThoughtOnlyContentDoc(doc, "contents")
|
||||
helps.EnsureGeminiBoundaryUserContentDoc(doc, "contents")
|
||||
replayScope := antigravityReasoningReplayScope{}
|
||||
var replayRequestPayload []byte
|
||||
if !isImagenModel(baseModel) && antigravityUsesReasoningReplayCache(baseModel) {
|
||||
var errReplay error
|
||||
replayScope, replayRequestPayload, _, errReplay = prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc)
|
||||
if errReplay != nil {
|
||||
return nil, errReplay
|
||||
}
|
||||
}
|
||||
helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String())
|
||||
baseURL := vertexBaseURL(location)
|
||||
url := fmt.Sprintf("%s/%s/projects/%s/locations/%s/publishers/google/models/%s:%s", baseURL, vertexAPIVersion, projectID, location, baseModel, action)
|
||||
// Imagen models don't support streaming, skip SSE params
|
||||
@@ -829,9 +870,13 @@ func (e *GeminiVertexExecutor) executeStreamWithServiceAccount(ctx context.Conte
|
||||
if errClose := httpResp.Body.Close(); errClose != nil {
|
||||
log.Errorf("vertex executor: close response body error: %v", errClose)
|
||||
}
|
||||
if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, replayScope, httpResp.StatusCode, b); errClear != nil {
|
||||
logAntigravityReasoningReplayDegraded(replayScope, "invalidate", errClear)
|
||||
}
|
||||
return nil, statusErr{code: httpResp.StatusCode, msg: string(b)}
|
||||
}
|
||||
|
||||
replayAccumulator := newAntigravityReasoningReplayAccumulator(replayScope, replayRequestPayload)
|
||||
out := make(chan cliproxyexecutor.StreamChunk, 1)
|
||||
go func() {
|
||||
defer close(out)
|
||||
@@ -853,9 +898,16 @@ func (e *GeminiVertexExecutor) executeStreamWithServiceAccount(ctx context.Conte
|
||||
}()
|
||||
var terminalDelivered bool
|
||||
var sawFinishReason bool
|
||||
var replayCommitted bool
|
||||
var pendingFilterLine []byte
|
||||
var pendingFilterMsg string
|
||||
var param any
|
||||
commitReplay := func() {
|
||||
if !replayCommitted && replayAccumulator != nil && replayAccumulator.terminal && streamErr == nil && helps.ApplyPatchTranslationError(param) == nil {
|
||||
replayAccumulator.Commit(ctx)
|
||||
replayCommitted = true
|
||||
}
|
||||
}
|
||||
helps.InitializeApplyPatchStream(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, ¶m)
|
||||
emitTranslatedLine := func(line []byte) bool {
|
||||
lines := helps.TranslateStreamWithClaudeInputTokens(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, bytes.Clone(line), ¶m, claudeInputTokens)
|
||||
@@ -863,6 +915,9 @@ func (e *GeminiVertexExecutor) executeStreamWithServiceAccount(ctx context.Conte
|
||||
helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error {
|
||||
return statusErr{code: http.StatusBadGateway, msg: msg}
|
||||
})
|
||||
if replayAccumulator != nil && replayAccumulator.terminal && !replayCommitted && antigravityStreamChunksHaveTerminalCommitEvent(lines) {
|
||||
commitReplay()
|
||||
}
|
||||
for i := range lines {
|
||||
select {
|
||||
case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}:
|
||||
@@ -878,6 +933,9 @@ func (e *GeminiVertexExecutor) executeStreamWithServiceAccount(ctx context.Conte
|
||||
for scanner.Scan() {
|
||||
line := scanner.Bytes()
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, line)
|
||||
if replayAccumulator != nil {
|
||||
replayAccumulator.ObserveSSELine(line)
|
||||
}
|
||||
reporter.ObserveResponseModel(line)
|
||||
rawPayload := helps.JSONPayload(line)
|
||||
if len(rawPayload) > 0 && len(pendingFilterLine) > 0 {
|
||||
@@ -970,6 +1028,7 @@ func (e *GeminiVertexExecutor) executeStreamWithServiceAccount(ctx context.Conte
|
||||
helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error {
|
||||
return statusErr{code: http.StatusBadGateway, msg: msg}
|
||||
})
|
||||
commitReplay()
|
||||
for i := range lines {
|
||||
select {
|
||||
case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}:
|
||||
@@ -1018,12 +1077,21 @@ func (e *GeminiVertexExecutor) executeStreamWithAPIKey(ctx context.Context, auth
|
||||
requestedModel := helps.PayloadRequestedModel(opts, req.Model)
|
||||
requestPath := helps.PayloadRequestPath(opts)
|
||||
helps.SetStringIfDifferentDoc(doc, "model", baseModel)
|
||||
helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String())
|
||||
internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
||||
|
||||
action := getVertexAction(baseModel, true)
|
||||
helps.DropGeminiTrailingThoughtOnlyContentDoc(doc, "contents")
|
||||
helps.EnsureGeminiBoundaryUserContentDoc(doc, "contents")
|
||||
replayScope := antigravityReasoningReplayScope{}
|
||||
var replayRequestPayload []byte
|
||||
if !isImagenModel(baseModel) && antigravityUsesReasoningReplayCache(baseModel) {
|
||||
var errReplay error
|
||||
replayScope, replayRequestPayload, _, errReplay = prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc)
|
||||
if errReplay != nil {
|
||||
return nil, errReplay
|
||||
}
|
||||
}
|
||||
helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String())
|
||||
// For API key auth, use simpler URL format without project/location
|
||||
url := vertexAPIKeyModelsURL(baseURL, baseModel, action)
|
||||
// Imagen models don't support streaming, skip SSE params
|
||||
@@ -1087,9 +1155,13 @@ func (e *GeminiVertexExecutor) executeStreamWithAPIKey(ctx context.Context, auth
|
||||
if errClose := httpResp.Body.Close(); errClose != nil {
|
||||
log.Errorf("vertex executor: close response body error: %v", errClose)
|
||||
}
|
||||
if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, replayScope, httpResp.StatusCode, b); errClear != nil {
|
||||
logAntigravityReasoningReplayDegraded(replayScope, "invalidate", errClear)
|
||||
}
|
||||
return nil, statusErr{code: httpResp.StatusCode, msg: string(b)}
|
||||
}
|
||||
|
||||
replayAccumulator := newAntigravityReasoningReplayAccumulator(replayScope, replayRequestPayload)
|
||||
out := make(chan cliproxyexecutor.StreamChunk, 1)
|
||||
go func() {
|
||||
defer close(out)
|
||||
@@ -1111,9 +1183,16 @@ func (e *GeminiVertexExecutor) executeStreamWithAPIKey(ctx context.Context, auth
|
||||
}()
|
||||
var terminalDelivered bool
|
||||
var sawFinishReason bool
|
||||
var replayCommitted bool
|
||||
var pendingFilterLine []byte
|
||||
var pendingFilterMsg string
|
||||
var param any
|
||||
commitReplay := func() {
|
||||
if !replayCommitted && replayAccumulator != nil && replayAccumulator.terminal && streamErr == nil && helps.ApplyPatchTranslationError(param) == nil {
|
||||
replayAccumulator.Commit(ctx)
|
||||
replayCommitted = true
|
||||
}
|
||||
}
|
||||
helps.InitializeApplyPatchStream(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, ¶m)
|
||||
emitTranslatedLine := func(line []byte) bool {
|
||||
lines := helps.TranslateStreamWithClaudeInputTokens(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, bytes.Clone(line), ¶m, claudeInputTokens)
|
||||
@@ -1121,6 +1200,9 @@ func (e *GeminiVertexExecutor) executeStreamWithAPIKey(ctx context.Context, auth
|
||||
helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error {
|
||||
return statusErr{code: http.StatusBadGateway, msg: msg}
|
||||
})
|
||||
if replayAccumulator != nil && replayAccumulator.terminal && !replayCommitted && antigravityStreamChunksHaveTerminalCommitEvent(lines) {
|
||||
commitReplay()
|
||||
}
|
||||
for i := range lines {
|
||||
select {
|
||||
case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}:
|
||||
@@ -1136,6 +1218,9 @@ func (e *GeminiVertexExecutor) executeStreamWithAPIKey(ctx context.Context, auth
|
||||
for scanner.Scan() {
|
||||
line := scanner.Bytes()
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, line)
|
||||
if replayAccumulator != nil {
|
||||
replayAccumulator.ObserveSSELine(line)
|
||||
}
|
||||
reporter.ObserveResponseModel(line)
|
||||
rawPayload := helps.JSONPayload(line)
|
||||
if len(rawPayload) > 0 && len(pendingFilterLine) > 0 {
|
||||
@@ -1228,6 +1313,7 @@ func (e *GeminiVertexExecutor) executeStreamWithAPIKey(ctx context.Context, auth
|
||||
helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error {
|
||||
return statusErr{code: http.StatusBadGateway, msg: msg}
|
||||
})
|
||||
commitReplay()
|
||||
for i := range lines {
|
||||
select {
|
||||
case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}:
|
||||
@@ -1269,13 +1355,18 @@ func (e *GeminiVertexExecutor) countTokensWithServiceAccount(ctx context.Context
|
||||
|
||||
fixGeminiImageAspectRatioDoc(baseModel, doc)
|
||||
_ = doc.Set("model", baseModel)
|
||||
helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String())
|
||||
respCtx := context.WithValue(ctx, "alt", opts.Alt)
|
||||
_ = doc.Delete("tools")
|
||||
_ = doc.Delete("generationConfig")
|
||||
_ = doc.Delete("safetySettings")
|
||||
internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
||||
helps.EnsureGeminiLeadingUserContentDoc(doc, "contents")
|
||||
if !isImagenModel(baseModel) && antigravityUsesReasoningReplayCache(baseModel) {
|
||||
if _, errReplay := prepareGeminiTopLevelCountTokensReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc); errReplay != nil {
|
||||
return cliproxyexecutor.Response{}, errReplay
|
||||
}
|
||||
}
|
||||
helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String())
|
||||
|
||||
baseURL := vertexBaseURL(location)
|
||||
url := fmt.Sprintf("%s/%s/projects/%s/locations/%s/publishers/google/models/%s:%s", baseURL, vertexAPIVersion, projectID, location, baseModel, "countTokens")
|
||||
@@ -1373,13 +1464,18 @@ func (e *GeminiVertexExecutor) countTokensWithAPIKey(ctx context.Context, auth *
|
||||
|
||||
fixGeminiImageAspectRatioDoc(baseModel, doc)
|
||||
_ = doc.Set("model", baseModel)
|
||||
helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String())
|
||||
respCtx := context.WithValue(ctx, "alt", opts.Alt)
|
||||
_ = doc.Delete("tools")
|
||||
_ = doc.Delete("generationConfig")
|
||||
_ = doc.Delete("safetySettings")
|
||||
internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
||||
helps.EnsureGeminiLeadingUserContentDoc(doc, "contents")
|
||||
if !isImagenModel(baseModel) && antigravityUsesReasoningReplayCache(baseModel) {
|
||||
if _, errReplay := prepareGeminiTopLevelCountTokensReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc); errReplay != nil {
|
||||
return cliproxyexecutor.Response{}, errReplay
|
||||
}
|
||||
}
|
||||
helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String())
|
||||
|
||||
// For API key auth, use simpler URL format without project/location
|
||||
url := vertexAPIKeyModelsURL(baseURL, baseModel, "countTokens")
|
||||
|
||||
Reference in New Issue
Block a user