Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix import on windows #3229

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ Object.assign(console, log.functions)
app
.whenReady()
.then(() => {
log.info('Starting cortex with path:', cortexPath)
const command = `${cortexPath} -a 127.0.0.1 -p 1337`

log.info('Starting cortex with command:', command)
// init cortex
// running shell command cortex init -s
exec(`${cortexPath}`, (error, stdout, stderr) => {
exec(`${command}`, (error, stdout, stderr) => {
if (error) {
log.error(`error: ${error.message}`)
return
Expand Down
1 change: 1 addition & 0 deletions web/containers/Providers/DownloadEventListener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const DownloadEventListener: React.FC = () => {
const localImportEvents: DownloadState2[] = []
// filter out the import local events
for (const event of downloadEvents) {
console.debug('Receiving event', event)
if (
isAbsolutePath(event.id) &&
event.type === 'model' &&
Expand Down
12 changes: 6 additions & 6 deletions web/hooks/useCortex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,12 @@ const useCortex = () => {

const isSystemAlive = useCallback(async () => {
try {
await cortex.system.status();
return true;
} catch {
return false;
await cortex.system.status()
return true
} catch {
return false
}
}, [host])
}, [cortex.system])

return {
fetchAssistants,
Expand Down Expand Up @@ -354,7 +354,7 @@ const useCortex = () => {
createModel,
initializeEngine,
getEngineStatuses,
isSystemAlive
isSystemAlive,
}
}

Expand Down
28 changes: 17 additions & 11 deletions web/hooks/useDropModelBinaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,23 @@ export default function useDropModelBinaries() {
)
const supportedFiles = files.filter((file) => file.path.endsWith('.gguf'))

const importingModels: ImportingModel[] = supportedFiles.map((file) => ({
importId: file.path,
modelId: undefined,
name: file.name.replace('.gguf', ''),
description: '',
path: file.path,
tags: [],
size: file.size,
status: 'PREPARING',
format: 'gguf',
}))
const importingModels: ImportingModel[] = supportedFiles.map((file) => {
const normalizedPath = isWindows
? file.path.replace(/\\/g, '/')
: file.path

return {
importId: normalizedPath,
modelId: undefined,
name: normalizedPath.replace('.gguf', ''),
description: '',
path: file.path,
tags: [],
size: file.size,
status: 'PREPARING',
format: 'gguf',
}
})
if (unsupportedFiles.length > 0) {
snackbar({
description: `Only files with .gguf extension can be imported.`,
Expand Down
75 changes: 66 additions & 9 deletions web/screens/Settings/ChooseWhatToImportModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { useCallback } from 'react'

import { SelectFileOption } from '@janhq/core'
import { ImportingModel, SelectFileOption } from '@janhq/core'
import { Button, Modal } from '@janhq/joi'
import { useSetAtom, useAtomValue } from 'jotai'

import useImportModel, {
import { snackbar } from '@/containers/Toast'

import {
setImportModelStageAtom,
getImportModelStageAtom,
} from '@/hooks/useImportModel'

import { importingModelsAtom } from '@/helpers/atoms/Model.atom'

const ChooseWhatToImportModal = () => {
const setImportModelStage = useSetAtom(setImportModelStageAtom)
const setImportingModels = useSetAtom(importingModelsAtom)
const importModelStage = useAtomValue(getImportModelStageAtom)
const { sanitizeFilePaths } = useImportModel()

const onImportFileClick = useCallback(async () => {
const options: SelectFileOption = {
Expand All @@ -24,10 +28,36 @@ const ChooseWhatToImportModal = () => {
{ name: 'All Files', extensions: ['*'] },
],
}
const filePaths = await window.core?.api?.selectFiles(options)
const filePaths: string[] = await window.core?.api?.selectFiles(options)
if (!filePaths || filePaths.length === 0) return
sanitizeFilePaths(filePaths)
}, [sanitizeFilePaths])

const importingModels: ImportingModel[] = filePaths
.filter((path) => path.endsWith('.gguf'))
.map((path) => {
const normalizedPath = isWindows ? path.replace(/\\/g, '/') : path

return {
importId: normalizedPath,
modelId: undefined,
name: normalizedPath.replace('.gguf', ''),
description: '',
path: path,
tags: [],
size: 0,
status: 'PREPARING',
format: 'gguf',
}
})
if (importingModels.length < 1) {
snackbar({
description: `Only files with .gguf extension can be imported.`,
type: 'error',
})
return
}
setImportingModels(importingModels)
setImportModelStage('MODEL_SELECTED')
}, [setImportingModels, setImportModelStage])

const onImportFolderClick = useCallback(async () => {
const options: SelectFileOption = {
Expand All @@ -36,10 +66,37 @@ const ChooseWhatToImportModal = () => {
allowMultiple: true,
selectDirectory: true,
}
const filePaths = await window.core?.api?.selectFiles(options)
const filePaths: string[] = await window.core?.api?.selectFiles(options)
if (!filePaths || filePaths.length === 0) return
sanitizeFilePaths(filePaths)
}, [sanitizeFilePaths])

console.log('filePaths folder', filePaths)
const importingModels: ImportingModel[] = filePaths
.filter((path) => path.endsWith('.gguf'))
.map((path) => {
const normalizedPath = isWindows ? path.replace(/\\/g, '/') : path

return {
importId: normalizedPath,
modelId: undefined,
name: normalizedPath.replace('.gguf', ''),
description: '',
path: path,
tags: [],
size: 0,
status: 'PREPARING',
format: 'gguf',
}
})
if (importingModels.length < 1) {
snackbar({
description: `Only files with .gguf extension can be imported.`,
type: 'error',
})
return
}
setImportingModels(importingModels)
setImportModelStage('MODEL_SELECTED')
}, [setImportingModels, setImportModelStage])

return (
<Modal
Expand Down
2 changes: 1 addition & 1 deletion web/screens/Settings/ImportSuccessIcon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const ImportSuccessIcon: React.FC<Props> = ({ onEditModelClick }) => {
}

const SuccessIcon = React.memo(() => (
<div className="bg-primary flex h-8 w-8 items-center justify-center rounded-full text-white">
<div className="bg-primary flex h-8 w-8 items-center justify-center rounded-full">
<Check size={20} />
</div>
))
Expand Down
71 changes: 58 additions & 13 deletions web/screens/Settings/SelectingModelModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,51 @@ const SelectingModelModal: React.FC = () => {
const setImportingModels = useSetAtom(importingModelsAtom)
const importModelStage = useAtomValue(getImportModelStageAtom)
const { onDropModels } = useDropModelBinaries()

const onImportFileWindowsClick = useCallback(async () => {
const options: SelectFileOption = {
title: 'Select model files',
buttonLabel: 'Select',
allowMultiple: true,
filters: [
{ name: 'GGUF Files', extensions: ['gguf'] },
{ name: 'All Files', extensions: ['*'] },
],
}
const filePaths: string[] = await window.core?.api?.selectFiles(options)
if (!filePaths || filePaths.length === 0) return

const importingModels: ImportingModel[] = filePaths
.filter((path) => path.endsWith('.gguf'))
.map((path) => {
const normalizedPath = isWindows ? path.replace(/\\/g, '/') : path

return {
importId: normalizedPath,
modelId: undefined,
name: normalizedPath.replace('.gguf', ''),
description: '',
path: path,
tags: [],
size: 0,
status: 'PREPARING',
format: 'gguf',
}
})
if (importingModels.length < 1) {
snackbar({
description: `Only files with .gguf extension can be imported.`,
type: 'error',
})
return
}
setImportingModels(importingModels)
setImportModelStage('MODEL_SELECTED')
}, [setImportingModels, setImportModelStage])

const onSelectFileClick = useCallback(async () => {
if (isWindows) {
setImportModelStage('CHOOSE_WHAT_TO_IMPORT')
return
return onImportFileWindowsClick()
}
const options: SelectFileOption = {
title: 'Select model folders',
Expand All @@ -38,17 +79,21 @@ const SelectingModelModal: React.FC = () => {

const importingModels: ImportingModel[] = filePaths
.filter((path) => path.endsWith('.gguf'))
.map((path) => ({
importId: path,
modelId: undefined,
name: path.replace('.gguf', ''),
description: '',
path: path,
tags: [],
size: 0,
status: 'PREPARING',
format: 'gguf',
}))
.map((path) => {
const normalizedPath = isWindows ? path.replace(/\\/g, '/') : path

return {
importId: normalizedPath,
modelId: undefined,
name: normalizedPath.replace('.gguf', ''),
description: '',
path: path,
tags: [],
size: 0,
status: 'PREPARING',
format: 'gguf',
}
})
if (importingModels.length < 1) {
snackbar({
description: `Only files with .gguf extension can be imported.`,
Expand Down
Loading