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

View File

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