Skip to content

Commit 406d4e8

Browse files
authored
Merge pull request #272 from trungutt/get-sessions-by-agentName
Add endpoint to retrieve sessions by agent filename
2 parents f839b69 + c46beed commit 406d4e8

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

pkg/server/server.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ func New(sessionStore session.Store, runConfig config.RuntimeConfig, teams map[s
101101
group.DELETE("/agents", s.deleteAgent)
102102
// List all sessions
103103
group.GET("/sessions", s.getSessions)
104+
// Get sessions by agent filename
105+
group.GET("/sessions/agent/:id", s.getSessionsByAgent)
104106
// Get a session by id
105107
group.GET("/sessions/:id", s.getSession)
106108
// Resume a session by id
@@ -769,6 +771,32 @@ func (s *Server) getSessions(c echo.Context) error {
769771
return c.JSON(http.StatusOK, responses)
770772
}
771773

774+
func (s *Server) getSessionsByAgent(c echo.Context) error {
775+
agentFilename := c.Param("id")
776+
if agentFilename == "" {
777+
return c.JSON(http.StatusBadRequest, map[string]string{"error": "id parameter is required"})
778+
}
779+
780+
sessions, err := s.sessionStore.GetSessionsByAgent(c.Request().Context(), agentFilename)
781+
if err != nil {
782+
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to get sessions for agent"})
783+
}
784+
785+
responses := make([]api.SessionsResponse, len(sessions))
786+
for i, sess := range sessions {
787+
responses[i] = api.SessionsResponse{
788+
ID: sess.ID,
789+
Title: sess.Title,
790+
CreatedAt: sess.CreatedAt.Format(time.RFC3339),
791+
NumMessages: len(sess.GetAllMessages()),
792+
InputTokens: sess.InputTokens,
793+
OutputTokens: sess.OutputTokens,
794+
GetMostRecentAgentFilename: sess.GetMostRecentAgentFilename(),
795+
}
796+
}
797+
return c.JSON(http.StatusOK, responses)
798+
}
799+
772800
func (s *Server) createSession(c echo.Context) error {
773801
var sessionTemplate session.Session
774802
if err := c.Bind(&sessionTemplate); err != nil {

pkg/session/store.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Store interface {
3030
AddSession(ctx context.Context, session *Session) error
3131
GetSession(ctx context.Context, id string) (*Session, error)
3232
GetSessions(ctx context.Context) ([]*Session, error)
33+
GetSessionsByAgent(ctx context.Context, agentFilename string) ([]*Session, error)
3334
DeleteSession(ctx context.Context, id string) error
3435
UpdateSession(ctx context.Context, session *Session) error
3536
}
@@ -260,6 +261,32 @@ func (s *SQLiteSessionStore) GetSessions(ctx context.Context) ([]*Session, error
260261
return sessions, nil
261262
}
262263

264+
// GetSessionsByAgent retrieves all sessions for a specific agent
265+
func (s *SQLiteSessionStore) GetSessionsByAgent(ctx context.Context, agentFilename string) ([]*Session, error) {
266+
allSessions, err := s.GetSessions(ctx)
267+
if err != nil {
268+
return nil, err
269+
}
270+
271+
filteredSessions := make([]*Session, 0)
272+
for _, session := range allSessions {
273+
// Check if any message in this session belongs to the specified agent
274+
hasAgentMessage := false
275+
for _, item := range session.Messages {
276+
if item.Message != nil && item.Message.AgentFilename == agentFilename {
277+
hasAgentMessage = true
278+
break
279+
}
280+
}
281+
282+
if hasAgentMessage {
283+
filteredSessions = append(filteredSessions, session)
284+
}
285+
}
286+
287+
return filteredSessions, nil
288+
}
289+
263290
// DeleteSession deletes a session by ID
264291
func (s *SQLiteSessionStore) DeleteSession(ctx context.Context, id string) error {
265292
if id == "" {

0 commit comments

Comments
 (0)