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(desktop): the chat window shows topics, artifacts and scene offers

booboosui10dc61bf38e2
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 8e01500..0dff5ac 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ChatScreen.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ChatScreen.kt
@@ -63,10 +63,12 @@ fun ChatScreen(onOpenSettings: () -> Unit) {
var input by remember { mutableStateOf("") }
var voice by remember { mutableStateOf(net.kuafuai.andee.desktop.voice.MacVoice.State.IDLE) }
var heard by remember { mutableStateOf("") }
+ var artifacts by remember { mutableStateOf(Pages.artifacts()) }
val list = rememberLazyListState()
LaunchedEffect(Unit) {
Scrollback.addListener { rows = Scrollback.snapshot() }
+ Pages.onChange = { artifacts = Pages.artifacts() }
Assistant.onProgress = { progress = it }
Assistant.onFinal = { progress = "" }
Assistant.onBusy = { b -> busy = b; if (!b) progress = "" }
@@ -105,16 +107,35 @@ fun ChatScreen(onOpenSettings: () -> Unit) {
Pill("⚙") { onOpenSettings() }
}
Spacer(Modifier.height(12.dp))
+ if (artifacts.isNotEmpty()) {
+ ArtifactsPanel(
+ artifacts,
+ onOpen = { a -> runCatching { net.kuafuai.andee.desktop.mac.Bridge.open(a.file!!.absolutePath) } },
+ onRevise = { a -> Thread { Assistant.say("继续修改「${a.title}」。先 get_page(id=${a.id}) 读回源码,改好后用 show_html 重画,不要新建产物。") }.start() },
+ onRollback = { v ->
+ Pages.restoreVersion(v.id)
+ Scrollback.add(Scrollback.Role.ASSISTANT, "↩️ 已把「${v.title}」回退到 v${v.version}")
+ },
+ )
+ Spacer(Modifier.height(8.dp))
+ }
LazyColumn(state = list, modifier = Modifier.weight(1f).fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(8.dp)) {
if (rows.isEmpty()) {
item {
- Text(
- if (Strings.lang() == "zh") "还没有对话。说点什么吧。" else "Nothing here yet. Say something.",
- color = Palette.secondary, fontSize = 14.sp,
- )
+ SceneOffer { p ->
+ ScenePresets.adoptIfMissing(p.name)
+ Thread { Assistant.say("进入「${p.title}」情景。${p.example}") }.start()
+ }
+ }
+ }
+ items(displayItems(rows), key = { itemKey(it) }) { it ->
+ when (it) {
+ is Scrollback.Row -> Bubble(it)
+ is TopicRow -> TopicCard(it.topic, it.count) {
+ Thread { Assistant.say("继续聊「${it.topic.title}」。先 get_topic(id=${it.topic.id}) 读回对话再接着聊。") }.start()
+ }
}
}
- items(rows, key = { it.id }) { r -> Bubble(r) }
}
if (heard.isNotEmpty()) {
Text("🎤 $heard", color = Palette.text, fontSize = 14.sp, modifier = Modifier.padding(vertical = 6.dp))
@@ -147,6 +168,29 @@ fun ChatScreen(onOpenSettings: () -> Unit) {
}
}
+/** One folded stretch rendered as a single card, at the position of its first row. */
+private data class TopicRow(val topic: Scrollback.Topic, val count: Int)
+
+private fun displayItems(rows: List<Scrollback.Row>): List<Any> {
+ val topics = Scrollback.topics().associateBy { it.id }
+ val seen = mutableSetOf<Long>()
+ val out = mutableListOf<Any>()
+ for (r in rows) {
+ val tid = r.topicId
+ if (tid == null) { out += r; continue }
+ if (seen.add(tid)) {
+ topics[tid]?.let { out += TopicRow(it, rows.count { x -> x.topicId == tid }) }
+ }
+ }
+ return out
+}
+
+private fun itemKey(it: Any): Any = when (it) {
+ is Scrollback.Row -> "r${it.id}"
+ is TopicRow -> "t${it.topic.id}"
+ else -> it
+}
+
fun noVoiceKey() =
if (Strings.lang() == "zh") "还没配语音 Key:在 ⚙ 设置的「语音」里填上。" else "No voice key yet: add it under Voice in ⚙ Settings."
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ChatUi.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ChatUi.kt
new file mode 100644
index 0000000..48453f3
--- /dev/null
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ChatUi.kt
@@ -0,0 +1,132 @@
+package net.kuafuai.andee.desktop
+
+import androidx.compose.foundation.background
+import androidx.compose.foundation.border
+import androidx.compose.foundation.clickable
+import androidx.compose.foundation.horizontalScroll
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Row
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.width
+import androidx.compose.foundation.rememberScrollState
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.foundation.text.selection.SelectionContainer
+import androidx.compose.material.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import java.text.SimpleDateFormat
+import java.util.Date
+import java.util.Locale
+
+/** A folded conversation, collapsed to one card: 继续话题 / 对话记录. */
+@Composable
+fun TopicCard(topic: Scrollback.Topic, count: Int, onContinue: () -> Unit) {
+ var expanded by remember { mutableStateOf(false) }
+ val fmt = SimpleDateFormat("MM-dd HH:mm", Locale.US)
+ Column(
+ Modifier.fillMaxWidth().background(Palette.panel, RoundedCornerShape(14.dp))
+ .border(1.dp, Palette.hairline, RoundedCornerShape(14.dp)).padding(12.dp),
+ verticalArrangement = Arrangement.spacedBy(8.dp),
+ ) {
+ Row(verticalAlignment = androidx.compose.ui.Alignment.CenterVertically) {
+ Text("🗂 " + topic.title, color = Palette.text, fontSize = 14.sp)
+ Spacer(Modifier.weight(1f))
+ Text("${fmt.format(Date(topic.endedAt))} · $count msgs", color = Palette.secondary, fontSize = 11.sp)
+ }
+ if (topic.summary.isNotBlank()) {
+ Text(topic.summary, color = Palette.secondary, fontSize = 12.sp)
+ }
+ if (expanded) {
+ SelectionContainer {
+ Text(
+ Scrollback.topicText(topic.id) ?: "(记录已不在)",
+ color = Palette.text, fontSize = 12.sp,
+ modifier = Modifier.fillMaxWidth().background(Palette.well, RoundedCornerShape(10.dp)).padding(10.dp),
+ )
+ }
+ }
+ Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
+ Pill(t("继续话题", "Continue")) { onContinue() }
+ Pill(if (expanded) t("收起记录", "Hide transcript") else t("对话记录", "Transcript")) { expanded = !expanded }
+ }
+ }
+}
+
+/** The 产物 shelf: one card per artifact, with open / continue / versions. */
+@Composable
+fun ArtifactsPanel(artifacts: List<Pages.Page>, onOpen: (Pages.Page) -> Unit, onRevise: (Pages.Page) -> Unit, onRollback: (Pages.Page) -> Unit) {
+ if (artifacts.isEmpty()) return
+ Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
+ Text(t("产物", "Artifacts"), color = Palette.secondary, fontSize = 13.sp)
+ Row(
+ Modifier.horizontalScroll(rememberScrollState()),
+ horizontalArrangement = Arrangement.spacedBy(8.dp),
+ ) {
+ for (a in artifacts) {
+ ArtifactCard(a, onOpen, onRevise, onRollback)
+ }
+ }
+ }
+}
+
+@Composable
+private fun ArtifactCard(a: Pages.Page, onOpen: (Pages.Page) -> Unit, onRevise: (Pages.Page) -> Unit, onRollback: (Pages.Page) -> Unit) {
+ var showVersions by remember { mutableStateOf(false) }
+ Column(
+ Modifier.width(200.dp).background(Palette.panel, RoundedCornerShape(12.dp))
+ .border(1.dp, Palette.hairline, RoundedCornerShape(12.dp)).padding(10.dp),
+ verticalArrangement = Arrangement.spacedBy(6.dp),
+ ) {
+ Text("📄 " + a.title, color = Palette.text, fontSize = 13.sp)
+ Text("v${a.version}", color = Palette.secondary, fontSize = 11.sp)
+ Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {
+ Pill(t("打开", "Open")) { onOpen(a) }
+ Pill(t("继续", "Edit")) { onRevise(a) }
+ Pill(t("版本", "Versions")) { showVersions = !showVersions }
+ }
+ if (showVersions) {
+ for (v in Pages.versionsOf(a.groupId)) {
+ Row(verticalAlignment = androidx.compose.ui.Alignment.CenterVertically) {
+ Text("v${v.version}${if (v.file == null) "(没了)" else ""}", color = if (v.id == a.id) Palette.text else Palette.secondary, fontSize = 11.sp)
+ Spacer(Modifier.weight(1f))
+ if (v.id != a.id && v.file != null) {
+ Text(t("回退", "Roll back"), color = Palette.accent, fontSize = 11.sp, modifier = Modifier.clickable { onRollback(v) })
+ }
+ }
+ }
+ }
+ }
+}
+
+private fun t(zh: String, en: String) = if (Strings.lang() == "zh") zh else en
+
+/** An empty chat offers scenes instead of sitting blank — the phone's presets bubbles. */
+@Composable
+fun SceneOffer(onPick: (ScenePresets.Preset) -> Unit) {
+ Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
+ Text(t("还没有对话。说点什么,或选一个情景:", "Nothing here yet. Say something, or pick a scene:"), color = Palette.secondary, fontSize = 13.sp)
+ Column(verticalArrangement = Arrangement.spacedBy(6.dp)) {
+ for (p in ScenePresets.ALL) {
+ Column(
+ Modifier.fillMaxWidth().background(Palette.panel, RoundedCornerShape(12.dp))
+ .border(1.dp, Palette.hairline, RoundedCornerShape(12.dp))
+ .clickable { onPick(p) }.padding(12.dp),
+ ) {
+ Text(p.title, color = Palette.text, fontSize = 14.sp)
+ Text(p.summary, color = Palette.secondary, fontSize = 12.sp)
+ }
+ }
+ }
+ }
+}
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Pages.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Pages.kt
index d69f2e2..c4d1028 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Pages.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Pages.kt
@@ -44,6 +44,9 @@ object Pages {
/** The id a `show_html` in this turn should file under; null = new artifact. Set by get_page, cleared each turn. */
@Volatile private var armedGroup: Long? = null
+ /** Fired on the UI thread's behalf after the shelf changes; the panel reads [artifacts] on it. */
+ @Volatile var onChange: () -> Unit = {}
+
fun beginTurn() { armedGroup = null }
fun armGroup(id: Long) { armedGroup = id }
@@ -71,6 +74,7 @@ object Pages {
trim()
save()
}
+ onChange()
return Page(id, System.currentTimeMillis(), title, group, version, file)
}
@@ -81,6 +85,13 @@ object Pages {
.sortedByDescending { it.ts }
}
+ /** The panel's list: one row per artifact, only ones whose bytes are still there (a row that opens nothing is worse than a shorter list). */
+ fun artifacts(): List<Page> = synchronized(lock) {
+ entries.groupBy { it.groupId }.values
+ .mapNotNull { revs -> revs.filter { it.file != null }.maxByOrNull { it.version } }
+ .sortedByDescending { it.ts }
+ }
+
/** Any revision is addressable, not just the current one. */
fun pageById(id: Long): Page? = synchronized(lock) { entries.firstOrNull { it.id == id } }
@@ -92,6 +103,14 @@ object Pages {
entries.filter { it.groupId == groupId && it.file != null }.maxByOrNull { it.version }
}
+ /** Make an older revision current again — as a new revision, so the history stays append-only. */
+ fun restoreVersion(id: Long): Page? {
+ val source = synchronized(lock) { entries.firstOrNull { it.id == id } } ?: return null
+ val file = source.file ?: return null
+ val html = runCatching { file.readText() }.getOrNull() ?: return null
+ return add(source.title, html, groupId = source.groupId)
+ }
+
private fun nextVersionOf(groupId: Long): Int = synchronized(lock) {
entries.filter { it.groupId == groupId }.maxOfOrNull { it.version }?.plus(1) ?: 1
}
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ScenePresets.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ScenePresets.kt
new file mode 100644
index 0000000..324263c
--- /dev/null
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ScenePresets.kt
@@ -0,0 +1,57 @@
+package net.kuafuai.andee.desktop
+
+/**
+ * Ready-made scenes offered on an empty chat — the phone's 情景 bubbles, on the
+ * Mac. Picking one is the "yes": it writes the scene into the notebook and then
+ * sends 「进入…情景」 as a normal turn, so the model still decides whether to step
+ * in. A preset is not a scene until the user picks it.
+ */
+object ScenePresets {
+
+ data class Preset(val name: String, val title: String, val example: String, val summary: String, val prompt: String)
+
+ val ALL = listOf(
+ Preset(
+ name = "chat_duty",
+ title = "消息值班台",
+ example = "帮我盯着通知,要紧的马上告诉我,其余的先别烦我",
+ summary = "盯着聊天应用的通知,要紧的立刻告诉你。",
+ prompt = "当我处于这个场景中时,我是这个人所有聊天应用通知的值班台。我读的是 get_notifications,它是跨应用的。" +
+ "每一轮从最新开始:需要现在处理的是真人的直接提问、@ 提及、带时间的截止事项、少数重要名字的消息;" +
+ "可以等的是一般群聊、简报、订阅推送。有重要的就立刻告诉用户,一条一行;其余保持安静。",
+ ),
+ Preset(
+ name = "english",
+ title = "陪我练英语",
+ example = "陪我练英语,用简单一点的话,错了帮我纠正",
+ summary = "用英语对话,纠正我的错误,别用太难的词。",
+ prompt = "当我处于这个场景中时,我们用英语对话。我用词要简单,句子要短;用户说错了就温和地纠正,给出正确的说法。" +
+ "除非用户切回中文,否则一直用英语。",
+ ),
+ Preset(
+ name = "shopping",
+ title = "比价购物",
+ example = "帮我在几个购物网站比价,找最划算的",
+ summary = "跨网站比价,给出最划算的选择。",
+ prompt = "当我处于这个场景中时,用户想买东西。我打开相关的购物网站或搜索结果页,比较价格、运费和优惠,给出最划算的选择。" +
+ "下单、付款前必须先问用户确认。",
+ ),
+ )
+
+ fun byName(name: String): Preset? = ALL.firstOrNull { it.name == name }
+
+ /** Write into the notebook if it is not there already; safe to tap twice. */
+ fun adoptIfMissing(name: String): Boolean {
+ val p = byName(name) ?: return false
+ if (DesktopNotebook.book.scene(p.name) != null) return false
+ val english = Strings.lang() == "en"
+ DesktopNotebook.book.saveScene(
+ name = p.name,
+ title = p.title,
+ summary = p.summary,
+ prompt = p.prompt,
+ triggerApps = emptyList(),
+ )
+ return true
+ }
+}