char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Stores split refactor (#27240)521a64cd0

char/aliceai-t5-moe
1.5 KiB51 linesraw
1import { error } from '@sveltejs/kit';
2import { browser } from '$app/environment';
3import { base } from '$app/paths';
4import { HEADERS } from '$lib/constants';
5import { MimeTypeApplication } from '$lib/enums';
6import { settingsStore } from '$lib/stores/settings/index.svelte';
7
8/**
9 * Validates API key by making a request to the server props endpoint
10 * Throws SvelteKit errors for authentication failures or server issues
11 */
12export async function validateApiKey(fetch: typeof globalThis.fetch): Promise<void> {
13	if (!browser) {
14		return;
15	}
16
17	const apiKey = settingsStore.config.apiKey;
18
19	try {
20		const headers: Record<string, string> = {
21			[HEADERS.CONTENT_TYPE]: MimeTypeApplication.JSON
22		};
23
24		// Probe /props even without a stored key: on a server started with
25		// --api-key the unauthenticated request returns 401 and surfaces the
26		// API key splash, which is the onboarding path for entering the key.
27		if (apiKey) {
28			headers[HEADERS.AUTHORIZATION] = `${HEADERS.BEARER}${apiKey}`;
29		}
30
31		const response = await fetch(`${base}/props`, { headers });
32
33		if (!response.ok) {
34			if (response.status === 401 || response.status === 403) {
35				throw error(401, 'Access denied');
36			}
37
38			console.warn(`Server responded with status ${response.status} during API key validation`);
39
40			return;
41		}
42	} catch (err) {
43		// If it's already a SvelteKit error, re-throw it
44		if (err && typeof err === 'object' && 'status' in err) {
45			throw err;
46		}
47
48		// Network or other errors
49		console.warn('Cannot connect to server for API key validation:', err);
50	}
51}