Browse Source

fix: 修改AI hook传参

liaojiaxing 1 month ago
parent
commit
12d14d67a6

+ 2 - 1
apps/designer/.umirc.ts

@@ -30,7 +30,8 @@ export default defineConfig({
   },
   proxy: {
     '/api': {
-      'target': 'http://ab.dev.jbpm.shalu.com/',
+      // 'target': 'http://ab.dev.jbpm.shalu.com/',
+      'target': 'https://edesign.shalu.com/',
       'changeOrigin': true,
       'pathRewrite': { '^/api' : '' },
     },

+ 15 - 4
apps/designer/src/hooks/useChat.ts

@@ -120,6 +120,7 @@ export function useChat({ app_name, onStart, onSuccess, onUpdate, onError }: Cha
    * @returns 
    */
   const changeConversation = async (key: string) => {
+    cancel();
     setActiveConversation(key);
     if (key === "1") {
       setMessages([]);
@@ -157,22 +158,27 @@ export function useChat({ app_name, onStart, onSuccess, onUpdate, onError }: Cha
     }
   };
 
+  const baseUrl = process.env.NODE_ENV === "production" ? "" : "/api";
+
   /**
    * 封装智能体
    */
   const [agent] = useXAgent<ResponseMessageItem>({
     request: async (message, { onError, onSuccess, onUpdate }) => {
+      const enterpriseCode = sessionStorage.getItem("enterpriseCode");
+      const token = localStorage.getItem("token_" + enterpriseCode) || '';
+
       abortController.current = new AbortController();
       const signal = abortController.current.signal;
       try {
         setLoading(true);
         const response = await fetch(
-          "https://design.shalu.com/api/ai/chat-message",
+          baseUrl + "/api/ai/chat-message",
           {
             method: "POST",
             body: JSON.stringify(message),
             headers: {
-              Authorization: localStorage.getItem("token_a") || "",
+              Authorization: token,
               "Content-Type": "application/json",
             },
             signal,
@@ -226,7 +232,11 @@ export function useChat({ app_name, onStart, onSuccess, onUpdate, onError }: Cha
    * 发起请求
    * @param chat_query 对话内容
    */
-  const onRequest = (chat_query: string) => {
+  const onRequest = (chat_query: string, callbacks?: {
+    onUpdate: (message: ResponseMessageItem) => void;
+    onSuccess: (message: ResponseMessageItem) => void;
+    onError: (error: Error) => void;
+  }) => {
     setConversationList((list) => {
       return list?.map((item) => {
         return {
@@ -243,7 +253,7 @@ export function useChat({ app_name, onStart, onSuccess, onUpdate, onError }: Cha
         conversation_id:
           activeConversation === "1" ? undefined : activeConversation,
       },
-      {
+      callbacks ?? {
         onSuccess: (data) => {
           onSuccess?.(data);
         },
@@ -282,6 +292,7 @@ export function useChat({ app_name, onStart, onSuccess, onUpdate, onError }: Cha
    * 新增会话
    */
   const addConversation = () => {
+    cancel();
     setMessages([]);
     setActiveConversation("1");
     // 还没产生对话时 直接清除当前对话

+ 70 - 46
apps/designer/src/pages/flow/components/Config/AiCreator.tsx

@@ -1,7 +1,6 @@
-import React, { useEffect, useMemo, useRef, useState } from "react";
+import React, { useRef, useState } from "react";
 import {
   CloseOutlined,
-  FieldTimeOutlined,
   SendOutlined,
   EditOutlined,
   DeleteOutlined,
@@ -72,6 +71,17 @@ export default function AICreator(props: {
       });
       handleParse();
     },
+    onError: (err) => {
+      messageApi.open({
+        key: messageKey,
+        type: 'error',
+        content: err.message || 'AI创作失败',
+        duration: 2,
+        style: {
+          marginTop: 300
+        }
+      });
+    }
   });
 
   const handleParse = () => {
@@ -135,75 +145,89 @@ export default function AICreator(props: {
 
   const [hoverId, setHoverId] = useState<string | null>(null);
 
-    // 获取items
-    const getItems = (list: ChatHistoryItem[]) => {
-      return (list || []).map((item) => {
-        return {
-          key: item.id,
-          label: (
-            <div className="w-180px relative">
-              <div className="w-full flex">
-                <span
-                  className="truncate"
-                  style={{
-                    overflow: "hidden",
-                    whiteSpace: "nowrap",
-                    textOverflow: "ellipsis",
+  // 获取items
+  const getItems = (list: ChatHistoryItem[]) => {
+    return (list || []).map((item) => {
+      return {
+        key: item.id,
+        label: (
+          <div className="w-180px relative">
+            <div className="w-full flex">
+              <span
+                className="truncate"
+                style={{
+                  overflow: "hidden",
+                  whiteSpace: "nowrap",
+                  textOverflow: "ellipsis",
+                }}
+              >
+                <Tooltip title={item.title}>{item.title}</Tooltip>
+              </span>
+            </div>
+            {/* TODO: 添加编辑删除按钮 */}
+            {hoverId === item.id && (
+              <div className="h-full w-50px text-right absolute right-0 top-0 bg-#fff">
+                <EditOutlined
+                  onClick={(e) => {
+                    e.stopPropagation();
                   }}
-                >
-                  <Tooltip title={item.title}>{item.title}</Tooltip>
-                </span>
+                />
+                <DeleteOutlined />
               </div>
-              {/* TODO: 添加编辑删除按钮 */}
-              {hoverId === item.id && (
-                <div className="h-full w-50px text-right absolute right-0 top-0 bg-#fff">
-                  <EditOutlined
-                    onClick={(e) => {
-                      e.stopPropagation();
-                    }}
-                  />
-                  <DeleteOutlined />
-                </div>
-              )}
-            </div>
-          ),
-          onClick: () => {
-            
-          },
-          onMouseOver: () => {
-            
-          },
-        };
-      });
-    };
+            )}
+          </div>
+        ),
+        onClick: () => {
+          
+        },
+        onMouseOver: () => {
+          
+        },
+      };
+    });
+  };
 
   const handleList = [
     {
-      key: "1",
+      key: "style",
       label: "风格美化",
       icon: "icon-yijianmeihua",
       color: "#a171f2",
     },
     {
-      key: "2",
+      key: "grammar",
       label: "语法修复",
       icon: "icon-tubiao_yufajiucuo",
       color: "#00c4ad",
     },
     {
-      key: "3",
+      key: "translation_en",
       label: "翻译为英文",
       icon: "icon-fanyiweiyingwen",
       color: "#8c4ff0",
     },
     {
-      key: "4",
+      key: "translation_zh",
       label: "翻译为中文",
       icon: "icon-fanyiweizhongwen",
       color: "#3d72fb",
     },
   ];
 
+  const handleAiFeature = (feature: string) => {
+    // onRequest('', {
+    //   onUpdata: (data) => {
+
+    //   },
+    //   onSuccess: (data) => {
+
+    //   },
+    //   onError: (err) => {
+
+    //   }
+    // })
+  }
+
   return (
     <div className="flex-1 h-full">
       {contextHolder}
@@ -291,7 +315,7 @@ export default function AICreator(props: {
           <div
             key={item.key}
             className="flex-[40%] h-50px bg-#fff rounded-10px shadow-sm flex items-center pl-10px text-12px cursor-pointer"
-            style={{}}
+            onClick={() => handleAiFeature(item.key)}
           >
             <i
               className={`iconfont ${item.icon} text-16px`}