Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support onResetMessage funtion #2

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ProChat/container/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const ProChat = memo<ProChatProps>(
assistantMeta,
showTitle,
request,
onResetMessage,
style,
className,
...props
Expand All @@ -42,6 +43,7 @@ export const ProChat = memo<ProChatProps>(
userMeta={userMeta}
assistantMeta={assistantMeta}
request={request}
onResetMessage={onResetMessage}
{...props}
devtoolOptions={__PRO_CHAT_STORE_DEVTOOLS__}
>
Expand Down
5 changes: 4 additions & 1 deletion src/ProChat/demos/meta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ export default () => {
<div style={{ background: theme.colorBgLayout }}>
<ProChat
showTitle
userMeta={{ avatar: '🧔', title: '罗辑' }}
userMeta={{
avatar: 'https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg',
title: 'Ant Design',
}}
assistantMeta={{ avatar: '🛸', title: '三体世界', backgroundColor: 'blue' }}
initialChats={chats.chats}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/ProChat/mocks/threebody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const chats = {
updateAt: 1697862243540,
},
Sb5pAzLL: {
content: '闭嘴',
content: '保持静默,不要回答,不要回答。',
createAt: 1697862247302,
id: 'Sb5pAzLL',
parentId: 'ZGxiX2p4',
Expand Down
7 changes: 5 additions & 2 deletions src/ProChat/store/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ export const chatAction: StateCreator<ChatStore, [['zustand/devtools', never]],
set,
get,
) => ({
clearMessage: () => {
const { dispatchMessage } = get();
clearMessage: async () => {
const { dispatchMessage, onResetMessage } = get();

// 重置消息,清空聊天记录,等待 onResetMessage 完成后再清空
if (onResetMessage) await onResetMessage();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该在 dispatch 后触发?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要是这个失败了,就不要触发后面的了,不然和服务器上的数据不同步了

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那 这个方法应该不叫 onResetMessage 吧? 叫beforeResetMessage 更合适?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on 更像是触发完成以后的回调?


dispatchMessage({ type: 'resetMessages' });

Expand Down
6 changes: 6 additions & 0 deletions src/ProChat/store/initialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export interface ChatPropsState {
*/
helloMessage?: string;
request?: string | ChatRequest;

/**
* 重置消息
* @returns
*/
onResetMessage?: () => Promise<void>;
// /**
// * 控制是否流式输出
// * @default true
Expand Down
6 changes: 3 additions & 3 deletions src/ProChat/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ export interface ModelConfig {
/**
* 角色所使用的语言模型
*/
model: string;
model?: string;
/**
* 语言模型参数
*/
params: ModelParams;
params?: ModelParams;
/**
* 系统角色
*/
systemRole: string;
systemRole?: string;
}
7 changes: 6 additions & 1 deletion src/components/Avatar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const Avatar = memo<AvatarProps>(
const isImage = Boolean(
avatar && ['/', 'http', 'data:'].some((index) => avatar.startsWith(index)),
);
const isBase64 = Boolean(avatar?.startsWith('data'));
const emoji = useMemo(() => avatar && !isImage && getEmoji(avatar), [avatar]);

const { styles, cx } = useStyles({ background, isEmoji: Boolean(emoji), size });
Expand All @@ -60,7 +61,11 @@ const Avatar = memo<AvatarProps>(
};

return isImage ? (
<AntAvatar src={avatar} {...avatarProps} {...props} />
<AntAvatar
src={isBase64 ? avatar : <img src={avatar} alt="avatar" />}
{...avatarProps}
{...props}
/>
) : (
<AntAvatar {...avatarProps} {...props}>
{emoji ? <Emoji emoji={emoji} size={size * 0.8} /> : text?.toUpperCase().slice(0, 2)}
Expand Down