UNPKG

@thoughtspot/visual-embed-sdk

Version:
140 lines 5.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BodylessConversation = exports.SpotterAgentEmbed = exports.ConversationMessage = void 0; const types_1 = require("../types"); const ts_embed_1 = require("./ts-embed"); const conversation_service_1 = require("../utils/graphql/nlsService/conversation-service"); const embedConfig_1 = require("./embedConfig"); const utils_1 = require("../utils"); class ConversationMessage extends ts_embed_1.TsEmbed { constructor(container, viewConfig) { viewConfig.embedComponentType = 'bodyless-conversation'; super(container, viewConfig); this.viewConfig = viewConfig; } getIframeSrc() { var _a; const { sessionId, genNo, acSessionId, acGenNo, convId, messageId, } = this.viewConfig; const path = 'conv-assist-answer'; const queryParams = this.getBaseQueryParams(); queryParams[types_1.Param.HideActions] = [...((_a = queryParams[types_1.Param.HideActions]) !== null && _a !== void 0 ? _a : [])]; queryParams[types_1.Param.isSpotterAgentEmbed] = true; let query = ''; const queryParamsString = (0, utils_1.getQueryParamString)(queryParams, true); if (queryParamsString) { query = `?${queryParamsString}`; } const tsPostHashParams = this.getThoughtSpotPostUrlParams({ sessionId, genNo, acSessionId, acGenNo, convId, messageId, }); return `${this.getEmbedBasePath(query)}/embed/${path}${tsPostHashParams}`; } async render() { await super.render(); const src = this.getIframeSrc(); await this.renderIFrame(src); return this; } } exports.ConversationMessage = ConversationMessage; /** * Create a conversation embed, which can be integrated inside * chatbots or other conversational interfaces. * @example * ```js * import { SpotterAgentEmbed } from '@thoughtspot/visual-embed-sdk'; * * const conversation = new SpotterAgentEmbed({ * worksheetId: 'worksheetId', * }); * * const { container, error } = await conversation.sendMessage('show me sales by region'); * * // append the container to the DOM * document.body.appendChild(container); // or to any other element * ``` * @group Embed components * @version SDK: 1.37.0 | ThoughtSpot: 10.9.0.cl */ class SpotterAgentEmbed { constructor(viewConfig) { this.viewConfig = viewConfig; const embedConfig = (0, embedConfig_1.getEmbedConfig)(); this.conversationService = new conversation_service_1.Conversation(embedConfig.thoughtSpotHost, viewConfig.worksheetId); } async sendMessage(userMessage) { const { data, error } = await this.conversationService.sendMessage(userMessage); if (error) { return { error }; } const container = document.createElement('div'); const embed = new ConversationMessage(container, { ...this.viewConfig, convId: data.convId, messageId: data.messageId, sessionId: data.sessionId, genNo: data.genNo, acSessionId: data.stateKey.transactionId, acGenNo: data.stateKey.generationNumber, }); await embed.render(); return { container, viz: embed }; } /** * Send a message to the conversation service and return only the data. * @param userMessage - The message to send to the conversation service. * @returns The data from the conversation service. */ async sendMessageData(userMessage) { try { const { data, error } = await this.conversationService.sendMessage(userMessage); if (error) { return { error }; } return { data: { convId: data.convId, messageId: data.messageId, sessionId: data.sessionId, genNo: data.genNo, acSessionId: data.stateKey.transactionId, acGenNo: data.stateKey.generationNumber, } }; } catch (error) { return { error: error }; } } } exports.SpotterAgentEmbed = SpotterAgentEmbed; /** * Create a conversation embed, which can be integrated inside * chatbots or other conversational interfaces. * @deprecated from SDK: 1.39.0 | ThoughtSpot: 10.10.0.cl * Use {@link SpotterAgentEmbed} instead * @example * ```js * import { SpotterAgentEmbed } from '@thoughtspot/visual-embed-sdk'; * * const conversation = new SpotterAgentEmbed({ * worksheetId: 'worksheetId', * }); * * const { container, error } = await conversation.sendMessage('show me sales by region'); * * // append the container to the DOM * document.body.appendChild(container); // or to any other element * ``` * @group Embed components */ class BodylessConversation extends SpotterAgentEmbed { constructor(viewConfig) { super(viewConfig); } } exports.BodylessConversation = BodylessConversation; //# sourceMappingURL=bodyless-conversation.js.map