import { tr } from '../lib/i18n'; import { useId, useState } from 'react'; import { Btn, Icon } from './ui'; import { CPU_ONLY_LINE, NO_CARD_LINE, OFF_AT_OR_BELOW_GB, gpuLabel, noteText, order, recommend, setRole, shortName, vramOn, warnings, type Role, type RoleCard, type Roles, type RoleWarning, } from '../lib/gpu-roles'; const ROLE_WORD: Record = { main: 'Main', helper: 'Helper', off: 'Off' }; const HINT: Record = { main: 'Does the main work', helper: 'Holds part of the model', off: 'Not used' }; /** The one plain line above the cards, built from translated pieces. */ export function recommendationLine(cards: RoleCard[], vendor?: string | null): string { const rec = recommend(cards, vendor); if (rec.main === null) return tr('No graphics card that Strata can use was found.'); const byi = new Map(cards.map(c => [c.index, c])); const m = byi.get(rec.main)!; if (cards.length === 1) return tr('Strata will use your {0}.', [shortName(m)]); const why = { same: 'the cards are alike', memory: 'it has more memory', faster: 'it is faster', only: 'it is the only card', none: '' }[rec.why]; let s = tr('{0} is Main: {1}.', [shortName(m), tr(why)]); const helpers = Object.keys(rec.roles).map(Number).filter(i => rec.roles[i] === 'helper').map(i => shortName(byi.get(i)!)); if (helpers.length) s += ' ' + tr(helpers.length > 1 ? '{0} help.' : '{0} helps.', [helpers.join(', ')]); for (const i of Object.keys(rec.roles).map(Number)) if (rec.roles[i] === 'off') { const code = rec.reasons[i], c = byi.get(i)!; s += ' ' + tr('{0} starts Off: {1}', [shortName(c), code === 'cannot_run' ? tr('Strata cannot run it.') : code === 'igpu' ? tr('built-in graphics.') : tr('{0} GB or less.', [OFF_AT_OR_BELOW_GB])]); } return s; } function warningText(w: RoleWarning, cards: RoleCard[]): string { const c = w.card !== null ? cards.find(x => x.index === w.card) : undefined; if (w.code === 'cannot_run') return tr('{0} cannot run Strata.', [c ? shortName(c) : '']); if (w.code === 'igpu_main') return tr('{0} is built-in graphics; it is slow as Main.', [c ? shortName(c) : '']); if (w.code === 'cpu_only') return tr(w.text === CPU_ONLY_LINE ? CPU_ONLY_LINE : NO_CARD_LINE); return tr('Not enough memory for this setup: about {0} GB is read from the drive.', [w.text.match(/about (\d+) GB/)?.[1] ?? '']); } export function noteLine(code: string | null, cards: RoleCard[]): string { if (!code) return ''; const kind = code.split(':')[0], c = cards.find(x => x.index === Number(code.split(':')[1])); if (kind === 'old_main_helper') return tr('{0} is now a Helper.', [c ? shortName(c) : '']); if (kind === 'promoted') return tr('{0} is now Main.', [c ? shortName(c) : '']); if (kind === 'only_main') return tr('The only card that is on has to be Main.'); return noteText(code, cards); } /** * Main / Helper / Off for every graphics card (the mockup GpuRoles-mockup.html). One click changes a role; "Back to recommended" restores the * recommendation. Warnings are short and never block Load. `roles` are the roles in force (the recommendation until the person changes one). */ export function GpuRoles({ cards, roles, custom, recommendedRoles, vendor, onChange, onBack, memoryGb, ramGb, spillGb = 0, engineRunsCpuOnly = false, disabled = false, usedBy }: { cards: RoleCard[]; roles: Roles; /** The person changed a role: "Your choice", and "Back to recommended" is offered. */ custom: boolean; /** The roles Strata recommends (the tags and "Back to recommended" use them); the pure recommendation when not given. */ recommendedRoles?: Roles; vendor?: string | null; onChange: (next: Roles) => void; onBack: () => void; memoryGb?: number | null; ramGb?: number | null; /** About how many GB of the model are read from the drive with these cards (0 when everything fits or it is unknown). */ spillGb?: number; engineRunsCpuOnly?: boolean; disabled?: boolean; /** Names of the loaded models that already use a card ("In use by ..."). */ usedBy?: (card: number) => string[]; }) { const titleId = useId(); const [note, setNote] = useState(''); const rec = { ...recommend(cards, vendor), ...(recommendedRoles ? { roles: recommendedRoles } : {}) }; const single = cards.length === 1; const warns = warnings(roles, cards, { spillGb, engineRunsCpuOnly }); const cardWarn = (i: number) => warns.filter(w => w.card === i); const globalWarn = warns.filter(w => w.card === null); const lst = order(roles, cards); const change = (idx: number, role: Role) => { const out = setRole(roles, cards, idx, role); setNote(noteLine(out.note, cards)); onChange(out.roles); }; const back = () => { setNote(''); onBack(); }; const fix = (w: RoleWarning) => w.fix && change(w.fix.card, w.fix.role); const fixLabel = (w: RoleWarning) => { if (!w.fix) return ''; const target = cards.find(c => c.index === w.fix!.card); return w.fix.role === 'off' ? tr('Turn it off') : tr('Make {0} Main', [target ? shortName(target) : '']); }; const have = vramOn(roles, cards) + Math.max(0, (ramGb ?? 0) - 10); return
{tr('Graphics cards')} {custom ? : null}

{tr(custom ? 'Your choice.' : 'Recommended.')} {custom ? tr('You changed the roles. Strata will use exactly these.') : recommendationLine(cards, vendor)}

{cards.map(c => { const role = roles[c.index] ?? 'off', isRec = rec.roles[c.index] === role, w = cardWarn(c.index), other = usedBy?.(c.index) ?? []; const shared = !!(c.integrated || c.uma || c.unified_gb); const options: Role[] = single ? ['main', 'off'] : ['main', 'helper', 'off']; return
{gpuLabel(c)} · {c.name}{isRec ? {tr('Recommended')} : {tr('Recommended: {0}', [tr(ROLE_WORD[rec.roles[c.index] ?? 'off'])])}} {shared ? tr("Uses your computer's memory (built-in graphics)") : tr('{0} GB memory', [Math.round(c.vram_gb)])} · {tr(HINT[role])} {options.map(r => )}
{other.length ?

{tr('In use by {0}', [other.join(', ')])}

: null} {w.map(x =>

{warningText(x, cards)}

{x.fix ? fix(x)}>{fixLabel(x)} : null}
)}
; })}
{note ?

{note}

: null}
{globalWarn.map(x =>

{warningText(x, cards)}

{x.code === 'cpu_only' && custom ? {tr('Back to recommended')} : null}
)}
{tr('Details')}
{tr('Main')}
{tr('The card that does the final steps of every answer: it writes the next words, runs the draft-ahead step and checks it, and keeps your conversation. Pick your fastest card with plenty of memory. There is only ever one.')}
{tr('Helper')}
{tr('Holds a share of the model and its fast memory, so more of it fits on graphics cards instead of being read from your drive. Prompts are read about 20% faster.')}
{tr('Off')}
{tr('Strata does not touch this card. Good for a card you use for something else, such as a game or your screen.')}
{memoryGb ?
= 1 ? 'bad' : 'ok') : 'bad'}>
: null}
{lst.length ? tr('For experts: the engine starts on cards {0} (Main last).', [lst.join(',')]) : null} {custom ? '' : tr('Not saved while you keep the recommendation: Strata picks again if you add or remove a card.')}
; }