feat(sessions): add keyword search, user-scoped pinning, and IM source visibility

Sessions today are a flat list ordered by updated_at. Two gaps showed up in
practice:
- Users cannot find specific chats as the list grows beyond a screen.
- IM-created sessions (WeCom/Feishu/Slack/...) are indistinguishable:
  every title was "IM-<platform>" or "IM-<platform>-<username>" and the
  list API hid the underlying im_channel_sessions mapping, so admins had
  no way to tell which Feishu group a session came from.

Backend
- Migration 000039 adds sessions.user_id (owner), is_pinned, pinned_at
  plus a composite index for the list query. Existing rows keep user_id
  NULL and stay visible at the tenant level for backward compatibility.
- CreateSession now writes the caller's user_id from auth context.
- GET /sessions accepts keyword / source / agent_id. When any filter is
  set, the response switches to enriched items that LEFT JOIN
  im_channel_sessions and expose im_platform / im_chat_id / im_thread_id
  / im_user_id / im_agent_id / im_channel_id. No filters => legacy shape,
  existing clients unaffected.
- Ordering: is_pinned DESC, pinned_at DESC NULLS LAST, updated_at DESC.
- POST/DELETE /sessions/:id/pin for user-scoped pin/unpin.
- IM session titles: "[platform] <user|chat|thread>" with short ID
  suffixes so group/DM/thread sessions are visually distinct without
  needing a round-trip to fetch a display name from the IM adapter.

Frontend
- Search input debounced at 300ms drives the keyword filter.
- Pinned chats render in a dedicated group above the time-based groups,
  with a pin icon and a pin/unpin entry in the per-chat dropdown.
- IM chats get a short [platform] badge in the list.
- Pin toggle is optimistic and guards against double-clicks.
- zh/en/ko/ru i18n keys added for the new strings.
This commit is contained in:
wizardchen
2026-04-30 15:14:49 +08:00
committed by lyingbug
parent e029d31f86
commit dbd804d6e3
15 changed files with 564 additions and 38 deletions
+96 -3
View File
@@ -108,6 +108,11 @@ func (h *Handler) CreateSession(c *gin.Context) {
Title: request.Title,
Description: request.Description,
}
// Attach the calling user as the session owner when available.
// API-key / legacy callers without a user id fall back to tenant-level visibility.
if userID, ok := types.UserIDFromContext(ctx); ok {
createdSession.UserID = userID
}
// Call service to create session
logger.Infof(ctx, "Calling session service to create session")
@@ -175,12 +180,15 @@ func (h *Handler) GetSession(c *gin.Context) {
// GetSessionsByTenant godoc
// @Summary 获取会话列表
// @Description 获取当前租户的会话列表,支持分页
// @Description 获取当前租户的会话列表,支持分页、关键字搜索、按来源/Agent 筛选
// @Tags 会话
// @Accept json
// @Produce json
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
// @Param keyword query string false "标题模糊搜索"
// @Param source query string false "来源过滤:web / feishu / wechat / slack / ..."
// @Param agent_id query string false "按 Agent 过滤(仅对 IM 会话生效)"
// @Success 200 {object} map[string]interface{} "会话列表"
// @Failure 400 {object} errors.AppError "请求参数错误"
// @Security Bearer
@@ -197,6 +205,36 @@ func (h *Handler) GetSessionsByTenant(c *gin.Context) {
return
}
keyword := c.Query("keyword")
source := c.Query("source")
agentID := c.Query("agent_id")
// When the caller uses any of the new filter knobs, return enriched items
// (with IM origin fields). Otherwise keep the legacy response so existing
// clients are unaffected.
if keyword != "" || source != "" || agentID != "" {
result, err := h.sessionService.ListSessions(ctx, &types.SessionListQuery{
Keyword: keyword,
Source: source,
AgentID: agentID,
Page: pagination.Page,
PageSize: pagination.PageSize,
})
if err != nil {
logger.ErrorWithFields(ctx, err, nil)
c.Error(errors.NewInternalServerError(err.Error()))
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": result.Data,
"total": result.Total,
"page": result.Page,
"page_size": result.PageSize,
})
return
}
// Use paginated query to get sessions
result, err := h.sessionService.GetPagedSessionsByTenant(ctx, &pagination)
if err != nil {
@@ -441,3 +479,58 @@ func (h *Handler) BatchDeleteSessions(c *gin.Context) {
"message": "Sessions deleted successfully",
})
}
// PinSession godoc
// @Summary 置顶会话
// @Description 将指定会话置顶(用户维度)
// @Tags 会话
// @Produce json
// @Param id path string true "会话ID"
// @Success 200 {object} map[string]interface{} "置顶成功"
// @Failure 404 {object} errors.AppError "会话不存在"
// @Security Bearer
// @Security ApiKeyAuth
// @Router /sessions/{id}/pin [post]
func (h *Handler) PinSession(c *gin.Context) {
h.setSessionPinned(c, true)
}
// UnpinSession godoc
// @Summary 取消置顶会话
// @Description 取消指定会话的置顶
// @Tags 会话
// @Produce json
// @Param id path string true "会话ID"
// @Success 200 {object} map[string]interface{} "取消置顶成功"
// @Failure 404 {object} errors.AppError "会话不存在"
// @Security Bearer
// @Security ApiKeyAuth
// @Router /sessions/{id}/pin [delete]
func (h *Handler) UnpinSession(c *gin.Context) {
h.setSessionPinned(c, false)
}
func (h *Handler) setSessionPinned(c *gin.Context, pinned bool) {
ctx := c.Request.Context()
id := secutils.SanitizeForLog(c.Param("id"))
if id == "" {
logger.Error(ctx, "Session ID is empty")
c.Error(errors.NewBadRequestError(errors.ErrInvalidSessionID.Error()))
return
}
if err := h.sessionService.SetSessionPinned(ctx, id, pinned); err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
"session_id": id,
"pinned": pinned,
})
c.Error(errors.NewInternalServerError(err.Error()))
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"is_pinned": pinned,
})
}