-
-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support pending/loading message while waiting for response (#821)
* support pending message draft * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * styling + pending message for /fix * change default pending message * remove persona groups * inline styling * single timestamp * use message id as component key Co-authored-by: david qiu <[email protected]> * fix conditional useEffect * prefer MUI Typography in PendingMessageElement to match font size * merge 2 outer div elements into 1 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: david qiu <[email protected]>
- Loading branch information
1 parent
ff022fe
commit 02d1966
Showing
9 changed files
with
262 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
packages/jupyter-ai/src/components/pending-messages.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
|
||
import { Box, Typography } from '@mui/material'; | ||
import { AiService } from '../handler'; | ||
import { ChatMessageHeader } from './chat-messages'; | ||
|
||
type PendingMessagesProps = { | ||
messages: AiService.PendingMessage[]; | ||
}; | ||
|
||
type PendingMessageElementProps = { | ||
text: string; | ||
ellipsis: boolean; | ||
}; | ||
|
||
function PendingMessageElement(props: PendingMessageElementProps): JSX.Element { | ||
const [dots, setDots] = useState(''); | ||
|
||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
setDots(dots => (dots.length < 3 ? dots + '.' : '')); | ||
}, 500); | ||
|
||
return () => clearInterval(interval); | ||
}, []); | ||
|
||
let text = props.text; | ||
if (props.ellipsis) { | ||
text = props.text + dots; | ||
} | ||
|
||
return ( | ||
<Box> | ||
{text.split('\n').map((line, index) => ( | ||
<Typography key={index} sx={{ lineHeight: 0.6 }}> | ||
{line} | ||
</Typography> | ||
))} | ||
</Box> | ||
); | ||
} | ||
|
||
export function PendingMessages( | ||
props: PendingMessagesProps | ||
): JSX.Element | null { | ||
const [timestamp, setTimestamp] = useState<string>(''); | ||
const [agentMessage, setAgentMessage] = | ||
useState<AiService.AgentChatMessage | null>(null); | ||
|
||
useEffect(() => { | ||
if (props.messages.length === 0) { | ||
setAgentMessage(null); | ||
setTimestamp(''); | ||
return; | ||
} | ||
const lastMessage = props.messages[props.messages.length - 1]; | ||
setAgentMessage({ | ||
type: 'agent', | ||
id: lastMessage.id, | ||
time: lastMessage.time, | ||
body: '', | ||
reply_to: '', | ||
persona: lastMessage.persona | ||
}); | ||
|
||
// timestamp format copied from ChatMessage | ||
const newTimestamp = new Date(lastMessage.time * 1000).toLocaleTimeString( | ||
[], | ||
{ | ||
hour: 'numeric', | ||
minute: '2-digit' | ||
} | ||
); | ||
setTimestamp(newTimestamp); | ||
}, [props.messages]); | ||
|
||
if (!agentMessage) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Box | ||
sx={{ | ||
padding: 4, | ||
borderTop: '1px solid var(--jp-border-color2)' | ||
}} | ||
> | ||
<ChatMessageHeader | ||
message={agentMessage} | ||
timestamp={timestamp} | ||
sx={{ | ||
marginBottom: 4 | ||
}} | ||
/> | ||
<Box | ||
sx={{ | ||
marginBottom: 1, | ||
paddingRight: 0, | ||
color: 'var(--jp-ui-font-color2)', | ||
'& > :not(:last-child)': { | ||
marginBottom: '2em' | ||
} | ||
}} | ||
> | ||
{props.messages.map(message => ( | ||
<PendingMessageElement | ||
key={message.id} | ||
text={message.body} | ||
ellipsis={message.ellipsis} | ||
/> | ||
))} | ||
</Box> | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters