char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
521a64cd0
char/aliceai-t5-moe
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 > { 13if ( ! browser ) { 14return ; 15} 16 17const apiKey = settingsStore . config . apiKey ; 18 19try { 20const 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. 27if ( apiKey ) { 28headers [ HEADERS . AUTHORIZATION ] = ` ${ HEADERS . BEARER } ${ apiKey } ` ; 29} 30 31const response = await fetch ( ` ${ base } /props` , { headers}); 32 33if ( ! response . ok ) { 34if ( response . status === 401 || response . status === 403 ) { 35throw error ( 401 , 'Access denied' ); 36} 37 38console . warn ( `Server responded with status ${ response . status } during API key validation` ); 39 40return ; 41} 42} catch ( err ) { 43// If it's already a SvelteKit error, re-throw it 44if ( err && typeof err === 'object' && 'status' in err ) { 45throw err ; 46} 47 48// Network or other errors 49console . warn ( 'Cannot connect to server for API key validation:' , err ); 50} 51}