-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettings.js
More file actions
409 lines (356 loc) · 14 KB
/
settings.js
File metadata and controls
409 lines (356 loc) · 14 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
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
// ─── Settings Panel ─────────────────────────────────────────────
// Manages the config.json settings UI with tabbed interface,
// toggle switches, and dynamic form rendering.
const SettingsPanel = (() => {
let config = null;
let originalConfig = null;
let activeTab = "agent";
let isOpen = false;
// Tab definitions with emoji icons and config path mappings
const TABS = [
{ id: "agent", icon: "🤖", label: "Agent", path: "agents.defaults" },
{ id: "provider", icon: "🔌", label: "Provider", path: "providers" },
{ id: "tools", icon: "🛠️", label: "Tools", path: "tools" },
{ id: "gateway", icon: "🌐", label: "Gateway", path: "gateway" },
{ id: "channels", icon: "📡", label: "Channels", path: "channels" },
];
// Fields that should use password input
const SECRET_KEYS = ["apiKey", "token", "secret", "password", "appSecret", "encryptKey", "verificationToken", "botToken", "appToken", "accessToken", "clawToken", "bridgeToken", "imapPassword", "smtpPassword"];
// Human-readable labels
const LABELS = {
workspace: "Workspace",
model: "Model",
provider: "Provider",
maxTokens: "Max Tokens",
temperature: "Temperature",
maxToolIterations: "Max Tool Iterations",
memoryWindow: "Memory Window",
reasoningEffort: "Reasoning Effort",
apiKey: "API Key",
apiBase: "API Base URL",
extraHeaders: "Extra Headers",
host: "Host",
port: "Port",
enabled: "Enabled",
intervalS: "Interval (seconds)",
heartbeat: "Heartbeat",
proxy: "Proxy",
search: "Web Search",
maxResults: "Max Results",
exec: "Execution",
timeout: "Timeout (seconds)",
pathAppend: "Additional Path",
restrictToWorkspace: "Restrict to Workspace",
mcpServers: "MCP Servers",
sendProgress: "Send Progress",
sendToolHints: "Send Tool Hints",
allowFrom: "Allow From",
replyToMessage: "Reply to Message",
groupPolicy: "Group Policy",
intents: "Intents",
gatewayUrl: "Gateway URL",
appId: "App ID",
appSecret: "App Secret",
webhookPath: "Webhook Path",
replyInThread: "Reply in Thread",
reactEmoji: "Reaction Emoji",
userTokenReadOnly: "Read Only User Token",
mode: "Mode",
baseUrl: "Base URL",
socketUrl: "Socket URL",
socketPath: "Socket Path",
consentGranted: "Consent Granted",
imapHost: "IMAP Host",
imapPort: "IMAP Port",
imapUsername: "IMAP Username",
imapPassword: "IMAP Password",
imapMailbox: "IMAP Mailbox",
imapUseSsl: "IMAP SSL",
smtpHost: "SMTP Host",
smtpPort: "SMTP Port",
smtpUsername: "SMTP Username",
smtpPassword: "SMTP Password",
smtpUseTls: "SMTP TLS",
smtpUseSsl: "SMTP SSL",
fromAddress: "From Address",
autoReplyEnabled: "Auto Reply",
pollIntervalSeconds: "Polling Interval (s)",
markSeen: "Mark as Seen",
maxBodyChars: "Max Body Chars",
subjectPrefix: "Subject Prefix",
homeserver: "Homeserver",
deviceId: "Device ID",
userId: "User ID",
e2EeEnabled: "E2E Encryption",
clientId: "Client ID",
clientSecret: "Client Secret",
};
function getLabel(key) {
return LABELS[key] || key.replace(/([A-Z])/g, " $1").replace(/^./, s => s.toUpperCase());
}
function isSecretField(key) {
return SECRET_KEYS.some(sk => key.toLowerCase().includes(sk.toLowerCase()));
}
// ─── Deep get/set by dot path ──────────────────────
function getByPath(obj, path) {
return path.split(".").reduce((o, k) => (o && o[k] !== undefined ? o[k] : undefined), obj);
}
function setByPath(obj, path, value) {
const keys = path.split(".");
let current = obj;
for (let i = 0; i < keys.length - 1; i++) {
if (current[keys[i]] === undefined) current[keys[i]] = {};
current = current[keys[i]];
}
current[keys[keys.length - 1]] = value;
}
// ─── API ───────────────────────────────────────────
async function loadConfig() {
try {
const res = await fetch("/api/v1/config");
const data = await res.json();
if (data.ok) {
config = data.config;
originalConfig = JSON.parse(JSON.stringify(config));
return true;
} else {
showToast("Error loading config: " + data.error, "error");
return false;
}
} catch (e) {
showToast("Unable to load configuration", "error");
return false;
}
}
async function saveConfig() {
try {
const res = await fetch("/api/v1/config", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ config }),
});
const data = await res.json();
if (data.ok) {
originalConfig = JSON.parse(JSON.stringify(config));
showToast("Configuration saved successfully! ✅", "success");
} else {
showToast("Error saving: " + data.error, "error");
}
} catch (e) {
showToast("Network error while saving", "error");
}
}
// ─── UI Rendering ─────────────────────────────────
function open() {
const panel = document.getElementById("settings-overlay");
if (!panel) return;
loadConfig().then(ok => {
if (ok) {
isOpen = true;
panel.classList.add("visible");
renderTabs();
renderActiveSection();
}
});
}
function close() {
const panel = document.getElementById("settings-overlay");
if (!panel) return;
// Check for unsaved changes
if (JSON.stringify(config) !== JSON.stringify(originalConfig)) {
if (!confirm("Hai modifiche non salvate. Vuoi chiudere senza salvare?")) return;
}
isOpen = false;
panel.classList.remove("visible");
}
function renderTabs() {
const tabBar = document.getElementById("settings-tabs");
if (!tabBar) return;
tabBar.innerHTML = TABS.map(tab => `
<button class="settings-tab ${tab.id === activeTab ? 'active' : ''}"
data-tab="${tab.id}" onclick="SettingsPanel.switchTab('${tab.id}')">
<span class="tab-icon">${tab.icon}</span>
<span class="tab-label">${tab.label}</span>
</button>
`).join("");
}
function switchTab(tabId) {
activeTab = tabId;
renderTabs();
renderActiveSection();
}
function renderActiveSection() {
const container = document.getElementById("settings-content");
if (!container || !config) return;
const tab = TABS.find(t => t.id === activeTab);
if (!tab) return;
const sectionData = getByPath(config, tab.path);
if (sectionData === undefined) {
container.innerHTML = '<div class="settings-empty">Section not found in configuration</div>';
return;
}
container.innerHTML = "";
if (tab.id === "provider") {
// Provider tab: each provider is a collapsible sub-section
renderProviders(container, sectionData, tab.path);
} else if (tab.id === "channels") {
// Channels: top-level booleans + sub-sections per channel
renderChannels(container, sectionData, tab.path);
} else {
// Generic: render all fields flat or with sub-sections
renderFields(container, sectionData, tab.path);
}
// Smooth scroll to top
container.scrollTop = 0;
}
function renderProviders(container, providers, basePath) {
for (const [name, providerConf] of Object.entries(providers)) {
const section = createSection(name, getLabel(name));
const hasKey = providerConf.apiKey && providerConf.apiKey.length > 0;
section.querySelector(".section-header").innerHTML +=
hasKey ? ' <span class="badge active">Active</span>' : ' <span class="badge inactive">Not configured</span>';
renderFields(section.querySelector(".section-body"), providerConf, `${basePath}.${name}`);
container.appendChild(section);
}
}
function renderChannels(container, channels, basePath) {
// Top-level channel settings
for (const [key, value] of Object.entries(channels)) {
if (typeof value !== "object") {
const field = createField(key, value, `${basePath}.${key}`);
container.appendChild(field);
}
}
// Each channel as a collapsible section
for (const [name, channelConf] of Object.entries(channels)) {
if (typeof channelConf !== "object") continue;
const section = createSection(name, getLabel(name));
const isEnabled = channelConf.enabled === true;
section.querySelector(".section-header").innerHTML +=
isEnabled ? ' <span class="badge active">Active</span>' : ' <span class="badge inactive">Disabled</span>';
renderFields(section.querySelector(".section-body"), channelConf, `${basePath}.${name}`);
container.appendChild(section);
}
}
function renderFields(container, obj, basePath) {
if (!obj || typeof obj !== "object") return;
for (const [key, value] of Object.entries(obj)) {
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
// Nested object → sub-section
const section = createSection(key, getLabel(key));
renderFields(section.querySelector(".section-body"), value, `${basePath}.${key}`);
container.appendChild(section);
} else {
const field = createField(key, value, `${basePath}.${key}`);
container.appendChild(field);
}
}
}
function createSection(id, title) {
const div = document.createElement("div");
div.className = "settings-section collapsed";
div.innerHTML = `
<div class="section-header" onclick="this.parentElement.classList.toggle('collapsed')">
<span class="section-arrow">▶</span>
<span class="section-title">${title}</span>
</div>
<div class="section-body"></div>
`;
return div;
}
function createField(key, value, configPath) {
const wrapper = document.createElement("div");
wrapper.className = "field";
const label = document.createElement("div");
label.className = "field-label";
label.innerHTML = `<span class="field-title">${getLabel(key)}</span>`;
const control = document.createElement("div");
control.className = "field-control";
if (typeof value === "boolean") {
// Toggle switch
const toggleId = `toggle-${configPath.replace(/\./g, "-")}`;
control.innerHTML = `
<label class="toggle" for="${toggleId}">
<input type="checkbox" id="${toggleId}" ${value ? "checked" : ""}
onchange="SettingsPanel.updateValue('${configPath}', this.checked)">
<span class="toggler"></span>
</label>
`;
} else if (typeof value === "number") {
control.innerHTML = `
<input type="number" value="${value}" step="any"
onchange="SettingsPanel.updateValue('${configPath}', parseFloat(this.value) || 0)">
`;
} else if (Array.isArray(value)) {
// Array of strings → textarea
control.innerHTML = `
<textarea rows="3" placeholder="One value per line"
onchange="SettingsPanel.updateValue('${configPath}', this.value.split('\\n').filter(v => v.trim()))"
>${value.join("\n")}</textarea>
`;
} else if (value === null) {
control.innerHTML = `
<input type="text" value="" placeholder="null (not set)"
onchange="SettingsPanel.updateValue('${configPath}', this.value || null)">
`;
} else if (isSecretField(key)) {
// Password field with toggle visibility
const fieldId = `secret-${configPath.replace(/\./g, "-")}`;
const masked = value ? "••••••••" : "";
control.innerHTML = `
<div class="secret-field">
<input type="password" id="${fieldId}" value="${escapeHtml(String(value || ""))}"
placeholder="Enter key..."
onchange="SettingsPanel.updateValue('${configPath}', this.value)">
<button type="button" class="toggle-secret" onclick="SettingsPanel.toggleSecret('${fieldId}')" title="Show/Hide">
👁️
</button>
</div>
`;
} else {
control.innerHTML = `
<input type="text" value="${escapeHtml(String(value || ""))}"
onchange="SettingsPanel.updateValue('${configPath}', this.value)">
`;
}
wrapper.appendChild(label);
wrapper.appendChild(control);
return wrapper;
}
// ─── Value updates ─────────────────────────────────
function updateValue(path, value) {
setByPath(config, path, value);
}
function toggleSecret(fieldId) {
const input = document.getElementById(fieldId);
if (!input) return;
input.type = input.type === "password" ? "text" : "password";
}
// ─── Toast Notifications ──────────────────────────
function showToast(message, type = "info") {
// Remove existing toasts
document.querySelectorAll(".toast").forEach(t => t.remove());
const toast = document.createElement("div");
toast.className = `toast toast-${type}`;
toast.textContent = message;
document.body.appendChild(toast);
requestAnimationFrame(() => toast.classList.add("visible"));
setTimeout(() => {
toast.classList.remove("visible");
setTimeout(() => toast.remove(), 300);
}, 3000);
}
// ─── Helpers ──────────────────────────────────────
function escapeHtml(str) {
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
}
// ─── Public API ───────────────────────────────────
return {
open,
close,
switchTab,
updateValue,
toggleSecret,
saveConfig,
};
})();