@elqnt/chat 3.0.2 → 3.0.3
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 +27 -0
- package/dist/api/index.d.mts +37 -1
- package/dist/api/index.d.ts +37 -1
- package/dist/api/index.js +40 -0
- package/dist/api/index.js.map +1 -1
- package/dist/api/index.mjs +35 -0
- package/dist/api/index.mjs.map +1 -1
- package/dist/hooks/index.d.mts +99 -2
- package/dist/hooks/index.d.ts +99 -2
- package/dist/hooks/index.js +619 -8
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +613 -7
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/index.d.mts +6 -3
- package/dist/index.d.ts +6 -3
- package/dist/index.js +625 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +617 -7
- package/dist/index.mjs.map +1 -1
- package/dist/models/index.d.mts +36 -3
- package/dist/models/index.d.ts +36 -3
- package/dist/models/index.js +6 -0
- package/dist/models/index.js.map +1 -1
- package/dist/models/index.mjs +4 -0
- package/dist/models/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/hooks/index.js
CHANGED
|
@@ -21,7 +21,12 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
// hooks/index.ts
|
|
22
22
|
var hooks_exports = {};
|
|
23
23
|
__export(hooks_exports, {
|
|
24
|
-
|
|
24
|
+
useApiAsync: () => import_hooks4.useApiAsync,
|
|
25
|
+
useChat: () => useChat,
|
|
26
|
+
useChatHistory: () => useChatHistory,
|
|
27
|
+
useChatMonitoring: () => useChatMonitoring,
|
|
28
|
+
useHumanAgentSessions: () => useHumanAgentSessions,
|
|
29
|
+
useOptionsRef: () => useOptionsRef
|
|
25
30
|
});
|
|
26
31
|
module.exports = __toCommonJS(hooks_exports);
|
|
27
32
|
|
|
@@ -397,8 +402,394 @@ function createSSETransport(options = {}) {
|
|
|
397
402
|
return transport;
|
|
398
403
|
}
|
|
399
404
|
|
|
405
|
+
// transport/sse-fetch.ts
|
|
406
|
+
function createFetchSSETransport(options = {}) {
|
|
407
|
+
const {
|
|
408
|
+
retryConfig = DEFAULT_RETRY_CONFIG,
|
|
409
|
+
debug = false,
|
|
410
|
+
logger = createLogger(debug),
|
|
411
|
+
customFetch = fetch
|
|
412
|
+
} = options;
|
|
413
|
+
let abortController;
|
|
414
|
+
let config;
|
|
415
|
+
let state = "disconnected";
|
|
416
|
+
let error;
|
|
417
|
+
let retryCount = 0;
|
|
418
|
+
let reconnectTimeout;
|
|
419
|
+
let intentionalDisconnect = false;
|
|
420
|
+
const metrics = {
|
|
421
|
+
latency: 0,
|
|
422
|
+
messagesSent: 0,
|
|
423
|
+
messagesReceived: 0,
|
|
424
|
+
messagesQueued: 0,
|
|
425
|
+
reconnectCount: 0,
|
|
426
|
+
transportType: "sse-fetch"
|
|
427
|
+
};
|
|
428
|
+
const globalHandlers = /* @__PURE__ */ new Set();
|
|
429
|
+
const typeHandlers = /* @__PURE__ */ new Map();
|
|
430
|
+
function emit(event) {
|
|
431
|
+
metrics.messagesReceived++;
|
|
432
|
+
metrics.lastMessageAt = Date.now();
|
|
433
|
+
globalHandlers.forEach((handler) => {
|
|
434
|
+
try {
|
|
435
|
+
handler(event);
|
|
436
|
+
} catch (err) {
|
|
437
|
+
logger.error("Error in message handler:", err);
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
const handlers = typeHandlers.get(event.type);
|
|
441
|
+
if (handlers) {
|
|
442
|
+
handlers.forEach((handler) => {
|
|
443
|
+
try {
|
|
444
|
+
handler(event);
|
|
445
|
+
} catch (err) {
|
|
446
|
+
logger.error(`Error in ${event.type} handler:`, err);
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
async function sendRest(endpoint, body) {
|
|
452
|
+
if (!config) {
|
|
453
|
+
throw new Error("Transport not connected");
|
|
454
|
+
}
|
|
455
|
+
const url = `${config.baseUrl}/${endpoint}`;
|
|
456
|
+
logger.debug(`POST ${endpoint}`, body);
|
|
457
|
+
const response = await customFetch(url, {
|
|
458
|
+
method: "POST",
|
|
459
|
+
headers: { "Content-Type": "application/json" },
|
|
460
|
+
body: JSON.stringify(body)
|
|
461
|
+
});
|
|
462
|
+
if (!response.ok) {
|
|
463
|
+
const errorText = await response.text();
|
|
464
|
+
throw new Error(`API error: ${response.status} - ${errorText}`);
|
|
465
|
+
}
|
|
466
|
+
const json = await response.json();
|
|
467
|
+
if (json && typeof json === "object" && "data" in json) {
|
|
468
|
+
return json.data;
|
|
469
|
+
}
|
|
470
|
+
return json;
|
|
471
|
+
}
|
|
472
|
+
function parseSSEChunk(chunk) {
|
|
473
|
+
const events = [];
|
|
474
|
+
const lines = chunk.split("\n");
|
|
475
|
+
let eventType = "message";
|
|
476
|
+
let data = "";
|
|
477
|
+
for (const line of lines) {
|
|
478
|
+
if (line.startsWith("event:")) {
|
|
479
|
+
eventType = line.slice(6).trim();
|
|
480
|
+
} else if (line.startsWith("data:")) {
|
|
481
|
+
data = line.slice(5).trim();
|
|
482
|
+
} else if (line === "" && data) {
|
|
483
|
+
try {
|
|
484
|
+
const parsed = JSON.parse(data);
|
|
485
|
+
events.push(parsed);
|
|
486
|
+
} catch {
|
|
487
|
+
logger.warn("Failed to parse SSE data:", data);
|
|
488
|
+
}
|
|
489
|
+
eventType = "message";
|
|
490
|
+
data = "";
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return events;
|
|
494
|
+
}
|
|
495
|
+
async function startStream(cfg) {
|
|
496
|
+
const url = `${cfg.baseUrl}/stream?orgId=${cfg.orgId}&userId=${cfg.userId}&clientType=${cfg.clientType}${cfg.chatKey ? `&chatId=${cfg.chatKey}` : ""}`;
|
|
497
|
+
logger.debug("Connecting to:", url);
|
|
498
|
+
abortController = new AbortController();
|
|
499
|
+
const response = await customFetch(url, {
|
|
500
|
+
method: "GET",
|
|
501
|
+
headers: {
|
|
502
|
+
Accept: "text/event-stream",
|
|
503
|
+
"Cache-Control": "no-cache"
|
|
504
|
+
},
|
|
505
|
+
signal: abortController.signal
|
|
506
|
+
});
|
|
507
|
+
if (!response.ok) {
|
|
508
|
+
throw new Error(`Stream connection failed: ${response.status}`);
|
|
509
|
+
}
|
|
510
|
+
if (!response.body) {
|
|
511
|
+
throw new Error("Response body is null - ReadableStream not supported");
|
|
512
|
+
}
|
|
513
|
+
const reader = response.body.getReader();
|
|
514
|
+
const decoder = new TextDecoder();
|
|
515
|
+
let buffer = "";
|
|
516
|
+
const readStream = async () => {
|
|
517
|
+
try {
|
|
518
|
+
while (true) {
|
|
519
|
+
const { done, value } = await reader.read();
|
|
520
|
+
if (done) {
|
|
521
|
+
logger.info("Stream ended");
|
|
522
|
+
break;
|
|
523
|
+
}
|
|
524
|
+
buffer += decoder.decode(value, { stream: true });
|
|
525
|
+
const lastNewline = buffer.lastIndexOf("\n\n");
|
|
526
|
+
if (lastNewline !== -1) {
|
|
527
|
+
const complete = buffer.slice(0, lastNewline + 2);
|
|
528
|
+
buffer = buffer.slice(lastNewline + 2);
|
|
529
|
+
const events = parseSSEChunk(complete);
|
|
530
|
+
events.forEach(emit);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
} catch (err) {
|
|
534
|
+
if (err.name === "AbortError") {
|
|
535
|
+
logger.debug("Stream aborted");
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
logger.error("Stream error:", err);
|
|
539
|
+
throw err;
|
|
540
|
+
}
|
|
541
|
+
if (!intentionalDisconnect) {
|
|
542
|
+
state = "disconnected";
|
|
543
|
+
scheduleReconnect();
|
|
544
|
+
}
|
|
545
|
+
};
|
|
546
|
+
readStream().catch((err) => {
|
|
547
|
+
if (!intentionalDisconnect) {
|
|
548
|
+
error = {
|
|
549
|
+
code: "CONNECTION_FAILED",
|
|
550
|
+
message: err.message,
|
|
551
|
+
retryable: true,
|
|
552
|
+
timestamp: Date.now(),
|
|
553
|
+
originalError: err
|
|
554
|
+
};
|
|
555
|
+
metrics.lastError = error;
|
|
556
|
+
state = "disconnected";
|
|
557
|
+
scheduleReconnect();
|
|
558
|
+
}
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
function scheduleReconnect() {
|
|
562
|
+
if (intentionalDisconnect || !config) return;
|
|
563
|
+
const maxRetries = retryConfig.maxRetries ?? DEFAULT_RETRY_CONFIG.maxRetries;
|
|
564
|
+
if (retryCount >= maxRetries) {
|
|
565
|
+
logger.error(`Max retries (${maxRetries}) exceeded`);
|
|
566
|
+
error = {
|
|
567
|
+
code: "CONNECTION_FAILED",
|
|
568
|
+
message: `Max retries (${maxRetries}) exceeded`,
|
|
569
|
+
retryable: false,
|
|
570
|
+
timestamp: Date.now()
|
|
571
|
+
};
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
const interval = calculateRetryInterval(retryCount, retryConfig);
|
|
575
|
+
retryCount++;
|
|
576
|
+
metrics.reconnectCount++;
|
|
577
|
+
logger.info(`Reconnecting in ${interval}ms (attempt ${retryCount})`);
|
|
578
|
+
state = "reconnecting";
|
|
579
|
+
reconnectTimeout = setTimeout(() => {
|
|
580
|
+
if (config) {
|
|
581
|
+
transport.connect(config).catch((err) => {
|
|
582
|
+
logger.error("Reconnect failed:", err);
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
}, interval);
|
|
586
|
+
}
|
|
587
|
+
const transport = {
|
|
588
|
+
async connect(cfg) {
|
|
589
|
+
config = cfg;
|
|
590
|
+
intentionalDisconnect = false;
|
|
591
|
+
if (abortController) {
|
|
592
|
+
abortController.abort();
|
|
593
|
+
abortController = void 0;
|
|
594
|
+
}
|
|
595
|
+
if (reconnectTimeout) {
|
|
596
|
+
clearTimeout(reconnectTimeout);
|
|
597
|
+
reconnectTimeout = void 0;
|
|
598
|
+
}
|
|
599
|
+
state = retryCount > 0 ? "reconnecting" : "connecting";
|
|
600
|
+
const connectionStart = Date.now();
|
|
601
|
+
try {
|
|
602
|
+
await startStream(cfg);
|
|
603
|
+
const connectionTime = Date.now() - connectionStart;
|
|
604
|
+
logger.info(`Connected in ${connectionTime}ms`);
|
|
605
|
+
state = "connected";
|
|
606
|
+
error = void 0;
|
|
607
|
+
retryCount = 0;
|
|
608
|
+
metrics.connectedAt = Date.now();
|
|
609
|
+
metrics.latency = connectionTime;
|
|
610
|
+
} catch (err) {
|
|
611
|
+
const connectError = {
|
|
612
|
+
code: "CONNECTION_FAILED",
|
|
613
|
+
message: err.message,
|
|
614
|
+
retryable: true,
|
|
615
|
+
timestamp: Date.now(),
|
|
616
|
+
originalError: err
|
|
617
|
+
};
|
|
618
|
+
error = connectError;
|
|
619
|
+
metrics.lastError = connectError;
|
|
620
|
+
state = "disconnected";
|
|
621
|
+
if (!intentionalDisconnect) {
|
|
622
|
+
scheduleReconnect();
|
|
623
|
+
}
|
|
624
|
+
throw connectError;
|
|
625
|
+
}
|
|
626
|
+
},
|
|
627
|
+
disconnect(intentional = true) {
|
|
628
|
+
logger.info("Disconnecting", { intentional });
|
|
629
|
+
intentionalDisconnect = intentional;
|
|
630
|
+
if (reconnectTimeout) {
|
|
631
|
+
clearTimeout(reconnectTimeout);
|
|
632
|
+
reconnectTimeout = void 0;
|
|
633
|
+
}
|
|
634
|
+
if (abortController) {
|
|
635
|
+
abortController.abort();
|
|
636
|
+
abortController = void 0;
|
|
637
|
+
}
|
|
638
|
+
state = "disconnected";
|
|
639
|
+
retryCount = 0;
|
|
640
|
+
},
|
|
641
|
+
async send(event) {
|
|
642
|
+
if (!config) {
|
|
643
|
+
throw new Error("Transport not connected");
|
|
644
|
+
}
|
|
645
|
+
switch (event.type) {
|
|
646
|
+
case "message":
|
|
647
|
+
await sendRest("send", {
|
|
648
|
+
orgId: event.orgId,
|
|
649
|
+
chatKey: event.chatKey,
|
|
650
|
+
userId: event.userId,
|
|
651
|
+
message: event.message
|
|
652
|
+
});
|
|
653
|
+
break;
|
|
654
|
+
case "typing":
|
|
655
|
+
await sendRest("typing", {
|
|
656
|
+
orgId: event.orgId,
|
|
657
|
+
chatKey: event.chatKey,
|
|
658
|
+
userId: event.userId,
|
|
659
|
+
typing: true
|
|
660
|
+
});
|
|
661
|
+
break;
|
|
662
|
+
case "stopped_typing":
|
|
663
|
+
await sendRest("typing", {
|
|
664
|
+
orgId: event.orgId,
|
|
665
|
+
chatKey: event.chatKey,
|
|
666
|
+
userId: event.userId,
|
|
667
|
+
typing: false
|
|
668
|
+
});
|
|
669
|
+
break;
|
|
670
|
+
case "load_chat":
|
|
671
|
+
await sendRest("load", {
|
|
672
|
+
orgId: event.orgId,
|
|
673
|
+
chatKey: event.chatKey,
|
|
674
|
+
userId: event.userId
|
|
675
|
+
});
|
|
676
|
+
break;
|
|
677
|
+
case "new_chat":
|
|
678
|
+
await sendRest("create", {
|
|
679
|
+
orgId: event.orgId,
|
|
680
|
+
userId: event.userId,
|
|
681
|
+
metadata: event.data
|
|
682
|
+
});
|
|
683
|
+
break;
|
|
684
|
+
case "end_chat":
|
|
685
|
+
await sendRest("end", {
|
|
686
|
+
orgId: event.orgId,
|
|
687
|
+
chatKey: event.chatKey,
|
|
688
|
+
userId: event.userId,
|
|
689
|
+
data: event.data
|
|
690
|
+
});
|
|
691
|
+
break;
|
|
692
|
+
default:
|
|
693
|
+
await sendRest("event", {
|
|
694
|
+
type: event.type,
|
|
695
|
+
orgId: event.orgId,
|
|
696
|
+
chatKey: event.chatKey,
|
|
697
|
+
userId: event.userId,
|
|
698
|
+
data: event.data
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
metrics.messagesSent++;
|
|
702
|
+
},
|
|
703
|
+
async sendMessage(message) {
|
|
704
|
+
if (!config) {
|
|
705
|
+
throw new Error("Transport not connected");
|
|
706
|
+
}
|
|
707
|
+
await sendRest("send", {
|
|
708
|
+
orgId: config.orgId,
|
|
709
|
+
chatKey: config.chatKey,
|
|
710
|
+
userId: config.userId,
|
|
711
|
+
message
|
|
712
|
+
});
|
|
713
|
+
metrics.messagesSent++;
|
|
714
|
+
},
|
|
715
|
+
async createChat(options2) {
|
|
716
|
+
if (!config) {
|
|
717
|
+
throw new Error("Transport not connected");
|
|
718
|
+
}
|
|
719
|
+
const response = await sendRest("create", {
|
|
720
|
+
orgId: options2.orgId,
|
|
721
|
+
userId: options2.userId,
|
|
722
|
+
metadata: options2.metadata
|
|
723
|
+
});
|
|
724
|
+
if (!response?.chatKey) {
|
|
725
|
+
throw new Error("Failed to create chat: no chatKey returned");
|
|
726
|
+
}
|
|
727
|
+
return { chatKey: response.chatKey };
|
|
728
|
+
},
|
|
729
|
+
async loadChatData(options2) {
|
|
730
|
+
if (!config) {
|
|
731
|
+
throw new Error("Transport not connected");
|
|
732
|
+
}
|
|
733
|
+
const response = await sendRest("load", {
|
|
734
|
+
orgId: options2.orgId,
|
|
735
|
+
chatKey: options2.chatKey,
|
|
736
|
+
userId: options2.userId
|
|
737
|
+
});
|
|
738
|
+
if (!response?.chat) {
|
|
739
|
+
throw new Error("Failed to load chat: no chat data returned");
|
|
740
|
+
}
|
|
741
|
+
return {
|
|
742
|
+
chat: response.chat,
|
|
743
|
+
agentId: response.agentId
|
|
744
|
+
};
|
|
745
|
+
},
|
|
746
|
+
onMessage(handler) {
|
|
747
|
+
globalHandlers.add(handler);
|
|
748
|
+
return () => globalHandlers.delete(handler);
|
|
749
|
+
},
|
|
750
|
+
on(eventType, handler) {
|
|
751
|
+
if (!typeHandlers.has(eventType)) {
|
|
752
|
+
typeHandlers.set(eventType, /* @__PURE__ */ new Set());
|
|
753
|
+
}
|
|
754
|
+
typeHandlers.get(eventType).add(handler);
|
|
755
|
+
return () => {
|
|
756
|
+
const handlers = typeHandlers.get(eventType);
|
|
757
|
+
if (handlers) {
|
|
758
|
+
handlers.delete(handler);
|
|
759
|
+
if (handlers.size === 0) {
|
|
760
|
+
typeHandlers.delete(eventType);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
};
|
|
764
|
+
},
|
|
765
|
+
getState() {
|
|
766
|
+
return state;
|
|
767
|
+
},
|
|
768
|
+
getMetrics() {
|
|
769
|
+
return { ...metrics };
|
|
770
|
+
},
|
|
771
|
+
getError() {
|
|
772
|
+
return error;
|
|
773
|
+
},
|
|
774
|
+
clearError() {
|
|
775
|
+
error = void 0;
|
|
776
|
+
}
|
|
777
|
+
};
|
|
778
|
+
return transport;
|
|
779
|
+
}
|
|
780
|
+
|
|
400
781
|
// hooks/use-chat.ts
|
|
401
782
|
function getDefaultTransport(type, debug) {
|
|
783
|
+
if (type === "sse-fetch") {
|
|
784
|
+
return createFetchSSETransport({ debug });
|
|
785
|
+
}
|
|
786
|
+
if (type === "sse") {
|
|
787
|
+
return createSSETransport({ debug });
|
|
788
|
+
}
|
|
789
|
+
const isReactNative = typeof navigator !== "undefined" && navigator.product === "ReactNative";
|
|
790
|
+
if (isReactNative || typeof EventSource === "undefined") {
|
|
791
|
+
return createFetchSSETransport({ debug });
|
|
792
|
+
}
|
|
402
793
|
return createSSETransport({ debug });
|
|
403
794
|
}
|
|
404
795
|
function useChat(options) {
|
|
@@ -509,6 +900,7 @@ function useChat(options) {
|
|
|
509
900
|
throw err;
|
|
510
901
|
}
|
|
511
902
|
setConnectionState("connecting");
|
|
903
|
+
onConnectionChange?.("connecting");
|
|
512
904
|
try {
|
|
513
905
|
const unsubscribe = transport.onMessage(handleEvent);
|
|
514
906
|
await transport.connect({
|
|
@@ -527,6 +919,7 @@ function useChat(options) {
|
|
|
527
919
|
} catch (err) {
|
|
528
920
|
const transportError = err;
|
|
529
921
|
setConnectionState("disconnected");
|
|
922
|
+
onConnectionChange?.("disconnected");
|
|
530
923
|
setError(transportError);
|
|
531
924
|
onErrorRef.current?.(transportError);
|
|
532
925
|
throw err;
|
|
@@ -556,7 +949,7 @@ function useChat(options) {
|
|
|
556
949
|
},
|
|
557
950
|
[orgId, userId]
|
|
558
951
|
);
|
|
559
|
-
const
|
|
952
|
+
const loadChat2 = (0, import_react.useCallback)(
|
|
560
953
|
async (key) => {
|
|
561
954
|
const transport = transportRef.current;
|
|
562
955
|
if (!transport) {
|
|
@@ -609,7 +1002,7 @@ function useChat(options) {
|
|
|
609
1002
|
},
|
|
610
1003
|
[orgId, chatKey, userId]
|
|
611
1004
|
);
|
|
612
|
-
const
|
|
1005
|
+
const endChat2 = (0, import_react.useCallback)(
|
|
613
1006
|
async (reason) => {
|
|
614
1007
|
const transport = transportRef.current;
|
|
615
1008
|
if (!transport) {
|
|
@@ -631,7 +1024,7 @@ function useChat(options) {
|
|
|
631
1024
|
},
|
|
632
1025
|
[orgId, chatKey, userId]
|
|
633
1026
|
);
|
|
634
|
-
const
|
|
1027
|
+
const sendEvent2 = (0, import_react.useCallback)(
|
|
635
1028
|
async (event) => {
|
|
636
1029
|
const transport = transportRef.current;
|
|
637
1030
|
if (!transport) {
|
|
@@ -716,10 +1109,10 @@ function useChat(options) {
|
|
|
716
1109
|
isConnected,
|
|
717
1110
|
// Chat operations
|
|
718
1111
|
startChat,
|
|
719
|
-
loadChat,
|
|
1112
|
+
loadChat: loadChat2,
|
|
720
1113
|
sendMessage,
|
|
721
|
-
sendEvent,
|
|
722
|
-
endChat,
|
|
1114
|
+
sendEvent: sendEvent2,
|
|
1115
|
+
endChat: endChat2,
|
|
723
1116
|
// Typing
|
|
724
1117
|
startTyping,
|
|
725
1118
|
stopTyping,
|
|
@@ -734,8 +1127,226 @@ function useChat(options) {
|
|
|
734
1127
|
clearError
|
|
735
1128
|
};
|
|
736
1129
|
}
|
|
1130
|
+
|
|
1131
|
+
// hooks/use-chat-history.ts
|
|
1132
|
+
var import_react3 = require("react");
|
|
1133
|
+
|
|
1134
|
+
// api/index.ts
|
|
1135
|
+
var import_browser = require("@elqnt/api-client/browser");
|
|
1136
|
+
async function getChatHistoryApi(options) {
|
|
1137
|
+
return (0, import_browser.browserApiRequest)("/api/v1/chats", {
|
|
1138
|
+
method: "POST",
|
|
1139
|
+
body: {
|
|
1140
|
+
limit: options.limit || 15,
|
|
1141
|
+
offset: options.offset || 0,
|
|
1142
|
+
...options.skipCache ? { skipCache: true } : {}
|
|
1143
|
+
},
|
|
1144
|
+
...options
|
|
1145
|
+
});
|
|
1146
|
+
}
|
|
1147
|
+
async function getChatApi(chatKey, options) {
|
|
1148
|
+
return (0, import_browser.browserApiRequest)(`/api/v1/chats/${chatKey}`, {
|
|
1149
|
+
method: "GET",
|
|
1150
|
+
...options
|
|
1151
|
+
});
|
|
1152
|
+
}
|
|
1153
|
+
async function updateChatApi(chatKey, updates, options) {
|
|
1154
|
+
return (0, import_browser.browserApiRequest)(`/api/v1/chats/${chatKey}`, {
|
|
1155
|
+
method: "PATCH",
|
|
1156
|
+
body: updates,
|
|
1157
|
+
...options
|
|
1158
|
+
});
|
|
1159
|
+
}
|
|
1160
|
+
async function deleteChatApi(chatKey, options) {
|
|
1161
|
+
return (0, import_browser.browserApiRequest)(`/api/v1/chats/${chatKey}`, {
|
|
1162
|
+
method: "DELETE",
|
|
1163
|
+
...options
|
|
1164
|
+
});
|
|
1165
|
+
}
|
|
1166
|
+
async function getActiveChatsCountApi(options) {
|
|
1167
|
+
return (0, import_browser.browserApiRequest)("/api/v1/chats/active/count", {
|
|
1168
|
+
method: "GET",
|
|
1169
|
+
...options
|
|
1170
|
+
});
|
|
1171
|
+
}
|
|
1172
|
+
async function getActiveChatsApi(options) {
|
|
1173
|
+
const params = new URLSearchParams();
|
|
1174
|
+
if (options.pastHours) params.set("pastHours", String(options.pastHours));
|
|
1175
|
+
const queryString = params.toString();
|
|
1176
|
+
return (0, import_browser.browserApiRequest)(`/api/v1/chats/active${queryString ? `?${queryString}` : ""}`, {
|
|
1177
|
+
method: "GET",
|
|
1178
|
+
...options
|
|
1179
|
+
});
|
|
1180
|
+
}
|
|
1181
|
+
async function getWaitingChatsCountApi(options) {
|
|
1182
|
+
return (0, import_browser.browserApiRequest)("/api/v1/chats/waiting/count", {
|
|
1183
|
+
method: "GET",
|
|
1184
|
+
...options
|
|
1185
|
+
});
|
|
1186
|
+
}
|
|
1187
|
+
async function getChatsByUserApi(userEmail, options) {
|
|
1188
|
+
return (0, import_browser.browserApiRequest)(`/api/v1/chats/user/${encodeURIComponent(userEmail)}`, {
|
|
1189
|
+
method: "GET",
|
|
1190
|
+
...options
|
|
1191
|
+
});
|
|
1192
|
+
}
|
|
1193
|
+
async function listQueuesApi(options) {
|
|
1194
|
+
return (0, import_browser.browserApiRequest)("/api/v1/queues", {
|
|
1195
|
+
method: "GET",
|
|
1196
|
+
...options
|
|
1197
|
+
});
|
|
1198
|
+
}
|
|
1199
|
+
async function getOnlineSessionsApi(options) {
|
|
1200
|
+
return (0, import_browser.browserApiRequest)("/api/v1/agents/sessions/online", {
|
|
1201
|
+
method: "GET",
|
|
1202
|
+
...options
|
|
1203
|
+
});
|
|
1204
|
+
}
|
|
1205
|
+
async function getAgentSessionApi(agentId, options) {
|
|
1206
|
+
return (0, import_browser.browserApiRequest)(`/api/v1/agents/sessions/${agentId}`, {
|
|
1207
|
+
method: "GET",
|
|
1208
|
+
...options
|
|
1209
|
+
});
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
// hooks/use-chat-history.ts
|
|
1213
|
+
var import_hooks = require("@elqnt/api-client/hooks");
|
|
1214
|
+
|
|
1215
|
+
// hooks/use-options-ref.ts
|
|
1216
|
+
var import_react2 = require("react");
|
|
1217
|
+
function useOptionsRef(options) {
|
|
1218
|
+
const optionsRef = (0, import_react2.useRef)(options);
|
|
1219
|
+
(0, import_react2.useEffect)(() => {
|
|
1220
|
+
optionsRef.current = options;
|
|
1221
|
+
}, [options]);
|
|
1222
|
+
return optionsRef;
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
// hooks/use-chat-history.ts
|
|
1226
|
+
function useChatHistory(options) {
|
|
1227
|
+
const optionsRef = useOptionsRef(options);
|
|
1228
|
+
const { execute: getChatHistory, loading: listLoading, error: listError } = (0, import_hooks.useApiAsync)(
|
|
1229
|
+
(params) => getChatHistoryApi({ ...optionsRef.current, ...params }),
|
|
1230
|
+
(data) => ({
|
|
1231
|
+
chats: data.chats,
|
|
1232
|
+
total: data.total,
|
|
1233
|
+
hasMore: data.hasMore
|
|
1234
|
+
}),
|
|
1235
|
+
{ chats: [], total: 0, hasMore: false }
|
|
1236
|
+
);
|
|
1237
|
+
const { execute: getChat, loading: getLoading, error: getError } = (0, import_hooks.useApiAsync)(
|
|
1238
|
+
(chatKey) => getChatApi(chatKey, optionsRef.current),
|
|
1239
|
+
(data) => data.chat || null,
|
|
1240
|
+
null
|
|
1241
|
+
);
|
|
1242
|
+
const { execute: updateChat, loading: updateLoading, error: updateError } = (0, import_hooks.useApiAsync)(
|
|
1243
|
+
(chatKey, updates) => updateChatApi(chatKey, updates, optionsRef.current),
|
|
1244
|
+
(data) => !!data.chatKey,
|
|
1245
|
+
false
|
|
1246
|
+
);
|
|
1247
|
+
const { execute: deleteChat, loading: deleteLoading, error: deleteError } = (0, import_hooks.useApiAsync)(
|
|
1248
|
+
(chatKey) => deleteChatApi(chatKey, optionsRef.current),
|
|
1249
|
+
(data) => data.success,
|
|
1250
|
+
false
|
|
1251
|
+
);
|
|
1252
|
+
const { execute: getChatsByUser, loading: userChatsLoading, error: userChatsError } = (0, import_hooks.useApiAsync)(
|
|
1253
|
+
(userEmail) => getChatsByUserApi(userEmail, optionsRef.current),
|
|
1254
|
+
(data) => data.chats,
|
|
1255
|
+
[]
|
|
1256
|
+
);
|
|
1257
|
+
const loading = listLoading || getLoading || updateLoading || deleteLoading || userChatsLoading;
|
|
1258
|
+
const error = listError || getError || updateError || deleteError || userChatsError;
|
|
1259
|
+
return (0, import_react3.useMemo)(
|
|
1260
|
+
() => ({
|
|
1261
|
+
loading,
|
|
1262
|
+
error,
|
|
1263
|
+
getChatHistory,
|
|
1264
|
+
getChat,
|
|
1265
|
+
updateChat,
|
|
1266
|
+
deleteChat,
|
|
1267
|
+
getChatsByUser
|
|
1268
|
+
}),
|
|
1269
|
+
[loading, error, getChatHistory, getChat, updateChat, deleteChat, getChatsByUser]
|
|
1270
|
+
);
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
// hooks/use-chat-monitoring.ts
|
|
1274
|
+
var import_react4 = require("react");
|
|
1275
|
+
var import_hooks2 = require("@elqnt/api-client/hooks");
|
|
1276
|
+
function useChatMonitoring(options) {
|
|
1277
|
+
const optionsRef = useOptionsRef(options);
|
|
1278
|
+
const { execute: getActiveChats, loading: activeLoading, error: activeError } = (0, import_hooks2.useApiAsync)(
|
|
1279
|
+
(pastHours) => getActiveChatsApi({ ...optionsRef.current, pastHours }),
|
|
1280
|
+
(data) => data.chats,
|
|
1281
|
+
[]
|
|
1282
|
+
);
|
|
1283
|
+
const { execute: getActiveChatsCount, loading: activeCountLoading, error: activeCountError } = (0, import_hooks2.useApiAsync)(
|
|
1284
|
+
() => getActiveChatsCountApi(optionsRef.current),
|
|
1285
|
+
(data) => data.count,
|
|
1286
|
+
0
|
|
1287
|
+
);
|
|
1288
|
+
const { execute: getWaitingChatsCount, loading: waitingCountLoading, error: waitingCountError } = (0, import_hooks2.useApiAsync)(
|
|
1289
|
+
() => getWaitingChatsCountApi(optionsRef.current),
|
|
1290
|
+
(data) => data.count,
|
|
1291
|
+
0
|
|
1292
|
+
);
|
|
1293
|
+
const { execute: listQueues, loading: queuesLoading, error: queuesError } = (0, import_hooks2.useApiAsync)(
|
|
1294
|
+
() => listQueuesApi(optionsRef.current),
|
|
1295
|
+
(data) => data.queues,
|
|
1296
|
+
[]
|
|
1297
|
+
);
|
|
1298
|
+
const loading = activeLoading || activeCountLoading || waitingCountLoading || queuesLoading;
|
|
1299
|
+
const error = activeError || activeCountError || waitingCountError || queuesError;
|
|
1300
|
+
return (0, import_react4.useMemo)(
|
|
1301
|
+
() => ({
|
|
1302
|
+
loading,
|
|
1303
|
+
error,
|
|
1304
|
+
getActiveChats,
|
|
1305
|
+
getActiveChatsCount,
|
|
1306
|
+
getWaitingChatsCount,
|
|
1307
|
+
listQueues
|
|
1308
|
+
}),
|
|
1309
|
+
[loading, error, getActiveChats, getActiveChatsCount, getWaitingChatsCount, listQueues]
|
|
1310
|
+
);
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
// hooks/use-human-agent-sessions.ts
|
|
1314
|
+
var import_react5 = require("react");
|
|
1315
|
+
var import_hooks3 = require("@elqnt/api-client/hooks");
|
|
1316
|
+
function useHumanAgentSessions(options) {
|
|
1317
|
+
const optionsRef = useOptionsRef(options);
|
|
1318
|
+
const { execute: getOnlineSessions, loading: onlineLoading, error: onlineError } = (0, import_hooks3.useApiAsync)(
|
|
1319
|
+
() => getOnlineSessionsApi(optionsRef.current),
|
|
1320
|
+
(data) => data.sessions,
|
|
1321
|
+
[]
|
|
1322
|
+
);
|
|
1323
|
+
const { execute: getAgentSession, loading: sessionLoading, error: sessionError } = (0, import_hooks3.useApiAsync)(
|
|
1324
|
+
(agentId) => getAgentSessionApi(agentId, optionsRef.current),
|
|
1325
|
+
(data) => data.session || null,
|
|
1326
|
+
null
|
|
1327
|
+
);
|
|
1328
|
+
const loading = onlineLoading || sessionLoading;
|
|
1329
|
+
const error = onlineError || sessionError;
|
|
1330
|
+
return (0, import_react5.useMemo)(
|
|
1331
|
+
() => ({
|
|
1332
|
+
loading,
|
|
1333
|
+
error,
|
|
1334
|
+
getOnlineSessions,
|
|
1335
|
+
getAgentSession
|
|
1336
|
+
}),
|
|
1337
|
+
[loading, error, getOnlineSessions, getAgentSession]
|
|
1338
|
+
);
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
// hooks/index.ts
|
|
1342
|
+
var import_hooks4 = require("@elqnt/api-client/hooks");
|
|
737
1343
|
// Annotate the CommonJS export names for ESM import in node:
|
|
738
1344
|
0 && (module.exports = {
|
|
739
|
-
|
|
1345
|
+
useApiAsync,
|
|
1346
|
+
useChat,
|
|
1347
|
+
useChatHistory,
|
|
1348
|
+
useChatMonitoring,
|
|
1349
|
+
useHumanAgentSessions,
|
|
1350
|
+
useOptionsRef
|
|
740
1351
|
});
|
|
741
1352
|
//# sourceMappingURL=index.js.map
|