1import { getAuthHeaders } from '$lib/utils';
 2
 3/**
 4 * PropsService - Server properties management
 5 *
 6 * This service handles communication with the /props endpoint to retrieve
 7 * server configuration, model information, and capabilities.
 8 *
 9 * **Responsibilities:**
10 * - Fetch server properties from /props endpoint
11 * - Handle API authentication
12 * - Parse and validate server response
13 *
14 * **Used by:**
15 * - serverStore: Primary consumer for server state management
16 */
17export class PropsService {
18	// ─────────────────────────────────────────────────────────────────────────────
19	// Fetching
20	// ─────────────────────────────────────────────────────────────────────────────
21
22	/**
23	 * Fetches server properties from the /props endpoint
24	 *
25	 * @param autoload - If false, prevents automatic model loading (default: false)
26	 * @returns {Promise<ApiLlamaCppServerProps>} Server properties
27	 * @throws {Error} If the request fails or returns invalid data
28	 */
29	static async fetch(autoload = false): Promise<ApiLlamaCppServerProps> {
30		const url = new URL('./props', window.location.href);
31		if (!autoload) {
32			url.searchParams.set('autoload', 'false');
33		}
34
35		const response = await fetch(url.toString(), {
36			headers: getAuthHeaders()
37		});
38
39		if (!response.ok) {
40			throw new Error(
41				`Failed to fetch server properties: ${response.status} ${response.statusText}`
42			);
43		}
44
45		const data = await response.json();
46		return data as ApiLlamaCppServerProps;
47	}
48
49	/**
50	 * Fetches server properties for a specific model (ROUTER mode)
51	 *
52	 * @param modelId - The model ID to fetch properties for
53	 * @param autoload - If false, prevents automatic model loading (default: false)
54	 * @returns {Promise<ApiLlamaCppServerProps>} Server properties for the model
55	 * @throws {Error} If the request fails or returns invalid data
56	 */
57	static async fetchForModel(modelId: string, autoload = false): Promise<ApiLlamaCppServerProps> {
58		const url = new URL('./props', window.location.href);
59		url.searchParams.set('model', modelId);
60		if (!autoload) {
61			url.searchParams.set('autoload', 'false');
62		}
63
64		const response = await fetch(url.toString(), {
65			headers: getAuthHeaders()
66		});
67
68		if (!response.ok) {
69			throw new Error(
70				`Failed to fetch model properties: ${response.status} ${response.statusText}`
71			);
72		}
73
74		const data = await response.json();
75		return data as ApiLlamaCppServerProps;
76	}
77}