Skip to content

Commit

Permalink
feat: After the voice in the new conversation window is played, jump …
Browse files Browse the repository at this point in the history
…to the tab of the conversation #1877 (#2440)

### What problem does this PR solve?

feat: After the voice in the new conversation window is played, jump to
the tab of the conversation #1877

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
  • Loading branch information
cike8899 authored Sep 14, 2024
1 parent b94c15e commit 1621313
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 31 deletions.
8 changes: 6 additions & 2 deletions web/src/components/message-item/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { useDeleteMessage, useFeedback } from '@/hooks/chat-hooks';
import { useSetModalState } from '@/hooks/common-hooks';
import { IRemoveMessageById, useSpeechWithSse } from '@/hooks/logic-hooks';
import { IFeedbackRequestBody } from '@/interfaces/request/chat';
import { ConversationContext } from '@/pages/chat/context';
import { getMessagePureId } from '@/utils/chat';
import { hexStringToUint8Array } from '@/utils/common-util';
import { SpeechPlayer } from 'openai-speech-stream-player';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useCallback, useContext, useEffect, useRef, useState } from 'react';

export const useSendFeedback = (messageId: string) => {
const { visible, hideModal, showModal } = useSetModalState();
Expand Down Expand Up @@ -58,21 +59,24 @@ export const useSpeech = (content: string, audioBinary?: string) => {
const { read } = useSpeechWithSse();
const player = useRef<SpeechPlayer>();
const [isPlaying, setIsPlaying] = useState<boolean>(false);
const callback = useContext(ConversationContext);

const initialize = useCallback(async () => {
player.current = new SpeechPlayer({
audio: ref.current!,
onPlaying: () => {
setIsPlaying(true);
callback?.(true);
},
onPause: () => {
setIsPlaying(false);
callback?.(false);
},
onChunkEnd: () => {},
mimeType: 'audio/mpeg',
});
await player.current.init();
}, []);
}, [callback]);

const pause = useCallback(() => {
player.current?.pause();
Expand Down
1 change: 1 addition & 0 deletions web/src/interfaces/database/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface PromptConfig {
parameters: Parameter[];
prologue: string;
system: string;
tts?: boolean;
}

export interface Parameter {
Expand Down
60 changes: 32 additions & 28 deletions web/src/pages/chat/chat-container/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ import {
} from '@/hooks/chat-hooks';
import { useFetchUserInfo } from '@/hooks/user-setting-hooks';
import { memo } from 'react';
import { ConversationContext } from '../context';
import styles from './index.less';

const ChatContainer = () => {
const { conversationId } = useGetChatSearchParams();
const { data: conversation } = useFetchNextConversation();

const {
value,
ref,
loading,
sendLoading,
derivedMessages,
handleInputChange,
handlePressEnter,
value,
regenerateMessage,
removeMessageById,
redirectToNewConversation,
} = useSendNextMessage();

const { visible, hideModal, documentId, selectedChunk, clickDocumentButton } =
Expand All @@ -52,33 +54,35 @@ const ChatContainer = () => {
<Flex flex={1} vertical className={styles.messageContainer}>
<div>
<Spin spinning={loading}>
{derivedMessages?.map((message, i) => {
return (
<MessageItem
loading={
message.role === MessageType.Assistant &&
sendLoading &&
derivedMessages.length - 1 === i
}
key={message.id}
item={message}
nickname={userInfo.nickname}
avatar={userInfo.avatar}
reference={buildMessageItemReference(
{
message: derivedMessages,
reference: conversation.reference,
},
message,
)}
clickDocumentButton={clickDocumentButton}
index={i}
removeMessageById={removeMessageById}
regenerateMessage={regenerateMessage}
sendLoading={sendLoading}
></MessageItem>
);
})}
<ConversationContext.Provider value={redirectToNewConversation}>
{derivedMessages?.map((message, i) => {
return (
<MessageItem
loading={
message.role === MessageType.Assistant &&
sendLoading &&
derivedMessages.length - 1 === i
}
key={message.id}
item={message}
nickname={userInfo.nickname}
avatar={userInfo.avatar}
reference={buildMessageItemReference(
{
message: derivedMessages,
reference: conversation.reference,
},
message,
)}
clickDocumentButton={clickDocumentButton}
index={i}
removeMessageById={removeMessageById}
regenerateMessage={regenerateMessage}
sendLoading={sendLoading}
></MessageItem>
);
})}
</ConversationContext.Provider>
</Spin>
</div>
<div ref={ref} />
Expand Down
5 changes: 5 additions & 0 deletions web/src/pages/chat/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createContext } from 'react';

export const ConversationContext = createContext<
null | ((isPlaying: boolean) => void)
>(null);
19 changes: 18 additions & 1 deletion web/src/pages/chat/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { useSearchParams } from 'umi';
Expand Down Expand Up @@ -340,6 +341,17 @@ export const useSendNextMessage = () => {
removeMessageById,
removeMessagesAfterCurrentMessage,
} = useSelectNextMessages();
const { data: dialog } = useFetchNextDialog();
const currentConversationIdRef = useRef<string>('');

const redirectToNewConversation = useCallback(
(isPlaying: boolean) => {
if (!conversationId && dialog?.prompt_config?.tts && !isPlaying) {
handleClickConversation(currentConversationIdRef.current);
}
},
[dialog, handleClickConversation, conversationId],
);

const sendMessage = useCallback(
async ({
Expand All @@ -365,14 +377,17 @@ export const useSendNextMessage = () => {
if (currentConversationId) {
console.info('111');
// new conversation
handleClickConversation(currentConversationId);
if (!dialog?.prompt_config?.tts) {
handleClickConversation(currentConversationId);
}
} else {
console.info('222');
// fetchConversation(conversationId);
}
}
},
[
dialog,
derivedMessages,
conversationId,
handleClickConversation,
Expand All @@ -390,6 +405,7 @@ export const useSendNextMessage = () => {
const data = await setConversation(message.content);
if (data.retcode === 0) {
const id = data.data.id;
currentConversationIdRef.current = id;
sendMessage({
message,
currentConversationId: id,
Expand Down Expand Up @@ -463,6 +479,7 @@ export const useSendNextMessage = () => {
ref,
derivedMessages,
removeMessageById,
redirectToNewConversation,
};
};

Expand Down

0 comments on commit 1621313

Please sign in to comment.