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 threadDirPath = join(path, 'threads', threadId)
const messageFile = 'messages.jsonl' const messageFile = 'messages.jsonl'
try { try {
const files: string[] = fs.readdirSync(threadDirPath) const files: string[] = fs.readdirSync(threadDirPath)
if (!files.includes(messageFile)) { if (!files.includes(messageFile)) {
throw Error(`${threadDirPath} not contains message file`) console.error(`${threadDirPath} not contains message file`)
return []
} }
const messageFilePath = join(threadDirPath, messageFile) const messageFilePath = join(threadDirPath, messageFile)
if (!fs.existsSync(messageFilePath)) {
console.debug('message file not found')
return []
}
const lines = fs const lines = fs
.readFileSync(messageFilePath, 'utf-8') .readFileSync(messageFilePath, 'utf-8')

View File

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

View File

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

View File

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

View File

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