Skip to content

Commit

Permalink
k
Browse files Browse the repository at this point in the history
  • Loading branch information
pablonyx committed Dec 18, 2024
1 parent 11866bf commit ae64e46
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
11 changes: 7 additions & 4 deletions web/package-lock.json

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

2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"favicon-fetch": "^1.0.0",
"formik": "^2.2.9",
"js-cookie": "^3.0.5",
"katex": "^0.16.15",
"katex": "^0.16.17",
"lodash": "^4.17.21",
"lucide-react": "^0.454.0",
"mdast-util-find-and-replace": "^3.0.1",
Expand Down
13 changes: 9 additions & 4 deletions web/src/app/chat/message/Messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import RegenerateOption from "../RegenerateOption";
import { LlmOverride } from "@/lib/hooks";
import { ContinueGenerating } from "./ContinueMessage";
import { MemoizedAnchor, MemoizedParagraph } from "./MemoizedTextComponents";
import { extractCodeText } from "./codeUtils";
import { extractCodeText, preprocessLaTeX } from "./codeUtils";
import ToolResult from "../../../components/tools/ToolResult";
import CsvContent from "../../../components/tools/CSVContent";
import SourceCard, {
Expand All @@ -75,7 +75,7 @@ import SourceCard, {

import remarkMath from "remark-math";
import rehypeKatex from "rehype-katex";
import "katex/dist/katex.min.css"; // Add this import for KaTeX styles
import "katex/dist/katex.min.css";

const TOOLS_WITH_CUSTOM_HANDLING = [
SEARCH_TOOL_NAME,
Expand Down Expand Up @@ -228,6 +228,7 @@ export const AIMessage = ({
setPresentingDocument?: (document: OnyxDocument) => void;
}) => {
const toolCallGenerating = toolCall && !toolCall.tool_result;

const processContent = (content: string | JSX.Element) => {
if (typeof content !== "string") {
return content;
Expand All @@ -246,12 +247,16 @@ export const AIMessage = ({

const lastMatch = matches[matches.length - 1];
if (!lastMatch.endsWith("```")) {
return content;
return preprocessLaTeX(content);
}
}

return content + (!isComplete && !toolCallGenerating ? " [*]() " : "");
return (
preprocessLaTeX(content) +
(!isComplete && !toolCallGenerating ? " [*]() " : "")
);
};

const finalContent = processContent(content as string);

const [isRegenerateHovered, setIsRegenerateHovered] = useState(false);
Expand Down
14 changes: 14 additions & 0 deletions web/src/app/chat/message/codeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,17 @@ export function extractCodeText(

return codeText || "";
}

export const preprocessLaTeX = (content: string) => {
// Replace block-level LaTeX delimiters \[ \] with $$ $$
const blockProcessedContent = content.replace(
/\\\[([\s\S]*?)\\\]/g,
(_, equation) => `$$${equation}$$`
);
// Replace inline LaTeX delimiters \( \) with $ $
const inlineProcessedContent = blockProcessedContent.replace(
/\\\(([\s\S]*?)\\\)/g,
(_, equation) => `$${equation}$`
);
return inlineProcessedContent;
};

0 comments on commit ae64e46

Please sign in to comment.