File size: 27,628 Bytes
695ff78
1e5ae4b
45e7f4a
695ff78
1e5ae4b
 
 
5d8b456
1e5ae4b
4c9164d
 
 
 
 
 
 
1e5ae4b
cc20f66
4c9164d
45e7f4a
1e5ae4b
 
695ff78
 
45e7f4a
 
 
 
 
4c9164d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a91bfdd
 
4c9164d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45e7f4a
a91bfdd
45e7f4a
 
4c9164d
 
 
 
 
 
 
45e7f4a
 
 
4c9164d
 
 
 
 
 
 
 
a91bfdd
 
 
4c9164d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
695ff78
 
4c9164d
 
 
695ff78
 
4c9164d
 
 
 
 
695ff78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c9164d
 
 
 
695ff78
4c9164d
 
 
 
 
 
 
a91bfdd
4c9164d
 
 
 
 
 
 
 
 
 
 
 
 
1e5ae4b
cc20f66
45e7f4a
 
 
 
 
 
cc20f66
45e7f4a
 
4c9164d
45e7f4a
cc20f66
 
 
45e7f4a
cc20f66
1e5ae4b
cc20f66
 
a91bfdd
 
 
695ff78
 
 
49c6642
4c9164d
 
 
 
cc20f66
 
 
4c9164d
45e7f4a
4c9164d
 
45e7f4a
 
4c9164d
45e7f4a
4c9164d
 
 
cc20f66
 
 
1e5ae4b
cc20f66
 
 
 
 
45e7f4a
 
 
 
 
 
 
 
 
 
a91bfdd
 
695ff78
 
 
 
 
45e7f4a
4c9164d
 
 
a91bfdd
 
 
 
4c9164d
 
45e7f4a
 
0120d07
45e7f4a
 
cc20f66
 
45e7f4a
 
 
a91bfdd
 
 
 
0120d07
 
45e7f4a
a91bfdd
 
cc20f66
45e7f4a
 
 
 
cc20f66
 
 
45e7f4a
cc20f66
 
695ff78
 
 
cc20f66
 
 
 
 
 
 
 
 
1e5ae4b
 
45e7f4a
 
 
 
 
 
4c9164d
1e5ae4b
a91bfdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c9164d
a91bfdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c9164d
0120d07
a91bfdd
 
 
0120d07
a91bfdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0120d07
 
a91bfdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0120d07
a91bfdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0120d07
a91bfdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c9164d
a91bfdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c9164d
a91bfdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c9164d
a91bfdd
 
4c9164d
a91bfdd
695ff78
a91bfdd
 
695ff78
1e5ae4b
 
a91bfdd
 
 
 
 
 
 
 
 
 
 
4c9164d
a91bfdd
 
4c9164d
 
a91bfdd
 
 
 
4c9164d
a91bfdd
 
 
 
4c9164d
 
a91bfdd
4c9164d
 
a91bfdd
 
 
4c9164d
 
 
 
 
 
 
a91bfdd
 
4c9164d
a91bfdd
 
 
 
 
 
 
 
 
4c9164d
 
1e5ae4b
a91bfdd
 
 
 
 
 
 
 
 
 
 
 
 
 
1e5ae4b
 
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
import { ChevronDown, Eye, EyeOff, KeyRound, Save, Trash2 } from 'lucide-react';
import type { FormEvent } from 'react';
import { useRef, useState } from 'react';
import { toast } from 'sonner';

import { ConfigError, Field } from '@/components/admin/admin-form-parts';
import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import {
    Select,
    SelectContent,
    SelectItem,
    SelectTrigger,
    SelectValue,
} from '@/components/ui/select';
import { Spinner } from '@/components/ui/spinner';
import { stringValue } from '@/lib/admin';
import { cn } from '@/lib/utils';
import { patchLlmConfig } from '@/lib/rag-client';
import type { LlmConfig, LlmForm } from '@/types/admin';

type ApiKeyMode = 'configured' | 'editing' | 'clearing';

const REASONING_EFFORT_OPTIONS: Record<string, string[]> = {
    deepseek: ['high', 'max'],
    openai: ['none', 'minimal', 'low', 'medium', 'high', 'xhigh'],
};

function SearchableSelect({
    disabled = false,
    onChange,
    options,
    placeholder = 'Pilih...',
    searchPlaceholder = 'Cari...',
    value,
}: {
    disabled?: boolean;
    onChange: (value: string) => void;
    options: string[];
    placeholder?: string;
    searchPlaceholder?: string;
    value: string;
}) {
    const [open, setOpen] = useState(false);
    const [query, setQuery] = useState('');
    const containerRef = useRef<HTMLDivElement>(null);

    const filtered = options.filter((o) =>
        o.toLowerCase().includes(query.toLowerCase()),
    );

    return (
        <div className="relative" ref={containerRef}>
            <button
                className={cn(
                    'flex h-9 w-full items-center justify-between rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background',
                    'focus:ring-2 focus:ring-ring focus:ring-offset-2 focus:outline-none',
                    disabled && 'cursor-not-allowed opacity-50',
                )}
                disabled={disabled}
                type="button"
                onClick={() => {
                    setOpen((v) => !v);
                    setQuery('');
                }}
            >
                <span className={value ? '' : 'text-muted-foreground'}>
                    {value || placeholder}
                </span>
                <ChevronDown className="size-4 shrink-0 opacity-50" />
            </button>

            {open && (
                <div
                    className="absolute z-50 mt-1 w-full rounded-md border bg-popover text-popover-foreground shadow-md"
                    onMouseDown={(e) => e.stopPropagation()}
                >
                    <div className="border-b p-1">
                        <Input
                            autoFocus
                            className="h-8 text-sm"
                            placeholder={searchPlaceholder}
                            value={query}
                            onChange={(e) => setQuery(e.target.value)}
                            onBlur={() => {
                                setTimeout(() => setOpen(false), 150);
                            }}
                        />
                    </div>
                    <div className="max-h-52 overflow-y-auto p-1">
                        {filtered.length > 0 ? (
                            filtered.map((option) => (
                                <button
                                    key={option}
                                    className={cn(
                                        'flex w-full items-center rounded-sm px-2 py-1.5 text-left text-sm hover:bg-accent hover:text-accent-foreground',
                                        value === option &&
                                            'bg-accent text-accent-foreground',
                                    )}
                                    type="button"
                                    onClick={() => {
                                        onChange(option);
                                        setOpen(false);
                                        setQuery('');
                                    }}
                                >
                                    {option}
                                </button>
                            ))
                        ) : (
                            <p className="py-6 text-center text-sm text-muted-foreground">
                                Tidak ditemukan.
                            </p>
                        )}
                    </div>
                </div>
            )}
        </div>
    );
}

function CreatableBaseUrlSelect({
    onChange,
    options,
    value,
}: {
    onChange: (value: string) => void;
    options: string[];
    value: string;
}) {
    const [customMode, setCustomMode] = useState(
        value !== '' && !options.includes(value),
    );

    const selectValue = customMode
        ? '__custom__'
        : options.includes(value)
          ? value
          : '';

    return (
        <div className="grid gap-2">
            <Select
                value={selectValue}
                onValueChange={(val) => {
                    if (val === '__custom__') {
                        setCustomMode(true);
                        if (options.includes(value)) onChange('');
                    } else {
                        setCustomMode(false);
                        onChange(val);
                    }
                }}
            >
                <SelectTrigger className="w-full">
                    <SelectValue placeholder="Pilih atau masukkan custom URL" />
                </SelectTrigger>
                <SelectContent>
                    {options.map((url) => (
                        <SelectItem key={url} value={url}>
                            {url}
                        </SelectItem>
                    ))}
                    <SelectItem value="__custom__">Custom URL...</SelectItem>
                </SelectContent>
            </Select>
            {customMode && (
                <Input
                    placeholder="https://custom-gateway.com/v1"
                    value={value}
                    onChange={(e) => onChange(e.target.value)}
                />
            )}
        </div>
    );
}

function ApiKeyInput({
    mode,
    onModeChange,
    onChange,
    value,
}: {
    mode: ApiKeyMode;
    onModeChange: (mode: ApiKeyMode) => void;
    onChange: (value: string) => void;
    value: string;
}) {
    const [show, setShow] = useState(false);

    if (mode === 'configured') {
        return (
            <div className="flex items-center gap-2">
                <div className="flex h-9 flex-1 items-center gap-2 rounded-md border border-input bg-muted/50 px-3 text-sm text-muted-foreground">
                    <KeyRound className="size-3.5 shrink-0" />
                    <span>API key tersimpan</span>
                </div>
                <Button
                    size="sm"
                    type="button"
                    variant="outline"
                    onClick={() => onModeChange('editing')}
                >
                    Ubah
                </Button>
                <Button
                    className="text-destructive hover:text-destructive"
                    size="sm"
                    type="button"
                    variant="outline"
                    onClick={() => onModeChange('clearing')}
                >
                    <Trash2 className="size-3.5" />
                </Button>
            </div>
        );
    }

    if (mode === 'clearing') {
        return (
            <div className="flex items-center gap-2">
                <div className="flex h-9 flex-1 items-center gap-2 rounded-md border border-destructive/50 bg-destructive/5 px-3 text-sm text-destructive">
                    <Trash2 className="size-3.5 shrink-0" />
                    <span>API key akan dihapus</span>
                </div>
                <Button
                    size="sm"
                    type="button"
                    variant="outline"
                    onClick={() => onModeChange('configured')}
                >
                    Batal
                </Button>
            </div>
        );
    }

    return (
        <div className="relative">
            <Input
                autoComplete="off"
                autoFocus
                className="pr-10"
                placeholder="sk-..."
                type={show ? 'text' : 'password'}
                value={value}
                onChange={(e) => onChange(e.target.value)}
            />
            <button
                className="absolute top-1/2 right-3 -translate-y-1/2 text-muted-foreground hover:text-foreground"
                type="button"
                onClick={() => setShow((v) => !v)}
            >
                {show ? (
                    <EyeOff className="size-4" />
                ) : (
                    <Eye className="size-4" />
                )}
            </button>
        </div>
    );
}

export function AdminLlmForm({ llm }: { llm?: LlmConfig | null }) {
    const cfg = llm ?? {};
    const providers = cfg.providers ?? {};
    const systemPromptsMap = cfg.system_prompts ?? {};
    const systemPromptKeys = Object.keys(systemPromptsMap);
    const initialSystemPromptKey = systemPromptKeys[0] ?? 'default';
    const initialProviderInfo = providers[cfg.active_provider ?? ''];

    const [formData, setFormData] = useState<LlmForm>({
        active_model: stringValue(cfg.active_model),
        active_provider: stringValue(cfg.active_provider),
        api_key: stringValue(cfg.api_key),
        base_url: stringValue(initialProviderInfo?.base_url),
        max_tokens: stringValue(cfg.max_tokens),
        reasoning_effort: stringValue(cfg.reasoning_effort),
        request_timeout: stringValue(cfg.request_timeout),
        system_prompt: systemPromptsMap[initialSystemPromptKey] ?? '',
        temperature: stringValue(cfg.temperature),
    });
    const [isSubmitting, setIsSubmitting] = useState(false);
    const [error, setError] = useState<string | undefined>();
    const [selectedTemplate, setSelectedTemplate] = useState<string>(
        initialSystemPromptKey,
    );
    const [apiKeyMode, setApiKeyMode] = useState<ApiKeyMode>(
        cfg.api_key_set ? 'configured' : 'editing',
    );

    function setField<K extends keyof LlmForm>(
        key: K,
        value: LlmForm[K],
    ): void {
        setFormData((prev) => ({ ...prev, [key]: value }));
    }

    function handleProviderChange(provider: string): void {
        const config = providers[provider];
        setFormData((prev) => ({
            ...prev,
            active_provider: provider,
            active_model: '',
            reasoning_effort: '',
            base_url: config?.base_url ?? '',
        }));
    }

    async function handleSubmit(
        event: FormEvent<HTMLFormElement>,
    ): Promise<void> {
        event.preventDefault();
        setIsSubmitting(true);
        setError(undefined);

        try {
            const payload: Record<string, unknown> = {};

            if (formData.active_provider) {
                payload.active_provider = formData.active_provider;
                payload.providers = {
                    [formData.active_provider]: {
                        base_url: formData.base_url || null,
                    },
                };
            }

            if (formData.active_model)
                payload.active_model = formData.active_model;
            if (apiKeyMode === 'clearing') {
                payload.api_key = null;
            } else if (apiKeyMode === 'editing' && formData.api_key) {
                payload.api_key = formData.api_key;
            }
            payload.reasoning_effort = formData.reasoning_effort || null;
            if (formData.max_tokens)
                payload.max_tokens = parseInt(formData.max_tokens, 10);
            if (formData.request_timeout)
                payload.request_timeout = parseInt(
                    formData.request_timeout,
                    10,
                );
            if (formData.temperature)
                payload.temperature = parseFloat(formData.temperature);
            if (formData.system_prompt) {
                payload.system_prompts = {
                    [selectedTemplate]: formData.system_prompt,
                };
            }

            const result = await patchLlmConfig(payload);

            const savedProviders = result.providers ?? {};
            const savedSystemPrompts = result.system_prompts ?? {};
            const savedKey =
                savedSystemPrompts[selectedTemplate] !== undefined
                    ? selectedTemplate
                    : (Object.keys(savedSystemPrompts)[0] ?? selectedTemplate);

            setSelectedTemplate(savedKey);

            const savedProviderInfo =
                savedProviders[result.active_provider ?? ''];
            setFormData({
                active_model: stringValue(result.active_model),
                active_provider: stringValue(result.active_provider),
                api_key: '',
                base_url: stringValue(savedProviderInfo?.base_url),
                max_tokens: stringValue(result.max_tokens),
                reasoning_effort: stringValue(result.reasoning_effort),
                request_timeout: stringValue(result.request_timeout),
                system_prompt: savedSystemPrompts[savedKey] ?? '',
                temperature: stringValue(result.temperature),
            });

            setApiKeyMode(result.api_key_set ? 'configured' : 'editing');
            toast.success('Konfigurasi LLM berhasil disimpan');
        } catch (e) {
            setError(
                e instanceof Error
                    ? e.message
                    : 'Gagal menyimpan konfigurasi LLM.',
            );
        } finally {
            setIsSubmitting(false);
        }
    }

    const providerConfig = providers[formData.active_provider];
    const reasoningEffortOptions =
        REASONING_EFFORT_OPTIONS[formData.active_provider] ?? [];
    const baseUrlOptions = providerConfig?.base_url
        ? [providerConfig.base_url]
        : [];

    return (
        <form
            className="grid gap-4"
            onSubmit={(e) => {
                void handleSubmit(e);
            }}
        >
            <ConfigError message={error} />

            {/* Model */}
            <Card className="border-(--lecturer-border) bg-(--lecturer-surface)">
                <CardContent className="px-6">
                    <div className="grid gap-3">
                        <h3 className="text-base font-semibold text-(--lecturer-text)">
                            Model
                        </h3>
                        <div className="grid gap-3 md:grid-cols-2">
                            <Field
                                current={llm?.active_provider}
                                hint="Penyedia layanan AI yang digunakan untuk menjawab pertanyaan."
                                label="Provider"
                            >
                                <Select
                                    value={formData.active_provider}
                                    onValueChange={handleProviderChange}
                                >
                                    <SelectTrigger className="w-full">
                                        <SelectValue placeholder="Pilih provider" />
                                    </SelectTrigger>
                                    <SelectContent>
                                        {Object.keys(providers).map((p) => (
                                            <SelectItem key={p} value={p}>
                                                {p.charAt(0).toUpperCase() +
                                                    p.slice(1)}
                                            </SelectItem>
                                        ))}
                                    </SelectContent>
                                </Select>
                            </Field>

                            <Field
                                current={llm?.active_model}
                                hint="Model AI yang digunakan. Pilih provider terlebih dahulu."
                                label="Model"
                            >
                                <SearchableSelect
                                    disabled={!formData.active_provider}
                                    options={
                                        providerConfig?.model_options ?? []
                                    }
                                    placeholder={
                                        formData.active_provider
                                            ? 'Pilih model'
                                            : 'Pilih provider dulu'
                                    }
                                    searchPlaceholder="Cari model..."
                                    value={formData.active_model}
                                    onChange={(v) =>
                                        setField('active_model', v)
                                    }
                                />
                            </Field>

                            <div className="md:col-span-2">
                                <Field
                                    hint="URL endpoint API provider. Pilih dari daftar atau masukkan custom URL."
                                    label="Base URL"
                                >
                                    <CreatableBaseUrlSelect
                                        key={formData.active_provider}
                                        options={baseUrlOptions}
                                        value={formData.base_url}
                                        onChange={(v) =>
                                            setField('base_url', v)
                                        }
                                    />
                                </Field>
                            </div>
                        </div>
                    </div>
                </CardContent>
            </Card>

            {/* Parameter */}
            <Card className="border-(--lecturer-border) bg-(--lecturer-surface)">
                <CardContent className="px-6">
                    <div className="grid gap-3">
                        <h3 className="text-base font-semibold text-(--lecturer-text)">
                            Parameter
                        </h3>
                        <div className="grid gap-3 md:grid-cols-2">
                            <Field
                                hint="Tingkat kreativitas jawaban (0–1). Rendah = konsisten, tinggi = lebih variatif."
                                label="Temperature"
                            >
                                <Input
                                    min="0"
                                    step="0.01"
                                    type="number"
                                    value={formData.temperature}
                                    onChange={(event) =>
                                        setField(
                                            'temperature',
                                            event.target.value,
                                        )
                                    }
                                />
                            </Field>

                            <Field
                                hint="Batas maksimum token yang dihasilkan per respons."
                                label="Max Tokens"
                            >
                                <Input
                                    min="1"
                                    type="number"
                                    value={formData.max_tokens}
                                    onChange={(event) =>
                                        setField(
                                            'max_tokens',
                                            event.target.value,
                                        )
                                    }
                                />
                            </Field>

                            <Field
                                hint="Batas waktu menunggu respons dari API dalam detik."
                                label="Request Timeout"
                            >
                                <Input
                                    min="1"
                                    type="number"
                                    value={formData.request_timeout}
                                    onChange={(event) =>
                                        setField(
                                            'request_timeout',
                                            event.target.value,
                                        )
                                    }
                                />
                            </Field>

                            <Field
                                hint="Tingkat usaha reasoning model. Tersedia sesuai provider yang dipilih."
                                label="Reasoning Effort"
                            >
                                <Select
                                    disabled={!formData.active_provider}
                                    value={
                                        formData.reasoning_effort || '__none__'
                                    }
                                    onValueChange={(val) =>
                                        setField(
                                            'reasoning_effort',
                                            val === '__none__' ? '' : val,
                                        )
                                    }
                                >
                                    <SelectTrigger className="w-full">
                                        <SelectValue placeholder="Tidak diatur" />
                                    </SelectTrigger>
                                    <SelectContent>
                                        <SelectItem value="__none__">
                                            Tidak diatur
                                        </SelectItem>
                                        {reasoningEffortOptions.map(
                                            (effort) => (
                                                <SelectItem
                                                    key={effort}
                                                    value={effort}
                                                >
                                                    {effort}
                                                </SelectItem>
                                            ),
                                        )}
                                    </SelectContent>
                                </Select>
                            </Field>
                        </div>
                    </div>
                </CardContent>
            </Card>

            {/* Autentikasi */}
            <Card className="border-(--lecturer-border) bg-(--lecturer-surface)">
                <CardContent className="px-6">
                    <div className="grid gap-3">
                        <h3 className="text-base font-semibold text-(--lecturer-text)">
                            Autentikasi
                        </h3>
                        <Field
                            hint="API key untuk autentikasi ke provider. Nilai disimpan terenkripsi."
                            label="API Key"
                        >
                            <ApiKeyInput
                                mode={apiKeyMode}
                                value={formData.api_key}
                                onChange={(v) => setField('api_key', v)}
                                onModeChange={setApiKeyMode}
                            />
                        </Field>
                    </div>
                </CardContent>
            </Card>

            {/* System Prompt */}
            <Card className="border-(--lecturer-border) bg-(--lecturer-surface)">
                <CardContent className="px-6">
                    <div className="grid gap-3">
                        <h3 className="text-base font-semibold text-(--lecturer-text)">
                            System Prompt
                        </h3>
                        <Field
                            hint="Pilih key system prompt yang ingin diedit. Setiap key menyimpan instruksi berbeda."
                            label="Template"
                        >
                            <Select
                                disabled={systemPromptKeys.length === 0}
                                value={selectedTemplate}
                                onValueChange={(key) => {
                                    setSelectedTemplate(key);
                                    setField(
                                        'system_prompt',
                                        systemPromptsMap[key] ?? '',
                                    );
                                }}
                            >
                                <SelectTrigger className="w-full">
                                    <SelectValue placeholder="Pilih key..." />
                                </SelectTrigger>
                                <SelectContent>
                                    {systemPromptKeys.map((key) => (
                                        <SelectItem key={key} value={key}>
                                            {key}
                                        </SelectItem>
                                    ))}
                                </SelectContent>
                            </Select>
                        </Field>

                        <Field
                            hint="Instruksi awal yang selalu dikirim ke model sebelum percakapan dimulai."
                            label="Prompt"
                        >
                            <textarea
                                className="min-h-36 w-full rounded-md border border-input px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:outline-none"
                                value={formData.system_prompt}
                                onChange={(event) => {
                                    setField(
                                        'system_prompt',
                                        event.target.value,
                                    );
                                }}
                            />
                        </Field>
                    </div>
                </CardContent>
            </Card>

            <div className="flex justify-end">
                <Button disabled={isSubmitting} type="submit">
                    {isSubmitting ? (
                        <Spinner className="size-4" />
                    ) : (
                        <Save className="size-4" />
                    )}
                    Simpan LLM
                </Button>
            </div>
        </form>
    );
}