Selaa lähdekoodia

feat: 新增think

liaojiaxing 1 kuukausi sitten
vanhempi
commit
2139fcabd9

+ 3 - 2
src/app.ts

@@ -1,5 +1,6 @@
+
 import '@unocss/reset/sanitize/sanitize.css';
-import { message, notification } from 'antd';
+import { message } from 'antd';
 import type { RequestConfig } from 'umi';
 
 // 与后端约定的响应数据格式
@@ -46,7 +47,7 @@ export const request: RequestConfig = {
         message.error('请求无响应,请稍后再试!');
       } else {
         // 发送请求时出了点问题
-        message.error('请求错误,请稍后再试!');
+        message.error(error || '请求错误,请稍后再试!');
       }
     },
   },

+ 7 - 2
src/components/ai/MarkdownViewer.tsx

@@ -3,7 +3,7 @@ import remarkGfm from "remark-gfm";
 import rehypeRaw from "rehype-raw";
 import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
 import { vscDarkPlus } from "react-syntax-highlighter/dist/esm/styles/prism";
-import { useState } from "react";
+import { useMemo, useState } from "react";
 import "./index.less";
 
 interface MarkdownViewerProps {
@@ -33,6 +33,11 @@ const CodeHeader: React.FC<{
 
 const MarkdownViewer: React.FC<MarkdownViewerProps> = ({ content }) => {
   const [copiedIndex, setCopiedIndex] = useState<number | null>(null);
+  const filterContent = useMemo(() => {
+    return content
+      .replaceAll("<think>", '<div class="think">')
+      .replaceAll("</think>", "</div>");
+  }, [content]);
 
   const handleCopy = (code: string, index: number) => {
     navigator.clipboard.writeText(code);
@@ -86,7 +91,7 @@ const MarkdownViewer: React.FC<MarkdownViewerProps> = ({ content }) => {
           },
         }}
       >
-        {content}
+        {filterContent}
       </ReactMarkdown>
     </div>
   );

+ 5 - 0
src/components/ai/index.less

@@ -34,4 +34,9 @@
       // background-color: #f9f9f9;
     }
   }
+
+  .think {
+    padding-left: 20px;
+    border-left: solid 4px #ddd;
+  }
 }

+ 6 - 6
src/hooks/useChat.ts

@@ -187,15 +187,15 @@ export function useChat({ app_name, onSuccess, onUpdate, onError }: ChatProps) {
               const data = JSON.parse(chunk.data);
               if (data?.event === "message") {
                 onUpdate(data);
-              }
-              if (data?.event === "message_end") {
+              } else if (data?.event === "message_end") {
                 onSuccess(data);
-              }
-              if (data?.event === "message_error") {
+              } else if (data?.event === "message_error") {
                 onError(data);
-              }
-              if (data?.event === "ping") {
+              } else if (data?.event === "ping") {
                 console.log(">>>> stream start <<<<");
+              } else {
+                console.log(">>>> stream error <<<<");
+                onError(Error(data?.message || '请求失败'));
               }
             }
           }

+ 3 - 0
src/pages/ai/Assistant.tsx

@@ -77,6 +77,9 @@ const roles: GetProp<typeof Bubble.List, "roles"> = {
   user: {
     placement: "end",
     avatar: { icon: <UserOutlined />, style: { background: "#87d068" } },
+    messageRender: (content) => {
+      return <div style={{ whiteSpace: 'pre-wrap' }}>{content}</div>
+    },
   },
 };