@elqnt/chat 3.0.2 → 3.1.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 +27 -0
- package/dist/api/index.d.mts +53 -2
- package/dist/api/index.d.ts +53 -2
- package/dist/api/index.js +79 -9
- package/dist/api/index.js.map +1 -1
- package/dist/api/index.mjs +65 -9
- package/dist/api/index.mjs.map +1 -1
- package/dist/hooks/index.d.mts +134 -2
- package/dist/hooks/index.d.ts +134 -2
- package/dist/hooks/index.js +713 -8
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +706 -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 +722 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +712 -7
- package/dist/index.mjs.map +1 -1
- package/dist/models/index.d.mts +78 -3
- package/dist/models/index.d.ts +78 -3
- package/dist/models/index.js +9 -0
- package/dist/models/index.js.map +1 -1
- package/dist/models/index.mjs +6 -0
- package/dist/models/index.mjs.map +1 -1
- package/dist/transport/index.js +2 -1
- package/dist/transport/index.js.map +1 -1
- package/dist/transport/index.mjs +2 -1
- package/dist/transport/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/hooks/index.js
CHANGED
|
@@ -21,7 +21,13 @@ 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
|
+
useMemory: () => useMemory,
|
|
30
|
+
useOptionsRef: () => useOptionsRef
|
|
25
31
|
});
|
|
26
32
|
module.exports = __toCommonJS(hooks_exports);
|
|
27
33
|
|
|
@@ -257,6 +263,383 @@ function createSSETransport(options = {}) {
|
|
|
257
263
|
state = "disconnected";
|
|
258
264
|
retryCount = 0;
|
|
259
265
|
},
|
|
266
|
+
async send(event) {
|
|
267
|
+
if (!config) {
|
|
268
|
+
throw new Error("Transport not connected");
|
|
269
|
+
}
|
|
270
|
+
switch (event.type) {
|
|
271
|
+
case "message":
|
|
272
|
+
await sendRest("send", {
|
|
273
|
+
orgId: event.orgId,
|
|
274
|
+
chatKey: event.chatKey,
|
|
275
|
+
userId: event.userId,
|
|
276
|
+
message: event.message,
|
|
277
|
+
...event.data ? { data: event.data } : {}
|
|
278
|
+
});
|
|
279
|
+
break;
|
|
280
|
+
case "typing":
|
|
281
|
+
await sendRest("typing", {
|
|
282
|
+
orgId: event.orgId,
|
|
283
|
+
chatKey: event.chatKey,
|
|
284
|
+
userId: event.userId,
|
|
285
|
+
typing: true
|
|
286
|
+
});
|
|
287
|
+
break;
|
|
288
|
+
case "stopped_typing":
|
|
289
|
+
await sendRest("typing", {
|
|
290
|
+
orgId: event.orgId,
|
|
291
|
+
chatKey: event.chatKey,
|
|
292
|
+
userId: event.userId,
|
|
293
|
+
typing: false
|
|
294
|
+
});
|
|
295
|
+
break;
|
|
296
|
+
case "load_chat":
|
|
297
|
+
await sendRest("load", {
|
|
298
|
+
orgId: event.orgId,
|
|
299
|
+
chatKey: event.chatKey,
|
|
300
|
+
userId: event.userId
|
|
301
|
+
});
|
|
302
|
+
break;
|
|
303
|
+
case "new_chat":
|
|
304
|
+
await sendRest("create", {
|
|
305
|
+
orgId: event.orgId,
|
|
306
|
+
userId: event.userId,
|
|
307
|
+
metadata: event.data
|
|
308
|
+
});
|
|
309
|
+
break;
|
|
310
|
+
case "end_chat":
|
|
311
|
+
await sendRest("end", {
|
|
312
|
+
orgId: event.orgId,
|
|
313
|
+
chatKey: event.chatKey,
|
|
314
|
+
userId: event.userId,
|
|
315
|
+
data: event.data
|
|
316
|
+
});
|
|
317
|
+
break;
|
|
318
|
+
default:
|
|
319
|
+
await sendRest("event", {
|
|
320
|
+
type: event.type,
|
|
321
|
+
orgId: event.orgId,
|
|
322
|
+
chatKey: event.chatKey,
|
|
323
|
+
userId: event.userId,
|
|
324
|
+
data: event.data
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
metrics.messagesSent++;
|
|
328
|
+
},
|
|
329
|
+
async sendMessage(message) {
|
|
330
|
+
if (!config) {
|
|
331
|
+
throw new Error("Transport not connected");
|
|
332
|
+
}
|
|
333
|
+
await sendRest("send", {
|
|
334
|
+
orgId: config.orgId,
|
|
335
|
+
chatKey: config.chatKey,
|
|
336
|
+
userId: config.userId,
|
|
337
|
+
message
|
|
338
|
+
});
|
|
339
|
+
metrics.messagesSent++;
|
|
340
|
+
},
|
|
341
|
+
async createChat(options2) {
|
|
342
|
+
if (!config) {
|
|
343
|
+
throw new Error("Transport not connected");
|
|
344
|
+
}
|
|
345
|
+
const response = await sendRest("create", {
|
|
346
|
+
orgId: options2.orgId,
|
|
347
|
+
userId: options2.userId,
|
|
348
|
+
metadata: options2.metadata
|
|
349
|
+
});
|
|
350
|
+
if (!response?.chatKey) {
|
|
351
|
+
throw new Error("Failed to create chat: no chatKey returned");
|
|
352
|
+
}
|
|
353
|
+
return { chatKey: response.chatKey };
|
|
354
|
+
},
|
|
355
|
+
async loadChatData(options2) {
|
|
356
|
+
if (!config) {
|
|
357
|
+
throw new Error("Transport not connected");
|
|
358
|
+
}
|
|
359
|
+
const response = await sendRest("load", {
|
|
360
|
+
orgId: options2.orgId,
|
|
361
|
+
chatKey: options2.chatKey,
|
|
362
|
+
userId: options2.userId
|
|
363
|
+
});
|
|
364
|
+
if (!response?.chat) {
|
|
365
|
+
throw new Error("Failed to load chat: no chat data returned");
|
|
366
|
+
}
|
|
367
|
+
return {
|
|
368
|
+
chat: response.chat,
|
|
369
|
+
agentId: response.agentId
|
|
370
|
+
};
|
|
371
|
+
},
|
|
372
|
+
onMessage(handler) {
|
|
373
|
+
globalHandlers.add(handler);
|
|
374
|
+
return () => globalHandlers.delete(handler);
|
|
375
|
+
},
|
|
376
|
+
on(eventType, handler) {
|
|
377
|
+
if (!typeHandlers.has(eventType)) {
|
|
378
|
+
typeHandlers.set(eventType, /* @__PURE__ */ new Set());
|
|
379
|
+
}
|
|
380
|
+
typeHandlers.get(eventType).add(handler);
|
|
381
|
+
return () => {
|
|
382
|
+
const handlers = typeHandlers.get(eventType);
|
|
383
|
+
if (handlers) {
|
|
384
|
+
handlers.delete(handler);
|
|
385
|
+
if (handlers.size === 0) {
|
|
386
|
+
typeHandlers.delete(eventType);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
},
|
|
391
|
+
getState() {
|
|
392
|
+
return state;
|
|
393
|
+
},
|
|
394
|
+
getMetrics() {
|
|
395
|
+
return { ...metrics };
|
|
396
|
+
},
|
|
397
|
+
getError() {
|
|
398
|
+
return error;
|
|
399
|
+
},
|
|
400
|
+
clearError() {
|
|
401
|
+
error = void 0;
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
return transport;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// transport/sse-fetch.ts
|
|
408
|
+
function createFetchSSETransport(options = {}) {
|
|
409
|
+
const {
|
|
410
|
+
retryConfig = DEFAULT_RETRY_CONFIG,
|
|
411
|
+
debug = false,
|
|
412
|
+
logger = createLogger(debug),
|
|
413
|
+
customFetch = fetch
|
|
414
|
+
} = options;
|
|
415
|
+
let abortController;
|
|
416
|
+
let config;
|
|
417
|
+
let state = "disconnected";
|
|
418
|
+
let error;
|
|
419
|
+
let retryCount = 0;
|
|
420
|
+
let reconnectTimeout;
|
|
421
|
+
let intentionalDisconnect = false;
|
|
422
|
+
const metrics = {
|
|
423
|
+
latency: 0,
|
|
424
|
+
messagesSent: 0,
|
|
425
|
+
messagesReceived: 0,
|
|
426
|
+
messagesQueued: 0,
|
|
427
|
+
reconnectCount: 0,
|
|
428
|
+
transportType: "sse-fetch"
|
|
429
|
+
};
|
|
430
|
+
const globalHandlers = /* @__PURE__ */ new Set();
|
|
431
|
+
const typeHandlers = /* @__PURE__ */ new Map();
|
|
432
|
+
function emit(event) {
|
|
433
|
+
metrics.messagesReceived++;
|
|
434
|
+
metrics.lastMessageAt = Date.now();
|
|
435
|
+
globalHandlers.forEach((handler) => {
|
|
436
|
+
try {
|
|
437
|
+
handler(event);
|
|
438
|
+
} catch (err) {
|
|
439
|
+
logger.error("Error in message handler:", err);
|
|
440
|
+
}
|
|
441
|
+
});
|
|
442
|
+
const handlers = typeHandlers.get(event.type);
|
|
443
|
+
if (handlers) {
|
|
444
|
+
handlers.forEach((handler) => {
|
|
445
|
+
try {
|
|
446
|
+
handler(event);
|
|
447
|
+
} catch (err) {
|
|
448
|
+
logger.error(`Error in ${event.type} handler:`, err);
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
async function sendRest(endpoint, body) {
|
|
454
|
+
if (!config) {
|
|
455
|
+
throw new Error("Transport not connected");
|
|
456
|
+
}
|
|
457
|
+
const url = `${config.baseUrl}/${endpoint}`;
|
|
458
|
+
logger.debug(`POST ${endpoint}`, body);
|
|
459
|
+
const response = await customFetch(url, {
|
|
460
|
+
method: "POST",
|
|
461
|
+
headers: { "Content-Type": "application/json" },
|
|
462
|
+
body: JSON.stringify(body)
|
|
463
|
+
});
|
|
464
|
+
if (!response.ok) {
|
|
465
|
+
const errorText = await response.text();
|
|
466
|
+
throw new Error(`API error: ${response.status} - ${errorText}`);
|
|
467
|
+
}
|
|
468
|
+
const json = await response.json();
|
|
469
|
+
if (json && typeof json === "object" && "data" in json) {
|
|
470
|
+
return json.data;
|
|
471
|
+
}
|
|
472
|
+
return json;
|
|
473
|
+
}
|
|
474
|
+
function parseSSEChunk(chunk) {
|
|
475
|
+
const events = [];
|
|
476
|
+
const lines = chunk.split("\n");
|
|
477
|
+
let eventType = "message";
|
|
478
|
+
let data = "";
|
|
479
|
+
for (const line of lines) {
|
|
480
|
+
if (line.startsWith("event:")) {
|
|
481
|
+
eventType = line.slice(6).trim();
|
|
482
|
+
} else if (line.startsWith("data:")) {
|
|
483
|
+
data = line.slice(5).trim();
|
|
484
|
+
} else if (line === "" && data) {
|
|
485
|
+
try {
|
|
486
|
+
const parsed = JSON.parse(data);
|
|
487
|
+
events.push(parsed);
|
|
488
|
+
} catch {
|
|
489
|
+
logger.warn("Failed to parse SSE data:", data);
|
|
490
|
+
}
|
|
491
|
+
eventType = "message";
|
|
492
|
+
data = "";
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
return events;
|
|
496
|
+
}
|
|
497
|
+
async function startStream(cfg) {
|
|
498
|
+
const url = `${cfg.baseUrl}/stream?orgId=${cfg.orgId}&userId=${cfg.userId}&clientType=${cfg.clientType}${cfg.chatKey ? `&chatId=${cfg.chatKey}` : ""}`;
|
|
499
|
+
logger.debug("Connecting to:", url);
|
|
500
|
+
abortController = new AbortController();
|
|
501
|
+
const response = await customFetch(url, {
|
|
502
|
+
method: "GET",
|
|
503
|
+
headers: {
|
|
504
|
+
Accept: "text/event-stream",
|
|
505
|
+
"Cache-Control": "no-cache"
|
|
506
|
+
},
|
|
507
|
+
signal: abortController.signal
|
|
508
|
+
});
|
|
509
|
+
if (!response.ok) {
|
|
510
|
+
throw new Error(`Stream connection failed: ${response.status}`);
|
|
511
|
+
}
|
|
512
|
+
if (!response.body) {
|
|
513
|
+
throw new Error("Response body is null - ReadableStream not supported");
|
|
514
|
+
}
|
|
515
|
+
const reader = response.body.getReader();
|
|
516
|
+
const decoder = new TextDecoder();
|
|
517
|
+
let buffer = "";
|
|
518
|
+
const readStream = async () => {
|
|
519
|
+
try {
|
|
520
|
+
while (true) {
|
|
521
|
+
const { done, value } = await reader.read();
|
|
522
|
+
if (done) {
|
|
523
|
+
logger.info("Stream ended");
|
|
524
|
+
break;
|
|
525
|
+
}
|
|
526
|
+
buffer += decoder.decode(value, { stream: true });
|
|
527
|
+
const lastNewline = buffer.lastIndexOf("\n\n");
|
|
528
|
+
if (lastNewline !== -1) {
|
|
529
|
+
const complete = buffer.slice(0, lastNewline + 2);
|
|
530
|
+
buffer = buffer.slice(lastNewline + 2);
|
|
531
|
+
const events = parseSSEChunk(complete);
|
|
532
|
+
events.forEach(emit);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
} catch (err) {
|
|
536
|
+
if (err.name === "AbortError") {
|
|
537
|
+
logger.debug("Stream aborted");
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
logger.error("Stream error:", err);
|
|
541
|
+
throw err;
|
|
542
|
+
}
|
|
543
|
+
if (!intentionalDisconnect) {
|
|
544
|
+
state = "disconnected";
|
|
545
|
+
scheduleReconnect();
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
readStream().catch((err) => {
|
|
549
|
+
if (!intentionalDisconnect) {
|
|
550
|
+
error = {
|
|
551
|
+
code: "CONNECTION_FAILED",
|
|
552
|
+
message: err.message,
|
|
553
|
+
retryable: true,
|
|
554
|
+
timestamp: Date.now(),
|
|
555
|
+
originalError: err
|
|
556
|
+
};
|
|
557
|
+
metrics.lastError = error;
|
|
558
|
+
state = "disconnected";
|
|
559
|
+
scheduleReconnect();
|
|
560
|
+
}
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
function scheduleReconnect() {
|
|
564
|
+
if (intentionalDisconnect || !config) return;
|
|
565
|
+
const maxRetries = retryConfig.maxRetries ?? DEFAULT_RETRY_CONFIG.maxRetries;
|
|
566
|
+
if (retryCount >= maxRetries) {
|
|
567
|
+
logger.error(`Max retries (${maxRetries}) exceeded`);
|
|
568
|
+
error = {
|
|
569
|
+
code: "CONNECTION_FAILED",
|
|
570
|
+
message: `Max retries (${maxRetries}) exceeded`,
|
|
571
|
+
retryable: false,
|
|
572
|
+
timestamp: Date.now()
|
|
573
|
+
};
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
const interval = calculateRetryInterval(retryCount, retryConfig);
|
|
577
|
+
retryCount++;
|
|
578
|
+
metrics.reconnectCount++;
|
|
579
|
+
logger.info(`Reconnecting in ${interval}ms (attempt ${retryCount})`);
|
|
580
|
+
state = "reconnecting";
|
|
581
|
+
reconnectTimeout = setTimeout(() => {
|
|
582
|
+
if (config) {
|
|
583
|
+
transport.connect(config).catch((err) => {
|
|
584
|
+
logger.error("Reconnect failed:", err);
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
}, interval);
|
|
588
|
+
}
|
|
589
|
+
const transport = {
|
|
590
|
+
async connect(cfg) {
|
|
591
|
+
config = cfg;
|
|
592
|
+
intentionalDisconnect = false;
|
|
593
|
+
if (abortController) {
|
|
594
|
+
abortController.abort();
|
|
595
|
+
abortController = void 0;
|
|
596
|
+
}
|
|
597
|
+
if (reconnectTimeout) {
|
|
598
|
+
clearTimeout(reconnectTimeout);
|
|
599
|
+
reconnectTimeout = void 0;
|
|
600
|
+
}
|
|
601
|
+
state = retryCount > 0 ? "reconnecting" : "connecting";
|
|
602
|
+
const connectionStart = Date.now();
|
|
603
|
+
try {
|
|
604
|
+
await startStream(cfg);
|
|
605
|
+
const connectionTime = Date.now() - connectionStart;
|
|
606
|
+
logger.info(`Connected in ${connectionTime}ms`);
|
|
607
|
+
state = "connected";
|
|
608
|
+
error = void 0;
|
|
609
|
+
retryCount = 0;
|
|
610
|
+
metrics.connectedAt = Date.now();
|
|
611
|
+
metrics.latency = connectionTime;
|
|
612
|
+
} catch (err) {
|
|
613
|
+
const connectError = {
|
|
614
|
+
code: "CONNECTION_FAILED",
|
|
615
|
+
message: err.message,
|
|
616
|
+
retryable: true,
|
|
617
|
+
timestamp: Date.now(),
|
|
618
|
+
originalError: err
|
|
619
|
+
};
|
|
620
|
+
error = connectError;
|
|
621
|
+
metrics.lastError = connectError;
|
|
622
|
+
state = "disconnected";
|
|
623
|
+
if (!intentionalDisconnect) {
|
|
624
|
+
scheduleReconnect();
|
|
625
|
+
}
|
|
626
|
+
throw connectError;
|
|
627
|
+
}
|
|
628
|
+
},
|
|
629
|
+
disconnect(intentional = true) {
|
|
630
|
+
logger.info("Disconnecting", { intentional });
|
|
631
|
+
intentionalDisconnect = intentional;
|
|
632
|
+
if (reconnectTimeout) {
|
|
633
|
+
clearTimeout(reconnectTimeout);
|
|
634
|
+
reconnectTimeout = void 0;
|
|
635
|
+
}
|
|
636
|
+
if (abortController) {
|
|
637
|
+
abortController.abort();
|
|
638
|
+
abortController = void 0;
|
|
639
|
+
}
|
|
640
|
+
state = "disconnected";
|
|
641
|
+
retryCount = 0;
|
|
642
|
+
},
|
|
260
643
|
async send(event) {
|
|
261
644
|
if (!config) {
|
|
262
645
|
throw new Error("Transport not connected");
|
|
@@ -399,6 +782,16 @@ function createSSETransport(options = {}) {
|
|
|
399
782
|
|
|
400
783
|
// hooks/use-chat.ts
|
|
401
784
|
function getDefaultTransport(type, debug) {
|
|
785
|
+
if (type === "sse-fetch") {
|
|
786
|
+
return createFetchSSETransport({ debug });
|
|
787
|
+
}
|
|
788
|
+
if (type === "sse") {
|
|
789
|
+
return createSSETransport({ debug });
|
|
790
|
+
}
|
|
791
|
+
const isReactNative = typeof navigator !== "undefined" && navigator.product === "ReactNative";
|
|
792
|
+
if (isReactNative || typeof EventSource === "undefined") {
|
|
793
|
+
return createFetchSSETransport({ debug });
|
|
794
|
+
}
|
|
402
795
|
return createSSETransport({ debug });
|
|
403
796
|
}
|
|
404
797
|
function useChat(options) {
|
|
@@ -509,6 +902,7 @@ function useChat(options) {
|
|
|
509
902
|
throw err;
|
|
510
903
|
}
|
|
511
904
|
setConnectionState("connecting");
|
|
905
|
+
onConnectionChange?.("connecting");
|
|
512
906
|
try {
|
|
513
907
|
const unsubscribe = transport.onMessage(handleEvent);
|
|
514
908
|
await transport.connect({
|
|
@@ -527,6 +921,7 @@ function useChat(options) {
|
|
|
527
921
|
} catch (err) {
|
|
528
922
|
const transportError = err;
|
|
529
923
|
setConnectionState("disconnected");
|
|
924
|
+
onConnectionChange?.("disconnected");
|
|
530
925
|
setError(transportError);
|
|
531
926
|
onErrorRef.current?.(transportError);
|
|
532
927
|
throw err;
|
|
@@ -556,7 +951,7 @@ function useChat(options) {
|
|
|
556
951
|
},
|
|
557
952
|
[orgId, userId]
|
|
558
953
|
);
|
|
559
|
-
const
|
|
954
|
+
const loadChat2 = (0, import_react.useCallback)(
|
|
560
955
|
async (key) => {
|
|
561
956
|
const transport = transportRef.current;
|
|
562
957
|
if (!transport) {
|
|
@@ -609,7 +1004,7 @@ function useChat(options) {
|
|
|
609
1004
|
},
|
|
610
1005
|
[orgId, chatKey, userId]
|
|
611
1006
|
);
|
|
612
|
-
const
|
|
1007
|
+
const endChat2 = (0, import_react.useCallback)(
|
|
613
1008
|
async (reason) => {
|
|
614
1009
|
const transport = transportRef.current;
|
|
615
1010
|
if (!transport) {
|
|
@@ -631,7 +1026,7 @@ function useChat(options) {
|
|
|
631
1026
|
},
|
|
632
1027
|
[orgId, chatKey, userId]
|
|
633
1028
|
);
|
|
634
|
-
const
|
|
1029
|
+
const sendEvent2 = (0, import_react.useCallback)(
|
|
635
1030
|
async (event) => {
|
|
636
1031
|
const transport = transportRef.current;
|
|
637
1032
|
if (!transport) {
|
|
@@ -716,10 +1111,10 @@ function useChat(options) {
|
|
|
716
1111
|
isConnected,
|
|
717
1112
|
// Chat operations
|
|
718
1113
|
startChat,
|
|
719
|
-
loadChat,
|
|
1114
|
+
loadChat: loadChat2,
|
|
720
1115
|
sendMessage,
|
|
721
|
-
sendEvent,
|
|
722
|
-
endChat,
|
|
1116
|
+
sendEvent: sendEvent2,
|
|
1117
|
+
endChat: endChat2,
|
|
723
1118
|
// Typing
|
|
724
1119
|
startTyping,
|
|
725
1120
|
stopTyping,
|
|
@@ -734,8 +1129,318 @@ function useChat(options) {
|
|
|
734
1129
|
clearError
|
|
735
1130
|
};
|
|
736
1131
|
}
|
|
1132
|
+
|
|
1133
|
+
// hooks/use-chat-history.ts
|
|
1134
|
+
var import_react3 = require("react");
|
|
1135
|
+
|
|
1136
|
+
// api/index.ts
|
|
1137
|
+
var import_browser2 = require("@elqnt/api-client/browser");
|
|
1138
|
+
|
|
1139
|
+
// api/memory.ts
|
|
1140
|
+
var import_browser = require("@elqnt/api-client/browser");
|
|
1141
|
+
var patchProfileApi = (patch, o) => (0, import_browser.browserApiRequest)("/api/v1/memory/profile", { method: "PATCH", body: patch, ...o });
|
|
1142
|
+
var replaceProfileApi = (p, o) => (0, import_browser.browserApiRequest)("/api/v1/memory/profile", { method: "PUT", body: p, ...o });
|
|
1143
|
+
var clearProfileApi = (o) => (0, import_browser.browserApiRequest)("/api/v1/memory/profile", { method: "DELETE", ...o });
|
|
1144
|
+
var deleteContactApi = (name, o) => (0, import_browser.browserApiRequest)(`/api/v1/memory/profile/contacts/${encodeURIComponent(name)}`, { method: "DELETE", ...o });
|
|
1145
|
+
var deleteNoteApi = (index, o) => (0, import_browser.browserApiRequest)(`/api/v1/memory/profile/notes/${index}`, { method: "DELETE", ...o });
|
|
1146
|
+
var clearSummaryApi = (chatKey, o) => (0, import_browser.browserApiRequest)(`/api/v1/chats/${chatKey}/summary`, { method: "DELETE", ...o });
|
|
1147
|
+
var regenerateSummaryApi = (chatKey, o) => (0, import_browser.browserApiRequest)(`/api/v1/chats/${chatKey}/summary/regenerate`, { method: "POST", ...o });
|
|
1148
|
+
|
|
1149
|
+
// api/index.ts
|
|
1150
|
+
async function getChatHistoryApi(options) {
|
|
1151
|
+
return (0, import_browser2.browserApiRequest)("/api/v1/chats", {
|
|
1152
|
+
method: "POST",
|
|
1153
|
+
body: {
|
|
1154
|
+
limit: options.limit || 15,
|
|
1155
|
+
offset: options.offset || 0,
|
|
1156
|
+
...options.skipCache ? { skipCache: true } : {}
|
|
1157
|
+
},
|
|
1158
|
+
...options
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
async function getChatApi(chatKey, options) {
|
|
1162
|
+
return (0, import_browser2.browserApiRequest)(`/api/v1/chats/${chatKey}`, {
|
|
1163
|
+
method: "GET",
|
|
1164
|
+
...options
|
|
1165
|
+
});
|
|
1166
|
+
}
|
|
1167
|
+
async function updateChatApi(chatKey, updates, options) {
|
|
1168
|
+
return (0, import_browser2.browserApiRequest)(`/api/v1/chats/${chatKey}`, {
|
|
1169
|
+
method: "PATCH",
|
|
1170
|
+
body: updates,
|
|
1171
|
+
...options
|
|
1172
|
+
});
|
|
1173
|
+
}
|
|
1174
|
+
async function deleteChatApi(chatKey, options) {
|
|
1175
|
+
return (0, import_browser2.browserApiRequest)(`/api/v1/chats/${chatKey}`, {
|
|
1176
|
+
method: "DELETE",
|
|
1177
|
+
...options
|
|
1178
|
+
});
|
|
1179
|
+
}
|
|
1180
|
+
async function getActiveChatsCountApi(options) {
|
|
1181
|
+
return (0, import_browser2.browserApiRequest)("/api/v1/chats/active/count", {
|
|
1182
|
+
method: "GET",
|
|
1183
|
+
...options
|
|
1184
|
+
});
|
|
1185
|
+
}
|
|
1186
|
+
async function getActiveChatsApi(options) {
|
|
1187
|
+
const params = new URLSearchParams();
|
|
1188
|
+
if (options.pastHours) params.set("pastHours", String(options.pastHours));
|
|
1189
|
+
const queryString = params.toString();
|
|
1190
|
+
return (0, import_browser2.browserApiRequest)(`/api/v1/chats/active${queryString ? `?${queryString}` : ""}`, {
|
|
1191
|
+
method: "GET",
|
|
1192
|
+
...options
|
|
1193
|
+
});
|
|
1194
|
+
}
|
|
1195
|
+
async function getWaitingChatsCountApi(options) {
|
|
1196
|
+
return (0, import_browser2.browserApiRequest)("/api/v1/chats/waiting/count", {
|
|
1197
|
+
method: "GET",
|
|
1198
|
+
...options
|
|
1199
|
+
});
|
|
1200
|
+
}
|
|
1201
|
+
async function getChatsByUserApi(userEmail, options) {
|
|
1202
|
+
return (0, import_browser2.browserApiRequest)(`/api/v1/chats/user/${encodeURIComponent(userEmail)}`, {
|
|
1203
|
+
method: "GET",
|
|
1204
|
+
...options
|
|
1205
|
+
});
|
|
1206
|
+
}
|
|
1207
|
+
async function listQueuesApi(options) {
|
|
1208
|
+
return (0, import_browser2.browserApiRequest)("/api/v1/queues", {
|
|
1209
|
+
method: "GET",
|
|
1210
|
+
...options
|
|
1211
|
+
});
|
|
1212
|
+
}
|
|
1213
|
+
async function getOnlineSessionsApi(options) {
|
|
1214
|
+
return (0, import_browser2.browserApiRequest)("/api/v1/agents/sessions/online", {
|
|
1215
|
+
method: "GET",
|
|
1216
|
+
...options
|
|
1217
|
+
});
|
|
1218
|
+
}
|
|
1219
|
+
async function getAgentSessionApi(agentId, options) {
|
|
1220
|
+
return (0, import_browser2.browserApiRequest)(`/api/v1/agents/sessions/${agentId}`, {
|
|
1221
|
+
method: "GET",
|
|
1222
|
+
...options
|
|
1223
|
+
});
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
// hooks/use-chat-history.ts
|
|
1227
|
+
var import_hooks = require("@elqnt/api-client/hooks");
|
|
1228
|
+
|
|
1229
|
+
// hooks/use-options-ref.ts
|
|
1230
|
+
var import_react2 = require("react");
|
|
1231
|
+
function useOptionsRef(options) {
|
|
1232
|
+
const optionsRef = (0, import_react2.useRef)(options);
|
|
1233
|
+
(0, import_react2.useEffect)(() => {
|
|
1234
|
+
optionsRef.current = options;
|
|
1235
|
+
}, [options]);
|
|
1236
|
+
return optionsRef;
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
// hooks/use-chat-history.ts
|
|
1240
|
+
function useChatHistory(options) {
|
|
1241
|
+
const optionsRef = useOptionsRef(options);
|
|
1242
|
+
const { execute: getChatHistory, loading: listLoading, error: listError } = (0, import_hooks.useApiAsync)(
|
|
1243
|
+
(params) => getChatHistoryApi({ ...optionsRef.current, ...params }),
|
|
1244
|
+
(data) => ({
|
|
1245
|
+
chats: data.chats,
|
|
1246
|
+
total: data.total,
|
|
1247
|
+
hasMore: data.hasMore
|
|
1248
|
+
}),
|
|
1249
|
+
{ chats: [], total: 0, hasMore: false }
|
|
1250
|
+
);
|
|
1251
|
+
const { execute: getChat, loading: getLoading, error: getError } = (0, import_hooks.useApiAsync)(
|
|
1252
|
+
(chatKey) => getChatApi(chatKey, optionsRef.current),
|
|
1253
|
+
(data) => data.chat || null,
|
|
1254
|
+
null
|
|
1255
|
+
);
|
|
1256
|
+
const { execute: updateChat, loading: updateLoading, error: updateError } = (0, import_hooks.useApiAsync)(
|
|
1257
|
+
(chatKey, updates) => updateChatApi(chatKey, updates, optionsRef.current),
|
|
1258
|
+
(data) => !!data.chatKey,
|
|
1259
|
+
false
|
|
1260
|
+
);
|
|
1261
|
+
const { execute: deleteChat, loading: deleteLoading, error: deleteError } = (0, import_hooks.useApiAsync)(
|
|
1262
|
+
(chatKey) => deleteChatApi(chatKey, optionsRef.current),
|
|
1263
|
+
(data) => data.success,
|
|
1264
|
+
false
|
|
1265
|
+
);
|
|
1266
|
+
const { execute: getChatsByUser, loading: userChatsLoading, error: userChatsError } = (0, import_hooks.useApiAsync)(
|
|
1267
|
+
(userEmail) => getChatsByUserApi(userEmail, optionsRef.current),
|
|
1268
|
+
(data) => data.chats,
|
|
1269
|
+
[]
|
|
1270
|
+
);
|
|
1271
|
+
const loading = listLoading || getLoading || updateLoading || deleteLoading || userChatsLoading;
|
|
1272
|
+
const error = listError || getError || updateError || deleteError || userChatsError;
|
|
1273
|
+
return (0, import_react3.useMemo)(
|
|
1274
|
+
() => ({
|
|
1275
|
+
loading,
|
|
1276
|
+
error,
|
|
1277
|
+
getChatHistory,
|
|
1278
|
+
getChat,
|
|
1279
|
+
updateChat,
|
|
1280
|
+
deleteChat,
|
|
1281
|
+
getChatsByUser
|
|
1282
|
+
}),
|
|
1283
|
+
[loading, error, getChatHistory, getChat, updateChat, deleteChat, getChatsByUser]
|
|
1284
|
+
);
|
|
1285
|
+
}
|
|
1286
|
+
|
|
1287
|
+
// hooks/use-chat-monitoring.ts
|
|
1288
|
+
var import_react4 = require("react");
|
|
1289
|
+
var import_hooks2 = require("@elqnt/api-client/hooks");
|
|
1290
|
+
function useChatMonitoring(options) {
|
|
1291
|
+
const optionsRef = useOptionsRef(options);
|
|
1292
|
+
const { execute: getActiveChats, loading: activeLoading, error: activeError } = (0, import_hooks2.useApiAsync)(
|
|
1293
|
+
(pastHours) => getActiveChatsApi({ ...optionsRef.current, pastHours }),
|
|
1294
|
+
(data) => data.chats,
|
|
1295
|
+
[]
|
|
1296
|
+
);
|
|
1297
|
+
const { execute: getActiveChatsCount, loading: activeCountLoading, error: activeCountError } = (0, import_hooks2.useApiAsync)(
|
|
1298
|
+
() => getActiveChatsCountApi(optionsRef.current),
|
|
1299
|
+
(data) => data.count,
|
|
1300
|
+
0
|
|
1301
|
+
);
|
|
1302
|
+
const { execute: getWaitingChatsCount, loading: waitingCountLoading, error: waitingCountError } = (0, import_hooks2.useApiAsync)(
|
|
1303
|
+
() => getWaitingChatsCountApi(optionsRef.current),
|
|
1304
|
+
(data) => data.count,
|
|
1305
|
+
0
|
|
1306
|
+
);
|
|
1307
|
+
const { execute: listQueues, loading: queuesLoading, error: queuesError } = (0, import_hooks2.useApiAsync)(
|
|
1308
|
+
() => listQueuesApi(optionsRef.current),
|
|
1309
|
+
(data) => data.queues,
|
|
1310
|
+
[]
|
|
1311
|
+
);
|
|
1312
|
+
const loading = activeLoading || activeCountLoading || waitingCountLoading || queuesLoading;
|
|
1313
|
+
const error = activeError || activeCountError || waitingCountError || queuesError;
|
|
1314
|
+
return (0, import_react4.useMemo)(
|
|
1315
|
+
() => ({
|
|
1316
|
+
loading,
|
|
1317
|
+
error,
|
|
1318
|
+
getActiveChats,
|
|
1319
|
+
getActiveChatsCount,
|
|
1320
|
+
getWaitingChatsCount,
|
|
1321
|
+
listQueues
|
|
1322
|
+
}),
|
|
1323
|
+
[loading, error, getActiveChats, getActiveChatsCount, getWaitingChatsCount, listQueues]
|
|
1324
|
+
);
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
// hooks/use-human-agent-sessions.ts
|
|
1328
|
+
var import_react5 = require("react");
|
|
1329
|
+
var import_hooks3 = require("@elqnt/api-client/hooks");
|
|
1330
|
+
function useHumanAgentSessions(options) {
|
|
1331
|
+
const optionsRef = useOptionsRef(options);
|
|
1332
|
+
const { execute: getOnlineSessions, loading: onlineLoading, error: onlineError } = (0, import_hooks3.useApiAsync)(
|
|
1333
|
+
() => getOnlineSessionsApi(optionsRef.current),
|
|
1334
|
+
(data) => data.sessions,
|
|
1335
|
+
[]
|
|
1336
|
+
);
|
|
1337
|
+
const { execute: getAgentSession, loading: sessionLoading, error: sessionError } = (0, import_hooks3.useApiAsync)(
|
|
1338
|
+
(agentId) => getAgentSessionApi(agentId, optionsRef.current),
|
|
1339
|
+
(data) => data.session || null,
|
|
1340
|
+
null
|
|
1341
|
+
);
|
|
1342
|
+
const loading = onlineLoading || sessionLoading;
|
|
1343
|
+
const error = onlineError || sessionError;
|
|
1344
|
+
return (0, import_react5.useMemo)(
|
|
1345
|
+
() => ({
|
|
1346
|
+
loading,
|
|
1347
|
+
error,
|
|
1348
|
+
getOnlineSessions,
|
|
1349
|
+
getAgentSession
|
|
1350
|
+
}),
|
|
1351
|
+
[loading, error, getOnlineSessions, getAgentSession]
|
|
1352
|
+
);
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
// hooks/use-memory.ts
|
|
1356
|
+
var import_react6 = require("react");
|
|
1357
|
+
function useMemory(options, initialProfile = null) {
|
|
1358
|
+
const [profile, setProfile] = (0, import_react6.useState)(initialProfile);
|
|
1359
|
+
const [loading, setLoading] = (0, import_react6.useState)(false);
|
|
1360
|
+
const requestCountRef = (0, import_react6.useRef)(0);
|
|
1361
|
+
const runProfileMutation = (0, import_react6.useCallback)(
|
|
1362
|
+
async (fn) => {
|
|
1363
|
+
requestCountRef.current += 1;
|
|
1364
|
+
setLoading(true);
|
|
1365
|
+
try {
|
|
1366
|
+
const response = await fn();
|
|
1367
|
+
if (!response.error && response.data) {
|
|
1368
|
+
setProfile(response.data);
|
|
1369
|
+
return response.data;
|
|
1370
|
+
}
|
|
1371
|
+
return null;
|
|
1372
|
+
} catch {
|
|
1373
|
+
return null;
|
|
1374
|
+
} finally {
|
|
1375
|
+
requestCountRef.current -= 1;
|
|
1376
|
+
if (requestCountRef.current === 0) {
|
|
1377
|
+
setLoading(false);
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
},
|
|
1381
|
+
[]
|
|
1382
|
+
);
|
|
1383
|
+
const optionsRef = (0, import_react6.useRef)(options);
|
|
1384
|
+
optionsRef.current = options;
|
|
1385
|
+
const patchProfile = (0, import_react6.useCallback)(
|
|
1386
|
+
(patch) => runProfileMutation(() => patchProfileApi(patch, optionsRef.current)),
|
|
1387
|
+
[runProfileMutation]
|
|
1388
|
+
);
|
|
1389
|
+
const replaceProfile = (0, import_react6.useCallback)(
|
|
1390
|
+
(p) => runProfileMutation(() => replaceProfileApi(p, optionsRef.current)),
|
|
1391
|
+
[runProfileMutation]
|
|
1392
|
+
);
|
|
1393
|
+
const clearProfile = (0, import_react6.useCallback)(
|
|
1394
|
+
() => runProfileMutation(() => clearProfileApi(optionsRef.current)),
|
|
1395
|
+
[runProfileMutation]
|
|
1396
|
+
);
|
|
1397
|
+
const deleteContact = (0, import_react6.useCallback)(
|
|
1398
|
+
(name) => runProfileMutation(() => deleteContactApi(name, optionsRef.current)),
|
|
1399
|
+
[runProfileMutation]
|
|
1400
|
+
);
|
|
1401
|
+
const deleteNote = (0, import_react6.useCallback)(
|
|
1402
|
+
(index) => runProfileMutation(() => deleteNoteApi(index, optionsRef.current)),
|
|
1403
|
+
[runProfileMutation]
|
|
1404
|
+
);
|
|
1405
|
+
const clearSummary = (0, import_react6.useCallback)(
|
|
1406
|
+
async (chatKey) => {
|
|
1407
|
+
await clearSummaryApi(chatKey, optionsRef.current);
|
|
1408
|
+
},
|
|
1409
|
+
[]
|
|
1410
|
+
);
|
|
1411
|
+
const regenerateSummary = (0, import_react6.useCallback)(
|
|
1412
|
+
async (chatKey) => {
|
|
1413
|
+
const response = await regenerateSummaryApi(chatKey, optionsRef.current);
|
|
1414
|
+
if (!response.error && response.data) {
|
|
1415
|
+
return response.data;
|
|
1416
|
+
}
|
|
1417
|
+
return null;
|
|
1418
|
+
},
|
|
1419
|
+
[]
|
|
1420
|
+
);
|
|
1421
|
+
return {
|
|
1422
|
+
profile,
|
|
1423
|
+
loading,
|
|
1424
|
+
patchProfile,
|
|
1425
|
+
replaceProfile,
|
|
1426
|
+
clearProfile,
|
|
1427
|
+
deleteContact,
|
|
1428
|
+
deleteNote,
|
|
1429
|
+
clearSummary,
|
|
1430
|
+
regenerateSummary
|
|
1431
|
+
};
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
// hooks/index.ts
|
|
1435
|
+
var import_hooks4 = require("@elqnt/api-client/hooks");
|
|
737
1436
|
// Annotate the CommonJS export names for ESM import in node:
|
|
738
1437
|
0 && (module.exports = {
|
|
739
|
-
|
|
1438
|
+
useApiAsync,
|
|
1439
|
+
useChat,
|
|
1440
|
+
useChatHistory,
|
|
1441
|
+
useChatMonitoring,
|
|
1442
|
+
useHumanAgentSessions,
|
|
1443
|
+
useMemory,
|
|
1444
|
+
useOptionsRef
|
|
740
1445
|
});
|
|
741
1446
|
//# sourceMappingURL=index.js.map
|