chore: try catch legacy assistant creation

This commit is contained in:
Louis 2024-10-21 21:42:55 +07:00
parent 718ee8dfa9
commit 523c745150
No known key found for this signature in database
GPG Key ID: 44FA9F4D33C37DE2
2 changed files with 73 additions and 67 deletions

View File

@ -63,39 +63,46 @@ export default class JanAssistantExtension extends AssistantExtension {
}
async getAssistants(): Promise<Assistant[]> {
// get all the assistant directories
// get all the assistant metadata json
const results: Assistant[] = []
const allFileName: string[] = await fs.readdirSync(
JanAssistantExtension._homeDir
)
for (const fileName of allFileName) {
const filePath = await joinPath([
JanAssistantExtension._homeDir,
fileName,
])
try {
// get all the assistant directories
// get all the assistant metadata json
const results: Assistant[] = []
if (!(await fs.fileStat(filePath))?.isDirectory) continue
const jsonFiles: string[] = (await fs.readdirSync(filePath)).filter(
(file: string) => file === 'assistant.json'
const allFileName: string[] = await fs.readdirSync(
JanAssistantExtension._homeDir
)
if (jsonFiles.length !== 1) {
// has more than one assistant file -> ignore
continue
for (const fileName of allFileName) {
const filePath = await joinPath([
JanAssistantExtension._homeDir,
fileName,
])
if (!(await fs.fileStat(filePath))?.isDirectory) continue
const jsonFiles: string[] = (await fs.readdirSync(filePath)).filter(
(file: string) => file === 'assistant.json'
)
if (jsonFiles.length !== 1) {
// has more than one assistant file -> ignore
continue
}
const content = await fs.readFileSync(
await joinPath([filePath, jsonFiles[0]]),
'utf-8'
)
const assistant: Assistant =
typeof content === 'object' ? content : JSON.parse(content)
results.push(assistant)
}
const content = await fs.readFileSync(
await joinPath([filePath, jsonFiles[0]]),
'utf-8'
)
const assistant: Assistant =
typeof content === 'object' ? content : JSON.parse(content)
results.push(assistant)
return results
} catch (err) {
console.debug(err)
return [this.defaultAssistant]
}
return results
}
async deleteAssistant(assistant: Assistant): Promise<void> {
@ -112,39 +119,39 @@ export default class JanAssistantExtension extends AssistantExtension {
}
private async createJanAssistant(): Promise<void> {
const janAssistant: Assistant = {
avatar: '',
thread_location: undefined,
id: 'jan',
object: 'assistant',
created_at: Date.now(),
name: 'Jan',
description: 'A default assistant that can use all downloaded models',
model: '*',
instructions: '',
tools: [
{
type: 'retrieval',
enabled: false,
useTimeWeightedRetriever: false,
settings: {
top_k: 2,
chunk_size: 1024,
chunk_overlap: 64,
retrieval_template: `Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
await this.createAssistant(this.defaultAssistant)
}
private defaultAssistant: Assistant = {
avatar: '',
thread_location: undefined,
id: 'jan',
object: 'assistant',
created_at: Date.now(),
name: 'Jan',
description: 'A default assistant that can use all downloaded models',
model: '*',
instructions: '',
tools: [
{
type: 'retrieval',
enabled: false,
useTimeWeightedRetriever: false,
settings: {
top_k: 2,
chunk_size: 1024,
chunk_overlap: 64,
retrieval_template: `Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
----------------
CONTEXT: {CONTEXT}
----------------
QUESTION: {QUESTION}
----------------
Helpful Answer:`,
},
},
],
file_ids: [],
metadata: undefined,
}
await this.createAssistant(janAssistant)
},
],
file_ids: [],
metadata: undefined,
}
}

View File

@ -87,23 +87,22 @@ export default class JanModelExtension extends ModelExtension {
* should compare and try import
*/
let currentModels: Model[] = []
if (!localStorage.getItem(ExtensionEnum.downloadedModels)) {
// Updated from an older version than 0.5.5
// Scan through the models folder and import them (Legacy flow)
// Return models immediately
currentModels = await scanModelsFolder().then((models) => {
return models ?? []
})
} else {
try {
try {
if (!localStorage.getItem(ExtensionEnum.downloadedModels)) {
// Updated from an older version than 0.5.5
// Scan through the models folder and import them (Legacy flow)
// Return models immediately
currentModels = await scanModelsFolder().then((models) => {
return models ?? []
})
} else {
currentModels = JSON.parse(
localStorage.getItem(ExtensionEnum.downloadedModels)
) as Model[]
} catch (e) {
currentModels = []
console.error(e)
}
} catch (e) {
currentModels = []
console.error(e)
}
/**