Skip to content

fix: render Ask AI answers as markdown with math 🤖 - #917

Closed
Wisesofthemall wants to merge 3 commits into
colorstackorg:mainfrom
Wisesofthemall:ask-ai-formatting/3-render
Closed

Wisesofthemall wants to merge 3 commits into
colorstackorg:mainfrom
Wisesofthemall:ask-ai-formatting/3-render

Conversation

@Wisesofthemall

@Wisesofthemall Wisesofthemall commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

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.

  • New AiMarkdown component (apps/member-profile/app/shared/components/ai-markdown.tsx) renders the blocks from parseAiMarkdown (feat: add KaTeX rendering and markdown parsers for AI answers 📝 #916) as React elements styled with Tailwind:
    • lists, headings, tables, code blocks, quotes and links
    • KaTeX math, inline and display
    • long math and wide tables scroll inside their own box instead of widening the page on mobile
  • Ask AI route (_profile.ask-ai.tsx): rebuilds the answer text from the existing answerSegments ([ref:N] included), renders it with AiMarkdown, and passes renderRef so citations still appear as the same ReferenceLink pills.
    • The server response shape is unchanged, so answers already cached in Redis (1 hour) render correctly too.
  • Prompt (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.
  • KaTeX CSS is imported by the component. React Router bundles it, including the fonts.

Screenshots 📸

The same answer before and after this PR, taken locally on /ask-ai with the answer stored in Redis:

Before (main) After (this PR)
Before: raw markdown After: formatted answer

Mobile (390px wide, no sideways page scroll):

After, on mobile

Sample answer source (1-everyday.md)
Great question! Here's how most members approach **offer negotiation** [ref:1]:

**Before you negotiate**
- Get the offer **in writing** first, including base, bonus and equity [ref:2].
- Research ranges on Levels.fyi for your *exact* level and location.
- Have a **competing offer** if you can; it's the strongest leverage you have.

**During the call**
1. Thank the recruiter and show you're *excited* about the role.
2. Ask: "Is there any flexibility on the base salary?"
3. If they say no, ask about a **signing bonus** or **relocation** instead [ref:3].

Some members got an extra $5,000 to $10,000 just by asking [ref:2]. Remember, the worst they can say is *no*.

• Tip: keep notes after every call.
• Tip: never accept on the spot; ask for a day or two.

More screenshots (markdown, math, tricky cases) are in #915 and #916.

Test cases 🧪

This sample answer was rendered on the server with renderToStaticMarkup(<AiMarkdown … />):

Here are a few tips for **technical interviews** [ref:1]:

1. Practice on LeetCode, focusing on *patterns* [ref:2].
2. Know the complexity of merge sort, \(O(n \log n)\).
- Premium costs $35 a month or $159 a year.

| Algorithm | Time |
|---|---|
| Binary search | \(O(\log n)\) |

\[ \sum_{i=1}^{n} i = \frac{n(n+1)}{2} \]

Use `git commit -m "$msg"` and see [the guide](https://leetcode.com).
Part of the answer Rendered as
**technical interviews** [ref:1] <strong> followed by the reference pill for thread 1
1. / 2. lines <ol class="list-decimal"> with <em>patterns</em> and pill 2 inside
\(O(n \log n)\) KaTeX inline math (<span class="katex">)
$35 … $159 literal text (prices are not math)
table <table> with a header row; KaTeX math in the cell
\[ … \] KaTeX display math (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 test in packages/utils passes: 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 build for member-profile succeeds, and the KaTeX fonts (KaTeX_*.woff2) are emitted.
  • ESLint and Prettier pass on every changed file.
  • tsc reports no errors in any changed file.
  • I ran the app locally (Postgres, Redis, react-router dev), logged in, and loaded /ask-ai with 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 🐞

  • Feature - A non-breaking change which adds functionality.
  • Fix - A non-breaking change which fixes an issue.
  • Refactor - A change that neither fixes a bug nor adds a feature.
  • Documentation - A change only to in-code or markdown documentation.
  • Tests - A change that adds missing unit/integration tests.
  • Chore - A change that is likely none of the above.

Checklist ✅

  • I have done a self-review of my code.
  • I have manually tested my code (if applicable). (local /ask-ai with stored answers; live questions not tested, see above)
  • I have added/updated any relevant documentation (if applicable).

🤖 Generated with Claude Code

Wisesofthemall and others added 3 commits November 4, 2026 04:36
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 levin0710 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Wisesofthemall

Copy link
Copy Markdown
Contributor Author

Closing in favor of #918, which uses react-markdown instead of our own parsing logic, per the review feedback. Thanks!

@Wisesofthemall
Wisesofthemall deleted the ask-ai-formatting/3-render branch November 7, 2026 04:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ASK AI formatting is off

2 participants