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
2.5 KiB77 linesraw
1import { redactValue } from './redact';
2import { CORS_PROXY, HEADERS } from '$lib/constants';
3import { MimeTypeApplication } from '$lib/enums';
4import { settingsStore } from '$lib/stores/settings/index.svelte';
5
6/**
7 * Get authorization headers for API requests
8 * Includes Bearer token if API key is configured
9 */
10export function getAuthHeaders(): Record<string, string> {
11	const currentConfig = settingsStore.config;
12	const apiKey = currentConfig.apiKey?.toString().trim();
13
14	return apiKey ? { [HEADERS.AUTHORIZATION]: `${HEADERS.BEARER}${apiKey}` } : {};
15}
16
17/**
18 * Get standard JSON headers with optional authorization
19 */
20export function getJsonHeaders(): Record<string, string> {
21	return {
22		[HEADERS.CONTENT_TYPE]: MimeTypeApplication.JSON,
23		...getAuthHeaders()
24	};
25}
26
27/**
28 * Sanitize HTTP headers by redacting sensitive values.
29 * Known sensitive headers (from HEADERS.REDACTED) and any extra headers
30 * specified by the caller are fully redacted. Headers listed in
31 * `partialRedactHeaders` are partially redacted, showing only the
32 * specified number of trailing characters.
33 *
34 * @param headers - Headers to sanitize
35 * @param extraRedactedHeaders - Additional header names to fully redact
36 * @param partialRedactHeaders - Map of header name -> number of trailing chars to keep visible
37 * @returns Object with header names as keys and (possibly redacted) values
38 */
39export function sanitizeHeaders(
40	headers?: HeadersInit,
41	extraRedactedHeaders?: Iterable<string>,
42	partialRedactHeaders?: Map<string, number>
43): Record<string, string> {
44	if (!headers) {
45		return {};
46	}
47
48	const normalized = new Headers(headers);
49	const sanitized: Record<string, string> = {};
50	const redactedHeaders = new Set(
51		Array.from(extraRedactedHeaders ?? [], (header) => header.toLowerCase())
52	);
53
54	for (const [key, value] of normalized.entries()) {
55		const normalizedKey = key.toLowerCase();
56		const unproxiedKey = normalizedKey.startsWith(CORS_PROXY.HEADER_PREFIX)
57			? normalizedKey.slice(CORS_PROXY.HEADER_PREFIX.length)
58			: normalizedKey;
59		const partialChars =
60			partialRedactHeaders?.get(normalizedKey) ?? partialRedactHeaders?.get(unproxiedKey);
61
62		if (partialChars !== undefined) {
63			sanitized[key] = redactValue(value, partialChars);
64		} else if (
65			HEADERS.REDACTED.has(normalizedKey) ||
66			HEADERS.REDACTED.has(unproxiedKey) ||
67			redactedHeaders.has(normalizedKey) ||
68			redactedHeaders.has(unproxiedKey)
69		) {
70			sanitized[key] = redactValue(value);
71		} else {
72			sanitized[key] = value;
73		}
74	}
75
76	return sanitized;
77}