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

chat-core-aws-connect: add resetSession #39

Merged
merged 6 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## ChatCoreAwsConnect.emit() method

Emit an event into the AWS Connect chat session. Supported events are: - `typing`<!-- -->: The customer is typing.
Emit an event into the Amazon Connect chat session. Supported events are: - `typing`<!-- -->: The customer is typing.

**Signature:**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## ChatCoreAwsConnect.getSession() method

Get the current AWS Connect chat session.
Get the current Amazon Connect chat session.

**Signature:**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## ChatCoreAwsConnect.init() method

Initialize the AWS Connect chat session using the credentials from the Chat API.
Initialize the Amazon Connect chat session using the credentials from the Chat API.

**Signature:**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## ChatCoreAwsConnect interface

Provides methods for interacting with Chat's AWS Connect integration.
Provides methods for interacting with Chat's Amazon Connect integration.

**Signature:**

Expand All @@ -16,9 +16,10 @@ export interface ChatCoreAwsConnect

| Method | Description |
| --- | --- |
| [emit(eventName, data)](./chat-core-aws-connect.chatcoreawsconnect.emit.md) | Emit an event into the AWS Connect chat session. Supported events are: - <code>typing</code>: The customer is typing. |
| [getSession()](./chat-core-aws-connect.chatcoreawsconnect.getsession.md) | Get the current AWS Connect chat session. |
| [init(messageResponse)](./chat-core-aws-connect.chatcoreawsconnect.init.md) | Initialize the AWS Connect chat session using the credentials from the Chat API. |
| [on(eventName, cb)](./chat-core-aws-connect.chatcoreawsconnect.on.md) | Register a callback for an event triggered within the AWS Connect chat session. Supported events are: - <code>message</code>: A new message has been received. - <code>typing</code>: The agent is typing. - <code>close</code>: The chat session has been closed. |
| [emit(eventName, data)](./chat-core-aws-connect.chatcoreawsconnect.emit.md) | Emit an event into the Amazon Connect chat session. Supported events are: - <code>typing</code>: The customer is typing. |
| [getSession()](./chat-core-aws-connect.chatcoreawsconnect.getsession.md) | Get the current Amazon Connect chat session. |
| [init(messageResponse)](./chat-core-aws-connect.chatcoreawsconnect.init.md) | Initialize the Amazon Connect chat session using the credentials from the Chat API. |
| [on(eventName, cb)](./chat-core-aws-connect.chatcoreawsconnect.on.md) | Register a callback for an event triggered within the Amazon Connect chat session. Supported events are: - <code>message</code>: A new message has been received. - <code>typing</code>: The agent is typing. - <code>close</code>: The chat session has been closed. |
| [processMessage(request)](./chat-core-aws-connect.chatcoreawsconnect.processmessage.md) | Process a message sent by the user. |
| [resetSession()](./chat-core-aws-connect.chatcoreawsconnect.resetsession.md) | Resets the [ChatCoreAwsConnect](./chat-core-aws-connect.chatcoreawsconnect.md) instance, clearing the underlying Amazon Connect session. |

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## ChatCoreAwsConnect.on() method

Register a callback for an event triggered within the AWS Connect chat session. Supported events are: - `message`<!-- -->: A new message has been received. - `typing`<!-- -->: The agent is typing. - `close`<!-- -->: The chat session has been closed.
Register a callback for an event triggered within the Amazon Connect chat session. Supported events are: - `message`<!-- -->: A new message has been received. - `typing`<!-- -->: The agent is typing. - `close`<!-- -->: The chat session has been closed.

**Signature:**

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/chat-core-aws-connect](./chat-core-aws-connect.md) &gt; [ChatCoreAwsConnect](./chat-core-aws-connect.chatcoreawsconnect.md) &gt; [resetSession](./chat-core-aws-connect.chatcoreawsconnect.resetsession.md)

## ChatCoreAwsConnect.resetSession() method

Resets the [ChatCoreAwsConnect](./chat-core-aws-connect.chatcoreawsconnect.md) instance, clearing the underlying Amazon Connect session.

**Signature:**

```typescript
resetSession(): void;
```
**Returns:**

void

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
| Interface | Description |
| --- | --- |
| [AwsConnectEventData](./chat-core-aws-connect.awsconnecteventdata.md) | Data associated with an AWS Connect event. |
| [ChatCoreAwsConnect](./chat-core-aws-connect.chatcoreawsconnect.md) | Provides methods for interacting with Chat's AWS Connect integration. |
| [ChatCoreAwsConnect](./chat-core-aws-connect.chatcoreawsconnect.md) | Provides methods for interacting with Chat's Amazon Connect integration. |
| [ChatCoreAwsConnectConfig](./chat-core-aws-connect.chatcoreawsconnectconfig.md) | Configuration for this instance of the [ChatCoreAwsConnect](./chat-core-aws-connect.chatcoreawsconnect.md)<!-- -->. |
| [LoggerConfig](./chat-core-aws-connect.loggerconfig.md) | Configuration for the internal logger of the AWS Connect Chat session. |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface ChatCoreAwsConnect {
init(messageResponse: MessageResponse): Promise<void>;
on<T extends keyof EventMap>(eventName: T, cb: EventCallback<T>): void;
processMessage(request: MessageRequest): Promise<void>;
resetSession(): void;
}

// @public
Expand Down
2 changes: 1 addition & 1 deletion packages/chat-core-aws-connect/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yext/chat-core-aws-connect",
"version": "0.1.0-alpha.2",
"version": "0.1.0",
"description": "Typescript Networking Library for the Yext Chat API Integration with Amazon Connect",
"main": "./dist/commonjs/index.js",
"module": "./dist/esm/index.mjs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AwsConnectEvent } from "../models/AwsConnectEvent";
import { EventMap, EventCallback } from "../models/EventCallback";
import { LoggerConfig } from "../models/LoggerConfig";
import { ChatCoreAwsConnectConfig } from "../models/ChatCoreAwsConnectConfig";
import { isCustomerChatSession } from "../models/ChatSession";
import "amazon-connect-chatjs";

/**
Expand All @@ -12,7 +13,7 @@ import "amazon-connect-chatjs";
* @internal
*/
export class ChatCoreAwsConnectImpl implements ChatCoreAwsConnect {
private session?: connect.ActiveChatSession;
private session?: connect.ActiveCustomerChatSession;
private eventListeners: { [T in keyof EventMap]?: EventCallback<T>[] } = {};
private loggerConfig: LoggerConfig = {
level: "ERROR",
Expand Down Expand Up @@ -51,11 +52,16 @@ export class ChatCoreAwsConnectImpl implements ChatCoreAwsConnect {
region: messageRsp.integrationDetails?.awsConnectHandoff?.region,
});

this.session = connect.ChatSession.create({
const sess = connect.ChatSession.create({
chatDetails: connectionCreds,
type: "CUSTOMER",
});

if (!isCustomerChatSession(sess)) {
throw new Error("Unexpected non-customer chat session type");
}
this.session = sess;

const { connectCalled, connectSuccess } = await this.session.connect({});
if (!connectCalled || !connectSuccess) {
throw new Error("Failed to connect to chat session");
Expand Down Expand Up @@ -97,7 +103,7 @@ export class ChatCoreAwsConnectImpl implements ChatCoreAwsConnect {
this.session?.onEnded((event: AwsConnectEvent) => {
this.eventListeners["close"]?.forEach((cb) => cb(event.data));
// Connection is closed. Clear session and create new one on next handoff request.
this.session = undefined;
this.resetSession();
});
}

Expand Down Expand Up @@ -130,4 +136,13 @@ export class ChatCoreAwsConnectImpl implements ChatCoreAwsConnect {
getSession(): connect.ActiveChatSession | undefined {
return this.session;
}

resetSession(): void {
if (this.session === undefined) {
return;
}

this.session.disconnectParticipant();
this.session = undefined;
}
}
15 changes: 10 additions & 5 deletions packages/chat-core-aws-connect/src/models/ChatCoreAwsConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { MessageRequest, MessageResponse } from "@yext/chat-core";
import { EventCallback, EventMap } from "./EventCallback";

/**
* Provides methods for interacting with Chat's AWS Connect integration.
* Provides methods for interacting with Chat's Amazon Connect integration.
*
* @public
*/
export interface ChatCoreAwsConnect {
/**
* Initialize the AWS Connect chat session using the credentials from the Chat API.
* Initialize the Amazon Connect chat session using the credentials from the Chat API.
*
* @param messageResponse - The response returned from a successful call to the Chat API.
*/
init(messageResponse: MessageResponse): Promise<void>;

/**
* Register a callback for an event triggered within the AWS Connect chat session.
* Register a callback for an event triggered within the Amazon Connect chat session.
* Supported events are:
* - `message`: A new message has been received.
* - `typing`: The agent is typing.
Expand All @@ -27,7 +27,7 @@ export interface ChatCoreAwsConnect {
on<T extends keyof EventMap>(eventName: T, cb: EventCallback<T>): void;

/**
* Emit an event into the AWS Connect chat session.
* Emit an event into the Amazon Connect chat session.
* Supported events are:
* - `typing`: The customer is typing.
*
Expand All @@ -44,10 +44,15 @@ export interface ChatCoreAwsConnect {
processMessage(request: MessageRequest): Promise<void>;

/**
* Get the current AWS Connect chat session.
* Get the current Amazon Connect chat session.
*
* @remarks
* If the session is not initialized, this method will return `undefined`.
*/
getSession(): connect.ActiveChatSession | undefined;

/**
* Resets the {@link ChatCoreAwsConnect} instance, clearing the underlying Amazon Connect session.
*/
resetSession(): void;
}
14 changes: 14 additions & 0 deletions packages/chat-core-aws-connect/src/models/ChatSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Returns true if the Amazon connect chat session is a customer chat session.
* @param session - The Amazon connect chat session.
*
* @internal
*/
export function isCustomerChatSession(
session: connect.ActiveChatSession
): session is connect.ActiveCustomerChatSession {
return (
(session as connect.ActiveCustomerChatSession).disconnectParticipant !==
undefined
);
}
1 change: 1 addition & 0 deletions packages/chat-core-aws-connect/src/models/connect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare global {
const ChatSession: typeof AwsChatSession;
const LogLevel: typeof AwsChatSession.LogLevel;
export type ActiveChatSession = ReturnType<typeof connect.ChatSession.create>;
export type ActiveCustomerChatSession = Extract<ActiveChatSession, { disconnectParticipant(): unknown }>;
export type Logger = typeof AwsChatSession.Logger;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
/><button onclick="getNextMessage()">Chat</button>
<p id="chatresult"></p>
<ul id="messages"></ul>
<button onclick="resetSession()">Reset Session</button>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ let chatCore = provideChatCore({
});

let coreAws = provideChatCoreAwsConnect();

window.resetSession = () => {
coreAws.resetSession();
};

const msgInput = document.getElementById("messageInput");
window.getNextMessage = async () => {
const req = {
Expand Down
35 changes: 32 additions & 3 deletions packages/chat-core-aws-connect/tests/ChatCoreAwsConnect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
AwsConnectEventData,
} from "../src/models/AwsConnectEvent";
import "amazon-connect-chatjs";
import "../src/models/connect";

const createSpy = jest.fn().mockReturnValue(mockChatSession());
const globalConfigSpy = jest.fn();
Expand All @@ -33,7 +32,7 @@ afterEach(() => {
jest.clearAllMocks();
});

function mockChatSession(): connect.ActiveChatSession {
function mockChatSession(): connect.ActiveCustomerChatSession {
return {
onMessage(_: (event: DeepPartial<AwsConnectEvent>) => void) {
return null;
Expand All @@ -53,7 +52,10 @@ function mockChatSession(): connect.ActiveChatSession {
connect() {
return { connectCalled: true, connectSuccess: true };
},
} as unknown as connect.ActiveChatSession;
disconnectParticipant() {
return null;
},
} as unknown as connect.ActiveCustomerChatSession;
}

function mockMessageResponse(): MessageResponse {
Expand Down Expand Up @@ -369,3 +371,30 @@ it("clear session on close event", async () => {
onEndedFn({});
expect(chatCoreAwsConnect.getSession()).toBeUndefined();
});

it("clears session on resetSession", async () => {
const chatCoreAwsConnect = provideChatCoreAwsConnect();
await chatCoreAwsConnect.init(mockMessageResponse());
expect(chatCoreAwsConnect.getSession()).toBeDefined();

chatCoreAwsConnect.resetSession();
expect(chatCoreAwsConnect.getSession()).toBeUndefined();
});

it("noops when clearing undefined session", async () => {
const chatCoreAwsConnect = provideChatCoreAwsConnect();
expect(chatCoreAwsConnect.getSession()).toBeUndefined();

chatCoreAwsConnect.resetSession();
expect(chatCoreAwsConnect.getSession()).toBeUndefined();
});

it("throws error when session is not a customer session", async () => {
const sess = {} as unknown as connect.ActiveChatSession;
createSpy.mockReturnValue(sess);

const chatCoreAwsConnect = provideChatCoreAwsConnect();
expect(() => chatCoreAwsConnect.init(mockMessageResponse())).rejects.toThrow(
"Unexpected non-customer chat session type"
);
});
Loading