From 4afa355b0b4c357702ada1fa04a1f454cd2bda3a Mon Sep 17 00:00:00 2001 From: end-4 <97237370+end-4@users.noreply.github.com> Date: Mon, 26 Feb 2024 22:43:27 +0700 Subject: [PATCH] ai: add latex rendering --- .config/ags/modules/.miscutils/md2pango.js | 3 + .../modules/sideleft/apis/ai_chatmessage.js | 66 ++++++++++++++++++- .config/ags/scss/_sidebars.scss | 9 +++ install.sh | 9 +++ scriptdata/installers | 15 +++++ 5 files changed, 100 insertions(+), 2 deletions(-) diff --git a/.config/ags/modules/.miscutils/md2pango.js b/.config/ags/modules/.miscutils/md2pango.js index 054752b1..8f56e3ae 100644 --- a/.config/ags/modules/.miscutils/md2pango.js +++ b/.config/ags/modules/.miscutils/md2pango.js @@ -83,4 +83,7 @@ console.log('uwu'); \`\`\` - Random instruction thing - To update arch lincox, run \`sudo pacman -Syu\` +\`\`\`tex +\\frac{d}{dx} \\left( \\frac{x-438}{x^2+23x-7} \\right) = \\frac{-x^2 + 869}{(x^2+23x-7)^2} +\`\`\` `; \ No newline at end of file diff --git a/.config/ags/modules/sideleft/apis/ai_chatmessage.js b/.config/ags/modules/sideleft/apis/ai_chatmessage.js index 5bc44f84..2b886482 100644 --- a/.config/ags/modules/sideleft/apis/ai_chatmessage.js +++ b/.config/ags/modules/sideleft/apis/ai_chatmessage.js @@ -3,12 +3,12 @@ import GtkSource from "gi://GtkSource?version=3.0"; import App from 'resource:///com/github/Aylur/ags/app.js'; import Widget from 'resource:///com/github/Aylur/ags/widget.js'; import * as Utils from 'resource:///com/github/Aylur/ags/utils.js'; -const { Box, Button, Label, Scrollable } = Widget; +const { Box, Button, Label, Icon, Scrollable } = Widget; const { execAsync, exec } = Utils; import { MaterialIcon } from '../../.commonwidgets/materialicon.js'; import md2pango from '../../.miscutils/md2pango.js'; - +const LATEX_DIR = `${GLib.get_user_cache_dir()}/ags/media/latex`; const CUSTOM_SOURCEVIEW_SCHEME_PATH = `${App.configDir}/assets/themes/sourceviewtheme.xml`; const CUSTOM_SCHEME_ID = 'custom'; const USERNAME = GLib.get_user_name(); @@ -82,7 +82,69 @@ const TextBlock = (content = '') => Label({ label: content, }); +Utils.execAsync(['bash', '-c', `rm ${LATEX_DIR}/*`]) + .then(() => Utils.execAsync(['bash', '-c', `mkdir -p ${LATEX_DIR}`])) + .catch(print); +const Latex = (content = '') => { + const latexViewArea = Box({ + // vscroll: 'never', + // hscroll: 'automatic', + attribute: { + render: async (self, text) => { + if (text.length == 0) return; + const styleContext = self.get_style_context(); + const fontSize = styleContext.get_property('font-size', Gtk.StateFlags.NORMAL); + + const timeSinceEpoch = Date.now(); + const fileName = `${timeSinceEpoch}.tex`; + const outFileName = `${timeSinceEpoch}-symbolic.svg`; + const scriptFileName = `${timeSinceEpoch}-render.sh`; + const filePath = `${LATEX_DIR}/${fileName}`; + const outFilePath = `${LATEX_DIR}/${outFileName}`; + const scriptFilePath = `${LATEX_DIR}/${scriptFileName}`; + + Utils.writeFile(text, filePath).catch(print); + // Since MicroTex doesn't support file path input properly, we gotta cat it + // And escaping such a command is a fucking pain so I decided to just generate a script + // Note: MicroTex doesn't support `&=` + // You can add this line in the middle for debugging: echo "$text" > ${filePath}.tmp + const renderScript = `#!/usr/bin/env bash +text=$(cat ${filePath} | sed 's/$/ \\\\\\\\/g' | sed 's/&=/=/g') +LaTeX -headless -input="$text" -output=${outFilePath} -textsize=${fontSize * 1.1} -padding=0 -maxwidth=${latexViewArea.get_allocated_width() * 0.85} +`; + Utils.writeFile(renderScript, scriptFilePath).catch(print); + Utils.exec(`chmod a+x ${scriptFilePath}`) + Utils.timeout(100, () => { + Utils.exec(`bash ${scriptFilePath}`); + Gtk.IconTheme.get_default().append_search_path(LATEX_DIR); + self.child?.destroy(); + self.child = Gtk.Image.new_from_file(outFilePath); + }) + } + }, + setup: (self) => self.attribute.render(self, content).catch(print), + }); + const wholeThing = Box({ + className: 'sidebar-chat-latex', + homogeneous: true, + attribute: { + 'updateText': (text) => { + latexViewArea.attribute.render(latexViewArea, text).catch(print); + } + }, + children: [Scrollable({ + vscroll: 'never', + hscroll: 'automatic', + child: latexViewArea + })] + }) + return wholeThing; +} + const CodeBlock = (content = '', lang = 'txt') => { + if (lang == 'tex' || lang == 'latex') { + return Latex(content); + } const topBar = Box({ className: 'sidebar-chat-codeblock-topbar', children: [ diff --git a/.config/ags/scss/_sidebars.scss b/.config/ags/scss/_sidebars.scss index 51a3b5dd..a4a98d5a 100644 --- a/.config/ags/scss/_sidebars.scss +++ b/.config/ags/scss/_sidebars.scss @@ -655,6 +655,15 @@ $colorpicker_rounding: 0.341rem; @include readingfont; } +.sidebar-chat-latex { + margin: 0rem 0.682rem; + @if $darkmode ==true { + background-color: white; + } + color: $onBackground; + // background-color: $termbg; +} + .sidebar-chat-codeblock { @include normal-rounding; // @include elevation2; diff --git a/install.sh b/install.sh index d09f97b8..8b2a1d02 100755 --- a/install.sh +++ b/install.sh @@ -127,6 +127,15 @@ if $(test -d /usr/local/share/icons/Bibata-Modern-Classic); then else ask_bibata=true fi if $ask_bibata;then showfun install-bibata;v install-bibata;fi + +if $(test -d /usr/local/bin/LaTex); then + echo -e "\e[33m[$0]: Program \"MicroTex\" already exists, no need to install.\e[0m" + echo -e "\e[34mYou can reinstall it in order to update to the latest version anyway.\e[0m" + ask_MicroTex=$ask +else ask_MicroTex=true +fi +if $ask_MicroTex;then showfun install-MicroTex;v install-MicroTex;fi + ##################################################################################### printf "\e[36m[$0]: 3. Copying\e[97m\n" diff --git a/scriptdata/installers b/scriptdata/installers index db3b29e8..aad02b94 100644 --- a/scriptdata/installers +++ b/scriptdata/installers @@ -86,3 +86,18 @@ install-bibata (){ x sudo cp -r $name /usr/local/share/icons x cd $base } + +install-MicroTex (){ + x mkdir -p $base/cache/MicroTex + x cd $base/cache/MicroTex + try git init -b master + try git remote add origin https://github.com/NanoMichael/MicroTeX.git + x git pull origin master && git submodule update --init --recursive + x mkdir -p build + x cd build + x cmake .. + x make -j32 + x sudo mkdir -p /usr/local/bin + x sudo cp ./LaTeX /usr/local/bin/ + x cd $base +}