fix(#566): jan cannot retrieve the conversations (#570)

Signed-off-by: James <james@jan.ai>
This commit is contained in:
NamH 2023-11-09 11:43:40 +07:00 committed by GitHub
parent f5e39fe2d7
commit 59ea9b3639
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 28 deletions

View File

@ -0,0 +1,8 @@
{
"semi": false,
"singleQuote": true,
"quoteProps": "consistent",
"trailingComma": "es5",
"endOfLine": "auto",
"plugins": ["prettier-plugin-tailwindcss"]
}

View File

@ -11,7 +11,8 @@
"scripts": { "scripts": {
"build": "tsc -b . && webpack --config webpack.config.js", "build": "tsc -b . && webpack --config webpack.config.js",
"postinstall": "rimraf *.tgz --glob && npm run build", "postinstall": "rimraf *.tgz --glob && npm run build",
"build:publish": "npm pack && cpx *.tgz ../../electron/core/pre-install" "build:publish": "npm pack && cpx *.tgz ../../electron/core/pre-install",
"build:debug": "rimraf *.tgz --glob && npm run build && npm pack"
}, },
"exports": { "exports": {
".": "./dist/index.js", ".": "./dist/index.js",

View File

@ -1,6 +1,6 @@
import { PluginType, fs } from "@janhq/core"; import { PluginType, fs } from '@janhq/core'
import { ConversationalPlugin } from "@janhq/core/lib/plugins"; import { ConversationalPlugin } from '@janhq/core/lib/plugins'
import { Conversation } from "@janhq/core/lib/types"; import { Conversation } from '@janhq/core/lib/types'
/** /**
* JSONConversationalPlugin is a ConversationalPlugin implementation that provides * JSONConversationalPlugin is a ConversationalPlugin implementation that provides
@ -11,44 +11,52 @@ export default class JSONConversationalPlugin implements ConversationalPlugin {
* Returns the type of the plugin. * Returns the type of the plugin.
*/ */
type(): PluginType { type(): PluginType {
return PluginType.Conversational; return PluginType.Conversational
} }
/** /**
* Called when the plugin is loaded. * Called when the plugin is loaded.
*/ */
onLoad() { onLoad() {
fs.mkdir("conversations") fs.mkdir('conversations')
console.debug("JSONConversationalPlugin loaded") console.debug('JSONConversationalPlugin loaded')
} }
/** /**
* Called when the plugin is unloaded. * Called when the plugin is unloaded.
*/ */
onUnload() { onUnload() {
console.debug("JSONConversationalPlugin unloaded") console.debug('JSONConversationalPlugin unloaded')
} }
/** /**
* Returns a Promise that resolves to an array of Conversation objects. * Returns a Promise that resolves to an array of Conversation objects.
*/ */
getConversations(): Promise<Conversation[]> { async getConversations(): Promise<Conversation[]> {
return this.getConversationDocs().then((conversationIds) => try {
Promise.all( const convoIds = await this.getConversationDocs()
conversationIds.map((conversationId) =>
fs const promises = convoIds.map((conversationId) => {
.readFile(`conversations/${conversationId}/${conversationId}.json`) return this.readConvo(conversationId)
.then((data) => { })
return JSON.parse(data) as Conversation; const promiseResults = await Promise.allSettled(promises)
}) const convos = promiseResults
) .map((result) => {
).then((conversations) => if (result.status === 'fulfilled') {
conversations.sort( return JSON.parse(result.value) as Conversation
(a, b) => }
new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime() })
) .filter((convo) => convo != null)
convos.sort(
(a, b) =>
new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime()
) )
); console.debug('getConversations: ', JSON.stringify(convos, null, 2))
return convos
} catch (error) {
console.error(error)
return []
}
} }
/** /**
@ -63,7 +71,7 @@ export default class JSONConversationalPlugin implements ConversationalPlugin {
`conversations/${conversation._id}/${conversation._id}.json`, `conversations/${conversation._id}/${conversation._id}.json`,
JSON.stringify(conversation) JSON.stringify(conversation)
) )
); )
} }
/** /**
@ -71,7 +79,16 @@ export default class JSONConversationalPlugin implements ConversationalPlugin {
* @param conversationId The ID of the conversation to delete. * @param conversationId The ID of the conversation to delete.
*/ */
deleteConversation(conversationId: string): Promise<void> { deleteConversation(conversationId: string): Promise<void> {
return fs.rmdir(`conversations/${conversationId}`); return fs.rmdir(`conversations/${conversationId}`)
}
/**
* A promise builder for reading a conversation from a file.
* @param convoId the conversation id we are reading from.
* @returns data of the conversation
*/
private async readConvo(convoId: string): Promise<any> {
return fs.readFile(`conversations/${convoId}/${convoId}.json`)
} }
/** /**
@ -81,7 +98,7 @@ export default class JSONConversationalPlugin implements ConversationalPlugin {
*/ */
private async getConversationDocs(): Promise<string[]> { private async getConversationDocs(): Promise<string[]> {
return fs.listFiles(`conversations`).then((files: string[]) => { return fs.listFiles(`conversations`).then((files: string[]) => {
return Promise.all(files.filter((file) => file.startsWith("jan-"))); return Promise.all(files.filter((file) => file.startsWith('jan-')))
}); })
} }
} }