Browse Source

feat: 重新生成按钮逻辑

Carson 2 months ago
parent
commit
281f74c89c

+ 17 - 10
app/run-agent-flow/components/ASideType/Agent.tsx

@@ -52,6 +52,7 @@ const Agent = ({ node, asideInstantAtom }) => {
         // userId: '1c9dc4b-d95f-11ea-af4c-52540005ab01'
       })
       ctrlRef.current = ctrl
+      setIsSending(true)
       for await (const chunk of chunks) {
         message.content += chunk;
         setMessages((prev) => [...prev])
@@ -60,9 +61,9 @@ const Agent = ({ node, asideInstantAtom }) => {
       message.isError = true
       message.error = e.message
     } finally {
-      setMessages((prev) => [...prev])
       message.isLoading = false
       setIsSending(false)
+      setMessages((prev) => [...prev])
       return message
     }
   }
@@ -90,7 +91,6 @@ const Agent = ({ node, asideInstantAtom }) => {
     ${formContext}
     ${nodeContext}
     `
-    setIsSending(true)
     const message = await onSend({ text, ignoreQuestionMessage: true })
     dispatchCardInstantData({ content: markdownit().render(message.content) })
   }
@@ -104,7 +104,6 @@ const Agent = ({ node, asideInstantAtom }) => {
   const onCommit = async () => {
     const text = input
     setInput('')
-    setIsSending(true)
     await onSend({ text })
   }
 
@@ -127,17 +126,25 @@ const Agent = ({ node, asideInstantAtom }) => {
 
   const effectButtons = []
   if (!isSending) {
-    effectButtons.push(
-      <button key="rerun" className='btn btn-xs' onClick={() => {
-        setMessages([])
-        onFirstSend()
-      }}>重新运行</button>,
-    )
+    if (messages?.length) {
+      effectButtons.push(
+        <button key="clear" className='btn btn-xs' onClick={() => {
+          setMessages([])
+          setSessionName(uuid4())
+        }}>清屏</button>,
+      )
+    } else {
+      effectButtons.push(
+        <button key="rerun" className='btn btn-primary btn-xs' onClick={() => {
+          onFirstSend()
+        }}>重新运行</button>,
+      )
+    }
   }
 
   return (
     <div className="w-full h-full flex relative">
-      <Chater messages={messages} node={node} onAccept={onAccept}></Chater>
+      <Chater messages={messages} node={node} onAccept={onAccept} onSend={onSend}></Chater>
 
       <Sender input={input} onInput={setInput} isSending={isSending} onStop={onStop} onCommit={onCommit} effectButtons={effectButtons} />
     </div>

+ 5 - 2
app/run-agent-flow/components/ASideType/Chater.tsx

@@ -18,7 +18,7 @@ type IMessage = {
 }
 type IMessages = IMessage[]
 
-const Chater = ({ messages, node, onAccept }: { messages: IMessages }) => {
+const Chater = ({ messages, node, onAccept, onSend }: { messages: IMessages }) => {
   const reversedMessages = useMemo(() => {
     return R.reverse(messages)
   }, [messages])
@@ -68,7 +68,10 @@ const Chater = ({ messages, node, onAccept }: { messages: IMessages }) => {
                         ? (
                           <>
                             <div className="divider my-0"></div>
-                            <button className="btn btn-xs btn-neutral" onClick={() => onAccept?.(message)}>采纳</button>
+                            <div className="flex gap-1 flex-wrap">
+                              <button className="btn btn-xs btn-neutral" onClick={() => onAccept?.(message)}>采纳</button>
+                              <button className="btn btn-xs btn-neutral" onClick={() => onSend?.({ text: '重新生成' })}>重新生成</button>
+                            </div>
                           </>
                         )
                         : null

+ 27 - 22
app/run-agent-flow/components/ASideType/Form.tsx

@@ -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} />