|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +import { useCallback, useState } from '@wordpress/element'; |
| 5 | +import debugFactory from 'debug'; |
| 6 | +/** |
| 7 | + * Internal dependencies |
| 8 | + */ |
| 9 | +import useAiSuggestions, { RequestingErrorProps } from '../use-ai-suggestions/index.js'; |
| 10 | +import type { PromptProp } from '../../types.js'; |
| 11 | + |
| 12 | +const debug = debugFactory( 'jetpack-ai-client:use-transcription-post-processing' ); |
| 13 | + |
| 14 | +/** |
| 15 | + * Post-processing types. |
| 16 | + */ |
| 17 | +export const TRANSCRIPTION_POST_PROCESSING_ACTION_SIMPLE_DRAFT = 'voice-to-content-simple-draft'; |
| 18 | +export type PostProcessingAction = typeof TRANSCRIPTION_POST_PROCESSING_ACTION_SIMPLE_DRAFT; |
| 19 | + |
| 20 | +/** |
| 21 | + * The return value for the transcription post-processing hook. |
| 22 | + */ |
| 23 | +export type UseTranscriptionPostProcessingReturn = { |
| 24 | + postProcessingResult: string; |
| 25 | + isProcessingTranscription: boolean; |
| 26 | + postProcessingError: string; |
| 27 | + processTranscription: ( action: PostProcessingAction, transcription: string ) => void; |
| 28 | +}; |
| 29 | + |
| 30 | +/** |
| 31 | + * The props for the transcription post-processing hook. |
| 32 | + */ |
| 33 | +export type UseTranscriptionPostProcessingProps = { |
| 34 | + feature: string; |
| 35 | + onReady?: ( postProcessingResult: string ) => void; |
| 36 | + onError?: ( error: string ) => void; |
| 37 | + onUpdate?: ( currentPostProcessingResult: string ) => void; |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + * A hook to handle transcription post-processing. |
| 42 | + * |
| 43 | + * @param {string} feature - The feature name that is calling the post-processing actions. |
| 44 | + * @returns {UseTranscriptionPostProcessingReturn} - Object with properties to get the post-processing results. |
| 45 | + */ |
| 46 | +export default function useTranscriptionPostProcessing( { |
| 47 | + feature, |
| 48 | + onReady, |
| 49 | + onError, |
| 50 | + onUpdate, |
| 51 | +}: UseTranscriptionPostProcessingProps ): UseTranscriptionPostProcessingReturn { |
| 52 | + const [ postProcessingResult, setPostProcessingResult ] = useState< string >( '' ); |
| 53 | + const [ postProcessingError, setPostProcessingError ] = useState< string >( '' ); |
| 54 | + const [ isProcessingTranscription, setIsProcessingTranscription ] = useState( false ); |
| 55 | + |
| 56 | + /** |
| 57 | + * Set-up the useAiSuggestions hook. |
| 58 | + */ |
| 59 | + const handleOnSuggestion = useCallback( |
| 60 | + ( suggestion: string ) => { |
| 61 | + setPostProcessingResult( suggestion ); |
| 62 | + onUpdate?.( suggestion ); |
| 63 | + }, |
| 64 | + [ setPostProcessingResult, onUpdate ] |
| 65 | + ); |
| 66 | + |
| 67 | + const handleOnDone = useCallback( |
| 68 | + ( result: string ) => { |
| 69 | + setPostProcessingResult( result ); |
| 70 | + onUpdate?.( result ); |
| 71 | + onReady?.( result ); |
| 72 | + }, |
| 73 | + [ setPostProcessingResult, onUpdate, onReady ] |
| 74 | + ); |
| 75 | + |
| 76 | + const handleOnError = useCallback( |
| 77 | + ( errorData: RequestingErrorProps ) => { |
| 78 | + setPostProcessingError( errorData.message ); |
| 79 | + onError?.( errorData.message ); |
| 80 | + }, |
| 81 | + [ setPostProcessingError, onError ] |
| 82 | + ); |
| 83 | + |
| 84 | + const { request } = useAiSuggestions( { |
| 85 | + autoRequest: false, |
| 86 | + onSuggestion: handleOnSuggestion, |
| 87 | + onDone: handleOnDone, |
| 88 | + onError: handleOnError, |
| 89 | + } ); |
| 90 | + |
| 91 | + const handleTranscriptionPostProcessing = useCallback( |
| 92 | + ( action: PostProcessingAction, transcription: string ) => { |
| 93 | + debug( 'Post-processing transcription' ); |
| 94 | + |
| 95 | + /** |
| 96 | + * Reset the transcription result and error. |
| 97 | + */ |
| 98 | + setPostProcessingResult( '' ); |
| 99 | + setPostProcessingError( '' ); |
| 100 | + setIsProcessingTranscription( true ); |
| 101 | + |
| 102 | + /** |
| 103 | + * Build the prompt to call the suggestion hook. |
| 104 | + */ |
| 105 | + const messages: PromptProp = [ |
| 106 | + { |
| 107 | + role: 'jetpack-ai', |
| 108 | + context: { |
| 109 | + type: action, |
| 110 | + content: transcription, |
| 111 | + }, |
| 112 | + }, |
| 113 | + ]; |
| 114 | + |
| 115 | + /** |
| 116 | + * Call the suggestion hook using the message. |
| 117 | + */ |
| 118 | + request( messages, { feature } ); |
| 119 | + }, |
| 120 | + [ |
| 121 | + setPostProcessingResult, |
| 122 | + setPostProcessingError, |
| 123 | + setIsProcessingTranscription, |
| 124 | + request, |
| 125 | + feature, |
| 126 | + ] |
| 127 | + ); |
| 128 | + |
| 129 | + return { |
| 130 | + postProcessingResult, |
| 131 | + isProcessingTranscription, |
| 132 | + postProcessingError, |
| 133 | + processTranscription: handleTranscriptionPostProcessing, |
| 134 | + }; |
| 135 | +} |
0 commit comments