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(brain): explain HTTP errors, point 402 at the provider's top-up page

booboosui6ed390cbc045
diff --git a/app/src/main/java/net/kuafuai/andee/brain/LlmClient.kt b/app/src/main/java/net/kuafuai/andee/brain/LlmClient.kt
index 6f79dc7..c1b5e10 100644
--- a/app/src/main/java/net/kuafuai/andee/brain/LlmClient.kt
+++ b/app/src/main/java/net/kuafuai/andee/brain/LlmClient.kt
@@ -9,6 +9,31 @@ import org.json.JSONArray
import org.json.JSONObject
import java.util.concurrent.TimeUnit
+/**
+ * A non-2xx answer from the backend. A type of its own so the status code
+ * survives to [LocalBrain], which has a `Context` and can turn it into a
+ * sentence the user can act on; this class has none and must not grow one.
+ * The message stays `LLM HTTP <code>: <body>` so logs read as they always did.
+ */
+class LlmHttpException(val code: Int, val body: String) :
+ IllegalStateException("LLM HTTP $code: ${body.take(600)}") {
+
+ /**
+ * The gateway's own words for what it disliked, without the JSON around
+ * them. Falls back to the raw body for gateways that don't follow the
+ * `{"error":{"message":…}}` shape.
+ */
+ val detail: String
+ get() = runCatching {
+ val err = JSONObject(body).opt("error")
+ when (err) {
+ is JSONObject -> err.optString("message")
+ is String -> err
+ else -> ""
+ }
+ }.getOrDefault("").ifBlank { body }.trim()
+}
+
/**
* One blocking call to an OpenAI-compatible `/chat/completions`, aimed at
* DeepSeek.
@@ -130,7 +155,7 @@ class LlmClient(
// place entirely.
val raw = resp.body?.string().orEmpty()
if (!resp.isSuccessful) {
- throw IllegalStateException("LLM HTTP ${resp.code}: ${raw.take(600)}")
+ throw LlmHttpException(resp.code, raw)
}
raw
}
diff --git a/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt b/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt
index b5ddaba..5896a7e 100644
--- a/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt
+++ b/app/src/main/java/net/kuafuai/andee/brain/LocalBrain.kt
@@ -327,15 +327,7 @@ class LocalBrain(
if (gen == generation.get()) {
Log.e(TAG, "turn $gen ($kind) failed", t)
// A silent turn has nobody to apologise to; it just ends.
- if (!kind.silent) {
- onFinal(
- AppLocale.str(
- appContext,
- R.string.brain_error,
- t.message ?: t.javaClass.simpleName,
- )
- )
- }
+ if (!kind.silent) onFinal(explainFailure(t))
} else {
// Almost always the cancelled call's own IOException.
Log.i(TAG, "turn $gen abandoned: ${t.message}")
@@ -356,6 +348,36 @@ class LocalBrain(
}
}
+ /**
+ * The sentence the user hears when a turn dies. A backend status code is
+ * the one failure where the user can actually do something about it, so it
+ * gets its own words instead of `HTTP 402: {"error":…}` read aloud. 402 is
+ * the one that matters most — an empty account looks, from the tablet,
+ * exactly like the ball going quiet. Anything unrecognised falls back to
+ * the old generic line with the raw message, so nothing is hidden.
+ */
+ private fun explainFailure(t: Throwable): String {
+ fun s(id: Int, vararg args: Any) = AppLocale.str(appContext, id, *args)
+ if (t is LlmHttpException) {
+ val codeFlying = cfg.mode == VoiceConfig.BRAIN_CODEFLYING
+ return when (t.code) {
+ 402 -> if (codeFlying) s(R.string.brain_error_402_codeflying) else s(R.string.brain_error_402)
+ 401, 403 -> s(R.string.brain_error_auth, t.code)
+ 400 -> s(R.string.brain_error_400, t.detail.take(160).ifBlank { "HTTP 400" })
+ 404 -> s(R.string.brain_error_404)
+ 408, 429 -> s(R.string.brain_error_429)
+ in 500..599 -> s(R.string.brain_error_5xx, t.code)
+ else -> s(R.string.brain_error, t.message ?: t.javaClass.simpleName)
+ }
+ }
+ return when (t) {
+ is java.net.SocketTimeoutException -> s(R.string.brain_error_timeout)
+ is java.net.UnknownHostException,
+ is java.net.ConnectException -> s(R.string.brain_error_network)
+ else -> s(R.string.brain_error, t.message ?: t.javaClass.simpleName)
+ }
+ }
+
/** Run one queued turn, if any. Loop thread only. */
private fun drainWakes() {
val (next, kind) = pendingWakes.removeFirstOrNull() ?: return
diff --git a/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt b/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt
index 00a9256..54fc64f 100644
--- a/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt
+++ b/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt
@@ -359,6 +359,7 @@ object SelfCheck {
detail = when (r.kind) {
Kind.URL -> R.string.check_detail_model_url
Kind.KEY -> R.string.check_detail_model_key
+ Kind.BALANCE -> R.string.check_detail_model_balance
Kind.MODEL -> R.string.check_detail_model_name
Kind.OTHER -> R.string.check_detail_model_other
},
@@ -804,7 +805,7 @@ object SelfCheck {
data class Bad(val kind: Kind, val detail: String) : Reach
}
- private enum class Kind { URL, KEY, MODEL, OTHER }
+ private enum class Kind { URL, KEY, BALANCE, MODEL, OTHER }
/**
* Ask the backend who it is, without paying for an answer.
@@ -844,6 +845,8 @@ object SelfCheck {
resp.code == 401 || resp.code == 403 -> return Reach.Bad(Kind.KEY, "HTTP ${resp.code}")
+ resp.code == 402 -> return Reach.Bad(Kind.BALANCE, "HTTP 402")
+
resp.code == 429 -> return Reach.Throttled
// No such endpoint on this gateway: not an error in the
@@ -892,6 +895,7 @@ object SelfCheck {
when {
resp.isSuccessful -> Reach.Ok
resp.code == 401 || resp.code == 403 -> Reach.Bad(Kind.KEY, "HTTP ${resp.code}")
+ resp.code == 402 -> Reach.Bad(Kind.BALANCE, "HTTP 402")
resp.code == 429 -> Reach.Throttled
// 400 is what a bad model name comes back as, and the body
// is where the gateway says which part of it it disliked.
diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xml
index dbca99a..5e3441b 100644
--- a/app/src/main/res/values-en/strings.xml
+++ b/app/src/main/res/values-en/strings.xml
@@ -74,7 +74,7 @@
<string name="settings_brain_codeflying">CodeFlying</string>
<string name="settings_show_hub">Show cloud option</string>
<string name="settings_show_hub_note">Adds a Cloud backend tab for connecting to a hub you host yourself.</string>
- <string name="settings_brain_codeflying_note">CodeFlying is connected: models and voice are set up and ready to go. You can also tap into a long list of third-party services — AI image, video and music generation, location, weather, PDF conversion, search, news, stocks and more. CodeFlying wraps and bills all of them in one place, so you never have to sign up with each provider or juggle their keys and invoices.\n\nNote: using CodeFlying backend services may incur charges.</string>
+ <string name="settings_brain_codeflying_note">CodeFlying (CodeFlying.app) is connected: models and voice are set up and ready to go. You can also tap into a long list of third-party services — AI image, video and music generation, location, weather, PDF conversion, search, news, stocks and more. CodeFlying wraps and bills all of them in one place, so you never have to sign up with each provider or juggle their keys and invoices.\n\nAccount and top-ups: open CodeFlying.app in a computer browser and sign in. If you see a balance error (HTTP 402), top up there.\n\nNote: using CodeFlying backend services may incur charges.</string>
<string name="settings_brain_codeflying_unconfigured">Want Andee to draw, make videos, write music, check the weather, search the news, convert PDFs, or watch stocks for you? Connect CodeFlying: models and voice come set up, and a long list of third-party services is one hookup away, with no separate sign-ups, keys or invoices.\n\nTo get started, open CodeFlying.app in a computer browser and generate your own Andee install package in one click, then install it over this app. New users get free credit, so you can try it first.\n\nNote: using CodeFlying backend services may incur charges.</string>
<string name="settings_hub_url">Hub URL</string>
<!-- The hub URL box is empty on a fresh install — the hint is what the
@@ -343,6 +343,15 @@
<!-- LocalBrain — display half only; see the comment on the Chinese side. -->
<string name="brain_no_key">The on-device brain has no API key yet — long-press the ball, open settings and put a DeepSeek key in the Brain section.</string>
<string name="brain_error">Something broke on my side: %1$s</string>
+ <string name="brain_error_402">The backend answered 402, which most likely means the account is out of balance. Open Settings → Backend to see which provider you are using, then top up on that provider\'s website and try again.</string>
+ <string name="brain_error_402_codeflying">The backend answered 402, which most likely means your CodeFlying balance has run out. Open CodeFlying.app in a computer browser, sign in, top up, and try again.</string>
+ <string name="brain_error_auth">The backend refused the request (HTTP %1$d): the key is wrong, expired or disabled. Check the key in Settings → Backend, and generate a new one on the provider\'s website if needed.</string>
+ <string name="brain_error_400">The backend rejected the request: %1$s\nThe usual cause is a misspelled model name. Check it in Settings → Backend.</string>
+ <string name="brain_error_404">The backend could not find that address (HTTP 404). Check the endpoint URL and the model name in Settings → Backend.</string>
+ <string name="brain_error_429">The backend is busy or being asked too often (HTTP 429). Say it again in a moment. If it keeps happening, check on the provider\'s website whether your quota has run out.</string>
+ <string name="brain_error_5xx">The backend itself is having trouble (HTTP %1$d). Your settings are not the problem. Try again in a few minutes, or check the provider\'s website for maintenance notices.</string>
+ <string name="brain_error_timeout">The backend took too long to answer. Check that the network is working and try again in a moment.</string>
+ <string name="brain_error_network">I could not reach the backend. Check this device\'s network; if that is fine, check the endpoint URL in Settings → Backend.</string>
<string name="brain_no_result">I got nothing back that time — say it again.</string>
<string name="brain_repeat_stop">I kept doing the same thing over and over (%1$s), so I stopped — that way is blocked. Try saying it differently, or take over.</string>
<string name="brain_max_steps">I have spent %1$d steps on this without finishing, so I stopped to ask: shall I try another way, or will you take over?</string>
@@ -402,6 +411,7 @@
<string name="check_detail_model_throttled">Reachable but rate-limited (HTTP 429). The configuration is right; it recovers on its own.</string>
<string name="check_detail_model_url">Could not connect: %1$s</string>
<string name="check_detail_model_key">Key rejected: %1$s</string>
+ <string name="check_detail_model_balance">The server answered %1$s: most likely the account is out of balance. Top up on the current provider\'s website and try again.</string>
<string name="check_detail_model_name">No model called “%1$s” on this server. It offers: %2$s</string>
<string name="check_detail_model_other">%1$s</string>
<string name="check_detail_notif_on">Granted, and the listener is bound.</string>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index a47399a..06f1de4 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -51,7 +51,7 @@
<string name="settings_brain_codeflying">CodeFlying</string>
<string name="settings_show_hub">显示云端选项</string>
<string name="settings_show_hub_note">把「云端」加到后端选项里,用来连你自己搭的 Hub。</string>
- <string name="settings_brain_codeflying_note">已接入 CodeFlying:默认模型和语音都配好了,开箱就能用。还能直接调用海量第三方能力 —— AI 画图/做视频/写音乐、定位、天气、PDF 转换、搜索、新闻、股票……全部由 CodeFlying 统一封装、统一计费,不用自己逐个去申请密钥、对账单。\n\n提示:使用 CodeFlying 的后端服务可能会产生费用。</string>
+ <string name="settings_brain_codeflying_note">已接入 CodeFlying(CodeFlying.net):默认模型和语音都配好了,开箱就能用。还能直接调用海量第三方能力 —— AI 画图/做视频/写音乐、定位、天气、PDF 转换、搜索、新闻、股票……全部由 CodeFlying 统一封装、统一计费,不用自己逐个去申请密钥、对账单。\n\n账户管理和充值:用电脑浏览器打开 CodeFlying.net 登录。如果提示余额不足(HTTP 402),去那里充值即可。\n\n提示:使用 CodeFlying 的后端服务可能会产生费用。</string>
<string name="settings_brain_codeflying_unconfigured">想让 Andee 直接会画图、做视频、写音乐、查天气、搜新闻、转 PDF、看股票?接上 CodeFlying 就行:默认模型和语音都帮你配好,海量第三方能力一次接入,不用自己逐个申请密钥、对账单。\n\n怎么开始:用电脑浏览器打开 CodeFlying.net,一键生成专属于你的 Andee 安装包,装上去替换当前应用就好。新用户可免费获得额度,先试试再决定。\n\n提示:使用 CodeFlying 的后端服务可能会产生费用。</string>
<string name="settings_hub_url">Hub 地址</string>
<!-- The hub URL box is empty on a fresh install — the hint is what the
@@ -409,6 +409,15 @@
-->
<string name="brain_no_key">本地大脑还没配 API key —— 长按小球打开设置,把 DeepSeek 的 key 填进「大脑」那一栏。</string>
<string name="brain_error">我这边出错了:%1$s</string>
+ <string name="brain_error_402">后端服务返回了 402,多半是账户余额不足了。到「设置」→「后端」看看当前用的是哪家服务商,打开它的官网充值后再试。</string>
+ <string name="brain_error_402_codeflying">后端服务返回了 402,多半是 CodeFlying 账户余额不足了。用电脑浏览器打开 CodeFlying.net 登录后充值,再试一次。</string>
+ <string name="brain_error_auth">后端拒绝了请求(HTTP %1$d):Key 填错、过期或被停用了。到「设置」→「后端」检查 Key,必要时去服务商官网重新生成一个。</string>
+ <string name="brain_error_400">后端说请求有问题:%1$s\n最常见的原因是模型名写错了,到「设置」→「后端」核对一下模型名。</string>
+ <string name="brain_error_404">后端找不到这个地址(HTTP 404)。到「设置」→「后端」检查接口地址和模型名有没有填错。</string>
+ <string name="brain_error_429">后端现在太忙或者请求太频繁了(HTTP 429),过一会儿再说一遍就行。如果一直这样,去服务商官网看看额度有没有用完。</string>
+ <string name="brain_error_5xx">后端服务自己出了问题(HTTP %1$d),不是你这边的设置有误。过几分钟再试;一直不行就去服务商官网看看是否在维护。</string>
+ <string name="brain_error_timeout">后端半天没有回应。先看看网络通不通,稍后再试一次。</string>
+ <string name="brain_error_network">连不上后端服务。检查一下这台设备的网络;如果网络没问题,到「设置」→「后端」核对接口地址。</string>
<string name="brain_no_result">我这轮没拿到结果,你再说一遍试试。</string>
<string name="brain_repeat_stop">我一直在重复同一个动作(%1$s),停下来了 —— 这条路走不通,换个说法或者你来操作一下。</string>
<string name="brain_max_steps">这件事我做了 %1$d 步还没办完,先停下来问你一句:要不要换个路子,或者你来接手?</string>
@@ -471,6 +480,7 @@
<string name="check_detail_model_throttled">连上了,但被限流(HTTP 429)。配置是对的,过一会儿会自己恢复。</string>
<string name="check_detail_model_url">连不上:%1$s</string>
<string name="check_detail_model_key">Key 被拒:%1$s</string>
+ <string name="check_detail_model_balance">服务端返回 %1$s:多半是账户余额不足。去当前服务商的官网充值后再试。</string>
<string name="check_detail_model_name">模型名「%1$s」不在服务端列表里。这台服务上有的:%2$s</string>
<string name="check_detail_model_other">%1$s</string>
<string name="check_detail_notif_on">已授权,监听器也连着。</string>