Skip to content

armgit / andee

Public Imported from GitHub
0

Andee lives on your phone or Mac and uses any app like you do—no integrations, no root, any model; memory and passwords never leave your device | Andee 住进你的手机或 Mac,像你一样操作任何 App——无需集成、无需 Root、支持任意模型,记忆和密码绝不离开设备。

fix(desktop): words typed or spoken over a question card answer it instead of starting a turn

booboosui06a3c94c256d
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Assistant.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Assistant.kt
index 8fcb8c1..e4aad32 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Assistant.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Assistant.kt
@@ -204,6 +204,10 @@ object Assistant {
fun say(text: String, attachments: List<Attachment> = emptyList()) {
val t = text.trim()
+ // A question card is up and the brain is blocked on it: these words are its answer, not a new
+ // turn (which would queue behind the very tool call waiting for them). Attachments cannot be
+ // an answer, so a message carrying any is still a message.
+ if (attachments.isEmpty() && AskGate.tryAnswer(t)) return
val photos = attachments.filterIsInstance<Attachment.Photo>()
val files = attachments.filterIsInstance<Attachment.FileRef>()
// Pictures alone are a complete message ("what is this"); so are files.
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ChatScreen.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ChatScreen.kt
index bbbe2e2..61f3a64 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ChatScreen.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ChatScreen.kt
@@ -179,7 +179,9 @@ fun ChatScreen(onOpenArtifacts: () -> Unit, onOpenTopic: (Long) -> Unit = {}, on
fun send() {
val t = input.trim()
- if ((t.isEmpty() && attached.isEmpty()) || busy) return
+ // Busy blocks a new message, but not the answer to a question the brain is waiting on.
+ val answering = AskGate.isWaiting
+ if ((t.isEmpty() && attached.isEmpty()) || (busy && !answering)) return
val sending = attached.toList()
input = ""
attached.clear()
@@ -285,7 +287,7 @@ fun ChatScreen(onOpenArtifacts: () -> Unit, onOpenTopic: (Long) -> Unit = {}, on
input = input,
onInput = { input = it },
listening = voice == net.kuafuai.andee.desktop.voice.MacVoice.State.LISTENING,
- canSend = (input.isNotBlank() || attached.isNotEmpty()) && !busy,
+ canSend = (input.isNotBlank() || attached.isNotEmpty()) && (!busy || AskGate.isWaiting),
attached = attached,
onRemove = { attached.remove(it) },
onAttach = { choose() },
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/UserPrompts.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/UserPrompts.kt
index c764b61..af0d7c3 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/UserPrompts.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/UserPrompts.kt
@@ -215,6 +215,10 @@ object AskGate {
private set
@Volatile var onChange: () -> Unit = {}
+ /** Compose-readable twin of "a question card is up", so the input box can stay open to answer it. */
+ private val waiting = androidx.compose.runtime.mutableStateOf(false)
+ val isWaiting: Boolean get() = waiting.value
+
fun ask(question: String, buttons: List<String>, timeoutMs: Long): Result =
run(question, buttons, timeoutMs)
@@ -227,12 +231,14 @@ object AskGate {
current?.answer?.complete(Result(null, null, "superseded"))
current = p
}
+ waiting.value = p.buttons.isNotEmpty()
onChange()
// Voice-first: the question is read out, exactly as the phone says it.
MacVoice.speak(question)
val r = runCatching { p.answer.get(timeoutMs, TimeUnit.MILLISECONDS) }
.getOrDefault(Result(null, null, "timeout"))
synchronized(this) { if (current === p) current = null }
+ waiting.value = current?.buttons?.isNotEmpty() == true
onChange()
return r
}
@@ -242,6 +248,36 @@ object AskGate {
p.answer.complete(Result(button, null, "button"))
}
+ /**
+ * Answer the card that is up with words, typed or spoken. A sentence that is (nearly) just a
+ * button label presses it; any other sentence to a question is the answer too, and travels back
+ * in the tool result for the model to judge against the options it offered.
+ *
+ * This is why those words must never start a turn of their own: the brain is blocked inside
+ * the very tool call this card belongs to, so a new turn would queue behind it while it waits
+ * for this answer — each waiting on the other, until the card times out. Same rule and same
+ * matching as the phone's `CardUi.tryAnswerByVoice`.
+ *
+ * @return true when the card took the words, so the caller must not send them as a message.
+ */
+ fun tryAnswer(said: String): Boolean {
+ val text = said.trim()
+ val p = current ?: return false
+ if (text.isEmpty() || p.answer.isDone) return false
+ // Alerts have no answer to give; a sentence said over one is an ordinary message.
+ if (p.buttons.isEmpty()) return false
+ val spoken = normalize(text)
+ val hit = p.buttons.firstOrNull { label ->
+ val l = normalize(label)
+ l.isNotEmpty() && (spoken == l || (spoken.contains(l) && l.length * 2 >= spoken.length))
+ }
+ return if (hit != null) p.answer.complete(Result(hit, text, "voice")) .let { true }
+ else p.answer.complete(Result(null, text, "voice_other")).let { true }
+ }
+
+ /** Lower-cased, spaces and punctuation dropped, so "好的。" equals "好的". */
+ private fun normalize(s: String): String = s.lowercase().filter { it.isLetterOrDigit() }
+
fun dismiss() {
current?.answer?.complete(Result(null, null, "tap_outside"))
}
diff --git a/desktop/src/test/kotlin/net/kuafuai/andee/desktop/AskGateTest.kt b/desktop/src/test/kotlin/net/kuafuai/andee/desktop/AskGateTest.kt
new file mode 100644
index 0000000..c6cb56e
--- /dev/null
+++ b/desktop/src/test/kotlin/net/kuafuai/andee/desktop/AskGateTest.kt
@@ -0,0 +1,54 @@
+package net.kuafuai.andee.desktop
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+class AskGateTest {
+ private fun ask(buttons: List<String>): Pair<Thread, Array<AskGate.Result?>> {
+ val out = arrayOfNulls<AskGate.Result>(1)
+ val t = Thread { out[0] = AskGate.ask("选哪个?", buttons, 20_000) }.also { it.start() }
+ val deadline = System.currentTimeMillis() + 3000
+ while (AskGate.current == null && System.currentTimeMillis() < deadline) Thread.sleep(10)
+ return t to out
+ }
+
+ @Test fun nothingWaitingMeansTheWordsAreAMessage() {
+ assertFalse(AskGate.tryAnswer("否"))
+ }
+
+ @Test fun aButtonLabelPressesIt() {
+ val (t, out) = ask(listOf("是", "否"))
+ assertTrue(AskGate.tryAnswer("否。"))
+ t.join(3000)
+ assertEquals("否", out[0]?.button)
+ assertEquals("voice", out[0]?.how)
+ }
+
+ @Test fun anyOtherSentenceIsTheAnswerAndReturnsToTheModel() {
+ val (t, out) = ask(listOf("是", "否"))
+ assertTrue(AskGate.tryAnswer("不用管了"))
+ t.join(3000)
+ assertEquals(null, out[0]?.button)
+ assertEquals("不用管了", out[0]?.heard)
+ assertEquals("voice_other", out[0]?.how)
+ }
+
+ @Test fun aSentenceThatOnlyStartsWithALabelIsNotThatButton() {
+ val (t, out) = ask(listOf("是", "否"))
+ AskGate.tryAnswer("否则帮我查一下天气")
+ t.join(3000)
+ assertEquals(null, out[0]?.button)
+ assertEquals("voice_other", out[0]?.how)
+ }
+
+ @Test fun anAlertTakesNoAnswer() {
+ val t = Thread { AskGate.alert("好了", 20_000) }.also { it.start() }
+ val deadline = System.currentTimeMillis() + 3000
+ while (AskGate.current == null && System.currentTimeMillis() < deadline) Thread.sleep(10)
+ assertFalse(AskGate.tryAnswer("知道了"))
+ AskGate.answer(null)
+ t.join(3000)
+ }
+}