diff --git a/internal/common/http.go b/internal/common/http.go index e3827b5e4d..190c7cea50 100644 --- a/internal/common/http.go +++ b/internal/common/http.go @@ -28,6 +28,7 @@ type response struct { Code ErrorCode `json:"code"` Data interface{} `json:"data"` Message interface{} `json:"message"` + Total interface{} `json:"total,omitempty"` } // errorResponse error response @@ -45,6 +46,16 @@ func SuccessWithData(c *gin.Context, data interface{}, message interface{}) { }) } +// SuccessWithDataAndTotal returns success response with data and total number +func SuccessWithDataAndTotal(c *gin.Context, data, total, message interface{}) { + c.JSON(http.StatusOK, response{ + Code: CodeSuccess, + Data: data, + Total: total, + Message: message, + }) +} + // SuccessNoMessage returns success response without message func SuccessNoMessage(c *gin.Context, data interface{}) { c.JSON(http.StatusOK, response{ diff --git a/internal/dao/chat_session.go b/internal/dao/chat_session.go index 78faafbf8f..46c2d3b670 100644 --- a/internal/dao/chat_session.go +++ b/internal/dao/chat_session.go @@ -39,6 +39,7 @@ type ListAgentSessionsParams struct { Desc bool SessionID string UserID string + TenantID string IncludeDSL bool Keywords string FromDate *time.Time diff --git a/internal/handler/agent.go b/internal/handler/agent.go index 6ba8cacc26..097d1a467f 100644 --- a/internal/handler/agent.go +++ b/internal/handler/agent.go @@ -917,11 +917,14 @@ func (h *AgentHandler) ListAgentSessions(c *gin.Context) { keywords := c.Query("keywords") fromDate := c.Query("from_date") toDate := c.Query("to_date") - orderby := c.DefaultQuery("orderby", "create_time") - desc := c.DefaultQuery("desc", "true") != "false" + orderby := c.DefaultQuery("orderby", "update_time") + descParam := c.Query("desc") + desc := descParam != "false" && descParam != "False" sessionID := c.Query("id") - expUserID := c.Query("user_id") - includeDSL := c.Query("dsl") == "true" + queryUserID := c.Query("user_id") + expUserID := c.Query("exp_user_id") + dslParam := c.Query("dsl") + includeDSL := dslParam != "false" && dslParam != "False" ctx := c.Request.Context() resp, code, err := h.agentService.ListAgentSessions(ctx, user.ID, user.ID, canvasID, service.ListAgentSessionsRequest{ Page: page, @@ -932,7 +935,7 @@ func (h *AgentHandler) ListAgentSessions(c *gin.Context) { OrderBy: orderby, Desc: desc, SessionID: sessionID, - UserID: user.ID, + UserID: queryUserID, ExpUserID: expUserID, IncludeDSL: includeDSL, }) @@ -940,7 +943,7 @@ func (h *AgentHandler) ListAgentSessions(c *gin.Context) { common.ErrorWithCode(c, code, err.Error()) return } - common.SuccessWithData(c, resp.Data, "success") + common.SuccessWithDataAndTotal(c, resp.Data, resp.Total, "success") } // CreateAgentSession POST /api/v1/agents/:canvas_id/sessions diff --git a/internal/service/agent_sessions.go b/internal/service/agent_sessions.go index e80cf3987d..41e58d04bb 100644 --- a/internal/service/agent_sessions.go +++ b/internal/service/agent_sessions.go @@ -310,7 +310,7 @@ func checkDuplicateSessionIDs(ids []string) ([]string, []string) { } // ListAgentSessions returns paginated agent sessions visible to the caller. -func (s *AgentService) ListAgentSessions(ctx context.Context, userID, tenantID, agentID string, req ListAgentSessionsRequest) (*ListAgentSessionsResponse, common.ErrorCode, error) { +func (s *AgentService) ListAgentSessions(ctx context.Context, userID, _ string, agentID string, req ListAgentSessionsRequest) (*ListAgentSessionsResponse, common.ErrorCode, error) { if agentID == "" { return nil, common.CodeArgumentError, errors.New("agent_id is required") } diff --git a/internal/service/agent_test.go b/internal/service/agent_test.go index e0282fae07..8a5ecfc3f5 100644 --- a/internal/service/agent_test.go +++ b/internal/service/agent_test.go @@ -881,6 +881,7 @@ func TestListAgentSessionsServiceSuccess(t *testing.T) { createAgentSessionTestCanvas(t, "canvas-1", "user-1") createAgentSessionTestConversation(t, "session-old", "canvas-1", "user-1", 1000) createAgentSessionTestConversation(t, "session-new", "canvas-1", "user-1", 3000) + createAgentSessionTestConversation(t, "session-team", "canvas-1", "team-user", 2000) createAgentSessionTestConversation(t, "session-other-agent", "canvas-other", "user-1", 9999) ctx := t.Context() @@ -897,11 +898,11 @@ func TestListAgentSessionsServiceSuccess(t *testing.T) { if code != common.CodeSuccess { t.Fatalf("expected code %d, got %d", common.CodeSuccess, code) } - if resp.Total != 2 { - t.Fatalf("expected total 2, got %d", resp.Total) + if resp.Total != 3 { + t.Fatalf("expected total 3, got %d", resp.Total) } - if len(resp.Data) != 2 { - t.Fatalf("expected 2 sessions, got %d", len(resp.Data)) + if len(resp.Data) != 3 { + t.Fatalf("expected 3 sessions, got %d", len(resp.Data)) } if resp.Data[0]["id"] != "session-new" { t.Fatalf("expected newest session first, got %v", resp.Data[0]["id"])