Skip to content

Commit

Permalink
chat-core-aws-connect: add resetSession
Browse files Browse the repository at this point in the history
Adds the resetSession method to ChatCoreAwsConnect, for use
by ChatHeadless when the conversation is reset in the middle of
an agent session.

Also implicitly calls disconnectParticipant if the session
is not already undefined.

TEST=auto

Added unit tests in chat-core-aws-connect and chat-headless,
saw expected results.
  • Loading branch information
popestr committed Jul 9, 2024
1 parent 64d508a commit 7029f2e
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 16 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,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 +130,14 @@ export class ChatCoreAwsConnectImpl implements ChatCoreAwsConnect {
getSession(): connect.ActiveChatSession | undefined {
return this.session;
}

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

const customerSession = this.session as connect.ActiveCustomerChatSession;
customerSession.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;
}
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(): any }>;

Check warning on line 8 in packages/chat-core-aws-connect/src/models/connect.d.ts

View workflow job for this annotation

GitHub Actions / linting / linting

Unexpected any. Specify a different type
export type Logger = typeof AwsChatSession.Logger;
}
}
1 change: 1 addition & 0 deletions packages/chat-core-aws-connect/test-browser-esm/index.html
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>
5 changes: 5 additions & 0 deletions packages/chat-core-aws-connect/test-browser-esm/src/script.js
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
20 changes: 20 additions & 0 deletions packages/chat-core-aws-connect/tests/ChatCoreAwsConnect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ function mockChatSession(): connect.ActiveChatSession {
connect() {
return { connectCalled: true, connectSuccess: true };
},
disconnectParticipant() {
return null;
},
} as unknown as connect.ActiveChatSession;
}

Expand Down Expand Up @@ -369,3 +372,20 @@ 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();
});

0 comments on commit 7029f2e

Please sign in to comment.