fix(Thread): #1212 thread.json not created when user change thread settings (#1570)

Signed-off-by: nam <namnh0122@gmail.com>
This commit is contained in:
NamH 2024-01-14 17:46:25 +07:00 committed by GitHub
parent 59564b710e
commit 4a2f5bce8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 26 deletions

View File

@ -93,16 +93,21 @@ export const deleteBuilder = async (configuration: RouteConfiguration, id: strin
}
}
export const getMessages = async (threadId: string) => {
export const getMessages = async (threadId: string): Promise<ThreadMessage[]> => {
const threadDirPath = join(path, 'threads', threadId)
const messageFile = 'messages.jsonl'
try {
const files: string[] = fs.readdirSync(threadDirPath)
if (!files.includes(messageFile)) {
throw Error(`${threadDirPath} not contains message file`)
console.error(`${threadDirPath} not contains message file`)
return []
}
const messageFilePath = join(threadDirPath, messageFile)
if (!fs.existsSync(messageFilePath)) {
console.debug('message file not found')
return []
}
const lines = fs
.readFileSync(messageFilePath, 'utf-8')

View File

@ -1,6 +1,11 @@
import { ExtensionType, fs, joinPath } from '@janhq/core'
import { ConversationalExtension } from '@janhq/core'
import { Thread, ThreadMessage } from '@janhq/core'
import {
ExtensionType,
fs,
joinPath,
ConversationalExtension,
Thread,
ThreadMessage,
} from '@janhq/core'
/**
* JSONConversationalExtension is a ConversationalExtension implementation that provides
@ -83,9 +88,9 @@ export default class JSONConversationalExtension
await fs.mkdirSync(threadDirPath)
}
await fs.writeFileSync(threadJsonPath, JSON.stringify(thread))
Promise.resolve()
await fs.writeFileSync(threadJsonPath, JSON.stringify(thread, null, 2))
} catch (err) {
console.error(err)
Promise.reject(err)
}
}
@ -212,7 +217,8 @@ export default class JSONConversationalExtension
if (
!files.includes(JSONConversationalExtension._threadMessagesFileName)
) {
throw Error(`${threadDirPath} not contains message file`)
console.debug(`${threadDirPath} not contains message file`)
return []
}
const messageFilePath = await joinPath([

View File

@ -55,15 +55,12 @@ const TopBar = () => {
const onCreateConversationClick = async () => {
if (assistants.length === 0) {
await getAssistants().then((res) => {
if (res) {
if (res.length === 0) {
alert('No assistant available')
return
}
requestCreateNewThread(res[0])
}
})
const res = await getAssistants()
if (res.length === 0) {
alert('No assistant available')
return
}
requestCreateNewThread(res[0])
} else {
requestCreateNewThread(assistants[0])
}

View File

@ -19,6 +19,7 @@ import {
setActiveThreadIdAtom,
threadStatesAtom,
updateThreadAtom,
updateThreadInitSuccessAtom,
} from '@/helpers/atoms/Thread.atom'
const createNewThreadAtom = atom(null, (get, set, newThread: Thread) => {
@ -41,9 +42,11 @@ const createNewThreadAtom = atom(null, (get, set, newThread: Thread) => {
export const useCreateNewThread = () => {
const threadStates = useAtomValue(threadStatesAtom)
const updateThreadFinishInit = useSetAtom(updateThreadInitSuccessAtom)
const createNewThread = useSetAtom(createNewThreadAtom)
const setActiveThreadId = useSetAtom(setActiveThreadIdAtom)
const updateThread = useSetAtom(updateThreadAtom)
const { deleteThread } = useDeleteThread()
const requestCreateNewThread = async (
@ -96,11 +99,13 @@ export const useCreateNewThread = () => {
updateThread(thread)
const threadState = threadStates[thread.id]
const isFinishInit = threadState?.isFinishInit ?? true
if (isFinishInit) {
extensionManager
if (!isFinishInit) {
updateThreadFinishInit(thread.id)
}
extensionManager
.get<ConversationalExtension>(ExtensionType.Conversational)
?.saveThread(thread)
}
}
return {

View File

@ -19,12 +19,8 @@ export default function useGetAssistants() {
useEffect(() => {
getAssistants()
.then((data) => {
setAssistants(data)
})
.catch((err) => {
console.error(err)
})
.then((data) => setAssistants(data))
.catch((err) => console.error(err))
}, [])
return { assistants }