Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expiration helpers #124

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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