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): ME drop the old computer's chat from the stack when the cloud view hands over to the new main computer

nana8da922e42df3
diff --git a/apps/mobile/src/__tests__/useTeammateNavigation.test.tsx b/apps/mobile/src/__tests__/useTeammateNavigation.test.tsx
index e96ea7bfa4..6d63fcc35e 100644
--- a/apps/mobile/src/__tests__/useTeammateNavigation.test.tsx
+++ b/apps/mobile/src/__tests__/useTeammateNavigation.test.tsx
@@ -4,11 +4,11 @@ import { createRoot, type Root } from 'react-dom/client';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { MobileUser } from '@/auth/AuthContext';
const h = vi.hoisted(() => ({
- disk: new Map<string, string>(), push: vi.fn(), dismissTo: vi.fn(), dismiss: vi.fn(), reset: vi.fn(), accountGeneration: 1,
+ disk: new Map<string, string>(), push: vi.fn(), replace: vi.fn(), dismissTo: vi.fn(), dismiss: vi.fn(), reset: vi.fn(), accountGeneration: 1,
user: { id: 'account', passportId: 'passport', membershipKind: 'personal', orgId: null } as MobileUser,
sessions: [] as import('@/session/types').RemoteSession[], upsert: vi.fn(), get: vi.fn(), set: vi.fn(), routes: [] as Array<{ name: string; params?: unknown }>,
}));
-vi.mock('expo-router', () => ({ useRouter: () => ({ dismissTo: h.dismissTo, dismiss: h.dismiss }), useNavigation: () => ({ reset: h.reset, getState: () => ({ routes: h.routes, index: h.routes.length - 1 }) }) }));
+vi.mock('expo-router', () => ({ useRouter: () => ({ dismissTo: h.dismissTo, dismiss: h.dismiss, replace: h.replace }), useNavigation: () => ({ reset: h.reset, getState: () => ({ routes: h.routes, index: h.routes.length - 1 }) }) }));
vi.mock('react-native', () => ({ Keyboard: { dismiss() {} } }));
vi.mock('react-i18next', () => ({ useTranslation: () => ({ i18n: { language: 'en' } }) }));
vi.mock('@/auth/AuthContext', () => ({ useAuth: () => ({ user: h.user, accountGeneration: h.accountGeneration }) }));
@@ -57,6 +57,23 @@ describe('header/home shared navigation', () => {
});
expect(h.set).toHaveBeenCalledTimes(1);
});
+ // Tier: behavior. After "continue on another computer" the cloud view replaced itself with the new chat, but the old
+ // computer's chat it was opened from stayed underneath, so back showed it ("Computer not connected") before the list.
+ it('handing over from the cloud view also drops the old computer\'s chat, so back goes to the list', async () => {
+ const moved = { ...teammate, key: 'win:writer', host: { deviceId: 'win', deviceName: 'Studio' } };
+ const old = { name: 'resources/[collectionId]/[resourceId]', key: 'old', params: { resourceId: 'writer', deviceId: 'mac' } };
+ const list = { name: 'devices/index', key: 'list' }; const cloud = { name: 'companions/cloud/[actorId]', key: 'cloud' };
+ h.routes = [list, old, cloud];
+ await render(); await act(async () => result.openTeammate(moved, { replace: true, leaving: { botId: 'writer', deviceId: 'mac' } }));
+ expect(h.reset).toHaveBeenCalledExactlyOnceWith({ index: 1, routes: [list, { name: 'resources/[collectionId]/[resourceId]', params: {
+ collectionId: 'teammates', resourceId: 'writer', resourceKind: 'bot', deviceId: 'win', deviceName: 'Studio', title: 'Writer',
+ } }] });
+ expect(h.replace).not.toHaveBeenCalled();
+ // Opened straight from the list: only the cloud view is replaced.
+ h.reset.mockClear(); h.routes = [list, cloud];
+ await act(async () => result.openTeammate(moved, { replace: true, leaving: { botId: 'writer', deviceId: 'mac' } }));
+ expect(h.reset).not.toHaveBeenCalled(); expect(h.replace).toHaveBeenCalledTimes(1);
+ });
it('opens a creation receipt through the canonical resolver with one atomic preference update', async () => {
await render(); await act(async () => result.openCreatedTeammate(teammate.host, teammate.item.ref));
expect(h.push).toHaveBeenCalledExactlyOnceWith({ pathname: '/resources/[collectionId]/[resourceId]', params: {
diff --git a/apps/mobile/src/session/CloudCompanionScreen.tsx b/apps/mobile/src/session/CloudCompanionScreen.tsx
index 79f9cbec55..4b13816b1a 100644
--- a/apps/mobile/src/session/CloudCompanionScreen.tsx
+++ b/apps/mobile/src/session/CloudCompanionScreen.tsx
@@ -135,9 +135,9 @@ export function CloudCompanionScreen({ actorId, botId, deviceId, deviceName, tit
if (arrived) {
switchingRef.current = { phase: 'idle' };
setSwitching(switchingRef.current);
- void openTeammate.current(arrived, { replace: true });
+ void openTeammate.current(arrived, { replace: true, leaving: { botId, deviceId } });
} else if (state !== switchingRef.current) setSwitching(state);
- }, [botId]);
+ }, [botId, deviceId]);
// Every roster change may be the arrival; the interval re-reads the roster and enforces the timeout.
useEffect(() => { if (waiting) tick(); });
useEffect(() => {
diff --git a/apps/mobile/src/session/teammateNavigation.ts b/apps/mobile/src/session/teammateNavigation.ts
index 9081e8174d..3eb0cc94ab 100644
--- a/apps/mobile/src/session/teammateNavigation.ts
+++ b/apps/mobile/src/session/teammateNavigation.ts
@@ -60,6 +60,22 @@ export function cloudCompanionRoute(hosted: HostedRemoteCollectionItem, actorId:
};
}
+/**
+ * Chat screens of one companion on one computer stacked right under the top route, counted from just below it.
+ * The cloud view opened from a chat leaves that chat underneath; once the companion runs elsewhere, back must skip it.
+ */
+export function staleCompanionChatCount(routes: readonly { name: string; params?: unknown }[], botId: string, deviceId: string): number {
+ let count = 0;
+ for (let index = routes.length - 2; index >= 0; index--) {
+ const route = routes[index]!;
+ const params = route.params as { resourceId?: string; deviceId?: string } | undefined;
+ if (!['sessions/[sessionId]', 'resources/[collectionId]/[resourceId]'].includes(route.name)
+ || params?.resourceId !== botId || params?.deviceId !== deviceId) break;
+ count += 1;
+ }
+ return count;
+}
+
/** Reuse the actual home route; retired collection routes are replaced. */
export function homeDismissCount(routes: readonly { name: string; params?: unknown }[]): number | null {
for (let index = routes.length - 1; index >= 0; index--) {
diff --git a/apps/mobile/src/session/useTeammateNavigation.ts b/apps/mobile/src/session/useTeammateNavigation.ts
index 98a08cf244..fcc7421329 100644
--- a/apps/mobile/src/session/useTeammateNavigation.ts
+++ b/apps/mobile/src/session/useTeammateNavigation.ts
@@ -8,7 +8,7 @@ import type { HostedRemoteCollectionItem, RemoteResourceHostTarget } from '@/dev
import { useGuardedPush } from '@/utils/useGuardedPush';
import { useHomeMode } from './useHomeMode';
import type { HomeMode } from './homeViewPreferenceStore';
-import { homeDismissCount, teammateIdentity, teammateResourceRoute, teammateSessionPreview } from './teammateNavigation';
+import { homeDismissCount, staleCompanionChatCount, teammateIdentity, teammateResourceRoute, teammateSessionPreview } from './teammateNavigation';
import { remoteSessionStore } from './remoteSessionStore';
import { readRemoteCollectionCache, writeRemoteCollectionCache } from '@/device-link/remoteResourceAvailability';
@@ -49,8 +49,12 @@ export function useTeammateNavigation() {
}
} finally { if (pendingAccount.current === accountGeneration) pendingAccount.current = null; }
}, [accountGeneration, navigation, preferences.setMode, router]);
- /** `replace` swaps the current screen (the cloud view handing over to the new main computer). */
- const openTeammate = useCallback(async (hosted: HostedRemoteCollectionItem, options?: { replace?: boolean }) => {
+ /**
+ * `replace` swaps the current screen (the cloud view handing over to the new main computer);
+ * `leaving` also drops that companion's chat on the computer being left from under it, so back goes to the list.
+ */
+ const openTeammate = useCallback(async (hosted: HostedRemoteCollectionItem,
+ options?: { replace?: boolean; leaving?: { botId: string; deviceId: string } }) => {
const identity = teammateIdentity(hosted);
if (!identity || pendingAccount.current === accountGeneration) return;
// Fence rapid taps before the async preference write, not only at router.push.
@@ -71,11 +75,18 @@ export function useTeammateNavigation() {
remoteSessionStore.upsertDeviceSession(hosted.host.deviceId, hosted.host.deviceName, preview);
}
const route = teammateResourceRoute(hosted, i18n.language);
- if (options?.replace) router.replace(route);
+ const state = options?.replace && options.leaving ? navigation.getState() : null;
+ const routes = state?.routes.slice(0, state.index + 1) ?? [];
+ const stale = options?.leaving ? staleCompanionChatCount(routes, options.leaving.botId, options.leaving.deviceId) : 0;
+ if (stale > 0) navigation.reset({
+ index: routes.length - stale - 1,
+ routes: [...routes.slice(0, routes.length - 1 - stale), { name: route.pathname.slice(1), params: route.params }],
+ } as never);
+ else if (options?.replace) router.replace(route);
else push(route);
}
} finally { if (pendingAccount.current === accountGeneration) pendingAccount.current = null; }
- }, [accountGeneration, i18n.language, preferences.selectTeammate, push, router, user?.id]);
+ }, [accountGeneration, i18n.language, navigation, preferences.selectTeammate, push, router, user?.id]);
const openCreatedTeammate = useCallback(async (host: RemoteResourceHostTarget, ref: RemoteResourceRef) => {
if (!mounted.current || current.current !== accountGeneration || !host.deviceId || !ref.id || !ref.collectionId
|| ref.kind !== 'bot' || pendingAccount.current === accountGeneration) return;