chore: make utils sanitize modelId

This commit is contained in:
Faisal Amir 2025-08-14 09:42:47 +07:00
parent 1f0e79e934
commit ace8214d4d
3 changed files with 11 additions and 14 deletions

View File

@ -155,3 +155,7 @@ export function formatDuration(startTime: number, endTime?: number): string {
return `${durationMs}ms`
}
}
export function sanitizeModelId(modelId: string): string {
return modelId.replace(/[^a-zA-Z0-9/_\-.]/g, '')
}

View File

@ -3,7 +3,7 @@ import { useVirtualizer } from '@tanstack/react-virtual'
import { createFileRoute, useNavigate, useSearch } from '@tanstack/react-router'
import { route } from '@/constants/routes'
import { useModelSources } from '@/hooks/useModelSources'
import { cn } from '@/lib/utils'
import { cn, sanitizeModelId } from '@/lib/utils'
import {
useState,
useMemo,
@ -287,8 +287,8 @@ function Hub() {
const handleDownload = () => {
// Immediately set local downloading state
addLocalDownloadingModel(modelId.replace(/[^a-zA-Z0-9/_\-.]/g, ''))
pullModel(modelId.replace(/[^a-zA-Z0-9/_\-.]/g, ''), modelUrl)
addLocalDownloadingModel(sanitizeModelId(modelId))
pullModel(sanitizeModelId(modelId), modelUrl)
}
return (
@ -745,16 +745,10 @@ function Hub() {
title={t('hub:downloadModel')}
onClick={() => {
addLocalDownloadingModel(
variant.model_id.replace(
/[^a-zA-Z0-9/_\-.]/g,
''
)
sanitizeModelId(variant.model_id)
)
pullModel(
variant.model_id.replace(
/[^a-zA-Z0-9/_\-.]/g,
''
),
sanitizeModelId(variant.model_id),
variant.path
)
}}

View File

@ -5,6 +5,7 @@ import {
SettingComponentProps,
} from '@janhq/core'
import { Model as CoreModel } from '@janhq/core'
import { sanitizeModelId } from '@/lib/utils'
// Types for model catalog
export interface ModelQuant {
model_id: string
@ -162,9 +163,7 @@ export const convertHfRepoToCatalogModel = (
}
// Generate model_id from filename (remove .gguf extension, case-insensitive)
const modelId = file.rfilename
.replace(/\.gguf$/i, '')
.replace(/[^a-zA-Z0-9/_\-.]/g, '')
const modelId = sanitizeModelId(file.rfilename.replace(/\.gguf$/i, ''))
return {
model_id: modelId,