This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMessages.tsx
159 lines (139 loc) · 4.14 KB
/
Messages.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { Message } from 'ai';
import { Typography } from 'antd';
import styled from 'styled-components';
import { useEffect, useRef } from 'react';
import { useFormContext } from 'react-hook-form';
import winkNLP from 'wink-nlp';
import model from 'wink-eng-lite-web-model';
const nlp = winkNLP(model);
const TEXT_COLOR = 'rgb(209, 213, 219)';
const ASSISTANT_BG_COLOR = 'rgb(68, 70, 84)';
const USER_BG_COLOR = 'rgb(52, 53, 65)';
const Container = styled.div`
padding-top: 64px;
padding-bottom: 110px;
flex-grow: 1;
display: flex;
flex-direction: column;
min-height: calc(100vh);
`;
const MessageDiv = styled.div<{ role: Message['role'] }>`
padding: 0.75rem;
transition: box-shadow 200ms ease-in;
display: flex;
align-items: center;
background-color: #2e3a46;
&:hover {
box-shadow: 0 4px 6px 0 rgba(0, 0, 0, 0.1);
}
.ant-typography-copy {
color: rgb(172, 172, 190) !important;
}
${(props) =>
props.role === 'assistant' &&
`
background-color: ${ASSISTANT_BG_COLOR};
`}
${(props) =>
props.role === 'user' &&
`
background-color: ${USER_BG_COLOR};
`}
`;
let speechSynthesis: SpeechSynthesis;
if (typeof window !== 'undefined') {
speechSynthesis = window.speechSynthesis;
}
const speak = (text: string) => {
const utterance = new SpeechSynthesisUtterance(text);
speechSynthesis.speak(utterance);
};
interface MessagesProps {
readonly messages: Message[];
readonly isLoading: boolean;
}
export default function Messages({ messages, isLoading }: MessagesProps) {
const { watch } = useFormContext();
const isWebSpeechEnabled = watch('isWebSpeechEnabled');
const isSpeechStopped = watch('isSpeechStopped');
const messagesEndRef = useRef<HTMLDivElement | null>(null);
const sentences = useRef<string[]>([]);
const speechIndex = useRef<number>(0);
// Scroll to the most recent message whenever a new message is added
useEffect(() => {
if (messagesEndRef.current) {
messagesEndRef.current.scrollIntoView({ behavior: 'smooth' });
}
}, [messages]);
// Web Speech API Hooks
useEffect(() => {
if (isWebSpeechEnabled) {
sentences.current = [];
}
}, [isWebSpeechEnabled]);
useEffect(() => {
if (
isWebSpeechEnabled &&
!isLoading &&
sentences.current.length > 0 &&
speechIndex.current !== 0 &&
!isSpeechStopped
) {
speak(sentences.current[speechIndex.current]);
speechIndex.current = 0;
sentences.current = [];
}
}, [isLoading, isSpeechStopped, isWebSpeechEnabled]);
useEffect(() => {
if (
isWebSpeechEnabled &&
sentences.current.length > 1 &&
!isSpeechStopped
) {
speak(sentences.current[speechIndex.current]);
speechIndex.current++;
}
}, [isSpeechStopped, isWebSpeechEnabled, sentences.current.length]);
useEffect(() => {
if (messages.length > 0 && isLoading) {
let message = messages[messages.length - 1];
if (message.role === 'assistant') {
const doc = nlp.readDoc(message.content);
sentences.current = doc.sentences().out();
}
}
}, [isLoading, messages]);
// End Web Speech API Hooks
return (
<Container>
{messages.map((msg) => {
return (
<MessageDiv key={msg.id} role={msg?.role}>
<div
style={{
width: '700px',
margin: '0 auto',
display: 'flex',
textAlign: 'left',
}}
>
<div>{msg?.role === 'assistant' ? '🤖' : '🧑💻'}</div>
<Typography.Paragraph
copyable={msg?.role === 'assistant'}
style={{
color: TEXT_COLOR,
marginLeft: '0.5rem',
marginBottom: 0,
textAlign: 'left',
}}
>
{msg.content}
</Typography.Paragraph>
</div>
</MessageDiv>
);
})}
<div ref={messagesEndRef} />
</Container>
);
}