kibana/x-pack/platform/packages/shared/kbn-elastic-assistant/index.ts
Kenneth Kreindler e3976c9c0f
[Security Solution] [AI assistant] Make the Security AI assistant global (#223936)
## Summary

Fixes https://github.com/elastic/security-team/issues/8934

Summarize your PR. If it involves visual changes include a screenshot or
gif.

This PR enables the Security AI assistant to be used globally - i.e. if
you are outside of the security solution (e.g. Discover), the Security
Assistant can still be opened.

Changes:
- A public module has been added to the elastic-assistant plugin
(previously it was a server-side-only plugin).
- The vast majority of the assistant (flyout and nav bar) has been moved
into the new elastic-assistantpublic plugin.
- Comment actions & message augmentations remain within the
security-solution.
- A new public plugin was created called elastic-assistant-shared state.
This plugin is used to share state between the elastic-assistant public
plugin and other plugins (e.g. security-solution).
- For example, the security solution registers comment actions in the
elastic-assistant-shared-state plugin. The elastic-assistant public
plugin then reads the comment actions from
elastic-assistant-shared-state and renders them in the assistant flyout.


![image](https://github.com/user-attachments/assets/3322e434-f2f4-42c7-ac8a-63070a1cb9ca)

### Considerations:
- Currently, the Security AI assistant is being displayed everywhere
except the observability solution (see implementation
[here](https://github.com/elastic/kibana/pull/223936/files#diff-5dd1ea91c2d5d242203cc58ee59ec283116e5e739ed82bae4e2cd78af322150c)).
This is only for testing while the PR is in review. We plan to add a
setting to the stack management that allows the user to configure where
they would like the assistant to be shown. This will be changed before
the PR is merged.

## How to test

Feel free to use the cloud and serverless deployments created by the CI
pipeline for testing. Credentials can be found on Buildkite.

### Verify that the Security AI assistant works as expected within the
security solution
Expected there to be no changes in how the security AI assistant works
within the Security Solution. Please do some exploratory testing to make
sure nothing has changed.

Start the branch locally and go to http://localhost:5601/app/security/

Things to test:
- Does the assistant open?
- Can I send an alert to the assistant from the alerts page?
- Does the assistant display code blocks correctly?
- Does the assistant display ESQL correctly (can I view the ESQL in the
timeline)?
- Do assistant messages have the correct comment actions? Do the comment
actions work?
- Are conversations displayed correctly?
- Do citations work?
- Does the assistant work in serverless? Does the assistant work as
expected in AI4SOC?
- Do quick prompts work?
- Can you select a system prompt for a new convo?
- Can you send alerts to the Security AI assistant?
- AI assistant in a space that has Security disabled.
- Does attack discovery work?

AI assistant open in Discover app:
<img width="1841" alt="image"
src="https://github.com/user-attachments/assets/0a13a100-d192-4fa4-b395-0951452e14c2"
/>

AI assistant in Security solution:
<img width="1841" alt="image"
src="https://github.com/user-attachments/assets/7ed38f37-79de-41a7-a80f-8b96147bfdf6"
/>


### Verify the Security AI assistant works in Discover (or anywhere
outside of the Security solution)?
Head over to http://localhost:5601/app/discover. Note that some
functionality is removed when using the AI assistant outside of
security:
- Only the "copy" comment action appears on messages.
- Code block augmentations (i.e. the button that opens ESQL inside of
the timeline) don't appear.

Things to test:
- Does the security AI assistant button appear in the nav bar?
- Can you open the security AI assistant?
- Are you able to send messages?
- Are conversations appearing as expected?
- Can you close the assistant?
- Do citations work?
- Can you switch to a different solution while the assistant is open?

Security AI assistant open in AI4SOC Discover:
<img width="1841" alt="image"
src="https://github.com/user-attachments/assets/36537b9b-e945-459e-ac13-43e9444e92b7"
/>


### Checklist

Check the PR satisfies following conditions. 

Reviewers should verify this PR satisfies this list as well.

- [X] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)
- [X]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [X] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [X] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [X] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.
- [X] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [X] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

### Identify risks

Does this PR introduce any risks? For example, consider risks like hard
to test bugs, performance regression, potential of data loss.

Describe the risk, its severity, and mitigation for each identified
risk. Invite stakeholders and evaluate how to proceed before merging.

- [ ] [See some risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
- [ ] ...

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
2025-06-24 19:11:43 +01:00

202 lines
9.4 KiB
TypeScript

/*
* 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.
*/
// To integrate the assistant into a Kibana app, perform the following three steps:
// Step 1: Wrap your Kibana app in the `AssistantProvider` component. This typically
// happens in the root of your app. Optionally provide a custom title for the assistant:
/** provides context (from the app) to the assistant, and injects Kibana services, like `http` */
export { AssistantProvider, useAssistantContext } from './impl/assistant_context';
// Step 2.1: Add the `AssistantOverlay` component to your app. This component displays the assistant
// overlay in a modal, bound to a shortcut key:
/** modal overlay for Elastic Assistant conversations */
export { AssistantOverlay } from './impl/assistant/assistant_overlay';
// In addition to the `AssistantOverlay`, or as an alternative, you may use the `Assistant` component
// to display the assistant without the modal overlay:
/** this component renders the Assistant without the modal overlay to, for example, render it in a Timeline tab */
export { Assistant } from './impl/assistant';
// Step 2.2: Provide spaceId to `AssistantSpaceIdProvider`
// The spaceId here will be used to fetch the assistant data from localstorage.
// So make sure not to provide null, undefined, or any fallback spaceId.
// Only render the `AssistantSpaceIdProvider` component when the spaceId is available.
export {
AssistantSpaceIdProvider,
useAssistantLastConversation,
} from './impl/assistant/use_space_aware_context';
// Step 3: Wherever you want to bring context into the assistant, use the any combination of the following
// components and hooks:
// - `NewChat` component
// - `NewChatByTitle` component
// - `useAssistantOverlay` hook
/**
* `NewChat` displays a _New chat_ icon button, providing all the context
* necessary to start a new chat. You may optionally style the button icon,
* or override the default _New chat_ text with custom content, like `🪄✨`
*
* USE THIS WHEN: All the data necessary to start a new chat is available
* in the same part of the React tree as the _New chat_ button.
*/
export { NewChat } from './impl/new_chat';
/**
* `NewChatByTitle` displays a _New chat_ icon button by providing only the `promptContextId`
* of a context that was (already) registered by the `useAssistantOverlay` hook. You may
* optionally style the button icon, or override the default _New chat_ text with custom
* content, like {'🪄✨'}
*
* USE THIS WHEN: all the data necessary to start a new chat is NOT available
* in the same part of the React tree as the _New chat_ button. When paired
* with the `useAssistantOverlay` hook, this option enables context to be be
* registered where the data is available, and then the _New chat_ button can be displayed
* in another part of the tree.
*/
export { NewChatByTitle } from './impl/new_chat_by_title';
/**
* `useAssistantOverlay` is a hook that registers context with the assistant overlay, and
* returns an optional `showAssistantOverlay` function to display the assistant overlay.
* As an alterative to using the `showAssistantOverlay` returned from this hook, you may
* use the `NewChatByTitle` component and pass it the `promptContextId` returned by this hook.
*
* USE THIS WHEN: You want to register context in one part of the tree, and then show
* a _New chat_ button in another part of the tree without passing around the data, or when
* you want to build a custom `New chat` button with features not not provided by the
* `NewChat` component.
*/
export { useAssistantOverlay } from './impl/assistant/use_assistant_overlay';
/** a helper that enriches content returned from a query with action buttons */
export { analyzeMarkdown } from './impl/assistant/use_conversation/helpers';
export { ConnectorSelectorInline } from './impl/connectorland/connector_selector_inline/connector_selector_inline';
export {
/** The Attack discovery local storage key */
ATTACK_DISCOVERY_STORAGE_KEY,
DEFAULT_ASSISTANT_NAMESPACE,
/** The default maximum number of alerts to be sent as context when generating Attack discoveries */
DEFAULT_ATTACK_DISCOVERY_MAX_ALERTS,
DEFAULT_LATEST_ALERTS,
DEFEND_INSIGHTS_STORAGE_KEY,
/** The end of the date range of alerts, sent as context to the LLM */
END_LOCAL_STORAGE_KEY,
/** Search bar filters that apply to the alerts sent as context to the LLM */
FILTERS_LOCAL_STORAGE_KEY,
KNOWLEDGE_BASE_LOCAL_STORAGE_KEY,
/** The local storage key that specifies the maximum number of alerts to send as context */
MAX_ALERTS_LOCAL_STORAGE_KEY,
/** The history view's end of the date range of attack discoveries */
HISTORY_END_LOCAL_STORAGE_KEY,
/** The history view's Search bar query that apply to the alerts sent as context to the LLM */
HISTORY_QUERY_LOCAL_STORAGE_KEY,
/** The history view's start date range of attack discoveries */
HISTORY_START_LOCAL_STORAGE_KEY,
/** Search bar query that apply to the alerts sent as context to the LLM */
QUERY_LOCAL_STORAGE_KEY,
/** The local storage key that specifies whether the settings tour should be shown */
SHOW_SETTINGS_TOUR_LOCAL_STORAGE_KEY,
/** The start of the date range of alerts, sent as context to the LLM */
START_LOCAL_STORAGE_KEY,
} from './impl/assistant_context/constants';
export type { AIConnector } from './impl/connectorland/connector_selector';
export { useLoadConnectors } from './impl/connectorland/use_load_connectors';
export type {
/** for rendering results in a code block */
CodeBlockDetails,
/** the type of query that will be executed for a code block */
QueryType,
} from './impl/assistant/use_conversation/helpers';
export type {
/** Feature Availability Interface */
AssistantAvailability,
/** Telemetry Interface */
AssistantTelemetry,
/** Conversation Interface */
Conversation,
/** Message interface on the client */
ClientMessage,
/** Function type to return messages UI */
GetAssistantMessages,
} from './impl/assistant_context/types';
/**
* This interface is used to pass context to the assistant,
* for the purpose of building prompts. Examples of context include:
* - a single alert
* - multiple alerts
* - a single event
* - multiple events
* - markdown
* - csv
* - anything else that the LLM can interpret
*/
export type { PromptContext } from './impl/assistant/prompt_context/types';
/**
* This interface is used to pass a default or base set of contexts to the Elastic Assistant when
* initializing it. This is used to provide 'category' options when users create Quick Prompts.
* Also, useful for collating all of a solutions' prompts in one place.
*
* e.g. see Security Solution's x-pack/solutions/security/plugins/security_solution/public/assistant/prompt_contexts/index.tsx
*/
export type { PromptContextTemplate } from './impl/assistant/prompt_context/types';
export { useFetchCurrentUserConversations } from './impl/assistant/api/conversations/use_fetch_current_user_conversations';
export * from './impl/assistant/api/conversations/bulk_update_actions_conversations';
export { getConversationById } from './impl/assistant/api/conversations/conversations';
export { UpgradeButtons } from './impl/upgrade/upgrade_buttons';
export { getUserConversationsExist, getPrompts, bulkUpdatePrompts } from './impl/assistant/api';
export {
/** A range slider component, typically used to configure the number of alerts sent as context */
AlertsRange,
/** This event occurs when the `AlertsRange` slider is changed */
type SingleRangeChangeEvent,
} from './impl/knowledge_base/alerts_range';
export {
/** A label instructing the user to send fewer alerts */
SELECT_FEWER_ALERTS,
/** Your anonymization settings will apply to these alerts (label) */
YOUR_ANONYMIZATION_SETTINGS,
} from './impl/knowledge_base/translations';
export { SearchAILakeConfigurationsSettingsManagement } from './impl/assistant/settings/search_ai_lake_configurations_settings_management';
export { CONVERSATIONS_TAB } from './impl/assistant/settings/const';
export type { ManagementSettingsTabs } from './impl/assistant/settings/types';
export { getNewSelectedPromptContext } from './impl/data_anonymization/get_new_selected_prompt_context';
export { getCombinedMessage } from './impl/assistant/prompt/helpers';
export { useChatComplete } from './impl/assistant/api/chat_complete/use_chat_complete';
export { useFetchAnonymizationFields } from './impl/assistant/api/anonymization_fields/use_fetch_anonymization_fields';
export interface UseAssistantAvailability {
// True when searchAiLake configurations is available
hasSearchAILakeConfigurations: boolean;
// True when user is Enterprise. When false, the Assistant is disabled and unavailable
isAssistantEnabled: boolean;
// When true, the Assistant is hidden and unavailable
hasAssistantPrivilege: boolean;
// When true, user has `All` privilege for `Connectors and Actions` (show/execute/delete/save ui capabilities)
hasConnectorsAllPrivilege: boolean;
// When true, user has `Read` privilege for `Connectors and Actions` (show/execute ui capabilities)
hasConnectorsReadPrivilege: boolean;
// When true, user has `Edit` privilege for `AnonymizationFields`
hasUpdateAIAssistantAnonymization: boolean;
// When true, user has `Edit` privilege for `Global Knowledge Base`
hasManageGlobalKnowledgeBase: boolean;
}