11package router
22
33import (
4- "context "
4+ "fmt "
55 "log"
66 "net/http"
77 "net/http/httputil"
@@ -42,33 +42,34 @@ func (s *Server) handleHealthReady(c *gin.Context) {
4242 })
4343}
4444
45- // handleAgentInvoke handles agent invocation requests
46- func (s * Server ) handleAgentInvoke (c * gin.Context ) {
47- agentNamespace := c .Param ("agentNamespace" )
48- agentName := c .Param ("agentName" )
49- path := c .Param ("path" )
50-
51- log .Printf ("Agent invoke request: namespace=%s, agent=%s, path=%s" , agentNamespace , agentName , path )
45+ // handleInvoke is a private helper function that handles invocation requests for both agents and code interpreters
46+ func (s * Server ) handleInvoke (c * gin.Context , namespace , name , path , kind string ) {
47+ log .Printf ("%s invoke request: namespace=%s, name=%s, path=%s" , kind , namespace , name , path )
5248
5349 // Extract session ID from header
5450 sessionID := c .GetHeader ("x-agentcube-session-id" )
5551
5652 // Get sandbox info from session manager
57- sandbox , err := s .sessionManager .GetSandboxBySession (sessionID , agentNamespace , agentName , "AgentRuntime" )
53+ sandbox , err := s .sessionManager .GetSandboxBySession (c . Request . Context (), sessionID , namespace , name , kind )
5854 if err != nil {
59- log .Printf ("Failed to get sandbox info: %v" , err )
60- c .JSON (http .StatusInternalServerError , gin.H {
61- "error" : "internal server error" ,
62- "code" : "INTERNAL_ERROR " ,
55+ log .Printf ("Failed to get sandbox info: %v, session id %s " , err , sessionID )
56+ c .JSON (http .StatusBadRequest , gin.H {
57+ "error" : fmt . Sprintf ( "Invalid session id %s" , sessionID ) ,
58+ "code" : "BadRequest " ,
6359 })
6460 return
6561 }
6662
6763 // Extract endpoint from sandbox - find matching entry point by path
6864 var endpoint string
6965 for _ , ep := range sandbox .EntryPoints {
70- if ep .Path == path || ep .Path == "" {
71- endpoint = ep .Endpoint
66+ if strings .HasPrefix (path , ep .Path ) {
67+ // Only add protocol if not already present
68+ if ep .Protocol != "" && ! strings .Contains (ep .Endpoint , "://" ) {
69+ endpoint = strings .ToLower (ep .Protocol ) + "://" + ep .Endpoint
70+ } else {
71+ endpoint = ep .Endpoint
72+ }
7273 break
7374 }
7475 }
@@ -83,12 +84,19 @@ func (s *Server) handleAgentInvoke(c *gin.Context) {
8384 })
8485 return
8586 }
86- endpoint = sandbox .EntryPoints [0 ].Endpoint
87+ // Only add protocol if not already present
88+ if sandbox .EntryPoints [0 ].Protocol != "" && ! strings .Contains (sandbox .EntryPoints [0 ].Endpoint , "://" ) {
89+ endpoint = strings .ToLower (sandbox .EntryPoints [0 ].Protocol ) + "://" + sandbox .EntryPoints [0 ].Endpoint
90+ } else {
91+ endpoint = sandbox .EntryPoints [0 ].Endpoint
92+ }
8793 }
8894
95+ log .Printf ("The selected entrypoint for session-id %s to sandbox is %s" , sandbox .SessionID , endpoint )
96+
8997 // Update session activity in Redis when receiving request
9098 if sandbox .SessionID != "" && sandbox .SandboxID != "" {
91- if err := s .redisClient .UpdateSandboxLastActivity (c .Request .Context (), sandbox .SandboxID , time .Now ()); err != nil {
99+ if err := s .redisClient .UpdateSessionLastActivity (c .Request .Context (), sandbox .SessionID , time .Now ()); err != nil {
92100 log .Printf ("Failed to update sandbox last activity for request: %v" , err )
93101 }
94102 }
@@ -97,59 +105,20 @@ func (s *Server) handleAgentInvoke(c *gin.Context) {
97105 s .forwardToSandbox (c , endpoint , path , sandbox .SessionID )
98106}
99107
108+ // handleAgentInvoke handles agent invocation requests
109+ func (s * Server ) handleAgentInvoke (c * gin.Context ) {
110+ namespace := c .Param ("namespace" )
111+ name := c .Param ("name" )
112+ path := c .Param ("path" )
113+ s .handleInvoke (c , namespace , name , path , "AgentRuntime" )
114+ }
115+
100116// handleCodeInterpreterInvoke handles code interpreter invocation requests
101117func (s * Server ) handleCodeInterpreterInvoke (c * gin.Context ) {
102118 namespace := c .Param ("namespace" )
103119 name := c .Param ("name" )
104120 path := c .Param ("path" )
105-
106- log .Printf ("Code interpreter invoke request: namespace=%s, name=%s, path=%s" , namespace , name , path )
107-
108- // Extract session ID from header
109- sessionID := c .GetHeader ("x-agentcube-session-id" )
110-
111- // Get sandbox info from session manager
112- sandbox , err := s .sessionManager .GetSandboxBySession (sessionID , namespace , name , "CodeInterpreter" )
113- if err != nil {
114- log .Printf ("Failed to get sandbox info: %v" , err )
115- c .JSON (http .StatusInternalServerError , gin.H {
116- "error" : "internal server error" ,
117- "code" : "INTERNAL_ERROR" ,
118- })
119- return
120- }
121-
122- // Extract endpoint from sandbox - find matching entry point by path
123- var endpoint string
124- for _ , ep := range sandbox .EntryPoints {
125- if ep .Path == path || ep .Path == "" {
126- endpoint = ep .Endpoint
127- break
128- }
129- }
130-
131- // If no matching endpoint found, use the first one as fallback
132- if endpoint == "" {
133- if len (sandbox .EntryPoints ) == 0 {
134- log .Printf ("No entry points found for sandbox: %s" , sandbox .SandboxID )
135- c .JSON (http .StatusInternalServerError , gin.H {
136- "error" : "internal server error" ,
137- "code" : "INTERNAL_ERROR" ,
138- })
139- return
140- }
141- endpoint = sandbox .EntryPoints [0 ].Endpoint
142- }
143-
144- // Update session activity in Redis when receiving request
145- if sandbox .SessionID != "" && sandbox .SandboxID != "" {
146- if err := s .redisClient .UpdateSandboxLastActivity (c .Request .Context (), sandbox .SandboxID , time .Now ()); err != nil {
147- log .Printf ("Failed to update sandbox last activity for request: %v" , err )
148- }
149- }
150-
151- // Forward request to sandbox with session ID
152- s .forwardToSandbox (c , endpoint , path , sandbox .SessionID )
121+ s .handleInvoke (c , namespace , name , path , "CodeInterpreter" )
153122}
154123
155124// forwardToSandbox forwards the request to the specified sandbox endpoint
@@ -228,10 +197,10 @@ func (s *Server) forwardToSandbox(c *gin.Context, endpoint, path, sessionID stri
228197 return nil
229198 }
230199
231- // Set timeout for the proxy request using configured timeout
232- ctx , cancel := context .WithTimeout (c .Request .Context (), time .Duration (s .config .RequestTimeout )* time .Second )
233- defer cancel ()
234- c .Request = c .Request .WithContext (ctx )
200+ // No timeout for invoke requests to allow long-running operations
201+ // ctx, cancel := context.WithTimeout(c.Request.Context(), time.Duration(s.config.RequestTimeout)*time.Second)
202+ // defer cancel()
203+ // c.Request = c.Request.WithContext(ctx)
235204
236205 // Use the proxy to serve the request
237206 proxy .ServeHTTP (c .Writer , c .Request )
0 commit comments