forked from jupyterlab/jupyter-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement chat UI * improve rendering of code blocks * handle empty and loading state in chat-input * align all messages to the left * css tweaks * add build:core yarn script * handle user selections * support math rendering * keep using old OpenAIChat provider for chat and magics * delete example messages
- Loading branch information
Showing
21 changed files
with
1,506 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React, { useState, useMemo } from 'react'; | ||
|
||
import type { CodeProps } from 'react-markdown/lib/ast-to-react'; | ||
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; | ||
import { duotoneLight } from 'react-syntax-highlighter/dist/esm/styles/prism'; | ||
import { Box, Button } from '@mui/material'; | ||
|
||
type ChatCodeViewProps = CodeProps; | ||
|
||
type ChatCodeInlineProps = ChatCodeViewProps; | ||
|
||
type ChatCodeBlockProps = ChatCodeViewProps & { | ||
language?: string; | ||
}; | ||
|
||
function ChatCodeInline({ | ||
className, | ||
children, | ||
...props | ||
}: ChatCodeInlineProps) { | ||
return ( | ||
<code {...props} className={className}> | ||
{children} | ||
</code> | ||
); | ||
} | ||
|
||
enum CopyStatus { | ||
None, | ||
Copying, | ||
Copied | ||
} | ||
|
||
const COPYBTN_TEXT_BY_STATUS: Record<CopyStatus, string> = { | ||
[CopyStatus.None]: 'Copy to clipboard', | ||
[CopyStatus.Copying]: 'Copying...', | ||
[CopyStatus.Copied]: 'Copied!' | ||
}; | ||
|
||
function ChatCodeBlock({ language, children, ...props }: ChatCodeBlockProps) { | ||
const value = useMemo(() => String(children).replace(/\n$/, ''), [children]); | ||
const [copyStatus, setCopyStatus] = useState<CopyStatus>(CopyStatus.None); | ||
|
||
const copy = async () => { | ||
setCopyStatus(CopyStatus.Copying); | ||
try { | ||
await navigator.clipboard.writeText(value); | ||
} catch (e) { | ||
console.error(e); | ||
setCopyStatus(CopyStatus.None); | ||
return; | ||
} | ||
|
||
setCopyStatus(CopyStatus.Copied); | ||
setTimeout(() => setCopyStatus(CopyStatus.None), 1000); | ||
}; | ||
|
||
return ( | ||
<Box sx={{ display: 'flex', flexDirection: 'column' }}> | ||
<SyntaxHighlighter | ||
{...props} | ||
children={value} | ||
style={duotoneLight} | ||
language={language} | ||
PreTag="div" | ||
/> | ||
<Button | ||
onClick={copy} | ||
disabled={copyStatus !== CopyStatus.None} | ||
sx={{ alignSelf: 'flex-end' }} | ||
> | ||
{COPYBTN_TEXT_BY_STATUS[copyStatus]} | ||
</Button> | ||
</Box> | ||
); | ||
} | ||
|
||
export function ChatCodeView({ | ||
inline, | ||
className, | ||
...props | ||
}: ChatCodeViewProps) { | ||
const match = /language-(\w+)/.exec(className || ''); | ||
return inline ? ( | ||
<ChatCodeInline {...props} /> | ||
) : ( | ||
<ChatCodeBlock {...props} language={match ? match[1] : undefined} /> | ||
); | ||
} |
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,71 @@ | ||
import React from 'react'; | ||
|
||
import { | ||
Box, | ||
IconButton, | ||
Input, | ||
SxProps, | ||
Theme, | ||
FormGroup, | ||
FormControlLabel, | ||
Checkbox | ||
} from '@mui/material'; | ||
import SendIcon from '@mui/icons-material/Send'; | ||
|
||
type ChatInputProps = { | ||
loading: boolean; | ||
value: string; | ||
onChange: (newValue: string) => unknown; | ||
onSend: () => unknown; | ||
hasSelection: boolean; | ||
includeSelection: boolean; | ||
toggleIncludeSelection: () => unknown; | ||
replaceSelection: boolean; | ||
toggleReplaceSelection: () => unknown; | ||
sx?: SxProps<Theme>; | ||
}; | ||
|
||
export function ChatInput(props: ChatInputProps): JSX.Element { | ||
return ( | ||
<Box sx={props.sx}> | ||
<Box sx={{ display: 'flex' }}> | ||
<Input | ||
value={props.value} | ||
onChange={e => props.onChange(e.target.value)} | ||
multiline | ||
sx={{ flexGrow: 1 }} | ||
/> | ||
<IconButton | ||
size="large" | ||
color="primary" | ||
onClick={props.onSend} | ||
disabled={props.loading || !props.value.trim().length} | ||
> | ||
<SendIcon /> | ||
</IconButton> | ||
</Box> | ||
{props.hasSelection && ( | ||
<FormGroup sx={{ display: 'flex', flexDirection: 'row' }}> | ||
<FormControlLabel | ||
control={ | ||
<Checkbox | ||
checked={props.includeSelection} | ||
onChange={props.toggleIncludeSelection} | ||
/> | ||
} | ||
label="Include selection" | ||
/> | ||
<FormControlLabel | ||
control={ | ||
<Checkbox | ||
checked={props.replaceSelection} | ||
onChange={props.toggleReplaceSelection} | ||
/> | ||
} | ||
label="Replace selection" | ||
/> | ||
</FormGroup> | ||
)} | ||
</Box> | ||
); | ||
} |
Oops, something went wrong.