Signed-off-by: nam <namnh0122@gmail.com>
This commit is contained in:
parent
59564b710e
commit
4a2f5bce8d
@ -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')
|
||||||
|
|||||||
@ -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([
|
||||||
|
|||||||
@ -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])
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 {
|
||||||
|
|||||||
@ -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 }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user