Repository navigation
fix: render Ask AI answers as markdown with math 🤖 - #917
Wisesofthemall wants to merge 3 commits into
Conversation
Adds the pure math helpers that decide which `$...$` spans are math and which are prices, plus code masking and LaTeX cleanup. Nothing uses them yet; they are the first piece of the fix for colorstackorg#914. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds `safeKatexRender` (falls back to the escaped source instead of KaTeX's red error output), `preprocessContent` (normalizes `\(...\)` and `\[...\]` to `$`/`$$` and strips stray HTML, never touching code), and the inline and block parsers that turn raw AI output into tokens a renderer can map to elements. Nothing uses them yet. Part of colorstackorg#914. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Renders the Ask AI answer with the new `AiMarkdown` component instead of raw text, so lists, bold, tables, code and LaTeX math display properly, with `[ref:N]` citations still shown as links to the Slack threads. Also tells the model to format its answer in markdown and to write math as `\(...\)` / `\[...\]`. Closes colorstackorg#914. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
levin0710
left a comment
There was a problem hiding this comment.
Hey @Wisesofthemall thanks for the PR! I'll just use this one to review since the stacking doesnt work since you are using a fork and i think the changes logically should live together in one PR for this particular case.
I am a bit concerned about introducing our own markdown parsing logic to our codebase. I think taking on the responsibility to maintain this logic is beyond what our goal is with oyster and could introduce bugs in the future.
Furthermore, I think the specific math parsing might be unnecessary since we don't anticipate our ask AI to return any Latex really. The goal of the ask AI feature is to answer questions using our slack workspace history. I don't anticipate those questions to be math related but rather information related.
I think we should try using an existing markdown parser library like react-markdown to solve this issue. That way we don't have to worry about the markdown parsing logic at all while still solving the bug and provide a better response experience.
|
Closing in favor of #918, which uses react-markdown instead of our own parsing logic, per the review feedback. Thanks! |
Description ✏️
Closes #914
Part 3 of 3. Depends on #915 and #916. The PR comes from a fork, so its diff includes their commits. Please review only the last commit,
38441ba, until they merge.This turns the formatting on for Ask AI. Before this PR, the answer was printed as raw text inside
<Text className="whitespace-break-spaces">, so the model's markdown showed up literally (**,1.,-, backticks), as in the issue's screenshot.AiMarkdowncomponent (apps/member-profile/app/shared/components/ai-markdown.tsx) renders the blocks fromparseAiMarkdown(feat: add KaTeX rendering and markdown parsers for AI answers 📝 #916) as React elements styled with Tailwind:_profile.ask-ai.tsx): rebuilds the answer text from the existinganswerSegments([ref:N]included), renders it withAiMarkdown, and passesrenderRefso citations still appear as the sameReferenceLinkpills.MEMBER_PROFILE_SYSTEM_PROMPT): asks the model to format its answer in markdown and to write math as\(...\)/\[...\]rather than$, so math is never confused with prices. It also asks the model not to use---as a divider, since---separates the JSON block from the answer.Screenshots 📸
The same answer before and after this PR, taken locally on
/ask-aiwith the answer stored in Redis:main)Mobile (390px wide, no sideways page scroll):
Sample answer source (
1-everyday.md)More screenshots (markdown, math, tricky cases) are in #915 and #916.
Test cases 🧪
This sample answer was rendered on the server with
renderToStaticMarkup(<AiMarkdown … />):**technical interviews** [ref:1]<strong>followed by the reference pill for thread 11./2.lines<ol class="list-decimal">with<em>patterns</em>and pill 2 inside\(O(n \log n)\)<span class="katex">)$35 … $159<table>with a header row; KaTeX math in the cell\[ … \]katex-display), centered and scrollable`git commit -m "$msg"`<code>, with the$untouched[the guide](https://leetcode.com)<a target="_blank" rel="noopener noreferrer">Other checks:
bun testinpackages/utilspasses: 98 pass, 0 fail. The parser and math cases are in feat: add math core for formatting AI answers 🧮 #915 and feat: add KaTeX rendering and markdown parsers for AI answers 📝 #916.react-router buildformember-profilesucceeds, and the KaTeX fonts (KaTeX_*.woff2) are emitted.tscreports no errors in any changed file.react-router dev), logged in, and loaded/ask-aiwith sample answers stored in Redis. The screenshots above come from that. I didn't submit live questions, because they need Anthropic, Pinecone and Slack keys. Please ask a list-style question and a math question on a preview before merging, to check that the updated prompt produces this format.Type of Change 🐞
Checklist ✅
/ask-aiwith stored answers; live questions not tested, see above)🤖 Generated with Claude Code