MCPcopy Create free account
hub / github.com/tbphp/gpt-load / HandleProxy

Method HandleProxy

internal/proxy/server.go:61–114  ·  view source on GitHub ↗

HandleProxy is the main entry point for proxy requests, refactored based on the stable .bak logic.

(c *gin.Context)

Source from the content-addressed store, hash-verified

59
60// HandleProxy is the main entry point for proxy requests, refactored based on the stable .bak logic.
61func (ps *ProxyServer) HandleProxy(c *gin.Context) {
62 startTime := time.Now()
63 groupName := c.Param("group_name")
64
65 originalGroup, err := ps.groupManager.GetGroupByName(groupName)
66 if err != nil {
67 response.Error(c, app_errors.ParseDBError(err))
68 return
69 }
70
71 // Select sub-group if this is an aggregate group
72 subGroupName, err := ps.subGroupManager.SelectSubGroup(originalGroup)
73 if err != nil {
74 logrus.WithFields(logrus.Fields{
75 "aggregate_group": originalGroup.Name,
76 "error": err,
77 }).Error("Failed to select sub-group from aggregate")
78 response.Error(c, app_errors.NewAPIError(app_errors.ErrNoKeysAvailable, "No available sub-groups"))
79 return
80 }
81
82 group := originalGroup
83 if subGroupName != "" {
84 group, err = ps.groupManager.GetGroupByName(subGroupName)
85 if err != nil {
86 response.Error(c, app_errors.ParseDBError(err))
87 return
88 }
89 }
90
91 channelHandler, err := ps.channelFactory.GetChannel(group)
92 if err != nil {
93 response.Error(c, app_errors.NewAPIError(app_errors.ErrInternalServer, fmt.Sprintf("Failed to get channel for group '%s': %v", groupName, err)))
94 return
95 }
96
97 bodyBytes, err := io.ReadAll(c.Request.Body)
98 if err != nil {
99 logrus.Errorf("Failed to read request body: %v", err)
100 response.Error(c, app_errors.NewAPIError(app_errors.ErrBadRequest, "Failed to read request body"))
101 return
102 }
103 c.Request.Body.Close()
104
105 finalBodyBytes, err := ps.applyParamOverrides(bodyBytes, group)
106 if err != nil {
107 response.Error(c, app_errors.NewAPIError(app_errors.ErrInternalServer, fmt.Sprintf("Failed to apply parameter overrides: %v", err)))
108 return
109 }
110
111 isStream := channelHandler.IsStreamRequest(c, bodyBytes)
112
113 ps.executeRequestWithRetry(c, channelHandler, originalGroup, group, finalBodyBytes, isStream, startTime, 0)
114}
115
116// executeRequestWithRetry is the core recursive function for handling requests and retries.
117func (ps *ProxyServer) executeRequestWithRetry(

Callers

nothing calls this directly

Calls 9

applyParamOverridesMethod · 0.95
ErrorFunction · 0.92
GetGroupByNameMethod · 0.80
SelectSubGroupMethod · 0.80
GetChannelMethod · 0.80
CloseMethod · 0.65
IsStreamRequestMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected