Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -46,6 +52,8 @@
shareError = null;
sharedUrl = null;
messages = [];

if (!huggingFaceStore.isSigningInWithHf) huggingFaceStore.oauthError = null;
});

void huggingFaceStore.restoreSession().then(() => {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -184,13 +214,50 @@
</AlertDialog.Footer>
{:else if !user}
<form class="contents" onsubmit={handleSignIn}>
<div class="flex flex-col gap-2">
<Button
disabled={isBusySigningIn}
onclick={handleSignInWithHf}
type="button"
variant="outline"
>
{#if isSigningInWithHf}
<Loader2 class="h-4 w-4 animate-spin" />
{:else}
<ModelOrgAvatar org="huggingface" size="size-4" />
{/if}
Sign in with Hugging Face
</Button>

{#if isSigningInWithHf}
<p class="text-xs text-muted-foreground">
Finish signing in on the Hugging Face page.
<button
class="underline"
onclick={() => huggingFaceStore.cancelSignInWithHf()}
type="button"
>
Cancel
</button>
</p>
{:else if huggingFaceStore.oauthError}
<p class="text-xs text-destructive">{huggingFaceStore.oauthError}</p>
{/if}
</div>

<div class="flex items-center gap-3 text-xs text-muted-foreground">
<span class="h-px flex-1 bg-border"></span>
or use an access token
<span class="h-px flex-1 bg-border"></span>
</div>

<div class="flex flex-col gap-2">
<Label for="hf-token">Hugging Face access token</Label>

<Input
bind:value={token}
autocomplete="off"
disabled={huggingFaceStore.isCheckingToken}
disabled={isBusySigningIn}
id="hf-token"
placeholder="hf_..."
type="password"
Expand All @@ -210,7 +277,7 @@
<AlertDialog.Footer>
<AlertDialog.Cancel type="button">Cancel</AlertDialog.Cancel>

<Button disabled={!token.trim() || huggingFaceStore.isCheckingToken} type="submit">
<Button disabled={!token.trim() || isBusySigningIn} type="submit">
{#if huggingFaceStore.isCheckingToken}
<Loader2 class="h-4 w-4 animate-spin" />
{/if}
Expand Down
28 changes: 28 additions & 0 deletions ui/apps/chat/src/lib/constants/hf-traces.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const HF_TRACES_DOCS_URL = `${HF_BASE_URL}/docs/hub/agent-traces`;
export const HF_NEW_TOKEN_URL = `${HF_BASE_URL}/settings/tokens/new?tokenType=write`;

export const HF_HTTP_UNAUTHORIZED = 401;
export const HF_HTTP_FORBIDDEN = 403;

export const HF_REPO_TYPE_DATASET = 'dataset';
export const HF_TRACES_REVISION = HF_MAIN_BRANCH;
Expand All @@ -35,3 +36,30 @@ export const HF_REPO_ID_REGEX = /^[\w.-]+\/[\w.-]+$/;

export const HF_TOKEN_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.huggingFaceToken`;
export const HF_TRACES_DATASET_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.huggingFaceTracesDataset`;

/**
* Signing in with a Hugging Face account: OAuth with PKCE, run in the browser.
* Reference: https://huggingface.co/docs/hub/oauth
*/
export const HF_OAUTH_AUTHORIZE_URL = `${HF_BASE_URL}/oauth/authorize`;
export const HF_OAUTH_REGISTER_URL = `${HF_BASE_URL}/oauth/register`;
export const HF_OAUTH_TOKEN_URL = `${HF_BASE_URL}/oauth/token`;
/**
* Read the account, then create datasets and commit traces to the ones the app created.
* `contribute-repos` reaches no other repository of the account.
*/
export const HF_OAUTH_SCOPES = ['openid', 'profile', 'contribute-repos'] as const;
/** The scope a share cannot do without: creating a dataset and committing a trace. */
export const HF_OAUTH_REQUIRED_SCOPE = 'contribute-repos';
/** The name the app registers under, shown on the Hugging Face authorization page. */
export const HF_OAUTH_CLIENT_NAME = 'Llama Chat';
/** Query parameter that marks a page load as a Hugging Face sign-in coming back. */
export const HF_OAUTH_CALLBACK_PARAM = 'hf_oauth';
export const HF_OAUTH_CHANNEL_NAME = 'llama-hf-oauth';
/** A sign-in that has not come back by then is dropped. */
export const HF_OAUTH_PENDING_TTL_MS = 15 * 60 * 1000;
/** Refresh an access token this long before it expires. */
export const HF_OAUTH_EXPIRY_MARGIN_MS = 60 * 1000;
export const HF_OAUTH_POPUP_NAME = 'hf-oauth';
export const HF_OAUTH_POPUP_FEATURES = 'popup,width=520,height=720';
export const HF_OAUTH_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.huggingFaceOAuth`;
6 changes: 6 additions & 0 deletions ui/apps/chat/src/lib/constants/storage.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ export const DISMISSED_RECOMMENDED_MCP_SERVERS_LOCALSTORAGE_KEY = `${STORAGE_APP
/** Key prefix for per-conversation resumable stream state, conversationId is appended */
export const STREAM_RESUME_LOCALSTORAGE_KEY_PREFIX = `${STORAGE_APP_NAME}.streamResume.`;

/**
* Key prefix for an agentic flow running in this tab, conversationId is appended. Session
* storage keeps the marker to this tab, it survives a reload of the tab and nothing else.
*/
export const AGENTIC_FLOW_SESSION_KEY_PREFIX = `${STORAGE_APP_NAME}.agenticFlow.`;

// Deprecated old key names (kept for backward compat while users migrate)
/** @deprecated Use {@link ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY} instead */
export const DEPRECATED_ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME_DEPRECATED}.alwaysAllowedTools`;
Expand Down
16 changes: 16 additions & 0 deletions ui/apps/chat/src/lib/enums/huggingface.enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,19 @@ export enum TraceShareScope {
ALL = 'all',
UP_TO_MESSAGE = 'up-to-message'
}

/** How the Hugging Face account a share uploads with was signed in. */
export enum HfAuthMethod {
/** A user access token pasted into the share dialog. */
ACCESS_TOKEN = 'access-token',
/** "Sign in with Hugging Face": OAuth in the browser. */
OAUTH = 'oauth'
}

/** How a Hugging Face sign-in that left for the authorization page comes back. */
export enum HfOAuthFlowMode {
/** In a popup, which closes itself once the tokens are in. */
POPUP = 'popup',
/** In this tab, which goes back to where it was. */
REDIRECT = 'redirect'
}
9 changes: 8 additions & 1 deletion ui/apps/chat/src/lib/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ export {
SpecialFileType
} from './files.enums';

export { HfEntryType, HfModelSort, SidecarForm, TraceShareScope } from './huggingface.enums';
export {
HfAuthMethod,
HfEntryType,
HfModelSort,
HfOAuthFlowMode,
SidecarForm,
TraceShareScope
} from './huggingface.enums';

export {
MCPConnectionPhase,
Expand Down
Loading
Loading