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
+48 -10
View File
@@ -1336,6 +1336,51 @@ func (s *Service) resolveSession(ctx context.Context, msg *IncomingMessage, tena
}
}
// buildUserSessionTitle produces a human-distinguishable title for a user-mode
// IM session. Platform adapters only surface ChatID, not a readable chat name,
// so we fall back to short ID suffixes to keep group/DM sessions visually distinct.
func buildUserSessionTitle(msg *IncomingMessage) string {
var b strings.Builder
fmt.Fprintf(&b, "[%s] ", msg.Platform)
if msg.UserName != "" {
b.WriteString(msg.UserName)
} else if msg.UserID != "" {
b.WriteString("user ")
b.WriteString(shortID(msg.UserID))
} else {
b.WriteString("user")
}
if msg.ChatType == ChatTypeGroup && msg.ChatID != "" {
fmt.Fprintf(&b, " · group %s", shortID(msg.ChatID))
} else if msg.ChatType == ChatTypeDirect {
b.WriteString(" · dm")
}
return b.String()
}
// buildThreadSessionTitle produces a title for a thread-mode IM session.
// In thread mode different users can share one session, so the user name is
// omitted and chat/thread IDs carry the distinguishing information.
func buildThreadSessionTitle(msg *IncomingMessage) string {
var b strings.Builder
fmt.Fprintf(&b, "[%s] ", msg.Platform)
if msg.ChatID != "" {
fmt.Fprintf(&b, "chat %s · ", shortID(msg.ChatID))
}
b.WriteString("thread ")
b.WriteString(shortID(msg.ThreadID))
return b.String()
}
// shortID returns the last 8 characters of id, or id itself when shorter.
// Used to keep long platform IDs readable inside titles without losing uniqueness.
func shortID(id string) string {
if len(id) > 8 {
return id[len(id)-8:]
}
return id
}
// resolveUserSession finds or creates a ChannelSession keyed by (platform, user_id, chat_id, tenant_id, agent_id).
// This is the original session resolution strategy.
func (s *Service) resolveUserSession(ctx context.Context, msg *IncomingMessage, tenantID uint64, agentID string, imChannelID string) (*ChannelSession, error) {
@@ -1353,10 +1398,7 @@ func (s *Service) resolveUserSession(ctx context.Context, msg *IncomingMessage,
}
// Create a new WeKnora session
title := fmt.Sprintf("IM-%s", msg.Platform)
if msg.UserName != "" {
title = fmt.Sprintf("IM-%s-%s", msg.Platform, msg.UserName)
}
title := buildUserSessionTitle(msg)
newSession := &types.Session{
TenantID: tenantID,
@@ -1427,12 +1469,8 @@ func (s *Service) resolveThreadSession(ctx context.Context, msg *IncomingMessage
return nil, fmt.Errorf("query thread session: %w", result.Error)
}
// Build a session title with thread ID suffix for traceability.
threadSuffix := threadID
if len(threadSuffix) > 8 {
threadSuffix = threadSuffix[len(threadSuffix)-8:]
}
title := fmt.Sprintf("IM-%s-thread-%s", msg.Platform, threadSuffix)
// Build a session title including chat + thread suffix for traceability.
title := buildThreadSessionTitle(msg)
newSession := &types.Session{
TenantID: tenantID,