mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-18 20:30:51 +08:00
remove all
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
// Package gemini provides in-provider request normalization for Gemini API.
|
||||
// It ensures incoming v1beta requests meet minimal schema requirements
|
||||
// expected by Google's Generative Language API.
|
||||
package gemini
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
|
||||
// ConvertGeminiRequestToGemini normalizes Gemini v1beta requests.
|
||||
// - Adds a default role for each content if missing or invalid.
|
||||
// The first message defaults to "user", then alternates user/model when needed.
|
||||
//
|
||||
// It keeps the payload otherwise unchanged.
|
||||
func ConvertGeminiRequestToGemini(_ string, inputRawJSON []byte, _ bool) []byte {
|
||||
rawJSON := bytes.Clone(inputRawJSON)
|
||||
// Fast path: if no contents field, return as-is
|
||||
contents := gjson.GetBytes(rawJSON, "contents")
|
||||
if !contents.Exists() {
|
||||
return rawJSON
|
||||
}
|
||||
|
||||
// Walk contents and fix roles
|
||||
out := rawJSON
|
||||
prevRole := ""
|
||||
idx := 0
|
||||
contents.ForEach(func(_ gjson.Result, value gjson.Result) bool {
|
||||
role := value.Get("role").String()
|
||||
|
||||
// Only user/model are valid for Gemini v1beta requests
|
||||
valid := role == "user" || role == "model"
|
||||
if role == "" || !valid {
|
||||
var newRole string
|
||||
if prevRole == "" {
|
||||
newRole = "user"
|
||||
} else if prevRole == "user" {
|
||||
newRole = "model"
|
||||
} else {
|
||||
newRole = "user"
|
||||
}
|
||||
path := fmt.Sprintf("contents.%d.role", idx)
|
||||
out, _ = sjson.SetBytes(out, path, newRole)
|
||||
role = newRole
|
||||
}
|
||||
|
||||
prevRole = role
|
||||
idx++
|
||||
return true
|
||||
})
|
||||
|
||||
return out
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package gemini
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// PassthroughGeminiResponseStream forwards Gemini responses unchanged.
|
||||
func PassthroughGeminiResponseStream(_ context.Context, _ string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, _ *any) []string {
|
||||
if bytes.HasPrefix(rawJSON, []byte("data:")) {
|
||||
rawJSON = bytes.TrimSpace(rawJSON[5:])
|
||||
}
|
||||
|
||||
if bytes.Equal(rawJSON, []byte("[DONE]")) {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
return []string{string(rawJSON)}
|
||||
}
|
||||
|
||||
// PassthroughGeminiResponseNonStream forwards Gemini responses unchanged.
|
||||
func PassthroughGeminiResponseNonStream(_ context.Context, _ string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, _ *any) string {
|
||||
return string(rawJSON)
|
||||
}
|
||||
|
||||
func GeminiTokenCount(ctx context.Context, count int64) string {
|
||||
return fmt.Sprintf(`{"totalTokens":%d,"promptTokensDetails":[{"modality":"TEXT","tokenCount":%d}]}`, count, count)
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package gemini
|
||||
|
||||
import (
|
||||
. "github.com/router-for-me/CLIProxyAPI/v6/internal/constant"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/translator/translator"
|
||||
)
|
||||
|
||||
// Register a no-op response translator and a request normalizer for Gemini→Gemini.
|
||||
// The request converter ensures missing or invalid roles are normalized to valid values.
|
||||
func init() {
|
||||
translator.Register(
|
||||
Gemini,
|
||||
Gemini,
|
||||
ConvertGeminiRequestToGemini,
|
||||
interfaces.TranslateResponse{
|
||||
Stream: PassthroughGeminiResponseStream,
|
||||
NonStream: PassthroughGeminiResponseNonStream,
|
||||
TokenCount: GeminiTokenCount,
|
||||
},
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user