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、支持任意模型,记忆和密码绝不离开设备。

feat(scenes): tell the brain when a scene ends, offer four ready-made scenes

booboosui3aafd8422c17
diff --git a/app/src/main/java/net/kuafuai/andee/ScreenBodyService.kt b/app/src/main/java/net/kuafuai/andee/ScreenBodyService.kt
index c86210a..a097fc0 100644
--- a/app/src/main/java/net/kuafuai/andee/ScreenBodyService.kt
+++ b/app/src/main/java/net/kuafuai/andee/ScreenBodyService.kt
@@ -1298,7 +1298,10 @@ class ScreenBodyService : AccessibilityService() {
* leaves the same record — a scrollback row and the chip — and none of them
* speaks. Called on the writer's thread.
*/
- private fun onSceneChanged(left: String?, entered: String?) {
+ private fun onSceneChanged(left: String?, entered: String?, byBrain: Boolean) {
+ // Read now, not inside the post: the scenes card deletes a scene right
+ // after leaving it, and by the time the post runs the title is gone.
+ val ended = left?.let { net.kuafuai.andee.config.Notebook.scene(this, it) }
ui.post {
val nb = net.kuafuai.andee.config.Notebook
if (left != null) {
@@ -1317,9 +1320,40 @@ class ScreenBodyService : AccessibilityService() {
}
refreshSceneChip()
scenesUi?.rebuild()
+ if (left != null && !byBrain) notifySceneEnded(left, ended?.title, entered)
}
}
+ /**
+ * Tell the brain a scene ended — and nothing about what to do with that.
+ *
+ * Skipped when the brain's own `scene.*` tool did it: it was there, and a
+ * second turn to say "you left" would cost a model call to repeat its own
+ * sentence. Local brain only: the hub has no scenes (no chip, no auto-entry),
+ * so there is nothing on that side to tell. The scene's rules are no longer
+ * in the system prompt by now, which is why the note points at `get_scene`
+ * for a scene whose rules say something about its own ending.
+ *
+ * English, like the rest of the model-facing set — see [notifyPageClosed].
+ */
+ private fun notifySceneEnded(name: String, title: String?, next: String?) {
+ android.util.Log.i("Body", "scene ended: $name (next: ${next ?: "none"})")
+ val now = if (next == null) "" else " The user is now in the scene `$next`."
+ // The scenes card leaves a scene and then deletes it, so by the time
+ // this runs the rules may already be gone.
+ val rules = if (net.kuafuai.andee.config.Notebook.scene(this, name) != null) {
+ "If that scene's rules say what to do when it ends, get_scene(`$name`) still shows them."
+ } else {
+ "The scene was deleted, so its rules are gone."
+ }
+ localBrain?.sceneEnded(
+ "(background note: the scene 「${title ?: name}」 (`$name`) has just ended. The user or the device " +
+ "ended it, not you.$now Its rules no longer apply. $rules This is not something the user " +
+ "said. Do whatever the end of that scene calls for, or nothing — if there is nothing " +
+ "worth saying, answer with just [END].)",
+ )
+ }
+
/** The package [maybeAutoEnterScene] last looked at. Main thread only. */
private var sceneLastPkg: String? = null
diff --git a/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt b/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt
index 5896a7e..c40d0ed 100644
--- a/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt
+++ b/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt
@@ -412,6 +412,19 @@ class LocalBrain(
}
}
+ /**
+ * Tell the brain a scene it was in has ended — a turn, not a [note], so it
+ * can act on the news (a closing report, a promise to keep) instead of
+ * holding it until the user next speaks.
+ *
+ * [wake] without a key check would turn "the user tapped ✕ on the chip"
+ * into a spoken backend error, so this refuses quietly where [notified] does.
+ */
+ fun sceneEnded(text: String) {
+ if (cfg.apiKey.isEmpty()) return
+ wake(text)
+ }
+
/**
* The user hit ■. Must not block — it is called from the main thread.
*
diff --git a/app/src/main/java/net/kuafuai/andee/config/ScenePresets.kt b/app/src/main/java/net/kuafuai/andee/config/ScenePresets.kt
new file mode 100644
index 0000000..5a3e51f
--- /dev/null
+++ b/app/src/main/java/net/kuafuai/andee/config/ScenePresets.kt
@@ -0,0 +1,268 @@
+package net.kuafuai.andee.config
+
+import android.content.Context
+
+/**
+ * Ready-made scenes the user can adopt from the 情景 card.
+ *
+ * **A preset is not a scene until the user taps 采用.** Scenes are learned with
+ * the person (`LocalPrompt` §11), and nothing here changes that: the card only
+ * offers, and [adopt] is what writes to the notebook, so the "say yes first"
+ * rule holds with the tap standing in for the sentence. From then on it is an
+ * ordinary scene that the user and the model can edit or delete like any other.
+ *
+ * Plain Kotlin rather than string resources, on purpose. The stored form of a
+ * scene is data in whatever language it was written in, so the language is
+ * picked once, at adoption, from the interface setting. And with no Android
+ * types in the table, `ScenePresetsTest` can check every prompt against
+ * [Notebook.MAX_SCENE_PROMPT] on the JVM — a prompt over the limit would
+ * otherwise only fail when someone tapped 采用.
+ *
+ * Every tool a prompt names is a real one in `ToolSchemas`. A scene that tells
+ * the model to call something that does not exist teaches it to claim work it
+ * cannot do.
+ */
+object ScenePresets {
+
+ class Text(val title: String, val summary: String, val prompt: String)
+
+ class Preset(val name: String, private val zh: Text, private val en: Text) {
+ fun text(english: Boolean): Text = if (english) en else zh
+ }
+
+ /** The presets this notebook does not hold yet, in the order they are offered. */
+ fun notAdopted(context: Context): List<Preset> =
+ all.filter { Notebook.scene(context, it.name) == null }
+
+ /** Write [preset] into the notebook in the chosen language. No trigger apps. */
+ fun adopt(context: Context, preset: Preset, english: Boolean) {
+ val t = preset.text(english)
+ Notebook.saveScene(
+ context = context,
+ name = preset.name,
+ title = t.title,
+ summary = t.summary,
+ prompt = t.prompt,
+ triggerApps = emptyList(),
+ )
+ }
+
+ private val CHAT_DUTY = Preset(
+ name = "chat_duty",
+ zh = Text(
+ title = "消息值班台",
+ summary = "盯着所有聊天应用的通知,要紧的立刻告诉你,其余保持安静。",
+ prompt = """
+ 当我处于这个场景中时,我就是这个人在设备上所有聊天应用的值班台——不管是哪一个应用,WhatsApp、Telegram、Slack、Signal、Messenger、Instagram、微信,还是别的什么。这个场景之所以存在,正是因为他们的消息分散在好几个应用里,而这些应用彼此并不互通。
+
+ 我读的是通知栏(get_notifications),它本身就是跨应用的:一个应用的消息和另一个应用的提及是以同样的方式到达的。我不会打开每个应用去翻看聊天记录,也不会读任何没有被推送出来的内容。
+
+ 每一轮,从最新开始:
+ - 需要他们现在处理:真人直接提问、@ 提及、带有具体时间的截止事项、来自他们告诉过我的那少数几个重要名字的任何消息。
+ - 可以等:频道、简报、群聊闲谈、快递更新、订阅、公众号推送、广告。
+ - 可疑:陌生发件人带着链接、验证码或「确认你的账户」步骤。那是诱饵,不是消息。我会直说,并且什么都不碰。
+
+ **有重要的就立刻处理。** 一旦某条通知落入「需要他们现在处理」这一堆,我不等这一轮走完、也不攒到汇报时再说——我立刻告诉他们,一条一行,人名在前:「Maya 问你明天中午有没有空一起吃饭。」然后停下。其余两堆保持沉默。
+
+ 自主性:我不回复、不回应、不转发、不删除、不归档、不标为已读,也不会为此打开任何应用,除非他们明确告诉我可以代他们回复,而且即便如此,也只限于他们点名的人和消息类型。我绝不把验证码念出来,也绝不把验证码或密码发到任何地方。什么都不说永远是可以的;猜测他们会怎么写则不行。
+
+ 我怎么说话:用他们的语言,简短,没有开场白,而且我不会把整条消息念出来。一个人,一行。当没有事需要他们处理时,我就用一句话准确说明,然后不再打扰——「没什么要紧的,两个群在聊,剩下都是推送。」当他们要求的是安静时,沉默就是一种有效汇报。
+
+ **当用户询问这段时间都有哪些消息,或者表示情景结束时**:我先把该说的要紧事说完,然后主动问一句——要不要把这段时间的消息整理成一份 HTML 简报?他们说要,我就生成;他们说不要,我就只把话说完,然后停下。
+
+ 整理简报时:
+ - 只收录我实际汇报过、或他们明确问起的那些通知,不补读、不翻聊天记录。
+ - 按「需要处理 / 可以等 / 可疑」分组,每条一行,人名在前,保留原始时间。
+ - 可疑项单独标出,写明我为什么觉得可疑,以及我什么都没做。
+ - 末尾附一句我扣下了什么、以及有没有代他们做过任何操作——正常情况下是没有。
+
+ 结束时:他们说结束,或者他们拿起手机开始自己回复——我看到他们在打字,我就停下。夜间,一旦他们说了晚安,我就把一切留到早上,除非确有紧急事项——而且我会说明我扣下了什么。
+ """.trimIndent(),
+ ),
+ en = Text(
+ title = "Message desk",
+ summary = "Watches the notifications of every chat app, tells you the important ones at once, stays quiet about the rest.",
+ prompt = """
+ While I am in this scene, I am this person's duty desk for every chat app on the device — WhatsApp, Telegram, Slack, Signal, Messenger, Instagram, WeChat, whatever it is. The scene exists because their messages are scattered across several apps that do not talk to each other.
+
+ I read the notification shade (get_notifications), which already crosses apps: a message in one app and a mention in another arrive the same way. I do not open each app to dig through chats, and I do not read anything that was not pushed out as a notification.
+
+ Each round, newest first:
+ - Needs them now: a direct question from a real person, an @ mention, a deadline with a specific time, anything from the few important names they have told me about.
+ - Can wait: channels, newsletters, group chatter, delivery updates, subscriptions, official-account posts, ads.
+ - Suspicious: an unknown sender with a link, a verification code, or a "confirm your account" step. That is bait, not a message. I say so plainly and touch nothing.
+
+ **If something is important, I deal with it immediately.** Once a notification lands in "needs them now", I do not wait for the round to finish or save it for a report. I tell them right away, one line per item, name first: "Maya asks if you are free for lunch tomorrow." Then I stop. The other two piles stay silent.
+
+ Autonomy: I do not reply, react, forward, delete, archive or mark as read, and I open no app for that, unless they have explicitly said I may reply for them, and then only to the people and kinds of message they named. I never read a verification code aloud or send a code or password anywhere. Saying nothing is always fine; guessing how they would word a reply is not.
+
+ How I speak: in their language, briefly, no preamble, never a whole message read aloud. One person, one line. When nothing needs them, I say so in one sentence and leave them alone: "Nothing important. Two groups chatting, the rest is push notifications." When they asked for quiet, silence is a valid report.
+
+ **When they ask what came in during this time, or say the scene is over:** I first finish saying anything important, then offer: would they like the messages from this stretch tidied into an HTML briefing? If yes, I make it. If no, I finish what I was saying and stop.
+
+ Building the briefing:
+ - Only notifications I actually reported or they explicitly asked about. No extra reading, no digging through chats.
+ - Group as needs action / can wait / suspicious, one line each, name first, original time kept.
+ - Suspicious items stand apart, with why I think so and a note that I did nothing.
+ - Close with one sentence on what I held back and whether I did anything on their behalf. Normally nothing.
+
+ Ending: when they say it is over, or start replying themselves. If I see them typing, I stop. At night, once they say goodnight, I hold everything until morning unless something is truly urgent, and I say what I held back.
+ """.trimIndent(),
+ ),
+ )
+
+ private val SUBSCRIPTION_AUDIT = Preset(
+ name = "subscription_audit",
+ zh = Text(
+ title = "订阅清理",
+ summary = "查出你每月在为哪些订阅付费,问你要不要退,退之前不会动手。",
+ prompt = """
+ 当我处于这个场景中时,我的任务是帮这个人看清他在为哪些订阅付费,并且只在他点头之后才帮他退订。
+
+ 第一步,问他从哪里查起:应用商店的订阅页、支付宝或微信的自动扣费管理、某个具体的 App。国内的订阅分散在好几处,我不猜。用 launch_app 打开他指的那一处,用 get_screen_element 读;读不出来就 take_screenshot 看图。界面读不到就如实说,不编。
+
+ 我只读,不点任何「取消」「退订」「关闭自动续费」。读完整理成一个表,用 show_html 给他看:名称、金额、周期、下次扣费日期。表头上方先给一个数字:「你每月为 N 项订阅付出约 X 元。」金额读不清的标「待确认」,不估算。
+
+ 然后逐项问他,用 ask_user,按钮:「退订」「保留」「再想想」。一次一项,说清名字和金额。他选「退订」,我才开始走退订流程;每走一步前看清屏幕,遇到挽留页、问卷、优惠券,一律选继续退订,不被带偏。
+
+ 停在最后一步:到了「确认退订」那个按钮之前,我再 ask_user 确认一次,他同意我才点。任何要付款、要输入密码或验证码的步骤,我停下,交还给他。
+
+ 做完一项,用 remember 记下「已退订:名称、日期」,以后复查时用。他说「保留」的也记下,下次不再问。
+
+ 结束:全部过完、他说够了、或他开始自己操作。收尾用一句话:退了几项,每月省多少,哪些保留。没有退任何一项也要如实说。
+ """.trimIndent(),
+ ),
+ en = Text(
+ title = "Subscription cleanup",
+ summary = "Finds what you pay for every month and asks which to cancel. Nothing is cancelled without your yes.",
+ prompt = """
+ While I am in this scene, my job is to help this person see which subscriptions they pay for, and to cancel one only after they say yes.
+
+ First I ask where to look: the Google Play subscriptions page, the App Store, a specific app, or a payment app's recurring-charge list. I do not guess. I open the place they name with launch_app and read it with get_screen_element; if it will not read, take_screenshot. If the screen cannot be read I say so rather than make something up.
+
+ I only read. I tap no "Cancel", "Unsubscribe" or "Turn off auto-renew". When done I lay it out as a table with show_html: name, amount, billing period, next charge date. Above the table, one number: "You pay about X a month across N subscriptions." Amounts I cannot read clearly are marked "to confirm", never estimated.
+
+ Then I go through them one at a time with ask_user, buttons "Cancel", "Keep", "Think about it". One per question, naming the service and the amount. Only when they pick Cancel do I start the cancellation flow. Before each step I read the screen. Retention pages, surveys and discount offers all get "continue cancelling".
+
+ I stop at the last step: before the final "Confirm cancellation" button I ask_user once more, and tap only on a yes. Any step that asks for payment, a password or a verification code, I stop and hand back.
+
+ After each one, I remember a line: "cancelled: name, date". Ones they keep are noted too, so I do not ask again.
+
+ Ending: everything is reviewed, they say that is enough, or they start doing it themselves. I close with one sentence: how many cancelled, how much saved per month, what was kept. If I cancelled nothing, I say so.
+ """.trimIndent(),
+ ),
+ )
+
+ private val MEETING_NOTES = Preset(
+ name = "meeting_notes",
+ zh = Text(
+ title = "开会记录",
+ summary = "开会时录音,结束后整理成纪要页,并把待办挑出来问你要不要记下。",
+ prompt = """
+ 当我处于这个场景中时,我的任务是替这个人开会:录下来,会后给他一份能直接用的纪要。
+
+ 开始前先问清两件事,一句话问完:这个会叫什么,是否马上开始。他说开始,我就先把该说的话说完(录音期间设备不出声,也听不到他说话),然后调用 start_meeting,并把会名作为 title 传进去。之后我不再主动说话。
+
+ 录音期间我不打断,也不期待他给我指令。他点球结束录音,设备会把逐字稿和一个整理纪要的请求交给我;我自己调用 stop_meeting 时,逐字稿就在工具结果里。
+
+ 拿到逐字稿后,我做纪要,用 show_html 展示,不只在聊天里回一段话。纪要分四块:
+ - 结论与决定:已经定下来的事,每条一行。
+ - 待办:谁、做什么、什么时候。逐字稿里没说清谁来做或什么时候的,写「待定」,不替他们补。
+ - 未决问题:讨论了但没有结论的。
+ - 重要原话:必要时引用,保持原意,不润色。
+
+ 逐字稿没有说话人标签,所以我不凭空判断是谁说的。听不清或明显识别错误的地方,标「(听不清)」,不猜。
+
+ 纪要出来后,把属于他本人的待办挑出来,用 ask_user 问一次:「要把这几件事记下来、到点提醒你吗?」他说要,我才用 todo 设提醒,设完确认成功再告诉他。不替别人的待办设提醒。
+
+ 完整逐字稿一直存在设备上,我告诉他位置,他之后想要原文可以直接去看。
+
+ 结束:纪要交付,待办问过。他让我继续录下一场,就重新开始这个流程。
+ """.trimIndent(),
+ ),
+ en = Text(
+ title = "Meeting notes",
+ summary = "Records the meeting, turns it into a minutes page afterwards, and offers to set reminders for your action items.",
+ prompt = """
+ While I am in this scene, my job is to sit through the meeting for this person: record it, and give them minutes they can use straight away.
+
+ Before starting I ask two things in one sentence: what the meeting is called, and whether to begin now. When they say go, I first finish anything I need to say (the device stays silent while recording and cannot hear them), then call start_meeting with the name as title. After that I do not speak unprompted.
+
+ While recording I do not interrupt and I do not expect instructions. When they tap the ball to end it, the device hands me the verbatim transcript and a request for minutes; if I end it myself with stop_meeting, the transcript is in the tool result.
+
+ With the transcript, I write the minutes and show them with show_html, not just a chat reply. Four blocks:
+ - Conclusions and decisions: what was settled, one line each.
+ - Action items: who, what, by when. Where the transcript does not say who or when, I write "TBD" and do not fill it in for them.
+ - Open questions: discussed without a conclusion.
+ - Key quotes: only where needed, faithful to the meaning, not polished.
+
+ The transcript has no speaker labels, so I do not invent who said what. Where the speech is unclear or plainly misrecognised, I mark "(inaudible)" instead of guessing.
+
+ Once the minutes are up, I pick out the action items that belong to this person and ask once with ask_user: "Want me to remind you about these?" Only on a yes do I set reminders with todo, and I confirm each succeeded before saying so. I do not set reminders for other people's items.
+
+ The full transcript stays on the device. I tell them where it is, in case they want the original wording later.
+
+ Ending: minutes delivered and action items asked about. If they want me to record the next meeting, the flow starts again.
+ """.trimIndent(),
+ ),
+ )
+
+ private val TIMED_BOOKING = Preset(
+ name = "timed_booking",
+ zh = Text(
+ title = "到点替我上",
+ summary = "抢票、挂号、报名:到点替你进去办,付款和最后确认永远留给你。",
+ prompt = """
+ 当我处于这个场景中时,我的任务是在约定的时间,替这个人去抢一个名额:车票、门诊号、报名。我替他冲到最后一步之前,最后一步永远是他的。
+
+ 先把事情问清楚,缺一项都不开始:要办的是什么,在哪个 App,几点开放,要哪个场次或哪位医生,几张或几个人,备选方案是什么。同行人和就诊人,我只从那个 App 自己保存的常用乘客、就诊人里选;里面没有,我停下,请他自己在 App 里添加。证件号我不让他念给我听,也不往保险箱的备注里存——备注是大脑读得到的明文。
+
+ 设置时间时,用 todo,一次性,内容写成「进入情景 timed_booking,打开某 App 的某页,等开放」。设好、看到成功结果,才对他说「已经设好」。
+
+ 再把话说实在:看 todo 返回的 scheduled。如果是 inexact,说明系统闹钟可能晚几分钟,而抢的是秒级的名额,我不承诺准点,只说「大约那个时间」。这时我的建议是:他自己提前几分钟到场,我在他身边把页面准备好。我如实告诉他,由他决定。
+
+ 到点以后:
+ 1. 用 launch_app 打开那个 App,用 get_screen_element 读页面,进入目标页。
+ 2. 登录失效时,按保险箱的流程登录;需要验证码就用 read_sms 读,不把验证码说出口。
+ 3. 开放前页面常常没有入口,我用 wait_for_screen 等,每次比上次更久,不重复同一个数字。
+ 4. 入口一出现就点,选好场次、人数,一路填到「提交订单」或「确认挂号」之前。
+ 5. 到这一步,我停下,用 ask_user 问他一次,写清楚:什么、几点、多少钱。他同意,我才点最后那一下。付款一律交还给他,我不输入支付密码,不替他确认扣款。
+
+ 任何一步我读不到界面、被验证码挡住、或者页面出现我没预料到的东西,我停下,如实告诉他卡在哪,不乱点。同一处点了三次没有变化,就换思路或交还。
+
+ 没抢到就直说,并问他要不要试备选方案。绝不说「应该抢到了」。
+
+ 结束:订单提交了、确认没抢到、或他说算了。结束时用 remember 记一笔这次走通的路径(哪个 App、哪个页面、哪里会卡),下次更快。我清楚说出结果:成了,还是没成,下一步是什么。
+ """.trimIndent(),
+ ),
+ en = Text(
+ title = "Book it for me at the time",
+ summary = "Tickets, appointments, sign-ups: goes in at the set time and does the legwork. Payment and the final confirmation stay with you.",
+ prompt = """
+ While I am in this scene, my job is to go and grab a slot for this person at an agreed time: a ticket, a clinic appointment, a registration. I run up to the last step for them, and the last step is always theirs.
+
+ First I get everything pinned down, and I do not start with anything missing: what to book, in which app, when it opens, which session or doctor, how many people, and the fallback plan. For fellow travellers and patients I only pick from the app's own saved passengers or patients. If they are not there, I stop and ask them to add them in the app themselves. I do not have them read ID numbers to me, and they should not go in a vault note: notes are plain text the brain can read.
+
+ To schedule, I use todo, one-off, with text like "enter scene timed_booking, open that app's page, wait for it to open". Only after I see it succeeded do I tell them it is set.
+
+ Then I am honest about timing: I read `scheduled` in the todo result. If it says inexact, the system alarm may fire several minutes late, and slots like this go in seconds, so I do not promise punctuality and only say "around that time". In that case my suggestion is that they be there themselves a few minutes early while I get the page ready beside them. I tell them this and they decide.
+
+ When the time comes:
+ 1. launch_app to open the app, get_screen_element to read the page, and go to the target page.
+ 2. If the login has expired, I log in by the vault flow; if a code is needed I read it with read_sms and never say it aloud.
+ 3. Before opening, the page often has no entry yet. I use wait_for_screen, each wait longer than the last, never the same number twice.
+ 4. The moment the entry appears I tap it, choose the session and head count, and fill everything up to just before "Submit order" or "Confirm appointment".
+ 5. There I stop and ask once with ask_user, stating what, when and how much. Only on a yes do I tap the final button. Payment always goes back to them: I do not enter a payment password and I do not confirm a charge.
+
+ At any step where I cannot read the screen, am blocked by a captcha, or meet something I did not expect, I stop, tell them honestly where I am stuck, and do not tap blindly. Three taps on the same spot with no change means change approach or hand back.
+
+ If I did not get it, I say so and ask whether to try the fallback. I never say "it should have gone through".
+
+ Ending: the order is submitted, it is confirmed I did not get it, or they say forget it. At the end I remember one line about the route that worked (which app, which page, where it stalls) so next time is faster. I state the result plainly: done or not, and the next step.
+ """.trimIndent(),
+ ),
+ )
+
+ /** In the order the card offers them. */
+ val all: List<Preset> = listOf(CHAT_DUTY, SUBSCRIPTION_AUDIT, MEETING_NOTES, TIMED_BOOKING)
+}
diff --git a/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.kt b/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.kt
index 2564587..b3d9b72 100644
--- a/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.kt
+++ b/app/src/main/java/net/kuafuai/andee/config/VoiceConfig.kt
@@ -511,23 +511,28 @@ data class VoiceConfig(
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
.getString("scene", "").orEmpty().trim().ifEmpty { null }
- fun setActiveScene(context: Context, name: String?) {
+ /**
+ * [byBrain] is true only for the `scene.*` tools. It exists for one
+ * reader: the scene-ended event, which must not tell the brain what the
+ * brain itself just did (a second turn to say "you left the scene").
+ */
+ fun setActiveScene(context: Context, name: String?, byBrain: Boolean = false) {
val next = name?.trim().orEmpty()
val p = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
val prev = p.getString("scene", "").orEmpty()
if (prev == next) return
p.edit().putString("scene", next).apply()
- onSceneChanged?.invoke(prev.ifEmpty { null }, next.ifEmpty { null })
+ onSceneChanged?.invoke(prev.ifEmpty { null }, next.ifEmpty { null }, byBrain)
}
/**
- * `(left, entered)`, either null. Set by the service so the card's chip
- * and the scrollback follow whoever changed it — the brain's tool, the
- * scenes card, the chip's ✕ or an app trigger. Called on the writer's
- * thread.
+ * `(left, entered, byBrain)`, either name null. Set by the service so the
+ * card's chip and the scrollback follow whoever changed it — the brain's
+ * tool, the scenes card, the chip's ✕ or an app trigger. Called on the
+ * writer's thread.
*/
@Volatile
- var onSceneChanged: ((String?, String?) -> Unit)? = null
+ var onSceneChanged: ((String?, String?, Boolean) -> Unit)? = null
/**
* The 火山 speaker for the ball's current look, falling back to
diff --git a/app/src/main/java/net/kuafuai/andee/net/CommandDispatcher.kt b/app/src/main/java/net/kuafuai/andee/net/CommandDispatcher.kt
index 4726610..32f8160 100644
--- a/app/src/main/java/net/kuafuai/andee/net/CommandDispatcher.kt
+++ b/app/src/main/java/net/kuafuai/andee/net/CommandDispatcher.kt
@@ -1156,7 +1156,7 @@ class CommandDispatcher(
"scene.enter" -> {
val s = sceneParam(params, method)
- VoiceConfig.setActiveScene(appContext, s.name)
+ VoiceConfig.setActiveScene(appContext, s.name, byBrain = true)
Notebook.markSceneUsed(appContext, s.name)
JSONObject().put("active", s.name).put("title", s.title)
.put("note", "The scene's instructions apply from your next reply.")
@@ -1164,14 +1164,14 @@ class CommandDispatcher(
"scene.exit" -> {
val was = VoiceConfig.activeScene(appContext)
- VoiceConfig.setActiveScene(appContext, null)
+ VoiceConfig.setActiveScene(appContext, null, byBrain = true)
JSONObject().put("left", was ?: JSONObject.NULL).put("active", JSONObject.NULL)
}
"scene.delete" -> {
val s = sceneParam(params, method)
if (VoiceConfig.activeScene(appContext) == s.name) {
- VoiceConfig.setActiveScene(appContext, null)
+ VoiceConfig.setActiveScene(appContext, null, byBrain = true)
}
Notebook.deleteScene(appContext, s.name)
JSONObject().put("deleted", s.name)
diff --git a/app/src/main/java/net/kuafuai/andee/ui/ScenesUi.kt b/app/src/main/java/net/kuafuai/andee/ui/ScenesUi.kt
index 38f696d..f7ddba3 100644
--- a/app/src/main/java/net/kuafuai/andee/ui/ScenesUi.kt
+++ b/app/src/main/java/net/kuafuai/andee/ui/ScenesUi.kt
@@ -12,6 +12,7 @@ import android.widget.ScrollView
import android.widget.TextView
import net.kuafuai.andee.R
import net.kuafuai.andee.config.Notebook
+import net.kuafuai.andee.config.ScenePresets
import net.kuafuai.andee.config.VoiceConfig
import net.kuafuai.andee.i18n.AppLocale
@@ -144,12 +145,70 @@ class ScenesUi(private val context: Context) {
matchWrap(),
)
}
+ addPresets(body)
val scroll = ScrollView(context)
scroll.addView(body, matchWrap())
card.addView(scroll, LinearLayout.LayoutParams(MATCH, 0, 1f))
return card
}
+ /**
+ * The ready-made scenes this notebook does not hold yet.
+ *
+ * Offered below the user's own and only while some are left, so a card
+ * that has adopted everything is exactly the card it was before. Adopting
+ * writes in the interface language at that moment; see [ScenePresets].
+ */
+ private fun addPresets(body: LinearLayout) {
+ val left = ScenePresets.notAdopted(context)
+ if (left.isEmpty()) return
+ val english = VoiceConfig.uiLanguage(context) == VoiceConfig.LANG_EN
+ body.addView(
+ TextView(context).apply {
+ text = lctx.getString(R.string.scenes_presets_title)
+ textSize = Glass.Type.CAPTION
+ setTextColor(Color.parseColor(Glass.SECONDARY))
+ setPadding(dp(4), dp(18), 0, dp(2))
+ },
+ matchWrap(),
+ )
+ body.addView(
+ TextView(context).apply {
+ text = lctx.getString(R.string.scenes_presets_note)
+ textSize = Glass.Type.MICRO
+ setTextColor(Color.parseColor(Glass.MUTED))
+ setPadding(dp(4), 0, dp(4), dp(10))
+ },
+ matchWrap(),
+ )
+ for (p in left) {
+ val t = p.text(english)
+ val row = LinearLayout(context).apply {
+ orientation = LinearLayout.VERTICAL
+ setPadding(dp(14), dp(12), dp(14), dp(12))
+ background = Glass.panel(context, dp(14))
+ }
+ row.addView(TextView(context).apply {
+ text = t.title
+ textSize = Glass.Type.BODY
+ setTextColor(Color.parseColor(Glass.TITLE))
+ }, matchWrap())
+ row.addView(TextView(context).apply {
+ text = t.summary
+ textSize = Glass.Type.CAPTION
+ setTextColor(Color.parseColor(Glass.SECONDARY))
+ setPadding(0, dp(4), 0, dp(10))
+ }, matchWrap())
+ row.addView(
+ action(lctx.getString(R.string.scenes_preset_adopt), Glass.ACCENT) {
+ ScenePresets.adopt(context, p, english)
+ rebuild()
+ },
+ )
+ body.addView(row, matchWrap().apply { bottomMargin = dp(10) })
+ }
+ }
+
private fun header(): LinearLayout {
val h = LinearLayout(context).apply {
orientation = LinearLayout.HORIZONTAL
diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xml
index 5e3441b..43c9011 100644
--- a/app/src/main/res/values-en/strings.xml
+++ b/app/src/main/res/values-en/strings.xml
@@ -174,6 +174,9 @@
<string name="scenes_chip">Scene · %1$s</string>
<string name="scenes_entered_row">(Entered scene: %1$s)</string>
<string name="scenes_exited_row">(Left scene: %1$s)</string>
+ <string name="scenes_presets_title">Ready-made scenes</string>
+ <string name="scenes_presets_note">Nothing is saved until you tap Adopt. After that it is yours like any other scene: change it or delete it whenever you like.</string>
+ <string name="scenes_preset_adopt">Adopt</string>
<string name="tree_helper_enabled_row">(This app was hiding its screen; turned on the system\'s accessibility enhance service so it shows it again)</string>
<string name="settings_scenes_browse">Manage scenes</string>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 06f1de4..5ba7f6d 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -154,6 +154,10 @@
<!-- Scrollback rows written by the device itself when a scene starts or ends. -->
<string name="scenes_entered_row">(进入情景:%1$s)</string>
<string name="scenes_exited_row">(退出情景:%1$s)</string>
+ <!-- Ready-made scenes offered at the foot of the card; see config/ScenePresets.kt. -->
+ <string name="scenes_presets_title">现成的情景</string>
+ <string name="scenes_presets_note">点「采用」才会存进来,存好之后和你自己的情景一样,随时可以改、可以删。</string>
+ <string name="scenes_preset_adopt">采用</string>
<!-- Scrollback row: the device switched on a system accessibility helper so an app would expose its UI. -->
<string name="tree_helper_enabled_row">(这个应用把界面藏起来了,已打开系统的「无障碍增强」服务让它交出来)</string>
<string name="settings_scenes_browse">管理情景</string>
diff --git a/app/src/test/java/net/kuafuai/andee/config/ScenePresetsTest.kt b/app/src/test/java/net/kuafuai/andee/config/ScenePresetsTest.kt
new file mode 100644
index 0000000..53f31a2
--- /dev/null
+++ b/app/src/test/java/net/kuafuai/andee/config/ScenePresetsTest.kt
@@ -0,0 +1,40 @@
+package net.kuafuai.andee.config
+
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+/**
+ * A preset that breaks `Notebook.saveScene`'s limits would only fail when a
+ * user taps 采用, in the language they happen to have set. Checking both
+ * languages here moves that to the build.
+ */
+class ScenePresetsTest {
+
+ private val nameRule = Regex("[a-z0-9_]{1,40}")
+
+ @Test
+ fun `every preset fits the limits saveScene enforces, in both languages`() {
+ for (p in ScenePresets.all) {
+ assertTrue("name ${p.name}", nameRule.matches(p.name))
+ for (english in listOf(false, true)) {
+ val t = p.text(english)
+ val tag = "${p.name}/${if (english) "en" else "zh"}"
+ assertTrue("$tag title blank", t.title.isNotBlank())
+ assertTrue("$tag title over 40", t.title.length <= 40)
+ assertTrue("$tag summary blank", t.summary.isNotBlank())
+ assertTrue("$tag summary over 160", t.summary.length <= 160)
+ assertTrue("$tag prompt blank", t.prompt.isNotBlank())
+ assertTrue(
+ "$tag prompt is ${t.prompt.length} chars, limit ${Notebook.MAX_SCENE_PROMPT}",
+ t.prompt.length <= Notebook.MAX_SCENE_PROMPT,
+ )
+ }
+ }
+ }
+
+ @Test
+ fun `preset names are unique`() {
+ val names = ScenePresets.all.map { it.name }
+ assertTrue(names.size == names.toSet().size)
+ }
+}