Skip to content

Commit

Permalink
feat: add import model (janhq#2104)
Browse files Browse the repository at this point in the history
Signed-off-by: James <[email protected]>
Co-authored-by: James <[email protected]>
  • Loading branch information
namchuai and James authored Feb 26, 2024
1 parent 92edd85 commit 773963a
Show file tree
Hide file tree
Showing 68 changed files with 2,214 additions and 620 deletions.
15 changes: 14 additions & 1 deletion core/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum NativeRoute {
openAppDirectory = 'openAppDirectory',
openFileExplore = 'openFileExplorer',
selectDirectory = 'selectDirectory',
selectModelFiles = 'selectModelFiles',
relaunch = 'relaunch',
}

Expand Down Expand Up @@ -46,6 +47,13 @@ export enum DownloadEvent {
onFileDownloadSuccess = 'onFileDownloadSuccess',
}

export enum LocalImportModelEvent {
onLocalImportModelUpdate = 'onLocalImportModelUpdate',
onLocalImportModelError = 'onLocalImportModelError',
onLocalImportModelSuccess = 'onLocalImportModelSuccess',
onLocalImportModelFinished = 'onLocalImportModelFinished',
}

export enum ExtensionRoute {
baseExtensions = 'baseExtensions',
getActiveExtensions = 'getActiveExtensions',
Expand All @@ -67,6 +75,7 @@ export enum FileSystemRoute {
}
export enum FileManagerRoute {
syncFile = 'syncFile',
copyFile = 'copyFile',
getJanDataFolderPath = 'getJanDataFolderPath',
getResourcePath = 'getResourcePath',
getUserHomePath = 'getUserHomePath',
Expand Down Expand Up @@ -126,4 +135,8 @@ export const CoreRoutes = [
]

export const APIRoutes = [...CoreRoutes, ...Object.values(NativeRoute)]
export const APIEvents = [...Object.values(AppEvent), ...Object.values(DownloadEvent)]
export const APIEvents = [
...Object.values(AppEvent),
...Object.values(DownloadEvent),
...Object.values(LocalImportModelEvent),
]
4 changes: 3 additions & 1 deletion core/src/extensions/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseExtension, ExtensionTypeEnum } from '../extension'
import { Model, ModelInterface } from '../index'
import { ImportingModel, Model, ModelInterface, OptionType } from '../index'

/**
* Model extension for managing models.
Expand All @@ -21,4 +21,6 @@ export abstract class ModelExtension extends BaseExtension implements ModelInter
abstract saveModel(model: Model): Promise<void>
abstract getDownloadedModels(): Promise<Model[]>
abstract getConfiguredModels(): Promise<Model[]>
abstract importModels(models: ImportingModel[], optionType: OptionType): Promise<void>
abstract updateModelInfo(modelInfo: Partial<Model>): Promise<Model>
}
11 changes: 9 additions & 2 deletions core/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,20 @@ const syncFile: (src: string, dest: string) => Promise<any> = (src, dest) =>
*/
const copyFileSync = (...args: any[]) => global.core.api?.copyFileSync(...args)

const copyFile: (src: string, dest: string) => Promise<void> = (src, dest) =>
global.core.api?.copyFile(src, dest)

/**
* Gets the file's stats.
*
* @param path - The path to the file.
* @param outsideJanDataFolder - Whether the file is outside the Jan data folder.
* @returns {Promise<FileStat>} - A promise that resolves with the file's stats.
*/
const fileStat: (path: string) => Promise<FileStat | undefined> = (path) =>
global.core.api?.fileStat(path)
const fileStat: (path: string, outsideJanDataFolder?: boolean) => Promise<FileStat | undefined> = (
path,
outsideJanDataFolder
) => global.core.api?.fileStat(path, outsideJanDataFolder)

// TODO: Export `dummy` fs functions automatically
// Currently adding these manually
Expand All @@ -90,6 +96,7 @@ export const fs = {
unlinkSync,
appendFileSync,
copyFileSync,
copyFile,
syncFile,
fileStat,
writeBlob,
Expand Down
2 changes: 1 addition & 1 deletion core/src/node/api/processors/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class Downloader implements Processor {
fileName,
downloadState: 'downloading',
}
console.log('progress: ', downloadState)
console.debug('progress: ', downloadState)
observer?.(DownloadEvent.onFileDownloadUpdate, downloadState)
DownloadManager.instance.downloadProgressMap[modelId] = downloadState
})
Expand Down
19 changes: 16 additions & 3 deletions core/src/node/api/processors/fsExt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { join } from 'path'
import fs from 'fs'
import { FileManagerRoute } from '../../../api'
import { appResourcePath, normalizeFilePath } from '../../helper/path'
import { getJanDataFolderPath, getJanDataFolderPath as getPath } from '../../helper'
import { Processor } from './Processor'
Expand Down Expand Up @@ -48,10 +47,12 @@ export class FSExt implements Processor {
}

// handle fs is directory here
fileStat(path: string) {
fileStat(path: string, outsideJanDataFolder?: boolean) {
const normalizedPath = normalizeFilePath(path)

const fullPath = join(getJanDataFolderPath(), normalizedPath)
const fullPath = outsideJanDataFolder
? normalizedPath
: join(getJanDataFolderPath(), normalizedPath)
const isExist = fs.existsSync(fullPath)
if (!isExist) return undefined

Expand All @@ -75,4 +76,16 @@ export class FSExt implements Processor {
console.error(`writeFile ${path} result: ${err}`)
}
}

copyFile(src: string, dest: string): Promise<void> {
return new Promise((resolve, reject) => {
fs.copyFile(src, dest, (err) => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
}
}
1 change: 1 addition & 0 deletions core/src/types/model/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './modelEntity'
export * from './modelInterface'
export * from './modelEvent'
export * from './modelImport'
22 changes: 22 additions & 0 deletions core/src/types/model/modelImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export type OptionType = 'SYMLINK' | 'MOVE_BINARY_FILE'

export type ModelImportOption = {
type: OptionType
title: string
description: string
}

export type ImportingModelStatus = 'PREPARING' | 'IMPORTING' | 'IMPORTED' | 'FAILED'

export type ImportingModel = {
importId: string
modelId: string | undefined
name: string
description: string
path: string
tags: string[]
size: number
status: ImportingModelStatus
format: string
percentage?: number
}
18 changes: 18 additions & 0 deletions electron/handlers/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,22 @@ export function handleAppIPCs() {
return filePaths[0]
}
})

ipcMain.handle(NativeRoute.selectModelFiles, async () => {
const mainWindow = WindowManager.instance.currentWindow
if (!mainWindow) {
console.error('No main window found')
return
}
const { canceled, filePaths } = await dialog.showOpenDialog(mainWindow, {
title: 'Select model files',
buttonLabel: 'Select',
properties: ['openFile', 'multiSelections'],
})
if (canceled) {
return
} else {
return filePaths
}
})
}
5 changes: 2 additions & 3 deletions electron/utils/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ export const setupReactDevTool = async () => {
) // Don't use import on top level, since the installer package is dev-only
try {
const name = await installExtension(REACT_DEVELOPER_TOOLS)
console.log(`Added Extension: ${name}`)
console.debug(`Added Extension: ${name}`)
} catch (err) {
console.log('An error occurred while installing devtools:')
console.error(err)
console.error('An error occurred while installing devtools:', err)
// Only log the error and don't throw it because it's not critical
}
}
Expand Down
4 changes: 2 additions & 2 deletions electron/utils/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function cleanLogs(
console.error('Error deleting log file:', err)
return
}
console.log(
console.debug(
`Deleted log file due to exceeding size limit: ${filePath}`
)
})
Expand All @@ -52,7 +52,7 @@ export function cleanLogs(
console.error('Error deleting log file:', err)
return
}
console.log(`Deleted old log file: ${filePath}`)
console.debug(`Deleted old log file: ${filePath}`)
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export function updateCudaExistence(

data['cuda'].exist = cudaExists
data['cuda'].version = cudaVersion
console.log(data['is_initial'], data['gpus_in_use'])
console.debug(data['is_initial'], data['gpus_in_use'])
if (cudaExists && data['is_initial'] && data['gpus_in_use'].length > 0) {
data.run_mode = 'gpu'
}
Expand Down
Loading

0 comments on commit 773963a

Please sign in to comment.