import { DecimalInput } from './DecimalInput'; import { tr } from '../lib/i18n'; import {nativeSettingsResponse,nativeSettingsValues} from '../lib/native-settings-response'; import { describeChange, type TunedChange } from '../lib/tune-values'; import { useEffect, useState } from 'react'; import { api } from '../lib/client'; import { refreshNative, useNative, type NativeField } from '../lib/native'; import { Btn, ErrorNotice, Select } from './ui'; import { draftingChoices } from '../lib/drafting'; import { NativeMcpEditor } from './NativeMcpEditor'; /** One of the speed features: what it is called, what it does, whether it is on by default, and the engine environment it writes outside its default (server/native-settings.mjs). */ interface SpeedSwitch { id: string; label: string; hint: string; default: 'on' | 'off'; env: Record; /** What an earlier test build wrote for the same state: it reads as set, and goes with it. */ legacy?: Record; /** The Expert settings group the row is in (Speed when not given). */ group?: string; /** The two option names instead of On / Off (the picture reader: Strata's own / llama.cpp's). */ words?: { on: string; off: string }; } const switchGroup = (sw: SpeedSwitch) => sw.group ?? 'Speed'; const switchHas = (entries: Record | undefined, env: Record | undefined) => !!entries && Object.keys(entries).length > 0 && Object.entries(entries).every(([name, value]) => env?.[name] === value); const switchIsSet = (sw: SpeedSwitch, env: Record | undefined) => switchHas(sw.env, env) || switchHas(sw.legacy, env); const switchIsOn = (sw: SpeedSwitch, env: Record | undefined) => switchIsSet(sw, env) !== (sw.default === 'on'); export function NativeSettings({ model }: { model: string; }) { const native = useNative(); const [revision,setRevision]=useState(0); const [hints, setHints] = useState>({}); // a field's hint worded for this model (the Drafting size) const [switches, setSwitches] = useState([]); // the speed switches that apply to this model on this PC const [tuned, setTuned] = useState<{ preset: { name: string } | null; changed: TunedChange[] } | null>(null); const [values, setValues] = useState>({}), [mcp, setMcp] = useState('{}'), [original, setOriginal] = useState(''), [text, setText] = useState(''), [tab, setTab] = useState('fields'), [pending, setPending] = useState(false), [error, setError] = useState(''), [notice, setNotice] = useState(''), [query, setQuery] = useState(''); useEffect(() => { let active = true; setOriginal(''); setValues({}); setText(''); setMcp('{}'); setError(''); setNotice(''); void api.get<{ values: Record; mcp: Record; tuned?: { preset: { name: string } | null; changed: TunedChange[] } | null; hints?: Record; speedSwitches?: SpeedSwitch[]; }>(`/api/native/models/${model}/settings`).then(input => { if (!active) return; setSwitches(input.speedSwitches ?? []); setTuned(input.tuned ?? null); setHints(input.hints ?? {}); const value = nativeSettingsResponse(input); setValues(value.values); setText(JSON.stringify(value.values, null, 2)); setMcp(JSON.stringify(value.mcp ?? {}, null, 2)); setOriginal(JSON.stringify({ values: value.values, mcp: value.mcp ?? {} })); }).catch(e => { if (active) setError(e.message); }); return () => { active = false; }; }, [model,revision]); const change = (id: string, value: any) => { const next = { ...values, [id]: value }; setValues(next); setText(JSON.stringify(next, null, 2)); setNotice(''); }; /** The default removes the switch's environment entries, the other state writes them: the model's file then carries nothing for a default launch. */ const changeSwitch = (sw: SpeedSwitch, on: boolean) => { const env = { ...(values.env ?? {}) }; const write = on !== (sw.default === 'on'); for (const [name, value] of Object.entries(sw.env)) { if (write) env[name] = value; else delete env[name]; } if (!write) for (const name of Object.keys(sw.legacy ?? {})) delete env[name]; change('env', env); }; async function save() { setPending(true); setError(''); try { const parsed = nativeSettingsValues(tab === 'json' ? JSON.parse(text) : values); const tools = JSON.parse(mcp); const input = await api.patch<{ values: Record; mcp: Record; }>(`/api/native/models/${model}/settings`, { values: parsed, mcp: tools }); const result = nativeSettingsResponse(input); setValues(result.values); setMcp(JSON.stringify(result.mcp, null, 2)); setText(JSON.stringify(result.values, null, 2)); setOriginal(JSON.stringify({ values: result.values, mcp: result.mcp })); setNotice('Saved. Load the model again to apply these settings. A backup of the previous configuration was kept.'); await refreshNative(true); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setPending(false); } } function chooseTab(next: string) { if (tab === 'json') { try { setValues(nativeSettingsValues(JSON.parse(text))); } catch { setError('Fix the JSON before changing tabs.'); return; } } setTab(next); } const dirty = (() => { try { return original !== JSON.stringify({ values: tab === 'json' ? JSON.parse(text) : values, mcp: JSON.parse(mcp) }); } catch { return true; } })(); if (!original) return
{tr('Native engine settings')} {error ? <> setRevision(value => value + 1)}/>
{tr('Details')}

{error}

:

{tr('Reading saved settings…')}

}
; const family = (native?.catalog?.families ?? []).find(f => f.sizes.some(s => s.id === model))?.family ?? null; const draftingOffered = !!draftingChoices(family, native?.capabilities?.dflash2); const rows = (native?.fields ?? []).filter(f => (f.id !== 'drafting' || draftingOffered) && `${tr(f.label)} ${f.id} ${tr(f.group)}`.toLowerCase().includes(query.toLowerCase())); const switchRows = switches.filter(sw => `${tr(sw.label)} ${sw.id} ${tr(switchGroup(sw))}`.toLowerCase().includes(query.toLowerCase())); const switchRow = (sw: SpeedSwitch) =>