From 1a255cbe63af8d2accf98a93d61d25c861da6268 Mon Sep 17 00:00:00 2001 From: licunxing <864255598@qq.com> Date: Mon, 25 Mar 2024 22:52:41 +0800 Subject: [PATCH] fix: first dialog not show content --- web/components/chat/chat-container.tsx | 2 +- web/components/chat/db-editor.tsx | 2 +- web/hooks/use-chat.ts | 23 +++++++++++++++++------ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/web/components/chat/chat-container.tsx b/web/components/chat/chat-container.tsx index a178299f7..a38ea6771 100644 --- a/web/components/chat/chat-container.tsx +++ b/web/components/chat/chat-container.tsx @@ -33,7 +33,7 @@ const ChatContainer = () => { const contextTemp = list[list.length - 1]?.context; if (contextTemp) { try { - const contextObj = JSON.parse(contextTemp); + const contextObj = typeof contextTemp === 'string' ? JSON.parse(contextTemp) : contextTemp; setChartsData(contextObj?.template_name === 'report' ? contextObj?.charts : undefined); } catch (e) { setChartsData(undefined); diff --git a/web/components/chat/db-editor.tsx b/web/components/chat/db-editor.tsx index 8c4994bdb..a1fdf153a 100644 --- a/web/components/chat/db-editor.tsx +++ b/web/components/chat/db-editor.tsx @@ -516,7 +516,7 @@ function DbEditor() { key: i + '', label: item?.title, children: ( -
+
{ diff --git a/web/hooks/use-chat.ts b/web/hooks/use-chat.ts index 937947ee8..c7c7f2fa5 100644 --- a/web/hooks/use-chat.ts +++ b/web/hooks/use-chat.ts @@ -1,7 +1,8 @@ import { EventStreamContentType, fetchEventSource } from '@microsoft/fetch-event-source'; import { message } from 'antd'; -import { useCallback, useEffect, useMemo } from 'react'; +import { useCallback, useContext, useEffect, useMemo } from 'react'; import i18n from '@/app/i18n'; +import { ChatContext } from '@/app/chat-context'; type Props = { queryAgentURL?: string; @@ -19,6 +20,7 @@ type ChatParams = { const useChat = ({ queryAgentURL = '/api/v1/chat/completions' }: Props) => { const ctrl = useMemo(() => new AbortController(), []); + const { scene } = useContext(ChatContext); const chat = useCallback( async ({ data, chatId, onMessage, onClose, onDone, onError }: ChatParams) => { @@ -61,16 +63,25 @@ const useChat = ({ queryAgentURL = '/api/v1/chat/completions' }: Props) => { onmessage: (event) => { let message = event.data; try { - message = JSON.parse(message).vis; + if (scene === 'chat_agent') { + message = JSON.parse(message).vis; + } else { + message = JSON.parse(message); + } } catch (e) { message.replaceAll('\\n', '\n'); } - if (message === '[DONE]') { - onDone?.(); - } else if (message?.startsWith('[ERROR]')) { - onError?.(message?.replace('[ERROR]', '')); + if (typeof message === 'string') { + if (message === '[DONE]') { + onDone?.(); + } else if (message?.startsWith('[ERROR]')) { + onError?.(message?.replace('[ERROR]', '')); + } else { + onMessage?.(message); + } } else { onMessage?.(message); + onDone?.(); } }, });