mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-24 14:30:20 -05:00
refac
This commit is contained in:
1 parent
6263394835
commit
e236677e93
6 files changed
+93
-38
No files matched your search
Vendored
+6
@@ -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>
|
||||
): () => void;
|
||||
signedIn(token: string): Promise<{ token: string; expiresAt: number | null }>;
|
||||
refresh(force?: boolean): Promise<{ token: string; expiresAt: number | null } | null>;
|
||||
signedOut(): Promise<void>;
|
||||
|
||||
@@ -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')) {
|
||||
|
||||
+11
-6
@@ -200,13 +200,18 @@ export const temporaryChatEnabled = writable(false);
|
||||
export const openTemporaryChatIds = writable<Set<string>>(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<DesktopEvent | null> = writable(null);
|
||||
|
||||
export const isLastActiveTab = writable(true);
|
||||
|
||||
@@ -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<void>,
|
||||
getUserId: () => string | undefined
|
||||
): Promise<void> {
|
||||
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<typeof setTimeout>;
|
||||
let event: DesktopEvent;
|
||||
const received = new Promise<void>((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);
|
||||
}
|
||||
}
|
||||
+15
-3
@@ -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<boolean> | undefined;
|
||||
@@ -63,11 +64,22 @@ export function renewTetherSession(force = false): Promise<boolean> {
|
||||
|
||||
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?.();
|
||||
};
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user