Compare commits

...
1 Commits
Author SHA1 Message Date
Wenyu Chen 212c547fe9 ui: add cancel button to cancel model loading (#32225) 2026-11-23 09:27:44 +01:00
3 changed files with 41 additions and 4 deletions
@@ -31,7 +31,29 @@
<div class={['flex w-5 shrink-0 items-center justify-center', className]}>
{#if isLoading}
<MODEL_LOAD_ICONS.loading class="{ICON_CLASS_DEFAULT} animate-spin text-muted-foreground" />
<MODEL_LOAD_ICONS.loading
class="{ICON_CLASS_DEFAULT} animate-spin text-muted-foreground {showAction && revealOnHover
? 'group-hover:hidden [@media(pointer:coarse)]:hidden'
: ''}"
/>
<div
class={!showAction
? 'hidden'
: revealOnHover
? 'hidden group-hover:flex [@media(pointer:coarse)]:flex'
: 'flex'}
>
<ActionIcon
class="h-5 w-5 hover:text-foreground"
icon={MODEL_LOAD_ICONS.cancel}
iconSize="h-4 w-4"
onclick={() => modelsStore.status.cancelLoad(option.model)}
stopPropagationOnClick
tooltip="Cancel loading model"
tooltipAsTitle
/>
</div>
{:else}
<!-- the state dot is what the row shows at rest; the action takes its place on hover -->
{#if showAction && revealOnHover}
@@ -22,6 +22,7 @@ import LoadIcon from '@lucide/svelte/icons/power';
import RetryIcon from '@lucide/svelte/icons/rotate-cw';
import PromptCommandIcon from '@lucide/svelte/icons/sparkles';
import VideoIcon from '@lucide/svelte/icons/video';
import CancelLoadIcon from '@lucide/svelte/icons/x';
import { ChatFormCommandAction, FileTypeCategory, ModelCapability } from '#lib/enums/index.ts';
import type { ModelCapabilities } from '#lib/types/models.js';
import type { Component } from 'svelte';
@@ -80,6 +81,7 @@ export const MODEL_DOWNLOAD_ICONS = {
/** Icons the model load control shows for its states and actions. */
export const MODEL_LOAD_ICONS = {
cancel: CancelLoadIcon,
failed: LoadFailedIcon,
load: LoadIcon,
loading: LoadingIcon,
@@ -152,6 +152,8 @@ export class ModelStatusManager {
private listedSidecarsByRepo = $derived.by(() =>
ModelsService.draftSidecarsByRepo(this.host.routerModels)
);
/** Models whose load the user cancelled; the failure the force-kill triggers is intentional, not a load failure. */
private loadCancelRequests = new SvelteSet<string>();
private loadingStates = new SvelteMap<string, boolean>();
private loadProgress = new SvelteMap<string, ModelLoadProgress>();
/** Paused downloads with their last reported progress, or null when none arrived before the pause. */
@@ -198,8 +200,8 @@ export class ModelStatusManager {
});
// /models/sse feed state, the single source of truth for status and load progress
private statusAbort: AbortController | null = null;
private statusReaderActive = false;
private statusReaderActive = false;
private statusWaiters = new SvelteMap<
string,
{ target: ServerModelStatus; resolve: () => void; reject: (e: Error) => void }[]
@@ -269,10 +271,15 @@ export class ModelStatusManager {
this.subscribe();
// mark before the kill: the feed can report the child's death before
// this request returns, and load() must not toast it as a failure
this.loadCancelRequests.add(modelId);
try {
await ModelsService.unload(modelId);
toast.info(`Load cancelled: ${this.host.toDisplayName(modelId)}`);
} catch (error) {
this.loadCancelRequests.delete(modelId);
toast.error(`Failed to cancel load: ${this.host.toDisplayName(modelId)}`);
throw error;
@@ -516,11 +523,17 @@ export class ModelStatusManager {
toast.success(`Model loaded: ${this.host.toDisplayName(modelId)}`);
} catch (error) {
this.rejectStatus(modelId, error instanceof Error ? error : new Error('load failed'));
this.host.error = error instanceof Error ? error.message : 'Failed to load model';
toast.error(`Failed to load model: ${this.host.toDisplayName(modelId)}`);
// a user cancel force-kills the child, which settles this waiter as a
// failure; cancelLoad already reported the cancel, so stay silent
if (!this.loadCancelRequests.delete(modelId)) {
this.host.error = error instanceof Error ? error.message : 'Failed to load model';
toast.error(`Failed to load model: ${this.host.toDisplayName(modelId)}`);
}
throw error;
} finally {
this.loadCancelRequests.delete(modelId);
this.loadingStates.set(modelId, false);
}
}