Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ jobs:
# Publish dialog holding their layout wide, narrow and on a phone, and the Admin screens
# where a rule or a grant names a team holding theirs wide and on a phone, an unticked
# tickbox's edge standing 3:1 from what it is drawn on in light and dark, a Needs you row read
# whole at a phone's width and a chat's with its answers on a line of their own, which only a
# whole at a phone's width and a chat's with its answers on a line of their own, a Bot's maths
# drawn as formulas in KaTeX's font with a wide one scrolling in its own block, which only a
# browser's layout, cascade
# and paint resolve, and the app following the system's light or dark, which only a
# browser's `prefers-color-scheme` reports, with what OpenBot Desktop's window is told of
Expand All @@ -206,7 +207,7 @@ jobs:
# test script's pretest does).
- run: |
bun run generate:app-config
bun test app/tests/tool-line-reduced-motion.test.tsx app/tests/onboarding-window-buttons.test.tsx app/tests/onboarding-orb-room.test.tsx app/tests/transcript-rail-clearance.test.tsx app/tests/a2ui-frame.test.tsx app/tests/update-banner-layout.test.tsx app/tests/composer-dictation-notice-layout.test.tsx app/tests/conversation-intro-halo.test.tsx app/tests/transcript-room.test.tsx app/tests/skins-window-buttons.test.tsx app/tests/opendots-rail-badge.test.tsx app/tests/account-version-layout.test.tsx app/tests/organization-menu-names.test.tsx app/tests/bot-orb-one-color.test.tsx app/tests/theme-follows-system.test.tsx app/tests/desktop-window-theme.test.tsx app/tests/bot-screen-record-layout.test.tsx app/tests/teams-screens-layout.test.tsx app/tests/team-grants-screens-layout.test.tsx app/tests/needs-you-board-layout.test.tsx app/tests/checkbox-contrast.test.tsx
bun test app/tests/tool-line-reduced-motion.test.tsx app/tests/onboarding-window-buttons.test.tsx app/tests/onboarding-orb-room.test.tsx app/tests/transcript-rail-clearance.test.tsx app/tests/a2ui-frame.test.tsx app/tests/update-banner-layout.test.tsx app/tests/composer-dictation-notice-layout.test.tsx app/tests/conversation-intro-halo.test.tsx app/tests/transcript-room.test.tsx app/tests/skins-window-buttons.test.tsx app/tests/opendots-rail-badge.test.tsx app/tests/account-version-layout.test.tsx app/tests/organization-menu-names.test.tsx app/tests/bot-orb-one-color.test.tsx app/tests/theme-follows-system.test.tsx app/tests/desktop-window-theme.test.tsx app/tests/bot-screen-record-layout.test.tsx app/tests/teams-screens-layout.test.tsx app/tests/team-grants-screens-layout.test.tsx app/tests/needs-you-board-layout.test.tsx app/tests/checkbox-contrast.test.tsx app/tests/maths-rendering.test.tsx
env:
OPENBOT_APP_BROWSER: '1'
# The desktop setup window's, for the same reason: the AI step's form holding still while the
Expand Down
2 changes: 2 additions & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@openbot/shared": "workspace:*",
"@shadcn/react": "^0.3.0",
"@streamdown/code": "2.0.0",
"@streamdown/math": "1.0.3",
"@tabler/icons-react": "^3.36.1",
"@tanstack/react-form": "^1.33.5",
"@tanstack/react-hotkeys": "^0.10.0",
Expand All @@ -36,6 +37,7 @@
"clsx": "^2.1.1",
"diff": "9.0.0",
"embla-carousel-react": "^8.6.0",
"katex": "0.16.47",
"motion": "^13.1.0",
"prompt-area": "^0.6.3",
"react": "^19.2.0",
Expand Down
14 changes: 7 additions & 7 deletions app/src/components/channels/chat-transcript.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import {
useRef,
useState,
} from "react";
import { Streamdown } from "streamdown";
import { InlineApproval } from "@/components/approvals/inline-approval";
import {
ArgumentsArrivingContext,
Expand Down Expand Up @@ -104,6 +103,7 @@ import {
folderStepSubject,
} from "@/lib/folders/folder-steps";
import {
BotMarkdown,
markdownComponents,
markdownPlugins,
replyRemarkPluginsFor,
Expand Down Expand Up @@ -965,24 +965,24 @@ const TranscriptMessage = memo(function TranscriptMessage({
taskId={report.taskId}
words={text}
>
<Streamdown
<BotMarkdown
components={markdownComponents}
mode={mode}
plugins={markdownPlugins}
remarkPlugins={replyRemarkPluginsFor(slackNames)}
>
{revealed}
</Streamdown>
</BotMarkdown>
</TaskReport>
) : (
<Streamdown
<BotMarkdown
components={markdownComponents}
mode={mode}
plugins={markdownPlugins}
remarkPlugins={replyRemarkPluginsFor(slackNames)}
>
{revealed}
</Streamdown>
</BotMarkdown>
)}
</div>
)}
Expand Down Expand Up @@ -1902,9 +1902,9 @@ export function ServerToolLine({
running={running}
>
{shown ? (
<Streamdown components={markdownComponents} plugins={markdownPlugins}>
<BotMarkdown components={markdownComponents} plugins={markdownPlugins}>
{forDisplay(shown)}
</Streamdown>
</BotMarkdown>
) : null}
</ToolLine>
);
Expand Down
6 changes: 3 additions & 3 deletions app/src/components/channels/group-chat.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { Link } from "@tanstack/react-router";
import { useEffect, useLayoutEffect, useRef } from "react";
import { Streamdown } from "streamdown";
import {
QuestionNotice,
type WaitingQuestion,
Expand All @@ -27,6 +26,7 @@ import {
sendGroupMessageMutationOptions,
} from "@/lib/groups";
import {
BotMarkdown,
markdownComponents,
markdownPlugins,
replyRemarkPlugins,
Expand Down Expand Up @@ -339,13 +339,13 @@ function GroupLine({
className="w-full"
>
<BubbleContent className="w-full">
<Streamdown
<BotMarkdown
components={markdownComponents}
plugins={markdownPlugins}
remarkPlugins={replyRemarkPlugins}
>
{message.text}
</Streamdown>
</BotMarkdown>
</BubbleContent>
</Bubble>
)}
Expand Down
11 changes: 7 additions & 4 deletions app/src/components/channels/tool-activity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
useState,
useSyncExternalStore,
} from "react";
import { Streamdown } from "streamdown";
import { PendingApprovals } from "@/components/approvals/inline-approval";
import { codingLook, foreignCodingCall } from "@/lib/copilot/coding-calls";
import {
Expand All @@ -41,7 +40,11 @@ import {
folderStepBody,
folderStepSubject,
} from "@/lib/folders/folder-steps";
import { markdownComponents, markdownPlugins } from "@/lib/markdown";
import {
BotMarkdown,
markdownComponents,
markdownPlugins,
} from "@/lib/markdown";
import {
RESEARCH_RESULTS_LABELS,
type ResearchSource,
Expand Down Expand Up @@ -872,12 +875,12 @@ export function StepOutput({
</pre>
) : (
<div className="[&_h1]:text-sm [&_h2]:text-xs [&_h3]:text-xs [&_h4]:text-xs [&_h1]:font-medium [&_h2]:font-medium [&_h3]:font-medium">
<Streamdown
<BotMarkdown
components={markdownComponents}
plugins={markdownPlugins}
>
{forDisplay(output)}
</Streamdown>
</BotMarkdown>
</div>
)}
{long && !open ? (
Expand Down
6 changes: 3 additions & 3 deletions app/src/components/gallery/markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useContext, useState } from "react";
import { Streamdown } from "streamdown";
import {
BotMarkdown,
markdownComponents,
markdownPlugins,
replyRemarkPlugins,
Expand Down Expand Up @@ -43,14 +43,14 @@ export function GalleryMarkdown({
);
if (!children) return null;
return (
<Streamdown
<BotMarkdown
className={cn("break-words", className)}
components={markdownComponents}
mode={mode}
plugins={markdownPlugins}
remarkPlugins={replyRemarkPlugins}
>
{children}
</Streamdown>
</BotMarkdown>
);
}
7 changes: 3 additions & 4 deletions app/src/components/learning/skill-files.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Streamdown } from "streamdown";
import { readableSkillBody } from "@/lib/learning/display";
import { markdownComponents } from "@/lib/markdown";
import { BotMarkdown, markdownComponents } from "@/lib/markdown";

/**
* A learned skill's files: SKILL.md rendered as the skill's own content, with no file name over it, and any other file
Expand All @@ -20,13 +19,13 @@ export function SkillFiles({
return (
<>
{skill?.text != null && (
<Streamdown
<BotMarkdown
className="min-w-0 text-sm [&_h1]:text-sm [&_h2]:text-sm [&_h3]:text-sm [&_li]:my-0 [&_ol]:my-1 [&_p]:my-1 [&_ul]:my-1 [&>*:first-child]:mt-0 [&>*:last-child]:mb-0"
components={markdownComponents}
mode="static"
>
{readableSkillBody(skill.text)}
</Streamdown>
</BotMarkdown>
)}
{others.length > 0 && (
<section className="grid gap-1.5">
Expand Down
7 changes: 3 additions & 4 deletions app/src/components/responsibilities/goal-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
import { useMutation, useQuery } from "@tanstack/react-query";
import { Link, type LinkProps, useNavigate } from "@tanstack/react-router";
import { Fragment, useId, useState } from "react";
import { Streamdown } from "streamdown";
import { LoadingRows } from "@/components/layout/loading-rows";
import {
PageCard,
Expand Down Expand Up @@ -57,7 +56,7 @@ import {
import { Separator } from "@/components/ui/separator";
import { Switch } from "@/components/ui/switch";
import { Textarea } from "@/components/ui/textarea";
import { markdownComponents } from "@/lib/markdown";
import { BotMarkdown, markdownComponents } from "@/lib/markdown";
import {
deleteResponsibilityMutationKey,
deleteResponsibilityMutationOptions,
Expand Down Expand Up @@ -686,12 +685,12 @@ function Runs({ goalId, channelId }: { goalId: string; channelId: string }) {
*/
function RunMarkdown({ children }: { children: string }) {
return (
<Streamdown
<BotMarkdown
className="min-w-0 text-sm [&_h1]:text-sm [&_h2]:text-sm [&_h3]:text-sm [&_li]:my-0 [&_ol]:my-1 [&_p]:my-1 [&_ul]:my-1 [&>*:first-child]:mt-0 [&>*:last-child]:mb-0"
components={markdownComponents}
mode="static"
>
{children}
</Streamdown>
</BotMarkdown>
);
}
11 changes: 7 additions & 4 deletions app/src/lib/copilot/a2ui-markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Streamdown } from "streamdown";
import { markdownComponents, markdownPlugins } from "@/lib/markdown";
import {
BotMarkdown,
markdownComponents,
markdownPlugins,
} from "@/lib/markdown";

/**
* Markdown and code a Bot draws in an interface, drawn the way its reply draws them.
Expand Down Expand Up @@ -31,15 +34,15 @@ function ReplyMarkdown({
}) {
return (
<div className="a2ui-markdown openbot-markdown">
<Streamdown
<BotMarkdown
components={markdownComponents}
// As the transcript does for a message still arriving: half a list or an unclosed bold
// marker is closed for the duration, rather than drawn as literal markers.
mode={streaming ? "streaming" : "static"}
plugins={markdownPlugins}
>
{children}
</Streamdown>
</BotMarkdown>
</div>
);
}
Expand Down
91 changes: 90 additions & 1 deletion app/src/lib/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ import {
IconTable,
} from "@tabler/icons-react";
import { useRouter } from "@tanstack/react-router";
import type { ComponentProps } from "react";
import {
type ComponentProps,
useEffect,
useMemo,
useSyncExternalStore,
} from "react";
import {
type CodeHighlighterPlugin,
defaultRemarkPlugins,
type MathPlugin,
type PluginConfig,
Streamdown,
type StreamdownProps,
} from "streamdown";
import { hasMaths, withMathDelimiters } from "@/lib/maths";
import {
NO_SLACK_NAMES,
remarkSlackMentions,
Expand Down Expand Up @@ -327,3 +336,83 @@ export function replyRemarkPluginsFor(

/** A reply with no conversation's Slack names to hand: mentions still never show as ids. */
export const replyRemarkPlugins = replyRemarkPluginsFor(NO_SLACK_NAMES);

let mathPlugin: MathPlugin | undefined;
let mathLoad: Promise<void> | undefined;
const mathListeners = new Set<() => void>();

/** Streamdown's maths plugin and KaTeX's stylesheet, fetched the first time a formula is drawn. */
function loadMathPlugin() {
mathLoad ??= import("@/lib/math-plugin")
.then(({ math }) => {
mathPlugin = math;
for (const listener of mathListeners) listener();
})
.catch((error: unknown) => {
// The formula stays drawn as its source; say why rather than leave it so for no reason.
console.error("Couldn't load maths rendering:", error);
mathLoad = undefined;
});
return mathLoad;
}

function onMathPlugin(listener: () => void) {
mathListeners.add(listener);
return () => {
mathListeners.delete(listener);
};
}

const currentMathPlugin = () => mathPlugin;

/** One plugin set per set the caller passes, so Streamdown sees the same object on every render. */
const withMath = new WeakMap<PluginConfig, PluginConfig>();
const NO_PLUGINS: PluginConfig = {};

function pluginsWithMath(
plugins: PluginConfig | undefined,
math: MathPlugin | undefined,
): PluginConfig | undefined {
if (!math) return plugins;
const base = plugins ?? NO_PLUGINS;
let all = withMath.get(base);
if (!all) {
all = { ...base, math };
withMath.set(base, all);
}
return all;
}

/**
* Streamdown, for a Bot's words wherever they are drawn, with its maths drawn as formulas.
*
* Takes everything Streamdown takes. The markdown has its `\( \)` and `\[ \]` written in the `$$`
* the maths plugin reads (`lib/maths.ts`), and the plugin is added to the caller's plugins.
*
* LOADED WITH THE FIRST FORMULA, NOT WITH THE CHAT, as the code plugin is. KaTeX and its stylesheet
* are most of the weight of drawing maths, and most conversations have none. The first formula is
* drawn as its source for the moment the plugin takes to arrive, then as a formula; every one after
* that draws as a formula from the start. Once in, the plugin stays with every surface, so a reply
* that grows a formula as it streams keeps the same plugins.
*/
export function BotMarkdown({
children,
plugins,
...props
}: Omit<StreamdownProps, "children"> & { children: string }) {
const markdown = useMemo(() => withMathDelimiters(children), [children]);
const wantsMaths = hasMaths(markdown);
const math = useSyncExternalStore(
onMathPlugin,
currentMathPlugin,
currentMathPlugin,
);
useEffect(() => {
if (wantsMaths && !math) void loadMathPlugin();
}, [wantsMaths, math]);
return (
<Streamdown {...props} plugins={pluginsWithMath(plugins, math)}>
{markdown}
</Streamdown>
);
}
18 changes: 18 additions & 0 deletions app/src/lib/math-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createMathPlugin } from "@streamdown/math";
/*
* KaTeX's stylesheet and its fonts, which the stylesheet names by relative URL: the build emits the
* fonts beside the app's other assets and serves them from the app's own origin, the desktop's
* included, so a formula never reaches out to a CDN. Imported here, with the plugin, so the
* stylesheet arrives with the first formula and not with every conversation.
*/
import "katex/dist/katex.min.css";

/**
* Streamdown's maths plugin (remark-math and KaTeX). A single `$` stays a dollar sign, which is the
* plugin's default: "$25,000" in an answer is a price. A formula KaTeX can't read is drawn as its
* source in the muted text colour, as the plugin does, rather than as an error.
*/
export const math = createMathPlugin({
singleDollarTextMath: false,
errorColor: "var(--muted-foreground)",
});
Loading
Loading