fix: agent log return zero total number (#17766)

### Summary

As title

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
Haruko386
2026-08-04 15:03:58 +08:00
committed by GitHub
parent 57b3a7384d
commit 970be641a4
5 changed files with 27 additions and 11 deletions

View File

@@ -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{

View File

@@ -39,6 +39,7 @@ type ListAgentSessionsParams struct {
Desc bool
SessionID string
UserID string
TenantID string
IncludeDSL bool
Keywords string
FromDate *time.Time

View File

@@ -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

View File

@@ -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")
}

View File

@@ -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"])