feat(im): IM session improvements — titles, in-flight recovery, classification

Three related improvements to the IM-session experience:

- Auto-generated titles: a new IM session starts untitled and gets a
  content-based title from its first message via the same GenerateTitleAsync
  flow web sessions use, instead of an identity title like "user · dm". Falls
  back to the identity title when the first message has no text. All platforms.

- In-flight reply recovery (web chat): opening a session whose reply is still
  being generated on an IM channel made the web client attach via
  continue-stream and fail (the IM answer never streams through this server),
  flashing a "stream failed" toast and forcing a manual refresh. Treat a failed
  attach to an IM reply (channel === 'im') as recoverable — poll until it
  completes, then reload the thread; on timeout surface the standard error.
  Web/api replies keep the original behaviour, untouched.

- Source-filter classification: the sidebar filter treats "web" as sessions
  with no IM mapping, but the list join excluded soft-deleted mappings, so
  /clear'd IM sessions leaked into the user's own web chats. Include soft-deleted
  mappings so a session ever bound to an IM channel stays under its platform.
  Adds a repository regression test.
This commit is contained in:
nullkey
2026-06-11 11:55:15 +08:00
committed by lyingbug
parent ac84dacc00
commit 5851c64ba1
5 changed files with 234 additions and 12 deletions
+10 -1
View File
@@ -136,7 +136,16 @@ func (r *sessionRepository) QueryPaged(
}
// LEFT JOIN IM mappings to surface origin fields and support source/agent filters.
joinClause := "LEFT JOIN im_channel_sessions ics ON ics.session_id = s.id AND ics.deleted_at IS NULL"
// Soft-deleted mappings are intentionally included: a session that was ever bound
// to an IM channel belongs to that platform, not "web". /clear and session
// recycling soft-delete the mapping (and start a fresh session), so filtering
// deleted mappings out here would mis-bucket those past IM conversations into the
// user's own web chats ("web" = ics.id IS NULL).
// Safe from row fan-out because the IM flow only ever creates a *fresh* session
// for a new mapping (never re-maps an existing one), so a session has at most one
// mapping row. If that ever changes, this JOIN would need a one-row-per-session
// guard (the unique index only constrains active mappings).
joinClause := "LEFT JOIN im_channel_sessions ics ON ics.session_id = s.id"
applySource := func(db *gorm.DB) *gorm.DB {
switch strings.ToLower(strings.TrimSpace(q.Source)) {
@@ -161,3 +161,64 @@ func TestSessionRepositoryDeleteAllHonorsUserScope(t *testing.T) {
require.Zero(t, countActiveSessionsForTest(t, db, legacySession.ID))
require.EqualValues(t, 1, countActiveSessionsForTest(t, db, otherTenantSession.ID))
}
// im_channel_sessions row for QueryPaged source-filter tests. Mirrors the columns
// the LEFT JOIN projects; kept local to avoid importing internal/im.
type testIMChannelSession struct {
ID string `gorm:"primaryKey"`
SessionID string
Platform string
ChatID string
ThreadID string
UserID string
AgentID string
IMChannelID string `gorm:"column:im_channel_id"`
TenantID uint64
DeletedAt gorm.DeletedAt
}
func (testIMChannelSession) TableName() string { return "im_channel_sessions" }
func listItemIDsForTest(items []*types.SessionListItem) []string {
ids := make([]string, 0, len(items))
for _, it := range items {
ids = append(ids, it.ID)
}
return ids
}
// A /clear (or session recycling) soft-deletes the IM mapping and starts a fresh
// session. The old session must stay under its IM platform, not leak into "web"
// (which would happen if the source-filter join excluded soft-deleted mappings).
func TestSessionRepositoryQueryPagedKeepsClearedIMSessionsOutOfWeb(t *testing.T) {
repo, db := newSessionRepositoryForTest(t)
require.NoError(t, db.AutoMigrate(&testIMChannelSession{}))
ctx := context.Background()
web := createSessionForTest(t, db, 1, "alice") // never bound to IM -> web
active := createSessionForTest(t, db, 1, "alice") // active IM mapping
cleared := createSessionForTest(t, db, 1, "alice") // IM mapping soft-deleted by /clear
require.NoError(t, db.Create(&testIMChannelSession{
ID: "m-active", SessionID: active.ID, Platform: "wecom", TenantID: 1,
}).Error)
clearedMapping := &testIMChannelSession{
ID: "m-cleared", SessionID: cleared.ID, Platform: "wecom", TenantID: 1,
}
require.NoError(t, db.Create(clearedMapping).Error)
require.NoError(t, db.Delete(clearedMapping).Error) // soft-delete, as /clear does
webItems, _, err := repo.QueryPaged(ctx, &types.SessionListQuery{
TenantID: 1, UserID: "alice", Source: "web", Page: 1, PageSize: 50,
})
require.NoError(t, err)
require.Equal(t, []string{web.ID}, listItemIDsForTest(webItems),
"web must exclude sessions that ever had an IM mapping, including cleared ones")
wecomItems, _, err := repo.QueryPaged(ctx, &types.SessionListQuery{
TenantID: 1, UserID: "alice", Source: "wecom", Page: 1, PageSize: 50,
})
require.NoError(t, err)
require.ElementsMatch(t, []string{active.ID, cleared.ID}, listItemIDsForTest(wecomItems),
"wecom must include both the active and the cleared IM session")
}