Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 保持新发送消息位于已显示的历史回复之后 (#4707)

Dash3c6bedf7327b
diff --git a/apps/mobile/app/sessions/[sessionId].tsx b/apps/mobile/app/sessions/[sessionId].tsx
index 98778a6438..5efd4462d8 100644
--- a/apps/mobile/app/sessions/[sessionId].tsx
+++ b/apps/mobile/app/sessions/[sessionId].tsx
@@ -1545,6 +1545,8 @@ export default function SessionScreen() {
});
const projection = remoteSessionStore.getInputProjection(sessionId);
const source = remoteSessionStore.getMessages(sessionId);
+ // Capture at send time, not when React later runs the state updater.
+ const observed = latestMessagesRef.current;
// Match Desktop's idle-send placement. Busy follow-ups retain the existing
// pending tail until authoritative history supplies their transcript row.
const busy = projection.pendingQueue.length > 0 || projection.steeringQueueClientIds.length > 0
@@ -1552,7 +1554,7 @@ export default function SessionScreen() {
|| remoteSessionStore.getPendingInteractions(sessionId).length > 0
|| currentTurnHasStreamingAssistant(source);
if (!busy) setOptimisticUserState((current) => current.sessionId !== sessionId ? current : {
- sessionId, items: appendOptimisticUserMessage(current.items, source, queued, sessionId),
+ sessionId, items: appendOptimisticUserMessage(current.items, source, queued, sessionId, observed),
});
setSendingQueueClientIds((current) => {
if (current.has(clientId)) return current;
diff --git a/apps/mobile/src/__tests__/sentMessageHandoff.test.ts b/apps/mobile/src/__tests__/sentMessageHandoff.test.ts
index 62a74e7c4d..2e50a766ee 100644
--- a/apps/mobile/src/__tests__/sentMessageHandoff.test.ts
+++ b/apps/mobile/src/__tests__/sentMessageHandoff.test.ts
@@ -23,6 +23,50 @@ const queueInput = (current: readonly QueuedRemoteMessage[] = [], overrides: Par
});
describe('sent message handoff', () => {
+ it.each([false, true])('keeps a new send after history-only replies (echo=%s)', async (hasEcho) => {
+ const previousUser = { ...echo, id: 'previous-user', clientId: 'previous-user', createdAt: '2026-09-16T23:59:00Z' };
+ const previousReply: RemoteMessage = { ...echo, id: 'previous-reply', clientId: 'previous-reply', role: 'assistant', createdAt: '2026-09-16T23:59:10Z' };
+ const freshReply: RemoteMessage = { ...previousReply, id: 'fresh-reply', clientId: 'fresh-reply', createdAt: '2026-09-16T23:59:20Z' };
+ const nextReply: RemoteMessage = { ...previousReply, id: 'next-reply', clientId: 'next-reply', createdAt: '2026-09-17T00:00:10Z' };
+ let history = [previousUser, previousReply];
+ const view = new HistoryViewController<RemoteMessage>({
+ page: async () => ({ version: 1, items: projectHistoryView(history, false), hasMore: false, nextCursor: null }),
+ details: async () => ({ version: 1, messages: [], hasMore: false, nextCursor: null }),
+ expanded: async () => undefined,
+ });
+ await view.refresh();
+ const handoff = new HistoryViewHandoff<RemoteMessage>(() => true);
+ const snapshot = view.getSnapshot();
+ const displayed = handoff.reconcile(snapshot, []).messages;
+ let slots = appendOptimisticUserMessage([], [], queued, 's', displayed);
+ // A fresh push received before React renders must also precede the send.
+ const freshSlots = appendOptimisticUserMessage([], [freshReply], queued, 's', displayed);
+ expect(projectOptimisticUserMessages([...displayed, freshReply, nextReply], freshSlots)
+ .map((row) => row.clientId)).toEqual(['previous-user', 'previous-reply', 'fresh-reply', 'sent', 'next-reply']);
+ const raw = [...(hasEcho ? [echo] : []), nextReply];
+ const render = () => {
+ const snapshot = view.getSnapshot();
+ slots = reconcileOptimisticUserMessages(slots, raw, new Set(['sent']), confirmedHistoryUserClientIds(snapshot, raw));
+ const state = handoff.reconcile(snapshot, raw);
+ return buildMobileHistoryRenderItems({ view, snapshot,
+ messages: projectOptimisticUserMessages(state.messages, slots), pendingHandoff: state.pending,
+ localUserClientIds: new Set(slots.map((entry) => entry.message.clientId)), streaming: false, sessionId: 's' })
+ .map((item) => item.key);
+ };
+ const expected = [
+ 'message-previous-user', 'message-previous-reply', 'message-sent', 'message-next-reply',
+ ];
+ expect(render()).toEqual(expected);
+ await view.refresh(); // A stale history refresh must retain the same position.
+ expect(render()).toEqual(expected);
+ history = [previousUser, previousReply, echo, nextReply];
+ await view.refresh();
+ expect(render()).toEqual(expected);
+ expect(slots).toEqual([]);
+ expect(appendOptimisticUserMessage([], [], queued, 's', history)).toEqual([]);
+ view.setActive(false);
+ });
+
it.each([false, true])('keeps exactly one user row across a stale history page (initially ready=%s)', async (ready) => {
let history: RemoteMessage[] = [];
const view = new HistoryViewController<RemoteMessage>({
diff --git a/apps/mobile/src/session/optimisticUserMessages.ts b/apps/mobile/src/session/optimisticUserMessages.ts
index 1eaffd87a1..bbdb960605 100644
--- a/apps/mobile/src/session/optimisticUserMessages.ts
+++ b/apps/mobile/src/session/optimisticUserMessages.ts
@@ -24,9 +24,11 @@ export function appendOptimisticUserMessage(
messages: readonly RemoteMessage[],
queued: QueuedRemoteMessage,
sessionId: string,
+ observedMessages: readonly RemoteMessage[] = [],
): readonly OptimisticUserMessage[] {
if (current.some((entry) => entry.message.clientId === queued.clientId)
- || messages.some((message) => message.clientId === queued.clientId)) return current;
+ || messages.some((message) => message.clientId === queued.clientId)
+ || observedMessages.some((message) => message.clientId === queued.clientId)) return current;
const source = queued.chatMessage;
return [...current, {
message: {
@@ -37,6 +39,9 @@ export function appendOptimisticUserMessage(
content: { ...source, text: pendingSendBubbleText(queued) },
},
precedingClientIds: new Set([
+ // History rows may be visible without ever entering the raw push store.
+ // Keep both those observed rows and pushes received since the last render.
+ ...observedMessages.map((message) => message.clientId),
...messages.map((message) => message.clientId),
...current.map((entry) => entry.message.clientId),
]),