Skip to content

Commit

Permalink
better initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
popestr committed Sep 25, 2024
1 parent 3ecb96d commit 31181ff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
23 changes: 16 additions & 7 deletions packages/chat-headless/src/ChatHeadlessImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ import {
ChatEventPayLoad,
} from "@yext/analytics";
import { getClientSdk } from "./utils/clientSdk";
import { isChatEventClient } from "./models/clients/ChatEventClient";
import {
ChatEventClient,
isChatEventClient,
} from "./models/clients/ChatEventClient";

const BASE_HANDOFF_CREDENTIALS_SESSION_STORAGE_KEY =
"yext_chat_handoff_credentials";
Expand Down Expand Up @@ -186,7 +189,7 @@ export class ChatHeadlessImpl implements ChatHeadless {
if (isChatEventClient(nextClient)) {
try {
if (this.sessionAgentCredentials) {
await nextClient.reinitializeSession(this.sessionAgentCredentials);
await this.reinitializeAgentSession(nextClient);
} else {
const creds = await nextClient.init({
conversationId: this.state.conversation.conversationId,
Expand All @@ -208,6 +211,12 @@ export class ChatHeadlessImpl implements ChatHeadless {
this.chatClient = nextClient;
}

private async reinitializeAgentSession(client: ChatEventClient) {
this.setCanSendMessage(false);
await client.reinitializeSession(this.sessionAgentCredentials);
this.setCanSendMessage(true);
}

addClientSdk(additionalClientSdk: Record<string, string>) {
const { analyticsConfig } = this.config;
this.config.analyticsConfig = {
Expand Down Expand Up @@ -254,8 +263,12 @@ export class ChatHeadlessImpl implements ChatHeadless {
);
return;
}

this.credentialsSessionStorageKey = `${BASE_HANDOFF_CREDENTIALS_SESSION_STORAGE_KEY}__${hostname}__${this.config.botId}`;

if (this.sessionAgentCredentials) {
this.handoff();
}
}

async report(
Expand Down Expand Up @@ -352,10 +365,6 @@ export class ChatHeadlessImpl implements ChatHeadless {
text?: string,
source: MessageSource = MessageSource.USER
): Promise<MessageResponse | undefined> {
if(this.sessionAgentCredentials && !isChatEventClient(this.chatClient)) {
await this.handoff();
}

const client = this.chatClient;
if (isChatEventClient(client)) {
const { conversationId, notes } = this.state.conversation;
Expand Down
19 changes: 14 additions & 5 deletions packages/chat-headless/tests/chatheadless.clients.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ it("reinitializes session using credentials saved in session storage", async ()
agent: agentClient,
});

expect(sessionStorage.getItem("yext_chat_handoff_credentials__localhost__botId")).toBeNull();
expect(
sessionStorage.getItem("yext_chat_handoff_credentials__localhost__botId")
).toBeNull();

// start with bot client, immediately trigger handoff
await headless.getNextMessage();
Expand All @@ -220,11 +222,16 @@ it("reinitializes session using credentials saved in session storage", async ()
agent: agentClient,
});

// agent client is the active client
await headless.getNextMessage();
expect(agentClient.init).toHaveBeenCalledTimes(0);
expect(agentClient.reinitializeSession).toHaveBeenCalledTimes(1);
expect(agentClient.reinitializeSession).toHaveBeenCalledWith("mock-conversation-id");
expect(agentClient.reinitializeSession).toHaveBeenCalledWith(
"mock-conversation-id"
);

// process the async reinitializeSession
await jest.runAllTimersAsync();

await headless.getNextMessage();
expect(botClient.getNextMessage).toHaveBeenCalledTimes(1);
expect(agentClient.processMessage).toHaveBeenCalledTimes(1);
});
Expand All @@ -247,7 +254,9 @@ it("does not reinitialize session if saveToLocalStorage is false", async () => {
expect(botClient.getNextMessage).toHaveBeenCalledTimes(1);
expect(agentClient.init).toHaveBeenCalledTimes(1);

expect(sessionStorage.getItem("yext_chat_handoff_credentials__localhost__botId")).toBeNull();
expect(
sessionStorage.getItem("yext_chat_handoff_credentials__localhost__botId")
).toBeNull();

agentClient = createMockEventClient(callbacks);
headless = provideChatHeadless(newConfig, {
Expand Down

0 comments on commit 31181ff

Please sign in to comment.