hazeruno commited on
Commit
e89b9e4
·
1 Parent(s): b69f37d

refactor: remove duplicate provider check in gemini v1beta1 route

Browse files

Simplifies routing logic by delegating all provider/mapping/proxy
decisions to FallbackHandler. Previously, the route checked for
provider/mapping availability before calling the handler, then
FallbackHandler performed the same checks again.

Changes:
- Remove model extraction and provider checking from route (lines 182-201)
- Route now only checks if request is POST with /models/ path
- FallbackHandler handles provider -> mapping -> proxy fallback
- Remove unused internal/util import

Benefits:
- Eliminates duplicate checks (addresses PR review feedback #2)
- Centralizes all provider/mapping logic in FallbackHandler
- Reduces routing code by ~20 lines
- Aligns with how other /api/provider routes work

Performance: No impact (checks still happen once in FallbackHandler)

Files changed (1) hide show
  1. internal/api/modules/amp/routes.go +6 -21
internal/api/modules/amp/routes.go CHANGED
@@ -9,7 +9,6 @@ import (
9
 
10
  "github.com/gin-gonic/gin"
11
  "github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
12
- "github.com/router-for-me/CLIProxyAPI/v6/internal/util"
13
  "github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers"
14
  "github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers/claude"
15
  "github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers/gemini"
@@ -175,30 +174,16 @@ func (m *AmpModule) registerManagementRoutes(engine *gin.Engine, baseHandler *ha
175
  }, m.modelMapper)
176
  geminiV1Beta1Handler := geminiV1Beta1Fallback.WrapHandler(geminiBridge)
177
 
178
- // Route POST model calls through Gemini bridge when a local provider exists, otherwise proxy.
 
179
  // All other methods (e.g., GET model listing) always proxy to upstream to preserve Amp CLI behavior.
180
  ampAPI.Any("/provider/google/v1beta1/*path", func(c *gin.Context) {
181
  if c.Request.Method == "POST" {
182
- // Attempt to extract the model name from the AMP-style path
183
  if path := c.Param("path"); strings.Contains(path, "/models/") {
184
- modelPart := path[strings.Index(path, "/models/")+len("/models/"):]
185
- if colonIdx := strings.Index(modelPart, ":"); colonIdx > 0 {
186
- modelPart = modelPart[:colonIdx]
187
- }
188
- if modelPart != "" {
189
- normalized, _ := util.NormalizeGeminiThinkingModel(modelPart)
190
- // Only handle locally when we have a provider or a valid mapping; otherwise fall back to proxy
191
- hasProvider := len(util.GetProviderName(normalized)) > 0
192
- if !hasProvider && m.modelMapper != nil {
193
- // Check if mapped model has provider (MapModel returns target only if it has providers)
194
- hasProvider = m.modelMapper.MapModel(normalized) != ""
195
- }
196
-
197
- if hasProvider {
198
- geminiV1Beta1Handler(c)
199
- return
200
- }
201
- }
202
  }
203
  }
204
  // Non-POST or no local provider available -> proxy upstream
 
9
 
10
  "github.com/gin-gonic/gin"
11
  "github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
 
12
  "github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers"
13
  "github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers/claude"
14
  "github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers/gemini"
 
174
  }, m.modelMapper)
175
  geminiV1Beta1Handler := geminiV1Beta1Fallback.WrapHandler(geminiBridge)
176
 
177
+ // Route POST model calls through Gemini bridge with FallbackHandler.
178
+ // FallbackHandler checks provider -> mapping -> proxy fallback automatically.
179
  // All other methods (e.g., GET model listing) always proxy to upstream to preserve Amp CLI behavior.
180
  ampAPI.Any("/provider/google/v1beta1/*path", func(c *gin.Context) {
181
  if c.Request.Method == "POST" {
 
182
  if path := c.Param("path"); strings.Contains(path, "/models/") {
183
+ // POST with /models/ path -> use Gemini bridge with fallback handler
184
+ // FallbackHandler will check provider/mapping and proxy if needed
185
+ geminiV1Beta1Handler(c)
186
+ return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  }
188
  }
189
  // Non-POST or no local provider available -> proxy upstream