refactor: enhance conversation handling and websocket integration with new team ID support
This commit is contained in:
@@ -5,6 +5,7 @@ import { toast } from "sonner"
|
||||
|
||||
import { createAdminWebSocketUrl } from "@/lib/api/admin"
|
||||
import { type AgentMessage } from "@/lib/api/agent"
|
||||
import { shouldReloadConversationListForRealtimePatch } from "@/lib/agent-conversation-realtime"
|
||||
import { readSession } from "@/lib/auth"
|
||||
import {
|
||||
normalizeRealtimeMessage,
|
||||
@@ -131,6 +132,11 @@ export function useAgentConversationRealtime() {
|
||||
|
||||
if (eventType.startsWith("conversation.") && payload) {
|
||||
store.applyRealtimeConversationChanged(payload)
|
||||
if (shouldReloadConversationListForRealtimePatch(payload)) {
|
||||
void store.resyncRealtimeData(conversationId).catch((error) => {
|
||||
toast.error(error instanceof Error ? error.message : "同步会话列表失败")
|
||||
})
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// ignore invalid ws payload
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import assert from "node:assert/strict"
|
||||
import test from "node:test"
|
||||
|
||||
import { shouldReloadConversationListForRealtimePatch } from "./agent-conversation-realtime.ts"
|
||||
|
||||
test("reloads conversation list when realtime patch changes list membership fields", () => {
|
||||
assert.equal(
|
||||
shouldReloadConversationListForRealtimePatch({
|
||||
conversationId: 1,
|
||||
status: 3,
|
||||
currentAssigneeId: 101,
|
||||
}),
|
||||
true
|
||||
)
|
||||
})
|
||||
|
||||
test("keeps local patching for message summary and unread-only changes", () => {
|
||||
assert.equal(
|
||||
shouldReloadConversationListForRealtimePatch({
|
||||
conversationId: 1,
|
||||
lastMessageId: 9,
|
||||
lastMessageSummary: "hello",
|
||||
agentUnreadCount: 1,
|
||||
}),
|
||||
false
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { RealtimeConversationPatch } from "@/lib/im-realtime-state"
|
||||
|
||||
const listMembershipFields = new Set<keyof RealtimeConversationPatch>([
|
||||
"status",
|
||||
"currentAssigneeId",
|
||||
"currentTeamId",
|
||||
])
|
||||
|
||||
export function shouldReloadConversationListForRealtimePatch(
|
||||
patch: RealtimeConversationPatch | null | undefined
|
||||
) {
|
||||
if (!patch) {
|
||||
return false
|
||||
}
|
||||
return Object.keys(patch).some((key) =>
|
||||
listMembershipFields.has(key as keyof RealtimeConversationPatch)
|
||||
)
|
||||
}
|
||||
@@ -40,6 +40,7 @@ export type ImConversation = {
|
||||
priority: number
|
||||
currentAssigneeId: number
|
||||
currentAssigneeName?: string
|
||||
currentTeamId?: number
|
||||
lastMessageId: number
|
||||
lastMessageAt?: string
|
||||
lastActiveAt?: string
|
||||
|
||||
Reference in New Issue
Block a user