@@ -4,7 +4,8 @@ import android.content.Context
44import kotlinx.coroutines.Dispatchers
55import kotlinx.coroutines.withContext
66import org.wordpress.android.fluxc.model.SiteModel
7- import org.wordpress.android.fluxc.network.rest.wpcom.jetpackai.JetpackAITranscriptionRestClient
7+ import org.wordpress.android.fluxc.network.rest.wpcom.jetpackai.JetpackAIQueryResponse
8+ import org.wordpress.android.fluxc.network.rest.wpcom.jetpackai.JetpackAITranscriptionResponse
89import org.wordpress.android.fluxc.store.jetpackai.JetpackAIStore
910import org.wordpress.android.viewmodel.ContextProvider
1011import java.io.File
@@ -14,36 +15,76 @@ import javax.inject.Inject
1415
1516class VoiceToContentUseCase @Inject constructor(
1617 private val jetpackAIStore : JetpackAIStore ,
17- private val contextProvider : ContextProvider
18+ private val fileHelperWrapper : VoiceToContentTempFileHelperWrapper
1819) {
1920 companion object {
2021 const val FEATURE = " voice_to_content"
21- private const val KILO_BYTE = 1024
22+ const val ROLE = " jetpack-ai"
23+ const val TYPE = " voice-to-content-simple-draft"
24+ const val JETPACK_AI_ERROR = " __JETPACK_AI_ERROR__"
2225 }
2326
2427 suspend fun execute (
2528 siteModel : SiteModel ,
2629 ): VoiceToContentResult =
2730 withContext(Dispatchers .IO ) {
28- val file = getAudioFile() ? : return @withContext VoiceToContentResult (isError = true )
29- val response = jetpackAIStore.fetchJetpackAITranscription(
31+ val file = fileHelperWrapper. getAudioFile() ? : return @withContext VoiceToContentResult (isError = true )
32+ val transcriptionResponse = jetpackAIStore.fetchJetpackAITranscription(
3033 siteModel,
3134 FEATURE ,
3235 file
3336 )
3437
35- when (response ) {
36- is JetpackAITranscriptionRestClient . JetpackAITranscriptionResponse .Success -> {
37- return @withContext VoiceToContentResult (content = response .model)
38+ val transcribedText : String? = when (transcriptionResponse ) {
39+ is JetpackAITranscriptionResponse .Success -> {
40+ transcriptionResponse .model
3841 }
39- is JetpackAITranscriptionRestClient . JetpackAITranscriptionResponse .Error -> {
40- return @withContext VoiceToContentResult (isError = true )
42+ is JetpackAITranscriptionResponse .Error -> {
43+ null
4144 }
4245 }
46+
47+ transcribedText?.let {
48+ val response = jetpackAIStore.fetchJetpackAIQuery(
49+ site = siteModel,
50+ feature = FEATURE ,
51+ role = ROLE ,
52+ message = it,
53+ stream = false ,
54+ type = TYPE
55+ )
56+
57+ when (response) {
58+ is JetpackAIQueryResponse .Success -> {
59+ val finalContent: String = response.choices[0 ].message.content
60+ // __JETPACK_AI_ERROR__ is a special marker we ask GPT to add to the request when it can’t
61+ // understand the request for any reason, so maybe something confused GPT on some requests.
62+ if (finalContent == JETPACK_AI_ERROR ) {
63+ return @withContext VoiceToContentResult (isError = true )
64+ } else {
65+ return @withContext VoiceToContentResult (content = response.choices[0 ].message.content)
66+ }
67+ }
68+
69+ is JetpackAIQueryResponse .Error -> {
70+ return @withContext VoiceToContentResult (isError = true )
71+ }
72+ }
73+ } ? : return @withContext VoiceToContentResult (isError = true )
4374 }
75+ }
76+
77+ // todo: build out the result object
78+ data class VoiceToContentResult (
79+ val content : String? = null ,
80+ val isError : Boolean = false
81+ )
4482
45- // todo: The next three methods are temporary to support development - remove when the real impl is in place
46- private fun getAudioFile (): File ? {
83+ // todo: Remove this class when real impl is in place - it's here so I can start unit tests
84+ class VoiceToContentTempFileHelperWrapper @Inject constructor(
85+ private val contextProvider : ContextProvider
86+ ) {
87+ fun getAudioFile (): File ? {
4788 val result = runCatching {
4889 getFileFromAssets(contextProvider.getContext())
4990 }
@@ -53,7 +94,7 @@ class VoiceToContentUseCase @Inject constructor(
5394 }
5495 }
5596
56- // todo: Do not forget to delete the test file from the asset directory - when the real impl is in place
97+ // todo: Do not forget to delete the test file from the asset directory
5798 private fun getFileFromAssets (context : Context ): File {
5899 val fileName = " jetpack-ai-transcription-test-audio-file.m4a"
59100 val file = File (context.filesDir, fileName)
@@ -74,10 +115,7 @@ class VoiceToContentUseCase @Inject constructor(
74115 }
75116 inputStream.close()
76117 }
118+ companion object {
119+ const val KILO_BYTE = 1024
120+ }
77121}
78-
79- // todo: build out the result object
80- data class VoiceToContentResult (
81- val content : String? = null ,
82- val isError : Boolean = false
83- )
0 commit comments