Skip to content

ui: sign in with Hugging Face to share conversations as traces - #92

Closed
allozaur wants to merge 0 commit into
allozaur/feat/hf-conversation-sharingfrom
allozaur/feat/hf-oauth-sign-in
Closed

allozaur wants to merge 0 commit into
allozaur/feat/hf-conversation-sharingfrom
allozaur/feat/hf-oauth-sign-in

Conversation

@allozaur

@allozaur allozaur commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Overview

The Share to Hugging Face dialog gets a "Sign in with Hugging Face" button, so a trace can be shared without creating and pasting an access token. Token sign-in stays next to it.

Demo

Demo

Recorded at 9f91b89; click the preview for the full video.

Additional information

Stacked on ggml-org#32608 (allozaur/feat/hf-conversation-sharing).

Checked against huggingface.co without signing in: one dynamic client registration with the redirect http://192.168.1.5:8080/?hf_oauth=1 and scope openid profile write-repos manage-repos was accepted as asked (201, public client, all four scopes). The authorization URL with that client and redirect goes on to the login page, while one with a redirect the client was not registered for gets a 400, so a LAN origin works. That probe used the earlier scopes; the sign-in now asks for contribute-repos instead. A full sign-in with an account on localhost, with that scope, and a share with the token it gives are in the demo above. Not checked: whether a dataset one browser's sign-in created can be written from another browser, since each registers its own client.

Known gap: the popup, callback and BroadcastChannel handling mirrors the MCP browser sign-in (fork PR #63) instead of sharing code with it; both move to one OAuth util once both land.

How it works
  • Hugging Face supports PKCE (S256), dynamic client registration (/oauth/register) and CORS on its token and registration endpoints, so it all runs in the browser, with no server and no client secret.
  • Redirect URIs must match exactly (only http loopback hosts may change port), and llama-server runs on any host and port, so a fixed app registration would only cover localhost. Instead the app registers itself as a public client (token_endpoint_auth_method: none) for its own origin's root, /?hf_oauth=1, and keeps the client ID for the next sign-in from that origin.
  • Scopes: openid profile contribute-repos: the app can create datasets and write only to the ones it created, so the consent page no longer asks to manage every repository. Tokens that come back without contribute-repos are refused with a message. A client registered for other scopes registers again, and a share to a dataset the sign-in may not write to (a 403) says to pick a new dataset name or use an access token.
  • The authorization page opens in a popup from the click, or in this tab when the popup is blocked. The UI is served with COOP same-origin, so the popup cannot reach its opener: the page load that comes back trades the code for tokens and announces the result on a BroadcastChannel; the dialog then checks the account with whoami-v2 as with a token.
  • Tokens stay in localStorage, apart from the pasted token's key. A token close to expiry is renewed with the refresh token before a share; one that cannot be renewed signs out. Sign-out forgets the tokens and keeps the client registration. One way of signing in at a time: signing in either way drops the other's token.
  • A callback whose state does not match a pending sign-in from this browser (or is older than 15 minutes) is ignored; the callback parameters are cleared from the address before the exchange, so a reload cannot repeat it.
What changed
  • HfOAuthService: stateless client: redirect URL, PKCE pair, state, registration, authorization URL, code exchange, refresh, write-scope check.
  • huggingFaceStore: signInWithHuggingFace(popup), completeSignInFromLocation(), cancelSignInWithHf(), authMethod, isSigningInWithHf, oauthError; token refresh before a share; the pasted-token signIn is unchanged for the dialog.
  • DialogConversationShare: the button, a "finish signing in on the Hugging Face page" note with Cancel, the OAuth error, and "or use an access token" above the existing form. The read-only token warning applies to pasted tokens only.
  • Root layout: finishes a sign-in on the page load that comes back, closes the popup, and shows a toast.
Testing

In ui/apps/chat: npm run check, npm run lint and the unit tests pass.

  • hf-oauth-service.test.ts (mocked fetch): the RFC 7636 PKCE test vector, the registration body, a refused registration and an unreachable Hub, the authorization URL, the code exchange body and tokens, a refused code, refresh keeping the refresh token, the write-scope check. hf-traces-service.test.ts adds the 403 message.
  • hf-oauth-sign-in.svelte.test.ts (browser, mocked Hub): a popup sign-in through to whoami-v2, reusing the registered client, a forged state, a refused registration pointing to token sign-in, tokens without write access, a cancelled sign-in, refreshing an expired token, sign-out keeping the client.
  • By hand in the dev server: the dialog shows the button above the token form; a callback page load with error=access_denied and a pending sign-in clears the address and the pending sign-in and shows "The sign-in was cancelled.".
Demo script

Recorded with a person at the keyboard for the sign-in (#130): DEMO_GITHUB_REPO=allozaur/llama.cpp DEMO_HEADED=1 npm run demo:pr -- 92. The share is real: it creates a public dataset on the account that signs in.

test.describe(() => {
	test.use({ traces: ['agentic-debugging'] });

	test('Sign in to Hugging Face and share a trace', async ({ demo, page }) => {
		// a dataset of its own per recording: the sign-in may only write to datasets it created
		const name = `llama-chat-demo-${Date.now().toString(36)}`;

		await demo.click(page.getByLabel('Expand navigation'));
		await demo.click(page.getByRole('button', { exact: true, name: 'Fix the flaky cache tests' }));
		await expect(page.getByRole('main')).toContainText('All 48 cache tests pass now');

		await demo.click(page.getByRole('button', { name: 'Share' }).last());

		const dialog = page.getByRole('alertdialog', { name: 'Share to Hugging Face' });

		await expect(dialog).toBeVisible();
		await demo.caption('Sign in with a Hugging Face account');
		await demo.click(dialog.getByRole('button', { name: 'Sign in with Hugging Face' }));
		await demo.manual(
			'sign in to Hugging Face and authorize Llama Chat in the popup',
			dialog.getByText('Signed in as')
		);

		await demo.click(dialog.getByLabel('Whole conversation'));
		await demo.click(dialog.getByRole('button', { name: 'Dataset' }));
		await demo.click(page.getByRole('option', { name: 'New dataset' }));

		// the field suggests a name: replace it, keeping the account it starts with
		const field = dialog.getByLabel('New dataset name');
		const owner = (await field.inputValue()).split('/')[0];

		await demo.click(field, 200);
		await field.press('ControlOrMeta+a');
		await field.pressSequentially(`${owner}/${name}`, { delay: 35 });
		await demo.pause(500);
		await demo.click(dialog.getByRole('button', { exact: true, name: 'Share' }), 1200);

		const link = dialog.getByRole('link', { name: 'Open on Hugging Face' });

		await expect(link).toBeVisible({ timeout: 30000 });
		await demo.hover(link, 900);

		const href = await link.getAttribute('href');

		await demo.visit(href!, page.getByText(name).first(), 3000);
		await demo.caption('The trace, in the trace viewer on the Hub');
	});
});

Requirements

@allozaur
allozaur force-pushed the allozaur/feat/hf-oauth-sign-in branch from abfbd5d to 78ef997 Compare November 20, 2026 23:08
@allozaur
allozaur force-pushed the allozaur/feat/hf-oauth-sign-in branch from e9ed2f1 to d763f55 Compare November 23, 2026 12:03
@allozaur
allozaur force-pushed the allozaur/feat/hf-conversation-sharing branch from 440d772 to ca7c30b Compare November 23, 2026 12:05
@allozaur
allozaur force-pushed the allozaur/feat/hf-oauth-sign-in branch 2 times, most recently from ba8d5fc to 9f91b89 Compare November 24, 2026 17:37
@allozaur allozaur closed this Nov 24, 2026
@allozaur
allozaur force-pushed the allozaur/feat/hf-oauth-sign-in branch from 9f91b89 to d94b69b Compare November 24, 2026 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant