Skip to content

Commit

Permalink
✨ feat: useChat add getChatLoadingId (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
ONLY-yours authored Mar 6, 2024
1 parent b180281 commit d62f849
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
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;
},
});

0 comments on commit d62f849

Please sign in to comment.