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

[Backport 2.x] [Chatbot] Add feature flags for traces and feedback #386

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions common/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const configSchema = schema.object({
enabled: schema.boolean({ defaultValue: true }),
chat: schema.object({
enabled: schema.boolean({ defaultValue: false }),
trace: schema.boolean({ defaultValue: true }),
feedback: schema.boolean({ defaultValue: true }),
}),
incontextInsight: schema.object({
enabled: schema.boolean({ defaultValue: true }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ export const GeneratePopoverBody: React.FC<{
};

const renderInnerFooter = () => {
const traceTip = 'Insight With RAG';
return (
<EuiPopoverFooter className="incontextInsightGeneratePopoverFooter" paddingSize="none">
{
Expand All @@ -332,6 +333,7 @@ export const GeneratePopoverBody: React.FC<{
onViewTrace={() => {
setShowInsight(true);
}}
traceTip={traceTip}
usageCollection={usageCollection}
isOnTrace={showInsight}
metricAppName={metricAppName}
Expand All @@ -345,6 +347,7 @@ export const GeneratePopoverBody: React.FC<{
showFeedback
showTraceIcon={insightAvailable}
onViewTrace={() => {}}
traceTip={traceTip}
usageCollection={usageCollection}
isOnTrace={showInsight}
metricAppName={metricAppName}
Expand Down
4 changes: 3 additions & 1 deletion public/tabs/chat/messages/message_action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface MessageActionsProps {
showTraceIcon?: boolean;
isOnTrace?: boolean;
traceInteractionId?: string;
traceTip?: string;
onViewTrace?: () => void;
shouldActionBarVisibleOnHover?: boolean;
isFullWidth?: boolean;
Expand All @@ -44,6 +45,7 @@ export const MessageActions: React.FC<MessageActionsProps> = ({
showTraceIcon = false,
isOnTrace = false,
traceInteractionId = null,
traceTip = 'info',
onViewTrace,
shouldActionBarVisibleOnHover = false,
isFullWidth = false,
Expand Down Expand Up @@ -146,7 +148,7 @@ export const MessageActions: React.FC<MessageActionsProps> = ({
trace: {
show: showTraceIcon && onViewTrace,
component: renderButtonWithTooltip(
'Insight with RAG',
traceTip,
<EuiSmallButtonIcon
aria-label="How was this generated?"
{...(traceInteractionId && {
Expand Down
78 changes: 78 additions & 0 deletions public/tabs/chat/messages/message_bubble.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { render, screen, fireEvent } from '@testing-library/react';

import { MessageBubble } from './message_bubble';
import { IOutput } from '../../../../common/types/chat_saved_object_attributes';
import * as services from '../../../services';
import * as useFeedbackHookExports from '../../../hooks/use_feed_back';
import * as useChatActionsExports from '../../../hooks/use_chat_actions';
import * as coreHookExports from '../../../contexts/core_context';
Expand All @@ -18,6 +19,9 @@ describe('<MessageBubble />', () => {
const reportUiStatsMock = jest.fn();

beforeEach(() => {
jest
.spyOn(services, 'getConfigSchema')
.mockReturnValue({ chat: { trace: true, feedback: true } });
jest
.spyOn(useFeedbackHookExports, 'useFeedback')
.mockReturnValue({ feedbackResult: undefined, sendFeedback: sendFeedbackMock });
Expand Down Expand Up @@ -289,4 +293,78 @@ describe('<MessageBubble />', () => {
);
expect(screen.queryByTestId('trace-icon-bar')).toBeNull();
});

it('should control view trace through config flag', () => {
render(
<MessageBubble
showActionBar={true}
showRegenerate={true}
message={{
type: 'output',
contentType: 'markdown',
content: 'here are the indices in your cluster: .alert',
interactionId: 'bar1',
}}
interaction={{
input: 'foo',
response: 'bar1',
conversation_id: 'foo',
interaction_id: 'bar1',
create_time: new Date().toLocaleString(),
}}
/>
);
expect(screen.queryByTestId('trace-icon-bar1')).toBeVisible();

jest.spyOn(services, 'getConfigSchema').mockReturnValue({ chat: { trace: false } });
render(
<MessageBubble
showActionBar={true}
showRegenerate={true}
message={{
type: 'output',
contentType: 'markdown',
content: 'here are the indices in your cluster: .alert',
interactionId: 'bar2',
}}
interaction={{
input: 'foo',
response: 'bar2',
conversation_id: 'foo',
interaction_id: 'bar2',
create_time: new Date().toLocaleString(),
}}
/>
);
expect(screen.queryByTestId('trace-icon-bar2')).toBeNull();
});

it('should control feedback buttons through config flag', () => {
const { rerender } = render(
<MessageBubble
showActionBar={true}
message={{
type: 'output',
contentType: 'markdown',
content: 'here are the indices in your cluster: .alert',
}}
/>
);
expect(screen.queryByLabelText('feedback thumbs down')).toBeVisible();
expect(screen.queryByLabelText('feedback thumbs up')).toBeVisible();

jest.spyOn(services, 'getConfigSchema').mockReturnValue({ chat: { feedback: false } });
rerender(
<MessageBubble
showActionBar={true}
message={{
type: 'output',
contentType: 'markdown',
content: 'here are the indices in your cluster: .alert',
}}
/>
);
expect(screen.queryByLabelText('feedback thumbs down')).toBeNull();
expect(screen.queryByLabelText('feedback thumbs up')).toBeNull();
});
});
7 changes: 6 additions & 1 deletion public/tabs/chat/messages/message_bubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import React from 'react';
import { IconType } from '@elastic/eui/src/components/icon/icon';
import { MessageActions } from './message_action';
import { useCore } from '../../../contexts';
import { getConfigSchema } from '../../../services';

// TODO: Replace with getChrome().logos.Chat.url
import { useChatActions } from '../../../hooks';
Expand Down Expand Up @@ -46,8 +47,11 @@ type MessageBubbleProps = {
export const MessageBubble: React.FC<MessageBubbleProps> = React.memo((props) => {
const { executeAction } = useChatActions();
const core = useCore();
const configSchema = getConfigSchema();

// According to the design of the feedback, only markdown type output is supported.
const showFeedback =
configSchema.chat.feedback &&
'message' in props &&
props.message.type === 'output' &&
props.message.contentType === 'markdown';
Expand Down Expand Up @@ -159,7 +163,8 @@ export const MessageBubble: React.FC<MessageBubbleProps> = React.memo((props) =>
interaction={props.interaction}
message={props.message as IOutput}
showFeedback={showFeedback}
showTraceIcon={!!props.message.interactionId}
showTraceIcon={configSchema.chat.trace && !!props.message.interactionId}
traceTip="How was this generated?"
traceInteractionId={props.message.interactionId || ''}
onViewTrace={() => {
const message = props.message as IOutput;
Expand Down
Loading