mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 04:50:52 +08:00
Gemini handler extracts model from URL path, not JSON body, so rewriting the request body alone wasn't sufficient for model mapping. - Add MappedModelContextKey constant for context passing - Update routes.go to use NewFallbackHandlerWithMapper - Add check for valid mapping before routing to local handler - Add tests for gemini bridge model mapping
58 lines
1.9 KiB
Go
58 lines
1.9 KiB
Go
package amp
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers/gemini"
|
|
)
|
|
|
|
// createGeminiBridgeHandler creates a handler that bridges AMP CLI's non-standard Gemini paths
|
|
// to our standard Gemini handler by rewriting the request context.
|
|
//
|
|
// AMP CLI format: /publishers/google/models/gemini-3-pro-preview:streamGenerateContent
|
|
// Standard format: /models/gemini-3-pro-preview:streamGenerateContent
|
|
//
|
|
// This extracts the model+method from the AMP path and sets it as the :action parameter
|
|
// so the standard Gemini handler can process it.
|
|
func createGeminiBridgeHandler(geminiHandler *gemini.GeminiAPIHandler) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// Get the full path from the catch-all parameter
|
|
path := c.Param("path")
|
|
|
|
// Extract model:method from AMP CLI path format
|
|
// Example: /publishers/google/models/gemini-3-pro-preview:streamGenerateContent
|
|
if idx := strings.Index(path, "/models/"); idx >= 0 {
|
|
// Extract everything after "/models/"
|
|
actionPart := path[idx+8:] // Skip "/models/"
|
|
|
|
// Check if model was mapped by FallbackHandler
|
|
if mappedModel, exists := c.Get(MappedModelContextKey); exists {
|
|
if strModel, ok := mappedModel.(string); ok && strModel != "" {
|
|
// Replace the model part in the action
|
|
// actionPart is like "model-name:method"
|
|
if colonIdx := strings.Index(actionPart, ":"); colonIdx > 0 {
|
|
method := actionPart[colonIdx:] // ":method"
|
|
actionPart = strModel + method
|
|
}
|
|
}
|
|
}
|
|
|
|
// Set this as the :action parameter that the Gemini handler expects
|
|
c.Params = append(c.Params, gin.Param{
|
|
Key: "action",
|
|
Value: actionPart,
|
|
})
|
|
|
|
// Call the standard Gemini handler
|
|
geminiHandler.GeminiHandler(c)
|
|
return
|
|
}
|
|
|
|
// If we can't parse the path, return 400
|
|
c.JSON(400, gin.H{
|
|
"error": "Invalid Gemini API path format",
|
|
})
|
|
}
|
|
}
|