@elqnt/chat 3.1.0 → 3.5.0
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.
- package/README.md +6 -0
- package/SKILL.md +531 -0
- package/TIER2_AUTH.md +258 -0
- package/dist/api/index.d.mts +11 -0
- package/dist/api/index.d.ts +11 -0
- package/dist/api/index.js +27 -9
- package/dist/api/index.js.map +1 -1
- package/dist/api/index.mjs +27 -10
- package/dist/api/index.mjs.map +1 -1
- package/dist/hooks/index.d.mts +75 -37
- package/dist/hooks/index.d.ts +75 -37
- package/dist/hooks/index.js +116 -16
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +115 -15
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/index.d.mts +2 -7
- package/dist/index.d.ts +2 -7
- package/dist/index.js +14 -1420
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +9 -1413
- package/dist/index.mjs.map +1 -1
- package/dist/models/index.d.mts +89 -4
- package/dist/models/index.d.ts +89 -4
- package/dist/models/index.js +12 -1
- package/dist/models/index.js.map +1 -1
- package/dist/models/index.mjs +8 -2
- package/dist/models/index.mjs.map +1 -1
- package/dist/transport/index.d.mts +2 -2
- package/dist/transport/index.d.ts +2 -2
- package/dist/transport/index.js +100 -11
- package/dist/transport/index.js.map +1 -1
- package/dist/transport/index.mjs +100 -12
- package/dist/transport/index.mjs.map +1 -1
- package/dist/{types-CQHtUQ6p.d.mts → types-CLtQA6Qq.d.mts} +16 -0
- package/dist/{types-7UNI1iYv.d.ts → types-CxibhkqW.d.ts} +16 -0
- package/package.json +8 -6
package/dist/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use client";
|
|
2
1
|
"use strict";
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -128,6 +127,7 @@ __export(index_exports, {
|
|
|
128
127
|
ChatEventTypeSyncMetadataResponse: () => ChatEventTypeSyncMetadataResponse,
|
|
129
128
|
ChatEventTypeSyncUserSession: () => ChatEventTypeSyncUserSession,
|
|
130
129
|
ChatEventTypeSyncUserSessionResponse: () => ChatEventTypeSyncUserSessionResponse,
|
|
130
|
+
ChatEventTypeTransportReconnected: () => ChatEventTypeTransportReconnected,
|
|
131
131
|
ChatEventTypeTyping: () => ChatEventTypeTyping,
|
|
132
132
|
ChatEventTypeUpdateRoom: () => ChatEventTypeUpdateRoom,
|
|
133
133
|
ChatEventTypeUserActivity: () => ChatEventTypeUserActivity,
|
|
@@ -166,6 +166,7 @@ __export(index_exports, {
|
|
|
166
166
|
ChatTypeGroup: () => ChatTypeGroup,
|
|
167
167
|
ChatTypePrivateRoom: () => ChatTypePrivateRoom,
|
|
168
168
|
ChatTypePublicRoom: () => ChatTypePublicRoom,
|
|
169
|
+
CloseAndDeleteChatSubject: () => CloseAndDeleteChatSubject,
|
|
169
170
|
CompleteChatByAgentSubject: () => CompleteChatByAgentSubject,
|
|
170
171
|
CreateAgentQueueSubject: () => CreateAgentQueueSubject,
|
|
171
172
|
DeleteAgentQueueSubject: () => DeleteAgentQueueSubject,
|
|
@@ -185,6 +186,8 @@ __export(index_exports, {
|
|
|
185
186
|
GetUserChatsSubject: () => GetUserChatsSubject,
|
|
186
187
|
GetWaitingForAgentChatCountSubject: () => GetWaitingForAgentChatCountSubject,
|
|
187
188
|
GetWaitingForAgentChatsSubject: () => GetWaitingForAgentChatsSubject,
|
|
189
|
+
ListIdleChatsSubject: () => ListIdleChatsSubject,
|
|
190
|
+
MarkChatClosedSubject: () => MarkChatClosedSubject,
|
|
188
191
|
MessageStatusDelivered: () => MessageStatusDelivered,
|
|
189
192
|
MessageStatusFailed: () => MessageStatusFailed,
|
|
190
193
|
MessageStatusRead: () => MessageStatusRead,
|
|
@@ -206,1420 +209,10 @@ __export(index_exports, {
|
|
|
206
209
|
UserStatusAway: () => UserStatusAway,
|
|
207
210
|
UserStatusBusy: () => UserStatusBusy,
|
|
208
211
|
UserStatusOffline: () => UserStatusOffline,
|
|
209
|
-
UserStatusOnline: () => UserStatusOnline
|
|
210
|
-
useApiAsync: () => import_hooks4.useApiAsync,
|
|
211
|
-
useChat: () => useChat,
|
|
212
|
-
useChatHistory: () => useChatHistory,
|
|
213
|
-
useChatMonitoring: () => useChatMonitoring,
|
|
214
|
-
useHumanAgentSessions: () => useHumanAgentSessions,
|
|
215
|
-
useMemory: () => useMemory,
|
|
216
|
-
useOptionsRef: () => useOptionsRef
|
|
212
|
+
UserStatusOnline: () => UserStatusOnline
|
|
217
213
|
});
|
|
218
214
|
module.exports = __toCommonJS(index_exports);
|
|
219
215
|
|
|
220
|
-
// hooks/use-chat.ts
|
|
221
|
-
var import_react = require("react");
|
|
222
|
-
|
|
223
|
-
// transport/types.ts
|
|
224
|
-
function createLogger(debug = false) {
|
|
225
|
-
return {
|
|
226
|
-
debug: debug ? console.log.bind(console, "[chat]") : () => {
|
|
227
|
-
},
|
|
228
|
-
info: console.info.bind(console, "[chat]"),
|
|
229
|
-
warn: console.warn.bind(console, "[chat]"),
|
|
230
|
-
error: console.error.bind(console, "[chat]")
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
var DEFAULT_RETRY_CONFIG = {
|
|
234
|
-
maxRetries: 10,
|
|
235
|
-
intervals: [1e3, 2e3, 5e3],
|
|
236
|
-
backoffMultiplier: 1.5,
|
|
237
|
-
maxBackoffTime: 3e4
|
|
238
|
-
};
|
|
239
|
-
function calculateRetryInterval(retryCount, config = DEFAULT_RETRY_CONFIG) {
|
|
240
|
-
const {
|
|
241
|
-
intervals = DEFAULT_RETRY_CONFIG.intervals,
|
|
242
|
-
backoffMultiplier = DEFAULT_RETRY_CONFIG.backoffMultiplier,
|
|
243
|
-
maxBackoffTime = DEFAULT_RETRY_CONFIG.maxBackoffTime
|
|
244
|
-
} = config;
|
|
245
|
-
if (retryCount < intervals.length) {
|
|
246
|
-
return intervals[retryCount];
|
|
247
|
-
}
|
|
248
|
-
const baseInterval = intervals[intervals.length - 1] || 5e3;
|
|
249
|
-
const backoffTime = baseInterval * Math.pow(backoffMultiplier, retryCount - intervals.length + 1);
|
|
250
|
-
return Math.min(backoffTime, maxBackoffTime);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
// transport/sse.ts
|
|
254
|
-
function createSSETransport(options = {}) {
|
|
255
|
-
const {
|
|
256
|
-
retryConfig = DEFAULT_RETRY_CONFIG,
|
|
257
|
-
debug = false,
|
|
258
|
-
logger = createLogger(debug)
|
|
259
|
-
} = options;
|
|
260
|
-
let eventSource;
|
|
261
|
-
let config;
|
|
262
|
-
let state = "disconnected";
|
|
263
|
-
let error;
|
|
264
|
-
let retryCount = 0;
|
|
265
|
-
let reconnectTimeout;
|
|
266
|
-
let intentionalDisconnect = false;
|
|
267
|
-
const metrics = {
|
|
268
|
-
latency: 0,
|
|
269
|
-
messagesSent: 0,
|
|
270
|
-
messagesReceived: 0,
|
|
271
|
-
messagesQueued: 0,
|
|
272
|
-
reconnectCount: 0,
|
|
273
|
-
transportType: "sse"
|
|
274
|
-
};
|
|
275
|
-
const globalHandlers = /* @__PURE__ */ new Set();
|
|
276
|
-
const typeHandlers = /* @__PURE__ */ new Map();
|
|
277
|
-
function emit(event) {
|
|
278
|
-
metrics.messagesReceived++;
|
|
279
|
-
metrics.lastMessageAt = Date.now();
|
|
280
|
-
globalHandlers.forEach((handler) => {
|
|
281
|
-
try {
|
|
282
|
-
handler(event);
|
|
283
|
-
} catch (err) {
|
|
284
|
-
logger.error("Error in message handler:", err);
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
const handlers = typeHandlers.get(event.type);
|
|
288
|
-
if (handlers) {
|
|
289
|
-
handlers.forEach((handler) => {
|
|
290
|
-
try {
|
|
291
|
-
handler(event);
|
|
292
|
-
} catch (err) {
|
|
293
|
-
logger.error(`Error in ${event.type} handler:`, err);
|
|
294
|
-
}
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
async function sendRest(endpoint, body) {
|
|
299
|
-
if (!config) {
|
|
300
|
-
throw new Error("Transport not connected");
|
|
301
|
-
}
|
|
302
|
-
const url = `${config.baseUrl}/${endpoint}`;
|
|
303
|
-
logger.debug(`POST ${endpoint}`, body);
|
|
304
|
-
const response = await fetch(url, {
|
|
305
|
-
method: "POST",
|
|
306
|
-
headers: { "Content-Type": "application/json" },
|
|
307
|
-
body: JSON.stringify(body)
|
|
308
|
-
});
|
|
309
|
-
if (!response.ok) {
|
|
310
|
-
const errorText = await response.text();
|
|
311
|
-
throw new Error(`API error: ${response.status} - ${errorText}`);
|
|
312
|
-
}
|
|
313
|
-
const json = await response.json();
|
|
314
|
-
if (json && typeof json === "object" && "data" in json) {
|
|
315
|
-
return json.data;
|
|
316
|
-
}
|
|
317
|
-
return json;
|
|
318
|
-
}
|
|
319
|
-
function handleMessage(event) {
|
|
320
|
-
if (!event.data || event.data === "") return;
|
|
321
|
-
try {
|
|
322
|
-
const data = JSON.parse(event.data);
|
|
323
|
-
logger.debug("Received:", data.type);
|
|
324
|
-
emit(data);
|
|
325
|
-
} catch (err) {
|
|
326
|
-
logger.error("Failed to parse SSE message:", err);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
function setupEventListeners(es) {
|
|
330
|
-
es.addEventListener("message", handleMessage);
|
|
331
|
-
const eventTypes = [
|
|
332
|
-
"reconnected",
|
|
333
|
-
"typing",
|
|
334
|
-
"stopped_typing",
|
|
335
|
-
"waiting",
|
|
336
|
-
"waiting_for_agent",
|
|
337
|
-
"human_agent_joined",
|
|
338
|
-
"human_agent_left",
|
|
339
|
-
"chat_ended",
|
|
340
|
-
"chat_updated",
|
|
341
|
-
"load_chat_response",
|
|
342
|
-
"new_chat_created",
|
|
343
|
-
"show_csat_survey",
|
|
344
|
-
"csat_response",
|
|
345
|
-
"user_suggested_actions",
|
|
346
|
-
"agent_execution_started",
|
|
347
|
-
"agent_execution_ended",
|
|
348
|
-
"agent_context_update",
|
|
349
|
-
"plan_pending_approval",
|
|
350
|
-
"step_started",
|
|
351
|
-
"step_completed",
|
|
352
|
-
"step_failed",
|
|
353
|
-
"plan_completed",
|
|
354
|
-
"skills_changed",
|
|
355
|
-
"summary_update"
|
|
356
|
-
];
|
|
357
|
-
eventTypes.forEach((type) => {
|
|
358
|
-
es.addEventListener(type, handleMessage);
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
function scheduleReconnect() {
|
|
362
|
-
if (intentionalDisconnect || !config) return;
|
|
363
|
-
const maxRetries = retryConfig.maxRetries ?? DEFAULT_RETRY_CONFIG.maxRetries;
|
|
364
|
-
if (retryCount >= maxRetries) {
|
|
365
|
-
logger.error(`Max retries (${maxRetries}) exceeded`);
|
|
366
|
-
error = {
|
|
367
|
-
code: "CONNECTION_FAILED",
|
|
368
|
-
message: `Max retries (${maxRetries}) exceeded`,
|
|
369
|
-
retryable: false,
|
|
370
|
-
timestamp: Date.now()
|
|
371
|
-
};
|
|
372
|
-
return;
|
|
373
|
-
}
|
|
374
|
-
const interval = calculateRetryInterval(retryCount, retryConfig);
|
|
375
|
-
retryCount++;
|
|
376
|
-
metrics.reconnectCount++;
|
|
377
|
-
logger.info(`Reconnecting in ${interval}ms (attempt ${retryCount})`);
|
|
378
|
-
state = "reconnecting";
|
|
379
|
-
reconnectTimeout = setTimeout(() => {
|
|
380
|
-
if (config) {
|
|
381
|
-
transport.connect(config).catch((err) => {
|
|
382
|
-
logger.error("Reconnect failed:", err);
|
|
383
|
-
});
|
|
384
|
-
}
|
|
385
|
-
}, interval);
|
|
386
|
-
}
|
|
387
|
-
const transport = {
|
|
388
|
-
async connect(cfg) {
|
|
389
|
-
config = cfg;
|
|
390
|
-
intentionalDisconnect = false;
|
|
391
|
-
if (eventSource) {
|
|
392
|
-
eventSource.close();
|
|
393
|
-
eventSource = void 0;
|
|
394
|
-
}
|
|
395
|
-
if (reconnectTimeout) {
|
|
396
|
-
clearTimeout(reconnectTimeout);
|
|
397
|
-
reconnectTimeout = void 0;
|
|
398
|
-
}
|
|
399
|
-
state = retryCount > 0 ? "reconnecting" : "connecting";
|
|
400
|
-
return new Promise((resolve, reject) => {
|
|
401
|
-
const connectionStart = Date.now();
|
|
402
|
-
const url = `${cfg.baseUrl}/stream?orgId=${cfg.orgId}&userId=${cfg.userId}&clientType=${cfg.clientType}${cfg.chatKey ? `&chatId=${cfg.chatKey}` : ""}`;
|
|
403
|
-
logger.debug("Connecting to:", url);
|
|
404
|
-
const es = new EventSource(url);
|
|
405
|
-
es.onopen = () => {
|
|
406
|
-
const connectionTime = Date.now() - connectionStart;
|
|
407
|
-
logger.info(`Connected in ${connectionTime}ms`);
|
|
408
|
-
state = "connected";
|
|
409
|
-
error = void 0;
|
|
410
|
-
retryCount = 0;
|
|
411
|
-
metrics.connectedAt = Date.now();
|
|
412
|
-
metrics.latency = connectionTime;
|
|
413
|
-
setupEventListeners(es);
|
|
414
|
-
resolve();
|
|
415
|
-
};
|
|
416
|
-
es.onerror = () => {
|
|
417
|
-
if (es.readyState === EventSource.CLOSED) {
|
|
418
|
-
const sseError = {
|
|
419
|
-
code: "CONNECTION_FAILED",
|
|
420
|
-
message: "SSE connection failed",
|
|
421
|
-
retryable: true,
|
|
422
|
-
timestamp: Date.now()
|
|
423
|
-
};
|
|
424
|
-
error = sseError;
|
|
425
|
-
metrics.lastError = sseError;
|
|
426
|
-
state = "disconnected";
|
|
427
|
-
if (!intentionalDisconnect) {
|
|
428
|
-
scheduleReconnect();
|
|
429
|
-
}
|
|
430
|
-
if (retryCount === 0) {
|
|
431
|
-
reject(sseError);
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
};
|
|
435
|
-
eventSource = es;
|
|
436
|
-
});
|
|
437
|
-
},
|
|
438
|
-
disconnect(intentional = true) {
|
|
439
|
-
logger.info("Disconnecting", { intentional });
|
|
440
|
-
intentionalDisconnect = intentional;
|
|
441
|
-
if (reconnectTimeout) {
|
|
442
|
-
clearTimeout(reconnectTimeout);
|
|
443
|
-
reconnectTimeout = void 0;
|
|
444
|
-
}
|
|
445
|
-
if (eventSource) {
|
|
446
|
-
eventSource.close();
|
|
447
|
-
eventSource = void 0;
|
|
448
|
-
}
|
|
449
|
-
state = "disconnected";
|
|
450
|
-
retryCount = 0;
|
|
451
|
-
},
|
|
452
|
-
async send(event) {
|
|
453
|
-
if (!config) {
|
|
454
|
-
throw new Error("Transport not connected");
|
|
455
|
-
}
|
|
456
|
-
switch (event.type) {
|
|
457
|
-
case "message":
|
|
458
|
-
await sendRest("send", {
|
|
459
|
-
orgId: event.orgId,
|
|
460
|
-
chatKey: event.chatKey,
|
|
461
|
-
userId: event.userId,
|
|
462
|
-
message: event.message,
|
|
463
|
-
...event.data ? { data: event.data } : {}
|
|
464
|
-
});
|
|
465
|
-
break;
|
|
466
|
-
case "typing":
|
|
467
|
-
await sendRest("typing", {
|
|
468
|
-
orgId: event.orgId,
|
|
469
|
-
chatKey: event.chatKey,
|
|
470
|
-
userId: event.userId,
|
|
471
|
-
typing: true
|
|
472
|
-
});
|
|
473
|
-
break;
|
|
474
|
-
case "stopped_typing":
|
|
475
|
-
await sendRest("typing", {
|
|
476
|
-
orgId: event.orgId,
|
|
477
|
-
chatKey: event.chatKey,
|
|
478
|
-
userId: event.userId,
|
|
479
|
-
typing: false
|
|
480
|
-
});
|
|
481
|
-
break;
|
|
482
|
-
case "load_chat":
|
|
483
|
-
await sendRest("load", {
|
|
484
|
-
orgId: event.orgId,
|
|
485
|
-
chatKey: event.chatKey,
|
|
486
|
-
userId: event.userId
|
|
487
|
-
});
|
|
488
|
-
break;
|
|
489
|
-
case "new_chat":
|
|
490
|
-
await sendRest("create", {
|
|
491
|
-
orgId: event.orgId,
|
|
492
|
-
userId: event.userId,
|
|
493
|
-
metadata: event.data
|
|
494
|
-
});
|
|
495
|
-
break;
|
|
496
|
-
case "end_chat":
|
|
497
|
-
await sendRest("end", {
|
|
498
|
-
orgId: event.orgId,
|
|
499
|
-
chatKey: event.chatKey,
|
|
500
|
-
userId: event.userId,
|
|
501
|
-
data: event.data
|
|
502
|
-
});
|
|
503
|
-
break;
|
|
504
|
-
default:
|
|
505
|
-
await sendRest("event", {
|
|
506
|
-
type: event.type,
|
|
507
|
-
orgId: event.orgId,
|
|
508
|
-
chatKey: event.chatKey,
|
|
509
|
-
userId: event.userId,
|
|
510
|
-
data: event.data
|
|
511
|
-
});
|
|
512
|
-
}
|
|
513
|
-
metrics.messagesSent++;
|
|
514
|
-
},
|
|
515
|
-
async sendMessage(message) {
|
|
516
|
-
if (!config) {
|
|
517
|
-
throw new Error("Transport not connected");
|
|
518
|
-
}
|
|
519
|
-
await sendRest("send", {
|
|
520
|
-
orgId: config.orgId,
|
|
521
|
-
chatKey: config.chatKey,
|
|
522
|
-
userId: config.userId,
|
|
523
|
-
message
|
|
524
|
-
});
|
|
525
|
-
metrics.messagesSent++;
|
|
526
|
-
},
|
|
527
|
-
async createChat(options2) {
|
|
528
|
-
if (!config) {
|
|
529
|
-
throw new Error("Transport not connected");
|
|
530
|
-
}
|
|
531
|
-
const response = await sendRest("create", {
|
|
532
|
-
orgId: options2.orgId,
|
|
533
|
-
userId: options2.userId,
|
|
534
|
-
metadata: options2.metadata
|
|
535
|
-
});
|
|
536
|
-
if (!response?.chatKey) {
|
|
537
|
-
throw new Error("Failed to create chat: no chatKey returned");
|
|
538
|
-
}
|
|
539
|
-
return { chatKey: response.chatKey };
|
|
540
|
-
},
|
|
541
|
-
async loadChatData(options2) {
|
|
542
|
-
if (!config) {
|
|
543
|
-
throw new Error("Transport not connected");
|
|
544
|
-
}
|
|
545
|
-
const response = await sendRest("load", {
|
|
546
|
-
orgId: options2.orgId,
|
|
547
|
-
chatKey: options2.chatKey,
|
|
548
|
-
userId: options2.userId
|
|
549
|
-
});
|
|
550
|
-
if (!response?.chat) {
|
|
551
|
-
throw new Error("Failed to load chat: no chat data returned");
|
|
552
|
-
}
|
|
553
|
-
return {
|
|
554
|
-
chat: response.chat,
|
|
555
|
-
agentId: response.agentId
|
|
556
|
-
};
|
|
557
|
-
},
|
|
558
|
-
onMessage(handler) {
|
|
559
|
-
globalHandlers.add(handler);
|
|
560
|
-
return () => globalHandlers.delete(handler);
|
|
561
|
-
},
|
|
562
|
-
on(eventType, handler) {
|
|
563
|
-
if (!typeHandlers.has(eventType)) {
|
|
564
|
-
typeHandlers.set(eventType, /* @__PURE__ */ new Set());
|
|
565
|
-
}
|
|
566
|
-
typeHandlers.get(eventType).add(handler);
|
|
567
|
-
return () => {
|
|
568
|
-
const handlers = typeHandlers.get(eventType);
|
|
569
|
-
if (handlers) {
|
|
570
|
-
handlers.delete(handler);
|
|
571
|
-
if (handlers.size === 0) {
|
|
572
|
-
typeHandlers.delete(eventType);
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
};
|
|
576
|
-
},
|
|
577
|
-
getState() {
|
|
578
|
-
return state;
|
|
579
|
-
},
|
|
580
|
-
getMetrics() {
|
|
581
|
-
return { ...metrics };
|
|
582
|
-
},
|
|
583
|
-
getError() {
|
|
584
|
-
return error;
|
|
585
|
-
},
|
|
586
|
-
clearError() {
|
|
587
|
-
error = void 0;
|
|
588
|
-
}
|
|
589
|
-
};
|
|
590
|
-
return transport;
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
// transport/sse-fetch.ts
|
|
594
|
-
function createFetchSSETransport(options = {}) {
|
|
595
|
-
const {
|
|
596
|
-
retryConfig = DEFAULT_RETRY_CONFIG,
|
|
597
|
-
debug = false,
|
|
598
|
-
logger = createLogger(debug),
|
|
599
|
-
customFetch = fetch
|
|
600
|
-
} = options;
|
|
601
|
-
let abortController;
|
|
602
|
-
let config;
|
|
603
|
-
let state = "disconnected";
|
|
604
|
-
let error;
|
|
605
|
-
let retryCount = 0;
|
|
606
|
-
let reconnectTimeout;
|
|
607
|
-
let intentionalDisconnect = false;
|
|
608
|
-
const metrics = {
|
|
609
|
-
latency: 0,
|
|
610
|
-
messagesSent: 0,
|
|
611
|
-
messagesReceived: 0,
|
|
612
|
-
messagesQueued: 0,
|
|
613
|
-
reconnectCount: 0,
|
|
614
|
-
transportType: "sse-fetch"
|
|
615
|
-
};
|
|
616
|
-
const globalHandlers = /* @__PURE__ */ new Set();
|
|
617
|
-
const typeHandlers = /* @__PURE__ */ new Map();
|
|
618
|
-
function emit(event) {
|
|
619
|
-
metrics.messagesReceived++;
|
|
620
|
-
metrics.lastMessageAt = Date.now();
|
|
621
|
-
globalHandlers.forEach((handler) => {
|
|
622
|
-
try {
|
|
623
|
-
handler(event);
|
|
624
|
-
} catch (err) {
|
|
625
|
-
logger.error("Error in message handler:", err);
|
|
626
|
-
}
|
|
627
|
-
});
|
|
628
|
-
const handlers = typeHandlers.get(event.type);
|
|
629
|
-
if (handlers) {
|
|
630
|
-
handlers.forEach((handler) => {
|
|
631
|
-
try {
|
|
632
|
-
handler(event);
|
|
633
|
-
} catch (err) {
|
|
634
|
-
logger.error(`Error in ${event.type} handler:`, err);
|
|
635
|
-
}
|
|
636
|
-
});
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
async function sendRest(endpoint, body) {
|
|
640
|
-
if (!config) {
|
|
641
|
-
throw new Error("Transport not connected");
|
|
642
|
-
}
|
|
643
|
-
const url = `${config.baseUrl}/${endpoint}`;
|
|
644
|
-
logger.debug(`POST ${endpoint}`, body);
|
|
645
|
-
const response = await customFetch(url, {
|
|
646
|
-
method: "POST",
|
|
647
|
-
headers: { "Content-Type": "application/json" },
|
|
648
|
-
body: JSON.stringify(body)
|
|
649
|
-
});
|
|
650
|
-
if (!response.ok) {
|
|
651
|
-
const errorText = await response.text();
|
|
652
|
-
throw new Error(`API error: ${response.status} - ${errorText}`);
|
|
653
|
-
}
|
|
654
|
-
const json = await response.json();
|
|
655
|
-
if (json && typeof json === "object" && "data" in json) {
|
|
656
|
-
return json.data;
|
|
657
|
-
}
|
|
658
|
-
return json;
|
|
659
|
-
}
|
|
660
|
-
function parseSSEChunk(chunk) {
|
|
661
|
-
const events = [];
|
|
662
|
-
const lines = chunk.split("\n");
|
|
663
|
-
let eventType = "message";
|
|
664
|
-
let data = "";
|
|
665
|
-
for (const line of lines) {
|
|
666
|
-
if (line.startsWith("event:")) {
|
|
667
|
-
eventType = line.slice(6).trim();
|
|
668
|
-
} else if (line.startsWith("data:")) {
|
|
669
|
-
data = line.slice(5).trim();
|
|
670
|
-
} else if (line === "" && data) {
|
|
671
|
-
try {
|
|
672
|
-
const parsed = JSON.parse(data);
|
|
673
|
-
events.push(parsed);
|
|
674
|
-
} catch {
|
|
675
|
-
logger.warn("Failed to parse SSE data:", data);
|
|
676
|
-
}
|
|
677
|
-
eventType = "message";
|
|
678
|
-
data = "";
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
return events;
|
|
682
|
-
}
|
|
683
|
-
async function startStream(cfg) {
|
|
684
|
-
const url = `${cfg.baseUrl}/stream?orgId=${cfg.orgId}&userId=${cfg.userId}&clientType=${cfg.clientType}${cfg.chatKey ? `&chatId=${cfg.chatKey}` : ""}`;
|
|
685
|
-
logger.debug("Connecting to:", url);
|
|
686
|
-
abortController = new AbortController();
|
|
687
|
-
const response = await customFetch(url, {
|
|
688
|
-
method: "GET",
|
|
689
|
-
headers: {
|
|
690
|
-
Accept: "text/event-stream",
|
|
691
|
-
"Cache-Control": "no-cache"
|
|
692
|
-
},
|
|
693
|
-
signal: abortController.signal
|
|
694
|
-
});
|
|
695
|
-
if (!response.ok) {
|
|
696
|
-
throw new Error(`Stream connection failed: ${response.status}`);
|
|
697
|
-
}
|
|
698
|
-
if (!response.body) {
|
|
699
|
-
throw new Error("Response body is null - ReadableStream not supported");
|
|
700
|
-
}
|
|
701
|
-
const reader = response.body.getReader();
|
|
702
|
-
const decoder = new TextDecoder();
|
|
703
|
-
let buffer = "";
|
|
704
|
-
const readStream = async () => {
|
|
705
|
-
try {
|
|
706
|
-
while (true) {
|
|
707
|
-
const { done, value } = await reader.read();
|
|
708
|
-
if (done) {
|
|
709
|
-
logger.info("Stream ended");
|
|
710
|
-
break;
|
|
711
|
-
}
|
|
712
|
-
buffer += decoder.decode(value, { stream: true });
|
|
713
|
-
const lastNewline = buffer.lastIndexOf("\n\n");
|
|
714
|
-
if (lastNewline !== -1) {
|
|
715
|
-
const complete = buffer.slice(0, lastNewline + 2);
|
|
716
|
-
buffer = buffer.slice(lastNewline + 2);
|
|
717
|
-
const events = parseSSEChunk(complete);
|
|
718
|
-
events.forEach(emit);
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
} catch (err) {
|
|
722
|
-
if (err.name === "AbortError") {
|
|
723
|
-
logger.debug("Stream aborted");
|
|
724
|
-
return;
|
|
725
|
-
}
|
|
726
|
-
logger.error("Stream error:", err);
|
|
727
|
-
throw err;
|
|
728
|
-
}
|
|
729
|
-
if (!intentionalDisconnect) {
|
|
730
|
-
state = "disconnected";
|
|
731
|
-
scheduleReconnect();
|
|
732
|
-
}
|
|
733
|
-
};
|
|
734
|
-
readStream().catch((err) => {
|
|
735
|
-
if (!intentionalDisconnect) {
|
|
736
|
-
error = {
|
|
737
|
-
code: "CONNECTION_FAILED",
|
|
738
|
-
message: err.message,
|
|
739
|
-
retryable: true,
|
|
740
|
-
timestamp: Date.now(),
|
|
741
|
-
originalError: err
|
|
742
|
-
};
|
|
743
|
-
metrics.lastError = error;
|
|
744
|
-
state = "disconnected";
|
|
745
|
-
scheduleReconnect();
|
|
746
|
-
}
|
|
747
|
-
});
|
|
748
|
-
}
|
|
749
|
-
function scheduleReconnect() {
|
|
750
|
-
if (intentionalDisconnect || !config) return;
|
|
751
|
-
const maxRetries = retryConfig.maxRetries ?? DEFAULT_RETRY_CONFIG.maxRetries;
|
|
752
|
-
if (retryCount >= maxRetries) {
|
|
753
|
-
logger.error(`Max retries (${maxRetries}) exceeded`);
|
|
754
|
-
error = {
|
|
755
|
-
code: "CONNECTION_FAILED",
|
|
756
|
-
message: `Max retries (${maxRetries}) exceeded`,
|
|
757
|
-
retryable: false,
|
|
758
|
-
timestamp: Date.now()
|
|
759
|
-
};
|
|
760
|
-
return;
|
|
761
|
-
}
|
|
762
|
-
const interval = calculateRetryInterval(retryCount, retryConfig);
|
|
763
|
-
retryCount++;
|
|
764
|
-
metrics.reconnectCount++;
|
|
765
|
-
logger.info(`Reconnecting in ${interval}ms (attempt ${retryCount})`);
|
|
766
|
-
state = "reconnecting";
|
|
767
|
-
reconnectTimeout = setTimeout(() => {
|
|
768
|
-
if (config) {
|
|
769
|
-
transport.connect(config).catch((err) => {
|
|
770
|
-
logger.error("Reconnect failed:", err);
|
|
771
|
-
});
|
|
772
|
-
}
|
|
773
|
-
}, interval);
|
|
774
|
-
}
|
|
775
|
-
const transport = {
|
|
776
|
-
async connect(cfg) {
|
|
777
|
-
config = cfg;
|
|
778
|
-
intentionalDisconnect = false;
|
|
779
|
-
if (abortController) {
|
|
780
|
-
abortController.abort();
|
|
781
|
-
abortController = void 0;
|
|
782
|
-
}
|
|
783
|
-
if (reconnectTimeout) {
|
|
784
|
-
clearTimeout(reconnectTimeout);
|
|
785
|
-
reconnectTimeout = void 0;
|
|
786
|
-
}
|
|
787
|
-
state = retryCount > 0 ? "reconnecting" : "connecting";
|
|
788
|
-
const connectionStart = Date.now();
|
|
789
|
-
try {
|
|
790
|
-
await startStream(cfg);
|
|
791
|
-
const connectionTime = Date.now() - connectionStart;
|
|
792
|
-
logger.info(`Connected in ${connectionTime}ms`);
|
|
793
|
-
state = "connected";
|
|
794
|
-
error = void 0;
|
|
795
|
-
retryCount = 0;
|
|
796
|
-
metrics.connectedAt = Date.now();
|
|
797
|
-
metrics.latency = connectionTime;
|
|
798
|
-
} catch (err) {
|
|
799
|
-
const connectError = {
|
|
800
|
-
code: "CONNECTION_FAILED",
|
|
801
|
-
message: err.message,
|
|
802
|
-
retryable: true,
|
|
803
|
-
timestamp: Date.now(),
|
|
804
|
-
originalError: err
|
|
805
|
-
};
|
|
806
|
-
error = connectError;
|
|
807
|
-
metrics.lastError = connectError;
|
|
808
|
-
state = "disconnected";
|
|
809
|
-
if (!intentionalDisconnect) {
|
|
810
|
-
scheduleReconnect();
|
|
811
|
-
}
|
|
812
|
-
throw connectError;
|
|
813
|
-
}
|
|
814
|
-
},
|
|
815
|
-
disconnect(intentional = true) {
|
|
816
|
-
logger.info("Disconnecting", { intentional });
|
|
817
|
-
intentionalDisconnect = intentional;
|
|
818
|
-
if (reconnectTimeout) {
|
|
819
|
-
clearTimeout(reconnectTimeout);
|
|
820
|
-
reconnectTimeout = void 0;
|
|
821
|
-
}
|
|
822
|
-
if (abortController) {
|
|
823
|
-
abortController.abort();
|
|
824
|
-
abortController = void 0;
|
|
825
|
-
}
|
|
826
|
-
state = "disconnected";
|
|
827
|
-
retryCount = 0;
|
|
828
|
-
},
|
|
829
|
-
async send(event) {
|
|
830
|
-
if (!config) {
|
|
831
|
-
throw new Error("Transport not connected");
|
|
832
|
-
}
|
|
833
|
-
switch (event.type) {
|
|
834
|
-
case "message":
|
|
835
|
-
await sendRest("send", {
|
|
836
|
-
orgId: event.orgId,
|
|
837
|
-
chatKey: event.chatKey,
|
|
838
|
-
userId: event.userId,
|
|
839
|
-
message: event.message
|
|
840
|
-
});
|
|
841
|
-
break;
|
|
842
|
-
case "typing":
|
|
843
|
-
await sendRest("typing", {
|
|
844
|
-
orgId: event.orgId,
|
|
845
|
-
chatKey: event.chatKey,
|
|
846
|
-
userId: event.userId,
|
|
847
|
-
typing: true
|
|
848
|
-
});
|
|
849
|
-
break;
|
|
850
|
-
case "stopped_typing":
|
|
851
|
-
await sendRest("typing", {
|
|
852
|
-
orgId: event.orgId,
|
|
853
|
-
chatKey: event.chatKey,
|
|
854
|
-
userId: event.userId,
|
|
855
|
-
typing: false
|
|
856
|
-
});
|
|
857
|
-
break;
|
|
858
|
-
case "load_chat":
|
|
859
|
-
await sendRest("load", {
|
|
860
|
-
orgId: event.orgId,
|
|
861
|
-
chatKey: event.chatKey,
|
|
862
|
-
userId: event.userId
|
|
863
|
-
});
|
|
864
|
-
break;
|
|
865
|
-
case "new_chat":
|
|
866
|
-
await sendRest("create", {
|
|
867
|
-
orgId: event.orgId,
|
|
868
|
-
userId: event.userId,
|
|
869
|
-
metadata: event.data
|
|
870
|
-
});
|
|
871
|
-
break;
|
|
872
|
-
case "end_chat":
|
|
873
|
-
await sendRest("end", {
|
|
874
|
-
orgId: event.orgId,
|
|
875
|
-
chatKey: event.chatKey,
|
|
876
|
-
userId: event.userId,
|
|
877
|
-
data: event.data
|
|
878
|
-
});
|
|
879
|
-
break;
|
|
880
|
-
default:
|
|
881
|
-
await sendRest("event", {
|
|
882
|
-
type: event.type,
|
|
883
|
-
orgId: event.orgId,
|
|
884
|
-
chatKey: event.chatKey,
|
|
885
|
-
userId: event.userId,
|
|
886
|
-
data: event.data
|
|
887
|
-
});
|
|
888
|
-
}
|
|
889
|
-
metrics.messagesSent++;
|
|
890
|
-
},
|
|
891
|
-
async sendMessage(message) {
|
|
892
|
-
if (!config) {
|
|
893
|
-
throw new Error("Transport not connected");
|
|
894
|
-
}
|
|
895
|
-
await sendRest("send", {
|
|
896
|
-
orgId: config.orgId,
|
|
897
|
-
chatKey: config.chatKey,
|
|
898
|
-
userId: config.userId,
|
|
899
|
-
message
|
|
900
|
-
});
|
|
901
|
-
metrics.messagesSent++;
|
|
902
|
-
},
|
|
903
|
-
async createChat(options2) {
|
|
904
|
-
if (!config) {
|
|
905
|
-
throw new Error("Transport not connected");
|
|
906
|
-
}
|
|
907
|
-
const response = await sendRest("create", {
|
|
908
|
-
orgId: options2.orgId,
|
|
909
|
-
userId: options2.userId,
|
|
910
|
-
metadata: options2.metadata
|
|
911
|
-
});
|
|
912
|
-
if (!response?.chatKey) {
|
|
913
|
-
throw new Error("Failed to create chat: no chatKey returned");
|
|
914
|
-
}
|
|
915
|
-
return { chatKey: response.chatKey };
|
|
916
|
-
},
|
|
917
|
-
async loadChatData(options2) {
|
|
918
|
-
if (!config) {
|
|
919
|
-
throw new Error("Transport not connected");
|
|
920
|
-
}
|
|
921
|
-
const response = await sendRest("load", {
|
|
922
|
-
orgId: options2.orgId,
|
|
923
|
-
chatKey: options2.chatKey,
|
|
924
|
-
userId: options2.userId
|
|
925
|
-
});
|
|
926
|
-
if (!response?.chat) {
|
|
927
|
-
throw new Error("Failed to load chat: no chat data returned");
|
|
928
|
-
}
|
|
929
|
-
return {
|
|
930
|
-
chat: response.chat,
|
|
931
|
-
agentId: response.agentId
|
|
932
|
-
};
|
|
933
|
-
},
|
|
934
|
-
onMessage(handler) {
|
|
935
|
-
globalHandlers.add(handler);
|
|
936
|
-
return () => globalHandlers.delete(handler);
|
|
937
|
-
},
|
|
938
|
-
on(eventType, handler) {
|
|
939
|
-
if (!typeHandlers.has(eventType)) {
|
|
940
|
-
typeHandlers.set(eventType, /* @__PURE__ */ new Set());
|
|
941
|
-
}
|
|
942
|
-
typeHandlers.get(eventType).add(handler);
|
|
943
|
-
return () => {
|
|
944
|
-
const handlers = typeHandlers.get(eventType);
|
|
945
|
-
if (handlers) {
|
|
946
|
-
handlers.delete(handler);
|
|
947
|
-
if (handlers.size === 0) {
|
|
948
|
-
typeHandlers.delete(eventType);
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
|
-
};
|
|
952
|
-
},
|
|
953
|
-
getState() {
|
|
954
|
-
return state;
|
|
955
|
-
},
|
|
956
|
-
getMetrics() {
|
|
957
|
-
return { ...metrics };
|
|
958
|
-
},
|
|
959
|
-
getError() {
|
|
960
|
-
return error;
|
|
961
|
-
},
|
|
962
|
-
clearError() {
|
|
963
|
-
error = void 0;
|
|
964
|
-
}
|
|
965
|
-
};
|
|
966
|
-
return transport;
|
|
967
|
-
}
|
|
968
|
-
|
|
969
|
-
// hooks/use-chat.ts
|
|
970
|
-
function getDefaultTransport(type, debug) {
|
|
971
|
-
if (type === "sse-fetch") {
|
|
972
|
-
return createFetchSSETransport({ debug });
|
|
973
|
-
}
|
|
974
|
-
if (type === "sse") {
|
|
975
|
-
return createSSETransport({ debug });
|
|
976
|
-
}
|
|
977
|
-
const isReactNative = typeof navigator !== "undefined" && navigator.product === "ReactNative";
|
|
978
|
-
if (isReactNative || typeof EventSource === "undefined") {
|
|
979
|
-
return createFetchSSETransport({ debug });
|
|
980
|
-
}
|
|
981
|
-
return createSSETransport({ debug });
|
|
982
|
-
}
|
|
983
|
-
function useChat(options) {
|
|
984
|
-
const {
|
|
985
|
-
baseUrl,
|
|
986
|
-
orgId,
|
|
987
|
-
userId,
|
|
988
|
-
clientType = "customer",
|
|
989
|
-
transport: transportOption,
|
|
990
|
-
onMessage,
|
|
991
|
-
onError,
|
|
992
|
-
onConnectionChange,
|
|
993
|
-
autoConnect = false,
|
|
994
|
-
retryConfig,
|
|
995
|
-
debug = false
|
|
996
|
-
} = options;
|
|
997
|
-
const [connectionState, setConnectionState] = (0, import_react.useState)("disconnected");
|
|
998
|
-
const [currentChat, setCurrentChat] = (0, import_react.useState)(null);
|
|
999
|
-
const [chatKey, setChatKey] = (0, import_react.useState)(null);
|
|
1000
|
-
const [messages, setMessages] = (0, import_react.useState)([]);
|
|
1001
|
-
const [error, setError] = (0, import_react.useState)(null);
|
|
1002
|
-
const [metrics, setMetrics] = (0, import_react.useState)({
|
|
1003
|
-
latency: 0,
|
|
1004
|
-
messagesSent: 0,
|
|
1005
|
-
messagesReceived: 0,
|
|
1006
|
-
messagesQueued: 0,
|
|
1007
|
-
reconnectCount: 0
|
|
1008
|
-
});
|
|
1009
|
-
const transportRef = (0, import_react.useRef)(null);
|
|
1010
|
-
const mountedRef = (0, import_react.useRef)(false);
|
|
1011
|
-
const onMessageRef = (0, import_react.useRef)(onMessage);
|
|
1012
|
-
const onErrorRef = (0, import_react.useRef)(onError);
|
|
1013
|
-
const typingTimeoutRef = (0, import_react.useRef)(null);
|
|
1014
|
-
(0, import_react.useEffect)(() => {
|
|
1015
|
-
onMessageRef.current = onMessage;
|
|
1016
|
-
onErrorRef.current = onError;
|
|
1017
|
-
}, [onMessage, onError]);
|
|
1018
|
-
(0, import_react.useEffect)(() => {
|
|
1019
|
-
if (typeof transportOption === "object") {
|
|
1020
|
-
transportRef.current = transportOption;
|
|
1021
|
-
} else {
|
|
1022
|
-
transportRef.current = getDefaultTransport(
|
|
1023
|
-
transportOption,
|
|
1024
|
-
debug
|
|
1025
|
-
);
|
|
1026
|
-
}
|
|
1027
|
-
}, [transportOption, debug]);
|
|
1028
|
-
const handleEvent = (0, import_react.useCallback)((event) => {
|
|
1029
|
-
if (!mountedRef.current) return;
|
|
1030
|
-
setMetrics((prev) => ({
|
|
1031
|
-
...prev,
|
|
1032
|
-
messagesReceived: prev.messagesReceived + 1,
|
|
1033
|
-
lastMessageAt: Date.now()
|
|
1034
|
-
}));
|
|
1035
|
-
switch (event.type) {
|
|
1036
|
-
case "new_chat_created":
|
|
1037
|
-
const newChatKey = event.data?.chatKey;
|
|
1038
|
-
if (newChatKey) {
|
|
1039
|
-
setChatKey(newChatKey);
|
|
1040
|
-
}
|
|
1041
|
-
break;
|
|
1042
|
-
case "load_chat_response":
|
|
1043
|
-
const chat = event.data?.chat;
|
|
1044
|
-
if (chat) {
|
|
1045
|
-
setCurrentChat(chat);
|
|
1046
|
-
setChatKey(chat.key);
|
|
1047
|
-
setMessages(chat.messages || []);
|
|
1048
|
-
}
|
|
1049
|
-
break;
|
|
1050
|
-
case "message":
|
|
1051
|
-
if (event.message) {
|
|
1052
|
-
setMessages((prev) => [...prev, event.message]);
|
|
1053
|
-
}
|
|
1054
|
-
break;
|
|
1055
|
-
case "chat_ended":
|
|
1056
|
-
setCurrentChat(null);
|
|
1057
|
-
setChatKey(null);
|
|
1058
|
-
break;
|
|
1059
|
-
case "error":
|
|
1060
|
-
const errorMsg = event.data?.message;
|
|
1061
|
-
if (errorMsg) {
|
|
1062
|
-
const transportError = {
|
|
1063
|
-
code: "NETWORK_ERROR",
|
|
1064
|
-
message: errorMsg,
|
|
1065
|
-
retryable: true,
|
|
1066
|
-
timestamp: Date.now()
|
|
1067
|
-
};
|
|
1068
|
-
setError(transportError);
|
|
1069
|
-
onErrorRef.current?.(transportError);
|
|
1070
|
-
}
|
|
1071
|
-
break;
|
|
1072
|
-
}
|
|
1073
|
-
onMessageRef.current?.(event);
|
|
1074
|
-
}, []);
|
|
1075
|
-
const connect = (0, import_react.useCallback)(async () => {
|
|
1076
|
-
const transport = transportRef.current;
|
|
1077
|
-
if (!transport) {
|
|
1078
|
-
throw new Error("Transport not initialized");
|
|
1079
|
-
}
|
|
1080
|
-
if (!orgId) {
|
|
1081
|
-
const err = {
|
|
1082
|
-
code: "CONNECTION_FAILED",
|
|
1083
|
-
message: "orgId is required",
|
|
1084
|
-
retryable: false,
|
|
1085
|
-
timestamp: Date.now()
|
|
1086
|
-
};
|
|
1087
|
-
setError(err);
|
|
1088
|
-
throw err;
|
|
1089
|
-
}
|
|
1090
|
-
setConnectionState("connecting");
|
|
1091
|
-
onConnectionChange?.("connecting");
|
|
1092
|
-
try {
|
|
1093
|
-
const unsubscribe = transport.onMessage(handleEvent);
|
|
1094
|
-
await transport.connect({
|
|
1095
|
-
baseUrl,
|
|
1096
|
-
orgId,
|
|
1097
|
-
userId,
|
|
1098
|
-
clientType,
|
|
1099
|
-
chatKey: chatKey || void 0,
|
|
1100
|
-
debug
|
|
1101
|
-
});
|
|
1102
|
-
setConnectionState("connected");
|
|
1103
|
-
setError(null);
|
|
1104
|
-
setMetrics(transport.getMetrics());
|
|
1105
|
-
onConnectionChange?.("connected");
|
|
1106
|
-
return;
|
|
1107
|
-
} catch (err) {
|
|
1108
|
-
const transportError = err;
|
|
1109
|
-
setConnectionState("disconnected");
|
|
1110
|
-
onConnectionChange?.("disconnected");
|
|
1111
|
-
setError(transportError);
|
|
1112
|
-
onErrorRef.current?.(transportError);
|
|
1113
|
-
throw err;
|
|
1114
|
-
}
|
|
1115
|
-
}, [baseUrl, orgId, userId, clientType, chatKey, debug, handleEvent, onConnectionChange]);
|
|
1116
|
-
const disconnect = (0, import_react.useCallback)(() => {
|
|
1117
|
-
const transport = transportRef.current;
|
|
1118
|
-
if (transport) {
|
|
1119
|
-
transport.disconnect(true);
|
|
1120
|
-
}
|
|
1121
|
-
setConnectionState("disconnected");
|
|
1122
|
-
onConnectionChange?.("disconnected");
|
|
1123
|
-
}, [onConnectionChange]);
|
|
1124
|
-
const startChat = (0, import_react.useCallback)(
|
|
1125
|
-
async (metadata) => {
|
|
1126
|
-
const transport = transportRef.current;
|
|
1127
|
-
if (!transport) {
|
|
1128
|
-
throw new Error("Transport not initialized");
|
|
1129
|
-
}
|
|
1130
|
-
const response = await transport.createChat({
|
|
1131
|
-
orgId,
|
|
1132
|
-
userId,
|
|
1133
|
-
metadata
|
|
1134
|
-
});
|
|
1135
|
-
setChatKey(response.chatKey);
|
|
1136
|
-
return response.chatKey;
|
|
1137
|
-
},
|
|
1138
|
-
[orgId, userId]
|
|
1139
|
-
);
|
|
1140
|
-
const loadChat2 = (0, import_react.useCallback)(
|
|
1141
|
-
async (key) => {
|
|
1142
|
-
const transport = transportRef.current;
|
|
1143
|
-
if (!transport) {
|
|
1144
|
-
throw new Error("Transport not initialized");
|
|
1145
|
-
}
|
|
1146
|
-
const response = await transport.loadChatData({
|
|
1147
|
-
orgId,
|
|
1148
|
-
chatKey: key,
|
|
1149
|
-
userId
|
|
1150
|
-
});
|
|
1151
|
-
setCurrentChat(response.chat);
|
|
1152
|
-
setChatKey(response.chat.key);
|
|
1153
|
-
setMessages(response.chat.messages || []);
|
|
1154
|
-
return response.chat;
|
|
1155
|
-
},
|
|
1156
|
-
[orgId, userId]
|
|
1157
|
-
);
|
|
1158
|
-
const sendMessage = (0, import_react.useCallback)(
|
|
1159
|
-
async (content, attachments) => {
|
|
1160
|
-
const transport = transportRef.current;
|
|
1161
|
-
if (!transport) {
|
|
1162
|
-
throw new Error("Transport not initialized");
|
|
1163
|
-
}
|
|
1164
|
-
if (!chatKey) {
|
|
1165
|
-
throw new Error("No active chat");
|
|
1166
|
-
}
|
|
1167
|
-
const message = {
|
|
1168
|
-
id: `msg_${Date.now()}_${Math.random().toString(36).slice(2)}`,
|
|
1169
|
-
role: "user",
|
|
1170
|
-
content,
|
|
1171
|
-
time: Date.now(),
|
|
1172
|
-
status: "sending",
|
|
1173
|
-
senderId: userId,
|
|
1174
|
-
createdAt: Date.now(),
|
|
1175
|
-
attachments
|
|
1176
|
-
};
|
|
1177
|
-
setMessages((prev) => [...prev, message]);
|
|
1178
|
-
await transport.send({
|
|
1179
|
-
type: "message",
|
|
1180
|
-
orgId,
|
|
1181
|
-
chatKey,
|
|
1182
|
-
userId,
|
|
1183
|
-
timestamp: Date.now(),
|
|
1184
|
-
message
|
|
1185
|
-
});
|
|
1186
|
-
setMetrics((prev) => ({
|
|
1187
|
-
...prev,
|
|
1188
|
-
messagesSent: prev.messagesSent + 1
|
|
1189
|
-
}));
|
|
1190
|
-
},
|
|
1191
|
-
[orgId, chatKey, userId]
|
|
1192
|
-
);
|
|
1193
|
-
const endChat2 = (0, import_react.useCallback)(
|
|
1194
|
-
async (reason) => {
|
|
1195
|
-
const transport = transportRef.current;
|
|
1196
|
-
if (!transport) {
|
|
1197
|
-
throw new Error("Transport not initialized");
|
|
1198
|
-
}
|
|
1199
|
-
if (!chatKey) {
|
|
1200
|
-
return;
|
|
1201
|
-
}
|
|
1202
|
-
await transport.send({
|
|
1203
|
-
type: "end_chat",
|
|
1204
|
-
orgId,
|
|
1205
|
-
chatKey,
|
|
1206
|
-
userId,
|
|
1207
|
-
timestamp: Date.now(),
|
|
1208
|
-
data: reason ? { reason } : void 0
|
|
1209
|
-
});
|
|
1210
|
-
setCurrentChat(null);
|
|
1211
|
-
setChatKey(null);
|
|
1212
|
-
},
|
|
1213
|
-
[orgId, chatKey, userId]
|
|
1214
|
-
);
|
|
1215
|
-
const sendEvent2 = (0, import_react.useCallback)(
|
|
1216
|
-
async (event) => {
|
|
1217
|
-
const transport = transportRef.current;
|
|
1218
|
-
if (!transport) {
|
|
1219
|
-
throw new Error("Transport not initialized");
|
|
1220
|
-
}
|
|
1221
|
-
await transport.send({
|
|
1222
|
-
...event,
|
|
1223
|
-
timestamp: Date.now()
|
|
1224
|
-
});
|
|
1225
|
-
},
|
|
1226
|
-
[]
|
|
1227
|
-
);
|
|
1228
|
-
const startTyping = (0, import_react.useCallback)(() => {
|
|
1229
|
-
const transport = transportRef.current;
|
|
1230
|
-
if (!transport || !chatKey) return;
|
|
1231
|
-
if (typingTimeoutRef.current) {
|
|
1232
|
-
clearTimeout(typingTimeoutRef.current);
|
|
1233
|
-
}
|
|
1234
|
-
transport.send({
|
|
1235
|
-
type: "typing",
|
|
1236
|
-
orgId,
|
|
1237
|
-
chatKey,
|
|
1238
|
-
userId,
|
|
1239
|
-
timestamp: Date.now()
|
|
1240
|
-
}).catch(() => {
|
|
1241
|
-
});
|
|
1242
|
-
typingTimeoutRef.current = setTimeout(() => {
|
|
1243
|
-
stopTyping();
|
|
1244
|
-
}, 3e3);
|
|
1245
|
-
}, [orgId, chatKey, userId]);
|
|
1246
|
-
const stopTyping = (0, import_react.useCallback)(() => {
|
|
1247
|
-
const transport = transportRef.current;
|
|
1248
|
-
if (!transport || !chatKey) return;
|
|
1249
|
-
if (typingTimeoutRef.current) {
|
|
1250
|
-
clearTimeout(typingTimeoutRef.current);
|
|
1251
|
-
typingTimeoutRef.current = null;
|
|
1252
|
-
}
|
|
1253
|
-
transport.send({
|
|
1254
|
-
type: "stopped_typing",
|
|
1255
|
-
orgId,
|
|
1256
|
-
chatKey,
|
|
1257
|
-
userId,
|
|
1258
|
-
timestamp: Date.now()
|
|
1259
|
-
}).catch(() => {
|
|
1260
|
-
});
|
|
1261
|
-
}, [orgId, chatKey, userId]);
|
|
1262
|
-
const on = (0, import_react.useCallback)(
|
|
1263
|
-
(eventType, handler) => {
|
|
1264
|
-
const transport = transportRef.current;
|
|
1265
|
-
if (!transport) {
|
|
1266
|
-
return () => {
|
|
1267
|
-
};
|
|
1268
|
-
}
|
|
1269
|
-
return transport.on(eventType, handler);
|
|
1270
|
-
},
|
|
1271
|
-
[]
|
|
1272
|
-
);
|
|
1273
|
-
const clearError = (0, import_react.useCallback)(() => {
|
|
1274
|
-
setError(null);
|
|
1275
|
-
transportRef.current?.clearError();
|
|
1276
|
-
}, []);
|
|
1277
|
-
(0, import_react.useEffect)(() => {
|
|
1278
|
-
mountedRef.current = true;
|
|
1279
|
-
if (autoConnect) {
|
|
1280
|
-
connect().catch(() => {
|
|
1281
|
-
});
|
|
1282
|
-
}
|
|
1283
|
-
return () => {
|
|
1284
|
-
mountedRef.current = false;
|
|
1285
|
-
if (typingTimeoutRef.current) {
|
|
1286
|
-
clearTimeout(typingTimeoutRef.current);
|
|
1287
|
-
}
|
|
1288
|
-
disconnect();
|
|
1289
|
-
};
|
|
1290
|
-
}, []);
|
|
1291
|
-
const isConnected = connectionState === "connected";
|
|
1292
|
-
return {
|
|
1293
|
-
// Connection
|
|
1294
|
-
connect,
|
|
1295
|
-
disconnect,
|
|
1296
|
-
connectionState,
|
|
1297
|
-
isConnected,
|
|
1298
|
-
// Chat operations
|
|
1299
|
-
startChat,
|
|
1300
|
-
loadChat: loadChat2,
|
|
1301
|
-
sendMessage,
|
|
1302
|
-
sendEvent: sendEvent2,
|
|
1303
|
-
endChat: endChat2,
|
|
1304
|
-
// Typing
|
|
1305
|
-
startTyping,
|
|
1306
|
-
stopTyping,
|
|
1307
|
-
// State
|
|
1308
|
-
currentChat,
|
|
1309
|
-
chatKey,
|
|
1310
|
-
messages,
|
|
1311
|
-
error,
|
|
1312
|
-
metrics,
|
|
1313
|
-
// Events
|
|
1314
|
-
on,
|
|
1315
|
-
clearError
|
|
1316
|
-
};
|
|
1317
|
-
}
|
|
1318
|
-
|
|
1319
|
-
// hooks/use-chat-history.ts
|
|
1320
|
-
var import_react3 = require("react");
|
|
1321
|
-
|
|
1322
|
-
// api/index.ts
|
|
1323
|
-
var import_browser2 = require("@elqnt/api-client/browser");
|
|
1324
|
-
|
|
1325
|
-
// api/memory.ts
|
|
1326
|
-
var import_browser = require("@elqnt/api-client/browser");
|
|
1327
|
-
var patchProfileApi = (patch, o) => (0, import_browser.browserApiRequest)("/api/v1/memory/profile", { method: "PATCH", body: patch, ...o });
|
|
1328
|
-
var replaceProfileApi = (p, o) => (0, import_browser.browserApiRequest)("/api/v1/memory/profile", { method: "PUT", body: p, ...o });
|
|
1329
|
-
var clearProfileApi = (o) => (0, import_browser.browserApiRequest)("/api/v1/memory/profile", { method: "DELETE", ...o });
|
|
1330
|
-
var deleteContactApi = (name, o) => (0, import_browser.browserApiRequest)(`/api/v1/memory/profile/contacts/${encodeURIComponent(name)}`, { method: "DELETE", ...o });
|
|
1331
|
-
var deleteNoteApi = (index, o) => (0, import_browser.browserApiRequest)(`/api/v1/memory/profile/notes/${index}`, { method: "DELETE", ...o });
|
|
1332
|
-
var clearSummaryApi = (chatKey, o) => (0, import_browser.browserApiRequest)(`/api/v1/chats/${chatKey}/summary`, { method: "DELETE", ...o });
|
|
1333
|
-
var regenerateSummaryApi = (chatKey, o) => (0, import_browser.browserApiRequest)(`/api/v1/chats/${chatKey}/summary/regenerate`, { method: "POST", ...o });
|
|
1334
|
-
|
|
1335
|
-
// api/index.ts
|
|
1336
|
-
async function getChatHistoryApi(options) {
|
|
1337
|
-
return (0, import_browser2.browserApiRequest)("/api/v1/chats", {
|
|
1338
|
-
method: "POST",
|
|
1339
|
-
body: {
|
|
1340
|
-
limit: options.limit || 15,
|
|
1341
|
-
offset: options.offset || 0,
|
|
1342
|
-
...options.skipCache ? { skipCache: true } : {}
|
|
1343
|
-
},
|
|
1344
|
-
...options
|
|
1345
|
-
});
|
|
1346
|
-
}
|
|
1347
|
-
async function getChatApi(chatKey, options) {
|
|
1348
|
-
return (0, import_browser2.browserApiRequest)(`/api/v1/chats/${chatKey}`, {
|
|
1349
|
-
method: "GET",
|
|
1350
|
-
...options
|
|
1351
|
-
});
|
|
1352
|
-
}
|
|
1353
|
-
async function updateChatApi(chatKey, updates, options) {
|
|
1354
|
-
return (0, import_browser2.browserApiRequest)(`/api/v1/chats/${chatKey}`, {
|
|
1355
|
-
method: "PATCH",
|
|
1356
|
-
body: updates,
|
|
1357
|
-
...options
|
|
1358
|
-
});
|
|
1359
|
-
}
|
|
1360
|
-
async function deleteChatApi(chatKey, options) {
|
|
1361
|
-
return (0, import_browser2.browserApiRequest)(`/api/v1/chats/${chatKey}`, {
|
|
1362
|
-
method: "DELETE",
|
|
1363
|
-
...options
|
|
1364
|
-
});
|
|
1365
|
-
}
|
|
1366
|
-
async function getActiveChatsCountApi(options) {
|
|
1367
|
-
return (0, import_browser2.browserApiRequest)("/api/v1/chats/active/count", {
|
|
1368
|
-
method: "GET",
|
|
1369
|
-
...options
|
|
1370
|
-
});
|
|
1371
|
-
}
|
|
1372
|
-
async function getActiveChatsApi(options) {
|
|
1373
|
-
const params = new URLSearchParams();
|
|
1374
|
-
if (options.pastHours) params.set("pastHours", String(options.pastHours));
|
|
1375
|
-
const queryString = params.toString();
|
|
1376
|
-
return (0, import_browser2.browserApiRequest)(`/api/v1/chats/active${queryString ? `?${queryString}` : ""}`, {
|
|
1377
|
-
method: "GET",
|
|
1378
|
-
...options
|
|
1379
|
-
});
|
|
1380
|
-
}
|
|
1381
|
-
async function getWaitingChatsCountApi(options) {
|
|
1382
|
-
return (0, import_browser2.browserApiRequest)("/api/v1/chats/waiting/count", {
|
|
1383
|
-
method: "GET",
|
|
1384
|
-
...options
|
|
1385
|
-
});
|
|
1386
|
-
}
|
|
1387
|
-
async function getChatsByUserApi(userEmail, options) {
|
|
1388
|
-
return (0, import_browser2.browserApiRequest)(`/api/v1/chats/user/${encodeURIComponent(userEmail)}`, {
|
|
1389
|
-
method: "GET",
|
|
1390
|
-
...options
|
|
1391
|
-
});
|
|
1392
|
-
}
|
|
1393
|
-
async function listQueuesApi(options) {
|
|
1394
|
-
return (0, import_browser2.browserApiRequest)("/api/v1/queues", {
|
|
1395
|
-
method: "GET",
|
|
1396
|
-
...options
|
|
1397
|
-
});
|
|
1398
|
-
}
|
|
1399
|
-
async function getOnlineSessionsApi(options) {
|
|
1400
|
-
return (0, import_browser2.browserApiRequest)("/api/v1/agents/sessions/online", {
|
|
1401
|
-
method: "GET",
|
|
1402
|
-
...options
|
|
1403
|
-
});
|
|
1404
|
-
}
|
|
1405
|
-
async function getAgentSessionApi(agentId, options) {
|
|
1406
|
-
return (0, import_browser2.browserApiRequest)(`/api/v1/agents/sessions/${agentId}`, {
|
|
1407
|
-
method: "GET",
|
|
1408
|
-
...options
|
|
1409
|
-
});
|
|
1410
|
-
}
|
|
1411
|
-
|
|
1412
|
-
// hooks/use-chat-history.ts
|
|
1413
|
-
var import_hooks = require("@elqnt/api-client/hooks");
|
|
1414
|
-
|
|
1415
|
-
// hooks/use-options-ref.ts
|
|
1416
|
-
var import_react2 = require("react");
|
|
1417
|
-
function useOptionsRef(options) {
|
|
1418
|
-
const optionsRef = (0, import_react2.useRef)(options);
|
|
1419
|
-
(0, import_react2.useEffect)(() => {
|
|
1420
|
-
optionsRef.current = options;
|
|
1421
|
-
}, [options]);
|
|
1422
|
-
return optionsRef;
|
|
1423
|
-
}
|
|
1424
|
-
|
|
1425
|
-
// hooks/use-chat-history.ts
|
|
1426
|
-
function useChatHistory(options) {
|
|
1427
|
-
const optionsRef = useOptionsRef(options);
|
|
1428
|
-
const { execute: getChatHistory, loading: listLoading, error: listError } = (0, import_hooks.useApiAsync)(
|
|
1429
|
-
(params) => getChatHistoryApi({ ...optionsRef.current, ...params }),
|
|
1430
|
-
(data) => ({
|
|
1431
|
-
chats: data.chats,
|
|
1432
|
-
total: data.total,
|
|
1433
|
-
hasMore: data.hasMore
|
|
1434
|
-
}),
|
|
1435
|
-
{ chats: [], total: 0, hasMore: false }
|
|
1436
|
-
);
|
|
1437
|
-
const { execute: getChat, loading: getLoading, error: getError } = (0, import_hooks.useApiAsync)(
|
|
1438
|
-
(chatKey) => getChatApi(chatKey, optionsRef.current),
|
|
1439
|
-
(data) => data.chat || null,
|
|
1440
|
-
null
|
|
1441
|
-
);
|
|
1442
|
-
const { execute: updateChat, loading: updateLoading, error: updateError } = (0, import_hooks.useApiAsync)(
|
|
1443
|
-
(chatKey, updates) => updateChatApi(chatKey, updates, optionsRef.current),
|
|
1444
|
-
(data) => !!data.chatKey,
|
|
1445
|
-
false
|
|
1446
|
-
);
|
|
1447
|
-
const { execute: deleteChat, loading: deleteLoading, error: deleteError } = (0, import_hooks.useApiAsync)(
|
|
1448
|
-
(chatKey) => deleteChatApi(chatKey, optionsRef.current),
|
|
1449
|
-
(data) => data.success,
|
|
1450
|
-
false
|
|
1451
|
-
);
|
|
1452
|
-
const { execute: getChatsByUser, loading: userChatsLoading, error: userChatsError } = (0, import_hooks.useApiAsync)(
|
|
1453
|
-
(userEmail) => getChatsByUserApi(userEmail, optionsRef.current),
|
|
1454
|
-
(data) => data.chats,
|
|
1455
|
-
[]
|
|
1456
|
-
);
|
|
1457
|
-
const loading = listLoading || getLoading || updateLoading || deleteLoading || userChatsLoading;
|
|
1458
|
-
const error = listError || getError || updateError || deleteError || userChatsError;
|
|
1459
|
-
return (0, import_react3.useMemo)(
|
|
1460
|
-
() => ({
|
|
1461
|
-
loading,
|
|
1462
|
-
error,
|
|
1463
|
-
getChatHistory,
|
|
1464
|
-
getChat,
|
|
1465
|
-
updateChat,
|
|
1466
|
-
deleteChat,
|
|
1467
|
-
getChatsByUser
|
|
1468
|
-
}),
|
|
1469
|
-
[loading, error, getChatHistory, getChat, updateChat, deleteChat, getChatsByUser]
|
|
1470
|
-
);
|
|
1471
|
-
}
|
|
1472
|
-
|
|
1473
|
-
// hooks/use-chat-monitoring.ts
|
|
1474
|
-
var import_react4 = require("react");
|
|
1475
|
-
var import_hooks2 = require("@elqnt/api-client/hooks");
|
|
1476
|
-
function useChatMonitoring(options) {
|
|
1477
|
-
const optionsRef = useOptionsRef(options);
|
|
1478
|
-
const { execute: getActiveChats, loading: activeLoading, error: activeError } = (0, import_hooks2.useApiAsync)(
|
|
1479
|
-
(pastHours) => getActiveChatsApi({ ...optionsRef.current, pastHours }),
|
|
1480
|
-
(data) => data.chats,
|
|
1481
|
-
[]
|
|
1482
|
-
);
|
|
1483
|
-
const { execute: getActiveChatsCount, loading: activeCountLoading, error: activeCountError } = (0, import_hooks2.useApiAsync)(
|
|
1484
|
-
() => getActiveChatsCountApi(optionsRef.current),
|
|
1485
|
-
(data) => data.count,
|
|
1486
|
-
0
|
|
1487
|
-
);
|
|
1488
|
-
const { execute: getWaitingChatsCount, loading: waitingCountLoading, error: waitingCountError } = (0, import_hooks2.useApiAsync)(
|
|
1489
|
-
() => getWaitingChatsCountApi(optionsRef.current),
|
|
1490
|
-
(data) => data.count,
|
|
1491
|
-
0
|
|
1492
|
-
);
|
|
1493
|
-
const { execute: listQueues, loading: queuesLoading, error: queuesError } = (0, import_hooks2.useApiAsync)(
|
|
1494
|
-
() => listQueuesApi(optionsRef.current),
|
|
1495
|
-
(data) => data.queues,
|
|
1496
|
-
[]
|
|
1497
|
-
);
|
|
1498
|
-
const loading = activeLoading || activeCountLoading || waitingCountLoading || queuesLoading;
|
|
1499
|
-
const error = activeError || activeCountError || waitingCountError || queuesError;
|
|
1500
|
-
return (0, import_react4.useMemo)(
|
|
1501
|
-
() => ({
|
|
1502
|
-
loading,
|
|
1503
|
-
error,
|
|
1504
|
-
getActiveChats,
|
|
1505
|
-
getActiveChatsCount,
|
|
1506
|
-
getWaitingChatsCount,
|
|
1507
|
-
listQueues
|
|
1508
|
-
}),
|
|
1509
|
-
[loading, error, getActiveChats, getActiveChatsCount, getWaitingChatsCount, listQueues]
|
|
1510
|
-
);
|
|
1511
|
-
}
|
|
1512
|
-
|
|
1513
|
-
// hooks/use-human-agent-sessions.ts
|
|
1514
|
-
var import_react5 = require("react");
|
|
1515
|
-
var import_hooks3 = require("@elqnt/api-client/hooks");
|
|
1516
|
-
function useHumanAgentSessions(options) {
|
|
1517
|
-
const optionsRef = useOptionsRef(options);
|
|
1518
|
-
const { execute: getOnlineSessions, loading: onlineLoading, error: onlineError } = (0, import_hooks3.useApiAsync)(
|
|
1519
|
-
() => getOnlineSessionsApi(optionsRef.current),
|
|
1520
|
-
(data) => data.sessions,
|
|
1521
|
-
[]
|
|
1522
|
-
);
|
|
1523
|
-
const { execute: getAgentSession, loading: sessionLoading, error: sessionError } = (0, import_hooks3.useApiAsync)(
|
|
1524
|
-
(agentId) => getAgentSessionApi(agentId, optionsRef.current),
|
|
1525
|
-
(data) => data.session || null,
|
|
1526
|
-
null
|
|
1527
|
-
);
|
|
1528
|
-
const loading = onlineLoading || sessionLoading;
|
|
1529
|
-
const error = onlineError || sessionError;
|
|
1530
|
-
return (0, import_react5.useMemo)(
|
|
1531
|
-
() => ({
|
|
1532
|
-
loading,
|
|
1533
|
-
error,
|
|
1534
|
-
getOnlineSessions,
|
|
1535
|
-
getAgentSession
|
|
1536
|
-
}),
|
|
1537
|
-
[loading, error, getOnlineSessions, getAgentSession]
|
|
1538
|
-
);
|
|
1539
|
-
}
|
|
1540
|
-
|
|
1541
|
-
// hooks/use-memory.ts
|
|
1542
|
-
var import_react6 = require("react");
|
|
1543
|
-
function useMemory(options, initialProfile = null) {
|
|
1544
|
-
const [profile, setProfile] = (0, import_react6.useState)(initialProfile);
|
|
1545
|
-
const [loading, setLoading] = (0, import_react6.useState)(false);
|
|
1546
|
-
const requestCountRef = (0, import_react6.useRef)(0);
|
|
1547
|
-
const runProfileMutation = (0, import_react6.useCallback)(
|
|
1548
|
-
async (fn) => {
|
|
1549
|
-
requestCountRef.current += 1;
|
|
1550
|
-
setLoading(true);
|
|
1551
|
-
try {
|
|
1552
|
-
const response = await fn();
|
|
1553
|
-
if (!response.error && response.data) {
|
|
1554
|
-
setProfile(response.data);
|
|
1555
|
-
return response.data;
|
|
1556
|
-
}
|
|
1557
|
-
return null;
|
|
1558
|
-
} catch {
|
|
1559
|
-
return null;
|
|
1560
|
-
} finally {
|
|
1561
|
-
requestCountRef.current -= 1;
|
|
1562
|
-
if (requestCountRef.current === 0) {
|
|
1563
|
-
setLoading(false);
|
|
1564
|
-
}
|
|
1565
|
-
}
|
|
1566
|
-
},
|
|
1567
|
-
[]
|
|
1568
|
-
);
|
|
1569
|
-
const optionsRef = (0, import_react6.useRef)(options);
|
|
1570
|
-
optionsRef.current = options;
|
|
1571
|
-
const patchProfile = (0, import_react6.useCallback)(
|
|
1572
|
-
(patch) => runProfileMutation(() => patchProfileApi(patch, optionsRef.current)),
|
|
1573
|
-
[runProfileMutation]
|
|
1574
|
-
);
|
|
1575
|
-
const replaceProfile = (0, import_react6.useCallback)(
|
|
1576
|
-
(p) => runProfileMutation(() => replaceProfileApi(p, optionsRef.current)),
|
|
1577
|
-
[runProfileMutation]
|
|
1578
|
-
);
|
|
1579
|
-
const clearProfile = (0, import_react6.useCallback)(
|
|
1580
|
-
() => runProfileMutation(() => clearProfileApi(optionsRef.current)),
|
|
1581
|
-
[runProfileMutation]
|
|
1582
|
-
);
|
|
1583
|
-
const deleteContact = (0, import_react6.useCallback)(
|
|
1584
|
-
(name) => runProfileMutation(() => deleteContactApi(name, optionsRef.current)),
|
|
1585
|
-
[runProfileMutation]
|
|
1586
|
-
);
|
|
1587
|
-
const deleteNote = (0, import_react6.useCallback)(
|
|
1588
|
-
(index) => runProfileMutation(() => deleteNoteApi(index, optionsRef.current)),
|
|
1589
|
-
[runProfileMutation]
|
|
1590
|
-
);
|
|
1591
|
-
const clearSummary = (0, import_react6.useCallback)(
|
|
1592
|
-
async (chatKey) => {
|
|
1593
|
-
await clearSummaryApi(chatKey, optionsRef.current);
|
|
1594
|
-
},
|
|
1595
|
-
[]
|
|
1596
|
-
);
|
|
1597
|
-
const regenerateSummary = (0, import_react6.useCallback)(
|
|
1598
|
-
async (chatKey) => {
|
|
1599
|
-
const response = await regenerateSummaryApi(chatKey, optionsRef.current);
|
|
1600
|
-
if (!response.error && response.data) {
|
|
1601
|
-
return response.data;
|
|
1602
|
-
}
|
|
1603
|
-
return null;
|
|
1604
|
-
},
|
|
1605
|
-
[]
|
|
1606
|
-
);
|
|
1607
|
-
return {
|
|
1608
|
-
profile,
|
|
1609
|
-
loading,
|
|
1610
|
-
patchProfile,
|
|
1611
|
-
replaceProfile,
|
|
1612
|
-
clearProfile,
|
|
1613
|
-
deleteContact,
|
|
1614
|
-
deleteNote,
|
|
1615
|
-
clearSummary,
|
|
1616
|
-
regenerateSummary
|
|
1617
|
-
};
|
|
1618
|
-
}
|
|
1619
|
-
|
|
1620
|
-
// hooks/index.ts
|
|
1621
|
-
var import_hooks4 = require("@elqnt/api-client/hooks");
|
|
1622
|
-
|
|
1623
216
|
// models/chat-models.ts
|
|
1624
217
|
var ChatStatusActive = "active";
|
|
1625
218
|
var ChatStatusDisconnected = "disconnected";
|
|
@@ -1651,6 +244,7 @@ var ChatEventTypeStoppedTyping = "stopped_typing";
|
|
|
1651
244
|
var ChatEventTypeRead = "read";
|
|
1652
245
|
var ChatEventTypeDelivered = "delivered";
|
|
1653
246
|
var ChatEventTypeReconnected = "reconnected";
|
|
247
|
+
var ChatEventTypeTransportReconnected = "transport_reconnected";
|
|
1654
248
|
var ChatEventTypeError = "error";
|
|
1655
249
|
var ChatEventTypeWaiting = "waiting";
|
|
1656
250
|
var ChatEventTypeLoadChat = "load_chat";
|
|
@@ -1790,6 +384,9 @@ var GetActiveChatsSubject = "chat.get_active_chats";
|
|
|
1790
384
|
var GetUserChatsSubject = "chat.get_user_chats";
|
|
1791
385
|
var GetChatSubject = "chat.get_chat";
|
|
1792
386
|
var ChatArchiveSubjectPattern = "chat.archive.%s.server";
|
|
387
|
+
var ListIdleChatsSubject = "chat.maintenance.list_idle_chats";
|
|
388
|
+
var CloseAndDeleteChatSubject = "chat.maintenance.close_and_delete_chat";
|
|
389
|
+
var MarkChatClosedSubject = "chat.maintenance.mark_closed";
|
|
1793
390
|
var StartAgentSessionSubject = "chat.agent_session.start";
|
|
1794
391
|
var EndAgentSessionSubject = "chat.agent_session.end";
|
|
1795
392
|
var UpdateAgentStatusSubject = "chat.agent_session.update_status";
|
|
@@ -1921,6 +518,7 @@ var TriggerMemorySummarizationSubject = "chat.memory.summarize.nightly";
|
|
|
1921
518
|
ChatEventTypeSyncMetadataResponse,
|
|
1922
519
|
ChatEventTypeSyncUserSession,
|
|
1923
520
|
ChatEventTypeSyncUserSessionResponse,
|
|
521
|
+
ChatEventTypeTransportReconnected,
|
|
1924
522
|
ChatEventTypeTyping,
|
|
1925
523
|
ChatEventTypeUpdateRoom,
|
|
1926
524
|
ChatEventTypeUserActivity,
|
|
@@ -1959,6 +557,7 @@ var TriggerMemorySummarizationSubject = "chat.memory.summarize.nightly";
|
|
|
1959
557
|
ChatTypeGroup,
|
|
1960
558
|
ChatTypePrivateRoom,
|
|
1961
559
|
ChatTypePublicRoom,
|
|
560
|
+
CloseAndDeleteChatSubject,
|
|
1962
561
|
CompleteChatByAgentSubject,
|
|
1963
562
|
CreateAgentQueueSubject,
|
|
1964
563
|
DeleteAgentQueueSubject,
|
|
@@ -1978,6 +577,8 @@ var TriggerMemorySummarizationSubject = "chat.memory.summarize.nightly";
|
|
|
1978
577
|
GetUserChatsSubject,
|
|
1979
578
|
GetWaitingForAgentChatCountSubject,
|
|
1980
579
|
GetWaitingForAgentChatsSubject,
|
|
580
|
+
ListIdleChatsSubject,
|
|
581
|
+
MarkChatClosedSubject,
|
|
1981
582
|
MessageStatusDelivered,
|
|
1982
583
|
MessageStatusFailed,
|
|
1983
584
|
MessageStatusRead,
|
|
@@ -1999,13 +600,6 @@ var TriggerMemorySummarizationSubject = "chat.memory.summarize.nightly";
|
|
|
1999
600
|
UserStatusAway,
|
|
2000
601
|
UserStatusBusy,
|
|
2001
602
|
UserStatusOffline,
|
|
2002
|
-
UserStatusOnline
|
|
2003
|
-
useApiAsync,
|
|
2004
|
-
useChat,
|
|
2005
|
-
useChatHistory,
|
|
2006
|
-
useChatMonitoring,
|
|
2007
|
-
useHumanAgentSessions,
|
|
2008
|
-
useMemory,
|
|
2009
|
-
useOptionsRef
|
|
603
|
+
UserStatusOnline
|
|
2010
604
|
});
|
|
2011
605
|
//# sourceMappingURL=index.js.map
|