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