From 89686255abc1c63cdcfed005dda3fec859f012aa Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 13 Nov 2024 07:16:24 +1100 Subject: [PATCH] [8.x] [Obs AI Assistant] Include an AdHoc instruction about the slack connector to avoid executing a loop (#199531) (#199860) # Backport This will backport the following commits from `main` to `8.x`: - [[Obs AI Assistant] Include an AdHoc instruction about the slack connector to avoid executing a loop (#199531)](https://github.com/elastic/kibana/pull/199531) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Viduni Wickramarachchi --- .../server/functions/execute_connector.ts | 4 ++- .../server/rule_connector/index.ts | 33 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant/server/functions/execute_connector.ts b/x-pack/plugins/observability_solution/observability_ai_assistant/server/functions/execute_connector.ts index 0088e35a6f6af..70f4ead5e6bb6 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant/server/functions/execute_connector.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant/server/functions/execute_connector.ts @@ -8,13 +8,15 @@ import { FunctionRegistrationParameters } from '.'; import { FunctionVisibility } from '../../common'; +export const EXECUTE_CONNECTOR_FUNCTION_NAME = 'execute_connector'; + export function registerExecuteConnectorFunction({ functions, resources, }: FunctionRegistrationParameters) { functions.registerFunction( { - name: 'execute_connector', + name: EXECUTE_CONNECTOR_FUNCTION_NAME, description: 'Use this function when user explicitly asks to call a kibana connector.', visibility: FunctionVisibility.AssistantOnly, parameters: { diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant_app/server/rule_connector/index.ts b/x-pack/plugins/observability_solution/observability_ai_assistant_app/server/rule_connector/index.ts index 59b883fef9c18..33f3bdd2c98f8 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant_app/server/rule_connector/index.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant_app/server/rule_connector/index.ts @@ -23,7 +23,6 @@ import { JiraParamsSchema, PagerdutyParamsSchema, SlackApiParamsSchema, - SlackParamsSchema, WebhookParamsSchema, } from '@kbn/stack-connectors-plugin/server'; import { ObservabilityAIAssistantRouteHandlerResources } from '@kbn/observability-ai-assistant-plugin/server/routes/types'; @@ -37,14 +36,26 @@ import { CompatibleJSONSchema } from '@kbn/observability-ai-assistant-plugin/com import { AlertDetailsContextualInsightsService } from '@kbn/observability-plugin/server/services'; import { getSystemMessageFromInstructions } from '@kbn/observability-ai-assistant-plugin/server/service/util/get_system_message_from_instructions'; import { AdHocInstruction } from '@kbn/observability-ai-assistant-plugin/common/types'; +import { EXECUTE_CONNECTOR_FUNCTION_NAME } from '@kbn/observability-ai-assistant-plugin/server/functions/execute_connector'; import { convertSchemaToOpenApi } from './convert_schema_to_open_api'; import { OBSERVABILITY_AI_ASSISTANT_CONNECTOR_ID } from '../../common/rule_connector'; const CONNECTOR_PRIVILEGES = ['api:observabilityAIAssistant', 'app:observabilityAIAssistant']; const connectorParamsSchemas: Record = { + '.slack': { + type: 'object', + properties: { + id: { type: 'string' }, + params: { + type: 'object', + properties: { + message: { type: 'string' }, + }, + }, + }, + }, '.slack_api': convertSchemaToOpenApi(SlackApiParamsSchema), - '.slack': convertSchemaToOpenApi(SlackParamsSchema), '.email': convertSchemaToOpenApi(EmailParamsSchema), '.webhook': convertSchemaToOpenApi(WebhookParamsSchema), '.jira': convertSchemaToOpenApi(JiraParamsSchema), @@ -189,6 +200,24 @@ If available, include the link of the conversation at the end of your answer.` ), }; + const hasSlackConnector = !!connectorsList.filter( + (connector) => connector.actionTypeId === '.slack' + ).length; + + if (hasSlackConnector && functionClient.hasFunction(EXECUTE_CONNECTOR_FUNCTION_NAME)) { + const slackConnectorInstruction: AdHocInstruction = { + instruction_type: 'application_instruction', + text: dedent( + `The execute_connector function can be used to invoke Kibana connectors. + To send to the Slack connector, you need the following arguments: + - the "id" of the connector + - the "params" parameter that you will fill with the message + Please include both "id" and "params.message" in the function arguments when executing the Slack connector..` + ), + }; + functionClient.registerAdhocInstruction(slackConnectorInstruction); + } + const alertsContext = await getAlertsContext( execOptions.params.rule, execOptions.params.alerts,