修:输出过长的回答时拉不下来
用户报的。定位到 ChatScreen 里这段:
LaunchedEffect(messages.size, last.content.length) {
listState.animateScrollToItem(messages.size - 1)
}
两个毛病叠在一起:
1. animateScrollToItem(index) 是把这一项的**顶部**对齐到视口顶部。
长回答比一屏高得多,所以它压根没"滚到底",只是回到那条回答的开头。
2. 流式输出时 content.length 每一帧都在变 → 这个 effect 每一帧重跑 →
用户刚往下滑一点就被拽回开头,等于根本滑不动。
所以现象是"短回答没事、长回答尤其还在输出的时候拉不下来"。
改法:
- 只有用户本来就贴着底部时才跟着滚(atBottom 用 derivedStateOf 算:
最后一项可见 且 它的底部已进入视口,留 24px 容差)。用户自己往上翻就完全不打扰。
- 跟的时候用 scrollToItem(index, Int.MAX_VALUE) 滚到**内容末尾**,不是项顶部;
也不用 animate —— 流式时每帧都要跟,动画互相打架看起来就是抖。
- 新增「回到底部」浮动按钮:长回答往上翻之后有个一键回去的路。
到底后自动消失。
顺带修一个编译坑:按钮一开始写在 Box 里,Kotlin 把 AnimatedVisibility 解析成
ColumnScope 那个重载(外层还有个 Column),报"不能在这个上下文调用"。
抽成没有 receiver 的独立 composable 就只解析到顶层那个了。
验收:
- 新增 scripts/repro-long-scroll.mjs:注入一条 3400 字的长回答,
截图 → 连滑三次 → 再截图,比较两张图有没有变。
- ★ 第一版复现脚本是假通过:App 启动时 messages 本来就是空的
(openConversation 只在用户点会话时才调),不点会话看到的是欢迎页,
那时候"滑不动"当然成立。补上"打开会话"这一步才测到真东西 ——
这个教训写进脚本注释了。
- 设备实测:能滚到真正的结尾、按钮正常出现和消失。
This commit is contained in:
@@ -13,6 +13,7 @@ import androidx.compose.animation.slideInVertically
|
|||||||
import androidx.compose.animation.slideOutVertically
|
import androidx.compose.animation.slideOutVertically
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.border
|
import androidx.compose.foundation.border
|
||||||
|
import androidx.compose.foundation.BorderStroke
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.horizontalScroll
|
import androidx.compose.foundation.horizontalScroll
|
||||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||||
@@ -30,6 +31,7 @@ import androidx.compose.foundation.text.selection.SelectionContainer
|
|||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material.icons.filled.*
|
import androidx.compose.material.icons.filled.*
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.Surface
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.*
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
@@ -50,6 +52,7 @@ import androidx.compose.ui.unit.dp
|
|||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import com.doubao.lynxclone.data.ChatMessage
|
import com.doubao.lynxclone.data.ChatMessage
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import com.doubao.lynxclone.speech.SpeechController
|
import com.doubao.lynxclone.speech.SpeechController
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
|
||||||
@@ -60,6 +63,42 @@ import kotlinx.coroutines.delay
|
|||||||
* 大头像 + 灰色问候气泡、白底描边快捷胶囊、输入框是大圆角描边胶囊、发送键是蓝色圆钮。
|
* 大头像 + 灰色问候气泡、白底描边快捷胶囊、输入框是大圆角描边胶囊、发送键是蓝色圆钮。
|
||||||
* 动效:气泡淡入上移、思考面板从底部升起、typing 三点跳动、按钮按下回弹。
|
* 动效:气泡淡入上移、思考面板从底部升起、typing 三点跳动、按钮按下回弹。
|
||||||
*/
|
*/
|
||||||
|
@Composable
|
||||||
|
private fun ScrollToBottomChip(
|
||||||
|
visible: Boolean,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
onClick: () -> Unit,
|
||||||
|
) {
|
||||||
|
AnimatedVisibility(
|
||||||
|
visible = visible,
|
||||||
|
enter = fadeIn() + slideInVertically { it / 2 },
|
||||||
|
exit = fadeOut() + slideOutVertically { it / 2 },
|
||||||
|
modifier = modifier,
|
||||||
|
) {
|
||||||
|
Surface(
|
||||||
|
shape = CircleShape,
|
||||||
|
color = doubaoCard(),
|
||||||
|
border = BorderStroke(1.dp, doubaoBorder()),
|
||||||
|
shadowElevation = 3.dp,
|
||||||
|
onClick = onClick,
|
||||||
|
) {
|
||||||
|
Row(
|
||||||
|
Modifier.padding(horizontal = 14.dp, vertical = 7.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
Icons.Filled.KeyboardArrowDown,
|
||||||
|
"回到底部",
|
||||||
|
modifier = Modifier.size(18.dp),
|
||||||
|
tint = Doubao.Blue,
|
||||||
|
)
|
||||||
|
Spacer(Modifier.width(3.dp))
|
||||||
|
Text("回到底部", color = Doubao.Blue, fontSize = 13.sp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ChatScreen(vm: AppViewModel) {
|
fun ChatScreen(vm: AppViewModel) {
|
||||||
val agent = vm.activeAgent
|
val agent = vm.activeAgent
|
||||||
@@ -124,17 +163,37 @@ fun ChatScreen(vm: AppViewModel) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 新消息 / 流式增量都滚到底
|
// 新消息 / 流式增量都滚到底,但**只在用户本来就贴着底部时**才跟。
|
||||||
LaunchedEffect(vm.messages.size, vm.messages.lastOrNull()?.content?.length) {
|
//
|
||||||
if (vm.messages.isNotEmpty()) listState.animateScrollToItem(vm.messages.size - 1)
|
// ★ 这里曾经有两个叠在一起的毛病,表现是"输出过长的回答时拉不下来":
|
||||||
|
// 1. animateScrollToItem(index) 是把这一项的**顶部**对齐到视口顶部。
|
||||||
|
// 长回答比一屏高得多,所以它压根没"滚到底",只是回到那条回答的开头。
|
||||||
|
// 2. 流式输出时 content.length 每一帧都在变 → 这个 effect 每一帧重跑 →
|
||||||
|
// 用户刚往下滑一点就被拽回开头,等于根本滑不动。
|
||||||
|
// 现在:贴着底部才跟;跟的时候用 scrollToItem + 大 offset 滚到**内容末尾**;
|
||||||
|
// 用户自己往上翻了就完全不打扰(要回底部有下面的按钮)。
|
||||||
|
val atBottom by remember {
|
||||||
|
derivedStateOf {
|
||||||
|
val info = listState.layoutInfo
|
||||||
|
val last = info.visibleItemsInfo.lastOrNull() ?: return@derivedStateOf true
|
||||||
|
last.index >= vm.messages.size - 1 &&
|
||||||
|
last.offset + last.size <= info.viewportEndOffset + 24
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val contentLen = vm.messages.lastOrNull()?.content?.length ?: 0
|
||||||
|
LaunchedEffect(vm.messages.size, contentLen) {
|
||||||
|
if (vm.messages.isEmpty() || !atBottom) return@LaunchedEffect
|
||||||
|
// 不用 animate:流式时每帧都要跟,动画会互相打架,看起来就是抖
|
||||||
|
listState.scrollToItem(vm.messages.size - 1, Int.MAX_VALUE)
|
||||||
}
|
}
|
||||||
|
|
||||||
Column(Modifier.fillMaxSize().background(doubaoBg())) {
|
Column(Modifier.fillMaxSize().background(doubaoBg())) {
|
||||||
ChatHeader(vm, agent?.name ?: "豆包", speech)
|
ChatHeader(vm, agent?.name ?: "豆包", speech)
|
||||||
|
|
||||||
|
Box(Modifier.weight(1f).fillMaxWidth()) {
|
||||||
LazyColumn(
|
LazyColumn(
|
||||||
state = listState,
|
state = listState,
|
||||||
modifier = Modifier.weight(1f).fillMaxWidth(),
|
modifier = Modifier.fillMaxSize(),
|
||||||
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 10.dp),
|
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 10.dp),
|
||||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||||
) {
|
) {
|
||||||
@@ -154,6 +213,21 @@ fun ChatScreen(vm: AppViewModel) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 「回到底部」:长回答往上翻之后,得有个一键回去的路。
|
||||||
|
// 只在用户确实翻上去了、且内容装不下一屏时才出现,平时不占地方。
|
||||||
|
//
|
||||||
|
// ★ 这里必须抽成独立 composable,不能直接在这儿写 AnimatedVisibility:
|
||||||
|
// 我们现在在 Box 里,但外层还有个 Column,Kotlin 会解析到
|
||||||
|
// ColumnScope.AnimatedVisibility 那个重载,报"不能在这个上下文调用"。
|
||||||
|
// 放到一个没有 receiver 的函数里,就只会解析到顶层那个。
|
||||||
|
ScrollToBottomChip(
|
||||||
|
visible = !atBottom && vm.messages.isNotEmpty(),
|
||||||
|
modifier = Modifier.align(Alignment.BottomCenter).padding(bottom = 12.dp),
|
||||||
|
) {
|
||||||
|
scope.launch { listState.animateScrollToItem(vm.messages.size - 1, Int.MAX_VALUE) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (vm.messages.isEmpty()) {
|
if (vm.messages.isEmpty()) {
|
||||||
Row(
|
Row(
|
||||||
Modifier.fillMaxWidth().horizontalScroll(rememberScrollState())
|
Modifier.fillMaxWidth().horizontalScroll(rememberScrollState())
|
||||||
|
|||||||
135
scripts/repro-long-scroll.mjs
Normal file
135
scripts/repro-long-scroll.mjs
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
/**
|
||||||
|
* 复现并验证「长回答拉不下来」。
|
||||||
|
*
|
||||||
|
* 做法:往会话里塞一条很长的助手消息,然后
|
||||||
|
* 截图 → 往上滑(看后面的内容)→ 再截图
|
||||||
|
* 比较两张图。内容真的有滚动,两张图就该不一样。
|
||||||
|
*
|
||||||
|
* 跑法: node scripts/repro-long-scroll.mjs emulator-5556
|
||||||
|
*/
|
||||||
|
import { spawnSync } from 'node:child_process';
|
||||||
|
import fs from 'node:fs';
|
||||||
|
import os from 'node:os';
|
||||||
|
import path from 'node:path';
|
||||||
|
|
||||||
|
const SERIAL = process.argv[2] || 'emulator-5556';
|
||||||
|
const ADB = process.env.ADB || 'C:\\Android\\Sdk\\platform-tools\\adb.exe';
|
||||||
|
const BASE = 'http://127.0.0.1:5199';
|
||||||
|
|
||||||
|
const sh = (cmd, timeout = 120000) =>
|
||||||
|
spawnSync(ADB, ['-s', SERIAL, 'shell', cmd], { encoding: 'utf8', timeout, maxBuffer: 16 * 1024 * 1024 });
|
||||||
|
|
||||||
|
/** 在设备上跑一段 shell:把 JSON 用 base64 送进去,绕开引号地狱 */
|
||||||
|
const curlJson = (method, url, obj) => {
|
||||||
|
const b64 = Buffer.from(JSON.stringify(obj), 'utf8').toString('base64');
|
||||||
|
const dataFlag = method === 'GET' ? '' : `-X ${method} -H 'Content-Type: application/json' -d @/data/local/tmp/body.json`;
|
||||||
|
return sh(
|
||||||
|
`echo '${b64}' | base64 -d > /data/local/tmp/body.json; curl -s --max-time 30 ${dataFlag} ${url}`,
|
||||||
|
).stdout || '';
|
||||||
|
};
|
||||||
|
|
||||||
|
/** 造一条足够长的回答:段落 + 列表 + 代码块 + 表格,顺便也压一压 Markdown 渲染 */
|
||||||
|
function longAnswer(paragraphs = 26) {
|
||||||
|
const out = ['# 长回答测试', ''];
|
||||||
|
for (let i = 1; i <= paragraphs; i += 1) {
|
||||||
|
out.push(`## 第 ${i} 节`);
|
||||||
|
out.push('');
|
||||||
|
out.push(`这是第 ${i} 段的正文,用来把内容撑长。**加粗**、\`行内代码\`、[链接](https://example.com) 都放一点,`
|
||||||
|
+ '好确认 Markdown 渲染和滚动是不是都能正常工作。');
|
||||||
|
out.push('');
|
||||||
|
out.push(`- 要点 ${i}.1`);
|
||||||
|
out.push(`- 要点 ${i}.2`);
|
||||||
|
out.push('');
|
||||||
|
if (i % 6 === 0) {
|
||||||
|
out.push('```python');
|
||||||
|
out.push(`def section_${i}():`);
|
||||||
|
out.push(` return ${i}`);
|
||||||
|
out.push('```');
|
||||||
|
out.push('');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out.push('—— 以上是结尾,能看到这一行说明滚动到底了。');
|
||||||
|
return out.join('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
const screenshot = (tag) => {
|
||||||
|
const remote = `/sdcard/scroll-${tag}.png`;
|
||||||
|
sh(`screencap -p ${remote}`);
|
||||||
|
const local = path.join(os.tmpdir(), `scroll-${tag}.png`);
|
||||||
|
spawnSync(ADB, ['-s', SERIAL, 'pull', remote, local], { encoding: 'utf8' });
|
||||||
|
return local;
|
||||||
|
};
|
||||||
|
|
||||||
|
const hashOf = (file) => {
|
||||||
|
const buf = fs.readFileSync(file);
|
||||||
|
// 简单校验和就够了,不需要密码学强度
|
||||||
|
let h = 0;
|
||||||
|
for (let i = 0; i < buf.length; i += 97) h = (h * 31 + buf[i]) >>> 0;
|
||||||
|
return h.toString(16);
|
||||||
|
};
|
||||||
|
|
||||||
|
// ① 找到当前会话,塞一条长消息
|
||||||
|
const stateRaw = sh(`curl -s --max-time 20 ${BASE}/api/state`).stdout || '';
|
||||||
|
let state;
|
||||||
|
try {
|
||||||
|
state = JSON.parse(stateRaw);
|
||||||
|
} catch {
|
||||||
|
console.log(' 拿不到 /api/state:', stateRaw.slice(0, 160));
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
const conv = (state.conversations || [])[0];
|
||||||
|
if (!conv) {
|
||||||
|
console.log(' 没有会话');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
console.log(` 会话: ${conv.id}(${conv.title || '无标题'})`);
|
||||||
|
|
||||||
|
const full = curlJson('GET', `${BASE}/api/conversations/${conv.id}`, null);
|
||||||
|
let messages = [];
|
||||||
|
try {
|
||||||
|
messages = JSON.parse(full).conversation?.messages || [];
|
||||||
|
} catch { /* 空会话 */ }
|
||||||
|
console.log(` 原有消息 ${messages.length} 条`);
|
||||||
|
|
||||||
|
const answer = longAnswer();
|
||||||
|
const injected = [
|
||||||
|
{ role: 'user', content: '给我一段很长的回答' },
|
||||||
|
{ role: 'assistant', content: answer },
|
||||||
|
];
|
||||||
|
// PATCH 是浅合并,直接覆盖 messages
|
||||||
|
const res = curlJson('PATCH', `${BASE}/api/conversations/${conv.id}`, { messages: injected });
|
||||||
|
console.log(` 注入长回答: ${answer.length} 字 → ${res.replace(/\s+/g, ' ').slice(0, 80)}`);
|
||||||
|
|
||||||
|
// ② 让 App 重新读一次(重启比找刷新入口可靠)
|
||||||
|
sh('am force-stop com.doubao.lynxclone');
|
||||||
|
spawnSync(ADB, ['-s', SERIAL, 'shell', 'am', 'start', '-n', 'com.doubao.lynxclone/.MainActivity'], { encoding: 'utf8' });
|
||||||
|
console.log(' 等待 App 起来…');
|
||||||
|
await new Promise((r) => setTimeout(r, 40000));
|
||||||
|
|
||||||
|
// ★ 必须手动打开一次会话:App 启动时 messages 是空的(openConversation 只在
|
||||||
|
// 用户点会话时才调),不点的话看到的是欢迎页 —— 那时候"滑不动"是当然的,
|
||||||
|
// 第一版复现脚本就栽在这儿,测了个寂寞。
|
||||||
|
// 抽屉图标 → 会话列表第一项。
|
||||||
|
sh('input tap 45 105');
|
||||||
|
await new Promise((r) => setTimeout(r, 2000));
|
||||||
|
sh('input tap 300 1000');
|
||||||
|
await new Promise((r) => setTimeout(r, 3000));
|
||||||
|
|
||||||
|
// ③ 截图 → 往上滑(想看到更下面的内容)→ 再截图
|
||||||
|
const before = screenshot('before');
|
||||||
|
// 连续滑三次,模拟用户使劲往上拉
|
||||||
|
for (let i = 0; i < 3; i += 1) {
|
||||||
|
sh('input swipe 360 1000 360 300 300');
|
||||||
|
await new Promise((r) => setTimeout(r, 500));
|
||||||
|
}
|
||||||
|
const after = screenshot('after');
|
||||||
|
|
||||||
|
const h1 = hashOf(before);
|
||||||
|
const h2 = hashOf(after);
|
||||||
|
console.log(`\n 滑动前 ${h1} 滑动后 ${h2}`);
|
||||||
|
if (h1 === h2) {
|
||||||
|
console.log(' [FAIL] 画面完全没变 —— 长回答确实拉不动');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
console.log(' [OK] 画面变了,能滚动');
|
||||||
|
console.log(` 截图: ${before} ${after}`);
|
||||||
Reference in New Issue
Block a user