Skip to content

Commit

Permalink
chat-headless: add support for session reinitialization
Browse files Browse the repository at this point in the history
Adds session state management for the credentials returned by the underlying
agent client, such that on agent client initialization, the returned credentials
are saved in session storage. When chat-headless is reloaded and getNextMessage
is called, the session in the agent client is reinitialized.

Session credentials are cleared any time the client is reset.

TEST=manual,auto

Wrote new unit tests, saw them pass.

Ran local test app with a Zendesk client, saw conversation persisted
across page refreshes.
  • Loading branch information
popestr committed Sep 25, 2024
1 parent a3b3581 commit 3ecb96d
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 29 deletions.
12 changes: 11 additions & 1 deletion apps/test-site/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
MessageSource,
} from "@yext/chat-headless-react";
import { useCallback, useEffect, useState } from "react";
import { provideChatCoreZendesk } from "@yext/chat-core-zendesk";
// import { provideChatCore } from '@yext/chat-core';

const config: HeadlessConfig = {
botId: process.env.REACT_APP_TEST_BOT_ID || "BOT_ID_HERE",
Expand All @@ -20,12 +22,20 @@ const config: HeadlessConfig = {
internalUser: true,
},
},
saveToLocalStorage: true,
};

function App() {
return (
<div className="App">
<ChatHeadlessProvider config={config}>
<ChatHeadlessProvider
config={config}
clients={{

Check warning on line 33 in apps/test-site/src/App.tsx

View workflow job for this annotation

GitHub Actions / linting / linting

JSX attribute values should not contain objects created in the same scope
agent: provideChatCoreZendesk({
integrationId: "65f4a281e2bab7777482db6d",
}),
}}
>
<ChatComponent />
</ChatHeadlessProvider>
</div>
Expand Down
34 changes: 34 additions & 0 deletions package-lock.json

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

10 changes: 8 additions & 2 deletions packages/chat-headless-react/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
## Testing Process

### Unit Testing

We use Jest as our framework for unit tests. Execute the unit tests with the following command:

```
npm run test
```

### Test Site

To facilitate manual verification, a React test site has been set up in `apps/test-site`. This site currently already have an App component that interfaces with the local `chat-headless-react` library.

To set up the test site, make sure you have a `.env` file configured following the `.sample.env` file. Then, run the following commands:

```
npm i
npm run start
```


## Build Process

Before initiating the build, run the linting process to identify and address any errors or warnings. Use the following command:

```
npm run lint
```

To build the library, execute:

```
npm run build
```

This will create the bundle in the `/dist` directory. This command will also generate documentation files and the `THIRD-PARTY-NOTICES` file.

For guidelines on pull request and version publish process, visit Chat SDK wiki page.
For guidelines on pull request and version publish process, visit Chat SDK wiki page.
4 changes: 2 additions & 2 deletions packages/chat-headless-react/THIRD-PARTY-NOTICES
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ SOFTWARE.
The following npm packages may be included in this product:

- @types/[email protected]
- @types/[email protected].12
- @types/[email protected].13
- @types/[email protected]
- @types/[email protected].5
- @types/[email protected].9
- @types/[email protected]

These packages each contain the following license and notice below:
Expand Down
15 changes: 11 additions & 4 deletions packages/chat-headless/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
## Testing Process

### Unit Testing

We use Jest as our framework for unit tests. Execute the unit tests with the following command:

```
npm run test
```

### Test Site

To facilitate manual verification, a React test site has been set up in `apps/test-site`. This site currently already have an App component that interfaces with the local `chat-headless-react` library. There are two methods to test the `chat-headless` library:
* Have chat-headless-react import the local chat-headless build. Then, the test site will receive the local changes when interfacing with chat-headless-react.
* Update the test site import the local chat-headless to test state update. You would need to create a new component to interface with chat-headless library.

- Have chat-headless-react import the local chat-headless build. Then, the test site will receive the local changes when interfacing with chat-headless-react.
- Update the test site import the local chat-headless to test state update. You would need to create a new component to interface with chat-headless library.

See Chat SDK wiki on how to link to local build.

To set up the test site, make sure you have a `.env` file configured following the `.sample.env` file. Then, run the following commands:

```
npm i
npm run start
```


## Build Process

Before initiating the build, run the linting process to identify and address any errors or warnings. Use the following command:

```
npm run lint
```

To build the library, execute:

```
npm run build
```

This will create the bundle in the `/dist` directory. This command will also generate documentation files and the `THIRD-PARTY-NOTICES` file.

For guidelines on pull request and version publish process, visit Chat SDK wiki page.
For guidelines on pull request and version publish process, visit Chat SDK wiki page.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Initializes the client, using credentials and data in the provided message to se
**Signature:**

```typescript
init(messageResponse: MessageResponse): Promise<void>;
init(messageResponse: MessageResponse): Promise<any>;
```

## Parameters
Expand All @@ -20,5 +20,5 @@ init(messageResponse: MessageResponse): Promise<void>;

**Returns:**

Promise&lt;void&gt;
Promise&lt;any&gt;

Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ export interface ChatEventClient
| [init(messageResponse)](./chat-headless.chateventclient.init.md) | Initializes the client, using credentials and data in the provided message to setup a chat session. |
| [on(eventName, cb)](./chat-headless.chateventclient.on.md) | Registers an event listener for a specified event. 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-headless.chateventclient.processmessage.md) | Processes a message request. The response should be emitted as a message event. |
| [reinitializeSession(credentials)](./chat-headless.chateventclient.reinitializesession.md) | Reinitialize the session using existing session data. |
| [resetSession()](./chat-headless.chateventclient.resetsession.md) | Reset the current chat session. |

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

[Home](./index.md) &gt; [@yext/chat-headless](./chat-headless.md) &gt; [ChatEventClient](./chat-headless.chateventclient.md) &gt; [reinitializeSession](./chat-headless.chateventclient.reinitializesession.md)

## ChatEventClient.reinitializeSession() method

Reinitialize the session using existing session data.

**Signature:**

```typescript
reinitializeSession(credentials: any): Promise<void>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| credentials | any | |

**Returns:**

Promise&lt;void&gt;

3 changes: 2 additions & 1 deletion packages/chat-headless/etc/chat-headless.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ export { ChatConfig }
export interface ChatEventClient {
emit(eventName: string, data: any): void;
getSession(): any;
init(messageResponse: MessageResponse): Promise<void>;
init(messageResponse: MessageResponse): Promise<any>;
on(eventName: "message" | "typing" | "close", cb: (data: any) => void): void;
processMessage(request: MessageRequest): Promise<void>;
reinitializeSession(credentials: any): Promise<void>;
resetSession(): void;
}

Expand Down
Loading

0 comments on commit 3ecb96d

Please sign in to comment.