Skip to content

Commit

Permalink
chore: use AI Block as buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Soare-Robert-Daniel committed Aug 16, 2023
1 parent 749b39f commit 014d4d5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/blocks/blocks/content-generator/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"attributes": {
"promptID": {
"type": "string"
},
"resultHistory": {
"type": "array",
"default": []
}
}
}
1 change: 1 addition & 0 deletions src/blocks/blocks/content-generator/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ const ContentGenerator = ({
actionButtons={PRESETS?.[attributes.promptID]?.actions}
onClose={() => removeBlock( clientId )}
promptPlaceholder={PRESETS?.[attributes.promptID]?.placeholder}
resultHistory={attributes.resultHistory}
>
{
hasInnerBlocks ? (
Expand Down
1 change: 1 addition & 0 deletions src/blocks/blocks/content-generator/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BlockProps, InspectorProps } from '../../helpers/blocks';

type Attributes = {
promptID: string;
resultHistory: {result: string, meta: { usedToken: number, prompt: string }}[]
}

export type ContentGeneratorAttrs = Partial<Attributes>
Expand Down
10 changes: 4 additions & 6 deletions src/blocks/components/prompt/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type PromptPlaceholderProps = {
mainActionLabel?: string
onPreview?: ( result: string ) => void
actionButtons?: ( props: {status?: string}) => ReactNode
resultHistory?: {result: string, meta: { usedToken: number, prompt: string }}[]
};

export const openAiAPIKeyName = 'themeisle_open_ai_api_key';
Expand Down Expand Up @@ -127,7 +128,7 @@ const PromptPlaceholder = ( props: PromptPlaceholderProps ) => {
const [ embeddedPrompts, setEmbeddedPrompts ] = useState<PromptsData>([]);
const [ result, setResult ] = useState<string | undefined>( undefined );

const [ resultHistory, setResultHistory ] = useState<{result: string, meta: { usedToken: number, prompt: string }}[]>([]);
const [ resultHistory, setResultHistory ] = useState<{result: string, meta: { usedToken: number, prompt: string }}[]>( props.resultHistory ?? []);
const [ resultHistoryIndex, setResultHistoryIndex ] = useState<number>( 0 );

const [ showResultArea, setShowResultArea ] = useState<boolean>( false );
Expand Down Expand Up @@ -178,9 +179,6 @@ const PromptPlaceholder = ( props: PromptPlaceholderProps ) => {
}, [ resultHistory ]);

useEffect( () => {
if ( ! result ) {
return;
}

if ( 0 > resultHistoryIndex ) {
return;
Expand All @@ -190,7 +188,7 @@ const PromptPlaceholder = ( props: PromptPlaceholderProps ) => {
return;
}

setResult( resultHistory[ resultHistoryIndex ].result );
setResult( resultHistory?.[ resultHistoryIndex ].result );
setTokenUsageDescription( __( 'Used tokens: ', 'otter-blocks' ) + resultHistory[ resultHistoryIndex ].meta.usedToken );
props.onPreview?.( resultHistory[ resultHistoryIndex ].result );

Expand Down Expand Up @@ -340,7 +338,7 @@ const PromptPlaceholder = ( props: PromptPlaceholderProps ) => {
return (
<div>
{
showResultArea ? (
( 0 < resultHistory?.length ) ? (
<PromptBlockEditor
title={ props.title }
currentResultIndex={ resultHistoryIndex + 1 }
Expand Down
21 changes: 18 additions & 3 deletions src/blocks/plugins/ai-content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import {
applyFilters
} from '@wordpress/hooks';

import { useDispatch, useSelect } from '@wordpress/data';
import { rawHandler } from '@wordpress/blocks';
import { useDispatch, useSelect, dispatch } from '@wordpress/data';
import { rawHandler, createBlock } from '@wordpress/blocks';
import { BlockControls } from '@wordpress/block-editor';

/**
Expand Down Expand Up @@ -188,7 +188,22 @@ const withConditions = createHigherOrderComponent( BlockEdit => {

const clientId = ! isMultipleSelection ? props.clientId : selectedBlocks.pop().clientId;

insertBlockBelow( props.clientId, newBlocks );
const aiBlock = createBlock(
'themeisle-blocks/content-generator',
{
promptID: 'textTransformation',
resultHistory: [{
result: response?.choices?.[0]?.message.content ?? '',
meta: {
usedToken: response?.usage.total_tokens,
prompt: ''
}
}]
},
newBlocks
);

insertBlockBelow( props.clientId, aiBlock );

setIsProcessing( prevState => ({ ...prevState, [ actionKey ]: false }) );
callback?.();
Expand Down

0 comments on commit 014d4d5

Please sign in to comment.