Skip to content

Commit

Permalink
Merge pull request #124 from nlxai/expiration-helpers
Browse files Browse the repository at this point in the history
Expiration helpers
  • Loading branch information
peterszerzo authored Jul 9, 2024
2 parents be3fddc + cdff1ce commit c51fc3a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
20 changes: 20 additions & 0 deletions packages/chat-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
21 changes: 4 additions & 17 deletions packages/chat-widget/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c51fc3a

Please sign in to comment.