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

[Observability AI Assistant] Create first Starter prompts #178907

Merged
merged 18 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
Expand Up @@ -86,6 +86,10 @@ export const renderApp = ({
const CloudProvider = plugins.cloud?.CloudContextProvider ?? React.Fragment;
const PresentationContextProvider = plugins.presentationUtil?.ContextProvider ?? React.Fragment;

const clearScreenContext = plugins.observabilityAIAssistant.service.setScreenContext({
starterPrompts: ['Do I have any alerts?', 'How can I create a new rule?', 'What are cases?'],
CoenWarmer marked this conversation as resolved.
Show resolved Hide resolved
});

ReactDOM.render(
<PresentationContextProvider>
<EuiErrorBoundary>
Expand Down Expand Up @@ -141,6 +145,7 @@ export const renderApp = ({
// via the ExploratoryView app, which uses search sessions. Therefore on unmounting we need to clear
// these sessions.
plugins.data.search.session.clear();
clearScreenContext();
ReactDOM.unmountComponentAtNode(element);
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,5 @@ export interface ObservabilityAIAssistantScreenContext {
value: any;
}>;
actions?: ScreenContextActionDefinition[];
starterPrompts?: string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ import type { UseKnowledgeBaseResult } from '../../hooks/use_knowledge_base';
import { useLicense } from '../../hooks/use_license';
import { useObservabilityAIAssistantChatService } from '../../hooks/use_observability_ai_assistant_chat_service';
import { ASSISTANT_SETUP_TITLE, EMPTY_CONVERSATION_TITLE, UPGRADE_LICENSE_TITLE } from '../../i18n';
import { nonNullable } from '../../utils/non_nullable';
import { PromptEditor } from '../prompt_editor/prompt_editor';
import { FlyoutPositionMode } from './chat_flyout';
import { ChatHeader } from './chat_header';
import { ChatTimeline } from './chat_timeline';
import { IncorrectLicensePanel } from './incorrect_license_panel';
import { StarterPrompts } from './starter_prompts';
import { WelcomeMessage } from './welcome_message';

const fullHeightClassName = css`
Expand Down Expand Up @@ -339,30 +341,69 @@ export function ChatBody({
className={animClassName}
>
{connectors.connectors?.length === 0 || messages.length === 1 ? (
<WelcomeMessage connectors={connectors} knowledgeBase={knowledgeBase} />
) : (
<ChatTimeline
messages={messages}
<WelcomeMessage
connectors={connectors}
knowledgeBase={knowledgeBase}
chatService={chatService}
currentUser={currentUser}
chatState={state}
hasConnector={!!connectors.connectors?.length}
onEdit={(editedMessage, newMessage) => {
setStickToBottom(true);
const indexOf = messages.indexOf(editedMessage);
next(messages.slice(0, indexOf).concat(newMessage));
}}
onFeedback={handleFeedback}
onRegenerate={(message) => {
next(reverseToLastUserMessage(messages, message));
}}
onSendTelemetry={(eventWithPayload) =>
chatService.sendAnalyticsEvent(eventWithPayload)
onSelectPrompt={(message) =>
next(
messages.concat([
{
'@timestamp': new Date().toISOString(),
message: { content: message, role: MessageRole.User },
},
])
)
}
onStopGenerating={stop}
onActionClick={handleActionClick}
/>
) : (
<>
<ChatTimeline
messages={messages}
knowledgeBase={knowledgeBase}
chatService={chatService}
currentUser={currentUser}
chatState={state}
hasConnector={!!connectors.connectors?.length}
onEdit={(editedMessage, newMessage) => {
setStickToBottom(true);
const indexOf = messages.indexOf(editedMessage);
next(messages.slice(0, indexOf).concat(newMessage));
}}
onFeedback={handleFeedback}
onRegenerate={(message) => {
next(reverseToLastUserMessage(messages, message));
}}
onSendTelemetry={(eventWithPayload) =>
chatService.sendAnalyticsEvent(eventWithPayload)
}
onStopGenerating={() => {
stop();
}}
onActionClick={handleActionClick}
/>

{!isLoading ? (
<>
<StarterPrompts
userPrompts={messages
CoenWarmer marked this conversation as resolved.
Show resolved Hide resolved
.filter((message) => message.message.role === MessageRole.User)
.map((message) => message.message.content)
.filter(nonNullable)}
onSelectPrompt={(message) =>
next(
messages.concat([
{
'@timestamp': new Date().toISOString(),
message: { content: message, role: MessageRole.User },
},
])
)
}
/>
<EuiSpacer size="l" />
</>
) : null}
</>
)}
</EuiPanel>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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, { useMemo } from 'react';
import { EuiFlexGroup, EuiFlexItem, EuiPanel } from '@elastic/eui';
import { css } from '@emotion/css';
import { useObservabilityAIAssistantAppService } from '../../hooks/use_observability_ai_assistant_app_service';
import { nonNullable } from '../../utils/non_nullable';

const starterPromptClassName = css`
max-width: 50%;
min-width: calc(50% - 8px);
`;

export function StarterPrompts({
userPrompts,
onSelectPrompt,
}: {
userPrompts: string[];
onSelectPrompt: (prompt: string) => void;
}) {
const service = useObservabilityAIAssistantAppService();

const starterPrompts = useMemo(
CoenWarmer marked this conversation as resolved.
Show resolved Hide resolved
() => [
...new Set(
service
.getScreenContexts()
.reverse()
.flatMap((context) => context.starterPrompts)
.filter(nonNullable)
.filter((prompt) => !userPrompts.includes(prompt))
.slice(0, 4)
),
],
[service, userPrompts]
CoenWarmer marked this conversation as resolved.
Show resolved Hide resolved
);

const handleSelectPrompt = (prompt: string) => {
onSelectPrompt(prompt);
};

return (
<EuiFlexGroup direction="row" gutterSize="m" wrap>
{starterPrompts.map((prompt) => (
<EuiFlexItem key={prompt} className={starterPromptClassName}>
<EuiPanel paddingSize="s" onClick={() => handleSelectPrompt(prompt)}>
{prompt}
</EuiPanel>
</EuiFlexItem>
))}
</EuiFlexGroup>
);
}
Loading