Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): preserve mobile compact commands (#2280)
许君山 Junshane6aa32bd21f3
diff --git a/apps/desktop/src/main/__tests__/mobileClientPromptNote.test.ts b/apps/desktop/src/main/__tests__/mobileClientPromptNote.test.tsindex 4fcce35684..fec025a0b6 100644--- a/apps/desktop/src/main/__tests__/mobileClientPromptNote.test.ts+++ b/apps/desktop/src/main/__tests__/mobileClientPromptNote.test.ts@@ -25,6 +25,7 @@ import {import {attachMainOwnedInputBoundary,buildMobileClientPromptNote,+ shouldPrependMobileClientPromptNote,stampMobileClientOrigin,stripMainOnlySendOpts,} from '../maker-ipc/mobileClientPromptNote';@@ -189,6 +190,39 @@ describe('prependNoteToWireUserMessage(wire 注入形态)', () => {});});+describe('shouldPrependMobileClientPromptNote(内置命令旁路)', () => {+ it('Claude Code /compact 保持在消息开头,含可选指令也旁路', () => {+ expect(shouldPrependMobileClientPromptNote('/compact', 'claude-code')).toBe(false);+ expect(shouldPrependMobileClientPromptNote('/compact focus on decisions', 'claude-code'))+ .toBe(false);+ expect(shouldPrependMobileClientPromptNote(+ { type: 'user', content: '/compact\nkeep the API contract' },+ 'claude-code',+ )).toBe(false);+ expect(shouldPrependMobileClientPromptNote(+ { type: 'user', content: [{ type: 'text', text: '/compact' }] },+ 'claude-code',+ )).toBe(false);+ });++ it('不把相似文本、带附件消息或其他 Agent 的输入误判成 Claude 命令', () => {+ expect(shouldPrependMobileClientPromptNote('/compactness', 'claude-code')).toBe(true);+ expect(shouldPrependMobileClientPromptNote(' /compact', 'claude-code')).toBe(true);+ expect(shouldPrependMobileClientPromptNote(+ {+ type: 'user',+ content: [+ { type: 'text', text: '/compact' },+ { type: 'image', url: 'x' },+ ],+ },+ 'claude-code',+ )).toBe(true);+ expect(shouldPrependMobileClientPromptNote('/compact', 'pi')).toBe(true);+ expect(shouldPrependMobileClientPromptNote('/compact', 'codex')).toBe(true);+ });+});+describe('注入接线(源码级守卫)', () => {const source = readFileSync(resolve(process.cwd(), 'src/main/maker-ipc/makerSendTransaction.ts'),@@ -196,10 +230,11 @@ describe('注入接线(源码级守卫)', () => {);it('说明只进 wire payload,落库走 persistUserMessage.content', () => {- expect(source).toMatch(- /const mobileClientNote =\s*deps\.isMobileClientInvoke\?\.\(\) === true/,+ expect(source).toContain(+ 'deps.isMobileClientInvoke?.() === true || so.fromMobileClient === true',);expect(source).toContain('prependNoteToWireUserMessage(withHandoff as HandoffWireMessage, mobileClientNote)');+ expect(source).toContain('shouldPrependMobileClientPromptNote(normalized, sess.agentKind)');// 落库内容必须仍取 persistUserMessage.content —— 若改成 outgoing,提示语会写进// 用户消息、污染界面显示的原话。expect(source).toContain('content: persistUserMessage.content');@@ -352,6 +387,7 @@ describe('排队 / 插入两条路径的接线(源码级守卫)', () => {it('steer 投递也注入说明,且只进 wire payload', () => {expect(register).toContain("isMobileControllerInvoke() || so.fromMobileClient === true");+ expect(register).toContain('shouldPrependMobileClientPromptNote(normalized, sess.agentKind)');expect(register).toContain('prependNoteToWireUserMessage(normalized as HandoffWireMessage, steerNote)');expect(register).toContain('await sess.steer(steerPayload as never');});diff --git a/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.tsindex ce96738c1c..de22a33ee0 100644--- a/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts+++ b/apps/desktop/src/main/maker-ipc/__tests__/makerSendTransaction.test.ts@@ -1298,6 +1298,74 @@ describe('maker SEND transaction', () => {});});+describe('mobile client prompt note', () => {+ it('keeps ordinary mobile messages annotated on the wire but persists the original text', async () => {+ const session = createSession({ agentKind: 'claude-code' });+ const { deps } = createDeps({+ getSession: vi.fn(() => session),+ isMobileClientInvoke: vi.fn(() => true),+ });+ const transaction = createMakerSendTransaction(deps);++ await transaction.sendToAgentAccepted('session-1', 'hello', undefined, {+ persistUserMessage: { clientId: 'mobile-1', content: 'hello' },+ });++ const sent = vi.mocked(session.send).mock.calls[0]?.[0];+ expect(sent).toEqual(expect.stringMatching(/^\[客户端说明\]/));+ expect(sent).toEqual(expect.stringMatching(/\n\nhello$/));+ expect(vi.mocked(deps.createDbMessage).mock.calls[0]?.[1].content).toBe('hello');+ });++ it('sends mobile Claude Code /compact commands without a prepended note', async () => {+ const session = createSession({ agentKind: 'claude-code' });+ const { deps } = createDeps({+ getSession: vi.fn(() => session),+ isMobileClientInvoke: vi.fn(() => true),+ });+ const transaction = createMakerSendTransaction(deps);++ await transaction.sendToAgentAccepted('session-1', '/compact focus on decisions');++ expect(session.send).toHaveBeenCalledWith('/compact focus on decisions', expect.anything());+ });++ it('keeps the mobile note for /compact text sent to a non-Claude agent', async () => {+ const session = createSession({ agentKind: 'pi' });+ const { deps } = createDeps({+ getSession: vi.fn(() => session),+ isMobileClientInvoke: vi.fn(() => true),+ });+ const transaction = createMakerSendTransaction(deps);++ await transaction.sendToAgentAccepted('session-1', '/compact');++ const sent = vi.mocked(session.send).mock.calls[0]?.[0];+ expect(sent).toEqual(expect.stringMatching(/^\[客户端说明\]/));+ });++ it('applies the same command bypass to coordinator-drained mobile messages', async () => {+ const session = createSession({ agentKind: 'claude-code' });+ const { deps } = createDeps({+ getSession: vi.fn(() => session),+ isMobileClientInvoke: vi.fn(() => false),+ });+ const transaction = createMakerSendTransaction(deps);++ await transaction.sendToAgentAccepted(+ 'session-1',+ { type: 'user', content: '/compact' },+ undefined,+ { fromMobileClient: true },+ );++ expect(session.send).toHaveBeenCalledWith(+ { type: 'user', content: '/compact' },+ expect.anything(),+ );+ });+});+describe('session-agent-switch handoff injection', () => {it('pending 命中时 wire payload 前置交接段,落库内容保持用户原文,accepted 后 consume', async () => {const consumePendingHandoff = vi.fn();diff --git a/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts b/apps/desktop/src/main/maker-ipc/makerSendTransaction.tsindex 78833855d9..2ec521ffce 100644--- a/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts+++ b/apps/desktop/src/main/maker-ipc/makerSendTransaction.ts@@ -22,7 +22,10 @@ import {prependNoteToWireUserMessage,type HandoffWireMessage,} from './agentHandoff.js';-import { buildMobileClientPromptNote } from './mobileClientPromptNote.js';+import {+ buildMobileClientPromptNote,+ shouldPrependMobileClientPromptNote,+} from './mobileClientPromptNote.js';import type { MakerSessionCreateOpts } from './sessionRequest.js';type CreateOpts = MakerSessionCreateOpts;@@ -742,7 +745,8 @@ export function createMakerSendTransaction(deps: MakerSendTransactionDeps): Make// 两个来源:直连 maker:send 走 async context(deps 注入);排队 / 插入路径走// coordinator 从队列项透传的 so.fromMobileClient(drain 时 context 已结束)。const mobileClientNote =- deps.isMobileClientInvoke?.() === true || so.fromMobileClient === true+ (deps.isMobileClientInvoke?.() === true || so.fromMobileClient === true)+ && shouldPrependMobileClientPromptNote(normalized, sess.agentKind)? buildMobileClientPromptNote(): null;const outgoing = mobileClientNotediff --git a/apps/desktop/src/main/maker-ipc/mobileClientPromptNote.ts b/apps/desktop/src/main/maker-ipc/mobileClientPromptNote.tsindex 8f1c7e62e2..b27a381d61 100644--- a/apps/desktop/src/main/maker-ipc/mobileClientPromptNote.ts+++ b/apps/desktop/src/main/maker-ipc/mobileClientPromptNote.ts@@ -41,6 +41,34 @@ export function buildMobileClientPromptNote(): string {);}+function singleTextContent(message: unknown): string | null {+ if (typeof message === 'string') return message;+ if (!message || typeof message !== 'object' || Array.isArray(message)) return null;+ const content = (message as { content?: unknown }).content;+ if (typeof content === 'string') return content;+ if (!Array.isArray(content) || content.length !== 1) return null;+ const part = content[0];+ if (!part || typeof part !== 'object' || Array.isArray(part)) return null;+ const { type, text } = part as { type?: unknown; text?: unknown };+ return (+ (type === 'text' || type === 'input_text' || type === 'output_text')+ && typeof text === 'string'+ ) ? text : null;+}++/**+ * Claude Code 内置命令必须位于消息开头;手机客户端说明不能抢占这个位置。+ * 只放行已由 palette 明确暴露的 `/compact`,其它 slash 文本继续保留来源说明。+ */+export function shouldPrependMobileClientPromptNote(+ message: unknown,+ agentKind: string,+): boolean {+ if (agentKind !== 'claude-code') return true;+ const text = singleTextContent(message);+ return text === null || !/^\/compact(?:\s|$)/.test(text);+}+/*** 在 IPC 边界给队列项盖上手机来源(返回新对象,不原地改入参)。*diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.tsindex d8eacc4216..cc40e3fd05 100644--- a/apps/desktop/src/main/maker-ipc/register.ts+++ b/apps/desktop/src/main/maker-ipc/register.ts@@ -721,6 +721,7 @@ import {import {attachMainOwnedInputBoundary,buildMobileClientPromptNote,+ shouldPrependMobileClientPromptNote,stripMainOnlySendOpts,stampMobileClientOrigin,type MainOwnedInputBoundaryStamp,@@ -9156,7 +9157,8 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)// 手机说明同样只进 wire payload(steer 路径不落库用户消息,天然不污染原话)。// 两个来源都要认:IPC 直连 steer 时 async context 在;coordinator 投递时靠透传。const steerNote =- isMobileControllerInvoke() || so.fromMobileClient === true+ (isMobileControllerInvoke() || so.fromMobileClient === true)+ && shouldPrependMobileClientPromptNote(normalized, sess.agentKind)? buildMobileClientPromptNote(): null;const steerPayload = steerNote