mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-29 05:12:44 +08:00
- Guard trailing frame emission in Gemini and Vertex Interactions streaming executors so buffered partial frames are only flushed on clean EOF with an active context, while preserving trailing newline detection via ScanInteractionsSSELines to prevent unterminated whitespace from triggering in-loop flushes. - Record non-nil scanner errors in internal API response diagnostics before EndApplyPatchStream finalization so raw read failures are never lost while keeping the sanitized 502 downstream apply_patch contract intact. - Add regression tests covering mid-frame scanner errors, canceled-context diagnostic recording, apply_patch scanner error diagnostics, unterminated whitespace tails, and clean EOF trailing frames. Closes: #8138
2445 lines
91 KiB
Go
2445 lines
91 KiB
Go
// Package executor provides runtime execution capabilities for various AI service providers.
|
|
// This file implements the Vertex AI Gemini executor that talks to Google Vertex AI
|
|
// endpoints using service account credentials or API keys.
|
|
package executor
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/MoeForge/moego/json"
|
|
"github.com/MoeForge/moego/ojson"
|
|
vertexauth "github.com/router-for-me/CLIProxyAPI/v8/internal/auth/vertex"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/clienterror"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
|
|
"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/thinking"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/util"
|
|
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
|
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
|
sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator"
|
|
log "github.com/sirupsen/logrus"
|
|
"golang.org/x/oauth2"
|
|
"golang.org/x/oauth2/google"
|
|
)
|
|
|
|
const (
|
|
// vertexAPIVersion aligns with current public Vertex Generative AI API.
|
|
vertexAPIVersion = "v1"
|
|
// vertexImagenMaxResponseBytes bounds the single JSON response returned by Imagen.
|
|
vertexImagenMaxResponseBytes = 64 << 20
|
|
)
|
|
|
|
// vertexSSEUsageFilter filters non-terminal usageMetadata per Vertex stream while
|
|
// preserving standard usageMetadata on trailing usage-only frames emitted after
|
|
// a finish signal, even when upstream omits traceId.
|
|
type vertexSSEUsageFilter struct {
|
|
emittedFinish bool
|
|
}
|
|
|
|
func (f *vertexSSEUsageFilter) filter(line []byte) []byte {
|
|
insp := helps.InspectGeminiZeroOutputPayload(line, false)
|
|
if insp.FinishReason != "" || insp.HasPromptBlock {
|
|
f.emittedFinish = true
|
|
return helps.FilterSSEUsageMetadata(line)
|
|
}
|
|
if f.emittedFinish && insp.HasUsageMetadata && !insp.HasVisibleOutput && !insp.HasThoughtOutput {
|
|
return line
|
|
}
|
|
return helps.FilterSSEUsageMetadata(line)
|
|
}
|
|
|
|
// isImagenModel checks if the model name is an Imagen image generation model.
|
|
// Imagen models use the :predict action instead of :generateContent.
|
|
func isImagenModel(model string) bool {
|
|
lowerModel := strings.ToLower(model)
|
|
return strings.Contains(lowerModel, "imagen")
|
|
}
|
|
|
|
// getVertexAction returns the appropriate action for the given model.
|
|
// Imagen models use "predict", while Gemini models use "generateContent".
|
|
func getVertexAction(model string, isStream bool) string {
|
|
if isImagenModel(model) {
|
|
return "predict"
|
|
}
|
|
if isStream {
|
|
return "streamGenerateContent"
|
|
}
|
|
return "generateContent"
|
|
}
|
|
|
|
// convertImagenToGeminiResponse converts Imagen API response to Gemini format
|
|
// so it can be processed by the standard translation pipeline.
|
|
// This ensures Imagen models return responses in the same format as gemini-3-pro-image-preview.
|
|
func convertImagenToGeminiResponse(data []byte, model string) []byte {
|
|
predictions := ojson.GetBytesNoCopy(data, "predictions")
|
|
if !predictions.Exists() || !predictions.IsArray() {
|
|
return data
|
|
}
|
|
|
|
// Build Gemini-compatible response with inlineData
|
|
parts := make([]map[string]any, 0)
|
|
for _, pred := range predictions.Array() {
|
|
imageData := pred.Get("bytesBase64Encoded").String()
|
|
mimeType := pred.Get("mimeType").String()
|
|
if mimeType == "" {
|
|
mimeType = "image/png"
|
|
}
|
|
if imageData != "" {
|
|
parts = append(parts, map[string]any{
|
|
"inlineData": map[string]any{
|
|
"mimeType": mimeType,
|
|
"data": imageData,
|
|
},
|
|
})
|
|
}
|
|
}
|
|
|
|
// Generate unique response ID using timestamp
|
|
responseId := fmt.Sprintf("imagen-%d", time.Now().UnixNano())
|
|
|
|
response := map[string]any{
|
|
"candidates": []map[string]any{{
|
|
"content": map[string]any{
|
|
"parts": parts,
|
|
"role": "model",
|
|
},
|
|
"finishReason": "STOP",
|
|
}},
|
|
"responseId": responseId,
|
|
"modelVersion": model,
|
|
// Imagen API doesn't return token counts, set to 0 for tracking purposes
|
|
"usageMetadata": map[string]any{
|
|
"promptTokenCount": 0,
|
|
"candidatesTokenCount": 0,
|
|
"totalTokenCount": 0,
|
|
},
|
|
}
|
|
|
|
result, err := json.Marshal(response)
|
|
if err != nil {
|
|
return data
|
|
}
|
|
return result
|
|
}
|
|
|
|
// convertToImagenRequest converts a Gemini-style request to Imagen API format.
|
|
// Imagen API uses a different structure: instances[].prompt instead of contents[].
|
|
func convertToImagenRequest(payload []byte) ([]byte, error) {
|
|
// Extract prompt from Gemini-style contents
|
|
prompt := ""
|
|
|
|
// Try to get prompt from contents[0].parts[0].text
|
|
contentsText := ojson.GetBytesNoCopy(payload, "contents.0.parts.0.text")
|
|
if contentsText.Exists() {
|
|
prompt = contentsText.String()
|
|
}
|
|
|
|
// If no contents, try messages format (OpenAI-compatible)
|
|
if prompt == "" {
|
|
messagesText := ojson.GetBytesNoCopy(payload, "messages.#.content")
|
|
if messagesText.Exists() && messagesText.IsArray() {
|
|
for _, msg := range messagesText.Array() {
|
|
if msg.String() != "" {
|
|
prompt = msg.String()
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// If still no prompt, try direct prompt field
|
|
if prompt == "" {
|
|
directPrompt := ojson.GetBytesNoCopy(payload, "prompt")
|
|
if directPrompt.Exists() {
|
|
prompt = directPrompt.String()
|
|
}
|
|
}
|
|
|
|
if prompt == "" {
|
|
return nil, fmt.Errorf("imagen: no prompt found in request")
|
|
}
|
|
|
|
// Build Imagen API request
|
|
imagenReq := map[string]any{
|
|
"instances": []map[string]any{
|
|
{
|
|
"prompt": prompt,
|
|
},
|
|
},
|
|
"parameters": map[string]any{
|
|
"sampleCount": 1,
|
|
},
|
|
}
|
|
|
|
// Extract optional parameters
|
|
if aspectRatio := ojson.GetBytesNoCopy(payload, "aspectRatio"); aspectRatio.Exists() {
|
|
imagenReq["parameters"].(map[string]any)["aspectRatio"] = aspectRatio.String()
|
|
}
|
|
if sampleCount := ojson.GetBytesNoCopy(payload, "sampleCount"); sampleCount.Exists() {
|
|
imagenReq["parameters"].(map[string]any)["sampleCount"] = int(sampleCount.Int())
|
|
}
|
|
if negativePrompt := ojson.GetBytesNoCopy(payload, "negativePrompt"); negativePrompt.Exists() {
|
|
imagenReq["instances"].([]map[string]any)[0]["negativePrompt"] = negativePrompt.String()
|
|
}
|
|
|
|
return json.Marshal(imagenReq)
|
|
}
|
|
|
|
func (e *GeminiVertexExecutor) translateImagenRequest(ctx context.Context, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, model string, stream bool) (body, original []byte, err error) {
|
|
from := opts.SourceFormat
|
|
to := sdktranslator.FromString("gemini")
|
|
translated, errTranslate := helps.TranslateRequestReturningError(ctx, opts.Headers, e.cfg, from, to, model, req.Payload, stream, false)
|
|
if errTranslate != nil {
|
|
return nil, nil, errTranslate
|
|
}
|
|
body, err = convertToImagenRequest(translated)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
originalPayload := req.Payload
|
|
if len(opts.OriginalRequest) > 0 {
|
|
originalPayload = opts.OriginalRequest
|
|
}
|
|
translatedOriginal, errTranslate := helps.TranslateRequestReturningError(ctx, opts.Headers, e.cfg, from, to, model, originalPayload, stream, false)
|
|
if errTranslate != nil {
|
|
return nil, nil, errTranslate
|
|
}
|
|
original, err = convertToImagenRequest(translatedOriginal)
|
|
return body, original, err
|
|
}
|
|
|
|
func readVertexImagenResponse(body io.Reader) ([]byte, error) {
|
|
data, errRead := io.ReadAll(io.LimitReader(body, vertexImagenMaxResponseBytes+1))
|
|
if errRead != nil {
|
|
return nil, errRead
|
|
}
|
|
if len(data) > vertexImagenMaxResponseBytes {
|
|
return nil, fmt.Errorf("vertex imagen: response exceeds %d bytes", vertexImagenMaxResponseBytes)
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
// GeminiVertexExecutor sends requests to Vertex AI Gemini endpoints using service account credentials.
|
|
type GeminiVertexExecutor struct {
|
|
cfg *config.Config
|
|
}
|
|
|
|
// NewGeminiVertexExecutor creates a new Vertex AI Gemini executor instance.
|
|
//
|
|
// Parameters:
|
|
// - cfg: The application configuration
|
|
//
|
|
// Returns:
|
|
// - *GeminiVertexExecutor: A new Vertex AI Gemini executor instance
|
|
func NewGeminiVertexExecutor(cfg *config.Config) *GeminiVertexExecutor {
|
|
return &GeminiVertexExecutor{cfg: cfg}
|
|
}
|
|
|
|
// Identifier returns the executor identifier.
|
|
func (e *GeminiVertexExecutor) Identifier() string { return "vertex" }
|
|
|
|
// PrepareRequest injects Vertex credentials into the outgoing HTTP request.
|
|
func (e *GeminiVertexExecutor) PrepareRequest(req *http.Request, auth *cliproxyauth.Auth) error {
|
|
if req == nil {
|
|
return nil
|
|
}
|
|
apiKey, _ := vertexAPICreds(auth)
|
|
if strings.TrimSpace(apiKey) != "" {
|
|
req.Header.Set("x-goog-api-key", apiKey)
|
|
req.Header.Del("Authorization")
|
|
return nil
|
|
}
|
|
_, _, saJSON, errCreds := vertexCreds(auth)
|
|
if errCreds != nil {
|
|
return errCreds
|
|
}
|
|
token, errToken := vertexAccessToken(req.Context(), e.cfg, auth, saJSON)
|
|
if errToken != nil {
|
|
return errToken
|
|
}
|
|
if strings.TrimSpace(token) == "" {
|
|
return statusErr{code: http.StatusUnauthorized, msg: "missing access token"}
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
req.Header.Del("x-goog-api-key")
|
|
return nil
|
|
}
|
|
|
|
// HttpRequest injects Vertex credentials into the request and executes it.
|
|
func (e *GeminiVertexExecutor) HttpRequest(ctx context.Context, auth *cliproxyauth.Auth, req *http.Request) (*http.Response, error) {
|
|
if req == nil {
|
|
return nil, fmt.Errorf("vertex executor: request is nil")
|
|
}
|
|
if ctx == nil {
|
|
ctx = req.Context()
|
|
}
|
|
httpReq := req.WithContext(ctx)
|
|
if err := e.PrepareRequest(httpReq, auth); err != nil {
|
|
return nil, err
|
|
}
|
|
httpClient := helps.NewProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
|
|
return httpClient.Do(httpReq)
|
|
}
|
|
|
|
// Execute performs a non-streaming request to the Vertex AI API.
|
|
func (e *GeminiVertexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (resp cliproxyexecutor.Response, err error) {
|
|
ctx = helps.EnsureSessionContext(ctx, opts, req.Payload)
|
|
if opts.Alt == "responses/compact" {
|
|
return resp, statusErr{code: http.StatusNotImplemented, msg: "/responses/compact not supported"}
|
|
}
|
|
if helps.ShouldExecuteVertexOpenAPIForRequest(e.cfg, auth, req.Model, opts) {
|
|
return e.executeOpenAPI(ctx, auth, req, opts)
|
|
}
|
|
if shouldExecuteVertexInteractions(auth, opts) {
|
|
return e.executeInteractions(ctx, auth, req, opts)
|
|
}
|
|
// Try API key authentication first
|
|
apiKey, baseURL := vertexAPICreds(auth)
|
|
|
|
// If no API key found, fall back to service account authentication
|
|
if apiKey == "" {
|
|
projectID, location, saJSON, errCreds := vertexCreds(auth)
|
|
if errCreds != nil {
|
|
return resp, errCreds
|
|
}
|
|
return e.executeWithServiceAccount(ctx, auth, req, opts, projectID, location, saJSON)
|
|
}
|
|
|
|
// Use API key authentication
|
|
return e.executeWithAPIKey(ctx, auth, req, opts, apiKey, baseURL)
|
|
}
|
|
|
|
// ExecuteStream performs a streaming request to the Vertex AI API.
|
|
func (e *GeminiVertexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, error) {
|
|
ctx = helps.EnsureSessionContext(ctx, opts, req.Payload)
|
|
if opts.Alt == "responses/compact" {
|
|
return nil, statusErr{code: http.StatusNotImplemented, msg: "/responses/compact not supported"}
|
|
}
|
|
if helps.ShouldExecuteVertexOpenAPIForRequest(e.cfg, auth, req.Model, opts) {
|
|
return e.executeOpenAPIStream(ctx, auth, req, opts)
|
|
}
|
|
if shouldExecuteVertexInteractions(auth, opts) {
|
|
return e.executeInteractionsStream(ctx, auth, req, opts)
|
|
}
|
|
// Try API key authentication first
|
|
apiKey, baseURL := vertexAPICreds(auth)
|
|
|
|
// If no API key found, fall back to service account authentication
|
|
if apiKey == "" {
|
|
projectID, location, saJSON, errCreds := vertexCreds(auth)
|
|
if errCreds != nil {
|
|
return nil, errCreds
|
|
}
|
|
return e.executeStreamWithServiceAccount(ctx, auth, req, opts, projectID, location, saJSON)
|
|
}
|
|
|
|
// Use API key authentication
|
|
return e.executeStreamWithAPIKey(ctx, auth, req, opts, apiKey, baseURL)
|
|
}
|
|
|
|
// CountTokens counts tokens for the given request using the Vertex AI API.
|
|
func (e *GeminiVertexExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
|
|
if helps.ShouldExecuteVertexOpenAPIForRequest(e.cfg, auth, req.Model, opts) {
|
|
normalizedReq, normalizedOpts := normalizeVertexOpenAPIRequest(req, opts)
|
|
return NewOpenAICompatExecutor(e.Identifier(), e.cfg).CountTokens(ctx, auth, normalizedReq, normalizedOpts)
|
|
}
|
|
// Try API key authentication first
|
|
apiKey, baseURL := vertexAPICreds(auth)
|
|
|
|
// If no API key found, fall back to service account authentication
|
|
if apiKey == "" {
|
|
projectID, location, saJSON, errCreds := vertexCreds(auth)
|
|
if errCreds != nil {
|
|
return cliproxyexecutor.Response{}, errCreds
|
|
}
|
|
return e.countTokensWithServiceAccount(ctx, auth, req, opts, projectID, location, saJSON)
|
|
}
|
|
|
|
// Use API key authentication
|
|
return e.countTokensWithAPIKey(ctx, auth, req, opts, apiKey, baseURL)
|
|
}
|
|
|
|
func normalizeVertexOpenAPIRequest(req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Request, cliproxyexecutor.Options) {
|
|
normalizedModel := helps.NormalizeVertexOpenAPIModel(req.Model)
|
|
if normalizedModel == "" || normalizedModel == req.Model {
|
|
return req, opts
|
|
}
|
|
if len(opts.OriginalRequest) == 0 && len(req.Payload) > 0 {
|
|
opts.OriginalRequest = req.Payload
|
|
}
|
|
req.Model = normalizedModel
|
|
return req, opts
|
|
}
|
|
|
|
func hasVertexServiceAccount(auth *cliproxyauth.Auth) bool {
|
|
if auth == nil || auth.Metadata == nil {
|
|
return false
|
|
}
|
|
sa, ok := auth.Metadata["service_account"].(map[string]any)
|
|
return ok && len(sa) > 0
|
|
}
|
|
|
|
func (e *GeminiVertexExecutor) vertexOpenAPILocation(auth *cliproxyauth.Auth, defaultLocation string) string {
|
|
if explicit := helps.VertexExplicitOpenAPILocation(e.cfg, auth); explicit != "" {
|
|
return explicit
|
|
}
|
|
return helps.VertexOpenAPILocation(auth, defaultLocation)
|
|
}
|
|
|
|
func (e *GeminiVertexExecutor) resolveOpenAPICredentials(ctx context.Context, auth *cliproxyauth.Auth) (string, string, error) {
|
|
apiKey, baseURL := vertexAPICreds(auth)
|
|
if apiKey == "" || hasVertexServiceAccount(auth) {
|
|
projectID, location, saJSON, errCreds := vertexCreds(auth)
|
|
if errCreds != nil {
|
|
return "", "", errCreds
|
|
}
|
|
token, errTok := vertexAccessToken(ctx, e.cfg, auth, saJSON)
|
|
if errTok != nil {
|
|
log.Errorf("vertex executor: access token error: %v", errTok)
|
|
return "", "", statusErr{code: 500, msg: "internal server error"}
|
|
}
|
|
if strings.TrimSpace(token) == "" {
|
|
return "", "", statusErr{code: http.StatusUnauthorized, msg: "missing access token"}
|
|
}
|
|
authBaseURL := helps.VertexAuthBaseURL(auth)
|
|
openAPILocation := e.vertexOpenAPILocation(auth, location)
|
|
if strings.Contains(authBaseURL, "/locations/") {
|
|
openAPILocation = helps.VertexExplicitOpenAPILocation(e.cfg, auth)
|
|
}
|
|
openAPIBaseURL := helps.VertexServiceAccountOpenAPIBaseURL(authBaseURL, vertexAPIVersion, projectID, openAPILocation)
|
|
return openAPIBaseURL, token, nil
|
|
}
|
|
if helps.IsGoogleVertexHost(baseURL) {
|
|
return "", "", statusErr{code: http.StatusBadRequest, msg: "vertex endpoints/openapi on aiplatform.googleapis.com requires service account credentials"}
|
|
}
|
|
openAPILocation := helps.VertexExplicitOpenAPILocation(e.cfg, auth)
|
|
if !strings.Contains(baseURL, "/locations/") && openAPILocation == "" {
|
|
openAPILocation = e.vertexOpenAPILocation(auth, "")
|
|
}
|
|
openAPIBaseURL := helps.VertexAPIKeyOpenAPIBaseURL(baseURL, vertexAPIVersion, helps.VertexAuthProjectID(auth), openAPILocation)
|
|
return openAPIBaseURL, apiKey, nil
|
|
}
|
|
|
|
func (e *GeminiVertexExecutor) executeOpenAPI(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (resp cliproxyexecutor.Response, err error) {
|
|
req, opts = normalizeVertexOpenAPIRequest(req, opts)
|
|
openAPIBaseURL, token, errCreds := e.resolveOpenAPICredentials(ctx, auth)
|
|
if errCreds != nil {
|
|
baseModel := thinking.ParseSuffix(req.Model).ModelName
|
|
reporter := helps.NewExecutorUsageReporter(ctx, e, baseModel, auth)
|
|
reporter.TrackFailure(ctx, &errCreds)
|
|
return resp, errCreds
|
|
}
|
|
return NewOpenAICompatExecutor(e.Identifier(), e.cfg).executeWithCredentials(ctx, auth, req, opts, openAPIBaseURL, token, nil)
|
|
}
|
|
|
|
func (e *GeminiVertexExecutor) executeOpenAPIStream(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, error) {
|
|
req, opts = normalizeVertexOpenAPIRequest(req, opts)
|
|
openAPIBaseURL, token, errCreds := e.resolveOpenAPICredentials(ctx, auth)
|
|
if errCreds != nil {
|
|
baseModel := thinking.ParseSuffix(req.Model).ModelName
|
|
reporter := helps.NewExecutorUsageReporter(ctx, e, baseModel, auth)
|
|
reporter.TrackFailure(ctx, &errCreds)
|
|
return nil, errCreds
|
|
}
|
|
return NewOpenAICompatExecutor(e.Identifier(), e.cfg).executeStreamWithCredentials(ctx, auth, req, opts, openAPIBaseURL, token, nil)
|
|
}
|
|
|
|
// Refresh refreshes the authentication credentials (no-op for Vertex).
|
|
func (e *GeminiVertexExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error) {
|
|
if refreshed, handled, err := helps.RefreshAuthViaHome(ctx, e.cfg, auth); handled {
|
|
return refreshed, err
|
|
}
|
|
return auth, nil
|
|
}
|
|
|
|
// executeWithServiceAccount handles authentication using service account credentials.
|
|
// This method contains the original service account authentication logic.
|
|
func (e *GeminiVertexExecutor) executeWithServiceAccount(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, projectID, location string, saJSON []byte) (resp cliproxyexecutor.Response, err error) {
|
|
baseModel := thinking.ParseSuffix(req.Model).ModelName
|
|
|
|
reporter := helps.NewExecutorUsageReporter(ctx, e, baseModel, auth)
|
|
defer reporter.TrackFailure(ctx, &err)
|
|
|
|
action := getVertexAction(baseModel, false)
|
|
if req.Metadata != nil {
|
|
if a, _ := req.Metadata["action"].(string); a == "countTokens" {
|
|
action = "countTokens"
|
|
}
|
|
}
|
|
var body []byte
|
|
var originalTranslated []byte
|
|
var doc *ojson.Doc
|
|
|
|
// Handle Imagen models with special request format
|
|
if isImagenModel(baseModel) && action != "countTokens" {
|
|
body, originalTranslated, err = e.translateImagenRequest(ctx, req, opts, baseModel, false)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
doc = ojson.NewDoc(body)
|
|
} else {
|
|
// Standard Gemini translation flow
|
|
from := opts.SourceFormat
|
|
to := sdktranslator.FromString("gemini")
|
|
|
|
originalPayloadSource := req.Payload
|
|
if len(opts.OriginalRequest) > 0 {
|
|
originalPayloadSource = opts.OriginalRequest
|
|
}
|
|
originalPayload := originalPayloadSource
|
|
originalTranslated = helps.TranslateRequestWithCodexMultiAgentV2(ctx, opts.Headers, e.cfg, from, to, baseModel, originalPayload, false)
|
|
body, err = helps.TranslateRequestReturningError(ctx, opts.Headers, e.cfg, from, to, baseModel, req.Payload, false, false)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
|
|
doc = ojson.NewDoc(body)
|
|
if err = helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier()); err != nil {
|
|
return resp, err
|
|
}
|
|
|
|
fixGeminiImageAspectRatioDoc(baseModel, doc)
|
|
helps.SetStringIfDifferentDoc(doc, "model", baseModel)
|
|
internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
|
}
|
|
|
|
if action != "countTokens" || !isImagenModel(baseModel) {
|
|
helps.EnsureGeminiLeadingUserContentDoc(doc, "contents")
|
|
}
|
|
if action != "countTokens" {
|
|
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)
|
|
endpointURL := fmt.Sprintf("%s/%s/projects/%s/locations/%s/publishers/google/models/%s:%s", baseURL, vertexAPIVersion, projectID, location, baseModel, action)
|
|
if opts.Alt != "" && action != "countTokens" {
|
|
endpointURL = endpointURL + "?$alt=" + url.QueryEscape(opts.Alt)
|
|
}
|
|
_ = doc.Delete("session_id")
|
|
|
|
helps.ApplyPayloadConfigWithRequestDoc(e.cfg, baseModel, "gemini", opts.SourceFormat.String(), "", doc, originalTranslated, helps.PayloadRequestedModel(opts, req.Model), helps.PayloadRequestPath(opts), opts.Headers)
|
|
body = doc.Bytes()
|
|
reporter.SetTranslatedReasoningEffort(body, "gemini")
|
|
httpReq, errNewReq := http.NewRequestWithContext(ctx, http.MethodPost, endpointURL, bytes.NewReader(body))
|
|
if errNewReq != nil {
|
|
return resp, errNewReq
|
|
}
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
if token, errTok := vertexAccessToken(ctx, e.cfg, auth, saJSON); errTok == nil && token != "" {
|
|
httpReq.Header.Set("Authorization", "Bearer "+token)
|
|
} else if errTok != nil {
|
|
log.Errorf("vertex executor: access token error: %v", errTok)
|
|
return resp, statusErr{code: 500, msg: "internal server error"}
|
|
}
|
|
applyGeminiHeaders(httpReq, auth, opts.Headers)
|
|
var attrs map[string]string
|
|
if auth != nil {
|
|
attrs = auth.Attributes
|
|
}
|
|
util.ApplyCustomHeadersFromAttrs(httpReq, attrs, opts.Headers)
|
|
|
|
var authID, authLabel, authType, authValue string
|
|
if auth != nil {
|
|
authID = auth.ID
|
|
authLabel = auth.Label
|
|
authType, authValue = auth.AccountInfo()
|
|
}
|
|
helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{
|
|
URL: endpointURL,
|
|
Method: http.MethodPost,
|
|
Headers: httpReq.Header.Clone(),
|
|
Body: body,
|
|
Provider: e.Identifier(),
|
|
AuthID: authID,
|
|
AuthLabel: authLabel,
|
|
AuthType: authType,
|
|
AuthValue: authValue,
|
|
})
|
|
|
|
httpClient := helps.NewProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
|
|
httpClient = reporter.TrackHTTPClient(httpClient)
|
|
httpResp, errDo := httpClient.Do(httpReq)
|
|
if errDo != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errDo)
|
|
return resp, errDo
|
|
}
|
|
defer func() {
|
|
if errClose := httpResp.Body.Close(); errClose != nil {
|
|
log.Errorf("vertex executor: close response body error: %v", errClose)
|
|
}
|
|
}()
|
|
helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())
|
|
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
|
|
b, errRead := io.ReadAll(httpResp.Body)
|
|
helps.AppendAPIResponseChunk(ctx, e.cfg, b)
|
|
if errRead != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
|
|
}
|
|
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 = newGeminiStatusErr(httpResp.StatusCode, httpResp.Header, b)
|
|
return resp, err
|
|
}
|
|
data, errRead := io.ReadAll(httpResp.Body)
|
|
helps.AppendAPIResponseChunk(ctx, e.cfg, data)
|
|
if errRead != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
|
|
return resp, errRead
|
|
}
|
|
reporter.ObserveResponseModel(data)
|
|
|
|
// For Imagen models, convert response to Gemini format before translation
|
|
// This ensures Imagen responses use the same format as gemini-3-pro-image-preview
|
|
if isImagenModel(baseModel) {
|
|
data = convertImagenToGeminiResponse(data, baseModel)
|
|
}
|
|
|
|
if blockMsg, blocked := geminiPromptBlockErrorMessage(data); blocked {
|
|
err = statusErr{code: http.StatusBadGateway, msg: blockMsg}
|
|
helps.RecordAPIResponseError(ctx, e.cfg, err)
|
|
reporter.PublishFailureWithDetail(ctx, helps.ParseGeminiUsage(data), err)
|
|
return cliproxyexecutor.Response{}, err
|
|
}
|
|
|
|
// Standard Gemini translation (works for both Gemini and converted Imagen responses)
|
|
responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts)
|
|
if action != "countTokens" && !isImagenModel(baseModel) {
|
|
if _, zeroErrMsg, zeroOutput := helps.CheckGeminiZeroOutputNonStream(responseFormat, data, false); zeroOutput {
|
|
gemDetail := helps.ParseGeminiUsage(data)
|
|
helps.ApplyGeminiRequestImageConfig(&gemDetail, body)
|
|
err = newGeminiZeroOutputStatusErr(zeroErrMsg)
|
|
helps.RecordAPIResponseError(ctx, e.cfg, err)
|
|
reporter.PublishFailureWithDetail(ctx, gemDetail, err)
|
|
return cliproxyexecutor.Response{}, err
|
|
}
|
|
cacheAntigravityReasoningReplayFromResponse(ctx, replayScope, replayRequestPayload, data)
|
|
}
|
|
to := sdktranslator.FromString("gemini")
|
|
var param any
|
|
out := sdktranslator.TranslateNonStream(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, data, ¶m)
|
|
if helps.ApplyPatchTranslationError(param) != nil || len(out) == 0 {
|
|
return cliproxyexecutor.Response{}, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage}
|
|
}
|
|
if errPayload, okErr := helps.TranslatedClaudeErrorPayload(responseFormat, out); okErr {
|
|
err = newGeminiTranslatedClaudeErrorStatusErr(errPayload)
|
|
reporter.PublishFailureWithDetail(ctx, helps.ParseGeminiUsage(data), err)
|
|
return cliproxyexecutor.Response{}, err
|
|
}
|
|
gemDetail := helps.ParseGeminiUsage(data)
|
|
helps.ApplyGeminiRequestImageConfig(&gemDetail, body)
|
|
reporter.Publish(ctx, gemDetail)
|
|
if responseFormat == sdktranslator.FormatOpenAIResponse {
|
|
out = helps.EnsureResponsesUsageDetails(out)
|
|
}
|
|
resp = cliproxyexecutor.Response{Payload: out, Headers: httpResp.Header.Clone()}
|
|
return resp, nil
|
|
}
|
|
|
|
// executeWithAPIKey handles authentication using API key credentials.
|
|
func (e *GeminiVertexExecutor) executeWithAPIKey(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, apiKey, baseURL string) (resp cliproxyexecutor.Response, err error) {
|
|
baseModel := thinking.ParseSuffix(req.Model).ModelName
|
|
|
|
reporter := helps.NewExecutorUsageReporter(ctx, e, baseModel, auth)
|
|
defer reporter.TrackFailure(ctx, &err)
|
|
|
|
from := opts.SourceFormat
|
|
responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts)
|
|
to := sdktranslator.FromString("gemini")
|
|
|
|
originalPayloadSource := req.Payload
|
|
if len(opts.OriginalRequest) > 0 {
|
|
originalPayloadSource = opts.OriginalRequest
|
|
}
|
|
originalPayload := originalPayloadSource
|
|
action := getVertexAction(baseModel, false)
|
|
if req.Metadata != nil {
|
|
if a, _ := req.Metadata["action"].(string); a == "countTokens" {
|
|
action = "countTokens"
|
|
}
|
|
}
|
|
var body []byte
|
|
var originalTranslated []byte
|
|
var doc *ojson.Doc
|
|
if isImagenModel(baseModel) && action != "countTokens" {
|
|
body, originalTranslated, err = e.translateImagenRequest(ctx, req, opts, baseModel, false)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
doc = ojson.NewDoc(body)
|
|
} else {
|
|
originalTranslated = helps.TranslateRequestWithCodexMultiAgentV2(ctx, opts.Headers, e.cfg, from, to, baseModel, originalPayload, false)
|
|
body, err = helps.TranslateRequestReturningError(ctx, opts.Headers, e.cfg, from, to, baseModel, req.Payload, false, false)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
|
|
doc = ojson.NewDoc(body)
|
|
if err = helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier()); err != nil {
|
|
return resp, err
|
|
}
|
|
|
|
fixGeminiImageAspectRatioDoc(baseModel, doc)
|
|
helps.SetStringIfDifferentDoc(doc, "model", baseModel)
|
|
internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
|
}
|
|
requestedModel := helps.PayloadRequestedModel(opts, req.Model)
|
|
requestPath := helps.PayloadRequestPath(opts)
|
|
|
|
if action != "countTokens" || !isImagenModel(baseModel) {
|
|
helps.EnsureGeminiLeadingUserContentDoc(doc, "contents")
|
|
}
|
|
if action != "countTokens" {
|
|
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, from.String())
|
|
}
|
|
|
|
// For API key auth, use simpler URL format without project/location.
|
|
// The helper honors base URLs that already encode "/publishers/..." so
|
|
// configurations like ".../v1/projects/P/locations/L/publishers/google/models"
|
|
// keep working without a duplicated "/v1" segment.
|
|
endpointURL := vertexAPIKeyModelsURL(baseURL, baseModel, action)
|
|
if opts.Alt != "" && action != "countTokens" {
|
|
endpointURL = endpointURL + "?$alt=" + url.QueryEscape(opts.Alt)
|
|
}
|
|
_ = doc.Delete("session_id")
|
|
|
|
helps.ApplyPayloadConfigWithRequestDoc(e.cfg, baseModel, to.String(), from.String(), "", doc, originalTranslated, requestedModel, requestPath, opts.Headers)
|
|
body = doc.Bytes()
|
|
reporter.SetTranslatedReasoningEffort(body, to.String())
|
|
httpReq, errNewReq := http.NewRequestWithContext(ctx, http.MethodPost, endpointURL, bytes.NewReader(body))
|
|
if errNewReq != nil {
|
|
return resp, errNewReq
|
|
}
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
if apiKey != "" {
|
|
httpReq.Header.Set("x-goog-api-key", apiKey)
|
|
}
|
|
applyGeminiHeaders(httpReq, auth, opts.Headers)
|
|
var attrs map[string]string
|
|
if auth != nil {
|
|
attrs = auth.Attributes
|
|
}
|
|
util.ApplyCustomHeadersFromAttrs(httpReq, attrs, opts.Headers)
|
|
|
|
var authID, authLabel, authType, authValue string
|
|
if auth != nil {
|
|
authID = auth.ID
|
|
authLabel = auth.Label
|
|
authType, authValue = auth.AccountInfo()
|
|
}
|
|
helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{
|
|
URL: endpointURL,
|
|
Method: http.MethodPost,
|
|
Headers: httpReq.Header.Clone(),
|
|
Body: body,
|
|
Provider: e.Identifier(),
|
|
AuthID: authID,
|
|
AuthLabel: authLabel,
|
|
AuthType: authType,
|
|
AuthValue: authValue,
|
|
})
|
|
|
|
httpClient := helps.NewProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
|
|
httpClient = reporter.TrackHTTPClient(httpClient)
|
|
httpResp, errDo := httpClient.Do(httpReq)
|
|
if errDo != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errDo)
|
|
return resp, errDo
|
|
}
|
|
defer func() {
|
|
if errClose := httpResp.Body.Close(); errClose != nil {
|
|
log.Errorf("vertex executor: close response body error: %v", errClose)
|
|
}
|
|
}()
|
|
helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())
|
|
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
|
|
b, errRead := io.ReadAll(httpResp.Body)
|
|
helps.AppendAPIResponseChunk(ctx, e.cfg, b)
|
|
if errRead != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
|
|
}
|
|
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 = newGeminiStatusErr(httpResp.StatusCode, httpResp.Header, b)
|
|
return resp, err
|
|
}
|
|
data, errRead := io.ReadAll(httpResp.Body)
|
|
helps.AppendAPIResponseChunk(ctx, e.cfg, data)
|
|
if errRead != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
|
|
return resp, errRead
|
|
}
|
|
reporter.ObserveResponseModel(data)
|
|
if isImagenModel(baseModel) {
|
|
data = convertImagenToGeminiResponse(data, baseModel)
|
|
}
|
|
if blockMsg, blocked := geminiPromptBlockErrorMessage(data); blocked {
|
|
err = statusErr{code: http.StatusBadGateway, msg: blockMsg}
|
|
helps.RecordAPIResponseError(ctx, e.cfg, err)
|
|
reporter.PublishFailureWithDetail(ctx, helps.ParseGeminiUsage(data), err)
|
|
return cliproxyexecutor.Response{}, err
|
|
}
|
|
if action != "countTokens" && !isImagenModel(baseModel) {
|
|
if _, zeroErrMsg, zeroOutput := helps.CheckGeminiZeroOutputNonStream(responseFormat, data, false); zeroOutput {
|
|
gemDetail := helps.ParseGeminiUsage(data)
|
|
helps.ApplyGeminiRequestImageConfig(&gemDetail, body)
|
|
err = newGeminiZeroOutputStatusErr(zeroErrMsg)
|
|
helps.RecordAPIResponseError(ctx, e.cfg, err)
|
|
reporter.PublishFailureWithDetail(ctx, gemDetail, err)
|
|
return cliproxyexecutor.Response{}, err
|
|
}
|
|
cacheAntigravityReasoningReplayFromResponse(ctx, replayScope, replayRequestPayload, data)
|
|
}
|
|
var param any
|
|
out := sdktranslator.TranslateNonStream(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, data, ¶m)
|
|
if helps.ApplyPatchTranslationError(param) != nil || len(out) == 0 {
|
|
return cliproxyexecutor.Response{}, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage}
|
|
}
|
|
if errPayload, okErr := helps.TranslatedClaudeErrorPayload(responseFormat, out); okErr {
|
|
err = newGeminiTranslatedClaudeErrorStatusErr(errPayload)
|
|
reporter.PublishFailureWithDetail(ctx, helps.ParseGeminiUsage(data), err)
|
|
return cliproxyexecutor.Response{}, err
|
|
}
|
|
gemDetail := helps.ParseGeminiUsage(data)
|
|
helps.ApplyGeminiRequestImageConfig(&gemDetail, body)
|
|
reporter.Publish(ctx, gemDetail)
|
|
if responseFormat == sdktranslator.FormatOpenAIResponse {
|
|
out = helps.EnsureResponsesUsageDetails(out)
|
|
}
|
|
resp = cliproxyexecutor.Response{Payload: out, Headers: httpResp.Header.Clone()}
|
|
return resp, nil
|
|
}
|
|
|
|
// executeStreamWithServiceAccount handles streaming authentication using service account credentials.
|
|
func (e *GeminiVertexExecutor) executeStreamWithServiceAccount(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, projectID, location string, saJSON []byte) (_ *cliproxyexecutor.StreamResult, err error) {
|
|
baseModel := thinking.ParseSuffix(req.Model).ModelName
|
|
|
|
reporter := helps.NewExecutorUsageReporter(ctx, e, baseModel, auth)
|
|
defer reporter.TrackFailure(ctx, &err)
|
|
|
|
from := opts.SourceFormat
|
|
responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts)
|
|
to := sdktranslator.FromString("gemini")
|
|
|
|
originalPayloadSource := req.Payload
|
|
if len(opts.OriginalRequest) > 0 {
|
|
originalPayloadSource = opts.OriginalRequest
|
|
}
|
|
originalPayload := originalPayloadSource
|
|
var body []byte
|
|
var originalTranslated []byte
|
|
var doc *ojson.Doc
|
|
if isImagenModel(baseModel) {
|
|
body, originalTranslated, err = e.translateImagenRequest(ctx, req, opts, baseModel, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
doc = ojson.NewDoc(body)
|
|
} else {
|
|
originalTranslated = helps.TranslateRequestWithCodexMultiAgentV2(ctx, opts.Headers, e.cfg, from, to, baseModel, originalPayload, true)
|
|
body, err = helps.TranslateRequestReturningError(ctx, opts.Headers, e.cfg, from, to, baseModel, req.Payload, true, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
doc = ojson.NewDoc(body)
|
|
if err = helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier()); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fixGeminiImageAspectRatioDoc(baseModel, doc)
|
|
helps.SetStringIfDifferentDoc(doc, "model", baseModel)
|
|
internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
|
}
|
|
requestedModel := helps.PayloadRequestedModel(opts, req.Model)
|
|
requestPath := helps.PayloadRequestPath(opts)
|
|
|
|
action := getVertexAction(baseModel, true)
|
|
if !isImagenModel(baseModel) {
|
|
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
|
|
}
|
|
}
|
|
if !isImagenModel(baseModel) {
|
|
helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String())
|
|
}
|
|
baseURL := vertexBaseURL(location)
|
|
endpointURL := 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
|
|
if !isImagenModel(baseModel) {
|
|
if opts.Alt == "" {
|
|
endpointURL = endpointURL + "?alt=sse"
|
|
} else {
|
|
endpointURL = endpointURL + "?$alt=" + url.QueryEscape(opts.Alt)
|
|
}
|
|
}
|
|
_ = doc.Delete("session_id")
|
|
|
|
helps.ApplyPayloadConfigWithRequestDoc(e.cfg, baseModel, to.String(), from.String(), "", doc, originalTranslated, requestedModel, requestPath, opts.Headers)
|
|
body = doc.Bytes()
|
|
reporter.SetTranslatedReasoningEffort(body, to.String())
|
|
httpReq, errNewReq := http.NewRequestWithContext(ctx, http.MethodPost, endpointURL, bytes.NewReader(body))
|
|
if errNewReq != nil {
|
|
return nil, errNewReq
|
|
}
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
if token, errTok := vertexAccessToken(ctx, e.cfg, auth, saJSON); errTok == nil && token != "" {
|
|
httpReq.Header.Set("Authorization", "Bearer "+token)
|
|
} else if errTok != nil {
|
|
log.Errorf("vertex executor: access token error: %v", errTok)
|
|
return nil, statusErr{code: 500, msg: "internal server error"}
|
|
}
|
|
applyGeminiHeaders(httpReq, auth, opts.Headers)
|
|
var attrs map[string]string
|
|
if auth != nil {
|
|
attrs = auth.Attributes
|
|
}
|
|
util.ApplyCustomHeadersFromAttrs(httpReq, attrs, opts.Headers)
|
|
|
|
var authID, authLabel, authType, authValue string
|
|
if auth != nil {
|
|
authID = auth.ID
|
|
authLabel = auth.Label
|
|
authType, authValue = auth.AccountInfo()
|
|
}
|
|
helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{
|
|
URL: endpointURL,
|
|
Method: http.MethodPost,
|
|
Headers: httpReq.Header.Clone(),
|
|
Body: body,
|
|
Provider: e.Identifier(),
|
|
AuthID: authID,
|
|
AuthLabel: authLabel,
|
|
AuthType: authType,
|
|
AuthValue: authValue,
|
|
})
|
|
|
|
httpClient := helps.NewProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
|
|
httpClient = reporter.TrackHTTPClient(httpClient)
|
|
httpResp, errDo := httpClient.Do(httpReq)
|
|
if errDo != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errDo)
|
|
return nil, errDo
|
|
}
|
|
helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())
|
|
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
|
|
b, errRead := io.ReadAll(httpResp.Body)
|
|
helps.AppendAPIResponseChunk(ctx, e.cfg, b)
|
|
if errRead != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
|
|
}
|
|
helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), b))
|
|
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, newGeminiStatusErr(httpResp.StatusCode, httpResp.Header, b)
|
|
}
|
|
|
|
replayAccumulator := newAntigravityReasoningReplayAccumulator(replayScope, replayRequestPayload)
|
|
out := make(chan cliproxyexecutor.StreamChunk, 1)
|
|
go func() {
|
|
defer close(out)
|
|
defer reporter.EnsurePublished(ctx)
|
|
defer func() {
|
|
if errClose := httpResp.Body.Close(); errClose != nil {
|
|
log.Errorf("vertex executor: close response body error: %v", errClose)
|
|
}
|
|
}()
|
|
var scanner *bufio.Scanner
|
|
var imagenLine []byte
|
|
if isImagenModel(baseModel) {
|
|
imagenData, errRead := readVertexImagenResponse(httpResp.Body)
|
|
helps.AppendAPIResponseChunk(ctx, e.cfg, imagenData)
|
|
if errRead != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
|
|
reporter.PublishFailure(ctx, errRead)
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Err: errRead}:
|
|
case <-ctx.Done():
|
|
}
|
|
return
|
|
}
|
|
imagenLine = convertImagenToGeminiResponse(imagenData, baseModel)
|
|
} else {
|
|
scanner = bufio.NewScanner(httpResp.Body)
|
|
scanner.Buffer(nil, streamScannerBuffer)
|
|
}
|
|
claudeInputTokens := helps.NewClaudeInputTokenState(from, to, responseFormat, originalPayload)
|
|
var streamUsage helps.StreamUsageBuffer
|
|
streamUsage.SetRequestPayload(body)
|
|
defer streamUsage.Publish(ctx, reporter)
|
|
var streamErr error
|
|
defer func() {
|
|
_ = helps.StopTranslatedClaudeStream(ctx, &streamErr, reporter, &streamUsage, out)
|
|
}()
|
|
var terminalDelivered bool
|
|
var sawFinishReason bool
|
|
var replayCommitted bool
|
|
var pendingFilterLine []byte
|
|
var pendingFilterMsg string
|
|
var param any
|
|
zeroOutputGuard := helps.NewGeminiZeroOutputStreamGuard(false, responseFormat)
|
|
failZeroOutput := func(finishReason string) bool {
|
|
streamErr = newGeminiZeroOutputStatusErr(helps.GeminiZeroOutputErrorMessage(responseFormat, finishReason))
|
|
helps.RecordAPIResponseError(ctx, e.cfg, streamErr)
|
|
_ = helps.StopTranslatedClaudeStream(ctx, &streamErr, reporter, &streamUsage, out)
|
|
return false
|
|
}
|
|
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)
|
|
var usageFilter vertexSSEUsageFilter
|
|
emitTranslatedLine := func(line []byte) bool {
|
|
filtered := usageFilter.filter(line)
|
|
lines := helps.TranslateStreamWithClaudeInputTokens(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, bytes.Clone(filtered), ¶m, claudeInputTokens)
|
|
helps.RecordApplyPatchStreamFailure(ctx, param, reporter, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage})
|
|
helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error {
|
|
return newGeminiTranslatedClaudeErrorStatusErr(msg)
|
|
})
|
|
if replayAccumulator != nil && replayAccumulator.terminal && !replayCommitted && antigravityStreamChunksHaveTerminalCommitEvent(lines) {
|
|
commitReplay()
|
|
}
|
|
for i := range lines {
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}:
|
|
if len(lines[i]) > 0 {
|
|
zeroOutputGuard.MarkEmitted()
|
|
}
|
|
if helps.IsGeminiTerminalStreamChunk(lines[i]) {
|
|
terminalDelivered = true
|
|
}
|
|
case <-ctx.Done():
|
|
return false
|
|
}
|
|
}
|
|
return !helps.StopApplyPatchStream(ctx, param, reporter, out, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage})
|
|
}
|
|
guardAndEmitLine := func(line []byte) bool {
|
|
shells, action, finishReason := zeroOutputGuard.Observe(line)
|
|
switch action {
|
|
case helps.GeminiZeroOutputContinue:
|
|
return true
|
|
case helps.GeminiZeroOutputFailNow:
|
|
return failZeroOutput(finishReason)
|
|
case helps.GeminiZeroOutputEmitErrorThenFail:
|
|
errFrame := zeroOutputGuard.AbnormalFrame()
|
|
if len(errFrame) == 0 {
|
|
errFrame = line
|
|
}
|
|
if !emitTranslatedLine(errFrame) {
|
|
return false
|
|
}
|
|
return failZeroOutput(finishReason)
|
|
default:
|
|
for _, shell := range shells {
|
|
if !emitTranslatedLine(shell) {
|
|
return false
|
|
}
|
|
}
|
|
return emitTranslatedLine(line)
|
|
}
|
|
}
|
|
for {
|
|
var line []byte
|
|
if isImagenModel(baseModel) {
|
|
if imagenLine == nil {
|
|
break
|
|
}
|
|
line = imagenLine
|
|
imagenLine = nil
|
|
} else {
|
|
if !scanner.Scan() {
|
|
break
|
|
}
|
|
line = scanner.Bytes()
|
|
}
|
|
if !isImagenModel(baseModel) {
|
|
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 && !ojson.ValidBytes(rawPayload) {
|
|
continue
|
|
}
|
|
if filterMsg, blocked := clienterror.GeminiFilterBlockMessage(rawPayload); blocked {
|
|
pendingFilterLine = bytes.Clone(line)
|
|
pendingFilterMsg = filterMsg
|
|
continue
|
|
}
|
|
if blockMsg, blocked := geminiPromptBlockErrorMessage(rawPayload); blocked {
|
|
streamUsage.ObserveGeminiStream(line)
|
|
blockErr := statusErr{code: http.StatusBadGateway, msg: blockMsg}
|
|
helps.RecordAPIResponseError(ctx, e.cfg, blockErr)
|
|
if !streamUsage.PublishFailure(ctx, reporter, blockErr) {
|
|
reporter.PublishFailure(ctx, blockErr)
|
|
}
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Err: blockErr}:
|
|
case <-ctx.Done():
|
|
}
|
|
return
|
|
}
|
|
if len(rawPayload) > 0 && len(pendingFilterLine) > 0 {
|
|
flushed := pendingFilterLine
|
|
pendingFilterLine = nil
|
|
pendingFilterMsg = ""
|
|
if !sawFinishReason && geminiStreamHasFinishSignal(helps.JSONPayload(flushed)) {
|
|
sawFinishReason = true
|
|
}
|
|
if !guardAndEmitLine(flushed) {
|
|
return
|
|
}
|
|
}
|
|
streamUsage.ObserveGeminiStream(line)
|
|
if !sawFinishReason && geminiStreamHasFinishSignal(rawPayload) {
|
|
sawFinishReason = true
|
|
}
|
|
if len(rawPayload) == 0 {
|
|
continue
|
|
}
|
|
if !guardAndEmitLine(line) {
|
|
return
|
|
}
|
|
}
|
|
if pendingFilterMsg != "" && (isImagenModel(baseModel) || scanner.Err() == nil) && !sawFinishReason {
|
|
filterErr := statusErr{code: http.StatusBadRequest, msg: pendingFilterMsg}
|
|
helps.RecordAPIResponseError(ctx, e.cfg, filterErr)
|
|
if !streamUsage.PublishFailure(ctx, reporter, filterErr) {
|
|
reporter.PublishFailure(ctx, filterErr)
|
|
}
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Err: filterErr}:
|
|
case <-ctx.Done():
|
|
}
|
|
return
|
|
}
|
|
if (scanner == nil || scanner.Err() == nil) && len(pendingFilterLine) > 0 {
|
|
if !guardAndEmitLine(pendingFilterLine) {
|
|
return
|
|
}
|
|
pendingFilterLine = nil
|
|
pendingFilterMsg = ""
|
|
}
|
|
if (isImagenModel(baseModel) || scanner.Err() == nil) && ctx.Err() == nil && zeroOutputGuard.HasAbnormalFinish() {
|
|
_, action, finishReason := zeroOutputGuard.Finalize()
|
|
if action == helps.GeminiZeroOutputEmitErrorThenFail {
|
|
errFrame := zeroOutputGuard.AbnormalFrame()
|
|
if len(errFrame) == 0 {
|
|
errFrame = []byte("[DONE]")
|
|
}
|
|
if !emitTranslatedLine(errFrame) {
|
|
return
|
|
}
|
|
}
|
|
failZeroOutput(finishReason)
|
|
return
|
|
}
|
|
if helps.EndApplyPatchStream(ctx, param, reporter, out, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage}) {
|
|
return
|
|
}
|
|
if helps.StopTranslatedClaudeStream(ctx, &streamErr, reporter, &streamUsage, out) {
|
|
return
|
|
}
|
|
if errScan := func() error {
|
|
if scanner == nil {
|
|
return nil
|
|
}
|
|
return scanner.Err()
|
|
}(); errScan != nil {
|
|
if terminalDelivered && errors.Is(errScan, context.Canceled) {
|
|
return
|
|
}
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errScan)
|
|
reporter.PublishFailure(ctx, errScan)
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Err: errScan}:
|
|
case <-ctx.Done():
|
|
}
|
|
return
|
|
}
|
|
if ctx.Err() != nil {
|
|
return
|
|
}
|
|
shells, action, finishReason := zeroOutputGuard.Finalize()
|
|
switch action {
|
|
case helps.GeminiZeroOutputFailNow:
|
|
failZeroOutput(finishReason)
|
|
return
|
|
case helps.GeminiZeroOutputEmitErrorThenFail:
|
|
errFrame := zeroOutputGuard.AbnormalFrame()
|
|
if len(errFrame) == 0 {
|
|
errFrame = []byte("[DONE]")
|
|
}
|
|
if !emitTranslatedLine(errFrame) {
|
|
return
|
|
}
|
|
failZeroOutput(finishReason)
|
|
return
|
|
default:
|
|
for _, shell := range shells {
|
|
if !emitTranslatedLine(shell) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
if (responseFormat == sdktranslator.FormatClaude || responseFormat == sdktranslator.FormatOpenAI) && !sawFinishReason {
|
|
truncErr := statusErr{code: http.StatusBadGateway, msg: "upstream stream closed before finishReason", streamTruncated: true}
|
|
helps.RecordAPIResponseError(ctx, e.cfg, truncErr)
|
|
if !streamUsage.PublishFailure(ctx, reporter, truncErr) {
|
|
reporter.PublishFailure(ctx, truncErr)
|
|
}
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Err: truncErr}:
|
|
case <-ctx.Done():
|
|
}
|
|
return
|
|
}
|
|
lines := helps.TranslateStreamWithClaudeInputTokens(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, []byte("[DONE]"), ¶m, claudeInputTokens)
|
|
helps.RecordApplyPatchStreamFailure(ctx, param, reporter, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage})
|
|
helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error {
|
|
return newGeminiTranslatedClaudeErrorStatusErr(msg)
|
|
})
|
|
commitReplay()
|
|
for i := range lines {
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}:
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
if helps.StopApplyPatchStream(ctx, param, reporter, out, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage}) {
|
|
return
|
|
}
|
|
if helps.StopTranslatedClaudeStream(ctx, &streamErr, reporter, &streamUsage, out) {
|
|
return
|
|
}
|
|
}()
|
|
return &cliproxyexecutor.StreamResult{Headers: httpResp.Header.Clone(), Chunks: out}, nil
|
|
}
|
|
|
|
// executeStreamWithAPIKey handles streaming authentication using API key credentials.
|
|
func (e *GeminiVertexExecutor) executeStreamWithAPIKey(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, apiKey, baseURL string) (_ *cliproxyexecutor.StreamResult, err error) {
|
|
baseModel := thinking.ParseSuffix(req.Model).ModelName
|
|
|
|
reporter := helps.NewExecutorUsageReporter(ctx, e, baseModel, auth)
|
|
defer reporter.TrackFailure(ctx, &err)
|
|
|
|
from := opts.SourceFormat
|
|
responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts)
|
|
to := sdktranslator.FromString("gemini")
|
|
|
|
originalPayloadSource := req.Payload
|
|
if len(opts.OriginalRequest) > 0 {
|
|
originalPayloadSource = opts.OriginalRequest
|
|
}
|
|
originalPayload := originalPayloadSource
|
|
var body []byte
|
|
var originalTranslated []byte
|
|
var doc *ojson.Doc
|
|
if isImagenModel(baseModel) {
|
|
body, originalTranslated, err = e.translateImagenRequest(ctx, req, opts, baseModel, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
doc = ojson.NewDoc(body)
|
|
} else {
|
|
originalTranslated = helps.TranslateRequestWithCodexMultiAgentV2(ctx, opts.Headers, e.cfg, from, to, baseModel, originalPayload, true)
|
|
body, err = helps.TranslateRequestReturningError(ctx, opts.Headers, e.cfg, from, to, baseModel, req.Payload, true, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
doc = ojson.NewDoc(body)
|
|
if err = helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier()); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fixGeminiImageAspectRatioDoc(baseModel, doc)
|
|
helps.SetStringIfDifferentDoc(doc, "model", baseModel)
|
|
internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents")
|
|
}
|
|
requestedModel := helps.PayloadRequestedModel(opts, req.Model)
|
|
requestPath := helps.PayloadRequestPath(opts)
|
|
|
|
action := getVertexAction(baseModel, true)
|
|
if !isImagenModel(baseModel) {
|
|
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
|
|
}
|
|
}
|
|
if !isImagenModel(baseModel) {
|
|
helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String())
|
|
}
|
|
// For API key auth, use simpler URL format without project/location
|
|
endpointURL := vertexAPIKeyModelsURL(baseURL, baseModel, action)
|
|
// Imagen models don't support streaming, skip SSE params
|
|
if !isImagenModel(baseModel) {
|
|
if opts.Alt == "" {
|
|
endpointURL = endpointURL + "?alt=sse"
|
|
} else {
|
|
endpointURL = endpointURL + "?$alt=" + url.QueryEscape(opts.Alt)
|
|
}
|
|
}
|
|
_ = doc.Delete("session_id")
|
|
|
|
helps.ApplyPayloadConfigWithRequestDoc(e.cfg, baseModel, to.String(), from.String(), "", doc, originalTranslated, requestedModel, requestPath, opts.Headers)
|
|
body = doc.Bytes()
|
|
reporter.SetTranslatedReasoningEffort(body, to.String())
|
|
httpReq, errNewReq := http.NewRequestWithContext(ctx, http.MethodPost, endpointURL, bytes.NewReader(body))
|
|
if errNewReq != nil {
|
|
return nil, errNewReq
|
|
}
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
if apiKey != "" {
|
|
httpReq.Header.Set("x-goog-api-key", apiKey)
|
|
}
|
|
applyGeminiHeaders(httpReq, auth, opts.Headers)
|
|
var attrs map[string]string
|
|
if auth != nil {
|
|
attrs = auth.Attributes
|
|
}
|
|
util.ApplyCustomHeadersFromAttrs(httpReq, attrs, opts.Headers)
|
|
|
|
var authID, authLabel, authType, authValue string
|
|
if auth != nil {
|
|
authID = auth.ID
|
|
authLabel = auth.Label
|
|
authType, authValue = auth.AccountInfo()
|
|
}
|
|
helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{
|
|
URL: endpointURL,
|
|
Method: http.MethodPost,
|
|
Headers: httpReq.Header.Clone(),
|
|
Body: body,
|
|
Provider: e.Identifier(),
|
|
AuthID: authID,
|
|
AuthLabel: authLabel,
|
|
AuthType: authType,
|
|
AuthValue: authValue,
|
|
})
|
|
|
|
httpClient := helps.NewProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
|
|
httpClient = reporter.TrackHTTPClient(httpClient)
|
|
httpResp, errDo := httpClient.Do(httpReq)
|
|
if errDo != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errDo)
|
|
return nil, errDo
|
|
}
|
|
helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())
|
|
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
|
|
b, errRead := io.ReadAll(httpResp.Body)
|
|
helps.AppendAPIResponseChunk(ctx, e.cfg, b)
|
|
if errRead != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
|
|
}
|
|
helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), b))
|
|
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, newGeminiStatusErr(httpResp.StatusCode, httpResp.Header, b)
|
|
}
|
|
|
|
replayAccumulator := newAntigravityReasoningReplayAccumulator(replayScope, replayRequestPayload)
|
|
out := make(chan cliproxyexecutor.StreamChunk, 1)
|
|
go func() {
|
|
defer close(out)
|
|
defer reporter.EnsurePublished(ctx)
|
|
defer func() {
|
|
if errClose := httpResp.Body.Close(); errClose != nil {
|
|
log.Errorf("vertex executor: close response body error: %v", errClose)
|
|
}
|
|
}()
|
|
var scanner *bufio.Scanner
|
|
var imagenLine []byte
|
|
if isImagenModel(baseModel) {
|
|
imagenData, errRead := readVertexImagenResponse(httpResp.Body)
|
|
helps.AppendAPIResponseChunk(ctx, e.cfg, imagenData)
|
|
if errRead != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
|
|
reporter.PublishFailure(ctx, errRead)
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Err: errRead}:
|
|
case <-ctx.Done():
|
|
}
|
|
return
|
|
}
|
|
imagenLine = convertImagenToGeminiResponse(imagenData, baseModel)
|
|
} else {
|
|
scanner = bufio.NewScanner(httpResp.Body)
|
|
scanner.Buffer(nil, streamScannerBuffer)
|
|
}
|
|
claudeInputTokens := helps.NewClaudeInputTokenState(from, to, responseFormat, originalPayload)
|
|
var streamUsage helps.StreamUsageBuffer
|
|
streamUsage.SetRequestPayload(body)
|
|
defer streamUsage.Publish(ctx, reporter)
|
|
var streamErr error
|
|
defer func() {
|
|
_ = helps.StopTranslatedClaudeStream(ctx, &streamErr, reporter, &streamUsage, out)
|
|
}()
|
|
var terminalDelivered bool
|
|
var sawFinishReason bool
|
|
var replayCommitted bool
|
|
var pendingFilterLine []byte
|
|
var pendingFilterMsg string
|
|
var param any
|
|
zeroOutputGuard := helps.NewGeminiZeroOutputStreamGuard(false, responseFormat)
|
|
failZeroOutput := func(finishReason string) bool {
|
|
streamErr = newGeminiZeroOutputStatusErr(helps.GeminiZeroOutputErrorMessage(responseFormat, finishReason))
|
|
helps.RecordAPIResponseError(ctx, e.cfg, streamErr)
|
|
_ = helps.StopTranslatedClaudeStream(ctx, &streamErr, reporter, &streamUsage, out)
|
|
return false
|
|
}
|
|
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)
|
|
var usageFilter vertexSSEUsageFilter
|
|
emitTranslatedLine := func(line []byte) bool {
|
|
filtered := usageFilter.filter(line)
|
|
lines := helps.TranslateStreamWithClaudeInputTokens(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, bytes.Clone(filtered), ¶m, claudeInputTokens)
|
|
helps.RecordApplyPatchStreamFailure(ctx, param, reporter, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage})
|
|
helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error {
|
|
return newGeminiTranslatedClaudeErrorStatusErr(msg)
|
|
})
|
|
if replayAccumulator != nil && replayAccumulator.terminal && !replayCommitted && antigravityStreamChunksHaveTerminalCommitEvent(lines) {
|
|
commitReplay()
|
|
}
|
|
for i := range lines {
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}:
|
|
if len(lines[i]) > 0 {
|
|
zeroOutputGuard.MarkEmitted()
|
|
}
|
|
if helps.IsGeminiTerminalStreamChunk(lines[i]) {
|
|
terminalDelivered = true
|
|
}
|
|
case <-ctx.Done():
|
|
return false
|
|
}
|
|
}
|
|
return !helps.StopApplyPatchStream(ctx, param, reporter, out, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage})
|
|
}
|
|
guardAndEmitLine := func(line []byte) bool {
|
|
shells, action, finishReason := zeroOutputGuard.Observe(line)
|
|
switch action {
|
|
case helps.GeminiZeroOutputContinue:
|
|
return true
|
|
case helps.GeminiZeroOutputFailNow:
|
|
return failZeroOutput(finishReason)
|
|
case helps.GeminiZeroOutputEmitErrorThenFail:
|
|
errFrame := zeroOutputGuard.AbnormalFrame()
|
|
if len(errFrame) == 0 {
|
|
errFrame = line
|
|
}
|
|
if !emitTranslatedLine(errFrame) {
|
|
return false
|
|
}
|
|
return failZeroOutput(finishReason)
|
|
default:
|
|
for _, shell := range shells {
|
|
if !emitTranslatedLine(shell) {
|
|
return false
|
|
}
|
|
}
|
|
return emitTranslatedLine(line)
|
|
}
|
|
}
|
|
for {
|
|
var line []byte
|
|
if isImagenModel(baseModel) {
|
|
if imagenLine == nil {
|
|
break
|
|
}
|
|
line = imagenLine
|
|
imagenLine = nil
|
|
} else {
|
|
if !scanner.Scan() {
|
|
break
|
|
}
|
|
line = scanner.Bytes()
|
|
}
|
|
if !isImagenModel(baseModel) {
|
|
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 && !ojson.ValidBytes(rawPayload) {
|
|
continue
|
|
}
|
|
if filterMsg, blocked := clienterror.GeminiFilterBlockMessage(rawPayload); blocked {
|
|
pendingFilterLine = bytes.Clone(line)
|
|
pendingFilterMsg = filterMsg
|
|
continue
|
|
}
|
|
if blockMsg, blocked := geminiPromptBlockErrorMessage(rawPayload); blocked {
|
|
streamUsage.ObserveGeminiStream(line)
|
|
blockErr := statusErr{code: http.StatusBadGateway, msg: blockMsg}
|
|
helps.RecordAPIResponseError(ctx, e.cfg, blockErr)
|
|
if !streamUsage.PublishFailure(ctx, reporter, blockErr) {
|
|
reporter.PublishFailure(ctx, blockErr)
|
|
}
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Err: blockErr}:
|
|
case <-ctx.Done():
|
|
}
|
|
return
|
|
}
|
|
if len(rawPayload) > 0 && len(pendingFilterLine) > 0 {
|
|
flushed := pendingFilterLine
|
|
pendingFilterLine = nil
|
|
pendingFilterMsg = ""
|
|
if !sawFinishReason && geminiStreamHasFinishSignal(helps.JSONPayload(flushed)) {
|
|
sawFinishReason = true
|
|
}
|
|
if !guardAndEmitLine(flushed) {
|
|
return
|
|
}
|
|
}
|
|
streamUsage.ObserveGeminiStream(line)
|
|
if !sawFinishReason && geminiStreamHasFinishSignal(rawPayload) {
|
|
sawFinishReason = true
|
|
}
|
|
if len(rawPayload) == 0 {
|
|
continue
|
|
}
|
|
if !guardAndEmitLine(line) {
|
|
return
|
|
}
|
|
}
|
|
if pendingFilterMsg != "" && (isImagenModel(baseModel) || scanner.Err() == nil) && !sawFinishReason {
|
|
filterErr := statusErr{code: http.StatusBadRequest, msg: pendingFilterMsg}
|
|
helps.RecordAPIResponseError(ctx, e.cfg, filterErr)
|
|
if !streamUsage.PublishFailure(ctx, reporter, filterErr) {
|
|
reporter.PublishFailure(ctx, filterErr)
|
|
}
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Err: filterErr}:
|
|
case <-ctx.Done():
|
|
}
|
|
return
|
|
}
|
|
if (scanner == nil || scanner.Err() == nil) && len(pendingFilterLine) > 0 {
|
|
if !guardAndEmitLine(pendingFilterLine) {
|
|
return
|
|
}
|
|
pendingFilterLine = nil
|
|
pendingFilterMsg = ""
|
|
}
|
|
if (isImagenModel(baseModel) || scanner.Err() == nil) && ctx.Err() == nil && zeroOutputGuard.HasAbnormalFinish() {
|
|
_, action, finishReason := zeroOutputGuard.Finalize()
|
|
if action == helps.GeminiZeroOutputEmitErrorThenFail {
|
|
errFrame := zeroOutputGuard.AbnormalFrame()
|
|
if len(errFrame) == 0 {
|
|
errFrame = []byte("[DONE]")
|
|
}
|
|
if !emitTranslatedLine(errFrame) {
|
|
return
|
|
}
|
|
}
|
|
failZeroOutput(finishReason)
|
|
return
|
|
}
|
|
if helps.EndApplyPatchStream(ctx, param, reporter, out, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage}) {
|
|
return
|
|
}
|
|
if helps.StopTranslatedClaudeStream(ctx, &streamErr, reporter, &streamUsage, out) {
|
|
return
|
|
}
|
|
if errScan := func() error {
|
|
if scanner == nil {
|
|
return nil
|
|
}
|
|
return scanner.Err()
|
|
}(); errScan != nil {
|
|
if terminalDelivered && errors.Is(errScan, context.Canceled) {
|
|
return
|
|
}
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errScan)
|
|
reporter.PublishFailure(ctx, errScan)
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Err: errScan}:
|
|
case <-ctx.Done():
|
|
}
|
|
return
|
|
}
|
|
if ctx.Err() != nil {
|
|
return
|
|
}
|
|
shells, action, finishReason := zeroOutputGuard.Finalize()
|
|
switch action {
|
|
case helps.GeminiZeroOutputFailNow:
|
|
failZeroOutput(finishReason)
|
|
return
|
|
case helps.GeminiZeroOutputEmitErrorThenFail:
|
|
errFrame := zeroOutputGuard.AbnormalFrame()
|
|
if len(errFrame) == 0 {
|
|
errFrame = []byte("[DONE]")
|
|
}
|
|
if !emitTranslatedLine(errFrame) {
|
|
return
|
|
}
|
|
failZeroOutput(finishReason)
|
|
return
|
|
default:
|
|
for _, shell := range shells {
|
|
if !emitTranslatedLine(shell) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
if (responseFormat == sdktranslator.FormatClaude || responseFormat == sdktranslator.FormatOpenAI) && !sawFinishReason {
|
|
truncErr := statusErr{code: http.StatusBadGateway, msg: "upstream stream closed before finishReason", streamTruncated: true}
|
|
helps.RecordAPIResponseError(ctx, e.cfg, truncErr)
|
|
if !streamUsage.PublishFailure(ctx, reporter, truncErr) {
|
|
reporter.PublishFailure(ctx, truncErr)
|
|
}
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Err: truncErr}:
|
|
case <-ctx.Done():
|
|
}
|
|
return
|
|
}
|
|
lines := helps.TranslateStreamWithClaudeInputTokens(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, []byte("[DONE]"), ¶m, claudeInputTokens)
|
|
helps.RecordApplyPatchStreamFailure(ctx, param, reporter, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage})
|
|
helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error {
|
|
return newGeminiTranslatedClaudeErrorStatusErr(msg)
|
|
})
|
|
commitReplay()
|
|
for i := range lines {
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}:
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
if helps.StopApplyPatchStream(ctx, param, reporter, out, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage}) {
|
|
return
|
|
}
|
|
if helps.StopTranslatedClaudeStream(ctx, &streamErr, reporter, &streamUsage, out) {
|
|
return
|
|
}
|
|
}()
|
|
return &cliproxyexecutor.StreamResult{Headers: httpResp.Header.Clone(), Chunks: out}, nil
|
|
}
|
|
|
|
// countTokensWithServiceAccount counts tokens using service account credentials.
|
|
func (e *GeminiVertexExecutor) countTokensWithServiceAccount(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, projectID, location string, saJSON []byte) (cliproxyexecutor.Response, error) {
|
|
baseModel := thinking.ParseSuffix(req.Model).ModelName
|
|
|
|
from := opts.SourceFormat
|
|
responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts)
|
|
to := sdktranslator.FromString("gemini")
|
|
|
|
translatedReq, err := helps.TranslateRequestReturningError(ctx, opts.Headers, e.cfg, from, to, baseModel, req.Payload, false, false)
|
|
if err != nil {
|
|
return cliproxyexecutor.Response{}, err
|
|
}
|
|
originalTranslatedForPayload := append([]byte(nil), translatedReq...)
|
|
if len(opts.OriginalRequest) > 0 {
|
|
originalTranslatedForPayload = helps.TranslateRequestWithCodexMultiAgentV2(ctx, opts.Headers, e.cfg, from, to, baseModel, opts.OriginalRequest, false)
|
|
}
|
|
|
|
doc := ojson.NewDoc(translatedReq)
|
|
if err = helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier()); err != nil {
|
|
return cliproxyexecutor.Response{}, err
|
|
}
|
|
|
|
fixGeminiImageAspectRatioDoc(baseModel, doc)
|
|
_ = doc.Set("model", baseModel)
|
|
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")
|
|
|
|
ctxWithFinalizer := helps.WithPayloadFinalizer(ctx, helps.NewPayloadFinalizer(e.cfg, e.Identifier(), baseModel, to.String(), "", originalTranslatedForPayload, req, opts))
|
|
helps.FinalizePayloadDoc(ctxWithFinalizer, doc)
|
|
translatedReq = doc.Bytes()
|
|
httpReq, errNewReq := http.NewRequestWithContext(respCtx, http.MethodPost, url, bytes.NewReader(translatedReq))
|
|
if errNewReq != nil {
|
|
return cliproxyexecutor.Response{}, errNewReq
|
|
}
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
if token, errTok := vertexAccessToken(ctx, e.cfg, auth, saJSON); errTok == nil && token != "" {
|
|
httpReq.Header.Set("Authorization", "Bearer "+token)
|
|
} else if errTok != nil {
|
|
log.Errorf("vertex executor: access token error: %v", errTok)
|
|
return cliproxyexecutor.Response{}, statusErr{code: 500, msg: "internal server error"}
|
|
}
|
|
applyGeminiHeaders(httpReq, auth, opts.Headers)
|
|
var attrs map[string]string
|
|
if auth != nil {
|
|
attrs = auth.Attributes
|
|
}
|
|
util.ApplyCustomHeadersFromAttrs(httpReq, attrs, opts.Headers)
|
|
|
|
var authID, authLabel, authType, authValue string
|
|
if auth != nil {
|
|
authID = auth.ID
|
|
authLabel = auth.Label
|
|
authType, authValue = auth.AccountInfo()
|
|
}
|
|
helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{
|
|
URL: url,
|
|
Method: http.MethodPost,
|
|
Headers: httpReq.Header.Clone(),
|
|
Body: translatedReq,
|
|
Provider: e.Identifier(),
|
|
AuthID: authID,
|
|
AuthLabel: authLabel,
|
|
AuthType: authType,
|
|
AuthValue: authValue,
|
|
})
|
|
|
|
httpClient := helps.NewProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
|
|
cliproxyexecutor.MarkUpstreamAttempt(ctx)
|
|
httpResp, errDo := httpClient.Do(httpReq)
|
|
if errDo != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errDo)
|
|
return cliproxyexecutor.Response{}, errDo
|
|
}
|
|
defer func() {
|
|
if errClose := httpResp.Body.Close(); errClose != nil {
|
|
log.Errorf("vertex executor: close response body error: %v", errClose)
|
|
}
|
|
}()
|
|
helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())
|
|
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
|
|
b, errRead := io.ReadAll(httpResp.Body)
|
|
helps.AppendAPIResponseChunk(ctx, e.cfg, b)
|
|
if errRead != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
|
|
}
|
|
helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), b))
|
|
return cliproxyexecutor.Response{}, newGeminiStatusErr(httpResp.StatusCode, httpResp.Header, b)
|
|
}
|
|
data, errRead := io.ReadAll(httpResp.Body)
|
|
helps.AppendAPIResponseChunk(ctx, e.cfg, data)
|
|
if errRead != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
|
|
return cliproxyexecutor.Response{}, errRead
|
|
}
|
|
count := ojson.GetBytesNoCopy(data, "totalTokens").Int()
|
|
out := sdktranslator.TranslateTokenCount(ctx, to, responseFormat, count, data)
|
|
return cliproxyexecutor.Response{Payload: out, Headers: httpResp.Header.Clone()}, nil
|
|
}
|
|
|
|
// countTokensWithAPIKey handles token counting using API key credentials.
|
|
func (e *GeminiVertexExecutor) countTokensWithAPIKey(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, apiKey, baseURL string) (cliproxyexecutor.Response, error) {
|
|
baseModel := thinking.ParseSuffix(req.Model).ModelName
|
|
|
|
from := opts.SourceFormat
|
|
responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts)
|
|
to := sdktranslator.FromString("gemini")
|
|
|
|
translatedReq, err := helps.TranslateRequestReturningError(ctx, opts.Headers, e.cfg, from, to, baseModel, req.Payload, false, false)
|
|
if err != nil {
|
|
return cliproxyexecutor.Response{}, err
|
|
}
|
|
originalTranslatedForPayload := append([]byte(nil), translatedReq...)
|
|
if len(opts.OriginalRequest) > 0 {
|
|
originalTranslatedForPayload = helps.TranslateRequestWithCodexMultiAgentV2(ctx, opts.Headers, e.cfg, from, to, baseModel, opts.OriginalRequest, false)
|
|
}
|
|
|
|
doc := ojson.NewDoc(translatedReq)
|
|
if err = helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier()); err != nil {
|
|
return cliproxyexecutor.Response{}, err
|
|
}
|
|
|
|
fixGeminiImageAspectRatioDoc(baseModel, doc)
|
|
_ = doc.Set("model", baseModel)
|
|
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")
|
|
|
|
ctxWithFinalizer := helps.WithPayloadFinalizer(ctx, helps.NewPayloadFinalizer(e.cfg, e.Identifier(), baseModel, to.String(), "", originalTranslatedForPayload, req, opts))
|
|
helps.FinalizePayloadDoc(ctxWithFinalizer, doc)
|
|
translatedReq = doc.Bytes()
|
|
httpReq, errNewReq := http.NewRequestWithContext(respCtx, http.MethodPost, url, bytes.NewReader(translatedReq))
|
|
if errNewReq != nil {
|
|
return cliproxyexecutor.Response{}, errNewReq
|
|
}
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
if apiKey != "" {
|
|
httpReq.Header.Set("x-goog-api-key", apiKey)
|
|
}
|
|
applyGeminiHeaders(httpReq, auth, opts.Headers)
|
|
var attrs map[string]string
|
|
if auth != nil {
|
|
attrs = auth.Attributes
|
|
}
|
|
util.ApplyCustomHeadersFromAttrs(httpReq, attrs, opts.Headers)
|
|
|
|
var authID, authLabel, authType, authValue string
|
|
if auth != nil {
|
|
authID = auth.ID
|
|
authLabel = auth.Label
|
|
authType, authValue = auth.AccountInfo()
|
|
}
|
|
helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{
|
|
URL: url,
|
|
Method: http.MethodPost,
|
|
Headers: httpReq.Header.Clone(),
|
|
Body: translatedReq,
|
|
Provider: e.Identifier(),
|
|
AuthID: authID,
|
|
AuthLabel: authLabel,
|
|
AuthType: authType,
|
|
AuthValue: authValue,
|
|
})
|
|
|
|
httpClient := helps.NewProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
|
|
cliproxyexecutor.MarkUpstreamAttempt(ctx)
|
|
httpResp, errDo := httpClient.Do(httpReq)
|
|
if errDo != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errDo)
|
|
return cliproxyexecutor.Response{}, errDo
|
|
}
|
|
defer func() {
|
|
if errClose := httpResp.Body.Close(); errClose != nil {
|
|
log.Errorf("vertex executor: close response body error: %v", errClose)
|
|
}
|
|
}()
|
|
helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())
|
|
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
|
|
b, errRead := io.ReadAll(httpResp.Body)
|
|
helps.AppendAPIResponseChunk(ctx, e.cfg, b)
|
|
if errRead != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
|
|
}
|
|
helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), b))
|
|
return cliproxyexecutor.Response{}, newGeminiStatusErr(httpResp.StatusCode, httpResp.Header, b)
|
|
}
|
|
data, errRead := io.ReadAll(httpResp.Body)
|
|
helps.AppendAPIResponseChunk(ctx, e.cfg, data)
|
|
if errRead != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
|
|
return cliproxyexecutor.Response{}, errRead
|
|
}
|
|
count := ojson.GetBytesNoCopy(data, "totalTokens").Int()
|
|
out := sdktranslator.TranslateTokenCount(ctx, to, responseFormat, count, data)
|
|
return cliproxyexecutor.Response{Payload: out, Headers: httpResp.Header.Clone()}, nil
|
|
}
|
|
|
|
// vertexCreds extracts project, location and raw service account JSON from auth metadata.
|
|
func vertexCreds(a *cliproxyauth.Auth) (projectID, location string, serviceAccountJSON []byte, err error) {
|
|
if a == nil || a.Metadata == nil {
|
|
return "", "", nil, fmt.Errorf("vertex executor: missing auth metadata")
|
|
}
|
|
if v, ok := a.Metadata["project_id"].(string); ok {
|
|
projectID = strings.TrimSpace(v)
|
|
}
|
|
if projectID == "" {
|
|
// Some service accounts may use "project"; still prefer standard field
|
|
if v, ok := a.Metadata["project"].(string); ok {
|
|
projectID = strings.TrimSpace(v)
|
|
}
|
|
}
|
|
if projectID == "" {
|
|
return "", "", nil, fmt.Errorf("vertex executor: missing project_id in credentials")
|
|
}
|
|
if v, ok := a.Metadata["location"].(string); ok && strings.TrimSpace(v) != "" {
|
|
location = strings.TrimSpace(v)
|
|
} else {
|
|
location = "global"
|
|
}
|
|
var sa map[string]any
|
|
if raw, ok := a.Metadata["service_account"].(map[string]any); ok {
|
|
sa = raw
|
|
}
|
|
if sa == nil {
|
|
return "", "", nil, fmt.Errorf("vertex executor: missing service_account in credentials")
|
|
}
|
|
normalized, errNorm := vertexauth.NormalizeServiceAccountMap(sa)
|
|
if errNorm != nil {
|
|
return "", "", nil, fmt.Errorf("vertex executor: %w", errNorm)
|
|
}
|
|
saJSON, errMarshal := json.Marshal(normalized)
|
|
if errMarshal != nil {
|
|
return "", "", nil, fmt.Errorf("vertex executor: marshal service_account failed: %w", errMarshal)
|
|
}
|
|
return projectID, location, saJSON, nil
|
|
}
|
|
|
|
// vertexAPICreds extracts API key and base URL from auth attributes following the claudeCreds pattern.
|
|
func vertexAPICreds(a *cliproxyauth.Auth) (apiKey, baseURL string) {
|
|
if a == nil {
|
|
return "", ""
|
|
}
|
|
if a.Attributes != nil {
|
|
apiKey = a.Attributes["api_key"]
|
|
baseURL = a.Attributes["base_url"]
|
|
}
|
|
if apiKey == "" && a.Metadata != nil {
|
|
if v, ok := a.Metadata["access_token"].(string); ok {
|
|
apiKey = v
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// vertexAPIKeyModelsURL builds the API-key models endpoint URL.
|
|
//
|
|
// The configured base URL is treated as a full resource prefix when it
|
|
// already contains a "/publishers/" segment (e.g. a real Vertex endpoint
|
|
// such as "https://aiplatform.googleapis.com/v1/projects/{p}/locations/{l}/publishers/google/models"),
|
|
// in which case only "/{model}:{action}" is appended. Otherwise the default
|
|
// "/v1/publishers/google/models" segment is inserted so short third-party
|
|
// hosts keep working; if the base already ends in a version segment such
|
|
// as "/v1" or "/v1beta1", only "/publishers/google/models" is appended to
|
|
// avoid producing a duplicated version path.
|
|
func vertexAPIKeyModelsURL(baseURL, model, action string) string {
|
|
base := strings.TrimRight(strings.TrimSpace(baseURL), "/")
|
|
if base == "" {
|
|
base = "https://aiplatform.googleapis.com"
|
|
}
|
|
if strings.Contains(base, "/publishers/") {
|
|
return fmt.Sprintf("%s/%s:%s", base, model, action)
|
|
}
|
|
if !vertexAPIVersionSuffix(base) {
|
|
base = fmt.Sprintf("%s/%s", base, vertexAPIVersion)
|
|
}
|
|
return fmt.Sprintf("%s/publishers/google/models/%s:%s", base, model, action)
|
|
}
|
|
|
|
// vertexAPIVersionSuffix reports whether the trimmed base URL's path already
|
|
// ends with a Google API version segment like "/v1" or "/v1beta1".
|
|
func vertexAPIVersionSuffix(base string) bool {
|
|
return helps.VertexAPIVersionSuffix(base)
|
|
}
|
|
|
|
func vertexBaseURL(location string) string {
|
|
loc := strings.TrimSpace(location)
|
|
if loc == "" || strings.EqualFold(loc, "global") {
|
|
return "https://aiplatform.googleapis.com"
|
|
} else if strings.EqualFold(loc, "us") || strings.EqualFold(loc, "eu") {
|
|
return fmt.Sprintf("https://aiplatform.%s.rep.googleapis.com", strings.ToLower(loc))
|
|
}
|
|
return fmt.Sprintf("https://%s-aiplatform.googleapis.com", loc)
|
|
}
|
|
|
|
func vertexAccessToken(ctx context.Context, cfg *config.Config, auth *cliproxyauth.Auth, saJSON []byte) (string, error) {
|
|
// Token exchange stays on the credential or global proxy. The execution proxy
|
|
// applies only to the model request that follows.
|
|
tokenCtx := cliproxyexecutor.WithoutRequestProxyURL(ctx)
|
|
if httpClient := helps.NewProxyAwareHTTPClient(tokenCtx, cfg, auth, 0); httpClient != nil {
|
|
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
|
|
}
|
|
// Use cloud-platform scope for Vertex AI.
|
|
creds, errCreds := google.CredentialsFromJSON(ctx, saJSON, "https://www.googleapis.com/auth/cloud-platform")
|
|
if errCreds != nil {
|
|
return "", fmt.Errorf("vertex executor: parse service account json failed: %w", errCreds)
|
|
}
|
|
tok, errTok := creds.TokenSource.Token()
|
|
if errTok != nil {
|
|
return "", fmt.Errorf("vertex executor: get access token failed: %w", errTok)
|
|
}
|
|
return tok.AccessToken, nil
|
|
}
|
|
|
|
// resolveVertexConfig finds the matching vertex-api-key configuration entry for the given auth.
|
|
func (e *GeminiVertexExecutor) resolveVertexConfig(auth *cliproxyauth.Auth) *config.VertexCompatKey {
|
|
return helps.ResolveVertexCompatConfig(e.cfg, auth)
|
|
}
|
|
|
|
// SupportsApplyPatch reports the actual executor contract, independent of its provider name.
|
|
func (e *GeminiVertexExecutor) SupportsApplyPatch() bool { return e != nil }
|
|
|
|
func shouldExecuteVertexInteractions(auth *cliproxyauth.Auth, opts cliproxyexecutor.Options) bool {
|
|
return opts.SourceFormat == sdktranslator.FormatInteractions && isNativeVertexInteractionsAuth(auth)
|
|
}
|
|
|
|
func isNativeVertexInteractionsAuth(auth *cliproxyauth.Auth) bool {
|
|
if auth == nil {
|
|
return false
|
|
}
|
|
if auth.Attributes != nil {
|
|
if v := strings.TrimSpace(auth.Attributes["interactions"]); strings.EqualFold(v, "true") || strings.EqualFold(v, "1") {
|
|
return true
|
|
}
|
|
}
|
|
if auth.Metadata != nil {
|
|
if v, ok := auth.Metadata["interactions"].(bool); ok && v {
|
|
return true
|
|
}
|
|
if v, ok := auth.Metadata["native_interactions"].(bool); ok && v {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func vertexInteractionsURL(baseURL, projectID string, isStream bool) string {
|
|
base := strings.TrimSpace(baseURL)
|
|
if base == "" {
|
|
base = "https://aiplatform.googleapis.com"
|
|
}
|
|
base = strings.TrimRight(base, "/")
|
|
var url string
|
|
if strings.TrimSpace(projectID) != "" {
|
|
url = fmt.Sprintf("%s/v1beta1/projects/%s/locations/global/interactions", base, strings.TrimSpace(projectID))
|
|
} else {
|
|
url = fmt.Sprintf("%s/v1beta1/interactions", base)
|
|
}
|
|
if isStream {
|
|
url = url + "?alt=sse"
|
|
}
|
|
return url
|
|
}
|
|
|
|
func (e *GeminiVertexExecutor) executeInteractions(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (resp cliproxyexecutor.Response, err error) {
|
|
targetName := thinking.ParseSuffix(req.Model).ModelName
|
|
reporter := helps.NewExecutorUsageReporter(ctx, e, targetName, auth)
|
|
defer reporter.TrackFailure(ctx, &err)
|
|
|
|
isCompat := helps.APIKeyModelIsCompat(req)
|
|
originalTranslated, body, errTranslate := helps.TranslateGeminiInteractionsRequestPair(ctx, e.cfg, targetName, req.Payload, opts, false, isCompat)
|
|
if errTranslate != nil {
|
|
return resp, errTranslate
|
|
}
|
|
doc := ojson.NewDoc(body)
|
|
if doc.Get("model").Exists() && targetName != "" {
|
|
helps.SetStringIfDifferentDoc(doc, "model", targetName)
|
|
}
|
|
if errThinking := helps.ApplyGeminiInteractionsThinkingDoc(doc, req, opts); errThinking != nil {
|
|
return resp, errThinking
|
|
}
|
|
requestedModel := helps.PayloadRequestedModel(opts, req.Model)
|
|
requestPath := helps.PayloadRequestPath(opts)
|
|
fromProtocol := opts.SourceFormat.String()
|
|
helps.SanitizeGeminiInteractionsUnsupportedInputIDsDoc(doc, true)
|
|
|
|
apiKey, baseURL := vertexAPICreds(auth)
|
|
var projectID string
|
|
var saJSON []byte
|
|
if apiKey == "" {
|
|
var errCreds error
|
|
projectID, _, saJSON, errCreds = vertexCreds(auth)
|
|
if errCreds != nil {
|
|
return resp, errCreds
|
|
}
|
|
if auth != nil && auth.Attributes != nil && auth.Attributes["base_url"] != "" {
|
|
baseURL = strings.TrimSpace(auth.Attributes["base_url"])
|
|
}
|
|
} else if auth != nil && auth.Metadata != nil {
|
|
if pid, ok := auth.Metadata["project_id"].(string); ok {
|
|
projectID = pid
|
|
}
|
|
}
|
|
|
|
url := vertexInteractionsURL(baseURL, projectID, false)
|
|
|
|
helps.ApplyPayloadConfigWithRequestDoc(e.cfg, targetName, "interactions", fromProtocol, "", doc, originalTranslated, requestedModel, requestPath, opts.Headers)
|
|
body = doc.Bytes()
|
|
reporter.SetTranslatedRequestMetadata(body, "interactions")
|
|
|
|
httpReq, errRequest := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
|
|
if errRequest != nil {
|
|
return resp, errRequest
|
|
}
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
if apiKey != "" {
|
|
httpReq.Header.Set("x-goog-api-key", apiKey)
|
|
} else {
|
|
token, errTok := vertexAccessToken(ctx, e.cfg, auth, saJSON)
|
|
if errTok != nil {
|
|
log.Errorf("vertex executor: interactions access token error: %v", errTok)
|
|
return resp, statusErr{code: 500, msg: "internal server error"}
|
|
}
|
|
if strings.TrimSpace(token) == "" {
|
|
return resp, statusErr{code: http.StatusUnauthorized, msg: "missing access token"}
|
|
}
|
|
httpReq.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
applyGeminiHeaders(httpReq, auth, opts.Headers)
|
|
helps.ApplyGeminiInteractionsRequestHeaders(httpReq, opts.Headers)
|
|
helps.ApplyGeminiInteractionsRevisionHeader(httpReq)
|
|
|
|
var attrs map[string]string
|
|
if auth != nil {
|
|
attrs = auth.Attributes
|
|
}
|
|
util.ApplyCustomHeadersFromAttrs(httpReq, attrs, opts.Headers)
|
|
|
|
var authID, authLabel, authType, authValue string
|
|
if auth != nil {
|
|
authID = auth.ID
|
|
authLabel = auth.Label
|
|
authType, authValue = auth.AccountInfo()
|
|
}
|
|
helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{
|
|
URL: url,
|
|
Method: http.MethodPost,
|
|
Headers: httpReq.Header.Clone(),
|
|
Body: body,
|
|
Provider: e.Identifier(),
|
|
AuthID: authID,
|
|
AuthLabel: authLabel,
|
|
AuthType: authType,
|
|
AuthValue: authValue,
|
|
})
|
|
|
|
httpClient := reporter.TrackHTTPClient(helps.NewProxyAwareHTTPClient(ctx, e.cfg, auth, 0))
|
|
httpResp, errDo := httpClient.Do(httpReq)
|
|
if errDo != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errDo)
|
|
return resp, errDo
|
|
}
|
|
defer func() {
|
|
if errClose := httpResp.Body.Close(); errClose != nil {
|
|
log.Errorf("vertex executor: close interactions response body error: %v", errClose)
|
|
}
|
|
}()
|
|
helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())
|
|
data, errRead := io.ReadAll(httpResp.Body)
|
|
helps.AppendAPIResponseChunk(ctx, e.cfg, data)
|
|
if errRead != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
|
|
return resp, errRead
|
|
}
|
|
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
|
|
helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), data))
|
|
err = newGeminiStatusErr(httpResp.StatusCode, httpResp.Header, data)
|
|
return resp, err
|
|
}
|
|
reporter.ObserveResponseModel(data)
|
|
targetFormat := cliproxyexecutor.ResponseFormatOrSource(opts)
|
|
var param any
|
|
out := sdktranslator.TranslateNonStream(ctx, sdktranslator.FormatInteractions, targetFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, data, ¶m)
|
|
if helps.ApplyPatchTranslationError(param) != nil || len(out) == 0 {
|
|
return cliproxyexecutor.Response{}, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage}
|
|
}
|
|
reporter.Publish(ctx, helps.ParseInteractionsUsage(data))
|
|
if targetFormat == sdktranslator.FormatOpenAIResponse {
|
|
out = helps.EnsureResponsesUsageDetails(out)
|
|
}
|
|
return cliproxyexecutor.Response{Payload: out, Headers: httpResp.Header.Clone()}, nil
|
|
}
|
|
|
|
func (e *GeminiVertexExecutor) executeInteractionsStream(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (_ *cliproxyexecutor.StreamResult, err error) {
|
|
targetName := thinking.ParseSuffix(req.Model).ModelName
|
|
reporter := helps.NewExecutorUsageReporter(ctx, e, targetName, auth)
|
|
defer reporter.TrackFailure(ctx, &err)
|
|
|
|
isCompat := helps.APIKeyModelIsCompat(req)
|
|
originalTranslated, body, errTranslate := helps.TranslateGeminiInteractionsRequestPair(ctx, e.cfg, targetName, req.Payload, opts, true, isCompat)
|
|
if errTranslate != nil {
|
|
return nil, errTranslate
|
|
}
|
|
doc := ojson.NewDoc(body)
|
|
if doc.Get("model").Exists() && targetName != "" {
|
|
helps.SetStringIfDifferentDoc(doc, "model", targetName)
|
|
}
|
|
if errThinking := helps.ApplyGeminiInteractionsThinkingDoc(doc, req, opts); errThinking != nil {
|
|
return nil, errThinking
|
|
}
|
|
requestedModel := helps.PayloadRequestedModel(opts, req.Model)
|
|
requestPath := helps.PayloadRequestPath(opts)
|
|
fromProtocol := opts.SourceFormat.String()
|
|
helps.SanitizeGeminiInteractionsUnsupportedInputIDsDoc(doc, true)
|
|
helps.SetBoolIfDifferentDoc(doc, "stream", true)
|
|
|
|
apiKey, baseURL := vertexAPICreds(auth)
|
|
var projectID string
|
|
var saJSON []byte
|
|
if apiKey == "" {
|
|
var errCreds error
|
|
projectID, _, saJSON, errCreds = vertexCreds(auth)
|
|
if errCreds != nil {
|
|
return nil, errCreds
|
|
}
|
|
if auth != nil && auth.Attributes != nil && auth.Attributes["base_url"] != "" {
|
|
baseURL = strings.TrimSpace(auth.Attributes["base_url"])
|
|
}
|
|
} else if auth != nil && auth.Metadata != nil {
|
|
if pid, ok := auth.Metadata["project_id"].(string); ok {
|
|
projectID = pid
|
|
}
|
|
}
|
|
|
|
url := vertexInteractionsURL(baseURL, projectID, true)
|
|
|
|
helps.ApplyPayloadConfigWithRequestDoc(e.cfg, targetName, "interactions", fromProtocol, "", doc, originalTranslated, requestedModel, requestPath, opts.Headers)
|
|
body = doc.Bytes()
|
|
reporter.SetTranslatedRequestMetadata(body, "interactions")
|
|
|
|
httpReq, errRequest := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
|
|
if errRequest != nil {
|
|
return nil, errRequest
|
|
}
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
if apiKey != "" {
|
|
httpReq.Header.Set("x-goog-api-key", apiKey)
|
|
} else {
|
|
token, errTok := vertexAccessToken(ctx, e.cfg, auth, saJSON)
|
|
if errTok != nil {
|
|
log.Errorf("vertex executor: interactions access token error: %v", errTok)
|
|
return nil, statusErr{code: 500, msg: "internal server error"}
|
|
}
|
|
if strings.TrimSpace(token) == "" {
|
|
return nil, statusErr{code: http.StatusUnauthorized, msg: "missing access token"}
|
|
}
|
|
httpReq.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
applyGeminiHeaders(httpReq, auth, opts.Headers)
|
|
helps.ApplyGeminiInteractionsRequestHeaders(httpReq, opts.Headers)
|
|
helps.ApplyGeminiInteractionsRevisionHeader(httpReq)
|
|
|
|
var attrs map[string]string
|
|
if auth != nil {
|
|
attrs = auth.Attributes
|
|
}
|
|
util.ApplyCustomHeadersFromAttrs(httpReq, attrs, opts.Headers)
|
|
|
|
var authID, authLabel, authType, authValue string
|
|
if auth != nil {
|
|
authID = auth.ID
|
|
authLabel = auth.Label
|
|
authType, authValue = auth.AccountInfo()
|
|
}
|
|
helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{
|
|
URL: url,
|
|
Method: http.MethodPost,
|
|
Headers: httpReq.Header.Clone(),
|
|
Body: body,
|
|
Provider: e.Identifier(),
|
|
AuthID: authID,
|
|
AuthLabel: authLabel,
|
|
AuthType: authType,
|
|
AuthValue: authValue,
|
|
})
|
|
|
|
httpClient := reporter.TrackHTTPClient(helps.NewProxyAwareHTTPClient(ctx, e.cfg, auth, 0))
|
|
httpResp, errDo := httpClient.Do(httpReq)
|
|
if errDo != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errDo)
|
|
return nil, errDo
|
|
}
|
|
helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())
|
|
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
|
|
data, errRead := io.ReadAll(httpResp.Body)
|
|
if errClose := httpResp.Body.Close(); errClose != nil {
|
|
log.Errorf("vertex executor: close interactions error response body error: %v", errClose)
|
|
}
|
|
helps.AppendAPIResponseChunk(ctx, e.cfg, data)
|
|
if errRead != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
|
|
}
|
|
helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), data))
|
|
err = newGeminiStatusErr(httpResp.StatusCode, httpResp.Header, data)
|
|
return nil, err
|
|
}
|
|
|
|
out := make(chan cliproxyexecutor.StreamChunk)
|
|
responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts)
|
|
go func() {
|
|
defer close(out)
|
|
defer reporter.EnsurePublished(ctx)
|
|
defer func() {
|
|
if errClose := httpResp.Body.Close(); errClose != nil {
|
|
log.Errorf("vertex executor: close interactions stream body error: %v", errClose)
|
|
}
|
|
}()
|
|
scanner := bufio.NewScanner(httpResp.Body)
|
|
scanner.Buffer(nil, helps.StreamScannerBuffer)
|
|
scanner.Split(helps.ScanInteractionsSSELines)
|
|
originalRequest := opts.OriginalRequest
|
|
if len(originalRequest) == 0 {
|
|
originalRequest = req.Payload
|
|
}
|
|
claudeInputTokens := helps.NewClaudeInputTokenState(opts.SourceFormat, sdktranslator.FormatInteractions, responseFormat, originalRequest)
|
|
var streamUsage helps.StreamUsageBuffer
|
|
defer streamUsage.Publish(ctx, reporter)
|
|
var param any
|
|
helps.InitializeApplyPatchStream(ctx, sdktranslator.FormatInteractions, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, ¶m)
|
|
var frame []byte
|
|
emitFrame := func() bool {
|
|
rawFrame := bytes.Clone(frame)
|
|
trimmed := bytes.TrimSpace(rawFrame)
|
|
frame = frame[:0]
|
|
if len(trimmed) == 0 {
|
|
return true
|
|
}
|
|
payload := helps.GeminiInteractionsSSEPayload(rawFrame)
|
|
if len(payload) == 0 && helps.GeminiInteractionsSSEDone(rawFrame) {
|
|
payload = []byte("[DONE]")
|
|
}
|
|
if len(payload) > 0 {
|
|
reporter.ObserveResponseModel(payload)
|
|
if detail, ok := helps.ParseInteractionsStreamUsage(payload); ok {
|
|
streamUsage.Observe(detail, true)
|
|
}
|
|
}
|
|
if responseFormat == sdktranslator.FormatInteractions {
|
|
visibleFrame := append(bytes.TrimRight(rawFrame, "\r\n"), '\n', '\n')
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Payload: visibleFrame}:
|
|
case <-ctx.Done():
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
if len(payload) == 0 {
|
|
return true
|
|
}
|
|
var lines [][]byte
|
|
lines = helps.TranslateStreamWithClaudeInputTokens(ctx, sdktranslator.FormatInteractions, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, payload, ¶m, claudeInputTokens)
|
|
helps.RecordApplyPatchStreamFailure(ctx, param, reporter, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage})
|
|
for i := range lines {
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}:
|
|
case <-ctx.Done():
|
|
return false
|
|
}
|
|
}
|
|
if helps.StopApplyPatchStream(ctx, param, reporter, out, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage}) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
for scanner.Scan() {
|
|
rawLine := scanner.Bytes()
|
|
hasNewline := len(rawLine) > 0 && rawLine[len(rawLine)-1] == '\n'
|
|
line := rawLine
|
|
if hasNewline {
|
|
line = line[:len(line)-1]
|
|
}
|
|
if len(line) > 0 && line[len(line)-1] == '\r' {
|
|
line = line[:len(line)-1]
|
|
}
|
|
helps.AppendAPIResponseChunk(ctx, e.cfg, line)
|
|
trimmed := bytes.TrimSpace(line)
|
|
if len(trimmed) == 0 {
|
|
if hasNewline && !emitFrame() {
|
|
return
|
|
}
|
|
continue
|
|
}
|
|
if len(frame) > 0 {
|
|
frame = append(frame, '\n')
|
|
}
|
|
frame = append(frame, line...)
|
|
}
|
|
errScan := scanner.Err()
|
|
if errScan != nil {
|
|
helps.RecordAPIResponseError(ctx, e.cfg, errScan)
|
|
}
|
|
if errScan == nil && ctx.Err() == nil {
|
|
if !emitFrame() {
|
|
return
|
|
}
|
|
}
|
|
if helps.EndApplyPatchStream(ctx, param, reporter, out, statusErr{code: http.StatusBadGateway, msg: helps.ApplyPatchUpstreamErrorMessage}) {
|
|
return
|
|
}
|
|
if errScan != nil {
|
|
reporter.PublishFailure(ctx, errScan)
|
|
select {
|
|
case out <- cliproxyexecutor.StreamChunk{Err: errScan}:
|
|
case <-ctx.Done():
|
|
}
|
|
}
|
|
}()
|
|
|
|
return &cliproxyexecutor.StreamResult{
|
|
Chunks: out,
|
|
Headers: httpResp.Header.Clone(),
|
|
}, nil
|
|
}
|