diff --git a/webapp/packages/chat-sdk/src/Chat/MessageContainer/index.tsx b/webapp/packages/chat-sdk/src/Chat/MessageContainer/index.tsx index 8e4084522..068beb3c4 100644 --- a/webapp/packages/chat-sdk/src/Chat/MessageContainer/index.tsx +++ b/webapp/packages/chat-sdk/src/Chat/MessageContainer/index.tsx @@ -19,6 +19,7 @@ type Props = { isDeveloper?: boolean; integrateSystem?: string; isSimpleMode?: boolean; + isDebugMode?: boolean; onMsgDataLoaded: ( data: MsgDataType, questionId: string | number, @@ -39,6 +40,7 @@ const MessageContainer: React.FC = ({ isDeveloper, integrateSystem, isSimpleMode, + isDebugMode, onMsgDataLoaded, onSendMsg, }) => { @@ -95,6 +97,7 @@ const MessageContainer: React.FC = ({ {identityMsg && } = ( const [showCaseVisible, setShowCaseVisible] = useState(false); const [isSimpleMode, setIsSimpleMode] = useState(false); + const [isDebugMode, setIsDebugMode] = useState(true); const conversationRef = useRef(); const chatFooterRef = useRef(); @@ -87,10 +89,28 @@ const Chat: ForwardRefRenderFunction = ( } }; + const updateAgentConfigMode = (agent: AgentType) => { + const agentConfig = jsonParse(agent?.agentConfig, {}); + const { simpleMode, debugMode } = agentConfig; + if (isBoolean(simpleMode)) { + setIsSimpleMode(simpleMode); + } else { + setIsSimpleMode(false); + } + if (isBoolean(debugMode)) { + setIsDebugMode(debugMode); + } else { + setIsDebugMode(true); + } + }; + const updateCurrentAgent = (agent?: AgentType) => { setCurrentAgent(agent); onCurrentAgentChange?.(agent); localStorage.setItem('AGENT_ID', `${agent?.id}`); + if (agent) { + updateAgentConfigMode(agent); + } if (!isCopilot) { window.history.replaceState({}, '', `${window.location.pathname}?agentId=${agent?.id}`); } @@ -398,6 +418,7 @@ const Chat: ForwardRefRenderFunction = ( void; onUpdateMessageScroll?: () => void; onSendMsg?: (msg: string) => void; @@ -58,6 +59,7 @@ const ChatItem: React.FC = ({ executeItemNode, renderCustomExecuteNode, isSimpleMode, + isDebugMode, onMsgDataLoaded, onUpdateMessageScroll, onSendMsg, @@ -341,7 +343,7 @@ const ChatItem: React.FC = ({ /> {executeMode && ( <> - {!isMobile && parseInfo?.sqlInfo && isDeveloper && !isSimpleMode && ( + {!isMobile && parseInfo?.sqlInfo && isDeveloper && isDebugMode && !isSimpleMode && ( = ({}) => { +const ChatDemo: React.FC = () => { return (
diff --git a/webapp/packages/chat-sdk/src/utils/utils.ts b/webapp/packages/chat-sdk/src/utils/utils.ts index 3b71b0b5e..8e51b48f3 100644 --- a/webapp/packages/chat-sdk/src/utils/utils.ts +++ b/webapp/packages/chat-sdk/src/utils/utils.ts @@ -1,5 +1,6 @@ import moment, { Moment } from 'moment'; import { NumericUnit } from '../common/constants'; +import { isString } from 'lodash'; export function formatByDecimalPlaces(value: number | string, decimalPlaces: number) { if (isNaN(+value) || decimalPlaces < 0 || decimalPlaces > 100) { @@ -255,3 +256,18 @@ export const getTextWidth = ( const metrics = context.measureText(text); return Math.ceil(metrics.width); }; + +export function jsonParse(config: any, defaultReturn?: any) { + if (!isString(config)) { + return config; + } + if (!config) { + return defaultReturn; + } + try { + return JSON.parse(config); + } catch (error) { + console.log(error); + return defaultReturn; + } +} diff --git a/webapp/packages/supersonic-fe/src/pages/Agent/AgentForm.tsx b/webapp/packages/supersonic-fe/src/pages/Agent/AgentForm.tsx index efd9c4fd1..65254454e 100644 --- a/webapp/packages/supersonic-fe/src/pages/Agent/AgentForm.tsx +++ b/webapp/packages/supersonic-fe/src/pages/Agent/AgentForm.tsx @@ -9,16 +9,17 @@ import { InputNumber, Select, Row, - Col, + message, Space, } from 'antd'; import { AgentType } from './type'; import { useEffect, useState } from 'react'; import styles from './style.less'; import { DeleteOutlined, PlusOutlined } from '@ant-design/icons'; -import { uuid, jsonParse } from '@/utils/utils'; +import { uuid, jsonParse, encryptPassword, decryptPassword } from '@/utils/utils'; import ToolsSection from './ToolsSection'; import globalStyles from '@/global.less'; +import { testLLMConn } from './service'; const FormItem = Form.Item; const { TextArea } = Input; @@ -38,11 +39,13 @@ const AgentForm: React.FC = ({ editAgent, onSaveAgent, onCreateToolBtnCli const [saveLoading, setSaveLoading] = useState(false); const [examples, setExamples] = useState<{ id: string; question?: string }[]>([]); const [activeKey, setActiveKey] = useState('basic'); + const [llmTestLoading, setLlmTestLoading] = useState(false); const [formData, setFormData] = useState({ enableSearch: true, llmConfig: { timeOut: 60, provider: 'OPEN_AI', + temperature: 0, }, agentConfig: { ...defaultAgentConfig, @@ -86,13 +89,28 @@ const AgentForm: React.FC = ({ editAgent, onSaveAgent, onCreateToolBtnCli id: editAgent?.id, ...(editAgent || {}), ...values, - agentConfig: JSON.stringify({ ...config, ...values.agentConfig }) as any, + agentConfig: JSON.stringify({ + ...config, + ...values.agentConfig, + debugMode: values.agentConfig?.simpleMode === true ? false : values.agentConfig?.debugMode, + }) as any, examples: examples.map((example) => example.question), enableSearch: values.enableSearch ? 1 : 0, }); setSaveLoading(false); }; + const testLLMConnect = async (params: any) => { + setLlmTestLoading(true); + const { code, msg, data } = await testLLMConn(params); + setLlmTestLoading(false); + if (code === 200 && data) { + message.success('连接成功'); + } else { + message.error(msg); + } + }; + const formTabList = [ { label: '基本信息', @@ -148,6 +166,7 @@ const AgentForm: React.FC = ({ editAgent, onSaveAgent, onCreateToolBtnCli @@ -273,14 +299,23 @@ const AgentForm: React.FC = ({ editAgent, onSaveAgent, onCreateToolBtnCli )} + {activeKey === 'llmConfig' && ( + + )} } defaultActiveKey="basic" diff --git a/webapp/packages/supersonic-fe/src/pages/Agent/service.ts b/webapp/packages/supersonic-fe/src/pages/Agent/service.ts index 1636d79dc..38ac4d2c3 100644 --- a/webapp/packages/supersonic-fe/src/pages/Agent/service.ts +++ b/webapp/packages/supersonic-fe/src/pages/Agent/service.ts @@ -34,3 +34,10 @@ export function getMetricList(modelId: number) { }, }); } + +export function testLLMConn(data: any) { + return request>('/api/chat/agent/testLLMConn', { + method: 'POST', + data, + }); +} diff --git a/webapp/packages/supersonic-fe/src/utils/utils.ts b/webapp/packages/supersonic-fe/src/utils/utils.ts index 1f48b8fd0..a36096371 100644 --- a/webapp/packages/supersonic-fe/src/utils/utils.ts +++ b/webapp/packages/supersonic-fe/src/utils/utils.ts @@ -472,14 +472,29 @@ export const objToArray = (_obj: ObjToArrayParams, keyType: string = 'string') = }); }; -export function encryptPassword(password: string, username: string) { +const encryptKey = CryptoJS.enc.Hex.parse( + '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', +); + +export const encryptPassword = (password: string, key?: string) => { if (!password) { return password; } - // TODO This key should be stored in a secure place - const key = CryptoJS.enc.Utf8.parse('supersonic@2024'); const srcs = CryptoJS.enc.Utf8.parse(password); - const encrypted = CryptoJS.AES.encrypt(srcs, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7}); + const encrypted = CryptoJS.AES.encrypt(srcs, key || encryptKey, { + mode: CryptoJS.mode.ECB, + padding: CryptoJS.pad.Pkcs7, + }); return encrypted.toString(); - }; + +export function decryptPassword(encryptPassword: string) { + if (!encryptPassword) { + return encryptPassword; + } + const decrypt = CryptoJS.AES.decrypt(encryptPassword, encryptKey, { + mode: CryptoJS.mode.ECB, + padding: CryptoJS.pad.Pkcs7, + }); + return CryptoJS.enc.Utf8.stringify(decrypt).toString(); +}