|
@@ -26,39 +26,44 @@ const Form = ({ node, asideInstantAtom }) => {
|
|
|
}, [messages, sessionName])
|
|
|
|
|
|
const onSend = async ({ text, ignoreQuestionMessage = false }: { text: string, ignoreQuestionMessage?: boolean }) => {
|
|
|
- // FIXME fixed assi id
|
|
|
- const assistantId = "8dcff108-64e8-11ef-826e-12e77c4cb76b"
|
|
|
- const newMessages = messages
|
|
|
+ const assistantId = R.path(['properties', 'item', 'assistant_id'], node)
|
|
|
+ const newMessages = []
|
|
|
if (!ignoreQuestionMessage) {
|
|
|
newMessages.push({ type: 'md', role: 'user', content: text })
|
|
|
}
|
|
|
const message = { type: 'md', role: 'assistant', content: '', isLoading: true }
|
|
|
newMessages.push(message)
|
|
|
- setMessages(() => [...newMessages])
|
|
|
+ setMessages((prev) => [...prev, ...newMessages])
|
|
|
// messageItem.current = message
|
|
|
- const [chunks, ctrl] = getChatResponse({
|
|
|
- text,
|
|
|
- assistantId,
|
|
|
- sessionName,
|
|
|
- // userId: session?.user?.id,
|
|
|
- // FIXME
|
|
|
- userId: '1c9dc4b-d95f-11ea-af4c-52540005ab01'
|
|
|
- })
|
|
|
- ctrlRef.current = ctrl
|
|
|
- for await (const chunk of chunks) {
|
|
|
- message.content += chunk;
|
|
|
- setMessages(() => [...newMessages])
|
|
|
+ try {
|
|
|
+ const [chunks, ctrl] = getChatResponse({
|
|
|
+ text,
|
|
|
+ assistantId,
|
|
|
+ sessionName,
|
|
|
+ userId: session?.user?.id,
|
|
|
+ // FIXME
|
|
|
+ // userId: '1c9dc4b-d95f-11ea-af4c-52540005ab01'
|
|
|
+ })
|
|
|
+ ctrlRef.current = ctrl
|
|
|
+ setIsSending(true)
|
|
|
+ for await (const chunk of chunks) {
|
|
|
+ message.content += chunk;
|
|
|
+ setMessages((prev) => [...prev])
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ message.isError = true
|
|
|
+ message.error = e.message
|
|
|
+ } finally {
|
|
|
+ message.isLoading = false
|
|
|
+ setIsSending(false)
|
|
|
+ setMessages((prev) => [...prev])
|
|
|
+ return message
|
|
|
}
|
|
|
- message.isLoading = false
|
|
|
- setMessages(() => [...newMessages])
|
|
|
- setIsSending(false)
|
|
|
- return message
|
|
|
}
|
|
|
|
|
|
const onCommit = async () => {
|
|
|
const text = input
|
|
|
setInput('')
|
|
|
- setIsSending(true)
|
|
|
await onSend({ text })
|
|
|
}
|
|
|
|
|
@@ -69,7 +74,7 @@ const Form = ({ node, asideInstantAtom }) => {
|
|
|
|
|
|
return (
|
|
|
<div className="w-full h-full flex relative">
|
|
|
- <Chater messages={messages} node={node}></Chater>
|
|
|
+ <Chater messages={messages} node={node} onSend={onSend}></Chater>
|
|
|
|
|
|
<Sender input={input} onInput={setInput} isSending={isSending} onStop={onStop} onCommit={onCommit} />
|
|
|
|