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): 同步新建任务草稿选区

DavidSheneffe2a712608
diff --git a/apps/mobile/app/sessions/new.tsx b/apps/mobile/app/sessions/new.tsx
index d7effce003..cae80a7e0e 100644
--- a/apps/mobile/app/sessions/new.tsx
+++ b/apps/mobile/app/sessions/new.tsx
@@ -488,6 +488,9 @@ export default function NewRemoteSessionScreen() {
workspaceKind: initialWorkingDir ? 'project' : 'dialogue',
workingDir: initialWorkingDir ?? '',
});
+ const firstMessageRef = useRef(draft.firstMessage);
+ const firstMessageSelectionRef = useRef({ start: draft.firstMessage.length, end: draft.firstMessage.length });
+ const [firstMessageSelection, setFirstMessageSelection] = useState(firstMessageSelectionRef.current);
const [creating, setCreating] = useState(false);
const [createStartedAt, setCreateStartedAt] = useState<number | null>(null);
const [createPhase, setCreatePhase] = useState<SlowSendPhase>('preparing');
@@ -611,6 +614,13 @@ export default function NewRemoteSessionScreen() {
if (!stashed) return;
// 返回编辑沿用草稿的工作区,不能被随后加载的全局默认覆盖。
userTouchedWorkspaceRef.current = true;
+ firstMessageRef.current = stashed.draft.firstMessage;
+ const restoredSelection = {
+ start: stashed.draft.firstMessage.length,
+ end: stashed.draft.firstMessage.length,
+ };
+ firstMessageSelectionRef.current = restoredSelection;
+ setFirstMessageSelection(restoredSelection);
setDraft(stashed.draft);
setAttachments([...stashed.attachments]);
if (stashed.notice) setAttachmentError(stashed.notice);
@@ -696,9 +706,6 @@ export default function NewRemoteSessionScreen() {
const appliedPermissionMemoryRef = useRef(false);
const userTouchedDeviceRef = useRef(false);
const userTouchedWorkspaceRef = useRef(false);
- const firstMessageRef = useRef(draft.firstMessage);
- const firstMessageSelectionRef = useRef({ start: draft.firstMessage.length, end: draft.firstMessage.length });
- const [firstMessageSelection, setFirstMessageSelection] = useState(firstMessageSelectionRef.current);
const firstMessageInputRef = useRef<NativeTextInput>(null);
const voiceDraftScrollRef = useRef<ScrollView>(null);
const voiceRecordingActiveRef = useRef(false);
@@ -3758,11 +3765,23 @@ export default function NewRemoteSessionScreen() {
]);
const selectSlashCommand = useCallback((command: MobileSlashCommand) => {
- setFirstMessageDraft((current) => insertSlashCommand(current, detectComposerTrigger(current), command));
+ const current = firstMessageRef.current;
+ const next = insertSlashCommand(current, detectComposerTrigger(current), command);
+ if (next === current) return;
+ setFirstMessageDraft(next);
+ const selection = { start: next.length, end: next.length };
+ firstMessageSelectionRef.current = selection;
+ setFirstMessageSelection(selection);
}, [setFirstMessageDraft]);
const selectAtResource = useCallback((item: MobileAtResourceItem) => {
- setFirstMessageDraft((current) => insertAtResource(current, detectComposerTrigger(current), item));
+ const current = firstMessageRef.current;
+ const next = insertAtResource(current, detectComposerTrigger(current), item);
+ if (next === current) return;
+ setFirstMessageDraft(next);
+ const selection = { start: next.length, end: next.length };
+ firstMessageSelectionRef.current = selection;
+ setFirstMessageSelection(selection);
}, [setFirstMessageDraft]);
const removeAttachment = useCallback((id: string) => {
diff --git a/apps/mobile/src/__tests__/newSession.test.ts b/apps/mobile/src/__tests__/newSession.test.ts
index 68b5fddfd7..0ec53fd2f4 100644
--- a/apps/mobile/src/__tests__/newSession.test.ts
+++ b/apps/mobile/src/__tests__/newSession.test.ts
@@ -1537,6 +1537,30 @@ describe('new session model', () => {
});
describe('new session composer surface', () => {
+ it('keeps the controlled caret at the end after palette insertion and draft restore', () => {
+ const newSource = readTextLf(resolve(process.cwd(), 'app/sessions/new.tsx'), 'utf8');
+ const slashStart = newSource.indexOf('const selectSlashCommand = useCallback');
+ const slashEnd = newSource.indexOf('const selectAtResource = useCallback', slashStart);
+ const slashSource = newSource.slice(slashStart, slashEnd);
+ const atStart = slashEnd;
+ const atEnd = newSource.indexOf('const removeAttachment = useCallback', atStart);
+ const atSource = newSource.slice(atStart, atEnd);
+ const restoreStart = newSource.indexOf('firstMessageRef.current = stashed.draft.firstMessage;');
+ const restoreEnd = newSource.indexOf('setDraft(stashed.draft);', restoreStart);
+ const restoreSource = newSource.slice(restoreStart, restoreEnd);
+
+ for (const source of [slashSource, atSource]) {
+ expect(source).toContain('const current = firstMessageRef.current;');
+ expect(source).toContain('const selection = { start: next.length, end: next.length };');
+ expect(source.indexOf('setFirstMessageDraft(next)')).toBeLessThan(
+ source.indexOf('setFirstMessageSelection(selection)'),
+ );
+ }
+ expect(restoreSource).toContain('firstMessageRef.current = stashed.draft.firstMessage;');
+ expect(restoreSource).toContain('firstMessageSelectionRef.current = restoredSelection;');
+ expect(restoreSource).toContain('setFirstMessageSelection(restoredSelection);');
+ });
+
it('does not double-apply the Android safe-area inset to the top navigation', () => {
const newSource = readTextLf(resolve(process.cwd(), 'app/sessions/new.tsx'), 'utf8');
diff --git a/apps/mobile/src/__tests__/newSessionWorkspaceEffects.test.tsx b/apps/mobile/src/__tests__/newSessionWorkspaceEffects.test.tsx
index d47d0e51e9..af0f64b85b 100644
--- a/apps/mobile/src/__tests__/newSessionWorkspaceEffects.test.tsx
+++ b/apps/mobile/src/__tests__/newSessionWorkspaceEffects.test.tsx
@@ -30,6 +30,7 @@ const declarations = new Set([
'appliedDefaultDeviceKeyRef', 'userTouchedDeviceRef', 'userTouchedWorkspaceRef',
'patchDraft', 'selectWorkingDir', 'rememberWorkingDirForDevice', 'selectDialogueWorkspace',
'selectRecentProject', 'openProjectBrowse',
+ 'firstMessageRef', 'firstMessageSelectionRef', 'firstMessageSelection',
]);
const effectMarkers = new Set([
'drainStashedNewSessionDraft', 'readNewSessionPreferences',
@@ -64,6 +65,7 @@ for (const name of [...declarations, ...effectMarkers]) {
interface WorkspaceState {
draft: NewSessionDraft;
+ firstMessageSelection: { start: number; end: number };
selectedDeviceId: string;
newSessionPreferencesLoaded: boolean;
selectDialogueWorkspace(): void;
@@ -91,7 +93,7 @@ const compiled = ts.transpileModule(`function usePageWorkspace(bindings) {
setDraft((current) => current.workspaceKind === 'project' ? { ...current, workingDir: '' } : current);
};
return { draft, selectedDeviceId, newSessionPreferencesLoaded,
- selectDialogueWorkspace, selectRecentProject, openProjectBrowse, switchDevice };
+ firstMessageSelection, selectDialogueWorkspace, selectRecentProject, openProjectBrowse, switchDevice };
}`, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText;
const usePageWorkspace = new Function(`${compiled}; return usePageWorkspace;`)() as
(bindings: Record<string, unknown>) => WorkspaceState;
@@ -119,7 +121,8 @@ function mountWorkspace(options: { initialWorkingDir?: string; restoredKind?: Ne
saveNewSessionPreferences: vi.fn(async () => {}),
drainStashedNewSessionDraft: vi.fn(() => options.restoredKind ? {
draft: { ...DEFAULT_NEW_SESSION_DRAFT, workspaceKind: options.restoredKind,
- workingDir: options.restoredKind === 'project' ? '/restored/project' : '' },
+ workingDir: options.restoredKind === 'project' ? '/restored/project' : '',
+ firstMessage: 'restored draft' },
attachments: [], deviceId: 'a', deviceName: 'A',
} : null),
loadBrowsePath: vi.fn(async () => {}), setDevicePickerOpen: vi.fn(),
@@ -182,6 +185,7 @@ describe('new session workspace page effects', () => {
expect(page.current.draft).toMatchObject({ workspaceKind: kind,
workingDir: kind === 'project' ? '/restored/project' : '' });
expect(page.current.selectedDeviceId).toBe('a');
+ expect(page.current.firstMessageSelection).toEqual({ start: 'restored draft'.length, end: 'restored draft'.length });
expect(page.bindings.pickInitialNewSessionWorkspace).not.toHaveBeenCalled();
});