Skip to content

Commit

Permalink
fix: should migrate symlink models
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan committed Aug 1, 2024
1 parent e7486ae commit 39f2f97
Showing 1 changed file with 40 additions and 33 deletions.
73 changes: 40 additions & 33 deletions electron/handlers/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,9 @@ export function handleAppIPCs() {
for (const modelName of allModelFolders) {
const modelFolderPath = join(janModelFolderPath, modelName)
const filesInModelFolder = readdirSync(modelFolderPath)
if (filesInModelFolder.length <= 1) {
// if only have model.json file or empty folder, we skip it
continue
}

const destinationPath = join(cortexModelFolderPath, modelName)

// create folder if not exist
if (!existsSync(destinationPath)) {
mkdirSync(destinationPath, { recursive: true })
}

try {
const modelJsonFullPath = join(
janModelFolderPath,
Expand All @@ -256,10 +247,23 @@ export function handleAppIPCs() {

const model = JSON.parse(readFileSync(modelJsonFullPath, 'utf-8'))
const fileNames: string[] = model.sources.map((x: any) => x.filename)
// prepend fileNames with cortexModelFolderPath
const files = fileNames.map((x: string) =>
join(cortexModelFolderPath, model.id, x)
)
let files: string[] = []

if(filesInModelFolder.length > 1) {
// prepend fileNames with cortexModelFolderPath
files = fileNames.map((x: string) =>
join(cortexModelFolderPath, model.id, x)
)
} else if(model.sources.length && !/^(http|https):\/\/[^/]+\/.*/.test(model.sources[0].url)) {
// Symlink case
files = [ model.sources[0].url ]
} else continue;

// create folder if not exist
// only for local model files
if (!existsSync(destinationPath) && filesInModelFolder.length > 1) {
mkdirSync(destinationPath, { recursive: true })
}

const engine = 'cortex.llamacpp'

Expand Down Expand Up @@ -288,24 +292,27 @@ export function handleAppIPCs() {
max_tokens: model.parameters?.max_tokens ?? 2048,
stream: model.parameters?.stream ?? true,
}

const { err } = await reflect({
src: modelFolderPath,
dest: destinationPath,
recursive: true,
exclude: ['model.json'],
delete: false,
overwrite: true,
errorOnExist: false,
})
if (err) console.error(err)
else {
// create the model.yml file
const modelYamlData = dump(updatedModelFormat)
const modelYamlPath = join(cortexModelFolderPath, `${modelName}.yaml`)

writeFileSync(modelYamlPath, modelYamlData)
if(filesInModelFolder.length > 1 ) {
const { err } = await reflect({
src: modelFolderPath,
dest: destinationPath,
recursive: true,
exclude: ['model.json'],
delete: false,
overwrite: true,
errorOnExist: false,
})

if (err) {
console.error(err);
continue;
}
}
// create the model.yml file
const modelYamlData = dump(updatedModelFormat)
const modelYamlPath = join(cortexModelFolderPath, `${modelName}.yaml`)

writeFileSync(modelYamlPath, modelYamlData)
} catch (err) {
console.error(err)
}
Expand Down Expand Up @@ -336,9 +343,9 @@ export function handleAppIPCs() {
'messages.jsonl'
)
const lines = readFileSync(messageFullPath, 'utf-8')
.toString()
.split('\n')
.filter((line: any) => line !== '')
.toString()
.split('\n')
.filter((line: any) => line !== '')
for (const line of lines) {
messages.push(JSON.parse(line))
}
Expand Down

0 comments on commit 39f2f97

Please sign in to comment.