From 211fad556d4b761e37899d68b6a75419d4fc3c0c Mon Sep 17 00:00:00 2001 From: arvinxx Date: Sun, 22 Oct 2023 13:47:45 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20feat:=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=9D=9E=E6=B5=81=E5=BC=8F=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ProChat/container/App.tsx | 10 +++++---- src/ProChat/container/index.tsx | 8 ++++++-- src/ProChat/demos/control.tsx | 5 ++--- src/ProChat/demos/default.tsx | 5 ++--- src/ProChat/demos/doc-mode.tsx | 5 ++--- src/ProChat/demos/helloMessage.tsx | 5 ++--- src/ProChat/demos/initialChats.tsx | 5 ++--- src/ProChat/demos/meta.tsx | 5 ++--- src/ProChat/demos/no-stream.tsx | 33 ++++++++++++++++++++++++++++++ src/ProChat/demos/request.tsx | 5 ++--- src/ProChat/index.md | 6 ++++++ src/ProChat/store/action.ts | 4 +++- src/ProChat/store/initialState.ts | 6 ++++++ 13 files changed, 74 insertions(+), 28 deletions(-) create mode 100644 src/ProChat/demos/no-stream.tsx diff --git a/src/ProChat/container/App.tsx b/src/ProChat/container/App.tsx index fa49c8c6..7bf58260 100644 --- a/src/ProChat/container/App.tsx +++ b/src/ProChat/container/App.tsx @@ -1,6 +1,6 @@ import BackBottom from '@/BackBottom'; import { createStyles } from 'antd-style'; -import { ReactNode, memo, useRef } from 'react'; +import { CSSProperties, ReactNode, memo, useRef } from 'react'; import { Flexbox } from 'react-layout-kit'; import ChatList from '../components/ChatList'; @@ -22,14 +22,16 @@ const useStyles = createStyles( interface ConversationProps { chatInput?: ReactNode; showTitle?: boolean; + style?: CSSProperties; + className?: string; } -const App = memo(({ chatInput, showTitle }) => { +const App = memo(({ chatInput, className, style, showTitle }) => { const ref = useRef(null); - const { styles } = useStyles(); + const { styles, cx } = useStyles(); const { styles: override } = useOverrideStyles(); return ( - +
diff --git a/src/ProChat/container/index.tsx b/src/ProChat/container/index.tsx index 749d1165..54ced77a 100644 --- a/src/ProChat/container/index.tsx +++ b/src/ProChat/container/index.tsx @@ -1,5 +1,5 @@ import { App as Container } from 'antd'; -import { ReactNode, memo } from 'react'; +import { CSSProperties, ReactNode, memo } from 'react'; import App from './App'; @@ -12,6 +12,8 @@ export interface ProChatProps extends ChatProps { renderInput?: ReactNode; __PRO_CHAT_STORE_DEVTOOLS__?: boolean | DevtoolsOptions; showTitle?: boolean; + style?: CSSProperties; + className?: string; } export const ProChat = memo( @@ -27,6 +29,8 @@ export const ProChat = memo( assistantMeta, showTitle, request, + style, + className, ...props }) => { return ( @@ -42,7 +46,7 @@ export const ProChat = memo( devtoolOptions={__PRO_CHAT_STORE_DEVTOOLS__} > - + { const [chats, setChats] = useState(); return ( - +
{ @@ -30,6 +29,6 @@ export default () => { return mockResponse.getResponse(); }} /> - +
); }; diff --git a/src/ProChat/demos/default.tsx b/src/ProChat/demos/default.tsx index d11ab3c9..baa2dc1a 100644 --- a/src/ProChat/demos/default.tsx +++ b/src/ProChat/demos/default.tsx @@ -4,14 +4,13 @@ import { ProChat } from '@ant-design/pro-chat'; import { useTheme } from 'antd-style'; -import { Flexbox } from 'react-layout-kit'; import { example } from '../mocks/basic'; export default () => { const theme = useTheme(); return ( - +
- +
); }; diff --git a/src/ProChat/demos/doc-mode.tsx b/src/ProChat/demos/doc-mode.tsx index b35fdca1..4f0a5212 100644 --- a/src/ProChat/demos/doc-mode.tsx +++ b/src/ProChat/demos/doc-mode.tsx @@ -3,7 +3,6 @@ */ import { ProChat } from '@ant-design/pro-chat'; import { useTheme } from 'antd-style'; -import { Flexbox } from 'react-layout-kit'; import { example } from '../mocks/fullFeature'; import { MockResponse } from '../mocks/streamResponse'; @@ -11,7 +10,7 @@ import { MockResponse } from '../mocks/streamResponse'; export default () => { const theme = useTheme(); return ( - +
{ @@ -24,6 +23,6 @@ export default () => { chats={example.chats} config={example.config} /> - +
); }; diff --git a/src/ProChat/demos/helloMessage.tsx b/src/ProChat/demos/helloMessage.tsx index d59d6380..cfadaad3 100644 --- a/src/ProChat/demos/helloMessage.tsx +++ b/src/ProChat/demos/helloMessage.tsx @@ -4,17 +4,16 @@ import { ProChat } from '@ant-design/pro-chat'; import { useTheme } from 'antd-style'; -import { Flexbox } from 'react-layout-kit'; export default () => { const theme = useTheme(); return ( - +
- +
); }; diff --git a/src/ProChat/demos/initialChats.tsx b/src/ProChat/demos/initialChats.tsx index 13474ff8..ca654eff 100644 --- a/src/ProChat/demos/initialChats.tsx +++ b/src/ProChat/demos/initialChats.tsx @@ -4,14 +4,13 @@ import { ProChat } from '@ant-design/pro-chat'; import { useTheme } from 'antd-style'; -import { Flexbox } from 'react-layout-kit'; import { example } from '../mocks/basic'; export default () => { const theme = useTheme(); return ( - +
- +
); }; diff --git a/src/ProChat/demos/meta.tsx b/src/ProChat/demos/meta.tsx index 0cd5b998..2f55d838 100644 --- a/src/ProChat/demos/meta.tsx +++ b/src/ProChat/demos/meta.tsx @@ -1,19 +1,18 @@ import { chats } from '@/ProChat/mocks/threebody'; import { ProChat } from '@ant-design/pro-chat'; import { useTheme } from 'antd-style'; -import { Flexbox } from 'react-layout-kit'; export default () => { const theme = useTheme(); return ( - +
- +
); }; diff --git a/src/ProChat/demos/no-stream.tsx b/src/ProChat/demos/no-stream.tsx new file mode 100644 index 00000000..88d8e721 --- /dev/null +++ b/src/ProChat/demos/no-stream.tsx @@ -0,0 +1,33 @@ +/** + * iframe: 500 + * title: 非流式的请求 + * description: 消息将在等待 5s 后返回 + */ +import { ProChat } from '@ant-design/pro-chat'; +import { useTheme } from 'antd-style'; + +const delay = (text: string) => + new Promise((resolve) => { + setTimeout(() => { + resolve(text); + }, 5000); + }); + +export default () => { + const theme = useTheme(); + + return ( +
+ { + const text = await delay( + `这是一条模拟非流式输出的消息的消息。本次会话传入了${messages.length}条消息`, + ); + + return new Response(text); + }} + style={{ height: '100vh' }} + /> +
+ ); +}; diff --git a/src/ProChat/demos/request.tsx b/src/ProChat/demos/request.tsx index 9e8b6e1a..61baf496 100644 --- a/src/ProChat/demos/request.tsx +++ b/src/ProChat/demos/request.tsx @@ -3,7 +3,6 @@ */ import { ProChat } from '@ant-design/pro-chat'; import { useTheme } from 'antd-style'; -import { Flexbox } from 'react-layout-kit'; import { MockResponse } from '../mocks/streamResponse'; @@ -11,7 +10,7 @@ export default () => { const theme = useTheme(); return ( - +
{ const mockedData: string = `这是一段模拟的流式字符串数据。本次会话传入了${messages.length}条消息`; @@ -21,6 +20,6 @@ export default () => { return mockResponse.getResponse(); }} /> - +
); }; diff --git a/src/ProChat/index.md b/src/ProChat/index.md index 496d3ae6..04add173 100644 --- a/src/ProChat/index.md +++ b/src/ProChat/index.md @@ -21,6 +21,12 @@ interface Params {} +### 非流式模式 + +非流式模式下,会话消息会一次性全部返回,适用于消息量较少的场景。如果消息量较大,可能用户体验较差。 + + + ## 受控模式 使用 `chats` 和 `onChatsChange` 实现 chats 会话消息的受控 diff --git a/src/ProChat/store/action.ts b/src/ProChat/store/action.ts index 762755a9..dbba42aa 100644 --- a/src/ProChat/store/action.ts +++ b/src/ProChat/store/action.ts @@ -96,11 +96,13 @@ export const chatAction: StateCreator { - const { chats } = get(); + const { chats, onChatsChange } = get(); const nextChats = messagesReducer(chats, payload); set({ chats: nextChats }, false, t('dispatchMessage')); + + onChatsChange?.(nextChats); }, generateMessage: async (messages, assistantId) => { const { dispatchMessage, toggleChatLoading, config, defaultModelFetcher } = get(); diff --git a/src/ProChat/store/initialState.ts b/src/ProChat/store/initialState.ts index 785c0053..5cce7f20 100644 --- a/src/ProChat/store/initialState.ts +++ b/src/ProChat/store/initialState.ts @@ -23,6 +23,11 @@ export interface ChatPropsState { */ helloMessage?: string; request?: string | ChatRequest; + // /** + // * 控制是否流式输出 + // * @default true + // */ + // stream: boolean; } export interface ChatState extends ChatPropsState { @@ -54,4 +59,5 @@ export const initialState: ChatState = { avatar: DEFAULT_AVATAR, }, config: initialModelConfig, + // stream: true, };