-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
207 lines (182 loc) · 8.03 KB
/
index.js
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
import { SlashCommand } from '../../../slash-commands/SlashCommand.js';
import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../../slash-commands/SlashCommandArgument.js';
import { enumIcons } from '../../../slash-commands/SlashCommandCommonEnumsProvider.js';
import { enumTypes, SlashCommandEnumValue } from '../../../slash-commands/SlashCommandEnumValue.js';
import { SlashCommandParser } from '../../../slash-commands/SlashCommandParser.js';
import { isTrueBoolean } from '../../../utils.js';
/**
* @typedef {Object} SamplerParameter - Represents a sampler parameter.
* @property {string} id - ID of the sampler parameter.
* @property {string} name - Name of the sampler parameter.
* @property {number} max - Maximum value.
* @property {number} min - Minimum value.
* @property {number} value - Current value.
* @property {string} type - 'range' or 'checkbox'.
* @property {boolean} checked - Only for checkbox type.
* @property {HTMLInputElement} control - The input element.
*/
/**
* Gets the title of the sampler parameter
* @param {HTMLElement} element Sampler parameter element.
* @returns {HTMLElement} Element containing the title of the sampler parameter.
*/
function getTitleParent(element) {
const parent = element.closest('.range-block') || element.closest('label') || element.closest('h4') || element.closest('div');
if (!(parent instanceof HTMLElement)) {
return null;
}
const rangeBlockTitle = parent.querySelector('.range-block-title');
if (rangeBlockTitle instanceof HTMLElement && rangeBlockTitle.textContent.trim()) {
return rangeBlockTitle;
}
const smallTextNode = parent.querySelector('small');
if (smallTextNode instanceof HTMLElement && smallTextNode.textContent.trim()) {
return smallTextNode;
}
const checkboxLabel = parent.querySelector('.checkbox_label');
if (checkboxLabel instanceof HTMLElement && checkboxLabel.textContent.trim()) {
return checkboxLabel;
}
return parent;
}
/**
* Enumerates all sampler parameters available in the UI.
* @returns {SamplerParameter[]} List of sampler parameters.
*/
function enumerateSamplerParameters() {
const leftPanel = document.getElementById('left-nav-panel');
const computedStyle = window.getComputedStyle(leftPanel);
const leftPanelDisplay = computedStyle.display;
if (leftPanelDisplay === 'none') {
leftPanel.style.opacity = '0';
leftPanel.style.visibility = 'hidden';
leftPanel.style.display = 'block';
}
const sanitizeId = id => id.replace('_counter', '').replace('_textgenerationwebui', '').replace('_openai', '').replace('_novel', '').replace('openai_', '').replace('oai_', '');
const isVisible = e => e instanceof HTMLElement && e.offsetHeight > 0 && e.offsetWidth > 0;
const rangeSliders = Array.from(leftPanel.querySelectorAll('input[type="range"], input[type="checkbox"], input[type="number"]:not([data-for])')).filter(isVisible);
const roundToPrecision = (num, precision = 1e4) => ((num = parseFloat(num + '') || 0), Math.round((num + Number.EPSILON) * precision) / precision);
/** @type {SamplerParameter[]} */
const samplerParameters = [];
for (const control of rangeSliders) {
if (!(control instanceof HTMLInputElement)) {
continue;
}
// Those are not sampler parameters.
if (control.closest('#openai_settings')) {
continue;
}
const name = getTitleParent(control)?.textContent?.trim();
const sampler = {
id: sanitizeId(control.id),
name: name,
max: roundToPrecision(parseFloat(control.max)),
min: roundToPrecision(parseFloat(control.min)),
value: roundToPrecision(parseFloat(control.value)),
checked: control.checked,
type: control.type,
control: control,
};
if (!sampler.id || !sampler.name) {
continue;
}
samplerParameters.push(sampler);
}
leftPanel.style.visibility = '';
leftPanel.style.opacity = '';
leftPanel.style.display = leftPanelDisplay;
return samplerParameters;
}
(() => {
const enumProvider = () => enumerateSamplerParameters()
.sort((a, b) => a.name.localeCompare(b.name))
.map(p => {
const name = `${p.name} ⇒ ${p.type == 'range' ? `${p.value} [${p.min}..${p.max}]` : p.checked}`;
const icon = p.type === 'range' ? enumIcons.number : enumIcons.boolean;
return new SlashCommandEnumValue(p.id, name, enumTypes.number, icon);
});
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'sampler-get',
helpString: 'Gets the value of a sampling parameter.',
returns: 'number / boolean',
callback: (_, name) => {
if (!name) {
throw new Error('Parameter name is required.');
}
if (typeof name !== 'string') {
throw new Error('Parameter name must be a string.');
}
name = name.trim().toLowerCase();
const parameter = enumerateSamplerParameters().find(p => p.name.toLowerCase() === name || p.id.toLowerCase() === name);
if (!parameter) {
throw new Error(`Parameter "${name}" not found.`);
}
return parameter.type === 'checkbox' ? String(parameter.checked) : String(parameter.value);
},
unnamedArgumentList: [
SlashCommandArgument.fromProps({
description: 'The name of the parameter to get.',
typeList: [ARGUMENT_TYPE.STRING],
enumProvider: enumProvider,
isRequired: true,
acceptsMultiple: false,
}),
],
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'sampler-set',
helpString: 'Sets the value of a sampling parameter.',
returns: 'void',
callback: (args, value) => {
let { name } = args;
if (!name) {
throw new Error('Parameter name is required.');
}
if (typeof name !== 'string') {
throw new Error('Parameter name must be a string.');
}
name = name.trim().toLowerCase();
const parameter = enumerateSamplerParameters().find(p => p.name.toLowerCase() === name || p.id.toLowerCase() === name);
if (!parameter) {
throw new Error(`Parameter "${name}" not found.`);
}
if (!value) {
throw new Error('Value is required.');
}
if (typeof value !== 'string') {
throw new Error('Value must be a string.');
}
if (parameter.type === 'checkbox') {
const isTrue = isTrueBoolean(String(value));
parameter.control.checked = isTrue;
} else {
const number = parseFloat(String(value));
if (isNaN(number) || !isFinite(number)) {
throw new Error('Value must be convertible to a finite number.');
}
const clamped = Math.min(Math.max(number, parameter.min), parameter.max);
parameter.control.value = String(clamped);
}
parameter.control.dispatchEvent(new Event('input', { bubbles: true }));
return '';
},
namedArgumentList: [
SlashCommandNamedArgument.fromProps({
name: 'name',
description: 'The name of the parameter to set.',
typeList: [ARGUMENT_TYPE.STRING],
enumProvider: enumProvider,
isRequired: true,
acceptsMultiple: false,
}),
],
unnamedArgumentList: [
SlashCommandArgument.fromProps({
description: 'The value to set.',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.BOOLEAN],
isRequired: true,
acceptsMultiple: false,
}),
],
}));
})();