@anker-in/campaign-ui 0.4.6 → 0.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/cjs/components/LiveChatWidget/LiveChatWidget.js +1 -1
  2. package/dist/cjs/components/LiveChatWidget/LiveChatWidget.js.map +3 -3
  3. package/dist/cjs/components/LiveChatWidget/api/chat.d.ts +1 -1
  4. package/dist/cjs/components/LiveChatWidget/api/chat.js +2 -2
  5. package/dist/cjs/components/LiveChatWidget/api/chat.js.map +3 -3
  6. package/dist/cjs/components/LiveChatWidget/components/MessageContent/TextBlock.js +1 -1
  7. package/dist/cjs/components/LiveChatWidget/components/MessageContent/TextBlock.js.map +3 -3
  8. package/dist/cjs/components/LiveChatWidget/components/MessageList.js +2 -2
  9. package/dist/cjs/components/LiveChatWidget/components/MessageList.js.map +3 -3
  10. package/dist/cjs/components/LiveChatWidget/hooks/useChatAPI.js +1 -1
  11. package/dist/cjs/components/LiveChatWidget/hooks/useChatAPI.js.map +3 -3
  12. package/dist/cjs/components/LiveChatWidget/hooks/useChatState.d.ts +5 -0
  13. package/dist/cjs/components/LiveChatWidget/hooks/useChatState.js +1 -1
  14. package/dist/cjs/components/LiveChatWidget/hooks/useChatState.js.map +3 -3
  15. package/dist/cjs/components/LiveChatWidget/utils/fetcher.d.ts +3 -1
  16. package/dist/cjs/components/LiveChatWidget/utils/fetcher.js +1 -1
  17. package/dist/cjs/components/LiveChatWidget/utils/fetcher.js.map +3 -3
  18. package/dist/esm/components/LiveChatWidget/LiveChatWidget.js +1 -1
  19. package/dist/esm/components/LiveChatWidget/LiveChatWidget.js.map +3 -3
  20. package/dist/esm/components/LiveChatWidget/api/chat.d.ts +1 -1
  21. package/dist/esm/components/LiveChatWidget/api/chat.js +2 -2
  22. package/dist/esm/components/LiveChatWidget/api/chat.js.map +3 -3
  23. package/dist/esm/components/LiveChatWidget/components/MessageContent/TextBlock.js +1 -1
  24. package/dist/esm/components/LiveChatWidget/components/MessageContent/TextBlock.js.map +3 -3
  25. package/dist/esm/components/LiveChatWidget/components/MessageList.js +2 -2
  26. package/dist/esm/components/LiveChatWidget/components/MessageList.js.map +3 -3
  27. package/dist/esm/components/LiveChatWidget/hooks/useChatAPI.js +1 -1
  28. package/dist/esm/components/LiveChatWidget/hooks/useChatAPI.js.map +3 -3
  29. package/dist/esm/components/LiveChatWidget/hooks/useChatState.d.ts +5 -0
  30. package/dist/esm/components/LiveChatWidget/hooks/useChatState.js +1 -1
  31. package/dist/esm/components/LiveChatWidget/hooks/useChatState.js.map +3 -3
  32. package/dist/esm/components/LiveChatWidget/utils/fetcher.d.ts +3 -1
  33. package/dist/esm/components/LiveChatWidget/utils/fetcher.js +1 -1
  34. package/dist/esm/components/LiveChatWidget/utils/fetcher.js.map +3 -3
  35. package/package.json +1 -1
  36. package/src/components/LiveChatWidget/LiveChatWidget.tsx +6 -4
  37. package/src/components/LiveChatWidget/api/chat.ts +3 -2
  38. package/src/components/LiveChatWidget/components/MessageContent/TextBlock.tsx +7 -5
  39. package/src/components/LiveChatWidget/components/MessageList.tsx +20 -4
  40. package/src/components/LiveChatWidget/hooks/useChatAPI.ts +2 -3
  41. package/src/components/LiveChatWidget/hooks/useChatState.ts +27 -7
  42. package/src/components/LiveChatWidget/utils/fetcher.ts +15 -2
@@ -461,6 +461,12 @@ export interface UseChatStateReturn {
461
461
  */
462
462
  handleSSEEvent: (event: SSEEvent) => void
463
463
 
464
+ /**
465
+ * 开始新的 stream 会话,返回绑定了当前 streamId 的事件处理器。
466
+ * 调用此方法会使之前所有 stream 的事件处理器失效,防止并发 stream 污染共享 buffer。
467
+ */
468
+ startStream: () => (event: SSEEvent) => void
469
+
464
470
  /**
465
471
  * 保存会话 ID
466
472
  */
@@ -560,6 +566,9 @@ export function useChatState(options: UseChatStateOptions = {}): UseChatStateRet
560
566
  // 卡片缓存队列,用于存储需要延迟显示的卡片(在文本完成后显示)
561
567
  const pendingCardsRef = useRef<MessageContent[]>([])
562
568
 
569
+ // 当前活跃 stream 的唯一 ID,用于丢弃过期 stream 的事件,防止并发 stream 共享 buffer
570
+ const activeStreamIdRef = useRef<string | null>(null)
571
+
563
572
  /**
564
573
  * 打开聊天窗口
565
574
  */
@@ -641,21 +650,20 @@ export function useChatState(options: UseChatStateOptions = {}): UseChatStateRet
641
650
  * 根据事件类型进行不同的处理
642
651
  */
643
652
  const handleSSEEvent = useCallback(
644
- (event: SSEEvent) => {
653
+ (event: SSEEvent, streamId?: string) => {
645
654
  const { event: eventType, data } = event
646
655
 
656
+ // 并发 stream 防护:除 message_start 外的所有事件必须匹配当前活跃 streamId
657
+ if (eventType !== 'message_start' && streamId !== activeStreamIdRef.current) {
658
+ return
659
+ }
660
+
647
661
  switch (eventType) {
648
662
  case 'message_start': {
649
- // 开始接收新消息
650
663
  setIsStreaming(true)
651
664
 
652
- // 重置文本消息回调标记
653
665
  textMessageCallbackTriggeredRef.current = false
654
-
655
- // 重置文本缓冲区
656
666
  textBufferRef.current = ''
657
-
658
- // 重置卡片缓存队列
659
667
  pendingCardsRef.current = []
660
668
 
661
669
  // T039: 保存 sessionId 和 userId(如果后端返回)
@@ -1088,6 +1096,17 @@ export function useChatState(options: UseChatStateOptions = {}): UseChatStateRet
1088
1096
  )
1089
1097
 
1090
1098
 
1099
+ const startStream = useCallback((): ((event: SSEEvent) => void) => {
1100
+ const streamId = `stream-${Date.now()}-${Math.random().toString(36).slice(2)}`
1101
+ activeStreamIdRef.current = streamId
1102
+ textBufferRef.current = ''
1103
+ pendingCardsRef.current = []
1104
+ productMapRef.current.clear()
1105
+ rawProductMapRef.current.clear()
1106
+ currentMessageRef.current = null
1107
+ return (event: SSEEvent) => handleSSEEvent(event, streamId)
1108
+ }, [handleSSEEvent])
1109
+
1091
1110
  return {
1092
1111
  messages,
1093
1112
  isOpen,
@@ -1104,6 +1123,7 @@ export function useChatState(options: UseChatStateOptions = {}): UseChatStateRet
1104
1123
  setMessages,
1105
1124
  clearMessages,
1106
1125
  handleSSEEvent,
1126
+ startStream,
1107
1127
  saveSession,
1108
1128
  clearSession,
1109
1129
  }
@@ -60,6 +60,8 @@ export interface FetcherOptions {
60
60
  recaptchaSitekey?: string
61
61
  recaptchaAction?: string
62
62
  recaptchaHeaderKey?: string
63
+ /** 外部传入的 AbortSignal,用于提前中止请求 */
64
+ signal?: AbortSignal
63
65
  }
64
66
 
65
67
  /**
@@ -76,8 +78,8 @@ export const fetcher = async ({
76
78
  recaptchaSitekey,
77
79
  recaptchaAction = '',
78
80
  recaptchaHeaderKey = 'X-Recaptcha-Token',
81
+ signal: externalSignal,
79
82
  }: FetcherOptions): Promise<Response> => {
80
- // 获取 reCAPTCHA headers(如果需要)
81
83
  let recaptchaHeaders: Record<string, string> = {}
82
84
  if (needRecaptcha) {
83
85
  if (!recaptchaSitekey) {
@@ -87,14 +89,23 @@ export const fetcher = async ({
87
89
  }
88
90
  }
89
91
 
90
- // 准备请求体
91
92
  const bodyData = body ? JSON.stringify(body) : undefined
92
93
 
94
+ // 合并外部 signal(用于手动中止)和超时 signal
93
95
  const controller = new AbortController()
94
96
  let timeoutTimer: NodeJS.Timeout | undefined
95
97
  if (timeout) {
96
98
  timeoutTimer = setTimeout(() => controller.abort(), timeout)
97
99
  }
100
+ // 如果外部 signal 已中止或在请求期间中止,同步到内部 controller
101
+ const onExternalAbort = () => controller.abort()
102
+ if (externalSignal) {
103
+ if (externalSignal.aborted) {
104
+ controller.abort()
105
+ } else {
106
+ externalSignal.addEventListener('abort', onExternalAbort, { once: true })
107
+ }
108
+ }
98
109
 
99
110
  try {
100
111
  const response = await fetch(url, {
@@ -119,6 +130,8 @@ export const fetcher = async ({
119
130
  clearTimeout(timeoutTimer)
120
131
  }
121
132
  throw error
133
+ } finally {
134
+ externalSignal?.removeEventListener('abort', onExternalAbort)
122
135
  }
123
136
  }
124
137