Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(shared-task): parse titled invitations and dedupe explicit links
DavidShen9f35117bf7b3
diff --git a/apps/desktop/src/renderer/features/device-link/__tests__/SharedTaskButton.test.tsx b/apps/desktop/src/renderer/features/device-link/__tests__/SharedTaskButton.test.tsxindex fb17ab8652..257effa5fc 100644--- a/apps/desktop/src/renderer/features/device-link/__tests__/SharedTaskButton.test.tsx+++ b/apps/desktop/src/renderer/features/device-link/__tests__/SharedTaskButton.test.tsx@@ -86,6 +86,20 @@ it('distinguishes a clipboard failure from a request failure and allows retry',expect(content).toContain('copy it and open Cindy on mobile');expect(parseSharedTaskInvitation(content, 'https://relay.example.test')).toEqual({ ok: true, invitation: 'A'.repeat(43) });});+it('copies a usable invitation when the task title contains a web link', async () => {+ const copy = vi.fn().mockResolvedValue(undefined);+ Object.defineProperty(navigator, 'clipboard', { configurable: true, value: { writeText: copy } });+ const title = 'Review https://docs.example.test/page';+ const invitation = 'A'.repeat(43);+ const invitationLink = 'https://relay.example.test/shared-task/join#' + invitation;+ state.host.mockImplementation(async command => command.action === 'invite'+ ? { invitation, invitationLink } : { available: true, detail: { ...detail, title } });+ await openWindow(); click('invite');+ await waitFor(() => expect(copy).toHaveBeenCalled());+ const content = copy.mock.lastCall![0] as string;+ expect(content).toContain(title);+ expect(parseSharedTaskInvitation(content, 'https://relay.example.test')).toEqual({ ok: true, invitation });+});it('does not copy when generating an invitation fails', async () => {const copy = vi.fn(); Object.defineProperty(navigator, 'clipboard', { configurable: true, value: { writeText: copy } });state.host.mockImplementation(async c => { if (c.action === 'invite') throw new Error('[DEVICE_LINK_TIMEOUT] timeout'); return { available: true, detail }; });diff --git a/apps/mobile/src/__tests__/clipboardSharedTaskInvitation.test.tsx b/apps/mobile/src/__tests__/clipboardSharedTaskInvitation.test.tsxindex c94038f989..2a8059411a 100644--- a/apps/mobile/src/__tests__/clipboardSharedTaskInvitation.test.tsx+++ b/apps/mobile/src/__tests__/clipboardSharedTaskInvitation.test.tsx@@ -38,6 +38,34 @@ it('reads on startup and identifies the invitation as clipboard admission', asynexpect(h.read).toHaveBeenCalledTimes(1);expect(getPendingSharedTaskInvitationIntent()).toMatchObject({ invitation: token, source: 'clipboard' });});+it('recognizes an invitation message whose task title contains an ordinary URL', async () => {+ h.read.mockResolvedValue(`邀请你加入「检查 https://docs.example.test/page」\n${link}\n复制后打开 Cindy`);+ await render();+ expect(getPendingSharedTaskInvitationIntent()).toMatchObject({ invitation: token, source: 'clipboard' });+});+it.each(['before-mount', 'before-login', 'while-running'] as const)('does not reoffer an explicit invitation from the clipboard: %s', async when => {+ const intent = 'cindy://shared-session?invitation=' + token + '&server=https%3A%2F%2Frelay.example.test';+ h.read.mockResolvedValue('');+ if (when !== 'before-mount') await render(when !== 'before-login');+ await act(async () => { receiveSharedTaskInvitationIntent(intent); });+ if (when === 'before-mount') await render();+ clearSharedTaskInvitationIntent(); // Explicit admission has consumed the link.+ h.read.mockResolvedValue(link);+ await render(); await state('background'); await state('active');+ expect(getPendingSharedTaskInvitationIntent()).toBeNull();+ // Dedupe does not block explicit retry or a newly copied invitation.+ expect(receiveSharedTaskInvitationIntent(intent)).toBe(true);+ expect(getPendingSharedTaskInvitationIntent()).toMatchObject({ invitation: token, source: 'link' });+ clearSharedTaskInvitationIntent(); h.read.mockResolvedValue(link.replace(token, 'B'.repeat(43)));+ await state('background'); await state('active');+ expect(getPendingSharedTaskInvitationIntent()).toMatchObject({ invitation: 'B'.repeat(43), source: 'clipboard' });+});+it('does not let a foreign-server explicit invitation suppress the current service clipboard', async () => {+ receiveSharedTaskInvitationIntent('cindy://shared-session?invitation=' + token + '&server=https%3A%2F%2Fother.example.test');+ await render(); clearSharedTaskInvitationIntent();+ await state('background'); await state('active');+ expect(getPendingSharedTaskInvitationIntent()).toMatchObject({ invitation: token, source: 'clipboard' });+});it.each(['account', 'new-link', 'expired'] as const)('ignores confirmation after the visible clipboard invitation is superseded by %s', async reason => {await render();const id = getPendingSharedTaskInvitationIntent()!.id;diff --git a/apps/mobile/src/device-link/sharedTaskInvitationIntent.ts b/apps/mobile/src/device-link/sharedTaskInvitationIntent.tsindex b9e502a695..e0e6cfefac 100644--- a/apps/mobile/src/device-link/sharedTaskInvitationIntent.ts+++ b/apps/mobile/src/device-link/sharedTaskInvitationIntent.ts@@ -42,5 +42,5 @@ export function confirmClipboardSharedTaskInvitation(id: number): void {notify();}export const getSharedTaskInvitationIntentSequence = () => sequence;-const subscribe = (listener: () => void) => { listeners.add(listener); return () => { listeners.delete(listener); }; };-export const usePendingSharedTaskInvitationIntent = () => useSyncExternalStore(subscribe, getPendingSharedTaskInvitationIntent, getPendingSharedTaskInvitationIntent);+export const subscribeSharedTaskInvitationIntent = (listener: () => void) => { listeners.add(listener); return () => { listeners.delete(listener); }; };+export const usePendingSharedTaskInvitationIntent = () => useSyncExternalStore(subscribeSharedTaskInvitationIntent, getPendingSharedTaskInvitationIntent, getPendingSharedTaskInvitationIntent);diff --git a/apps/mobile/src/device-link/useClipboardSharedTaskInvitation.ts b/apps/mobile/src/device-link/useClipboardSharedTaskInvitation.tsindex acb69d24d3..2dbfabc66f 100644--- a/apps/mobile/src/device-link/useClipboardSharedTaskInvitation.ts+++ b/apps/mobile/src/device-link/useClipboardSharedTaskInvitation.ts@@ -1,12 +1,12 @@import { useEffect, useRef } from 'react';import { AppState, type AppStateStatus } from 'react-native';import * as Clipboard from 'expo-clipboard';-import { parseSharedTaskInvitation } from '@cindy/device-link';+import { parseSharedTaskInvitation, sharedTaskInvitationServer } from '@cindy/device-link';import { getMobileAuthOwner, isMobileAuthOwnerCurrent, subscribeMobileAuthOwner } from '@/auth/authOwnerGeneration';import { DEVICE_LINK_API_BASE_URL } from '@/config/env';import {getPendingSharedTaskInvitationIntent, getSharedTaskInvitationIntentSequence,- receiveSharedTaskInvitationIntent,+ receiveSharedTaskInvitationIntent, subscribeSharedTaskInvitationIntent,} from './sharedTaskInvitationIntent';/** Clipboard contents stay in memory. Only a valid link for this service opens admission. */@@ -14,6 +14,19 @@ export function useClipboardSharedTaskInvitation(enabled: boolean, joining: boolconst joiningRef = useRef(joining);joiningRef.current = joining;const seenInvitations = useRef(new Set<string>());+ useEffect(() => {+ const rememberExplicitInvitation = () => {+ const intent = getPendingSharedTaskInvitationIntent();+ if (intent?.source === 'link' && intent.server === sharedTaskInvitationServer(DEVICE_LINK_API_BASE_URL)) {+ seenInvitations.current.add(intent.invitation);+ }+ };+ // Observe even before login enables clipboard reads, and before admission clears+ // the pending intent. Explicit links remain usable; only clipboard offers dedupe.+ const stopWatching = subscribeSharedTaskInvitationIntent(rememberExplicitInvitation);+ rememberExplicitInvitation();+ return stopWatching;+ }, []);useEffect(() => {if (!enabled) return;let disposed = false;diff --git a/packages/device-link/src/__tests__/sharedTaskInvitation.test.ts b/packages/device-link/src/__tests__/sharedTaskInvitation.test.tsindex 8d4e21a289..c27220be7f 100644--- a/packages/device-link/src/__tests__/sharedTaskInvitation.test.ts+++ b/packages/device-link/src/__tests__/sharedTaskInvitation.test.ts@@ -25,6 +25,17 @@ describe('shared task invitation handoff', () => {expect(parseSharedTaskInvitation(link + hint, server)).toEqual({ ok: false, reason: 'invalid' });}});+ it('extracts the unique invitation when the task title contains ordinary URLs', () => {+ const message = `邀请你加入「检查 https://docs.example.test/ 和 http://example.test/page」\n${link}?app=cindycn\n复制后打开 Cindy`;+ expect(parseSharedTaskInvitation(message, server)).toEqual({ ok: true, invitation: token });+ expect(parseSharedTaskInvitation(message, 'https://other.example.test')).toEqual({ ok: false, reason: 'different-server' });+ expect(parseSharedTaskInvitation('https://docs.example.test/#' + token, server)).toEqual({ ok: false, reason: 'invalid' });+ });+ it('still rejects multiple invitations, including invalid and foreign-server candidates', () => {+ for (const other of [link.replace(token, 'B'.repeat(43)), link.replace(server, 'https://other.example.test'), link + '?app=unknown', link.replace(token, 'invalid')]) {+ expect(parseSharedTaskInvitation(`https://docs.example.test/\n${other}\n${link}`, server)).toEqual({ ok: false, reason: 'invalid' });+ }+ });it('does not extract credentials from arbitrary URLs or ambiguous invitations', () => {for (const input of [link + 'B', link + '/more', link.replace('/join#', '/join?token='), link.replace('https:', 'javascript:'),link.replace('https://', 'https://user:password@'), `${link} ${link}`, 'a'.repeat(8193)]) {diff --git a/packages/device-link/src/sharedTaskInvitation.ts b/packages/device-link/src/sharedTaskInvitation.tsindex 18f4b40d2e..ffb18ceadb 100644--- a/packages/device-link/src/sharedTaskInvitation.ts+++ b/packages/device-link/src/sharedTaskInvitation.ts@@ -35,7 +35,12 @@ export function parseSharedTaskInvitation(input: string, server: string): Sharedif (input.length > 8192) return invalid;const text = input.trim();if (TOKEN.test(text)) return { ok: true, invitation: text };- const links = text.match(/https?:\/\/[^\s<>"',。!?、()「」“”‘’\[\])]+/g);+ // Task titles may contain ordinary URLs. Count all invitation-shaped URLs (even+ // invalid or foreign-server ones) so multiple invitations remain ambiguous.+ const links = text.match(/https?:\/\/[^\s<>"',。!?、()「」“”‘’\[\])]+/g)?.filter(link => {+ try { return new URL(link.replace(/[.,;!?]+$/, '')).pathname.endsWith(SHARED_TASK_INVITATION_PATH); }+ catch { return false; }+ });if (links?.length !== 1) return invalid;try {const url = new URL(links[0].replace(/[.,;!?]+$/, ''));