Skip to content

Commit

Permalink
assign the appropriate message source based on the client (BOT vs AGE…
Browse files Browse the repository at this point in the history
…NT) (#55)

J=CLIP-1341
TEST=manual,auto

tested via test-site using aws connect agent handoff and verified that the messages show up with the right sources.
added and ran unit tests.
  • Loading branch information
anguyen-yext2 authored Jul 22, 2024
1 parent 93df9d0 commit 27dd6d0
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
28 changes: 23 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/chat-headless/THIRD-PARTY-NOTICES
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The following npm package may be included in this product:

- @yext/chat-core@0.8.2
- @yext/chat-core@0.9.0

This package contains the following license and notice below:

Expand Down
4 changes: 2 additions & 2 deletions packages/chat-headless/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yext/chat-headless",
"version": "0.10.1",
"version": "0.11.0",
"description": "A state manager library powered by Redux for Yext Chat integrations",
"main": "./dist/commonjs/src/index.js",
"module": "./dist/esm/src/index.mjs",
Expand Down Expand Up @@ -40,7 +40,7 @@
"dependencies": {
"@reduxjs/toolkit": "^1.9.5",
"@yext/analytics": "^0.6.3",
"@yext/chat-core": "^0.8.2"
"@yext/chat-core": "^0.9.0"
},
"devDependencies": {
"@babel/preset-env": "^7.21.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/chat-headless/src/ChatHeadlessImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class ChatHeadlessImpl implements ChatHeadless {

client.on("message", (data: string) => {
this.addMessage({
source: MessageSource.BOT,
source: client === this.botClient ? MessageSource.BOT : MessageSource.AGENT,
text: data,
timestamp: new Date().toISOString(),
});
Expand Down
31 changes: 30 additions & 1 deletion packages/chat-headless/tests/chatheadless.clients.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Message, MessageResponse } from "@yext/chat-core";
import { Message, MessageResponse, MessageSource } from "@yext/chat-core";
import { provideChatHeadless } from "../src/HeadlessProvider";
import { ChatEventClient, ChatHttpClient, HeadlessConfig } from "../src/models";
import * as analyticsLib from "@yext/analytics";
Expand Down Expand Up @@ -156,6 +156,35 @@ it("resets session and uses bot client on reset", async () => {
expect(botClient.getNextMessage).toHaveBeenCalledTimes(2);
});

it("update state messages with appropriate source during handoff", async () => {
const botClient = createMockHttpClient([
{ message: createMessage("message 1"), notes: {}, integrationDetails: {} }, //trigger handoff
{ message: createMessage("message 2"), notes: {} },
]);
const callbacks: Record<string, any[]> = {};
const agentClient = createMockEventClient(callbacks);
const headless = provideChatHeadless(config, {
bot: botClient,
agent: agentClient,
});

//latest message is fromt BOT, then bot client handoff to agent client
await headless.getNextMessage();
expect(headless.state.conversation.messages.at(-1)?.source).toEqual(MessageSource.BOT);

//agent client handle next user message
await headless.getNextMessage("user message 1");
expect(headless.state.conversation.messages.at(-2)?.source).toEqual(MessageSource.USER);
expect(headless.state.conversation.messages.at(-1)?.source).toEqual(MessageSource.AGENT);

//agent client handoff to bot client
callbacks["close"]?.forEach((cb) => cb());

//bot client handle next user message
await headless.getNextMessage("user message 2");
expect(headless.state.conversation.messages.at(-1)?.source).toEqual(MessageSource.BOT);
});

function createMessage(text: string): Message {
return {
text,
Expand Down

0 comments on commit 27dd6d0

Please sign in to comment.