Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
76 lines
2.8 KiB
TypeScript
76 lines
2.8 KiB
TypeScript
import { cjk } from '@streamdown/cjk';
|
|
import type { ComponentProps } from 'react';
|
|
import { memo, useEffect, useMemo, useState } from 'react';
|
|
import { Streamdown } from 'streamdown';
|
|
|
|
import { scheduleWhenAiComposerIdle } from '../ai/aiMarkdownWarmup';
|
|
import { cn } from '../../lib/utils';
|
|
import { hasMarkdownCodeFence } from './hasMarkdownCodeFence';
|
|
import {
|
|
getCachedStreamdownCodePlugin,
|
|
warmAiCodeHighlighter,
|
|
} from './streamdownCodeWarmup';
|
|
|
|
const STREAMDOWN_CLASS = [
|
|
'size-full [&>*:first-child]:mt-0 [&>*:last-child]:mb-0',
|
|
'[&_code]:text-[12px] [&_code]:font-mono',
|
|
'[&_p_code]:px-[0.4em] [&_p_code]:py-[0.15em] [&_p_code]:rounded [&_p_code]:bg-foreground/[0.06] [&_p_code]:text-[85%] [&_p_code]:whitespace-normal [&_p_code]:[overflow-wrap:anywhere]',
|
|
'[&_p]:my-1.5',
|
|
'[&_ul]:my-1.5 [&_ul]:pl-4 [&_ul]:list-disc',
|
|
'[&_ol]:my-1.5 [&_ol]:pl-4 [&_ol]:list-decimal',
|
|
'[&_li]:my-0.5',
|
|
'[&_h1]:text-base [&_h1]:font-semibold [&_h1]:mt-4 [&_h1]:mb-2',
|
|
'[&_h2]:text-sm [&_h2]:font-semibold [&_h2]:mt-3 [&_h2]:mb-1.5',
|
|
'[&_h3]:text-sm [&_h3]:font-medium [&_h3]:mt-2 [&_h3]:mb-1',
|
|
'[&_blockquote]:border-l-2 [&_blockquote]:border-border/50 [&_blockquote]:pl-3 [&_blockquote]:text-muted-foreground',
|
|
'[&_a]:text-primary [&_a]:underline',
|
|
'[&_hr]:border-border/30 [&_hr]:my-3',
|
|
'[&_table]:text-[12px] [&_th]:px-2 [&_th]:py-1 [&_th]:border [&_th]:border-border/30 [&_th]:bg-muted/20 [&_td]:px-2 [&_td]:py-1 [&_td]:border [&_td]:border-border/30',
|
|
].join(' ');
|
|
|
|
export type MessageResponseProps = ComponentProps<typeof Streamdown>;
|
|
|
|
function MessageResponseView({ className, children, ...props }: MessageResponseProps) {
|
|
const [codePlugin, setCodePlugin] = useState(getCachedStreamdownCodePlugin);
|
|
const source = typeof children === 'string' ? children : '';
|
|
const wantsCode = hasMarkdownCodeFence(source);
|
|
|
|
useEffect(() => {
|
|
if (!wantsCode || codePlugin) return undefined;
|
|
let cancelled = false;
|
|
const cancelIdle = scheduleWhenAiComposerIdle(() => {
|
|
void warmAiCodeHighlighter().then((plugin) => {
|
|
if (!cancelled) setCodePlugin(plugin);
|
|
});
|
|
});
|
|
return () => {
|
|
cancelled = true;
|
|
cancelIdle();
|
|
};
|
|
}, [codePlugin, wantsCode]);
|
|
|
|
const plugins = useMemo(
|
|
() => (codePlugin ? { cjk, code: codePlugin } : { cjk }),
|
|
[codePlugin],
|
|
);
|
|
|
|
return (
|
|
<Streamdown
|
|
className={cn(STREAMDOWN_CLASS, className)}
|
|
plugins={plugins}
|
|
{...props}
|
|
>
|
|
{children}
|
|
</Streamdown>
|
|
);
|
|
}
|
|
|
|
/** Streamdown + CJK only. Shiki loads later when a fence exists and the composer is idle. */
|
|
export const MessageResponse = memo(
|
|
MessageResponseView,
|
|
(prevProps, nextProps) =>
|
|
prevProps.children === nextProps.children &&
|
|
nextProps.isAnimating === prevProps.isAnimating,
|
|
);
|
|
MessageResponse.displayName = 'MessageResponse';
|