generated from arvinxx/npm-template
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
300 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* compact: true | ||
*/ | ||
import { ProChat, ProChatProvider, useProChat } from '@ant-design/pro-chat'; | ||
import { Button, Divider, Flex, message } from 'antd'; | ||
import { useTheme } from 'antd-style'; | ||
|
||
import { MockResponse } from '@/ProChat/mocks/streamResponse'; | ||
import { example } from '../mocks/basic'; | ||
|
||
const Chat = () => { | ||
const theme = useTheme(); | ||
return ( | ||
<div style={{ background: theme.colorBgLayout }}> | ||
<ProChat | ||
request={async (messages) => { | ||
const mockedData: string = `这是一段模拟的流式字符串数据。本次会话传入了${messages.length}条消息`; | ||
|
||
const mockResponse = new MockResponse(mockedData, 100); | ||
|
||
return mockResponse.getResponse(); | ||
}} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
const Control = () => { | ||
const proChat = useProChat(); | ||
|
||
return ( | ||
<Flex style={{ padding: 24 }} gap={8} justify={'space-between'}> | ||
<Flex gap={8}> | ||
<Button | ||
type={'primary'} | ||
onClick={() => { | ||
proChat.sendMessage('这是程序化发送的消息'); | ||
}} | ||
> | ||
发送一条消息 | ||
</Button> | ||
<Button | ||
onClick={() => { | ||
const messages = proChat.getChatMessages(); | ||
|
||
const msg = messages.at(-1); | ||
if (msg) { | ||
message.info(msg.content); | ||
} else { | ||
message.warning('会话为空'); | ||
} | ||
}} | ||
> | ||
获取最新会话消息 | ||
</Button> | ||
</Flex> | ||
|
||
<Button | ||
onClick={() => { | ||
const messages = proChat.getChatMessages(); | ||
const { id, content } = messages[0] || {}; | ||
|
||
if (!id) return; | ||
proChat.setMessageContent(id, content + '👋'); | ||
}} | ||
> | ||
修改首条消息,添加表情:👋 | ||
</Button> | ||
<Flex gap={8}> | ||
<Button | ||
danger | ||
onClick={() => { | ||
const messages = proChat.getChatMessages(); | ||
proChat.deleteMessage(messages[0].id); | ||
message.success('已删除第一条消息'); | ||
}} | ||
> | ||
删除第一条消息 | ||
</Button> | ||
<Button | ||
type={'primary'} | ||
danger | ||
onClick={() => { | ||
proChat.clearMessage(); | ||
}} | ||
> | ||
清空消息 | ||
</Button> | ||
</Flex> | ||
</Flex> | ||
); | ||
}; | ||
|
||
export default () => ( | ||
<ProChatProvider initialChats={example.chats}> | ||
<Control /> | ||
<Divider>🔼 程序化控制 | 🔽 用户控制</Divider> | ||
<Chat /> | ||
</ProChatProvider> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* compact: true | ||
*/ | ||
import { ProChat, ProChatInstance } from '@ant-design/pro-chat'; | ||
import { useTheme } from 'antd-style'; | ||
import { useRef } from 'react'; | ||
|
||
import { MockResponse } from '@/ProChat/mocks/streamResponse'; | ||
import { Button } from 'antd'; | ||
import { example } from '../mocks/basic'; | ||
|
||
export default () => { | ||
const theme = useTheme(); | ||
const proChatRef = useRef<ProChatInstance>(); | ||
|
||
return ( | ||
<div style={{ background: theme.colorBgLayout }}> | ||
<Button | ||
type={'primary'} | ||
onClick={() => { | ||
if (!proChatRef.current) return; | ||
const messages = proChatRef.current.getChatMessages(); | ||
const { id, content } = messages[0] || {}; | ||
|
||
if (!id) return; | ||
proChatRef.current.setMessageContent(id, content + '👋'); | ||
}} | ||
> | ||
修改首条消息,添加表情:👋 | ||
</Button> | ||
<ProChat | ||
initialChats={example.chats} | ||
chatRef={proChatRef} | ||
request={async (messages) => { | ||
const mockedData: string = `这是一段模拟的流式字符串数据。本次会话传入了${messages.length}条消息`; | ||
|
||
const mockResponse = new MockResponse(mockedData, 100); | ||
|
||
return mockResponse.getResponse(); | ||
}} | ||
/> | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.