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
+10
View File
@@ -25,6 +25,11 @@ type SessionService interface {
BatchDeleteSessions(ctx context.Context, ids []string) error
// DeleteAllSessions deletes all sessions for the current tenant
DeleteAllSessions(ctx context.Context) error
// ListSessions returns a page of sessions for the current tenant/user with
// search/source filters and pin-aware ordering. User scope is pulled from ctx.
ListSessions(ctx context.Context, query *types.SessionListQuery) (*types.PageResult, error)
// SetSessionPinned pins or unpins the session for the current user scope.
SetSessionPinned(ctx context.Context, sessionID string, pinned bool) error
// GenerateTitle generates a title for the current conversation
// modelID: optional model ID to use for title generation (if empty, uses first available KnowledgeQA model)
GenerateTitle(ctx context.Context, session *types.Session, messages []types.Message, modelID string) (string, error)
@@ -57,8 +62,13 @@ type SessionRepository interface {
GetByTenantID(ctx context.Context, tenantID uint64) ([]*types.Session, error)
// GetPagedByTenantID gets paged sessions of a tenant
GetPagedByTenantID(ctx context.Context, tenantID uint64, page *types.Pagination) ([]*types.Session, int64, error)
// QueryPaged lists sessions with filters, user-scoped ownership and pin-aware ordering.
QueryPaged(ctx context.Context, q *types.SessionListQuery) ([]*types.SessionListItem, int64, error)
// Update updates a session
Update(ctx context.Context, session *types.Session) error
// SetPinned pins or unpins a session row scoped by tenant.
// userID, when non-empty, is enforced so users cannot pin sessions they don't own.
SetPinned(ctx context.Context, tenantID uint64, userID string, id string, pinned bool) error
// Delete deletes a session
Delete(ctx context.Context, tenantID uint64, id string) error
// BatchDelete deletes multiple sessions by IDs
+35
View File
@@ -81,6 +81,13 @@ type Session struct {
Description string `json:"description"`
// Tenant ID
TenantID uint64 `json:"tenant_id" gorm:"index"`
// UserID is the owner of the session. Empty for legacy rows (visible at
// tenant level) and for IM-created sessions that do not map to a WeKnora user.
UserID string `json:"user_id,omitempty" gorm:"type:varchar(36);index"`
// IsPinned indicates whether the session is pinned in the list.
IsPinned bool `json:"is_pinned" gorm:"default:false"`
// PinnedAt records when the session was pinned; nil when not pinned.
PinnedAt *time.Time `json:"pinned_at,omitempty"`
// // Strategy configuration
// KnowledgeBaseID string `json:"knowledge_base_id"` // 关联的知识库ID
@@ -112,6 +119,34 @@ func (s *Session) BeforeCreate(tx *gorm.DB) (err error) {
return nil
}
// SessionListQuery bundles the parameters for listing sessions.
// UserID empty means "tenant-wide" (used by API-key callers / legacy rows).
// Keyword matches title ILIKE '%keyword%'.
// Source values: "web" (no IM mapping) or an IM platform name (e.g. "feishu", "wechat").
// AgentID currently only filters sessions that have an IM channel mapping.
type SessionListQuery struct {
TenantID uint64
UserID string
Keyword string
Source string
AgentID string
Page int
PageSize int
}
// SessionListItem is a session row enriched with its IM origin (when any).
// IM-related fields are populated from the im_channel_sessions table via LEFT JOIN
// and are empty for Web-created sessions.
type SessionListItem struct {
Session
IMPlatform string `json:"im_platform,omitempty" gorm:"column:im_platform"`
IMChatID string `json:"im_chat_id,omitempty" gorm:"column:im_chat_id"`
IMThreadID string `json:"im_thread_id,omitempty" gorm:"column:im_thread_id"`
IMUserID string `json:"im_user_id,omitempty" gorm:"column:im_user_id"`
IMAgentID string `json:"im_agent_id,omitempty" gorm:"column:im_agent_id"`
IMChannelID string `json:"im_channel_id,omitempty" gorm:"column:im_channel_id"`
}
// StringArray represents a list of strings
type StringArray []string