File size: 21,669 Bytes
42ebd3d
 
a2acc77
42ebd3d
3aa90bf
 
42ebd3d
 
 
 
a2acc77
42ebd3d
3aa90bf
 
42ebd3d
 
 
 
 
 
9aaa3b5
42ebd3d
 
 
 
3aa90bf
42ebd3d
 
a203f2f
 
 
 
42ebd3d
 
9aaa3b5
 
a203f2f
 
 
 
42ebd3d
 
 
 
a2acc77
42ebd3d
 
 
 
 
 
 
 
 
 
 
 
a2acc77
42ebd3d
 
 
 
 
 
 
 
a2acc77
42ebd3d
 
a2acc77
42ebd3d
 
 
 
 
 
 
 
9aaa3b5
a2acc77
42ebd3d
 
 
 
 
a2acc77
42ebd3d
 
 
a2acc77
42ebd3d
a2acc77
42ebd3d
a2acc77
42ebd3d
 
 
 
 
a2acc77
42ebd3d
 
 
3aa90bf
42ebd3d
 
3aa90bf
42ebd3d
a2acc77
42ebd3d
a2acc77
 
 
 
 
 
 
42ebd3d
a2acc77
42ebd3d
 
 
 
 
38c0699
 
 
 
a2acc77
42ebd3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2acc77
42ebd3d
a2acc77
42ebd3d
a2acc77
 
42ebd3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2acc77
42ebd3d
 
 
 
 
 
 
 
 
 
9aaa3b5
 
 
 
182e1d2
d4c0d7d
a203f2f
 
 
 
 
 
 
 
9aaa3b5
 
42ebd3d
 
 
3aa90bf
42ebd3d
52fd9d4
42ebd3d
 
 
 
 
 
 
 
0bf6b6f
42ebd3d
 
 
 
 
 
3aa90bf
 
 
 
 
 
 
 
 
42ebd3d
 
 
 
3aa90bf
42ebd3d
 
 
 
 
 
 
fbcc1ed
42ebd3d
 
3aa90bf
42ebd3d
96020a5
42ebd3d
 
 
 
 
 
3aa90bf
42ebd3d
96020a5
42ebd3d
 
 
 
 
2fa4cfe
42ebd3d
a2acc77
42ebd3d
0bf6b6f
42ebd3d
 
3aa90bf
 
 
 
 
 
42ebd3d
 
 
a2acc77
3aa90bf
 
 
 
 
 
42ebd3d
 
 
 
 
 
3aa90bf
 
 
 
 
 
42ebd3d
 
 
 
 
 
 
 
 
3aa90bf
2d1c19b
 
 
 
 
3aa90bf
2d1c19b
 
42ebd3d
 
 
3aa90bf
42ebd3d
 
0bf6b6f
3aa90bf
 
 
 
 
42ebd3d
 
 
 
0bf6b6f
42ebd3d
 
 
 
 
3aa90bf
42ebd3d
 
 
 
 
 
 
 
0bf6b6f
49ce423
 
 
 
071fed2
49ce423
96020a5
49ce423
 
071fed2
 
 
49ce423
 
 
 
38c0699
49ce423
 
38c0699
a203f2f
96020a5
49ce423
42ebd3d
 
 
 
 
0bf6b6f
42ebd3d
 
 
 
 
 
a2acc77
0bf6b6f
 
 
42ebd3d
0bf6b6f
42ebd3d
 
 
 
0bf6b6f
a2acc77
 
42ebd3d
 
 
0bf6b6f
42ebd3d
0bf6b6f
 
3aa90bf
 
 
42ebd3d
 
0bf6b6f
 
42ebd3d
 
9aaa3b5
 
 
 
 
071fed2
 
 
9aaa3b5
 
 
 
 
 
 
 
071fed2
 
 
9aaa3b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42ebd3d
 
 
 
 
 
 
 
 
 
 
a2acc77
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
import * as RadixDialog from '@radix-ui/react-dialog';
import { motion } from 'framer-motion';
import { useState } from 'react';
import { classNames } from '~/utils/classNames';
import { DialogTitle, dialogVariants, dialogBackdropVariants } from '~/components/ui/Dialog';
import { IconButton } from '~/components/ui/IconButton';
import { providersList } from '~/lib/stores/settings';
import { db, getAll, deleteById } from '~/lib/persistence';
import { toast } from 'react-toastify';
import { useNavigate } from '@remix-run/react';
import commit from '~/commit.json';
import Cookies from 'js-cookie';
import styles from './Settings.module.scss';
import { Switch } from '~/components/ui/Switch';

interface SettingsProps {
  open: boolean;
  onClose: () => void;
}

type TabType = 'chat-history' | 'providers' | 'features' | 'debug' | 'connection';

// Providers that support base URL configuration
const URL_CONFIGURABLE_PROVIDERS = ['Ollama', 'LMStudio', 'OpenAILike'];

export const SettingsWindow = ({ open, onClose }: SettingsProps) => {
  const navigate = useNavigate();
  const [activeTab, setActiveTab] = useState<TabType>('chat-history');
  const [isDebugEnabled, setIsDebugEnabled] = useState(() => {
    const savedDebugState = Cookies.get('isDebugEnabled');
    return savedDebugState === 'true';
  });
  const [searchTerm, setSearchTerm] = useState('');
  const [isDeleting, setIsDeleting] = useState(false);
  const [githubUsername, setGithubUsername] = useState(Cookies.get('githubUsername') || '');
  const [githubToken, setGithubToken] = useState(Cookies.get('githubToken') || '');
  const [isLocalModelsEnabled, setIsLocalModelsEnabled] = useState(() => {
    const savedLocalModelsState = Cookies.get('isLocalModelsEnabled');
    return savedLocalModelsState === 'true';
  });

  // Load base URLs from cookies
  const [baseUrls, setBaseUrls] = useState(() => {
    const savedUrls = Cookies.get('providerBaseUrls');

    if (savedUrls) {
      try {
        return JSON.parse(savedUrls);
      } catch (error) {
        console.error('Failed to parse base URLs from cookies:', error);
        return {
          Ollama: 'http://localhost:11434',
          LMStudio: 'http://localhost:1234',
          OpenAILike: '',
        };
      }
    }

    return {
      Ollama: 'http://localhost:11434',
      LMStudio: 'http://localhost:1234',
      OpenAILike: '',
    };
  });

  const handleBaseUrlChange = (provider: string, url: string) => {
    setBaseUrls((prev: Record<string, string>) => {
      const newUrls = { ...prev, [provider]: url };
      Cookies.set('providerBaseUrls', JSON.stringify(newUrls));

      return newUrls;
    });
  };

  const tabs: { id: TabType; label: string; icon: string }[] = [
    { id: 'chat-history', label: 'Chat History', icon: 'i-ph:book' },
    { id: 'providers', label: 'Providers', icon: 'i-ph:key' },
    { id: 'features', label: 'Features', icon: 'i-ph:star' },
    { id: 'connection', label: 'Connection', icon: 'i-ph:link' },
    ...(isDebugEnabled ? [{ id: 'debug' as TabType, label: 'Debug Tab', icon: 'i-ph:bug' }] : []),
  ];

  // Load providers from cookies on mount
  const [providers, setProviders] = useState(() => {
    const savedProviders = Cookies.get('providers');

    if (savedProviders) {
      try {
        const parsedProviders = JSON.parse(savedProviders);

        // Merge saved enabled states with the base provider list
        return providersList.map((provider) => ({
          ...provider,
          isEnabled: parsedProviders[provider.name] || false,
        }));
      } catch (error) {
        console.error('Failed to parse providers from cookies:', error);
      }
    }

    return providersList;
  });

  const handleToggleProvider = (providerName: string, enabled: boolean) => {
    setProviders((prevProviders) => {
      const newProviders = prevProviders.map((provider) =>
        provider.name === providerName ? { ...provider, isEnabled: enabled } : provider,
      );

      // Save to cookies
      const enabledStates = newProviders.reduce(
        (acc, provider) => ({
          ...acc,
          [provider.name]: provider.isEnabled,
        }),
        {},
      );
      Cookies.set('providers', JSON.stringify(enabledStates));

      return newProviders;
    });
  };

  const filteredProviders = providers
    .filter((provider) => {
      const isLocalModelProvider = ['OpenAILike', 'LMStudio', 'Ollama'].includes(provider.name);
      return isLocalModelsEnabled || !isLocalModelProvider;
    })
    .filter((provider) => provider.name.toLowerCase().includes(searchTerm.toLowerCase()))
    .sort((a, b) => a.name.localeCompare(b.name));

  const handleCopyToClipboard = () => {
    const debugInfo = {
      OS: navigator.platform,
      Browser: navigator.userAgent,
      ActiveFeatures: providers.filter((provider) => provider.isEnabled).map((provider) => provider.name),
      BaseURLs: {
        Ollama: process.env.REACT_APP_OLLAMA_URL,
        OpenAI: process.env.REACT_APP_OPENAI_URL,
        LMStudio: process.env.REACT_APP_LM_STUDIO_URL,
      },
      Version: versionHash,
    };
    navigator.clipboard.writeText(JSON.stringify(debugInfo, null, 2)).then(() => {
      alert('Debug information copied to clipboard!');
    });
  };

  const downloadAsJson = (data: any, filename: string) => {
    const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
    const url = URL.createObjectURL(blob);
    const link = document.createElement('a');
    link.href = url;
    link.download = filename;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    URL.revokeObjectURL(url);
  };

  const handleDeleteAllChats = async () => {
    if (!db) {
      toast.error('Database is not available');
      return;
    }

    try {
      setIsDeleting(true);

      const allChats = await getAll(db);

      // Delete all chats one by one
      await Promise.all(allChats.map((chat) => deleteById(db!, chat.id)));

      toast.success('All chats deleted successfully');
      navigate('/', { replace: true });
    } catch (error) {
      toast.error('Failed to delete chats');
      console.error(error);
    } finally {
      setIsDeleting(false);
    }
  };

  const handleExportAllChats = async () => {
    if (!db) {
      toast.error('Database is not available');
      return;
    }

    try {
      const allChats = await getAll(db);
      const exportData = {
        chats: allChats,
        exportDate: new Date().toISOString(),
      };

      downloadAsJson(exportData, `all-chats-${new Date().toISOString()}.json`);
      toast.success('Chats exported successfully');
    } catch (error) {
      toast.error('Failed to export chats');
      console.error(error);
    }
  };

  const versionHash = commit.commit; // Get the version hash from commit.json

  const handleSaveConnection = () => {
    Cookies.set('githubUsername', githubUsername);
    Cookies.set('githubToken', githubToken);
    toast.success('GitHub credentials saved successfully!');
  };

  const handleToggleDebug = (enabled: boolean) => {
    setIsDebugEnabled(enabled);
    Cookies.set('isDebugEnabled', String(enabled));
  };

  const handleToggleLocalModels = (enabled: boolean) => {
    setIsLocalModelsEnabled(enabled);
    Cookies.set('isLocalModelsEnabled', String(enabled));
  };

  return (
    <RadixDialog.Root open={open}>
      <RadixDialog.Portal>
        <RadixDialog.Overlay asChild onClick={onClose}>
          <motion.div
            className="bg-black/50 fixed inset-0 z-max backdrop-blur-sm"
            initial="closed"
            animate="open"
            exit="closed"
            variants={dialogBackdropVariants}
          />
        </RadixDialog.Overlay>
        <RadixDialog.Content asChild>
          <motion.div
            className="fixed top-[50%] left-[50%] z-max h-[85vh] w-[90vw] max-w-[900px] translate-x-[-50%] translate-y-[-50%] border border-bolt-elements-borderColor rounded-lg shadow-lg focus:outline-none overflow-hidden"
            initial="closed"
            animate="open"
            exit="closed"
            variants={dialogVariants}
          >
            <div className="flex h-full">
              <div
                className={classNames(
                  'w-48 border-r border-bolt-elements-borderColor bg-bolt-elements-background-depth-1 p-4 flex flex-col justify-between',
                  styles['settings-tabs'],
                )}
              >
                <DialogTitle className="flex-shrink-0 text-lg font-semibold text-bolt-elements-textPrimary mb-2">
                  Settings
                </DialogTitle>
                {tabs.map((tab) => (
                  <button
                    key={tab.id}
                    onClick={() => setActiveTab(tab.id)}
                    className={classNames(activeTab === tab.id ? styles.active : '')}
                  >
                    <div className={tab.icon} />
                    {tab.label}
                  </button>
                ))}
                <div className="mt-auto flex flex-col gap-2">
                  <a
                    href="https://github.com/stackblitz-labs/bolt.diy"
                    target="_blank"
                    rel="noopener noreferrer"
                    className={classNames(styles['settings-button'], 'flex items-center gap-2')}
                  >
                    <div className="i-ph:github-logo" />
                    GitHub
                  </a>
                  <a
                    href="https://coleam00.github.io/bolt.new-any-llm"
                    target="_blank"
                    rel="noopener noreferrer"
                    className={classNames(styles['settings-button'], 'flex items-center gap-2')}
                  >
                    <div className="i-ph:book" />
                    Docs
                  </a>
                </div>
              </div>

              <div className="flex-1 flex flex-col p-8 pt-10 bg-bolt-elements-background-depth-2">
                <div className="flex-1 overflow-y-auto">
                  {activeTab === 'chat-history' && (
                    <div className="p-4">
                      <h3 className="text-lg font-medium text-bolt-elements-textPrimary mb-4">Chat History</h3>
                      <button
                        onClick={handleExportAllChats}
                        className={classNames(
                          'bg-bolt-elements-button-primary-background',
                          'rounded-lg px-4 py-2 mb-4 transition-colors duration-200',
                          'hover:bg-bolt-elements-button-primary-backgroundHover',
                          'text-bolt-elements-button-primary-text',
                        )}
                      >
                        Export All Chats
                      </button>

                      <div
                        className={classNames(
                          'text-bolt-elements-textPrimary rounded-lg py-4 mb-4',
                          styles['settings-danger-area'],
                        )}
                      >
                        <h4 className="font-semibold">Danger Area</h4>
                        <p className="mb-2">This action cannot be undone!</p>
                        <button
                          onClick={handleDeleteAllChats}
                          disabled={isDeleting}
                          className={classNames(
                            'bg-bolt-elements-button-danger-background',
                            'rounded-lg px-4 py-2 transition-colors duration-200',
                            isDeleting
                              ? 'opacity-50 cursor-not-allowed'
                              : 'hover:bg-bolt-elements-button-danger-backgroundHover',
                            'text-bolt-elements-button-danger-text',
                          )}
                        >
                          {isDeleting ? 'Deleting...' : 'Delete All Chats'}
                        </button>
                      </div>
                    </div>
                  )}
                  {activeTab === 'providers' && (
                    <div className="p-4">
                      <div className="flex mb-4">
                        <input
                          type="text"
                          placeholder="Search providers..."
                          value={searchTerm}
                          onChange={(e) => setSearchTerm(e.target.value)}
                          className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor"
                        />
                      </div>
                      {filteredProviders.map((provider) => (
                        <div
                          key={provider.name}
                          className="flex flex-col mb-2 provider-item hover:bg-bolt-elements-bg-depth-3 p-4 rounded-lg border border-bolt-elements-borderColor "
                        >
                          <div className="flex items-center justify-between mb-2">
                            <span className="text-bolt-elements-textPrimary">{provider.name}</span>
                            <Switch
                              className="ml-auto"
                              checked={provider.isEnabled}
                              onCheckedChange={(enabled) => handleToggleProvider(provider.name, enabled)}
                            />
                          </div>
                          {/* Base URL input for configurable providers */}
                          {URL_CONFIGURABLE_PROVIDERS.includes(provider.name) && provider.isEnabled && (
                            <div className="mt-2">
                              <label className="block text-sm text-bolt-elements-textSecondary mb-1">Base URL:</label>
                              <input
                                type="text"
                                value={baseUrls[provider.name]}
                                onChange={(e) => handleBaseUrlChange(provider.name, e.target.value)}
                                placeholder={`Enter ${provider.name} base URL`}
                                className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor"
                              />
                            </div>
                          )}
                        </div>
                      ))}
                    </div>
                  )}
                  {activeTab === 'features' && (
                    <div className="p-4 bg-bolt-elements-bg-depth-2 border border-bolt-elements-borderColor rounded-lg mb-4">
                      <div className="mb-6">
                        <h3 className="text-lg font-medium text-bolt-elements-textPrimary mb-4">Optional Features</h3>
                        <div className="flex items-center justify-between mb-2">
                          <span className="text-bolt-elements-textPrimary">Debug Info</span>
                          <Switch className="ml-auto" checked={isDebugEnabled} onCheckedChange={handleToggleDebug} />
                        </div>
                      </div>

                      <div className="mb-6 border-t border-bolt-elements-borderColor pt-4">
                        <h3 className="text-lg font-medium text-bolt-elements-textPrimary mb-4">
                          Experimental Features
                        </h3>
                        <p className="text-sm text-bolt-elements-textSecondary mb-4">
                          Disclaimer: Experimental features may be unstable and are subject to change.
                        </p>
                        <div className="flex items-center justify-between mb-2">
                          <span className="text-bolt-elements-textPrimary">Enable Local Models</span>
                          <Switch
                            className="ml-auto"
                            checked={isLocalModelsEnabled}
                            onCheckedChange={handleToggleLocalModels}
                          />
                        </div>
                      </div>
                    </div>
                  )}
                  {activeTab === 'debug' && isDebugEnabled && (
                    <div className="p-4">
                      <h3 className="text-lg font-medium text-bolt-elements-textPrimary mb-4">Debug Tab</h3>
                      <button
                        onClick={handleCopyToClipboard}
                        className="bg-blue-500 text-white rounded-lg px-4 py-2 hover:bg-blue-600 mb-4 transition-colors duration-200"
                      >
                        Copy to Clipboard
                      </button>

                      <h4 className="text-md font-medium text-bolt-elements-textPrimary">System Information</h4>
                      <p className="text-bolt-elements-textSecondary">OS: {navigator.platform}</p>
                      <p className="text-bolt-elements-textSecondary">Browser: {navigator.userAgent}</p>

                      <h4 className="text-md font-medium text-bolt-elements-textPrimary mt-4">Active Features</h4>
                      <ul>
                        {providers
                          .filter((provider) => provider.isEnabled)
                          .map((provider) => (
                            <li key={provider.name} className="text-bolt-elements-textSecondary">
                              {provider.name}
                            </li>
                          ))}
                      </ul>

                      <h4 className="text-md font-medium text-bolt-elements-textPrimary mt-4">Base URLs</h4>
                      <ul>
                        <li className="text-bolt-elements-textSecondary">Ollama: {process.env.REACT_APP_OLLAMA_URL}</li>
                        <li className="text-bolt-elements-textSecondary">OpenAI: {process.env.REACT_APP_OPENAI_URL}</li>
                        <li className="text-bolt-elements-textSecondary">
                          LM Studio: {process.env.REACT_APP_LM_STUDIO_URL}
                        </li>
                      </ul>

                      <h4 className="text-md font-medium text-bolt-elements-textPrimary mt-4">Version Information</h4>
                      <p className="text-bolt-elements-textSecondary">Version Hash: {versionHash}</p>
                    </div>
                  )}
                  {activeTab === 'connection' && (
                    <div className="p-4 mb-4 border border-bolt-elements-borderColor rounded-lg bg-bolt-elements-background-depth-3">
                      <h3 className="text-lg font-medium text-bolt-elements-textPrimary mb-4">GitHub Connection</h3>
                      <div className="flex mb-4">
                        <div className="flex-1 mr-2">
                          <label className="block text-sm text-bolt-elements-textSecondary mb-1">
                            GitHub Username:
                          </label>
                          <input
                            type="text"
                            value={githubUsername}
                            onChange={(e) => setGithubUsername(e.target.value)}
                            className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor"
                          />
                        </div>
                        <div className="flex-1">
                          <label className="block text-sm text-bolt-elements-textSecondary mb-1">
                            Personal Access Token:
                          </label>
                          <input
                            type="password"
                            value={githubToken}
                            onChange={(e) => setGithubToken(e.target.value)}
                            className="w-full bg-white dark:bg-bolt-elements-background-depth-4 relative px-2 py-1.5 rounded-md focus:outline-none placeholder-bolt-elements-textTertiary text-bolt-elements-textPrimary dark:text-bolt-elements-textPrimary border border-bolt-elements-borderColor"
                          />
                        </div>
                      </div>
                      <div className="flex mb-4">
                        <button
                          onClick={handleSaveConnection}
                          className="bg-bolt-elements-button-primary-background rounded-lg px-4 py-2 mr-2 transition-colors duration-200 hover:bg-bolt-elements-button-primary-backgroundHover text-bolt-elements-button-primary-text"
                        >
                          Save Connection
                        </button>
                      </div>
                    </div>
                  )}
                </div>
              </div>
            </div>
            <RadixDialog.Close asChild onClick={onClose}>
              <IconButton icon="i-ph:x" className="absolute top-[10px] right-[10px]" />
            </RadixDialog.Close>
          </motion.div>
        </RadixDialog.Content>
      </RadixDialog.Portal>
    </RadixDialog.Root>
  );
};