Skip to content

Commit

Permalink
Merge pull request #5 from mslinnea/dev-meta
Browse files Browse the repository at this point in the history
Address feedback & update to use AI Services helpers
  • Loading branch information
mslinnea authored Nov 14, 2024
2 parents 25d4185 + 720a8f4 commit 1bd1565
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
44 changes: 25 additions & 19 deletions src/components/metaDescription/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import { useState } from 'react';
import {
Button,
TextareaControl,
Spinner,
Button,
TextareaControl,
Spinner,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { usePostMetaValue } from '@alleyinteractive/block-editor-tools';
import generatePrompt from './prompt';

const { store: aiStore } = window.aiServices.ai;
const { store: aiStore, helpers: aiHelpers } = window.aiServices.ai;

function MetaDescriptionField() {
const [metaDescription, setMetaDescription] = usePostMetaValue('_meta_description');
const [isGeneratingDescription, setIsGeneratingDescription] = useState(false);
const service = useSelect((select) => select(aiStore).getAvailableService(['text_generation']));
const postContent = useSelect((select) => select('core/editor').getEditedPostAttribute('content'));
const postTitle = useSelect((select) => select('core/editor').getEditedPostAttribute('title'));
const service = useSelect(
(select) => select(aiStore)
.getAvailableService(
{ capabilities: ['text_generation'] },
),
[],
);
const postContent = useSelect(
(select) => select('core/editor')
.getEditedPostAttribute('content'),
[],
);
const postTitle = useSelect(
(select) => select('core/editor')
.getEditedPostAttribute('title'),
[],
);

if (!service) {
return null;
Expand All @@ -26,14 +40,7 @@ function MetaDescriptionField() {
let candidates;
try {
candidates = await service.generateText(
{
role: 'user',
parts: [
{
text: generatePrompt({ postTitle, postContent }),
},
],
},
generatePrompt({ postTitle, postContent }),
{
feature: 'add-meta-description-plugin',
capabilities: ['text_generation'],
Expand All @@ -45,9 +52,8 @@ function MetaDescriptionField() {
return;
}

const description = candidates[0].content.parts[0].text.replaceAll(
'\n\n\n\n',
'\n\n',
const description = aiHelpers.getTextFromContents(
aiHelpers.getCandidateContents(candidates),
);

setMetaDescription(description);
Expand All @@ -58,7 +64,7 @@ function MetaDescriptionField() {
<div className="meta-description-field">
<TextareaControl
label="Meta Description"
rows={8}
rows={8}
value={metaDescription}
onChange={(value) => setMetaDescription(value)}
/>
Expand Down
39 changes: 22 additions & 17 deletions src/components/metaKeywords/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,30 @@ import {
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { usePostMetaValue } from '@alleyinteractive/block-editor-tools';
import generateKeywordsPrompt from './prompt'; // Adjust this if you have a separate function for keywords
import generateKeywordsPrompt from './prompt';

const { store: aiStore } = window.aiServices.ai;
const { store: aiStore, helpers: aiHelpers } = window.aiServices.ai;

function MetaKeywordsField() {
const [metaKeywords, setMetaKeywords] = usePostMetaValue('_meta_keywords');
const [isGeneratingKeywords, setIsGeneratingKeywords] = useState(false);
const service = useSelect((select) => select(aiStore).getAvailableService(['text_generation']));
const postContent = useSelect((select) => select('core/editor').getEditedPostAttribute('content'));
const postTitle = useSelect((select) => select('core/editor').getEditedPostAttribute('title'));

const service = useSelect(
(select) => select(aiStore)
.getAvailableService(
{ capabilities: ['text_generation'] },
),
[],
);
const postContent = useSelect(
(select) => select('core/editor')
.getEditedPostAttribute('content'),
[],
);
const postTitle = useSelect(
(select) => select('core/editor')
.getEditedPostAttribute('title'),
[],
);
if (!service) {
return null;
}
Expand All @@ -26,14 +39,7 @@ function MetaKeywordsField() {
let candidates;
try {
candidates = await service.generateText(
{
role: 'user',
parts: [
{
text: generateKeywordsPrompt({ postTitle, postContent }),
},
],
},
generateKeywordsPrompt({ postTitle, postContent }),
{
feature: 'add-meta-keywords-plugin',
capabilities: ['text_generation'],
Expand All @@ -45,9 +51,8 @@ function MetaKeywordsField() {
return;
}

const keywords = candidates[0].content.parts[0].text.replaceAll(
'\n\n\n\n',
'\n\n',
const keywords = aiHelpers.getTextFromContents(
aiHelpers.getCandidateContents(candidates),
);

setMetaKeywords(keywords);
Expand Down

0 comments on commit 1bd1565

Please sign in to comment.