Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 <text font=\"title\" color=\"white\">12:30</text>\n=======\n" +
" <text font=\"title\" color=\"white\">12:45</text>\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</text>"))
wait(By.textContains("12:30</text>"))
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"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Pair<String, String>?>(null) }
var shownChanges by remember { mutableStateOf<String?>(null) }
var askName by remember { mutableStateOf(false) }
var confirmNetwork by remember { mutableStateOf<String?>(null) }
var confirmApply by remember { mutableStateOf(false) }
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) },
Expand Down Expand Up @@ -715,23 +754,45 @@ 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.
*/
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,
)
}
}
Expand All @@ -740,13 +801,13 @@ private data class TurnUi(
@Composable
private fun TurnList(
turns: List<TurnUi>, 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))
Expand All @@ -755,27 +816,31 @@ 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)) {
Row(verticalAlignment = Alignment.CenterVertically) {
Icon(Icons.Default.Edit, null, Modifier.size(14.dp), tint = MaterialTheme.colorScheme.onSurfaceVariant)
Text("The widget's code as it is installed", style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(start = 6.dp))
}
VersionLine(turn, running, onVersion)
VersionLine(turn, running, onVersion, onChanges)
}
return
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
Expand All @@ -93,14 +99,16 @@ 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
var note: String? = null
var lastError: String? = null
var lastLogs: List<String> = 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))

Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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)
Expand Down
Loading
Loading