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): ball captions, stop on tap, prompts beside the ball, and fixes

booboosui87349b9743ba
diff --git a/app/src/main/java/net/kuafuai/andee/text/Markdown.kt b/core/src/main/kotlin/net/kuafuai/andee/text/Markdown.kt
similarity index 100%
rename from app/src/main/java/net/kuafuai/andee/text/Markdown.kt
rename to core/src/main/kotlin/net/kuafuai/andee/text/Markdown.kt
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/App.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/App.kt
index 8f32878..be1c2bb 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/App.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/App.kt
@@ -13,11 +13,11 @@ import androidx.compose.ui.unit.dp
/**
* The window body: one photograph behind everything, the sidebar and the content
- * as glass on it, the approval and question cards above both. Its own function so
+ * as glass on it, (the approval and question cards are their own window beside the ball). Its own function so
* the screenshot harness draws exactly what the app draws.
*/
@Composable
-fun AppRoot(nav: Nav, pending: ApprovalGate.Pending?, ask: AskGate.Pending?) {
+fun AppRoot(nav: Nav) {
Box(Modifier.fillMaxSize()) {
BackdropLayer()
Row(Modifier.fillMaxSize()) {
@@ -28,27 +28,24 @@ fun AppRoot(nav: Nav, pending: ApprovalGate.Pending?, ask: AskGate.Pending?) {
val card = if (screen == "chat") Modifier else Modifier.padding(14.dp).frostedCard()
Box(Modifier.widthIn(max = 900.dp).fillMaxHeight().then(card)) {
when {
- screen == "settings" -> SettingsScreen(
- onDone = { nav.back() },
- onOpenNotebook = { nav.push("notebook") },
- onOpenVault = { nav.push("vault") },
- onOpenScenes = { nav.push("scenes") },
- onOpenExpressions = { nav.push("expressions") },
- onOpenSelfCheck = { nav.push("selfcheck") },
- )
+ screen == "settings" -> SettingsScreen(onDone = { nav.back() })
screen == "selfcheck" -> SelfCheckCard(onDone = { nav.back() }, onOpenSettings = { nav.push("settings") })
screen == "notebook" -> NotebookCard(onDone = { nav.back() })
screen == "vault" -> VaultCard(onDone = { nav.back() })
screen == "scenes" -> ScenesCard(onDone = { nav.back() })
screen == "expressions" -> ExpressionsCard(onDone = { nav.back() })
- screen == "artifacts" -> ArtifactsCard(onDone = { nav.back() })
- screen.startsWith("topic:") -> TopicScreen(screen.removePrefix("topic:").toLongOrNull() ?: -1L, onDone = { nav.back() })
- else -> ChatScreen(onOpenArtifacts = { nav.go("artifacts") })
+ screen == "artifacts" -> ArtifactsCard(onDone = { nav.back() }, onContinue = { nav.go("chat"); ComposerFocus.pending = true })
+ screen.startsWith("topic:") -> TopicScreen(screen.removePrefix("topic:").toLongOrNull() ?: -1L, onDone = { nav.back() },
+ onContinue = { nav.go("chat"); ComposerFocus.pending = true })
+ else -> ChatScreen(
+ onOpenArtifacts = { nav.go("artifacts") },
+ onOpenTopic = { nav.push("topic:$it") },
+ // 继续话题 from the card itself: the same two steps as on the topic page.
+ onContinueTopic = { Assistant.beginTopic(it); ComposerFocus.pending = true },
+ )
}
}
}
}
- pending?.let { ApprovalCard(it) }
- ask?.let { AskCard(it) }
}
}
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ApprovalCard.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ApprovalCard.kt
index 2a2a1ab..522c600 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ApprovalCard.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ApprovalCard.kt
@@ -80,9 +80,10 @@ fun ApprovalCard(p: ApprovalGate.Pending) {
Tier.MEDIUM -> Color(0xFFFFD60A)
else -> Palette.secondary
}
- Box(Modifier.fillMaxSize().background(Color(0xB3000000)), contentAlignment = Alignment.Center) {
+ // Just the card: it lives in its own window beside the ball (PromptWindow), not over the chat.
+ run {
Column(
- Modifier.widthIn(max = 460.dp).fillMaxWidth().padding(20.dp)
+ Modifier.fillMaxWidth()
.background(Palette.card, RoundedCornerShape(18.dp))
.border(1.dp, Palette.hairline, RoundedCornerShape(18.dp)).padding(20.dp),
verticalArrangement = Arrangement.spacedBy(10.dp),
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 2ec0493..fc815b2 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Assistant.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Assistant.kt
@@ -21,8 +21,22 @@ object Assistant {
@Volatile var onBusy: (Boolean) -> Unit = {}
@Volatile var onSceneChanged: (String?) -> Unit = {}
- /** Speak answers aloud: on after the user spoke, off after they typed (a typed question wants a quiet answer). */
- @Volatile var speakReplies = false
+ /**
+ * Answers are read aloud however the question came in — the phone does the same, and a voice that
+ * goes quiet when you start typing reads as broken rather than polite. Quiet is the user's call:
+ * the speaker button in the chat header, remembered across launches.
+ */
+ @Volatile var muted: Boolean = Settings.get("mute") == "1"
+ private set
+ @Volatile var onMuteChanged: () -> Unit = {}
+
+ fun setMuted(m: Boolean) {
+ muted = m
+ Settings.save(mapOf("mute" to if (m) "1" else "0"))
+ // Muting means now, not after the sentence it was already saying.
+ if (m) net.kuafuai.andee.desktop.voice.MacVoice.stopSpeaking()
+ onMuteChanged()
+ }
/**
* Shows a command for approval and returns the button pressed. Set by the
@@ -43,9 +57,12 @@ object Assistant {
/** Hides Andee's own windows for the instant of a click; set by the window. */
@Volatile var hideWindows: (Boolean) -> Unit = {}
+ /** Gets the chat window out of the way before a screen tool runs; set by the window. */
+ @Volatile var yieldScreen: () -> Unit = {}
+
/** The screen hand: accessibility tree, clicks, keys and screenshots. */
val screen: net.kuafuai.andee.desktop.mac.MacScreen by lazy {
- net.kuafuai.andee.desktop.mac.MacScreen { hide -> hideWindows(hide) }
+ net.kuafuai.andee.desktop.mac.MacScreen({ hide -> hideWindows(hide) }, { yieldScreen() })
}
/** Apps, calendar, reminders, contacts, volume, battery and the vault. */
@@ -111,14 +128,15 @@ object Assistant {
val b = LocalBrain(
cfg = cfg,
host = DesktopBrainHost(providers(), MacPrompt.Capabilities(shell = true, screen = true, voice = true)),
- onProgress = { onProgress(it) },
+ onProgress = { BallCaption.push(it); onProgress(it) },
onFinal = { text ->
val clean = net.kuafuai.andee.brain.SpeechMood.parse(text).text.replace("[END]", "").trim()
Scrollback.add(Scrollback.Role.ASSISTANT, clean)
MacBall.onAnswer(text)
+ BallCaption.clear()
onFinal(clean)
busy(false)
- if (speakReplies) net.kuafuai.andee.desktop.voice.MacVoice.speak(text)
+ if (!muted) net.kuafuai.andee.desktop.voice.MacVoice.speak(text)
},
onTurnEnd = { busy(false) },
onSweepSpent = { Sweep.spent(it) },
@@ -155,7 +173,7 @@ object Assistant {
progressClear()
}
- private fun progressClear() { onProgress("") }
+ private fun progressClear() { BallCaption.clear(); onProgress("") }
/** The quiet-hour review (also the sidebar's 整理 button). Silent: no answer, no voice, no task light. */
fun runSweep() {
@@ -166,20 +184,101 @@ object Assistant {
/** A turn started or ended: the window's stop button and the ball's working face. */
private fun busy(b: Boolean) {
MacBall.onBusy(b)
+ if (b) BallCaption.thinking() else BallCaption.clear()
onBusy(b)
}
- fun say(text: String, spoken: Boolean = false) {
- speakReplies = spoken
+ /** What the user tapped 继续 on and has not yet said what to do with. Cleared by the next words, or by stop. */
+ sealed class Pending {
+ data class Edit(val page: Pages.Page) : Pending()
+ data class Topic(val topic: Scrollback.Topic) : Pending()
+ }
+ @Volatile var pending: Pending? = null
+ private set
+ @Volatile var onPendingChanged: () -> Unit = {}
+
+ fun beginEdit(page: Pages.Page) { pending = Pending.Edit(page); onPendingChanged() }
+ fun beginTopic(topic: Scrollback.Topic) { pending = Pending.Topic(topic); onPendingChanged() }
+ fun cancelPending() { if (pending != null) { pending = null; onPendingChanged() } }
+
+ fun say(text: String, attachments: List<Attachment> = emptyList()) {
val t = text.trim()
- if (t.isEmpty()) return
- Scrollback.add(Scrollback.Role.USER, t)
+ val photos = attachments.filterIsInstance<Attachment.Photo>()
+ val files = attachments.filterIsInstance<Attachment.FileRef>()
+ // Pictures alone are a complete message ("what is this"); so are files.
+ if (t.isEmpty() && attachments.isEmpty()) return
+ // A 继续 waiting for its words: the scrollback gets what they said, the brain gets that plus
+ // what it is about. The artifact is named here, before the brain can call show_html, so the
+ // revision is filed under it without the model having to keep an id straight.
+ val p = pending
+ pending = null
+ if (p != null) onPendingChanged()
+ val zh = Strings.lang() == "zh"
+ val row = when (p) {
+ is Pending.Edit -> if (zh) "修改「${p.page.title}」:$t" else "Revise “${p.page.title}”: $t"
+ is Pending.Topic -> if (zh) "继续「${p.topic.title}」:$t" else "Continue “${p.topic.title}”: $t"
+ null -> t
+ }
+ // The record says what was sent in words, since a picture has no text to show.
+ val zh0 = Strings.lang() == "zh"
+ val tag = buildList {
+ if (photos.isNotEmpty()) add(if (zh0) "[${photos.size} 张图片]" else "[${photos.size} picture${if (photos.size > 1) "s" else ""}]")
+ files.forEach { add("[${it.label}]") }
+ }.joinToString(" ")
+ Scrollback.add(Scrollback.Role.USER, listOf(tag, row).filter { it.isNotEmpty() }.joinToString(" "))
busy(true)
- ensure().submit(t)
+ // submit() begins the turn, which clears the revision target; so arm after, from the turn's own start.
+ val forBrain = when (p) {
+ is Pending.Edit -> { Pages.armNextTurn(p.page.groupId); editQuery(p.page, row) }
+ is Pending.Topic -> topicQuery(p.topic, row)
+ null -> t
+ }
+ val withFiles = if (files.isEmpty()) forBrain else forBrain + Attachments.fileNote(files)
+ ensure().submit(withFiles, photos.map { it.jpegBase64 })
}
+ private fun editQuery(page: Pages.Page, said: String): String {
+ val cap = net.kuafuai.andee.ui.ScrollbackLimits.ARTIFACT_SOURCE_MAX
+ val html = page.file?.let { runCatching { it.readText() }.getOrNull() }.orEmpty()
+ val cut = html.length > cap
+ return buildString {
+ append(said)
+ append("\n\n(I picked this from the Artifacts panel: it is about the page you made earlier titled \"")
+ append(page.title)
+ append("\". Apply my change above to that page and show the full revised page with show_html,")
+ append(" keeping everything I did not ask to change. Its current source follows")
+ if (cut) append(" — truncated at $cap characters; rebuild the missing tail in the same style")
+ append(".)\n\n<<<CURRENT PAGE SOURCE>>>\n")
+ append(if (cut) html.take(cap) else html)
+ append("\n<<<END PAGE SOURCE>>>")
+ }
+ }
+
+ private fun topicQuery(t: Scrollback.Topic, said: String): String {
+ val cap = net.kuafuai.andee.ui.ScrollbackLimits.TOPIC_TEXT_MAX
+ val text = Scrollback.topicText(t.id)
+ return buildString {
+ append(said)
+ append("\n\n(I picked this from a finished topic in our conversation, topic #").append(t.id)
+ append(" \"").append(t.title).append("\", and want to carry on with it. What it came to: ").append(t.summary)
+ if (text == null) {
+ append("\nIts full text is no longer stored — work from the summary and say so if a detail is missing.)")
+ } else {
+ val cut = text.length > cap
+ append("\nThe conversation as it happened follows")
+ if (cut) append(" — cut at $cap characters, the beginning only")
+ append(".)\n\n<<<TOPIC CONVERSATION>>>\n").append(if (cut) text.take(cap) else text)
+ append("\n<<<END TOPIC CONVERSATION>>>")
+ }
+ }
+ }
+
+ /** 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()
+
/** The user's stop: the turn ends, and every command it started ends with it. */
fun stop() {
+ cancelPending()
brain?.cancel()
shell.stopAll()
net.kuafuai.andee.desktop.voice.MacVoice.stopSpeaking()
@@ -197,11 +296,10 @@ object Assistant {
val clean = net.kuafuai.andee.brain.SpeechMood.parse(line).text.trim()
Scrollback.add(Scrollback.Role.ASSISTANT, clean)
onFinal(clean)
- net.kuafuai.andee.desktop.voice.MacVoice.speak(line)
+ if (!muted) net.kuafuai.andee.desktop.voice.MacVoice.speak(line)
}
override fun act(text: String) {
busy(true)
- speakReplies = true
ensure().notified(text)
}
}
@@ -212,7 +310,6 @@ object Assistant {
// The user ended the recording themselves, so the brain does not have the
// transcript — ask it for the minutes on a fresh turn (the phone's asr.final path).
busy(true)
- speakReplies = false
ensure().submit(meetingMinutesQuery(payload))
}
Meeting.onRecordingChanged = { MacBall.onRecording(it) }
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Attachments.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Attachments.kt
new file mode 100644
index 0000000..544cd7a
--- /dev/null
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Attachments.kt
@@ -0,0 +1,140 @@
+package net.kuafuai.andee.desktop
+
+import androidx.compose.ui.graphics.ImageBitmap
+import androidx.compose.ui.graphics.toComposeImageBitmap
+import net.kuafuai.andee.util.Log
+import org.jetbrains.skia.Bitmap
+import org.jetbrains.skia.Canvas
+import org.jetbrains.skia.EncodedImageFormat
+import org.jetbrains.skia.Image
+import org.jetbrains.skia.Paint
+import org.jetbrains.skia.SamplingMode
+import java.io.File
+import java.util.Base64
+
+/**
+ * What the user attaches to a message. A picture goes to the model as pixels;
+ * a file goes as a path, because the model cannot read bytes it is not shown and
+ * `run_command` already reads anything outside the credentials.
+ */
+sealed class Attachment {
+ abstract val label: String
+
+ /** A picture ready to send: [jpegBase64] is what the brain gets, [thumb] is what the strip shows. */
+ class Photo(override val label: String, val jpegBase64: String, val thumb: ImageBitmap) : Attachment()
+
+ /** A file the model is pointed at. Nothing is copied: the original is where `run_command` can read it. */
+ class FileRef(val file: File) : Attachment() {
+ override val label: String get() = file.name
+ val bytes: Long get() = file.length()
+ }
+}
+
+object Attachments {
+
+ private const val TAG = "Attachments"
+
+ /** Same ceiling as the phone's picker. */
+ const val MAX = 6
+ private const val LONG_EDGE = 1568
+ private const val JPEG_QUALITY = 85
+ private const val THUMB_EDGE = 112
+
+ private val IMAGE_EXT = setOf("png", "jpg", "jpeg", "gif", "webp", "bmp", "heic", "tif", "tiff")
+
+ fun isImage(f: File) = f.extension.lowercase() in IMAGE_EXT
+
+ /** A picked or dropped file becomes a photo if it decodes as one, else a file reference. Null if unreadable. */
+ fun fromFile(f: File): Attachment? {
+ if (!f.isFile || !f.canRead()) return null
+ if (isImage(f)) fromBytes(f.name, runCatching { f.readBytes() }.getOrNull() ?: return null)?.let { return it }
+ return Attachment.FileRef(f)
+ }
+
+ /**
+ * Decode (upright — a phone photo is stored sideways with a flag, and the model must not be
+ * shown it rotated), shrink, and re-encode as JPEG.
+ */
+ fun fromBytes(label: String, bytes: ByteArray): Attachment.Photo? = runCatching {
+ // The decoder applies the EXIF orientation itself (a portrait phone photo arrives 200 wide, 400
+ // tall). Applying the tag again on top is a second rotation that cancels the first, which is
+ // exactly what this did until a test caught it.
+ val image = Image.makeFromEncoded(bytes)
+ val upright = Bitmap().also {
+ it.allocN32Pixels(image.width, image.height)
+ check(image.readPixels(it)) { "the decoder returned no pixels" }
+ }
+ val scaled = shrink(upright, LONG_EDGE)
+ val jpeg = Image.makeFromBitmap(scaled).encodeToData(EncodedImageFormat.JPEG, JPEG_QUALITY)
+ ?: return null
+ val thumb = Image.makeFromBitmap(shrink(scaled, THUMB_EDGE)).toComposeImageBitmap()
+ Attachment.Photo(label, Base64.getEncoder().encodeToString(jpeg.bytes), thumb)
+ }.onFailure { Log.w(TAG, "could not read $label as a picture: ${it.message}") }.getOrNull()
+
+ private fun shrink(src: Bitmap, edge: Int): Bitmap {
+ val long = maxOf(src.width, src.height)
+ if (long <= edge) return src
+ val k = edge.toFloat() / long
+ val w = maxOf(1, (src.width * k).toInt())
+ val h = maxOf(1, (src.height * k).toInt())
+ val out = Bitmap().also { it.allocN32Pixels(w, h) }
+ Canvas(out).drawImageRect(
+ Image.makeFromBitmap(src),
+ org.jetbrains.skia.Rect.makeWH(src.width.toFloat(), src.height.toFloat()),
+ org.jetbrains.skia.Rect.makeWH(w.toFloat(), h.toFloat()),
+ SamplingMode.LINEAR, Paint(), true,
+ )
+ return out
+ }
+
+ /** The picture on the system clipboard, if there is one — a screenshot copied with ⌃⌘⇧4, or an image copied from a page. */
+ fun fromClipboard(): Attachment.Photo? = runCatching {
+ val cb = java.awt.Toolkit.getDefaultToolkit().systemClipboard
+ if (!cb.isDataFlavorAvailable(java.awt.datatransfer.DataFlavor.imageFlavor)) return null
+ val img = cb.getData(java.awt.datatransfer.DataFlavor.imageFlavor) as? java.awt.Image ?: return null
+ val w = img.getWidth(null); val h = img.getHeight(null)
+ if (w <= 0 || h <= 0) return null
+ val buf = java.awt.image.BufferedImage(w, h, java.awt.image.BufferedImage.TYPE_INT_ARGB)
+ buf.createGraphics().apply { drawImage(img, 0, 0, null); dispose() }
+ val out = java.io.ByteArrayOutputStream()
+ javax.imageio.ImageIO.write(buf, "png", out)
+ fromBytes("pasted", out.toByteArray())
+ }.getOrNull()
+
+ /** Files copied in Finder (⌘C on a file) arrive as a list, not as data. */
+ fun filesFromClipboard(): List<File> = runCatching {
+ val cb = java.awt.Toolkit.getDefaultToolkit().systemClipboard
+ if (!cb.isDataFlavorAvailable(java.awt.datatransfer.DataFlavor.javaFileListFlavor)) return emptyList()
+ @Suppress("UNCHECKED_CAST")
+ (cb.getData(java.awt.datatransfer.DataFlavor.javaFileListFlavor) as List<File>)
+ }.getOrDefault(emptyList())
+
+ /** The native open panel. Blocking: call off the UI thread. */
+ fun choose(): List<File> {
+ val d = java.awt.FileDialog(null as java.awt.Frame?, if (Strings.lang() == "zh") "选择图片或文件" else "Choose pictures or files", java.awt.FileDialog.LOAD)
+ d.isMultipleMode = true
+ d.isVisible = true
+ return d.files.toList()
+ }
+
+ fun size(bytes: Long): String = when {
+ bytes >= 1 shl 20 -> "%.1f MB".format(bytes / (1024.0 * 1024.0))
+ bytes >= 1024 -> "${bytes / 1024} KB"
+ else -> "$bytes B"
+ }
+
+ /**
+ * What the brain is told about the files. English like every device-composed prompt, so its own
+ * language rule decides the reply from the user's words. The path is the capability; the
+ * sentence says what it is, so "这个文件" resolves without a guess.
+ */
+ fun fileNote(files: List<Attachment.FileRef>): String = buildString {
+ append("\n\n(The user attached ").append(if (files.size == 1) "a file" else "${files.size} files")
+ append(" to the message above. You cannot see inside ").append(if (files.size == 1) "it" else "them")
+ append(" from here: read ").append(if (files.size == 1) "it" else "them")
+ append(" with run_command if the request needs the contents — cat or head for text, file/mdls for a type, ")
+ append("textutil -convert txt for a document. \"这个文件 / this file\" means:\n")
+ for (f in files) append("- ").append(f.file.absolutePath).append(" (").append(size(f.bytes)).append(")\n")
+ append(")")
+ }
+}
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/BallCaption.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/BallCaption.kt
new file mode 100644
index 0000000..3820dfd
--- /dev/null
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/BallCaption.kt
@@ -0,0 +1,176 @@
+package net.kuafuai.andee.desktop
+
+import androidx.compose.animation.core.animateFloatAsState
+import androidx.compose.animation.core.tween
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.Column
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.draw.alpha
+import androidx.compose.animation.core.animateDpAsState
+import androidx.compose.foundation.layout.offset
+import androidx.compose.runtime.key
+import androidx.compose.ui.draw.BlurredEdgeTreatment
+import androidx.compose.ui.draw.blur
+import androidx.compose.ui.draw.drawWithContent
+import androidx.compose.ui.graphics.BlendMode
+import androidx.compose.ui.graphics.Brush
+import androidx.compose.ui.graphics.CompositingStrategy
+import androidx.compose.ui.graphics.graphicsLayer
+import androidx.compose.ui.text.TextStyle
+import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.padding
+import androidx.compose.material.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
+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.text.style.TextOverflow
+import androidx.compose.ui.unit.DpSize
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import androidx.compose.ui.window.ApplicationScope
+import androidx.compose.ui.window.Window
+import androidx.compose.ui.window.WindowPosition
+import androidx.compose.ui.window.rememberWindowState
+import kotlinx.coroutines.delay
+import java.awt.GraphicsEnvironment
+
+/** The caption window's title; the native side finds the window by it. */
+const val CAPTION_TITLE = "Andee Caption"
+
+/**
+ * What the ball is doing, in a few short lines beside it: the model's own words between steps and
+ * the one-line narration of each tool. Not the whole transcript (the chat window has that), just
+ * enough that a ball glowing for twenty seconds is not a mystery. The newest line is brightest and
+ * the oldest fades, and the whole thing fades out when the turn ends.
+ */
+object BallCaption {
+ private const val MAX_LINES = 3
+ private const val MAX_CHARS = 34
+
+ /** Newest last. Written from the brain's thread, read by the composable. */
+ var lines by mutableStateOf<List<String>>(emptyList())
+ private set
+
+ fun thinking() = push(if (Strings.lang() == "zh") "思考中…" else "Thinking…")
+
+ fun push(raw: String) {
+ // A progress line may be a paragraph; the first sentence is the gist.
+ val one = raw.trim().lineSequence().firstOrNull { it.isNotBlank() }?.trim().orEmpty()
+ if (one.isEmpty()) return clear()
+ val cut = if (one.length > MAX_CHARS) one.take(MAX_CHARS - 1) + "…" else one
+ if (lines.lastOrNull() == cut) return
+ lines = (lines + cut).takeLast(MAX_LINES)
+ }
+
+ fun clear() { if (lines.isNotEmpty()) lines = emptyList() }
+}
+
+private val CAPTION_W = 252.dp
+
+/** Window height for [n] lines. */
+private fun heightFor(n: Int) = (20 + 22 * n.coerceIn(1, 3)).dp
+
+/**
+ * A second borderless, fully transparent window under (or, near the bottom of the screen, over) the
+ * ball: just words with a shadow, no pane. It is a window of its own because the ball's is exactly
+ * the ball's size; it exists only while there is something to say and fades in and out.
+ */
+@Composable
+fun ApplicationScope.BallCaptionWindow(ball: java.awt.Window?, suppressed: Boolean = false) {
+ val lines = BallCaption.lines
+ // Keep the last words on screen while they fade out.
+ var shown by remember { mutableStateOf<List<String>>(emptyList()) }
+ if (lines.isNotEmpty()) shown = lines
+ val alpha by animateFloatAsState(if (lines.isEmpty()) 0f else 1f, tween(if (lines.isEmpty()) 600 else 280))
+ val visible = ball != null && !suppressed && (lines.isNotEmpty() || alpha > 0.01f)
+
+ val state = rememberWindowState(size = DpSize(CAPTION_W, heightFor(1)), position = WindowPosition(0.dp, 0.dp))
+ state.size = DpSize(CAPTION_W, heightFor(shown.size))
+ Window(
+ onCloseRequest = {},
+ state = state,
+ title = CAPTION_TITLE,
+ visible = visible,
+ undecorated = true,
+ transparent = true,
+ resizable = false,
+ alwaysOnTop = true,
+ focusable = false,
+ ) {
+ val w = window
+ LaunchedEffect(visible) {
+ if (!visible) return@LaunchedEffect
+ var floated = false
+ while (true) {
+ ball?.let { place(w, it) }
+ if (!floated && w.isShowing) floated = net.kuafuai.andee.desktop.mac.Bridge.floatWindow(CAPTION_TITLE)
+ delay(80)
+ }
+ }
+ // Glass lettering, no pane: the words are a top-lit gradient (bright above, a touch clearer
+ // below), wrapped in a faint cool glow and a tight dark shadow so they hold on a white page
+ // and a black one. Older lines recede (dimmer, slightly defocused); the ends of the block
+ // dissolve into the desktop instead of stopping.
+ val face = Brush.verticalGradient(listOf(Color.White, Color(0xFFDCE8FF)))
+ Column(
+ Modifier.fillMaxSize().alpha(alpha).padding(horizontal = 6.dp, vertical = 6.dp)
+ .graphicsLayer { compositingStrategy = CompositingStrategy.Offscreen }
+ .drawWithContent {
+ drawContent()
+ drawRect(
+ Brush.horizontalGradient(0f to Color.Transparent, 0.14f to Color.Black, 0.86f to Color.Black, 1f to Color.Transparent),
+ blendMode = BlendMode.DstIn,
+ )
+ },
+ horizontalAlignment = Alignment.CenterHorizontally,
+ ) {
+ shown.forEachIndexed { i, line ->
+ val newest = i == shown.lastIndex
+ key(line) {
+ // Each line rises a few dp into place as it arrives.
+ var risen by remember { mutableStateOf(false) }
+ LaunchedEffect(Unit) { risen = true }
+ val rise by animateDpAsState(if (risen) 0.dp else 7.dp, tween(320))
+ val born by animateFloatAsState(if (risen) 1f else 0f, tween(320))
+ val dim by animateFloatAsState(if (newest) 1f else 0.55f, tween(300))
+ Box(Modifier.offset(y = rise).alpha(born * dim).padding(vertical = 1.dp)) {
+ Text( // glow
+ line, color = Color(0xFF9CC4FF).copy(alpha = 0.6f), fontSize = 13.sp, fontWeight = FontWeight.Medium,
+ maxLines = 1, overflow = TextOverflow.Ellipsis,
+ modifier = Modifier.blur(7.dp, BlurredEdgeTreatment.Unbounded),
+ )
+ Text( // contact shadow
+ line, color = Color.Black.copy(alpha = 0.55f), fontSize = 13.sp, fontWeight = FontWeight.Medium,
+ maxLines = 1, overflow = TextOverflow.Ellipsis,
+ modifier = Modifier.offset(y = 1.dp).blur(2.5.dp, BlurredEdgeTreatment.Unbounded),
+ )
+ Text( // the lettering
+ line, fontSize = 13.sp, fontWeight = FontWeight.Medium, maxLines = 1, overflow = TextOverflow.Ellipsis,
+ style = TextStyle(brush = face, letterSpacing = 0.3.sp),
+ modifier = if (newest) Modifier else Modifier.blur(0.7.dp, BlurredEdgeTreatment.Unbounded),
+ )
+ }
+ }
+ }
+ }
+ }
+}
+
+/** Centred under the ball, or above it when there is no room below. */
+internal fun place(w: java.awt.Window, ball: java.awt.Window, gap: Int = -14) {
+ val screen = GraphicsEnvironment.getLocalGraphicsEnvironment().screenDevices
+ .map { it.defaultConfiguration.bounds }.firstOrNull { it.contains(ball.x + ball.width / 2, ball.y + ball.height / 2) }
+ ?: GraphicsEnvironment.getLocalGraphicsEnvironment().maximumWindowBounds
+ var x = ball.x + ball.width / 2 - w.width / 2
+ x = x.coerceIn(screen.x + 4, maxOf(screen.x + 4, screen.x + screen.width - w.width - 4))
+ val below = ball.y + ball.height + gap
+ val y = if (below + w.height < screen.y + screen.height - 8) below else ball.y - w.height - gap
+ if (w.x != x || w.y != y) w.setLocation(x, y)
+}
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Cards.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Cards.kt
index 856231e..76c0a42 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Cards.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Cards.kt
@@ -11,6 +11,9 @@ 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.shape.CircleShape
+import androidx.compose.foundation.layout.size
+import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
@@ -66,7 +69,7 @@ fun SelfCheckCard(onDone: () -> Unit, onOpenSettings: () -> Unit) {
Modifier.fillMaxWidth().glassPanel(14).padding(12.dp),
verticalAlignment = Alignment.CenterVertically,
) {
- Text(if (r.ok) "✓" else "!", color = if (r.ok) Palette.ok else Palette.warn, fontSize = 16.sp)
+ Icon(if (r.ok) AndeeIcon.CHECK_CIRCLE else AndeeIcon.ALERT_CIRCLE, if (r.ok) Palette.ok else Palette.warn, 20.dp)
Spacer(Modifier.width(10.dp))
Column(Modifier.weight(1f)) {
Text(r.name, color = Palette.label, fontSize = 14.sp)
@@ -88,7 +91,7 @@ private fun openPane(anchor: String) {
/** 产物: the pages Andee has made, one per artifact with its versions. */
@Composable
-fun ArtifactsCard(onDone: () -> Unit) {
+fun ArtifactsCard(onDone: () -> Unit, onContinue: () -> Unit) {
val zh = Strings.lang() == "zh"
var refresh by remember { mutableStateOf(0) }
val artifacts = Pages.artifacts()
@@ -102,7 +105,7 @@ fun ArtifactsCard(onDone: () -> Unit) {
Column(Modifier.fillMaxWidth().glassPanel(14).padding(12.dp)) {
Row(verticalAlignment = Alignment.CenterVertically) {
Column(Modifier.weight(1f)) {
- Text("📄 " + a.title, color = Palette.label, fontSize = 14.sp)
+ Text(a.title, color = Palette.label, fontSize = 14.sp)
Text("v${a.version} · ${LocalTime.format(a.ts)}", color = Palette.secondary, fontSize = 11.sp)
}
}
@@ -111,8 +114,8 @@ fun ArtifactsCard(onDone: () -> Unit) {
modifier = Modifier.clickable { runCatching { net.kuafuai.andee.desktop.mac.Bridge.open(a.file!!.absolutePath) } }.padding(vertical = 6.dp))
Text(if (zh) "继续" else "Edit", color = Palette.accent, fontSize = 12.sp,
modifier = Modifier.clickable {
- Thread { Assistant.say("继续修改「${a.title}」。先 get_page(id=${a.id}) 读回源码,改好后用 show_html 重画,不要新建产物。") }.start()
- onDone()
+ Assistant.beginEdit(a)
+ onContinue()
}.padding(vertical = 6.dp))
Text(if (zh) "版本" else "Versions", color = Palette.secondary, fontSize = 12.sp,
modifier = Modifier.clickable { showVersions = !showVersions }.padding(vertical = 6.dp))
@@ -126,7 +129,7 @@ fun ArtifactsCard(onDone: () -> Unit) {
Text(if (zh) "回退到这版" else "Roll back", color = Palette.accent, fontSize = 12.sp,
modifier = Modifier.clickable {
Pages.restoreVersion(v.id)
- Scrollback.add(Scrollback.Role.ASSISTANT, "↩️ 已把「${v.title}」回退到 v${v.version}")
+ Scrollback.add(Scrollback.Role.ASSISTANT, "已把「${v.title}」回退到 v${v.version}")
refresh++
}.padding(vertical = 3.dp))
}
@@ -261,7 +264,13 @@ fun ScenesCard(onDone: () -> Unit) {
Column(Modifier.fillMaxWidth().glassPanel(14).padding(12.dp)) {
Row(verticalAlignment = Alignment.CenterVertically) {
Column(Modifier.weight(1f)) {
- Text((if (isActive) "● " else "") + s.title, color = if (isActive) Palette.accent else Palette.label, fontSize = 14.sp)
+ Row(verticalAlignment = Alignment.CenterVertically) {
+ if (isActive) {
+ Box(Modifier.size(7.dp).background(Palette.accent, CircleShape))
+ Spacer(Modifier.width(7.dp))
+ }
+ Text(s.title, color = if (isActive) Palette.accent else Palette.label, fontSize = 14.sp)
+ }
Text(s.summary + " · ${s.uses} 次", color = Palette.secondary, fontSize = 11.sp)
}
}
@@ -294,7 +303,7 @@ fun ScenesCard(onDone: () -> Unit) {
/** One finished topic, read back: the verbatim text, and a way to pick it up again. */
@Composable
-fun TopicScreen(topicId: Long, onDone: () -> Unit) {
+fun TopicScreen(topicId: Long, onDone: () -> Unit, onContinue: () -> Unit) {
val zh = Strings.lang() == "zh"
val topic = Scrollback.topics().firstOrNull { it.id == topicId }
val text = Scrollback.topicText(topicId)
@@ -310,8 +319,8 @@ fun TopicScreen(topicId: Long, onDone: () -> Unit) {
Spacer(Modifier.height(10.dp))
}
PrimaryButton(if (zh) "接着聊这个话题" else "Continue this topic") {
- Thread { Assistant.say("继续聊「${topic.title}」。先 get_topic(id=${topic.id}) 读回对话再接着聊。") }.start()
- onDone()
+ Assistant.beginTopic(topic)
+ onContinue()
}
Spacer(Modifier.height(12.dp))
androidx.compose.foundation.text.selection.SelectionContainer {
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 945769c..84f518a 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ChatScreen.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ChatScreen.kt
@@ -5,7 +5,10 @@ import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
+import androidx.compose.ui.focus.focusRequester
import androidx.compose.foundation.layout.Column
+import androidx.compose.ui.draw.clip
+import androidx.compose.ui.onExternalDrag
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
@@ -36,6 +39,8 @@ import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.isShiftPressed
+import androidx.compose.ui.input.key.isMetaPressed
+import androidx.compose.ui.input.key.isCtrlPressed
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
@@ -79,19 +84,25 @@ object Palette {
val card get() = cardSolid
}
+@OptIn(androidx.compose.ui.ExperimentalComposeUiApi::class)
@Composable
-fun ChatScreen(onOpenArtifacts: () -> Unit) {
+fun ChatScreen(onOpenArtifacts: () -> Unit, onOpenTopic: (Long) -> Unit = {}, onContinueTopic: (Scrollback.Topic) -> Unit = {}) {
var rows by remember { mutableStateOf(Scrollback.snapshot()) }
var progress by remember { mutableStateOf("") }
var busy by remember { mutableStateOf(false) }
var input by remember { mutableStateOf("") }
var voice by remember { mutableStateOf(net.kuafuai.andee.desktop.voice.MacVoice.State.IDLE) }
var heard by remember { mutableStateOf("") }
+ var pending by remember { mutableStateOf(Assistant.pending) }
+ val attached = remember { androidx.compose.runtime.mutableStateListOf<Attachment>().apply { addAll(ChatPreview.attachments) } }
+ var dropping by remember { mutableStateOf(false) }
+ var muted by remember { mutableStateOf(Assistant.muted) }
var artifacts by remember { mutableStateOf(Pages.artifacts()) }
var recording by remember { mutableStateOf(Meeting.isActive()) }
val list = rememberLazyListState()
- // Folded stretches live in the sidebar's topic list; the chat is the conversation in progress.
- val items = rows.filter { it.topicId == null }
+ // A folded stretch is one card standing where its messages stood — the phone's rule, so the
+ // conversation still reads top to bottom. The sidebar's topic list is only another way in.
+ val items = remember(rows) { chatItems(rows) }
// Registered on enter, removed on leave: the sidebar remounts this screen on every
// navigation, and a listener left behind per visit is a leak that grows all day.
@@ -107,13 +118,16 @@ fun ChatScreen(onOpenArtifacts: () -> Unit) {
Assistant.onProgress = { progress = it }
Assistant.onFinal = { progress = "" }
Assistant.onBusy = { b -> busy = b; if (!b) progress = "" }
+ Assistant.onPendingChanged = { pending = Assistant.pending }
+ Assistant.onMuteChanged = { muted = Assistant.muted }
net.kuafuai.andee.desktop.voice.MacVoice.onState = { voice = it }
net.kuafuai.andee.desktop.voice.MacVoice.onPartial = { heard = it }
net.kuafuai.andee.desktop.voice.MacVoice.onFinal = { said ->
heard = ""
- Thread { Assistant.say(said, spoken = true) }.start()
+ Thread { Assistant.say(said) }.start()
}
}
+ LaunchedEffect(Unit) { pending = Assistant.pending }
LaunchedEffect(items.size) { if (items.isNotEmpty()) list.animateScrollToItem(items.size - 1) }
// The ball is the phone's way in: a tap talks, a second tap says "done".
@@ -122,38 +136,86 @@ fun ChatScreen(onOpenArtifacts: () -> Unit) {
if (!net.kuafuai.andee.desktop.voice.MacVoice.toggle()) progress = noVoiceKey()
}
+ /** Pictures and files from any door — the clip button, a paste, a drop — end up on the same strip. */
+ fun attach(items: List<Attachment>) {
+ for (a in items) if (attached.size < Attachments.MAX) attached += a
+ }
+ fun attachFiles(files: List<java.io.File>) {
+ Thread {
+ val got = files.mapNotNull { Attachments.fromFile(it) }
+ javax.swing.SwingUtilities.invokeLater { attach(got) }
+ }.start()
+ }
+ fun choose() {
+ Thread {
+ val files = Attachments.choose()
+ if (files.isNotEmpty()) attachFiles(files)
+ }.start()
+ }
+ /** ⌘V: a picture or copied files are taken; plain text is left for the field to paste. True if handled. */
+ fun pasteAttachment(): Boolean {
+ val files = Attachments.filesFromClipboard()
+ if (files.isNotEmpty()) { attachFiles(files); return true }
+ val pic = Attachments.fromClipboard() ?: return false
+ attach(listOf(pic)); return true
+ }
+
fun send() {
val t = input.trim()
- if (t.isEmpty() || busy) return
+ if ((t.isEmpty() && attached.isEmpty()) || busy) return
+ val sending = attached.toList()
input = ""
- Thread { Assistant.say(t) }.start()
+ attached.clear()
+ Thread { Assistant.say(t, attachments = sending) }.start()
}
+ Box(
+ Modifier.fillMaxSize().onExternalDrag(
+ onDragStart = { dropping = true },
+ onDragExit = { dropping = false },
+ onDrop = { v ->
+ dropping = false
+ val data = v.dragData
+ if (data is androidx.compose.ui.DragData.FilesList) {
+ attachFiles(data.readFiles().mapNotNull { runCatching { java.io.File(java.net.URI(it)) }.getOrNull() })
+ }
+ },
+ ),
+ ) {
Column(Modifier.fillMaxSize()) {
Header(
+ muted = muted, onToggleMute = { Assistant.setMuted(!muted) },
+ live = recording || voice == net.kuafuai.andee.desktop.voice.MacVoice.State.LISTENING,
status = when {
- recording -> if (Strings.lang() == "zh") "● 正在录音" else "● Recording"
- voice == net.kuafuai.andee.desktop.voice.MacVoice.State.LISTENING -> if (Strings.lang() == "zh") "● 正在听" else "● Listening"
+ recording -> if (Strings.lang() == "zh") "正在录音" else "Recording"
+ voice == net.kuafuai.andee.desktop.voice.MacVoice.State.LISTENING -> if (Strings.lang() == "zh") "正在听" else "Listening"
busy -> if (Strings.lang() == "zh") "思考中…" else "Thinking…"
voice == net.kuafuai.andee.desktop.voice.MacVoice.State.SPEAKING -> if (Strings.lang() == "zh") "正在说" else "Speaking"
else -> null
},
- stoppable = recording || busy,
- onStop = {
- if (recording) Meeting.stopFromUser() else if (busy) Assistant.stop()
- },
)
Column(Modifier.weight(1f).fillMaxWidth().padding(horizontal = 16.dp)) {
if (artifacts.isNotEmpty()) {
- Text(
- "📄 " + (if (Strings.lang() == "zh") "产物" else "Artifacts") + " (${artifacts.size})",
- color = Palette.accent, fontSize = 13.sp,
- modifier = Modifier.fillMaxWidth().glassSurface(12).clickable { onOpenArtifacts() }.padding(12.dp),
- )
+ Row(
+ Modifier.fillMaxWidth().glassSurface(12).clickable { onOpenArtifacts() }.padding(12.dp),
+ verticalAlignment = Alignment.CenterVertically,
+ ) {
+ Icon(AndeeIcon.DOCUMENT, Palette.accent, 16.dp)
+ Spacer(Modifier.width(8.dp))
+ Text(
+ (if (Strings.lang() == "zh") "产物" else "Artifacts") + " (${artifacts.size})",
+ color = Palette.accent, fontSize = 13.sp, modifier = Modifier.weight(1f),
+ )
+ Icon(AndeeIcon.CHEVRON, Palette.accent, 14.dp)
+ }
Spacer(Modifier.height(10.dp))
}
- LazyColumn(state = list, modifier = Modifier.weight(1f).fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(10.dp)) {
+ LazyColumn(
+ state = list, modifier = Modifier.weight(1f).fillMaxWidth(),
+ contentPadding = androidx.compose.foundation.layout.PaddingValues(top = 4.dp, bottom = 18.dp),
+ verticalArrangement = Arrangement.spacedBy(10.dp),
+ ) {
if (items.isEmpty()) {
item {
Box(Modifier.fillMaxWidth().padding(top = 24.dp), contentAlignment = Alignment.Center) {
@@ -164,57 +226,128 @@ fun ChatScreen(onOpenArtifacts: () -> Unit) {
}
}
}
- items(items, key = { it.id }) { Bubble(it) }
+ items(items, key = { it.key }) { it ->
+ when (it) {
+ is ChatItem.Message -> Bubble(it.row)
+ is ChatItem.Folded -> TopicCard(it.topic, it.count, onOpen = { onOpenTopic(it.topic.id) }, onContinue = { onContinueTopic(it.topic) })
+ }
+ }
}
}
if (heard.isNotEmpty()) {
- Text("🎤 $heard", color = Palette.text, fontSize = 13.sp, modifier = Modifier.padding(horizontal = 16.dp).padding(bottom = 4.dp))
+ Row(Modifier.padding(horizontal = 16.dp).padding(bottom = 4.dp), verticalAlignment = Alignment.CenterVertically) {
+ Icon(AndeeIcon.MIC, Palette.accent, 14.dp)
+ Spacer(Modifier.width(6.dp))
+ Text(heard, color = Palette.text, fontSize = 13.sp)
+ }
}
if (progress.isNotEmpty()) {
Text(progress, color = Palette.secondary, fontSize = 13.sp, modifier = Modifier.padding(horizontal = 16.dp).padding(bottom = 4.dp))
}
+ pending?.let { p ->
+ val zh = Strings.lang() == "zh"
+ Row(
+ Modifier.padding(horizontal = 16.dp).padding(top = 4.dp, bottom = 8.dp).fillMaxWidth().glassSurface(12).padding(horizontal = 14.dp, vertical = 11.dp),
+ verticalAlignment = Alignment.CenterVertically,
+ ) {
+ Icon(AndeeIcon.DOCUMENT, Palette.accent, 16.dp)
+ Spacer(Modifier.width(8.dp))
+ Text(
+ when (p) {
+ is Assistant.Pending.Edit -> if (zh) "修改「${p.page.title}」:说说想怎么改" else "Revising “${p.page.title}”: say what to change"
+ is Assistant.Pending.Topic -> if (zh) "继续「${p.topic.title}」:想从哪接着聊" else "Continuing “${p.topic.title}”: where to take it"
+ },
+ color = Palette.label, fontSize = 13.sp, modifier = Modifier.weight(1f),
+ )
+ Box(Modifier.clickable { Assistant.cancelPending() }.padding(4.dp)) { Icon(AndeeIcon.CLOSE, Palette.secondary, 14.dp) }
+ }
+ }
InputBar(
input = input,
onInput = { input = it },
listening = voice == net.kuafuai.andee.desktop.voice.MacVoice.State.LISTENING,
- canSend = input.isNotBlank() && !busy,
+ canSend = (input.isNotBlank() || attached.isNotEmpty()) && !busy,
+ attached = attached,
+ onRemove = { attached.remove(it) },
+ onAttach = { choose() },
+ onPaste = { pasteAttachment() },
+ stoppable = recording || busy,
+ onStop = { if (recording) Meeting.stopFromUser() else if (busy) Assistant.stop() },
onMic = { toggleMic() },
onSend = { send() },
)
}
+ if (dropping) {
+ Box(
+ Modifier.fillMaxSize().padding(10.dp)
+ .background(Palette.accent.copy(alpha = 0.14f), RoundedCornerShape(18.dp))
+ .border(2.dp, Palette.accent, RoundedCornerShape(18.dp)),
+ contentAlignment = Alignment.Center,
+ ) {
+ Text(if (Strings.lang() == "zh") "松开,加到这条消息里" else "Drop to add to this message", color = Palette.title, fontSize = 16.sp)
+ }
+ }
+ }
}
-/** Title plus a one-line status, with stop and settings on the right — the phone's control bar. */
+/** Title, or a one-line status when something is live. Stop is with the input box, where the hand already is. */
@Composable
-private fun Header(status: String?, stoppable: Boolean, onStop: () -> Unit) {
+private fun Header(status: String?, live: Boolean, muted: Boolean, onToggleMute: () -> Unit) {
Row(
Modifier.fillMaxWidth().padding(start = 16.dp, end = 16.dp, top = 14.dp, bottom = 10.dp),
verticalAlignment = Alignment.CenterVertically,
) {
+ if (live) {
+ Box(Modifier.size(8.dp).background(Palette.accent, CircleShape))
+ Spacer(Modifier.width(8.dp))
+ }
Text(
status ?: (if (Strings.lang() == "zh") "对话" else "Chat"),
- color = if (status?.startsWith("●") == true) Palette.accent else Palette.title,
+ color = if (live) Palette.accent else Palette.title,
fontSize = 18.sp, fontWeight = FontWeight.SemiBold,
)
Spacer(Modifier.weight(1f))
- if (stoppable) {
- Pill(if (Strings.lang() == "zh") "■ 停止" else "■ Stop") { onStop() }
- Spacer(Modifier.width(8.dp))
+ val zh = Strings.lang() == "zh"
+ Row(
+ Modifier.clip(RoundedCornerShape(16.dp)).clickable { onToggleMute() }.padding(horizontal = 10.dp, vertical = 6.dp),
+ verticalAlignment = Alignment.CenterVertically,
+ ) {
+ Icon(if (muted) AndeeIcon.VOLUME_OFF else AndeeIcon.VOLUME, if (muted) Palette.secondary else Palette.label, 18.dp)
+ Spacer(Modifier.width(6.dp))
+ Text(
+ if (muted) (if (zh) "静音" else "Muted") else (if (zh) "朗读" else "Voice on"),
+ color = if (muted) Palette.secondary else Palette.label, fontSize = 12.sp,
+ )
}
}
}
/** The mic and send live with the text field, as a chat app's composer does. */
@Composable
-private fun InputBar(input: String, onInput: (String) -> Unit, listening: Boolean, canSend: Boolean, onMic: () -> Unit, onSend: () -> Unit) {
+private fun InputBar(
+ input: String, onInput: (String) -> Unit, listening: Boolean, canSend: Boolean,
+ attached: List<Attachment>, onRemove: (Attachment) -> Unit, onAttach: () -> Unit, onPaste: () -> Boolean,
+ stoppable: Boolean, onStop: () -> Unit, onMic: () -> Unit, onSend: () -> Unit,
+) {
+ Column {
+ if (attached.isNotEmpty()) AttachmentStrip(attached, onRemove)
+ val focus = remember { androidx.compose.ui.focus.FocusRequester() }
+ androidx.compose.runtime.LaunchedEffect(ComposerFocus.pending) {
+ if (ComposerFocus.pending) {
+ kotlinx.coroutines.delay(120) // the window has to be up before it can take focus
+ runCatching { focus.requestFocus() }
+ ComposerFocus.pending = false
+ }
+ }
Row(
Modifier.fillMaxWidth().padding(horizontal = 16.dp).padding(top = 8.dp, bottom = 14.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
- RoundButton(listening, onMic) { if (listening) "◼" else "🎤" }
+ RoundButton(false.not(), onAttach, AndeeIcon.PAPERCLIP, quiet = true)
+ RoundButton(listening, onMic, if (listening) AndeeIcon.STOP else AndeeIcon.MIC)
Box(
Modifier.weight(1f).background(Palette.well, RoundedCornerShape(18.dp))
.border(1.dp, Palette.hairline, RoundedCornerShape(18.dp)).padding(horizontal = 14.dp, vertical = 10.dp),
@@ -224,9 +357,12 @@ private fun InputBar(input: String, onInput: (String) -> Unit, listening: Boolea
onValueChange = onInput,
textStyle = TextStyle(color = Palette.text, fontSize = 15.sp),
cursorBrush = SolidColor(Palette.accent),
- modifier = Modifier.fillMaxWidth().onPreviewKeyEvent { e ->
+ modifier = Modifier.fillMaxWidth().focusRequester(focus).onPreviewKeyEvent { e ->
if (e.type == KeyEventType.KeyDown && e.key == Key.Enter && !e.isShiftPressed) {
if (canSend) onSend(); true
+ } else if (e.type == KeyEventType.KeyDown && e.key == Key.V && (e.isMetaPressed || e.isCtrlPressed)) {
+ // A copied picture or file is ours; copied text falls through to the field.
+ onPaste()
} else false
},
)
@@ -237,25 +373,132 @@ private fun InputBar(input: String, onInput: (String) -> Unit, listening: Boolea
)
}
}
- RoundButton(canSend, onSend) { "↑" }
+ if (stoppable) RoundButton(true, onStop, AndeeIcon.STOP, danger = true)
+ RoundButton(canSend, onSend, AndeeIcon.SEND)
+ }
}
}
+/** What is about to go: a thumbnail per picture, a name and size per file, each with a way to take it off. */
@Composable
-private fun RoundButton(enabled: Boolean, onClick: () -> Unit, glyph: () -> String) {
+private fun AttachmentStrip(items: List<Attachment>, onRemove: (Attachment) -> Unit) {
+ Row(
+ Modifier.fillMaxWidth().padding(horizontal = 16.dp).padding(top = 6.dp, bottom = 4.dp),
+ horizontalArrangement = Arrangement.spacedBy(8.dp),
+ ) {
+ for (a in items) {
+ Box {
+ when (a) {
+ is Attachment.Photo -> androidx.compose.foundation.Image(
+ a.thumb, a.label,
+ Modifier.size(56.dp).clip(RoundedCornerShape(10.dp)).border(1.dp, Palette.glassEdge, RoundedCornerShape(10.dp)),
+ contentScale = androidx.compose.ui.layout.ContentScale.Crop,
+ )
+ is Attachment.FileRef -> Row(
+ Modifier.height(56.dp).widthIn(max = 240.dp).glassSurface(10).padding(start = 12.dp, end = 26.dp),
+ verticalAlignment = Alignment.CenterVertically,
+ ) {
+ Icon(AndeeIcon.FILE, Palette.accent, 18.dp)
+ Spacer(Modifier.width(8.dp))
+ Column {
+ Text(a.label, color = Palette.label, fontSize = 13.sp, maxLines = 1, overflow = androidx.compose.ui.text.style.TextOverflow.Ellipsis)
+ Text(Attachments.size(a.bytes), color = Palette.secondary, fontSize = 11.sp)
+ }
+ }
+ }
+ Box(
+ Modifier.align(Alignment.TopEnd).padding(3.dp).size(18.dp)
+ .background(Color(0xCC000000), CircleShape).clickable { onRemove(a) },
+ contentAlignment = Alignment.Center,
+ ) { Icon(AndeeIcon.CLOSE, Color.White, 10.dp) }
+ }
+ }
+ }
+}
+
+@Composable
+private fun RoundButton(enabled: Boolean, onClick: () -> Unit, icon: AndeeIcon, danger: Boolean = false, quiet: Boolean = false) {
+ // A quiet button is always available but never the main action: no fill, the same rim as a disabled one.
+ val filled = enabled && !quiet
Box(
Modifier.size(40.dp)
- .background(if (enabled) Palette.accent else Palette.well, CircleShape)
- .border(1.dp, if (enabled) Color.Transparent else Palette.hairline, CircleShape)
+ .background(if (!filled) Palette.well else if (danger) Palette.danger else Palette.accent, CircleShape)
+ .border(1.dp, if (filled) Color.Transparent else Palette.hairline, CircleShape)
.clickable(enabled = enabled) { onClick() },
contentAlignment = Alignment.Center,
) {
- Text(glyph(), color = if (enabled) Color(0xFF0A2540) else Palette.secondary, fontSize = 17.sp)
+ Icon(icon, if (filled) Color(0xFF0A2540) else if (quiet) Palette.label else Palette.secondary, 18.dp)
+ }
+}
+
+/** Preview-only: what the offscreen render starts the strip with. Empty in the app. */
+object ChatPreview { var attachments: List<Attachment> = emptyList() }
+
+sealed class ChatItem(val key: String) {
+ class Message(val row: Scrollback.Row) : ChatItem("m${row.id}")
+ class Folded(val topic: Scrollback.Topic, val count: Int) : ChatItem("t${topic.id}")
+}
+
+/**
+ * Rows in order, with each folded stretch collapsed to one entry at the place of its first row.
+ * A topic whose rows have all aged out of the record is not drawn — it stays reachable from the
+ * sidebar and through list_topics.
+ */
+fun chatItems(rows: List<Scrollback.Row>): List<ChatItem> {
+ val topics = Scrollback.topics().associateBy { it.id }
+ val counts = rows.mapNotNull { it.topicId }.groupingBy { it }.eachCount()
+ val seen = HashSet<Long>()
+ val out = ArrayList<ChatItem>()
+ for (r in rows) {
+ val t = r.topicId
+ if (t == null) { out += ChatItem.Message(r); continue }
+ val topic = topics[t] ?: continue
+ if (seen.add(t)) out += ChatItem.Folded(topic, counts[t] ?: 0)
+ }
+ return out
+}
+
+/** One finished topic, standing where its messages stood: what it was, how long, and the two ways back in. */
+@Composable
+private fun TopicCard(topic: Scrollback.Topic, count: Int, onOpen: () -> Unit, onContinue: () -> Unit) {
+ val zh = Strings.lang() == "zh"
+ val fmt = remember { java.text.SimpleDateFormat("MM-dd HH:mm", java.util.Locale.US) }
+ Column(
+ Modifier.fillMaxWidth().glassPanel(16).padding(horizontal = 16.dp, vertical = 12.dp),
+ ) {
+ Row(verticalAlignment = Alignment.CenterVertically) {
+ Icon(AndeeIcon.CHAT, Palette.secondary, 14.dp)
+ Spacer(Modifier.width(6.dp))
+ Text(if (zh) "话题" else "Topic", color = Palette.secondary, fontSize = 12.sp)
+ Spacer(Modifier.weight(1f))
+ Text(
+ fmt.format(java.util.Date(topic.endedAt)) + " · " + (if (zh) "$count 条" else "$count msgs"),
+ color = Palette.photoInk, fontSize = 12.sp,
+ )
+ }
+ Spacer(Modifier.height(6.dp))
+ Text(topic.title, color = Palette.title, fontSize = 15.sp, fontWeight = FontWeight.Medium)
+ if (topic.summary.isNotBlank() && topic.summary != topic.title) {
+ Spacer(Modifier.height(2.dp))
+ Text(topic.summary, color = Palette.label, fontSize = 13.sp, lineHeight = 18.sp, maxLines = 3, overflow = androidx.compose.ui.text.style.TextOverflow.Ellipsis)
+ }
+ Spacer(Modifier.height(8.dp))
+ Row(horizontalArrangement = Arrangement.spacedBy(16.dp)) {
+ Text(if (zh) "继续话题" else "Continue", color = Palette.accent, fontSize = 13.sp,
+ modifier = Modifier.clickable { onContinue() }.padding(vertical = 4.dp))
+ Text(if (zh) "对话记录" else "Transcript", color = Palette.accent, fontSize = 13.sp,
+ modifier = Modifier.clickable { onOpen() }.padding(vertical = 4.dp))
+ }
}
}
+/** Asked for by the ball's double-tap: the next time the composer is on screen, put the cursor in it. */
+object ComposerFocus {
+ var pending by androidx.compose.runtime.mutableStateOf(false)
+}
+
fun noVoiceKey() =
- if (Strings.lang() == "zh") "还没配语音 Key:在 ⚙ 设置的「语音」里填上。" else "No voice key yet: add it under Voice in ⚙ Settings."
+ if (Strings.lang() == "zh") "还没配语音 Key:在设置的「语音」里填上。" else "No voice key yet: add it under Voice in Settings."
@Composable
private fun Bubble(r: Scrollback.Row) {
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/DesktopBrainHost.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/DesktopBrainHost.kt
index 68e85f5..43989d4 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/DesktopBrainHost.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/DesktopBrainHost.kt
@@ -52,7 +52,7 @@ class DesktopBrainHost(
override fun activeSceneName(): String? = Settings.get("scene").ifEmpty { null }
?.takeIf { DesktopNotebook.book.scene(it) != null }
- override fun persona(): String = ""
+ override fun persona(): String = net.kuafuai.andee.ui.ball.BallLooks.byName(Settings.get("ball").ifEmpty { null }).persona
override fun platformPrompt(): String = MacPrompt.text(caps)
@@ -116,7 +116,17 @@ class DesktopBrainHost(
object Strings {
private val cache = HashMap<String, Properties>()
- fun lang(): String = Settings.get("lang").ifEmpty {
+ /**
+ * Bumped when the language is changed from the settings picker. [lang] reads it, so every
+ * composable that asks which language it is in is subscribed and redraws at once, with no
+ * screen having to be told. (Reading Compose state off the UI thread is fine; it is a plain read.)
+ */
+ private val rev = androidx.compose.runtime.mutableStateOf(0)
+
+ /** The language was just written; redraw everything that reads it. */
+ fun changed() { rev.value++ }
+
+ fun lang(): String = rev.value.let { Settings.get("lang") }.ifEmpty {
if (Locale.getDefault().language == "zh") "zh" else "en"
}
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/FloatingBall.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/FloatingBall.kt
index 6c8ef7a..1b49292 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/FloatingBall.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/FloatingBall.kt
@@ -30,12 +30,20 @@ private val BALL_WINDOW = 112.dp
* over full-screen apps (that last part is the native `floatWindow`, because a
* JVM's "always on top" is only the floating level on the current Space).
*
- * Tap talks, as on the phone. Long-press shows or hides the chat window — the
- * phone's long-press unfolds the card. Quitting is ⌘Q, since closing the chat only hides it. Drag moves it, and where it was left is
- * remembered.
+ * While a task is running or being read out, any press stops it (see `working` / `onStop`).
+ *
+ * The phone's three gestures: tap talks, double-tap types (the chat window comes
+ * up with the cursor in the input box), long-press opens the window. Quitting is
+ * ⌘Q, since closing the chat only hides it. Drag moves it, and where it was left
+ * is remembered. A tap waits out the double-tap interval before it fires, which
+ * is what lets the two be told apart.
*/
@Composable
-fun ApplicationScope.FloatingBall(onTap: () -> Unit, onLongPress: () -> Unit, onWindow: (java.awt.Window) -> Unit) {
+fun ApplicationScope.FloatingBall(
+ onTap: () -> Unit, onDoubleTap: () -> Unit, onLongPress: () -> Unit, onWindow: (java.awt.Window) -> Unit,
+ /** True while a task is running or being read out; a press then stops it at once. */
+ working: () -> Boolean = { false }, onStop: () -> Unit = {},
+) {
val state = rememberWindowState(size = DpSize(BALL_WINDOW, BALL_WINDOW), position = savedPosition())
Window(
onCloseRequest = ::exitApplication,
@@ -77,7 +85,16 @@ fun ApplicationScope.FloatingBall(onTap: () -> Unit, onLongPress: () -> Unit, on
}
}
.pointerInput(Unit) {
- detectTapGestures(onTap = { onTap() }, onLongPress = { onLongPress() })
+ // A tap waits out the double-tap interval before it fires, which is half a second of
+ // "nothing happened" when the user is trying to make it stop. So while it is working
+ // the stop happens on the press itself, and the rest of that gesture is ignored.
+ var stopped = false
+ detectTapGestures(
+ onPress = { stopped = working(); if (stopped) onStop() },
+ onTap = { if (!stopped) onTap() },
+ onDoubleTap = { if (!stopped) onDoubleTap() },
+ onLongPress = { if (!stopped) onLongPress() },
+ )
},
contentAlignment = Alignment.Center,
) {
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Glass.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Glass.kt
index 8ff7ed7..c1a1bd1 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Glass.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Glass.kt
@@ -90,8 +90,9 @@ fun GlassHeader(title: String, subtitle: String? = null, onClose: () -> Unit, tr
}
Spacer(Modifier.weight(1f))
trailing?.invoke()
- Text("✕", color = Palette.secondary, fontSize = 18.sp,
- modifier = Modifier.glassPanel(18).clickable { onClose() }.padding(horizontal = 12.dp, vertical = 6.dp))
+ Box(Modifier.glassPanel(18).clickable { onClose() }.padding(horizontal = 12.dp, vertical = 8.dp)) {
+ Icon(AndeeIcon.CLOSE, Palette.secondary, 16.dp)
+ }
}
}
@@ -142,14 +143,27 @@ fun GlassField(label: String, value: String, onValue: (String) -> Unit, secret:
}
@Composable
-fun PrimaryButton(label: String, onClick: () -> Unit) {
+fun PrimaryButton(label: String, modifier: Modifier = Modifier, onClick: () -> Unit) {
Text(
label, color = Palette.ON_TINT(), fontSize = 14.sp, fontWeight = FontWeight.Medium,
textAlign = androidx.compose.ui.text.style.TextAlign.Center,
- modifier = Modifier.fillMaxWidth()
+ modifier = modifier.fillMaxWidth()
.background(Palette.tint, RoundedCornerShape(14.dp))
.clickable { onClick() }.padding(vertical = 11.dp),
)
}
+/** The quiet twin of [PrimaryButton]: same height and width rules, glass fill instead of the tint. */
+@Composable
+fun SecondaryButton(label: String, modifier: Modifier = Modifier, onClick: () -> Unit) {
+ Text(
+ label, color = Palette.text, fontSize = 14.sp, fontWeight = FontWeight.Medium,
+ textAlign = androidx.compose.ui.text.style.TextAlign.Center,
+ modifier = modifier.fillMaxWidth()
+ .background(Palette.panel, RoundedCornerShape(14.dp))
+ .border(1.dp, Palette.hairline, RoundedCornerShape(14.dp))
+ .clickable { onClick() }.padding(vertical = 11.dp),
+ )
+}
+
private fun Palette.ON_TINT(): Color = Color(0xFFFFFFFF)
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Icons.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Icons.kt
new file mode 100644
index 0000000..f26e0f2
--- /dev/null
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Icons.kt
@@ -0,0 +1,64 @@
+package net.kuafuai.andee.desktop
+
+import androidx.compose.foundation.Canvas
+import androidx.compose.foundation.layout.size
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.remember
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.geometry.Offset
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.graphics.StrokeCap
+import androidx.compose.ui.graphics.StrokeJoin
+import androidx.compose.ui.graphics.drawscope.Fill
+import androidx.compose.ui.graphics.drawscope.Stroke
+import androidx.compose.ui.graphics.drawscope.withTransform
+import androidx.compose.ui.graphics.vector.PathParser
+import androidx.compose.ui.unit.Dp
+import androidx.compose.ui.unit.dp
+
+/**
+ * The app's icons, as SVG path data on a 24-unit grid, drawn in the text colour.
+ * The outlines are from Lucide (https://lucide.dev, ISC licence), copied as path
+ * data; STOP is our own filled square.
+ * An emoji is a colour bitmap from whatever font the Mac has, so it ignores the
+ * palette and looks different on every machine; a path is the same everywhere
+ * and takes the colour it is given.
+ *
+ * Strokes are 2 units, round-capped and round-joined, as Lucide draws them.
+ */
+enum class AndeeIcon(val d: String, val filled: Boolean = false) {
+ CHAT("M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"),
+ DOCUMENT("M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z M14 2v4a2 2 0 0 0 2 2h4 M10 9H8 M16 13H8 M16 17H8"),
+ NOTEBOOK("M2 6h4 M2 10h4 M2 14h4 M2 18h4 M6 2h12a2 2 0 0 1 2 2v16a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2v-16a2 2 0 0 1 2 -2z M9.5 8h5 M9.5 12H16 M9.5 16H14"),
+ SPARKLE("M9.937 15.5A2 2 0 0 0 8.5 14.063l-6.135-1.582a.5.5 0 0 1 0-.962L8.5 9.936A2 2 0 0 0 9.937 8.5l1.582-6.135a.5.5 0 0 1 .963 0L14.063 8.5A2 2 0 0 0 15.5 9.937l6.135 1.581a.5.5 0 0 1 0 .964L15.5 14.063a2 2 0 0 0-1.437 1.437l-1.582 6.135a.5.5 0 0 1-.963 0z M20 3v4 M22 5h-4 M4 17v2 M5 18H3"),
+ LOCK("M5 11h14a2 2 0 0 1 2 2v7a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2v-7a2 2 0 0 1 2 -2z M7 11V7a5 5 0 0 1 10 0v4"),
+ FACE("M2 12a10 10 0 1 0 20 0a10 10 0 1 0 -20 0 M8 14s1.5 2 4 2 4-2 4-2 M9 9L9.01 9 M15 9L15.01 9"),
+ CHECK_CIRCLE("M2 12a10 10 0 1 0 20 0a10 10 0 1 0 -20 0 M0 0m9 12 2 2 4-4"),
+ ALERT_CIRCLE("M2 12a10 10 0 1 0 20 0a10 10 0 1 0 -20 0 M12 8L12 12 M12 16L12.01 16"),
+ SLIDERS("M21 4L14 4 M10 4L3 4 M21 12L12 12 M8 12L3 12 M21 20L16 20 M12 20L3 20 M14 2L14 6 M8 10L8 14 M16 18L16 22"),
+ PLUS("M5 12h14 M12 5v14"),
+ MIC("M12 2a3 3 0 0 0-3 3v7a3 3 0 0 0 6 0V5a3 3 0 0 0-3-3Z M19 10v2a7 7 0 0 1-14 0v-2 M12 19L12 22"),
+ SEND("m5 12 7-7 7 7 M12 19V5"),
+ PAPERCLIP("M13.234 20.252 21 12.3 M16 6l-8.414 8.586a2 2 0 0 0 0 2.828 2 2 0 0 0 2.828 0l8.414-8.586a4 4 0 0 0 0-5.656 4 4 0 0 0-5.656 0l-8.415 8.585a6 6 0 1 0 8.486 8.486"),
+ IMAGE("M5 3h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z M9 7a2 2 0 1 0 0 4a2 2 0 1 0 0-4z M21 15l-3.086-3.086a2 2 0 0 0-2.828 0L6 21"),
+ FILE("M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7z M14 2v4a2 2 0 0 0 2 2h4"),
+ VOLUME("M11 4.702a.705.705 0 0 0-1.203-.498L6.413 7.587A1.4 1.4 0 0 1 5.416 8H3a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h2.416a1.4 1.4 0 0 1 .997.413l3.383 3.384A.705.705 0 0 0 11 19.298z M16 9a5 5 0 0 1 0 6 M19.364 18.364a9 9 0 0 0 0-12.728"),
+ VOLUME_OFF("M11 4.702a.705.705 0 0 0-1.203-.498L6.413 7.587A1.4 1.4 0 0 1 5.416 8H3a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h2.416a1.4 1.4 0 0 1 .997.413l3.383 3.384A.705.705 0 0 0 11 19.298z M22 9L16 15 M16 9l6 6"),
+ STOP("M8 6h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2z", filled = true),
+ CLOSE("M18 6 6 18 M0 0m6 6 12 12"),
+ CHEVRON("m9 18 6-6-6-6"),
+ CHECK("M20 6 9 17l-5-5"),
+ ;
+}
+
+@Composable
+fun Icon(icon: AndeeIcon, tint: Color, size: Dp = 18.dp, modifier: Modifier = Modifier) {
+ val path = remember(icon) { PathParser().parsePathString(icon.d).toPath() }
+ Canvas(modifier.size(size)) {
+ val k = this.size.width / 24f
+ withTransform({ scale(k, k, pivot = Offset.Zero) }) {
+ if (icon.filled) drawPath(path, tint, style = Fill)
+ else drawPath(path, tint, style = Stroke(width = 2f, cap = StrokeCap.Round, join = StrokeJoin.Round))
+ }
+ }
+}
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Main.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Main.kt
index 1e40b9f..14eb563 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Main.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Main.kt
@@ -1,6 +1,9 @@
package net.kuafuai.andee.desktop
+import androidx.compose.runtime.DisposableEffect
+import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
+import kotlinx.coroutines.delay
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
@@ -28,23 +31,64 @@ fun main() {
// The chat window comes and goes (the ball is always there); the nav says what it shows.
var chat by remember { mutableStateOf(true) }
val nav = remember { Nav() }
- ApprovalGate.onChange = { pending = ApprovalGate.current; if (pending != null) { chat = true; nav.go("chat") } }
+ ApprovalGate.onChange = { pending = ApprovalGate.current }
var ballWindow by remember { mutableStateOf<java.awt.Window?>(null) }
var chatWindow by remember { mutableStateOf<java.awt.Window?>(null) }
var ask by remember { mutableStateOf(AskGate.current) }
- AskGate.onChange = { ask = AskGate.current; if (ask != null) { chat = true; nav.go("chat") } }
+ AskGate.onChange = { ask = AskGate.current }
// A click aimed at another app must not land on Andee: both windows step aside for the instant of it.
// (Native, not Window.opacity: AWT refuses that on the decorated chat window.)
+ // Like the phone folding its card before it drives another app: the chat window would cover
+ // that app, and while it is frontmost the brain's keystrokes would land in Andee's own input.
+ // The ball stays; a long-press or a Dock click brings the chat back.
+ Assistant.yieldScreen = { if (chat) { chat = false; Thread.sleep(220) } }
Assistant.hideWindows = { hide -> net.kuafuai.andee.desktop.mac.Bridge.setOwnAlpha(if (hide) 0.0 else 1.0) }
+ // Bringing the window up is two things: make it visible, then pull the app forward (an
+ // accessory-style ball never owns the foreground, so a visible window can still sit behind).
+ var frontTick by remember { mutableStateOf(0) }
+ fun openChat(screen: String = "chat", typing: Boolean = false) {
+ chat = true
+ nav.go(screen)
+ if (typing) ComposerFocus.pending = true
+ frontTick++
+ }
+ LaunchedEffect(frontTick) {
+ if (frontTick == 0) return@LaunchedEffect
+ delay(60)
+ chatWindow?.let { w ->
+ if (w is java.awt.Frame && (w.extendedState and java.awt.Frame.ICONIFIED) != 0) w.extendedState = java.awt.Frame.NORMAL
+ w.toFront()
+ }
+ runCatching {
+ val d = java.awt.Desktop.getDesktop()
+ if (d.isSupported(java.awt.Desktop.Action.APP_REQUEST_FOREGROUND)) d.requestForeground(true)
+ }
+ }
+ // A click on the Dock icon. With the window closed there is nothing for the system to raise,
+ // so without this the icon opened it only when it happened to still be on screen.
+ DisposableEffect(Unit) {
+ val d = runCatching { java.awt.Desktop.getDesktop() }.getOrNull()
+ val l = java.awt.desktop.AppReopenedListener { javax.swing.SwingUtilities.invokeLater { openChat() } }
+ runCatching { if (d?.isSupported(java.awt.Desktop.Action.APP_EVENT_REOPENED) == true) d.addAppEventListener(l) }
+ onDispose { runCatching { d?.removeAppEventListener(l) } }
+ }
+
FloatingBall(
+ // A press while it is working is a stop (handled in FloatingBall, on the press itself);
+ // these run only when it was idle.
+ working = { Assistant.working() },
+ onStop = { Assistant.stop() },
onTap = {
if (Meeting.isActive()) Meeting.stopFromUser()
- else if (!net.kuafuai.andee.desktop.voice.MacVoice.toggle()) { chat = true; nav.go("settings") }
+ else if (!net.kuafuai.andee.desktop.voice.MacVoice.toggle()) openChat("settings")
},
- onLongPress = { chat = !chat },
+ onDoubleTap = { openChat(typing = true) },
+ onLongPress = { openChat() },
onWindow = { ballWindow = it },
)
+ BallCaptionWindow(ballWindow, suppressed = pending != null || ask != null)
+ PromptWindow(ballWindow, pending, ask)
Window(
onCloseRequest = { chat = false },
@@ -66,7 +110,7 @@ fun main() {
shortcut = androidx.compose.ui.input.key.KeyShortcut(androidx.compose.ui.input.key.Key.Q, meta = true))
}
}
- AppRoot(nav, pending, ask)
+ AppRoot(nav)
}
}
}
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Meeting.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Meeting.kt
index 5b3ee0b..8a35050 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Meeting.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Meeting.kt
@@ -83,7 +83,7 @@ object Meeting {
recording.set(true)
exec.execute { micLoop() }
onRecordingChanged(true)
- Scrollback.add(Scrollback.Role.ASSISTANT, "📼 " + (if (Strings.lang() == "zh") "开始录音了。" else "Recording started."))
+ Scrollback.add(Scrollback.Role.ASSISTANT, (if (Strings.lang() == "zh") "开始录音了。" else "Recording started."))
Log.i(TAG, "meeting started: $title")
return JSONObject().put("recording", true).put("title", title).put("started_at", startedAt)
}
@@ -122,7 +122,7 @@ object Meeting {
active = null
if (seg != null) retire(seg)
onRecordingChanged(false)
- Scrollback.add(Scrollback.Role.ASSISTANT, "📼 " + (if (Strings.lang() == "zh") "录音结束了。" else "Recording ended."))
+ Scrollback.add(Scrollback.Role.ASSISTANT, (if (Strings.lang() == "zh") "录音结束了。" else "Recording ended."))
return result(stoppedBy)
}
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 881964b..1247af2 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Pages.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Pages.kt
@@ -55,9 +55,17 @@ object Pages {
base = b
synchronized(lock) { entries.clear(); nextId = 1L }
armedGroup = null
+ carriedGroup = null
}
- fun beginTurn() { armedGroup = null }
+ /** A 继续修改 tap names the artifact before the turn exists; the turn's own start must not wipe it. */
+ @Volatile private var carriedGroup: Long? = null
+ fun armNextTurn(groupId: Long) { carriedGroup = groupId }
+
+ /** For tests: re-read the index from [base]. */
+ internal fun reloadForTest() { synchronized(lock) { entries.clear(); nextId = 1L }; load() }
+
+ fun beginTurn() { armedGroup = carriedGroup; carriedGroup = null }
fun armGroup(id: Long) { armedGroup = id }
/** The phone's resolveRevisionTarget: explicit wins, and either way the target is consumed. */
@@ -157,6 +165,40 @@ object Pages {
}
}
+ /**
+ * An early build read `show_html`'s `revise` (a *page* id) as a *group* id, so a revision of
+ * page 2 was filed under a group named "2" that nothing else belonged to, and the shelf showed
+ * one artifact as two. A real group is always named after its own first page, so a group whose
+ * name is the id of a page in a *different* group is exactly that mistake. Fold it into the
+ * group it was meant for and renumber, so the history reads v1..vN in the order it was made.
+ * Returns whether anything changed.
+ */
+ internal fun repairMisfiled(): Boolean = synchronized(lock) {
+ var changed = false
+ var moved = true
+ while (moved) {
+ moved = false
+ val ownerOf = entries.associate { it.id to it.groupId }
+ val stray = entries.groupBy { it.groupId }.keys.firstOrNull { g ->
+ val owner = ownerOf[g]
+ owner != null && owner != g && entries.none { it.id == g && it.groupId == g }
+ } ?: break
+ val target = ownerOf.getValue(stray)
+ for (i in entries.indices) if (entries[i].groupId == stray) entries[i] = entries[i].copy(groupId = target)
+ moved = true; changed = true
+ }
+ if (changed) {
+ for ((_, revs) in entries.groupBy { it.groupId }) {
+ revs.sortedWith(compareBy({ it.ts }, { it.id })).forEachIndexed { n, r ->
+ val i = entries.indexOfFirst { it.id == r.id }
+ entries[i] = entries[i].copy(version = n + 1)
+ }
+ }
+ Log.i(TAG, "re-filed revisions that an earlier build had split into a second artifact")
+ }
+ changed
+ }
+
private fun save() = synchronized(lock) {
val arr = JSONArray()
for (e in entries) arr.put(
@@ -182,6 +224,7 @@ object Pages {
entries.add(Page(id, e.getLong("ts"), e.getString("title"), e.getLong("group"), e.optInt("version", 1), file))
}
nextId = maxOf(o.optLong("next", 1L), (entries.maxOfOrNull { it.id } ?: 0L) + 1)
+ if (repairMisfiled()) save()
}.onFailure { Log.e(TAG, "could not read the page shelf; starting empty", it) }
}
}
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/PromptWindow.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/PromptWindow.kt
new file mode 100644
index 0000000..2829caa
--- /dev/null
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/PromptWindow.kt
@@ -0,0 +1,80 @@
+package net.kuafuai.andee.desktop
+
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.wrapContentHeight
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.layout.onSizeChanged
+import androidx.compose.ui.platform.LocalDensity
+import androidx.compose.ui.unit.DpSize
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.window.ApplicationScope
+import androidx.compose.ui.window.Window
+import androidx.compose.ui.window.WindowPosition
+import androidx.compose.ui.window.rememberWindowState
+import kotlinx.coroutines.delay
+
+/** The prompt window's title; the native side finds the window by it. */
+const val PROMPT_TITLE = "Andee Prompt"
+
+private val PROMPT_W = 460.dp
+
+/**
+ * A command to approve or a question to answer, as a card beside the ball, above every app.
+ *
+ * It used to be drawn inside the chat window, which had to be forced open and brought forward to
+ * show it: with the chat closed and only the ball on screen, the user saw nothing and the task sat
+ * waiting for its two-minute timeout. The phone's card is a child of the ball's own window for the
+ * same reason. This one is a window of its own (the ball's is exactly the ball's size), focusable
+ * because the "type 确定 to confirm" box needs the keyboard, and it exists only while a prompt is up.
+ */
+@Composable
+fun ApplicationScope.PromptWindow(ball: java.awt.Window?, pending: ApprovalGate.Pending?, ask: AskGate.Pending?) {
+ val up = ball != null && (pending != null || ask != null)
+ val density = LocalDensity.current
+ // The card sizes itself; the window follows it. Measured, because a transparent window cannot
+ // wrap its content and one big enough for the tallest card would swallow clicks around the small ones.
+ var height by remember { mutableStateOf(320.dp) }
+ val state = rememberWindowState(size = DpSize(PROMPT_W, height), position = WindowPosition(0.dp, 0.dp))
+ state.size = DpSize(PROMPT_W, height)
+ Window(
+ onCloseRequest = {},
+ state = state,
+ title = PROMPT_TITLE,
+ visible = up,
+ undecorated = true,
+ transparent = true,
+ resizable = false,
+ alwaysOnTop = true,
+ focusable = true,
+ ) {
+ val w = window
+ LaunchedEffect(up, pending, ask) {
+ if (!up) return@LaunchedEffect
+ var floated = false
+ var raised = false
+ // Re-placed while it is up: the card grows when the confirm box opens, and the ball can be dragged.
+ while (true) {
+ ball?.let { place(w, it, gap = 8) }
+ if (!floated && w.isShowing) floated = net.kuafuai.andee.desktop.mac.Bridge.floatWindow(PROMPT_TITLE)
+ if (!raised && w.isShowing) { w.toFront(); w.requestFocus(); raised = true }
+ delay(80)
+ }
+ }
+ Box(
+ Modifier.wrapContentHeight(align = Alignment.Top, unbounded = true)
+ .onSizeChanged { height = with(density) { it.height.toDp() } },
+ ) {
+ when {
+ pending != null -> ApprovalCard(pending)
+ ask != null -> AskCard(ask)
+ }
+ }
+ }
+}
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Settings.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Settings.kt
index 30853f0..bc9d1a7 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Settings.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Settings.kt
@@ -34,7 +34,7 @@ object Settings {
"brain", "llm_api_key", "llm_base_url", "llm_model", "llm_thinking", "llm_reasoning_effort",
"api_key", "asr_endpoint", "tts_endpoint", "asr_resource_id", "tts_resource_id", "tts_speaker",
"voice_provider", "lang", "max_steps", "scene", "ball", "ball_pos", "notify", "sweep", "sweep_quiet_minutes", "sweep_daily_cap",
- "shell_mode", "shell_workdirs", "shell_critical_confirm",
+ "shell_mode", "shell_workdirs", "shell_critical_confirm", "mute",
)
private val file get() = AppDirs.file("settings.properties")
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/SettingsScreen.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/SettingsScreen.kt
index d14e1b3..5a5c5ae 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/SettingsScreen.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/SettingsScreen.kt
@@ -26,17 +26,11 @@ import androidx.compose.ui.unit.sp
/**
* The settings sheet, the phone's card shape: a Glass header, segmented
- * pickers, and a 更多 section that opens the notebook, scenes, the expression
- * card and the self-check — the same four doors the phone's card reaches.
+ * pickers and fields. The notebook, scenes, vault, looks and self-check are in the sidebar.
*/
@Composable
fun SettingsScreen(
onDone: () -> Unit,
- onOpenNotebook: () -> Unit,
- onOpenVault: () -> Unit,
- onOpenScenes: () -> Unit,
- onOpenExpressions: () -> Unit,
- onOpenSelfCheck: () -> Unit,
) {
val zh = Strings.lang() == "zh"
fun t(a: String, b: String) = if (zh) a else b
@@ -48,7 +42,13 @@ fun SettingsScreen(
GlassHeader(t("设置", "Settings"), subtitle = t("所有设置都存在本机", "Everything stays on this Mac"), onClose = onDone)
SectionTitle(t("界面语言", "Language"))
- SegmentedPicker(listOf("zh" to "中", "en" to "EN"), lang) { lang = it; v["lang"] = it }
+ SegmentedPicker(listOf("zh" to "中", "en" to "EN"), lang) {
+ // Written the instant it is tapped, as on the phone: the redraw in the new language is
+ // the confirmation. Everything else typed on this page stays unsaved and survives the redraw.
+ lang = it
+ Settings.save(mapOf("lang" to it))
+ Strings.changed()
+ }
SectionTitle(t("大脑", "Backend"))
SegmentedPicker(listOf("local" to t("本机", "Own key"), "codeflying" to "CodeFlying"), v["brain"].orEmpty().ifEmpty { "local" }) { v["brain"] = it }
@@ -97,22 +97,17 @@ fun SettingsScreen(
SectionTitle(t("语音", "Voice"))
GlassField(t("语音 Key", "Voice key"), v["api_key"].orEmpty(), { v["api_key"] = it }, secret = true)
- SectionTitle(t("更多", "More"))
- MoreRow(t("小本本", "Notebook"), t("它记得的事", "What it remembers"), onOpenNotebook)
- MoreRow(t("保险箱", "Vault"), t("你自己的账号", "Your own accounts"), onOpenVault)
- MoreRow(t("情景", "Scenes"), t("和它一起学会的做事方式", "Ways of working it learned"), onOpenScenes)
- MoreRow(t("形象与表情", "Look & expressions"), t("换一个样子", "Pick a face"), onOpenExpressions)
- MoreRow(t("自检", "Self-check"), t("看看哪里没开", "What is missing"), onOpenSelfCheck)
Spacer(Modifier.height(20.dp))
- Row(horizontalArrangement = Arrangement.spacedBy(10.dp)) {
- PrimaryButton(t("保存", "Save")) {
+ // Equal halves, cancel on the left and the main action on the right.
+ Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(10.dp)) {
+ SecondaryButton(t("取消", "Cancel"), Modifier.weight(1f)) { onDone() }
+ PrimaryButton(t("保存", "Save"), Modifier.weight(1f)) {
Settings.save(v.toMap())
v["ball"]?.let { MacBall.pickLook(it) }
Thread { Assistant.ensure() }.start()
onDone()
}
- Pill(t("取消", "Cancel")) { onDone() }
}
Spacer(Modifier.height(12.dp))
Text(
@@ -122,21 +117,6 @@ fun SettingsScreen(
}
}
-@Composable
-private fun MoreRow(title: String, subtitle: String, onClick: () -> Unit) {
- Row(
- Modifier.fillMaxWidth().glassPanel(14).clickable { onClick() }.padding(12.dp),
- verticalAlignment = Alignment.CenterVertically,
- ) {
- Column(Modifier.weight(1f)) {
- Text(title, color = Palette.label, fontSize = 14.sp)
- Text(subtitle, color = Palette.secondary, fontSize = 11.sp)
- }
- Text("›", color = Palette.secondary, fontSize = 18.sp)
- }
- Spacer(Modifier.height(8.dp))
-}
-
@Composable
private fun FullOpenConfirm(zh: Boolean, onYes: () -> Unit, onNo: () -> Unit) {
fun t(a: String, b: String) = if (zh) a else b
@@ -155,9 +135,9 @@ private fun FullOpenConfirm(zh: Boolean, onYes: () -> Unit, onNo: () -> Unit) {
cursorBrush = androidx.compose.ui.graphics.SolidColor(Palette.accent),
modifier = Modifier.fillMaxWidth().glassWell(10).padding(10.dp),
)
- Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
- if (typed.trim() == "开启" || typed.trim().equals("enable", true)) PrimaryButton(t("开启完全开放", "Enable full access")) { onYes() }
- Pill(t("取消", "Cancel")) { onNo() }
+ Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
+ SecondaryButton(t("取消", "Cancel"), Modifier.weight(1f)) { onNo() }
+ if (typed.trim() == "开启" || typed.trim().equals("enable", true)) PrimaryButton(t("开启完全开放", "Enable full access"), Modifier.weight(1f)) { onYes() }
}
}
}
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Sidebar.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Sidebar.kt
index 4af6110..6ceaebf 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Sidebar.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Sidebar.kt
@@ -70,22 +70,26 @@ fun Sidebar(nav: Nav, width: androidx.compose.ui.unit.Dp = 228.dp) {
Text("Andee", color = Palette.title, fontSize = 18.sp, fontWeight = FontWeight.SemiBold)
}
Spacer(Modifier.height(10.dp))
- Text(
- "+ " + t("新对话", "New chat"), color = Palette.title, fontSize = 14.sp, fontWeight = FontWeight.Medium,
- modifier = Modifier.fillMaxWidth()
+ Row(
+ Modifier.fillMaxWidth()
.background(Palette.tint.copy(alpha = 0.55f), RoundedCornerShape(12.dp))
.border(1.dp, Palette.accent.copy(alpha = 0.6f), RoundedCornerShape(12.dp))
.clickable { Assistant.newConversation(); nav.go("chat") }
.padding(horizontal = 12.dp, vertical = 9.dp),
- )
+ verticalAlignment = Alignment.CenterVertically,
+ ) {
+ Icon(AndeeIcon.PLUS, Palette.title, 16.dp)
+ Spacer(Modifier.width(8.dp))
+ Text(t("新对话", "New chat"), color = Palette.title, fontSize = 14.sp, fontWeight = FontWeight.Medium)
+ }
Spacer(Modifier.height(12.dp))
- NavItem("💬", t("对话", "Chat"), here == "chat") { nav.go("chat") }
- NavItem("📄", t("产物", "Artifacts") + if (artifactCount > 0) " $artifactCount" else "", here == "artifacts") { nav.go("artifacts") }
- NavItem("📓", t("小本本", "Notebook"), here == "notebook") { nav.go("notebook") }
- NavItem("🎭", t("情景", "Scenes"), here == "scenes") { nav.go("scenes") }
- NavItem("🔐", t("保险箱", "Vault"), here == "vault") { nav.go("vault") }
- NavItem("🙂", t("形象与表情", "Look"), here == "expressions") { nav.go("expressions") }
+ NavItem(AndeeIcon.CHAT, t("对话", "Chat"), here == "chat") { nav.go("chat") }
+ NavItem(AndeeIcon.DOCUMENT, t("产物", "Artifacts") + if (artifactCount > 0) " $artifactCount" else "", here == "artifacts") { nav.go("artifacts") }
+ NavItem(AndeeIcon.NOTEBOOK, t("小本本", "Notebook"), here == "notebook") { nav.go("notebook") }
+ NavItem(AndeeIcon.SPARKLE, t("情景", "Scenes"), here == "scenes") { nav.go("scenes") }
+ NavItem(AndeeIcon.LOCK, t("保险箱", "Vault"), here == "vault") { nav.go("vault") }
+ NavItem(AndeeIcon.FACE, t("形象与表情", "Look"), here == "expressions") { nav.go("expressions") }
Spacer(Modifier.height(14.dp))
Row(Modifier.fillMaxWidth().padding(horizontal = 8.dp), verticalAlignment = Alignment.CenterVertically) {
@@ -126,13 +130,13 @@ fun Sidebar(nav: Nav, width: androidx.compose.ui.unit.Dp = 228.dp) {
}
Spacer(Modifier.height(8.dp))
- NavItem("✅", t("自检", "Self-check"), here == "selfcheck") { nav.go("selfcheck") }
- NavItem("🔧", t("设置", "Settings"), here == "settings") { nav.go("settings") }
+ NavItem(AndeeIcon.CHECK_CIRCLE, t("自检", "Self-check"), here == "selfcheck") { nav.go("selfcheck") }
+ NavItem(AndeeIcon.SLIDERS, t("设置", "Settings"), here == "settings") { nav.go("settings") }
}
}
@Composable
-private fun NavItem(icon: String, label: String, selected: Boolean, onClick: () -> Unit) {
+private fun NavItem(icon: AndeeIcon, label: String, selected: Boolean, onClick: () -> Unit) {
Row(
Modifier.fillMaxWidth()
.background(if (selected) Palette.glass else Color.Transparent, RoundedCornerShape(10.dp))
@@ -141,7 +145,8 @@ private fun NavItem(icon: String, label: String, selected: Boolean, onClick: ()
.padding(horizontal = 10.dp, vertical = 8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
- Text(icon, fontSize = 14.sp, modifier = Modifier.width(24.dp))
+ Icon(icon, if (selected) Palette.title else Palette.secondary, 18.dp)
+ Spacer(Modifier.width(10.dp))
Text(label, color = if (selected) Palette.title else Palette.label, fontSize = 14.sp)
}
}
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/UiShot.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/UiShot.kt
index 104d049..7b95dc9 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/UiShot.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/UiShot.kt
@@ -24,9 +24,22 @@ fun main(args: Array<String>) {
val out = File(args.getOrElse(3) { "/tmp/ui.png" })
System.setProperty("andee.home", File.createTempFile("andee-shot", "").also { it.delete(); it.mkdirs() }.path)
if (args.getOrElse(4) { "" } == "empty") Scrollback.wipe() else seed()
+ @Suppress("UNUSED_VARIABLE") val unused = 0
+ if (args.getOrElse(4) { "" } == "pending") {
+ Scrollback.add(Scrollback.Role.ASSISTANT, "这一版改的是手感和反馈:\n\n" + "金豆出现了,每吃 5 个普通豆随机长出一颗金色豆子,6.5 秒后消失,最后一秒会闪,吃到 3 分。撞死时棋盘会抖一下、蛇头位置炸开一层红粒子。".repeat(4) + "\n\n要是你原本想改的是别的东西,跟我说一句我就接着改。")
+ Pages.add("贪吃蛇", "<html>", null)?.let { Assistant.beginEdit(it) }
+ }
+ if (args.getOrElse(4) { "" } == "attach") {
+ val img = java.awt.image.BufferedImage(600, 400, java.awt.image.BufferedImage.TYPE_INT_RGB).also { b ->
+ b.createGraphics().apply { paint = java.awt.GradientPaint(0f, 0f, java.awt.Color(0x2D6CDF), 600f, 400f, java.awt.Color(0xF2A33A)); fillRect(0, 0, 600, 400); dispose() }
+ }
+ val png = java.io.ByteArrayOutputStream().also { javax.imageio.ImageIO.write(img, "png", it) }.toByteArray()
+ val doc = File(System.getProperty("andee.home"), "季度复盘-final.pdf").also { it.writeText("%PDF") }
+ ChatPreview.attachments = listOfNotNull(Attachments.fromBytes("a.png", png), Attachments.fromBytes("b.png", png), Attachments.fromFile(doc))
+ }
val nav = Nav().also { if (screen != "chat") it.go(screen) }
val density = Density(2f)
- val scene = ImageComposeScene(width = w * 2, height = h * 2, density = density) { AppRoot(nav, null, null) }
+ val scene = ImageComposeScene(width = w * 2, height = h * 2, density = density) { AppRoot(nav) }
// Let the backdrop (decoded off the main thread) and the layout settle.
repeat(12) { scene.render(it * 50_000_000L); Thread.sleep(60) }
val img = scene.render(700_000_000L)
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 50f2e16..c764b61 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/UserPrompts.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/UserPrompts.kt
@@ -89,14 +89,18 @@ class UiTools : DesktopBrainHost.ToolProvider {
val title = p.optString("title").ifEmpty { defaultTitle }
// Ownership is decided here, never by the model: get_page armed a group,
// an explicit `revise` wins, and either way the target is consumed.
- val group = Pages.armOrResolve(p.optString("revise").trim().ifEmpty { null }?.toLongOrNull())
+ // `revise` is a page id (what list_pages shows), not a group id: translate it, or an id that
+ // happens to equal some other artifact's group files the page under the wrong artifact.
+ // An id that names nothing is ignored rather than refused — the page is still worth showing.
+ val explicit = p.optString("revise").trim().toLongOrNull()?.let { Pages.pageById(it)?.groupId }
+ val group = Pages.armOrResolve(explicit)
val page = Pages.add(title, html, group) ?: throw IllegalStateException("the page could not be saved")
// The phone renders in an in-app WebView; the Mac's honest first cut opens
// the page in the default browser. The shelf (list_pages / get_page / the
// per-turn index) is what the model reads back, and that works the same.
runCatching { net.kuafuai.andee.desktop.mac.Bridge.open(page.file!!.absolutePath) }
.onFailure { Log.w("UiTools", "could not open the page: ${it.message}") }
- Scrollback.add(Scrollback.Role.ASSISTANT, "📄 ${title}")
+ Scrollback.add(Scrollback.Role.ASSISTANT, title)
return JSONObject().put("opened", true).put("id", page.id).put("title", title)
.put("version", page.version).put("closed", false)
}
@@ -249,9 +253,9 @@ fun AskCard(p: AskGate.Pending) {
val zh = Strings.lang() == "zh"
fun t(a: String, b: String) = if (zh) a else b
val isAlert = p.buttons.isEmpty()
- Box(Modifier.fillMaxSize().background(Color(0xB3000000)), contentAlignment = Alignment.Center) {
+ run {
Column(
- Modifier.widthIn(max = 440.dp).fillMaxWidth().padding(20.dp)
+ Modifier.fillMaxWidth()
.background(Palette.card, RoundedCornerShape(18.dp))
.border(1.dp, Palette.hairline, RoundedCornerShape(18.dp)).padding(20.dp),
verticalArrangement = Arrangement.spacedBy(12.dp),
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/mac/MacDevice.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/mac/MacDevice.kt
index 078e4b3..7291d80 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/mac/MacDevice.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/mac/MacDevice.kt
@@ -237,7 +237,7 @@ class MacDevice : DesktopBrainHost.ToolProvider {
return JSONObject().put("error", "The camera could not be used on this Mac (no camera, or the Camera permission is missing). Say so; do not claim to see.")
}
val b64 = java.util.Base64.getEncoder().encodeToString(f.readBytes())
- Scrollback.add(Scrollback.Role.ASSISTANT, "📷 " + (if (Strings.lang() == "zh") "看了一眼(" else "Looked (") + (if (front) "front" else "back") + ")")
+ Scrollback.add(Scrollback.Role.ASSISTANT, (if (Strings.lang() == "zh") "看了一眼(" else "Looked (") + (if (front) "front" else "back") + ")")
return JSONObject().put("png_base64", b64).put("camera", if (front) "front" else "back")
} finally { f.delete() }
}
@@ -370,7 +370,7 @@ object MacVault {
val pw = find(label).optString("password")
require(pw.isNotEmpty()) { "That entry has no password saved." }
require(Bridge.focusedValue().optBoolean("focused")) { "Click into the password field first." }
- Scrollback.add(Scrollback.Role.ASSISTANT, "🔐 正在填入「$label」的密码… / filling the password for “$label”…")
+ Scrollback.add(Scrollback.Role.ASSISTANT, "正在填入「$label」的密码… / filling the password for “$label”…")
val ok = Bridge.setFocusedValue(pw) || run { Bridge.typeText(pw); true }
val len = Bridge.focusedValue().optString("value").length
Log.i("Vault", "filled $label (${pw.length} chars)")
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/mac/MacScreen.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/mac/MacScreen.kt
index 40b178d..01a8203 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/mac/MacScreen.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/mac/MacScreen.kt
@@ -29,6 +29,11 @@ import javax.imageio.ImageIO
class MacScreen(
/** Hide our own windows for a gesture or a capture, and show them again after. */
private val hideOwnWindows: (Boolean) -> Unit,
+ /**
+ * Called before any screen tool runs: gets the chat window off the screen, as the phone folds its
+ * card. Otherwise it covers the app being driven and, when it is frontmost, takes the keystrokes.
+ */
+ private val yieldScreen: () -> Unit = {},
) : DesktopBrainHost.ToolProvider {
private data class El(val eid: String, val cx: Double, val cy: Double, val press: Boolean, val editable: Boolean, val label: String)
@@ -43,6 +48,7 @@ class MacScreen(
override fun dispatch(method: String, p: JSONObject, silent: Boolean, untrusted: Boolean): Any? {
if (!Bridge.available) throw IllegalStateException("The Mac screen bridge did not load; screen tools cannot run here.")
if (!Bridge.axTrusted()) throw IllegalStateException(PermissionHelp.ACCESSIBILITY)
+ yieldScreen()
return when (method) {
"screen.ui_tree" -> uiTree(p.optBoolean("with_shot", false))
"screen.tap_id" -> tapId(req(p, "eid"), long = false)
@@ -302,7 +308,8 @@ class MacScreen(
requireNorm(cx, "x"); requireNorm(cy, "y")
val left = (cx - w / 2).coerceIn(0, 999); val top = (cy - h / 2).coerceIn(0, 999)
val right = (left + w).coerceAtMost(1000); val bottom = (top + h).coerceAtMost(1000)
- val full = capture()
+ // Native resolution: a crop of an already-shrunk picture magnifies blur, not detail.
+ val full = capture(0)
val sx = full.width / 1000.0; val sy = full.height / 1000.0
val sub = full.getSubimage((left * sx).toInt(), (top * sy).toInt(), ((right - left) * sx).toInt().coerceAtLeast(4), ((bottom - top) * sy).toInt().coerceAtLeast(4))
val k = SHOT_MAX_SIDE.toDouble() / maxOf(sub.width, sub.height)
@@ -356,14 +363,24 @@ class MacScreen(
g.color = Color(255, 255, 255, 38); g.stroke = BasicStroke(3f); g.drawLine(0, y, img.width, y)
g.color = Color(0, 0, 0, 96); g.stroke = BasicStroke(1f); g.drawLine(0, y, img.width, y)
}
- val size = (minOf(cw, ch) * 0.2).coerceIn(9.0, 24.0).toFloat()
+ // Rulers on the top and left edges only, never inside a cell: a label at a cell's centre sat on
+ // whatever that cell held, and the picture the brain aimed from had its target covered.
+ val size = (minOf(cw, ch) * 0.2).coerceIn(10.0, 24.0).toFloat()
g.font = Font(Font.SANS_SERIF, Font.BOLD, size.toInt())
- for (r in 0 until GRID_ROWS) for (c in 0 until GRID_COLS) {
- val label = "${'A' + c}${r + 1}".let { if (zoom) it.lowercase() else it }
- val w = g.fontMetrics.stringWidth(label)
- val tx = ((c + 0.5) * cw - w / 2).toInt(); val ty = ((r + 0.5) * ch + size * 0.35).toInt()
- g.color = Color(0, 0, 0, 165); for (dx in -1..1) for (dy in -1..1) g.drawString(label, tx + dx, ty + dy)
- g.color = Color(255, 255, 255, 175); g.drawString(label, tx, ty)
+ val fm = g.fontMetrics
+ fun mark(label: String, cx: Double, baseline: Double) {
+ val w = fm.stringWidth(label)
+ val pad = size * 0.25
+ g.color = Color(0, 0, 0, 150)
+ g.fillRoundRect((cx - w / 2 - pad).toInt(), (baseline - size * 1.02).toInt(), (w + 2 * pad).toInt(), (size * 1.32).toInt(), (size * 0.6).toInt(), (size * 0.6).toInt())
+ g.color = Color(255, 255, 255, 235)
+ g.drawString(label, (cx - w / 2).toInt(), baseline.toInt())
+ }
+ val top = size * 1.25
+ for (c in 0 until GRID_COLS) { val l = ('A' + c).toString(); mark(if (zoom) l.lowercase() else l, (c + 0.5) * cw, top) }
+ for (r in 0 until GRID_ROWS) {
+ val l = (r + 1).toString()
+ mark(l, fm.stringWidth(l) / 2.0 + size * 0.35, (r + 0.5) * ch + size * 0.35)
}
g.dispose()
}
@@ -404,7 +421,7 @@ class MacScreen(
companion object {
private const val GRID_COLS = 8
private const val GRID_ROWS = 12
- private const val SHOT_MAX_SIDE = 1280
+ private const val SHOT_MAX_SIDE = 1600
private const val MAX_LINES = 300
val METHODS = setOf(
@@ -416,7 +433,8 @@ class MacScreen(
val SCREEN_RECORDING_NOTE: String get() = PermissionHelp.SCREEN_RECORDING
private const val AIM_GRID_NOTE =
- "This image carries an 8x12 aim grid, A1 (top-left) to H12 (bottom-right), each label at the centre of its cell. " +
+ "This image carries an 8x12 aim grid, A1 (top-left) to H12 (bottom-right): letters run along the TOP edge and numbers " +
+ "down the LEFT edge like a map's rulers, and nothing is printed inside a cell. " +
"To click something, name its cell and which part of the cell — {\"cell\": \"D7\", \"part\": \"bottom-right\"} on " +
"tap_by_coordinates. Andee's own windows are never in this picture."
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/shell/ShellTools.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/shell/ShellTools.kt
index dc016ed..47a7290 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/shell/ShellTools.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/shell/ShellTools.kt
@@ -50,7 +50,7 @@ class ShellTools(
private val trashTool: File by lazy { locateTrashTool() }
private val trash: TrashBroker by lazy {
TrashBroker({ ShellPolicy.workDirs() }, { trashTool }).start()
- .also { b -> b.onTrashed = { n -> record("🗑 moved $n item(s) to the Trash") } }
+ .also { b -> b.onTrashed = { n -> record("moved $n item(s) to the Trash") } }
}
/**
* The shims and the helper sockets live outside Andee's data folder: the
@@ -75,7 +75,7 @@ class ShellTools(
init {
// Keys are a capability like the vault: a turn a stranger started cannot use them.
AndeeAgent.allowSigning = { turn == Turn.USER || turn == Turn.WAKE_OWN }
- AndeeAgent.onSign = { key, host -> record("🔑 $key" + (host?.let { " → $it" } ?: "")) }
+ AndeeAgent.onSign = { key, host -> record("key $key" + (host?.let { " → $it" } ?: "")) }
}
/** Triple-click / stop: end every running command and say the user did it. */
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.kt
index 2030e05..bafa6d5 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
@@ -37,7 +37,13 @@ object MacVoice {
enum class State { IDLE, LISTENING, SPEAKING }
/** Every change goes to the ball as well as the window, which has its own listener slot. */
+ @Volatile private var current = State.IDLE
+
+ /** The answer is being read out loud right now. */
+ fun isSpeaking() = current == State.SPEAKING
+
private fun state(s: State) {
+ current = s
net.kuafuai.andee.desktop.MacBall.onVoice(s)
onState(s)
}
@@ -57,7 +63,9 @@ object MacVoice {
asrUid = Settings.uid(),
ttsEndpoint = Settings.get("tts_endpoint").ifEmpty { "wss://openspeech.bytedance.com/api/v3/tts/bidirection" },
ttsResourceId = Settings.get("tts_resource_id").ifEmpty { "seed-tts-2.0" },
- ttsSpeaker = Settings.get("tts_speaker").ifEmpty { "zh_female_vv_uranus_bigtts" },
+ // The voice is the character's: read from the look the ball is wearing, per utterance, as the phone does.
+ ttsSpeaker = net.kuafuai.andee.ui.ball.BallLooks.byName(Settings.get("ball").ifEmpty { null }).voice
+ .ifEmpty { "zh_female_vv_uranus_bigtts" },
ttsSampleRate = 24_000,
voiceProvider = Settings.get("voice_provider").ifEmpty { VoiceSettings.VOICE_HUOSHAN },
googleApiKey = Settings.get("google_api_key"),
@@ -142,10 +150,20 @@ object MacVoice {
// ---- speaking ---------------------------------------------------------------
- /** Speak [raw] (mood markers are stripped first). Returns at once; audio plays in the background. */
+ /** The brain's "this settles it" sentinel; the phone strips it before speaking too (case-insensitive). */
+ private val END_MARKER = Regex("""(?i)\[END]""")
+
+ /**
+ * What the mouth gets: the same three steps the phone takes, in the same order — the end
+ * sentinel, then Markdown, then mood markers. Whatever survives is read out loud as a word.
+ */
+ internal fun speakable(raw: String): String =
+ SpeechMood.parse(net.kuafuai.andee.text.stripMarkdown(END_MARKER.replace(raw, ""))).text.trim()
+
+ /** Speak [raw] (sentinel, Markdown and mood markers are stripped first). Returns at once; audio plays in the background. */
fun speak(raw: String) {
if (net.kuafuai.andee.desktop.Meeting.isActive()) return // never speak into the recording
- val text = SpeechMood.parse(raw).text.trim()
+ val text = speakable(raw)
if (text.isEmpty() || !configured()) return
stopSpeaking()
val cfg = settings()
diff --git a/desktop/src/test/kotlin/net/kuafuai/andee/desktop/AttachmentsTest.kt b/desktop/src/test/kotlin/net/kuafuai/andee/desktop/AttachmentsTest.kt
new file mode 100644
index 0000000..57caa8e
--- /dev/null
+++ b/desktop/src/test/kotlin/net/kuafuai/andee/desktop/AttachmentsTest.kt
@@ -0,0 +1,101 @@
+package net.kuafuai.andee.desktop
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNotNull
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import java.awt.Color
+import java.awt.image.BufferedImage
+import java.io.ByteArrayOutputStream
+import java.io.File
+import java.util.Base64
+import javax.imageio.ImageIO
+
+class AttachmentsTest {
+
+ private fun png(w: Int, h: Int, fill: Color = Color.RED): ByteArray {
+ val img = BufferedImage(w, h, BufferedImage.TYPE_INT_RGB)
+ val g = img.createGraphics(); g.color = fill; g.fillRect(0, 0, w, h); g.dispose()
+ return ByteArrayOutputStream().also { ImageIO.write(img, "png", it) }.toByteArray()
+ }
+
+ private fun decode(b64: String): BufferedImage = ImageIO.read(Base64.getDecoder().decode(b64).inputStream())
+
+ @Test
+ fun `a small picture is kept at its size and sent as a JPEG`() {
+ val p = Attachments.fromBytes("a.png", png(300, 200))!!
+ val out = decode(p.jpegBase64)
+ assertEquals(300, out.width); assertEquals(200, out.height)
+ }
+
+ @Test
+ fun `a large picture is shrunk to the long edge, proportions kept`() {
+ val p = Attachments.fromBytes("big.png", png(4000, 2000))!!
+ val out = decode(p.jpegBase64)
+ assertEquals(1568, out.width)
+ assertEquals(784, out.height)
+ }
+
+ @Test
+ fun `the thumbnail is small`() {
+ val p = Attachments.fromBytes("big.png", png(4000, 2000))!!
+ assertTrue(maxOf(p.thumb.width, p.thumb.height) <= 112)
+ }
+
+ @Test
+ fun `bytes that are not a picture are refused rather than thrown`() {
+ assertNull(Attachments.fromBytes("x.png", "not an image".toByteArray()))
+ }
+
+ @Test
+ fun `a sideways phone photo is turned upright`() {
+ // A 400x200 JPEG with EXIF orientation 6 (rotate 90° clockwise) is a portrait picture.
+ val jpeg = ByteArrayOutputStream().also { ImageIO.write(BufferedImage(400, 200, BufferedImage.TYPE_INT_RGB), "jpg", it) }.toByteArray()
+ val withExif = withOrientation(jpeg, 6)
+ val out = decode(Attachments.fromBytes("p.jpg", withExif)!!.jpegBase64)
+ assertEquals("width and height swap", 200, out.width)
+ assertEquals(400, out.height)
+ }
+
+ @Test
+ fun `an image file becomes a photo and any other file a reference`() {
+ val dir = File.createTempFile("att", "").also { it.delete(); it.mkdirs() }
+ val pic = File(dir, "p.png").also { it.writeBytes(png(50, 50)) }
+ val doc = File(dir, "notes.pdf").also { it.writeText("%PDF-1.4") }
+ assertTrue(Attachments.fromFile(pic) is Attachment.Photo)
+ val ref = Attachments.fromFile(doc)
+ assertTrue(ref is Attachment.FileRef)
+ assertEquals("notes.pdf", ref!!.label)
+ assertNull(Attachments.fromFile(File(dir, "missing.txt")))
+ }
+
+ @Test
+ fun `a file named like a picture that is not one still reaches the model as a file`() {
+ val f = File.createTempFile("fake", ".png").also { it.writeText("hello") }
+ assertTrue(Attachments.fromFile(f) is Attachment.FileRef)
+ }
+
+ @Test
+ fun `the note names every path so this file means something`() {
+ val a = File.createTempFile("note", ".txt").also { it.writeText("x") }
+ val note = Attachments.fileNote(listOf(Attachment.FileRef(a)))
+ assertTrue(note.contains(a.absolutePath))
+ assertTrue(note.contains("run_command"))
+ }
+
+ /** Insert a minimal EXIF APP1 segment carrying only the Orientation tag. */
+ private fun withOrientation(jpeg: ByteArray, value: Int): ByteArray {
+ val tiff = byteArrayOf(
+ 0x4D, 0x4D, 0x00, 0x2A, 0x00, 0x00, 0x00, 0x08, // big-endian TIFF header, IFD at 8
+ 0x00, 0x01, // one entry
+ 0x01, 0x12, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, // tag 0x0112 (Orientation), SHORT, count 1
+ 0x00, value.toByte(), 0x00, 0x00, // the value
+ 0x00, 0x00, 0x00, 0x00, // no next IFD
+ )
+ val body = "Exif\u0000\u0000".toByteArray(Charsets.ISO_8859_1) + tiff
+ val len = body.size + 2
+ val app1 = byteArrayOf(0xFF.toByte(), 0xE1.toByte(), (len shr 8).toByte(), len.toByte()) + body
+ return jpeg.copyOfRange(0, 2) + app1 + jpeg.copyOfRange(2, jpeg.size)
+ }
+}
diff --git a/desktop/src/test/kotlin/net/kuafuai/andee/desktop/PagesTest.kt b/desktop/src/test/kotlin/net/kuafuai/andee/desktop/PagesTest.kt
index 042564f..72a2c00 100644
--- a/desktop/src/test/kotlin/net/kuafuai/andee/desktop/PagesTest.kt
+++ b/desktop/src/test/kotlin/net/kuafuai/andee/desktop/PagesTest.kt
@@ -55,6 +55,39 @@ class PagesTest {
assertNull(Pages.armOrResolve(null)) // armed was cleared, not just overridden
}
+ @Test
+ fun `a target carried from a tap survives the turn start exactly once`() {
+ Pages.resetForTest(fresh())
+ val a = Pages.add("a", "<html>", null)!!
+ Pages.armNextTurn(a.groupId)
+ Pages.beginTurn() // the turn the tap started
+ assertEquals(a.groupId, Pages.armOrResolve(null))
+ Pages.beginTurn() // the turn after is unrelated
+ assertNull(Pages.armOrResolve(null))
+ }
+
+ @Test
+ fun `a split artifact on disk is folded back into one`() {
+ val dir = fresh()
+ Pages.resetForTest(dir)
+ File(dir, "pages").mkdirs()
+ // What the shelf looked like: 3 and 4 were revisions of page 2, filed under a group named "2".
+ for (id in 1..4) File(dir, "pages/page_$id.html").writeText("x")
+ File(dir, "pages.json").writeText(
+ """{"next":5,"pages":[
+ {"id":1,"ts":1,"title":"s","group":1,"version":1,"file":true},
+ {"id":2,"ts":2,"title":"s","group":1,"version":2,"file":true},
+ {"id":3,"ts":3,"title":"s","group":2,"version":1,"file":true},
+ {"id":4,"ts":4,"title":"s","group":2,"version":2,"file":true}]}""",
+ )
+ Pages.reloadForTest()
+ assertEquals(1, Pages.artifacts().size)
+ assertEquals(listOf(4, 3, 2, 1), Pages.versionsOf(1).map { it.version })
+ assertEquals(4L, Pages.artifacts().single().id)
+ // and a healthy shelf is left alone
+ assertTrue(!Pages.repairMisfiled())
+ }
+
@Test
fun `the armed target is cleared at the turn boundary`() {
Pages.resetForTest(fresh())
diff --git a/desktop/src/test/kotlin/net/kuafuai/andee/desktop/SpeakableTest.kt b/desktop/src/test/kotlin/net/kuafuai/andee/desktop/SpeakableTest.kt
new file mode 100644
index 0000000..97aed18
--- /dev/null
+++ b/desktop/src/test/kotlin/net/kuafuai/andee/desktop/SpeakableTest.kt
@@ -0,0 +1,25 @@
+package net.kuafuai.andee.desktop
+
+import net.kuafuai.andee.desktop.voice.MacVoice
+import org.junit.Test
+import org.junit.Assert.assertEquals
+
+class SpeakableTest {
+ @Test fun endSentinelIsNotRead() {
+ assertEquals("好了。", MacVoice.speakable("好了。[END]"))
+ assertEquals("好了。", MacVoice.speakable("好了。 [end]"))
+ }
+
+ @Test fun markdownIsNotRead() {
+ assertEquals("已经打开微信了", MacVoice.speakable("已经**打开微信**了"))
+ assertEquals("结果\n第一步", MacVoice.speakable("## 结果\n- 第一步"))
+ }
+
+ @Test fun moodMarkersAreNotRead() {
+ assertEquals("找到了,在第三页。", MacVoice.speakable("[happy] 找到了,在第三页。[END]"))
+ }
+
+ @Test fun snakeCaseSurvives() {
+ assertEquals("调用 tap_screen 完成", MacVoice.speakable("调用 tap_screen 完成"))
+ }
+}