Files
tiny-worlds/web/knobs.mjs
T

154 lines
6.8 KiB
JavaScript
Raw Normal View History

2026-11-16 21:32:02 +00:00
// web/knobs.mjs -- the sampling knobs: the definitions table and the
// per-role knob row.
//
// The knobs are named, well-known, widely supported. The definitions
// table is the single home of every knob text -- label, hint, bounds
// -- red-pen the prose here, in place. Bounds are display knowledge
// probed from this endpoint's own validation errors (the /model
// route's validation stays generic: a finite number). The provider's
// serving defaults are unknown and stay unstated; the only default
// anyone can honestly name is the deployment layer (server.json
// params), which rides the run detail as paramDefaults.
//
// Two defaults, never conflated: a knob unset means its value is NOT
// sent to the provider; a value present is sent, and the row marks it
// when it equals the deployment default. The explicit override toggle
// carries the distinction: off (the visible default) disables the
// input and saving sends nothing for the knob (null clears a
// previously-set value); on demands a number.
//
// The hint bar is shared by both roles -- one explanation at a time,
// driven by focus (a tap focuses), cleared when focus leaves the
// knobs entirely.
/** @typedef {import('../server/shapes.mjs').RoleParams} RoleParams */
import { el } from './dom.mjs';
/** One knob's definition: the texts are the product, the shape is
* schema-ready for later kinds (bools: what to expect on and off;
* enums: explanation-only) -- nothing else ships today. All five
* knobs are the GM's here; the mind verb (server/loop.mjs) sets
* only the four that are hers -- max_tokens is excluded by ruling
* (a budget, not a mind). */
/** @typedef {{ key: keyof RoleParams, kind: 'number', label: string, hint: string, bounds: string }} KnobDef */
/** @type {KnobDef[]} */
const DEFS = [
{
key: 'temperature',
kind: 'number',
label: 'temperature',
hint: 'How adventurous the word choice is. Higher: more varied and surprising, occasionally unhinged. Lower: more predictable and conservative; at 0 the same prompt tends to get the same words.',
bounds: 'This endpoint: 0 to 2.',
},
{
key: 'top_p',
kind: 'number',
label: 'top_p',
hint: 'Narrows the field to the most likely words that together cover this much probability. Higher: more words in play, more variety. Lower: fewer, safer choices; very low makes her repeat herself.',
bounds: 'This endpoint: above 0 (0 is refused), up to 1.',
},
{
key: 'top_k',
kind: 'number',
label: 'top_k',
hint: 'Like top_p but by count -- only the k most likely words are considered each step. Higher: more variety. Lower: more locked-in; at 1 she picks the single most likely word every time. -1 and 0 turn the filter off.',
bounds: 'No known bounds on this endpoint; -1 and 0 both mean the filter is off.',
},
{
key: 'repetition_penalty',
kind: 'number',
label: 'repetition_penalty',
hint: 'A direct nudge against repeating recent words and phrasings. 1 is no effect; raise it to make her evade repetition. Too high and she substitutes odd words to escape.',
bounds: 'Must be above 0 on this endpoint, no known max.',
},
{
key: 'max_tokens',
kind: 'number',
label: 'max_tokens',
hint: 'The response\'s hard length budget. Higher: room for long turns. Lower: cheaper and shorter, but the turn can cut off mid-sentence -- and on reasoning models thinking counts against this budget first, so a low cap can be eaten entirely before she writes a word.',
bounds: '1 or more on this endpoint; no enforced max (values above the model\'s output ceiling are accepted).',
},
];
/** The hint bar's text for one knob: label, the explanation, the
* bounds, and the honest default statement for this role's
* deployment layer. Never invents an unknown value. */
/** @param {KnobDef} def @param {number|undefined|null} dep @returns {string} */
function hintText(def, dep) {
const defLine = dep !== undefined && dep !== null
? `Unset = the deployment default (${dep}).`
: 'Unset = not sent; the provider\'s own default applies (unknown).';
return `${def.label.replace(/_/g, ' ')} -- ${def.hint} ${def.bounds} ${defLine}`;
}
/** One role's knob row.
* @param {string} role
* @param {RoleParams|undefined} current the run's own knobs
* @param {RoleParams|undefined|null} deployment the deployment layer (server.json params)
* @param {HTMLElement} hintBar the shared hint bar
* @returns {{ wrap: HTMLElement, read: () => Record<string, number|null> }}
*/
export function knobsBox(role, current, deployment, hintBar) {
const wrap = el('div', 'knobs');
wrap.appendChild(el('div', 'knobs-head', `${role} knobs`));
/** @type {Record<string, { tog: HTMLInputElement, inp: HTMLInputElement }>} */
const rows = {};
for (const def of DEFS) {
const row = el('div', 'knob');
const tog = /** @type {HTMLInputElement} */ (el('input'));
tog.type = 'checkbox';
tog.title = 'override';
const lab = el('label', null, `${def.label.replace(/_/g, ' ')} `);
const inp = /** @type {HTMLInputElement} */ (el('input'));
inp.type = 'number';
inp.step = 'any';
const mark = el('span', 'knob-default', ' \u00b7 deployment default');
lab.appendChild(inp);
lab.appendChild(mark);
row.appendChild(tog);
row.appendChild(lab);
wrap.appendChild(row);
const set = current?.[def.key];
tog.checked = set !== undefined;
if (set !== undefined) inp.value = String(set);
const dep = deployment?.[def.key];
// the toggle owns the state: off disables the input (the value
// stays for a change of mind), on demands a number; a value equal
// to the deployment default is still sent, and the row marks it
const sync = () => {
inp.disabled = !tog.checked;
mark.hidden = !(tog.checked && inp.value.trim() !== '' && Number(inp.value) === dep);
};
tog.addEventListener('change', sync);
inp.addEventListener('input', sync);
sync();
inp.addEventListener('focus', () => { hintBar.textContent = hintText(def, dep); });
rows[def.key] = { tog, inp };
}
// the bar keeps its text while focus moves inside the row, clears
// when it leaves the knobs entirely
wrap.addEventListener('focusout', (e) => {
if (!wrap.contains(/** @type {?Node} */ (e.relatedTarget))) hintBar.textContent = '';
});
const read = () => {
/** @type {Record<string, number|null>} */ const out = {};
for (const def of DEFS) {
const { tog, inp } = rows[def.key];
if (tog.checked) {
const v = inp.value.trim();
if (v === '' || !Number.isFinite(Number(v))) {
throw new Error(`${def.label}: a number is required -- or switch the override off`);
}
out[def.key] = Number(v);
} else if (current?.[def.key] !== undefined) {
out[def.key] = null; // clearing a previously-set knob
}
}
return out;
};
return { wrap, read };
}