Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow html behind feature flag #565

Merged
merged 3 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backend/chainlit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
# Show the prompt playground
prompt_playground = true

# Process and display HTML in messages. This can be a security risk (see https://stackoverflow.com/questions/19603097/why-is-it-dangerous-to-render-user-generated-html-or-javascript)
unsafe_allow_html = false

# Process and display mathematical expressions. This can clash with "$" characters in messages.
latex = false

# Authorize users to upload files with messages
multi_modal = true

Expand Down Expand Up @@ -157,6 +163,8 @@ class SpeechToTextFeature:
class FeaturesSettings(DataClassJsonMixin):
prompt_playground: bool = True
multi_modal: bool = True
latex: bool = False
unsafe_allow_html: bool = False
speech_to_text: Optional[SpeechToTextFeature] = None


Expand Down
1 change: 0 additions & 1 deletion frontend/src/AppWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export default function AppWrapper() {

useEffect(() => {
if (!data) return;

setProjectSettings(data);
setAppSettings((prev) => ({
...prev,
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/components/molecules/chatProfiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export default function ChatProfiles() {
return null;
}

const allowHtml = pSettings?.features?.unsafe_allow_html;
const latex = pSettings?.features?.latex;

const popoverOpen = Boolean(anchorEl);

return (
Expand Down Expand Up @@ -167,7 +170,9 @@ export default function ChatProfiles() {
disableRestoreFocus
>
<Box p={2} maxWidth="20rem">
<Markdown>{chatProfileDescription}</Markdown>
<Markdown allowHtml={allowHtml} latex={latex}>
{chatProfileDescription}
</Markdown>
</Box>
</Popover>
<NewChatDialog
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/components/organisms/chat/Messages/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ const MessageContainer = memo(
const memoizedContext = useMemo(() => {
return {
askUser,
allowHtml: projectSettings?.features?.unsafe_allow_html,
latex: projectSettings?.features?.latex,
avatars,
defaultCollapseContent: appSettings.defaultCollapseContent,
expandAll: appSettings.expandAll,
Expand All @@ -198,6 +200,7 @@ const MessageContainer = memo(
highlightedMessage,
loading,
projectSettings?.ui?.name,
projectSettings?.features?.unsafe_allow_html,
onElementRefClick,
onError,
onFeedbackUpdated,
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/components/organisms/chat/Messages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ const Messages = ({
return !idToResume &&
!messages.length &&
projectSettings?.ui.show_readme_as_default ? (
<WelcomeScreen markdown={projectSettings?.markdown} />
<WelcomeScreen
markdown={projectSettings?.markdown}
allowHtml={projectSettings?.features?.unsafe_allow_html}
latex={projectSettings?.features?.latex}
/>
) : (
<MessageContainer
avatars={avatars}
Expand Down
59 changes: 34 additions & 25 deletions frontend/src/components/organisms/chat/Messages/welcomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,41 @@ import { Box } from '@mui/material';

import { Markdown } from '@chainlit/react-components';

const WelcomeScreen = memo(({ markdown }: { markdown?: string }) => {
if (!markdown) return <Box flexGrow={1} />;
const WelcomeScreen = memo(
({
markdown,
allowHtml
}: {
markdown?: string;
allowHtml?: boolean;
latex?: boolean;
}) => {
if (!markdown) return <Box flexGrow={1} />;

return (
<Box overflow="auto" flexGrow={1}>
<Box
id="welcome-screen"
sx={{
p: 2,
boxSizing: 'border-box',
maxWidth: '60rem',
width: '100%',
mx: 'auto',
color: 'text.primary',
lineHeight: '25px',
fontSize: '1rem',
fontFamily:
'-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji',
display: 'flex',
flexDirection: 'column'
}}
>
<Markdown>{markdown}</Markdown>
return (
<Box overflow="auto" flexGrow={1}>
<Box
id="welcome-screen"
sx={{
p: 2,
boxSizing: 'border-box',
maxWidth: '60rem',
width: '100%',
mx: 'auto',
color: 'text.primary',
lineHeight: '25px',
fontSize: '1rem',
fontFamily:
'-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji',
display: 'flex',
flexDirection: 'column'
}}
>
<Markdown allowHtml={allowHtml}>{markdown}</Markdown>
</Box>
</Box>
</Box>
);
});
);
}
);

export default WelcomeScreen;
6 changes: 5 additions & 1 deletion frontend/src/pages/Readme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export default function Readme() {
return (
<Page>
<Box my={2} display="flex" flexGrow={1}>
<WelcomeScreen markdown={projectSettings?.markdown} />
<WelcomeScreen
markdown={projectSettings?.markdown}
allowHtml={projectSettings?.features?.unsafe_allow_html}
latex={projectSettings?.features?.latex}
/>
</Box>
</Page>
);
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/state/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface IProjectSettings {
};
features: {
multi_modal?: boolean;
unsafe_allow_html?: boolean;
latex?: boolean;
speech_to_text?: {
enabled?: boolean;
language?: string;
Expand Down
8 changes: 1 addition & 7 deletions libs/react-client/src/types/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IFileElement, IMessageElement } from './element';
import { IFileElement } from './element';

interface IBaseTemplate {
template?: string;
Expand Down Expand Up @@ -57,9 +57,3 @@ export interface IMessage {
waitForAnswer?: boolean;
subMessages?: IMessage[];
}

export interface IMessageContent {
elements: IMessageElement[];
message: IMessage;
preserveSize?: boolean;
}
1 change: 1 addition & 0 deletions libs/react-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"react-plotly.js": "^2.6.0",
"react-resizable": "^3.0.5",
"rehype-katex": "^7.0.0",
"rehype-raw": "^7.0.0",
"remark-gfm": "^4.0.0",
"remark-math": "^6.0.0",
"swr": "^2.2.2",
Expand Down
45 changes: 45 additions & 0 deletions libs/react-components/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 28 additions & 3 deletions libs/react-components/src/Markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { useMemo } from 'react';
import ReactMarkdown from 'react-markdown';
import { PluggableList } from 'react-markdown/lib';
import rehypeKatex from 'rehype-katex';
import rehypeRaw from 'rehype-raw';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
import { Code } from 'src/Code';
Expand All @@ -21,15 +24,37 @@ import type { IMessageElement } from 'client-types/';
import { InlineCode } from './InlineCode';

interface Props {
allowHtml?: boolean;
latex?: boolean;
refElements?: IMessageElement[];
children: string;
}

function Markdown({ refElements, children }: Props) {
function Markdown({ refElements, allowHtml, latex, children }: Props) {
const rehypePlugins = useMemo(() => {
let rehypePlugins: PluggableList = [];
if (allowHtml) {
rehypePlugins = [rehypeRaw as any, ...rehypePlugins];
}
if (latex) {
rehypePlugins = [rehypeKatex as any, ...rehypePlugins];
}
return rehypePlugins;
}, [allowHtml, latex]);

const remarkPlugins = useMemo(() => {
let remarkPlugins: PluggableList = [remarkGfm as any];

if (latex) {
remarkPlugins = [remarkMath as any, ...remarkPlugins];
}
return remarkPlugins;
}, [latex]);

return (
<ReactMarkdown
remarkPlugins={[remarkGfm as any, remarkMath]}
rehypePlugins={[rehypeKatex]}
remarkPlugins={remarkPlugins}
rehypePlugins={rehypePlugins}
className="markdown-body"
components={{
a({ children, ...props }) {
Expand Down
4 changes: 4 additions & 0 deletions libs/react-components/src/messages/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const Message = memo(
hideCot,
highlightedMessage,
defaultCollapseContent,
allowHtml,
latex,
onError
} = useContext(MessageContext);

Expand Down Expand Up @@ -106,6 +108,8 @@ const Message = memo(
elements={elements}
message={message}
preserveSize={!!message.streaming || !defaultCollapseContent}
allowHtml={allowHtml}
latex={latex}
/>
<DetailsButton
message={message}
Expand Down
16 changes: 13 additions & 3 deletions libs/react-components/src/messages/components/MessageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@ import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';

import type { IMessageContent } from 'client-types/';
import type { IMessage, IMessageElement } from 'client-types/';

import { MessageButtons } from './MessageButtons';

const COLLAPSE_MIN_LINES = 25; // Set this to the maximum number of lines you want to display before collapsing
const COLLAPSE_MIN_LENGTH = 3000; // Set this to the maximum number of characters you want to display before collapsing

export interface Props {
elements: IMessageElement[];
message: IMessage;
preserveSize?: boolean;
allowHtml?: boolean;
latex?: boolean;
}

const MessageContent = memo(
({ message, elements, preserveSize }: IMessageContent) => {
({ message, elements, preserveSize, allowHtml, latex }: Props) => {
const { preparedContent, inlinedElements, refElements } = prepareContent({
elements,
id: message.id,
Expand All @@ -37,7 +45,9 @@ const MessageContent = memo(
}}
component="div"
>
<Markdown refElements={refElements}>{preparedContent}</Markdown>
<Markdown allowHtml={allowHtml} latex={latex} refElements={refElements}>
{preparedContent}
</Markdown>
</Typography>
);

Expand Down
2 changes: 2 additions & 0 deletions libs/react-components/src/types/messageContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ interface IMessageContext {
loading: boolean;
showFeedbackButtons: boolean;
uiName: string;
allowHtml?: boolean;
latex?: boolean;
onPlaygroundButtonClick?: (message: IMessage) => void;
onElementRefClick?: (element: IMessageElement) => void;
onFeedbackUpdated?: (
Expand Down