@@ -22,43 +22,35 @@ import (
2222 "github.com/docker/cagent/pkg/config"
2323 "github.com/docker/cagent/pkg/runtime"
2424 "github.com/docker/cagent/pkg/session"
25- "github.com/docker/cagent/pkg/team"
26- "github.com/docker/cagent/pkg/teamloader"
2725 "github.com/docker/cagent/pkg/tools"
2826)
2927
3028type Server struct {
3129 e * echo.Echo
32- runtimes * concurrent.Map [string , runtime.Runtime ]
3330 runtimeCancels * concurrent.Map [string , context.CancelFunc ]
3431 sessionStore session.Store
3532 runConfig * config.RuntimeConfig
36- agentSources config. Sources
33+ sm * sessionManager
3734}
3835
39- type Opt func (* Server ) error
40-
41- func New (sessionStore session.Store , runConfig * config.RuntimeConfig , agentSources config.Sources ) (* Server , error ) {
36+ func New (ctx context.Context , sessionStore session.Store , runConfig * config.RuntimeConfig , refreshInterval time.Duration , agentSources config.Sources ) (* Server , error ) {
4237 e := echo .New ()
4338 e .Use (middleware .CORS ())
4439 e .Use (middleware .Logger ())
4540
4641 s := & Server {
4742 e : e ,
48- runtimes : concurrent .NewMap [string , runtime.Runtime ](),
4943 runtimeCancels : concurrent .NewMap [string , context.CancelFunc ](),
5044 sessionStore : sessionStore ,
5145 runConfig : runConfig ,
52- agentSources : agentSources ,
46+ sm : newSessionManager ( ctx , agentSources , refreshInterval ) ,
5347 }
5448
5549 group := e .Group ("/api" )
5650
5751 // List all available agents
5852 group .GET ("/agents" , s .getAgents )
5953
60- // SESSIONS
61-
6254 // List all sessions
6355 group .GET ("/sessions" , s .getSessions )
6456 // Get a session by id
@@ -95,11 +87,9 @@ func (s *Server) Serve(ctx context.Context, ln net.Listener) error {
9587 return nil
9688}
9789
98- // API handlers
99-
10090func (s * Server ) getAgents (c echo.Context ) error {
10191 agents := []api.Agent {}
102- for k , agentSource := range s .agentSources {
92+ for k , agentSource := range s .sm . sources {
10393 slog .Debug ("API source" , "source" , agentSource .Name ())
10494
10595 c , err := config .Load (c .Request ().Context (), agentSource )
@@ -252,7 +242,7 @@ func (s *Server) resumeSession(c echo.Context) error {
252242 return echo .NewHTTPError (http .StatusBadRequest , fmt .Sprintf ("invalid request body: %v" , err ))
253243 }
254244
255- rt , exists := s .runtimes .Load (sessionID )
245+ rt , exists := s .sm . runtimes .Load (sessionID )
256246 if ! exists {
257247 return echo .NewHTTPError (http .StatusNotFound , fmt .Sprintf ("runtime not found: %s" , sessionID ))
258248 }
@@ -273,9 +263,9 @@ func (s *Server) deleteSession(c echo.Context) error {
273263 }
274264
275265 // Clean up the runtime
276- if _ , exists := s .runtimes .Load (sessionID ); exists {
266+ if _ , exists := s .sm . runtimes .Load (sessionID ); exists {
277267 slog .Debug ("Removing runtime for session" , "session_id" , sessionID )
278- s .runtimes .Delete (sessionID )
268+ s .sm . runtimes .Delete (sessionID )
279269 }
280270
281271 // Delete the session from storage
@@ -307,7 +297,7 @@ func (s *Server) runAgent(c echo.Context) error {
307297 rc := s .runConfig .Clone ()
308298 rc .WorkingDir = sess .WorkingDir
309299
310- rt , err := s .runtimeForSession (c .Request ().Context (), sess , agentFilename , currentAgent , rc )
300+ rt , err := s .sm . runtimeForSession (c .Request ().Context (), sess , agentFilename , currentAgent , rc )
311301 if err != nil {
312302 return echo .NewHTTPError (http .StatusInternalServerError , fmt .Sprintf ("failed to get runtime for session: %v" , err ))
313303 }
@@ -332,7 +322,6 @@ func (s *Server) runAgent(c echo.Context) error {
332322 c .Response ().Header ().Set ("Connection" , "keep-alive" )
333323 c .Response ().WriteHeader (http .StatusOK )
334324
335- // Create a cancellable context for this stream
336325 streamCtx , cancel := context .WithCancel (c .Request ().Context ())
337326 s .runtimeCancels .Store (sess .ID , cancel )
338327 defer func () {
@@ -356,56 +345,14 @@ func (s *Server) runAgent(c echo.Context) error {
356345 return nil
357346}
358347
359- func (s * Server ) runtimeForSession (ctx context.Context , sess * session.Session , agentFilename , currentAgent string , runConfig * config.RuntimeConfig ) (runtime.Runtime , error ) {
360- rt , exists := s .runtimes .Load (sess .ID )
361- if exists {
362- return rt , nil
363- }
364-
365- t , err := s .loadTeam (ctx , agentFilename , runConfig )
366- if err != nil {
367- return nil , err
368- }
369-
370- agent , err := t .Agent (currentAgent )
371- if err != nil {
372- return nil , echo .NewHTTPError (http .StatusNotFound , fmt .Sprintf ("agent not found: %v" , err ))
373- }
374- sess .MaxIterations = agent .MaxIterations ()
375-
376- opts := []runtime.Opt {
377- runtime .WithCurrentAgent (currentAgent ),
378- runtime .WithManagedOAuth (false ),
379- runtime .WithRootSessionID (sess .ID ),
380- }
381- rt , err = runtime .New (t , opts ... )
382- if err != nil {
383- slog .Error ("Failed to create runtime" , "error" , err )
384- return nil , echo .NewHTTPError (http .StatusInternalServerError , fmt .Sprintf ("failed to create runtime: %v" , err ))
385- }
386- s .runtimes .Store (sess .ID , rt )
387- slog .Debug ("Runtime created for session" , "session_id" , sess .ID )
388-
389- return rt , nil
390- }
391-
392- func (s * Server ) loadTeam (ctx context.Context , agentFilename string , runConfig * config.RuntimeConfig ) (* team.Team , error ) {
393- agentSource , found := s .agentSources [agentFilename ]
394- if ! found {
395- return nil , fmt .Errorf ("agent not found: %s" , agentFilename )
396- }
397-
398- return teamloader .Load (ctx , agentSource , runConfig )
399- }
400-
401348func (s * Server ) elicitation (c echo.Context ) error {
402349 sessionID := c .Param ("id" )
403350 var req api.ResumeElicitationRequest
404351 if err := c .Bind (& req ); err != nil {
405352 return echo .NewHTTPError (http .StatusBadRequest , fmt .Sprintf ("invalid request body: %v" , err ))
406353 }
407354
408- rt , exists := s .runtimes .Load (sessionID )
355+ rt , exists := s .sm . runtimes .Load (sessionID )
409356 if ! exists {
410357 return c .JSON (http .StatusNotFound , map [string ]string {"error" : fmt .Sprintf ("runtime not found: %s" , sessionID )})
411358 }
0 commit comments