From 78ef997ebb347e9f34494b1c42ea8961ef91fe46 Mon Sep 17 00:00:00 2001 From: Aleksander Grygier Date: Thu, 19 Nov 2026 17:12:09 +0100 Subject: [PATCH 1/3] ui: sign in to Hugging Face with the browser to share traces The share dialog gets a "Sign in with Hugging Face" button next to the access-token form. It runs OAuth with PKCE in the browser: the app registers itself with Hugging Face for the redirect URL of the origin it is served from, opens the authorization page in a popup (or this tab when the popup is blocked), and the page that comes back trades the code for tokens and tells the other tabs on a BroadcastChannel. Tokens stay in localStorage and are renewed with the refresh token before they expire. When Hugging Face turns the registration down or grants no write access, the dialog says so and points to signing in with an access token. Assisted-by: Claude (Opus 5.5 Medium) --- .../dialogs/DialogConversationShare.svelte | 77 ++++- .../src/lib/constants/hf-traces.constants.ts | 24 ++ .../chat/src/lib/enums/huggingface.enums.ts | 16 + ui/apps/chat/src/lib/enums/index.ts | 9 +- .../chat/src/lib/services/hf-oauth.service.ts | 217 ++++++++++++ ui/apps/chat/src/lib/services/index.ts | 9 + .../chat/src/lib/stores/huggingface.svelte.ts | 324 +++++++++++++++++- ui/apps/chat/src/lib/types/huggingface.d.ts | 42 ++- ui/apps/chat/src/lib/types/index.ts | 4 + ui/apps/chat/src/routes/+layout.svelte | 31 +- .../client/hf-oauth-sign-in.svelte.test.ts | 199 +++++++++++ .../chat/tests/unit/hf-oauth-service.test.ts | 172 ++++++++++ 12 files changed, 1102 insertions(+), 22 deletions(-) create mode 100644 ui/apps/chat/src/lib/services/hf-oauth.service.ts create mode 100644 ui/apps/chat/tests/client/hf-oauth-sign-in.svelte.test.ts create mode 100644 ui/apps/chat/tests/unit/hf-oauth-service.test.ts diff --git a/ui/apps/chat/src/lib/components/app/dialogs/DialogConversationShare.svelte b/ui/apps/chat/src/lib/components/app/dialogs/DialogConversationShare.svelte index b8ab9600b32e..c51eeb680c66 100644 --- a/ui/apps/chat/src/lib/components/app/dialogs/DialogConversationShare.svelte +++ b/ui/apps/chat/src/lib/components/app/dialogs/DialogConversationShare.svelte @@ -7,8 +7,14 @@ import { Label } from '@ggml-org/llama-ui/ui/label'; import * as RadioGroup from '@ggml-org/llama-ui/ui/radio-group'; import { Check, Copy, ExternalLink, Loader2, Share2, TriangleAlert } from '@lucide/svelte'; - import { HF_NEW_TOKEN_URL, HF_REPO_ID_REGEX, HF_TRACES_DOCS_URL } from '#lib/constants/index.ts'; - import { TraceShareScope } from '#lib/enums/index.ts'; + import { + HF_NEW_TOKEN_URL, + HF_OAUTH_POPUP_FEATURES, + HF_OAUTH_POPUP_NAME, + HF_REPO_ID_REGEX, + HF_TRACES_DOCS_URL + } from '#lib/constants/index.ts'; + import { HfAuthMethod, TraceShareScope } from '#lib/enums/index.ts'; import { conversationsStore, huggingFaceStore, uiStore } from '#lib/stores/index.ts'; import { copyToClipboard, filterByLeafNodeId } from '#lib/utils/index.ts'; import { untrack } from 'svelte'; @@ -46,6 +52,8 @@ shareError = null; sharedUrl = null; messages = []; + + if (!huggingFaceStore.isSigningInWithHf) huggingFaceStore.oauthError = null; }); void huggingFaceStore.restoreSession().then(() => { @@ -57,6 +65,15 @@ }); }); + // a sign-in that finished elsewhere (a popup, another tab) suggests the account's dataset + $effect(() => { + if (!user) return; + + untrack(() => { + if (!dataset) dataset = huggingFaceStore.suggestedDataset; + }); + }); + let leafId = $derived( scope === TraceShareScope.UP_TO_MESSAGE && options?.messageId ? options.messageId @@ -74,7 +91,11 @@ let hasAttachments = $derived(tracePath.some((message) => !!message.extra?.length)); let user = $derived(huggingFaceStore.user); - let isReadOnlyToken = $derived(user?.tokenRole === 'read'); + let isReadOnlyToken = $derived( + huggingFaceStore.authMethod === HfAuthMethod.ACCESS_TOKEN && user?.tokenRole === 'read' + ); + let isSigningInWithHf = $derived(huggingFaceStore.isSigningInWithHf); + let isBusySigningIn = $derived(huggingFaceStore.isCheckingToken || isSigningInWithHf); let isDatasetValid = $derived(HF_REPO_ID_REGEX.test(dataset.trim())); let canShare = $derived( !!user && !isReadOnlyToken && isDatasetValid && tracePath.length > 0 && !isSharing @@ -104,6 +125,15 @@ } } + function handleSignInWithHf() { + signInError = null; + + // opened in the click itself: a popup opened after an await is blocked + const popup = window.open('about:blank', HF_OAUTH_POPUP_NAME, HF_OAUTH_POPUP_FEATURES); + + void huggingFaceStore.signInWithHuggingFace(popup); + } + function handleSignOut() { huggingFaceStore.signOut(); } @@ -184,13 +214,50 @@ {:else if !user}
+
+ + + {#if isSigningInWithHf} +

+ Finish signing in on the Hugging Face page. + +

+ {:else if huggingFaceStore.oauthError} +

{huggingFaceStore.oauthError}

+ {/if} +
+ +
+ + or use an access token + +
+
Cancel -