Skip to content

Commit

Permalink
fix(Thread): janhq#1212 thread.json not created when user change thre…
Browse files Browse the repository at this point in the history
…ad settings (janhq#1570)

Signed-off-by: nam <[email protected]>
  • Loading branch information
namchuai authored Jan 14, 2024
1 parent 59564b7 commit 4a2f5bc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 26 deletions.
9 changes: 7 additions & 2 deletions core/src/node/api/common/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
18 changes: 12 additions & 6 deletions extensions/conversational-extension/src/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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([
Expand Down
15 changes: 6 additions & 9 deletions web/containers/Layout/TopBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down
11 changes: 8 additions & 3 deletions web/hooks/useCreateNewThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
setActiveThreadIdAtom,
threadStatesAtom,
updateThreadAtom,
updateThreadInitSuccessAtom,
} from '@/helpers/atoms/Thread.atom'

const createNewThreadAtom = atom(null, (get, set, newThread: Thread) => {
Expand All @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 2 additions & 6 deletions web/hooks/useGetAssistants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 4a2f5bc

Please sign in to comment.