diff --git a/src/lib/apis/configs/index.ts b/src/lib/apis/configs/index.ts index 15f32e94ac..93df4464dc 100644 --- a/src/lib/apis/configs/index.ts +++ b/src/lib/apis/configs/index.ts @@ -509,6 +509,17 @@ export const getOAuthClientAuthorizationUrl = (clientId: string, type: null | st return `${WEBUI_BASE_URL}/oauth/clients/${oauthClientId}/authorize`; }; +export const initiateOAuthRedirect = (tool: { + id: string; + serverId: string; + authType?: string | null; +}) => { + sessionStorage.setItem('pendingOAuthToolId', tool.id); + sessionStorage.setItem('oauthRedirectInProgressToolId', tool.id); + const authUrl = getOAuthClientAuthorizationUrl(tool.serverId, tool.authType ?? 'mcp'); + window.open(authUrl, '_self', 'noopener'); +}; + export const getCodeExecutionConfig = async (token: string) => { let error = null; diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 98ae358aa1..49b7ffb67a 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -97,6 +97,7 @@ import { uploadFile } from '$lib/apis/files'; import { createOpenAITextStream } from '$lib/apis/streaming'; import { getFunctions } from '$lib/apis/functions'; + import { initiateOAuthRedirect } from '$lib/apis/configs'; import { updateFolderById } from '$lib/apis/folders'; import Banner from '../common/Banner.svelte'; @@ -343,6 +344,27 @@ console.log('saveSessionSelectedModels', selectedModels, sessionStorage.selectedModels); }; + const continueOAuthRedirect = async () => { + if (pendingOAuthTools.length === 0) { + sessionStorage.removeItem('oauthRedirectInProgressToolId'); + return; + } + + if (chatIdProp) { + return; + } + + const nextTool = pendingOAuthTools[0]; + if (sessionStorage.getItem('oauthRedirectInProgressToolId') === nextTool.id) { + sessionStorage.removeItem('oauthRedirectInProgressToolId'); + return; + } + + saveSessionSelectedModels(); + await tick(); + initiateOAuthRedirect(nextTool); + }; + let oldSelectedModelIds = ['']; $: if (!equal(selectedModelIds, oldSelectedModelIds)) { onSelectedModelIdsChange(); @@ -423,6 +445,7 @@ } selectedToolIds = authed; pendingOAuthTools = unauthed; + await continueOAuthRedirect(); } else if ($settings?.tools) { selectedToolIds = $settings.tools; } else { diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 4cdabb64f9..409861f495 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -62,7 +62,7 @@ import { getSessionUser } from '$lib/apis/auths'; import { WEBUI_BASE_URL, WEBUI_API_BASE_URL, PASTED_TEXT_CHARACTER_LIMIT } from '$lib/constants'; - import { getOAuthClientAuthorizationUrl } from '$lib/apis/configs'; + import { initiateOAuthRedirect } from '$lib/apis/configs'; import { createNoteHandler } from '../notes/utils'; import { getSuggestionRenderer } from '../common/RichTextInput/suggestions'; @@ -1986,12 +1986,7 @@