|
| 1 | +/* |
| 2 | + * Copyright (c) AXA Group Operations Spain S.A. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | + * a copy of this software and associated documentation files (the |
| 6 | + * "Software"), to deal in the Software without restriction, including |
| 7 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | + * permit persons to whom the Software is furnished to do so, subject to |
| 10 | + * the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be |
| 13 | + * included in all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 19 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 20 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 21 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +import ConversationContext from './conversation-context'; |
| 25 | +import { Session } from "../types/session"; |
| 26 | + |
| 27 | +/** |
| 28 | + * In memory conversation context manager. |
| 29 | + */ |
| 30 | +class MemoryConversationContext extends ConversationContext { |
| 31 | + private readonly conversationContexts: { [conversationId: string]: any }; |
| 32 | + |
| 33 | + /** |
| 34 | + * Constructor of the class. |
| 35 | + * @param {Object} settings Settings for the instance. |
| 36 | + */ |
| 37 | + constructor(settings: object) { |
| 38 | + super(settings); |
| 39 | + this.conversationContexts = {}; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Gets the conversation context from the session. |
| 44 | + * @param {Object} session Chatbot session of the conversation. |
| 45 | + * @returns {Promise<Object>} Promise to resolve the conversation context. |
| 46 | + */ |
| 47 | + public getConversationContext(session: Session): Promise<Object> { |
| 48 | + return new Promise((resolve, reject) => { |
| 49 | + const conversationId = this.getConversationId(session); |
| 50 | + if (!conversationId) { |
| 51 | + return reject(new Error('No conversation id found')); |
| 52 | + } |
| 53 | + if (!this.conversationContexts[conversationId]) { |
| 54 | + this.conversationContexts[conversationId] = {}; |
| 55 | + } |
| 56 | + return resolve(this.conversationContexts[conversationId]); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + public setConversationContext(session: Session, context: any): Promise<void> { |
| 61 | + return new Promise((resolve, reject) => { |
| 62 | + const conversationId = this.getConversationId(session); |
| 63 | + if (!conversationId) { |
| 64 | + return reject(new Error('No conversation id found')); |
| 65 | + } |
| 66 | + this.conversationContexts[conversationId] = context; |
| 67 | + return resolve(); |
| 68 | + }); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export default MemoryConversationContext; |
| 73 | + |
0 commit comments