-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsForm.tsx
More file actions
218 lines (206 loc) · 7.1 KB
/
SettingsForm.tsx
File metadata and controls
218 lines (206 loc) · 7.1 KB
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
'use client';
import React, { useCallback, useState } from 'react';
import {
CCard,
CCardBody,
CCardHeader,
CForm,
CFormLabel,
CFormInput,
CFormSwitch,
CFormSelect,
CButton,
CAlert,
CRow,
CCol,
} from '@coreui/react-pro';
import type { AgentVendor } from '@/lib/types';
import {
AGENT_VENDORS,
defaultModelForVendor,
modelsForVendor,
} from '@/lib/agent-vendors';
interface SettingsFormProps {
settings: Record<string, unknown>;
}
/** Settings values arrive as JSON — strip the wrapping quotes if any. */
function unquote(val: unknown): string {
if (typeof val === 'string') {
const trimmed = val.trim();
if (trimmed.startsWith('"') && trimmed.endsWith('"')) {
return trimmed.slice(1, -1);
}
return trimmed;
}
return String(val ?? '');
}
/** Build a clean draft object from the raw settings record. */
function buildDraft(raw: Record<string, unknown>) {
return {
max_concurrency: Number(raw.max_concurrency || 2),
queue_paused: Boolean(raw.queue_paused),
auto_enqueue: Boolean(raw.auto_enqueue),
notifications_enabled: raw.notifications_enabled !== false,
system_llm_vendor: (unquote(raw.system_llm_vendor) || 'glm') as AgentVendor,
system_llm_model: unquote(raw.system_llm_model) || 'glm-5.1',
};
}
export function SettingsForm({ settings: initial }: SettingsFormProps) {
const [draft, setDraft] = useState(() => buildDraft(initial));
const [saving, setSaving] = useState(false);
const [saved, setSaved] = useState(false);
const [error, setError] = useState('');
// ── Local-only state changes (no API call) ─────────────────────────
const set = useCallback(
<K extends keyof typeof draft>(key: K, value: (typeof draft)[K]) => {
setSaved(false);
setDraft((prev) => ({ ...prev, [key]: value }));
},
[],
);
// Vendor change auto-resets model if the current model doesn't belong
// to the new vendor's suggested list.
const handleVendorChange = useCallback(
(next: AgentVendor) => {
setDraft((prev) => {
const belongs = modelsForVendor(next).some((m) => m.id === prev.system_llm_model);
return {
...prev,
system_llm_vendor: next,
system_llm_model: belongs ? prev.system_llm_model : defaultModelForVendor(next),
};
});
setSaved(false);
},
[],
);
// ── Save all settings at once ──────────────────────────────────────
const handleSave = useCallback(async () => {
setSaving(true);
setSaved(false);
setError('');
try {
const pairs: [string, unknown][] = [
['max_concurrency', draft.max_concurrency],
['queue_paused', draft.queue_paused],
['auto_enqueue', draft.auto_enqueue],
['notifications_enabled', draft.notifications_enabled],
['system_llm_vendor', draft.system_llm_vendor],
['system_llm_model', draft.system_llm_model],
];
await Promise.all(
pairs.map(([key, value]) =>
fetch('/api/settings', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ key, value }),
}),
),
);
setSaved(true);
setTimeout(() => setSaved(false), 3000);
} catch {
setError('Failed to save settings');
} finally {
setSaving(false);
}
}, [draft]);
// Model datalist for the selected system LLM vendor
const sysModelList = modelsForVendor(draft.system_llm_vendor);
return (
<>
{/* General Settings */}
<CCard className="mb-4">
<CCardHeader><strong>General</strong></CCardHeader>
<CCardBody>
{error && <CAlert color="danger" className="py-2">{error}</CAlert>}
{saved && <CAlert color="success" className="py-2">Settings saved.</CAlert>}
<CForm onSubmit={(e) => { e.preventDefault(); handleSave(); }}>
<CRow className="mb-3">
<CCol md={4}>
<CFormLabel>Max Concurrency</CFormLabel>
<CFormInput
type="number"
min="1"
max="10"
value={String(draft.max_concurrency)}
onChange={(e) => set('max_concurrency', parseInt(e.target.value) || 1)}
/>
</CCol>
</CRow>
<CRow className="mb-3">
<CCol md={4}>
<CFormSwitch
label="Queue Paused"
checked={draft.queue_paused}
onChange={(e) => set('queue_paused', e.target.checked)}
/>
</CCol>
<CCol md={4}>
<CFormSwitch
label="Auto-enqueue on create"
checked={draft.auto_enqueue}
onChange={(e) => set('auto_enqueue', e.target.checked)}
/>
</CCol>
<CCol md={4}>
<CFormSwitch
label="Notifications enabled"
checked={draft.notifications_enabled}
onChange={(e) => set('notifications_enabled', e.target.checked)}
/>
</CCol>
</CRow>
{/* System LLM — used for Fill Task and other non-agent API calls */}
<hr className="my-3" />
<CFormLabel className="fw-semibold">
System LLM{' '}
<small className="fw-normal text-body-secondary">
— used by Fill Task and other non-agent features
</small>
</CFormLabel>
<CRow className="mb-3">
<CCol md={3}>
<CFormLabel className="mb-1">Vendor</CFormLabel>
<CFormSelect
value={draft.system_llm_vendor}
onChange={(e) => handleVendorChange(e.target.value as AgentVendor)}
>
{AGENT_VENDORS.map((v) => (
<option key={v.id} value={v.id}>
{v.label}
</option>
))}
</CFormSelect>
</CCol>
<CCol md={9}>
<CFormLabel className="mb-1">Model</CFormLabel>
<CFormInput
value={draft.system_llm_model}
onChange={(e) => set('system_llm_model', e.target.value)}
list="settings-system-llm-models"
placeholder="Type or select a model…"
autoComplete="off"
/>
<datalist id="settings-system-llm-models">
{sysModelList.map((m) => (
<option key={m.id} value={m.id}>
{m.label}
</option>
))}
</datalist>
</CCol>
</CRow>
<CButton
type="submit"
color="primary"
disabled={saving}
>
{saving ? 'Saving…' : 'Save Settings'}
</CButton>
</CForm>
</CCardBody>
</CCard>
</>
);
}