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

AI Assistant: Add thumbs feedback on AI Assistant #40728

Open
wants to merge 3 commits into
base: trunk
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

AI Client: Add thumbs feedback on AI Assistant
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,16 @@ export function BlockAIControl(
);

const message =
showGuideLine && ! loading && ! editRequest && ( customFooter || <GuidelineMessage /> );
showGuideLine &&
! loading &&
! editRequest &&
( customFooter || (
<GuidelineMessage
showAIFeedbackThumbs={ true }
ratedItem={ 'ai-assistant' }
prompt={ value }
/>
) );

return (
<AIControl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ export function ExtensionAIControl(
const [ editRequest, setEditRequest ] = useState( false );
const [ lastValue, setLastValue ] = useState( value || null );
const promptUserInputRef = useRef( null );
const isDone = value?.length <= 0 && state === 'done';
const [ initialPlaceholder ] = useState( placeholder );
const [ prompt, setPrompt ] = useState( null );

// Pass the ref to forwardRef.
useImperativeHandle( ref, () => promptUserInputRef.current );
Expand All @@ -95,8 +98,16 @@ export function ExtensionAIControl(
}
}, [ editRequest ] );

useEffect( () => {
if ( placeholder !== initialPlaceholder ) {
// The prompt is used to determine if there was a toolbar action
setPrompt( placeholder );
}
}, [ placeholder ] );

const sendHandler = useCallback( () => {
setLastValue( value );
setPrompt( value );
setEditRequest( false );
onSend?.( value );
}, [ onSend, value ] );
Expand Down Expand Up @@ -183,7 +194,7 @@ export function ExtensionAIControl(
</Button>
</div>
) }
{ value?.length <= 0 && state === 'done' && (
{ isDone && (
<div className="jetpack-components-ai-control__controls-prompt_button_wrapper">
<ButtonGroup>
<Button
Expand Down Expand Up @@ -233,7 +244,15 @@ export function ExtensionAIControl(
/>
);
} else if ( showGuideLine ) {
message = <GuidelineMessage />;
message = isDone ? (
<GuidelineMessage
showAIFeedbackThumbs={ true }
ratedItem={ 'ai-assistant' }
prompt={ prompt }
/>
) : (
<GuidelineMessage />
);
}

return (
Expand Down
48 changes: 35 additions & 13 deletions projects/js-packages/ai-client/src/components/message/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import { ExternalLink, Button } from '@wordpress/components';
import { createInterpolateElement } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { Icon, check, arrowRight } from '@wordpress/icons';
import { Icon, check } from '@wordpress/icons';
import clsx from 'clsx';
/**
* Internal dependencies
*/
import './style.scss';
import errorExclamation from '../../icons/error-exclamation.js';
import { ERROR_QUOTA_EXCEEDED } from '../../types.js';
import AiFeedbackThumbs from '../ai-feedback/index.js';
/**
* Types
*/
Expand All @@ -30,13 +31,22 @@ export type MessageSeverityProp =
| typeof MESSAGE_SEVERITY_INFO
| null;

type RateProps = {
ratedItem?: string;
prompt?: string;
onRate?: ( rating: string ) => void;
};

export type MessageProps = {
icon?: React.ReactNode;
severity?: MessageSeverityProp;
showSidebarIcon?: boolean;
onSidebarIconClick?: () => void;
showAIFeedbackThumbs?: boolean;
children: React.ReactNode;
};
} & RateProps;

export type GuidelineMessageProps = {
showAIFeedbackThumbs?: boolean;
} & RateProps;

export type OnUpgradeClick = ( event?: React.MouseEvent< HTMLButtonElement > ) => void;

Expand Down Expand Up @@ -71,8 +81,10 @@ const messageIconsMap = {
export default function Message( {
severity = MESSAGE_SEVERITY_INFO,
icon = null,
showSidebarIcon = false,
onSidebarIconClick = () => {},
showAIFeedbackThumbs = false,
ratedItem = '',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GuidelineMessage passes down ratedItem which, in turn, is passed down to AiFeedbackThumbs but, so far, it always matches the feature prop (ai-assistant). Could ratedItem hold a value different to feature? I'm merely curious since looking at the code it seems like a duped prop value.

prompt = '',
onRate = () => {},
children,
}: MessageProps ): React.ReactElement {
return (
Expand All @@ -85,11 +97,17 @@ export default function Message( {
{ ( messageIconsMap[ severity ] || icon ) && (
<Icon icon={ messageIconsMap[ severity ] || icon } />
) }
<div className="jetpack-ai-assistant__message-content">{ children }</div>
{ showSidebarIcon && (
<Button className="jetpack-ai-assistant__message-sidebar" onClick={ onSidebarIconClick }>
<Icon size={ 20 } icon={ arrowRight } />
</Button>
{ <div className="jetpack-ai-assistant__message-content">{ children }</div> }
{ showAIFeedbackThumbs && (
<AiFeedbackThumbs
disabled={ false }
ratedItem={ ratedItem }
feature="ai-assistant"
options={ {
prompt,
} }
onRate={ onRate }
/>
) }
</div>
);
Expand All @@ -111,11 +129,15 @@ function LearnMoreLink(): React.ReactElement {
/**
* React component to render a guideline message.
*
* @param {GuidelineMessageProps} props - Component props.
* @return {React.ReactElement} - Message component.
*/
export function GuidelineMessage(): React.ReactElement {
export function GuidelineMessage( {
showAIFeedbackThumbs = false,
...props
}: GuidelineMessageProps ): React.ReactElement {
return (
<Message>
<Message showAIFeedbackThumbs={ showAIFeedbackThumbs } { ...props }>
<span>
{ __( 'AI-generated content could be inaccurate or biased.', 'jetpack-ai-client' ) }
</span>
Expand Down
Loading