diff --git a/Android/app/build.gradle.kts b/Android/app/build.gradle.kts index ad480267..8f31a11c 100644 --- a/Android/app/build.gradle.kts +++ b/Android/app/build.gradle.kts @@ -61,6 +61,11 @@ android { } buildTypes { + debug { + // `-PdevSuffix=.dev` installs a debug build beside the Play Store one on the same + // device, with its own data. Without it the debug build keeps the Play package name. + (project.findProperty("devSuffix") as String?)?.takeIf { it.isNotBlank() }?.let { applicationIdSuffix = it; versionNameSuffix = "-dev" } + } release { isMinifyEnabled = true isShrinkResources = true diff --git a/Android/app/src/androidTest/java/com/everettjf/scriptwidget/AiConversationUiTest.kt b/Android/app/src/androidTest/java/com/everettjf/scriptwidget/AiConversationUiTest.kt new file mode 100644 index 00000000..02361cd5 --- /dev/null +++ b/Android/app/src/androidTest/java/com/everettjf/scriptwidget/AiConversationUiTest.kt @@ -0,0 +1,246 @@ +package com.everettjf.scriptwidget + +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.uiautomator.By +import androidx.test.uiautomator.BySelector +import androidx.test.uiautomator.UiDevice +import androidx.test.uiautomator.UiObject2 +import androidx.test.uiautomator.Until +import com.everettjf.scriptwidget.ai.AiServices +import com.everettjf.scriptwidget.core.ai.AiCancellation +import com.everettjf.scriptwidget.core.ai.AiChat +import com.everettjf.scriptwidget.core.ai.AiChatResult +import com.everettjf.scriptwidget.core.ai.AiMessage +import com.everettjf.scriptwidget.core.ai.AiProfile +import com.everettjf.scriptwidget.core.ai.AiProviderKind +import com.everettjf.scriptwidget.core.ai.AiTokenUsage +import com.everettjf.scriptwidget.ui.AiConversationViewModel +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import java.io.File +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit + +/** + * The AI conversation screen, driven the way a person uses it: the real app, + * the real runtime checking every version, and a scripted model in place of + * a provider. It proves the screen and the saving, not a live model's output. + * + * Each step saves a picture under the app's external files directory + * (the `ai-conversation` folder) for a person to look at. + */ +@RunWith(AndroidJUnit4::class) +class AiConversationUiTest { + private val instrumentation = InstrumentationRegistry.getInstrumentation() + private val context = instrumentation.targetContext + private val device = UiDevice.getInstance(instrumentation) + private val pictures = File(context.getExternalFilesDir(null), "ai-conversation") + private val replies = ArrayDeque() + private val requests = ArrayList() + /** When set, the next reply waits for it, so that the running state can be looked at. */ + @Volatile private var gate: CountDownLatch? = null + + private var previousKind: AiProviderKind? = null + private var previousProfile: AiProfile? = null + private var previousKey: String? = null + private val created = ArrayList() + private val before = HashSet() + + @Before fun prepare() { + AppServices.attach(context) + pictures.mkdirs() + File(context.noBackupFilesDir, "ai-conversations").deleteRecursively() + before += AppServices.library.list().map { it.name } + // A provider must look set up; the scripted model never sends the key anywhere. + val settings = AiServices.settings(context) + previousKind = settings.activeKind + previousProfile = settings.profile(AiProviderKind.ANTHROPIC) + previousKey = AiServices.credentials(context).get(AiProviderKind.ANTHROPIC.id) + settings.activeKind = AiProviderKind.ANTHROPIC + settings.save(AiProfile.defaults(AiProviderKind.ANTHROPIC), "scripted-model-no-real-key") + AiConversationViewModel.chatFactory = { _, _ -> + AiChat { messages: List, _: AiCancellation -> + requests += messages.last().content + gate?.await(20, TimeUnit.SECONDS) + Thread.sleep(400) + AiChatResult(replies.removeFirst(), AiTokenUsage(1200, 180, 1380)) + } + } + } + + @After fun restore() { + val settings = AiServices.settings(context) + previousProfile?.let { settings.save(it, null) } + val key = previousKey + if (key != null) AiServices.credentials(context).set(AiProviderKind.ANTHROPIC.id, key) else AiServices.credentials(context).remove(AiProviderKind.ANTHROPIC.id) + previousKind?.let { settings.activeKind = it } + for (item in AppServices.library.list()) if (item.name !in before) runCatching { AppServices.library.delete(item.name) } + File(context.noBackupFilesDir, "ai-conversations").deleteRecursively() + } + + private fun widget(color: String, title: String, extra: String = "") = "\$render(\n \n $title\n Tuesday, 3 March$extra\n \n);" + + private fun reply(note: String, code: String, hosts: String? = null) = + "SUMMARY: $note\n```jsx\n$code\n```" + (hosts?.let { "\n```json\n{\"permissions\":[\"network\"],\"networkDomains\":[\"$it\"]}\n```" } ?: "") + + private fun wait(selector: BySelector, seconds: Long = 20): UiObject2 = + assertNotNull("not on screen: $selector", device.wait(Until.findObject(selector), seconds * 1000)).let { device.findObject(selector) } + + private fun tap(selector: BySelector) { wait(selector).click(); device.waitForIdle() } + + private fun picture(name: String) { device.waitForIdle(); Thread.sleep(700); device.takeScreenshot(File(pictures, "$name.png")) } + + private fun ask(text: String) { + wait(By.clazz("android.widget.EditText")).text = text + tap(By.desc("Send")) + } + + /** Opens the app at its first screen and goes to a new AI conversation. */ + private fun openNewConversation() { + // Started the way a launcher does; a start from the test's own context is held back as a background launch. + device.executeShellCommand("am start --activity-clear-task -n ${context.packageName}/${MainActivity::class.java.name}") + tap(By.desc("New widget")) + tap(By.text("Generate with AI")) + wait(By.text("What should the widget show?")) + } + + @Test fun aStoppedTurnChangesNothingAndTheDraftIsKept() { + openNewConversation() + gate = CountDownLatch(1) + ask("A big red countdown to Friday") + wait(By.text("Writing the widget…")) + tap(By.desc("Stop")) + wait(By.text("Stopped. The widget is unchanged.")) + gate!!.countDown(); gate = null + assertTrue("nothing was made", device.findObject(By.text("Save widget")) == null) + picture("20-stopped") + + // The same request, sent again, runs. + replies += reply("A red countdown card.", widget("#b91c1c", "3 days")) + tap(By.text("Send again")) + wait(By.text("A red countdown card.")) + wait(By.text("Save widget")) + + // Leaving keeps it as a draft, offered the next time. + tap(By.desc("Back")) + tap(By.desc("New widget")) + tap(By.text("Generate with AI")) + wait(By.text("Continue a draft")) + picture("21-continue-a-draft") + tap(By.text("A big red countdown to Friday")) + wait(By.text("A red countdown card.")) + wait(By.text("shown above")) + picture("22-draft-reopened") + + tap(By.desc("More")) + tap(By.text("Delete this draft")) + wait(By.text("What should the widget show?")) + assertTrue(AiServices.conversations(context).summaries().isEmpty()) + } + + @Test fun aWidgetIsMadeChangedSavedAndImproved() { + openNewConversation() + picture("01-empty") + + // An example fills the field; nothing is sent until Send. + tap(By.text("Clock")) + assertTrue(wait(By.clazz("android.widget.EditText")).text.contains("minimalist clock")) + picture("02-example") + + // The first turn. While the model works there is a Stop button and no version yet. + replies += reply("A dark clock card with the time and the date.", widget("#1e1b4b", "12:30")) + gate = CountDownLatch(1) + tap(By.desc("Send")) + wait(By.desc("Stop")) + wait(By.text("Writing the widget…")) + picture("03-writing") + gate!!.countDown(); gate = null + wait(By.text("A dark clock card with the time and the date.")) + wait(By.text("shown above")) + wait(By.text("Save widget")) + picture("04-first-version") + // The request field stays above the keyboard, with the preview still in view. + wait(By.clazz("android.widget.EditText")).click() + Thread.sleep(1200) + picture("04b-keyboard") + device.pressBack() + Thread.sleep(800) + tap(By.text("Medium")) + picture("04c-medium") + wait(By.checked(true).hasChild(By.text("Medium"))) + tap(By.text("Small")) + + // A change that does not run at first is repaired, and says so. + replies += reply("Made the background blue.", "\$render(") + replies += reply("Made the background blue.", widget("#1d4ed8", "12:30")) + ask("Make the background blue") + wait(By.text("Fixed automatically 1 time")) + wait(By.text("Made the background blue.")) + assertTrue("the current code is sent with the request", requests[1].contains("#1e1b4b") && requests[1].contains("Make the background blue")) + assertTrue("the failure is sent back", requests[2].contains("Your previous code failed to run")) + picture("05-repaired") + + // A widget that uses the network says which host it contacts. + replies += reply("Added a line fetched from example.com.", "let line = \"offline\";\ntry { line = (await fetch(\"https://example.com/\")).slice(0, 12); } catch (error) {}\n" + widget("#1d4ed8", "12:30", "\n {line}"), "example.com") + ask("Add a line with the first characters of example.com") + wait(By.text("May contact: example.com")) + picture("06-network") + + // A change that never runs leaves the widget as it was. + repeat(4) { replies += reply("Tried a chart.", "\$render(") } + ask("Add a chart of my steps") + wait(By.text("It did not run, so the widget is unchanged."), 40) + wait(By.text("Try again")) + wait(By.text("May contact: example.com")) + picture("07-did-not-run") + + // Going back to the first version, and saving that one: it uses no network, so nothing is asked. + tap(By.desc("Previous version")) + tap(By.desc("Previous version")) + wait(By.text("1 of 3")) + assertTrue(device.wait(Until.gone(By.text("May contact: example.com")), 10_000)) + picture("08-older-version") + tap(By.text("Save widget")) + wait(By.text("Save as a widget")) + assertEquals("the suggested name is short", "A minimalist clock widget", wait(By.clazz("android.widget.EditText")).text) + picture("09-name") + tap(By.text("OK")) + wait(By.text("Improve with AI")) + val saved = AppServices.library.list().single { it.name !in before } + created += saved.name + assertTrue(saved.files.readText("main.jsx")!!.contains("#1e1b4b")) + assertEquals("a widget that uses no network gets no permission", 0, saved.permissions.size) + assertNotNull("the conversation follows the widget", AiServices.conversations(context).latest(saved.installation)) + picture("10-saved") + + // The widget's own screen leads back into the same conversation. + tap(By.text("Improve with AI")) + // The list opens at its newest turn. + wait(By.text("It did not run, so the widget is unchanged.")) + wait(By.text("In the widget")) + picture("11-improve") + + // The AI works on a copy; the widget changes when Apply is confirmed, and gains the host then. + replies += reply("Green now, with a line from example.org.", "let line = \"offline\";\ntry { line = (await fetch(\"https://example.org/\")).slice(0, 12); } catch (error) {}\n" + widget("#15803d", "12:30", "\n {line}"), "example.org") + ask("Make it green and add a line from example.org") + wait(By.text("Apply to widget")) + assertTrue("the widget is untouched until Apply", AppServices.library.find(saved.name)!!.files.readText("main.jsx")!!.contains("#1e1b4b")) + picture("12-not-applied") + tap(By.text("Apply to widget")) + wait(By.text("Allow and apply")) + picture("13-confirm-apply") + tap(By.text("Allow and apply")) + wait(By.text("In the widget")) + val applied = AppServices.library.find(saved.name)!! + assertTrue(applied.files.readText("main.jsx")!!.contains("#15803d")) + assertTrue(applied.files.readText("widget.json")!!.contains("example.org")) + assertEquals(listOf("network"), applied.permissions) + picture("14-applied") + } +} diff --git a/Android/app/src/main/java/com/everettjf/scriptwidget/MainActivity.kt b/Android/app/src/main/java/com/everettjf/scriptwidget/MainActivity.kt index 16952533..7935bebc 100644 --- a/Android/app/src/main/java/com/everettjf/scriptwidget/MainActivity.kt +++ b/Android/app/src/main/java/com/everettjf/scriptwidget/MainActivity.kt @@ -11,7 +11,7 @@ import androidx.compose.runtime.mutableStateOf import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.rememberNavController -import com.everettjf.scriptwidget.ui.AiScreen +import com.everettjf.scriptwidget.ui.AiConversationScreen import com.everettjf.scriptwidget.ui.AppTheme import com.everettjf.scriptwidget.ui.DetailScreen import com.everettjf.scriptwidget.ui.EditorScreen @@ -58,8 +58,8 @@ class MainActivity : ComponentActivity() { composable("live-activities") { com.everettjf.scriptwidget.ui.LiveActivitiesScreen(navigation) } composable("skills") { com.everettjf.scriptwidget.ui.SkillsScreen(navigation) } composable("plugins") { com.everettjf.scriptwidget.ui.PluginsScreen(navigation, AppServices.plugins) } - composable("ai") { AiScreen(navigation, null) } - composable("ai/{name}") { entry -> AiScreen(navigation, Uri.decode(entry.arguments?.getString("name").orEmpty())) } + composable("ai") { AiConversationScreen(navigation, null) } + composable("ai/{name}") { entry -> AiConversationScreen(navigation, Uri.decode(entry.arguments?.getString("name").orEmpty())) } composable("package/{name}") { entry -> DetailScreen(navigation, Uri.decode(entry.arguments?.getString("name").orEmpty())) } composable("editor/{name}") { entry -> EditorScreen(navigation, Uri.decode(entry.arguments?.getString("name").orEmpty())) } } diff --git a/Android/app/src/main/java/com/everettjf/scriptwidget/ai/AiSettingsStore.kt b/Android/app/src/main/java/com/everettjf/scriptwidget/ai/AiSettingsStore.kt index b0ba74e5..967ab74f 100644 --- a/Android/app/src/main/java/com/everettjf/scriptwidget/ai/AiSettingsStore.kt +++ b/Android/app/src/main/java/com/everettjf/scriptwidget/ai/AiSettingsStore.kt @@ -2,6 +2,7 @@ package com.everettjf.scriptwidget.ai import android.content.Context import android.content.res.AssetManager +import com.everettjf.scriptwidget.core.ai.AiConversationStore import com.everettjf.scriptwidget.core.ai.AiProfile import com.everettjf.scriptwidget.core.ai.AiProviderKind import com.everettjf.scriptwidget.core.ai.AiReference @@ -62,6 +63,7 @@ class AiSettingsStore(context: Context, private val credentials: CredentialStore object AiServices { @Volatile private var instance: Holder? = null @Volatile private var cachedReference: AiReference? = null + @Volatile private var cachedConversations: AiConversationStore? = null private class Holder(val credentials: CredentialStore, val settings: AiSettingsStore) @@ -73,6 +75,14 @@ object AiServices { fun settings(context: Context): AiSettingsStore = holder(context).settings + /** + * AI conversations, kept in the no-backup directory: they hold the user's requests and + * widget sources and stay on this device. They never hold a key or name a provider. + */ + fun conversations(context: Context): AiConversationStore = cachedConversations ?: synchronized(this) { + cachedConversations ?: AiConversationStore(java.io.File(context.applicationContext.noBackupFilesDir, "ai-conversations")).also { cachedConversations = it } + } + /** * The component/API reference for the system prompt, read from the same `Script.bundle` * examples the Apple apps sample (`AIReferenceSnapshotLoader`). Blocking on first use. diff --git a/Android/app/src/main/java/com/everettjf/scriptwidget/ui/AiConversationScreen.kt b/Android/app/src/main/java/com/everettjf/scriptwidget/ui/AiConversationScreen.kt new file mode 100644 index 00000000..a0570324 --- /dev/null +++ b/Android/app/src/main/java/com/everettjf/scriptwidget/ui/AiConversationScreen.kt @@ -0,0 +1,841 @@ +package com.everettjf.scriptwidget.ui + +import android.content.Context +import android.net.Uri +import android.text.format.DateUtils +import android.widget.Toast +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.horizontalScroll +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.ExperimentalLayoutApi +import androidx.compose.foundation.layout.FlowRow +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.heightIn +import androidx.compose.foundation.layout.imePadding +import androidx.compose.foundation.layout.navigationBarsPadding +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.layout.widthIn +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.lazy.rememberLazyListState +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.text.KeyboardOptions +import androidx.compose.foundation.verticalScroll +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.automirrored.filled.ArrowBack +import androidx.compose.material.icons.automirrored.filled.Send +import androidx.compose.material.icons.filled.AutoFixHigh +import androidx.compose.material.icons.filled.CheckCircle +import androidx.compose.material.icons.filled.ChevronLeft +import androidx.compose.material.icons.filled.ChevronRight +import androidx.compose.material.icons.filled.Code +import androidx.compose.material.icons.filled.DeleteOutline +import androidx.compose.material.icons.filled.Edit +import androidx.compose.material.icons.filled.History +import androidx.compose.material.icons.filled.Language +import androidx.compose.material.icons.filled.MoreVert +import androidx.compose.material.icons.filled.Stop +import androidx.compose.material3.AlertDialog +import androidx.compose.material3.AssistChip +import androidx.compose.material3.Button +import androidx.compose.material3.Card +import androidx.compose.material3.CardDefaults +import androidx.compose.material3.CircularProgressIndicator +import androidx.compose.material3.DropdownMenu +import androidx.compose.material3.DropdownMenuItem +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.FilledIconButton +import androidx.compose.material3.FilledTonalIconButton +import androidx.compose.material3.FilterChip +import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.ModalBottomSheet +import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.Scaffold +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.material3.TopAppBar +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.alpha +import androidx.compose.ui.draw.clip +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.semantics.contentDescription +import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.input.KeyboardCapitalization +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.ViewModel +import androidx.lifecycle.compose.LifecycleEventEffect +import androidx.lifecycle.viewModelScope +import androidx.lifecycle.viewmodel.compose.viewModel +import androidx.navigation.NavController +import com.everettjf.scriptwidget.AppServices +import com.everettjf.scriptwidget.InstalledPackage +import com.everettjf.scriptwidget.ai.AiServices +import com.everettjf.scriptwidget.core.PlatformSupport +import com.everettjf.scriptwidget.core.RunRequest +import com.everettjf.scriptwidget.core.ai.AgentLoop +import com.everettjf.scriptwidget.core.ai.AgentLoopEvent +import com.everettjf.scriptwidget.core.ai.AgentLoopOutcome +import com.everettjf.scriptwidget.core.ai.AgentLoopRequest +import com.everettjf.scriptwidget.core.ai.AiCancellation +import com.everettjf.scriptwidget.core.ai.AiClient +import com.everettjf.scriptwidget.core.ai.AiConversation +import com.everettjf.scriptwidget.core.ai.AiConversationLimits +import com.everettjf.scriptwidget.core.ai.AiConversationSummary +import com.everettjf.scriptwidget.core.ai.AiConversationTurn +import com.everettjf.scriptwidget.core.ai.AiConversationTurns +import com.everettjf.scriptwidget.core.ai.AiConversationVersion +import com.everettjf.scriptwidget.core.ai.AiExamplePrompts +import com.everettjf.scriptwidget.core.ai.AiManifestDeclaration +import com.everettjf.scriptwidget.core.ai.AiManifests +import com.everettjf.scriptwidget.core.ai.AiProfile +import com.everettjf.scriptwidget.core.ai.AiTokenUsage +import com.everettjf.scriptwidget.core.ai.AiWidgetSize +import com.everettjf.scriptwidget.core.ai.PromptBuilder +import com.everettjf.scriptwidget.core.ai.ValidationResult +import com.everettjf.scriptwidget.core.pkg.PackageLimits +import com.everettjf.scriptwidget.core.runtime.MemoryPackageFiles +import com.everettjf.scriptwidget.core.runtime.PackageFiles +import com.everettjf.scriptwidget.widget.WidgetScheduler +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import java.net.URI + +private const val PREVIEW_PACKAGE_ID = "ai-preview" + +/** A package's files with some text files replaced in memory; nothing is written to disk. */ +private class OverlayPackageFiles(private val base: PackageFiles, private val overrides: Map) : PackageFiles { + override fun paths(): List = (base.paths() + overrides.keys).distinct().sorted() + override fun exists(path: String): Boolean = overrides.containsKey(path) || base.exists(path) + override fun readText(path: String): String? = overrides[path] ?: base.readText(path) + override fun readBytes(path: String): ByteArray? = overrides[path]?.toByteArray() ?: base.readBytes(path) +} + +/** + * How a version of a conversation becomes a package: in memory for the preview, and on disk + * when it is saved or applied. The model only ever influences the network declaration. + */ +internal object AiCandidates { + /** Entry file of an installed package: the manifest's `entry`, or `main.jsx` for a legacy package. */ + fun entryOf(installed: InstalledPackage): String = AiManifests.entry(installed.manifest) + + /** Hosts an installed widget may already contact. */ + fun allowedHosts(installed: InstalledPackage?): List = AiManifestDeclaration.fromManifest(installed?.manifest)?.networkDomains.orEmpty() + + /** + * The declaration a version runs and is saved with. A new widget gets exactly what the + * version declares. An installed widget keeps the hosts it already has and gains the + * declared ones: a conversation never takes a permission away from a widget. + */ + fun declaration(installed: InstalledPackage?, domains: List): String? { + val hosts = (allowedHosts(installed) + domains).distinct() + return if (hosts.isEmpty()) null else AiManifestDeclaration(listOf("network"), hosts).toJson() + } + + /** Declared hosts the installed widget may not contact yet; the user is asked before they are added. */ + fun newHosts(installed: InstalledPackage?, domains: List): List = domains.filter { it !in allowedHosts(installed) } + + /** The manifest text of a version, or null for a legacy package that has no `widget.json`. */ + fun manifestText(installed: InstalledPackage?, domains: List, newName: String): String? { + if (installed != null && installed.manifest == null) return null + return AiManifests.text(installed?.manifest, declaration(installed, domains), newName) + } + + /** In-memory package of a version: a fresh one, or the installed package with its entry replaced. */ + fun files(installed: InstalledPackage?, source: String, domains: List, newName: String = "AI Widget"): PackageFiles { + val manifest = manifestText(installed, domains, newName) + if (installed == null) return MemoryPackageFiles(mapOf(AiManifests.DEFAULT_ENTRY to source, AiManifests.FILE_NAME to manifest!!)) + val overrides = LinkedHashMap() + overrides[entryOf(installed)] = source + if (manifest != null) overrides[AiManifests.FILE_NAME] = manifest + return OverlayPackageFiles(installed.files, overrides) + } + + /** Compiles and executes a candidate in memory, exactly as the preview will. Blocking. */ + fun validate(installed: InstalledPackage?, source: String, declarationJson: String?, family: String, baseline: Set): ValidationResult { + if (source.toByteArray().size > PackageLimits.MAX_SCRIPT_BYTES) return ValidationResult.failure("resourceLimit: main.jsx exceeds ${PackageLimits.MAX_SCRIPT_BYTES / 1024} KiB.") + val domains = declarationJson?.let { runCatching { AiManifestDeclaration.parse(it).networkDomains }.getOrNull() }.orEmpty() + val files = files(installed, source, domains) + val runtime = AppServices.runtime + val execution = runtime.run(RunRequest(files, PREVIEW_PACKAGE_ID, family, darkMode = runtime.isDarkMode())).execution + val logs = execution.logs.map { "[${it.level}] ${it.message}" } + val result = ValidationResult.fromExecution(execution.tree, execution.error?.kind, execution.error?.message, logs) + if (!result.ok) return result + val unsupported = PlatformSupport.unsupported(AiManifests.parse(files.readText(AiManifests.FILE_NAME)), files).filter { it !in baseline } + if (unsupported.isNotEmpty()) return ValidationResult.failure("The widget uses features that are not supported on Android: ${unsupported.joinToString(" ")}", logs) + return result + } +} + +/** + * The conversation on screen. It owns the conversation object: every change + * happens on the main thread and is followed by [changed], which redraws and + * saves. The model call and the trial run happen off the main thread. + */ +class AiConversationViewModel : ViewModel() { + /** Read in a composable to be redrawn when the conversation changes. */ + var revision by mutableIntStateOf(0); private set + var conversation by mutableStateOf(AiConversation()); private set + /** The installed widget the conversation is about; null for a new widget. */ + var installed by mutableStateOf(null); private set + /** What the installed widget's entry file holds, to tell whether the version on screen is applied. */ + var installedSource by mutableStateOf(null); private set + var isLoading by mutableStateOf(true); private set + var isMissing by mutableStateOf(false); private set + /** What is happening in the running turn, in words. */ + var activity by mutableStateOf(null); private set + var drafts by mutableStateOf>(emptyList()); private set + + private var cancellation: AiCancellation? = null + private var generation = 0 + private var opened = false + + val isRunning: Boolean get() = conversation.isRunning + + /** Opens the conversation of the installed widget [name], or an empty one for a new widget. */ + fun open(context: Context, name: String?) { + if (opened) return + opened = true + val application = context.applicationContext + viewModelScope.launch { + val store = AiServices.conversations(application) + val loaded = withContext(Dispatchers.IO) { + val target = name?.let { AppServices.library.find(it) } + val source = target?.let { it.files.readText(AiCandidates.entryOf(it)) } + val existing = target?.let { store.latest(it.installation) } + Triple(target, source, existing) to store.summaries(draftsOnly = true) + } + val (target, source, existing) = loaded.first + drafts = loaded.second + if (name != null && (target == null || source == null)) { isMissing = true; isLoading = false; return@launch } + installed = target + installedSource = source + if (target != null && source != null) { + val bound = existing ?: AiConversation(packageId = target.installation, title = target.displayName) + // Code the conversation has not seen (the editor, a Gallery update) becomes a version of its own. + val adopted = bound.adoptExternalSource(source, AiCandidates.allowedHosts(target)) + conversation = bound + if (adopted && existing != null) changed(application) + } + isLoading = false + revision += 1 + } + } + + /** Continues a draft. */ + fun openDraft(context: Context, id: String) { + if (isRunning) return + val application = context.applicationContext + viewModelScope.launch { + val draft = withContext(Dispatchers.IO) { AiServices.conversations(application).load(id) } ?: return@launch + conversation = draft + revision += 1 + } + } + + /** Leaves the conversation on screen as it is saved and starts an empty one. */ + fun startNew(context: Context) { + if (isRunning) return + conversation = AiConversation(packageId = installed?.installation, title = installed?.displayName.orEmpty()).also { fresh -> + val source = installedSource + if (installed != null && source != null) fresh.adoptExternalSource(source, AiCandidates.allowedHosts(installed)) + } + revision += 1 + refreshDrafts(context) + } + + fun deleteDraft(context: Context, id: String) { + val application = context.applicationContext + viewModelScope.launch { + withContext(Dispatchers.IO) { AiServices.conversations(application).delete(id) } + if (conversation.id == id) startNew(application) else refreshDrafts(application) + } + } + + private fun refreshDrafts(context: Context) { + val application = context.applicationContext + viewModelScope.launch { drafts = withContext(Dispatchers.IO) { AiServices.conversations(application).summaries(draftsOnly = true) } } + } + + fun setSize(context: Context, size: AiWidgetSize) { + if (conversation.size == size) return + conversation.size = size + changed(context) + } + + fun selectVersion(context: Context, id: String) { + if (isRunning) return + if (conversation.selectVersion(id)) changed(context) + } + + /** Sends [request] as the next turn. */ + fun send(context: Context, request: String) = start(context) { AiConversationTurns.begin(conversation, request, "", null) } + + /** Sends a turn that did not succeed again. */ + fun retry(context: Context, turnId: String) = start(context) { AiConversationTurns.retry(conversation, turnId, "", null) } + + private fun start(context: Context, begin: () -> AiConversationTurns.Started?) { + val application = context.applicationContext + if (isRunning) return + val started = begin() ?: return + val turnId = started.turn.id + val token = AiCancellation() + cancellation = token + val run = ++generation + activity = "Preparing…" + changed(application) + val target = installed + val family = conversation.size.family + val mode = started.loopRequest.mode as AgentLoopRequest.Mode.Turn + + viewModelScope.launch { + val outcome: AgentLoopOutcome? = withContext(Dispatchers.IO) { + try { + val settings = AiServices.settings(application).load() + settings.profile.validationError()?.let { return@withContext AgentLoopOutcome.Failed(it, AiTokenUsage.ZERO) } + val key = AiServices.credentials(application).get(settings.profile.kind.id) + ?: return@withContext AgentLoopOutcome.Failed("No API key is set. Open Settings → AI to add one.", AiTokenUsage.ZERO) + val baseline = target?.let { PlatformSupport.unsupported(it.manifest, it.files).toSet() } ?: emptySet() + // Skills are read when the turn is sent, so a Skill switched on mid-conversation applies from the next turn. + val context = mode.context + val withSkills = com.everettjf.scriptwidget.core.ai.AiTurnContext( + context.history, context.currentCode, context.previousCode, context.request, AppServices.skillsPrompt(), + context.size, context.currentDeclaration, context.runtimeDiagnostic, + ) + val request = AgentLoopRequest(AgentLoopRequest.Mode.Turn(withSkills), context.size, PromptBuilder.systemPrompt(AiServices.reference(application)), AiConversationLimits.MAX_ATTEMPTS_PER_TURN) + AgentLoop(chatFactory(settings, key)).run( + request, + validate = { source, declaration -> AiCandidates.validate(target, source, declaration, family, baseline) }, + cancellation = token, + onEvent = { event -> viewModelScope.launch { if (run == generation) apply(turnId, event) } }, + ) + } catch (_: Exception) { + // Nothing here may surface exception text: it could describe the request. + null + } + } + if (run != generation) return@launch + AiConversationTurns.applyOutcome(conversation, turnId, outcome) + cancellation = null + activity = null + changed(application) + } + } + + private fun apply(turnId: String, event: AgentLoopEvent) { + AiConversationTurns.applyEvent(conversation, turnId, event) + when (event) { + is AgentLoopEvent.Thinking -> activity = if (event.iteration == 1) "Writing the widget…" else "It did not run. Fixing it, attempt ${event.iteration} of ${AiConversationLimits.MAX_ATTEMPTS_PER_TURN}…" + is AgentLoopEvent.Running -> activity = "Checking that it runs…" + else -> Unit + } + revision += 1 + } + + /** Stops the running turn at once; the widget stays as it was. */ + fun stop(context: Context) { + val running = conversation.turns.firstOrNull { it.status == AiConversationTurn.Status.RUNNING } ?: return + generation += 1 + cancellation?.cancel() + cancellation = null + AiConversationTurns.applyOutcome(conversation, running.id, AgentLoopOutcome.Cancelled(running.usage)) + activity = null + changed(context) + } + + /** The conversation now belongs to the widget it was saved as or applied to. */ + fun bind(context: Context, saved: InstalledPackage, source: String) { + installed = saved + installedSource = source + conversation.packageId = saved.installation + changed(context) + } + + override fun onCleared() { cancellation?.cancel() } + + companion object { + /** The model a turn talks to. Tests put a scripted one here; nothing else changes it. */ + @Volatile internal var chatFactory: (com.everettjf.scriptwidget.core.ai.AiSettings, String) -> com.everettjf.scriptwidget.core.ai.AiChat = + { settings, key -> AiClient().bind(settings, key) } + } + + /** Redraws and saves. A conversation nobody has written to is not saved. */ + private fun changed(context: Context) { + revision += 1 + if (conversation.turns.isEmpty()) return + val application = context.applicationContext + val text = conversation + viewModelScope.launch(Dispatchers.Main.immediate) { + // Encoded on the main thread, where the conversation is owned; written off it. + val snapshot = AiConversation.fromJson(text.toJson()) ?: return@launch + // A running turn is read back as interrupted, which is what a closed app leaves behind. + withContext(Dispatchers.IO) { AiServices.conversations(application).save(snapshot) } + } + } +} + +/** + * The AI conversation about one widget: the preview on top, the turns below, + * the request field at the bottom. [name] is the installed widget to work on, + * or null to make a new one. + */ +@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class) +@Composable +fun AiConversationScreen(navigation: NavController, name: String?) { + val context = LocalContext.current + val model: AiConversationViewModel = viewModel() + LaunchedEffect(name) { model.open(context, name) } + + var settingsRevision by remember { mutableIntStateOf(0) } + LifecycleEventEffect(Lifecycle.Event.ON_RESUME) { settingsRevision += 1 } + // The profile is not secret; whether a key exists is read off the main thread. + var profile by remember { mutableStateOf(null) } + var hasKey by remember { mutableStateOf(true) } + LaunchedEffect(settingsRevision) { + val loaded = withContext(Dispatchers.IO) { AiServices.settings(context).load().profile.let { it to AiServices.credentials(context).has(it.kind.id) } } + profile = loaded.first; hasKey = loaded.second + } + val ready = profile?.isConfigured == true && hasKey + + var draft by rememberSaveable { mutableStateOf("") } + var menu by remember { mutableStateOf(false) } + var showDrafts by remember { mutableStateOf(false) } + var shownCode by remember { mutableStateOf?>(null) } + var askName by remember { mutableStateOf(false) } + var confirmNetwork by remember { mutableStateOf(null) } + var confirmApply by remember { mutableStateOf(false) } + val scope = rememberCoroutineScope() + val dark = isSystemInDarkTheme() + + @Suppress("UNUSED_VARIABLE") val redraw = model.revision + val conversation = model.conversation + val installed = model.installed + val current = conversation.currentVersion + val running = model.isRunning + + if (model.isMissing) { Text("This widget is no longer installed.", Modifier.padding(32.dp)); return } + + fun leave() { model.stop(context); navigation.popBackStack() } + + fun send() { + val text = draft.trim() + if (text.isEmpty() || running || !ready) return + draft = "" + model.send(context, text) + } + + fun finishSave(result: Result, source: String, open: Boolean) { + result.onSuccess { saved -> + model.bind(context, saved, source) + WidgetScheduler.refreshPackage(context, saved.installation) + if (open) navigation.navigate("package/${Uri.encode(saved.name)}") { popUpTo("home") } + else Toast.makeText(context, "Applied to ${saved.displayName}", Toast.LENGTH_SHORT).show() + }.onFailure { Toast.makeText(context, it.message ?: "Could not save the widget", Toast.LENGTH_LONG).show() } + } + + fun saveNew(newName: String, version: AiConversationVersion) { + scope.launch { + finishSave(withContext(Dispatchers.IO) { runCatching { + val library = AppServices.library + val unique = library.store.uniqueName(newName) + library.create(unique, version.source, AiCandidates.manifestText(null, version.networkDomains, unique)) + } }, version.source, open = true) + } + } + + fun apply(target: InstalledPackage, version: AiConversationVersion) { + scope.launch { + finishSave(withContext(Dispatchers.IO) { runCatching { + val library = AppServices.library + library.writeFile(target.name, AiCandidates.entryOf(target), version.source.toByteArray(Charsets.UTF_8)) + val manifest = AiCandidates.manifestText(target, version.networkDomains, target.name) + if (manifest != null && AiCandidates.newHosts(target, version.networkDomains).isNotEmpty()) library.writeFile(target.name, AiManifests.FILE_NAME, manifest.toByteArray(Charsets.UTF_8)) + library.find(target.name) ?: target + } }, version.source, open = false) + } + } + + Scaffold( + topBar = { + TopAppBar( + title = { + @Suppress("UNUSED_VARIABLE") val redrawTitle = model.revision + Column { + Text(if (installed != null) installed.displayName else conversation.title.ifEmpty { "New AI widget" }, maxLines = 1, overflow = TextOverflow.Ellipsis) + Text( + if (installed != null) "AI conversation" else if (conversation.turns.isEmpty()) "Describe it, then keep asking for changes" else "Draft", + style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant, maxLines = 1, + ) + } + }, + navigationIcon = { IconButton(onClick = { leave() }) { Icon(Icons.AutoMirrored.Filled.ArrowBack, "Back") } }, + actions = { + @Suppress("UNUSED_VARIABLE") val redrawActions = model.revision + if (installed == null && model.drafts.isNotEmpty()) IconButton(enabled = !running, onClick = { showDrafts = true }) { Icon(Icons.Default.History, "Drafts") } + IconButton(onClick = { menu = true }) { Icon(Icons.Default.MoreVert, "More") } + DropdownMenu(expanded = menu, onDismissRequest = { menu = false }) { + DropdownMenuItem(text = { Text("New conversation") }, enabled = !running && conversation.turns.isNotEmpty(), onClick = { menu = false; draft = ""; model.startNew(context) }) + if (installed == null) DropdownMenuItem(text = { Text("Delete this draft") }, enabled = !running && conversation.turns.isNotEmpty(), onClick = { menu = false; model.deleteDraft(context, conversation.id) }) + DropdownMenuItem(text = { Text("AI settings") }, onClick = { menu = false; navigation.navigate("settings") }) + } + }, + ) + }, + bottomBar = { + Composer( + text = draft, onText = { draft = it.take(AiConversationLimits.MAX_REQUEST_CHARACTERS) }, running = running, enabled = ready && !model.isLoading, + placeholder = if (current == null) "Describe the widget you want…" else "Say what to change…", + onSend = { send() }, onStop = { model.stop(context) }, + ) + }, + ) { padding -> + // Read here as well: this block is its own scope, and the conversation changes in place. + @Suppress("UNUSED_VARIABLE") val redrawContent = model.revision + Column(Modifier.padding(padding).fillMaxSize()) { + if (!ready && profile != null) Card(colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.tertiaryContainer), modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp)) { + Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) { + Text("Set up an AI provider", style = MaterialTheme.typography.titleSmall) + Text("Choose a provider and enter its API key in Settings → AI. The key is stored encrypted on this device.", style = MaterialTheme.typography.bodySmall) + TextButton(onClick = { navigation.navigate("settings") }) { Text("Open Settings") } + } + } + + if (current != null) { + val ordered = conversation.versions.sortedBy { it.number } + val index = ordered.indexOfFirst { it.id == current.id } + PreviewPanel( + size = conversation.size, version = current, position = index + 1, count = ordered.size, + previousId = ordered.getOrNull(index - 1)?.id, nextId = if (index >= 0) ordered.getOrNull(index + 1)?.id else null, + installed = installed, dark = dark, running = running, + applied = installed != null && model.installedSource == current.source, + onSize = { model.setSize(context, it) }, onVersion = { model.selectVersion(context, it) }, + onCode = { shownCode = "Version ${current.number}" to current.source }, + onSave = { if (installed == null) askName = true else confirmApply = true }, + ) + } + + if (conversation.turns.none { !it.isManualEdit }) { + EmptyState( + installed = installed, profile = profile, drafts = if (installed == null) model.drafts else emptyList(), enabled = ready, + onExample = { example -> draft = example.prompt; model.setSize(context, example.size) }, + onDraft = { model.openDraft(context, it) }, onAllDrafts = { showDrafts = true }, + modifier = Modifier.weight(1f), + ) + } else { + TurnList( + turns = conversation.turns.map { TurnUi.of(conversation, it) }, totalTokens = conversation.totalUsage.totalTokens, + activity = model.activity, running = running, + onVersion = { model.selectVersion(context, it) }, onRetry = { model.retry(context, it) }, + onFailedCode = { id -> conversation.turn(id)?.failedSource?.let { shownCode = "The attempt that did not run" to it } }, + modifier = Modifier.weight(1f), + ) + } + } + } + + shownCode?.let { (title, code) -> + AlertDialog( + onDismissRequest = { shownCode = null }, title = { Text(title) }, + text = { + Text(code, style = MaterialTheme.typography.bodySmall, fontFamily = FontFamily.Monospace, + modifier = Modifier.heightIn(max = 420.dp).verticalScroll(rememberScrollState()).horizontalScroll(rememberScrollState())) + }, + confirmButton = { TextButton(onClick = { shownCode = null }) { Text("Close") } }, + ) + } + + if (askName && current != null) NameDialog("Save as a widget", suggestedName(conversation.title), onDismiss = { askName = false }) { newName -> + askName = false + if (current.networkDomains.isEmpty()) saveNew(newName, current) else confirmNetwork = newName + } + confirmNetwork?.let { newName -> + if (current != null) AlertDialog( + onDismissRequest = { confirmNetwork = null }, title = { Text("Allow this widget to use the network?") }, + text = { Text("The AI wrote a widget that contacts: ${current.networkDomains.joinToString(", ")}. Saving gives it the network permission for these hosts only.") }, + confirmButton = { TextButton(onClick = { confirmNetwork = null; saveNew(newName, current) }) { Text("Save and allow") } }, + dismissButton = { TextButton(onClick = { confirmNetwork = null }) { Text("Cancel") } }, + ) + } + if (confirmApply && current != null && installed != null) { + val added = AiCandidates.newHosts(installed, current.networkDomains) + AlertDialog( + onDismissRequest = { confirmApply = false }, title = { Text("Apply version ${current.number} to ${installed.displayName}?") }, + text = { + Text( + "${AiCandidates.entryOf(installed)} is replaced with this version. Widgets on the home screen showing it are redrawn." + + if (added.isEmpty()) "" else "\n\nThis version contacts ${added.joinToString(", ")}. Applying adds the network permission for these hosts to the widget.", + ) + }, + confirmButton = { TextButton(onClick = { confirmApply = false; apply(installed, current) }) { Text(if (added.isEmpty()) "Apply" else "Allow and apply") } }, + dismissButton = { TextButton(onClick = { confirmApply = false }) { Text("Cancel") } }, + ) + } + if (showDrafts) ModalBottomSheet(onDismissRequest = { showDrafts = false }) { + Column(Modifier.fillMaxWidth().padding(bottom = 24.dp)) { + Text("Drafts", style = MaterialTheme.typography.titleMedium, modifier = Modifier.padding(horizontal = 20.dp, vertical = 8.dp)) + Text("Conversations you have not saved as a widget. They stay on this device.", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(horizontal = 20.dp)) + LazyColumn(Modifier.heightIn(max = 420.dp).padding(top = 8.dp)) { + items(model.drafts, key = { it.id }) { item -> + DraftRow(item, onOpen = { showDrafts = false; model.openDraft(context, item.id) }, onDelete = { model.deleteDraft(context, item.id) }) + } + } + } + } +} + +@Composable +private fun DraftRow(item: AiConversationSummary, onOpen: () -> Unit, onDelete: (() -> Unit)?) { + Row(Modifier.fillMaxWidth().clickable(onClick = onOpen).padding(start = 20.dp, end = 8.dp, top = 6.dp, bottom = 6.dp), verticalAlignment = Alignment.CenterVertically) { + Column(Modifier.weight(1f)) { + Text(item.title.ifEmpty { "Untitled" }, maxLines = 1, overflow = TextOverflow.Ellipsis, style = MaterialTheme.typography.bodyLarge) + Text( + (if (item.versionCount == 0) "No version yet" else if (item.versionCount == 1) "1 version" else "${item.versionCount} versions") + " · " + + (if (System.currentTimeMillis() - item.updatedAt < DateUtils.MINUTE_IN_MILLIS) "just now" else DateUtils.getRelativeTimeSpanString(item.updatedAt, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS)), + style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + if (onDelete != null) IconButton(onClick = onDelete) { Icon(Icons.Default.DeleteOutline, "Delete draft ${item.title}") } + } +} + +/** A short name for a new widget from what the user first asked: its first phrase, a few words long. */ +internal fun suggestedName(title: String): String { + val phrase = title.trim().split('.', ',', ':', ';', '!', '?', '。', ',', '\n').firstOrNull { it.isNotBlank() }?.trim().orEmpty().removeSuffix("…") + val words = phrase.split(' ').filter { it.isNotBlank() } + var name = "" + for (word in words) { if ((name + " " + word).trim().length > 28) break; name = (name + " " + word).trim() } + if (name.isEmpty()) name = phrase.take(28) + return name.replace(Regex("[/\\\\:*?\"<>|]"), " ").trim().ifBlank { "AI Widget" }.replaceFirstChar { it.uppercase() } +} + +/** The widget as it is now, its size, its versions and the button that keeps it. */ +@Composable +private fun PreviewPanel( + size: AiWidgetSize, version: AiConversationVersion, position: Int, count: Int, previousId: String?, nextId: String?, + installed: InstalledPackage?, dark: Boolean, running: Boolean, applied: Boolean, + onSize: (AiWidgetSize) -> Unit, onVersion: (String) -> Unit, onCode: () -> Unit, onSave: () -> Unit, +) { + Surface(color = MaterialTheme.colorScheme.surfaceContainerLow, modifier = Modifier.fillMaxWidth()) { + Column(Modifier.padding(horizontal = 16.dp, vertical = 10.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(8.dp)) { + // The version runs from memory with exactly the manifest it would be saved with. + val files = remember(version.id, installed?.installation) { AiCandidates.files(installed, version.source, version.networkDomains) } + Box(contentAlignment = Alignment.TopEnd) { + WidgetPreview(files = files, packageID = PREVIEW_PACKAGE_ID, family = size.family, dark = dark, revision = version.number, maxHeight = 190f) + if (running) Surface(shape = RoundedCornerShape(12.dp), color = MaterialTheme.colorScheme.surface.copy(alpha = 0.92f), modifier = Modifier.padding(8.dp)) { + Text("Version ${version.number} while the AI works", style = MaterialTheme.typography.labelSmall, modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp)) + } + } + Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)) { + for (option in AiWidgetSize.entries) FilterChip(selected = size == option, onClick = { onSize(option) }, label = { Text(option.displayName) }) + Spacer(Modifier.weight(1f)) + IconButton(enabled = !running && previousId != null, onClick = { previousId?.let(onVersion) }) { Icon(Icons.Default.ChevronLeft, "Previous version") } + // Position among the versions that are kept, which is what the arrows move through. + Text("$position of $count", style = MaterialTheme.typography.labelLarge, maxLines = 1, modifier = Modifier.semantics { contentDescription = "Version ${version.number}, $position of $count" }) + IconButton(enabled = !running && nextId != null, onClick = { nextId?.let(onVersion) }) { Icon(Icons.Default.ChevronRight, "Next version") } + } + if (version.networkDomains.isNotEmpty()) Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { + Icon(Icons.Default.Language, null, Modifier.size(14.dp), tint = MaterialTheme.colorScheme.onSurfaceVariant) + Text("May contact: ${version.networkDomains.joinToString(", ")}", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, maxLines = 2, overflow = TextOverflow.Ellipsis, modifier = Modifier.padding(start = 6.dp)) + } + Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) { + TextButton(onClick = onCode) { Icon(Icons.Default.Code, null, Modifier.size(18.dp)); Text("Code", Modifier.padding(start = 6.dp)) } + Spacer(Modifier.weight(1f)) + if (installed != null && applied) Row(verticalAlignment = Alignment.CenterVertically) { + Icon(Icons.Default.CheckCircle, null, Modifier.size(16.dp), tint = MaterialTheme.colorScheme.primary) + Text("In the widget", style = MaterialTheme.typography.labelLarge, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(start = 6.dp, end = 8.dp)) + } else Button(enabled = !running, onClick = onSave) { Text(if (installed == null) "Save widget" else "Apply to widget") } + } + } + } + HorizontalDivider() +} + +@OptIn(ExperimentalLayoutApi::class) +@Composable +private fun EmptyState( + installed: InstalledPackage?, profile: AiProfile?, drafts: List, enabled: Boolean, + onExample: (com.everettjf.scriptwidget.core.ai.AiExamplePrompt) -> Unit, onDraft: (String) -> Unit, onAllDrafts: () -> Unit, modifier: Modifier = Modifier, +) { + Column(modifier.fillMaxWidth().verticalScroll(rememberScrollState()).padding(16.dp), verticalArrangement = Arrangement.spacedBy(14.dp)) { + if (installed == null) { + Text("What should the widget show?", style = MaterialTheme.typography.titleMedium) + Text("Describe it in your own words. You see it as soon as it runs, and can keep asking for changes: “make the title smaller”, “use a green background”.", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) + Text("Try an example", style = MaterialTheme.typography.labelLarge) + FlowRow(horizontalArrangement = Arrangement.spacedBy(8.dp)) { + for (example in AiExamplePrompts.all) AssistChip(enabled = enabled, onClick = { onExample(example) }, label = { Text(example.title) }) + } + if (drafts.isNotEmpty()) { + Text("Continue a draft", style = MaterialTheme.typography.labelLarge) + Card(Modifier.fillMaxWidth()) { + Column { + for (item in drafts.take(3)) DraftRow(item, onOpen = { onDraft(item.id) }, onDelete = null) + if (drafts.size > 3) TextButton(onClick = onAllDrafts, modifier = Modifier.padding(horizontal = 12.dp)) { Text("All ${drafts.size} drafts") } + } + } + } + } else { + Text("Ask for a change", style = MaterialTheme.typography.titleMedium) + Text("The AI works on a copy. ${installed.displayName} stays as it is until you tap Apply to widget.", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) + } + profile?.let { active -> + val host = runCatching { URI(active.normalizedBaseUrl).host }.getOrNull().orEmpty() + Text( + "${active.kind.displayName} · ${active.model.ifBlank { "no model" }}. Your requests and the widget's code are sent to ${host.ifEmpty { "the configured server" }}. " + + "A change that does not run is repaired up to three times; each try is a request the provider bills.", + style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + } +} + +/** + * One turn as the list draws it. The conversation changes in place, which Compose can not + * see, so the screen makes these values anew whenever it changes and the rows compare them. + */ +private data class TurnUi( + val id: String, val request: String, val status: AiConversationTurn.Status, val note: String?, val error: String?, val repairs: Int, val tokens: Int, + val abandoned: Boolean, val manual: Boolean, val versionId: String?, val versionNumber: Int?, val isCurrent: Boolean, val hasFailedSource: Boolean, +) { + companion object { + fun of(conversation: AiConversation, turn: AiConversationTurn): TurnUi { + val version = conversation.version(turn.versionId) + return TurnUi( + turn.id, turn.request, turn.status, turn.note, + // The first line says what went wrong; the rest is the runtime's view of the source. + turn.errorSummary?.lineSequence()?.firstOrNull { it.isNotBlank() }?.trim(), + turn.repairs, turn.usage.totalTokens, turn.isAbandoned, turn.isManualEdit, version?.id, version?.number, + version != null && version.id == conversation.currentVersionId, turn.failedSource != null, + ) + } + } +} + +@Composable +private fun TurnList( + turns: List, totalTokens: Int, activity: String?, running: Boolean, + onVersion: (String) -> Unit, onRetry: (String) -> Unit, onFailedCode: (String) -> Unit, modifier: Modifier = Modifier, +) { + val state = rememberLazyListState() + // The newest turn stays in view while it changes. + LaunchedEffect(turns.size, turns.lastOrNull(), activity) { if (turns.isNotEmpty()) state.animateScrollToItem(turns.size) } + LazyColumn(modifier.fillMaxWidth(), state = state, contentPadding = androidx.compose.foundation.layout.PaddingValues(horizontal = 16.dp, vertical = 12.dp), verticalArrangement = Arrangement.spacedBy(14.dp)) { + items(turns, key = { it.id }) { turn -> TurnRow(turn, activity, running, onVersion, onRetry, onFailedCode) } + item(key = "total") { + if (totalTokens > 0) Text("$totalTokens tokens in this conversation", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.fillMaxWidth().padding(top = 2.dp)) + else Spacer(Modifier.size(1.dp)) + } + } +} + +@Composable +private fun VersionLine(turn: TurnUi, running: Boolean, onVersion: (String) -> Unit) { + val versionId = turn.versionId ?: return + Row(verticalAlignment = Alignment.CenterVertically) { + Icon(Icons.Default.CheckCircle, null, Modifier.size(16.dp), tint = if (turn.isCurrent) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurfaceVariant) + Text("Version ${turn.versionNumber}", style = MaterialTheme.typography.labelLarge, color = if (turn.isCurrent) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(start = 6.dp)) + if (turn.isCurrent) Text("shown above", style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(start = 8.dp)) + else TextButton(enabled = !running, onClick = { onVersion(versionId) }, contentPadding = androidx.compose.foundation.layout.PaddingValues(horizontal = 8.dp)) { Text("Show this version") } + if (turn.abandoned) Text("left behind", style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(start = 4.dp)) + } +} + +@Composable +private fun TurnRow(turn: TurnUi, activity: String?, running: Boolean, onVersion: (String) -> Unit, onRetry: (String) -> Unit, onFailedCode: (String) -> Unit) { + val faded = if (turn.abandoned) 0.55f else 1f + if (turn.manual) { + Column(Modifier.fillMaxWidth().alpha(faded), verticalArrangement = Arrangement.spacedBy(2.dp)) { + Row(verticalAlignment = Alignment.CenterVertically) { + Icon(Icons.Default.Edit, null, Modifier.size(14.dp), tint = MaterialTheme.colorScheme.onSurfaceVariant) + Text("The widget's code as it is installed", style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(start = 6.dp)) + } + VersionLine(turn, running, onVersion) + } + return + } + Column(Modifier.fillMaxWidth().alpha(faded), verticalArrangement = Arrangement.spacedBy(8.dp)) { + Box(Modifier.fillMaxWidth(), contentAlignment = Alignment.CenterEnd) { + Text( + turn.request, style = MaterialTheme.typography.bodyLarge, color = MaterialTheme.colorScheme.onPrimaryContainer, + modifier = Modifier.widthIn(max = 300.dp).clip(RoundedCornerShape(18.dp, 18.dp, 4.dp, 18.dp)).background(MaterialTheme.colorScheme.primaryContainer).padding(horizontal = 14.dp, vertical = 10.dp), + ) + } + when (turn.status) { + AiConversationTurn.Status.RUNNING -> Row(verticalAlignment = Alignment.CenterVertically) { + CircularProgressIndicator(Modifier.size(16.dp), strokeWidth = 2.dp) + Text(activity ?: "Working…", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(start = 10.dp)) + } + AiConversationTurn.Status.SUCCEEDED -> { + Text(turn.note ?: "Done.", style = MaterialTheme.typography.bodyLarge) + if (turn.repairs > 0) Row(verticalAlignment = Alignment.CenterVertically) { + Icon(Icons.Default.AutoFixHigh, null, Modifier.size(14.dp), tint = MaterialTheme.colorScheme.onSurfaceVariant) + Text(if (turn.repairs == 1) "Fixed automatically 1 time" else "Fixed automatically ${turn.repairs} times", style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(start = 6.dp)) + } + VersionLine(turn, running, onVersion) + } + AiConversationTurn.Status.UNCONVERGED -> { + Text("It did not run, so the widget is unchanged.", style = MaterialTheme.typography.bodyLarge, color = MaterialTheme.colorScheme.error) + turn.error?.let { Text(it, style = MaterialTheme.typography.bodySmall, fontFamily = FontFamily.Monospace, color = MaterialTheme.colorScheme.onSurfaceVariant, maxLines = 3, overflow = TextOverflow.Ellipsis) } + Row { + TextButton(enabled = !running, onClick = { onRetry(turn.id) }) { Text("Try again") } + if (turn.hasFailedSource) TextButton(onClick = { onFailedCode(turn.id) }) { Text("Show its code") } + } + } + AiConversationTurn.Status.FAILED -> { + Text(turn.error ?: "The request could not be completed.", style = MaterialTheme.typography.bodyLarge, color = MaterialTheme.colorScheme.error) + TextButton(enabled = !running, onClick = { onRetry(turn.id) }) { Text("Retry") } + } + AiConversationTurn.Status.CANCELLED -> { + Text("Stopped. The widget is unchanged.", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) + TextButton(enabled = !running, onClick = { onRetry(turn.id) }) { Text("Send again") } + } + AiConversationTurn.Status.INTERRUPTED -> { + Text("The app closed before this finished. The widget is unchanged.", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) + TextButton(enabled = !running, onClick = { onRetry(turn.id) }) { Text("Send again") } + } + } + if (turn.tokens > 0 && turn.status != AiConversationTurn.Status.RUNNING) Text("${turn.tokens} tokens", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + } +} + +/** The request field. It stays above the keyboard. */ +@Composable +private fun Composer(text: String, onText: (String) -> Unit, running: Boolean, enabled: Boolean, placeholder: String, onSend: () -> Unit, onStop: () -> Unit) { + Surface(tonalElevation = 3.dp) { + Row(Modifier.fillMaxWidth().navigationBarsPadding().imePadding().padding(horizontal = 12.dp, vertical = 8.dp), verticalAlignment = Alignment.Bottom) { + OutlinedTextField( + text, onText, enabled = enabled && !running, maxLines = 5, placeholder = { Text(placeholder) }, shape = RoundedCornerShape(24.dp), + keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences), modifier = Modifier.weight(1f), + ) + Spacer(Modifier.width(8.dp)) + if (running) FilledTonalIconButton(onClick = onStop, modifier = Modifier.padding(bottom = 4.dp)) { Icon(Icons.Default.Stop, "Stop") } + else FilledIconButton(enabled = enabled && text.isNotBlank(), onClick = onSend, modifier = Modifier.padding(bottom = 4.dp)) { Icon(Icons.AutoMirrored.Filled.Send, "Send") } + } + } +} diff --git a/Android/app/src/main/java/com/everettjf/scriptwidget/ui/AiScreen.kt b/Android/app/src/main/java/com/everettjf/scriptwidget/ui/AiScreen.kt index 27fbbe3a..603412ad 100644 --- a/Android/app/src/main/java/com/everettjf/scriptwidget/ui/AiScreen.kt +++ b/Android/app/src/main/java/com/everettjf/scriptwidget/ui/AiScreen.kt @@ -93,372 +93,6 @@ import kotlinx.coroutines.withContext import org.json.JSONObject import java.net.URI -private const val PREVIEW_PACKAGE_ID = "ai-preview" -private const val MAXIMUM_PROMPT_CHARACTERS = 4000 - -/** Where the generate screen is in a run. Port of `AIGenerateSession.Phase`. */ -internal sealed class AiPhase { - data object Idle : AiPhase() - class Preparing : AiPhase() - class Thinking(val iteration: Int) : AiPhase() - class Running(val iteration: Int) : AiPhase() - class Fixing(val iteration: Int, val errorSummary: String) : AiPhase() - data object Done : AiPhase() - class Exhausted(val lastError: String?) : AiPhase() - class Failed(val message: String) : AiPhase() - data object Cancelled : AiPhase() -} - -/** One finished iteration; [errorSummary] is null when it rendered. */ -internal class AiIteration(val iteration: Int, val errorSummary: String?) - -/** A package's files with some text files replaced in memory; nothing is written to disk. */ -private class OverlayPackageFiles(private val base: PackageFiles, private val overrides: Map) : PackageFiles { - override fun paths(): List = (base.paths() + overrides.keys).distinct().sorted() - override fun exists(path: String): Boolean = overrides.containsKey(path) || base.exists(path) - override fun readText(path: String): String? = overrides[path] ?: base.readText(path) - override fun readBytes(path: String): ByteArray? = overrides[path]?.toByteArray() ?: base.readBytes(path) -} - -/** - * Text of the enabled Skills for the prompt, or null. Skills only add - * instructions to the request; wire the Skill store in here when it exists. - */ -private fun enabledSkillsPrompt(): String? = com.everettjf.scriptwidget.AppServices.skillsPrompt() - -/** - * Drives the agent loop off the main thread and exposes its progress as - * Compose state. Port of `AIGenerateSession`. - */ -class AiGenerateViewModel : ViewModel() { - private var phaseState by mutableStateOf(AiPhase.Idle) - private val historyState = mutableStateListOf() - private val main = Handler(Looper.getMainLooper()) - private var cancellation: AiCancellation? = null - private var generation = 0 - - internal val phase: AiPhase get() = phaseState - internal val history: List get() = historyState - var usage by mutableStateOf(AiTokenUsage.ZERO); private set - var maxIterations by mutableIntStateOf(AiSettings.DEFAULT_MAX_ITERATIONS); private set - /** Latest candidate source, or null before the model has produced one. */ - var source by mutableStateOf(null); private set - /** Canonical network declaration of the latest candidate. */ - var declaration by mutableStateOf(null); private set - /** Bumped whenever the candidate changes so the preview reruns. */ - var revision by mutableIntStateOf(0); private set - var isRunning by mutableStateOf(false); private set - /** The installed package being improved, resolved when a run starts. */ - private var target: InstalledPackage? = null - - /** Starts a run. [name] is the installed package to improve, or null for a new widget. */ - fun start(context: Context, description: String, family: String, name: String?) { - val application = context.applicationContext - val prompt = description.trim() - if (prompt.isEmpty() || isRunning) return - cancellation?.cancel() - val token = AiCancellation() - cancellation = token - val run = ++generation - historyState.clear() - usage = AiTokenUsage.ZERO - source = null - declaration = null - isRunning = true - phaseState = AiPhase.Preparing() - - viewModelScope.launch(Dispatchers.IO) { - val outcome: AgentLoopOutcome? = try { - val store = AiServices.settings(application) - val settings = store.load() - settings.profile.validationError()?.let { return@launch finish(run, AiPhase.Failed(it)) } - val key = AiServices.credentials(application).get(settings.profile.kind.id) - ?: return@launch finish(run, AiPhase.Failed("API key is not set. Open Settings → AI to configure it.")) - val installed = name?.let { AppServices.library.find(it) ?: return@launch finish(run, AiPhase.Failed("This widget is no longer installed.")) } - target = installed - val mode = if (installed == null) AgentLoopRequest.Mode.Fresh(prompt) else { - val current = installed.files.readText(entryOf(installed)) ?: return@launch finish(run, AiPhase.Failed("The widget's ${entryOf(installed)} could not be read.")) - AgentLoopRequest.Mode.Refine(current, prompt, AiManifestDeclaration.fromManifest(installed.manifest)?.toJson()) - } - val baseline = installed?.let { PlatformSupport.unsupported(it.manifest, it.files).toSet() } ?: emptySet() - val request = AgentLoopRequest(mode, AiWidgetSize.fromFamily(family), PromptBuilder.systemPrompt(AiServices.reference(application)), settings.maxIterations, enabledSkillsPrompt()) - post(run) { maxIterations = AgentLoop.iterationLimit(settings.maxIterations) } - AgentLoop(AiClient().bind(settings, key)).run( - request, - validate = { candidate, candidateDeclaration -> validate(installed, candidate, candidateDeclaration, family, baseline) }, - cancellation = token, - onEvent = { event -> post(run) { apply(event) } }, - ) - } catch (_: Exception) { - // Nothing here may surface exception text: it could describe the request. - null - } - post(run) { apply(outcome) } - } - } - - /** Stops the current run; the in-flight request is aborted. */ - fun cancel() { - generation += 1 - cancellation?.cancel() - cancellation = null - if (isRunning) phaseState = AiPhase.Cancelled - isRunning = false - } - - override fun onCleared() { cancellation?.cancel() } - - private fun finish(run: Int, phase: AiPhase) = post(run) { isRunning = false; phaseState = phase } - - /** Applies a state change on the main thread, unless the run was cancelled or replaced. */ - private fun post(run: Int, change: () -> Unit) { main.post { if (run == generation) change() } } - - private fun apply(event: AgentLoopEvent) { - when (event) { - is AgentLoopEvent.Thinking -> phaseState = AiPhase.Thinking(event.iteration) - is AgentLoopEvent.Produced -> { source = event.source; declaration = event.manifestJson } - is AgentLoopEvent.Running -> phaseState = AiPhase.Running(event.iteration) - is AgentLoopEvent.RanFailed -> { - phaseState = AiPhase.Fixing(event.iteration, event.errorSummary) - historyState += AiIteration(event.iteration, event.errorSummary) - revision += 1 - } - is AgentLoopEvent.RanSucceeded -> { historyState += AiIteration(event.iteration, null); revision += 1 } - is AgentLoopEvent.TokensUsed -> usage = event.usage - } - } - - private fun apply(outcome: AgentLoopOutcome?) { - isRunning = false - cancellation = null - if (outcome != null) usage = outcome.usage - phaseState = when (outcome) { - is AgentLoopOutcome.Succeeded -> { source = outcome.source; declaration = outcome.manifestJson; AiPhase.Done } - is AgentLoopOutcome.Exhausted -> { source = outcome.lastSource ?: source; declaration = outcome.lastManifestJson; AiPhase.Exhausted(outcome.lastError) } - is AgentLoopOutcome.Cancelled -> AiPhase.Cancelled - is AgentLoopOutcome.Failed -> AiPhase.Failed(outcome.message) - null -> AiPhase.Failed("Generation could not be completed.") - } - revision += 1 - } - - /** Compiles and executes a candidate in memory, exactly as the preview will. Runs on the loop's thread. */ - private fun validate(installed: InstalledPackage?, candidate: String, candidateDeclaration: String?, family: String, baseline: Set): ValidationResult { - if (candidate.toByteArray().size > PackageLimits.MAX_SCRIPT_BYTES) return ValidationResult.failure("resourceLimit: main.jsx exceeds ${PackageLimits.MAX_SCRIPT_BYTES / 1024} KiB.") - val files = candidateFiles(installed, candidate, candidateDeclaration, "AI Widget") - val runtime = AppServices.runtime - val execution = runtime.run(RunRequest(files, PREVIEW_PACKAGE_ID, family, darkMode = runtime.isDarkMode())).execution - val logs = execution.logs.map { "[${it.level}] ${it.message}" } - val result = ValidationResult.fromExecution(execution.tree, execution.error?.kind, execution.error?.message, logs) - if (!result.ok) return result - val manifest = AiManifests.parse(files.readText(AiManifests.FILE_NAME)) - val unsupported = PlatformSupport.unsupported(manifest, files).filter { it !in baseline } - if (unsupported.isNotEmpty()) return ValidationResult.failure("The widget uses features that are not supported on Android: ${unsupported.joinToString(" ")}", logs) - return result - } - - companion object { - /** Entry file of an installed package: the manifest's `entry`, or `main.jsx` for a legacy package. */ - fun entryOf(installed: InstalledPackage): String = AiManifests.entry(installed.manifest) - - /** - * The manifest text a candidate would be saved with, or null for a legacy package that has - * no `widget.json`. Only `network` and `networkDomains` come from the model's declaration. - * When improving a widget, a reply without a declaration keeps the manifest unchanged. - */ - fun manifestText(installed: InstalledPackage?, declaration: String?, newName: String): String? { - if (installed != null && installed.manifest == null) return null - return AiManifests.text(installed?.manifest, declaration, newName) - } - - /** In-memory package for a candidate: a fresh one, or the installed package with its entry replaced. */ - fun candidateFiles(installed: InstalledPackage?, source: String, declaration: String?, newName: String): PackageFiles { - val manifest = manifestText(installed, declaration, newName) - if (installed == null) return MemoryPackageFiles(mapOf(AiManifests.DEFAULT_ENTRY to source, AiManifests.FILE_NAME to manifest!!)) - val overrides = LinkedHashMap() - overrides[entryOf(installed)] = source - if (manifest != null) overrides[AiManifests.FILE_NAME] = manifest - return OverlayPackageFiles(installed.files, overrides) - } - } -} - -/** - * Generates a new widget from a description ([name] is null) or improves the - * installed package [name], whose current entry source is sent to the model. - */ -@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class) -@Composable -fun AiScreen(navigation: NavController, name: String?) { - val context = LocalContext.current - val model: AiGenerateViewModel = viewModel() - val installed = remember(name) { name?.let { AppServices.library.find(it) } } - if (name != null && installed == null) { Text("This widget is no longer installed.", Modifier.padding(32.dp)); return } - - var prompt by rememberSaveable { mutableStateOf("") } - var family by rememberSaveable { mutableStateOf("medium") } - var showCode by rememberSaveable { mutableStateOf(false) } - var askName by remember { mutableStateOf(false) } - var confirmReplace by remember { mutableStateOf(false) } - var settingsRevision by remember { mutableIntStateOf(0) } - LifecycleEventEffect(Lifecycle.Event.ON_RESUME) { settingsRevision += 1 } - // Provider summary: the profile is not secret; whether a key exists is read off the main thread. - var profile by remember { mutableStateOf(null) } - var hasKey by remember { mutableStateOf(true) } - LaunchedEffect(settingsRevision) { - val loaded = withContext(Dispatchers.IO) { AiServices.settings(context).load().profile.let { it to AiServices.credentials(context).has(it.kind.id) } } - profile = loaded.first; hasKey = loaded.second - } - val dark = isSystemInDarkTheme() - val scope = rememberCoroutineScope() - val phase = model.phase - val candidate = model.source - val ready = profile?.isConfigured == true && hasKey - - Scaffold(topBar = { - TopAppBar( - title = { Text(if (installed == null) "Generate with AI" else "Improve ${installed.displayName}", maxLines = 1) }, - navigationIcon = { IconButton(onClick = { model.cancel(); navigation.popBackStack() }) { Icon(Icons.AutoMirrored.Filled.ArrowBack, "Back") } }, - ) - }) { padding -> - Column(Modifier.padding(padding).fillMaxSize().verticalScroll(rememberScrollState()).padding(16.dp), verticalArrangement = Arrangement.spacedBy(12.dp), horizontalAlignment = Alignment.CenterHorizontally) { - if (!ready && profile != null) Card(colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.tertiaryContainer), modifier = Modifier.fillMaxWidth()) { - Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) { - Text("Set up an AI provider", style = MaterialTheme.typography.titleSmall) - Text("Choose a provider and enter its API key in Settings → AI. The key is stored encrypted on this device.", style = MaterialTheme.typography.bodySmall) - TextButton(onClick = { navigation.navigate("settings") }) { Text("Open Settings") } - } - } - OutlinedTextField( - prompt, { prompt = it.take(MAXIMUM_PROMPT_CHARACTERS) }, enabled = !model.isRunning, minLines = 3, maxLines = 8, - label = { Text(if (installed == null) "Describe the widget" else "Describe the change") }, modifier = Modifier.fillMaxWidth(), - ) - if (installed == null) FlowRow(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(6.dp)) { - for (example in AiExamplePrompts.all) AssistChip(enabled = !model.isRunning, onClick = { prompt = example.prompt; family = example.size.family }, label = { Text(example.title) }) - } - Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp)) { - for (size in AiWidgetSize.entries) FilterChip(selected = family == size.family, enabled = !model.isRunning, onClick = { family = size.family }, label = { Text(size.displayName) }) - } - profile?.let { active -> - val host = runCatching { URI(active.normalizedBaseUrl).host }.getOrNull().orEmpty() - Text( - "${active.kind.displayName} · ${active.model.ifBlank { "no model" }}. Your description" + (if (installed == null) " is" else " and this widget's ${AiGenerateViewModel.entryOf(installed)} are") + - " sent to ${host.ifEmpty { "the configured server" }}. Each attempt is a separate request, so cost grows with iterations.", - style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.fillMaxWidth(), - ) - } - Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(12.dp)) { - Button(enabled = ready && !model.isRunning && prompt.isNotBlank(), onClick = { model.start(context, prompt, family, name) }, modifier = Modifier.weight(1f)) { - Text(if (candidate == null) (if (installed == null) "Generate" else "Improve") else "Try again") - } - OutlinedButton(enabled = model.isRunning, onClick = { model.cancel() }, modifier = Modifier.weight(1f)) { Text("Cancel") } - } - - AiProgress(phase, model.history, model.usage, model.maxIterations) - - if (candidate != null) { - // The candidate runs from memory with exactly the manifest it would be saved with. - val files = remember(candidate, model.declaration, installed) { AiGenerateViewModel.candidateFiles(installed, candidate, model.declaration, "AI Widget") } - WidgetPreview(files = files, packageID = PREVIEW_PACKAGE_ID, family = family, dark = dark, revision = model.revision) - val domains = remember(model.declaration) { model.declaration?.let { runCatching { AiManifestDeclaration.parse(it).networkDomains }.getOrNull() }.orEmpty() } - if (domains.isNotEmpty()) Text("Network access requested: ${domains.joinToString(", ")}", style = MaterialTheme.typography.bodySmall, modifier = Modifier.fillMaxWidth()) - TextButton(onClick = { showCode = !showCode }) { Text(if (showCode) "Hide code" else "Show code") } - if (showCode) Card(Modifier.fillMaxWidth()) { - Text(candidate, style = MaterialTheme.typography.bodySmall, fontFamily = FontFamily.Monospace, - modifier = Modifier.heightIn(max = 320.dp).verticalScroll(rememberScrollState()).horizontalScroll(rememberScrollState()).padding(12.dp)) - } - val finished = !model.isRunning && (phase is AiPhase.Done || phase is AiPhase.Exhausted || phase is AiPhase.Cancelled || phase is AiPhase.Failed) - if (finished) Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(12.dp)) { - if (installed != null) Button(onClick = { confirmReplace = true }, modifier = Modifier.weight(1f)) { Text("Replace") } - val saveNew: @Composable () -> Unit = { Text("Save as new widget") } - if (installed == null) Button(onClick = { askName = true }, modifier = Modifier.weight(1f), content = { saveNew() }) - else OutlinedButton(onClick = { askName = true }, modifier = Modifier.weight(1f), content = { saveNew() }) - } - } - } - } - - fun saved(result: Result) { - result.onSuccess { saved -> - WidgetScheduler.refreshPackage(context, saved.installation) - navigation.navigate("package/${Uri.encode(saved.name)}") { popUpTo("home") } - }.onFailure { Toast.makeText(context, it.message ?: "Could not save the widget", Toast.LENGTH_LONG).show() } - } - - if (askName && candidate != null) NameDialog("Save as new widget", installed?.let { "${it.name} AI" } ?: "AI Widget", onDismiss = { askName = false }) { newName -> - askName = false - val declaration = model.declaration - scope.launch { saved(withContext(Dispatchers.IO) { runCatching { saveAsNew(installed, newName, candidate, declaration) } }) } - } - if (confirmReplace && candidate != null && installed != null) AlertDialog( - onDismissRequest = { confirmReplace = false }, title = { Text("Replace ${installed.displayName}?") }, - text = { Text("${AiGenerateViewModel.entryOf(installed)} is overwritten with the generated code. Widgets on the home screen showing it are redrawn.") }, - confirmButton = { TextButton(onClick = { - confirmReplace = false - val declaration = model.declaration - scope.launch { saved(withContext(Dispatchers.IO) { runCatching { replace(installed, candidate, declaration) } }) } - }) { Text("Replace") } }, - dismissButton = { TextButton(onClick = { confirmReplace = false }) { Text("Cancel") } }, - ) -} - -/** Creates a new package from a candidate; when improving, the original's other files are copied first. */ -private fun saveAsNew(installed: InstalledPackage?, requestedName: String, source: String, declaration: String?): InstalledPackage { - val library = AppServices.library - if (installed == null) { - val name = library.store.uniqueName(requestedName) - return library.create(name, source, AiGenerateViewModel.manifestText(null, declaration, name)) - } - val duplicate = library.duplicate(installed.name) - return try { - replace(library.rename(duplicate.name, library.store.uniqueName(requestedName)), source, declaration) - } catch (error: Exception) { - // Do not leave a half-made copy behind. - runCatching { library.delete(duplicate.name) } - throw error - } -} - -/** Writes a candidate over an installed package's entry and, when its network declaration changed, its manifest. */ -private fun replace(installed: InstalledPackage, source: String, declaration: String?): InstalledPackage { - val library = AppServices.library - library.writeFile(installed.name, AiGenerateViewModel.entryOf(installed), source.toByteArray(Charsets.UTF_8)) - val manifest = AiGenerateViewModel.manifestText(installed, declaration, installed.name) - if (manifest != null && declaration != null) library.writeFile(installed.name, AiManifests.FILE_NAME, manifest.toByteArray(Charsets.UTF_8)) - return library.find(installed.name) ?: installed -} - -@Composable -private fun AiProgress(phase: AiPhase, history: List, usage: AiTokenUsage, maxIterations: Int) { - if (phase is AiPhase.Idle) return - Card(Modifier.fillMaxWidth()) { - Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) { - val iteration = when (phase) { is AiPhase.Thinking -> phase.iteration; is AiPhase.Running -> phase.iteration; is AiPhase.Fixing -> phase.iteration; else -> 0 } - val status = when (phase) { - is AiPhase.Preparing -> "Preparing the request…" - is AiPhase.Thinking -> "Iteration ${phase.iteration} / $maxIterations: waiting for the model…" - is AiPhase.Running -> "Iteration ${phase.iteration} / $maxIterations: running the code…" - is AiPhase.Fixing -> "Iteration ${phase.iteration} / $maxIterations: the code failed, asking for a fix…" - is AiPhase.Done -> "Done. The widget rendered without errors." - is AiPhase.Exhausted -> "Did not converge, showing the last attempt." - is AiPhase.Failed -> phase.message - is AiPhase.Cancelled -> "Cancelled." - is AiPhase.Idle -> "" - } - Text(status, style = MaterialTheme.typography.titleSmall, color = if (phase is AiPhase.Failed || phase is AiPhase.Exhausted) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.onSurface) - if (iteration > 0) LinearProgressIndicator(progress = { (iteration.toFloat() / maxIterations.coerceAtLeast(1)).coerceIn(0f, 1f) }, modifier = Modifier.fillMaxWidth()) - else if (phase is AiPhase.Preparing) LinearProgressIndicator(Modifier.fillMaxWidth()) - if (phase is AiPhase.Exhausted && phase.lastError != null) Text(phase.lastError.take(600), style = MaterialTheme.typography.bodySmall, fontFamily = FontFamily.Monospace) - for (record in history.takeLast(8)) Text( - if (record.errorSummary == null) "✓ Attempt ${record.iteration} rendered" else "✗ Attempt ${record.iteration}: ${record.errorSummary.take(300)}", - style = MaterialTheme.typography.bodySmall, - color = if (record.errorSummary == null) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurfaceVariant, - ) - if (usage.totalTokens > 0) Text("Tokens used: ${usage.totalTokens} (${usage.promptTokens} in, ${usage.completionTokens} out)", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant) - } - } -} - /** AI provider settings: provider, base URL, model, API key and a connection test. Shown inside the Settings screen. */ @OptIn(ExperimentalLayoutApi::class) @Composable diff --git a/Android/app/src/main/java/com/everettjf/scriptwidget/ui/DetailScreen.kt b/Android/app/src/main/java/com/everettjf/scriptwidget/ui/DetailScreen.kt index a0969e70..24f59ef2 100644 --- a/Android/app/src/main/java/com/everettjf/scriptwidget/ui/DetailScreen.kt +++ b/Android/app/src/main/java/com/everettjf/scriptwidget/ui/DetailScreen.kt @@ -260,7 +260,7 @@ fun DetailScreen(navigation: NavController, name: String) { text = { Text("The package and its saved data are removed from this device. Widgets showing it will ask you to choose another.") }, confirmButton = { TextButton(onClick = { confirmDelete = false - runCatching { AppServices.library.delete(item.name); AppServices.runtime.storage.clear(item.installation) } + runCatching { AppServices.library.delete(item.name); AppServices.runtime.storage.clear(item.installation); com.everettjf.scriptwidget.ai.AiServices.conversations(context).deleteForPackage(item.installation) } // Grants, snapshots and scheduled reminders belong to the installation and go with it. runCatching { com.everettjf.scriptwidget.SystemModules.retireInstallation(context, item.installation) } runCatching { com.everettjf.scriptwidget.actions.SurfaceHooks.onPackageDeleted(context, item.installation) } diff --git a/Android/app/src/main/java/com/everettjf/scriptwidget/ui/HomeScreen.kt b/Android/app/src/main/java/com/everettjf/scriptwidget/ui/HomeScreen.kt index 13663f57..0115bb91 100644 --- a/Android/app/src/main/java/com/everettjf/scriptwidget/ui/HomeScreen.kt +++ b/Android/app/src/main/java/com/everettjf/scriptwidget/ui/HomeScreen.kt @@ -126,7 +126,8 @@ fun HomeScreen(navigation: NavController) { topBar = { TopAppBar(title = { Text("My Widgets") }) }, bottomBar = { MainBar(navigation, MainTab.WIDGETS) }, floatingActionButton = { - ExtendedFloatingActionButton(onClick = { showAdd = true }, icon = { Icon(Icons.Default.Add, null) }, text = { Text("New widget") }) + // Material hides the text of an extended button from screen readers; the icon carries the name. + ExtendedFloatingActionButton(onClick = { showAdd = true }, icon = { Icon(Icons.Default.Add, "New widget") }, text = { Text("New widget") }) }, ) { padding -> if (packages.isEmpty()) { diff --git a/Android/app/src/main/java/com/everettjf/scriptwidget/ui/Preview.kt b/Android/app/src/main/java/com/everettjf/scriptwidget/ui/Preview.kt index 7f552ad0..ecc4ecbd 100644 --- a/Android/app/src/main/java/com/everettjf/scriptwidget/ui/Preview.kt +++ b/Android/app/src/main/java/com/everettjf/scriptwidget/ui/Preview.kt @@ -48,7 +48,7 @@ suspend fun renderPreview(files: PackageFiles, packageID: String, family: String @Composable fun WidgetPreview( files: PackageFiles, packageID: String, family: String, dark: Boolean, modifier: Modifier = Modifier, parameter: String = "", - buildDirectory: File? = null, revision: Int = 0, onResult: (ExecutionResult?) -> Unit = {}, + buildDirectory: File? = null, revision: Int = 0, maxHeight: Float? = null, onResult: (ExecutionResult?) -> Unit = {}, ) { val density = LocalDensity.current.density val state by produceState(initialValue = null, packageID, family, dark, parameter, revision) { @@ -57,7 +57,8 @@ fun WidgetPreview( } val (width, height) = previewSizes[family] ?: previewSizes.getValue("medium") Box( - modifier.widthIn(max = width.dp).fillMaxWidth().aspectRatio(width / height).clip(RoundedCornerShape(22.dp)).background(MaterialTheme.colorScheme.surfaceVariant), + // With a height limit the picture is drawn smaller, never cropped. + modifier.widthIn(max = (maxHeight?.let { minOf(width, it * width / height) } ?: width).dp).fillMaxWidth().aspectRatio(width / height).clip(RoundedCornerShape(22.dp)).background(MaterialTheme.colorScheme.surfaceVariant), contentAlignment = Alignment.Center, ) { val bitmap = state?.bitmap diff --git a/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AgentLoop.kt b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AgentLoop.kt index f4b6a92a..4aefe2c0 100644 --- a/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AgentLoop.kt +++ b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AgentLoop.kt @@ -5,7 +5,7 @@ sealed class AgentLoopOutcome { abstract val usage: AiTokenUsage /** The candidate compiled and rendered. [manifestJson] is its canonical package declaration, if any. */ - class Succeeded(val source: String, val manifestJson: String?, override val usage: AiTokenUsage) : AgentLoopOutcome() + class Succeeded(val source: String, val manifestJson: String?, override val usage: AiTokenUsage, val note: String? = null) : AgentLoopOutcome() /** The iteration cap was reached; the last attempt is returned for manual repair. */ class Exhausted(val lastSource: String?, val lastManifestJson: String?, val lastError: String?, override val usage: AiTokenUsage) : AgentLoopOutcome() @@ -28,6 +28,8 @@ sealed class AgentLoopEvent { class RanSucceeded(val iteration: Int) : AgentLoopEvent() /** Cumulative token usage so far. */ class TokensUsed(val usage: AiTokenUsage) : AgentLoopEvent() + /** In a conversation turn: the model's one-sentence account of the candidate. */ + class Note(val iteration: Int, val text: String) : AgentLoopEvent() } /** What to generate. Port of `AgentLoopRequest`. */ @@ -45,6 +47,8 @@ class AgentLoopRequest( class Fresh(val userDescription: String) : Mode() /** Change an existing widget; [currentDeclaration] is its current network declaration JSON, if any. */ class Refine(val currentCode: String, val instruction: String, val currentDeclaration: String? = null, val runtimeDiagnostic: String? = null) : Mode() + /** One turn of a conversation; Skills are part of [context], not of the request. */ + class Turn(val context: AiTurnContext) : Mode() } } @@ -86,7 +90,13 @@ class AgentLoop(private val chat: AiChat) { ) latest = AiCandidate(mode.currentCode, mode.currentDeclaration) } + is AgentLoopRequest.Mode.Turn -> { + firstUser = AiMessage(AiMessage.Role.USER, PromptBuilder.userPromptTurn(mode.context)) + latest = null + } } + val isTurn = request.mode is AgentLoopRequest.Mode.Turn + var note: String? = null var lastError: String? = null var lastLogs: List = emptyList() @@ -100,7 +110,7 @@ class AgentLoop(private val chat: AiChat) { listOf( system, firstUser, AiMessage(AiMessage.Role.ASSISTANT, previous.asAssistantMessage()), - AiMessage(AiMessage.Role.USER, PromptBuilder.userPromptFix(previous.source, error, lastLogs)), + AiMessage(AiMessage.Role.USER, PromptBuilder.userPromptFix(previous.source, error, lastLogs) + if (isTurn) PromptBuilder.TURN_REPAIR_SUFFIX else ""), ) } else { listOf(system, firstUser) @@ -120,7 +130,12 @@ class AgentLoop(private val chat: AiChat) { onEvent(AgentLoopEvent.TokensUsed(usage)) if (cancellation.isCancelled) return AgentLoopOutcome.Cancelled(usage) - val extracted = AiOutput.extract(result.content) + val extracted = if (isTurn) { + val reply = PromptBuilder.parseTurnReply(result.content) + // A repair may leave the summary out; the first one still describes the turn. + reply.note?.let { note = it; onEvent(AgentLoopEvent.Note(iteration, it)) } + reply.candidate + } else AiOutput.extract(result.content) // Parse the declaration before anything runs: an invalid one is feedback for the model, never a guess. var declarationError: String? = null val declaration = extracted.manifestJson?.let { text -> @@ -145,7 +160,7 @@ class AgentLoop(private val chat: AiChat) { if (validation.ok) { onEvent(AgentLoopEvent.RanSucceeded(iteration)) - return AgentLoopOutcome.Succeeded(candidate.source, candidate.manifestJson, usage) + return AgentLoopOutcome.Succeeded(candidate.source, candidate.manifestJson, usage, note) } val summary = validation.errorSummary ?: "Runtime returned no element." lastError = summary diff --git a/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiConversation.kt b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiConversation.kt new file mode 100644 index 00000000..5520f92a --- /dev/null +++ b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiConversation.kt @@ -0,0 +1,286 @@ +package com.everettjf.scriptwidget.core.ai + +import org.json.JSONArray +import org.json.JSONObject +import java.util.UUID + +/** Bounds of a conversation. Port of `AIConversationLimits`. */ +object AiConversationLimits { + /** One generation and up to three repairs. */ + const val MAX_ATTEMPTS_PER_TURN = 4 + const val MAX_VERSIONS = 30 + const val MAX_CONVERSATIONS = 50 + const val MAX_REQUEST_CHARACTERS = 4_000 + const val MAX_FILE_BYTES = 12 * 1024 * 1024 + /** History sent to the model: the newest turns, shortened. */ + const val HISTORY_TURNS = 12 + const val HISTORY_REQUEST_CHARACTERS = 500 + const val HISTORY_NOTE_CHARACTERS = 200 + /** The version before the current one is sent for reference when it is small. */ + const val PREVIOUS_SOURCE_CHARACTERS = 8_000 + const val TITLE_CHARACTERS = 40 + const val NOTE_CHARACTERS = 300 + const val ERROR_CHARACTERS = 600 +} + +/** One version of the widget's source a turn produced. Port of `AIConversationVersion`. */ +class AiConversationVersion( + val id: String, + /** 1-based, in the order the versions were made; never reused. */ + val number: Int, + val source: String, + val parentId: String?, + val createdAt: Long, + /** Hosts the widget declared it contacts; empty for a widget that stays offline. */ + val networkDomains: List, +) { + /** The canonical declaration JSON of this version, or null when it declares no network use. */ + val declarationJson: String? get() = if (networkDomains.isEmpty()) null else AiManifestDeclaration(listOf("network"), networkDomains).toJson() +} + +/** What became of one request. Port of `AIConversationTurn`. */ +class AiConversationTurn( + val id: String, + /** The user's own words; empty for a manual edit. */ + val request: String, + /** The version the turn started from; null for the first turn of a new widget. */ + val baseVersionId: String?, + var status: Status, + /** The model's one-sentence account of what it did. */ + var note: String? = null, + /** Why the turn did not succeed; safe to show. */ + var errorSummary: String? = null, + /** How many attempts failed to run before the turn ended. */ + var repairs: Int = 0, + /** The version a successful turn produced. */ + var versionId: String? = null, + /** The last attempt of a turn that never ran, for the user to look at. */ + var failedSource: String? = null, + var usage: AiTokenUsage = AiTokenUsage.ZERO, + val createdAt: Long, + /** The user went back to an older version and continued from there. */ + var isAbandoned: Boolean = false, + /** Not a request: the widget's code was changed outside the conversation. */ + val isManualEdit: Boolean = false, +) { + enum class Status(val id: String) { + /** Waiting for the model or running the result. Never read back from disk as such. */ + RUNNING("running"), + SUCCEEDED("succeeded"), + /** Every attempt failed to run; the widget was left as it was. */ + UNCONVERGED("unconverged"), + /** The provider could not be reached or refused the request. */ + FAILED("failed"), + CANCELLED("cancelled"), + /** The app closed while the turn was running. */ + INTERRUPTED("interrupted"); + + companion object { fun fromId(id: String?): Status? = entries.firstOrNull { it.id == id } } + } +} + +/** + * A multi-turn AI conversation about one widget: what the user asked in each + * turn, what came of it, and every version of the source a successful turn + * produced. Port of `AIConversation`. + * + * A conversation never holds a credential and names no provider: the profile + * that is active when a turn is sent is used. + * + * Not thread-safe: change it from one thread. + */ +class AiConversation( + val id: String = UUID.randomUUID().toString(), + var title: String = "", + var size: AiWidgetSize = AiWidgetSize.MEDIUM, + val turns: MutableList = ArrayList(), + val versions: MutableList = ArrayList(), + var currentVersionId: String? = null, + /** Counts versions ever made, so a number is not reused after pruning. */ + var versionCounter: Int = 0, + /** The installation id of the widget the conversation belongs to, which survives a rename; null while it is a draft. */ + var packageId: String? = null, + val createdAt: Long = System.currentTimeMillis(), + var updatedAt: Long = createdAt, +) { + val isDraft: Boolean get() = packageId == null + val currentVersion: AiConversationVersion? get() = version(currentVersionId) + val latestVersion: AiConversationVersion? get() = versions.maxByOrNull { it.number } + val totalUsage: AiTokenUsage get() = turns.fold(AiTokenUsage.ZERO) { sum, turn -> sum + turn.usage } + val isRunning: Boolean get() = turns.any { it.status == AiConversationTurn.Status.RUNNING } + + fun version(id: String?): AiConversationVersion? = id?.let { wanted -> versions.firstOrNull { it.id == wanted } } + + fun turn(id: String): AiConversationTurn? = turns.firstOrNull { it.id == id } + + /** + * Starts a turn from the current version. Turns made after that version + * are marked abandoned: the conversation continues from where the user is. + */ + fun beginTurn(request: String, now: Long = System.currentTimeMillis()): AiConversationTurn { + abandonTurnsOffCurrentPath() + val turn = AiConversationTurn( + id = UUID.randomUUID().toString(), + request = request.take(AiConversationLimits.MAX_REQUEST_CHARACTERS), + baseVersionId = currentVersionId, + status = AiConversationTurn.Status.RUNNING, + createdAt = now, + ) + turns += turn + if (title.isEmpty()) title = titleFrom(request) + updatedAt = now + return turn + } + + /** Records a version and makes it the current one. */ + fun addVersion(source: String, networkDomains: List = emptyList(), now: Long = System.currentTimeMillis()): AiConversationVersion { + versionCounter += 1 + val version = AiConversationVersion(UUID.randomUUID().toString(), versionCounter, source, currentVersionId, now, networkDomains.toList()) + versions += version + currentVersionId = version.id + pruneVersions() + updatedAt = now + return version + } + + /** Shows another version. Returns false when it does not exist. */ + fun selectVersion(id: String, now: Long = System.currentTimeMillis()): Boolean { + if (version(id) == null) return false + currentVersionId = id + updatedAt = now + return true + } + + /** The turns the model is told about: finished, not abandoned, not manual edits, oldest first. */ + fun historyForPrompt(): List = + turns.filter { it.status == AiConversationTurn.Status.SUCCEEDED && !it.isAbandoned && !it.isManualEdit }.takeLast(AiConversationLimits.HISTORY_TURNS) + + /** + * Takes over code that did not come from the AI: what the installed widget + * holds when it differs from every version the conversation knows as current. + * Returns whether a version was added. + */ + fun adoptExternalSource(source: String, networkDomains: List = emptyList(), now: Long = System.currentTimeMillis()): Boolean { + if (currentVersion?.source == source) return false + abandonTurnsOffCurrentPath() + val base = currentVersionId + val version = addVersion(source, networkDomains, now) + turns += AiConversationTurn( + id = UUID.randomUUID().toString(), request = "", baseVersionId = base, status = AiConversationTurn.Status.SUCCEEDED, + versionId = version.id, createdAt = now, isManualEdit = true, + ) + return true + } + + /** A turn left running by a closed app can never finish. */ + fun markRunningTurnsInterrupted() { + for (turn in turns) if (turn.status == AiConversationTurn.Status.RUNNING) turn.status = AiConversationTurn.Status.INTERRUPTED + } + + private fun abandonTurnsOffCurrentPath() { + val current = currentVersionId ?: return + if (version(current) == null) return + for (turn in turns) { + // A turn whose version was pruned keeps the state it had. + val produced = version(turn.versionId) ?: continue + turn.isAbandoned = !(produced.id == current || isAncestor(produced.id, current)) + } + } + + private fun isAncestor(candidate: String, versionId: String): Boolean { + var cursor = version(versionId)?.parentId + var steps = 0 + while (cursor != null && steps <= versions.size) { + if (cursor == candidate) return true + cursor = version(cursor)?.parentId + steps += 1 + } + return false + } + + /** Keeps the newest versions and always the current one. */ + private fun pruneVersions() { + val excess = versions.size - AiConversationLimits.MAX_VERSIONS + if (excess <= 0) return + val removed = versions.filter { it.id != currentVersionId }.sortedBy { it.number }.take(excess).map { it.id }.toSet() + versions.removeAll { it.id in removed } + } + + fun toJson(): JSONObject { + val root = JSONObject() + root.put("formatVersion", FORMAT_VERSION) + root.put("id", id) + root.put("title", title) + root.put("size", size.family) + root.put("currentVersionId", currentVersionId ?: JSONObject.NULL) + root.put("versionCounter", versionCounter) + root.put("packageId", packageId ?: JSONObject.NULL) + root.put("createdAt", createdAt) + root.put("updatedAt", updatedAt) + root.put("versions", JSONArray().also { array -> + for (version in versions) array.put(JSONObject() + .put("id", version.id).put("number", version.number).put("source", version.source) + .put("parentId", version.parentId ?: JSONObject.NULL).put("createdAt", version.createdAt) + .put("networkDomains", JSONArray(version.networkDomains))) + }) + root.put("turns", JSONArray().also { array -> + for (turn in turns) array.put(JSONObject() + .put("id", turn.id).put("request", turn.request).put("baseVersionId", turn.baseVersionId ?: JSONObject.NULL) + .put("status", turn.status.id).put("note", turn.note ?: JSONObject.NULL).put("errorSummary", turn.errorSummary ?: JSONObject.NULL) + .put("repairs", turn.repairs).put("versionId", turn.versionId ?: JSONObject.NULL).put("failedSource", turn.failedSource ?: JSONObject.NULL) + .put("promptTokens", turn.usage.promptTokens).put("completionTokens", turn.usage.completionTokens).put("totalTokens", turn.usage.totalTokens) + .put("createdAt", turn.createdAt).put("isAbandoned", turn.isAbandoned).put("isManualEdit", turn.isManualEdit)) + }) + return root + } + + companion object { + const val FORMAT_VERSION = 1 + + fun titleFrom(request: String): String { + val line = request.lineSequence().firstOrNull { it.isNotBlank() }?.trim().orEmpty() + return if (line.length > AiConversationLimits.TITLE_CHARACTERS) line.take(AiConversationLimits.TITLE_CHARACTERS) + "…" else line + } + + /** + * Reads a saved conversation, failing closed: an unknown format version or a + * malformed field gives null, never a partly read conversation. + */ + fun fromJson(root: JSONObject): AiConversation? = runCatching { + if (root.getInt("formatVersion") != FORMAT_VERSION) return null + val versions = ArrayList() + val versionArray = root.getJSONArray("versions") + for (index in 0 until versionArray.length()) { + val item = versionArray.getJSONObject(index) + val domains = item.optJSONArray("networkDomains")?.let { array -> List(array.length()) { array.getString(it) } } ?: emptyList() + versions += AiConversationVersion(item.getString("id"), item.getInt("number"), item.getString("source"), text(item, "parentId"), item.getLong("createdAt"), domains) + } + val turns = ArrayList() + val turnArray = root.getJSONArray("turns") + for (index in 0 until turnArray.length()) { + val item = turnArray.getJSONObject(index) + turns += AiConversationTurn( + id = item.getString("id"), request = item.getString("request"), baseVersionId = text(item, "baseVersionId"), + status = AiConversationTurn.Status.fromId(item.getString("status")) ?: return null, + note = text(item, "note"), errorSummary = text(item, "errorSummary"), repairs = item.optInt("repairs"), + versionId = text(item, "versionId"), failedSource = text(item, "failedSource"), + usage = AiTokenUsage(item.optInt("promptTokens"), item.optInt("completionTokens"), item.optInt("totalTokens")), + createdAt = item.getLong("createdAt"), isAbandoned = item.optBoolean("isAbandoned"), isManualEdit = item.optBoolean("isManualEdit"), + ) + } + val id = root.getString("id") + if (!SAFE_ID.matches(id)) return null + AiConversation( + id = id, title = root.optString("title"), size = AiWidgetSize.fromFamily(root.optString("size")), + turns = turns, versions = versions, currentVersionId = text(root, "currentVersionId"), + versionCounter = root.getInt("versionCounter"), packageId = text(root, "packageId"), + createdAt = root.getLong("createdAt"), updatedAt = root.getLong("updatedAt"), + ).also { it.markRunningTurnsInterrupted() } + }.getOrNull() + + /** Conversation ids become file names. */ + val SAFE_ID = Regex("^[A-Za-z0-9-]{8,64}$") + + private fun text(item: JSONObject, key: String): String? = if (item.isNull(key)) null else item.getString(key) + } +} diff --git a/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiConversationStore.kt b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiConversationStore.kt new file mode 100644 index 00000000..1deed67b --- /dev/null +++ b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiConversationStore.kt @@ -0,0 +1,82 @@ +package com.everettjf.scriptwidget.core.ai + +import org.json.JSONObject +import java.io.File + +/** What a list of conversations shows, without the sources. */ +class AiConversationSummary(val id: String, val title: String, val packageId: String?, val versionCount: Int, val updatedAt: Long) + +/** + * Conversations as files, one JSON file each, in a directory that belongs to + * the app. Port of `AIConversationStore`. + * + * Files that cannot be read, are too large or are not conversations are + * ignored, never deleted and never partly used. Only the newest + * [AiConversationLimits.MAX_CONVERSATIONS] are kept. + * + * All methods block; call them off the main thread. + */ +class AiConversationStore(private val directory: File) { + + @Synchronized + fun save(conversation: AiConversation): Boolean { + if (!AiConversation.SAFE_ID.matches(conversation.id)) return false + val bytes = conversation.toJson().toString().toByteArray(Charsets.UTF_8) + if (bytes.size > AiConversationLimits.MAX_FILE_BYTES) return false + return runCatching { + directory.mkdirs() + val target = file(conversation.id) + val temporary = File(directory, "${conversation.id}.tmp") + temporary.writeBytes(bytes) + if (!temporary.renameTo(target)) { temporary.delete(); return false } + prune() + true + }.getOrDefault(false) + } + + @Synchronized + fun load(id: String): AiConversation? { + if (!AiConversation.SAFE_ID.matches(id)) return null + return read(file(id)) + } + + @Synchronized + fun delete(id: String) { + if (AiConversation.SAFE_ID.matches(id)) file(id).delete() + } + + /** Newest first. With [draftsOnly], conversations that belong to an installed widget are left out. */ + @Synchronized + fun summaries(draftsOnly: Boolean = false): List = + all().filter { !draftsOnly || it.isDraft }.filter { it.turns.isNotEmpty() }.sortedByDescending { it.updatedAt } + .map { AiConversationSummary(it.id, it.title, it.packageId, it.versions.size, it.updatedAt) } + + /** The newest conversation of an installed widget. */ + @Synchronized + fun latest(packageId: String): AiConversation? = all().filter { it.packageId == packageId }.maxByOrNull { it.updatedAt } + + /** A widget was deleted: its conversations go with it. */ + @Synchronized + fun deleteForPackage(packageId: String) { + for (conversation in all()) if (conversation.packageId == packageId) file(conversation.id).delete() + } + + private fun file(id: String) = File(directory, "$id.json") + + private fun files(): List = directory.listFiles { file -> file.isFile && file.name.endsWith(".json") }?.toList() ?: emptyList() + + private fun all(): List = files().mapNotNull { read(it) } + + private fun read(file: File): AiConversation? { + if (!file.isFile || file.length() > AiConversationLimits.MAX_FILE_BYTES) return null + val conversation = runCatching { AiConversation.fromJson(JSONObject(file.readText(Charsets.UTF_8))) }.getOrNull() ?: return null + // The name is the id: a file copied under another name is not trusted. + return conversation.takeIf { file.name == "${it.id}.json" } + } + + private fun prune() { + val known = files().mapNotNull { file -> read(file)?.let { file to it.updatedAt } } + if (known.size <= AiConversationLimits.MAX_CONVERSATIONS) return + for ((file, _) in known.sortedByDescending { it.second }.drop(AiConversationLimits.MAX_CONVERSATIONS)) file.delete() + } +} diff --git a/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiConversationTurns.kt b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiConversationTurns.kt new file mode 100644 index 00000000..3e516976 --- /dev/null +++ b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiConversationTurns.kt @@ -0,0 +1,87 @@ +package com.everettjf.scriptwidget.core.ai + +/** + * How a conversation changes over one turn. Port of the turn handling of + * `AIConversationSession`, without the threads: the caller starts a turn, + * runs [AgentLoop] with the request from [loopRequest], feeds its events to + * [applyEvent] and its outcome to [applyOutcome], all on the thread that owns + * the conversation. + * + * A turn that does not succeed leaves the versions and the current version + * exactly as they were. + */ +object AiConversationTurns { + + /** + * Starts a turn for [request] and returns it with what to send. The request is blank-checked + * by the caller. A conversation that is already running a turn is not started again. + */ + fun begin(conversation: AiConversation, request: String, systemPrompt: String, skillsText: String?, runtimeDiagnostic: String? = null, now: Long = System.currentTimeMillis()): Started? { + if (conversation.isRunning || request.isBlank()) return null + // The history is read before the new turn is added, and after turns off the current path are marked. + val turn = conversation.beginTurn(request.trim(), now) + val current = conversation.currentVersion + val context = AiTurnContext( + history = conversation.historyForPrompt().map { AiTurnContext.Exchange(it.request, it.note) }, + currentCode = current?.source, + previousCode = conversation.version(current?.parentId)?.source, + request = turn.request, + skillsText = skillsText, + size = conversation.size, + currentDeclaration = current?.declarationJson, + runtimeDiagnostic = runtimeDiagnostic, + ) + return Started(turn, AgentLoopRequest(AgentLoopRequest.Mode.Turn(context), conversation.size, systemPrompt, AiConversationLimits.MAX_ATTEMPTS_PER_TURN)) + } + + class Started(val turn: AiConversationTurn, val loopRequest: AgentLoopRequest) + + /** Sends a turn that did not succeed again: it is taken out and its request starts a new turn. */ + fun retry(conversation: AiConversation, turnId: String, systemPrompt: String, skillsText: String?, now: Long = System.currentTimeMillis()): Started? { + val turn = conversation.turn(turnId) ?: return null + if (conversation.isRunning || turn.isManualEdit || turn.status == AiConversationTurn.Status.SUCCEEDED || turn.status == AiConversationTurn.Status.RUNNING) return null + conversation.turns.remove(turn) + return begin(conversation, turn.request, systemPrompt, skillsText, now = now) + } + + /** Records progress of the running turn [turnId]. */ + fun applyEvent(conversation: AiConversation, turnId: String, event: AgentLoopEvent) { + val turn = conversation.turn(turnId)?.takeIf { it.status == AiConversationTurn.Status.RUNNING } ?: return + when (event) { + is AgentLoopEvent.RanFailed -> { turn.repairs += 1; turn.errorSummary = clipError(event.errorSummary) } + is AgentLoopEvent.TokensUsed -> turn.usage = event.usage + is AgentLoopEvent.Note -> turn.note = event.text + else -> Unit + } + } + + /** + * Ends the running turn [turnId]. A null [outcome] is a failure with no text to show. + * Only a success adds a version and moves the current version. + */ + fun applyOutcome(conversation: AiConversation, turnId: String, outcome: AgentLoopOutcome?, now: Long = System.currentTimeMillis()) { + val turn = conversation.turn(turnId)?.takeIf { it.status == AiConversationTurn.Status.RUNNING } ?: return + if (outcome != null) turn.usage = outcome.usage + when (outcome) { + is AgentLoopOutcome.Succeeded -> { + val domains = outcome.manifestJson?.let { runCatching { AiManifestDeclaration.parse(it).networkDomains }.getOrNull() }.orEmpty() + val version = conversation.addVersion(outcome.source, domains, now) + turn.status = AiConversationTurn.Status.SUCCEEDED + turn.versionId = version.id + turn.note = outcome.note ?: turn.note ?: "Done." + turn.errorSummary = null + } + is AgentLoopOutcome.Exhausted -> { + turn.status = AiConversationTurn.Status.UNCONVERGED + turn.errorSummary = clipError(outcome.lastError ?: "The widget did not run.") + turn.failedSource = outcome.lastSource + } + is AgentLoopOutcome.Cancelled -> { turn.status = AiConversationTurn.Status.CANCELLED; turn.errorSummary = null } + is AgentLoopOutcome.Failed -> { turn.status = AiConversationTurn.Status.FAILED; turn.errorSummary = clipError(outcome.message) } + null -> { turn.status = AiConversationTurn.Status.FAILED; turn.errorSummary = "The request could not be completed." } + } + conversation.updatedAt = now + } + + private fun clipError(text: String): String = PromptBuilder.clip(text, AiConversationLimits.ERROR_CHARACTERS) +} diff --git a/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/PromptBuilder.kt b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/PromptBuilder.kt index f8e89bd6..86191f47 100644 --- a/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/PromptBuilder.kt +++ b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/PromptBuilder.kt @@ -262,8 +262,103 @@ Preserve working behavior that is unrelated to the request. Use only documented Return the FULL updated JSX only (plus the network declaration block only if it uses the network). No explanation. """.trim() } + + /** + * Port of `userPromptTurn`: one turn of a conversation about a widget. The reply form it + * asks for (a SUMMARY line, the code, the declaration) replaces the system prompt's + * "only JSX" rule, so a turn can say what it changed. + */ + fun userPromptTurn(context: AiTurnContext): String { + val parts = ArrayList() + parts += "You are working with the user on one ScriptWidget widget over several turns.\nWidget size: ${context.size.family}\nSize hint: ${context.size.designHint}" + val skills = context.skillsText?.trim().orEmpty().take(MAXIMUM_SKILLS_CHARACTERS) + if (skills.isNotEmpty()) parts += "Apply these ScriptWidget skills in every turn:\n$skills" + if (context.history.isNotEmpty()) { + val lines = context.history.mapIndexed { index, exchange -> + "${index + 1}. User: ${clip(exchange.request, AiConversationLimits.HISTORY_REQUEST_CHARACTERS)}\n You: ${clip(exchange.note ?: "Done.", AiConversationLimits.HISTORY_NOTE_CHARACTERS)}" + } + parts += "Earlier turns, oldest first (each one was applied):\n" + lines.joinToString("\n") + } + val previous = context.previousCode + if (!previous.isNullOrEmpty() && previous.length <= AiConversationLimits.PREVIOUS_SOURCE_CHARACTERS) { + parts += "The version before your last change, for reference only (the user may ask to bring something back):\n\n```jsx\n$previous\n```" + } + val current = context.currentCode + if (!current.isNullOrEmpty()) { + val declaration = if (context.currentDeclaration.isNullOrBlank()) "It declares no network use." else "Its network declaration: ${context.currentDeclaration}" + val diagnostic = if (context.runtimeDiagnostic.isNullOrBlank()) "" else "\n\nThe latest local ScriptWidget runtime diagnostic is:\n${clip(context.runtimeDiagnostic, 2_000)}" + parts += "Current working widget code:\n\n```jsx\n$current\n```\n\n$declaration$diagnostic\n\nThe user now asks:\n${context.request}\n\n" + + "Change only what the request needs and keep everything else as it is." + } else { + parts += "The user asks for a new widget:\n${context.request}" + } + parts += """ +Reply in exactly this form, which replaces the output rule above for this conversation: +SUMMARY: one short sentence saying what you made or changed, in the language the user wrote in +then the FULL widget code in one ```jsx fenced block. +If, and only if, the widget uses `fetch` or `${'$'}http`, end with one more fenced block +that declares every host it contacts: +```json +{"permissions": ["network"], "networkDomains": ["api.example.com"]} +``` +Hosts are exact lowercase names or `*.example.com`. A host that is not declared is +blocked. No other permission can be declared. Nothing else. +""".trim() + return parts.joinToString("\n\n") + } + + /** Added to a repair request inside a conversation, where the reply form still holds. */ + const val TURN_REPAIR_SUFFIX = "\n\nKeep the reply form of this conversation: the full code in one ```jsx block, then the " + + "```json network declaration block only if the widget uses the network." + + /** + * Port of `parseTurnReply`: the model's note, the widget code and the declaration, if any. + * A reply without the SUMMARY line or without a fence is still read as code. The code ends + * with a line break, like a file. + */ + fun parseTurnReply(raw: String): AiTurnReply { + val candidate = AiOutput.extract(raw) + var note: String? = null + val fence = raw.indexOf("```") + val head = if (fence >= 0) raw.substring(0, fence) else raw + var source = candidate.source + for (line in head.lines()) { + val trimmed = line.trim() + if (!trimmed.lowercase().startsWith("summary:")) continue + val value = trimmed.substring("summary:".length).trim() + if (value.isNotEmpty()) note = clip(value, AiConversationLimits.NOTE_CHARACTERS) + // Without a fence the SUMMARY line is part of what was read as code. + if (fence < 0) source = source.replaceFirst(line, "").trim() + break + } + return AiTurnReply(note, AiCandidate(if (source.isEmpty()) "" else source + "\n", candidate.manifestJson)) + } + + fun clip(text: String, limit: Int): String = if (text.length > limit) text.take(limit) + "…" else text +} + +/** What one turn of a conversation tells the model. Port of `AITurnContext`. */ +class AiTurnContext( + /** Earlier applied turns, oldest first. */ + val history: List, + /** The widget as it is now; null when the turn starts a new widget. */ + val currentCode: String?, + /** The version before the current one, if any. */ + val previousCode: String?, + val request: String, + /** Formatted text of the enabled Skills; null for none. */ + val skillsText: String?, + val size: AiWidgetSize, + /** Canonical network declaration of the current widget, if any. */ + val currentDeclaration: String? = null, + val runtimeDiagnostic: String? = null, +) { + class Exchange(val request: String, val note: String?) } +/** A turn reply, split. */ +class AiTurnReply(val note: String?, val candidate: AiCandidate) + /** A starter prompt shown on the generate screen. Port of `AIExamplePrompt`. */ class AiExamplePrompt(val title: String, val size: AiWidgetSize, val prompt: String) diff --git a/Android/core/src/test/java/com/everettjf/scriptwidget/core/ai/AgentLoopTest.kt b/Android/core/src/test/java/com/everettjf/scriptwidget/core/ai/AgentLoopTest.kt index 5f091979..5464963c 100644 --- a/Android/core/src/test/java/com/everettjf/scriptwidget/core/ai/AgentLoopTest.kt +++ b/Android/core/src/test/java/com/everettjf/scriptwidget/core/ai/AgentLoopTest.kt @@ -34,6 +34,7 @@ class AgentLoopTest { is AgentLoopEvent.RanFailed -> "failed${event.iteration}" is AgentLoopEvent.RanSucceeded -> "succeeded${event.iteration}" is AgentLoopEvent.TokensUsed -> "tokens${event.usage.totalTokens}" + is AgentLoopEvent.Note -> "note${event.iteration}" } @Test diff --git a/Android/core/src/test/java/com/everettjf/scriptwidget/core/ai/AiConversationTest.kt b/Android/core/src/test/java/com/everettjf/scriptwidget/core/ai/AiConversationTest.kt new file mode 100644 index 00000000..52b64c83 --- /dev/null +++ b/Android/core/src/test/java/com/everettjf/scriptwidget/core/ai/AiConversationTest.kt @@ -0,0 +1,346 @@ +package com.everettjf.scriptwidget.core.ai + +import org.json.JSONObject +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.File + +/** The conversation model, its files and its turns, with a fake model and a fake runtime. */ +class AiConversationTest { + @get:Rule val folder = TemporaryFolder() + + private class FakeChat(private val replies: List) : AiChat { + val conversations = ArrayList>() + override fun chat(messages: List, cancellation: AiCancellation): AiChatResult { + conversations += messages + return when (val reply = replies[minOf(conversations.size, replies.size) - 1]) { + is Exception -> throw reply + else -> AiChatResult(reply as String, AiTokenUsage(10, 5, 15)) + } + } + } + + private fun reply(note: String, code: String, declaration: String? = null) = + "SUMMARY: $note\n```jsx\n$code\n```" + (declaration?.let { "\n```json\n$it\n```" } ?: "") + + /** Runs one whole turn the way the app does, on this thread. */ + private fun turn(conversation: AiConversation, request: String, chat: AiChat, validate: (String, String?) -> ValidationResult = { _, _ -> ValidationResult.success() }): AiConversationTurn { + val started = AiConversationTurns.begin(conversation, request, "SYSTEM", null)!! + val outcome = AgentLoop(chat).run(started.loopRequest, validate, onEvent = { AiConversationTurns.applyEvent(conversation, started.turn.id, it) }) + AiConversationTurns.applyOutcome(conversation, started.turn.id, outcome) + return started.turn + } + + @Test + fun aTurnThatRunsBecomesAVersionWithItsNote() { + val conversation = AiConversation() + val chat = FakeChat(listOf(reply("Made a clock.", "\$render()"))) + val turn = turn(conversation, "a clock\nwith seconds", chat) + assertEquals(AiConversationTurn.Status.SUCCEEDED, turn.status) + assertEquals("Made a clock.", turn.note) + assertEquals(0, turn.repairs) + assertEquals(15, turn.usage.totalTokens) + assertEquals("a clock", conversation.title) + val version = conversation.currentVersion!! + assertEquals(1, version.number) + assertEquals("\$render()\n", version.source) + assertEquals(turn.versionId, version.id) + assertNull(version.declarationJson) + val user = chat.conversations.single().last().content + assertTrue(user.contains("The user asks for a new widget:\na clock\nwith seconds")) + assertTrue(user.contains("SUMMARY:")) + } + + @Test + fun theSecondTurnSendsTheCurrentCodeAndTheHistory() { + val conversation = AiConversation() + turn(conversation, "a clock", FakeChat(listOf(reply("Made a clock.", "ONE")))) + turn(conversation, "bigger", FakeChat(listOf(reply("Bigger.", "TWO")))) + val chat = FakeChat(listOf(reply("Red.", "THREE"))) + turn(conversation, "make it red", chat) + val user = chat.conversations.single().last().content + assertTrue(user.contains("1. User: a clock\n You: Made a clock.")) + assertTrue(user.contains("2. User: bigger\n You: Bigger.")) + assertTrue(user.contains("Current working widget code:\n\n```jsx\nTWO\n")) + assertTrue("the version before is sent for reference", user.contains("```jsx\nONE\n")) + assertTrue(user.contains("It declares no network use.")) + assertTrue(user.contains("The user now asks:\nmake it red")) + assertEquals(listOf(1, 2, 3), conversation.versions.map { it.number }) + assertEquals(conversation.versions[1].id, conversation.versions[2].parentId) + } + + @Test + fun aRepairedTurnCountsItsRepairsAndKeepsTheFirstNote() { + val conversation = AiConversation() + val chat = FakeChat(listOf(reply("Made a list.", "BROKEN"), "```jsx\nFIXED\n```")) + val turn = turn(conversation, "a list", chat) { source, _ -> if (source.contains("BROKEN")) ValidationResult.failure("scriptError: boom") else ValidationResult.success() } + assertEquals(AiConversationTurn.Status.SUCCEEDED, turn.status) + assertEquals(1, turn.repairs) + assertEquals("Made a list.", turn.note) + assertNull(turn.errorSummary) + assertEquals("FIXED\n", conversation.currentVersion!!.source) + assertEquals(1, conversation.versions.size) + assertTrue(chat.conversations[1].last().content.contains("Keep the reply form of this conversation")) + } + + @Test + fun aTurnThatNeverRunsLeavesTheWidgetAsItWas() { + val conversation = AiConversation() + turn(conversation, "a clock", FakeChat(listOf(reply("Made a clock.", "GOOD")))) + val before = conversation.currentVersionId + val chat = FakeChat(listOf(reply("Tried.", "BAD"))) + val turn = turn(conversation, "add a chart", chat) { _, _ -> ValidationResult.failure("scriptError: no chart") } + assertEquals(AiConversationTurn.Status.UNCONVERGED, turn.status) + assertEquals(AiConversationLimits.MAX_ATTEMPTS_PER_TURN, chat.conversations.size) + assertEquals(AiConversationLimits.MAX_ATTEMPTS_PER_TURN, turn.repairs) + assertEquals("scriptError: no chart", turn.errorSummary) + assertEquals("BAD\n", turn.failedSource) + assertNull(turn.versionId) + assertEquals(before, conversation.currentVersionId) + assertEquals(1, conversation.versions.size) + assertFalse("a failed turn is not history", conversation.historyForPrompt().any { it.id == turn.id }) + } + + @Test + fun aProviderFailureIsShownAndCanBeRetried() { + val conversation = AiConversation() + val failed = turn(conversation, "a clock", FakeChat(listOf(AiClientException(AiErrorKind.HTTP, "The provider refused the request (401).")))) + assertEquals(AiConversationTurn.Status.FAILED, failed.status) + assertEquals("The provider refused the request (401).", failed.errorSummary) + assertTrue(conversation.versions.isEmpty()) + + val started = AiConversationTurns.retry(conversation, failed.id, "SYSTEM", null)!! + assertEquals("a clock", started.turn.request) + assertEquals(listOf(started.turn.id), conversation.turns.map { it.id }) + val outcome = AgentLoop(FakeChat(listOf(reply("Made a clock.", "OK")))).run(started.loopRequest, { _, _ -> ValidationResult.success() }) + AiConversationTurns.applyOutcome(conversation, started.turn.id, outcome) + assertEquals(AiConversationTurn.Status.SUCCEEDED, started.turn.status) + assertNull("a turn that ran can not be sent again", AiConversationTurns.retry(conversation, started.turn.id, "SYSTEM", null)) + } + + @Test + fun anUnknownFailureShowsNoExceptionText() { + val conversation = AiConversation() + val failed = turn(conversation, "a clock", FakeChat(listOf(IllegalStateException("Authorization: Bearer sk-secret")))) + assertEquals(AiConversationTurn.Status.FAILED, failed.status) + assertFalse(conversation.toJson().toString().contains("sk-secret")) + } + + @Test + fun aCancelledTurnChangesNothing() { + val conversation = AiConversation() + val started = AiConversationTurns.begin(conversation, "a clock", "SYSTEM", null)!! + assertNull("one turn at a time", AiConversationTurns.begin(conversation, "another", "SYSTEM", null)) + val cancellation = AiCancellation().also { it.cancel() } + val outcome = AgentLoop(FakeChat(listOf("unused"))).run(started.loopRequest, { _, _ -> ValidationResult.success() }, cancellation) + AiConversationTurns.applyOutcome(conversation, started.turn.id, outcome) + assertEquals(AiConversationTurn.Status.CANCELLED, started.turn.status) + assertTrue(conversation.versions.isEmpty()) + assertFalse(conversation.isRunning) + } + + @Test + fun theDeclarationOfATurnIsKeptWithItsVersionAndSentBack() { + val conversation = AiConversation() + val validated = ArrayList() + turn(conversation, "bitcoin price", FakeChat(listOf(reply("Shows the price.", "FETCH", """{"permissions":["network"],"networkDomains":["API.Example.com"]}""")))) { _, manifest -> validated += manifest; ValidationResult.success() } + assertEquals(listOf("api.example.com"), conversation.currentVersion!!.networkDomains) + assertEquals("""{"permissions":["network"],"networkDomains":["api.example.com"]}""", validated.single()) + val chat = FakeChat(listOf(reply("Offline now.", "STATIC"))) + turn(conversation, "use fixed data", chat) + assertTrue(chat.conversations.single().last().content.contains("""Its network declaration: {"permissions":["network"],"networkDomains":["api.example.com"]}""")) + assertTrue("a version without a declaration has no network", conversation.currentVersion!!.networkDomains.isEmpty()) + } + + @Test + fun anInvalidDeclarationIsNotRunAndIsRepaired() { + val conversation = AiConversation() + val ran = ArrayList() + val chat = FakeChat(listOf( + reply("Reads the calendar.", "CAL", """{"permissions":["calendar"],"networkDomains":[]}"""), + reply("Plain.", "PLAIN"), + )) + val turn = turn(conversation, "my calendar", chat) { source, _ -> ran += source; ValidationResult.success() } + assertEquals(listOf("PLAIN\n"), ran) + assertEquals(1, turn.repairs) + assertTrue(chat.conversations[1].last().content.contains("Invalid package declaration")) + } + + @Test + fun goingBackAndAskingAgainLeavesTheLaterTurnsBehind() { + val conversation = AiConversation() + val first = turn(conversation, "a clock", FakeChat(listOf(reply("Clock.", "ONE")))) + val second = turn(conversation, "blue", FakeChat(listOf(reply("Blue.", "TWO")))) + assertTrue(conversation.selectVersion(first.versionId!!)) + val chat = FakeChat(listOf(reply("Green.", "THREE"))) + val third = turn(conversation, "green", chat) + assertTrue(second.isAbandoned) + assertFalse(first.isAbandoned) + assertFalse(third.isAbandoned) + val user = chat.conversations.single().last().content + assertFalse("a turn left behind is not sent", user.contains("blue")) + assertTrue(user.contains("```jsx\nONE\n")) + assertEquals(first.versionId, conversation.currentVersion!!.parentId) + assertEquals(3, conversation.currentVersion!!.number) + assertFalse(conversation.selectVersion("missing")) + } + + @Test + fun codeChangedOutsideTheConversationBecomesAVersionThatIsNotHistory() { + val conversation = AiConversation() + turn(conversation, "a clock", FakeChat(listOf(reply("Clock.", "ONE")))) + assertFalse("the same code adds nothing", conversation.adoptExternalSource("ONE\n")) + assertTrue(conversation.adoptExternalSource("EDITED", listOf("a.example.com"))) + val manual = conversation.turns.last() + assertTrue(manual.isManualEdit) + assertEquals(2, conversation.currentVersion!!.number) + assertEquals(listOf("a.example.com"), conversation.currentVersion!!.networkDomains) + val chat = FakeChat(listOf(reply("Red.", "THREE"))) + turn(conversation, "red", chat) + val user = chat.conversations.single().last().content + assertTrue(user.contains("```jsx\nEDITED\n")) + assertTrue(user.contains("1. User: a clock")) + assertFalse(user.contains("2. User:")) + assertNull(AiConversationTurns.retry(conversation, manual.id, "SYSTEM", null)) + } + + @Test + fun onlyTheNewestVersionsAreKeptAndNumbersAreNotReused() { + val conversation = AiConversation() + repeat(AiConversationLimits.MAX_VERSIONS + 5) { conversation.addVersion("v$it") } + assertEquals(AiConversationLimits.MAX_VERSIONS, conversation.versions.size) + assertEquals(AiConversationLimits.MAX_VERSIONS + 5, conversation.currentVersion!!.number) + assertEquals(6, conversation.versions.minOf { it.number }) + conversation.selectVersion(conversation.versions.first().id) + conversation.addVersion("another") + assertEquals(AiConversationLimits.MAX_VERSIONS + 6, conversation.currentVersion!!.number) + } + + @Test + fun aConversationSurvivesItsFile() { + val conversation = AiConversation(size = AiWidgetSize.LARGE) + turn(conversation, "bitcoin", FakeChat(listOf(reply("Price.", "FETCH", """{"permissions":["network"],"networkDomains":["api.example.com"]}""")))) + turn(conversation, "chart", FakeChat(listOf(reply("Tried.", "BAD")))) { _, _ -> ValidationResult.failure("scriptError: x") } + conversation.packageId = "Bitcoin" + val read = AiConversation.fromJson(JSONObject(conversation.toJson().toString()))!! + assertEquals(conversation.toJson().toString(), read.toJson().toString()) + assertEquals(AiWidgetSize.LARGE, read.size) + assertEquals("Bitcoin", read.packageId) + assertEquals(listOf("api.example.com"), read.currentVersion!!.networkDomains) + assertEquals(AiConversationTurn.Status.UNCONVERGED, read.turns[1].status) + assertEquals("BAD\n", read.turns[1].failedSource) + } + + @Test + fun aRunningTurnIsReadBackAsInterrupted() { + val conversation = AiConversation() + AiConversationTurns.begin(conversation, "a clock", "SYSTEM", null)!! + val read = AiConversation.fromJson(JSONObject(conversation.toJson().toString()))!! + assertEquals(AiConversationTurn.Status.INTERRUPTED, read.turns.single().status) + assertFalse(read.isRunning) + assertNotNull(AiConversationTurns.retry(read, read.turns.single().id, "SYSTEM", null)) + } + + @Test + fun malformedConversationsFailClosed() { + val good = AiConversation().also { it.addVersion("x") }.toJson() + assertNull(AiConversation.fromJson(JSONObject(good.toString()).put("formatVersion", 2))) + assertNull(AiConversation.fromJson(JSONObject(good.toString()).put("id", "../../escape"))) + assertNull(AiConversation.fromJson(JSONObject(good.toString()).put("versions", "none"))) + assertNull(AiConversation.fromJson(JSONObject("{}"))) + assertNotNull(AiConversation.fromJson(good)) + } + + @Test + fun theStoreKeepsConversationsAndIgnoresWhatIsNotOne() { + val directory = File(folder.root, "ai-conversations") + val store = AiConversationStore(directory) + assertTrue(store.summaries().isEmpty()) + val draft = AiConversation(updatedAt = 10).also { turn(it, "a clock", FakeChat(listOf(reply("Clock.", "ONE")))); it.updatedAt = 10 } + val bound = AiConversation().also { turn(it, "weather", FakeChat(listOf(reply("Weather.", "W")))); it.packageId = "Weather"; it.updatedAt = 20 } + assertTrue(store.save(draft)) + assertTrue(store.save(bound)) + File(directory, "notes.json").writeText("{\"hello\": 1}") + File(directory, "broken.json").writeText("{") + File(directory, "copied-under-another-name.json").writeText(draft.toJson().toString()) + + assertEquals(listOf(bound.id, draft.id), store.summaries().map { it.id }) + assertEquals(listOf(draft.id), store.summaries(draftsOnly = true).map { it.id }) + assertEquals("a clock", store.load(draft.id)!!.title) + assertEquals(bound.id, store.latest("Weather")!!.id) + assertNull(store.latest("Missing")) + assertNull(store.load("../notes")) + assertTrue("foreign files are left alone", File(directory, "notes.json").isFile && File(directory, "broken.json").isFile) + + store.delete(draft.id) + assertNull(store.load(draft.id)) + store.deleteForPackage("Weather") + assertNull(store.latest("Weather")) + assertFalse(directory.list()!!.any { it.endsWith(".tmp") }) + } + + @Test + fun theStoreKeepsOnlyTheNewestConversations() { + val store = AiConversationStore(File(folder.root, "many")) + val ids = (1..AiConversationLimits.MAX_CONVERSATIONS + 3).map { index -> + AiConversation().also { it.beginTurn("request $index"); it.markRunningTurnsInterrupted(); it.updatedAt = index.toLong(); store.save(it) }.id + } + val kept = store.summaries().map { it.id } + assertEquals(AiConversationLimits.MAX_CONVERSATIONS, kept.size) + assertFalse(ids.take(3).any { it in kept }) + assertEquals(ids.last(), kept.first()) + } + + @Test + fun aConversationFileHoldsNoCredentialOrProvider() { + val conversation = AiConversation() + turn(conversation, "a clock", FakeChat(listOf(reply("Clock.", "ONE")))) + val keys = HashSet() + fun collect(value: Any?) { + when (value) { + is JSONObject -> for (key in value.keys()) { keys += key; collect(value.opt(key)) } + is org.json.JSONArray -> for (index in 0 until value.length()) collect(value.opt(index)) + } + } + collect(conversation.toJson()) + assertTrue(keys.none { key -> listOf("key", "token", "secret", "authorization", "baseurl", "model", "provider").any { key.lowercase().contains(it) && key !in setOf("promptTokens", "completionTokens", "totalTokens") } }) + } + + @Test + fun turnRepliesAreReadLeniently() { + val full = PromptBuilder.parseTurnReply("SUMMARY: Made it blue.\n```jsx\n\$render() // end\n```\n```json\n{\"permissions\":[\"network\"],\"networkDomains\":[\"a.example.com\"]}\n```") + assertEquals("Made it blue.", full.note) + assertEquals("\$render() // end\n", full.candidate.source) + assertTrue(full.candidate.manifestJson!!.contains("a.example.com")) + + val bare = PromptBuilder.parseTurnReply("\$render()") + assertNull(bare.note) + assertEquals("\$render()\n", bare.candidate.source) + + val unfenced = PromptBuilder.parseTurnReply("Summary: plain\n\$render()") + assertEquals("plain", unfenced.note) + assertEquals("\$render()\n", unfenced.candidate.source) + + assertEquals("", PromptBuilder.parseTurnReply("SUMMARY: nothing").candidate.source) + assertEquals(AiConversationLimits.NOTE_CHARACTERS + 1, PromptBuilder.parseTurnReply("SUMMARY: " + "x".repeat(900) + "\n```jsx\nA\n```").note!!.length) + } + + @Test + fun skillsAndDiagnosticsArePartOfATurn() { + val conversation = AiConversation() + conversation.addVersion("ONE") + val started = AiConversationTurns.begin(conversation, "fix it", "SYSTEM", "Skill: use rounded corners", "scriptError: line 3")!! + val mode = started.loopRequest.mode as AgentLoopRequest.Mode.Turn + val text = PromptBuilder.userPromptTurn(mode.context) + assertTrue(text.contains("Apply these ScriptWidget skills in every turn:\nSkill: use rounded corners")) + assertTrue(text.contains("The latest local ScriptWidget runtime diagnostic is:\nscriptError: line 3")) + assertEquals(AiConversationLimits.MAX_ATTEMPTS_PER_TURN, started.loopRequest.maxIterations) + assertNull(AiConversationTurns.begin(AiConversation(), " ", "SYSTEM", null)) + } +} diff --git a/Android/scripts/device-test.sh b/Android/scripts/device-test.sh index 65557abd..3d99cb09 100755 --- a/Android/scripts/device-test.sh +++ b/Android/scripts/device-test.sh @@ -3,20 +3,23 @@ # uninstalling the test APK, so files the tests save can be pulled afterwards. # scripts/device-test.sh core [ClassName[#method]] -> prints the result # The app is tested as its `play` flavor; set FLAVOR=foss for the build without Firebase. +# SUFFIX=.dev tests a copy installed beside the Play Store app, with its own data. set -e cd "$(dirname "$0")/.." module="${1:-core}" package="com.everettjf.scriptwidget.$module.test" -[ "$module" = "app" ] && package="com.atomsprout.scriptwidget.test" +suffix="${SUFFIX:-}" +application="com.atomsprout.scriptwidget$suffix" +[ "$module" = "app" ] && package="$application.test" flavor="${FLAVOR:-play}" variant="$(printf %s "$flavor" | cut -c1 | tr a-z A-Z)$(printf %s "$flavor" | cut -c2-)Debug" if [ "$module" = "app" ]; then - ./gradlew -q ":app:install${variant}AndroidTest" ":app:install${variant}" >/dev/null + ./gradlew -q ${suffix:+-PdevSuffix=$suffix} ":app:install${variant}AndroidTest" ":app:install${variant}" >/dev/null else ./gradlew -q ":$module:installDebugAndroidTest" >/dev/null fi # Lets the app's tests bind widgets through their own AppWidgetHost. -[ "$module" = "app" ] && adb shell appwidget grantbind --package com.atomsprout.scriptwidget --user 0 +[ "$module" = "app" ] && adb shell appwidget grantbind --package "$application" --user 0 class="" [ -n "$2" ] && class="-e class com.everettjf.scriptwidget.$( [ "$module" = "app" ] && echo "" || echo "$module." )$2" adb shell am instrument -w $class "$package/androidx.test.runner.AndroidJUnitRunner" | grep -v "^$" | tail -40 diff --git a/docs/ai-conversation.md b/docs/ai-conversation.md index d9fd027c..9422e679 100644 --- a/docs/ai-conversation.md +++ b/docs/ai-conversation.md @@ -6,7 +6,7 @@ The AI window of the Mac app is a conversation about one widget: describe it, lo ![The running app after a session with a local stand-in provider: a repaired turn, a turn left behind, and a change that did not run](images/ai-conversation-mac-live.png) -iPhone and iPad still have the earlier single request with a Refine field; the conversation is planned for them and will use the same shared code. +The Android app has the same conversation on one screen; see [On Android](#on-android). iPhone and iPad still have the earlier single request with a Refine field; the conversation is planned for them and will use the same shared code. ## Using it @@ -50,6 +50,19 @@ The **Copilot** tab of the editor is the conversation about the open widget. A w - **Fix Runtime Error** sends the error the editor's preview last reported. - The conversation changes the widget's main file. With another file selected, the panel asks you to select the main file. +## On Android + +![The conversation on a phone: the preview and its versions on top, the turns below](images/ai-conversation-android.png) + +**Generate with AI** under New widget, and **Improve with AI** on a widget's page, open the conversation. It follows the rules on this page with these differences: + +- The preview is on top and the turns are below it. The reply appears when it is complete; code is not written out as it arrives. +- An installed widget is changed with **Apply to widget**, which asks first and names any host the version adds. +- There is no editor panel. Code changed in the editor is taken over as a version the next time the conversation opens. +- Conversations stay in the app's no-backup directory on the device. + +More in the [Android notes](android/README.md#ai-conversation). + ## When something goes wrong | What happened | What you see | diff --git a/docs/android/README.md b/docs/android/README.md index 69e9b7a9..14b42acd 100644 --- a/docs/android/README.md +++ b/docs/android/README.md @@ -37,12 +37,18 @@ Run this after changing `Web/preview-engine/src`, the support bundle or `Android cd Android && ./gradlew :core:testDebugUnitTest :app:assembleDebug ``` -Unit tests cover package and archive hardening, Gallery, Skills, plugins and the AI client. They run on the JVM. +Unit tests cover package and archive hardening, Gallery, Skills, plugins, the AI client and the AI conversation. They run on the JVM. ```bash Android/scripts/device-test.sh core ``` +A debug build has the Play package name, so a phone that has the app from Google Play refuses it (the signatures differ). `-PdevSuffix=.dev` builds a copy that installs beside it with its own data, and `SUFFIX=.dev Android/scripts/device-test.sh app` tests that copy: + +```bash +cd Android && ./gradlew -PdevSuffix=.dev :app:installPlayDebug +``` + Device tests need a connected device or emulator. They cover the engine, parity with JavaScriptCore on the bundled corpus (`Web/preview-engine/test/parity/native-trees.json`), rendering of every corpus tree, native APIs (including a live HTTPS request) and the timeline planner. Pass a class name to run one suite, for example `device-test.sh core ParityTest`. Requirements: JDK 21, Android SDK 37, NDK 28.1, Node 24. Create `Android/local.properties` with `sdk.dir=`. @@ -169,6 +175,18 @@ adb shell "am broadcast -a com.atomsprout.scriptwidget.automation.RUN_ACTION -p The three bundled examples are under Templates → Android only: Scrolling Checklist, Wallpaper Colors and Automation Counter. Their source is in `Android/app/src/main/assets/Script.android`. +### AI conversation + +**Generate with AI** and **Improve with AI** open a conversation about one widget, as the Mac app's AI window does: describe it, look at the preview, ask for a change, look again. The rules are in [AI conversation](../ai-conversation.md); this is what is particular to Android. + +- One screen: the preview on top with its size and its versions, the turns below, the request field above the keyboard. +- The reply is shown when it is complete. Code is not written out as it arrives. +- **Save widget** makes a new widget from the version on screen. For an installed widget the AI works on a copy in memory, and **Apply to widget** writes the version on screen into it. A version that contacts a host the widget may not reach yet is applied only after you agree, and the network permission for that host is added then. A conversation never removes a host a widget already has. +- A conversation belongs to a widget by its installation, so it follows a rename and is deleted with the widget. Code changed elsewhere (the editor, a Gallery update) is taken over as a version the next time the conversation opens. +- Conversations that are not saved as a widget are drafts, offered under **Continue a draft**. All conversations are files in the app's no-backup directory: they stay on the device and are not part of a device backup. They hold requests, notes and widget sources, never a key or a provider. +- Enabled Skills are read when a request is sent. +- The Mac app's editor panel, the three sizes side by side and the list of providers are not on Android: there is one provider profile at a time, chosen in Settings → AI. + ### Synced documents and iPhone `$icloud` on Android keeps the iOS contract: immutable revisions, both heads preserved on concurrent writes, explicit resolve, and no claim that an upload finished. It cannot exchange data with an iPhone. iOS reads only its iCloud container, which no Android documents provider exposes, and the two platforms encode fractional numbers and some key orders differently, so iOS would reject Android-written revisions. Exchange between two Android devices is covered by unit tests on a plain directory; it also passed on the Pixel through a folder picked with the system picker, but not through a remote cloud provider. diff --git a/docs/android/TODO.md b/docs/android/TODO.md index e8e48752..a304dabe 100644 --- a/docs/android/TODO.md +++ b/docs/android/TODO.md @@ -11,7 +11,7 @@ What is left before 1.0.0 (version code 1) is public on Google Play. On 15 Novem | A real registration endpoint receiving the device token | A push server reachable over HTTPS and a test package that declares it | | `$icloud` sync through a remote cloud provider | A cloud account on two Android devices and a shared folder picked in the app | | Other vendors' phones (Samsung, Xiaomi and others): launcher sizes, background limits, alarms | The devices, with USB debugging on. Run `Android/scripts/device-test.sh core` and `app` | -| AI generation from the app's own screen with a live model, and OpenAI-compatible or local providers | A key typed into Settings → AI by its owner. The loop itself passed against Anthropic in `AiLiveModelTest` | +| The AI conversation with a live model, and OpenAI-compatible or local providers | A key typed into Settings → AI by its owner. The screen passed on the Pixel 6 with a scripted model (`AiConversationUiTest`); the loop itself passed against Anthropic in `AiLiveModelTest` | | Wallpaper colors after the wallpaper changes | Change the wallpaper or the color theme on a phone with a `material.` widget placed, and check that the widget follows. The colors themselves, in light and dark, were checked on the Pixel 6 | | The automation entry point from Tasker or MacroDroid | One of those apps installed, with a Send Intent task. It was exercised with `adb shell am broadcast` and from a device test only | | Scrolling lists on other launchers and on Android 15 and 16 | The emulators and other vendors' phones. Lists were checked on the Pixel 6 (Android 17, Pixel Launcher) only | @@ -44,6 +44,7 @@ What is left before 1.0.0 (version code 1) is public on Google Play. On 15 Novem Scrolling lists, wallpaper colors and the automation entry point are done; see [Only on Android](README.md#only-on-android). +- The AI conversation does not write code out as it arrives, shows one size at a time and has no place in the editor yet; the Mac app has all three. - AI generation and the Studio completions do not know `` and the `material.` colors yet; the reference they use is the iOS one. - A way for a script to hand data to an action (a row's own identifier), so a list row can do more than toggle a stored value. - Sending broadcasts from a script to automation apps, after a separate security review. diff --git a/docs/android/testing.md b/docs/android/testing.md index 5dc76b02..c59bd455 100644 --- a/docs/android/testing.md +++ b/docs/android/testing.md @@ -6,7 +6,7 @@ What was run before handing this build to testers, and what was not. | Suite | Count | Where | Command | | --- | --- | --- | --- | -| Unit tests | 474 | JVM | `cd Android && ./gradlew :core:testDebugUnitTest` | +| Unit tests | 502 | JVM | `cd Android && ./gradlew :core:testDebugUnitTest` | | Core device tests | 27 | see below | `Android/scripts/device-test.sh core` | | App device tests | 56 | see below | `Android/scripts/device-test.sh app` | @@ -24,6 +24,8 @@ The version was then renumbered to 1.0.0 (version code 1) with no code change: t The app was then split into the `play` and `foss` flavors (14 November 2026; see [Android distribution](distribution.md)), moving the Firebase code out of the shared source. On the Pixel 6: the 482 JVM tests passed; the app suite passed as `foss` (63 tests; it has no push delivery test) and as `play` (64 tests), with the live-model, push-wait and picked-folder tests skipped as before. The core suite was not re-run, since `core` did not change. The signed `foss` release APK (28.5 MB, no Firebase or Google Play services classes, not debuggable) was installed in place of the debug build: it started, listed the starter widgets, drew the three Android-only template previews, and logged no crash. Widgets were not placed on the home screen from this APK. +The AI conversation (19 November 2026) was run on the Pixel 6 only, as a debug copy installed beside the Play Store app (`-PdevSuffix=.dev`): the 502 JVM tests passed, both flavors built, and the app suite passed (66 tests; the live-model, push-wait and picked-folder tests skipped). Two of them are new: `AiConversationUiTest` drives the real screen with the real runtime and a scripted model through making a widget, a repaired change, a network widget, a change that never runs, an older version, saving, improving the saved widget and applying with a new host, and through Stop, Send again and drafts. It saves a picture of each step under the app's external files. The conversation was not run with a live model on a device: that copy has no key. The core device suite was not rerun; the core change is covered by JVM tests. The emulators were not used. + | Device | Android | Core | App | | --- | --- | --- | --- | | Pixel 6 (physical) | 17 (API 37) | 27 passed (the example.com fetch check was skipped on the last run: the site answered 403; it had returned 200 on earlier runs) | 56 passed | diff --git a/docs/images/ai-conversation-android.png b/docs/images/ai-conversation-android.png new file mode 100644 index 00000000..5c72a750 Binary files /dev/null and b/docs/images/ai-conversation-android.png differ