-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
150 changed files
with
7,885 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# @kbn/elastic-assistant | ||
|
||
The `Elastic Assistant` is a user interface for interacting with generative AIs, like `ChatGPT`. | ||
|
||
This package provides: | ||
|
||
- Components for rendering the `Elastic Assistant` | ||
- Hooks for passing context (for example, fields in an alert) to the `Elastic Assistant`, enabling users to include this content in their queries | ||
|
||
## Maintainers | ||
|
||
Maintained by the Security Solution team | ||
|
||
## Running unit tests with code coverage | ||
|
||
To (interactively) run unit tests with code coverage, run the following command: | ||
|
||
```sh | ||
cd $KIBANA_HOME && node scripts/jest --watch x-pack/packages/kbn-elastic-assistant --coverage | ||
``` |
84 changes: 84 additions & 0 deletions
84
x-pack/packages/kbn-elastic-assistant/impl/assistant/api.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { OpenAiProviderType } from '@kbn/stack-connectors-plugin/public/common'; | ||
|
||
import { HttpSetup } from '@kbn/core-http-browser'; | ||
import type { Message } from '../assistant_context/types'; | ||
import { Conversation } from '../assistant_context/types'; | ||
import { API_ERROR } from './translations'; | ||
|
||
export interface FetchConnectorExecuteAction { | ||
apiConfig: Conversation['apiConfig']; | ||
http: HttpSetup; | ||
messages: Message[]; | ||
signal?: AbortSignal | undefined; | ||
} | ||
|
||
export const fetchConnectorExecuteAction = async ({ | ||
http, | ||
messages, | ||
apiConfig, | ||
signal, | ||
}: FetchConnectorExecuteAction): Promise<string> => { | ||
const outboundMessages = messages.map((msg) => ({ | ||
role: msg.role, | ||
content: msg.content, | ||
})); | ||
|
||
const body = | ||
apiConfig?.provider === OpenAiProviderType.OpenAi | ||
? { | ||
model: 'gpt-3.5-turbo', | ||
messages: outboundMessages, | ||
n: 1, | ||
stop: null, | ||
temperature: 0.2, | ||
} | ||
: { | ||
messages: outboundMessages, | ||
}; | ||
|
||
const requestBody = { | ||
params: { | ||
subActionParams: { | ||
body: JSON.stringify(body), | ||
}, | ||
subAction: 'test', | ||
}, | ||
}; | ||
|
||
try { | ||
// TODO: Find return type for this API | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const response = await http.fetch<any>( | ||
`/api/actions/connector/${apiConfig?.connectorId}/_execute`, | ||
{ | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(requestBody), | ||
signal, | ||
} | ||
); | ||
|
||
const data = response.data; | ||
if (response.status !== 'ok') { | ||
return API_ERROR; | ||
} | ||
|
||
if (data.choices && data.choices.length > 0 && data.choices[0].message.content) { | ||
const result = data.choices[0].message.content.trim(); | ||
return result; | ||
} else { | ||
return API_ERROR; | ||
} | ||
} catch (error) { | ||
return API_ERROR; | ||
} | ||
}; |
90 changes: 90 additions & 0 deletions
90
x-pack/packages/kbn-elastic-assistant/impl/assistant/assistant_overlay/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import React, { useCallback, useEffect, useState } from 'react'; | ||
import { EuiModal } from '@elastic/eui'; | ||
|
||
import useEvent from 'react-use/lib/useEvent'; | ||
// eslint-disable-next-line @kbn/eslint/module_migration | ||
import styled from 'styled-components'; | ||
import { ShowAssistantOverlayProps, useAssistantContext } from '../../assistant_context'; | ||
import { Assistant } from '..'; | ||
import { WELCOME_CONVERSATION_TITLE } from '../use_conversation/translations'; | ||
|
||
const isMac = navigator.platform.toLowerCase().indexOf('mac') >= 0; | ||
|
||
const StyledEuiModal = styled(EuiModal)` | ||
min-width: 1200px; | ||
max-height: 100%; | ||
height: 100%; | ||
`; | ||
|
||
/** | ||
* Modal container for Security Assistant conversations, receiving the page contents as context, plus whatever | ||
* component currently has focus and any specific context it may provide through the SAssInterface. | ||
*/ | ||
export const AssistantOverlay: React.FC = React.memo(() => { | ||
const [isModalVisible, setIsModalVisible] = useState(false); | ||
const [conversationId, setConversationId] = useState<string | undefined>( | ||
WELCOME_CONVERSATION_TITLE | ||
); | ||
const [promptContextId, setPromptContextId] = useState<string | undefined>(); | ||
const { setShowAssistantOverlay } = useAssistantContext(); | ||
|
||
// Bind `showAssistantOverlay` in SecurityAssistantContext to this modal instance | ||
const showOverlay = useCallback( | ||
() => | ||
({ | ||
showOverlay: so, | ||
promptContextId: pid, | ||
conversationId: cid, | ||
}: ShowAssistantOverlayProps) => { | ||
setIsModalVisible(so); | ||
setPromptContextId(pid); | ||
setConversationId(cid); | ||
}, | ||
[setIsModalVisible] | ||
); | ||
useEffect(() => { | ||
setShowAssistantOverlay(showOverlay); | ||
}, [setShowAssistantOverlay, showOverlay]); | ||
|
||
// Register keyboard listener to show the modal when cmd + ; is pressed | ||
const onKeyDown = useCallback( | ||
(event: KeyboardEvent) => { | ||
if (event.key === ';' && (isMac ? event.metaKey : event.ctrlKey)) { | ||
event.preventDefault(); | ||
setIsModalVisible(!isModalVisible); | ||
} | ||
}, | ||
[isModalVisible] | ||
); | ||
useEvent('keydown', onKeyDown); | ||
|
||
// Modal control functions | ||
const cleanupAndCloseModal = useCallback(() => { | ||
setIsModalVisible(false); | ||
setPromptContextId(undefined); | ||
setConversationId(conversationId); | ||
}, [conversationId]); | ||
|
||
const handleCloseModal = useCallback(() => { | ||
cleanupAndCloseModal(); | ||
}, [cleanupAndCloseModal]); | ||
|
||
return ( | ||
<> | ||
{isModalVisible && ( | ||
<StyledEuiModal onClose={handleCloseModal}> | ||
<Assistant conversationId={conversationId} promptContextId={promptContextId} /> | ||
</StyledEuiModal> | ||
)} | ||
</> | ||
); | ||
}); | ||
|
||
AssistantOverlay.displayName = 'AssistantOverlay'; |
15 changes: 15 additions & 0 deletions
15
x-pack/packages/kbn-elastic-assistant/impl/assistant/assistant_overlay/translations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
|
||
export const CANCEL_BUTTON = i18n.translate( | ||
'xpack.elasticAssistant.assistant.overlay.CancelButton', | ||
{ | ||
defaultMessage: 'Cancel', | ||
} | ||
); |
Oops, something went wrong.