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: useChat add getChatLoadingId #111

Merged
merged 1 commit into from
Mar 6, 2024
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
7 changes: 7 additions & 0 deletions src/ProChat/hooks/useProChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export interface ProChatInstance
* @returns
*/
scrollToBottom?: () => void;
/**
* 获取当前 loading 生成的消息 id
* @returns 消息 id | undefined
*/
getChatLoadingId: () => string | undefined;
}

export const useProChat = () => {
Expand All @@ -51,6 +56,7 @@ export const useProChat = () => {
deleteMessage,
clearMessage,
dispatchMessage,
getChatLoadingId,
} = storeApi.getState();

const getChats = useRefFunction(() => storeApi.getState().chats);
Expand All @@ -72,6 +78,7 @@ export const useProChat = () => {
getChatMessages,
resendMessage,
sendMessage,
getChatLoadingId,
stopGenerateMessage,
deleteMessage,
clearMessage,
Expand Down
17 changes: 17 additions & 0 deletions src/ProChat/store/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,26 @@ export interface ChatAction {
*/
getMessageId: (messages: ChatMessage[], parentId: string) => Promise<string>;

/**
* 用于更新一条消息的内容(目前仅用于平滑输出时候,其余情况请直接使用 dispatchMessage )
*/
updateMessageContent: (id: string, content: string) => Promise<void>;

/**
* 创建一条平滑输出的内容
*/
createSmoothMessage: (id: string) => {
startAnimation: (speed?: number) => Promise<void>;
stopAnimation: () => void;
outputQueue: string[];
isAnimationActive: boolean;
};

/**
* 获取当前 loading 生成的消息 id
* @returns 消息 id | undefined
*/
getChatLoadingId: () => string | undefined;
}

export const chatAction: StateCreator<ChatStore, [['zustand/devtools', never]], [], ChatAction> = (
Expand Down Expand Up @@ -445,4 +457,9 @@ export const chatAction: StateCreator<ChatStore, [['zustand/devtools', never]],
if (typeof genMessageId === 'function') return genMessageId(messages, parentId);
return nanoid();
},

getChatLoadingId: () => {
const { chatLoadingId } = get();
return chatLoadingId;
},
});
Loading