From 4a2f5bce8d8747d4b07e0fdca20cf0f895ed8ffb Mon Sep 17 00:00:00 2001 From: NamH Date: Sun, 14 Jan 2024 17:46:25 +0700 Subject: [PATCH] fix(Thread): #1212 thread.json not created when user change thread settings (#1570) Signed-off-by: nam --- core/src/node/api/common/builder.ts | 9 +++++++-- .../conversational-extension/src/index.ts | 18 ++++++++++++------ web/containers/Layout/TopBar/index.tsx | 15 ++++++--------- web/hooks/useCreateNewThread.ts | 11 ++++++++--- web/hooks/useGetAssistants.ts | 8 ++------ 5 files changed, 35 insertions(+), 26 deletions(-) diff --git a/core/src/node/api/common/builder.ts b/core/src/node/api/common/builder.ts index 90b05540cd..dbc52b4305 100644 --- a/core/src/node/api/common/builder.ts +++ b/core/src/node/api/common/builder.ts @@ -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 => { 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') diff --git a/extensions/conversational-extension/src/index.ts b/extensions/conversational-extension/src/index.ts index a8ffd51dcd..70eb9f8513 100644 --- a/extensions/conversational-extension/src/index.ts +++ b/extensions/conversational-extension/src/index.ts @@ -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([ diff --git a/web/containers/Layout/TopBar/index.tsx b/web/containers/Layout/TopBar/index.tsx index af310a60d3..005348ba7c 100644 --- a/web/containers/Layout/TopBar/index.tsx +++ b/web/containers/Layout/TopBar/index.tsx @@ -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]) } diff --git a/web/hooks/useCreateNewThread.ts b/web/hooks/useCreateNewThread.ts index 325e7621c9..81114333a2 100644 --- a/web/hooks/useCreateNewThread.ts +++ b/web/hooks/useCreateNewThread.ts @@ -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(ExtensionType.Conversational) ?.saveThread(thread) - } } return { diff --git a/web/hooks/useGetAssistants.ts b/web/hooks/useGetAssistants.ts index 0fa66c9c9b..bc81968357 100644 --- a/web/hooks/useGetAssistants.ts +++ b/web/hooks/useGetAssistants.ts @@ -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 }