File size: 7,369 Bytes
82be208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<script lang="ts">
	import { KeyValuePairs } from '$lib/components/app';
	import { Input } from '$lib/components/ui/input';
	import { Switch } from '$lib/components/ui/switch';
	import { CLI_FLAGS, HEADERS, MCP_SERVER_URL_PLACEHOLDER } from '$lib/constants';
	import { UrlProtocol } from '$lib/enums';
	import { mcpStore } from '$lib/stores';
	import type { KeyValuePair } from '$lib/types';
	import { parseHeadersToArray, serializeHeaders } from '$lib/utils';

	interface Props {
		url: string;
		headers: string;
		name?: string;
		onNameChange?: (name: string) => void;
		/** Shown in the empty display name field, e.g. the current automatic label. */
		namePlaceholder?: string;
		useProxy?: boolean;
		onUrlChange: (url: string) => void;
		onHeadersChange: (headers: string) => void;
		onUseProxyChange?: (useProxy: boolean) => void;
		urlError?: string | null;
		id?: string;
		/**
		 * "Wants Authorization" is the user's *intent* to add a Bearer token
		 * (separate from `hasAuthorization` which reflects what's already in
		 * the headers). Bindable so a parent - e.g. the recommendation cards
		 * on the "Add New Server" dialog - can flip the switch on when the
		 * picked server ships a `needsAuthorization: true` flag.
		 */
		wantsAuthorization?: boolean;
		/**
		 * Marks the "Authorization" field as required. Locks the toggle so the
		 * user can't dismiss it, and visually marks the field with a red
		 * asterisk. The parent is expected to gate its submit affordance on
		 * the bearer token actually being filled. Used by the "Add New Server"
		 * dialog for recommendations whose `needsAuthorization` flag is true.
		 */
		required?: boolean;
	}

	let {
		headers,
		id = 'server',
		name = '',
		namePlaceholder = 'Name reported by the server',
		onHeadersChange,
		onNameChange,
		onUrlChange,
		onUseProxyChange,
		required = false,
		url,
		urlError = null,
		useProxy = false,
		wantsAuthorization = $bindable(false)
	}: Props = $props();

	let isWebSocket = $derived(
		url.toLowerCase().startsWith(UrlProtocol.WEBSOCKET) ||
			url.toLowerCase().startsWith(UrlProtocol.WEBSOCKET_SECURE)
	);

	let headerPairs = $derived<KeyValuePair[]>(parseHeadersToArray(headers));

	// Heuristic: this dedicated UI only owns Authorization headers that already
	// carry a Bearer scheme. Anything else (e.g. Basic, raw tokens) stays in the
	// KV section so the user can still edit those values verbatim.
	const matchesAuthorizationKey = (key: string): boolean =>
		HEADERS.REDACTED.has(key.trim().toLowerCase());

	const isBearerScheme = (value: string): boolean =>
		value.trim().toLowerCase().startsWith(HEADERS.BEARER.toLowerCase());

	const ownedByBearerUi = (p: KeyValuePair): boolean =>
		matchesAuthorizationKey(p.key) && isBearerScheme(p.value);

	let hasAuthorization = $derived(headerPairs.some(ownedByBearerUi));

	let showAuthorization = $derived(hasAuthorization || wantsAuthorization);

	let urlInput: HTMLInputElement | null = $state(null);
	let bearerInput: HTMLInputElement | null = $state(null);

	$effect(() => {
		urlInput?.focus();
	});

	$effect(() => {
		if (wantsAuthorization && bearerInput) {
			bearerInput.focus();
		}
	});

	let bearerToken = $derived.by(() => {
		const auth = headerPairs.find(ownedByBearerUi);

		if (!auth) return '';

		return auth.value.trim().slice(HEADERS.BEARER.length).trim();
	});

	$effect(() => {
		if (!headers.trim()) {
			wantsAuthorization = false;
		}
	});

	function updateHeaderPairs(newPairs: KeyValuePair[]) {
		headerPairs = newPairs;
		onHeadersChange(serializeHeaders(newPairs));
	}

	// The dedicated UI owns the Authorization slot end-to-end when the user
	// engages it: any prior Authorization row (Bearer or otherwise) is replaced
	// by exactly one { Authorization: "Bearer <token>" } entry. JSON's last-key
	// behavior would otherwise pick one arbitrarily, so we strip first.
	function updateBearerToken(token: string) {
		const filtered = headerPairs.filter((p) => !matchesAuthorizationKey(p.key));
		const trimmed = token.trim();

		if (trimmed) {
			filtered.push({ key: HEADERS.AUTHORIZATION, value: `${HEADERS.BEARER}${trimmed}` });
		}

		updateHeaderPairs(filtered);
	}

	function setUseAuthorization(checked: boolean) {
		wantsAuthorization = checked;

		if (!checked) {
			// Only drop the entry this UI owns; a non-Bearer Authorization row
			// authored in the KV section must survive a toggle off untouched.
			const filtered = headerPairs.filter((p) => !ownedByBearerUi(p));

			updateHeaderPairs(filtered);
		}
	}
</script>

<div class="grid gap-2">
	<div class="mb-4">
		<label class="mb-2 block text-xs font-medium select-none" for="server-url-{id}">
			Server URL <span class="text-destructive">*</span>
		</label>

		<Input
			bind:ref={urlInput}
			class={urlError ? 'border-destructive' : ''}
			id="server-url-{id}"
			oninput={(e) => onUrlChange(e.currentTarget.value)}
			placeholder={MCP_SERVER_URL_PLACEHOLDER}
			type="url"
			value={url}
		/>

		{#if urlError}
			<p class="mt-1.5 text-xs text-destructive">{urlError}</p>
		{/if}
	</div>

	<div class="mb-4">
		<label class="mb-2 block text-xs font-medium select-none" for="server-name-{id}">
			Display name
		</label>

		<Input
			id="server-name-{id}"
			oninput={(e) => onNameChange?.(e.currentTarget.value)}
			placeholder={namePlaceholder}
			type="text"
			value={name}
		/>
	</div>

	<label class="flex items-center gap-2 cursor-pointer select-none">
		<Switch
			checked={showAuthorization}
			disabled={required}
			id="use-authorization-{id}"
			onCheckedChange={setUseAuthorization}
		/>

		<span class="text-xs text-muted-foreground">
			Authorization{#if required}
				<span class="text-destructive">*</span>{/if}
		</span>
	</label>

	{#if showAuthorization}
		<div class="relative mt-2">
			<Input
				bind:ref={bearerInput}
				autocomplete="off"
				class="pl-16"
				id="bearer-token-{id}"
				oninput={(e) => updateBearerToken(e.currentTarget.value)}
				placeholder="Paste token here"
				type="password"
				value={bearerToken}
			/>

			<span
				class="pointer-events-none absolute inset-y-0 left-3 flex items-center text-sm font-medium text-foreground"
			>
				Bearer
			</span>
		</div>
	{/if}

	<KeyValuePairs
		addButtonLabel="Add"
		class="mt-3"
		emptyMessage="No custom headers configured."
		keyPlaceholder="Header name"
		onPairsChange={(pairs) => {
			const auth = headerPairs.find(ownedByBearerUi);

			updateHeaderPairs(auth ? [...pairs, auth] : pairs);
		}}
		pairs={headerPairs.filter((p) => !ownedByBearerUi(p))}
		sectionLabel="Custom Headers"
		sectionLabelOptional
		valuePlaceholder="Value"
	/>

	{#if !isWebSocket && onUseProxyChange}
		<label
			class={[
				'mt-3 flex items-start gap-2',
				mcpStore.isProxyAvailable && 'cursor-pointer',
				!mcpStore.isProxyAvailable && 'opacity-80'
			]}
		>
			<Switch
				checked={useProxy}
				class="mt-1"
				disabled={!mcpStore.isProxyAvailable}
				id="use-proxy-{id}"
				onCheckedChange={(checked) => onUseProxyChange?.(checked)}
			/>

			<span>
				<span class="text-xs text-muted-foreground">Use llama-server proxy</span>

				<br />

				{#if !mcpStore.isProxyAvailable}
					<span class="inline-flex gap-0.75 text-xs text-muted-foreground/60"
						>(Run <pre>llama-server</pre>
						with
						<pre>{CLI_FLAGS.MCP_PROXY}</pre>
						flag)</span
					>
				{/if}
			</span>
		</label>
	{/if}
</div>