mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
fix(assistant): finish an OpenRouter sign-in whose code came back after a second question mark
The return address already carries a query, so a code appended with ? instead of & ended up inside the ai marker and the page did nothing, without a word.
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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('<svg><path d="M0 0"/></svg>');
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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\./];
|
||||
|
||||
/**
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user