From cdff1ceafbef2dff6409eac58afbc66d1f3d259f Mon Sep 17 00:00:00 2001 From: Peter Szerzo Date: Tue, 9 Jul 2024 17:18:30 +0200 Subject: [PATCH] Expiration helpers --- packages/chat-core/src/index.ts | 20 ++++++++++++++++++++ packages/chat-widget/src/index.tsx | 21 ++++----------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/packages/chat-core/src/index.ts b/packages/chat-core/src/index.ts index 4b8b9040..5bd6e96d 100644 --- a/packages/chat-core/src/index.ts +++ b/packages/chat-core/src/index.ts @@ -944,6 +944,26 @@ export default function createConversation( }; } +/** + * Get current expiration timestamp from the current list of reponses + * @param responses - the current list of user and bot responses (first argument in the subscribe callback) + * @returns an expiration timestamp in Unix Epoch (`new Date().getTime()`), or `null` if this is not known (typically occurs if the bot has not responded yet) + */ +export const getCurrentExpirationTimestamp = ( + responses: Response[], +): number | null => { + let expirationTimestamp: number | null = null; + responses.forEach((response) => { + if ( + response.type === "bot" && + response.payload.expirationTimestamp != null + ) { + expirationTimestamp = response.payload.expirationTimestamp; + } + }); + return expirationTimestamp; +}; + /** * This package is intentionally designed with a subscription-based API as opposed to a promise-based one where each message corresponds to a single bot response, available asynchronously. * diff --git a/packages/chat-widget/src/index.tsx b/packages/chat-widget/src/index.tsx index 6f27e54b..c72f3d77 100644 --- a/packages/chat-widget/src/index.tsx +++ b/packages/chat-widget/src/index.tsx @@ -38,6 +38,9 @@ export { default as React } from "react"; /** @hidden */ export { default as ReactDOM } from "react-dom"; +/** @hidden */ +export { getCurrentExpirationTimestamp } from "@nlxai/chat-core"; + export { type Props, type TitleBar, @@ -320,23 +323,7 @@ const retrieveSession = ( const responses: Response[] | undefined = data?.data?.responses; const conversationId: string | undefined = data?.data?.conversationId; if (responses != null) { - let expirationTimestamp: number | undefined; - responses.forEach((response) => { - if ( - response.type === "bot" && - response.payload.expirationTimestamp != null - ) { - expirationTimestamp = response.payload.expirationTimestamp; - } - }); - if ( - expirationTimestamp == null || - new Date().getTime() < expirationTimestamp - ) { - return { responses, conversationId }; - } else { - return { responses }; - } + return { responses, conversationId }; } return null; } catch (err) {