Skip to content

feat(composer): compact input row and show the context budget - #8

Merged
CjiW merged 8 commits into
masterfrom
ui/composer-context-budget
Oct 21, 2026
Merged

CjiW merged 8 commits into
masterfrom
ui/composer-context-budget

Conversation

@CjiW

@CjiW CjiW commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

目标

输入区占的高度全是从对话正文里抢来的;腾出来的空间放在输入框上方,用一行居中的小字显示工作目录与上下文占用(如 📁 ~/xbot-android · 85K/200K)。

输入区压缩:约 72dp → 约 48dp

元素 之前 现在
文本框 OutlinedTextField(装饰盒内固定 56dp 最小高度) BasicTextField + 自绘圆角背景/边框/占位符,单行 ≈36dp
图标按钮 默认 48dp 触控尺寸 / 24dp 图标 36dp / 20dp 图标
间距/内边距 h 10dp v 8dp、spacedBy 4dp h 8dp v 6dp、spacedBy 2dp

M3 的 OutlinedTextField 把 TextFieldDefaults.MinHeight(56dp)写在装饰盒内部,外面套
Modifier.height() / defaultMinSize() 只会裁掉内容 —— 想真正压低必须换 BasicTextField。

上下文预算:输入框上方居中一行文字

居中显示 已用输入/上下文上限(formatTokens 压成 K/M,上限未知时右半是 —),点击弹出明细(已用输入 / 上下文上限 / 当前占用 / 模型)。

  • 配色:<60% 常规次要文字色,60%~85% 琥珀,>=85% error。
  • 数据源:POST /api/session/status(客户端此前从未调用)→ token_usage.max_context_tokens / usage_percent。
  • 三种口径严格区分,绝不编造数字:
    1. available == false(新会话,服务端还没有真实 API token 数)→ 这一行整个不存在;
    2. 有服务端 usage_percent → 以它为准;
    3. 回合进行中的 progress 事件只带 prompt_tokens、没有上限 → 用上次 status 的上限估算,弹窗标注「回合进行中估算」;上限未知时保持不动。
  • 刷新时机:reload() 成功后、SSE 中 busy 由 true 转 false 时。

工作目录

服务端本来就给了 work_dir(会话列表)和 cwd(session status / progress),但客户端从没显示过 —— 同一台服务器上的多个会话看起来一模一样。

  • 会话项:路径显示在时间行左侧,weight(1f, fill = false) + 省略号,卡片高度不变。
  • 聊天页:与上下文占用同一行、整体居中。
  • 两处共用 ui/components/PathText.kt:Icons.Outlined.Folder + shortenPath(),
    ^/(home|Users|root)/<user>/ → ~/,段数过多时只留最后两段并加 …/ —— 路径尾巴才是有信息量的部分,Compose 默认的右截断会把目录名切掉。
  • ChatUiState.cwd 由 session status(空值保留旧值)与 progress 事件(回合中可变)共同维护。

顺带修复:Rewind 一直失败

真机上报 Unexpected symbol 't' in numeric literal at path: $.data.files_rewound。
服务端 protocol.HistoryRewindResult 里 history_rewound / files_rewound 是 bool,客户端模型声明成了 Int
—— 回退在服务端已经成功,客户端却在反序列化阶段抛异常,用户既看不到成功也看不到文件结果。

  • 类型改为 Boolean;
  • 结果文案拆成两件事:history_rewound=false 是「没生效」,files_rewound=false + checkpoint_error 非空是「历史回退了但文件快照没恢复」,不再用一句「已回退」盖掉;
  • 新增 RewindResponseTest(真机原始 payload + 失败场景);顺手用脚本比对了 protocol/ 里 31 个 bool json tag,没有其他同类错配。

第二轮(真机反馈):回退后消息不消失。 服务端已删除的 turn 之所以还留在屏幕上,是因为 reload() 在会话已加载时
无条件走合并分支,而 mergeLatestHistory 只增不减;open() 会重置 state 才「看起来好了」。

  • reload(replaceTurns) 增加替换语义;Rewind 成功回调与 SSE session.action == "history_rewound" 都走替换,
    普通重载(重连 / resync)继续合并,保住上滑加载的旧页;
  • 标志经 ChatUiState.reloadReplacesHistory 传递 —— 不能复用 needsReload,resync_required 也用它且必须合并;
  • 新增 docs/agent/chat.md 说明两条历史路径;单测同时钉住「替换会丢」与「合并会留」两种行为。

验证

  • ./gradlew :app:testDebugUnitTest ✅ — ChatReducerTest 23 个用例 + RewindResponseTest 2 个用例 + 3 个「历史装载」用例(上下文预算 6、cwd 3、rewind 2、reload 语义 3)
  • ./gradlew :app:lintDebug ✅
  • ./gradlew :app:assembleRelease ✅(签名 CN=xbot)
  • 真机试用包:https://minio.cjiw.site/uploads/share/android/xbot-android-22.apk
    (versionCode 22 / 0.3.4,sha256 6e613d46…,公网回拉校验一致)

版本号

本 PR 顺带把版本推到 0.3.4(versionCode 20)。同一版本的 trial 包 17/18/19 已在测试机上迭代过,
Android 不允许降级,所以每次都要往上加。如果不想让功能 PR 带版本号,可以摘掉 14de4ca 这个 commit,发版时再统一改。

文档

  • docs/agent/ui-composer.md:OutlinedTextField 的 56dp 陷阱、上下文预算的数据来源与三种口径、cwd 的来源与显示位置。

CjiW added 8 commits October 21, 2026 08:26
The input row was ~72dp tall on its own: Material3's OutlinedTextField pins
its decoration box to TextFieldDefaults.MinHeight (56dp), and the icons kept
their 48dp touch size. Every pixel there is a pixel the transcript loses.

Render the field with BasicTextField plus our own rounded background so the
row is ~36dp, and shrink the icon buttons to 36dp with 20dp glyphs.

The freed space pays for a context-budget indicator: a 14dp arc plus the
percentage, sitting left of the send button and turning amber at 60% and red
at 85%, with a tap-through dialog showing prompt tokens, the context cap and
the model.

The data comes from POST /api/session/status, which the client never called:
TokenUsage now carries available/max_context_tokens/usage_percent/model, and
ChatReducer.withContextBudget keeps the server's percentage authoritative
while hiding the indicator entirely until the server has real token counts
(available=false on a fresh session, so no misleading 0%). Progress events
only carry prompt_tokens, so withLiveTokenUsage estimates against the cap
from the last status call and leaves the value alone when the cap is unknown.
Two traps worth not rediscovering: Material3's OutlinedTextField pins a
56dp minimum inside its decoration box so height has to come from
BasicTextField, and the only real context-budget source is
/api/session/status (progress events carry prompt tokens but no cap).
versionCode 17 is above the 0.3.3 (16) build that is already installed on the
test device, so the composer work can be sideloaded and picked up by the
in-app updater. The 0.3.4 entry also covers the indicator's data contract:
no percentage until the server has real token counts.
A ring plus a percentage inside the input row cost ~45dp of horizontal
space and still made the reader do the division. Print the ratio instead,
right-aligned on its own line above the field: 85K/200K. It only exists
when the server has real token counts, so an empty session keeps the
composer at its compact height.

versionCode 18: the 17 build of 0.3.4 is already on the test device, and
Android refuses a downgrade.
Right-aligning it put the number in the corner furthest from the text
being typed; centred it reads as a caption for the whole input row.

versionCode 19: 18 is already on the test device.
The server already sends work_dir on every chat and cwd in session status,
but neither reached the UI, so two chats on the same server looked
identical until you opened them.

Session rows put the path on the time line (weight 1f + ellipsis, so the
card never grows); the chat screen prints it next to the context budget
above the composer. Both use PathText, which keeps the tail of the path
installed of the head: right-truncation would cut the leaf directory that
actually identifies the workspace.

ChatUiState.cwd is fed from session status (blank keeps the previous
value) and from progress events, which can carry a new cwd mid-turn.

versionCode 20.
The server's protocol.HistoryRewindResult declares both flags as bool, but
RewindResponse typed them as Int. kotlinx.serialization therefore threw
"Unexpected symbol 't' in numeric literal at path: $.data.files_rewound"
on a rewind that had already succeeded server-side -- the user saw a JSON
parse error and no rewind dialog feedback at all.

Type them as Boolean and report the two outcomes separately: history not
truncated is a real failure, while files_rewound=false with a non-blank
checkpoint_error means the history moved but the file snapshot did not.
The full "已回退" message used to hide that difference.

Adds RewindResponseTest with the exact payload captured on device, plus a
failed-rewind case. A sweep of every bool json tag in protocol/ against the
client models found no other bool-as-Int mismatches.

versionCode 21.
reload() merged the newest page into the existing turns whenever the chat
already had history, and mergeLatestHistory only ever adds: turns the server
no longer returns survive. After a rewind the truncated messages therefore
stayed on screen until the chat was reopened, because open() resets the state
and takes the replacing path.

Add reload(replaceTurns): the rewind success handler and the server's
history_rewound session event now ask for a replacing reload, while ordinary
reloads (reconnect, resync) keep merging so pages loaded by scrolling up are
preserved. The flag travels through ChatUiState.reloadReplacesHistory because
needsReload alone is also used by resync_required, which must merge.

Tests pin both behaviours: withHistory drops the removed turn,
mergeLatestHistory keeps it (the old bug), and the session event sets both
flags.

versionCode 22, plus docs/agent/chat.md on the two history paths.
@CjiW
CjiW merged commit f463f07 into master Oct 21, 2026
1 check passed
@CjiW
CjiW deleted the ui/composer-context-budget branch October 21, 2026 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant