diff --git a/gui/package-lock.json b/gui/package-lock.json index bdfdd6b62..a5786fb30 100644 --- a/gui/package-lock.json +++ b/gui/package-lock.json @@ -50,6 +50,7 @@ "rehype-katex": "^7.0.0", "rehype-wrap-all": "^1.1.0", "remark-math": "^6.0.0", + "reselect": "^5.1.1", "seti-file-icons": "^0.0.8", "socket.io-client": "^4.7.2", "styled-components": "^5.3.6", @@ -1180,6 +1181,11 @@ } } }, + "node_modules/@reduxjs/toolkit/node_modules/reselect": { + "version": "4.1.8", + "resolved": "https://registry.npmjs.org/reselect/-/reselect-4.1.8.tgz", + "integrity": "sha512-ab9EmR80F/zQTMNeneUr4cv+jSwPJgIlvEmVwLerwrWVbpLlBuls9XHzIeTFy4cegU2NHBp3va0LKOzU5qFEYQ==" + }, "node_modules/@remirror/core-constants": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/@remirror/core-constants/-/core-constants-2.0.2.tgz", @@ -6917,9 +6923,9 @@ } }, "node_modules/reselect": { - "version": "4.1.8", - "resolved": "https://registry.npmjs.org/reselect/-/reselect-4.1.8.tgz", - "integrity": "sha512-ab9EmR80F/zQTMNeneUr4cv+jSwPJgIlvEmVwLerwrWVbpLlBuls9XHzIeTFy4cegU2NHBp3va0LKOzU5qFEYQ==" + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/reselect/-/reselect-5.1.1.tgz", + "integrity": "sha512-K/BG6eIky/SBpzfHZv/dd+9JBFiS4SWV7FIujVyJRux6e45+73RaUHXLmIR1f7WOMaQ0U1km6qwklRQxpJJY0w==" }, "node_modules/resolve": { "version": "1.22.8", diff --git a/gui/package.json b/gui/package.json index 190235d68..3b9842d82 100644 --- a/gui/package.json +++ b/gui/package.json @@ -52,6 +52,7 @@ "rehype-katex": "^7.0.0", "rehype-wrap-all": "^1.1.0", "remark-math": "^6.0.0", + "reselect": "^5.1.1", "seti-file-icons": "^0.0.8", "socket.io-client": "^4.7.2", "styled-components": "^5.3.6", diff --git a/gui/src/components/markdown/LinkableCode.tsx b/gui/src/components/markdown/LinkableCode.tsx index 3481942c3..716e26dd6 100644 --- a/gui/src/components/markdown/LinkableCode.tsx +++ b/gui/src/components/markdown/LinkableCode.tsx @@ -1,7 +1,9 @@ import { useContext, useEffect, useState } from "react"; +import { useSelector } from "react-redux"; import styled from "styled-components"; import { defaultBorderRadius, vscInputBackground } from ".."; import { IdeMessengerContext } from "../../context/IdeMessenger"; +import { memoizedContextItemsSelector } from "../../redux/slices/stateSlice"; const StyledCode = styled.code<{ link: boolean }>` color: ${(props) => (props.link ? "#ff4343" : "#f78383")}; @@ -18,37 +20,64 @@ const StyledCode = styled.code<{ link: boolean }>` function LinkableCode(props: any) { const ideMessenger = useContext(IdeMessengerContext); + const contextItems = useSelector(memoizedContextItemsSelector); + const [linkingDone, setLinkingDone] = useState(false); const [isLink, setIsLink] = useState(false); const [filepath, setFilepath] = useState(""); + const [line, setLine] = useState(0); useEffect(() => { if (linkingDone) return; setLinkingDone(true); // Get filename from props.children - let filename: string | undefined = undefined; + let content: string | undefined = undefined; if (typeof props.children === "string") { - filename = props.children; + content = props.children; } else if ( Array.isArray(props.children) && props.children.length > 0 && typeof props.children[0] === "string" ) { - filename = props.children[0]; + content = props.children[0]; } - if (!filename) return; + if (!content) return; // Check if this is a real file - // TODO - let link = filename.slice(-4).includes("."); + const contextItemFileMatch = contextItems.find( + (item) => + item.id.providerTitle === "file" && item.description.endsWith(content), + ); + if (contextItemFileMatch) { + setIsLink(true); + setFilepath(contextItemFileMatch.description); + setLine(0); + } - // setIsLink(link); - setFilepath(filename); - }, [linkingDone]); + if (content.length > 6) { + const contextItemContentMatch = contextItems.find((item) => + item.content.includes(content), + ); + if (contextItemContentMatch) { + setIsLink(true); + setFilepath(contextItemContentMatch.description); + setLine( + contextItemContentMatch.content + .split("\n") + .findIndex((line) => line.includes(content)) + 1, + ); + } + } + }, [linkingDone, contextItems]); const onClick = () => { - ideMessenger.post("showFile", { filepath }); + if (!isLink) return; + ideMessenger.post("showLines", { + filepath, + startLine: line, + endLine: line + 1, + }); }; return ( diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index 50139b3b2..7ae3c9a61 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -14,6 +14,7 @@ import { } from ".."; import { RootState } from "../../redux/store"; import { getFontSize } from "../../util"; +import LinkableCode from "./LinkableCode"; import PreWithToolbar from "./PreWithToolbar"; import { SyntaxHighlightedPre } from "./SyntaxHighlightedPre"; import "./katex.css"; @@ -169,6 +170,17 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( ); }, + code: ({ node, ...codeProps }) => { + if ( + codeProps.className?.split(" ").includes("hljs") || + codeProps.children?.length > 1 + ) { + return {codeProps.children}; + } + return ( + {codeProps.children} + ); + }, // pre: ({ node, ...preProps }) => { // const codeString = // preProps.children?.[0]?.props?.children?.[0].trim() || ""; diff --git a/gui/src/redux/slices/stateSlice.ts b/gui/src/redux/slices/stateSlice.ts index 5c04d27fe..16222ccbf 100644 --- a/gui/src/redux/slices/stateSlice.ts +++ b/gui/src/redux/slices/stateSlice.ts @@ -11,7 +11,19 @@ import { } from "core"; import { BrowserSerializedContinueConfig } from "core/config/load"; import { stripImages } from "core/llm/countTokens"; +import { createSelector } from "reselect"; import { v4 } from "uuid"; +import { RootState } from "../store"; + +export const memoizedContextItemsSelector = createSelector( + [(state: RootState) => state.state.history], + (history) => { + return history.reduce((acc, item) => { + acc.push(...item.contextItems); + return acc; + }, []); + }, +); const TEST_CONTEXT_ITEMS: ContextItemWithId[] = [ {