Skip to content

Commit

Permalink
AI Assistant: Add option to generate title (#30646)
Browse files Browse the repository at this point in the history
* add option to generate a post title

* changelog

* renaming lastPromptType attribute

* fix text case

* replace Done with Accept title
  • Loading branch information
dhasilva committed May 11, 2023
1 parent 3c7910a commit 8924786
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Unreleased block


Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const AIControl = ( {
getSuggestionFromOpenAI,
retryRequest,
handleAcceptContent,
handleAcceptTitle,
handleTryAgain,
handleGetSuggestion,
isWaitingState,
Expand All @@ -32,6 +33,7 @@ const AIControl = ( {
postTitle,
wholeContent,
content,
promptType,
} ) => {
const handleInputEnter = event => {
if ( event.key === 'Enter' && ! event.shiftKey ) {
Expand Down Expand Up @@ -80,13 +82,15 @@ const AIControl = ( {
getSuggestionFromOpenAI={ getSuggestionFromOpenAI }
retryRequest={ retryRequest }
handleAcceptContent={ handleAcceptContent }
handleAcceptTitle={ handleAcceptTitle }
handleGetSuggestion={ handleGetSuggestion }
handleTryAgain={ handleTryAgain }
showRetry={ showRetry }
toggleAIType={ toggleAIType }
contentBefore={ contentBefore }
hasPostTitle={ !! postTitle?.length }
wholeContent={ wholeContent }
promptType={ promptType }
/>
) }
<div className="jetpack-ai-assistant__input-wrapper">
Expand Down Expand Up @@ -124,12 +128,14 @@ const ToolbarControls = ( {
getSuggestionFromOpenAI,
retryRequest,
handleAcceptContent,
handleAcceptTitle,
handleTryAgain,
showRetry,
toggleAIType,
contentBefore,
hasPostTitle,
wholeContent,
promptType,
} ) => {
return (
<BlockControls>
Expand All @@ -138,11 +144,17 @@ const ToolbarControls = ( {
<ToolbarGroup>
{ ! showRetry && contentIsLoaded && animationDone && (
<>
<ToolbarButton onClick={ handleAcceptContent }>
{ __( 'Done', 'jetpack' ) }
</ToolbarButton>
{ promptType === 'generateTitle' ? (
<ToolbarButton onClick={ handleAcceptTitle }>
{ __( 'Accept title', 'jetpack' ) }
</ToolbarButton>
) : (
<ToolbarButton onClick={ handleAcceptContent }>
{ __( 'Done', 'jetpack' ) }
</ToolbarButton>
) }
<ToolbarButton onClick={ handleTryAgain }>
{ __( 'Try Again', 'jetpack' ) }
{ __( 'Try again', 'jetpack' ) }
</ToolbarButton>
</>
) }
Expand Down Expand Up @@ -192,6 +204,11 @@ const ToolbarControls = ( {
onClick: () => getSuggestionFromOpenAI( 'simplify' ),
isDisabled: ! contentBefore?.length,
},
{
title: __( 'Generate a post title', 'jetpack' ),
onClick: () => getSuggestionFromOpenAI( 'generateTitle' ),
isDisabled: ! wholeContent?.length,
},
] }
/>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ export default {
content: {
type: 'string',
},
promptType: {
type: 'string',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ export const createPrompt = (
return expandPrompt;
}

if ( type === 'generateTitle' ) {
if ( ! content.length ) {
return '';
}

return buildPromptTemplate( {
request: 'Generate a title for this blog post',
rules: [ 'Only output the raw title, without any prefix or quotes' ],
content,
} );
}

// TODO: add some error handling if user supplied prompts or existing content is too short.

// We prevent a prompt if everything is empty.
Expand Down
10 changes: 9 additions & 1 deletion projects/plugins/jetpack/extensions/blocks/ai-assistant/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export default function AIAssistantEdit( { attributes, setAttributes, clientId }
const { tracks } = useAnalytics();
const postId = useSelect( select => select( 'core/editor' ).getCurrentPostId() );

const { replaceBlocks, replaceBlock } = useDispatch( blockEditorStore );
const { replaceBlocks, replaceBlock, removeBlock } = useDispatch( blockEditorStore );
const { editPost } = useDispatch( 'core/editor' );
const { mediaUpload } = useSelect( select => {
const { getSettings } = select( blockEditorStore );
const settings = getSettings();
Expand Down Expand Up @@ -107,6 +108,11 @@ export default function AIAssistantEdit( { attributes, setAttributes, clientId }
replaceBlocks( clientId, rawHandler( { HTML: attributes.content } ) );
};

const handleAcceptTitle = () => {
editPost( { title: attributes.content.trim() } );
removeBlock( clientId );
};

const handleTryAgain = () => {
setAttributes( { content: undefined } );
};
Expand Down Expand Up @@ -155,6 +161,7 @@ export default function AIAssistantEdit( { attributes, setAttributes, clientId }
getSuggestionFromOpenAI={ getSuggestionFromOpenAI }
retryRequest={ retryRequest }
handleAcceptContent={ handleAcceptContent }
handleAcceptTitle={ handleAcceptTitle }
handleGetSuggestion={ handleGetSuggestion }
handleTryAgain={ handleTryAgain }
isWaitingState={ isWaitingState }
Expand All @@ -166,6 +173,7 @@ export default function AIAssistantEdit( { attributes, setAttributes, clientId }
postTitle={ postTitle }
userPrompt={ userPrompt }
wholeContent={ wholeContent }
promptType={ attributes.promptType }
/>
{ ! loadingImages && resultImages.length > 0 && (
<Flex direction="column" style={ { width: '100%' } }>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ const useSuggestionsFromOpenAI = ( {

if ( ! retryRequest ) {
setLastPrompt( prompt );
setAttributes( { promptType: type } );
}

apiFetch( {
Expand Down

0 comments on commit 8924786

Please sign in to comment.