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(page): replace an open show_html page in place instead of refusing
booboosui1e17e7c95d61
diff --git a/CLAUDE.md b/CLAUDE.mdindex 583c2f1..9786814 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -347,7 +347,7 @@ Two things in it are not styling:**Frosted glass is not available** for these pills, so don't try again. `RenderEffect` (API 31+) blurs a View's *own* content, not what is behind it; blurring behind requires `Window.setBackgroundBlurRadius`, which is window-level and cannot be scoped to one floating child. Translucent fill plus a 1 dp `#33FFFFFF` stroke is the approximation — and the stroke is load-bearing anyway, because the brain picks the page background and a dark pill on a dark page vanishes. (The *cards* do get a real blur — see `ui/Glass.kt` below. Each of them is a whole window, which is exactly the thing a pill is not.)-**`show_html` does not block.** It used to sit in a latch until the user tapped 完成 — up to five minutes — and because `LocalBrain` runs a turn on one thread and runs tools on it synchronously, that latch held the *conversation*: the user's next sentence was accepted and then queued behind it. It now returns as soon as the page is up, with `closed: false`, and the news that the user finished with the page arrives by a second route: the local brain gets a silent `LocalBrain.note()` (readable by the next turn, never spoken — a turn here would mean a model call and a sentence every time anyone dismisses a page), and a hub gets a `page.closed` event it may ignore. Two consequences worth knowing. `HtmlActivity` keeps exactly **one** page (`pageUp`), so a second `show_html` while one is up is **refused** rather than stacked — the shared static file would otherwise lose to whichever activity read it last. And the ball's perch, which used to be released by that same latch, is now released by the close callback.+**`show_html` does not block.** It used to sit in a latch until the user tapped 完成 — up to five minutes — and because `LocalBrain` runs a turn on one thread and runs tools on it synchronously, that latch held the *conversation*: the user's next sentence was accepted and then queued behind it. It now returns as soon as the page is up, with `closed: false`, and the news that the user finished with the page arrives by a second route: the local brain gets a silent `LocalBrain.note()` (readable by the next turn, never spoken — a turn here would mean a model call and a sentence every time anyone dismisses a page), and a hub gets a `page.closed` event it may ignore. Two consequences worth knowing. `HtmlActivity` keeps exactly **one** page (`pageUp`), and a second `show_html` while one is up is now **replaced in place** rather than stacked, so the shared static file cannot lose to whichever activity read it last. That used to be a *refusal* — an error telling the model to ask the user to dismiss the page — and the model said exactly that, so the most natural request there is, "改一下这页", ended with the user told to close the page by hand. What the limit forbids is one *instance*, not one render: `HtmlActivity.replaceFile` loads the new bytes into the running WebView (archived like a fresh page, so both versions are in the scrollback) and refuses only a page that is already on its way out. And the ball's perch, which used to be released by that same latch, is now released by the close callback.The device injects **no CSS** into `show_html`; styling belongs to the brain. So the house style (slate tokens, the `#9DB4FF` accent, an explicitly-named CJK font stack) lives in the `show_html` **description** in `ToolSchemas` — that prompt is the only lever on page quality, which matters most for the small models this runs against. `HtmlActivity` only fades the page in once `onPageFinished` lands, with a 2.5 s backstop because a top-level script that throws can leave that callback pending forever, and half a page beats a black screen.diff --git a/app/src/main/java/net/kuafuai/andee/brain/LocalPrompt.kt b/app/src/main/java/net/kuafuai/andee/brain/LocalPrompt.ktindex 9ff237a..79b9579 100644--- a/app/src/main/java/net/kuafuai/andee/brain/LocalPrompt.kt+++ b/app/src/main/java/net/kuafuai/andee/brain/LocalPrompt.kt@@ -246,7 +246,7 @@ When **not** to put up a page:- Anything one sentence can answer (what time is it, will it rain tomorrow, turn the light off) — a full screen for that is an interruption.- **A long piece of prose.** It is long, but it has no structure — write it in the reply and the user double-taps to read it full screen. See the top of this section.-- **Mid-way through operating the device.** `show_html` covers the current app — and then the user cannot see what you are tapping. Do not put one up half-way through the job; wait until it is done. (The page no longer blocks you: the call returns at once, and you are told when they close it. Only one page at a time.)+- **Mid-way through operating the device.** `show_html` covers the current app — and then the user cannot see what you are tapping. Do not put one up half-way through the job; wait until it is done. (The page no longer blocks you: the call returns at once, and you are told when they close it. Only one page at a time; calling `show_html` again replaces it in place, which is how you revise a page — never ask the user to close it first.)- When the user has said they only want to listen.**Images cannot go in a page**: this device cannot turn an image into a URL, so an `img` tag will not open. To show them a picture, use some other route; if there is none, describe it honestly in words.diff --git a/app/src/main/java/net/kuafuai/andee/net/CommandDispatcher.kt b/app/src/main/java/net/kuafuai/andee/net/CommandDispatcher.ktindex 34aca01..f5197db 100644--- a/app/src/main/java/net/kuafuai/andee/net/CommandDispatcher.kt+++ b/app/src/main/java/net/kuafuai/andee/net/CommandDispatcher.kt@@ -600,21 +600,41 @@ class CommandDispatcher(val defaultTitle = AppLocale.str(appContext, R.string.html_default_title)val title = params?.optString("title")?.ifEmpty { defaultTitle } ?: defaultTitleif (net.kuafuai.andee.ui.HtmlActivity.pageUp) {- // **Returns at once, and this is the reason it has to.**- // This call used to wait on a latch for the user to close- // the page, up to five minutes, and that wait was the whole- // problem: [net.kuafuai.andee.brain.LocalBrain] runs a turn on- // one thread and runs tools synchronously on it, so a parked- // `show_html` parked the conversation — the user's next- // sentence was accepted and then sat in the queue until the- // page went away. Refusing the second page instead of- // stacking it is the other half of the same constraint:- // `HtmlActivity` keeps ONE page (see its `pageUp`), so two- // in flight would mean the shared static file losing to- // whichever activity read it last.- org.json.JSONObject()- .put("error", "There is still a page open on screen (${net.kuafuai.andee.ui.HtmlActivity.titleText}). " +- "Ask the user to dismiss it first, or fold the new content into what they are already looking at.")+ // A page is already up: put the new one *in its place*.+ // This used to be refused with "ask the user to dismiss it+ // first", and the model said exactly that — so the most+ // natural request there is, "改一下这页", ended with the user+ // being told to close the page by hand. `HtmlActivity` still+ // keeps ONE page (see its `pageUp`); what that forbids is a+ // second instance over the shared static file, not a second+ // render, so the replacement goes into the running WebView.+ // Archived like a fresh page, so both versions are in the+ // scrollback.+ val archived = net.kuafuai.andee.ui.ChatHistory.addPage(title, html)+ val openedAt = android.os.SystemClock.elapsedRealtime()+ val replaced = archived != null &&+ net.kuafuai.andee.ui.HtmlActivity.replaceFile(appContext, archived, title) {+ pageClosed(title, openedAt)+ }+ if (replaced) {+ org.json.JSONObject()+ .put("shown", true)+ .put("replaced", true)+ .put("closed", false)+ .put("title", title)+ .put(+ "note",+ "The page that was on screen now shows this new version, in place — the user did " ++ "not have to close anything. This step ends here; I will tell you when they close it.",+ )+ } else {+ // The only ways here are a page already on its way out+ // (the user tapped ✕ a moment ago) or an archive write+ // that failed. Neither is the user's to fix.+ org.json.JSONObject()+ .put("error", "The page on screen is closing right now and could not take the new version. " ++ "Call show_html again in a moment — do not ask the user to close anything.")+ }} else {// The page is the whole point of the call, and our own card// is a fullscreen opaque overlay that sits above everydiff --git a/app/src/main/java/net/kuafuai/andee/net/ToolSchemas.kt b/app/src/main/java/net/kuafuai/andee/net/ToolSchemas.ktindex 0fb49d2..22385ed 100644--- a/app/src/main/java/net/kuafuai/andee/net/ToolSchemas.kt+++ b/app/src/main/java/net/kuafuai/andee/net/ToolSchemas.kt@@ -830,8 +830,10 @@ object ToolSchemas {columns, cards, steps) or is interactive; length alone is not areason.- ONE PAGE AT A TIME: while a page is up, calling this again is- refused rather than stacked. Compose everything into the one page.+ ONE PAGE AT A TIME: while a page is up, calling this again REPLACES+ it in place — that is how you revise a page the user asked you to+ change. Send the whole new page, not a diff. Never ask the user to+ close the page first.Inline CSS and JS ONLY. The device may have no usable internet, so apage that fetches a font, a framework or an image over the networkdiff --git a/app/src/main/java/net/kuafuai/andee/ui/HtmlActivity.kt b/app/src/main/java/net/kuafuai/andee/ui/HtmlActivity.ktindex 365f16d..a402e44 100644--- a/app/src/main/java/net/kuafuai/andee/ui/HtmlActivity.kt+++ b/app/src/main/java/net/kuafuai/andee/ui/HtmlActivity.kt@@ -58,6 +58,25 @@ class HtmlActivity : StageActivity() {*/@Volatile var pageUp = false+ /**+ * The page that is on screen, for [replaceFile].+ *+ * A weak reference rather than a second `pageUp` flag, because the+ * question being asked is not "is one up" but "which object do I hand+ * new bytes to" — and the answer has to survive being asked from a+ * dispatcher worker while the field is written from the main thread.+ *+ * Set at the top of [onCreate], before that method reads the statics+ * below, and cleared in [onDestroy]. Those two facts are what close the+ * handoff window: a `startActivity` that has been *asked* for but has not+ * run yet is covered by the statics being the live values, and one that+ * has run is covered by this.+ */+ @Volatile private var live: java.lang.ref.WeakReference<HtmlActivity>? = null++ /** Main thread. [replaceFile] posts here; a dispatcher worker only waits on it. */+ internal val mainHandler = android.os.Handler(android.os.Looper.getMainLooper())+/*** Origin the brain's page runs as.*@@ -129,11 +148,84 @@ class HtmlActivity : StageActivity() {throw it}}++ /**+ * Put new bytes on the page that is already up. Returns false when there+ * is nothing to hand them to.+ *+ * **The reason this exists is that "one page at a time" used to mean+ * "refused".** A user who asked for a change to the page in front of+ * them — "把标题改短点" — got a model call, a refused `ui.show_html`, and+ * a sentence telling them to close the page by hand first: an+ * instruction to do the one thing the assistant was there to do. The+ * limit is real (one [htmlFile], one WebView), but what it forbids is two+ * *instances*, not a second render.+ *+ * Safe against the handoff gap by two paths, and both are needed:+ * before [onCreate] has run there is no instance and the statics are the+ * live values, so writing them is the replacement; once it has run,+ * [swapTo] swaps them under the running WebView. A page that is already+ * on its way out is refused rather than swapped into — writing into a+ * window that is being destroyed loses the new page entirely, and the+ * caller is told to open it as a fresh page instead.+ */+ fun replaceFile(+ context: android.content.Context,+ file: java.io.File,+ title: String,+ onClosed: () -> Unit,+ ): Boolean {+ if (!pageUp) return false+ val running = live?.get()+ if (running == null) {+ titleText = title+ onClose = onClosed+ htmlFile = file+ return true+ }+ val done = java.util.concurrent.CountDownLatch(1)+ var ok = false+ mainHandler.post {+ ok = if (running.isLeaving) {+ false+ } else {+ running.swapTo(file, title, onClosed)+ true+ }+ done.countDown()+ }+ done.await(500, java.util.concurrent.TimeUnit.MILLISECONDS)+ return ok+ }}private var web: WebView? = nullprivate var spinner: ProgressBar? = null+ /**+ * Swap this page's contents. Main thread only — [replaceFile] posts here.+ *+ * Not a second Activity and not a `startActivity`: the WebView is already on+ * screen and already sized, so the new render lands in the same window at+ * the same scroll position of the *chrome*, which is what "改了" should look+ * like. The scroll position of the page itself resets, and that is correct —+ * different bytes are not the same document.+ *+ * The statics are updated before the load because [onDestroy] reads them,+ * and a page closed two seconds after a replacement has to be the page the+ * caller was told about.+ */+ private fun swapTo(file: java.io.File, title: String, closed: () -> Unit) {+ titleText = title+ onClose = closed+ htmlFile = file+ val html = runCatching { file.readText() }.getOrNull() ?: return+ // No spinner and no stage animation: the window is already up, and+ // growing it again would read as a second page opening. A slow render+ // shows the old page for a beat, which is the honest thing to show.+ web?.loadDataWithBaseURL(BASE_URL, html, "text/html", "utf-8", null)+ }+@SuppressLint("SetJavaScriptEnabled")override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)@@ -143,6 +235,10 @@ class HtmlActivity : StageActivity() {// Again here, not instead of in [showFile]: this one covers the activity// being recreated by the system after process death, where no caller ran.pageUp = true+ // Before the statics below are read, so a replacement that lands while+ // this method is running either writes the statics (this is not live+ // yet) or takes the running path — never both and never neither.+ live = java.lang.ref.WeakReference(this)// Only when no caller named the page. [titleText] itself is set by// [showFile] before this activity starts, so the common path is already// non-blank and this is the fallback for the recreated-instance case.@@ -289,6 +385,7 @@ class HtmlActivity : StageActivity() {// [ChatHistory]'s job, against its own budget. See [showFile].htmlFile = nullpageUp = false+ live = nullsuper.onDestroy()}diff --git a/app/src/main/java/net/kuafuai/andee/ui/Stage.kt b/app/src/main/java/net/kuafuai/andee/ui/Stage.ktindex 42054c4..da4b4c6 100644--- a/app/src/main/java/net/kuafuai/andee/ui/Stage.kt+++ b/app/src/main/java/net/kuafuai/andee/ui/Stage.kt@@ -79,6 +79,9 @@ abstract class StageActivity : AppCompatActivity() {*/private var leaving = false+ /** Main thread only, like [leaving] itself. Read by [HtmlActivity.replaceFile]. */+ internal val isLeaving: Boolean get() = leaving+/*** Called once, the instant [leave] starts, while this window is still on* screen. Put the overlay back from here.