From ff7d1746d2240a6742174c104c5d57434c597f28 Mon Sep 17 00:00:00 2001 From: Ryan Pope Date: Wed, 2 Oct 2024 15:24:27 -0400 Subject: [PATCH] chat-headless: add support for session reinitialization (#59) 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. --- package-lock.json | 38 +- packages/chat-headless-react/CONTRIBUTING.md | 10 +- .../chat-headless-react/THIRD-PARTY-NOTICES | 672 ------------------ packages/chat-headless-react/package.json | 2 +- packages/chat-headless/CONTRIBUTING.md | 15 +- packages/chat-headless/THIRD-PARTY-NOTICES | 2 +- .../chat-headless.chateventclient.init.md | 4 +- .../docs/chat-headless.chateventclient.md | 1 + ...ess.chateventclient.reinitializesession.md | 24 + .../chat-headless/etc/chat-headless.api.md | 3 +- packages/chat-headless/package.json | 2 +- .../chat-headless/src/ChatHeadlessImpl.ts | 118 ++- .../src/models/clients/ChatEventClient.ts | 7 +- .../tests/chatheadless.clients.test.ts | 138 +++- .../chat-headless/tests/chatheadless.test.ts | 1 + 15 files changed, 333 insertions(+), 704 deletions(-) create mode 100644 packages/chat-headless/docs/chat-headless.chateventclient.reinitializesession.md diff --git a/package-lock.json b/package-lock.json index e7cca90..4e68410 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,37 @@ "turbo": "latest" } }, + "../chat-core/packages/chat-core-zendesk": { + "name": "@yext/chat-core-zendesk", + "version": "0.1.3", + "extraneous": true, + "license": "BSD-3-Clause", + "dependencies": { + "smooch": "5.6.0" + }, + "devDependencies": { + "@babel/preset-env": "^7.21.5", + "@babel/preset-typescript": "^7.21.5", + "@microsoft/api-documenter": "^7.22.4", + "@microsoft/api-extractor": "^7.34.8", + "@types/jest": "^29.5.1", + "@types/smooch": "^5.3.7", + "@yext/chat-core": "^0.9.1", + "@yext/eslint-config": "^1.0.0", + "babel-jest": "^29.5.0", + "eslint": "^8.39.0", + "generate-license-file": "^1.0.0", + "jest": "^29.5.0", + "jest-environment-jsdom": "^29.5.0", + "prettier": "^2.8.8", + "rollup": "^3.29.0", + "rollup-plugin-typescript2": "^0.35.0", + "typescript": "^5.0.4" + }, + "peerDependencies": { + "@yext/chat-core": "^0.9.1" + } + }, "apps/test-site": { "version": "0.1.0", "dependencies": { @@ -18720,9 +18751,12 @@ "node": "^12.20.0 || >=14" } }, + "packages/chat-core-zendesk": { + "extraneous": true + }, "packages/chat-headless": { "name": "@yext/chat-headless", - "version": "0.11.1", + "version": "0.12.0", "license": "BSD-3-Clause", "dependencies": { "@reduxjs/toolkit": "^1.9.5", @@ -18750,7 +18784,7 @@ "license": "BSD-3-Clause", "dependencies": { "@reduxjs/toolkit": "^1.9.5", - "@yext/chat-headless": "^0.11.1", + "@yext/chat-headless": "^0.12.0", "react-redux": "^8.0.5" }, "devDependencies": { diff --git a/packages/chat-headless-react/CONTRIBUTING.md b/packages/chat-headless-react/CONTRIBUTING.md index 2f54fe9..3bacb6f 100644 --- a/packages/chat-headless-react/CONTRIBUTING.md +++ b/packages/chat-headless-react/CONTRIBUTING.md @@ -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. \ No newline at end of file +For guidelines on pull request and version publish process, visit Chat SDK wiki page. diff --git a/packages/chat-headless-react/THIRD-PARTY-NOTICES b/packages/chat-headless-react/THIRD-PARTY-NOTICES index 7c4a9ce..fbfb099 100644 --- a/packages/chat-headless-react/THIRD-PARTY-NOTICES +++ b/packages/chat-headless-react/THIRD-PARTY-NOTICES @@ -1,677 +1,5 @@ This file was generated with the generate-license-file npm package! https://www.npmjs.com/package/generate-license-file -The following npm package may be included in this product: - - - @babel/runtime@7.25.6 - -This package contains the following license and notice below: - -MIT License - -Copyright (c) 2014-present Sebastian McKenzie and other contributors - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - ------------ - -The following npm package may be included in this product: - - - @reduxjs/toolkit@1.9.7 - -This package contains the following license and notice below: - -MIT License - -Copyright (c) 2018 Mark Erikson - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - ------------ - -The following npm packages may be included in this product: - - - @types/hoist-non-react-statics@3.3.5 - - @types/prop-types@15.7.12 - - @types/react-dom@18.3.0 - - @types/react@18.3.5 - - @types/use-sync-external-store@0.0.3 - -These packages each contain the following license and notice below: - -MIT License - - Copyright (c) Microsoft Corporation. - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE - ------------ - -The following npm package may be included in this product: - - - @yext/analytics@0.6.6 - -This package contains the following license and notice below: - -The Analytics files listed in this repository are licensed under the below license. All other features and products are subject to separate agreements -and certain functionality requires paid subscriptions to Yext products. - -BSD 3-Clause License - -Copyright (c) 2022, Yext -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ------------ - -The following npm packages may be included in this product: - - - @yext/chat-core@0.9.1 - - @yext/chat-headless@0.11.1 - -These packages each contain the following license and notice below: - -Contains information from the language-subtag-registry JSON Database (https://github.com/mattcg/language-subtag-registry/tree/master/data/json) which is made available under the ODC Attribution License (https://github.com/mattcg/language-subtag-registry/blob/master/LICENSE.md). - -The files listed in this repository are licensed under the below license. All other features and products are subject to separate agreements and certain functionality requires paid subscriptions to Yext products. - -BSD 3-Clause License - -Copyright (c) 2023, Yext -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ------------ - -The following npm package may be included in this product: - - - cross-fetch@3.1.8 - -This package contains the following license and notice below: - -The MIT License (MIT) - -Copyright (c) 2017 Leonardo Quixadá - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - ------------ - -The following npm package may be included in this product: - - - csstype@3.1.3 - -This package contains the following license and notice below: - -Copyright (c) 2017-2018 Fredrik Nicol - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - ------------ - -The following npm package may be included in this product: - - - hoist-non-react-statics@3.3.2 - -This package contains the following license and notice below: - -Software License Agreement (BSD License) -======================================== - -Copyright (c) 2015, Yahoo! Inc. All rights reserved. ----------------------------------------------------- - -Redistribution and use of this software in source and binary forms, with or -without modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - * Neither the name of Yahoo! Inc. nor the names of YUI's contributors may be - used to endorse or promote products derived from this software without - specific prior written permission of Yahoo! Inc. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ------------ - -The following npm package may be included in this product: - - - immer@9.0.21 - -This package contains the following license and notice below: - -MIT License - -Copyright (c) 2017 Michel Weststrate - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - ------------ - -The following npm package may be included in this product: - - - js-tokens@4.0.0 - -This package contains the following license and notice below: - -The MIT License (MIT) - -Copyright (c) 2014, 2015, 2016, 2017, 2018 Simon Lydell - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - ------------ - -The following npm package may be included in this product: - - - layerr@3.0.0 - -This package contains the following license and notice below: - -MIT License - -Copyright (c) 2020 Perry Mitchell - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - ------------ - -The following npm package may be included in this product: - - - loose-envify@1.4.0 - -This package contains the following license and notice below: - -The MIT License (MIT) - -Copyright (c) 2015 Andres Suarez - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - ------------ - -The following npm package may be included in this product: - - - node-fetch@2.7.0 - -This package contains the following license and notice below: - -The MIT License (MIT) - -Copyright (c) 2016 David Frank - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - ------------ - -The following npm packages may be included in this product: - - - react-dom@18.3.1 - - react-is@16.13.1 - - react-is@18.2.0 - - react@18.3.1 - - scheduler@0.23.2 - - use-sync-external-store@1.2.2 - -These packages each contain the following license and notice below: - -MIT License - -Copyright (c) Facebook, Inc. and its affiliates. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - ------------ - -The following npm packages may be included in this product: - - - react-redux@8.1.3 - - redux-thunk@2.4.2 - -These packages each contain the following license and notice below: - -The MIT License (MIT) - -Copyright (c) 2015-present Dan Abramov - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - ------------ - -The following npm package may be included in this product: - - - redux@4.2.1 - -This package contains the following license and notice below: - -The MIT License (MIT) - -Copyright (c) 2015-present Dan Abramov - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - ------------ - -The following npm package may be included in this product: - - - regenerator-runtime@0.14.1 - -This package contains the following license and notice below: - -MIT License - -Copyright (c) 2014-present, Facebook, Inc. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - ------------ - -The following npm package may be included in this product: - - - reselect@4.1.8 - -This package contains the following license and notice below: - -The MIT License (MIT) - -Copyright (c) 2015-2018 Reselect Contributors - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - ------------ - -The following npm package may be included in this product: - - - tr46@0.0.3 - -This package contains the following license and notice below: - -(MIT) - ------------ - -The following npm package may be included in this product: - - - ulidx@2.4.1 - -This package contains the following license and notice below: - -MIT License - -Copyright (c) 2021 Perry Mitchell - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - ------------ - -The following npm package may be included in this product: - - - webidl-conversions@3.0.1 - -This package contains the following license and notice below: - -# The BSD 2-Clause License - -Copyright (c) 2014, Domenic Denicola -All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ------------ - -The following npm package may be included in this product: - - - whatwg-url@5.0.0 - -This package contains the following license and notice below: - -The MIT License (MIT) - -Copyright (c) 2015–2016 Sebastian Mayr - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - ------------ - This file was generated with the generate-license-file npm package! https://www.npmjs.com/package/generate-license-file diff --git a/packages/chat-headless-react/package.json b/packages/chat-headless-react/package.json index 3a604f2..57b9fb7 100644 --- a/packages/chat-headless-react/package.json +++ b/packages/chat-headless-react/package.json @@ -39,7 +39,7 @@ "homepage": "https://github.com/yext/chat-headless#readme", "dependencies": { "@reduxjs/toolkit": "^1.9.5", - "@yext/chat-headless": "^0.11.1", + "@yext/chat-headless": "^0.12.0", "react-redux": "^8.0.5" }, "peerDependencies": { diff --git a/packages/chat-headless/CONTRIBUTING.md b/packages/chat-headless/CONTRIBUTING.md index 4334002..039dd96 100644 --- a/packages/chat-headless/CONTRIBUTING.md +++ b/packages/chat-headless/CONTRIBUTING.md @@ -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. \ No newline at end of file +For guidelines on pull request and version publish process, visit Chat SDK wiki page. diff --git a/packages/chat-headless/THIRD-PARTY-NOTICES b/packages/chat-headless/THIRD-PARTY-NOTICES index 5f8f64c..bb81f95 100644 --- a/packages/chat-headless/THIRD-PARTY-NOTICES +++ b/packages/chat-headless/THIRD-PARTY-NOTICES @@ -3,7 +3,7 @@ https://www.npmjs.com/package/generate-license-file The following npm package may be included in this product: - - @babel/runtime@7.25.6 + - @babel/runtime@7.25.7 This package contains the following license and notice below: diff --git a/packages/chat-headless/docs/chat-headless.chateventclient.init.md b/packages/chat-headless/docs/chat-headless.chateventclient.init.md index 11e9745..b2280cd 100644 --- a/packages/chat-headless/docs/chat-headless.chateventclient.init.md +++ b/packages/chat-headless/docs/chat-headless.chateventclient.init.md @@ -9,7 +9,7 @@ Initializes the client, using credentials and data in the provided message to se **Signature:** ```typescript -init(messageResponse: MessageResponse): Promise; +init(messageResponse: MessageResponse): Promise; ``` ## Parameters @@ -20,5 +20,5 @@ init(messageResponse: MessageResponse): Promise; **Returns:** -Promise<void> +Promise<any> diff --git a/packages/chat-headless/docs/chat-headless.chateventclient.md b/packages/chat-headless/docs/chat-headless.chateventclient.md index f6457d1..53876b0 100644 --- a/packages/chat-headless/docs/chat-headless.chateventclient.md +++ b/packages/chat-headless/docs/chat-headless.chateventclient.md @@ -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: - message: A new message has been received. - typing: The agent is typing. - close: 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. | diff --git a/packages/chat-headless/docs/chat-headless.chateventclient.reinitializesession.md b/packages/chat-headless/docs/chat-headless.chateventclient.reinitializesession.md new file mode 100644 index 0000000..580e2ce --- /dev/null +++ b/packages/chat-headless/docs/chat-headless.chateventclient.reinitializesession.md @@ -0,0 +1,24 @@ + + +[Home](./index.md) > [@yext/chat-headless](./chat-headless.md) > [ChatEventClient](./chat-headless.chateventclient.md) > [reinitializeSession](./chat-headless.chateventclient.reinitializesession.md) + +## ChatEventClient.reinitializeSession() method + +Reinitialize the session using existing session data. + +**Signature:** + +```typescript +reinitializeSession(credentials: any): Promise; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| credentials | any | | + +**Returns:** + +Promise<void> + diff --git a/packages/chat-headless/etc/chat-headless.api.md b/packages/chat-headless/etc/chat-headless.api.md index bc24c62..4cf9549 100644 --- a/packages/chat-headless/etc/chat-headless.api.md +++ b/packages/chat-headless/etc/chat-headless.api.md @@ -41,9 +41,10 @@ export { ChatConfig } export interface ChatEventClient { emit(eventName: string, data: any): void; getSession(): any; - init(messageResponse: MessageResponse): Promise; + init(messageResponse: MessageResponse): Promise; on(eventName: "message" | "typing" | "close", cb: (data: any) => void): void; processMessage(request: MessageRequest): Promise; + reinitializeSession(credentials: any): Promise; resetSession(): void; } diff --git a/packages/chat-headless/package.json b/packages/chat-headless/package.json index 6ceecbf..502faa7 100644 --- a/packages/chat-headless/package.json +++ b/packages/chat-headless/package.json @@ -1,6 +1,6 @@ { "name": "@yext/chat-headless", - "version": "0.11.1", + "version": "0.12.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", diff --git a/packages/chat-headless/src/ChatHeadlessImpl.ts b/packages/chat-headless/src/ChatHeadlessImpl.ts index cff79ca..ad2c494 100644 --- a/packages/chat-headless/src/ChatHeadlessImpl.ts +++ b/packages/chat-headless/src/ChatHeadlessImpl.ts @@ -34,7 +34,13 @@ 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"; /** * Concrete implementation of {@link ChatHeadless} @@ -48,6 +54,7 @@ export class ChatHeadlessImpl implements ChatHeadless { private clients: ChatClient[]; private stateManager: ReduxStateManager; private chatAnalyticsService: ChatAnalyticsService; + private credentialsSessionStorageKey: string | undefined; private isImpressionAnalyticEventSent = false; @@ -91,6 +98,39 @@ export class ChatHeadlessImpl implements ChatHeadless { } } + private get sessionAgentCredentials(): unknown | void { + if (this.credentialsSessionStorageKey) { + const credentials = sessionStorage.getItem( + this.credentialsSessionStorageKey + ); + if (credentials) { + try { + return JSON.parse(credentials); + } catch (e) { + this.removeSessionAgentCredentials(); + throw new Error( + `Error occurred while parsing credentials from session storage: ${e}` + ); + } + } + } + } + + private setSessionAgentCredentials(credentials: unknown) { + if (this.credentialsSessionStorageKey) { + sessionStorage.setItem( + this.credentialsSessionStorageKey, + JSON.stringify(credentials) + ); + } + } + + private removeSessionAgentCredentials() { + if (this.credentialsSessionStorageKey) { + sessionStorage.removeItem(this.credentialsSessionStorageKey); + } + } + get state(): State { return this.stateManager.getState(); } @@ -117,7 +157,8 @@ export class ChatHeadlessImpl implements ChatHeadless { client.on("message", (data: string) => { this.addMessage({ - source: client === this.botClient ? MessageSource.BOT : MessageSource.AGENT, + source: + client === this.botClient ? MessageSource.BOT : MessageSource.AGENT, text: data, timestamp: new Date().toISOString(), }); @@ -137,6 +178,10 @@ export class ChatHeadlessImpl implements ChatHeadless { * Switches the current chat client with the next client, if available. */ private async handoff(integrationDetails?: IntegrationDetails) { + if (isChatEventClient(this.chatClient)) { + this.removeSessionAgentCredentials(); + } + let nextClient: ChatClient | undefined = undefined; for (const client of this.clients) { if (this.chatClient !== client) { @@ -150,15 +195,21 @@ export class ChatHeadlessImpl implements ChatHeadless { if (isChatEventClient(nextClient)) { try { - await nextClient.init({ - conversationId: this.state.conversation.conversationId, - message: this.state.conversation.messages.at(-1) || { - source: MessageSource.BOT, - text: "", - }, - notes: this.state.conversation.notes || {}, - integrationDetails, - }); + if (this.sessionAgentCredentials) { + await this.reinitializeAgentSession(nextClient); + } else { + const creds = await nextClient.init({ + conversationId: this.state.conversation.conversationId, + message: this.state.conversation.messages.at(-1) || { + source: MessageSource.BOT, + text: "", + }, + notes: this.state.conversation.notes || {}, + integrationDetails, + }); + + this.setSessionAgentCredentials(creds); + } } catch (e) { console.error("Error occurred while initializing next client:", e); return; @@ -167,6 +218,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) { const { analyticsConfig } = this.config; this.config.analyticsConfig = { @@ -182,6 +239,21 @@ export class ChatHeadlessImpl implements ChatHeadless { } initLocalStorage() { + const hostname = window.location.hostname; + if (!hostname) { + console.warn( + "Unable to get hostname of current page. State will not be persisted while navigating across pages." + ); + return; + } + + if (!localStorage) { + console.warn( + "Local storage is not available. State will not be persisted while navigating across pages." + ); + return; + } + this.setState({ ...this.state, conversation: loadSessionState(this.config.botId), @@ -191,6 +263,27 @@ export class ChatHeadlessImpl implements ChatHeadless { callback: () => saveSessionState(this.config.botId, this.state.conversation), }); + + if (!sessionStorage) { + console.warn( + "Session storage is not available. State will not be persisted while navigating across pages." + ); + return; + } + + this.credentialsSessionStorageKey = `${BASE_HANDOFF_CREDENTIALS_SESSION_STORAGE_KEY}__${hostname}__${this.config.botId}`; + + try { + const credentials = this.sessionAgentCredentials; + if (credentials) { + this.handoff(); + } + } catch (e) { + console.error( + "Error occurred while initializing agent session using stored credentials:", + e + ); + } } async report( @@ -267,6 +360,7 @@ export class ChatHeadlessImpl implements ChatHeadless { restartConversation() { if (isChatEventClient(this.chatClient)) { + this.removeSessionAgentCredentials(); this.chatClient.resetSession(); } this.chatClient = this.botClient; @@ -438,7 +532,7 @@ export class ChatHeadlessImpl implements ChatHeadless { this.setCanSendMessage(true); this.setChatLoadingStatus(false); if (!!messageResponse.integrationDetails) { - this.handoff(messageResponse.integrationDetails); + await this.handoff(messageResponse.integrationDetails); } return messageResponse; } diff --git a/packages/chat-headless/src/models/clients/ChatEventClient.ts b/packages/chat-headless/src/models/clients/ChatEventClient.ts index b06ad3b..5562f5a 100644 --- a/packages/chat-headless/src/models/clients/ChatEventClient.ts +++ b/packages/chat-headless/src/models/clients/ChatEventClient.ts @@ -15,7 +15,7 @@ export interface ChatEventClient { * * @param messageResponse - The message response that initiated the handoff to the chat client. */ - init(messageResponse: MessageResponse): Promise; + init(messageResponse: MessageResponse): Promise; /** * Registers an event listener for a specified event. @@ -53,6 +53,11 @@ export interface ChatEventClient { * Reset the current chat session. */ resetSession(): void; + + /** + * Reinitialize the session using existing session data. + */ + reinitializeSession(credentials: any): Promise; } export function isChatEventClient( diff --git a/packages/chat-headless/tests/chatheadless.clients.test.ts b/packages/chat-headless/tests/chatheadless.clients.test.ts index e01fb7f..aba5612 100644 --- a/packages/chat-headless/tests/chatheadless.clients.test.ts +++ b/packages/chat-headless/tests/chatheadless.clients.test.ts @@ -9,6 +9,8 @@ beforeEach(() => { jest.spyOn(analyticsLib, "provideChatAnalytics").mockReturnValue({ report: jest.fn(), }); + localStorage.clear(); + sessionStorage.clear(); }); const config: HeadlessConfig = { @@ -170,19 +172,142 @@ it("update state messages with appropriate source during handoff", async () => { //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); + 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); + 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); + expect(headless.state.conversation.messages.at(-1)?.source).toEqual( + MessageSource.BOT + ); +}); + +it("reinitializes session using credentials saved in session storage", async () => { + const botClient = createMockHttpClient([ + { message: createMessage("message 1"), notes: {}, integrationDetails: {} }, //trigger handoff + { message: createMessage("message 2"), notes: {} }, + ]); + const callbacks: Record = {}; + let agentClient = createMockEventClient(callbacks); + let headless = provideChatHeadless(config, { + bot: botClient, + agent: agentClient, + }); + + expect( + sessionStorage.getItem("yext_chat_handoff_credentials__localhost__botId") + ).toBeNull(); + + // start with bot client, immediately trigger handoff + await headless.getNextMessage(); + expect(agentClient.init).toHaveBeenCalledTimes(1); + expect(botClient.getNextMessage).toHaveBeenCalledTimes(1); + + agentClient = createMockEventClient(callbacks); + headless = provideChatHeadless(config, { + bot: botClient, + agent: agentClient, + }); + + expect(agentClient.init).toHaveBeenCalledTimes(0); + expect(agentClient.reinitializeSession).toHaveBeenCalledTimes(1); + 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); +}); + +it("does not reinitialize session if saveToLocalStorage is false", async () => { + const botClient = createMockHttpClient([ + { message: createMessage("message 1"), notes: {}, integrationDetails: {} }, //trigger handoff + { message: createMessage("message 2"), notes: {} }, + ]); + const callbacks: Record = {}; + let agentClient = createMockEventClient(callbacks); + const newConfig = { ...config, saveToLocalStorage: false }; + let headless = provideChatHeadless(newConfig, { + bot: botClient, + agent: agentClient, + }); + + // start with bot client, immediately trigger handoff + await headless.getNextMessage(); + expect(botClient.getNextMessage).toHaveBeenCalledTimes(1); + expect(agentClient.init).toHaveBeenCalledTimes(1); + + expect( + sessionStorage.getItem("yext_chat_handoff_credentials__localhost__botId") + ).toBeNull(); + + agentClient = createMockEventClient(callbacks); + headless = provideChatHeadless(newConfig, { + bot: botClient, + agent: agentClient, + }); + + // agent does not reinitialize, nothing saved to session + expect(agentClient.reinitializeSession).toHaveBeenCalledTimes(0); + + // bot client is the active client + await headless.getNextMessage(); + expect(botClient.getNextMessage).toHaveBeenCalledTimes(2); +}); + +it("defaults to bot client if session storage contains invalid data", async () => { + const botClient = createMockHttpClient([ + { message: createMessage("message 1"), notes: {}, integrationDetails: {} }, //trigger handoff + { message: createMessage("message 2"), notes: {} }, + ]); + const callbacks: Record = {}; + let agentClient = createMockEventClient(callbacks); + const newConfig = { ...config, saveToLocalStorage: true }; + let headless = provideChatHeadless(newConfig, { + bot: botClient, + agent: agentClient, + }); + + // start with bot client, immediately trigger handoff + await headless.getNextMessage(); + expect(botClient.getNextMessage).toHaveBeenCalledTimes(1); + expect(agentClient.init).toHaveBeenCalledTimes(1); + + // save invalid data to session storage + sessionStorage.setItem( + "yext_chat_handoff_credentials__localhost__botId", + "invalid" + ); + + agentClient = createMockEventClient(callbacks); + headless = provideChatHeadless(newConfig, { + bot: botClient, + agent: agentClient, + }); + + // agent does not reinitialize, nothing saved to session + expect(agentClient.reinitializeSession).toHaveBeenCalledTimes(0); + + // bot client is the active client + await headless.getNextMessage(); + expect(botClient.getNextMessage).toHaveBeenCalledTimes(2); }); function createMessage(text: string): Message { @@ -212,7 +337,9 @@ function createMockEventClient( callbacks?: Record ): ChatEventClient { const client: ChatEventClient = { - init: jest.fn(), + init: jest.fn(async () => { + return Promise.resolve("mock-conversation-id"); + }), on: (event, cb) => { if (!callbacks) { return; @@ -231,6 +358,7 @@ function createMockEventClient( emit: jest.fn(), getSession: jest.fn(), resetSession: jest.fn(), + reinitializeSession: jest.fn(), }; return client; } diff --git a/packages/chat-headless/tests/chatheadless.test.ts b/packages/chat-headless/tests/chatheadless.test.ts index 0eae889..3d06dbb 100644 --- a/packages/chat-headless/tests/chatheadless.test.ts +++ b/packages/chat-headless/tests/chatheadless.test.ts @@ -37,6 +37,7 @@ beforeEach(() => { return client; }); localStorage.clear(); + sessionStorage.clear(); }); describe("setters work as expected", () => {