import {localeTag} from '../lib/i18n';
import { tr } from '../lib/i18n';
import { useEffect, useId, useRef, useState } from 'react';
import type { PointerEvent } from 'react';
import { closestStop, dialAngle, dialIndex, unwrapAngle } from '../lib/tuning-controls';
import { Btn, Dialog, Icon, Select } from './ui';
import { DEFAULT_EXECUTION, FIT_TEXT, OUTPUT_TOKENS, PROFILES, WORKLOADS, RECOMMENDED_CONTEXT_TOKENS, durationText, speedText } from '../lib/experience';
import type { Execution, Experience, Profile, Workload } from '../lib/experience';
export function ProfileChoice({ value, onChange }: {
value: Profile;
onChange: (profile: Profile) => void;
}) {
return
{tr(PROFILES.map((p, i) => ))}
;
}
function point(angle: number, r = 78) { const a = angle * Math.PI / 180; return { x: 110 + r * Math.sin(a), y: 110 - r * Math.cos(a) }; }
function angleAt(index: number, count: number) { return -135 + 270 * index / Math.max(1, count - 1); }
export function Dial({ label, hint, values, value, saved, recommended, onChange, onReset, tone = 'accent', gestureHint = true, exactEntry = true }: {
label: string;
hint: string;
values: {
value: number;
label: string;
}[];
value: number;
saved: number;
recommended: number;
onChange: (v: number) => void;
onReset?: () => void;
tone?: 'accent' | 'info';
gestureHint?: boolean;
exactEntry?: boolean;
}) {
const id = useId();
const drag = useRef<{
angle: number;
} | null>(null);
const index = Math.max(0, values.findIndex(v => v.value === value));
const [dragIndex, setDragIndex] = useState(null);
const [entry, setEntry] = useState(String(value));
const [entryError, setEntryError] = useState('');
useEffect(() => { setEntry(String(value)); setEntryError(''); }, [value]);
const shown = dragIndex ?? index;
function select(i: number) { onChange(values[Math.max(0, Math.min(values.length - 1, Math.round(i)))].value); }
function track(e: PointerEvent, initial = false) {
const r = e.currentTarget.getBoundingClientRect();
const x = e.clientX - r.left - r.width / 2, y = e.clientY - r.top - r.height / 2;
if (Math.hypot(x, y) < r.width * .12)
return;
const raw = dialAngle(x, y);
const angle = initial || !drag.current ? raw : unwrapAngle(raw, drag.current.angle);
drag.current = { angle };
const next = dialIndex(angle, values.length);
setDragIndex(next);
select(next);
}
function finish(e: PointerEvent) {
if (!drag.current)
return;
track(e);
drag.current = null;
setDragIndex(null);
if (e.currentTarget.hasPointerCapture(e.pointerId))
e.currentTarget.releasePointerCapture(e.pointerId);
}
function commitEntry() {
const n = Number(entry);
if (!Number.isFinite(n) || n <= 0) {
setEntryError('Enter a positive number.');
return;
}
const snapped = closestStop(n, values.map(v => v.value));
onChange(snapped);
setEntry(String(snapped));
setEntryError('');
}
const pointer = angleAt(shown, values.length);
const known = values.some(v => v.value === value);
return
{tr(label)}
v.value === value)?.label ?? `${value} custom`} tabIndex={0} data-custom={!known || undefined} data-dragging={dragIndex !== null} onPointerDown={e => {
if (e.button !== 0)
return;
e.preventDefault();
e.currentTarget.focus();
e.currentTarget.setPointerCapture(e.pointerId);
drag.current = { angle: angleAt(index, values.length) };
track(e, true);
}} onPointerMove={e => {
if (drag.current)
track(e);
}} onPointerUp={finish} onPointerCancel={() => { drag.current = null; setDragIndex(null); }} onKeyDown={e => {
if (!['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Home', 'End', 'PageUp', 'PageDown'].includes(e.key))
return;
e.preventDefault();
select(e.key === 'Home' ? 0 : e.key === 'End' ? values.length - 1 : index + (['ArrowUp', 'ArrowRight', 'PageUp'].includes(e.key) ? 1 : -1));
}}>
{tr(dragIndex !== null ? values[Math.round(dragIndex)].label : known ? values[index].label : value.toLocaleString(localeTag()))}{tr(value === recommended ? 'Recommended' : known ? 'Custom' : 'Expert value')}
{tr(values.map(v => {tr(v.label)}))}
{tr(gestureHint ? {tr("Turn the dial \u00B7 or use arrow keys")}
: null)}
{tr(exactEntry ? : null)}
{tr(entryError ? {tr(entryError)}
: null)}{tr(hint)}
;
}
export function TuningDials({ config, saved = config, contextMax, batchSupported, batchAuto, onBatchAuto, onChange, showContext = true, showBatch = true, exactEntry = true }: {
config: Execution;
saved?: Execution;
contextMax: number;
batchSupported: boolean;
batchAuto: boolean;
onBatchAuto: (v: boolean) => void;
onChange: (patch: Partial) => void;
showContext?: boolean;
showBatch?: boolean;
exactEntry?: boolean;
}) {
const contexts = [4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576].filter(v => v <= contextMax).map(value => ({ value, label: `${value / 1024}K` }));
const recommended = Math.min(RECOMMENDED_CONTEXT_TOKENS, contexts.at(-1)?.value ?? 4096);
return
{tr(showContext ? contexts.length ?
onChange({ context })}/> : {tr("No supported context length.")}
: null)}
{tr(showBatch ? batchSupported ? {tr("Prompt batch \u00B7 ")}{tr(batchAuto ? 'Automatic' : `Custom: ${config.batch.toLocaleString(localeTag())}`)} {tr("Adjust")}
({ value, label: value.toLocaleString(localeTag()) }))} value={config.batch} saved={saved.batch} recommended={DEFAULT_EXECUTION.batch} onReset={() => onBatchAuto(true)} onChange={batch => { onBatchAuto(false); onChange({ batch }); }}/>
: {tr("Prompt batch")}
{tr("This adapter cannot apply prompt batch settings.")}
: null)}
;
}
export function WorkloadChoice({ value, context, onChange }: {
value: Workload;
context: number;
onChange: (w: Workload) => void;
}) {
return