diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/ai/MockWritingAssistant.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/ai/MockWritingAssistant.kt new file mode 100644 index 000000000..8945613fe --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/ai/MockWritingAssistant.kt @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.ai + +import kotlinx.coroutines.delay + +/** + * TODO: Remove before shipping. Debug-only mock for testing the AI writing help UI + * on devices without Gemini Nano / AICore support. + */ +class MockWritingAssistant : WritingAssistant { + override suspend fun checkAvailability(): WritingAssistantStatus = WritingAssistantStatus.Available + + override suspend fun transform( + text: String, + tone: WritingTone, + ): WritingResult { + delay(800) + + val transformed = + when (tone) { + WritingTone.CORRECT -> { + correctMock(text) + } + + WritingTone.REPHRASE -> { + "Here's another way to put it: $text" + } + + WritingTone.SHORTER -> { + text + .split(".") + .firstOrNull() + ?.trim() + ?.plus(".") ?: text + } + + WritingTone.ELABORATE -> { + "$text Furthermore, this point deserves deeper consideration and nuance." + } + + WritingTone.FRIENDLY -> { + "Hey! $text Hope that makes sense! :)" + } + + WritingTone.PROFESSIONAL -> { + "I would like to bring to your attention the following: $text" + } + + WritingTone.MORE_DIRECT -> { + text.replace("I think ", "").replace("maybe ", "").replace("perhaps ", "") + } + + WritingTone.PUNCHY -> { + text.uppercase().replace(".", "!") + } + + WritingTone.EMOJIFY -> { + "$text \uD83D\uDE80\uD83D\uDD25\u2728" + } + } + + return WritingResult( + originalText = text, + transformedText = transformed, + tone = tone, + ) + } + + private fun correctMock(text: String): String = + text + .replace("teh ", "the ") + .replace("dont ", "don't ") + .replace("cant ", "can't ") + .replace("wont ", "won't ") + .replace("i ", "I ") + + override fun close() {} +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt index 2ec52b771..cb892662c 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt @@ -44,6 +44,7 @@ import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.User +import com.vitorpamplona.amethyst.service.ai.MockWritingAssistant import com.vitorpamplona.amethyst.service.ai.WritingAssistant import com.vitorpamplona.amethyst.service.ai.WritingAssistantFactory import com.vitorpamplona.amethyst.service.ai.WritingAssistantStatus @@ -297,6 +298,9 @@ open class ShortNotePostViewModel : var wantsAnonymousPost by mutableStateOf(false) // AI Writing Help + // TODO: Remove useMockAi before shipping. Set to true to test UI without Gemini Nano. + private val useMockAi = true + var wantsAiHelp by mutableStateOf(false) var aiResult by mutableStateOf(null) var aiStatus by mutableStateOf(WritingAssistantStatus.Unavailable) @@ -305,7 +309,12 @@ open class ShortNotePostViewModel : fun initWritingAssistant(context: android.content.Context) { if (writingAssistant == null) { - writingAssistant = WritingAssistantFactory.create(context) + writingAssistant = + if (useMockAi) { + MockWritingAssistant() + } else { + WritingAssistantFactory.create(context) + } viewModelScope.launch(Dispatchers.IO) { aiStatus = writingAssistant?.checkAvailability() ?: WritingAssistantStatus.Unavailable }