char-fork/llama.cpp

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

Aleksander Grygierui: Restructure repo to use `tools/ui` folder and `ui` / `UI` / `llama-ui` / `LLAMA_UI` naming (#23064)59778f019

char/aliceai-t5-moe
2.5 KiB111 linesraw
1/**
2 * HTTP request inspection utilities for diagnostic logging.
3 * These helpers extract metadata from fetch-style request arguments
4 * without exposing sensitive payload data.
5 */
6
7export interface RequestBodySummary {
8	kind: string;
9	size?: number;
10}
11
12export function getRequestUrl(input: RequestInfo | URL): string {
13	if (typeof input === 'string') {
14		return input;
15	}
16
17	if (input instanceof URL) {
18		return input.href;
19	}
20
21	return input.url;
22}
23
24export function getRequestMethod(
25	input: RequestInfo | URL,
26	init?: RequestInit,
27	baseInit?: RequestInit
28): string {
29	if (init?.method) {
30		return init.method;
31	}
32
33	if (typeof Request !== 'undefined' && input instanceof Request) {
34		return input.method;
35	}
36
37	return baseInit?.method ?? 'GET';
38}
39
40export function getRequestBody(
41	input: RequestInfo | URL,
42	init?: RequestInit
43): BodyInit | null | undefined {
44	if (init?.body !== undefined) {
45		return init.body;
46	}
47
48	if (typeof Request !== 'undefined' && input instanceof Request) {
49		return input.body;
50	}
51
52	return undefined;
53}
54
55export function summarizeRequestBody(body: BodyInit | null | undefined): RequestBodySummary {
56	if (body == null) {
57		return { kind: 'empty' };
58	}
59
60	if (typeof body === 'string') {
61		return { kind: 'string', size: body.length };
62	}
63
64	if (body instanceof Blob) {
65		return { kind: 'blob', size: body.size };
66	}
67
68	if (body instanceof URLSearchParams) {
69		return { kind: 'urlsearchparams', size: body.toString().length };
70	}
71
72	if (body instanceof FormData) {
73		return { kind: 'formdata' };
74	}
75
76	if (body instanceof ArrayBuffer) {
77		return { kind: 'arraybuffer', size: body.byteLength };
78	}
79
80	if (ArrayBuffer.isView(body)) {
81		return { kind: body.constructor.name, size: body.byteLength };
82	}
83
84	return { kind: typeof body };
85}
86
87export function formatDiagnosticErrorMessage(error: unknown): string {
88	const message = error instanceof Error ? error.message : String(error);
89
90	return message.includes('Failed to fetch') ? `${message} (check CORS?)` : message;
91}
92
93export function extractJsonRpcMethods(body: BodyInit | null | undefined): string[] | undefined {
94	if (typeof body !== 'string') {
95		return undefined;
96	}
97
98	try {
99		const parsed = JSON.parse(body);
100		const messages = Array.isArray(parsed) ? parsed : [parsed];
101		const methods = messages
102			.map((message: Record<string, unknown>) =>
103				typeof message?.method === 'string' ? (message.method as string) : undefined
104			)
105			.filter((method: string | undefined): method is string => Boolean(method));
106
107		return methods.length > 0 ? methods : undefined;
108	} catch {
109		return undefined;
110	}
111}