File size: 10,016 Bytes
2a8cee5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState } from 'react';
import { useAppContext } from '../utils/app.context';
import { CONFIG_DEFAULT, CONFIG_INFO } from '../Config';
import { isDev } from '../Config';
import StorageUtils from '../utils/storage';

type SettKey = keyof typeof CONFIG_DEFAULT;

const COMMON_SAMPLER_KEYS: SettKey[] = [
  'temperature',
  'top_k',
  'top_p',
  'min_p',
  'max_tokens',
];
const OTHER_SAMPLER_KEYS: SettKey[] = [
  'dynatemp_range',
  'dynatemp_exponent',
  'typical_p',
  'xtc_probability',
  'xtc_threshold',
];
const PENALTY_KEYS: SettKey[] = [
  'repeat_last_n',
  'repeat_penalty',
  'presence_penalty',
  'frequency_penalty',
  'dry_multiplier',
  'dry_base',
  'dry_allowed_length',
  'dry_penalty_last_n',
];

export default function SettingDialog({
  show,
  onClose,
}: {
  show: boolean;
  onClose: () => void;
}) {
  const { config, saveConfig } = useAppContext();

  // clone the config object to prevent direct mutation
  const [localConfig, setLocalConfig] = useState<typeof CONFIG_DEFAULT>(
    JSON.parse(JSON.stringify(config))
  );

  const resetConfig = () => {
    if (window.confirm('Are you sure to reset all settings?')) {
      setLocalConfig(CONFIG_DEFAULT);
    }
  };

  const handleSave = () => {
    saveConfig(localConfig);
    onClose();
  };

  const debugImportDemoConv = async () => {
    const res = await fetch('/demo-conversation.json');
    const demoConv = await res.json();
    StorageUtils.remove(demoConv.id);
    for (const msg of demoConv.messages) {
      StorageUtils.appendMsg(demoConv.id, msg);
    }
    onClose();
  };

  return (
    <dialog className={`modal ${show ? 'modal-open' : ''}`}>
      <div className="modal-box">
        <h3 className="text-lg font-bold mb-6">Settings</h3>
        <div className="h-[calc(90vh-12rem)] overflow-y-auto">
          <p className="opacity-40 mb-6">
            Settings below are saved in browser's localStorage
          </p>

          <SettingsModalShortInput
            configKey="apiKey"
            configDefault={CONFIG_DEFAULT}
            value={localConfig.apiKey}
            onChange={(value) =>
              setLocalConfig({ ...localConfig, apiKey: value })
            }
          />

          <label className="form-control mb-2">
            <div className="label">
              System Message (will be disabled if left empty)
            </div>
            <textarea
              className="textarea textarea-bordered h-24"
              placeholder={`Default: ${CONFIG_DEFAULT.systemMessage}`}
              value={localConfig.systemMessage}
              onChange={(e) =>
                setLocalConfig({
                  ...localConfig,
                  systemMessage: e.target.value,
                })
              }
            />
          </label>

          {COMMON_SAMPLER_KEYS.map((key) => (
            <SettingsModalShortInput
              key={key}
              configKey={key}
              configDefault={CONFIG_DEFAULT}
              value={localConfig[key]}
              onChange={(value) =>
                setLocalConfig({ ...localConfig, [key]: value })
              }
            />
          ))}

          <details className="collapse collapse-arrow bg-base-200 mb-2 overflow-visible">
            <summary className="collapse-title font-bold">
              Other sampler settings
            </summary>
            <div className="collapse-content">
              <SettingsModalShortInput
                label="Samplers queue"
                configKey="samplers"
                configDefault={CONFIG_DEFAULT}
                value={localConfig.samplers}
                onChange={(value) =>
                  setLocalConfig({ ...localConfig, samplers: value })
                }
              />
              {OTHER_SAMPLER_KEYS.map((key) => (
                <SettingsModalShortInput
                  key={key}
                  configKey={key}
                  configDefault={CONFIG_DEFAULT}
                  value={localConfig[key]}
                  onChange={(value) =>
                    setLocalConfig({ ...localConfig, [key]: value })
                  }
                />
              ))}
            </div>
          </details>

          <details className="collapse collapse-arrow bg-base-200 mb-2 overflow-visible">
            <summary className="collapse-title font-bold">
              Penalties settings
            </summary>
            <div className="collapse-content">
              {PENALTY_KEYS.map((key) => (
                <SettingsModalShortInput
                  key={key}
                  configKey={key}
                  configDefault={CONFIG_DEFAULT}
                  value={localConfig[key]}
                  onChange={(value) =>
                    setLocalConfig({ ...localConfig, [key]: value })
                  }
                />
              ))}
            </div>
          </details>

          <details className="collapse collapse-arrow bg-base-200 mb-2 overflow-visible">
            <summary className="collapse-title font-bold">
              Reasoning models
            </summary>
            <div className="collapse-content">
              <div className="flex flex-row items-center mb-2">
                <input
                  type="checkbox"
                  className="checkbox"
                  checked={localConfig.showThoughtInProgress}
                  onChange={(e) =>
                    setLocalConfig({
                      ...localConfig,
                      showThoughtInProgress: e.target.checked,
                    })
                  }
                />
                <span className="ml-4">
                  Expand though process by default for generating message
                </span>
              </div>
              <div className="flex flex-row items-center mb-2">
                <input
                  type="checkbox"
                  className="checkbox"
                  checked={localConfig.excludeThoughtOnReq}
                  onChange={(e) =>
                    setLocalConfig({
                      ...localConfig,
                      excludeThoughtOnReq: e.target.checked,
                    })
                  }
                />
                <span className="ml-4">
                  Exclude thought process when sending request to API
                  (Recommended for DeepSeek-R1)
                </span>
              </div>
            </div>
          </details>

          <details className="collapse collapse-arrow bg-base-200 mb-2 overflow-visible">
            <summary className="collapse-title font-bold">
              Advanced config
            </summary>
            <div className="collapse-content">
              {/* this button only shows in dev mode, used to import a demo conversation to test message rendering */}
              {isDev && (
                <div className="flex flex-row items-center mb-2">
                  <button className="btn" onClick={debugImportDemoConv}>
                    (debug) Import demo conversation
                  </button>
                </div>
              )}
              <div className="flex flex-row items-center mb-2">
                <input
                  type="checkbox"
                  className="checkbox"
                  checked={localConfig.showTokensPerSecond}
                  onChange={(e) =>
                    setLocalConfig({
                      ...localConfig,
                      showTokensPerSecond: e.target.checked,
                    })
                  }
                />
                <span className="ml-4">Show tokens per second</span>
              </div>
              <label className="form-control mb-2">
                <div className="label inline">
                  Custom JSON config (For more info, refer to{' '}
                  <a
                    className="underline"
                    href="https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md"
                    target="_blank"
                    rel="noopener noreferrer"
                  >
                    server documentation
                  </a>
                  )
                </div>
                <textarea
                  className="textarea textarea-bordered h-24"
                  placeholder='Example: { "mirostat": 1, "min_p": 0.1 }'
                  value={localConfig.custom}
                  onChange={(e) =>
                    setLocalConfig({ ...localConfig, custom: e.target.value })
                  }
                />
              </label>
            </div>
          </details>
        </div>

        <div className="modal-action">
          <button className="btn" onClick={resetConfig}>
            Reset to default
          </button>
          <button className="btn" onClick={onClose}>
            Close
          </button>
          <button className="btn btn-primary" onClick={handleSave}>
            Save
          </button>
        </div>
      </div>
    </dialog>
  );
}

function SettingsModalShortInput({
  configKey,
  configDefault,
  value,
  onChange,
  label,
}: {
  configKey: SettKey;
  configDefault: typeof CONFIG_DEFAULT;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  value: any;
  onChange: (value: string) => void;
  label?: string;
}) {
  return (
    <label className="input input-bordered join-item grow flex items-center gap-2 mb-2">
      <div className="dropdown dropdown-hover">
        <div tabIndex={0} role="button" className="font-bold">
          {label || configKey}
        </div>
        <div className="dropdown-content menu bg-base-100 rounded-box z-10 w-64 p-2 shadow mt-4">
          {CONFIG_INFO[configKey] ?? '(no help message available)'}
        </div>
      </div>
      <input
        type="text"
        className="grow"
        placeholder={`Default: ${configDefault[configKey] || 'none'}`}
        value={value}
        onChange={(e) => onChange(e.target.value)}
      />
    </label>
  );
}