okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b3299a01
main
1import { localeTag } from '../lib/i18n' ; 2import { tr } from '../lib/i18n' ; 3import { useEffect , useState } from 'react' ; 4import { Btn , Dialog , ErrorNotice , Icon } from './ui' ; 5import { api } from '../lib/client' ; 6import { addLocalModel } from '../lib/store' ; 7import { sizeGb } from '../lib/format' ; 8import type { LocalModelFolder , LocalModelPreview } from '../lib/types' ; 9const fileSize = ( bytes :number ) => bytes < 1024 ** 2 ?` ${ Math . max ( 1 , Math . round ( bytes / 1024 ))} KB` :bytes < 1024 ** 3 ?` ${ Math . round ( bytes / 1024 ** 2 )} MB` :sizeGb ( bytes / 1024 ** 3 ); 10export function LocalModelImport ({ open, directory, onClose, onChoose} :{ 11open :boolean ; 12directory :string ; 13onClose :() => void ; 14onChoose :( id :string ) => void ; 15}) { 16const [ folder , setFolder ] = useState < LocalModelFolder | null >( null ), [ path , setPath ] = useState ( '' ), [ preview , setPreview ] = useState < LocalModelPreview | null >( null ); 17const [ name , setName ] = useState ( '' ), [ pending , setPending ] = useState ( false ), [ error , setError ] = useState ( '' ); 18useEffect (() => { 19if ( open ) { 20setPreview ( null ); 21setError ( '' ); 22void browse ( directory ); 23} 24}, [ open ]); 25async function browse ( input :string ) { 26setPending ( true ); 27setError ( '' ); 28try { 29const next = await api . get < LocalModelFolder >( `/api/local-models/browse?directory= ${ encodeURIComponent ( input )} ` ); 30setFolder ( next ); 31setPath ( next . directory ); 32setPreview ( null ); 33} 34catch ( e ) { 35setPath ( input ); 36setError ( e instanceof Error ?e . message :String ( e )); 37} 38finally { 39setPending ( false ); 40} 41} 42async function inspect ( file :string ) { 43setPending ( true ); 44setError ( '' ); 45try { 46const next = await api . post < LocalModelPreview >( '/api/local-models/inspect' , { path :file }); 47setPreview ( next ); 48setName ( next . variant . name ); 49} 50catch ( e ) { 51setError ( e instanceof Error ?e . message :String ( e )); 52} 53finally { 54setPending ( false ); 55} 56} 57async function useModel () { 58if ( ! preview ) 59return ; 60setPending ( true ); 61setError ( '' ); 62try { 63const id = await addLocalModel ( preview . variant . local ! . path , name ); 64onChoose ( id ); 65onClose (); 66} 67catch ( e ) { 68setError ( e instanceof Error ?e . message :String ( e )); 69} 70finally { 71setPending ( false ); 72} 73} 74const close = () => { 75if ( ! pending ) 76onClose (); 77}; 78return < Dialog open = { open } title = { tr ( "Use a local model" )} className = "local-model-dialog" onClose = { close } actions = {<>< Btn disabled = { pending } onClick = { preview ?() => { setPreview ( null ); setError ( '' ); } :close }>{ tr ( preview ?'Choose another file' :'Cancel' )}</ Btn >{ tr ( preview ?< Btn variant = "primary" icon = "models" disabled = { pending || ! name . trim ()} onClick = {() => void useModel ()}>{ tr ( pending ?'Adding…' :'Use this model' )}</ Btn > :null )}</>}> 79{ preview ?< div className = "local-model-review" > 80< div className = "local-model-review__identity" >< Icon name = "model-file" size = { 25 }/>< div >< strong >{ preview . variant . name }</ strong >< small >{ tr ( "Local GGUF \u00B7 " )}{ tr ( preview . variant . local ! . shards === 1 ?'One file' :` ${ preview . variant . local ! . shards } files found` )}</ small ></ div ></ div > 81< dl className = "local-model-facts" >< div >< dt >{ tr ( "Architecture" )}</ dt >< dd >{ tr ( preview . variant . local ! . architecture )}</ dd ></ div >< div >< dt >{ tr ( "Quantization" )}</ dt >< dd >{ tr ( preview . variant . quant )}</ dd ></ div >< div >< dt >{ tr ( "Size" )}</ dt >< dd >{ tr ( fileSize ( preview . variant . local ! . files . reduce (( n , f ) => n + f . bytes , 0 )))}</ dd ></ div >< div >< dt >{ tr ( "Context limit" )}</ dt >< dd >{ tr ( Math . floor ( preview . variant . local ! . contextLength / 1024 ). toLocaleString ( localeTag ()))}{ tr ( "K tokens" )}</ dd ></ div ></ dl > 82< label className = "local-model-name" >{ tr ( "Display name" )}< input className = "st-input" value = { name } maxLength = { 160 } onChange = { e => setName ( e . target . value )} disabled = { pending }/></ label > 83< details className = "local-model-files" >< summary >{ tr ( preview . variant . local ! . shards === 1 ?'File location' :'Model files' )}</ summary >< code >{ preview . variant . local ! . path }</ code >{ tr ( preview . files . length > 1 ?< ul >{ tr ( preview . files . map ( file => < li key = { file }>{ tr ( file )}</ li >))}</ ul > :null )}</ details > 84< p className = "small muted" >{ tr ( "Strata uses these files in place. You can load the model after adding it." )}</ p > 85< p className = "local-model-compatibility small" >< Icon name = "info" size = { 16 }/>< span >{ tr ( "Custom model \u00B7 engine compatibility is checked when loading. Memory estimates use the file\u2019s metadata." )}</ span ></ p > 86{ tr ( ! preview . variant . local ! . hasChatTemplate ?< p className = "small muted" >{ tr ( "No chat template is declared; response formatting depends on the engine." )}</ p > :null )} 87</ div > :< div className = "local-model-browser" > 88< p className = "small muted" >{ tr ( "Choose the model’s .gguf file. For a split model, choose any shard; Strata finds the first and checks the full set." )}</ p > 89< form className = "local-model-browser__path" onSubmit = { e => { 90e . preventDefault (); 91if ( ! pending ) 92void ( / \.gguf$ / i . test ( path . trim ()) ?inspect ( path ) :browse ( path )); 93}}>< input className = "st-input" aria-label = { tr ( "Model file or folder path" )} placeholder = { tr ( "Model file or folder path" )} value = { path } disabled = { pending } onChange = { e => setPath ( e . target . value )}/>< Btn type = "submit" disabled = { pending || ! path . trim ()}>{ tr ( "Go" )}</ Btn ></ form > 94< div className = "local-model-browser__navigation" >< div >{ folder ?. shortcuts . map ( shortcut => < Btn key = { shortcut . path } size = "sm" icon = "folder" disabled = { pending } onClick = {() => void browse ( shortcut . path )}>{ tr ( shortcut . name )}</ Btn >)}</ div >< Btn size = "sm" disabled = { pending || ! folder ?. parent } onClick = {() => folder ?. parent && void browse ( folder . parent )}>{ tr ( "Up a folder" )}</ Btn ></ div > 95< div className = "local-model-browser__list" aria-label = { tr ( "Local model files" )} aria-busy = { pending }>{ pending ?< p className = "small muted" role = "status" >{ tr ( "Reading files\u2026" )}</ p > :folder ?. entries . length ?folder . entries . map ( entry => < button key = { entry . path } className = "local-model-file" aria-label = { tr ( "{0} {1}" , [ entry . kind === 'folder' ?tr ( 'Open folder' ) :tr ( 'Choose file' ), entry . name ])} onClick = {() => void ( entry . kind === 'folder' ?browse ( entry . path ) :inspect ( entry . path ))}>< Icon name = { entry . kind === 'folder' ?'folder' :'model-file' } size = { 19 }/>< span title = { entry . name }>{ entry . name }</ span >{ tr ( entry . kind === 'folder' ?< Icon name = "chevron" size = { 15 }/> :< small >{ tr ( fileSize ( entry . bytes ?? 0 ))}</ small >)}</ button >) :< div className = "local-model-browser__empty" >< Icon name = "folder" size = { 26 }/>< strong >{ tr ( "No GGUF files here" )}</ strong >< p className = "small muted" >{ tr ( "Open another folder or enter a model\u2019s full path above." )}</ p ></ div >}</ div > 96{ tr ( folder ?. truncated ?< p className = "small muted" >{ tr ( "Showing the first 1,000 items. Enter a path to open a specific file." )}</ p > :null )} 97< p className = "xs muted" >{ tr ( "Only folders and GGUF files are shown. LoRA adapters and image projectors need separate support." )}</ p > 98</ div >} 99{ tr ( error ?< ErrorNotice message = { error } next = { tr ( "Choose a complete text model GGUF file." )}/> :null )} 100</ Dialog >; 101}