fix:path rewriting & model list

This commit is contained in:
XOF
2025-11-25 20:48:15 +08:00
parent ad1e6180cf
commit 04d36e4d9e
10 changed files with 262 additions and 74 deletions

View File

@@ -986,6 +986,7 @@ func (h *ProxyHandler) getMaxRetries(isPreciseRouting bool, finalOpConfig *model
}
func (h *ProxyHandler) handleListModelsRequest(c *gin.Context) {
authTokenValue, exists := c.Get("authToken")
if !exists {
errToJSON(c, uuid.New().String(), errors.NewAPIError(errors.ErrUnauthorized, "Auth token not found in context"))
@@ -996,7 +997,61 @@ func (h *ProxyHandler) handleListModelsRequest(c *gin.Context) {
errToJSON(c, uuid.New().String(), errors.NewAPIError(errors.ErrInternalServer, "Invalid auth token type in context"))
return
}
modelNames := h.resourceService.GetAllowedModelsForToken(authToken)
groupName := c.Param("group_name")
h.logger.Infof("List models request: path=%s, groupName=%s", c.Request.URL.Path, groupName)
isPreciseRouting := groupName != ""
var modelNames []string
if isPreciseRouting {
group, ok := h.groupManager.GetGroupByName(groupName)
if !ok {
errToJSON(c, uuid.New().String(), errors.NewAPIError(errors.ErrNotFound, "Group not found"))
return
}
for _, modelMapping := range group.AllowedModels {
modelNames = append(modelNames, modelMapping.ModelName)
}
if len(modelNames) == 0 {
h.logger.Infof("Triggering passthrough for model list")
initialResources, err := h.resourceService.GetResourceFromGroup(c.Request.Context(), authToken, groupName)
if err != nil {
errToJSON(c, uuid.New().String(), errors.NewAPIError(errors.ErrInternalServer, "Failed to get resources"))
return
}
targetURL, _ := url.Parse(initialResources.UpstreamEndpoint.URL)
apiPath := strings.TrimPrefix(c.Request.URL.Path, "/proxy/"+groupName)
targetURL.Path = h.channel.RewritePath(targetURL.Path, apiPath)
h.logger.Infof("Final upstream path: %s", targetURL.String())
targetURL.RawQuery = c.Request.URL.RawQuery
req, _ := http.NewRequestWithContext(c.Request.Context(), "GET", targetURL.String(), nil)
h.channel.ModifyRequest(req, initialResources.APIKey)
client := &http.Client{Transport: h.transparentProxy.Transport}
resp, err := client.Do(req)
if err != nil {
errToJSON(c, uuid.New().String(), errors.NewAPIError(errors.ErrBadGateway, "Failed to fetch models"))
return
}
defer resp.Body.Close()
c.Writer.WriteHeader(resp.StatusCode)
for k, v := range resp.Header {
c.Writer.Header()[k] = v
}
io.Copy(c.Writer, resp.Body)
h.logger.Infof("Passthrough response sent")
return
}
} else {
modelNames = h.resourceService.GetAllowedModelsForToken(authToken)
}
if strings.Contains(c.Request.URL.Path, "/v1beta/") {
h.respondWithGeminiFormat(c, modelNames)
} else {