File size: 5,036 Bytes
9b72f0d
 
 
 
 
 
 
 
 
 
 
cc1f67c
9b72f0d
 
 
 
 
 
 
 
cc1f67c
9b72f0d
 
 
 
cc1f67c
9b72f0d
cc1f67c
9b72f0d
 
 
 
 
 
 
 
ec2237a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b72f0d
 
 
 
 
 
 
cc1f67c
 
 
 
 
 
 
 
 
9b72f0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc1f67c
9b72f0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {
  Button,
  InputCheckbox,
  InputCheckboxList,
  InputSelect,
  InputSlider,
  InputTextarea,
} from "@theme";
import { formatBytes } from "@utils/format.ts";
import { MODELS } from "@utils/models.ts";
import { TOOLS } from "@utils/tools.ts";
import { useEffect, useRef } from "react";
import { Controller, useForm } from "react-hook-form";

import type { ChatSettings } from "./types.ts";

export default function ChatSettingsModalForm({
  defaultValues,
  onSubmit,
  downloadedModels,
  visible,
}: {
  defaultValues: ChatSettings;
  onSubmit: (data: ChatSettings) => void;
  downloadedModels: Array<string>;
  visible: boolean;
}) {
  const selectModelRef = useRef<HTMLSelectElement>(null);
  const {
    control,
    handleSubmit,
    formState: { errors },
  } = useForm<ChatSettings>({
    defaultValues,
  });

  const modelOptions = Object.values(
    Object.entries(MODELS).reduce(
      (groups, [key, model]) => {
        const groupName = model.group;

        if (!groups[groupName]) {
          groups[groupName] = {
            label: groupName.charAt(0).toUpperCase() + groupName.slice(1),
            options: [],
          };
        }

        groups[groupName].options.push({
          value: key,
          label: `${model.title} - ${formatBytes(model.size)}${downloadedModels.includes(key) ? " *" : ""}`,
          title: model.title,
        });

        return groups;
      },
      {} as Record<
        string,
        {
          label: string;
          options: Array<{ value: string; label: string; title: string }>;
        }
      >
    )
  )
    .sort((a, b) => a.label.localeCompare(b.label))
    .map((group) => ({
      ...group,
      options: group.options
        .sort((a, b) => a.title.localeCompare(b.title))
        .map(({ value, label }) => ({ value, label })),
    }));

  const toolOptions = TOOLS.map((tool) => ({
    name: tool.name,
    label: tool.name,
    description: tool.description,
  }));

  useEffect(() => {
    if (visible && selectModelRef.current) {
      // Use setTimeout to ensure the element is rendered and ready for focus
      setTimeout(() => {
        selectModelRef.current?.focus();
      }, 200);
    }
  }, [visible]);

  return (
    <form onSubmit={handleSubmit(onSubmit)} className="flex flex-col gap-6">
      <Controller
        name="modelKey"
        control={control}
        rules={{ required: "Model is required" }}
        render={({ field }) => (
          <InputSelect
            id="model-select"
            label="Model"
            error={errors.modelKey?.message as string}
            value={field.value}
            onChange={field.onChange}
            options={modelOptions}
            required
            tooltip="(*) are already downloaded"
            ref={selectModelRef}
          />
        )}
      />

      <Controller
        name="systemPrompt"
        control={control}
        rules={{ required: "System prompt is required" }}
        render={({ field }) => (
          <InputTextarea
            id="system-prompt"
            label="System Prompt"
            placeholder="You are a helpful AI Assistant"
            error={errors.systemPrompt?.message as string}
            value={field.value}
            onChange={field.onChange}
            rows={6}
            required
          />
        )}
      />

      <Controller
        name="tools"
        control={control}
        render={({ field }) => (
          <InputCheckboxList
            id="tools"
            label="Tools (only if supported by the model)"
            options={toolOptions}
            value={field.value}
            onChange={field.onChange}
            error={errors.tools?.message as string}
          />
        )}
      />

      <Controller
        name="temperature"
        control={control}
        rules={{
          required: "Temperature is required",
          min: { value: 0, message: "Temperature must be at least 0" },
          max: { value: 2, message: "Temperature must be at most 2" },
        }}
        render={({ field }) => (
          <InputSlider
            id="temperature"
            label="Temperature"
            value={field.value}
            onChange={field.onChange}
            min={0}
            max={2}
            step={0.1}
            error={errors.temperature?.message as string}
            tooltip="Controls randomness in responses. Lower values are more focused, higher values are more creative."
          />
        )}
      />

      <Controller
        name="enableThinking"
        control={control}
        render={({ field }) => (
          <InputCheckbox
            id="enable-thinking"
            label="Enable Thinking"
            description="Allow the model to show its reasoning process (only if supported by the model)"
            checked={field.value}
            onChange={field.onChange}
            error={errors.enableThinking?.message as string}
          />
        )}
      />

      <Button type="submit">Start Conversation</Button>
    </form>
  );
}