"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.functionRoutes = void 0; var _datemath = _interopRequireDefault(require("@elastic/datemath")); var _boom = require("@hapi/boom"); var _esQuery = require("@kbn/es-query"); var _ioTsUtils = require("@kbn/io-ts-utils"); var t = _interopRequireWildcard(require("io-ts")); var _lodash = require("lodash"); var _technical_rule_data_field_names = require("@kbn/rule-registry-plugin/common/technical_rule_data_field_names"); var _create_observability_ai_assistant_server_route = require("../create_observability_ai_assistant_server_route"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /* * 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. */ const functionElasticsearchRoute = (0, _create_observability_ai_assistant_server_route.createObservabilityAIAssistantServerRoute)({ endpoint: 'POST /internal/observability_ai_assistant/functions/elasticsearch', options: { tags: ['access:ai_assistant'] }, params: t.type({ body: t.intersection([t.type({ method: t.union([t.literal('GET'), t.literal('POST'), t.literal('PATCH'), t.literal('PUT'), t.literal('DELETE')]), path: t.string }), t.partial({ body: t.any })]) }), handler: async resources => { const { method, path, body } = resources.params.body; const response = await (await resources.context.core).elasticsearch.client.asCurrentUser.transport.request({ method, path, body }); return response; } }); const OMITTED_ALERT_FIELDS = ['tags', 'event.action', 'event.kind', 'kibana.alert.rule.execution.uuid', 'kibana.alert.rule.revision', 'kibana.alert.rule.tags', 'kibana.alert.rule.uuid', 'kibana.alert.workflow_status', 'kibana.space_ids', 'kibana.alert.time_range', 'kibana.version']; const functionAlertsRoute = (0, _create_observability_ai_assistant_server_route.createObservabilityAIAssistantServerRoute)({ endpoint: 'POST /internal/observability_ai_assistant/functions/alerts', options: { tags: ['access:ai_assistant'] }, params: t.type({ body: t.intersection([t.type({ featureIds: t.array(t.string), start: t.string, end: t.string }), t.partial({ filter: t.string, includeRecovered: _ioTsUtils.toBooleanRt })]) }), handler: async resources => { const { featureIds, start: startAsDatemath, end: endAsDatemath, filter, includeRecovered } = resources.params.body; const racContext = await resources.context.rac; const alertsClient = await racContext.getAlertsClient(); const start = _datemath.default.parse(startAsDatemath).valueOf(); const end = _datemath.default.parse(endAsDatemath).valueOf(); const kqlQuery = !filter ? [] : [(0, _esQuery.toElasticsearchQuery)((0, _esQuery.fromKueryExpression)(filter))]; const response = await alertsClient.find({ featureIds, query: { bool: { filter: [{ range: { '@timestamp': { gte: start, lte: end } } }, ...kqlQuery, ...(!includeRecovered ? [{ term: { [_technical_rule_data_field_names.ALERT_STATUS]: _technical_rule_data_field_names.ALERT_STATUS_ACTIVE } }] : [])] } } }); // trim some fields const alerts = response.hits.hits.map(hit => (0, _lodash.omit)(hit._source, ...OMITTED_ALERT_FIELDS)); return { content: { total: response.hits.total.value, alerts } }; } }); const functionRecallRoute = (0, _create_observability_ai_assistant_server_route.createObservabilityAIAssistantServerRoute)({ endpoint: 'POST /internal/observability_ai_assistant/functions/recall', params: t.type({ body: t.type({ queries: t.array(_ioTsUtils.nonEmptyStringRt) }) }), options: { tags: ['access:ai_assistant'] }, handler: async resources => { const client = await resources.service.getClient({ request: resources.request }); if (!client) { throw (0, _boom.notImplemented)(); } return client.recall(resources.params.body.queries); } }); const functionSummariseRoute = (0, _create_observability_ai_assistant_server_route.createObservabilityAIAssistantServerRoute)({ endpoint: 'POST /internal/observability_ai_assistant/functions/summarize', params: t.type({ body: t.type({ id: t.string, text: _ioTsUtils.nonEmptyStringRt, confidence: t.union([t.literal('low'), t.literal('medium'), t.literal('high')]), is_correction: _ioTsUtils.toBooleanRt, public: _ioTsUtils.toBooleanRt, labels: t.record(t.string, t.string) }) }), options: { tags: ['access:ai_assistant'] }, handler: async resources => { const client = await resources.service.getClient({ request: resources.request }); if (!client) { throw (0, _boom.notImplemented)(); } const { confidence, id, is_correction: isCorrection, text, public: isPublic, labels } = resources.params.body; return client.summarize({ entry: { confidence, id, is_correction: isCorrection, text, public: isPublic, labels } }); } }); const getKnowledgeBaseStatus = (0, _create_observability_ai_assistant_server_route.createObservabilityAIAssistantServerRoute)({ endpoint: 'GET /internal/observability_ai_assistant/functions/kb_status', options: { tags: ['access:ai_assistant'] }, handler: async resources => { const client = await resources.service.getClient({ request: resources.request }); if (!client) { throw (0, _boom.notImplemented)(); } return await client.getKnowledgeBaseStatus(); } }); const setupKnowledgeBaseRoute = (0, _create_observability_ai_assistant_server_route.createObservabilityAIAssistantServerRoute)({ endpoint: 'POST /internal/observability_ai_assistant/functions/setup_kb', options: { tags: ['access:ai_assistant'], timeout: { idleSocket: 20 * 60 * 1000 // 20 minutes } }, handler: async resources => { const client = await resources.service.getClient({ request: resources.request }); if (!client) { throw (0, _boom.notImplemented)(); } await client.setupKnowledgeBase(); return {}; } }); const functionRoutes = { ...functionElasticsearchRoute, ...functionRecallRoute, ...functionSummariseRoute, ...setupKnowledgeBaseRoute, ...getKnowledgeBaseStatus, ...functionAlertsRoute }; exports.functionRoutes = functionRoutes;