Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e64fa48823 | ||
|
|
beff9282f6 | ||
|
|
31a9e2d11f | ||
|
|
423faae3da | ||
|
|
ead71fb7ef | ||
|
|
58b7afdf1e |
@@ -141,7 +141,7 @@ outLoop:
|
|||||||
log.Debugf("Request use account: %s, project id: %s", cliClient.GetEmail(), cliClient.GetProjectID())
|
log.Debugf("Request use account: %s, project id: %s", cliClient.GetEmail(), cliClient.GetProjectID())
|
||||||
}
|
}
|
||||||
// Send the message and receive response chunks and errors via channels.
|
// Send the message and receive response chunks and errors via channels.
|
||||||
respChan, errChan := cliClient.SendRawMessageStream(cliCtx, rawJson)
|
respChan, errChan := cliClient.SendRawMessageStream(cliCtx, rawJson, "")
|
||||||
hasFirstResponse := false
|
hasFirstResponse := false
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@@ -220,7 +220,7 @@ func (h *APIHandlers) internalGenerateContent(c *gin.Context, rawJson []byte) {
|
|||||||
log.Debugf("Request use account: %s, project id: %s", cliClient.GetEmail(), cliClient.GetProjectID())
|
log.Debugf("Request use account: %s, project id: %s", cliClient.GetEmail(), cliClient.GetProjectID())
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := cliClient.SendRawMessage(cliCtx, rawJson)
|
resp, err := cliClient.SendRawMessage(cliCtx, rawJson, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.StatusCode == 429 && h.cfg.QuotaExceeded.SwitchProject {
|
if err.StatusCode == 429 && h.cfg.QuotaExceeded.SwitchProject {
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -14,11 +14,27 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *APIHandlers) GeminiHandler(c *gin.Context) {
|
func (h *APIHandlers) GeminiModels(c *gin.Context) {
|
||||||
var person struct {
|
c.Status(http.StatusOK)
|
||||||
|
c.Header("Content-Type", "application/json; charset=UTF-8")
|
||||||
|
_, _ = c.Writer.Write([]byte(`{"models":[{"name":"models/gemini-2.5-flash","version":"001","displayName":"Gemini `))
|
||||||
|
_, _ = c.Writer.Write([]byte(`2.5 Flash","description":"Stable version of Gemini 2.5 Flash, our mid-size multimod`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`al model that supports up to 1 million tokens, released in June of 2025.","inputTok`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`enLimit":1048576,"outputTokenLimit":65536,"supportedGenerationMethods":["generateCo`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`ntent","countTokens","createCachedContent","batchGenerateContent"],"temperature":1,`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`"topP":0.95,"topK":64,"maxTemperature":2,"thinking":true},{"name":"models/gemini-2.`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`5-pro","version":"2.5","displayName":"Gemini 2.5 Pro","description":"Stable release`))
|
||||||
|
_, _ = c.Writer.Write([]byte(` (June 17th, 2025) of Gemini 2.5 Pro","inputTokenLimit":1048576,"outputTokenLimit":`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`65536,"supportedGenerationMethods":["generateContent","countTokens","createCachedCo`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`ntent","batchGenerateContent"],"temperature":1,"topP":0.95,"topK":64,"maxTemperatur`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`e":2,"thinking":true}],"nextPageToken":""}`))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *APIHandlers) GeminiGetHandler(c *gin.Context) {
|
||||||
|
var request struct {
|
||||||
Action string `uri:"action" binding:"required"`
|
Action string `uri:"action" binding:"required"`
|
||||||
}
|
}
|
||||||
if err := c.ShouldBindUri(&person); err != nil {
|
if err := c.ShouldBindUri(&request); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, ErrorResponse{
|
c.JSON(http.StatusBadRequest, ErrorResponse{
|
||||||
Error: ErrorDetail{
|
Error: ErrorDetail{
|
||||||
Message: fmt.Sprintf("Invalid request: %v", err),
|
Message: fmt.Sprintf("Invalid request: %v", err),
|
||||||
@@ -27,7 +43,45 @@ func (h *APIHandlers) GeminiHandler(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
action := strings.Split(person.Action, ":")
|
if request.Action == "gemini-2.5-pro" {
|
||||||
|
c.Status(http.StatusOK)
|
||||||
|
c.Header("Content-Type", "application/json; charset=UTF-8")
|
||||||
|
_, _ = c.Writer.Write([]byte(`{"name":"models/gemini-2.5-pro","version":"2.5","displayName":"Gemini 2.5 Pro",`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`"description":"Stable release (June 17th, 2025) of Gemini 2.5 Pro","inputTokenL`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`imit":1048576,"outputTokenLimit":65536,"supportedGenerationMethods":["generateC`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`ontent","countTokens","createCachedContent","batchGenerateContent"],"temperatur`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`e":1,"topP":0.95,"topK":64,"maxTemperature":2,"thinking":true}`))
|
||||||
|
} else if request.Action == "gemini-2.5-flash" {
|
||||||
|
c.Status(http.StatusOK)
|
||||||
|
c.Header("Content-Type", "application/json; charset=UTF-8")
|
||||||
|
_, _ = c.Writer.Write([]byte(`{"name":"models/gemini-2.5-flash","version":"001","displayName":"Gemini 2.5 Fla`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`sh","description":"Stable version of Gemini 2.5 Flash, our mid-size multimodal `))
|
||||||
|
_, _ = c.Writer.Write([]byte(`model that supports up to 1 million tokens, released in June of 2025.","inputTo`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`kenLimit":1048576,"outputTokenLimit":65536,"supportedGenerationMethods":["gener`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`ateContent","countTokens","createCachedContent","batchGenerateContent"],"temper`))
|
||||||
|
_, _ = c.Writer.Write([]byte(`ature":1,"topP":0.95,"topK":64,"maxTemperature":2,"thinking":true}`))
|
||||||
|
} else {
|
||||||
|
c.Status(http.StatusNotFound)
|
||||||
|
_, _ = c.Writer.Write([]byte(
|
||||||
|
`{"error":{"message":"Not Found","code":404,"status":"NOT_FOUND"}}`,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *APIHandlers) GeminiHandler(c *gin.Context) {
|
||||||
|
var request struct {
|
||||||
|
Action string `uri:"action" binding:"required"`
|
||||||
|
}
|
||||||
|
if err := c.ShouldBindUri(&request); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, ErrorResponse{
|
||||||
|
Error: ErrorDetail{
|
||||||
|
Message: fmt.Sprintf("Invalid request: %v", err),
|
||||||
|
Type: "invalid_request_error",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
action := strings.Split(request.Action, ":")
|
||||||
if len(action) != 2 {
|
if len(action) != 2 {
|
||||||
c.JSON(http.StatusNotFound, ErrorResponse{
|
c.JSON(http.StatusNotFound, ErrorResponse{
|
||||||
Error: ErrorDetail{
|
Error: ErrorDetail{
|
||||||
@@ -47,6 +101,8 @@ func (h *APIHandlers) GeminiHandler(c *gin.Context) {
|
|||||||
h.geminiGenerateContent(c, rawJson)
|
h.geminiGenerateContent(c, rawJson)
|
||||||
} else if method == "streamGenerateContent" {
|
} else if method == "streamGenerateContent" {
|
||||||
h.geminiStreamGenerateContent(c, rawJson)
|
h.geminiStreamGenerateContent(c, rawJson)
|
||||||
|
} else if method == "countTokens" {
|
||||||
|
h.geminiCountTokens(c, rawJson)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,6 +122,8 @@ func (h *APIHandlers) geminiStreamGenerateContent(c *gin.Context, rawJson []byte
|
|||||||
modelResult := gjson.GetBytes(rawJson, "model")
|
modelResult := gjson.GetBytes(rawJson, "model")
|
||||||
modelName := modelResult.String()
|
modelName := modelResult.String()
|
||||||
|
|
||||||
|
alt := h.getAlt(c)
|
||||||
|
|
||||||
cliCtx, cliCancel := context.WithCancel(context.Background())
|
cliCtx, cliCancel := context.WithCancel(context.Background())
|
||||||
var cliClient *client.Client
|
var cliClient *client.Client
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -118,7 +176,7 @@ outLoop:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send the message and receive response chunks and errors via channels.
|
// Send the message and receive response chunks and errors via channels.
|
||||||
respChan, errChan := cliClient.SendRawMessageStream(cliCtx, rawJson)
|
respChan, errChan := cliClient.SendRawMessageStream(cliCtx, rawJson, alt)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
// Handle client disconnection.
|
// Handle client disconnection.
|
||||||
@@ -135,14 +193,33 @@ outLoop:
|
|||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
if cliClient.GetGenerativeLanguageAPIKey() == "" {
|
if cliClient.GetGenerativeLanguageAPIKey() == "" {
|
||||||
responseResult := gjson.GetBytes(chunk, "response")
|
if alt == "" {
|
||||||
if responseResult.Exists() {
|
responseResult := gjson.GetBytes(chunk, "response")
|
||||||
chunk = []byte(responseResult.Raw)
|
if responseResult.Exists() {
|
||||||
|
chunk = []byte(responseResult.Raw)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
chunkTemplate := "[]"
|
||||||
|
responseResult := gjson.ParseBytes(chunk)
|
||||||
|
if responseResult.IsArray() {
|
||||||
|
responseResultItems := responseResult.Array()
|
||||||
|
for i := 0; i < len(responseResultItems); i++ {
|
||||||
|
responseResultItem := responseResultItems[i]
|
||||||
|
if responseResultItem.Get("response").Exists() {
|
||||||
|
chunkTemplate, _ = sjson.SetRaw(chunkTemplate, "-1", responseResultItem.Get("response").Raw)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chunk = []byte(chunkTemplate)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_, _ = c.Writer.Write([]byte("data: "))
|
if alt == "" {
|
||||||
_, _ = c.Writer.Write(chunk)
|
_, _ = c.Writer.Write([]byte("data: "))
|
||||||
_, _ = c.Writer.Write([]byte("\n\n"))
|
_, _ = c.Writer.Write(chunk)
|
||||||
|
_, _ = c.Writer.Write([]byte("\n\n"))
|
||||||
|
} else {
|
||||||
|
_, _ = c.Writer.Write(chunk)
|
||||||
|
}
|
||||||
flusher.Flush()
|
flusher.Flush()
|
||||||
}
|
}
|
||||||
// Handle errors from the backend.
|
// Handle errors from the backend.
|
||||||
@@ -165,9 +242,79 @@ outLoop:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *APIHandlers) geminiCountTokens(c *gin.Context, rawJson []byte) {
|
||||||
|
c.Header("Content-Type", "application/json")
|
||||||
|
|
||||||
|
alt := h.getAlt(c)
|
||||||
|
// orgRawJson := rawJson
|
||||||
|
modelResult := gjson.GetBytes(rawJson, "model")
|
||||||
|
modelName := modelResult.String()
|
||||||
|
cliCtx, cliCancel := context.WithCancel(context.Background())
|
||||||
|
var cliClient *client.Client
|
||||||
|
defer func() {
|
||||||
|
if cliClient != nil {
|
||||||
|
cliClient.RequestMutex.Unlock()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
|
var errorResponse *client.ErrorMessage
|
||||||
|
cliClient, errorResponse = h.getClient(modelName)
|
||||||
|
if errorResponse != nil {
|
||||||
|
c.Status(errorResponse.StatusCode)
|
||||||
|
_, _ = fmt.Fprint(c.Writer, errorResponse.Error)
|
||||||
|
cliCancel()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if glAPIKey := cliClient.GetGenerativeLanguageAPIKey(); glAPIKey != "" {
|
||||||
|
log.Debugf("Request use generative language API Key: %s", glAPIKey)
|
||||||
|
} else {
|
||||||
|
log.Debugf("Request use account: %s, project id: %s", cliClient.GetEmail(), cliClient.GetProjectID())
|
||||||
|
|
||||||
|
template := `{"request":{}}`
|
||||||
|
if gjson.GetBytes(rawJson, "generateContentRequest").Exists() {
|
||||||
|
template, _ = sjson.SetRaw(template, "request", gjson.GetBytes(rawJson, "generateContentRequest").Raw)
|
||||||
|
template, _ = sjson.Delete(template, "generateContentRequest")
|
||||||
|
} else if gjson.GetBytes(rawJson, "contents").Exists() {
|
||||||
|
template, _ = sjson.SetRaw(template, "request.contents", gjson.GetBytes(rawJson, "contents").Raw)
|
||||||
|
template, _ = sjson.Delete(template, "contents")
|
||||||
|
}
|
||||||
|
rawJson = []byte(template)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := cliClient.SendRawTokenCount(cliCtx, rawJson, alt)
|
||||||
|
if err != nil {
|
||||||
|
if err.StatusCode == 429 && h.cfg.QuotaExceeded.SwitchProject {
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
c.Status(err.StatusCode)
|
||||||
|
_, _ = c.Writer.Write([]byte(err.Error.Error()))
|
||||||
|
cliCancel()
|
||||||
|
// log.Debugf(err.Error.Error())
|
||||||
|
// log.Debugf(string(rawJson))
|
||||||
|
// log.Debugf(string(orgRawJson))
|
||||||
|
}
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
if cliClient.GetGenerativeLanguageAPIKey() == "" {
|
||||||
|
responseResult := gjson.GetBytes(resp, "response")
|
||||||
|
if responseResult.Exists() {
|
||||||
|
resp = []byte(responseResult.Raw)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_, _ = c.Writer.Write(resp)
|
||||||
|
cliCancel()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (h *APIHandlers) geminiGenerateContent(c *gin.Context, rawJson []byte) {
|
func (h *APIHandlers) geminiGenerateContent(c *gin.Context, rawJson []byte) {
|
||||||
c.Header("Content-Type", "application/json")
|
c.Header("Content-Type", "application/json")
|
||||||
|
|
||||||
|
alt := h.getAlt(c)
|
||||||
|
|
||||||
modelResult := gjson.GetBytes(rawJson, "model")
|
modelResult := gjson.GetBytes(rawJson, "model")
|
||||||
modelName := modelResult.String()
|
modelName := modelResult.String()
|
||||||
cliCtx, cliCancel := context.WithCancel(context.Background())
|
cliCtx, cliCancel := context.WithCancel(context.Background())
|
||||||
@@ -217,7 +364,7 @@ func (h *APIHandlers) geminiGenerateContent(c *gin.Context, rawJson []byte) {
|
|||||||
} else {
|
} else {
|
||||||
log.Debugf("Request use account: %s, project id: %s", cliClient.GetEmail(), cliClient.GetProjectID())
|
log.Debugf("Request use account: %s, project id: %s", cliClient.GetEmail(), cliClient.GetProjectID())
|
||||||
}
|
}
|
||||||
resp, err := cliClient.SendRawMessage(cliCtx, rawJson)
|
resp, err := cliClient.SendRawMessage(cliCtx, rawJson, alt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.StatusCode == 429 && h.cfg.QuotaExceeded.SwitchProject {
|
if err.StatusCode == 429 && h.cfg.QuotaExceeded.SwitchProject {
|
||||||
continue
|
continue
|
||||||
@@ -240,3 +387,16 @@ func (h *APIHandlers) geminiGenerateContent(c *gin.Context, rawJson []byte) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *APIHandlers) getAlt(c *gin.Context) string {
|
||||||
|
var alt string
|
||||||
|
var hasAlt bool
|
||||||
|
alt, hasAlt = c.GetQuery("alt")
|
||||||
|
if !hasAlt {
|
||||||
|
alt, _ = c.GetQuery("$alt")
|
||||||
|
}
|
||||||
|
if alt == "sse" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return alt
|
||||||
|
}
|
||||||
|
|||||||
@@ -75,8 +75,9 @@ func (s *Server) setupRoutes() {
|
|||||||
v1beta := s.engine.Group("/v1beta")
|
v1beta := s.engine.Group("/v1beta")
|
||||||
v1beta.Use(AuthMiddleware(s.cfg))
|
v1beta.Use(AuthMiddleware(s.cfg))
|
||||||
{
|
{
|
||||||
v1beta.GET("/models", s.handlers.Models)
|
v1beta.GET("/models", s.handlers.GeminiModels)
|
||||||
v1beta.POST("/models/:action", s.handlers.GeminiHandler)
|
v1beta.POST("/models/:action", s.handlers.GeminiHandler)
|
||||||
|
v1beta.GET("/models/:action", s.handlers.GeminiGetHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Root endpoint
|
// Root endpoint
|
||||||
@@ -151,7 +152,11 @@ func AuthMiddleware(cfg *config.Config) gin.HandlerFunc {
|
|||||||
authHeader := c.GetHeader("Authorization")
|
authHeader := c.GetHeader("Authorization")
|
||||||
authHeaderGoogle := c.GetHeader("X-Goog-Api-Key")
|
authHeaderGoogle := c.GetHeader("X-Goog-Api-Key")
|
||||||
authHeaderAnthropic := c.GetHeader("X-Api-Key")
|
authHeaderAnthropic := c.GetHeader("X-Api-Key")
|
||||||
if authHeader == "" && authHeaderGoogle == "" && authHeaderAnthropic == "" {
|
|
||||||
|
// Get the API key from the query parameter
|
||||||
|
apiKeyQuery, _ := c.GetQuery("key")
|
||||||
|
|
||||||
|
if authHeader == "" && authHeaderGoogle == "" && authHeaderAnthropic == "" && apiKeyQuery == "" {
|
||||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
||||||
"error": "Missing API key",
|
"error": "Missing API key",
|
||||||
})
|
})
|
||||||
@@ -170,7 +175,7 @@ func AuthMiddleware(cfg *config.Config) gin.HandlerFunc {
|
|||||||
// Find the API key in the in-memory list
|
// Find the API key in the in-memory list
|
||||||
var foundKey string
|
var foundKey string
|
||||||
for i := range cfg.ApiKeys {
|
for i := range cfg.ApiKeys {
|
||||||
if cfg.ApiKeys[i] == apiKey || cfg.ApiKeys[i] == authHeaderGoogle || cfg.ApiKeys[i] == authHeaderAnthropic {
|
if cfg.ApiKeys[i] == apiKey || cfg.ApiKeys[i] == authHeaderGoogle || cfg.ApiKeys[i] == authHeaderAnthropic || cfg.ApiKeys[i] == apiKeyQuery {
|
||||||
foundKey = cfg.ApiKeys[i]
|
foundKey = cfg.ApiKeys[i]
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -168,11 +168,12 @@ func getTokenFromWeb(ctx context.Context, config *oauth2.Config) (*oauth2.Token,
|
|||||||
codeChan := make(chan string)
|
codeChan := make(chan string)
|
||||||
errChan := make(chan error)
|
errChan := make(chan error)
|
||||||
|
|
||||||
// Create a new HTTP server.
|
// Create a new HTTP server with its own multiplexer.
|
||||||
server := &http.Server{Addr: ":8085"}
|
mux := http.NewServeMux()
|
||||||
|
server := &http.Server{Addr: ":8085", Handler: mux}
|
||||||
config.RedirectURL = "http://localhost:8085/oauth2callback"
|
config.RedirectURL = "http://localhost:8085/oauth2callback"
|
||||||
|
|
||||||
http.HandleFunc("/oauth2callback", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/oauth2callback", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if err := r.URL.Query().Get("error"); err != "" {
|
if err := r.URL.Query().Get("error"); err != "" {
|
||||||
_, _ = fmt.Fprintf(w, "Authentication failed: %s", err)
|
_, _ = fmt.Fprintf(w, "Authentication failed: %s", err)
|
||||||
errChan <- fmt.Errorf("authentication failed via callback: %s", err)
|
errChan <- fmt.Errorf("authentication failed via callback: %s", err)
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ const (
|
|||||||
apiVersion = "v1internal"
|
apiVersion = "v1internal"
|
||||||
pluginVersion = "0.1.9"
|
pluginVersion = "0.1.9"
|
||||||
|
|
||||||
glEndPoint = "https://generativelanguage.googleapis.com/"
|
glEndPoint = "https://generativelanguage.googleapis.com"
|
||||||
glApiVersion = "v1beta"
|
glApiVersion = "v1beta"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -241,7 +241,7 @@ func (c *Client) makeAPIRequest(ctx context.Context, endpoint, method string, bo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// APIRequest handles making requests to the CLI API endpoints.
|
// APIRequest handles making requests to the CLI API endpoints.
|
||||||
func (c *Client) APIRequest(ctx context.Context, endpoint string, body interface{}, stream bool) (io.ReadCloser, *ErrorMessage) {
|
func (c *Client) APIRequest(ctx context.Context, endpoint string, body interface{}, alt string, stream bool) (io.ReadCloser, *ErrorMessage) {
|
||||||
var jsonBody []byte
|
var jsonBody []byte
|
||||||
var err error
|
var err error
|
||||||
if byteBody, ok := body.([]byte); ok {
|
if byteBody, ok := body.([]byte); ok {
|
||||||
@@ -257,25 +257,39 @@ func (c *Client) APIRequest(ctx context.Context, endpoint string, body interface
|
|||||||
if c.glAPIKey == "" {
|
if c.glAPIKey == "" {
|
||||||
// Add alt=sse for streaming
|
// Add alt=sse for streaming
|
||||||
url = fmt.Sprintf("%s/%s:%s", codeAssistEndpoint, apiVersion, endpoint)
|
url = fmt.Sprintf("%s/%s:%s", codeAssistEndpoint, apiVersion, endpoint)
|
||||||
if stream {
|
if alt == "" && stream {
|
||||||
url = url + "?alt=sse"
|
url = url + "?alt=sse"
|
||||||
|
} else {
|
||||||
|
if alt != "" {
|
||||||
|
url = url + fmt.Sprintf("?$alt=%s", alt)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
modelResult := gjson.GetBytes(jsonBody, "model")
|
if endpoint == "countTokens" {
|
||||||
url = fmt.Sprintf("%s/%s/models/%s:%s", glEndPoint, glApiVersion, modelResult.String(), endpoint)
|
modelResult := gjson.GetBytes(jsonBody, "model")
|
||||||
if stream {
|
url = fmt.Sprintf("%s/%s/models/%s:%s", glEndPoint, glApiVersion, modelResult.String(), endpoint)
|
||||||
url = url + "?alt=sse"
|
} else {
|
||||||
}
|
modelResult := gjson.GetBytes(jsonBody, "model")
|
||||||
jsonBody = []byte(gjson.GetBytes(jsonBody, "request").Raw)
|
url = fmt.Sprintf("%s/%s/models/%s:%s", glEndPoint, glApiVersion, modelResult.String(), endpoint)
|
||||||
systemInstructionResult := gjson.GetBytes(jsonBody, "systemInstruction")
|
if alt == "" && stream {
|
||||||
if systemInstructionResult.Exists() {
|
url = url + "?alt=sse"
|
||||||
jsonBody, _ = sjson.SetRawBytes(jsonBody, "system_instruction", []byte(systemInstructionResult.Raw))
|
} else {
|
||||||
jsonBody, _ = sjson.DeleteBytes(jsonBody, "systemInstruction")
|
if alt != "" {
|
||||||
jsonBody, _ = sjson.DeleteBytes(jsonBody, "session_id")
|
url = url + fmt.Sprintf("?$alt=%s", alt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
jsonBody = []byte(gjson.GetBytes(jsonBody, "request").Raw)
|
||||||
|
systemInstructionResult := gjson.GetBytes(jsonBody, "systemInstruction")
|
||||||
|
if systemInstructionResult.Exists() {
|
||||||
|
jsonBody, _ = sjson.SetRawBytes(jsonBody, "system_instruction", []byte(systemInstructionResult.Raw))
|
||||||
|
jsonBody, _ = sjson.DeleteBytes(jsonBody, "systemInstruction")
|
||||||
|
jsonBody, _ = sjson.DeleteBytes(jsonBody, "session_id")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// log.Debug(string(jsonBody))
|
// log.Debug(string(jsonBody))
|
||||||
|
// log.Debug(url)
|
||||||
reqBody := bytes.NewBuffer(jsonBody)
|
reqBody := bytes.NewBuffer(jsonBody)
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, "POST", url, reqBody)
|
req, err := http.NewRequestWithContext(ctx, "POST", url, reqBody)
|
||||||
@@ -392,7 +406,7 @@ func (c *Client) SendMessage(ctx context.Context, rawJson []byte, model string,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
respBody, err := c.APIRequest(ctx, "generateContent", byteRequestBody, false)
|
respBody, err := c.APIRequest(ctx, "generateContent", byteRequestBody, "", false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.StatusCode == 429 {
|
if err.StatusCode == 429 {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
@@ -544,7 +558,7 @@ func (c *Client) SendMessageStream(ctx context.Context, rawJson []byte, model st
|
|||||||
|
|
||||||
// Attempt to establish a streaming connection with the API
|
// Attempt to establish a streaming connection with the API
|
||||||
var err *ErrorMessage
|
var err *ErrorMessage
|
||||||
stream, err = c.APIRequest(ctx, "streamGenerateContent", byteRequestBody, true)
|
stream, err = c.APIRequest(ctx, "streamGenerateContent", byteRequestBody, "", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Handle quota exceeded errors by marking the model and potentially retrying
|
// Handle quota exceeded errors by marking the model and potentially retrying
|
||||||
if err.StatusCode == 429 {
|
if err.StatusCode == 429 {
|
||||||
@@ -593,8 +607,49 @@ func (c *Client) SendMessageStream(ctx context.Context, rawJson []byte, model st
|
|||||||
return dataChan, errChan
|
return dataChan, errChan
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SendRawTokenCount handles a token count.
|
||||||
|
func (c *Client) SendRawTokenCount(ctx context.Context, rawJson []byte, alt string) ([]byte, *ErrorMessage) {
|
||||||
|
modelResult := gjson.GetBytes(rawJson, "model")
|
||||||
|
model := modelResult.String()
|
||||||
|
modelName := model
|
||||||
|
for {
|
||||||
|
if c.isModelQuotaExceeded(modelName) {
|
||||||
|
if c.cfg.QuotaExceeded.SwitchPreviewModel && c.glAPIKey == "" {
|
||||||
|
modelName = c.getPreviewModel(model)
|
||||||
|
if modelName != "" {
|
||||||
|
log.Debugf("Model %s is quota exceeded. Switch to preview model %s", model, modelName)
|
||||||
|
rawJson, _ = sjson.SetBytes(rawJson, "model", modelName)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, &ErrorMessage{
|
||||||
|
StatusCode: 429,
|
||||||
|
Error: fmt.Errorf(`{"error":{"code":429,"message":"All the models of '%s' are quota exceeded","status":"RESOURCE_EXHAUSTED"}}`, model),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
respBody, err := c.APIRequest(ctx, "countTokens", rawJson, alt, false)
|
||||||
|
if err != nil {
|
||||||
|
if err.StatusCode == 429 {
|
||||||
|
now := time.Now()
|
||||||
|
c.modelQuotaExceeded[modelName] = &now
|
||||||
|
if c.cfg.QuotaExceeded.SwitchPreviewModel && c.glAPIKey == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
delete(c.modelQuotaExceeded, modelName)
|
||||||
|
bodyBytes, errReadAll := io.ReadAll(respBody)
|
||||||
|
if errReadAll != nil {
|
||||||
|
return nil, &ErrorMessage{StatusCode: 500, Error: errReadAll}
|
||||||
|
}
|
||||||
|
return bodyBytes, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SendRawMessage handles a single conversational turn, including tool calls.
|
// SendRawMessage handles a single conversational turn, including tool calls.
|
||||||
func (c *Client) SendRawMessage(ctx context.Context, rawJson []byte) ([]byte, *ErrorMessage) {
|
func (c *Client) SendRawMessage(ctx context.Context, rawJson []byte, alt string) ([]byte, *ErrorMessage) {
|
||||||
if c.glAPIKey == "" {
|
if c.glAPIKey == "" {
|
||||||
rawJson, _ = sjson.SetBytes(rawJson, "project", c.GetProjectID())
|
rawJson, _ = sjson.SetBytes(rawJson, "project", c.GetProjectID())
|
||||||
}
|
}
|
||||||
@@ -618,7 +673,7 @@ func (c *Client) SendRawMessage(ctx context.Context, rawJson []byte) ([]byte, *E
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
respBody, err := c.APIRequest(ctx, "generateContent", rawJson, false)
|
respBody, err := c.APIRequest(ctx, "generateContent", rawJson, alt, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.StatusCode == 429 {
|
if err.StatusCode == 429 {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
@@ -639,7 +694,7 @@ func (c *Client) SendRawMessage(ctx context.Context, rawJson []byte) ([]byte, *E
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SendRawMessageStream handles a single conversational turn, including tool calls.
|
// SendRawMessageStream handles a single conversational turn, including tool calls.
|
||||||
func (c *Client) SendRawMessageStream(ctx context.Context, rawJson []byte) (<-chan []byte, <-chan *ErrorMessage) {
|
func (c *Client) SendRawMessageStream(ctx context.Context, rawJson []byte, alt string) (<-chan []byte, <-chan *ErrorMessage) {
|
||||||
dataTag := []byte("data: ")
|
dataTag := []byte("data: ")
|
||||||
errChan := make(chan *ErrorMessage)
|
errChan := make(chan *ErrorMessage)
|
||||||
dataChan := make(chan []byte)
|
dataChan := make(chan []byte)
|
||||||
@@ -672,7 +727,7 @@ func (c *Client) SendRawMessageStream(ctx context.Context, rawJson []byte) (<-ch
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var err *ErrorMessage
|
var err *ErrorMessage
|
||||||
stream, err = c.APIRequest(ctx, "streamGenerateContent", rawJson, true)
|
stream, err = c.APIRequest(ctx, "streamGenerateContent", rawJson, alt, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.StatusCode == 429 {
|
if err.StatusCode == 429 {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
@@ -688,21 +743,32 @@ func (c *Client) SendRawMessageStream(ctx context.Context, rawJson []byte) (<-ch
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
scanner := bufio.NewScanner(stream)
|
if alt == "" {
|
||||||
for scanner.Scan() {
|
scanner := bufio.NewScanner(stream)
|
||||||
line := scanner.Bytes()
|
for scanner.Scan() {
|
||||||
if bytes.HasPrefix(line, dataTag) {
|
line := scanner.Bytes()
|
||||||
dataChan <- line[6:]
|
if bytes.HasPrefix(line, dataTag) {
|
||||||
|
dataChan <- line[6:]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if errScanner := scanner.Err(); errScanner != nil {
|
if errScanner := scanner.Err(); errScanner != nil {
|
||||||
errChan <- &ErrorMessage{500, errScanner}
|
errChan <- &ErrorMessage{500, errScanner}
|
||||||
_ = stream.Close()
|
_ = stream.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
data, err := io.ReadAll(stream)
|
||||||
|
if err != nil {
|
||||||
|
errChan <- &ErrorMessage{500, err}
|
||||||
|
_ = stream.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dataChan <- data
|
||||||
|
}
|
||||||
_ = stream.Close()
|
_ = stream.Close()
|
||||||
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return dataChan, errChan
|
return dataChan, errChan
|
||||||
@@ -754,7 +820,7 @@ func (c *Client) CheckCloudAPIIsEnabled() (bool, error) {
|
|||||||
// A simple request to test the API endpoint.
|
// A simple request to test the API endpoint.
|
||||||
requestBody := fmt.Sprintf(`{"project":"%s","request":{"contents":[{"role":"user","parts":[{"text":"Be concise. What is the capital of France?"}]}],"generationConfig":{"thinkingConfig":{"include_thoughts":false,"thinkingBudget":0}}},"model":"gemini-2.5-flash"}`, c.tokenStorage.ProjectID)
|
requestBody := fmt.Sprintf(`{"project":"%s","request":{"contents":[{"role":"user","parts":[{"text":"Be concise. What is the capital of France?"}]}],"generationConfig":{"thinkingConfig":{"include_thoughts":false,"thinkingBudget":0}}},"model":"gemini-2.5-flash"}`, c.tokenStorage.ProjectID)
|
||||||
|
|
||||||
stream, err := c.APIRequest(ctx, "streamGenerateContent", []byte(requestBody), true)
|
stream, err := c.APIRequest(ctx, "streamGenerateContent", []byte(requestBody), "", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If a 403 Forbidden error occurs, it likely means the API is not enabled.
|
// If a 403 Forbidden error occurs, it likely means the API is not enabled.
|
||||||
if err.StatusCode == 403 {
|
if err.StatusCode == 403 {
|
||||||
@@ -771,6 +837,7 @@ func (c *Client) CheckCloudAPIIsEnabled() (bool, error) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Warnf("\n\nPlease copy this message and create an issue.\n\n%s\n\n", errJson)
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
return false, err.Error
|
return false, err.Error
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ func DoLogin(cfg *config.Config, projectID string) {
|
|||||||
// If the check fails (returns false), the CheckCloudAPIIsEnabled function
|
// If the check fails (returns false), the CheckCloudAPIIsEnabled function
|
||||||
// will have already printed instructions, so we can just exit.
|
// will have already printed instructions, so we can just exit.
|
||||||
if !isChecked {
|
if !isChecked {
|
||||||
|
log.Fatal("Failed to check if Cloud AI API is enabled. If you encounter an error message, please create an issue.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user