Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security solution] Assistant package assistant/index.tsx cleanup #190151

Merged
merged 37 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
9172fe5
cleanup welcome convo
stephmilovic Aug 5, 2024
3f133fa
more wip
stephmilovic Aug 5, 2024
00aca9f
more
stephmilovic Aug 6, 2024
6dc33a8
Merge branch 'main' into assistant_package_cleanup
stephmilovic Aug 6, 2024
67e4877
Merge branch 'main' into assistant_package_cleanup
stephmilovic Aug 7, 2024
aacc48d
Merge branch 'main' into assistant_package_cleanup
stephmilovic Aug 7, 2024
476c353
it practically works
stephmilovic Aug 7, 2024
de63956
update
stephmilovic Aug 7, 2024
ed77db6
fix type
stephmilovic Aug 8, 2024
03a1825
fix useEffect
stephmilovic Aug 8, 2024
8294f72
another great fix
stephmilovic Aug 8, 2024
952e0c8
fix tests
stephmilovic Aug 8, 2024
7ba2f75
add test
stephmilovic Aug 8, 2024
d169910
remove unused files
stephmilovic Aug 8, 2024
7814717
fixes
stephmilovic Aug 8, 2024
eabd4b4
update quick prompt color
stephmilovic Aug 8, 2024
65ef9f4
fix test
stephmilovic Aug 8, 2024
fe36c24
more better?
stephmilovic Aug 9, 2024
74d4d42
comment
stephmilovic Aug 12, 2024
42ec4c1
rm unused consts
stephmilovic Aug 12, 2024
1453e91
Merge branch 'main' into assistant_package_cleanup
stephmilovic Aug 12, 2024
d4628e3
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Aug 12, 2024
2677caf
fix type
stephmilovic Aug 12, 2024
0d79e29
fix
stephmilovic Aug 12, 2024
aa90c5d
Merge branch 'main' into assistant_package_cleanup
stephmilovic Aug 12, 2024
b8b9fc9
fix translations
stephmilovic Aug 12, 2024
6905d68
Merge branch 'main' into assistant_package_cleanup
stephmilovic Aug 13, 2024
d230696
fix loading
stephmilovic Aug 13, 2024
7926944
fix connector icons
stephmilovic Aug 13, 2024
486c828
Merge branch 'main' into assistant_package_cleanup
elasticmachine Aug 13, 2024
dac74f1
fix system prompt on chat clear
stephmilovic Aug 14, 2024
ba548b5
Merge branch 'main' into assistant_package_cleanup
stephmilovic Aug 14, 2024
c1edeec
update loading icon
stephmilovic Aug 14, 2024
405968d
fix basic license
stephmilovic Aug 15, 2024
6a67d87
revert entries delete
stephmilovic Aug 15, 2024
c81feb3
x
stephmilovic Aug 15, 2024
1382cd4
Merge branch 'main' into assistant_package_cleanup
spong Aug 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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, { Dispatch, SetStateAction } from 'react';
import { EuiFlexGroup, EuiFlexItem, EuiPanel, EuiText } from '@elastic/eui';
import { css } from '@emotion/react';
import { PromptResponse } from '@kbn/elastic-assistant-common';
import { QueryObserverResult } from '@tanstack/react-query';
import { Conversation } from '../../..';
import { AssistantAnimatedIcon } from '../assistant_animated_icon';
import { SystemPrompt } from '../prompt_editor/system_prompt';
import { SetupKnowledgeBaseButton } from '../../knowledge_base/setup_knowledge_base_button';
import * as i18n from '../translations';

interface Props {
currentConversation: Conversation | undefined;
currentSystemPromptId: string | undefined;
handleOnSystemPromptSelectionChange: (systemPromptId?: string) => void;

isSettingsModalVisible: boolean;
refetchCurrentUserConversations: () => Promise<
QueryObserverResult<Record<string, Conversation>, unknown>
>;
setIsSettingsModalVisible: Dispatch<SetStateAction<boolean>>;
allSystemPrompts: PromptResponse[];
}

export const EmptyConvo: React.FC<Props> = ({
allSystemPrompts,
currentConversation,
currentSystemPromptId,
handleOnSystemPromptSelectionChange,
isSettingsModalVisible,
refetchCurrentUserConversations,
setIsSettingsModalVisible,
}) => {
return (
<EuiFlexGroup alignItems="center" justifyContent="center">
<EuiFlexItem grow={false}>
<EuiPanel
hasShadow={false}
css={css`
max-width: 400px;
text-align: center;
`}
>
<EuiFlexGroup alignItems="center" justifyContent="center" direction="column">
<EuiFlexItem grow={false}>
<AssistantAnimatedIcon />
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiText>
<h3>{i18n.EMPTY_SCREEN_TITLE}</h3>
<p>{i18n.EMPTY_SCREEN_DESCRIPTION}</p>
</EuiText>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<SystemPrompt
conversation={currentConversation}
currentSystemPromptId={currentSystemPromptId}
onSystemPromptSelectionChange={handleOnSystemPromptSelectionChange}
isSettingsModalVisible={isSettingsModalVisible}
setIsSettingsModalVisible={setIsSettingsModalVisible}
allSystemPrompts={allSystemPrompts}
refetchConversations={refetchCurrentUserConversations}
/>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<SetupKnowledgeBaseButton />
</EuiFlexItem>
</EuiFlexGroup>
</EuiPanel>
</EuiFlexItem>
</EuiFlexGroup>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* 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, {
Dispatch,
FunctionComponent,
SetStateAction,
useEffect,
useMemo,
useRef,
} from 'react';
import { EuiFlexGroup, EuiFlexItem, EuiPanel, EuiText } from '@elastic/eui';
import { HttpSetup } from '@kbn/core-http-browser';
import { euiThemeVars } from '@kbn/ui-theme';
import { css } from '@emotion/react';
import { PromptResponse } from '@kbn/elastic-assistant-common';
import { QueryObserverResult } from '@tanstack/react-query';
import { EmptyConvo } from './empty_convo';
import { WelcomeSetup } from './welcome_setup';
import { Conversation } from '../../..';
import { UpgradeLicenseCallToAction } from '../upgrade_license_cta';
import * as i18n from '../translations';
interface Props {
allSystemPrompts: PromptResponse[];
comments: JSX.Element;
currentConversation: Conversation | undefined;
currentSystemPromptId: string | undefined;
handleOnConversationSelected: ({ cId, cTitle }: { cId: string; cTitle: string }) => Promise<void>;
handleOnSystemPromptSelectionChange: (systemPromptId?: string) => void;
isAssistantEnabled: boolean;
isSettingsModalVisible: boolean;
isWelcomeSetup: boolean;
refetchCurrentUserConversations: () => Promise<
QueryObserverResult<Record<string, Conversation>, unknown>
>;
http: HttpSetup;
setIsSettingsModalVisible: Dispatch<SetStateAction<boolean>>;
}

export const AssistantBody: FunctionComponent<Props> = ({
allSystemPrompts,
comments,
currentConversation,
currentSystemPromptId,
handleOnConversationSelected,
handleOnSystemPromptSelectionChange,
http,
isAssistantEnabled,
isSettingsModalVisible,
isWelcomeSetup,
refetchCurrentUserConversations,
setIsSettingsModalVisible,
}) => {
const isNewConversation = useMemo(
() => currentConversation?.messages.length === 0,
[currentConversation?.messages.length]
);

const disclaimer = useMemo(
() =>
isNewConversation && (
<EuiText
data-test-subj="assistant-disclaimer"
textAlign="center"
color={euiThemeVars.euiColorMediumShade}
size="xs"
css={css`
margin: 0 ${euiThemeVars.euiSizeL} ${euiThemeVars.euiSizeM} ${euiThemeVars.euiSizeL};
`}
>
{i18n.DISCLAIMER}
</EuiText>
),
[isNewConversation]
);

// Start Scrolling
const commentsContainerRef = useRef<HTMLDivElement | null>(null);

useEffect(() => {
const parent = commentsContainerRef.current?.parentElement;

if (!parent) {
return;
}
// when scrollHeight changes, parent is scrolled to bottom
parent.scrollTop = parent.scrollHeight;

(
commentsContainerRef.current?.childNodes[0].childNodes[0] as HTMLElement
).lastElementChild?.scrollIntoView();
});
// End Scrolling

if (!isAssistantEnabled) {
return <UpgradeLicenseCallToAction http={http} />;
}

return (
<EuiFlexGroup direction="column" justifyContent="spaceBetween">
<EuiFlexItem grow={false}>
{isWelcomeSetup ? (
<WelcomeSetup
currentConversation={currentConversation}
handleOnConversationSelected={handleOnConversationSelected}
/>
) : currentConversation?.messages.length === 0 ? (
<EmptyConvo
allSystemPrompts={allSystemPrompts}
currentConversation={currentConversation}
currentSystemPromptId={currentSystemPromptId}
handleOnSystemPromptSelectionChange={handleOnSystemPromptSelectionChange}
isSettingsModalVisible={isSettingsModalVisible}
refetchCurrentUserConversations={refetchCurrentUserConversations}
setIsSettingsModalVisible={setIsSettingsModalVisible}
/>
) : (
<EuiPanel
hasShadow={false}
panelRef={(element) => {
commentsContainerRef.current = (element?.parentElement as HTMLDivElement) || null;
}}
>
{comments}
</EuiPanel>
)}
</EuiFlexItem>
<EuiFlexItem grow={false}>{disclaimer}</EuiFlexItem>
</EuiFlexGroup>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 from 'react';
import { EuiFlexGroup, EuiFlexItem, EuiPanel, EuiText } from '@elastic/eui';
import { css } from '@emotion/react';
import { Conversation } from '../../..';
import { AssistantAnimatedIcon } from '../assistant_animated_icon';
import { ConnectorSetup } from '../../connectorland/connector_setup';
import * as i18n from '../translations';

interface Props {
currentConversation: Conversation | undefined;
handleOnConversationSelected: ({ cId, cTitle }: { cId: string; cTitle: string }) => Promise<void>;
}

export const WelcomeSetup: React.FC<Props> = ({
handleOnConversationSelected,
currentConversation,
}) => {
return (
<EuiFlexGroup alignItems="center" justifyContent="center">
<EuiFlexItem grow={false}>
<EuiPanel
hasShadow={false}
css={css`
max-width: 400px;
text-align: center;
`}
>
<EuiFlexGroup alignItems="center" justifyContent="center" direction="column">
<EuiFlexItem grow={false}>
<AssistantAnimatedIcon />
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiText>
<h3>{i18n.WELCOME_SCREEN_TITLE}</h3>
</EuiText>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiText color="subdued">
<p>{i18n.WELCOME_SCREEN_DESCRIPTION}</p>
</EuiText>
</EuiFlexItem>
<EuiFlexItem grow={false} data-test-subj="connector-prompt">
<ConnectorSetup
conversation={currentConversation}
onConversationUpdate={handleOnConversationSelected}
/>
</EuiFlexItem>
</EuiFlexGroup>
</EuiPanel>
</EuiFlexItem>
</EuiFlexGroup>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const testProps = {
onChatCleared: jest.fn(),
showAnonymizedValues: false,
conversations: mockConversations,
refetchConversationsState: jest.fn(),
refetchCurrentUserConversations: jest.fn(),
isAssistantEnabled: true,
anonymizationFields: { total: 0, page: 1, perPage: 1000, data: [] },
refetchAnonymizationFieldsResults: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { css } from '@emotion/react';
import { euiThemeVars } from '@kbn/ui-theme';
import { isEmpty } from 'lodash';
import { DataStreamApis } from '../use_data_stream_apis';
import { Conversation } from '../../..';
import { AssistantTitle } from '../assistant_title';
import { ConnectorSelectorInline } from '../../connectorland/connector_selector_inline/connector_selector_inline';
Expand All @@ -43,7 +44,7 @@ interface OwnProps {
onConversationSelected: ({ cId, cTitle }: { cId: string; cTitle: string }) => void;
conversations: Record<string, Conversation>;
conversationsLoaded: boolean;
refetchConversationsState: () => Promise<void>;
refetchCurrentUserConversations: DataStreamApis['refetchCurrentUserConversations'];
onConversationCreate: () => Promise<void>;
isAssistantEnabled: boolean;
refetchPrompts?: (
Expand Down Expand Up @@ -72,7 +73,7 @@ export const AssistantHeader: React.FC<Props> = ({
onConversationSelected,
conversations,
conversationsLoaded,
refetchConversationsState,
refetchCurrentUserConversations,
onConversationCreate,
isAssistantEnabled,
refetchPrompts,
Expand Down Expand Up @@ -164,7 +165,7 @@ export const AssistantHeader: React.FC<Props> = ({
onConversationSelected={onConversationSelected}
conversations={conversations}
conversationsLoaded={conversationsLoaded}
refetchConversationsState={refetchConversationsState}
refetchCurrentUserConversations={refetchCurrentUserConversations}
refetchPrompts={refetchPrompts}
/>
</EuiFlexItem>
Expand Down Expand Up @@ -199,7 +200,7 @@ export const AssistantHeader: React.FC<Props> = ({
<AssistantTitle
title={selectedConversation?.title}
selectedConversation={selectedConversation}
refetchConversationsState={refetchConversationsState}
refetchCurrentUserConversations={refetchCurrentUserConversations}
/>
</EuiFlexItem>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ export const UnifiedTimelineGlobalStyles = createGlobalStyle`

export const AssistantOverlay = React.memo<Props>(({ currentUserAvatar }) => {
const [isModalVisible, setIsModalVisible] = useState(false);
const [conversationTitle, setConversationTitle] = useState<string | undefined>(
WELCOME_CONVERSATION_TITLE
);
const [conversationTitle, setConversationTitle] = useState<string>(WELCOME_CONVERSATION_TITLE);
const [promptContextId, setPromptContextId] = useState<string | undefined>();
const { assistantTelemetry, setShowAssistantOverlay, getLastConversationId } =
useAssistantContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const testProps = {
docLinks: { ELASTIC_WEBSITE_URL: 'https://www.elastic.co/', DOC_LINK_VERSION: '7.15' },
selectedConversation: undefined,
onChange: jest.fn(),
refetchConversationsState: jest.fn(),
refetchCurrentUserConversations: jest.fn(),
};

describe('AssistantTitle', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import React, { useCallback, useEffect, useState } from 'react';
import { EuiFlexGroup, EuiFlexItem, EuiInlineEditTitle } from '@elastic/eui';
import { css } from '@emotion/react';
import { DataStreamApis } from '../use_data_stream_apis';
import type { Conversation } from '../../..';
import { AssistantAvatar } from '../assistant_avatar/assistant_avatar';
import { useConversation } from '../use_conversation';
Expand All @@ -20,8 +21,8 @@ import { NEW_CHAT } from '../conversations/conversation_sidepanel/translations';
export const AssistantTitle: React.FC<{
title?: string;
selectedConversation: Conversation | undefined;
refetchConversationsState: () => Promise<void>;
}> = ({ title, selectedConversation, refetchConversationsState }) => {
refetchCurrentUserConversations: DataStreamApis['refetchCurrentUserConversations'];
}> = ({ title, selectedConversation, refetchCurrentUserConversations }) => {
const [newTitle, setNewTitle] = useState(title);
const [newTitleError, setNewTitleError] = useState(false);
const { updateConversationTitle } = useConversation();
Expand All @@ -35,10 +36,10 @@ export const AssistantTitle: React.FC<{
conversationId: selectedConversation.id,
updatedTitle,
});
await refetchConversationsState();
await refetchCurrentUserConversations();
}
},
[refetchConversationsState, selectedConversation, updateConversationTitle]
[refetchCurrentUserConversations, selectedConversation, updateConversationTitle]
);

useEffect(() => {
Expand Down
Loading