diff --git a/Android/app/src/androidTest/java/com/everettjf/scriptwidget/AiConversationUiTest.kt b/Android/app/src/androidTest/java/com/everettjf/scriptwidget/AiConversationUiTest.kt index 02361cd5..8da38b3d 100644 --- a/Android/app/src/androidTest/java/com/everettjf/scriptwidget/AiConversationUiTest.kt +++ b/Android/app/src/androidTest/java/com/everettjf/scriptwidget/AiConversationUiTest.kt @@ -242,5 +242,20 @@ class AiConversationUiTest { assertTrue(applied.files.readText("widget.json")!!.contains("example.org")) assertEquals(listOf("network"), applied.permissions) picture("14-applied") + + // A small change may come as edit blocks. The version says how many lines it changed, and shows them. + replies += "SUMMARY: The time reads 12:45.\n```edit\n<<<<<<< SEARCH\n 12:30\n=======\n" + + " 12:45\n>>>>>>> REPLACE\n```" + ask("Make the time say 12:45") + wait(By.text("The time reads 12:45.")) + assertTrue("the reply form offers edit blocks", requests.last().contains("<<<<<<< SEARCH")) + tap(By.descContains("1 lines added, 1 removed")) + wait(By.textContains("Changes in version")) + wait(By.textContains("12:45")) + wait(By.textContains("12:30")) + picture("15-changes") + tap(By.text("Close")) + val edited = AiServices.conversations(context).latest(saved.installation)!!.currentVersion!!.source + assertTrue("only that line changed", edited.contains("12:45") && !edited.contains("12:30") && edited.contains("#15803d") && edited.contains("example.org")) } } 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 index a0570324..c56d04f9 100644 --- a/Android/app/src/main/java/com/everettjf/scriptwidget/ui/AiConversationScreen.kt +++ b/Android/app/src/main/java/com/everettjf/scriptwidget/ui/AiConversationScreen.kt @@ -82,6 +82,7 @@ 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.graphics.Color import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.semantics import androidx.compose.ui.text.font.FontFamily @@ -109,6 +110,7 @@ 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.AiSourceDiff import com.everettjf.scriptwidget.core.ai.AiConversationTurns import com.everettjf.scriptwidget.core.ai.AiConversationVersion import com.everettjf.scriptwidget.core.ai.AiExamplePrompts @@ -271,6 +273,14 @@ class AiConversationViewModel : ViewModel() { refreshDrafts(context) } + /** Opens a conversation that starts from the version on screen, with no earlier turn to send. */ + fun continueFromHere(context: Context) { + if (isRunning) return + conversation = conversation.continuedFromCurrentVersion() ?: return + revision += 1 + refreshDrafts(context) + } + fun deleteDraft(context: Context, id: String) { val application = context.applicationContext viewModelScope.launch { @@ -428,6 +438,7 @@ fun AiConversationScreen(navigation: NavController, name: String?) { var menu by remember { mutableStateOf(false) } var showDrafts by remember { mutableStateOf(false) } var shownCode by remember { mutableStateOf?>(null) } + var shownChanges by remember { mutableStateOf(null) } var askName by remember { mutableStateOf(false) } var confirmNetwork by remember { mutableStateOf(null) } var confirmApply by remember { mutableStateOf(false) } @@ -509,11 +520,20 @@ fun AiConversationScreen(navigation: NavController, name: String?) { ) }, bottomBar = { + Column { + @Suppress("UNUSED_VARIABLE") val redrawNotice = model.revision + conversation.usageAdvice?.let { advice -> + Row(Modifier.fillMaxWidth().background(MaterialTheme.colorScheme.surfaceContainer).padding(start = 16.dp, end = 8.dp, top = 4.dp), verticalAlignment = Alignment.CenterVertically) { + Text(advice.message, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.weight(1f)) + if (current != null) TextButton(enabled = !running, onClick = { draft = ""; model.continueFromHere(context) }) { Text("Start new from here") } + } + } 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. @@ -554,12 +574,31 @@ fun AiConversationScreen(navigation: NavController, name: String?) { 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 } }, + onChanges = { shownChanges = it }, modifier = Modifier.weight(1f), ) } } } + shownChanges?.let { versionId -> + val version = conversation.version(versionId) + val parent = conversation.version(version?.parentId) + if (version == null || parent == null) shownChanges = null else { + val lines = remember(versionId) { AiSourceDiff.lines(parent.source, version.source) } + AlertDialog( + onDismissRequest = { shownChanges = null }, title = { Text("Changes in version ${version.number}") }, + text = { + if (lines == null) Text("These versions are too long to compare line by line.") + else LazyColumn(Modifier.heightIn(max = 420.dp).horizontalScroll(rememberScrollState())) { + items(lines) { line -> DiffLine(line) } + } + }, + confirmButton = { TextButton(onClick = { shownChanges = null }) { Text("Close") } }, + ) + } + } + shownCode?.let { (title, code) -> AlertDialog( onDismissRequest = { shownCode = null }, title = { Text(title) }, @@ -715,6 +754,24 @@ private fun EmptyState( } } +/** One line of a comparison. The sign is drawn as well as the color, so that the color is not the only cue. */ +@Composable +private fun DiffLine(line: AiSourceDiff.Line) { + if (line.kind == AiSourceDiff.Kind.SKIPPED) { + Text(if (line.skipped == 1) "⋯ 1 unchanged line" else "⋯ ${line.skipped} unchanged lines", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(vertical = 4.dp)) + return + } + val (sign, tint, label) = when (line.kind) { + AiSourceDiff.Kind.ADDED -> Triple("+", Color(0xFF2E7D32).copy(alpha = 0.20f), "Added") + AiSourceDiff.Kind.REMOVED -> Triple("−", Color(0xFFC62828).copy(alpha = 0.18f), "Removed") + else -> Triple(" ", Color.Transparent, "Unchanged") + } + Text( + "$sign ${line.text}", style = MaterialTheme.typography.bodySmall, fontFamily = FontFamily.Monospace, softWrap = false, + modifier = Modifier.background(tint).padding(horizontal = 4.dp, vertical = 1.dp).semantics { contentDescription = "$label: ${line.text}" }, + ) +} + /** * 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. @@ -722,16 +779,20 @@ private fun EmptyState( 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, + /** Lines the version added and removed against the one it was made from; null when there is nothing to compare. */ + val added: Int? = null, val removed: Int? = null, ) { companion object { fun of(conversation: AiConversation, turn: AiConversationTurn): TurnUi { val version = conversation.version(turn.versionId) + val changes = conversation.version(version?.parentId)?.let { parent -> AiSourceDiff.summary(parent.source, version!!.source) }?.takeIf { !it.isEmpty } 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, + changes?.added, changes?.removed, ) } } @@ -740,13 +801,13 @@ private data class TurnUi( @Composable private fun TurnList( turns: List, totalTokens: Int, activity: String?, running: Boolean, - onVersion: (String) -> Unit, onRetry: (String) -> Unit, onFailedCode: (String) -> Unit, modifier: Modifier = Modifier, + onVersion: (String) -> Unit, onRetry: (String) -> Unit, onFailedCode: (String) -> Unit, onChanges: (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) } + items(turns, key = { it.id }) { turn -> TurnRow(turn, activity, running, onVersion, onRetry, onFailedCode, onChanges) } 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)) @@ -755,19 +816,23 @@ private fun TurnList( } @Composable -private fun VersionLine(turn: TurnUi, running: Boolean, onVersion: (String) -> Unit) { +private fun VersionLine(turn: TurnUi, running: Boolean, onVersion: (String) -> Unit, onChanges: (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.added != null && turn.removed != null) TextButton( + onClick = { onChanges(versionId) }, contentPadding = androidx.compose.foundation.layout.PaddingValues(horizontal = 8.dp), + modifier = Modifier.semantics { contentDescription = "${turn.added} lines added, ${turn.removed} removed. Show the changes." }, + ) { Text("+${turn.added} −${turn.removed}", fontFamily = FontFamily.Monospace) } 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) { +private fun TurnRow(turn: TurnUi, activity: String?, running: Boolean, onVersion: (String) -> Unit, onRetry: (String) -> Unit, onFailedCode: (String) -> Unit, onChanges: (String) -> Unit) { val faded = if (turn.abandoned) 0.55f else 1f if (turn.manual) { Column(Modifier.fillMaxWidth().alpha(faded), verticalArrangement = Arrangement.spacedBy(2.dp)) { @@ -775,7 +840,7 @@ private fun TurnRow(turn: TurnUi, activity: String?, running: Boolean, onVersion 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) + VersionLine(turn, running, onVersion, onChanges) } return } @@ -797,7 +862,7 @@ private fun TurnRow(turn: TurnUi, activity: String?, running: Boolean, onVersion 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) + VersionLine(turn, running, onVersion, onChanges) } AiConversationTurn.Status.UNCONVERGED -> { Text("It did not run, so the widget is unchanged.", style = MaterialTheme.typography.bodyLarge, color = MaterialTheme.colorScheme.error) 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 4aefe2c0..9e8bc07d 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 @@ -59,7 +59,11 @@ class AgentLoopRequest( * candidate is judged by the `validate` callback so this class has no * dependency on the JavaScript runtime and can be tested on the JVM. */ -class AgentLoop(private val chat: AiChat) { +class AgentLoop( + private val chat: AiChat, + /** The tags and attributes a candidate may use; null turns the check off. See [AiJsxCheck]. */ + private val surface: AiJsxCheck.Surface? = AiJsxCheck.ANDROID, +) { /** * Runs until a candidate validates, the cap is reached, the provider fails or [cancellation] fires. @@ -78,6 +82,8 @@ class AgentLoop(private val chat: AiChat) { val system = AiMessage(AiMessage.Role.SYSTEM, request.systemPrompt) val firstUser: AiMessage var latest: AiCandidate? + // In a turn: the code the model was shown last, which a reply written as edit blocks changes. + var editBase: String? = null when (val mode = request.mode) { is AgentLoopRequest.Mode.Fresh -> { firstUser = AiMessage(AiMessage.Role.USER, PromptBuilder.userPromptFirst(PromptBuilder.augmentWithSkills(mode.userDescription, request.skillsText), request.size)) @@ -93,6 +99,7 @@ class AgentLoop(private val chat: AiChat) { is AgentLoopRequest.Mode.Turn -> { firstUser = AiMessage(AiMessage.Role.USER, PromptBuilder.userPromptTurn(mode.context)) latest = null + editBase = mode.context.currentCode } } val isTurn = request.mode is AgentLoopRequest.Mode.Turn @@ -100,7 +107,8 @@ class AgentLoop(private val chat: AiChat) { var lastError: String? = null var lastLogs: List = emptyList() - for (iteration in 1..iterationLimit(request.maxIterations)) { + val limit = iterationLimit(request.maxIterations) + for (iteration in 1..limit) { if (cancellation.isCancelled) return AgentLoopOutcome.Cancelled(usage) onEvent(AgentLoopEvent.Thinking(iteration)) @@ -134,7 +142,22 @@ class AgentLoop(private val chat: AiChat) { 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 + val edited = try { + AiSourceEdits.parse(result.content)?.let { edits -> + val base = editBase + if (base.isNullOrEmpty()) throw AiSourceEdits.Failure("there is no current code to change") + AiSourceEdits.apply(edits, base) + } + } catch (failure: AiSourceEdits.Failure) { + // Refused whole: the model is told why and asked for all of the code. + val summary = "The edit blocks could not be applied: ${failure.reason}. Reply with the full widget code instead." + if (!editBase.isNullOrEmpty()) latest = AiCandidate(editBase!!, latest?.manifestJson) + lastError = summary + lastLogs = emptyList() + onEvent(AgentLoopEvent.RanFailed(iteration, summary, emptyList())) + continue + } + if (edited != null) AiCandidate(edited, reply.candidate.manifestJson) else 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 @@ -143,10 +166,11 @@ class AgentLoop(private val chat: AiChat) { } val candidate = AiCandidate(extracted.source, declaration) latest = candidate + if (isTurn && candidate.source.isNotEmpty()) editBase = candidate.source onEvent(AgentLoopEvent.Produced(iteration, candidate.source, candidate.manifestJson)) onEvent(AgentLoopEvent.Running(iteration)) - val validation = when { + val ran = when { candidate.source.isEmpty() -> ValidationResult.failure("The reply contained no JSX code.") result.truncated -> ValidationResult.failure("The reply was cut off at the output limit. Return a shorter, complete widget.") declarationError != null -> ValidationResult.failure("Invalid package declaration: $declarationError") @@ -158,6 +182,16 @@ class AgentLoop(private val chat: AiChat) { } if (cancellation.isCancelled) return AgentLoopOutcome.Cancelled(usage) + // An attribute that does not exist is ignored when the widget runs, so running proves + // nothing about it. Not on the last attempt: a widget that runs is better than none. + val unknown = if (surface != null && candidate.source.isNotEmpty() && declarationError == null && !result.truncated) AiJsxCheck.summary(candidate.source, surface) else null + val validation = when { + unknown == null -> ran + !ran.ok -> ValidationResult.failure((ran.errorSummary ?: "Runtime returned no element.") + "\n\n" + unknown, ran.logs) + iteration < limit -> ValidationResult.failure(unknown, ran.logs) + else -> ran + } + if (validation.ok) { onEvent(AgentLoopEvent.RanSucceeded(iteration)) return AgentLoopOutcome.Succeeded(candidate.source, candidate.manifestJson, usage, note) 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 index 5520f92a..e34c56d0 100644 --- 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 @@ -21,6 +21,24 @@ object AiConversationLimits { const val TITLE_CHARACTERS = 40 const val NOTE_CHARACTERS = 300 const val ERROR_CHARACTERS = 600 + /** From here on the user is told that the conversation has grown heavy. */ + const val HEAVY_PROMPT_TOKENS = 30_000 + const val HEAVY_TOTAL_TOKENS = 300_000 +} + +/** + * Something the user should know about the size of a conversation; the way out is to continue + * in a new conversation from the current version. Port of `AIConversationUsageAdvice`. + */ +data class AiConversationUsageAdvice(val reason: Reason, val tokens: Int = 0) { + enum class Reason { HEAVY_REQUESTS, HEAVY_TOTAL, VERSIONS_FULL, HISTORY_FULL } + + val message: String get() = when (reason) { + Reason.HEAVY_REQUESTS -> "Each request now sends about ${"%,d".format(tokens)} tokens. A new conversation from this version sends less." + Reason.HEAVY_TOTAL -> "This conversation has used ${"%,d".format(tokens)} tokens. A new conversation from this version starts small again." + Reason.VERSIONS_FULL -> "A conversation keeps ${AiConversationLimits.MAX_VERSIONS} versions; the oldest are dropped from now on." + Reason.HISTORY_FULL -> "The AI is told about the last ${AiConversationLimits.HISTORY_TURNS} turns only; older requests are no longer sent." + } } /** One version of the widget's source a turn produced. Port of `AIConversationVersion`. */ @@ -109,6 +127,32 @@ class AiConversation( val totalUsage: AiTokenUsage get() = turns.fold(AiTokenUsage.ZERO) { sum, turn -> sum + turn.usage } val isRunning: Boolean get() = turns.any { it.status == AiConversationTurn.Status.RUNNING } + /** What the newest finished turn sent per request, on average over its attempts. */ + val lastRequestTokens: Int get() { + val turn = turns.lastOrNull { it.usage.promptTokens > 0 && it.status != AiConversationTurn.Status.RUNNING } ?: return 0 + val attempts = if (turn.status == AiConversationTurn.Status.SUCCEEDED) turn.repairs + 1 else maxOf(1, turn.repairs) + return turn.usage.promptTokens / maxOf(1, attempts) + } + + /** The most pressing thing to say about the size of the conversation, if any. */ + val usageAdvice: AiConversationUsageAdvice? get() { + if (lastRequestTokens >= AiConversationLimits.HEAVY_PROMPT_TOKENS) return AiConversationUsageAdvice(AiConversationUsageAdvice.Reason.HEAVY_REQUESTS, lastRequestTokens) + if (totalUsage.totalTokens >= AiConversationLimits.HEAVY_TOTAL_TOKENS) return AiConversationUsageAdvice(AiConversationUsageAdvice.Reason.HEAVY_TOTAL, totalUsage.totalTokens) + if (versions.size >= AiConversationLimits.MAX_VERSIONS) return AiConversationUsageAdvice(AiConversationUsageAdvice.Reason.VERSIONS_FULL) + val told = turns.count { it.status == AiConversationTurn.Status.SUCCEEDED && !it.isAbandoned && !it.isManualEdit } + if (told > AiConversationLimits.HISTORY_TURNS) return AiConversationUsageAdvice(AiConversationUsageAdvice.Reason.HISTORY_FULL) + return null + } + + /** + * A new conversation that starts from the current version: same size, same widget, one + * version, no turns, and so no history to send. Null without a version. + */ + fun continuedFromCurrentVersion(now: Long = System.currentTimeMillis()): AiConversation? { + val current = currentVersion ?: return null + return AiConversation(title = title, size = size, packageId = packageId, createdAt = now).also { it.addVersion(current.source, current.networkDomains, now) } + } + fun version(id: String?): AiConversationVersion? = id?.let { wanted -> versions.firstOrNull { it.id == wanted } } fun turn(id: String): AiConversationTurn? = turns.firstOrNull { it.id == id } diff --git a/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiJsxCheck.kt b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiJsxCheck.kt new file mode 100644 index 00000000..168f6397 --- /dev/null +++ b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiJsxCheck.kt @@ -0,0 +1,222 @@ +package com.everettjf.scriptwidget.core.ai + +/** + * Finds tags and attributes in generated code that ScriptWidget does not have. + * Port of `AIJSXCheck`; the two must report the same problems for the same code. + * + * The runtime ignores an unknown attribute, and a value outside a closed list, + * without an error: the widget runs and shows something else. The repair loop + * can only fix what it is told, so this check tells it before the widget runs. + * + * It reads the code loosely, not as a parser would, and is built to stay + * silent when unsure: a tag whose name starts with a capital letter is taken + * for the script's own component, and anything that is not plainly a tag is + * skipped. It grants nothing and blocks nothing at + * run time; the runtime's own switch stays the authority. + */ +object AiJsxCheck { + /** Attribute name to its closed list of values, or null when any value goes. */ + class Surface(val globals: Map?>, val tags: Map?>>) + + const val MAX_PROBLEMS = 5 + + /** The tags of this system: the shared ones and those marked `@android`. */ + val ANDROID: Surface by lazy { parseSurface(AiJsxSurfaceData.TEXT, "android") } + + /** Reads the generated list; [system] chooses which system-only tags count. */ + fun parseSurface(text: String, system: String?): Surface { + val globals = LinkedHashMap?>() + val tags = LinkedHashMap?>>() + fun attribute(word: String): Pair?> { + val at = word.indexOf('=') + return if (at < 0) word to null else word.substring(0, at) to word.substring(at + 1).split("|").toSet() + } + for (line in text.lines()) { + val words = line.trim().split(" ").filter { it.isNotEmpty() } + if (words.size < 2) continue + when { + words[0] == "global" -> attribute(words[1]).let { globals[it.first] = it.second } + words[0] == "tag" || (system != null && words[0] == "tag@$system") -> + tags[words[1]] = words.drop(2).associate { attribute(it) } + } + } + return Surface(globals, tags) + } + + /** What is wrong with [source], at most [MAX_PROBLEMS] sentences; empty when nothing is. */ + fun problems(source: String, surface: Surface = ANDROID): List { + val found = LinkedHashSet() + for (tag in tags(source)) { + if (found.size >= MAX_PROBLEMS) break + // A capital letter marks the script's own component, even one named like a tag; a dotted name is a member. + if (tag.name.first() !in 'a'..'z' || tag.name.contains('.')) continue + val name = tag.name + val own = surface.tags[name] + if (own == null) { + found += "<$name> is not a ScriptWidget tag. The tags are: ${surface.tags.keys.joinToString(", ")}." + continue + } + for (attribute in tag.attributes) { + if (found.size >= MAX_PROBLEMS) break + val known = own.containsKey(attribute.name) || surface.globals.containsKey(attribute.name) + if (!known) { + val names = (own.keys + surface.globals.keys).joinToString(", ") + found += "<$name> has no attribute \"${attribute.name}\"; it would be ignored. Its attributes are: $names." + continue + } + val allowed = own[attribute.name] ?: surface.globals[attribute.name] ?: continue + val value = attribute.literal ?: continue + if (value !in allowed) { + found += "${attribute.name}=\"$value\" on <$name> is not one of its values; it would be ignored. Use one of: ${allowed.joinToString(", ")}." + } + } + } + return found.toList() + } + + /** One line for the model, or null when [source] has no problem. */ + fun summary(source: String, surface: Surface = ANDROID): String? { + val problems = problems(source, surface) + if (problems.isEmpty()) return null + return "The code uses tags or attributes that ScriptWidget does not have:\n" + problems.joinToString("\n") { "- $it" } + } + + internal class Attribute(val name: String, /** The value when it is written as a plain string. */ val literal: String?) + internal class Tag(val name: String, val attributes: List) + + private val TAG_BEFORE_KEYWORDS = setOf("return", "yield", "case", "default", "await", "else", "do", "in", "of", "typeof", "void", "delete", "new", "throw") + + /** The opening tags of [source], read loosely. */ + internal fun tags(source: String): List { + val text = source + val length = text.length + val result = ArrayList() + var index = 0 + while (index < length) { + val character = text[index] + when { + character == '/' && index + 1 < length && text[index + 1] == '/' -> { + index = text.indexOf('\n', index).let { if (it < 0) length else it } + } + character == '/' && index + 1 < length && text[index + 1] == '*' -> { + index = text.indexOf("*/", index + 2).let { if (it < 0) length else it + 2 } + } + character == '"' || character == '\'' -> index = skipQuoted(text, index, sameLineOnly = true) + character == '`' -> index = skipQuoted(text, index, sameLineOnly = false) + character == '<' && index + 1 < length && text[index + 1].isAsciiLetter() && startsTag(text, index) -> { + val read = readTag(text, index + 1) + if (read == null) { + index += 1 + } else { + result += read.first + index = read.second + } + } + else -> index += 1 + } + } + return result + } + + /** Whether the `<` at [at] can open a tag: after an operator or a keyword, not after a value. */ + private fun startsTag(text: String, at: Int): Boolean { + var before = at - 1 + while (before >= 0 && text[before].isWhitespace()) before -= 1 + if (before < 0) return true + val character = text[before] + if (character == ')' || character == ']') return false + if (!character.isWordCharacter()) return true + var start = before + while (start >= 0 && text[start].isWordCharacter()) start -= 1 + return text.substring(start + 1, before + 1) in TAG_BEFORE_KEYWORDS + } + + /** Reads a tag whose name starts at [from]; the tag and the index after its `>`, or null when it is not one. */ + private fun readTag(text: String, from: Int): Pair? { + val length = text.length + var index = from + while (index < length && (text[index].isAsciiLetter() || text[index] in '0'..'9' || text[index] == '.' || text[index] == '_')) index += 1 + val name = text.substring(from, index) + val attributes = ArrayList() + while (true) { + while (index < length && text[index].isWhitespace()) index += 1 + if (index >= length) return null + val character = text[index] + if (character == '>') return Tag(name, attributes) to index + 1 + if (character == '/') { + return if (index + 1 < length && text[index + 1] == '>') Tag(name, attributes) to index + 2 else null + } + if (character == '{') { + // {...spread} + index = skipBraces(text, index) ?: return null + continue + } + if (!(character.isAsciiLetter() || character == '_')) return null + val start = index + while (index < length && (text[index].isAsciiLetter() || text[index] in '0'..'9' || text[index] == '_' || text[index] == '-' || text[index] == ':')) index += 1 + val attributeName = text.substring(start, index) + while (index < length && text[index].isWhitespace()) index += 1 + if (index >= length || text[index] != '=') { + attributes += Attribute(attributeName, null) + continue + } + index += 1 + while (index < length && text[index].isWhitespace()) index += 1 + if (index >= length) return null + when (text[index]) { + '"', '\'' -> { + val end = text.indexOf(text[index], index + 1) + if (end < 0) return null + attributes += Attribute(attributeName, text.substring(index + 1, end)) + index = end + 1 + } + '{' -> { + val end = skipBraces(text, index) ?: return null + val inner = text.substring(index + 1, end - 1).trim() + // {"value"} is as plain as "value". + val literal = if (inner.length >= 2 && (inner.first() == '"' || inner.first() == '\'') && inner.last() == inner.first() && + inner.indexOf(inner.first(), 1) == inner.length - 1 && !inner.contains('\\')) inner.substring(1, inner.length - 1) else null + attributes += Attribute(attributeName, literal) + index = end + } + else -> return null + } + } + } + + /** The index after the `}` that closes the `{` at [from], or null when it is not closed. */ + private fun skipBraces(text: String, from: Int): Int? { + var depth = 0 + var index = from + while (index < text.length) { + when (text[index]) { + '{' -> { depth += 1; index += 1 } + '}' -> { depth -= 1; index += 1; if (depth == 0) return index } + '"', '\'' -> index = skipQuoted(text, index, sameLineOnly = true) + '`' -> index = skipQuoted(text, index, sameLineOnly = false) + else -> index += 1 + } + } + return null + } + + /** + * The index after the quoted text that starts at [from]. A quote that is not closed on its line, + * an apostrophe in a sentence for one, is passed over as a single character. + */ + private fun skipQuoted(text: String, from: Int, sameLineOnly: Boolean): Int { + val quote = text[from] + var index = from + 1 + while (index < text.length) { + val character = text[index] + if (character == '\\') { index += 2; continue } + if (character == quote) return index + 1 + if (sameLineOnly && character == '\n') return from + 1 + index += 1 + } + return from + 1 + } + + private fun Char.isAsciiLetter(): Boolean = this in 'a'..'z' || this in 'A'..'Z' + private fun Char.isWordCharacter(): Boolean = isAsciiLetter() || this in '0'..'9' || this == '_' || this == '$' +} diff --git a/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiJsxSurfaceData.kt b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiJsxSurfaceData.kt new file mode 100644 index 00000000..7fd1e410 --- /dev/null +++ b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiJsxSurfaceData.kt @@ -0,0 +1,59 @@ +package com.everettjf.scriptwidget.core.ai + +// Generated by Editor/editorfe/scripts/generate-jsx-surface.mjs from scriptWidgetAPI.js and coverage.js. Do not edit; run `npm run docs` in Editor/editorfe. + +internal object AiJsxSurfaceData { + /** One line per general attribute ("global") and per tag ("tag"). An attribute with a closed list of values is written name=a|b|c. */ + val TEXT = """ +global background +global backgroundMaterial=none|ultraThin|thin|regular|thick|ultraThick +global color +global font +global fontWeight=ultraLight|thin|light|regular|medium|semibold|bold|heavy|black +global fontDesign=default|rounded|serif|monospaced +global frame +global padding +global corner +global opacity +global alignment +global clip=circle|rect|capsule|ellipse +global rotation +global rotation3d +global shadow +global animation +global widgetAccentable +global linkurl +global weight=ultraLight|thin|light|regular|medium|semibold|bold|heavy|black +global key +tag vstack spacing +tag hstack spacing +tag zstack +tag vgrid columns spacing alignment +tag hgrid columns spacing alignment +tag text text +tag date date style=time|date|relative|offset|timer +tag image systemName name id url src ratio mode=fit|fill accentedRenderingMode +tag gif file +tag spacer minLength +tag rect trim stroke +tag roundedrect radius +tag capsule trim stroke +tag ellipse trim stroke +tag circle trim stroke +tag gauge type value style text current min max angle thickness needleColor label title sections labelFont labelColor titleColor titleFont +tag chart type data category hideLegend hideXAxis hideYAxis +tag link url +tag divider thickness axis=horizontal|vertical +tag line thickness length axis=horizontal|vertical +tag icon systemName size +tag label title systemName +tag progress value total label style=linear|circular trackColor thickness +tag ring value thickness trackColor +tag badge text radius +tag chip text radius borderColor +tag stat title value subtitle mutedColor +tag button action onClick actionID +tag toggle on onClick actionID stateKey +tag@android list spacing +""".trim() +} diff --git a/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiSourceDiff.kt b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiSourceDiff.kt new file mode 100644 index 00000000..ee73c1dd --- /dev/null +++ b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiSourceDiff.kt @@ -0,0 +1,83 @@ +package com.everettjf.scriptwidget.core.ai + +/** + * What changed between two versions of a widget's code, line by line. Port of + * `AISourceDiff`. Computed on the device; nothing is sent anywhere. + */ +object AiSourceDiff { + enum class Kind { SAME, ADDED, REMOVED, SKIPPED } + + /** One line to show. For [Kind.SKIPPED], [skipped] unchanged lines are left out and [text] is empty. */ + data class Line(val kind: Kind, val text: String, val skipped: Int = 0) + + data class Summary(val added: Int, val removed: Int) { + val isEmpty: Boolean get() = added == 0 && removed == 0 + } + + /** Unchanged lines kept around a change. */ + const val CONTEXT_LINES = 3 + /** Above this many lines on a side the comparison is not made; the caller shows the code instead. */ + const val MAX_LINES = 4_000 + /** The changed middle parts may hold this many line pairs. */ + private const val MAX_CELLS = 4_000_000L + + fun split(text: String): List { + val lines = text.replace("\r\n", "\n").split("\n") + return if (lines.lastOrNull() == "") lines.dropLast(1) else lines + } + + /** How many lines were added and removed; null when a side is too long to compare. */ + fun summary(old: String, new: String): Summary? { + val all = fullLines(old, new) ?: return null + return Summary(all.count { it.kind == Kind.ADDED }, all.count { it.kind == Kind.REMOVED }) + } + + /** The changed lines with a few unchanged ones around them; null when a side is too long. */ + fun lines(old: String, new: String, context: Int = CONTEXT_LINES): List? { + val all = fullLines(old, new) ?: return null + if (all.none { it.kind != Kind.SAME }) return emptyList() + val keep = BooleanArray(all.size) + all.forEachIndexed { index, line -> + if (line.kind != Kind.SAME) for (near in maxOf(0, index - context)..minOf(all.size - 1, index + context)) keep[near] = true + } + val result = ArrayList() + var skipped = 0 + fun flush() { if (skipped > 0) result += Line(Kind.SKIPPED, "", skipped); skipped = 0 } + all.forEachIndexed { index, line -> if (keep[index]) { flush(); result += line } else skipped += 1 } + flush() + return result + } + + private fun fullLines(old: String, new: String): List? { + val before = split(old) + val after = split(new) + if (before.size > MAX_LINES || after.size > MAX_LINES) return null + // What both sides start and end with needs no comparison. + var head = 0 + while (head < before.size && head < after.size && before[head] == after[head]) head += 1 + var tail = 0 + while (tail < before.size - head && tail < after.size - head && before[before.size - 1 - tail] == after[after.size - 1 - tail]) tail += 1 + val a = before.subList(head, before.size - tail) + val b = after.subList(head, after.size - tail) + if (a.size.toLong() * b.size.toLong() > MAX_CELLS) return null + + // Longest common subsequence of the middle parts. + val table = Array(a.size + 1) { IntArray(b.size + 1) } + for (i in a.indices.reversed()) for (j in b.indices.reversed()) { + table[i][j] = if (a[i] == b[j]) table[i + 1][j + 1] + 1 else maxOf(table[i + 1][j], table[i][j + 1]) + } + val result = ArrayList(before.size + after.size) + for (index in 0 until head) result += Line(Kind.SAME, after[index]) + var i = 0 + var j = 0 + while (i < a.size || j < b.size) { + when { + i < a.size && j < b.size && a[i] == b[j] -> { result += Line(Kind.SAME, b[j]); i += 1; j += 1 } + i < a.size && (j >= b.size || table[i + 1][j] >= table[i][j + 1]) -> { result += Line(Kind.REMOVED, a[i]); i += 1 } + else -> { result += Line(Kind.ADDED, b[j]); j += 1 } + } + } + for (index in after.size - tail until after.size) result += Line(Kind.SAME, after[index]) + return result + } +} diff --git a/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiSourceEdits.kt b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiSourceEdits.kt new file mode 100644 index 00000000..1230dd50 --- /dev/null +++ b/Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiSourceEdits.kt @@ -0,0 +1,99 @@ +package com.everettjf.scriptwidget.core.ai + +/** One change to a widget's source: [search] is replaced by [replace]. */ +data class AiSourceEdit(val search: String, val replace: String) + +/** + * A reply that changes part of the current code instead of sending all of it. + * Port of `AISourceEdits`; the two must read and apply the same replies in the + * same way. + * + * The model writes one block per place: + * + * <<<<<<< SEARCH + * lines copied from the current code + * ======= + * the lines that replace them + * >>>>>>> REPLACE + * + * Nothing is guessed. The searched text has to be in the code exactly once; + * otherwise the whole reply is refused and the model is asked again. What comes + * out is run and checked like any other candidate. + */ +object AiSourceEdits { + const val SEARCH_MARKER = "<<<<<<< SEARCH" + const val DIVIDER_MARKER = "=======" + const val REPLACE_MARKER = ">>>>>>> REPLACE" + const val MAX_EDITS = 20 + + class Failure(val reason: String) : Exception(reason) + + /** Whether [reply] is written as edit blocks. */ + fun isEditReply(reply: String): Boolean = lines(reply).any { it.trim() == SEARCH_MARKER } + + /** + * The edits of [reply], in order; null when it has no edit block. + * @throws Failure for a block that is not complete, has nothing to search for, or too many blocks. + */ + fun parse(reply: String): List? { + if (!isEditReply(reply)) return null + val edits = ArrayList() + var search: ArrayList? = null + var replace: ArrayList? = null + for (line in lines(reply)) { + val marker = line.trim() + when { + marker == SEARCH_MARKER -> { + if (search != null) throw Failure("an edit block starts before the one before it ends") + search = ArrayList() + } + marker == DIVIDER_MARKER && search != null && replace == null -> replace = ArrayList() + marker == REPLACE_MARKER -> { + val from = search + val to = replace + if (from == null || to == null) throw Failure("an edit block ends without its SEARCH part and its ======= line") + val text = from.joinToString("\n") + if (text.isBlank()) throw Failure("edit ${edits.size + 1} has nothing in its SEARCH part") + edits += AiSourceEdit(text, to.joinToString("\n")) + if (edits.size > MAX_EDITS) throw Failure("there are more than $MAX_EDITS edit blocks") + search = null + replace = null + } + replace != null -> replace.add(line) + search != null -> search.add(line) + } + } + if (search != null) throw Failure("the last edit block has no >>>>>>> REPLACE line") + return edits + } + + /** + * [source] with [edits] applied one after the other. The result ends with a line break. + * @throws Failure when a searched text is not in the code, or is there more than once. + */ + fun apply(edits: List, source: String): String { + var text = normalized(source) + edits.forEachIndexed { index, edit -> + val count = occurrences(text, edit.search) + if (count == 0) throw Failure("the SEARCH text of edit ${index + 1} is not in the current code; it has to be copied exactly, with its indentation") + if (count > 1) throw Failure("the SEARCH text of edit ${index + 1} is in the current code $count times; add neighbouring lines so that it is there once") + val at = text.indexOf(edit.search) + text = text.substring(0, at) + edit.replace + text.substring(at + edit.search.length) + } + return text.trimEnd('\n', ' ', '\t') + "\n" + } + + private fun normalized(text: String): String = text.replace("\r\n", "\n").replace('\r', '\n') + + private fun lines(text: String): List = normalized(text).split("\n") + + private fun occurrences(text: String, part: String): Int { + var count = 0 + var from = text.indexOf(part) + while (from >= 0) { + count += 1 + from = text.indexOf(part, from + part.length) + } + return count + } +} 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 86191f47..9f2b3cef 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 @@ -292,10 +292,11 @@ Return the FULL updated JSX only (plus the network declaration block only if it } else { parts += "The user asks for a new widget:\n${context.request}" } + val codeRule = if (current.isNullOrEmpty()) "then the FULL widget code in one ```jsx fenced block." else EDIT_REPLY_RULE 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. +$codeRule If, and only if, the widget uses `fetch` or `${'$'}http`, end with one more fenced block that declares every host it contacts: ```json @@ -307,6 +308,26 @@ blocked. No other permission can be declared. Nothing else. return parts.joinToString("\n\n") } + /** + * How a turn that changes existing code may answer: with all of the code, or with the + * places that change. The same words as `PromptBuilder.editReplyRule`; see [AiSourceEdits]. + */ + val EDIT_REPLY_RULE = """ +then EITHER the FULL widget code in one ```jsx fenced block, +OR, when only a few places change, edit blocks instead of the full code: +```edit +<<<<<<< SEARCH +lines copied exactly from the current code, with their indentation +======= +the lines that replace them +>>>>>>> REPLACE +``` +Write one SEARCH/REPLACE pair for each place, as many as the change needs. Each SEARCH +text must be in the current code exactly once: copy enough neighbouring lines to make +it so. An empty REPLACE part deletes the lines. Prefer edit blocks for a small change +and the full code when most of the widget changes. Never use both forms in one reply. +""".trim() + /** 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." diff --git a/Android/core/src/test/java/com/everettjf/scriptwidget/core/ai/AiJsxCheckTest.kt b/Android/core/src/test/java/com/everettjf/scriptwidget/core/ai/AiJsxCheckTest.kt new file mode 100644 index 00000000..2f167f5c --- /dev/null +++ b/Android/core/src/test/java/com/everettjf/scriptwidget/core/ai/AiJsxCheckTest.kt @@ -0,0 +1,111 @@ +package com.everettjf.scriptwidget.core.ai + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test +import java.io.File + +/** The check for tags and attributes ScriptWidget does not have. Mirrors the Swift tests. */ +class AiJsxCheckTest { + private val apple = AiJsxCheck.parseSurface(AiJsxSurfaceData.TEXT, null) + + private fun problems(source: String) = AiJsxCheck.problems(source, AiJsxCheck.ANDROID) + + @Test + fun aWidgetThatUsesOnlyWhatExistsHasNoProblem() { + val source = """ + // is a tag + const title = "a in a string"; + const wide = count + It's {title} + + + {rows.map((row) => )} + + , + ); + """.trimIndent() + assertEquals(emptyList(), problems(source)) + assertNull(AiJsxCheck.summary(source)) + } + + @Test + fun anAttributeOrValueOrTagThatDoesNotExistIsNamed() { + val weekday = problems("${'$'}render()") + assertEquals(1, weekday.size) + assertTrue(weekday[0], weekday[0].contains("style=\"weekday\"") && weekday[0].contains("time, date, relative, offset, timer")) + + val format = problems("${'$'}render()") + assertTrue(format[0], format[0].contains(" has no attribute \"format\"") && format[0].contains("style")) + + val tag = problems("${'$'}render(a)") + assertTrue(tag[0], tag[0].contains(" is not a ScriptWidget tag") && tag[0].contains("vstack")) + + assertTrue(problems("return a")[0].contains("fontWeight=\"huge\"")) + assertEquals("a value that is computed is not judged", emptyList(), problems("${'$'}render(a)")) + assertTrue(AiJsxCheck.summary("${'$'}render()")!!.startsWith("The code uses tags or attributes that ScriptWidget does not have:\n- ")) + } + + @Test + fun theSameProblemIsReportedOnceAndTheListIsShort() { + val many = (1..12).joinToString("") { "a" } + "a" + assertEquals(AiJsxCheck.MAX_PROBLEMS, problems("${'$'}render($many)").size) + assertEquals(1, problems("${'$'}render()").size) + } + + @Test + fun theScrollingListIsATagOnAndroidOnly() { + val source = "${'$'}render(a)" + assertEquals(emptyList(), AiJsxCheck.problems(source, AiJsxCheck.ANDROID)) + assertTrue(AiJsxCheck.problems(source, apple)[0].contains(" is not a ScriptWidget tag")) + } + + private class FakeChat(private val replies: List) : AiChat { + val conversations = ArrayList>() + override fun chat(messages: List, cancellation: AiCancellation): AiChatResult { + conversations += messages + return AiChatResult(replies[minOf(conversations.size, replies.size) - 1], AiTokenUsage(10, 5, 15)) + } + } + + private fun request(maxIterations: Int) = AgentLoopRequest(AgentLoopRequest.Mode.Fresh("the weekday"), AiWidgetSize.MEDIUM, "SYSTEM", maxIterations) + + @Test + fun theLoopSendsAMadeUpValueBackAlthoughTheWidgetRuns() { + val wrong = "```jsx\n${'$'}render()\n```" + val right = "```jsx\n${'$'}render()\n```" + val chat = FakeChat(listOf(wrong, right)) + val ran = ArrayList() + val outcome = AgentLoop(chat).run(request(4), { source, _ -> ran += source; ValidationResult.success() }) + assertTrue((outcome as AgentLoopOutcome.Succeeded).source.contains("style=\"date\"")) + assertEquals("both ran; running alone would have accepted the first", 2, ran.size) + assertTrue(chat.conversations[1].last().content.contains("style=\"weekday\" on is not one of its values")) + } + + @Test + fun onTheLastAttemptAWidgetThatRunsIsKept() { + val wrong = "```jsx\n${'$'}render()\n```" + val chat = FakeChat(listOf(wrong)) + val outcome = AgentLoop(chat).run(request(3), { _, _ -> ValidationResult.success() }) + assertTrue(outcome is AgentLoopOutcome.Succeeded) + assertEquals(3, chat.conversations.size) + } + + /** Every template, example and Gallery widget in the repository passes: the check must not cry wolf. */ + @Test + fun everyBundledWidgetPasses() { + var root = File("").absoluteFile + while (!File(root, "Shared/ScriptWidgetRuntime/Resource/Script.bundle").isDirectory) root = root.parentFile ?: error("repository root not found") + val folders = listOf("Shared/ScriptWidgetRuntime/Resource/Script.bundle", "Gallery", "Android/app/src/main/assets/Script.android", "Scripts") + val files = folders.map { File(root, it) }.filter { it.isDirectory } + .flatMap { folder -> folder.walkTopDown().filter { it.isFile && it.extension == "jsx" && !it.path.contains("__Build") }.toList() } + assertTrue("found ${files.size} widgets", files.size > 50) + val wrong = files.mapNotNull { file -> + AiJsxCheck.problems(file.readText()).takeIf { it.isNotEmpty() }?.let { "${file.relativeTo(root)}: ${it.joinToString(" | ") { text -> text.take(110) }}" } + } + assertEquals(wrong.joinToString("\n"), 0, wrong.size) + } +} diff --git a/Android/core/src/test/java/com/everettjf/scriptwidget/core/ai/AiSourceEditsTest.kt b/Android/core/src/test/java/com/everettjf/scriptwidget/core/ai/AiSourceEditsTest.kt new file mode 100644 index 00000000..9666c1a2 --- /dev/null +++ b/Android/core/src/test/java/com/everettjf/scriptwidget/core/ai/AiSourceEditsTest.kt @@ -0,0 +1,174 @@ +package com.everettjf.scriptwidget.core.ai + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test + +/** Edit replies: read, applied, refused, and what the loop does with them. Mirrors the Swift tests. */ +class AiSourceEditsTest { + private val good = "\$render(Hello)\n" + private val better = "\$render(HelloWorld)\n" + + private class FakeChat(private val replies: List) : AiChat { + val conversations = ArrayList>() + override fun chat(messages: List, cancellation: AiCancellation): AiChatResult { + conversations += messages + return AiChatResult(replies[minOf(conversations.size, replies.size) - 1], AiTokenUsage(10, 5, 15)) + } + } + + private fun edit(summary: String, vararg pairs: Pair): String = + "SUMMARY: $summary\n```edit\n" + pairs.joinToString("\n") { "<<<<<<< SEARCH\n${it.first}\n=======\n${it.second}\n>>>>>>> REPLACE" } + "\n```" + + private fun turn(current: String?) = AgentLoopRequest( + AgentLoopRequest.Mode.Turn(AiTurnContext(emptyList(), current, null, "Say Hi instead", null, AiWidgetSize.MEDIUM)), + AiWidgetSize.MEDIUM, "SYSTEM", AiConversationLimits.MAX_ATTEMPTS_PER_TURN, + ) + + private fun reason(body: () -> Unit): String = try { body(); "none" } catch (failure: AiSourceEdits.Failure) { failure.reason } + + @Test + fun editBlocksAreReadAndAppliedInOrder() { + val reply = "SUMMARY: Two changes.\n```edit\n<<<<<<< SEARCH\n Hello\n=======\n Hi\n there\n>>>>>>> REPLACE\n" + + "<<<<<<< SEARCH\nconst a = 1;\n=======\n>>>>>>> REPLACE\n```" + val edits = AiSourceEdits.parse(reply)!! + assertEquals(listOf(AiSourceEdit(" Hello", " Hi\n there"), AiSourceEdit("const a = 1;", "")), edits) + val source = "const a = 1;\r\n\$render(\n Hello\n)" + assertEquals("\n\$render(\n Hi\n there\n)\n", AiSourceEdits.apply(edits, source)) + assertNull("a full reply has no edits", AiSourceEdits.parse("SUMMARY: x\n```jsx\n\$render(a)\n```")) + } + + @Test + fun anEditThatIsMissingAmbiguousOrMalformedIsRefused() { + val source = "a\na\nb\n" + assertTrue(reason { AiSourceEdits.apply(listOf(AiSourceEdit("c", "x")), source) }.contains("is not in the current code")) + assertTrue(reason { AiSourceEdits.apply(listOf(AiSourceEdit("a", "x")), source) }.contains("2 times")) + assertTrue(reason { AiSourceEdits.apply(listOf(AiSourceEdit("b", "x"), AiSourceEdit("nowhere", "y")), source) }.contains("edit 2")) + assertTrue(reason { AiSourceEdits.parse("<<<<<<< SEARCH\na\n=======\nb") }.contains("no >>>>>>> REPLACE")) + assertTrue(reason { AiSourceEdits.parse("<<<<<<< SEARCH\n\n=======\nb\n>>>>>>> REPLACE") }.contains("nothing in its SEARCH")) + assertTrue(reason { AiSourceEdits.parse("<<<<<<< SEARCH\na\n>>>>>>> REPLACE") }.contains("ends without")) + val many = List(AiSourceEdits.MAX_EDITS + 1) { "<<<<<<< SEARCH\na\n=======\nb\n>>>>>>> REPLACE" }.joinToString("\n") + assertTrue(reason { AiSourceEdits.parse(many) }.contains("more than")) + } + + @Test + fun theTurnPromptOffersEditBlocksOnlyWhenThereIsCodeToChange() { + fun prompt(current: String?) = PromptBuilder.userPromptTurn(AiTurnContext(emptyList(), current, null, "x", null, AiWidgetSize.MEDIUM)) + assertTrue(prompt("CODE").contains(AiSourceEdits.SEARCH_MARKER)) + assertFalse(prompt(null).contains(AiSourceEdits.SEARCH_MARKER)) + } + + @Test + fun aTurnAnsweredWithEditBlocksChangesOnlyThatPlace() { + val chat = FakeChat(listOf(edit("Says Hi now.", "Hello" to "Hi"))) + val validated = ArrayList() + val outcome = AgentLoop(chat).run(turn(good), { source, _ -> validated += source; ValidationResult.success() }) + outcome as AgentLoopOutcome.Succeeded + assertEquals("\$render(Hi)\n", outcome.source) + assertEquals("Says Hi now.", outcome.note) + assertEquals(listOf(outcome.source), validated) + assertEquals(1, chat.conversations.size) + } + + @Test + fun anEditThatDoesNotApplyIsSentBackAndTheFullCodeIsAccepted() { + val chat = FakeChat(listOf(edit("Says Hi now.", "Goodbye" to "Hi"), "SUMMARY: Says Hi now.\n```jsx\n$better```")) + val validated = ArrayList() + val outcome = AgentLoop(chat).run(turn(good), { source, _ -> validated += source; ValidationResult.success() }) + outcome as AgentLoopOutcome.Succeeded + assertEquals(better, outcome.source) + assertEquals("nothing runs from a refused edit", listOf(better), validated) + val repair = chat.conversations[1].last().content + assertTrue(repair.contains("could not be applied")) + assertTrue(repair.contains("is not in the current code")) + assertTrue("the code it was shown is sent again", repair.contains("Hello")) + } + + @Test + fun editsThatNeverApplyEndTheTurnWithoutACandidate() { + val chat = FakeChat(listOf(edit("Tried.", "nowhere" to "x"))) + var runs = 0 + val outcome = AgentLoop(chat).run(turn(good), { _, _ -> runs += 1; ValidationResult.success() }) + assertTrue(outcome is AgentLoopOutcome.Exhausted) + assertEquals(0, runs) + assertEquals(AiConversationLimits.MAX_ATTEMPTS_PER_TURN, chat.conversations.size) + } + + @Test + fun editBlocksForANewWidgetAreRefused() { + val chat = FakeChat(listOf(edit("Tried.", "a" to "b"), "SUMMARY: Made it.\n```jsx\n$good```")) + val outcome = AgentLoop(chat).run(turn(null), { _, _ -> ValidationResult.success() }) + assertEquals(good, (outcome as AgentLoopOutcome.Succeeded).source) + } + + @Test + fun aRepairWrittenAsEditsChangesTheFailedAttempt() { + val broken = "\$render(Hi)\n" + val chat = FakeChat(listOf("SUMMARY: Says Hi now.\n```jsx\n$broken```", edit("Fixed the tag.", "" to ""))) + val outcome = AgentLoop(chat).run(turn(good), { source, _ -> if (source.contains("")) ValidationResult.failure("transformError") else ValidationResult.success() }) + assertEquals("\$render(Hi)\n", (outcome as AgentLoopOutcome.Succeeded).source) + } +} + +/** What changed between versions, and the notice for a conversation that has grown. Mirrors the Swift tests. */ +class AiSourceDiffTest { + @Test + fun theDiffShowsChangedLinesWithContextAndLeavesTheRestOut() { + val old = (1..20).joinToString("\n") { "line $it" } + "\n" + val new = old.replace("line 10\n", "line ten\nline ten and a half\n") + assertEquals(AiSourceDiff.Summary(2, 1), AiSourceDiff.summary(old, new)) + val lines = AiSourceDiff.lines(old, new)!! + val k = AiSourceDiff.Kind.entries + assertEquals( + listOf(k[3], k[0], k[0], k[0], k[2], k[1], k[1], k[0], k[0], k[0], k[3]), + lines.map { it.kind }, + ) + assertEquals(listOf(6, 7), lines.filter { it.kind == AiSourceDiff.Kind.SKIPPED }.map { it.skipped }) + assertEquals(listOf("line ten", "line ten and a half"), lines.filter { it.kind == AiSourceDiff.Kind.ADDED }.map { it.text }) + assertEquals("line 10", lines.first { it.kind == AiSourceDiff.Kind.REMOVED }.text) + assertEquals(emptyList(), AiSourceDiff.lines(old, old)) + assertEquals("line endings are not a change", AiSourceDiff.Summary(0, 0), AiSourceDiff.summary("a\r\nb\n", "a\nb")) + assertNull(AiSourceDiff.summary("x\n".repeat(AiSourceDiff.MAX_LINES + 1), "x")) + } + + @Test + fun usageAdviceAppearsOnlyWhenAConversationHasGrown() { + val conversation = AiConversation(size = AiWidgetSize.LARGE, packageId = "installation-1") + assertNull(conversation.usageAdvice) + assertNull("nothing to continue from", conversation.continuedFromCurrentVersion()) + fun turn(prompt: Int, repairs: Int = 0) { + val turn = conversation.beginTurn("Change it") + val version = conversation.addVersion("\$render(${conversation.versionCounter})\n", listOf("example.com")) + turn.status = AiConversationTurn.Status.SUCCEEDED + turn.versionId = version.id + turn.repairs = repairs + turn.usage = AiTokenUsage(prompt, 100, prompt + 100) + } + turn(5_000) + assertNull(conversation.usageAdvice) + turn(70_000, repairs = 1) + assertEquals(35_000, conversation.lastRequestTokens) + assertEquals(AiConversationUsageAdvice(AiConversationUsageAdvice.Reason.HEAVY_REQUESTS, 35_000), conversation.usageAdvice) + turn(5_000) + assertNull("it follows the newest turn", conversation.usageAdvice) + repeat(AiConversationLimits.HISTORY_TURNS) { turn(5_000) } + assertEquals(AiConversationUsageAdvice.Reason.HISTORY_FULL, conversation.usageAdvice?.reason) + repeat(20) { turn(5_000) } + assertEquals(AiConversationUsageAdvice.Reason.VERSIONS_FULL, conversation.usageAdvice?.reason) + turn(200_000) + assertEquals(AiConversationUsageAdvice.Reason.HEAVY_REQUESTS, conversation.usageAdvice?.reason) + assertTrue(conversation.usageAdvice!!.message.contains("200,000")) + + val next = conversation.continuedFromCurrentVersion()!! + assertFalse(next.id == conversation.id) + assertEquals(listOf(conversation.currentVersion!!.source), next.versions.map { it.source }) + assertEquals(listOf("example.com"), next.currentVersion!!.networkDomains) + assertEquals(AiWidgetSize.LARGE, next.size) + assertEquals("installation-1", next.packageId) + assertTrue(next.turns.isEmpty()) + assertNull(next.usageAdvice) + assertTrue("no earlier turn is sent", next.historyForPrompt().isEmpty()) + } +} diff --git a/Editor/editorfe/package.json b/Editor/editorfe/package.json index 7956005b..578ec395 100644 --- a/Editor/editorfe/package.json +++ b/Editor/editorfe/package.json @@ -6,7 +6,7 @@ "scripts": { "start": "vite", "build": "vite build", - "docs": "node scripts/generate-api-docs.mjs", + "docs": "node scripts/generate-api-docs.mjs && node scripts/generate-jsx-surface.mjs", "test": "node --test", "release": "sh ./release.sh" }, diff --git a/Editor/editorfe/scripts/generate-jsx-surface.mjs b/Editor/editorfe/scripts/generate-jsx-surface.mjs new file mode 100644 index 00000000..8ce8a659 --- /dev/null +++ b/Editor/editorfe/scripts/generate-jsx-surface.mjs @@ -0,0 +1,104 @@ +// Writes the list of JSX tags and attributes that the AI code check uses, as a +// Swift file and a Kotlin file. Run by `npm run docs`. +// +// The names are the union of Studio's API metadata and the web renderer's +// coverage table, so that an attribute either of them knows is never reported. +// The closed lists of values come from the metadata. +// +// The native JSX switch stays the runtime authority. This list only lets the +// apps tell a model "there is no such attribute" before a widget is run, because +// the runtime ignores an unknown attribute without an error. + +import { readFile, writeFile } from "node:fs/promises"; +import { fileURLToPath } from "node:url"; +import { scriptWidgetAPI as api } from "../src/scriptWidgetAPI.js"; +// Every attribute a native builder reads; its own test derives the list from the Swift sources. +import { coverage, generalProps } from "../../../Web/preview-engine/src/render/coverage.js"; + +// Attributes whose values the runtime reads from a closed list and silently +// replaces when they are not in it. Only these have their values checked; an +// attribute such as `font` also takes forms the metadata does not list. +const closedValues = new Set([ + "*.fontWeight", "*.fontDesign", "*.backgroundMaterial", "*.clip", + "Date.style", "Image.mode", "Divider.axis", "Line.axis", "Progress.style", +]); + +// Names the runtime reads that the metadata does not list. +const extraGlobals = { weight: api.globalProperties.fontWeight.values, key: null }; + +// Tags that only one system has: [system, tag, attributes]. +const platformTags = [["android", "list", ["spacing"]]]; + +function entry(owner, name, property) { + const values = closedValues.has(`${owner}.${name}`) ? property?.values : null; + return values?.length ? `${name}=${values.join("|")}` : name; +} + +export function jsxSurfaceText() { + const lines = []; + const globals = new Map(Object.entries(api.globalProperties).map(([name, property]) => [name, entry("*", name, property)])); + for (const name of Object.keys(generalProps)) if (!globals.has(name)) globals.set(name, name); + for (const [name, values] of Object.entries(extraGlobals)) globals.set(name, values ? `${name}=${values.join("|")}` : name); + for (const text of globals.values()) lines.push(`global ${text}`); + + const tags = new Map(); + for (const [name, component] of Object.entries(api.components)) { + tags.set(name.toLowerCase(), new Map(Object.entries(component.properties ?? {}).map(([key, property]) => [key, entry(name, key, property)]))); + } + for (const [name, row] of Object.entries(coverage.tags)) { + if (name !== name.toLowerCase()) continue; // Fragment is not written as a tag + const properties = tags.get(name) ?? new Map(); + for (const key of Object.keys(row.props ?? {})) if (!properties.has(key) && !globals.has(key)) properties.set(key, key); + tags.set(name, properties); + } + for (const [name, properties] of tags) lines.push(["tag", name, ...properties.values()].join(" ")); + for (const [system, name, properties] of platformTags) lines.push([`tag@${system}`, name, ...properties].join(" ")); + return lines.join("\n"); +} + +const note = "Generated by Editor/editorfe/scripts/generate-jsx-surface.mjs from scriptWidgetAPI.js and coverage.js. Do not edit; run `npm run docs` in Editor/editorfe."; + +export const outputs = { + "Shared/ScriptWidgetRuntime/AI/AIJSXSurfaceData.swift": () => `// +// AIJSXSurfaceData.swift +// ScriptWidget +// +// ${note} +// + +enum AIJSXSurfaceData { + /// One line per general attribute ("global") and per tag ("tag"). An + /// attribute with a closed list of values is written name=a|b|c. + static let text = """ +${jsxSurfaceText().split("\n").map((line) => ` ${line}`).join("\n")} + """ +} +`, + "Android/core/src/main/java/com/everettjf/scriptwidget/core/ai/AiJsxSurfaceData.kt": () => `package com.everettjf.scriptwidget.core.ai + +// ${note} + +internal object AiJsxSurfaceData { + /** One line per general attribute ("global") and per tag ("tag"). An attribute with a closed list of values is written name=a|b|c. */ + val TEXT = """ +${jsxSurfaceText()} +""".trim() +} +`, +}; + +export const repositoryRoot = new URL("../../../", import.meta.url); + +if (process.argv[1] === fileURLToPath(import.meta.url)) { + for (const [path, content] of Object.entries(outputs)) await writeFile(new URL(path, repositoryRoot), content()); +} + +// For the test that the checked-in files are current. +export async function staleOutputs() { + const stale = []; + for (const [path, content] of Object.entries(outputs)) { + const current = await readFile(new URL(path, repositoryRoot), "utf8").catch(() => ""); + if (current !== content()) stale.push(path); + } + return stale; +} diff --git a/Editor/editorfe/test/jsxSurface.test.js b/Editor/editorfe/test/jsxSurface.test.js new file mode 100644 index 00000000..190eb4bd --- /dev/null +++ b/Editor/editorfe/test/jsxSurface.test.js @@ -0,0 +1,17 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { jsxSurfaceText, staleOutputs } from "../scripts/generate-jsx-surface.mjs"; + +test("the generated tag and attribute lists are current", async () => { + assert.deepEqual(await staleOutputs(), [], "run `npm run docs` in Editor/editorfe"); +}); + +test("the list names every tag in lower case and only checks values of closed lists", () => { + const lines = jsxSurfaceText().split("\n"); + assert.ok(lines.includes("tag date date style=time|date|relative|offset|timer")); + assert.ok(lines.includes("tag vstack spacing")); + assert.ok(lines.includes("tag@android list spacing")); + assert.ok(lines.includes("global font"), "font takes more forms than the metadata lists"); + assert.ok(lines.some((line) => line.startsWith("global weight="))); + assert.ok(lines.every((line) => /^(global|tag|tag@[a-z]+) /.test(line))); +}); diff --git a/Scripts/ai-conversation-quality.sh b/Scripts/ai-conversation-quality.sh new file mode 100755 index 00000000..5e850e92 --- /dev/null +++ b/Scripts/ai-conversation-quality.sh @@ -0,0 +1,25 @@ +#!/bin/sh +# Opt-in multi-turn AI quality run: several small conversations against a real +# provider, with what each turn changed, missed and cost. It uses provider +# tokens. Pass the key through the environment; never write it into a command. +set -eu +ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +cd "$ROOT" +: "${ROCKY_OPENAI_APIKEY:?Set ROCKY_OPENAI_APIKEY to your provider key}" +: "${ROCKY_OPENAI_BASEURL:?Set ROCKY_OPENAI_BASEURL to your provider base URL}" +: "${ROCKY_OPENAI_MODEL:?Set ROCKY_OPENAI_MODEL to a model returned by your server}" +OUTPUT=${AI_CONVERSATION_QUALITY_OUTPUT:-"$ROOT/__Release/ai-conversation-quality/$(date +%Y%m%d-%H%M%S)"} +case "$OUTPUT" in /*) ;; *) echo 'AI_CONVERSATION_QUALITY_OUTPUT must be an absolute path' >&2; exit 2 ;; esac +export TEST_RUNNER_ROCKY_OPENAI_APIKEY="$ROCKY_OPENAI_APIKEY" +export TEST_RUNNER_ROCKY_OPENAI_BASEURL="$ROCKY_OPENAI_BASEURL" +export TEST_RUNNER_ROCKY_OPENAI_MODEL="$ROCKY_OPENAI_MODEL" +export TEST_RUNNER_AI_CONVERSATION_QUALITY=1 +export TEST_RUNNER_AI_CONVERSATION_QUALITY_OUTPUT="$OUTPUT" +STATUS=0 +xcodebuild -quiet -project macOS/ScriptWidgetMac.xcodeproj -scheme ScriptWidgetRuntimeTests \ + -destination 'platform=macOS' -derivedDataPath "${SCRIPTWIDGET_DERIVED_DATA:-/tmp/scriptwidget-ai-quality}" \ + -only-testing:ScriptWidgetRuntimeTests/AIConversationTests/testConversationQualityAgainstARealProviderWhenConfigured \ + CODE_SIGNING_ALLOWED=NO test || STATUS=$? +[ -f "$OUTPUT/report.md" ] && cat "$OUTPUT/report.md" +printf 'Multi-turn AI quality artifacts: %s\n' "$OUTPUT" +exit "$STATUS" diff --git a/Shared/ScriptWidgetRuntime/AI/AIConversation.swift b/Shared/ScriptWidgetRuntime/AI/AIConversation.swift index fe6508ea..46e7a676 100644 --- a/Shared/ScriptWidgetRuntime/AI/AIConversation.swift +++ b/Shared/ScriptWidgetRuntime/AI/AIConversation.swift @@ -27,6 +27,38 @@ enum AIConversationLimits { /// The version before the current one is sent for reference when it is small. static let previousSourceCharacters = 8_000 static let titleCharacters = 40 + /// From here on the user is told that the conversation has grown heavy. + static let heavyPromptTokens = 30_000 + static let heavyTotalTokens = 300_000 +} + +/// Something the user should know about the size of a conversation, with the +/// way out: continue in a new conversation from the current version. +struct AIConversationUsageAdvice: Equatable { + enum Reason: Equatable { + /// Each request now sends about this many tokens. + case heavyRequests(tokens: Int) + /// The conversation has used this many tokens in all. + case heavyTotal(tokens: Int) + /// The oldest versions are being dropped. + case versionsFull + /// Turns older than the newest ones are no longer sent. + case historyFull + } + let reason: Reason + + var message: String { + switch reason { + case .heavyRequests(let tokens): + return "Each request now sends about \(tokens.formatted()) tokens. A new conversation from this version sends less." + case .heavyTotal(let tokens): + return "This conversation has used \(tokens.formatted()) tokens. A new conversation from this version starts small again." + case .versionsFull: + return "A conversation keeps \(AIConversationLimits.maxVersions) versions; the oldest are dropped from now on." + case .historyFull: + return "The AI is told about the last \(AIConversationLimits.historyTurns) turns only; older requests are no longer sent." + } + } } struct AIConversationVersion: Codable, Identifiable, Equatable { @@ -187,6 +219,41 @@ struct AIConversation: Codable, Identifiable, Equatable { var latestVersion: AIConversationVersion? { versions.max { $0.number < $1.number } } var totalUsage: AITokenUsage { turns.reduce(.zero) { $0 + $1.usage } } + /// What the newest finished turn sent per request, on average over its attempts. + var lastRequestTokens: Int { + guard let turn = turns.last(where: { $0.usage.promptTokens > 0 && $0.status != .running }) else { return 0 } + let attempts = turn.status == .succeeded ? turn.repairs + 1 : max(1, turn.repairs) + return turn.usage.promptTokens / max(1, attempts) + } + + /// The most pressing thing to say about the size of the conversation, if any. + var usageAdvice: AIConversationUsageAdvice? { + if lastRequestTokens >= AIConversationLimits.heavyPromptTokens { + return AIConversationUsageAdvice(reason: .heavyRequests(tokens: lastRequestTokens)) + } + if totalUsage.totalTokens >= AIConversationLimits.heavyTotalTokens { + return AIConversationUsageAdvice(reason: .heavyTotal(tokens: totalUsage.totalTokens)) + } + if versions.count >= AIConversationLimits.maxVersions { + return AIConversationUsageAdvice(reason: .versionsFull) + } + let told = turns.filter { $0.status == .succeeded && !$0.isAbandoned && $0.isManualEdit != true }.count + if told > AIConversationLimits.historyTurns { + return AIConversationUsageAdvice(reason: .historyFull) + } + return nil + } + + /// A new draft that starts from the current version: same size and skills, + /// one version, no turns, and so no history to send. nil without a version. + func continuedFromCurrentVersion(now: Date = Date()) -> AIConversation? { + guard let current = currentVersion else { return nil } + var next = AIConversation(size: size, skillIDs: skillIDs, now: now) + next.title = title + next.addVersion(source: current.source, networkDomains: current.networkDomains ?? [], now: now) + return next + } + func version(_ id: UUID?) -> AIConversationVersion? { guard let id else { return nil } return versions.first { $0.id == id } diff --git a/macOS/ScriptWidgetMac/AIGenerate/AIConversationTurnsView.swift b/Shared/ScriptWidgetRuntime/AI/AIConversationTurnsView.swift similarity index 77% rename from macOS/ScriptWidgetMac/AIGenerate/AIConversationTurnsView.swift rename to Shared/ScriptWidgetRuntime/AI/AIConversationTurnsView.swift index 1fcc044b..d6591d04 100644 --- a/macOS/ScriptWidgetMac/AIGenerate/AIConversationTurnsView.swift +++ b/Shared/ScriptWidgetRuntime/AI/AIConversationTurnsView.swift @@ -1,10 +1,11 @@ // // AIConversationTurnsView.swift -// ScriptWidgetMac +// ScriptWidget // // The turns of an AI conversation, one under the other: the request, what // came of it, the version it made, and what to do when it did not work. -// Used by the AI window and by the editor's Copilot panel. +// Used by the AI window and the editor's Copilot panel on the Mac, and by +// the AI screen on iPhone and iPad. // import SwiftUI @@ -84,7 +85,7 @@ struct AIConversationTurnsView: View { .lineLimit(8, reservesSpace: true) .frame(maxWidth: .infinity, alignment: .leading) .padding(8) - .background(Color(nsColor: .textBackgroundColor).opacity(0.6), in: .rect(cornerRadius: 6)) + .background(Self.codeBackground.opacity(0.6), in: .rect(cornerRadius: 6)) .accessibilityLabel("Code being written") } case .succeeded: @@ -143,10 +144,17 @@ struct AIConversationTurnsView: View { .foregroundStyle(.secondary) } else { Button("Go to This Version") { session.selectVersion(version.id) } + #if os(macOS) .buttonStyle(.link) + #else + .buttonStyle(.borderless) + #endif .font(.caption) .disabled(session.isRunning) } + if let parent = session.conversation.version(version.parentID) { + AIVersionChangesButton(title: "Changes in Version \(version.number)", old: parent.source, new: version.source) + } if turn.isAbandoned { Text("left behind") .font(.caption) @@ -171,8 +179,46 @@ struct AIConversationTurnsView: View { } } + private static var codeBackground: Color { + #if os(macOS) + Color(nsColor: .textBackgroundColor) + #else + Color(uiColor: .secondarySystemGroupedBackground) + #endif + } private static func tail(of text: String, lines: Int) -> String { text.split(separator: "\n", omittingEmptySubsequences: false).suffix(lines).joined(separator: "\n") } } + +/// Shown above the message field when a conversation has grown heavy, with +/// the way out. Nothing is shown while the conversation is small. +struct AIConversationUsageNotice: View { + let conversation: AIConversation + /// Opens the conversation that continues from the current version. + let onContinue: (AIConversation) -> Void + + var body: some View { + if let advice = conversation.usageAdvice { + HStack(alignment: .firstTextBaseline, spacing: 8) { + Image(systemName: "gauge.with.dots.needle.67percent") + .foregroundStyle(.orange) + .accessibilityHidden(true) + Text(advice.message) + .font(.caption) + .foregroundStyle(.secondary) + .fixedSize(horizontal: false, vertical: true) + Spacer(minLength: 4) + if let next = conversation.continuedFromCurrentVersion() { + Button("Start New From Here") { onContinue(next) } + .font(.caption) + .controlSize(.small) + } + } + .padding(.horizontal, 12) + .padding(.vertical, 6) + .accessibilityElement(children: .contain) + } + } +} diff --git a/Shared/ScriptWidgetRuntime/AI/AIJSXCheck.swift b/Shared/ScriptWidgetRuntime/AI/AIJSXCheck.swift new file mode 100644 index 00000000..ac6c50c8 --- /dev/null +++ b/Shared/ScriptWidgetRuntime/AI/AIJSXCheck.swift @@ -0,0 +1,307 @@ +// +// AIJSXCheck.swift +// ScriptWidget +// +// Finds tags and attributes in generated code that ScriptWidget does not +// have. The Kotlin port, AiJsxCheck, must report the same problems for the +// same code. +// +// The runtime ignores an unknown attribute, and a value outside a closed +// list, without an error: the widget runs and shows something else. The +// repair loop can only fix what it is told, so this check tells it. +// +// It reads the code loosely, not as a parser would, and is built to stay +// silent when unsure: a tag whose name starts with a capital letter is taken +// for the script's own component, and anything that is not plainly a tag is +// skipped. It grants nothing and blocks nothing at run time; the runtime's own +// switch stays the authority. +// + +import Foundation + +enum AIJSXCheck { + /// Attribute name to its closed list of values, or nil when any value goes. + struct Surface { + let globals: [String: Set?] + let tags: [String: [String: Set?]] + /// Names in the order of the generated list, for messages. + let globalNames: [String] + let tagNames: [String] + let attributeNames: [String: [String]] + } + + static let maxProblems = 5 + + /// The tags of the Apple systems: the shared ones only. + static let apple = parseSurface(AIJSXSurfaceData.text, system: nil) + + /// Reads the generated list; `system` chooses which system-only tags count. + static func parseSurface(_ text: String, system: String?) -> Surface { + var globals: [String: Set?] = [:] + var tags: [String: [String: Set?]] = [:] + var globalNames: [String] = [] + var tagNames: [String] = [] + var attributeNames: [String: [String]] = [:] + func attribute(_ word: Substring) -> (String, Set?) { + guard let at = word.firstIndex(of: "=") else { return (String(word), nil) } + let values = word[word.index(after: at)...].split(separator: "|").map(String.init) + return (String(word[..= 2 else { continue } + if words[0] == "global" { + let (name, values) = attribute(words[1]) + globals[name] = values + globalNames.append(name) + } else if words[0] == "tag" || (system != nil && words[0] == "tag@\(system ?? "")") { + var own: [String: Set?] = [:] + var names: [String] = [] + for word in words.dropFirst(2) { + let (name, values) = attribute(word) + own[name] = values + names.append(name) + } + let tag = String(words[1]) + tags[tag] = own + tagNames.append(tag) + attributeNames[tag] = names + } + } + return Surface(globals: globals, tags: tags, globalNames: globalNames, tagNames: tagNames, attributeNames: attributeNames) + } + + /// What is wrong with `source`, at most `maxProblems` sentences; empty when nothing is. + static func problems(in source: String, surface: Surface = apple) -> [String] { + var found: [String] = [] + func add(_ text: String) { if !found.contains(text) { found.append(text) } } + for tag in tags(in: source) { + if found.count >= maxProblems { break } + // A capital letter marks the script's own component, even one named like a tag; a dotted name is a member. + guard let first = tag.name.unicodeScalars.first, ("a"..."z").contains(first), !tag.name.contains(".") else { continue } + let name = tag.name + guard let own = surface.tags[name] else { + add("<\(name)> is not a ScriptWidget tag. The tags are: \(surface.tagNames.joined(separator: ", ")).") + continue + } + for attribute in tag.attributes { + if found.count >= maxProblems { break } + let ownValues = own[attribute.name] + let globalValues = surface.globals[attribute.name] + if ownValues == nil && globalValues == nil { + let names = ((surface.attributeNames[name] ?? []) + surface.globalNames).joined(separator: ", ") + add("<\(name)> has no attribute \"\(attribute.name)\"; it would be ignored. Its attributes are: \(names).") + continue + } + // The tag's own list wins over the general one, as in the Kotlin port. + guard let allowed = (ownValues ?? nil) ?? (globalValues ?? nil), let value = attribute.literal else { continue } + if !allowed.contains(value) { + let values = orderedValues(name: name, attribute: attribute.name) + add("\(attribute.name)=\"\(value)\" on <\(name)> is not one of its values; it would be ignored. Use one of: \(values.joined(separator: ", ")).") + } + } + } + return found + } + + /// One text for the model, or nil when `source` has no problem. + static func summary(of source: String, surface: Surface = apple) -> String? { + let problems = problems(in: source, surface: surface) + guard !problems.isEmpty else { return nil } + return "The code uses tags or attributes that ScriptWidget does not have:\n" + problems.map { "- \($0)" }.joined(separator: "\n") + } + + /// The values of a closed list in the order they were generated in. + private static func orderedValues(name: String, attribute: String) -> [String] { + for line in AIJSXSurfaceData.text.split(whereSeparator: \.isNewline) { + let words = line.split(separator: " ") + guard words.count >= 2 else { continue } + let candidates: ArraySlice + if words[0] == "global" { candidates = words[1...] } else if words[1] == name { candidates = words.dropFirst(2) } else { continue } + for word in candidates where word.hasPrefix(attribute + "=") { + return word.dropFirst(attribute.count + 1).split(separator: "|").map(String.init) + } + } + return [] + } + + struct Attribute: Equatable { + let name: String + /// The value when it is written as a plain string. + let literal: String? + } + + struct Tag: Equatable { + let name: String + let attributes: [Attribute] + } + + private static let tagBeforeKeywords: Set = [ + "return", "yield", "case", "default", "await", "else", "do", "in", "of", "typeof", "void", "delete", "new", "throw", + ] + + /// The opening tags of `source`, read loosely. + static func tags(in source: String) -> [Tag] { + let text = Array(source.unicodeScalars) + let length = text.count + var result: [Tag] = [] + var index = 0 + while index < length { + let character = text[index] + if character == "/", index + 1 < length, text[index + 1] == "/" { + while index < length, text[index] != "\n" { index += 1 } + } else if character == "/", index + 1 < length, text[index + 1] == "*" { + index += 2 + while index < length, !(text[index] == "*" && index + 1 < length && text[index + 1] == "/") { index += 1 } + index = min(length, index + 2) + } else if character == "\"" || character == "'" { + index = skipQuoted(text, from: index, sameLineOnly: true) + } else if character == "`" { + index = skipQuoted(text, from: index, sameLineOnly: false) + } else if character == "<", index + 1 < length, isLetter(text[index + 1]), startsTag(text, at: index) { + if let (tag, next) = readTag(text, from: index + 1) { + result.append(tag) + index = next + } else { + index += 1 + } + } else { + index += 1 + } + } + return result + } + + /// Whether the `<` at `at` can open a tag: after an operator or a keyword, not after a value. + private static func startsTag(_ text: [Unicode.Scalar], at: Int) -> Bool { + var before = at - 1 + while before >= 0, isSpace(text[before]) { before -= 1 } + guard before >= 0 else { return true } + let character = text[before] + if character == ")" || character == "]" { return false } + guard isWord(character) else { return true } + var start = before + while start >= 0, isWord(text[start]) { start -= 1 } + return tagBeforeKeywords.contains(string(text, start + 1, before + 1)) + } + + /// Reads a tag whose name starts at `from`; the tag and the index after its `>`, or nil when it is not one. + private static func readTag(_ text: [Unicode.Scalar], from: Int) -> (Tag, Int)? { + let length = text.count + var index = from + while index < length, isLetter(text[index]) || isDigit(text[index]) || text[index] == "." || text[index] == "_" { index += 1 } + let name = string(text, from, index) + var attributes: [Attribute] = [] + while true { + while index < length, isSpace(text[index]) { index += 1 } + guard index < length else { return nil } + let character = text[index] + if character == ">" { return (Tag(name: name, attributes: attributes), index + 1) } + if character == "/" { + guard index + 1 < length, text[index + 1] == ">" else { return nil } + return (Tag(name: name, attributes: attributes), index + 2) + } + if character == "{" { + // {...spread} + guard let end = skipBraces(text, from: index) else { return nil } + index = end + continue + } + guard isLetter(character) || character == "_" else { return nil } + let start = index + while index < length, isLetter(text[index]) || isDigit(text[index]) || text[index] == "_" || text[index] == "-" || text[index] == ":" { index += 1 } + let attributeName = string(text, start, index) + while index < length, isSpace(text[index]) { index += 1 } + guard index < length, text[index] == "=" else { + attributes.append(Attribute(name: attributeName, literal: nil)) + continue + } + index += 1 + while index < length, isSpace(text[index]) { index += 1 } + guard index < length else { return nil } + let opener = text[index] + if opener == "\"" || opener == "'" { + var end = index + 1 + while end < length, text[end] != opener { end += 1 } + guard end < length else { return nil } + attributes.append(Attribute(name: attributeName, literal: string(text, index + 1, end))) + index = end + 1 + } else if opener == "{" { + guard let end = skipBraces(text, from: index) else { return nil } + let inner = string(text, index + 1, end - 1).trimmingCharacters(in: .whitespacesAndNewlines) + let scalars = Array(inner.unicodeScalars) + // {"value"} is as plain as "value". + var literal: String? + if scalars.count >= 2, let quote = scalars.first, quote == "\"" || quote == "'", scalars.last == quote, + !scalars[1..<(scalars.count - 1)].contains(quote), !scalars.contains("\\") { + literal = string(scalars, 1, scalars.count - 1) + } + attributes.append(Attribute(name: attributeName, literal: literal)) + index = end + } else { + return nil + } + } + } + + /// The index after the `}` that closes the `{` at `from`, or nil when it is not closed. + private static func skipBraces(_ text: [Unicode.Scalar], from: Int) -> Int? { + var depth = 0 + var index = from + while index < text.count { + let character = text[index] + if character == "{" { + depth += 1 + index += 1 + } else if character == "}" { + depth -= 1 + index += 1 + if depth == 0 { return index } + } else if character == "\"" || character == "'" { + index = skipQuoted(text, from: index, sameLineOnly: true) + } else if character == "`" { + index = skipQuoted(text, from: index, sameLineOnly: false) + } else { + index += 1 + } + } + return nil + } + + /// The index after the quoted text that starts at `from`. A quote that is + /// not closed on its line, an apostrophe in a sentence for one, is passed + /// over as a single character. + private static func skipQuoted(_ text: [Unicode.Scalar], from: Int, sameLineOnly: Bool) -> Int { + let quote = text[from] + var index = from + 1 + while index < text.count { + let character = text[index] + if character == "\\" { + index += 2 + continue + } + if character == quote { return index + 1 } + if sameLineOnly, character == "\n" { return from + 1 } + index += 1 + } + return from + 1 + } + + private static func isLetter(_ scalar: Unicode.Scalar) -> Bool { + ("a"..."z").contains(scalar) || ("A"..."Z").contains(scalar) + } + + private static func isDigit(_ scalar: Unicode.Scalar) -> Bool { ("0"..."9").contains(scalar) } + + private static func isWord(_ scalar: Unicode.Scalar) -> Bool { + isLetter(scalar) || isDigit(scalar) || scalar == "_" || scalar == "$" + } + + private static func isSpace(_ scalar: Unicode.Scalar) -> Bool { scalar.properties.isWhitespace } + + private static func string(_ text: [Unicode.Scalar], _ from: Int, _ to: Int) -> String { + var view = String.UnicodeScalarView() + view.append(contentsOf: text[from.. [String] { + var lines = text.replacingOccurrences(of: "\r\n", with: "\n").components(separatedBy: "\n") + if lines.last == "" { lines.removeLast() } + return lines + } + + /// How many lines were added and removed; nil when either side is too long to compare. + static func summary(old: String, new: String) -> Summary? { + guard let all = fullLines(old: old, new: new) else { return nil } + return Summary(added: all.filter { $0.kind == .added }.count, removed: all.filter { $0.kind == .removed }.count) + } + + /// The changed lines with a few unchanged ones around them; nil when either side is too long. + static func lines(old: String, new: String, context: Int = contextLines) -> [Line]? { + guard let all = fullLines(old: old, new: new) else { return nil } + guard all.contains(where: { $0.kind != .same }) else { return [] } + // Keep a line when a change is within `context` lines of it. + var keep = [Bool](repeating: false, count: all.count) + for (index, line) in all.enumerated() where line.kind != .same { + for near in max(0, index - context)...min(all.count - 1, index + context) { keep[near] = true } + } + var result: [Line] = [] + var skipped = 0 + func flush() { + if skipped > 0 { result.append(Line(id: result.count, kind: .skipped(count: skipped), text: "")) } + skipped = 0 + } + for (index, line) in all.enumerated() { + if keep[index] { + flush() + result.append(Line(id: result.count, kind: line.kind, text: line.text)) + } else { + skipped += 1 + } + } + flush() + return result + } + + private static func fullLines(old: String, new: String) -> [(kind: Kind, text: String)]? { + let before = split(old), after = split(new) + guard before.count <= maxLines, after.count <= maxLines else { return nil } + let difference = after.difference(from: before) + var removed = Set(), inserted = Set() + for change in difference { + switch change { + case .remove(let offset, _, _): removed.insert(offset) + case .insert(let offset, _, _): inserted.insert(offset) + } + } + var result: [(kind: Kind, text: String)] = [] + var oldIndex = 0, newIndex = 0 + while oldIndex < before.count || newIndex < after.count { + if oldIndex < before.count, removed.contains(oldIndex) { + result.append((.removed, before[oldIndex])) + oldIndex += 1 + } else if newIndex < after.count, inserted.contains(newIndex) { + result.append((.added, after[newIndex])) + newIndex += 1 + } else if oldIndex < before.count, newIndex < after.count { + result.append((.same, after[newIndex])) + oldIndex += 1 + newIndex += 1 + } else { + break + } + } + return result + } +} + +/// The changes of one version against the one it was made from. +struct AISourceDiffView: View { + let old: String + let new: String + + var body: some View { + if let lines = AISourceDiff.lines(old: old, new: new) { + if lines.isEmpty { + Text("The code is the same.") + .foregroundStyle(.secondary) + .padding() + } else { + ScrollView([.vertical, .horizontal]) { + VStack(alignment: .leading, spacing: 0) { + ForEach(lines) { line in row(line) } + } + .padding(.vertical, 8) + } + } + } else { + Text("These versions are too long to compare line by line.") + .foregroundStyle(.secondary) + .padding() + } + } + + @ViewBuilder + private func row(_ line: AISourceDiff.Line) -> some View { + switch line.kind { + case .skipped(let count): + Text("⋯ \(count) unchanged line\(count == 1 ? "" : "s")") + .font(.caption) + .foregroundStyle(.secondary) + .padding(.horizontal, 12) + .padding(.vertical, 4) + case .same: + code(" ", line.text, background: .clear, label: "Unchanged") + case .added: + code("+", line.text, background: Color.green.opacity(0.18), label: "Added") + case .removed: + code("−", line.text, background: Color.red.opacity(0.16), label: "Removed") + } + } + + /// The sign is drawn as well as the color, so that the color is not the only cue. + private func code(_ sign: String, _ text: String, background: Color, label: String) -> some View { + HStack(alignment: .firstTextBaseline, spacing: 6) { + Text(sign) + .foregroundStyle(.secondary) + .frame(width: 12) + Text(text.isEmpty ? " " : text) + .textSelection(.enabled) + } + .font(.system(.footnote, design: .monospaced)) + .padding(.horizontal, 8) + .padding(.vertical, 1) + .frame(maxWidth: .infinity, alignment: .leading) + .background(background) + .accessibilityElement(children: .ignore) + .accessibilityLabel("\(label): \(text)") + } +} + +/// "+3 −2" on a version's line; opens the changes of that version. +struct AIVersionChangesButton: View { + let title: String + let old: String + let new: String + + @State private var isShown = false + + var body: some View { + if let summary = AISourceDiff.summary(old: old, new: new), !summary.isEmpty { + Button { + isShown = true + } label: { + Text("+\(summary.added) −\(summary.removed)") + .font(.caption.monospacedDigit()) + } + #if os(macOS) + .buttonStyle(.link) + #else + .buttonStyle(.borderless) + #endif + .accessibilityLabel("\(summary.added) lines added, \(summary.removed) removed. Show the changes.") + .sheet(isPresented: $isShown) { + VStack(alignment: .leading, spacing: 0) { + HStack { + VStack(alignment: .leading, spacing: 2) { + Text(title).font(.headline) + Text("\(summary.added) line\(summary.added == 1 ? "" : "s") added, \(summary.removed) removed") + .font(.caption) + .foregroundStyle(.secondary) + } + Spacer() + Button("Done") { isShown = false } + .keyboardShortcut(.defaultAction) + } + .padding(12) + Divider() + AISourceDiffView(old: old, new: new) + .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) + } + #if os(macOS) + .frame(minWidth: 560, minHeight: 420) + #endif + } + } + } +} diff --git a/Shared/ScriptWidgetRuntime/AI/AISourceEdits.swift b/Shared/ScriptWidgetRuntime/AI/AISourceEdits.swift new file mode 100644 index 00000000..2d45b6a4 --- /dev/null +++ b/Shared/ScriptWidgetRuntime/AI/AISourceEdits.swift @@ -0,0 +1,121 @@ +// +// AISourceEdits.swift +// ScriptWidget +// +// A reply that changes part of the current code instead of sending all of +// it. The Kotlin port, AiSourceEdits, must read and apply the same replies +// in the same way. +// +// The model writes one block per place: +// +// <<<<<<< SEARCH +// lines copied from the current code +// ======= +// the lines that replace them +// >>>>>>> REPLACE +// +// Nothing is guessed. The searched text has to be in the code exactly once; +// otherwise the whole reply is refused and the model is asked again. What +// comes out is run and checked like any other candidate. +// + +import Foundation + +/// One change to a widget's source: `search` is replaced by `replace`. +struct AISourceEdit: Equatable { + let search: String + let replace: String +} + +enum AISourceEdits { + static let searchMarker = "<<<<<<< SEARCH" + static let dividerMarker = "=======" + static let replaceMarker = ">>>>>>> REPLACE" + static let maxEdits = 20 + + struct Failure: Error, Equatable { + let reason: String + } + + /// Whether `reply` is written as edit blocks. + static func isEditReply(_ reply: String) -> Bool { + lines(reply).contains { $0.trimmingCharacters(in: .whitespaces) == searchMarker } + } + + /// The edits of `reply`, in order; nil when it has no edit block. Throws a + /// `Failure` for a block that is not complete, has nothing to search for, + /// or when there are too many blocks. + static func parse(_ reply: String) throws -> [AISourceEdit]? { + guard isEditReply(reply) else { return nil } + var edits: [AISourceEdit] = [] + var search: [String]? + var replace: [String]? + for line in lines(reply) { + let marker = line.trimmingCharacters(in: .whitespaces) + if marker == searchMarker { + guard search == nil else { throw Failure(reason: "an edit block starts before the one before it ends") } + search = [] + } else if marker == dividerMarker, search != nil, replace == nil { + replace = [] + } else if marker == replaceMarker { + guard let from = search, let to = replace else { + throw Failure(reason: "an edit block ends without its SEARCH part and its ======= line") + } + let text = from.joined(separator: "\n") + guard !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { + throw Failure(reason: "edit \(edits.count + 1) has nothing in its SEARCH part") + } + edits.append(AISourceEdit(search: text, replace: to.joined(separator: "\n"))) + guard edits.count <= maxEdits else { throw Failure(reason: "there are more than \(maxEdits) edit blocks") } + search = nil + replace = nil + } else if replace != nil { + replace?.append(line) + } else if search != nil { + search?.append(line) + } + } + guard search == nil else { throw Failure(reason: "the last edit block has no >>>>>>> REPLACE line") } + return edits + } + + /// `source` with `edits` applied one after the other. The result ends with + /// a line break. Throws a `Failure` when a searched text is not in the + /// code, or is there more than once. + static func apply(_ edits: [AISourceEdit], to source: String) throws -> String { + var text = normalized(source) + for (index, edit) in edits.enumerated() { + let count = occurrences(of: edit.search, in: text) + guard count > 0 else { + throw Failure(reason: "the SEARCH text of edit \(index + 1) is not in the current code; it has to be copied exactly, with its indentation") + } + guard count == 1 else { + throw Failure(reason: "the SEARCH text of edit \(index + 1) is in the current code \(count) times; add neighbouring lines so that it is there once") + } + guard let range = text.range(of: edit.search, options: .literal) else { continue } + text.replaceSubrange(range, with: edit.replace) + } + while let last = text.unicodeScalars.last, last == "\n" || last == " " || last == "\t" { + text.unicodeScalars.removeLast() + } + return text + "\n" + } + + private static func normalized(_ text: String) -> String { + text.replacingOccurrences(of: "\r\n", with: "\n").replacingOccurrences(of: "\r", with: "\n") + } + + private static func lines(_ text: String) -> [String] { + normalized(text).components(separatedBy: "\n") + } + + private static func occurrences(of part: String, in text: String) -> Int { + var count = 0 + var from = text.startIndex + while let range = text.range(of: part, options: .literal, range: from..>>>>>> REPLACE + ``` + Write one SEARCH/REPLACE pair for each place, as many as the change needs. Each SEARCH + text must be in the current code exactly once: copy enough neighbouring lines to make + it so. An empty REPLACE part deletes the lines. Prefer edit blocks for a small change + and the full code when most of the widget changes. Never use both forms in one reply. + """ + /// Splits a turn reply into the model's note, the widget code and the /// network declaration, if any. A reply without the SUMMARY line or without /// a fence is still read as code. diff --git a/Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift b/Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift index d8af0f17..34794cfc 100644 --- a/Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift +++ b/Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift @@ -9,9 +9,11 @@ // import XCTest -#if os(macOS) import SwiftUI +#if os(macOS) import AppKit +#else +import UIKit #endif @testable import ScriptWidget @@ -176,6 +178,269 @@ final class AIConversationTests: XCTestCase { XCTAssertTrue(second.contains("Keep it tidy."), "skills apply to every turn") } + // MARK: - edit replies + + private func edit(_ summary: String, _ pairs: [(String, String)]) -> ConversationProviderStub.Reply { + let blocks = pairs.map { "<<<<<<< SEARCH\n\($0.0)\n=======\n\($0.1)\n>>>>>>> REPLACE" }.joined(separator: "\n") + return .content("SUMMARY: \(summary)\n```edit\n\(blocks)\n```") + } + + func testEditBlocksAreReadAndAppliedInOrder() throws { + let reply = """ + SUMMARY: Two changes. + ```edit + <<<<<<< SEARCH + Hello + ======= + Hi + there + >>>>>>> REPLACE + <<<<<<< SEARCH + const a = 1; + ======= + >>>>>>> REPLACE + ``` + """ + let edits = try XCTUnwrap(AISourceEdits.parse(reply)) + XCTAssertEqual(edits, [ + AISourceEdit(search: " Hello", replace: " Hi\n there"), + AISourceEdit(search: "const a = 1;", replace: ""), + ]) + let source = "const a = 1;\r\n$render(\n Hello\n)" + XCTAssertEqual(try AISourceEdits.apply(edits, to: source), "\n$render(\n Hi\n there\n)\n") + XCTAssertNil(try AISourceEdits.parse("SUMMARY: x\n```jsx\n$render(a)\n```"), "a full reply has no edits") + } + + func testEditThatIsMissingAmbiguousOrMalformedIsRefused() throws { + func reason(_ body: () throws -> Void) -> String { + do { try body() } catch { return (error as? AISourceEdits.Failure)?.reason ?? "other" } + return "none" + } + let source = "a\na\nb\n" + XCTAssertTrue(reason { _ = try AISourceEdits.apply([AISourceEdit(search: "c", replace: "x")], to: source) }.contains("is not in the current code")) + XCTAssertTrue(reason { _ = try AISourceEdits.apply([AISourceEdit(search: "a", replace: "x")], to: source) }.contains("2 times")) + // Nothing is applied when a later edit fails. + XCTAssertTrue(reason { + _ = try AISourceEdits.apply([AISourceEdit(search: "b", replace: "x"), AISourceEdit(search: "nowhere", replace: "y")], to: source) + }.contains("edit 2")) + XCTAssertTrue(reason { _ = try AISourceEdits.parse("<<<<<<< SEARCH\na\n=======\nb") }.contains("no >>>>>>> REPLACE")) + XCTAssertTrue(reason { _ = try AISourceEdits.parse("<<<<<<< SEARCH\n\n=======\nb\n>>>>>>> REPLACE") }.contains("nothing in its SEARCH")) + XCTAssertTrue(reason { _ = try AISourceEdits.parse("<<<<<<< SEARCH\na\n>>>>>>> REPLACE") }.contains("ends without")) + let many = Array(repeating: "<<<<<<< SEARCH\na\n=======\nb\n>>>>>>> REPLACE", count: AISourceEdits.maxEdits + 1).joined(separator: "\n") + XCTAssertTrue(reason { _ = try AISourceEdits.parse(many) }.contains("more than")) + } + + func testTurnPromptOffersEditBlocksOnlyWhenThereIsCodeToChange() { + func prompt(_ current: String?) -> String { + PromptBuilder.userPromptTurn(AITurnContext( + history: [], currentCode: current, previousCode: nil, request: "x", + skillsText: nil, size: .medium, currentDomains: [], runtimeDiagnostic: nil + )) + } + XCTAssertTrue(prompt("CODE").contains(AISourceEdits.searchMarker)) + XCTAssertFalse(prompt(nil).contains(AISourceEdits.searchMarker)) + } + + func testTurnAnsweredWithEditBlocksChangesOnlyThatPlace() async { + ConversationProviderStub.enqueue([ + reply("Made a greeting.", Self.goodWidget), + edit("Says Hi now.", [("Hello", "Hi")]), + ]) + let session = makeSession() + session.send("A greeting card") + await session.settle() + session.send("Say Hi instead") + await session.settle() + + let turn = session.conversation.turns[1] + XCTAssertEqual(turn.status, .succeeded, turn.errorSummary ?? "") + XCTAssertEqual(turn.note, "Says Hi now.") + XCTAssertEqual(turn.repairs, 0) + XCTAssertEqual(session.currentSource, "$render(Hi)\n") + XCTAssertEqual(texts(session.previewElement), ["Hi"]) + } + + func testEditThatDoesNotApplyIsSentBackAndTheFullCodeIsAccepted() async { + ConversationProviderStub.enqueue([ + reply("Made a greeting.", Self.goodWidget), + edit("Says Hi now.", [("Goodbye", "Hi")]), + reply("Says Hi now.", Self.betterWidget), + ]) + let session = makeSession() + session.send("A greeting card") + await session.settle() + session.send("Say Hi instead") + await session.settle() + + let turn = session.conversation.turns[1] + XCTAssertEqual(turn.status, .succeeded, turn.errorSummary ?? "") + XCTAssertEqual(turn.repairs, 1) + XCTAssertEqual(session.currentSource, Self.betterWidget) + let repair = ConversationProviderStub.bodies.last ?? "" + XCTAssertTrue(repair.contains("could not be applied"), "the model is told why") + XCTAssertTrue(repair.contains("is not in the current code")) + } + + func testEditsThatNeverApplyLeaveTheWidgetAsItWas() async { + ConversationProviderStub.enqueue([reply("Made a greeting.", Self.goodWidget)]) + ConversationProviderStub.enqueue(Array(repeating: edit("Tried.", [("nowhere", "x")]), count: 6)) + let session = makeSession() + session.send("A greeting card") + await session.settle() + session.send("Change it") + await session.settle() + + XCTAssertEqual(session.conversation.turns[1].status, .unconverged) + XCTAssertEqual(session.currentSource, Self.goodWidget) + XCTAssertEqual(session.conversation.versions.count, 1) + } + + // MARK: - what changed between versions + + func testDiffShowsChangedLinesWithContextAndLeavesTheRestOut() throws { + let old = (1...20).map { "line \($0)" }.joined(separator: "\n") + "\n" + let new = old.replacingOccurrences(of: "line 10\n", with: "line ten\nline ten and a half\n") + XCTAssertEqual(AISourceDiff.summary(old: old, new: new), AISourceDiff.Summary(added: 2, removed: 1)) + let lines = try XCTUnwrap(AISourceDiff.lines(old: old, new: new)) + XCTAssertEqual(lines.map(\.kind), [ + .skipped(count: 6), .same, .same, .same, .removed, .added, .added, .same, .same, .same, .skipped(count: 7), + ]) + XCTAssertEqual(lines.filter { $0.kind == .added }.map(\.text), ["line ten", "line ten and a half"]) + XCTAssertEqual(lines.first { $0.kind == .removed }?.text, "line 10") + XCTAssertEqual(AISourceDiff.lines(old: old, new: old), []) + XCTAssertEqual(AISourceDiff.summary(old: "a\r\nb\n", new: "a\nb"), AISourceDiff.Summary(added: 0, removed: 0), "line endings are not a change") + let long = String(repeating: "x\n", count: AISourceDiff.maxLines + 1) + XCTAssertNil(AISourceDiff.summary(old: long, new: "x")) + } + + // MARK: - a conversation that has grown heavy + + func testUsageAdviceAppearsOnlyWhenAConversationHasGrown() throws { + var conversation = AIConversation(size: .large, skillIDs: ["visual-polish"]) + XCTAssertNil(conversation.usageAdvice) + XCTAssertNil(conversation.continuedFromCurrentVersion(), "nothing to continue from") + + func turn(_ prompt: Int, repairs: Int = 0) { + let turn = conversation.beginTurn(request: "Change it") + let version = conversation.addVersion(source: "$render(\(conversation.versionCounter))\n", networkDomains: ["example.com"]) + conversation.updateTurn(turn.id) { + $0.status = .succeeded + $0.versionID = version.id + $0.repairs = repairs + $0.usage = AITokenUsage(promptTokens: prompt, completionTokens: 100, totalTokens: prompt + 100) + } + } + turn(5_000) + XCTAssertNil(conversation.usageAdvice) + // Two attempts shared 70,000 tokens: 35,000 each. + turn(70_000, repairs: 1) + XCTAssertEqual(conversation.lastRequestTokens, 35_000) + XCTAssertEqual(conversation.usageAdvice?.reason, .heavyRequests(tokens: 35_000)) + turn(5_000) + XCTAssertNil(conversation.usageAdvice, "it follows the newest turn") + for _ in 0.. is a tag + const title = "a in a string"; + const wide = count + It's {title} + + + {rows.map((row) => )} + + + , + ); + """ + XCTAssertEqual(AIJSXCheck.problems(in: source), []) + XCTAssertNil(AIJSXCheck.summary(of: source)) + } + + func testAttributeOrValueOrTagThatDoesNotExistIsNamed() { + let weekday = AIJSXCheck.problems(in: "$render()") + XCTAssertEqual(weekday, ["style=\"weekday\" on is not one of its values; it would be ignored. Use one of: time, date, relative, offset, timer."]) + + let format = AIJSXCheck.problems(in: "$render()") + XCTAssertTrue(format.first?.hasPrefix(" has no attribute \"format\"; it would be ignored. Its attributes are: date, style, ") == true, format.first ?? "") + + let tag = AIJSXCheck.problems(in: "$render(a)") + XCTAssertTrue(tag.first?.hasPrefix(" is not a ScriptWidget tag. The tags are: vstack, hstack, ") == true, tag.first ?? "") + + XCTAssertTrue(AIJSXCheck.problems(in: "return a").first?.contains("fontWeight=\"huge\"") == true) + XCTAssertEqual(AIJSXCheck.problems(in: "$render(a)"), [], "a value that is computed is not judged") + XCTAssertEqual(AIJSXCheck.problems(in: "$render()").count, 1) + let many = (1...12).map { "a" }.joined() + XCTAssertEqual(AIJSXCheck.problems(in: "$render(\(many))").count, AIJSXCheck.maxProblems) + XCTAssertTrue(AIJSXCheck.problems(in: "$render()").first?.contains(" is not a ScriptWidget tag") == true, + "the scrolling list is a tag on Android only") + XCTAssertEqual(AIJSXCheck.problems(in: "$render()", surface: AIJSXCheck.parseSurface(AIJSXSurfaceData.text, system: "android")), []) + } + + /// Every template and example in the app passes: the check must not cry wolf. + func testEveryBundledWidgetPassesTheCheck() throws { + let bundle = try XCTUnwrap(Bundle.main.url(forResource: "Script", withExtension: "bundle")) + var count = 0 + var wrong: [String] = [] + let files = try XCTUnwrap(FileManager.default.enumerator(at: bundle, includingPropertiesForKeys: nil)) + for case let file as URL in files where file.pathExtension == "jsx" { + count += 1 + let problems = AIJSXCheck.problems(in: try String(contentsOf: file, encoding: .utf8)) + if !problems.isEmpty { wrong.append("\(file.path.replacingOccurrences(of: bundle.path, with: "")): \(problems.joined(separator: " | "))") } + } + XCTAssertGreaterThan(count, 50) + XCTAssertEqual(wrong, []) + } + + func testMadeUpValueIsSentBackAlthoughTheWidgetRuns() async { + let wrong = "$render()\n" + let right = "$render()\n" + ConversationProviderStub.enqueue([reply("Shows the weekday.", wrong), reply("Shows the date.", right)]) + let session = makeSession() + session.send("Show the weekday") + await session.settle() + + let turn = session.conversation.turns[0] + XCTAssertEqual(turn.status, .succeeded, turn.errorSummary ?? "") + XCTAssertEqual(turn.repairs, 1) + XCTAssertEqual(session.currentSource, right) + XCTAssertTrue(ConversationProviderStub.bodies.last?.contains("is not one of its values") == true, "the model is told what does not exist") + } + + func testOnTheLastAttemptAWidgetThatRunsIsKept() async { + let wrong = "$render()\n" + ConversationProviderStub.enqueue(Array(repeating: reply("Shows the weekday.", wrong), count: 6)) + let session = makeSession() + session.send("Show the weekday") + await session.settle() + + let turn = session.conversation.turns[0] + XCTAssertEqual(turn.status, .succeeded, turn.errorSummary ?? "") + XCTAssertEqual(turn.repairs, AIConversationLimits.maxAttemptsPerTurn - 1) + XCTAssertEqual(session.currentSource, wrong) + } + func testChangeThatNeverRunsLeavesTheWidgetAndPreviewAsTheyWere() async { ConversationProviderStub.enqueue([reply("Made a greeting.", Self.goodWidget)]) ConversationProviderStub.enqueue(Array(repeating: reply("Broke it.", Self.brokenWidget), count: 6)) @@ -774,6 +1039,196 @@ final class AIConversationTests: XCTestCase { XCTAssertNotNil(session.previewElement) } + // MARK: - multi-turn quality + + /// One request of a multi-turn case, and what the code has to look like after it. + struct ConversationEvalStep { + let request: String + /// Go back to this version number before sending, as a user would. + var fromVersion: Int? = nil + /// Text the code must hold after the turn, compared without case. + var mustContain: [String] = [] + var mustNotContain: [String] = [] + /// A small request should touch few lines: added plus removed, at most this. + var maxChangedLines: Int? = nil + } + + struct ConversationEvalCase { + let id: String + let size: AIWidgetSize + let steps: [ConversationEvalStep] + } + + /// The cases the live run sends. Every later step is a small change, so + /// that a model that rewrites everything shows in the changed lines. + static let conversationEvalCases: [ConversationEvalCase] = [ + ConversationEvalCase(id: "greeting", size: .medium, steps: [ + ConversationEvalStep(request: "A card that says Hello in large text with today's date under it, on a light background", mustContain: ["Hello"]), + ConversationEvalStep(request: "Change the word Hello to Welcome. Change nothing else.", mustContain: ["Welcome"], mustNotContain: ["Hello"], maxChangedLines: 6), + ConversationEvalStep(request: "Add a line at the bottom that says Have a nice day", mustContain: ["Welcome", "Have a nice day"], maxChangedLines: 12), + ConversationEvalStep(request: "Make the background dark blue and all text white", fromVersion: 1, mustContain: ["Hello"], mustNotContain: ["Have a nice day"]), + ]), + ConversationEvalCase(id: "habits", size: .large, steps: [ + ConversationEvalStep(request: "A habit tracker with three rows: Water 6 of 8, Reading 20 of 30 and Steps 4200 of 8000. Each row has its name, the numbers and a progress bar. Use fixed sample data.", mustContain: ["Water", "Reading", "Steps"]), + ConversationEvalStep(request: "Rename Reading to Study. Change nothing else.", mustContain: ["Study", "Water", "Steps"], mustNotContain: ["Reading"], maxChangedLines: 6), + ConversationEvalStep(request: "Remove the Steps row", mustContain: ["Study", "Water"], mustNotContain: ["Steps"]), + ConversationEvalStep(request: "Add a title at the top that says Today", mustContain: ["Today", "Study", "Water"], maxChangedLines: 12), + ]), + ConversationEvalCase(id: "clock", size: .small, steps: [ + ConversationEvalStep(request: "The current time in large text and the date under it in small text, centered, on a black background with white text", mustContain: [" AIConversationSession, output: URL? = nil + ) async throws -> [[String: Any]] { + var records: [[String: Any]] = [] + for evalCase in cases { + let session = makeSession() + session.setSize(evalCase.size) + for (index, step) in evalCase.steps.enumerated() { + if let number = step.fromVersion, let version = session.conversation.versions.first(where: { $0.number == number }) { + session.selectVersion(version.id) + } + let before = session.currentSource + let started = Date() + session.send(step.request) + await session.settle() + let turn = try XCTUnwrap(session.conversation.turns.last) + let after = turn.status == .succeeded ? session.currentSource : (turn.failedSource ?? "") + let changes = before.isEmpty ? nil : AISourceDiff.summary(old: before, new: after) + let changed = changes.map { $0.added + $0.removed } + let missing = turn.status == .succeeded + ? step.mustContain.filter { !after.localizedCaseInsensitiveContains($0) } : step.mustContain + let unwanted = step.mustNotContain.filter { after.localizedCaseInsensitiveContains($0) } + let tooMany = turn.status == .succeeded && step.maxChangedLines != nil && (changed ?? 0) > (step.maxChangedLines ?? 0) + var record: [String: Any] = [ + "case": evalCase.id, "turn": index + 1, "request": step.request, + "status": turn.status.rawValue, "repairs": turn.repairs, + "promptTokens": turn.usage.promptTokens, "completionTokens": turn.usage.completionTokens, + "seconds": (Date().timeIntervalSince(started) * 10).rounded() / 10, + "lines": AISourceDiff.split(after).count, + "missing": missing, "unwanted": unwanted, "tooManyChangedLines": tooMany, + "passed": turn.status == .succeeded && missing.isEmpty && unwanted.isEmpty && !tooMany, + "note": turn.note ?? "", "error": turn.errorSummary ?? "", + ] + if let changes { record["added"] = changes.added; record["removed"] = changes.removed } + if let limit = step.maxChangedLines { record["maxChangedLines"] = limit } + records.append(record) + if let output { + try after.write(to: output.appendingPathComponent("\(evalCase.id)-turn-\(index + 1).jsx"), atomically: true, encoding: .utf8) + } + } + } + if let output { + let data = try JSONSerialization.data(withJSONObject: ["turns": records], options: [.prettyPrinted, .sortedKeys]) + try data.write(to: output.appendingPathComponent("conversation-quality.json")) + try Self.conversationEvalReport(records).write(to: output.appendingPathComponent("report.md"), atomically: true, encoding: .utf8) + } + return records + } + + /// The records as a page a person reads. + static func conversationEvalReport(_ records: [[String: Any]]) -> String { + func number(_ record: [String: Any], _ key: String) -> Int { record[key] as? Int ?? 0 } + let passed = records.filter { $0["passed"] as? Bool == true }.count + let later = records.filter { number($0, "turn") > 1 && $0["status"] as? String == "succeeded" } + let changed = later.map { number($0, "added") + number($0, "removed") } + let share = later.map { Double(number($0, "added") + number($0, "removed")) / Double(max(1, number($0, "lines"))) } + var lines = [ + "# Multi-turn AI quality", "", + "- Turns passed: \(passed) of \(records.count)", + "- Repairs: \(records.map { number($0, "repairs") }.reduce(0, +))", + "- Tokens: \(records.map { number($0, "promptTokens") }.reduce(0, +)) sent, \(records.map { number($0, "completionTokens") }.reduce(0, +)) received", + "- Changed lines per later turn: \(changed.isEmpty ? 0 : changed.reduce(0, +) / changed.count) on average; \(share.isEmpty ? 0 : Int((share.reduce(0, +) / Double(share.count) * 100).rounded()))% of the widget's lines", + "", "| Case | Turn | Result | Repairs | +/− lines | Sent | Received | Seconds | Problem |", "| --- | --- | --- | --- | --- | --- | --- | --- | --- |", + ] + for record in records { + var problems: [String] = [] + if let error = record["error"] as? String, !error.isEmpty, record["status"] as? String != "succeeded" { problems.append(String(error.prefix(80))) } + if let missing = record["missing"] as? [String], !missing.isEmpty { problems.append("missing: " + missing.joined(separator: ", ")) } + if let unwanted = record["unwanted"] as? [String], !unwanted.isEmpty { problems.append("still there: " + unwanted.joined(separator: ", ")) } + if record["tooManyChangedLines"] as? Bool == true { problems.append("changed more than \(number(record, "maxChangedLines")) lines") } + let diff = record["added"] == nil ? "new" : "+\(number(record, "added")) −\(number(record, "removed"))" + lines.append("| \(record["case"] ?? "") | \(number(record, "turn")) | \(record["passed"] as? Bool == true ? "passed" : (record["status"] as? String ?? "")) | \(number(record, "repairs")) | \(diff) | \(number(record, "promptTokens")) | \(number(record, "completionTokens")) | \(record["seconds"] ?? "") | \(problems.joined(separator: "; ").replacingOccurrences(of: "|", with: "/").replacingOccurrences(of: "\n", with: " ")) |") + } + return lines.joined(separator: "\n") + "\n" + } + + /// The harness itself, with scripted replies: no key, no network. + func testConversationEvalRecordsWhatEachTurnChangedAndMissed() async throws { + let first = "$render(\n \n Hello\n Today\n \n)\n" + ConversationProviderStub.enqueue([ + reply("Made a greeting.", first), + edit("Says Welcome.", [(" Hello", " Welcome")]), + // Asked for one more line, rewrote everything and forgot it. + reply("Rewrote it.", "$render(\n \n Welcome\n \n Now\n \n)\n"), + ]) + let output = directory.appendingPathComponent("eval", isDirectory: true) + try FileManager.default.createDirectory(at: output, withIntermediateDirectories: true) + let records = try await runConversationEval([ + ConversationEvalCase(id: "scripted", size: .medium, steps: [ + ConversationEvalStep(request: "A greeting", mustContain: ["Hello"]), + ConversationEvalStep(request: "Say Welcome", mustContain: ["Welcome"], mustNotContain: ["Hello"], maxChangedLines: 2), + ConversationEvalStep(request: "Add a line that says Bye", mustContain: ["Bye"], maxChangedLines: 2), + ]), + ], makeSession: { self.makeSession() }, output: output) + + XCTAssertEqual(records.map { $0["passed"] as? Bool }, [true, true, false]) + XCTAssertNil(records[0]["added"], "a new widget has nothing to compare with") + XCTAssertEqual(records[1]["added"] as? Int, 1) + XCTAssertEqual(records[1]["removed"] as? Int, 1) + XCTAssertEqual(records[2]["missing"] as? [String], ["Bye"]) + XCTAssertEqual(records[2]["tooManyChangedLines"] as? Bool, true) + let report = try String(contentsOf: output.appendingPathComponent("report.md"), encoding: .utf8) + XCTAssertTrue(report.contains("Turns passed: 2 of 3")) + XCTAssertTrue(report.contains("missing: Bye; changed more than 2 lines")) + XCTAssertTrue(FileManager.default.fileExists(atPath: output.appendingPathComponent("scripted-turn-2.jsx").path)) + XCTAssertFalse(try String(contentsOf: output.appendingPathComponent("conversation-quality.json"), encoding: .utf8).contains(Self.secret)) + } + + /// The multi-turn cases against a real provider. It costs tokens, so it runs + /// only with AI_CONVERSATION_QUALITY=1 and ROCKY_OPENAI_APIKEY set; see + /// Scripts/ai-conversation-quality.sh. The key is never written. + func testConversationQualityAgainstARealProviderWhenConfigured() async throws { + let environment = ProcessInfo.processInfo.environment + func read(_ name: String) -> String? { + for key in [name, "TEST_RUNNER_\(name)"] { + if let value = environment[key]?.trimmingCharacters(in: .whitespacesAndNewlines), !value.isEmpty { return value } + } + return nil + } + guard read("AI_CONVERSATION_QUALITY") == "1", let key = read("ROCKY_OPENAI_APIKEY") else { + throw XCTSkip("Set AI_CONVERSATION_QUALITY=1 and ROCKY_OPENAI_APIKEY to run the multi-turn quality cases.") + } + let profile = AIProfile( + id: "live-conversation-quality", name: "live", + baseURL: read("ROCKY_OPENAI_BASEURL") ?? AIProfile.defaultBaseURL, + model: read("ROCKY_OPENAI_MODEL") ?? AIProfile.defaultModel, + apiKey: key, authMethod: .apiKey + ) + let settings = AISettings(profile: profile, maxIterations: 20, temperature: 0.2) + let output = read("AI_CONVERSATION_QUALITY_OUTPUT").map { URL(fileURLWithPath: $0, isDirectory: true) } + if let output { try FileManager.default.createDirectory(at: output, withIntermediateDirectories: true) } + let store = try XCTUnwrap(self.store) + let records = try await runConversationEval( + Self.conversationEvalCases, + makeSession: { AIConversationSession(store: store, loadSettings: { settings }, skillsText: { _ in nil }) }, + output: output + ) + print(Self.conversationEvalReport(records)) + let failed = records.filter { $0["passed"] as? Bool != true }.map { "\($0["case"] ?? "") turn \($0["turn"] ?? "")" } + XCTAssertEqual(failed, [], "see report.md") + } + /// One turn that needs the network, against a real provider and a real /// public host (httpbin.org). Runs only with ROCKY_OPENAI_APIKEY set. func testNetworkWidgetAgainstARealProviderWhenConfigured() async throws { @@ -821,20 +1276,21 @@ final class AIConversationTests: XCTestCase { XCTAssertTrue(session.currentSource.contains("httpbin.org")) } - // MARK: - window snapshot (macOS, on request) + // MARK: - pictures of the window and the screen (on request) - #if os(macOS) - /// Writes pictures of the conversation window for review. Runs only when - /// AI_CONVERSATION_SNAPSHOT_DIR names a folder; it asserts nothing about looks. - func testWriteWindowSnapshotsWhenRequested() async throws { + private static func snapshotFolder() throws -> URL { let environment = ProcessInfo.processInfo.environment guard let folder = environment["AI_CONVERSATION_SNAPSHOT_DIR"] ?? environment["TEST_RUNNER_AI_CONVERSATION_SNAPSHOT_DIR"] else { - throw XCTSkip("Set AI_CONVERSATION_SNAPSHOT_DIR to write window pictures") + throw XCTSkip("Set AI_CONVERSATION_SNAPSHOT_DIR to write pictures") } let output = URL(fileURLWithPath: folder, isDirectory: true) try FileManager.default.createDirectory(at: output, withIntermediateDirectories: true) + return output + } + /// Two turns that worked and one that did not, as the pictures show them. + private static func sampleConversation() -> AIConversation { let card = """ const today = new Date().toLocaleDateString("en-US", { weekday: "long", month: "long", day: "numeric" }); @@ -874,8 +1330,15 @@ final class AIConversationTests: XCTestCase { $0.repairs = 3 $0.usage = usage } + return conversation + } - try await snapshot(AIGenerateWindowView(conversation: conversation), to: output.appendingPathComponent("conversation.png")) + #if os(macOS) + /// Writes pictures of the conversation window for review. Runs only when + /// AI_CONVERSATION_SNAPSHOT_DIR names a folder; it asserts nothing about looks. + func testWriteWindowSnapshotsWhenRequested() async throws { + let output = try Self.snapshotFolder() + try await snapshot(AIGenerateWindowView(conversation: Self.sampleConversation()), to: output.appendingPathComponent("conversation.png")) try await snapshot(AIGenerateWindowView(), to: output.appendingPathComponent("empty.png")) } @@ -899,6 +1362,35 @@ final class AIConversationTests: XCTestCase { host.cacheDisplay(in: host.bounds, to: bitmap) try XCTUnwrap(bitmap.representation(using: .png, properties: [:])).write(to: url) } + #else + /// Writes pictures of the conversation screen of the device the tests run + /// on, an iPhone or an iPad. Runs only when AI_CONVERSATION_SNAPSHOT_DIR + /// names a folder; it asserts nothing about looks. + func testWriteScreenSnapshotsWhenRequested() async throws { + let output = try Self.snapshotFolder() + let device = UIDevice.current.userInterfaceIdiom == .pad ? "ipad" : "iphone" + try await snapshot(NavigationStack { AIGenerateView(conversation: Self.sampleConversation()) }, + to: output.appendingPathComponent("\(device)-conversation.png")) + try await snapshot(NavigationStack { AIGenerateView() }, + to: output.appendingPathComponent("\(device)-empty.png")) + } + + private func snapshot(_ view: some View, to url: URL) async throws { + guard let scene = UIApplication.shared.connectedScenes.compactMap({ $0 as? UIWindowScene }).first else { + throw XCTSkip("No window scene to draw in") + } + let window = UIWindow(windowScene: scene) + window.rootViewController = UIHostingController(rootView: view) + window.overrideUserInterfaceStyle = .light + window.makeKeyAndVisible() + defer { window.isHidden = true } + // The preview runs the widget off the main thread; give it time to arrive. + try await Task.sleep(nanoseconds: 3_000_000_000) + let image = UIGraphicsImageRenderer(bounds: window.bounds).image { _ in + window.drawHierarchy(in: window.bounds, afterScreenUpdates: true) + } + try XCTUnwrap(image.pngData()).write(to: url) + } #endif // MARK: - helpers diff --git a/docs/ai-conversation-plan.md b/docs/ai-conversation-plan.md index 0c66c3ec..38558dd6 100644 --- a/docs/ai-conversation-plan.md +++ b/docs/ai-conversation-plan.md @@ -1,6 +1,6 @@ -# AI 对话:后续计划 +# AI 对话:计划与进度 -更新日期:2026-11-19。状态:计划,第三、四阶段都还没有开始。已经做完的部分见 [AI conversation](ai-conversation.md)。 +更新日期:2026-11-19。状态:第三、四阶段已实现,等待合并;下面“还没有做的”是剩余事项。功能说明见 [AI conversation](ai-conversation.md)。 ## 已完成 @@ -11,62 +11,72 @@ | — | 运行时:以 `//` 注释结尾且没有换行的源码可以编译 | #88 | | — | Mac Copilot 面板的界面修正 | #89 | | — | Android 的 AI 页面改成同样的对话 | #90 | +| 3 | 只改局部、属性校验、差异视图、用量提醒、多轮评测(Mac、iPhone、iPad、Android) | 本次 | +| 4 | iPhone 和 iPad 的对话界面 | 本次 | 验证到的程度: -- Mac:用签名的 Debug 版和真实模型走完了生成、自动修复、带联网确认的保存、Copilot、Apply、手动修改记为版本、重启后恢复。 +- Mac(第一、二阶段):用签名的 Debug 版和真实模型走完了生成、自动修复、带联网确认的保存、Copilot、Apply、手动修改记为版本、重启后恢复。 +- 第三阶段的逻辑:Swift 和 Kotlin 各有测试,用脚本模型和真实的运行时。**没有用真实模型跑过**:只改局部的实际命中率、属性校验带来的额外修复次数,要等 `Scripts/ai-conversation-quality.sh` 用真实密钥跑一次才知道。 +- iPhone、iPad:页面用测试渲染成图片检查过布局,会话逻辑与 Mac 共用同一套测试,并在 iPhone 和 iPad 模拟器上各跑了一遍。**没有在模拟器或真机上实际点按过**:键盘弹出、横竖屏、保存流程的交互没有验证。 - Android:JVM 测试和 Pixel 6 上的界面测试通过,界面测试用的是脚本模型。**真实模型在手机上还没有测**,需要所有者自己在 Settings → AI 里填密钥。 -## 实测发现、还没有处理的问题 +## 实测发现的问题 -1. **每轮都重写整个文件。** 一个小改动显示为“22 行有变化”,慢、费 token,也容易改坏没有要求改的地方。 -2. **模型会编造不存在的属性**,例如 ``。运行时不报错,只是显示成别的内容,所以自动修复发现不了。 -3. **Mac 上没有看到逐字输出的文字**,虽然流式请求已经接入。需要确认是提供方没有分段返回,还是界面没有显示。 -4. `LibrarySessionExecutionTests` 里的几个用例在 CI 的慢机器上会超过 5 秒限时,偶发失败。与 AI 无关,但会挡住合并。 +1. **每轮都重写整个文件。** 已处理:见下面 3.1。效果要用真实模型量。 +2. **模型会编造不存在的属性**,例如 ``。已处理:见下面 3.2。 +3. **Mac 上没有看到逐字输出的文字**,虽然流式请求已经接入。未处理:需要一个真实提供方才能确认是提供方没有分段返回,还是界面没有显示。 +4. `LibrarySessionExecutionTests` 里的几个用例在 CI 的慢机器上会超过 5 秒限时,偶发失败。未处理:与 AI 无关,但会挡住合并。 -## 第三阶段:改得更省、更准、看得清 - -建议顺序如下,前一项是后一项的前提。 +## 第三阶段:做了什么 ### 3.1 只改局部 -- 回复格式增加“修改”形式:若干段“原文 → 新文”,由 App 应用到当前版本的源码上。整文件回复继续支持,第一轮和大改动仍然用它。 -- 应用规则放在共享代码里,Swift 和 Kotlin 各一份,行为一致:原文必须在当前源码中恰好出现一次;找不到或出现多次时这一轮按失败处理并进入修复,不猜测。 -- 应用后的结果和整文件回复走同一条路:同样的校验、同样的运行限制、同样的权限。模型输出仍然是不可信内容。 -- 完成标准:评测里的小改动,变化行数明显少于现在,成功率不下降。 +- 有现成代码的轮次,模型可以用 `<<<<<<< SEARCH` / `=======` / `>>>>>>> REPLACE` 块只回复要改的地方;新 Widget 和修复仍然回复整份代码。 +- 每段原文必须在当前代码里恰好出现一次。找不到或出现多次时整条回复作废,把原因发回模型并要求回复整份代码,不猜测。 +- 应用后的代码和整份回复走同一条路:同样的运行、同样的限制和权限。 +- 代码:`AISourceEdits.swift` 和 `AiSourceEdits.kt`,行为一致,测试互相对照。 ### 3.2 属性校验 -- 在运行之前,用编辑器已有的 API 清单(`Editor/editorfe/src/scriptWidgetAPI.js` 对应的数据)检查标签名和属性名,未知的名字作为错误发回模型修复。 -- 只检查名字是否存在,不改变运行时的行为;运行时的 JSX 开关仍然是唯一的依据,清单与它不一致时以运行时为准并修清单。 -- Android 独有的 `` 和 `material.` 颜色要在 Android 的清单里有,否则会被误报。 +- Widget 运行之后,检查代码里的标签名、属性名,以及有固定取值的属性的值。有问题就把“写了什么、实际有什么”发回模型修复;最后一次尝试不拦,能运行的 Widget 保留。 +- 名单由 `npm run docs` 生成:编辑器的 `scriptWidgetAPI.js` 加上网页渲染器的 `coverage.js`(后者的测试保证它覆盖原生代码读取的每个属性)。只有运行时确实按固定名单读取的属性才检查取值。 +- 宁可漏报不误报:首字母大写的标签当作脚本自己的组件,计算出来的值不判断。仓库里所有模板、示例和 Gallery 的 Widget 都通过检查,这一条有测试。 +- Android 独有的 `` 只在 Android 的名单里。`material.` 颜色是取值,不在检查范围内。 +- 代码:`AIJSXCheck.swift` 和 `AiJsxCheck.kt`。 ### 3.3 差异视图 -- 每个版本可以查看相对上一版改了哪几行。Mac 先做,放在对话的版本行里;Android 之后做。 -- 差异在本地计算,不发给提供方。 +- 每个版本显示相对上一版增删了几行(“+3 −2”),点开看具体的行和前后各三行。Mac、iPhone、iPad、Android 都有。 +- 在本机计算,不发给提供方。 ### 3.4 用量提醒 -- 显示这段对话已经用掉的 token。接近上下文上限或单轮花费明显变大时提示,并建议从当前版本开一段新对话。 -- 历史的裁剪规则保持现在的做法:不在当前路径上的轮次不发送。 +- 单次请求约 30,000 token 以上、整段对话累计 300,000 token、版本满 30 个、轮次超过 12 轮时,在输入框上方提示。 +- “Start New From Here” 从当前版本开一段新对话:只带这一个版本,不带历史。 ### 3.5 多轮评测 -- 在现有的 [AI 质量测试](ai-quality-testing.md) 里加入连续几轮修改的题目:生成、改一处、再改一处、回到旧版本再改。 -- 记录每轮的成功与否、修复次数、变化行数和 token,用来比较提示词和模型的改动。 -- 真实提供方的评测只在设置了环境变量时运行,密钥只通过环境变量传入,不写进命令、文件或日志。 +- `Scripts/ai-conversation-quality.sh`:四段小对话(问候卡片、习惯打卡、时钟、中文卡片),共 14 轮,其中一轮先回到旧版本。记录每轮是否通过、修复次数、收发的 token、改动的行数。 +- 同一套流程用脚本回复在常规测试里跑一遍,不需要密钥。 -## 第四阶段:iPhone 和 iPad +## 第四阶段:做了什么 -- 用和 Mac 相同的共享代码(`AIConversation`、`AIConversationStore`、`AIConversationSession`),只写界面。 -- iPhone 参考 Android 的一屏布局:预览在上,对话在中,输入框贴着键盘。iPad 参考 Mac 的左右布局。 -- 现有的单次请求加 Refine 的页面在对话可用后移除。 -- 需要在模拟器和真机上验证,包括键盘、横竖屏和小组件扩展的内存不受影响。 +- “Generate with AI” 在 iPhone 和 iPad 上是同一套对话,用的是 Mac 的共享代码(会话、存储、轮次列表)。 +- iPhone:预览在上,对话在中,输入框贴着键盘,打字时预览缩小。iPad:对话在左,预览和三个尺寸在右。 +- 原来“单次请求加 Refine”的两个页面已删除。 +- 编辑器里的 Copilot 也改成了对话,从 Mac 的面板移植:AI 改的是副本,Apply 才写入文档,新域名先确认。 +- 对话界面的文字加了简体中文(iPhone、iPad)。 -## Android 与 Mac 的差距 +## 还没有做的 -记在 [`docs/android/TODO.md`](android/TODO.md):回复不逐字输出、一次只看一个尺寸、编辑器里没有 Copilot。第三阶段的 3.1、3.2 要同时在 Kotlin 里实现。 +- **用真实模型验证第三阶段**:跑一次 `Scripts/ai-conversation-quality.sh`,看通过率和改动行数。需要密钥,由所有者通过环境变量提供。 +- **iPhone、iPad 上实际操作一遍**:键盘、横竖屏、保存和联网确认。 +- **Android 上真实模型的验证**。 +- **Android 的三项差距**:回复不逐字输出、一次只看一个尺寸、编辑器里没有 Copilot。记在 [`docs/android/TODO.md`](android/TODO.md)。 +- **Mac 和 Android 的对话界面没有中文**:Mac 应用整体还没有本地化,Android 的界面文字写在代码里,都需要先搭本地化的架子。运行时拼出来的几句(进度、用量提醒)在 iPhone 上也还是英文。 +- **iPhone、iPad 编辑器里的 Copilot 面板**只编译和跑了共用的测试,没有渲染成图片看过,也没有点过。 +- 上面“实测发现的问题”里的第 3、4 条。 ## 不变的约束 diff --git a/docs/ai-conversation.md b/docs/ai-conversation.md index ed933b0a..94c8778b 100644 --- a/docs/ai-conversation.md +++ b/docs/ai-conversation.md @@ -1,12 +1,12 @@ # AI conversation -The AI window of the Mac app is a conversation about one widget: describe it, look at the preview, ask for a change, look again. This page says what it does, what it sends to the AI provider and what it keeps on the Mac. Provider setup is in [AI generation](ai-generate.md). +Making a widget with AI is a conversation about one widget: describe it, look at the preview, ask for a change, look again. The Mac app, the iPhone and iPad app and the Android app all have it. This page describes the Mac window first, then what differs elsewhere; it says what the conversation does, what it sends to the AI provider and what it keeps on the device. Provider setup is in [AI generation](ai-generate.md). ![The conversation on the left and the preview on the right; the third turn did not run and the widget stayed at version 2](images/ai-conversation-mac.png) ![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) -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. +iPhone and iPad have the same conversation, see [On iPhone and iPad](#on-iphone-and-ipad), and so does the Android app, see [On Android](#on-android). ## Using it @@ -17,6 +17,8 @@ The Android app has the same conversation on one screen; see [On Android](#on-an 5. If you ask for something while an older version is shown, the conversation continues from that version, and the turns after it are marked "left behind" and are no longer sent. 6. **Save Widget** saves the version on screen as a widget. +A version shows how many lines it added and removed against the one it was made from, as **+3 −2**; a click shows those lines with a little of what is around them. The comparison is made on the device. + While the AI writes, the code appears under the request as it arrives. A provider that can not stream is asked in the plain way instead, and the code appears when it is complete. Under the preview the widget is drawn at the Small, Medium and Large sizes; a click shows that size above. The size picker offers the other sizes. All of this runs on the Mac and costs no tokens. A size larger than the preview area is drawn smaller to fit. @@ -50,6 +52,20 @@ 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 iPhone and iPad + +![The conversation on an iPhone: the preview on top, the turns below and the message field at the bottom](images/ai-conversation-iphone.png) + +**Generate with AI** under New Widget opens the conversation. It uses the same code as the Mac window and follows the rules on this page, with these differences: + +- On an iPhone the preview is on top, the turns are below it and the message field sits above the keyboard. While you type, the preview gets smaller to leave room. On an iPad the conversation is on the left and the preview, with the three sizes under it, on the right. +- **Save** in the top bar asks for a name, and asks again before saving a widget that uses the network. The **…** menu holds a new conversation, the drafts, Skills, the provider, the code, the logs and deleting the conversation. +- Conversations are files in the app's own Application Support folder on the device, as on the Mac. +- The **Copilot** tab of the editor is the conversation about the open widget, with the rules of [In the editor](#in-the-editor): the AI works on a copy, **Apply** writes the version on screen into the document, and a new host is asked about first. It has no Manage Skills entry; Skills are managed in Settings. +- With the device set to Simplified Chinese, the conversation's own texts are in Chinese. The AI's notes are in the language you write in. + +![The conversation on an iPad: the turns on the left, the preview and the three sizes on the right](images/ai-conversation-ipad.png) + ## On Android ![The conversation on a phone: the preview and its versions on top, the turns below](images/ai-conversation-android.png) @@ -57,6 +73,7 @@ The **Copilot** tab of the editor is the conversation about the open widget. A w **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. +- **+1 −1** on a version opens the changed lines, as on the Mac. - 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. @@ -68,12 +85,55 @@ More in the [Android notes](android/README.md#ai-conversation). | What happened | What you see | |---|---| | The code did not run | The AI is told the error and tries again, up to three times. A turn that needed that says "Fixed automatically". | +| The code uses a tag, an attribute or a value that does not exist | The same: the AI is told which one and what exists instead. See [Tags and attributes that do not exist](#tags-and-attributes-that-do-not-exist). | +| The AI sent a change that does not fit the code | The same: the AI is told which part did not fit and sends the whole code. See [Changing a part of the code](#changing-a-part-of-the-code). | | It still did not run | The widget and the preview stay as they were. The turn shows the error, **Try Again** and **Show Its Code**. | | The provider could not be reached or refused | The turn shows the reason and **Retry**. Everything else stays usable. | | You pressed Stop (⌘.) | The request ends at once and the widget stays as it was. | | The app closed during a turn | The turn is shown as interrupted, with **Send Again**. | | No provider is set up | A notice with a button to the AI settings; nothing can be sent. | +## Changing a part of the code + +For a small change the AI may send only the places that change, instead of the whole code: + +``` +<<<<<<< SEARCH +lines copied from the current code +======= +the lines that replace them +>>>>>>> REPLACE +``` + +- The app puts each block into the current version. The searched lines have to be in the code exactly once. If they are missing, or there more than once, nothing is applied: the AI is told which block did not fit and sends the whole code instead. The app never guesses where a change belongs. +- What comes out is run and checked like any other reply, with the same limits and permissions. A change that does not run leaves the widget as it was. +- A new widget, and a repair, are always sent whole. + +This makes a small change quicker and cheaper, and keeps the AI from rewriting parts you did not ask about. The **+3 −2** on a version shows what a turn really changed. + +## Tags and attributes that do not exist + +ScriptWidget ignores an attribute it does not know, and a value outside a fixed list, without an error: `` runs and shows the time. The AI would never learn of its mistake. So after a reply ran, the app looks through its code for: + +- a tag that ScriptWidget does not have; +- an attribute that the tag does not have; +- a value outside a fixed list, for the attributes that have one (`style` of ``, `fontWeight`, `fontDesign`, `clip`, `backgroundMaterial`, `mode` of ``, `axis`, `style` of ``). + +When it finds one, the AI is told what it wrote and what exists, and tries again; this counts as one of the three repairs. On the last attempt a widget that runs is kept as it is. + +The check reads the code loosely and stays silent when unsure. A tag that starts with a capital letter is your own component and is not checked, and a value that is computed is not judged. It grants nothing and blocks nothing when a widget runs: the runtime decides that, as before. Its lists are written by `npm run docs` in `Editor/editorfe` from Studio's API metadata and the web renderer's coverage table, and every template and example in the app passes it. The scrolling `` is a tag on Android only. + +## When a conversation has grown + +A long conversation sends more with every request. Above the message field a line appears when: + +- one request now sends about 30,000 tokens or more; +- the conversation has used 300,000 tokens in all; +- it holds 30 versions, so the oldest are dropped from now on; +- it has more than 12 turns, so the oldest are no longer sent. + +**Start New From Here** opens a new draft with the version on screen as its first version, the same size and Skills, and no earlier turn to send. The old conversation stays among the drafts. + ## Drafts A conversation is saved after every change, without asking. One that has not been saved as a widget is a draft: closing the window keeps it, and the window offers it under **Continue a draft** next time. The **…** menu starts a new conversation, opens another draft or deletes the current one. @@ -104,8 +164,6 @@ The preview package carries a Package 2.0 `widget.json` with no permission, or w In the editor, the copy runs with the widget's own `widget.json` plus the declared hosts, and a legacy widget without `widget.json` runs as a legacy package, as it does when installed. A `widget.json` that is present but can not be read turns the panel off; it is never treated as absent. -The earlier single request on iPhone and iPad still runs its candidate in a package without `widget.json`. - ## Limits | | | @@ -118,7 +176,7 @@ The earlier single request on iPhone and iPad still runs its candidate in a pack ## Not in this version -Targeted edits instead of whole-file replies, a view of what changed between versions, and the iPhone and iPad interface. The [plan](ai-conversation-plan.md) (Chinese) lists them in order. +Code written out as it arrives on Android, an editor panel on Android, and translations of the conversation's own texts on the Mac and on Android (the Mac app has no translation yet; iPhone and iPad have Simplified Chinese). The [plan](ai-conversation-plan.md) (Chinese) says what was done in which order and what is open. ## For contributors @@ -126,8 +184,10 @@ Targeted edits instead of whole-file replies, a view of what changed between ver - `AIConversationStore.swift`: the files. - `AIConversationSession.swift`: the state a view observes; it sends turns through `AgentLoop` in its `turn` mode. - `PromptBuilder.userPromptTurn` and `parseTurnReply`: the request and reply format. `AINetworkDeclaration` reads and checks the declaration. +- `AISourceEdits.swift`: reads and applies edit blocks. `AIJSXCheck.swift` with the generated `AIJSXSurfaceData.swift`: the check for tags and attributes. `AISourceDiff.swift`: the comparison of two versions and its view. The Kotlin ports are `AiSourceEdits`, `AiJsxCheck` and `AiSourceDiff`; a change to one side belongs in the other too. - `AIClient.chatStream`: server-sent events, with the fallback to `chat`. -- `macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift`: the window. +- `macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift`: the window. `iOS/ScriptWidget/View/AIGenerate/AIGenerateView.swift`: the iPhone and iPad screen. Both draw the turns with the shared `AIConversationTurnsView.swift`. - `Tests/ScriptWidgetRuntimeTests/AIConversationTests.swift`: tests against a stub provider. They need no key and make no network request. - `testThreeTurnsAgainstARealProviderWhenConfigured` sends three turns to a real provider when `ROCKY_OPENAI_APIKEY`, `ROCKY_OPENAI_BASEURL` and `ROCKY_OPENAI_MODEL` are set, as in [AI quality testing](ai-quality-testing.md). It costs tokens and is skipped otherwise. `AI_CONVERSATION_LIVE_DIR` names a folder for the three sources and a short report. Pass the key through the environment; never write it into a command or a file of the repository. -- The picture on this page is written by the same test file when `AI_CONVERSATION_SNAPSHOT_DIR` names a folder (prefix it with `TEST_RUNNER_` for `xcodebuild`). It draws the window with a prepared conversation; it does not click through it. +- `Scripts/ai-conversation-quality.sh` runs four small conversations against a real provider and reports what each turn changed, missed and cost; see [AI quality testing](ai-quality-testing.md#several-turns). +- The pictures of the Mac window, the iPhone and the iPad on this page are written by the same test file when `AI_CONVERSATION_SNAPSHOT_DIR` names a folder (prefix it with `TEST_RUNNER_` for `xcodebuild`); the iPhone and iPad ones come from running it on those simulators. They draw the conversation with a prepared one; they do not click through it. diff --git a/docs/ai-quality-testing.md b/docs/ai-quality-testing.md index 28a69864..36067522 100644 --- a/docs/ai-quality-testing.md +++ b/docs/ai-quality-testing.md @@ -19,6 +19,21 @@ Artifacts default to the git-ignored `__Release/ai-quality//`. Overri The default CI suite skips the opt-in live test. Deterministic frame and appearance regressions run in the normal suite without a key. +## Several turns + +`Scripts/ai-conversation-quality.sh` sends four small conversations to a real provider through the same session the apps use: a greeting card, a habit tracker, a clock, and a card asked for in Chinese. The first turn of each makes a widget; every later turn is a small change, and one goes back to an older version first. It takes the same three variables as above and uses provider tokens: 14 turns, plus repairs. + +```sh +ROCKY_OPENAI_APIKEY="$DEEPSEEK_API_KEY" \ +ROCKY_OPENAI_BASEURL=https://api.deepseek.com \ +ROCKY_OPENAI_MODEL=deepseek-flash \ +./Scripts/ai-conversation-quality.sh +``` + +Artifacts go to the git-ignored `__Release/ai-conversation-quality//`, or to an absolute `AI_CONVERSATION_QUALITY_OUTPUT`: the code after every turn, `conversation-quality.json` and `report.md`. A turn passes when it ran, the code holds the texts the request asked for, no longer holds the ones it asked to remove, and a small request changed no more lines than its limit. The report adds repairs, tokens sent and received, and the changed lines of the later turns as a share of the widget, which is the number to watch when the prompt or the model changes: a model that rewrites the whole widget for "rename this" shows there. + +The checks look at the code, not at the picture. A turn that passes can still look wrong; open the saved code in the app to judge that. `testConversationEvalRecordsWhatEachTurnChangedAndMissed` runs the same harness with scripted replies in the normal suite, without a key. + ## Compare without another model call Replay a prior run's JSX through the current runtime. This separates renderer fixes from randomness in generation and does not require credentials: diff --git a/docs/android/README.md b/docs/android/README.md index 14b42acd..fbfb1fea 100644 --- a/docs/android/README.md +++ b/docs/android/README.md @@ -185,6 +185,10 @@ The three bundled examples are under Templates → Android only: Scrolling Check - 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. +- A version shows how many lines it added and removed, and a tap shows them. A small change may arrive as edit blocks, and a tag, attribute or value that does not exist is sent back to the model; both work as described in [AI conversation](../ai-conversation.md#changing-a-part-of-the-code). The check knows `` on Android. +- A conversation that has grown heavy says so above the request field and offers **Start new from here**. + +![The changed lines of a version](../images/ai-conversation-android-changes.png) - 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 diff --git a/docs/android/TODO.md b/docs/android/TODO.md index a304dabe..a1c27cc7 100644 --- a/docs/android/TODO.md +++ b/docs/android/TODO.md @@ -45,7 +45,8 @@ Scrolling lists, wallpaper colors and the automation entry point are done; see [ - 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. +- AI generation and the Studio completions do not know `` and the `material.` colors yet; the reference they use is the iOS one. (The check for tags that do not exist does accept `` on Android.) +- Edit replies, the attribute check, the changed lines and the heavy-conversation notice have not been run with a live model on a device; see `docs/ai-conversation-plan.md`. - 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. - Faster refresh with exact alarms or a foreground service, at a battery and permission cost. diff --git a/docs/android/testing.md b/docs/android/testing.md index c59bd455..2c8a1911 100644 --- a/docs/android/testing.md +++ b/docs/android/testing.md @@ -26,6 +26,8 @@ The app was then split into the `play` and `foss` flavors (14 November 2026; see 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. +Edit replies, the check for tags and attributes that do not exist, the changed lines of a version and the heavy-conversation notice (19 November 2026) were run the same way, on the Pixel 6 with the `.dev` copy: the 519 JVM tests passed, both flavors built, and the app suite passed (66 tests, with the same three kinds skipped). `AiConversationUiTest` now ends with a turn answered as an edit block and opens the changed lines of that version. `AiJsxCheckTest` runs the check over every template, example and Gallery widget in the repository and finds nothing. Again no live model was used on the device, the core device suite was not rerun and 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-changes.png b/docs/images/ai-conversation-android-changes.png new file mode 100644 index 00000000..b0d5b083 Binary files /dev/null and b/docs/images/ai-conversation-android-changes.png differ diff --git a/docs/images/ai-conversation-ipad.png b/docs/images/ai-conversation-ipad.png new file mode 100644 index 00000000..48876252 Binary files /dev/null and b/docs/images/ai-conversation-ipad.png differ diff --git a/docs/images/ai-conversation-iphone.png b/docs/images/ai-conversation-iphone.png new file mode 100644 index 00000000..2031dac2 Binary files /dev/null and b/docs/images/ai-conversation-iphone.png differ diff --git a/docs/images/ai-conversation-mac.png b/docs/images/ai-conversation-mac.png index ba8bb9d4..38290043 100644 Binary files a/docs/images/ai-conversation-mac.png and b/docs/images/ai-conversation-mac.png differ diff --git a/iOS/ScriptWidget.xcodeproj/project.pbxproj b/iOS/ScriptWidget.xcodeproj/project.pbxproj index 9989b7c1..9a7653a7 100644 --- a/iOS/ScriptWidget.xcodeproj/project.pbxproj +++ b/iOS/ScriptWidget.xcodeproj/project.pbxproj @@ -57,6 +57,11 @@ A102000600000000000000B0 /* AgentLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101000600000000000000B0 /* AgentLoop.swift */; }; A102000700000000000000B0 /* AIGenerateSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101000700000000000000B0 /* AIGenerateSession.swift */; }; A102002300000000000000B0 /* AIConversationSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101002300000000000000B0 /* AIConversationSession.swift */; }; + A102002400000000000000B0 /* AIConversationTurnsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101002400000000000000B0 /* AIConversationTurnsView.swift */; }; + A102002500000000000000B0 /* AISourceEdits.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101002500000000000000B0 /* AISourceEdits.swift */; }; + A102002800000000000000B0 /* AISourceDiff.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101002800000000000000B0 /* AISourceDiff.swift */; }; + A102002700000000000000B0 /* AIJSXSurfaceData.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101002700000000000000B0 /* AIJSXSurfaceData.swift */; }; + A102002600000000000000B0 /* AIJSXCheck.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101002600000000000000B0 /* AIJSXCheck.swift */; }; A102002200000000000000B0 /* AIConversationStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101002200000000000000B0 /* AIConversationStore.swift */; }; A102002100000000000000B0 /* AIConversation.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101002100000000000000B0 /* AIConversation.swift */; }; A102000800000000000000B0 /* AIGenerateProgressView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101000800000000000000B0 /* AIGenerateProgressView.swift */; }; @@ -70,7 +75,6 @@ A102001000000000000000B0 /* AIWidgetSkills.swift in Sources */ = {isa = PBXBuildFile; fileRef = A101001000000000000000B0 /* AIWidgetSkills.swift */; }; A104000100000000000000B0 /* SettingAIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A103000100000000000000B0 /* SettingAIView.swift */; }; A104000200000000000000B0 /* AIGenerateView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A103000200000000000000B0 /* AIGenerateView.swift */; }; - A104000300000000000000B0 /* AIReviewView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A103000300000000000000B0 /* AIReviewView.swift */; }; A106000300000000000000B0 /* SwiftOpenAI in Frameworks */ = {isa = PBXBuildFile; productRef = A106000200000000000000B0 /* SwiftOpenAI */; }; A109000300000000000000B0 /* ScriptWidgetGallery.swift in Sources */ = {isa = PBXBuildFile; fileRef = A109000100000000000000B0 /* ScriptWidgetGallery.swift */; }; A109000400000000000000B0 /* ScriptWidgetGalleryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A109000200000000000000B0 /* ScriptWidgetGalleryView.swift */; }; @@ -556,6 +560,11 @@ A101000600000000000000B0 /* AgentLoop.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AgentLoop.swift; sourceTree = ""; }; A101000700000000000000B0 /* AIGenerateSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIGenerateSession.swift; sourceTree = ""; }; A101002300000000000000B0 /* AIConversationSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIConversationSession.swift; sourceTree = ""; }; + A101002400000000000000B0 /* AIConversationTurnsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIConversationTurnsView.swift; sourceTree = ""; }; + A101002500000000000000B0 /* AISourceEdits.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AISourceEdits.swift; sourceTree = ""; }; + A101002800000000000000B0 /* AISourceDiff.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AISourceDiff.swift; sourceTree = ""; }; + A101002700000000000000B0 /* AIJSXSurfaceData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIJSXSurfaceData.swift; sourceTree = ""; }; + A101002600000000000000B0 /* AIJSXCheck.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIJSXCheck.swift; sourceTree = ""; }; A101002200000000000000B0 /* AIConversationStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIConversationStore.swift; sourceTree = ""; }; A101002100000000000000B0 /* AIConversation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIConversation.swift; sourceTree = ""; }; A101000800000000000000B0 /* AIGenerateProgressView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIGenerateProgressView.swift; sourceTree = ""; }; @@ -569,7 +578,6 @@ A101001000000000000000B0 /* AIWidgetSkills.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIWidgetSkills.swift; sourceTree = ""; }; A103000100000000000000B0 /* SettingAIView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingAIView.swift; sourceTree = ""; }; A103000200000000000000B0 /* AIGenerateView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIGenerateView.swift; sourceTree = ""; }; - A103000300000000000000B0 /* AIReviewView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIReviewView.swift; sourceTree = ""; }; A109000100000000000000B0 /* ScriptWidgetGallery.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptWidgetGallery.swift; sourceTree = ""; }; A109000200000000000000B0 /* ScriptWidgetGalleryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptWidgetGalleryView.swift; sourceTree = ""; }; A110000100000000000000B0 /* ScriptWidgetDataSources.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptWidgetDataSources.swift; sourceTree = ""; }; @@ -961,6 +969,11 @@ A101000600000000000000B0 /* AgentLoop.swift */, A101000700000000000000B0 /* AIGenerateSession.swift */, A101002300000000000000B0 /* AIConversationSession.swift */, + A101002400000000000000B0 /* AIConversationTurnsView.swift */, + A101002500000000000000B0 /* AISourceEdits.swift */, + A101002800000000000000B0 /* AISourceDiff.swift */, + A101002700000000000000B0 /* AIJSXSurfaceData.swift */, + A101002600000000000000B0 /* AIJSXCheck.swift */, A101002200000000000000B0 /* AIConversationStore.swift */, A101002100000000000000B0 /* AIConversation.swift */, A101000800000000000000B0 /* AIGenerateProgressView.swift */, @@ -979,7 +992,6 @@ isa = PBXGroup; children = ( A103000200000000000000B0 /* AIGenerateView.swift */, - A103000300000000000000B0 /* AIReviewView.swift */, ); path = AIGenerate; sourceTree = ""; @@ -2097,12 +2109,16 @@ A102000600000000000000B0 /* AgentLoop.swift in Sources */, A102000700000000000000B0 /* AIGenerateSession.swift in Sources */, A102002300000000000000B0 /* AIConversationSession.swift in Sources */, + A102002400000000000000B0 /* AIConversationTurnsView.swift in Sources */, + A102002500000000000000B0 /* AISourceEdits.swift in Sources */, + A102002800000000000000B0 /* AISourceDiff.swift in Sources */, + A102002700000000000000B0 /* AIJSXSurfaceData.swift in Sources */, + A102002600000000000000B0 /* AIJSXCheck.swift in Sources */, A102002200000000000000B0 /* AIConversationStore.swift in Sources */, A102002100000000000000B0 /* AIConversation.swift in Sources */, A102000800000000000000B0 /* AIGenerateProgressView.swift in Sources */, A104000100000000000000B0 /* SettingAIView.swift in Sources */, A104000200000000000000B0 /* AIGenerateView.swift in Sources */, - A104000300000000000000B0 /* AIReviewView.swift in Sources */, F29119702793035E00B860B0 /* ScriptWidgetElementTagZStack.swift in Sources */, D132336528E08C15002C26A2 /* ScriptLiveActivityManager.swift in Sources */, F2B20CC425D2EF5E00B62DAC /* NameAutoImageView.swift in Sources */, diff --git a/iOS/ScriptWidget/Localizations/zh-Hans.lproj/Localizable.strings b/iOS/ScriptWidget/Localizations/zh-Hans.lproj/Localizable.strings index 8fa119ff..16c42c72 100644 --- a/iOS/ScriptWidget/Localizations/zh-Hans.lproj/Localizable.strings +++ b/iOS/ScriptWidget/Localizations/zh-Hans.lproj/Localizable.strings @@ -203,3 +203,82 @@ "Copy Email Details" = "复制邮件详情"; "Please describe your feedback here." = "请在此描述你的反馈。"; + +/* AI conversation */ +"New AI Widget" = "新的 AI 小组件"; +"Save Widget" = "保存小组件"; +"Widget name" = "小组件名称"; +"Choose a name for your new widget." = "给新的小组件起个名字。"; +"Allow this widget to use the network?" = "允许这个小组件联网吗?"; +"Save and Allow" = "保存并允许"; +"Delete this conversation?" = "删除这段对话?"; +"Its versions are removed. A widget you already saved stays in your library." = "对话里的版本会被删除。已经保存的小组件仍在你的库里。"; +"Saving the Widget" = "保存小组件"; +"New Conversation" = "新对话"; +"Drafts" = "草稿"; +"Untitled" = "未命名"; +"Skills for this conversation" = "这段对话使用的技能"; +"Provider" = "服务商"; +"Unnamed" = "未命名"; +"View Code" = "查看代码"; +"Logs" = "日志"; +"Debug Outlines" = "调试边框"; +"Delete This Conversation" = "删除这段对话"; +"Conversation options" = "对话选项"; +"Set up an AI provider first" = "请先设置 AI 服务商"; +"The selected provider has no server, model or key yet. Add them in Settings, under AI. Keys are kept in the Keychain." = "当前服务商还没有填写服务器、模型或密钥。请在“设置”的 AI 里填写。密钥保存在钥匙串中。"; +"Describe a widget, then keep asking for changes." = "先描述一个小组件,再接着说要改什么。"; +"Every change that runs becomes a version you can go back to. Nothing is saved to your library until you tap Save." = "每次能运行的修改都会成为一个可以回去的版本。点“保存”之前,不会写入你的库。"; +"Try an example" = "试试示例"; +"Continue a draft" = "继续草稿"; +"Describe the widget you want…" = "描述你想要的小组件…"; +"Say what to change…" = "说说要改什么…"; +"Message to the AI" = "给 AI 的消息"; +"Stop" = "停止"; +"Send" = "发送"; +"Showing version %lld while the AI works" = "AI 工作期间显示的是版本 %lld"; +"Previous version" = "上一个版本"; +"Next version" = "下一个版本"; +"Version %lld of %lld" = "版本 %lld / %lld"; +"No version yet" = "还没有版本"; +"Building and checking your widget…" = "正在生成并检查小组件…"; +"Your widget appears here as soon as it runs." = "小组件一运行成功就显示在这里。"; +"Attempts of the Last Turn" = "上一轮的各次尝试"; +"Attempt %lld" = "第 %lld 次尝试"; +"Ran" = "运行成功"; +"Done" = "完成"; +"This conversation continues from an earlier one. Say what to change." = "这段对话接着之前的一段继续。说说要改什么。"; +"%lld tokens in this conversation" = "这段对话共用了 %lld 个 token"; +"This version may contact: %@" = "这个版本可能访问:%@"; +"Code from the editor" = "来自编辑器的代码"; +"%lld tokens" = "%lld 个 token"; +"Done." = "完成。"; +"This change did not run, so the widget was left as it was." = "这次修改没能运行,小组件保持原样。"; +"Try Again" = "再试一次"; +"Show Its Code" = "看它的代码"; +"Retry" = "重试"; +"Stopped." = "已停止。"; +"Send Again" = "重新发送"; +"Interrupted when the app closed." = "App 关闭时被中断。"; +"Version %lld" = "版本 %lld"; +"shown now" = "当前显示"; +"Go to This Version" = "回到这个版本"; +"left behind" = "已放弃"; +"Code being written" = "正在写的代码"; +"The code is the same." = "代码没有变化。"; +"These versions are too long to compare line by line." = "这两个版本太长,无法逐行比较。"; +"Start New From Here" = "从这里开新对话"; +"AI Copilot" = "AI Copilot"; +"AI Copilot Unavailable" = "AI Copilot 不可用"; +"Start a New Conversation" = "开始新对话"; +"Say what to change. The AI works on a copy: you see the result here first, and the widget changes only when you apply it." = "说说要改什么。AI 改的是副本:结果先显示在这里,点“应用”后小组件才会改变。"; +"Set up an AI provider in Settings → AI first." = "请先在“设置 → AI”里设置服务商。"; +"This widget is read-only." = "这个小组件是只读的。"; +"The conversation changes the widget's main file. Select it in the file list to continue." = "对话修改的是小组件的主文件。请在文件列表里选中它再继续。"; +"Version %lld is not in the editor yet" = "版本 %lld 还没有写入编辑器"; +"%lld changed lines" = "改动 %lld 行"; +"Show Code" = "查看代码"; +"Apply to Editor" = "应用到编辑器"; +"Allow and Apply" = "允许并应用"; +"Fix Runtime Error" = "修复运行错误"; +"May contact: %@" = "可能访问:%@"; diff --git a/iOS/ScriptWidget/View/AIGenerate/AIGenerateView.swift b/iOS/ScriptWidget/View/AIGenerate/AIGenerateView.swift index 33f49a11..307f908b 100644 --- a/iOS/ScriptWidget/View/AIGenerate/AIGenerateView.swift +++ b/iOS/ScriptWidget/View/AIGenerate/AIGenerateView.swift @@ -2,213 +2,782 @@ // AIGenerateView.swift // ScriptWidget // -// Prompt input → agent loop → review. The session object lives for -// the duration of this view (including nested refines). +// The AI widget conversation for iPhone and iPad. On an iPhone the preview +// is on top, the conversation below it and the message field above the +// keyboard; on an iPad the conversation and the preview are side by side. +// Each turn that runs becomes a version the user can go back to, and the +// conversation is kept as a draft until it is saved as a widget. +// +// It uses the same session, store and turn views as the Mac window. // import SwiftUI +import WidgetKit struct AIGenerateView: View { + /// A new session is made whenever the user starts or opens a conversation. + @State private var conversation: AIConversation + @State private var sessionKey = UUID() + + /// Opens on a new conversation, or on one that was saved earlier. + init(conversation: AIConversation = AIConversation()) { + _conversation = State(initialValue: conversation) + } + + var body: some View { + AIConversationScreen( + conversation: conversation, + onOpen: { opened in + conversation = opened + sessionKey = UUID() + } + ) + .id(sessionKey) + } +} + +private struct AIConversationScreen: View { + let onOpen: (AIConversation) -> Void + @Environment(\.dismiss) private var dismiss - @StateObject private var session = AIGenerateSession() + @Environment(\.horizontalSizeClass) private var horizontalSizeClass + @StateObject private var session: AIConversationSession - @State private var prompt: String = "" - @State private var showReview = false + @State private var draftText = "" + @State private var saveName = "" + @State private var saveError: String? + @State private var shownCode: ShownCode? + @State private var showingLogs = false + @State private var showingSaveName = false + @State private var confirmingNetworkSave = false + @State private var confirmingDelete = false + @State private var isDebugMode = false @State private var profiles: [AIProfile] = [] - @State private var activeProfileID: String = "" - @State private var selectedSkillIDs: Set = [] + @State private var activeProfileID = "" + @State private var isConfigured = true @State private var skills: [AIWidgetSkill] = AIWidgetSkills.builtIns + @State private var drafts: [AIConversationSummary] = [] + @FocusState private var composerFocused: Bool + + private struct ShownCode: Identifiable { + let id = UUID() + let title: String + let code: String + } - private let placeholderPrompt = "e.g. Show the current weather for my location, with a minimalist dark background." + init(conversation: AIConversation, onOpen: @escaping (AIConversation) -> Void) { + self.onOpen = onOpen + _session = StateObject(wrappedValue: AIConversationSession(conversation: conversation)) + } + + private var conversation: AIConversation { session.conversation } + /// Nothing to show yet needs little room; a widget gets more, less while typing. + private var compactStageHeight: CGFloat { + if conversation.currentVersion == nil && !session.isRunning { return 96 } + return composerFocused ? 120 : 210 + } + private var isWide: Bool { horizontalSizeClass == .regular } + private var sortedVersions: [AIConversationVersion] { + conversation.versions.sorted { $0.number < $1.number } + } var body: some View { - ScrollView { - VStack(alignment: .leading, spacing: 16) { - header - - VStack(alignment: .leading, spacing: 6) { - Text("Describe your widget") - .font(.headline) - ZStack(alignment: .topLeading) { - TextEditor(text: $prompt) - .frame(minHeight: 120) - .padding(StudioDesign.compactSpacing) - .background(StudioDesign.cardBackground, in: .rect(cornerRadius: StudioDesign.controlCornerRadius)) - .overlay { - RoundedRectangle(cornerRadius: StudioDesign.controlCornerRadius, style: .continuous) - .stroke(StudioDesign.separator, lineWidth: 0.5) - } - if prompt.isEmpty { - Text(placeholderPrompt) - .foregroundStyle(.secondary) - .padding(.top, 12) - .padding(.leading, 10) - .allowsHitTesting(false) - } + Group { + if isWide { + HStack(spacing: 0) { + chat + .frame(maxWidth: .infinity) + Divider() + ScrollView { + previewPanel(stageHeight: 380, showsThumbnails: true) + .padding(16) } + .frame(width: 400) + .background(StudioDesign.groupedBackground) + } + } else { + VStack(spacing: 0) { + // While typing, the widget stays in view but gives room to the conversation. + previewPanel(stageHeight: compactStageHeight, showsThumbnails: false) + .padding(.horizontal, 12) + .padding(.vertical, 8) + .background(StudioDesign.groupedBackground) + Divider() + chat } + .animation(.easeInOut(duration: 0.2), value: composerFocused) + } + } + .navigationTitle(conversation.title.isEmpty ? "New AI Widget" : conversation.title) + .navigationBarTitleDisplayMode(.inline) + .toolbar { + ToolbarItemGroup(placement: .navigationBarTrailing) { + optionsMenu + Button("Save") { beginSave() } + .fontWeight(.semibold) + .disabled(session.currentSource.isEmpty || session.isRunning) + .accessibilityLabel("Save Widget") + } + } + .onAppear { + reloadSettings() + skills = AIWidgetSkillManager.shared.allSkills() + reloadDrafts() + } + .onDisappear { session.cancel() } + .onReceive(NotificationCenter.default.publisher(for: AISettingsStore.changedNotification)) { _ in + reloadSettings() + } + .onReceive(NotificationCenter.default.publisher(for: AIWidgetSkillManager.changedNotification)) { _ in + skills = AIWidgetSkillManager.shared.allSkills() + } + .sheet(item: $shownCode) { codeSheet($0) } + .sheet(isPresented: $showingLogs) { logsSheet } + .alert("Save Widget", isPresented: $showingSaveName) { + TextField("Widget name", text: $saveName) + .textInputAutocapitalization(.words) + Button("Cancel", role: .cancel) { } + Button("Save") { + if session.currentDomains.isEmpty { performSave() } else { confirmingNetworkSave = true } + } + } message: { + Text("Choose a name for your new widget.") + } + .confirmationDialog( + "Allow this widget to use the network?", + isPresented: $confirmingNetworkSave, + titleVisibility: .visible + ) { + Button("Save and Allow") { performSave() } + Button("Cancel", role: .cancel) { } + } message: { + Text("The AI wrote a widget that contacts: \(session.currentDomains.joined(separator: ", ")). Saving gives it the network permission for these hosts only. You can change this later in the widget's configuration.") + } + .confirmationDialog( + "Delete this conversation?", + isPresented: $confirmingDelete, + titleVisibility: .visible + ) { + Button("Delete", role: .destructive) { + session.cancel() + AIConversationStore.shared.delete(id: conversation.id) + onOpen(AIConversation()) + } + Button("Cancel", role: .cancel) { } + } message: { + Text("Its versions are removed. A widget you already saved stays in your library.") + } + .alert("Saving the Widget", isPresented: saveErrorBinding) { + Button("OK") { saveError = nil } + } message: { + Text(saveError ?? "") + } + } - examplesSection - skillsSection + // MARK: - toolbar - if profiles.count > 1 { - HStack { - Text("Profile") - .font(.subheadline) - Spacer() - Picker("", selection: $activeProfileID) { - ForEach(profiles) { profile in - Text(profile.name.isEmpty ? "Unnamed" : profile.name).tag(profile.id) - } - } - .pickerStyle(.menu) - .labelsHidden() - .onChange(of: activeProfileID) { newValue in - AISettingsStore.shared.setActiveProfile(id: newValue) - } + private var optionsMenu: some View { + Menu { + Button { + onOpen(AIConversation()) + } label: { + Label("New Conversation", systemImage: "plus") + } + .disabled(conversation.turns.isEmpty) + + if !otherDrafts.isEmpty { + Section("Drafts") { + ForEach(otherDrafts) { draft in + Button(draft.title.isEmpty ? "Untitled" : draft.title) { open(draft) } + } + } + } + + Section("Skills for this conversation") { + ForEach(skills) { skill in + Toggle(isOn: skillBinding(skill.id)) { + Label(skill.title, systemImage: skill.symbol) } } + } - Picker("Size", selection: $session.size) { - ForEach(AIWidgetSize.allCases) { size in - Text(size.displayName).tag(size) + if profiles.count > 1 { + Picker("Provider", selection: activeProfileBinding) { + ForEach(profiles) { profile in + Text(profile.name.isEmpty ? "Unnamed" : profile.name).tag(profile.id) } } .pickerStyle(.menu) + .disabled(session.isRunning) + } + Section { Button { - session.start(userDescription: AIWidgetSkills.augment(prompt, selectedIDs: selectedSkillIDs)) + shownCode = ShownCode( + title: "Version \(conversation.currentVersion?.number ?? 0)", + code: session.currentSource + ) } label: { - HStack { - Image(systemName: "sparkles") - Text(session.isRunning ? "Generating..." : "Generate") - .fontWeight(.semibold) - } - .frame(maxWidth: .infinity) - .padding(.vertical, 6) + Label("View Code", systemImage: "curlybraces") } - .buttonStyle(.borderedProminent) - .disabled(session.isRunning || prompt.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) + .disabled(session.currentSource.isEmpty) + Button { + showingLogs = true + } label: { + Label("Logs", systemImage: "text.alignleft") + } + .disabled(session.attemptLogs.isEmpty) + Toggle(isOn: $isDebugMode) { + Label("Debug Outlines", systemImage: "square.dashed") + } + } - if session.isRunning || hasOutcome { - AIGenerateProgressView(session: session) + if !conversation.turns.isEmpty { + Button(role: .destructive) { + confirmingDelete = true + } label: { + Label("Delete This Conversation", systemImage: "trash") } } - .padding() - } - .background(StudioDesign.groupedBackground) - .navigationTitle("Generate") - .navigationBarTitleDisplayMode(.inline) - .onAppear { - loadProfiles() - skills = AIWidgetSkillManager.shared.allSkills() + } label: { + Label("Conversation options", systemImage: "ellipsis.circle") + .labelStyle(.iconOnly) } - .onReceive(NotificationCenter.default.publisher(for: AISettingsStore.changedNotification)) { _ in - loadProfiles() - } - .onReceive(NotificationCenter.default.publisher(for: AIWidgetSkillManager.changedNotification)) { _ in - skills = AIWidgetSkillManager.shared.allSkills() - } - .onChange(of: session.phase) { newPhase in - if case .done = newPhase { - showReview = true - } else if case .exhausted = newPhase { - showReview = true + } + + // MARK: - conversation + + private var chat: some View { + ScrollViewReader { proxy in + ScrollView { + VStack(alignment: .leading, spacing: 14) { + if !isConfigured { + notConfiguredNotice + } else if conversation.turns.isEmpty { + if conversation.currentVersion == nil { + emptyState + } else { + Text("This conversation continues from an earlier one. Say what to change.") + .font(.callout) + .foregroundStyle(.secondary) + } + } + AIConversationTurnsView(session: session, canSend: isConfigured) { title, code in + shownCode = ShownCode(title: title, code: code) + } + if conversation.totalUsage.totalTokens > 0 { + Text("\(conversation.totalUsage.totalTokens) tokens in this conversation") + .font(.caption2) + .foregroundStyle(.tertiary) + .frame(maxWidth: .infinity, alignment: .center) + } + Color.clear.frame(height: 1).id("bottom") + } + .padding(12) + .frame(maxWidth: .infinity, alignment: .leading) + } + .scrollDismissesKeyboard(.interactively) + .onAppear { proxy.scrollTo("bottom", anchor: .bottom) } + .onChange(of: conversation.turns.count) { _ in + withAnimation { proxy.scrollTo("bottom", anchor: .bottom) } + } + .onChange(of: session.activity) { _ in + proxy.scrollTo("bottom", anchor: .bottom) + } + .onChange(of: composerFocused) { focused in + if focused { withAnimation { proxy.scrollTo("bottom", anchor: .bottom) } } } } - .navigationDestination(isPresented: $showReview) { - AIReviewView(session: session) { - dismiss() + .safeAreaInset(edge: .bottom, spacing: 0) { + VStack(spacing: 0) { + AIConversationUsageNotice(conversation: conversation) { next in + session.cancel() + onOpen(next) + } + composer } + .background(.bar) + } + } + + private var notConfiguredNotice: some View { + VStack(alignment: .leading, spacing: 8) { + Label("Set up an AI provider first", systemImage: "exclamationmark.triangle") + .font(.headline) + Text("The selected provider has no server, model or key yet. Add them in Settings, under AI. Keys are kept in the Keychain.") + .font(.callout) + .foregroundStyle(.secondary) } + .studioCard() } - private var header: some View { - HStack(spacing: 10) { - Image(systemName: "sparkles") - .font(.title2) - .foregroundStyle(.tint) - VStack(alignment: .leading, spacing: 2) { - Text("AI Widget Generator") - .font(.title3.weight(.semibold)) - Text("Describe what you want; the AI will iterate until the widget runs.") + private var emptyState: some View { + VStack(alignment: .leading, spacing: 16) { + VStack(alignment: .leading, spacing: 4) { + Text("Describe a widget, then keep asking for changes.") + .font(.headline) + Text("Every change that runs becomes a version you can go back to. Nothing is saved to your library until you tap Save.") .font(.footnote) .foregroundStyle(.secondary) } - Spacer() + VStack(alignment: .leading, spacing: 8) { + Text("Try an example") + .font(.subheadline.weight(.medium)) + .foregroundStyle(.secondary) + ScrollView(.horizontal, showsIndicators: false) { + HStack(spacing: 8) { + ForEach(AIExamplePrompts.all) { example in + Button { + draftText = example.prompt + session.setSize(example.size) + composerFocused = true + } label: { + Label(example.title, systemImage: example.symbol) + .font(.caption) + .padding(.horizontal, 10) + .padding(.vertical, 7) + .background(Color.accentColor.opacity(0.12), in: .capsule) + .foregroundStyle(.tint) + } + .buttonStyle(.plain) + } + } + .padding(.vertical, 2) + } + } + if !drafts.isEmpty { + VStack(alignment: .leading, spacing: 0) { + Text("Continue a draft") + .font(.subheadline.weight(.medium)) + .foregroundStyle(.secondary) + .padding(.bottom, 8) + ForEach(drafts.prefix(5)) { draft in + HStack(spacing: 10) { + Button { open(draft) } label: { + HStack(spacing: 10) { + Image(systemName: "doc.text") + .foregroundStyle(.tint) + VStack(alignment: .leading, spacing: 2) { + Text(draft.title.isEmpty ? "Untitled" : draft.title) + .lineLimit(1) + .foregroundStyle(.primary) + Text("\(draft.versionCount) version\(draft.versionCount == 1 ? "" : "s") · \(draft.updatedAt.formatted(.relative(presentation: .named)))") + .font(.caption) + .foregroundStyle(.secondary) + } + Spacer(minLength: 0) + } + .contentShape(.rect) + } + .buttonStyle(.plain) + Button { + AIConversationStore.shared.delete(id: draft.id) + reloadDrafts() + } label: { + Image(systemName: "trash") + .frame(width: 44, height: 44) + } + .buttonStyle(.borderless) + .accessibilityLabel("Delete draft \(draft.title)") + } + if draft.id != drafts.prefix(5).last?.id { Divider() } + } + } + .studioCard(padding: 12) + } } } - private func loadProfiles() { - profiles = AISettingsStore.shared.loadProfiles() - activeProfileID = AISettingsStore.shared.loadActiveProfileID() + private var composer: some View { + HStack(alignment: .bottom, spacing: 8) { + TextField( + conversation.versions.isEmpty ? "Describe the widget you want…" : "Say what to change…", + text: $draftText, + axis: .vertical + ) + .lineLimit(1...5) + .padding(.horizontal, 12) + .padding(.vertical, 9) + .background(StudioDesign.cardBackground, in: .rect(cornerRadius: 20)) + .overlay { + RoundedRectangle(cornerRadius: 20, style: .continuous) + .stroke(StudioDesign.separator, lineWidth: 0.5) + } + .focused($composerFocused) + .accessibilityLabel("Message to the AI") + + if session.isRunning { + Button { + session.cancel() + } label: { + Image(systemName: "stop.circle.fill") + .font(.system(size: 32)) + } + .accessibilityLabel("Stop") + } else { + Button(action: sendDraft) { + Image(systemName: "arrow.up.circle.fill") + .font(.system(size: 32)) + } + .disabled(!canSend) + .accessibilityLabel("Send") + } + } + .padding(.horizontal, 12) + .padding(.vertical, 8) + } + + private var canSend: Bool { + isConfigured && !session.isRunning + && !draftText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty + } + + private func sendDraft() { + guard canSend else { return } + let text = draftText + draftText = "" + session.send(text) } - private var hasOutcome: Bool { - switch session.phase { - case .idle: return false - default: return true + // MARK: - preview + + private func previewPanel(stageHeight: CGFloat, showsThumbnails: Bool) -> some View { + VStack(alignment: .leading, spacing: 8) { + ZStack(alignment: .top) { + StudioDesign.cardBackground + previewContent + .frame(maxWidth: .infinity, maxHeight: .infinity) + if session.isRunning, session.previewElement != nil { + Label("Showing version \(conversation.currentVersion?.number ?? 0) while the AI works", systemImage: "clock") + .font(.caption) + .padding(.horizontal, 8) + .padding(.vertical, 4) + .background(.regularMaterial, in: .capsule) + .padding(6) + } + } + .frame(maxWidth: .infinity) + .frame(height: stageHeight) + .clipShape(.rect(cornerRadius: StudioDesign.cardCornerRadius)) + .overlay { + RoundedRectangle(cornerRadius: StudioDesign.cardCornerRadius, style: .continuous) + .stroke(StudioDesign.separator, lineWidth: 0.5) + } + + HStack(spacing: 4) { + Menu { + Picker("Size", selection: sizeBinding) { + ForEach(AIWidgetSize.allCases) { size in + Text(size.displayName).tag(size) + } + } + } label: { + Label(conversation.size.displayName, systemImage: "rectangle.3.group") + .font(.subheadline) + } + .accessibilityLabel("Size: \(conversation.size.displayName)") + .accessibilityHint("Runs the widget again on this device. Nothing is sent to the AI.") + Spacer(minLength: 8) + versionStepper + } + + if showsThumbnails, conversation.currentVersion != nil { + thumbnails + } + + if !session.currentDomains.isEmpty { + Label("This version may contact: \(session.currentDomains.joined(separator: ", "))", systemImage: "globe") + .font(.caption) + .foregroundStyle(.secondary) + .lineLimit(2) + } } } - private var examplesSection: some View { - VStack(alignment: .leading, spacing: 6) { - Text("Try an example") - .font(.subheadline.weight(.medium)) + @ViewBuilder + private var versionStepper: some View { + if let current = conversation.currentVersion { + let index = sortedVersions.firstIndex { $0.id == current.id } ?? 0 + HStack(spacing: 2) { + Button { + session.selectVersion(sortedVersions[index - 1].id) + } label: { + Image(systemName: "chevron.left") + .frame(width: 36, height: 32) + } + .disabled(index == 0 || session.isRunning) + .accessibilityLabel("Previous version") + Text("Version \(current.number) of \(sortedVersions.last?.number ?? current.number)") + .font(.subheadline.monospacedDigit()) + Button { + session.selectVersion(sortedVersions[index + 1].id) + } label: { + Image(systemName: "chevron.right") + .frame(width: 36, height: 32) + } + .disabled(index >= sortedVersions.count - 1 || session.isRunning) + .accessibilityLabel("Next version") + } + } else { + Text("No version yet") + .font(.subheadline) .foregroundStyle(.secondary) - ScrollView(.horizontal, showsIndicators: false) { - HStack(spacing: 8) { - ForEach(AIExamplePrompts.all) { example in - Button { - prompt = example.prompt - session.size = example.size - } label: { - HStack(spacing: 6) { - Image(systemName: example.symbol) - Text(example.title) + .frame(height: 32) + } + } + + /// The widget at the three home-screen sizes; a tap shows that size above. + private var thumbnails: some View { + HStack(alignment: .bottom, spacing: 12) { + ForEach(AIConversationSession.thumbnailSizes) { size in + let scale: CGFloat = 0.26 + let full = size.previewSize + Button { + session.setSize(size) + } label: { + VStack(spacing: 4) { + Group { + if let element = session.previews[size], let package = session.previewPackage { + ScriptWidgetElementView( + element: element, + context: ScriptWidgetElementContext( + runtime: nil, debugMode: false, scriptName: "AI Preview", + scriptParameter: "", package: package + ) + ) + .frame(width: full.width, height: full.height) + .background(Color(uiColor: .systemBackground)) + .cornerRadius(18) + .scaleEffect(scale) + .allowsHitTesting(false) + } else { + ProgressView() } - .font(.caption) - .padding(.horizontal, 10) - .padding(.vertical, 6) - .background(Color.accentColor.opacity(0.12)) - .foregroundStyle(.tint) - .clipShape(Capsule()) } - .buttonStyle(.plain) + .frame(width: full.width * scale, height: full.height * scale) + .overlay { + RoundedRectangle(cornerRadius: 5, style: .continuous) + .stroke(size == conversation.size ? Color.accentColor : StudioDesign.separator, + lineWidth: size == conversation.size ? 2 : 1) + } + Text(size.displayName) + .font(.caption2) + .foregroundStyle(size == conversation.size ? .primary : .secondary) } + .contentShape(.rect) } - .padding(.vertical, 2) + .buttonStyle(.plain) + .accessibilityLabel("Show the \(size.displayName) size") } + Spacer(minLength: 0) } } - private var skillsSection: some View { - VStack(alignment: .leading, spacing: 8) { - Text("Skills") - .font(.subheadline.weight(.medium)) - Text("Combine reusable expert instructions with your request.") - .font(.caption) - .foregroundStyle(.secondary) - LazyVGrid(columns: [GridItem(.adaptive(minimum: 145))], alignment: .leading, spacing: 8) { - ForEach(skills) { skill in - Toggle(isOn: skillBinding(skill.id)) { - Label(skill.title, systemImage: skill.symbol) - .font(.caption) + @ViewBuilder + private var previewContent: some View { + let size = conversation.size.previewSize + if let element = session.previewElement, let package = session.previewPackage { + let context = ScriptWidgetElementContext( + runtime: nil, + debugMode: isDebugMode, + scriptName: "AI Preview", + scriptParameter: "", + package: package + ) + // A size larger than the space is drawn smaller, never cut off. + GeometryReader { proxy in + let scale = min(1, (proxy.size.width - 20) / size.width, (proxy.size.height - 20) / size.height) + ScriptWidgetElementView(element: element, context: context) + .frame(width: size.width, height: size.height) + .background(Color(uiColor: .systemBackground)) + .cornerRadius(conversation.size.previewIsCircular ? size.height / 2 : 22) + .scaleEffect(max(0.1, scale)) + .frame(width: proxy.size.width, height: proxy.size.height) + } + .accessibilityLabel("Widget preview, version \(conversation.currentVersion?.number ?? 0)") + } else if session.isRunning { + VStack(spacing: StudioDesign.compactSpacing) { + ProgressView() + Text("Building and checking your widget…") + .font(.footnote) + .foregroundStyle(.secondary) + } + } else { + VStack(spacing: 6) { + Image(systemName: "sparkles.rectangle.stack") + .font(.title3) + .foregroundStyle(.secondary) + Text("Your widget appears here as soon as it runs.") + .font(.footnote) + .foregroundStyle(.secondary) + .multilineTextAlignment(.center) + } + .padding() + .accessibilityElement(children: .combine) + } + } + + // MARK: - sheets + + private func codeSheet(_ shown: ShownCode) -> some View { + NavigationStack { + ScrollView { + Text(shown.code) + .font(.system(.footnote, design: .monospaced)) + .frame(maxWidth: .infinity, alignment: .leading) + .padding() + .textSelection(.enabled) + } + .navigationTitle(shown.title) + .navigationBarTitleDisplayMode(.inline) + .toolbar { + ToolbarItem(placement: .navigationBarTrailing) { + Button("Done") { shownCode = nil } + } + } + } + } + + private var logsSheet: some View { + NavigationStack { + List { + ForEach(session.attemptLogs) { record in + Section("Attempt \(record.attempt)") { + if let error = record.errorSummary { + Label(error, systemImage: "exclamationmark.triangle") + .foregroundStyle(.orange) + } else { + Label("Ran", systemImage: "checkmark.circle") + .foregroundStyle(.green) + } + ForEach(Array(record.lines.enumerated()), id: \.offset) { _, line in + Text(line) + .font(.caption.monospaced()) + .textSelection(.enabled) + } } - .toggleStyle(.button) - .accessibilityHint(skill.summary) + } + } + .navigationTitle("Attempts of the Last Turn") + .navigationBarTitleDisplayMode(.inline) + .toolbar { + ToolbarItem(placement: .navigationBarTrailing) { + Button("Done") { showingLogs = false } } } } } + // MARK: - helpers + + private var saveErrorBinding: Binding { + Binding(get: { saveError != nil }, set: { if !$0 { saveError = nil } }) + } + + private var sizeBinding: Binding { + Binding(get: { conversation.size }, set: { session.setSize($0) }) + } + + private var activeProfileBinding: Binding { + Binding(get: { activeProfileID }, set: { newValue in + activeProfileID = newValue + AISettingsStore.shared.setActiveProfile(id: newValue) + }) + } + private func skillBinding(_ id: String) -> Binding { Binding( - get: { selectedSkillIDs.contains(id) }, + get: { conversation.skillIDs.contains(id) }, set: { enabled in - if enabled { selectedSkillIDs.insert(id) } - else { selectedSkillIDs.remove(id) } + var ids = Set(conversation.skillIDs) + if enabled { ids.insert(id) } else { ids.remove(id) } + session.setSkills(ids) } ) } + + private var otherDrafts: [AIConversationSummary] { + Array(drafts.filter { $0.id != conversation.id }.prefix(8)) + } + + private func open(_ draft: AIConversationSummary) { + guard let loaded = AIConversationStore.shared.load(id: draft.id) else { + reloadDrafts() + return + } + session.cancel() + onOpen(loaded) + } + + private func reloadDrafts() { + drafts = AIConversationStore.shared.summaries(draftsOnly: true) + } + + private func reloadSettings() { + profiles = AISettingsStore.shared.loadProfiles() + activeProfileID = AISettingsStore.shared.loadActiveProfileID() + isConfigured = AISettingsStore.shared.load().isConfigured + } + + private func beginSave() { + composerFocused = false + if saveName.trimmingCharacters(in: .whitespaces).isEmpty { + saveName = "AI " + Self.defaultNameFormatter.string(from: Date()) + } + showingSaveName = true + } + + private static let defaultNameFormatter: DateFormatter = { + let formatter = DateFormatter() + formatter.dateFormat = "yyyy-MM-dd HHmm" + return formatter + }() + + private func performSave() { + let trimmed = saveName.trimmingCharacters(in: .whitespacesAndNewlines) + guard !trimmed.isEmpty else { + saveError = "Widget name can not be empty." + return + } + // Resolve the name first so that the package just created can be found again. + let packageName = sharedScriptManager.getValidPackageName(recommendPackageName: trimmed) + guard !packageName.isEmpty else { + saveError = "This name can not be used for a widget, or the widget library is not available." + return + } + let result = sharedScriptManager.createScript( + content: session.currentSource, + recommendPackageName: packageName, + imageCopyPath: nil + ) + guard result.0 else { + saveError = result.1 + return + } + var warning: String? + let domains = session.currentDomains + if !domains.isEmpty { + // The declaration came from the model; writeManifest validates it again. + let package = sharedScriptManager.getScriptPackage(packageName: packageName) + if var manifest = package.readManifest() { + manifest.permissions = [.network] + manifest.networkDomains = domains + let written = package.writeManifest(manifest) + if !written.0 { warning = written.1 } + } else { + warning = "The widget's configuration could not be read." + } + } + session.markSaved( + packageName: packageName, + packageID: sharedScriptManager.getScriptPackage(packageName: packageName).readManifest()?.id + ) + NotificationCenter.default.post(name: ScriptWidgetHomeViewDataObject.scriptCreateNotification, object: nil) + WidgetCenter.shared.reloadAllTimelines() + if let warning { + saveError = "The widget was saved, but its network permission could not be added: \(warning) Add it in the widget's configuration." + } else { + dismiss() + } + } } diff --git a/iOS/ScriptWidget/View/AIGenerate/AIReviewView.swift b/iOS/ScriptWidget/View/AIGenerate/AIReviewView.swift deleted file mode 100644 index b3d4e0aa..00000000 --- a/iOS/ScriptWidget/View/AIGenerate/AIReviewView.swift +++ /dev/null @@ -1,300 +0,0 @@ -// -// AIReviewView.swift -// ScriptWidget -// -// Shows the generated widget, lets the user refine it, inspect the -// code / logs, discard, or save into the real Scripts library. -// - -import SwiftUI -import WidgetKit - -struct AIReviewView: View { - @ObservedObject var session: AIGenerateSession - /// Called when the user successfully saves (so the parent sheet can close). - var onSaved: () -> Void - - @Environment(\.dismiss) private var dismiss - - @State private var refineInstruction: String = "" - @State private var showingCodeSheet = false - @State private var showingLogsSheet = false - @State private var showingSaveNamePrompt = false - @State private var saveName: String = "" - @State private var saveError: String? - @State private var isDebugMode = false - @State private var previewPackage: ScriptWidgetPackage? - - private var jsx: String { session.lastJSX ?? "" } - private var isExhausted: Bool { - if case .exhausted = session.phase { return true } - return false - } - - var body: some View { - ScrollView { - VStack(alignment: .leading, spacing: 16) { - banner - previewSection - actionsSection - refineSection - - if session.isRunning { - AIGenerateProgressView(session: session) - } - } - .padding() - } - .background(StudioDesign.groupedBackground) - .navigationTitle("Review") - .navigationBarTitleDisplayMode(.inline) - .toolbar { - ToolbarItem(placement: .navigationBarTrailing) { - menuToolbar - } - } - .sheet(isPresented: $showingCodeSheet) { codeSheet } - .sheet(isPresented: $showingLogsSheet) { logsSheet } - .alert("Save Widget", isPresented: $showingSaveNamePrompt) { - TextField("Widget name", text: $saveName) - .textInputAutocapitalization(.words) - Button("Cancel", role: .cancel) { } - Button("Save") { performSave() } - } message: { - Text("Choose a name for your new widget.") - } - .alert("Save Failed", isPresented: saveErrorBinding) { - Button("OK") { saveError = nil } - } message: { - Text(saveError ?? "") - } - .onAppear(perform: ensurePreviewPackage) - .onChange(of: jsx) { _ in - refreshPreviewPackage() - } - } - - // MARK: - sub-sections - - @ViewBuilder private var banner: some View { - if isExhausted { - HStack(spacing: 8) { - Image(systemName: "exclamationmark.triangle.fill") - Text("Did not fully converge — showing the last attempt.") - .font(.footnote) - } - .foregroundStyle(.orange) - } - } - - @ViewBuilder private var previewSection: some View { - let size = session.size.previewSize - ZStack { - StudioDesign.cardBackground - previewContent(size: size) - } - .frame(maxWidth: .infinity) - .frame(height: Swift.max(size.height + 40, 200)) - .clipShape(.rect(cornerRadius: StudioDesign.cardCornerRadius)) - .overlay { - RoundedRectangle(cornerRadius: StudioDesign.cardCornerRadius, style: .continuous) - .stroke(StudioDesign.separator, lineWidth: 0.5) - } - } - - @ViewBuilder - private func previewContent(size: CGSize) -> some View { - if let element = session.resultElement, let pkg = previewPackage { - let context = ScriptWidgetElementContext( - runtime: nil, - debugMode: isDebugMode, - scriptName: "AI Preview", - scriptParameter: "", - package: pkg - ) - ScriptWidgetElementView(element: element, context: context) - .frame(width: size.width, height: size.height) - .background(Color(UIColor.systemBackground)) - .cornerRadius(session.size.previewIsCircular ? size.height / 2 : 10) - } else { - VStack(spacing: StudioDesign.compactSpacing) { - Image(systemName: "rectangle.slash") - .font(.title2) - .foregroundStyle(.secondary) - Text("Preview Unavailable") - .font(.headline) - Text("Refine the request or inspect the iteration logs for details.") - .font(.footnote) - .foregroundStyle(.secondary) - .multilineTextAlignment(.center) - } - .padding() - .accessibilityElement(children: .combine) - } - } - - private var actionsSection: some View { - HStack(spacing: 12) { - Button(role: .destructive) { - dismiss() - } label: { - Label("Discard", systemImage: "trash") - .frame(maxWidth: .infinity) - } - .buttonStyle(.bordered) - - Toggle(isOn: $isDebugMode) { - Text("Debug") - .font(.caption) - } - .toggleStyle(.button) - .controlSize(.small) - - Button { - saveName = "AI " + AIReviewView.defaultNameFormatter.string(from: Date()) - showingSaveNamePrompt = true - } label: { - Label("Save Widget", systemImage: "square.and.arrow.down") - .frame(maxWidth: .infinity) - } - .buttonStyle(.borderedProminent) - .disabled(jsx.isEmpty) - } - } - - private var refineSection: some View { - VStack(alignment: .leading, spacing: 6) { - Text("Refine") - .font(.headline) - Text("Ask the AI to change something — it'll iterate again.") - .font(.caption) - .foregroundStyle(.secondary) - HStack { - TextField("e.g. use a darker background and larger title", text: $refineInstruction) - .textFieldStyle(.roundedBorder) - Button { - let instruction = refineInstruction - refineInstruction = "" - session.refine(currentCode: jsx, refineInstruction: instruction) - } label: { - Image(systemName: "arrow.right.circle.fill") - .font(.title3) - } - .disabled(jsx.isEmpty || refineInstruction.trimmingCharacters(in: .whitespaces).isEmpty || session.isRunning) - } - } - } - - @ViewBuilder private var menuToolbar: some View { - Menu { - Button { showingCodeSheet = true } label: { - Label("View Code", systemImage: "curlybraces") - } - Button { showingLogsSheet = true } label: { - Label("Logs", systemImage: "text.alignleft") - } - } label: { - Label("More", systemImage: "ellipsis.circle") - .labelStyle(.iconOnly) - } - .accessibilityLabel("Review options") - } - - @ViewBuilder private var codeSheet: some View { - NavigationStack { - ScrollView { - Text(jsx) - .font(.system(.footnote, design: .monospaced)) - .frame(maxWidth: .infinity, alignment: .leading) - .padding() - .textSelection(.enabled) - } - .navigationTitle("Generated JSX") - .navigationBarTitleDisplayMode(.inline) - .toolbar { - ToolbarItem(placement: .navigationBarTrailing) { - Button("Done") { showingCodeSheet = false } - } - } - } - } - - @ViewBuilder private var logsSheet: some View { - NavigationStack { - List { - ForEach(session.iterationHistory) { record in - Section("Iteration \(record.iteration)") { - if let err = record.errorSummary { - Label(err, systemImage: "exclamationmark.triangle") - .foregroundStyle(.orange) - } else { - Label("Success", systemImage: "checkmark.circle") - .foregroundStyle(.green) - } - if !record.logs.isEmpty { - ForEach(Array(record.logs.enumerated()), id: \.offset) { _, line in - Text(line) - .font(.caption.monospaced()) - .textSelection(.enabled) - } - } - } - } - } - .navigationTitle("Iteration Logs") - .navigationBarTitleDisplayMode(.inline) - .toolbar { - ToolbarItem(placement: .navigationBarTrailing) { - Button("Done") { showingLogsSheet = false } - } - } - } - } - - // MARK: - helpers - - private var saveErrorBinding: Binding { - Binding( - get: { saveError != nil }, - set: { if !$0 { saveError = nil } } - ) - } - - private func ensurePreviewPackage() { - if previewPackage == nil { - previewPackage = try? AgentRuntimeBridge.shared.makeSandboxPackage(prefix: "preview") - } - refreshPreviewPackage() - } - - private func refreshPreviewPackage() { - guard let pkg = previewPackage else { return } - _ = pkg.writeMainFile(content: jsx) - } - - private func performSave() { - let trimmed = saveName.trimmingCharacters(in: .whitespacesAndNewlines) - guard !trimmed.isEmpty else { - saveError = "Widget name can not be empty." - return - } - let result = sharedScriptManager.createScript( - content: jsx, - recommendPackageName: trimmed, - imageCopyPath: nil - ) - if result.0 { - NotificationCenter.default.post(name: ScriptWidgetHomeViewDataObject.scriptCreateNotification, object: nil) - WidgetCenter.shared.reloadAllTimelines() - onSaved() - } else { - saveError = result.1 - } - } - - private static let defaultNameFormatter: DateFormatter = { - let f = DateFormatter() - f.dateFormat = "yyyy-MM-dd HHmm" - return f - }() -} diff --git a/iOS/ScriptWidget/View/CodeEditor/ScriptCodeEditorView.swift b/iOS/ScriptWidget/View/CodeEditor/ScriptCodeEditorView.swift index a774f8bf..ea9a5195 100644 --- a/iOS/ScriptWidget/View/CodeEditor/ScriptCodeEditorView.swift +++ b/iOS/ScriptWidget/View/CodeEditor/ScriptCodeEditorView.swift @@ -277,179 +277,370 @@ private struct ScriptCodeStudioPanelView: View { } } +/// The editor's Copilot panel: the AI conversation about this widget. It is +/// the same conversation the widget was made in, when it was made by the AI. +/// +/// The AI works on a copy. Its versions change the small preview here and +/// nothing else, until "Apply to Editor" writes the version on screen into +/// the open document through the editor's own conflict check. private struct ScriptCodeCopilotView: View { let scriptModel: ScriptModel let editorID: UUID let documentID: String? - @State private var proposalSnapshot: StudioDocumentSnapshot? - @State private var requestID = UUID() - @State private var isApplying = false - @State private var isReading = false - @StateObject private var session = AIGenerateSession() - @State private var instruction = "" - @State private var originalCode = "" - @State private var proposedCode = "" + @State private var conversation: AIConversation? + @State private var sessionKey = UUID() + + var body: some View { + let package = scriptModel.package + let manifest = package.readManifest() + Group { + if package.hasManifest && manifest == nil { + // A widget.json that can not be read is never treated as absent. + unavailable("This widget's widget.json can not be read, so the AI can not work on it. Fix it in the widget's configuration first.") + } else { + StudioCopilotConversationView( + scriptModel: scriptModel, + manifest: manifest, + editorID: editorID, + documentID: documentID, + conversation: conversation ?? Self.conversation(for: package, manifest: manifest), + onStartOver: { + conversation = Self.newConversation(for: package, manifest: manifest) + sessionKey = UUID() + } + ) + .id(sessionKey) + } + } + .id(package.path) + } + + private func unavailable(_ text: String) -> some View { + VStack(spacing: 8) { + Image(systemName: "sparkles").font(.title2).foregroundStyle(.secondary) + Text("AI Copilot Unavailable").font(.headline) + Text(text).font(.callout).foregroundStyle(.secondary).multilineTextAlignment(.center) + } + .padding() + .frame(maxWidth: .infinity, maxHeight: .infinity) + } + + private static func conversation(for package: ScriptWidgetPackage, manifest: WidgetPackageManifest?) -> AIConversation { + AIConversationStore.shared.latest(packageID: manifest?.id, packageName: package.name) + ?? newConversation(for: package, manifest: manifest) + } + + private static func newConversation(for package: ScriptWidgetPackage, manifest: WidgetPackageManifest?) -> AIConversation { + var conversation = AIConversation() + conversation.title = package.name + conversation.savedPackageName = package.name + conversation.savedPackageID = manifest?.id + return conversation + } +} + +private struct StudioCopilotConversationView: View { + let scriptModel: ScriptModel + let manifest: WidgetPackageManifest? + let editorID: UUID + let documentID: String? + let onStartOver: () -> Void + + @StateObject private var session: AIConversationSession + @State private var draftText = "" @State private var statusMessage: String? - @State private var selectedSkillIDs: Set = [] + /// What the editor held the last time this panel looked. + @State private var knownEditorContent: String? + @State private var isReading = false + @State private var isApplying = false + @State private var requestID = UUID() + @State private var shownCode: ShownCode? + @State private var confirmingNetwork = false + @State private var isConfigured = true @State private var skills: [AIWidgetSkill] = AIWidgetSkills.builtIns - @State private var showingOriginal = false - @State private var showingProposal = true - private var changeSummary: AICopilotChangeSummary { - .compare(original: originalCode, proposed: proposedCode) + private struct ShownCode: Identifiable { + let id = UUID() + let title: String + let code: String } - var body: some View { - ScrollView { - VStack(alignment: .leading, spacing: 14) { - Text("Ask Studio for a focused change. The current code and latest runtime diagnostic are included, then the proposal is validated locally.") - .font(.callout) - .foregroundStyle(.secondary) + init(scriptModel: ScriptModel, manifest: WidgetPackageManifest?, editorID: UUID, documentID: String?, + conversation: AIConversation, onStartOver: @escaping () -> Void) { + self.scriptModel = scriptModel + self.manifest = manifest + self.editorID = editorID + self.documentID = documentID + self.onStartOver = onStartOver + _session = StateObject(wrappedValue: AIConversationSession( + conversation: conversation, + sandbox: AgentSandboxSetup(seedDirectory: scriptModel.package.path, baseManifest: manifest) + )) + } - TextEditor(text: $instruction) - .frame(minHeight: 100) - .padding(4) - .background(StudioDesign.cardBackground, in: .rect(cornerRadius: StudioDesign.controlCornerRadius)) - .accessibilityLabel("Copilot instruction") + private var package: ScriptWidgetPackage { scriptModel.package } + private var conversation: AIConversation { session.conversation } - Text("Skills") - .font(.headline) - LazyVGrid(columns: [GridItem(.adaptive(minimum: 145))], alignment: .leading, spacing: 8) { - ForEach(skills) { skill in - Toggle(isOn: skillBinding(skill.id)) { - Label(skill.title, systemImage: skill.symbol) - .font(.caption) - } - .toggleStyle(.button) - .accessibilityHint(skill.summary) - } - } + /// The conversation changes the widget's entry file and no other. + private var isEntryDocument: Bool { + let entry = manifest?.entry ?? "main.jsx" + return documentID != nil + && documentID == package.resolvedPackageURL(relativePath: entry)?.standardizedFileURL.path + } - HStack { - Button("Propose Change", systemImage: "wand.and.stars") { - beginCopilotRequest(fixCurrentError: false) - } - .buttonStyle(.borderedProminent) + private var canWork: Bool { isConfigured && isEntryDocument && !package.readonly } - Button("Fix Error", systemImage: "wrench.and.screwdriver") { - beginCopilotRequest(fixCurrentError: true) - } - } - .disabled(session.isRunning || isReading || isApplying || scriptModel.package.readonly) + /// The version on screen is not what the editor holds. + private var hasUnappliedVersion: Bool { + guard let known = knownEditorContent, !session.isRunning, !session.currentSource.isEmpty else { return false } + return session.currentSource != known + } - if session.isRunning { - AIGenerateProgressView(session: session) - Button("Cancel", role: .cancel) { session.cancel() } - } + /// Hosts the version declares that the widget may not contact yet. + private var hostsToAllow: [String] { + guard let manifest else { return [] } + return session.currentDomains.filter { !manifest.networkDomains.contains($0) } + } - if let statusMessage { - Text(statusMessage) - .font(.caption) - .foregroundStyle(.secondary) + var body: some View { + VStack(spacing: 0) { + ScrollViewReader { proxy in + ScrollView { + VStack(alignment: .leading, spacing: 12) { + header + notices + if conversation.currentVersion != nil { preview } + AIConversationTurnsView(session: session, canSend: canWork) { title, code in + shownCode = ShownCode(title: title, code: code) + } + Color.clear.frame(height: 1).id("bottom") + } + .padding(12) + .frame(maxWidth: .infinity, alignment: .leading) } - - if !proposedCode.isEmpty { - proposalReview + .onAppear { proxy.scrollTo("bottom", anchor: .bottom) } + .onChange(of: conversation.turns.count) { _ in + withAnimation { proxy.scrollTo("bottom", anchor: .bottom) } } + .onChange(of: session.activity) { _ in proxy.scrollTo("bottom", anchor: .bottom) } } - .padding() + if hasUnappliedVersion { + Divider() + applyBar + } + Divider() + AIConversationUsageNotice(conversation: conversation) { _ in + session.cancel() + onStartOver() + } + composer + } + .onAppear { + isConfigured = AISettingsStore.shared.load().isConfigured + skills = AIWidgetSkillManager.shared.allSkills() + lookAtEditor() } - .onReceive(session.$phase) { phase in - consume(phase: phase) + .onDisappear { + requestID = UUID() + session.cancel() + } + .onChange(of: documentID) { _ in lookAtEditor() } + .onReceive(NotificationCenter.default.publisher(for: AISettingsStore.changedNotification)) { _ in + isConfigured = AISettingsStore.shared.load().isConfigured } - .onChange(of: documentID) { _ in resetProposal() } - .onDisappear { resetProposal() } - .onAppear { skills = AIWidgetSkillManager.shared.allSkills() } .onReceive(NotificationCenter.default.publisher(for: AIWidgetSkillManager.changedNotification)) { _ in skills = AIWidgetSkillManager.shared.allSkills() } + .sheet(item: $shownCode) { shown in + NavigationStack { + ScrollView { + Text(shown.code) + .font(.system(.footnote, design: .monospaced)) + .frame(maxWidth: .infinity, alignment: .leading) + .padding() + .textSelection(.enabled) + } + .navigationTitle(shown.title) + .navigationBarTitleDisplayMode(.inline) + .toolbar { + ToolbarItem(placement: .navigationBarTrailing) { + Button("Done") { shownCode = nil } + } + } + } + } + .confirmationDialog("Allow this widget to use the network?", isPresented: $confirmingNetwork, titleVisibility: .visible) { + Button("Allow and Apply") { apply(allowingHosts: true) } + Button("Cancel", role: .cancel) { } + } message: { + Text("This version contacts: \(hostsToAllow.joined(separator: ", ")). Applying it adds the network permission for these hosts to the widget. You can change this in the widget's configuration.") + } } - private var proposalReview: some View { - VStack(alignment: .leading, spacing: 10) { - HStack { - Text("Review Proposal").font(.headline) - Spacer() - Text("\(changeSummary.changedLines) changed lines") - .font(.caption.monospacedDigit()) - .foregroundStyle(.secondary) - } - DisclosureGroup("Original · \(changeSummary.originalLines) lines", isExpanded: $showingOriginal) { - codeBlock(originalCode) - } - DisclosureGroup("Proposed · \(changeSummary.proposedLines) lines", isExpanded: $showingProposal) { - codeBlock(proposedCode) - } - HStack { - Button("Reject", role: .destructive) { - proposedCode = "" - statusMessage = "Proposal rejected; the editor was not changed." + // MARK: - parts + + private var header: some View { + HStack(alignment: .firstTextBaseline) { + Label("AI Copilot", systemImage: "sparkles") + .font(.headline) + Spacer() + Menu { + Section("Skills for this conversation") { + ForEach(skills) { skill in + Toggle(isOn: skillBinding(skill.id)) { Label(skill.title, systemImage: skill.symbol) } + } } - Spacer() - Button("Apply", systemImage: "checkmark") { - applyProposal() + Button("Start a New Conversation", systemImage: "plus") { + session.cancel() + onStartOver() } - .buttonStyle(.borderedProminent) + .disabled(conversation.turns.isEmpty) + } label: { + Label("Copilot options", systemImage: "ellipsis.circle") + .labelStyle(.iconOnly) } - .disabled(isApplying || proposalSnapshot == nil) } } - private func codeBlock(_ code: String) -> some View { - ScrollView(.horizontal) { - Text(code) - .font(.caption.monospaced()) + @ViewBuilder + private var notices: some View { + if conversation.turns.isEmpty { + Text("Say what to change. The AI works on a copy: you see the result here first, and the widget changes only when you apply it.") + .font(.caption) + .foregroundStyle(.secondary) + } + if !isConfigured { + Label("Set up an AI provider in Settings → AI first.", systemImage: "exclamationmark.triangle") + .font(.caption) + .foregroundStyle(.orange) + } else if package.readonly { + Label("This widget is read-only.", systemImage: "lock") + .font(.caption) + .foregroundStyle(.secondary) + } else if !isEntryDocument { + Label("The conversation changes the widget's main file. Select it in the file list to continue.", systemImage: "doc") + .font(.caption) + .foregroundStyle(.secondary) + } + if let statusMessage { + Text(statusMessage) + .font(.caption) + .foregroundStyle(.secondary) .textSelection(.enabled) - .frame(maxWidth: .infinity, alignment: .leading) - .padding(8) } - .frame(maxHeight: 190) - .background(StudioDesign.cardBackground, in: .rect(cornerRadius: StudioDesign.controlCornerRadius)) } - private func resetProposal() { - requestID = UUID() - proposalSnapshot = nil - proposedCode = "" - isApplying = false - isReading = false - statusMessage = nil - session.cancel() + /// The version on screen, drawn small. It is the AI's copy, not the editor's document. + private var preview: some View { + let size = conversation.size.previewSize + let scale = min(1, 150 / size.height, 300 / size.width) + return VStack(alignment: .leading, spacing: 4) { + Group { + if let element = session.previewElement, let previewPackage = session.previewPackage { + ScriptWidgetElementView( + element: element, + context: ScriptWidgetElementContext( + runtime: nil, debugMode: false, scriptName: scriptModel.name, + scriptParameter: "", package: previewPackage + ) + ) + .frame(width: size.width, height: size.height) + .background(Color(uiColor: .systemBackground)) + .cornerRadius(18) + .scaleEffect(scale) + .allowsHitTesting(false) + } else { + ProgressView() + } + } + .frame(width: size.width * scale, height: size.height * scale) + Text(hasUnappliedVersion + ? "Version \(conversation.currentVersion?.number ?? 0), not applied yet" + : "Version \(conversation.currentVersion?.number ?? 0)") + .font(.caption2) + .foregroundStyle(.secondary) + if !session.currentDomains.isEmpty { + Label("May contact: \(session.currentDomains.joined(separator: ", "))", systemImage: "globe") + .font(.caption2) + .foregroundStyle(.secondary) + .lineLimit(2) + } + } } - private func applyProposal() { - guard !isApplying, let snapshot = proposalSnapshot else { return } - let code = proposedCode, token = requestID - isApplying = true - statusMessage = "Applying and saving…" - MirrorEditorService.replaceDocument(with: code, snapshot: snapshot) { result in - guard requestID == token else { return } - isApplying = false - statusMessage = result.message - if result == .saved { - originalCode = code - proposedCode = "" - proposalSnapshot = nil + private var applyBar: some View { + let change = AICopilotChangeSummary.compare(original: knownEditorContent ?? "", proposed: session.currentSource) + return HStack { + VStack(alignment: .leading, spacing: 2) { + Text("Version \(conversation.currentVersion?.number ?? 0) is not in the editor yet") + .font(.callout.weight(.medium)) + Text("\(change.changedLines) changed lines") + .font(.caption.monospacedDigit()) + .foregroundStyle(.secondary) } + Spacer() + Button("Show Code") { + shownCode = ShownCode(title: "Version \(conversation.currentVersion?.number ?? 0)", code: session.currentSource) + } + .controlSize(.small) + Button("Apply", systemImage: "checkmark") { + if hostsToAllow.isEmpty { apply(allowingHosts: false) } else { confirmingNetwork = true } + } + .buttonStyle(.borderedProminent) + .controlSize(.small) + .fixedSize() + .accessibilityLabel("Apply to Editor") + .disabled(isApplying || !canWork) } + .padding(10) } - private func beginCopilotRequest(fixCurrentError: Bool) { - let trimmed = instruction.trimmingCharacters(in: .whitespacesAndNewlines) - let diagnostic = ScriptCodePreviewDiagnosticStore.shared.diagnostic(for: scriptModel.package.path) - let requestText: String - if fixCurrentError { - requestText = trimmed.isEmpty ? "Fix the current runtime error while preserving the widget design." : trimmed - } else { - guard !trimmed.isEmpty else { - statusMessage = "Describe the change you want first." - return + private var composer: some View { + VStack(alignment: .leading, spacing: 6) { + HStack(alignment: .bottom, spacing: 8) { + TextField("Say what to change…", text: $draftText, axis: .vertical) + .lineLimit(1...5) + .padding(.horizontal, 12) + .padding(.vertical, 9) + .background(StudioDesign.cardBackground, in: .rect(cornerRadius: 20)) + .overlay { + RoundedRectangle(cornerRadius: 20, style: .continuous) + .stroke(StudioDesign.separator, lineWidth: 0.5) + } + .accessibilityLabel("Message to the AI") + if session.isRunning { + Button { session.cancel() } label: { + Image(systemName: "stop.circle.fill").font(.system(size: 30)) + } + .accessibilityLabel("Stop") + } else { + Button { send(fixCurrentError: false) } label: { + Image(systemName: "arrow.up.circle.fill").font(.system(size: 30)) + } + .disabled(!canSend) + .accessibilityLabel("Send") + } } - requestText = trimmed + Button("Fix Runtime Error", systemImage: "wrench.and.screwdriver") { send(fixCurrentError: true) } + .controlSize(.small) + .disabled(!canWork || session.isRunning || isReading || isApplying) } + .padding(10) + } + + private var canSend: Bool { + canWork && !session.isRunning && !isReading && !isApplying + && !draftText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty + } + + // MARK: - actions - guard !isReading, !isApplying, let documentID else { return } + /// Reads the open document. Code the conversation has never seen (the user + /// edited it, or it is the first look) becomes a version; a pending AI + /// version is left alone. + private func lookAtEditor(then next: ((StudioDocumentSnapshot) -> Void)? = nil) { + guard isEntryDocument, let documentID, !isReading else { return } let token = UUID() requestID = token isReading = true @@ -457,47 +648,85 @@ private struct ScriptCodeCopilotView: View { guard requestID == token else { return } isReading = false guard let snapshot else { - statusMessage = "The editor is not ready yet." + if next != nil { statusMessage = "The editor is not ready yet." } return } - proposalSnapshot = snapshot - originalCode = snapshot.content - proposedCode = "" - statusMessage = diagnostic == nil && fixCurrentError - ? "No current runtime error was recorded; asking AI to inspect the code." - : nil - session.copilot( - currentCode: snapshot.content, - instruction: AIWidgetSkills.augment(requestText, selectedIDs: selectedSkillIDs), - runtimeDiagnostic: diagnostic - ) + knownEditorContent = snapshot.content + if !conversation.versions.isEmpty || next != nil { + session.adoptEditorSource(snapshot.content) + } + next?(snapshot) } } - private func consume(phase: AIGenerateSession.Phase) { - guard proposalSnapshot != nil else { return } - switch phase { - case .done(let jsx): - proposedCode = jsx - statusMessage = "Proposal passed the local ScriptWidget runtime." - case .exhausted(let lastJSX, let error): - proposedCode = lastJSX ?? "" - statusMessage = "Iteration limit reached: \(error ?? "unknown runtime error")" - case .failed(let message): - statusMessage = message - case .cancelled: - statusMessage = "Request cancelled." - default: - break + private func send(fixCurrentError: Bool) { + let typed = draftText.trimmingCharacters(in: .whitespacesAndNewlines) + let request = fixCurrentError && typed.isEmpty + ? "Fix the current runtime error while keeping the widget's design." + : typed + guard canWork, !request.isEmpty, !session.isRunning, !isReading, !isApplying else { return } + let diagnostic = fixCurrentError + ? ScriptCodePreviewDiagnosticStore.shared.diagnostic(for: package.path) + : nil + statusMessage = fixCurrentError && diagnostic == nil + ? "No runtime error was recorded; asking the AI to look at the code." + : nil + draftText = "" + lookAtEditor { _ in + session.send(request, runtimeDiagnostic: diagnostic) + } + } + + /// Writes the version on screen into the open document. The document must + /// still be what this panel last saw; the editor checks again on its side. + private func apply(allowingHosts: Bool) { + guard canWork, !isApplying, let documentID else { return } + let source = session.currentSource + let expected = knownEditorContent + let token = UUID() + requestID = token + isApplying = true + statusMessage = "Applying and saving…" + MirrorEditorService.requestSnapshot(target: StudioEditorTarget(editorID: editorID, documentID: documentID)) { snapshot in + guard requestID == token else { return } + guard let snapshot else { + isApplying = false + statusMessage = StudioDocumentApplyResult.unavailable.message + return + } + guard snapshot.content == expected else { + isApplying = false + knownEditorContent = snapshot.content + session.adoptEditorSource(snapshot.content) + statusMessage = "The file changed in the editor, so nothing was applied. The conversation now continues from the editor's code." + return + } + if allowingHosts, !hostsToAllow.isEmpty, var updated = package.readManifest() { + if !updated.permissions.contains(.network) { updated.permissions.append(.network) } + for host in hostsToAllow where !updated.networkDomains.contains(host) { updated.networkDomains.append(host) } + let written = package.writeManifest(updated) + guard written.0 else { + isApplying = false + statusMessage = "The network permission could not be added, so nothing was applied: \(written.1)" + return + } + } + MirrorEditorService.replaceDocument(with: source, snapshot: snapshot) { result in + guard requestID == token else { return } + isApplying = false + statusMessage = result.message + if result == .saved { knownEditorContent = source } + } } } private func skillBinding(_ id: String) -> Binding { Binding( - get: { selectedSkillIDs.contains(id) }, + get: { conversation.skillIDs.contains(id) }, set: { enabled in - if enabled { selectedSkillIDs.insert(id) } - else { selectedSkillIDs.remove(id) } + var ids = Set(conversation.skillIDs) + if enabled { ids.insert(id) } else { ids.remove(id) } + session.setSkills(ids) } ) } diff --git a/macOS/ScriptWidgetMac.xcodeproj/project.pbxproj b/macOS/ScriptWidgetMac.xcodeproj/project.pbxproj index 63a6bc3d..b12f2fac 100644 --- a/macOS/ScriptWidgetMac.xcodeproj/project.pbxproj +++ b/macOS/ScriptWidgetMac.xcodeproj/project.pbxproj @@ -65,6 +65,10 @@ A202000600000000000000B0 /* AgentLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201000600000000000000B0 /* AgentLoop.swift */; }; A202000700000000000000B0 /* AIGenerateSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201000700000000000000B0 /* AIGenerateSession.swift */; }; A202002300000000000000B0 /* AIConversationSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201002300000000000000B0 /* AIConversationSession.swift */; }; + A202002500000000000000B0 /* AISourceEdits.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201002500000000000000B0 /* AISourceEdits.swift */; }; + A202002800000000000000B0 /* AISourceDiff.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201002800000000000000B0 /* AISourceDiff.swift */; }; + A202002700000000000000B0 /* AIJSXSurfaceData.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201002700000000000000B0 /* AIJSXSurfaceData.swift */; }; + A202002600000000000000B0 /* AIJSXCheck.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201002600000000000000B0 /* AIJSXCheck.swift */; }; A202002200000000000000B0 /* AIConversationStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201002200000000000000B0 /* AIConversationStore.swift */; }; A202002100000000000000B0 /* AIConversation.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201002100000000000000B0 /* AIConversation.swift */; }; A202000800000000000000B0 /* AIGenerateProgressView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A201000800000000000000B0 /* AIGenerateProgressView.swift */; }; @@ -374,6 +378,10 @@ A201000600000000000000B0 /* AgentLoop.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AgentLoop.swift; sourceTree = ""; }; A201000700000000000000B0 /* AIGenerateSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIGenerateSession.swift; sourceTree = ""; }; A201002300000000000000B0 /* AIConversationSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIConversationSession.swift; sourceTree = ""; }; + A201002500000000000000B0 /* AISourceEdits.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AISourceEdits.swift; sourceTree = ""; }; + A201002800000000000000B0 /* AISourceDiff.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AISourceDiff.swift; sourceTree = ""; }; + A201002700000000000000B0 /* AIJSXSurfaceData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIJSXSurfaceData.swift; sourceTree = ""; }; + A201002600000000000000B0 /* AIJSXCheck.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIJSXCheck.swift; sourceTree = ""; }; A201002200000000000000B0 /* AIConversationStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIConversationStore.swift; sourceTree = ""; }; A201002100000000000000B0 /* AIConversation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIConversation.swift; sourceTree = ""; }; A201000800000000000000B0 /* AIGenerateProgressView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIGenerateProgressView.swift; sourceTree = ""; }; @@ -595,6 +603,11 @@ A201000600000000000000B0 /* AgentLoop.swift */, A201000700000000000000B0 /* AIGenerateSession.swift */, A201002300000000000000B0 /* AIConversationSession.swift */, + A201002500000000000000B0 /* AISourceEdits.swift */, + A201002800000000000000B0 /* AISourceDiff.swift */, + A201002700000000000000B0 /* AIJSXSurfaceData.swift */, + A201002600000000000000B0 /* AIJSXCheck.swift */, + A203004100000000000000B0 /* AIConversationTurnsView.swift */, A201002200000000000000B0 /* AIConversationStore.swift */, A201002100000000000000B0 /* AIConversation.swift */, A201000800000000000000B0 /* AIGenerateProgressView.swift */, @@ -613,7 +626,6 @@ isa = PBXGroup; children = ( A203000200000000000000B0 /* AIGenerateWindowView.swift */, - A203004100000000000000B0 /* AIConversationTurnsView.swift */, ); path = AIGenerate; sourceTree = ""; @@ -1391,6 +1403,10 @@ A202000600000000000000B0 /* AgentLoop.swift in Sources */, A202000700000000000000B0 /* AIGenerateSession.swift in Sources */, A202002300000000000000B0 /* AIConversationSession.swift in Sources */, + A202002500000000000000B0 /* AISourceEdits.swift in Sources */, + A202002800000000000000B0 /* AISourceDiff.swift in Sources */, + A202002700000000000000B0 /* AIJSXSurfaceData.swift in Sources */, + A202002600000000000000B0 /* AIJSXCheck.swift in Sources */, A202002200000000000000B0 /* AIConversationStore.swift in Sources */, A202002100000000000000B0 /* AIConversation.swift in Sources */, A202000800000000000000B0 /* AIGenerateProgressView.swift in Sources */, diff --git a/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift b/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift index e5781f49..8c9c588e 100644 --- a/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift +++ b/macOS/ScriptWidgetMac/AIGenerate/AIGenerateWindowView.swift @@ -188,7 +188,13 @@ private struct AIConversationWindowContent: View { if !isConfigured { notConfiguredNotice } else if conversation.turns.isEmpty { - emptyState + if conversation.currentVersion == nil { + emptyState + } else { + Text("This conversation continues from an earlier one. Say what to change.") + .font(.callout) + .foregroundStyle(.secondary) + } } AIConversationTurnsView(session: session, canSend: isConfigured) { title, code in shownCode = ShownCode(title: title, code: code) @@ -207,6 +213,10 @@ private struct AIConversationWindowContent: View { } } Divider() + AIConversationUsageNotice(conversation: conversation) { next in + session.cancel() + onOpen(next) + } composer } }