|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import expect from '@kbn/expect'; |
| 9 | +import { MessageAddEvent, MessageRole } from '@kbn/observability-ai-assistant-plugin/common'; |
| 10 | +import { VisualizeESQLUserIntention } from '@kbn/observability-ai-assistant-plugin/common/functions/visualize_esql'; |
| 11 | +import { |
| 12 | + LlmProxy, |
| 13 | + createLlmProxy, |
| 14 | +} from '../../../../../../../observability_ai_assistant_api_integration/common/create_llm_proxy'; |
| 15 | +import { |
| 16 | + getMessageAddedEvents, |
| 17 | + invokeChatCompleteWithFunctionRequest, |
| 18 | +} from '../../utils/conversation'; |
| 19 | +import type { DeploymentAgnosticFtrProviderContext } from '../../../../../ftr_provider_context'; |
| 20 | + |
| 21 | +export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderContext) { |
| 22 | + const es = getService('es'); |
| 23 | + const log = getService('log'); |
| 24 | + const observabilityAIAssistantAPIClient = getService('observabilityAIAssistantApi'); |
| 25 | + |
| 26 | + describe('visualize_query', function () { |
| 27 | + this.tags(['skipCloud']); |
| 28 | + let llmProxy: LlmProxy; |
| 29 | + let connectorId: string; |
| 30 | + let events: MessageAddEvent[]; |
| 31 | + const query = `FROM test_index`; |
| 32 | + |
| 33 | + before(async () => { |
| 34 | + llmProxy = await createLlmProxy(log); |
| 35 | + connectorId = await observabilityAIAssistantAPIClient.createProxyActionConnector({ |
| 36 | + port: llmProxy.getPort(), |
| 37 | + }); |
| 38 | + await es.index({ |
| 39 | + index: 'test_index', |
| 40 | + refresh: true, |
| 41 | + id: 'index_id', |
| 42 | + document: { bar: 'foo' }, |
| 43 | + }); |
| 44 | + void llmProxy.interceptWithResponse('Hello from LLM Proxy'); |
| 45 | + const responseBody = await invokeChatCompleteWithFunctionRequest({ |
| 46 | + connectorId, |
| 47 | + observabilityAIAssistantAPIClient, |
| 48 | + functionCall: { |
| 49 | + name: 'visualize_query', |
| 50 | + trigger: MessageRole.Assistant, |
| 51 | + arguments: JSON.stringify({ |
| 52 | + query, |
| 53 | + intention: VisualizeESQLUserIntention.visualizeAuto, |
| 54 | + }), |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + await llmProxy.waitForAllInterceptorsToHaveBeenCalled(); |
| 59 | + |
| 60 | + events = getMessageAddedEvents(responseBody); |
| 61 | + }); |
| 62 | + |
| 63 | + after(async () => { |
| 64 | + await es.indices.delete({ |
| 65 | + index: 'test_index', |
| 66 | + }); |
| 67 | + llmProxy.close(); |
| 68 | + await observabilityAIAssistantAPIClient.deleteActionConnector({ |
| 69 | + actionId: connectorId, |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should execute the visualize_query function and return expected messages', async () => { |
| 74 | + const functionResponse = events[0]; |
| 75 | + expect(functionResponse.message.message.name).to.be('visualize_query'); |
| 76 | + |
| 77 | + const parsedResponse = JSON.parse(functionResponse.message.message.content!); |
| 78 | + expect(parsedResponse.message).to.contain(query); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should contain expected document data in response', async () => { |
| 82 | + const functionResponse = events[0]; |
| 83 | + const parsedData = JSON.parse(functionResponse.message.message.data!); |
| 84 | + |
| 85 | + expect(parsedData.columns[0].id).to.be('bar'); |
| 86 | + expect(parsedData.rows[0][0]).to.be('foo'); |
| 87 | + expect(parsedData).to.have.property('correctedQuery', query); |
| 88 | + }); |
| 89 | + }); |
| 90 | +} |
0 commit comments