"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useConversation = useConversation; var _i18n = require("@kbn/i18n"); var _lodash = require("lodash"); var _react = require("react"); var _use_abortable_async = require("./use_abortable_async"); var _use_kibana = require("./use_kibana"); var _use_observability_ai_assistant = require("./use_observability_ai_assistant"); var _use_timeline = require("./use_timeline"); /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ function useConversation({ conversationId, chatService, connectorId }) { const service = (0, _use_observability_ai_assistant.useObservabilityAIAssistant)(); const { services: { notifications } } = (0, _use_kibana.useKibana)(); const [displayedMessages, setDisplayedMessages] = (0, _react.useState)([]); const conversation = (0, _use_abortable_async.useAbortableAsync)(({ signal }) => { if (!conversationId) { const nextConversation = (0, _use_timeline.createNewConversation)({ contexts: (chatService === null || chatService === void 0 ? void 0 : chatService.getContexts()) || [] }); setDisplayedMessages(nextConversation.messages); return nextConversation; } return service.callApi('GET /internal/observability_ai_assistant/conversation/{conversationId}', { signal, params: { path: { conversationId } } }).then(nextConversation => { setDisplayedMessages(nextConversation.messages); return nextConversation; }).catch(error => { setDisplayedMessages([]); throw error; }); }, [conversationId, chatService]); return { conversation, displayedMessages, setDisplayedMessages, save: (messages, handleRefreshConversations) => { const conversationObject = conversation.value; return conversationId ? service.callApi(`PUT /internal/observability_ai_assistant/conversation/{conversationId}`, { signal: null, params: { path: { conversationId }, body: { conversation: (0, _lodash.merge)({ '@timestamp': conversationObject['@timestamp'], conversation: { id: conversationId } }, (0, _lodash.omit)(conversationObject, 'conversation.last_updated', 'namespace', 'user'), { messages }) } } }).catch(err => { notifications.toasts.addError(err, { title: _i18n.i18n.translate('xpack.observabilityAiAssistant.errorUpdatingConversation', { defaultMessage: 'Could not update conversation' }) }); throw err; }) : service.callApi(`POST /internal/observability_ai_assistant/conversation`, { signal: null, params: { body: { conversation: (0, _lodash.merge)({}, conversationObject, { messages }) } } }).then(nextConversation => { if (connectorId) { service.callApi(`PUT /internal/observability_ai_assistant/conversation/{conversationId}/auto_title`, { signal: null, params: { path: { conversationId: nextConversation.conversation.id }, body: { connectorId } } }).then(() => { handleRefreshConversations === null || handleRefreshConversations === void 0 ? void 0 : handleRefreshConversations(); return conversation.refresh(); }); } return nextConversation; }).catch(err => { notifications.toasts.addError(err, { title: _i18n.i18n.translate('xpack.observabilityAiAssistant.errorCreatingConversation', { defaultMessage: 'Could not create conversation' }) }); throw err; }); }, saveTitle: (title, handleRefreshConversations) => { if (conversationId) { return service.callApi('PUT /internal/observability_ai_assistant/conversation/{conversationId}/title', { signal: null, params: { path: { conversationId }, body: { title } } }).then(() => { handleRefreshConversations === null || handleRefreshConversations === void 0 ? void 0 : handleRefreshConversations(); }); } return Promise.resolve(); } }; }