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: a long task no longer empties the history, a new request interrupts the old one, voice ends on silence
booboosui63c99e6a50fe
diff --git a/core/src/main/kotlin/net/kuafuai/andee/brain/HistoryTrim.kt b/core/src/main/kotlin/net/kuafuai/andee/brain/HistoryTrim.ktnew file mode 100644index 0000000..05aeedc--- /dev/null+++ b/core/src/main/kotlin/net/kuafuai/andee/brain/HistoryTrim.kt@@ -0,0 +1,32 @@+package net.kuafuai.andee.brain++/**+ * Which messages the hard backstop drops when the history is past its limit.+ *+ * Pure, so the shape that once emptied a conversation is pinned in a test: a long task is ONE plain user+ * message followed by a hundred assistant and tool messages. Cutting "to the next clean boundary" from the+ * front ran off the end when no boundary followed, and threw away everything but the system prompt — so the+ * next sentence arrived with nothing before it and the model had to ask what was being built.+ */+object HistoryTrim {++ /**+ * @param boundary one flag per message, true for a plain `user` message (the only place a conversation+ * may be cut). Index 0 is the system message and is never dropped.+ * @return indexes to remove. Whole turns from the front, oldest first, and never the newest turn: it+ * stays intact however long it has grown, because it is the one the conversation is still in.+ */+ fun dropIndexes(boundary: List<Boolean>, max: Int): List<Int> {+ if (boundary.size <= max) return emptyList()+ val newest = (boundary.lastIndex downTo 1).firstOrNull { boundary[it] } ?: return emptyList()+ val drop = ArrayList<Int>()+ var i = 1+ var size = boundary.size+ while (size > max && i < newest) {+ // One whole turn: its user message and everything up to the next user message.+ drop += i; size--; i+++ while (i < newest && !boundary[i]) { drop += i; size--; i++ }+ }+ return drop+ }+}diff --git a/core/src/main/kotlin/net/kuafuai/andee/brain/LocalBrain.kt b/core/src/main/kotlin/net/kuafuai/andee/brain/LocalBrain.ktindex 9b34724..bf48587 100644--- a/core/src/main/kotlin/net/kuafuai/andee/brain/LocalBrain.kt+++ b/core/src/main/kotlin/net/kuafuai/andee/brain/LocalBrain.kt@@ -1302,13 +1302,9 @@ class LocalBrain(* mid-task takes the task with it. The system message at index 0 stays.*/private fun trimHistory() {- if (history.size <= MAX_HISTORY) return- while (history.size > MAX_HISTORY && history.size > 1) {- drop(1)- // Then keep going to the next clean boundary — a plain `user`- // message is the only place a conversation can be cut.- while (history.size > 1 && !isBoundary(history[1])) drop(1)- }+ val before = history.size+ if (before <= MAX_HISTORY) return+ for (gone in HistoryTrim.dropIndexes(history.map { isBoundary(it) }, MAX_HISTORY).sortedDescending()) drop(gone)host.logInfo(TAG, "history trimmed to ${history.size} msgs")}diff --git a/core/src/test/kotlin/net/kuafuai/andee/brain/HistoryTrimTest.kt b/core/src/test/kotlin/net/kuafuai/andee/brain/HistoryTrimTest.ktnew file mode 100644index 0000000..3b5e8c6--- /dev/null+++ b/core/src/test/kotlin/net/kuafuai/andee/brain/HistoryTrimTest.kt@@ -0,0 +1,37 @@+package net.kuafuai.andee.brain++import org.junit.Assert.assertEquals+import org.junit.Assert.assertTrue+import org.junit.Test++class HistoryTrimTest {+ private fun turn(len: Int): List<Boolean> = listOf(true) + List(len - 1) { false }++ @Test fun underTheLimitDropsNothing() {+ assertEquals(emptyList<Int>(), HistoryTrim.dropIndexes(listOf(false) + turn(10), 60))+ }++ @Test fun oneLongTaskIsNeverEmptied() {+ // system + one user message + 103 assistant/tool messages: no boundary after the user message.+ val b = listOf(false) + turn(104)+ assertEquals("nothing may be dropped from the only turn", emptyList<Int>(), HistoryTrim.dropIndexes(b, 60))+ }++ @Test fun oldTurnsGoOldestFirstAndTheNewestSurvivesWhole() {+ val b = listOf(false) + turn(10) + turn(10) + turn(10) + turn(50)+ val d = HistoryTrim.dropIndexes(b, 60)+ val kept = b.indices.filter { it !in d }+ assertTrue(0 in kept)+ // The newest turn (starts at index 31) is untouched.+ assertTrue((31 until b.size).all { it in kept })+ // Dropped ranges are whole turns: the first message kept after the system message is a user message.+ assertTrue(b[kept[1]])+ assertEquals(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), d.take(10))+ }++ @Test fun stopsAsSoonAsItIsUnderTheLimit() {+ val b = listOf(false) + turn(30) + turn(30) + turn(5)+ val d = HistoryTrim.dropIndexes(b, 60)+ assertEquals(30, d.size)+ }+}diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Assistant.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Assistant.ktindex e4aad32..97a92b6 100644--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Assistant.kt+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Assistant.kt@@ -218,6 +218,9 @@ object Assistant {val p = pendingpending = nullif (p != null) onPendingChanged()+ // A new request replaces whatever is running: cut the old turn off before anything else, the way+ // ■ does, so it stops talking and stops driving the screen while the new one starts.+ if (brain?.isBusy() == true || net.kuafuai.andee.desktop.voice.MacVoice.isSpeaking()) interruptForNewRequest()val zh = Strings.lang() == "zh"val row = when (p) {is Pending.Edit -> if (zh) "修改「${p.page.title}」:$t" else "Revise “${p.page.title}”: $t"@@ -281,6 +284,13 @@ object Assistant {/** A turn is running, or its answer is being read out: what a tap on the ball should cut short. */fun working(): Boolean = brain?.isBusy() == true || net.kuafuai.andee.desktop.voice.MacVoice.isSpeaking()+ /**+ * A new request replaces the running one: the same cut as ■ (the turn, the commands it started, and+ * the voice still reading the old answer). [say] has already taken the 继续 target before this, so the+ * stop clearing [pending] loses nothing.+ */+ private fun interruptForNewRequest() = stop()+/** The user's stop: the turn ends, and every command it started ends with it. */fun stop() {cancelPending()diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/voice/MacVoice.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/voice/MacVoice.ktindex 51a87b2..ad9ac66 100644--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/voice/MacVoice.kt+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/voice/MacVoice.kt@@ -93,6 +93,7 @@ object MacVoice {val cfg = settings()val engine: AsrEngine = if (cfg.voiceProvider == VoiceSettings.VOICE_GOOGLE) GoogleAsr(cfg, listener) else HuoshanAsr(cfg, listener)asr = engine+ armWatchdog(engine)state(State.LISTENING)io.execute {runCatching {@@ -129,23 +130,98 @@ object MacVoice {fun recordingMeeting(): Boolean = net.kuafuai.andee.desktop.Meeting.isActive()fun stopListening() {+ disarmWatchdog()if (listening.compareAndSet(true, false)) asr?.endOfTurn(bySilence = false)}private val partial = StringBuilder()+ // ---- the silence rule: the phone's, ported ---------------------------------------------------------+ //+ // The user says what they have to say and stops talking; nobody should have to press anything a second+ // time. The clock is fed by the recogniser's own output and by nothing else (a local energy gate is a+ // threshold that separates the user from their room, and every room is different). Three rules hold+ // the edges, none needing calibration:+ // * nothing counts until the engine can hear (onReady) — the mic opens before the session comes up;+ // * a turn that has heard nothing at all gets SILENT_START_GRACE_MS before silence may end it;+ // * a turn never outlives MAX_TURN_MS, whatever the recogniser says.+ private const val SILENT_START_GRACE_MS = 4_000L+ private const val MAX_TURN_MS = 60_000L+ private const val SILENCE_CHECK_MS = 250L++ @Volatile private var turnStartedAt = 0L+ @Volatile private var lastSpeechAt = 0L+ @Volatile private var lastPartial = ""+ @Volatile private var engineReady = false+ @Volatile private var heardAnything = false+ @Volatile private var turnSilenceMs = 3_000L+ @Volatile private var watchdog: java.util.concurrent.ScheduledFuture<*>? = null+ private val watchdogExec = java.util.concurrent.Executors.newSingleThreadScheduledExecutor { r ->+ Thread(r, "voice-silence").apply { isDaemon = true }+ }++ private fun armWatchdog(engine: AsrEngine) {+ turnStartedAt = System.currentTimeMillis()+ lastSpeechAt = turnStartedAt+ lastPartial = ""+ engineReady = false+ heardAnything = false+ turnSilenceMs = engine.silenceMs+ watchdog?.cancel(false)+ watchdog = watchdogExec.scheduleWithFixedDelay({ runCatching { checkSilence(engine) } }, SILENCE_CHECK_MS, SILENCE_CHECK_MS, java.util.concurrent.TimeUnit.MILLISECONDS)+ }++ private fun disarmWatchdog() { watchdog?.cancel(false); watchdog = null }++ private fun checkSilence(engine: AsrEngine) {+ if (asr !== engine || !listening.get()) { disarmWatchdog(); return }+ val now = System.currentTimeMillis()+ if (now - turnStartedAt >= MAX_TURN_MS) {+ Log.i(TAG, "turn hit ${MAX_TURN_MS}ms ceiling — auto-stop")+ endBySilence(engine); return+ }+ if (!engineReady) return+ // Nothing heard yet: there is nothing to have been silent *after*. Bounded, or a tap followed by+ // nothing would hold the microphone to the ceiling.+ if (!heardAnything && now - turnStartedAt < SILENT_START_GRACE_MS) return+ val quiet = now - lastSpeechAt+ if (quiet < turnSilenceMs) return+ Log.i(TAG, "silence ${quiet}ms (limit=$turnSilenceMs) — auto-stop")+ endBySilence(engine)+ }++ private fun endBySilence(engine: AsrEngine) {+ disarmWatchdog()+ // Exactly as if the user had tapped again; the engine is told it was the caller's silence, so it+ // knows the last results are already in hand.+ if (listening.compareAndSet(true, false)) engine.endOfTurn(bySilence = true)+ }+private val listener = object : AsrEngine.Listener {- override fun onPartial(text: String) { onPartial.invoke(text) }+ override fun onReady() {+ lastSpeechAt = System.currentTimeMillis()+ engineReady = true+ }+ override fun onPartial(text: String) {+ if (text.isNotEmpty()) {+ heardAnything = true+ // Only a change counts: a recogniser may re-send the same hypothesis while the user is quiet.+ if (text != lastPartial) { lastPartial = text; lastSpeechAt = System.currentTimeMillis() }+ }+ onPartial.invoke(text)+ }override fun onFinal(text: String) {+ disarmWatchdog()listening.set(false)if (text.isNotBlank()) onFinal.invoke(text.trim())}override fun onError(msg: String) {+ disarmWatchdog()listening.set(false)Log.w(TAG, "asr: $msg")onPartial.invoke("")}- override fun onClosed() { listening.set(false) }+ override fun onClosed() { disarmWatchdog(); listening.set(false) }}// ---- speaking ---------------------------------------------------------------