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): 停止后消息菜单也能删除消息 (#5823)

Dash45f828920a32
diff --git a/apps/mobile/app/sessions/[sessionId].tsx b/apps/mobile/app/sessions/[sessionId].tsx
index 48c2ec7425..21cbed8738 100644
--- a/apps/mobile/app/sessions/[sessionId].tsx
+++ b/apps/mobile/app/sessions/[sessionId].tsx
@@ -130,6 +130,7 @@ import {
humanizeRemoteError,
isAutoRecoveringRemoteError,
isPreconditionFailedRemoteError,
+ isSessionRunningRemoteError,
} from '@/device-link/remoteStatus';
import { agentAuthGateHint, agentAuthGateVerdict } from '@/session/agentAuthGate';
import { isTransientRemoteError, withTransientRemoteRetry } from '@/device-link/remoteRetry';
@@ -9257,6 +9258,8 @@ export default function SessionScreen() {
const deleteMessage = useCallback((clientId: string) => {
if (!deviceId || messageActionBusy) return;
+ // 运行中能否删由被控端判定:点停止后本地运行态可能晚于被控端收敛,
+ // 不能拿它挡住已经停下的任务。被控端拒绝(SESSION_RUNNING)时再提示等回复结束。
Alert.alert(t('session.screen.deleteMessageTitle'), t('session.screen.deleteMessageBody'), [
{ text: t('session.common.cancel'), style: 'cancel' },
{
@@ -9279,7 +9282,11 @@ export default function SessionScreen() {
deviceId,
);
} catch (err) {
- setError(formatRemoteError(err));
+ if (isSessionRunningRemoteError(err)) {
+ Alert.alert(t('session.screen.deleteMessageBusy'));
+ } else {
+ setError(formatRemoteError(err));
+ }
} finally {
setMessageActionBusy(null);
}
diff --git a/apps/mobile/src/__tests__/messageActionDesktopFirst.test.ts b/apps/mobile/src/__tests__/messageActionDesktopFirst.test.ts
index b08739da5d..05c83280b4 100644
--- a/apps/mobile/src/__tests__/messageActionDesktopFirst.test.ts
+++ b/apps/mobile/src/__tests__/messageActionDesktopFirst.test.ts
@@ -19,7 +19,10 @@ describe('mobile message actions desktop-first surface', () => {
expect(source).toContain('<MessageActionSheet');
expect(source).toContain('buildMobileMessageMenu({');
expect(source).toContain("&& (item.message.kind === 'user' || item.message.kind === 'assistant')");
- expect(source).toContain('&& actions.isSessionStreaming !== true');
+ // 运行中删除入口的可见性与 SESSION_RUNNING 识别由
+ // messageSourceLabelsRender / remoteStatus 的行为测试覆盖。
+ const screenSource = readFileSync(resolve(process.cwd(), 'app/sessions/[sessionId].tsx'), 'utf8');
+ expect(screenSource).toContain('if (isSessionRunningRemoteError(err)) {');
expect(source).toContain('hitSlop={MESSAGE_CONTROL_HIT_SLOP}');
expect(source).toContain('buttonSize={actionBar.buttonSize}');
expect(source).toContain('iconSize={actionBar.iconSize}');
diff --git a/apps/mobile/src/__tests__/messageSourceLabelsRender.test.tsx b/apps/mobile/src/__tests__/messageSourceLabelsRender.test.tsx
index 08757b6650..e9929a303d 100644
--- a/apps/mobile/src/__tests__/messageSourceLabelsRender.test.tsx
+++ b/apps/mobile/src/__tests__/messageSourceLabelsRender.test.tsx
@@ -215,8 +215,12 @@ vi.mock("@/session/mediaPlayerWebView", () => ({
vi.mock("@/session/MarkdownBlockContent", () => ({
MarkdownBlockContent: () => null,
}));
+// 只把菜单项 id 投到 DOM,供删除入口可见性断言;不渲染真实底部面板。
vi.mock("@/session/MessageActionSheet", () => ({
- MessageActionSheet: () => null,
+ MessageActionSheet: ({ items }: any) =>
+ createElement("div", {
+ "data-menu-items": items.map((item: any) => item.id).join(","),
+ }),
}));
vi.mock("@/session/AuthorizationMessageCard", () => ({
AuthorizationMessageCard: () => null,
@@ -337,6 +341,31 @@ describe("device source label", () => {
});
});
+describe("message delete entry", () => {
+ const menus = (html: string) =>
+ [...html.matchAll(/data-menu-items="([^"]*)"/g)].map((m) => m[1]);
+
+ it("stays in the More menu while the session still looks running locally", () => {
+ // 点停止后手机本地运行态可能晚于被控端收敛:删除不能因此被藏起来,
+ // 运行中能否删由被控端 SESSION_RUNNING 判定。
+ const messages = [msg("u1", "first", null), msg("u2", "second", null)];
+ for (const isSessionStreaming of [true, false]) {
+ const items = menus(
+ render(messages, { onDeleteMessage: vi.fn(), isSessionStreaming }),
+ );
+ expect(items.length).toBe(2);
+ for (const ids of items) expect(ids.split(",")).toContain("delete");
+ }
+ });
+
+ it("is absent when the screen offers no delete action (shared task guest)", () => {
+ const items = menus(
+ render([msg("u1", "first", null)], { isSessionStreaming: false }),
+ );
+ for (const ids of items) expect(ids.split(",")).not.toContain("delete");
+ });
+});
+
describe("sender source labels", () => {
it("renders plugin, shared-task author (above the bubble) and redacted automation labels", () => {
const html = render([
diff --git a/apps/mobile/src/__tests__/remoteStatus.test.ts b/apps/mobile/src/__tests__/remoteStatus.test.ts
index 135b93d2d0..46b459d4e2 100644
--- a/apps/mobile/src/__tests__/remoteStatus.test.ts
+++ b/apps/mobile/src/__tests__/remoteStatus.test.ts
@@ -9,6 +9,7 @@ import {
formatRemoteError,
humanizeRemoteError,
isAutoRecoveringRemoteError,
+ isSessionRunningRemoteError,
relayStatusHint,
relayStatusLabel,
} from '@/device-link/remoteStatus';
@@ -57,6 +58,20 @@ describe('remoteStatus', () => {
expect(formatRemoteError(new Error('[NOT_CONNECTED] offline'))).toBe('[NOT_CONNECTED] offline');
});
+ it('recognizes a host refusal because the task is still running', () => {
+ // 被控端 handler 抛 `[SESSION_RUNNING] ...`,经隧道到手机时外层 code 是 IPC_ERROR。
+ expect(isSessionRunningRemoteError(
+ new DeviceLinkError('IPC_ERROR' as never, '[SESSION_RUNNING] Session s1 is running a turn'),
+ )).toBe(true);
+ expect(isSessionRunningRemoteError(
+ Object.assign(new Error('Session s1 has background activity'), { code: 'SESSION_RUNNING' }),
+ )).toBe(true);
+ expect(isSessionRunningRemoteError(
+ new DeviceLinkError('IPC_ERROR' as never, '[NOT_FOUND] Message 不存在或不可删除'),
+ )).toBe(false);
+ expect(isSessionRunningRemoteError(new DeviceLinkError('NOT_CONNECTED', 'offline'))).toBe(false);
+ });
+
it('keeps the composer writable for automatic recovery, but blocks deterministic failures', () => {
for (const error of [
'[NOT_CONNECTED] offline',
diff --git a/apps/mobile/src/device-link/remoteStatus.ts b/apps/mobile/src/device-link/remoteStatus.ts
index 8f25eb9ec2..5675042a71 100644
--- a/apps/mobile/src/device-link/remoteStatus.ts
+++ b/apps/mobile/src/device-link/remoteStatus.ts
@@ -119,6 +119,14 @@ export function isAutoRecoveringRemoteError(error: unknown): boolean {
|| isDeviceUnresponsiveRemoteError(error);
}
+/**
+ * 被控端因任务仍在运行而拒绝操作(如删除消息)。handler 的 `[SESSION_RUNNING]`
+ * 前缀随隧道放在 message 里(外层 code 为 IPC_ERROR),code 与 message 两种形态都认。
+ */
+export function isSessionRunningRemoteError(error: unknown): boolean {
+ return formatRemoteErrorShared(error).includes('SESSION_RUNNING');
+}
+
/**
* 权威同步的一轮内置瞬态重试耗尽后,页面继续自动恢复所用的外层退避。
* 900ms 尽快吃掉短抖动,随后指数放缓并封顶 30s,避免电脑长期离线时形成请求风暴。
diff --git a/apps/mobile/src/i18n/locales/en/session.json b/apps/mobile/src/i18n/locales/en/session.json
index 27b0b51bf7..3da0ca4756 100644
--- a/apps/mobile/src/i18n/locales/en/session.json
+++ b/apps/mobile/src/i18n/locales/en/session.json
@@ -242,6 +242,7 @@
"deleteMessageTitle": "Delete this message?",
"deleteMessageBody": "Deleting a user message removes only that message; deleting an AI message removes the entire turn generated after the previous user input. Later messages are kept. On your next send, the AI context is rebuilt from the remaining records.",
"deleteMessageAction": "Delete this message",
+ "deleteMessageBusy": "Wait for the current response to finish before deleting a message.",
"readingCapabilities": "Reading {{agent}} capabilities…",
"editingQueueMessageNumbered": "Editing queued message #{{index}}",
"editingQueueMessage": "Editing queued message",
diff --git a/apps/mobile/src/i18n/locales/ja/session.json b/apps/mobile/src/i18n/locales/ja/session.json
index db86589e79..1e87be3353 100644
--- a/apps/mobile/src/i18n/locales/ja/session.json
+++ b/apps/mobile/src/i18n/locales/ja/session.json
@@ -242,6 +242,7 @@
"deleteMessageTitle": "このメッセージを削除しますか?",
"deleteMessageBody": "ユーザーメッセージはこの1件のみ削除されます。AI メッセージの場合は、直前のユーザー入力以降に生成されたターン全体が削除されます。以降の会話は保持されます。次回の送信時に、残った記録から AI のコンテキストが再構築されます。",
"deleteMessageAction": "このメッセージを削除",
+ "deleteMessageBusy": "現在の応答が終わってからメッセージを削除してください。",
"readingCapabilities": "{{agent}} の実行機能を読み込み中…",
"editingQueueMessageNumbered": "キュー内メッセージ {{index}} 件目を編集中",
"editingQueueMessage": "キュー内メッセージを編集中",
diff --git a/apps/mobile/src/i18n/locales/ko/session.json b/apps/mobile/src/i18n/locales/ko/session.json
index 11b65d97d2..9440777c09 100644
--- a/apps/mobile/src/i18n/locales/ko/session.json
+++ b/apps/mobile/src/i18n/locales/ko/session.json
@@ -238,6 +238,7 @@
"deleteMessageTitle": "이 메시지를 삭제할까요?",
"deleteMessageBody": "사용자 메시지는 이 항목만 삭제됩니다. AI 메시지의 경우 이전 사용자 입력 이후 생성된 전체 턴이 삭제됩니다. 이후 대화는 유지됩니다. 다음 전송 시 남은 기록에서 AI 컨텍스트가 다시 구성됩니다.",
"deleteMessageAction": "이 메시지 삭제",
+ "deleteMessageBusy": "현재 응답이 끝난 후 메시지를 삭제해 주세요.",
"readingCapabilities": "{{agent}} 실행 기능을 불러오는 중…",
"editingQueueMessageNumbered": "대기열 메시지 {{index}}번째 편집 중",
"editingQueueMessage": "대기열 메시지 편집 중",
diff --git a/apps/mobile/src/i18n/locales/zh-CN/session.json b/apps/mobile/src/i18n/locales/zh-CN/session.json
index 99c4141196..1b867bb352 100644
--- a/apps/mobile/src/i18n/locales/zh-CN/session.json
+++ b/apps/mobile/src/i18n/locales/zh-CN/session.json
@@ -238,6 +238,7 @@
"deleteMessageTitle": "删除本条消息?",
"deleteMessageBody": "用户消息只删除本条;AI 消息会删除上一次用户输入之后产生的整轮输出。之后的消息继续保留。下次发送时,AI 上下文会从剩余记录重建。",
"deleteMessageAction": "删除本条消息",
+ "deleteMessageBusy": "请等当前回复结束后再删除消息",
"readingCapabilities": "正在读取 {{agent}} 运行能力…",
"editingQueueMessageNumbered": "正在编辑第 {{index}} 条排队消息",
"editingQueueMessage": "正在编辑排队消息",
diff --git a/apps/mobile/src/i18n/locales/zh-TW/session.json b/apps/mobile/src/i18n/locales/zh-TW/session.json
index d7d8e8d99f..678a3b5a5e 100644
--- a/apps/mobile/src/i18n/locales/zh-TW/session.json
+++ b/apps/mobile/src/i18n/locales/zh-TW/session.json
@@ -242,6 +242,7 @@
"deleteMessageTitle": "刪除本條訊息?",
"deleteMessageBody": "使用者訊息只刪除本條;AI 訊息會刪除上一次使用者輸入之後產生的整輪輸出。之後的訊息繼續保留。下次傳送時,AI 上下文會從剩餘記錄重建。",
"deleteMessageAction": "刪除本條訊息",
+ "deleteMessageBusy": "請等當前回覆結束後再刪除訊息",
"readingCapabilities": "正在讀取 {{agent}} 執行能力…",
"editingQueueMessageNumbered": "正在編輯第 {{index}} 條排隊訊息",
"editingQueueMessage": "正在編輯排隊訊息",
diff --git a/apps/mobile/src/session/MessageRenderer.tsx b/apps/mobile/src/session/MessageRenderer.tsx
index fac75a677b..a36f3912f4 100644
--- a/apps/mobile/src/session/MessageRenderer.tsx
+++ b/apps/mobile/src/session/MessageRenderer.tsx
@@ -3512,11 +3512,11 @@ function MessageBubble({
&& item.message.kind === 'user'
&& !isFirstUserMessage
);
+ // 对齐桌面:任务运行中也保留删除入口,点了由页面提示等回复结束,不让菜单项时有时无。
const canDelete = !!(
showCompletedActionBar
&& clientId
&& actions.onDeleteMessage
- && actions.isSessionStreaming !== true
&& (item.message.kind === 'user' || item.message.kind === 'assistant')
);
const canCopyLink = !!(showCompletedActionBar && clientId && actions.onCopyMessageLink);