Drive the composer's stop control and IME action from one derivation
While a turn was in flight the trailing composer button became the stop control, but the text field's keyboard send action still checked only canSend, so a prompt could be sent from the IME during a run even though the visible control had become stop. Add ChatFormat.composerAction(thread) -> ComposerAction.Send/Stop as the single pure derivation and read it from both the trailing button and the keyboard action, so the two controls can no longer disagree. ChatFormatTest.composerControlIsStopWhileARunIsInFlightAndSendOtherwise covers Send when idle and Stop while running. android/ only; no new dependency.
This commit is contained in:
@@ -100,6 +100,18 @@ internal fun sessionStatusLabel(session: ChatSession, runningLocally: Boolean):
|
||||
else -> "Idle"
|
||||
}
|
||||
|
||||
/** Which action the composer's single primary control offers for a chat. */
|
||||
enum class ComposerAction { Send, Stop }
|
||||
|
||||
/**
|
||||
* The composer's primary control: while a turn is in flight the send control
|
||||
* becomes the stop control (interrupt), otherwise it sends the draft. One
|
||||
* derivation drives both the trailing button and the keyboard's send action, so
|
||||
* a running turn can never be sent to from the other control.
|
||||
*/
|
||||
internal fun composerAction(thread: ChatThread): ComposerAction =
|
||||
if (thread.running) ComposerAction.Stop else ComposerAction.Send
|
||||
|
||||
/**
|
||||
* One line of a derived unified diff. [CONTEXT] lines are unchanged, [ADD] came
|
||||
* from the edit's `newString` and [REMOVE] from its `oldString`.
|
||||
|
||||
@@ -983,6 +983,7 @@ private fun ChatComposer(
|
||||
onSend: () -> Unit,
|
||||
onStop: () -> Unit,
|
||||
) {
|
||||
val action = composerAction(thread)
|
||||
Surface(color = MaterialTheme.colorScheme.surfaceContainer) {
|
||||
Column(Modifier.fillMaxWidth().padding(CHAT_GUTTER)) {
|
||||
if (thread.readOnly) {
|
||||
@@ -1001,17 +1002,18 @@ private fun ChatComposer(
|
||||
placeholder = { Text("Message the agent…") },
|
||||
minLines = 1,
|
||||
maxLines = 6,
|
||||
keyboardActions = KeyboardActions(onSend = { if (thread.canSend) onSend() }),
|
||||
keyboardActions = KeyboardActions(
|
||||
onSend = { if (action == ComposerAction.Send && thread.canSend) onSend() },
|
||||
),
|
||||
modifier = Modifier.weight(1f),
|
||||
)
|
||||
if (thread.running) {
|
||||
when (action) {
|
||||
// While a turn is in flight the send control becomes the stop
|
||||
// control, so it must interrupt rather than send the draft.
|
||||
IconButton(onClick = onStop) {
|
||||
ComposerAction.Stop -> IconButton(onClick = onStop) {
|
||||
Icon(Icons.Default.Stop, contentDescription = "Stop", tint = MaterialTheme.colorScheme.error)
|
||||
}
|
||||
} else {
|
||||
IconButton(onClick = onSend, enabled = thread.canSend) {
|
||||
ComposerAction.Send -> IconButton(onClick = onSend, enabled = thread.canSend) {
|
||||
Icon(Icons.AutoMirrored.Filled.Send, contentDescription = "Send")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,6 +74,14 @@ class ChatFormatTest {
|
||||
assertEquals("Idle", sessionStatusLabel(ChatSession(id = "s"), runningLocally = false))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun composerControlIsStopWhileARunIsInFlightAndSendOtherwise() {
|
||||
// While a turn is in flight the send control becomes the stop control:
|
||||
// the phone's composer offers the interrupt, never a second send.
|
||||
assertEquals(ComposerAction.Stop, composerAction(ChatThread(sessionId = "s", running = true)))
|
||||
assertEquals(ComposerAction.Send, composerAction(ChatThread(sessionId = "s")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toolEditDiffMarksOnlyChangedLinesAndCountsThem() {
|
||||
val input = JsonObject(
|
||||
|
||||
Reference in New Issue
Block a user