Skip to content

Commit

Permalink
fix: GET /models does not work due to new default model dir (janhq#1392)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan authored Jan 5, 2024
1 parent b639c4e commit e9d55e2
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions core/src/node/api/common/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,26 @@ const path = join(os.homedir(), 'jan')

export const getBuilder = async (configuration: RouteConfiguration) => {
const directoryPath = join(path, configuration.dirName)

try {
if (!(await fs.existsSync(directoryPath))) {
if (!fs.existsSync(directoryPath)) {
console.debug('model folder not found')
return []
}

const files: string[] = await fs.readdirSync(directoryPath)
const files: string[] = fs.readdirSync(directoryPath)

const allDirectories: string[] = []
for (const file of files) {
if (file === '.DS_Store') continue
allDirectories.push(file)
}

const readJsonPromises = allDirectories.map(async (dirName) => {
const jsonPath = join(directoryPath, dirName, configuration.metadataFileName)
return await readModelMetadata(jsonPath)
})

const results = await Promise.all(readJsonPromises)
const results = allDirectories
.map((dirName) => {
const jsonPath = join(directoryPath, dirName, configuration.metadataFileName)
return readModelMetadata(jsonPath)
})
.filter((data) => !!data)
const modelData = results
.map((result: any) => {
try {
Expand All @@ -52,8 +51,12 @@ export const getBuilder = async (configuration: RouteConfiguration) => {
}
}

const readModelMetadata = async (path: string) => {
return fs.readFileSync(path, 'utf-8')
const readModelMetadata = (path: string): string | undefined => {
if (fs.existsSync(path)) {
return fs.readFileSync(path, 'utf-8')
} else {
return undefined
}
}

export const retrieveBuilder = async (configuration: RouteConfiguration, id: string) => {
Expand Down Expand Up @@ -99,7 +102,7 @@ export const getMessages = async (threadId: string) => {
const threadDirPath = join(path, 'threads', threadId)
const messageFile = 'messages.jsonl'
try {
const files: string[] = await fs.readdirSync(threadDirPath)
const files: string[] = fs.readdirSync(threadDirPath)
if (!files.includes(messageFile)) {
throw Error(`${threadDirPath} not contains message file`)
}
Expand Down

0 comments on commit e9d55e2

Please sign in to comment.