-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.tsx
533 lines (494 loc) · 19.1 KB
/
index.tsx
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
'use client'
import { useEffect, useState, useCallback, ChangeEvent } from 'react'
import { openExternalUrl, AppConfiguration } from '@janhq/core'
import {
ScrollArea,
Switch,
Input,
Tooltip,
Checkbox,
useClickOutside,
} from '@janhq/joi'
import { useAtom, useAtomValue } from 'jotai'
import { ChevronDownIcon } from 'lucide-react'
import { AlertTriangleIcon, AlertCircleIcon } from 'lucide-react'
import { twMerge } from 'tailwind-merge'
import { useDebouncedCallback } from 'use-debounce'
import { snackbar, toaster } from '@/containers/Toast'
import { useActiveModel } from '@/hooks/useActiveModel'
import { useConfigurations } from '@/hooks/useConfigurations'
import { useSettings } from '@/hooks/useSettings'
import DataFolder from './DataFolder'
import FactoryReset from './FactoryReset'
import {
experimentalFeatureEnabledAtom,
ignoreSslAtom,
proxyAtom,
proxyEnabledAtom,
vulkanEnabledAtom,
quickAskEnabledAtom,
} from '@/helpers/atoms/AppConfig.atom'
type GPU = {
id: string
vram: number | null
name: string
}
/**
* Advanced Settings Screen
* @returns
*/
const Advanced = () => {
const [experimentalEnabled, setExperimentalEnabled] = useAtom(
experimentalFeatureEnabledAtom
)
const [vulkanEnabled, setVulkanEnabled] = useAtom(vulkanEnabledAtom)
const [proxyEnabled, setProxyEnabled] = useAtom(proxyEnabledAtom)
const quickAskEnabled = useAtomValue(quickAskEnabledAtom)
const [proxy, setProxy] = useAtom(proxyAtom)
const [ignoreSSL, setIgnoreSSL] = useAtom(ignoreSslAtom)
const [partialProxy, setPartialProxy] = useState<string>(proxy)
const [gpuEnabled, setGpuEnabled] = useState<boolean>(false)
const [gpuList, setGpuList] = useState<GPU[]>([])
const [gpusInUse, setGpusInUse] = useState<string[]>([])
const [dropdownOptions, setDropdownOptions] = useState<HTMLDivElement | null>(
null
)
const { configurePullOptions } = useConfigurations()
const [toggle, setToggle] = useState<HTMLDivElement | null>(null)
const { readSettings, saveSettings } = useSettings()
const { stopModel } = useActiveModel()
const [open, setOpen] = useState(false)
const selectedGpu = gpuList
.filter((x) => gpusInUse.includes(x.id))
.map((y) => {
return y['name']
})
/**
* There could be a case where the state update is not synced
* so that retrieving state value from other hooks would not be accurate
* there is also a case where state update persist everytime user type in the input
*/
const updatePullOptions = useDebouncedCallback(
() => configurePullOptions(),
300
)
/**
* Handle proxy change
*/
const onProxyChange = useCallback(
(event: ChangeEvent<HTMLInputElement>) => {
const value = event.target.value || ''
setPartialProxy(value)
if (value.trim().startsWith('http')) {
setProxy(value.trim())
} else {
setProxy('')
}
updatePullOptions()
},
[setPartialProxy, setProxy, updatePullOptions]
)
/**
* Update Quick Ask Enabled
* @param e
* @param relaunch
* @returns void
*/
const updateQuickAskEnabled = async (
e: boolean,
relaunch: boolean = true
) => {
const appConfiguration: AppConfiguration =
await window.core?.api?.getAppConfigurations()
appConfiguration.quick_ask = e
await window.core?.api?.updateAppConfiguration(appConfiguration)
if (relaunch) window.core?.api?.relaunch()
}
/**
* Update Vulkan Enabled
* @param e
* @param relaunch
* @returns void
*/
const updateVulkanEnabled = async (e: boolean, relaunch: boolean = true) => {
toaster({
title: 'Reload',
description: 'Vulkan settings updated. Reload now to apply the changes.',
})
stopModel()
setVulkanEnabled(e)
await saveSettings({ vulkan: e, gpusInUse: [] })
// Relaunch to apply settings
if (relaunch) window.location.reload()
}
/**
* Update Experimental Enabled
* @param e
* @returns
*/
const updateExperimentalEnabled = async (
e: ChangeEvent<HTMLInputElement>
) => {
setExperimentalEnabled(e.target.checked)
// If it checked, we don't need to do anything else
// Otherwise have to reset other settings
if (e.target.checked) return
// It affects other settings, so we need to reset them
const isRelaunch = quickAskEnabled || vulkanEnabled
if (quickAskEnabled) await updateQuickAskEnabled(false, false)
if (vulkanEnabled) await updateVulkanEnabled(false, false)
if (isRelaunch) window.core?.api?.relaunch()
}
/**
* useEffect to set GPU enabled if possible
*/
useEffect(() => {
const setUseGpuIfPossible = async () => {
const settings = await readSettings()
setGpuEnabled(settings.run_mode === 'gpu' && settings.gpus?.length > 0)
setGpusInUse(settings.gpus_in_use || [])
setVulkanEnabled(settings.vulkan || false)
if (settings.gpus) {
setGpuList(settings.gpus)
}
}
setUseGpuIfPossible()
}, [readSettings, setGpuList, setGpuEnabled, setGpusInUse, setVulkanEnabled])
/**
* Handle GPU Change
* @param gpuId
* @returns
*/
const handleGPUChange = async (gpuId: string) => {
let updatedGpusInUse = [...gpusInUse]
if (updatedGpusInUse.includes(gpuId)) {
updatedGpusInUse = updatedGpusInUse.filter((id) => id !== gpuId)
if (
gpuEnabled &&
updatedGpusInUse.length === 0 &&
gpuId &&
gpuId.trim()
) {
// Vulkan support only allow 1 active device at a time
if (vulkanEnabled) {
updatedGpusInUse = []
}
updatedGpusInUse.push(gpuId)
}
} else {
// Vulkan support only allow 1 active device at a time
if (vulkanEnabled) {
updatedGpusInUse = []
}
if (gpuId && gpuId.trim()) updatedGpusInUse.push(gpuId)
}
setGpusInUse(updatedGpusInUse)
await saveSettings({ gpusInUse: updatedGpusInUse.filter((e) => !!e) })
// Reload window to apply changes
// This will trigger engine servers to restart
window.location.reload()
}
const gpuSelectionPlaceHolder =
gpuList.length > 0 ? 'Select GPU' : "You don't have any compatible GPU"
/**
* Handle click outside
*/
useClickOutside(() => setOpen(false), null, [dropdownOptions, toggle])
return (
<ScrollArea className="h-full w-full px-4">
<div className="block w-full py-4">
{/* Experimental */}
<div className="flex w-full flex-col items-start justify-between gap-4 border-b border-[hsla(var(--app-border))] py-4 first:pt-0 last:border-none sm:flex-row">
<div className="flex-shrink-0 space-y-1">
<div className="flex gap-x-2">
<h6 className="font-semibold capitalize">Experimental Mode</h6>
</div>
<p className="font-medium leading-relaxed text-[hsla(var(--text-secondary))]">
Enable new features that may be unstable.
</p>
</div>
<Switch
data-testid="experimental-switch"
checked={experimentalEnabled}
onChange={updateExperimentalEnabled}
/>
</div>
{/* CPU / GPU switching */}
{!isMac && (
<div className="flex w-full flex-col items-start justify-between border-b border-[hsla(var(--app-border))] py-4 first:pt-0 last:border-none">
<div className="flex w-full items-start justify-between">
<div className="space-y-1">
<div className="flex gap-x-2">
<h6 className="font-semibold capitalize">GPU Acceleration</h6>
</div>
<p className="pr-8 leading-relaxed">
Enable to enhance model performance by utilizing your GPU
devices for acceleration. Read{' '}
<span>
{' '}
<span
className="cursor-pointer text-[var(--app-link)]"
onClick={() =>
openExternalUrl(
'https://jan.ai/guides/troubleshooting/gpu-not-used/'
)
}
>
troubleshooting guide
</span>{' '}
</span>{' '}
for further assistance.
</p>
</div>
<div className="flex items-center">
{gpuList.length > 0 && !gpuEnabled && (
<Tooltip
trigger={
<AlertCircleIcon
size={16}
className="mr-2 text-[hsla(var(--warning-bg))]"
/>
}
content="Disabling NVIDIA GPU Acceleration may result in reduced
performance. It is recommended to keep this enabled for
optimal user experience."
/>
)}
<Tooltip
trigger={
<Switch
disabled={gpuList.length === 0 || vulkanEnabled}
checked={gpuEnabled}
onChange={(e) => {
if (e.target.checked === true) {
saveSettings({ runMode: 'gpu' })
setGpuEnabled(true)
snackbar({
description:
'Successfully turned on GPU Acceleration',
type: 'success',
})
} else {
saveSettings({ runMode: 'cpu' })
setGpuEnabled(false)
snackbar({
description:
'Successfully turned off GPU Acceleration',
type: 'success',
})
}
// Stop any running model to apply the changes
if (e.target.checked !== gpuEnabled) {
stopModel().finally(() => {
setTimeout(() => {
window.location.reload()
}, 300)
})
}
}}
/>
}
content="Your current device does not have a compatible GPU for
monitoring. To enable GPU monitoring, please ensure your
device has a supported Nvidia or AMD GPU with updated
drivers."
disabled={gpuList.length > 0}
/>
</div>
</div>
<div className="mt-2 flex w-full flex-col rounded-lg px-2 py-4">
<label className="mb-2 mr-2 inline-block font-medium">
Choose device(s)
</label>
<div className="relative w-full md:w-1/2" ref={setToggle}>
<Input
value={selectedGpu.join() || ''}
className={twMerge(
'w-full cursor-pointer',
gpuList.length === 0 && 'pointer-events-none'
)}
readOnly
disabled={gpuList.length === 0}
placeholder={gpuSelectionPlaceHolder}
suffixIcon={
<ChevronDownIcon
size={14}
className={twMerge(
gpuList.length === 0 && 'pointer-events-none',
open && 'rotate-180'
)}
/>
}
onClick={() => setOpen(!open)}
/>
{gpuList.length > 0 && (
<div
className={twMerge(
'absolute right-0 top-0 z-20 mt-10 max-h-80 w-full overflow-hidden rounded-lg border border-[hsla(var(--app-border))] bg-[hsla(var(--app-bg))] shadow-sm',
open ? 'flex' : 'hidden'
)}
ref={setDropdownOptions}
>
<div className="w-full p-4">
<p>
{vulkanEnabled ? 'Vulkan Supported GPUs' : 'Nvidia'}
</p>
<div className="py-2">
<div className="rounded-lg">
{gpuList
.filter((gpu) =>
vulkanEnabled
? gpu.name
: gpu.name?.toLowerCase().includes('nvidia')
)
.map((gpu) => (
<div
key={gpu.id}
className="mt-2 flex items-center space-x-2"
>
<Checkbox
id={`gpu-${gpu.id}`}
name="gpu-nvidia"
value={gpu.id}
checked={gpusInUse.includes(gpu.id)}
onChange={() => handleGPUChange(gpu.id)}
label={
<span>
<span>{gpu.name}</span>
{!vulkanEnabled && (
<span>{gpu.vram}MB VRAM</span>
)}
</span>
}
/>
</div>
))}
</div>
{gpuEnabled && gpusInUse.length > 1 && (
<div className="mt-2 flex items-start space-x-2 text-[hsla(var(--warning-bg))]">
<AlertTriangleIcon
size={16}
className="flex-shrink-0"
/>
<p className="text-xs leading-relaxed">
If multi-GPU is enabled with different GPU models
or without NVLink, it could impact token speed.
</p>
</div>
)}
</div>
</div>
</div>
)}
</div>
</div>
</div>
)}
{/* Vulkan for AMD GPU/ APU and Intel Arc GPU */}
{!isMac && gpuList.length > 0 && experimentalEnabled && (
<div className="flex w-full flex-col items-start justify-between gap-4 border-b border-[hsla(var(--app-border))] py-4 first:pt-0 last:border-none sm:flex-row">
<div className="space-y-1">
<div className="flex gap-x-2">
<h6 className="font-semibold capitalize">Vulkan Support</h6>
</div>
<p className="font-medium leading-relaxed text-[hsla(var(--text-secondary))]">
Enable Vulkan with AMD GPU/APU and Intel Arc GPU for better
model performance (reload needed).
</p>
</div>
<div className="flex-shrink-0">
<Switch
checked={vulkanEnabled}
onChange={(e) => updateVulkanEnabled(e.target.checked)}
/>
</div>
</div>
)}
<DataFolder />
{/* Proxy */}
<div className="flex w-full flex-col items-start justify-between gap-4 border-b border-[hsla(var(--app-border))] py-4 first:pt-0 last:border-none sm:flex-row">
<div className="w-full space-y-1">
<div className="flex w-full justify-between gap-x-2">
<h6 className="font-semibold capitalize">HTTPS Proxy</h6>
</div>
<p className="font-medium leading-relaxed text-[hsla(var(--text-secondary))]">
Specify the HTTPS proxy or leave blank (proxy auto-configuration
and SOCKS not supported).
</p>
</div>
<div className="flex w-full flex-shrink-0 flex-col items-end gap-2 pr-1 sm:w-1/2">
<Switch
data-testid="proxy-switch"
checked={proxyEnabled}
onChange={() => {
setProxyEnabled(!proxyEnabled)
updatePullOptions()
}}
/>
<div className="w-full">
<Input
data-testid="proxy-input"
placeholder={'http://<user>:<password>@<domain or IP>:<port>'}
value={partialProxy}
onChange={onProxyChange}
/>
</div>
</div>
</div>
{/* Ignore SSL certificates */}
<div className="flex w-full flex-col items-start justify-between gap-4 border-b border-[hsla(var(--app-border))] py-4 first:pt-0 last:border-none sm:flex-row">
<div className="flex-shrink-0 space-y-1">
<div className="flex gap-x-2">
<h6 className="font-semibold capitalize">
Ignore SSL certificates
</h6>
</div>
<p className="font-medium leading-relaxed text-[hsla(var(--text-secondary))]">
Allow self-signed or unverified certificates - may be required for
certain proxies.
</p>
</div>
<Switch
data-testid="ignore-ssl-switch"
checked={ignoreSSL}
onChange={(e) => {
setIgnoreSSL(e.target.checked)
updatePullOptions()
}}
/>
</div>
{experimentalEnabled && (
<div className="flex w-full flex-col items-start justify-between gap-4 border-b border-[hsla(var(--app-border))] py-4 first:pt-0 last:border-none sm:flex-row">
<div className="flex-shrink-0 space-y-1">
<div className="flex gap-x-2">
<h6 className="font-semibold capitalize">Jan Quick Ask</h6>
</div>
<p className="font-medium leading-relaxed text-[hsla(var(--text-secondary))]">
Enable Quick Ask to be triggered via the default hotkey{' '}
<span className="text-[hsla(var(--text-secondary)] bg-secondary inline-flex items-center justify-center rounded-full px-1 py-0.5 text-xs font-bold">
<span className="font-bold">{isMac ? '⌘' : 'Ctrl'} + J</span>
</span>{' '}
(reload needed).
</p>
</div>
<Switch
data-testid="quick-ask-switch"
checked={quickAskEnabled}
onChange={() => {
toaster({
title: 'Reload',
description:
'Quick Ask settings updated. Reload now to apply the changes.',
})
updateQuickAskEnabled(!quickAskEnabled)
}}
/>
</div>
)}
{/* Factory Reset */}
<FactoryReset />
</div>
</ScrollArea>
)
}
export default Advanced