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:
+32
-4
@@ -1176,6 +1176,16 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, chann
|
||||
}
|
||||
}
|
||||
|
||||
// Title an untitled IM session from its first text message, like web chats.
|
||||
// GenerateTitleAsync self-guards on a non-empty title and persists to the DB;
|
||||
// nil eventBus is fine (IM has no live stream — the sidebar reloads it).
|
||||
if session.Title == "" && strings.TrimSpace(msg.Content) != "" {
|
||||
// Copy the session: the async title goroutine writes Title while the QA
|
||||
// worker below shares the same *session.
|
||||
sessionForTitle := *session
|
||||
s.sessionService.GenerateTitleAsync(sessionCtx, &sessionForTitle, msg.Content, "", nil)
|
||||
}
|
||||
|
||||
// 5. Enqueue the QA request into the bounded worker pool.
|
||||
// The worker pool controls LLM concurrency and provides backpressure.
|
||||
qaCtx, qaCancel := context.WithCancel(sessionCtx)
|
||||
@@ -1480,8 +1490,23 @@ func shortID(id string) string {
|
||||
return id
|
||||
}
|
||||
|
||||
// imInitialSessionTitle picks a new IM session's starting title: "" when the
|
||||
// message has text to summarise (so it gets a content-based title later, like
|
||||
// web chats), otherwise the IM identity title so the row is never blank.
|
||||
func imInitialSessionTitle(msg *IncomingMessage, identityTitle func(*IncomingMessage) string) string {
|
||||
if strings.TrimSpace(msg.Content) != "" {
|
||||
return ""
|
||||
}
|
||||
return identityTitle(msg)
|
||||
}
|
||||
|
||||
// resolveUserSession finds or creates a ChannelSession keyed by (platform, user_id, chat_id, tenant_id, agent_id).
|
||||
// This is the original session resolution strategy.
|
||||
//
|
||||
// Invariant: a cache miss creates a brand-new session — we never attach a second
|
||||
// mapping to an existing session. The session-list source filter (repository
|
||||
// QueryPaged) relies on this one-mapping-per-session property; if this ever
|
||||
// re-maps an existing session, that JOIN needs a one-row-per-session guard.
|
||||
func (s *Service) resolveUserSession(ctx context.Context, msg *IncomingMessage, tenantID uint64, agentID string, imChannelID string) (*ChannelSession, error) {
|
||||
var cs ChannelSession
|
||||
result := s.db.Where("platform = ? AND user_id = ? AND chat_id = ? AND tenant_id = ? AND agent_id = ? AND deleted_at IS NULL",
|
||||
@@ -1496,8 +1521,10 @@ func (s *Service) resolveUserSession(ctx context.Context, msg *IncomingMessage,
|
||||
return nil, fmt.Errorf("query channel session: %w", result.Error)
|
||||
}
|
||||
|
||||
// Create a new WeKnora session
|
||||
title := buildUserSessionTitle(msg)
|
||||
// Create a new WeKnora session. Start untitled when there's text to summarise
|
||||
// so it gets a content-based title after the first message (see HandleMessage);
|
||||
// fall back to the IM identity title otherwise.
|
||||
title := imInitialSessionTitle(msg, buildUserSessionTitle)
|
||||
|
||||
newSession := &types.Session{
|
||||
TenantID: tenantID,
|
||||
@@ -1568,8 +1595,9 @@ func (s *Service) resolveThreadSession(ctx context.Context, msg *IncomingMessage
|
||||
return nil, fmt.Errorf("query thread session: %w", result.Error)
|
||||
}
|
||||
|
||||
// Build a session title including chat + thread suffix for traceability.
|
||||
title := buildThreadSessionTitle(msg)
|
||||
// Start untitled when there's text to summarise so it gets a content-based
|
||||
// title after the first message; fall back to the chat/thread identity title.
|
||||
title := imInitialSessionTitle(msg, buildThreadSessionTitle)
|
||||
|
||||
newSession := &types.Session{
|
||||
TenantID: tenantID,
|
||||
|
||||
@@ -107,3 +107,49 @@ func TestBuildThreadSessionTitle(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIMInitialSessionTitle(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
msg *IncomingMessage
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "text message starts untitled so it gets a content-based title later",
|
||||
msg: &IncomingMessage{
|
||||
Platform: "feishu",
|
||||
UserName: "李四",
|
||||
ChatType: ChatTypeDirect,
|
||||
Content: "如何配置单点登录?",
|
||||
},
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "whitespace-only content falls back to the identity title",
|
||||
msg: &IncomingMessage{
|
||||
Platform: "feishu",
|
||||
UserName: "李四",
|
||||
ChatType: ChatTypeDirect,
|
||||
Content: " ",
|
||||
},
|
||||
want: "李四 · dm",
|
||||
},
|
||||
{
|
||||
name: "non-text message (no content) falls back to the identity title",
|
||||
msg: &IncomingMessage{
|
||||
Platform: "wecom",
|
||||
UserID: "WeCom_ZhangSan",
|
||||
ChatType: ChatTypeGroup,
|
||||
ChatID: "wc_group_1234",
|
||||
},
|
||||
want: "user ZhangSan · group oup_1234",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := imInitialSessionTitle(tt.msg, buildUserSessionTitle); got != tt.want {
|
||||
t.Errorf("imInitialSessionTitle() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user