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): allow cancelling in-flight outbox messages (#5218)

Tsai Yuand656b0fc1ec3
diff --git a/apps/mobile/app/sessions/[sessionId].tsx b/apps/mobile/app/sessions/[sessionId].tsx
index 5511a2f5ae..9eb9cb1b29 100644
--- a/apps/mobile/app/sessions/[sessionId].tsx
+++ b/apps/mobile/app/sessions/[sessionId].tsx
@@ -5588,7 +5588,13 @@ export default function SessionScreen() {
if (isMobileAuthOwnerCurrent(owner) && outboxSessionAliveRef.current === sessionId) setError(formatRemoteError(err));
});
};
- const outboxDisplayItems = useMemo(() => outboxItems.map(outboxDisplayItem), [outboxItems]);
+ const outboxDisplayItems = useMemo(() => outboxItems.map((item) => {
+ const record = findDurableOutboxRecord(item.clientId);
+ return {
+ ...outboxDisplayItem(item),
+ canCancel: !(record?.creation && !record.prepared && !record.template),
+ };
+ }), [outboxItems, deviceId, sessionId]);
// Legacy creation tasks have no app-owned first record. Never downgrade durable follow-ups
// into the old ephemeral draft recovery; new creation tasks use the branch above.
diff --git a/apps/mobile/src/__tests__/PendingSendBubble.touch.test.tsx b/apps/mobile/src/__tests__/PendingSendBubble.touch.test.tsx
index 00400a6f76..5772b94742 100644
--- a/apps/mobile/src/__tests__/PendingSendBubble.touch.test.tsx
+++ b/apps/mobile/src/__tests__/PendingSendBubble.touch.test.tsx
@@ -39,6 +39,7 @@ let host: HTMLDivElement;
let frameId = 0;
const select = vi.fn();
const openLink = vi.fn();
+const removeOutbox = vi.fn();
const item: MobilePendingSendItem = {
type: 'pending_send', key: 'message-a', clientId: 'a', phase: 'queued', text: 'hello',
queueIndex: 1, sentInlineTokens: [], thumbs: [], fileCount: 0, attachmentCount: 0, uploadedCount: 0,
@@ -60,7 +61,7 @@ afterEach(() => { act(() => root.unmount()); vi.useRealTimers(); vi.unstubAllGlo
function show(overrides: Partial<MobilePendingSendItem> = {}, selectedClientId: string | null = null) {
handlers.clear();
const actions: PendingSendBubbleActions = { selectedClientId, onSelect: select, onRemove: vi.fn(),
- onBeginEdit: vi.fn(), onSteer: vi.fn(), onRetryOutbox: vi.fn(), onRemoveOutbox: vi.fn() };
+ onBeginEdit: vi.fn(), onSteer: vi.fn(), onRetryOutbox: vi.fn(), onRemoveOutbox: removeOutbox };
act(() => root.render(<PendingSendBubble item={{ ...item, ...overrides }} actions={actions}
renderImage={() => null} renderFile={() => null}
renderText={() => <MessageBodyText testID="link" onPress={openLink}>link</MessageBodyText>} />));
@@ -92,6 +93,15 @@ it.each(['move', 'end', 'cancel', 'long', 'multi', 'disabled', 'unmount', 'recyc
flush(); expect(select).not.toHaveBeenCalled();
},
);
+it('lets an in-flight local outbox message open its cancel action', () => {
+ show({ actions: null, phase: 'sending', queueIndex: null });
+ touch('Start'); touch('End'); flush();
+ expect(select).toHaveBeenCalledWith('a');
+
+ show({ actions: null, phase: 'sending', queueIndex: null }, 'a');
+ act(() => host.querySelector<HTMLElement>('[data-testid="pendingSend.outboxRemove.a"]')!.click());
+ expect(removeOutbox).toHaveBeenCalledWith('a');
+});
it('lets a real body link consume the pending tap before the frame commits', () => {
show(); touch('Start'); touch('End');
act(() => host.querySelector<HTMLElement>('[data-testid="link"]')!.click());
diff --git a/apps/mobile/src/__tests__/pendingSendItems.test.ts b/apps/mobile/src/__tests__/pendingSendItems.test.ts
index 869e8c1e4e..ecf4eadb52 100644
--- a/apps/mobile/src/__tests__/pendingSendItems.test.ts
+++ b/apps/mobile/src/__tests__/pendingSendItems.test.ts
@@ -345,6 +345,12 @@ describe('buildPendingSendItems', () => {
expect(items[2].actions).toBeNull();
});
+ it('keeps a first-message creation row non-interactive until creation recovery is available', () => {
+ const [item] = build({ outbox: [outboxItem('first', { canCancel: false })] });
+ expect(item.canCancel).toBe(false);
+ expect(isPendingSendItemSelected(item, 'first')).toBe(false);
+ });
+
it('never exposes queue actions for items that left the queue', () => {
const [settling] = build({ settling: [queued('gone')] });
expect(settling.actions).toBeNull();
diff --git a/apps/mobile/src/session/PendingSendBubble.tsx b/apps/mobile/src/session/PendingSendBubble.tsx
index 14ec253f19..57b475b2bc 100644
--- a/apps/mobile/src/session/PendingSendBubble.tsx
+++ b/apps/mobile/src/session/PendingSendBubble.tsx
@@ -36,6 +36,7 @@ import {
useSentAttachmentThumbsVersion,
} from '@/session/sentAttachmentThumbStore';
import {
+ isPendingSendItemInteractive,
isPendingSendItemSelected,
pendingSendSpins,
type MobilePendingSendItem,
@@ -207,7 +208,12 @@ export function PendingSendBubble({
const spinning = pendingSendSpins(item.phase);
const editing = item.phase === 'editing';
const failed = item.phase === 'failed';
- const interactive = item.actions !== null || failed;
+ // Local outbox rows have not reached the desktop queue yet, but users must
+ // still be able to cancel them while upload/enqueue is in flight. Settling
+ // rows deliberately remain non-interactive: they have already left the
+ // queue and use the existing recovery path instead.
+ const interactive = isPendingSendItemInteractive(item);
+ const outbox = interactive && item.actions === null;
const selected = isPendingSendItemSelected(item, actions.selectedClientId);
const bubbleLabel = item.text || t('message.queue.attachmentMessage');
const uploadsPending = item.phase === 'uploading';
@@ -441,23 +447,23 @@ export function PendingSendBubble({
/>
</View>
) : null}
- {selected && failed ? (
+ {selected && outbox ? (
<View style={styles.actionRow} testID={`pendingSend.outboxActions.${item.clientId}`}>
<ActionPill
busy={actions.busy}
icon={Trash2}
- label={t('message.queue.delete')}
+ label={t(failed ? 'message.queue.delete' : 'message.queue.cancel')}
onPress={() => actions.onRemoveOutbox(item.clientId)}
testID={`pendingSend.outboxRemove.${item.clientId}`}
/>
- <ActionPill
+ {failed ? <ActionPill
busy={actions.busy}
cta
icon={RotateCcw}
label={t('message.queue.retry')}
onPress={() => actions.onRetryOutbox(item.clientId)}
testID={`pendingSend.outboxRetry.${item.clientId}`}
- />
+ /> : null}
</View>
) : null}
</View>
diff --git a/apps/mobile/src/session/pendingSendItems.ts b/apps/mobile/src/session/pendingSendItems.ts
index 3eac758043..57b59773b8 100644
--- a/apps/mobile/src/session/pendingSendItems.ts
+++ b/apps/mobile/src/session/pendingSendItems.ts
@@ -71,6 +71,8 @@ export interface MobilePendingSendItem {
attachmentCount: number;
uploadedCount: number;
errorText: string | null;
+ /** Local outbox may defer cancellation while the first-message creation task owns recovery. */
+ canCancel?: boolean;
/** 可否轻点展开操作行:只有还在队列里的条目能取消 / 编辑 / 插队。 */
actions: MobilePendingSendActions | null;
/** 展开后显示的提示(插队限制等)。 */
@@ -95,11 +97,20 @@ export function buildMobileMessageListExtraData(
/** 待发送气泡是否处于展开态;生产渲染与状态转换测试共用同一判据。 */
export function isPendingSendItemSelected(
- item: Pick<MobilePendingSendItem, 'actions' | 'clientId' | 'phase'>,
+ item: Pick<MobilePendingSendItem, 'actions' | 'canCancel' | 'clientId' | 'phase' | 'queueIndex'>,
selectedClientId: string | null,
): boolean {
- const interactive = item.actions !== null || item.phase === 'failed';
- return interactive && selectedClientId === item.clientId;
+ return isPendingSendItemInteractive(item) && selectedClientId === item.clientId;
+}
+
+/** Local outbox rows can be cancelled before desktop accepts them; settled rows cannot. */
+export function isPendingSendItemInteractive(
+ item: Pick<MobilePendingSendItem, 'actions' | 'canCancel' | 'phase' | 'queueIndex'>,
+): boolean {
+ return item.actions !== null
+ || (item.canCancel !== false && (item.phase === 'uploading'
+ || item.phase === 'failed'
+ || (item.phase === 'sending' && item.queueIndex === null)));
}
export function pendingSendItemKey(clientId: string): string {
@@ -313,6 +324,7 @@ export function buildPendingSendItems(input: BuildPendingSendItemsInput): Mobile
attachmentCount: item.attachmentCount,
uploadedCount: item.uploadedCount,
errorText: item.errorText,
+ canCancel: item.canCancel,
actions: null,
hint: null,
});
diff --git a/apps/mobile/src/session/sessionOutbox.ts b/apps/mobile/src/session/sessionOutbox.ts
index b4eb17683e..fe9c57b176 100644
--- a/apps/mobile/src/session/sessionOutbox.ts
+++ b/apps/mobile/src/session/sessionOutbox.ts
@@ -155,6 +155,8 @@ export interface MobileOutboxDisplayItem {
fileCount: number;
fileNames?: string[];
failed: boolean;
+ /** False while a first-message creation record still owns recovery of the draft. */
+ canCancel?: boolean;
/** 失败原因(附件失败给统一文案,enqueue 失败给 RPC 错误)。 */
errorText: string | null;
}