diff --git a/client/src/components/Admin/addons/useAiAddonTile.ts b/client/src/components/Admin/addons/useAiAddonTile.ts index 113bb94e9..75e909305 100644 --- a/client/src/components/Admin/addons/useAiAddonTile.ts +++ b/client/src/components/Admin/addons/useAiAddonTile.ts @@ -1,5 +1,6 @@ import { useState } from 'react'; import { useSearchParams } from 'react-router'; +import { openrouterReturnCode } from '../../Settings/ai/aiConnectModel'; import type { Addon } from './addonModel'; import { usesConnection } from './aiAddons'; @@ -11,7 +12,7 @@ import { usesConnection } from './aiAddons'; */ export function useAiAddonTile(aiAddons: Addon[]) { const [params] = useSearchParams(); - const [open, setOpen] = useState(() => params.get('ai') === 'openrouter'); + const [open, setOpen] = useState(() => openrouterReturnCode(params) !== undefined); const canSetUp = usesConnection(aiAddons); return { enabledCount: aiAddons.filter((a) => a.enabled).length, diff --git a/client/src/components/Settings/ai/aiConnectModel.test.ts b/client/src/components/Settings/ai/aiConnectModel.test.ts index 7df0d6f6e..eede6e392 100644 --- a/client/src/components/Settings/ai/aiConnectModel.test.ts +++ b/client/src/components/Settings/ai/aiConnectModel.test.ts @@ -1,4 +1,4 @@ -// FE-AI-CONNECT-MODEL-001 to FE-AI-CONNECT-MODEL-007: the pure rules behind connecting an AI provider. +// FE-AI-CONNECT-MODEL-001 to FE-AI-CONNECT-MODEL-008: the pure rules behind connecting an AI provider. import type { AiConnectionView } from '@trek/shared'; import { describe, expect, it } from 'vitest'; import { OnlineOnlyError } from '../../../repo/onlineOnly'; @@ -6,6 +6,7 @@ import { connectErrorKey, httpHostOf, isPublicHttpsUrl, + openrouterReturnCode, refusalCode, rememberOpenrouterFlow, repairModeOf, @@ -93,4 +94,14 @@ describe('aiConnectModel', () => { expect(src).not.toContain('<'); expect(decodeURIComponent(src.split(',')[1])).toBe(''); }); + + it('FE-AI-CONNECT-MODEL-008: reads the OpenRouter return with the code after an & or after a second ?', () => { + const read = (query: string) => openrouterReturnCode(new URLSearchParams(query)); + expect(read('tab=integrations&ai=openrouter&code=abc')).toBe('abc'); + expect(read('tab=integrations&ai=openrouter?code=abc')).toBe('abc'); + expect(read('tab=addons&ai=openrouter?code=abc&state=x')).toBe('abc'); + expect(read('ai=openrouter')).toBeNull(); + expect(read('tab=integrations')).toBeUndefined(); + expect(read('ai=chatgpt&code=abc')).toBeUndefined(); + }); }); diff --git a/client/src/components/Settings/ai/aiConnectModel.ts b/client/src/components/Settings/ai/aiConnectModel.ts index ed7b39265..4a7eb5137 100644 --- a/client/src/components/Settings/ai/aiConnectModel.ts +++ b/client/src/components/Settings/ai/aiConnectModel.ts @@ -99,6 +99,21 @@ export function takeOpenrouterFlow(): string | null { } } +/** + * The code an OpenRouter sign-in came back with, or `undefined` when the page + * was not opened by one. The return address already carries a query + * (`?tab=…&ai=openrouter`), and OpenRouter may add its code with a second `?` + * rather than an `&`, which leaves it inside the `ai` value + * (`ai=openrouter?code=…`); both shapes count. + */ +export function openrouterReturnCode(params: URLSearchParams): string | null | undefined { + const ai = params.get('ai'); + if (ai === null) return undefined; + const [marker, rest] = ai.split('?', 2); + if (marker !== 'openrouter') return undefined; + return params.get('code') ?? new URLSearchParams(rest ?? '').get('code'); +} + const PRIVATE_V4 = [/^10\./, /^127\./, /^169\.254\./, /^192\.168\./, /^172\.(1[6-9]|2\d|3[01])\./, /^0\./]; /** diff --git a/client/src/components/Settings/ai/useAiConnect.test.ts b/client/src/components/Settings/ai/useAiConnect.test.ts index fc7638d91..1a5f1fc2a 100644 --- a/client/src/components/Settings/ai/useAiConnect.test.ts +++ b/client/src/components/Settings/ai/useAiConnect.test.ts @@ -326,4 +326,19 @@ describe('OpenRouter', () => { await waitFor(() => expect(toast.error).toHaveBeenCalledTimes(1)); expect(repo.openrouterFinish).not.toHaveBeenCalled(); }); + + it('FE-HOOK-AI-CONNECT-012b: a code OpenRouter added with a second question mark still finishes the sign-in', async () => { + rememberOpenrouterFlow(FLOW.flowId); + const onConnected = vi.fn(); + const { result } = renderHook( + () => { + useOpenrouterReturn(onConnected); + return useLocation(); + }, + { wrapper: wrapperAt('/settings?tab=integrations&ai=openrouter?code=or-code') } + ); + await waitFor(() => expect(onConnected).toHaveBeenCalledTimes(1)); + expect(repo.openrouterFinish).toHaveBeenCalledWith({ flowId: FLOW.flowId, code: 'or-code' }); + expect(result.current.search).toBe('?tab=integrations'); + }); }); diff --git a/client/src/components/Settings/ai/useAiConnect.ts b/client/src/components/Settings/ai/useAiConnect.ts index f2005c239..497544db2 100644 --- a/client/src/components/Settings/ai/useAiConnect.ts +++ b/client/src/components/Settings/ai/useAiConnect.ts @@ -9,6 +9,7 @@ import { callbackMatchesFlow, connectErrorKey, httpHostOf, + openrouterReturnCode, refusalCode, rememberOpenrouterFlow, takeOpenrouterFlow, @@ -366,9 +367,9 @@ export function useOpenrouterReturn(onConnected: () => void) { }); useEffect(() => { - if (handled.current || params.get('ai') !== 'openrouter') return; + const code = openrouterReturnCode(params); + if (handled.current || code === undefined) return; handled.current = true; - const code = params.get('code'); setParams( (prev) => { const next = new URLSearchParams(prev);