Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

perf(models): replace dropdown compat indicator with hub estimator

Akarshan1f0f047a4e58
diff --git a/web-app/src/containers/ModelSupportStatus.tsx b/web-app/src/containers/ModelSupportStatus.tsx
index 6b8d7930b..6de68e85b 100644
--- a/web-app/src/containers/ModelSupportStatus.tsx
+++ b/web-app/src/containers/ModelSupportStatus.tsx
@@ -1,5 +1,4 @@
-import { useCallback, useEffect, useState } from 'react'
-import { invoke } from '@tauri-apps/api/core'
+import { useEffect, useState } from 'react'
import { cn } from '@/lib/utils'
import {
Tooltip,
@@ -7,8 +6,13 @@ import {
TooltipProvider,
TooltipTrigger,
} from '@/components/ui/tooltip'
-import { getJanDataFolderPath, joinPath, fs } from '@janhq/core'
import { useServiceHub } from '@/hooks/useServiceHub'
+import { useHardware } from '@/hooks/useHardware'
+import {
+ DEFAULT_CTX_LENGTH,
+ estimateModelFit,
+ type FitTier,
+} from '@/lib/modelCompatibility'
interface ModelSupportStatusProps {
modelId: string | undefined
@@ -17,29 +21,14 @@ interface ModelSupportStatusProps {
className?: string
}
-type CheckResult = {
- status: 'RED' | 'YELLOW' | 'GREEN' | 'GREY' | null
- effectiveCtx: number
-}
-
-const readTrainContext = async (
- modelPath: string
-): Promise<number | undefined> => {
- try {
- const result = await invoke<{ metadata?: Record<string, string> }>(
- 'plugin:llamacpp|read_gguf_metadata',
- { path: modelPath }
- )
- const meta = result?.metadata
- const arch = meta?.['general.architecture']
- if (!arch) return undefined
- const raw = meta?.[`${arch}.context_length`]
- const parsed = raw ? parseInt(raw, 10) : NaN
- return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined
- } catch (e) {
- console.error('Error reading GGUF context length:', e)
- return undefined
- }
+const TIER_STYLES: Record<FitTier, { dot: string; label: string }> = {
+ green: { dot: 'bg-green-500', label: 'Should run comfortably on your device' },
+ yellow: {
+ dot: 'bg-yellow-500',
+ label: 'Will run but leaves little memory headroom',
+ },
+ red: { dot: 'bg-red-500', label: 'Likely exceeds your available memory' },
+ unknown: { dot: 'bg-secondary', label: 'Fit unknown' },
}
export const ModelSupportStatus = ({
@@ -48,141 +37,45 @@ export const ModelSupportStatus = ({
contextSize,
className,
}: ModelSupportStatusProps) => {
- const [modelSupportStatus, setModelSupportStatus] = useState<
- 'RED' | 'YELLOW' | 'GREEN' | 'LOADING' | null | 'GREY'
- >(null)
- const [effectiveCtx, setEffectiveCtx] = useState<number>(contextSize)
const serviceHub = useServiceHub()
+ const hardwareData = useHardware((s) => s.hardwareData)
+ const [sizeBytes, setSizeBytes] = useState<number | null>(null)
- // Helper function to check model support with proper path resolution
- const checkModelSupportWithPath = useCallback(
- async (id: string, ctxSize: number): Promise<CheckResult> => {
- try {
- const janDataFolder = await getJanDataFolderPath()
-
- // First try the standard downloaded model path
- const ggufModelPath = await joinPath([
- janDataFolder,
- 'llamacpp',
- 'models',
- id,
- 'model.gguf',
- ])
-
- let actualModelPath: string | null = null
- if (await fs.existsSync(ggufModelPath)) {
- actualModelPath = ggufModelPath
- } else {
- // If model.gguf doesn't exist, try reading from model.yml (for imported models)
- const modelConfigPath = await joinPath([
- janDataFolder,
- 'llamacpp',
- 'models',
- id,
- 'model.yml',
- ])
-
- if (!(await fs.existsSync(modelConfigPath))) {
- console.error(
- `Neither model.gguf nor model.yml found for model: ${id}`
- )
- return { status: null, effectiveCtx: ctxSize }
- }
-
- const modelConfig = await serviceHub
- .app()
- .readYaml<{ model_path: string }>(
- `llamacpp/models/${id}/model.yml`
- )
-
- actualModelPath =
- modelConfig.model_path.startsWith('/') ||
- modelConfig.model_path.match(/^[A-Za-z]:/)
- ? modelConfig.model_path
- : await joinPath([janDataFolder, modelConfig.model_path])
- }
-
- const trainCtx = await readTrainContext(actualModelPath)
- const ctxForCheck = trainCtx
- ? Math.min(ctxSize, trainCtx)
- : ctxSize
- const status = await serviceHub
- .models()
- .isModelSupported(actualModelPath, ctxForCheck)
- return { status, effectiveCtx: trainCtx ?? ctxSize }
- } catch (error) {
- console.error(
- 'Error checking model support with path resolution:',
- error
- )
- return { status: null, effectiveCtx: ctxSize }
- }
- },
- [serviceHub]
- )
-
- // Helper function to get icon color based on model support status
- const getStatusColor = (): string => {
- switch (modelSupportStatus) {
- case 'GREEN':
- return 'bg-green-500'
- case 'YELLOW':
- return 'bg-yellow-500'
- case 'RED':
- return 'bg-red-500'
- case 'LOADING':
- return 'bg-secondary'
- default:
- return 'bg-secondary'
+ useEffect(() => {
+ if (!modelId || provider !== 'llamacpp') {
+ setSizeBytes(null)
+ return
}
- }
-
- // Helper function to get tooltip text based on model support status
- const getStatusTooltip = (): string => {
- switch (modelSupportStatus) {
- case 'GREEN':
- return `Works Well on your device (ctx: ${effectiveCtx})`
- case 'YELLOW':
- return `Might work on your device (ctx: ${effectiveCtx})`
- case 'RED':
- return `Doesn't work on your device (ctx: ${effectiveCtx})`
- case 'LOADING':
- return 'Checking device compatibility...'
- default:
- return 'Unknown'
+ let cancelled = false
+ serviceHub
+ .models()
+ .fetchModels()
+ .then((infos) => {
+ if (cancelled) return
+ const match = infos.find(
+ (i) => i.id === modelId && i.providerId === provider
+ )
+ setSizeBytes(match?.sizeBytes ?? null)
+ })
+ .catch(() => {
+ if (!cancelled) setSizeBytes(null)
+ })
+ return () => {
+ cancelled = true
}
- }
+ }, [modelId, provider, serviceHub])
- // Check model support when model changes
- useEffect(() => {
- const checkModelSupport = async () => {
- if (modelId && provider === 'llamacpp') {
- // Set loading state immediately
- setModelSupportStatus('LOADING')
- try {
- const { status, effectiveCtx: ctx } = await checkModelSupportWithPath(
- modelId,
- contextSize
- )
- setEffectiveCtx(ctx)
- setModelSupportStatus(status)
- } catch (error) {
- console.error('Error checking model support:', error)
- setModelSupportStatus('RED')
- }
- } else {
- // Only show status for llamacpp models since isModelSupported is specific to llamacpp
- setModelSupportStatus(null)
- }
- }
+ if (!modelId || provider !== 'llamacpp') return null
- checkModelSupport()
- }, [modelId, provider, contextSize, checkModelSupportWithPath])
+ const tier = estimateModelFit(
+ sizeBytes,
+ contextSize || DEFAULT_CTX_LENGTH,
+ hardwareData
+ )
+ if (tier === 'unknown') return null
- // Don't render anything if no status or not llamacpp
- if (!modelSupportStatus || provider !== 'llamacpp') {
- return null
- }
+ const style = TIER_STYLES[tier]
+ const tooltip = `${style.label} (estimated, ctx: ${contextSize || DEFAULT_CTX_LENGTH})`
return (
<TooltipProvider>
@@ -191,15 +84,14 @@ export const ModelSupportStatus = ({
<div
className={cn(
'size-2 flex items-center justify-center rounded-full',
- modelSupportStatus === 'LOADING'
- ? 'size-2.5 border border-t-transparent animate-spin'
- : getStatusColor(),
+ style.dot,
className
)}
+ aria-label={`Device compatibility: ${tier}`}
/>
</TooltipTrigger>
<TooltipContent>
- <p>{getStatusTooltip()}</p>
+ <p>{tooltip}</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>