Repository navigation
feat: add math core for formatting AI answers 🧮 - #915
Closed
Wisesofthemall wants to merge 1 commit into
Closed
Wisesofthemall wants to merge 1 commit into
Wisesofthemall wants to merge 1 commit 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>
Wisesofthemall
requested review from
levin0710,
queenellie873 and
ramiAbdou
as code owners
November 4, 2026 09:40
This was referenced Nov 4, 2026
Wisesofthemall
marked this pull request as draft
November 4, 2026 09:50
Wisesofthemall
marked this pull request as ready for review
November 4, 2026 10:05
3 of 9 tasks
Contributor
Author
|
Closing in favor of #918, which uses react-markdown instead of our own parsing logic, per the review feedback. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description ✏️
Part 1 of 3 for #914 (Ask AI formatting is off). Next: #916 (KaTeX and parsers), then #917 (switches it on). Nothing uses this code yet.
The Ask AI answer is shown as raw text today, so the model's markdown (
**bold**,1.lists, backticks) appears as-is. The fix renders the answer as markdown with math. This PR adds the hardest part: deciding which$...$spans are math and which are prices.What's added:
packages/utils/src/ai-formatting/math.ts, exported as@oyster/utils/ai-formatting. It has no KaTeX import, so it runs on the server and in tests.findInlineMathDollarSpansis a scanner, not a regex. When it rejects a span, it moves forward one character from that span's opening$, so the rejected span's closing$can still open the next span. That's howCosts $5, $10 and $x$.still finds$x$. A lone$skips to the end of its line, so one price doesn't stop later math from rendering.isInlineMathSpanandlooksLikeLatexdecide whether a span is math, using the same spacing rules as pandoc:$5 and $10and$5-$10are currency.$ E = mc^2 $is math.$x-axis$is prose.protectCodeRegionsmasks code fences and inline code so that later passes never touch them.normalizeLatexandbalanceLatexBracesfix common model mistakes before KaTeX runs:𝑥x 2written instead ofx^2}packages/utils/tsconfig.jsonnow sets"target": "ESNext", likeapps/member-profile. The regexes need theuflag and lookbehind, which the old ES5 default target doesn't allow.Screenshots 📸
These were taken locally on the
/ask-aipage with the full stack (#917) applied. A sample answer was stored in Redis so the page renders it without calling the AI. The sample's markdown source is in the collapsed section below the image.The math-vs-currency logic in this PR. Prices, ranges,
5 * 3 * 2, hyphenated words and$inside code all stay as plain text. Only$x$and$E = mc^2$become math:Sample answer source (
4-tricky.md)Test cases 🧪
Run with
cd packages/utils && bun test(no database needed). 60 pass, 0 fail.Costs $5, $10, $20 and $x$.xis math$5 and $10,$5-$10,$100,000 is twice $50,000$3x$,$f(x)$,$f''(t)$,$|x - 3|$,$A'$,$O(n \log n)$$2x < 4$,$x >= 0$,$a != b$$x-axis$,$p-value$,$low-level$$ E = mc^2 $^beats the spacing rule)$ x $It costs $5.thenSolve $x^2 = 4$.on the next line`price = $5`and a fence containing$x$w = abba \in \Sigma^*}}dropped𝑥 + 𝐀,x 2 + y 2 = 1x + A,x^2 + y^2 = 1Full test output
(The 13 tests not listed are the existing ones in
src/index.test.ts.)Type of Change 🐞
Checklist ✅
🤖 Generated with Claude Code