Repository navigation
chore: upgrade @assistant-ui to 0.14 and replace all custom streaming-rendering code with built-in APIs #51653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
6376c69
6a88a08
0d92a99
cab0f69
24a940f
fbe98ca
4b12864
94756bd
27f8d78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ import { | |
| import { | ||
| type AssistantRuntime, | ||
| type ExternalStoreAdapter, | ||
| fromThreadMessageLike, | ||
| generateId, | ||
| type ThreadMessage, | ||
| useRuntimeAdapters | ||
| } from '@assistant-ui/react' | ||
|
|
@@ -135,10 +137,16 @@ class IncrementalExternalStoreThreadRuntimeCore extends ExternalStoreThreadRunti | |
| } | ||
|
|
||
| if (hasUpcomingMessage(isRunning, messages)) { | ||
| self._assistantOptimisticId = this.repository.appendOptimisticMessage(messages.at(-1)?.id ?? null, { | ||
| role: 'assistant', | ||
| content: [] | ||
| }) | ||
| const optimisticId = generateId() | ||
| this.repository.addOrUpdateMessage( | ||
| messages.at(-1)?.id ?? null, | ||
| fromThreadMessageLike( | ||
| { role: 'assistant', content: [], metadata: { isOptimistic: true } }, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The inlining of the removed This is the highest-risk spot since it's the one place we hand-reimplement a core method against a core that jumped two minors. Questions:
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. faithful, yes. on whether isOptimistic is a contract or an implementation detail: it's a documented public field, not something we're reaching past. core's published types carry the JSDoc on on eviction (Q1): verified clean against the installed 0.2.18. stop bypasses core's |
||
| optimisticId, | ||
| { type: 'running' } | ||
| ) | ||
| ) | ||
| self._assistantOptimisticId = optimisticId | ||
| } | ||
|
|
||
| this.repository.resetHead(self._assistantOptimisticId ?? messages.at(-1)?.id ?? null) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import { escapeCurrencyDollars, normalizeMathDelimiters } from '@assistant-ui/react-streamdown' | ||
|
|
||
| import { isLikelyProseFence, sanitizeLanguageTag } from '@/lib/markdown-code' | ||
| import { stripPreviewTargets } from '@/lib/preview-targets' | ||
|
|
||
|
|
@@ -310,41 +312,6 @@ function normalizeFenceBlocks(text: string): string { | |
| return out.join('\n') | ||
| } | ||
|
|
||
| // Convert LaTeX bracket delimiters to remark-math's dollar-sign syntax. | ||
| // Models often emit `\(...\)` for inline math and `\[...\]` for display | ||
| // math (the standard LaTeX convention) instead of `$...$` / `$$...$$`. | ||
| // remark-math only natively recognizes the dollar form, so we rewrite at | ||
| // preprocess time. Done with simple non-greedy matches keyed on the | ||
| // escaped-bracket sequences — these are rare enough in non-math content | ||
| // (you'd have to write a literal `\(` followed eventually by a literal | ||
| // `\)` with NO interleaving newline-paragraph-break) that false positives | ||
| // are extremely unlikely. | ||
| const LATEX_INLINE_RE = /\\\(([^\n]+?)\\\)/g | ||
| const LATEX_DISPLAY_RE = /\\\[([\s\S]+?)\\\]/g | ||
|
|
||
| function rewriteLatexBracketDelimiters(text: string): string { | ||
| return text | ||
| .replace(LATEX_INLINE_RE, (_, body: string) => `$${body}$`) | ||
| .replace(LATEX_DISPLAY_RE, (_, body: string) => `$$${body}$$`) | ||
| } | ||
|
|
||
| // Escape `$<digit>` patterns so they don't get eaten as math delimiters. | ||
| // Models commonly write currency amounts ($5, $19.99, $1,299) in prose. | ||
| // With `singleDollarTextMath: true`, remark-math is greedy and matches | ||
| // EVERY pair of `$`s — including the open of `$5` to the next `$10`, | ||
| // rendering "5 in my pocket and you have " as italicized math text. | ||
| // The de-facto convention across math-supporting LLM UIs is to treat | ||
| // `$` followed by a digit as currency rather than math, since math | ||
| // expressions almost always start with a letter or `\command`. Trade- | ||
| // off: a math expression like `$5x = 10$` would have its leading 5 | ||
| // escaped — annoying but rare. The escape `\$` survives to render as | ||
| // a literal `$` in the final output. | ||
| const CURRENCY_DOLLAR_RE = /(^|[^\\])\$(?=\d)/g | ||
|
|
||
| function escapeCurrencyDollars(text: string): string { | ||
| return text.replace(CURRENCY_DOLLAR_RE, '$1\\$') | ||
| } | ||
|
|
||
| export function preprocessMarkdown(text: string): string { | ||
| const cleaned = text.replace(REASONING_BLOCK_RE, '').replace(PREVIEW_MARKER_RE, '') | ||
| const scrubbed = scrubBacktickNoise(cleaned) | ||
|
|
@@ -377,12 +344,12 @@ export function preprocessMarkdown(text: string): string { | |
| const leading = part.match(/^\s*/)?.[0] ?? '' | ||
| const trailing = part.match(/\s*$/)?.[0] ?? '' | ||
|
|
||
| // rewriteLatexBracketDelimiters runs only on prose segments so | ||
| // we don't accidentally touch `\(` inside a code block. | ||
| // escapeCurrencyDollars likewise only runs on prose, so legit | ||
| // `$5` literals inside fenced code stay intact. | ||
| // normalizeMathDelimiters rewrites LaTeX bracket delimiters and | ||
| // custom math tags to dollar form. escapeCurrencyDollars escapes | ||
| // `$<digit>` so currency isn't eaten as math. Both run only on | ||
| // prose segments so code blocks stay untouched. | ||
| const transformed = normalizeVisibleProse( | ||
| stripPreviewTargets(rewriteLatexBracketDelimiters(escapeCurrencyDollars(part))) | ||
| stripPreviewTargets(normalizeMathDelimiters(escapeCurrencyDollars(part))) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This swap is a behavior-changing superset, not 1:1 — worth calling out explicitly in the PR body so nobody assumes byte-identical output. Comparing the deleted custom helpers vs the built-ins now resolved (
Questions:
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agreed on the headline: it's a behavior-changing superset, not 1:1, and i'll say so in the PR body. all four are intentional. three of the four are exactly right: the double-backslash matching, one correction on the trim: on Q1 (fixtures): correct, there were none that distinguish old from new. the 16 on Q2 (widened rewrite): real but bounded. |
||
| ) | ||
|
|
||
| return leading + transformed + trailing | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pipeline reorder (
smooth → defer → preprocess⟶preprocess → smooth → defer) is not intermediate-frame identical for reasoning text, even though the final render is. In the built-inStreamdownTextPrimitive,useSmoothruns on the already-remend-repaired full text, and the revealed prefix is then rendered withparseIncompleteMarkdown: falseand no per-prefix repair.Consequence (reasoning only — this
MarkdownTextContentpath withsmooth; the body-textMarkdownTextisdefer-only and genuinely equivalent): the OLD code re-rantailBoundedRemendon each revealed prefix, so an incomplete**boldshowed up already-styled during the reveal. The NEW code reveals a prefix of the repaired text, so the opener is shown before its closer →**, backtick,$briefly flash as literal syntax at the typewriter frontier until the reveal catches up.Questions:
SMOOTH_OPTIONSmatches the old constants, but the built-inTextStreamAnimatoruses a different rate algorithm than the deleteduseSmoothReveal(incl. amaxCharIntervalMsdefault of 5ms). Did the reveal cadence visibly match the old feel side-by-side, or just approximately?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
confirmed, and you're right that this is the one place "end result is identical" overclaims. it's intermediate-frame divergent on the reasoning path only (
MarkdownTextContentwithsmooth; body text isdefer-only and genuinely equivalent). the mechanism is exactly as you describe: the built-in runspreprocesson the full text,useSmoothslices a prefix of the already-repaired string, and because we passparseMarkdownIntoBlocksFnthe built-in tail-remend is gated off (!parseMarkdownIntoBlocksFn) withparseIncompleteMarkdownfalse, so the reveal frontier is never re-repaired and an unclosed**, backtick, or$shows raw until its closer is revealed.treating it as acceptable: cosmetic and reasoning-only. narrowed the PR body to final-state equivalence and fixed the misleading inline comment in fbe98ca. if we decide the flicker isn't shippable, the fix is either dropping
parseMarkdownIntoBlocksFnon the smooth surface (costs the reasoning block-parse cache) or settingparseIncompleteMarkdownnon-false there (full remend per flush on reasoning).on cadence (Q2): approximately, not exact.
maxCharIntervalMsis unset so it defaults to 5ms against the old ~33ms floor, so the tail reveals faster. if the feel is off we can setmaxCharIntervalMsexplicitly to match.