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 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
+
+
+
+**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.
+
+
+
## On Android

@@ -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 `