@ai-group/chat-sdk 3.6.3 → 3.6.4

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 (34) hide show
  1. package/dist/cjs/components/FileGallery/index.d.ts +0 -30
  2. package/dist/cjs/components/FileGallery/index.js +287 -209
  3. package/dist/cjs/components/FileGallery/index.js.map +2 -2
  4. package/dist/cjs/components/FileGallery/styles.d.ts +5 -0
  5. package/dist/cjs/components/FileGallery/styles.js +106 -0
  6. package/dist/cjs/components/FileGallery/styles.js.map +2 -2
  7. package/dist/cjs/components/XAdkProvider/compound/Sender.js +3 -2
  8. package/dist/cjs/components/XAdkProvider/compound/Sender.js.map +2 -2
  9. package/dist/cjs/components/XAdkSender/index.js +1 -0
  10. package/dist/cjs/components/XAdkSender/index.js.map +2 -2
  11. package/dist/cjs/hooks/useADKChat.js +347 -156
  12. package/dist/cjs/hooks/useADKChat.js.map +3 -3
  13. package/dist/cjs/types/FileGallery.d.ts +41 -0
  14. package/dist/cjs/types/FileGallery.js.map +1 -1
  15. package/dist/cjs/types/XAdkProvider.d.ts +4 -0
  16. package/dist/cjs/types/XAdkProvider.js.map +1 -1
  17. package/dist/esm/components/FileGallery/index.d.ts +0 -30
  18. package/dist/esm/components/FileGallery/index.js +362 -364
  19. package/dist/esm/components/FileGallery/index.js.map +1 -1
  20. package/dist/esm/components/FileGallery/styles.d.ts +5 -0
  21. package/dist/esm/components/FileGallery/styles.js +10 -2
  22. package/dist/esm/components/FileGallery/styles.js.map +1 -1
  23. package/dist/esm/components/XAdkProvider/compound/Sender.js +5 -3
  24. package/dist/esm/components/XAdkProvider/compound/Sender.js.map +1 -1
  25. package/dist/esm/components/XAdkSender/index.js +1 -0
  26. package/dist/esm/components/XAdkSender/index.js.map +1 -1
  27. package/dist/esm/hooks/useADKChat.js +300 -129
  28. package/dist/esm/hooks/useADKChat.js.map +1 -1
  29. package/dist/esm/types/FileGallery.d.ts +41 -0
  30. package/dist/esm/types/FileGallery.js.map +1 -1
  31. package/dist/esm/types/XAdkProvider.d.ts +4 -0
  32. package/dist/esm/types/XAdkProvider.js.map +1 -1
  33. package/dist/umd/chat-sdk.min.js +1 -1
  34. package/package.json +1 -1
@@ -49,6 +49,158 @@ var combineTextParts = (parts) => {
49
49
  }
50
50
  return result;
51
51
  };
52
+ var DEFAULT_SESSION_EVENTS_POLLING_INTERVAL = 1e4;
53
+ var getMessageEventId = (messageItem) => {
54
+ var _a;
55
+ return messageItem.eventId || ((_a = messageItem.raw) == null ? void 0 : _a.id);
56
+ };
57
+ var mapSessionMessages = (sessionEvents) => {
58
+ const mapped = [];
59
+ sessionEvents.forEach((item) => {
60
+ if (!item || !item.content || !Array.isArray(item.content.parts))
61
+ return;
62
+ if (item.author === "user" && item.content.parts.some((part) => part.task))
63
+ return;
64
+ const role = (item.content.role || "").toLowerCase() === "user" ? "user" : "bot";
65
+ const parts = item.content.parts.filter((part) => {
66
+ if (!part)
67
+ return false;
68
+ return Boolean(
69
+ part.text || part.inlineData || part.functionCall || part.functionResponse || part.fileData || part.executableCode || part.codeExecutionResult || part.errorMessage
70
+ );
71
+ });
72
+ if (parts.length === 0)
73
+ return;
74
+ const textParts = parts.filter(
75
+ (part) => part.text || part.errorMessage || part.inlineData
76
+ );
77
+ const fileDataParts = parts.filter((part) => part.fileData);
78
+ const otherParts = parts.filter(
79
+ (part) => !part.text && !part.errorMessage && !part.inlineData && !part.fileData
80
+ );
81
+ if (textParts.length > 0 || fileDataParts.length > 0) {
82
+ const messageItem = {
83
+ id: (0, import_uuid.v4)(),
84
+ author: item.author,
85
+ invocationId: item.invocationId,
86
+ eventId: item.id,
87
+ timestamp: item.timestamp,
88
+ isLike: item.isLike,
89
+ role,
90
+ modelCode: item.modelCode,
91
+ usageMetadata: item.usageMetadata,
92
+ finishReason: item.finishReason,
93
+ raw: item
94
+ };
95
+ textParts.forEach((part) => {
96
+ if (part.inlineData) {
97
+ messageItem.inlineData = {
98
+ displayName: part.inlineData.displayName,
99
+ data: part.inlineData.data,
100
+ mimeType: part.inlineData.mimeType
101
+ };
102
+ }
103
+ if (part.text) {
104
+ messageItem.text = (messageItem.text ? `${messageItem.text}
105
+ ` : "") + part.text;
106
+ if (typeof part.thought !== "undefined")
107
+ messageItem.thought = part.thought;
108
+ }
109
+ if (part.errorMessage) {
110
+ messageItem.text = (messageItem.text ? `${messageItem.text}
111
+ ` : "") + part.errorMessage;
112
+ }
113
+ });
114
+ if (fileDataParts.length > 0) {
115
+ messageItem.fileData = fileDataParts.map((part) => ({
116
+ displayName: part.fileData.displayName || "",
117
+ fileUri: part.fileData.fileUri,
118
+ mimeType: part.fileData.mimeType
119
+ }));
120
+ }
121
+ mapped.push(messageItem);
122
+ }
123
+ otherParts.forEach((part) => {
124
+ const messageItem = {
125
+ id: (0, import_uuid.v4)(),
126
+ author: item.author,
127
+ invocationId: item.invocationId,
128
+ eventId: item.id,
129
+ timestamp: item.timestamp,
130
+ isLike: item.isLike,
131
+ role,
132
+ modelCode: item.modelCode,
133
+ usageMetadata: item.usageMetadata,
134
+ finishReason: item.finishReason,
135
+ raw: item
136
+ };
137
+ if (part.functionCall)
138
+ messageItem.functionCall = part.functionCall;
139
+ if (part.executableCode)
140
+ messageItem.executableCode = part.executableCode;
141
+ if (part.codeExecutionResult)
142
+ messageItem.codeExecutionResult = part.codeExecutionResult;
143
+ if (part.functionResponse) {
144
+ const functionCallMessage = mapped.find(
145
+ (currentMessage) => {
146
+ var _a, _b;
147
+ return ((_a = currentMessage.functionCall) == null ? void 0 : _a.id) === ((_b = part.functionResponse) == null ? void 0 : _b.id);
148
+ }
149
+ );
150
+ if (functionCallMessage) {
151
+ functionCallMessage.functionResponse = part.functionResponse;
152
+ }
153
+ return;
154
+ }
155
+ mapped.push(messageItem);
156
+ });
157
+ });
158
+ return mapped;
159
+ };
160
+ var insertMissingSessionEvents = (currentMessages, sessionEvents) => {
161
+ const renderedEventIds = new Set(
162
+ currentMessages.map(getMessageEventId).filter(Boolean)
163
+ );
164
+ const missingEvents = sessionEvents.filter((event) => {
165
+ var _a, _b;
166
+ const parts = (_a = event.content) == null ? void 0 : _a.parts;
167
+ return Boolean(event.id) && ((_b = event.content) == null ? void 0 : _b.role) === "model" && Array.isArray(parts) && parts.length > 0 && !renderedEventIds.has(event.id);
168
+ });
169
+ if (missingEvents.length === 0)
170
+ return currentMessages;
171
+ const missingMessages = mapSessionMessages(missingEvents);
172
+ if (missingMessages.length === 0)
173
+ return currentMessages;
174
+ const eventOrder = new Map(
175
+ sessionEvents.map((event, index) => [event.id, index])
176
+ );
177
+ const getMessageOrder = (messageItem) => {
178
+ var _a;
179
+ const eventId = getMessageEventId(messageItem);
180
+ if (eventId && eventOrder.has(eventId))
181
+ return eventOrder.get(eventId);
182
+ const timestamp = messageItem.timestamp ?? ((_a = messageItem.raw) == null ? void 0 : _a.timestamp);
183
+ if (timestamp !== void 0) {
184
+ const nextEventIndex = sessionEvents.findIndex(
185
+ (event) => (event.timestamp || 0) > timestamp
186
+ );
187
+ return nextEventIndex < 0 ? sessionEvents.length + 0.5 : nextEventIndex - 0.5;
188
+ }
189
+ return Number.POSITIVE_INFINITY;
190
+ };
191
+ const nextMessages = [...currentMessages];
192
+ missingMessages.forEach((messageItem) => {
193
+ const targetOrder = getMessageOrder(messageItem);
194
+ let insertIndex = nextMessages.findIndex((currentMessage) => {
195
+ const currentOrder = getMessageOrder(currentMessage);
196
+ return Number.isFinite(currentOrder) && currentOrder > targetOrder;
197
+ });
198
+ if (insertIndex < 0)
199
+ insertIndex = nextMessages.length;
200
+ nextMessages.splice(insertIndex, 0, messageItem);
201
+ });
202
+ return nextMessages;
203
+ };
52
204
  function useADKChat({
53
205
  url = window.location.origin,
54
206
  token,
@@ -62,13 +214,24 @@ function useADKChat({
62
214
  onSuccess,
63
215
  onStream
64
216
  }) {
65
- const [loading, setLoading] = (0, import_react.useState)(false);
217
+ var _a, _b;
218
+ const [loading, setLoadingState] = (0, import_react.useState)(false);
219
+ const loadingRef = (0, import_react.useRef)(false);
220
+ const setLoading = (0, import_react.useCallback)((nextLoading) => {
221
+ loadingRef.current = nextLoading;
222
+ setLoadingState(nextLoading);
223
+ }, []);
66
224
  const [sessionLoading, setSessionLoading] = (0, import_react.useState)(false);
67
225
  const sendingRef = (0, import_react.useRef)(false);
68
226
  const ctrl = (0, import_react.useRef)(null);
69
227
  const tokenRef = (0, import_react.useRef)(token);
70
228
  tokenRef.current = token;
71
229
  const { appNo, showFirstSession } = config || {};
230
+ const pollSessionEvents = ((_a = config == null ? void 0 : config.session) == null ? void 0 : _a.pollSessionEvents) ?? true;
231
+ const sessionEventsPollingInterval = Math.max(
232
+ ((_b = config == null ? void 0 : config.session) == null ? void 0 : _b.sessionEventsPollingInterval) ?? DEFAULT_SESSION_EVENTS_POLLING_INTERVAL,
233
+ 1e3
234
+ );
72
235
  const [appInfo, setAppInfo] = (0, import_react.useState)(null);
73
236
  const [initialized, setInitialized] = (0, import_react.useState)(false);
74
237
  const [sessionList, setSessionList] = (0, import_react.useState)([]);
@@ -81,6 +244,10 @@ function useADKChat({
81
244
  const [prologue, setPrologue] = (0, import_react.useState)("");
82
245
  const [suggestedQuestions, setSuggestedQuestions] = (0, import_react.useState)([]);
83
246
  const [messages, setMessages] = (0, import_react.useState)([]);
247
+ const pendingSessionEventsRef = (0, import_react.useRef)(null);
248
+ const sessionEventsFlushTimerRef = (0, import_react.useRef)(null);
249
+ const sessionEventsSyncingRef = (0, import_react.useRef)(false);
250
+ const sessionDetailLoadingRef = (0, import_react.useRef)(false);
84
251
  const stateDeltaRef = (0, import_react.useRef)(void 0);
85
252
  const backgroundRef = (0, import_react.useRef)(/* @__PURE__ */ new Map());
86
253
  const messagesRef = (0, import_react.useRef)(messages);
@@ -90,17 +257,17 @@ function useADKChat({
90
257
  const mergedMessages = (0, import_react.useMemo)(() => {
91
258
  const fnResMap = {};
92
259
  messages.forEach((msg) => {
93
- var _a;
260
+ var _a2;
94
261
  if (msg.functionResponse) {
95
- fnResMap[((_a = msg == null ? void 0 : msg.functionResponse) == null ? void 0 : _a.id) || ""] = msg.functionResponse;
262
+ fnResMap[((_a2 = msg == null ? void 0 : msg.functionResponse) == null ? void 0 : _a2.id) || ""] = msg.functionResponse;
96
263
  }
97
264
  });
98
265
  return messages.map((msg) => {
99
- var _a;
266
+ var _a2;
100
267
  if (msg.functionCall) {
101
268
  return {
102
269
  ...msg,
103
- functionResponse: fnResMap[((_a = msg == null ? void 0 : msg.functionCall) == null ? void 0 : _a.id) || ""]
270
+ functionResponse: fnResMap[((_a2 = msg == null ? void 0 : msg.functionCall) == null ? void 0 : _a2.id) || ""]
104
271
  };
105
272
  }
106
273
  return msg;
@@ -108,6 +275,66 @@ function useADKChat({
108
275
  }, [messages]);
109
276
  const textMsgRef = (0, import_react.useRef)(null);
110
277
  const eventDataRef = (0, import_react.useRef)(/* @__PURE__ */ new Map());
278
+ const cancelSessionEventsFlush = (0, import_react.useCallback)(() => {
279
+ if (sessionEventsFlushTimerRef.current === null)
280
+ return;
281
+ clearTimeout(sessionEventsFlushTimerRef.current);
282
+ sessionEventsFlushTimerRef.current = null;
283
+ }, []);
284
+ const flushPendingSessionEvents = (0, import_react.useCallback)(() => {
285
+ const pending = pendingSessionEventsRef.current;
286
+ const replyInProgress = loadingRef.current || sendingRef.current || textMsgRef.current !== null;
287
+ if (!pending || replyInProgress || pending.sessionId !== currentSessionIdRef.current)
288
+ return;
289
+ const previewMessages = insertMissingSessionEvents(
290
+ messagesRef.current,
291
+ pending.events
292
+ );
293
+ pendingSessionEventsRef.current = null;
294
+ if (previewMessages === messagesRef.current)
295
+ return;
296
+ sessionEventsSyncingRef.current = true;
297
+ setLoading(true);
298
+ sessionEventsFlushTimerRef.current = setTimeout(() => {
299
+ sessionEventsFlushTimerRef.current = null;
300
+ if (pending.sessionId === currentSessionIdRef.current) {
301
+ setMessages((prev) => {
302
+ const nextMessages = insertMissingSessionEvents(
303
+ prev,
304
+ pending.events
305
+ );
306
+ messagesRef.current = nextMessages;
307
+ return nextMessages;
308
+ });
309
+ }
310
+ sessionEventsSyncingRef.current = false;
311
+ setLoading(false);
312
+ }, 0);
313
+ }, [setLoading]);
314
+ const syncSessionEvents = (0, import_react.useCallback)(
315
+ (events, targetSessionId) => {
316
+ if (targetSessionId !== currentSessionIdRef.current)
317
+ return;
318
+ pendingSessionEventsRef.current = {
319
+ sessionId: targetSessionId,
320
+ events
321
+ };
322
+ flushPendingSessionEvents();
323
+ },
324
+ [flushPendingSessionEvents]
325
+ );
326
+ (0, import_react.useEffect)(() => {
327
+ pendingSessionEventsRef.current = null;
328
+ cancelSessionEventsFlush();
329
+ if (sessionEventsSyncingRef.current) {
330
+ sessionEventsSyncingRef.current = false;
331
+ setLoading(false);
332
+ }
333
+ }, [cancelSessionEventsFlush, currentSessionId, setLoading]);
334
+ (0, import_react.useEffect)(() => {
335
+ if (!loading)
336
+ flushPendingSessionEvents();
337
+ }, [flushPendingSessionEvents, loading]);
111
338
  const insertMessage = (0, import_react.useCallback)((msg) => {
112
339
  setMessages((prev) => {
113
340
  const lastMessage = prev[prev.length - 1];
@@ -167,9 +394,9 @@ function useADKChat({
167
394
  [withEventMeta]
168
395
  );
169
396
  const storeEvents = (0, import_react.useCallback)((part, event) => {
170
- var _a, _b;
397
+ var _a2, _b2;
171
398
  let title = "";
172
- if (part == null && ((_a = event.actions) == null ? void 0 : _a.artifactDelta)) {
399
+ if (part == null && ((_a2 = event.actions) == null ? void 0 : _a2.artifactDelta)) {
173
400
  title += "eventAction: artifact";
174
401
  } else if (part) {
175
402
  if (part.text) {
@@ -179,7 +406,7 @@ function useADKChat({
179
406
  } else if (part.functionResponse) {
180
407
  title += `functionResponse:${part.functionResponse.name}`;
181
408
  } else if (part.executableCode) {
182
- title += `executableCode:${(_b = part.executableCode.code) == null ? void 0 : _b.slice(0, 10)}`;
409
+ title += `executableCode:${(_b2 = part.executableCode.code) == null ? void 0 : _b2.slice(0, 10)}`;
183
410
  } else if (part.codeExecutionResult) {
184
411
  title += `codeExecutionResult:${part.codeExecutionResult.outcome}`;
185
412
  } else if (part.errorMessage) {
@@ -193,7 +420,7 @@ function useADKChat({
193
420
  }, []);
194
421
  const storeMessage = (0, import_react.useCallback)(
195
422
  (part, event, role) => {
196
- var _a, _b;
423
+ var _a2, _b2;
197
424
  const msg = {
198
425
  id: (0, import_uuid.v4)(),
199
426
  author: event.author,
@@ -217,7 +444,7 @@ function useADKChat({
217
444
  } else if (part.text) {
218
445
  msg.text = part.text;
219
446
  msg.thought = part.thought;
220
- if ((_b = (_a = event == null ? void 0 : event.groundingMetadata) == null ? void 0 : _a.searchEntryPoint) == null ? void 0 : _b.renderedContent) {
447
+ if ((_b2 = (_a2 = event == null ? void 0 : event.groundingMetadata) == null ? void 0 : _a2.searchEntryPoint) == null ? void 0 : _b2.renderedContent) {
221
448
  msg.renderedContent = event.groundingMetadata.searchEntryPoint.renderedContent;
222
449
  }
223
450
  } else if (part.fileData) {
@@ -240,8 +467,8 @@ function useADKChat({
240
467
  );
241
468
  const processPart = (0, import_react.useCallback)(
242
469
  (event, part, ignore = false) => {
243
- var _a, _b;
244
- const renderedContent = (_b = (_a = event.groundingMetadata) == null ? void 0 : _a.searchEntryPoint) == null ? void 0 : _b.renderedContent;
470
+ var _a2, _b2;
471
+ const renderedContent = (_b2 = (_a2 = event.groundingMetadata) == null ? void 0 : _a2.searchEntryPoint) == null ? void 0 : _b2.renderedContent;
245
472
  if (part.text) {
246
473
  if (ignore)
247
474
  return;
@@ -295,8 +522,8 @@ function useADKChat({
295
522
  );
296
523
  const processFollowupPart = (0, import_react.useCallback)(
297
524
  (event) => {
298
- var _a, _b;
299
- const part = ((_b = (_a = event == null ? void 0 : event.content) == null ? void 0 : _a.parts) == null ? void 0 : _b[0]) || null;
525
+ var _a2, _b2;
526
+ const part = ((_b2 = (_a2 = event == null ? void 0 : event.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b2[0]) || null;
300
527
  const text = (part == null ? void 0 : part.text) || "";
301
528
  storeEvents(part, event);
302
529
  if (text) {
@@ -480,7 +707,7 @@ function useADKChat({
480
707
  }
481
708
  },
482
709
  onclose: () => {
483
- var _a;
710
+ var _a2;
484
711
  const isBackground = currentSessionIdRef.current !== targetSessionId;
485
712
  if (isBackground) {
486
713
  const bg = backgroundRef.current.get(targetSessionId);
@@ -491,7 +718,7 @@ function useADKChat({
491
718
  } else {
492
719
  setLoading(false);
493
720
  if (textMsgRef.current) {
494
- onMessage == null ? void 0 : onMessage(((_a = textMsgRef.current) == null ? void 0 : _a.text) || "", textMsgRef.current);
721
+ onMessage == null ? void 0 : onMessage(((_a2 = textMsgRef.current) == null ? void 0 : _a2.text) || "", textMsgRef.current);
495
722
  }
496
723
  textMsgRef.current = null;
497
724
  }
@@ -527,7 +754,7 @@ function useADKChat({
527
754
  files = [],
528
755
  functionResponse
529
756
  }) => {
530
- if (loading || sendingRef.current)
757
+ if (loadingRef.current || sendingRef.current)
531
758
  return;
532
759
  if (!text.trim() && !(files == null ? void 0 : files.length) && !functionResponse)
533
760
  return;
@@ -547,11 +774,11 @@ function useADKChat({
547
774
  id: (0, import_uuid.v4)(),
548
775
  role: "user",
549
776
  fileData: files.map((file) => {
550
- var _a, _b, _c;
777
+ var _a2, _b2, _c;
551
778
  return {
552
779
  displayName: file.fileName || file.name,
553
- mimeType: file.mimeType || ((_a = file.response) == null ? void 0 : _a.mimeType),
554
- fileUri: file.tempUrl || ((_b = file.response) == null ? void 0 : _b.fileUrl) || ((_c = file.response) == null ? void 0 : _c.tempUrl)
780
+ mimeType: file.mimeType || ((_a2 = file.response) == null ? void 0 : _a2.mimeType),
781
+ fileUri: file.tempUrl || ((_b2 = file.response) == null ? void 0 : _b2.fileUrl) || ((_c = file.response) == null ? void 0 : _c.tempUrl)
555
782
  };
556
783
  })
557
784
  });
@@ -574,10 +801,10 @@ function useADKChat({
574
801
  parts: functionResponse ? [{ functionResponse }] : [{ text }]
575
802
  },
576
803
  files: files.map((file) => {
577
- var _a, _b, _c, _d, _e, _f;
804
+ var _a2, _b2, _c, _d, _e, _f;
578
805
  return {
579
- fileName: file.fileName || ((_a = file.response) == null ? void 0 : _a.fileName),
580
- fileId: file.fileId || ((_b = file.response) == null ? void 0 : _b.fileId),
806
+ fileName: file.fileName || ((_a2 = file.response) == null ? void 0 : _a2.fileName),
807
+ fileId: file.fileId || ((_b2 = file.response) == null ? void 0 : _b2.fileId),
581
808
  tempUrl: file.tempUrl || ((_c = file.response) == null ? void 0 : _c.fileUrl) || ((_d = file.response) == null ? void 0 : _d.tempUrl),
582
809
  type: file.type || ((_e = file.response) == null ? void 0 : _e.fileType),
583
810
  mimeType: file.mimeType || ((_f = file.response) == null ? void 0 : _f.mimeType)
@@ -593,7 +820,7 @@ function useADKChat({
593
820
  initAppConversations();
594
821
  };
595
822
  const reChat = async () => {
596
- if (loading || sendingRef.current)
823
+ if (loadingRef.current || sendingRef.current)
597
824
  return;
598
825
  if (messages.length === 0)
599
826
  return;
@@ -632,8 +859,8 @@ function useADKChat({
632
859
  }
633
860
  };
634
861
  const confirmFnCall = (fnCall, confirmed) => {
635
- var _a;
636
- const functionResponse = ((_a = strategies == null ? void 0 : strategies.createFunctionResponse) == null ? void 0 : _a.call(
862
+ var _a2;
863
+ const functionResponse = ((_a2 = strategies == null ? void 0 : strategies.createFunctionResponse) == null ? void 0 : _a2.call(
637
864
  strategies,
638
865
  fnCall,
639
866
  confirmed
@@ -645,7 +872,7 @@ function useADKChat({
645
872
  startChat({ functionResponse });
646
873
  };
647
874
  const suggestChat = (text) => {
648
- if (loading)
875
+ if (loadingRef.current)
649
876
  return;
650
877
  if (!text.trim())
651
878
  return;
@@ -653,126 +880,43 @@ function useADKChat({
653
880
  startChat({ text });
654
881
  };
655
882
  const stopChat = (0, import_react.useCallback)(() => {
656
- var _a;
657
- (_a = ctrl.current) == null ? void 0 : _a.abort();
883
+ var _a2;
884
+ pendingSessionEventsRef.current = null;
885
+ cancelSessionEventsFlush();
886
+ sessionEventsSyncingRef.current = false;
887
+ (_a2 = ctrl.current) == null ? void 0 : _a2.abort();
658
888
  ctrl.current = null;
659
889
  setLoading(false);
660
890
  textMsgRef.current = null;
661
- }, []);
891
+ }, [cancelSessionEventsFlush, setLoading]);
662
892
  const clearChat = () => {
663
- var _a, _b;
893
+ var _a2, _b2;
664
894
  const newSessionId = (0, import_uuid.v4)();
665
895
  setCurrentSessionId(newSessionId);
666
896
  stopChat();
667
- setPrologue(((_a = appInfo == null ? void 0 : appInfo.onboardingInfo) == null ? void 0 : _a.prologue) || "");
897
+ setPrologue(((_a2 = appInfo == null ? void 0 : appInfo.onboardingInfo) == null ? void 0 : _a2.prologue) || "");
668
898
  setMessages([]);
669
- setSuggestedQuestions(((_b = appInfo == null ? void 0 : appInfo.onboardingInfo) == null ? void 0 : _b.suggested_questions) || []);
670
- };
671
- const formatMessages = (messages2, isReplace = false) => {
672
- const mapped = [];
673
- messages2.forEach((item) => {
674
- if (!item || !item.content || !Array.isArray(item.content.parts))
675
- return;
676
- const role = (item.content.role || "").toLowerCase() === "user" ? "user" : "bot";
677
- const parts = Array.isArray(item.content.parts) ? item.content.parts.filter((p) => {
678
- if (!p)
679
- return false;
680
- return Boolean(
681
- p.text || p.inlineData || p.functionCall || p.functionResponse || p.fileData || p.executableCode || p.codeExecutionResult || p.errorMessage
682
- );
683
- }) : [];
684
- if (parts.length === 0)
685
- return;
686
- const textParts = parts.filter(
687
- (p) => p.text || p.errorMessage || p.inlineData
688
- );
689
- const fileDataParts = parts.filter((p) => p.fileData);
690
- const otherParts = parts.filter(
691
- (p) => !p.text && !p.errorMessage && !p.inlineData && !p.fileData
692
- );
693
- if (textParts.length > 0 || fileDataParts.length > 0) {
694
- const msg = {
695
- id: (0, import_uuid.v4)(),
696
- author: item.author,
697
- invocationId: item.invocationId,
698
- eventId: item.id,
699
- timestamp: item.timestamp,
700
- isLike: item.isLike,
701
- role,
702
- modelCode: item.modelCode,
703
- usageMetadata: item.usageMetadata,
704
- finishReason: item.finishReason,
705
- raw: item
706
- };
707
- for (const part of textParts) {
708
- if (part.inlineData) {
709
- msg.inlineData = {
710
- displayName: part.inlineData.displayName,
711
- data: part.inlineData.data,
712
- mimeType: part.inlineData.mimeType
713
- };
714
- }
715
- if (part.text) {
716
- msg.text = (msg.text ? msg.text + "\n" : "") + part.text;
717
- if (typeof part.thought !== "undefined")
718
- msg.thought = part.thought;
719
- }
720
- if (part.errorMessage) {
721
- msg.text = (msg.text ? msg.text + "\n" : "") + part.errorMessage;
722
- }
723
- }
724
- if (fileDataParts.length > 0) {
725
- msg.fileData = fileDataParts.map((part) => ({
726
- displayName: part.fileData.displayName || "",
727
- fileUri: part.fileData.fileUri,
728
- mimeType: part.fileData.mimeType
729
- }));
730
- }
731
- mapped.push(msg);
732
- }
733
- otherParts.forEach((part) => {
734
- const msg = {
735
- id: (0, import_uuid.v4)(),
736
- author: item.author,
737
- invocationId: item.invocationId,
738
- eventId: item.id,
739
- timestamp: item.timestamp,
740
- isLike: item.isLike,
741
- role,
742
- modelCode: item.modelCode,
743
- usageMetadata: item.usageMetadata,
744
- finishReason: item.finishReason,
745
- raw: item
746
- };
747
- if (part.functionCall)
748
- msg.functionCall = part.functionCall;
749
- if (part.executableCode)
750
- msg.executableCode = part.executableCode;
751
- if (part.codeExecutionResult)
752
- msg.codeExecutionResult = part.codeExecutionResult;
753
- if (part.functionResponse) {
754
- const functionCallMsg = mapped.find(
755
- (msg2) => {
756
- var _a, _b;
757
- return ((_a = msg2.functionCall) == null ? void 0 : _a.id) === ((_b = part.functionResponse) == null ? void 0 : _b.id);
758
- }
759
- );
760
- if (functionCallMsg) {
761
- functionCallMsg.functionResponse = part.functionResponse;
762
- }
763
- return;
764
- }
765
- mapped.push(msg);
766
- });
767
- });
768
- setMessages((prev) => isReplace ? mapped : [...prev, ...mapped]);
899
+ setSuggestedQuestions(((_b2 = appInfo == null ? void 0 : appInfo.onboardingInfo) == null ? void 0 : _b2.suggested_questions) || []);
769
900
  };
901
+ const formatMessages = (0, import_react.useCallback)(
902
+ (sessionEvents, isReplace = false) => {
903
+ const mapped = mapSessionMessages(sessionEvents);
904
+ setMessages((prev) => isReplace ? mapped : [...prev, ...mapped]);
905
+ },
906
+ []
907
+ );
770
908
  const setCurrentSession = async (sessionId) => {
771
- var _a, _b;
909
+ var _a2, _b2;
772
910
  if (sessionId) {
773
911
  if (sessionId === currentSessionId) {
774
912
  return;
775
913
  }
914
+ pendingSessionEventsRef.current = null;
915
+ cancelSessionEventsFlush();
916
+ if (sessionEventsSyncingRef.current) {
917
+ sessionEventsSyncingRef.current = false;
918
+ setLoading(false);
919
+ }
776
920
  const prevSessionId = currentSessionId;
777
921
  if (ctrl.current) {
778
922
  backgroundRef.current.set(prevSessionId, {
@@ -805,29 +949,34 @@ function useADKChat({
805
949
  }
806
950
  setInitialized(false);
807
951
  setCurrentSessionId(sessionId);
808
- const { data, result } = await (0, import_api.fetchSessionDetail)({
809
- url,
810
- appNo,
811
- sessionId,
812
- token: tokenRef.current
813
- });
814
- if ((result == null ? void 0 : result.code) === import_constants.API_SUCCESS_CODE) {
815
- setPrologue(((_a = appInfo == null ? void 0 : appInfo.onboardingInfo) == null ? void 0 : _a.prologue) || "");
816
- if (Array.isArray(data) && data.length > 0) {
817
- formatMessages(data, true);
818
- } else {
819
- setSuggestedQuestions(
820
- ((_b = appInfo == null ? void 0 : appInfo.onboardingInfo) == null ? void 0 : _b.suggested_questions) || []
821
- );
952
+ sessionDetailLoadingRef.current = true;
953
+ try {
954
+ const { data, result } = await (0, import_api.fetchSessionDetail)({
955
+ url,
956
+ appNo,
957
+ sessionId,
958
+ token: tokenRef.current
959
+ });
960
+ if ((result == null ? void 0 : result.code) === import_constants.API_SUCCESS_CODE) {
961
+ setPrologue(((_a2 = appInfo == null ? void 0 : appInfo.onboardingInfo) == null ? void 0 : _a2.prologue) || "");
962
+ if (Array.isArray(data) && data.length > 0) {
963
+ formatMessages(data, true);
964
+ } else {
965
+ setSuggestedQuestions(
966
+ ((_b2 = appInfo == null ? void 0 : appInfo.onboardingInfo) == null ? void 0 : _b2.suggested_questions) || []
967
+ );
968
+ }
822
969
  }
970
+ } finally {
971
+ sessionDetailLoadingRef.current = false;
972
+ setInitialized(true);
823
973
  }
824
- setInitialized(true);
825
974
  } else {
826
975
  setCurrentSessionId((0, import_uuid.v4)());
827
976
  }
828
977
  };
829
978
  const initAppConversations = async (fetchDetail = false) => {
830
- var _a, _b;
979
+ var _a2, _b2;
831
980
  try {
832
981
  setSessionLoading(true);
833
982
  const {
@@ -863,9 +1012,9 @@ function useADKChat({
863
1012
  if (showFirstSession) {
864
1013
  setCurrentSession(sessionId);
865
1014
  } else {
866
- fetchDetail && setPrologue(((_a = appInfo == null ? void 0 : appInfo.onboardingInfo) == null ? void 0 : _a.prologue) || "");
1015
+ fetchDetail && setPrologue(((_a2 = appInfo == null ? void 0 : appInfo.onboardingInfo) == null ? void 0 : _a2.prologue) || "");
867
1016
  fetchDetail && setSuggestedQuestions(
868
- ((_b = appInfo == null ? void 0 : appInfo.onboardingInfo) == null ? void 0 : _b.suggested_questions) || []
1017
+ ((_b2 = appInfo == null ? void 0 : appInfo.onboardingInfo) == null ? void 0 : _b2.suggested_questions) || []
869
1018
  );
870
1019
  }
871
1020
  if (!initialized) {
@@ -993,16 +1142,58 @@ function useADKChat({
993
1142
  }
994
1143
  }, [appInfo]);
995
1144
  (0, import_react.useEffect)(() => {
1145
+ if (!enabled || !initialized || !pollSessionEvents || !appNo || !currentSessionId)
1146
+ return;
1147
+ let disposed = false;
1148
+ let requesting = false;
1149
+ const pollingSessionId = currentSessionId;
1150
+ const poll = async () => {
1151
+ if (requesting || sessionDetailLoadingRef.current)
1152
+ return;
1153
+ requesting = true;
1154
+ try {
1155
+ const { data, result } = await (0, import_api.fetchSessionDetail)({
1156
+ url,
1157
+ appNo,
1158
+ sessionId: pollingSessionId,
1159
+ token: tokenRef.current
1160
+ });
1161
+ if (!disposed && (result == null ? void 0 : result.code) === import_constants.API_SUCCESS_CODE && Array.isArray(data)) {
1162
+ syncSessionEvents(data, pollingSessionId);
1163
+ }
1164
+ } catch {
1165
+ } finally {
1166
+ requesting = false;
1167
+ }
1168
+ };
1169
+ void poll();
1170
+ const timer = window.setInterval(poll, sessionEventsPollingInterval);
996
1171
  return () => {
997
- var _a;
998
- (_a = ctrl.current) == null ? void 0 : _a.abort();
1172
+ disposed = true;
1173
+ window.clearInterval(timer);
1174
+ };
1175
+ }, [
1176
+ appNo,
1177
+ currentSessionId,
1178
+ enabled,
1179
+ initialized,
1180
+ pollSessionEvents,
1181
+ sessionEventsPollingInterval,
1182
+ syncSessionEvents,
1183
+ url
1184
+ ]);
1185
+ (0, import_react.useEffect)(() => {
1186
+ return () => {
1187
+ var _a2;
1188
+ cancelSessionEventsFlush();
1189
+ (_a2 = ctrl.current) == null ? void 0 : _a2.abort();
999
1190
  backgroundRef.current.forEach((bg) => {
1000
- var _a2;
1001
- return (_a2 = bg.ctrl) == null ? void 0 : _a2.abort();
1191
+ var _a3;
1192
+ return (_a3 = bg.ctrl) == null ? void 0 : _a3.abort();
1002
1193
  });
1003
1194
  backgroundRef.current.clear();
1004
1195
  };
1005
- }, []);
1196
+ }, [cancelSessionEventsFlush]);
1006
1197
  return {
1007
1198
  appInfo,
1008
1199
  startChat,