From e236677e93b5efe79a2da33dfa1d91c0811f22f0 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 5 Nov 2026 16:00:05 +0900 Subject: [PATCH] refac --- src/app.d.ts | 6 ++++ src/lib/components/chat/Chat.svelte | 43 ++++++++++++++--------------- src/lib/stores/index.ts | 17 ++++++++---- src/lib/utils/spotlight.ts | 40 +++++++++++++++++++++++++++ src/lib/utils/tether.ts | 18 ++++++++++-- src/routes/+layout.svelte | 7 +---- 6 files changed, 93 insertions(+), 38 deletions(-) create mode 100644 src/lib/utils/spotlight.ts diff --git a/src/app.d.ts b/src/app.d.ts index e06ceb17ef..5e60ffcba1 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -4,6 +4,12 @@ declare global { interface Window { tether?: { version: 1; + onQuery?( + callback: (query: { + query: string; + files: { name: string; mimeType: string; dataUrl: string }[]; + }) => Promise + ): () => void; signedIn(token: string): Promise<{ token: string; expiresAt: number | null }>; refresh(force?: boolean): Promise<{ token: string; expiresAt: number | null } | null>; signedOut(): Promise; diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 29224c8811..8000f26b51 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -2878,36 +2878,33 @@ if (chatInputPrompt !== null) messageInput?.setText(chatInputPrompt); if (modelSearch) await openModelSelectorWithSearch(modelSearch); - // Consume one-shot desktop event (e.g. Spotlight query, call shortcut) - if ($desktopEvent) { + // Consume call requests or the acknowledged Tether handoff. + if ($desktopEvent && ($desktopEvent.type === 'call' || !embedded)) { const event = $desktopEvent; desktopEvent.set(null); if (event.type === 'call') { openCallOverlay(); } else if (event.type === 'query') { - const query = event.data?.query; - const eventFiles = event.data?.files; - - // Attach screenshot images from desktop (e.g. Spotlight region capture) - if (eventFiles?.length) { - for (const ef of eventFiles) { - files = [ - ...files, - { - type: 'image', - url: ef.dataUrl, - name: ef.name - } - ]; - } - } - - if (query) { - messageInput?.setTextHandler(query, () => submitHandler(prompt)); - } else if (eventFiles?.length) { + if (!event.handoff.isCurrent()) { + event.handoff.complete('The signed-in account changed. Send again from Spotlight.'); + } else if (!messageInput) { + event.handoff.complete('The WebUI composer is not ready. Send again from Spotlight.'); + } else { + // Replace the restored new-chat draft; do not mix in its old attachments. + files = event.data.files.map((file) => ({ + type: 'image', + url: file.dataUrl, + name: file.name + })); + messageInput.setText(event.data.query ?? ''); await tick(); - submitHandler(''); + if (event.handoff.isCurrent()) { + event.handoff.complete(); + void submitHandler(event.data.query ?? ''); + } else { + event.handoff.complete('The handoff expired. Send again from Spotlight.'); + } } } } else if ($page.url.searchParams.get('q')) { diff --git a/src/lib/stores/index.ts b/src/lib/stores/index.ts index d95d67beb3..1e5310f874 100644 --- a/src/lib/stores/index.ts +++ b/src/lib/stores/index.ts @@ -200,13 +200,18 @@ export const temporaryChatEnabled = writable(false); export const openTemporaryChatIds = writable>(new Set()); export const sideChatRequest = writable<{ parentId: string; prompt: string } | null>(null); -// Transient one-shot event from the desktop shell (Spotlight, drag-and-drop, etc.). -// Set by +layout.svelte, consumed and cleared by Chat.svelte. -export type DesktopEventFile = { name: string; mimeType: string; dataUrl: string }; -export type DesktopEvent = { - type: string; - data?: any; +export type SpotlightQuery = { + query: string; + files: { name: string; mimeType: string; dataUrl: string }[]; }; +// One-shot call requests and acknowledged Tether queries, consumed by Chat.svelte. +export type DesktopEvent = + | { type: 'call' } + | { + type: 'query'; + data: SpotlightQuery; + handoff: { isCurrent: () => boolean; complete: (error?: string) => void }; + }; export const desktopEvent: Writable = writable(null); export const isLastActiveTab = writable(true); diff --git a/src/lib/utils/spotlight.ts b/src/lib/utils/spotlight.ts new file mode 100644 index 0000000000..badabe01d3 --- /dev/null +++ b/src/lib/utils/spotlight.ts @@ -0,0 +1,40 @@ +import { desktopEvent, type DesktopEvent, type SpotlightQuery } from '$lib/stores'; +import { get } from 'svelte/store'; + +// Acknowledge when the composer owns the draft, not when model generation finishes. +export async function receiveSpotlightQuery( + data: SpotlightQuery, + navigate: () => Promise, + getUserId: () => string | undefined +): Promise { + const userId = getUserId(); + if (!userId) throw new Error('Sign in to WebUI, then send again. Your Spotlight draft is kept.'); + let active = true; + let timer: ReturnType; + let event: DesktopEvent; + const received = new Promise((resolve, reject) => { + event = { + type: 'query', + data, + handoff: { + isCurrent: () => active && getUserId() === userId, + complete: (error?: string) => { + if (error) reject(new Error(error)); + else resolve(); + } + } + }; + timer = setTimeout( + () => reject(new Error('The WebUI composer is not ready. Your Spotlight draft is kept.')), + 15_000 + ); + }); + desktopEvent.set(event!); + try { + await Promise.all([navigate(), received]); + } finally { + active = false; + clearTimeout(timer!); + if (get(desktopEvent) === event!) desktopEvent.set(null); + } +} diff --git a/src/lib/utils/tether.ts b/src/lib/utils/tether.ts index e0dd333b27..f01992a37b 100644 --- a/src/lib/utils/tether.ts +++ b/src/lib/utils/tether.ts @@ -2,6 +2,7 @@ import { get } from 'svelte/store'; import { goto } from '$app/navigation'; import { WEBUI_API_BASE_URL } from '$lib/constants'; import { user, socket } from '$lib/stores'; +import { receiveSpotlightQuery } from './spotlight'; let nativeFetch: typeof fetch; let renewal: Promise | undefined; @@ -63,11 +64,22 @@ export function renewTetherSession(force = false): Promise { export async function initializeTether(fetcher: typeof fetch): Promise<() => void> { nativeFetch = fetcher; - if (!window.tether) return () => {}; - const unsubscribe = window.tether.onToken((access) => { + const tether = window.tether; + if (!tether) return () => {}; + const unsubscribeToken = tether.onToken((access) => { void applyTetherToken(access).catch(() => {}); }); // Connectivity errors must not erase credentials or force a sign-out. await renewTetherSession().catch(() => {}); - return unsubscribe; + const unsubscribeQuery = tether.onQuery?.((query) => + receiveSpotlightQuery( + query, + () => goto('/'), + () => get(user)?.id + ) + ); + return () => { + unsubscribeToken(); + unsubscribeQuery?.(); + }; } diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index e2dd8db691..e07c2308fc 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1054,13 +1054,8 @@ await goto(event.data.path); return; } - if (event.type === 'query' && (event.data?.query || event.data?.files?.length)) { - desktopEvent.set(event); - await goto('/'); - return; - } if (event.type === 'call') { - desktopEvent.set(event); + desktopEvent.set({ type: 'call' }); await goto('/'); return; }