Files
shadcn-ui/apps/v4/lib/llm.ts
shadcn 18fcf0f766 feat: @shadcn/react (#11022)
* feat(@shadcn/react): add message-scroller package

Add the @shadcn/react headless primitives package with MessageScroller
scroll anchoring, streaming follow, history prepend, and jump-to-message
behavior. Includes geometry helpers, use-render utility, and unit,
browser, and perf tests.

* feat(registry): add chat components

Add MessageScroller, Message, Bubble, Attachment, and Marker registry
sources for base and radix, style variants, preview-03 chat blocks,
and registry index wiring.

* feat(v4): integrate chat components into docs site

Wire chat components into the v4 app with docs routes, example preview
pages, message part renderers, markdown support, registry build updates,
and supporting lib utilities.

* feat(examples): add chat component demos

Add base and radix example demos for MessageScroller, Message, Bubble,
Attachment, Marker, scroll-fade, and shimmer.

* docs: add chat component documentation

Add component and utility docs for the chat component set, update docs
navigation, and add the June 2026 chat components changelog entry.

* chore: regenerate registry JSON output

Rebuild public registry artifacts for all style variants with the new
chat components.

* chore(release): add @shadcn/react publish and CI pipeline

Add Changesets prerelease workflow, browser test job, RELEASING docs,
and monorepo wiring for publishing @shadcn/react independently from
the shadcn CLI.

* docs: fix display of component preview on mobile

* fix

* fix

* docs: add message scroller docs

* style: format

* fix
2026-06-26 21:19:31 +04:00

144 lines
4.1 KiB
TypeScript

import fs from "fs"
import { ExamplesIndex } from "@/examples/__index__"
import { PAGES_NEW } from "@/lib/docs"
import { getPagesFromFolder, type PageTreeFolder } from "@/lib/page-tree"
import { source } from "@/lib/source"
import { absoluteUrl } from "@/lib/utils"
import { Index as StylesIndex } from "@/registry/__index__"
import { type Style } from "@/registry/_legacy-styles"
import { BASES } from "@/registry/bases"
import { Index as BasesIndex } from "@/registry/bases/__index__"
function getBaseForStyle(styleName: string) {
for (const base of BASES) {
if (styleName.startsWith(`${base.name}-`)) {
return base.name
}
}
return null
}
function getDemoFilePath(name: string, styleName: string) {
const base = getBaseForStyle(styleName)
const demo =
ExamplesIndex[styleName]?.[name] ??
(base ? ExamplesIndex[base]?.[name] : undefined)
if (!demo) {
return null
}
return demo.filePath
}
function getRegistryEntry(name: string, styleName: string) {
const base = getBaseForStyle(styleName)
return (
StylesIndex[styleName]?.[name] ??
(base ? BasesIndex[base]?.[name] : undefined)
)
}
function getComponentsList(variant: "all" | "new") {
const componentsFolder = source.pageTree.children.find(
(page) => page.$id === "components"
)
if (componentsFolder?.type !== "folder") {
return ""
}
return getPagesFromFolder(componentsFolder as PageTreeFolder, "radix")
.filter(
(component) => variant === "all" || PAGES_NEW.includes(component.url)
)
.map((component) => {
const slug = component.url.replace(/^\/docs\//, "").split("/")
const description = source.getPage(slug)?.data.description?.trim()
const url = absoluteUrl(component.url.replace("/radix/", "/"))
return `- [${component.name}](${url})${
description ? `: ${description}` : ""
}`
})
.join("\n")
}
export function replaceComponentsList(content: string) {
return content
.replace(
/<ComponentsList\s+variant=["']new["']\s*\/>/g,
getComponentsList("new")
)
.replace(/<ComponentsList\s*\/>/g, getComponentsList("all"))
}
export function processMdxForLLMs(content: string, style: Style["name"]) {
content = replaceComponentsList(content)
const componentPreviewRegex =
/<ComponentPreview[\s\S]*?name="([^"]+)"[\s\S]*?\/>/g
return content.replace(componentPreviewRegex, (match, name) => {
try {
// Try to extract styleName from the match.
const styleNameMatch = match.match(/styleName="([^"]+)"/)
const effectiveStyle = styleNameMatch ? styleNameMatch[1] : style
let src = getDemoFilePath(name, effectiveStyle)
if (!src) {
const component = getRegistryEntry(name, effectiveStyle)
if (!component?.files) {
return match
}
src = component.files[0]?.path
}
if (!src) {
return match
}
let source = fs.readFileSync(src, "utf8")
// Replace all base-specific paths.
for (const base of BASES) {
source = source.replaceAll(
`@/registry/bases/${base.name}/`,
"@/components/"
)
source = source.replaceAll(
`@/examples/${base.name}/ui-rtl/`,
"@/components/ui/"
)
source = source.replaceAll(
`@/examples/${base.name}/ui/`,
"@/components/ui/"
)
source = source.replaceAll(`@/examples/${base.name}/lib/`, "@/lib/")
source = source.replaceAll(`@/examples/${base.name}/hooks/`, "@/hooks/")
}
source = source.replace(
/@\/styles\/([\w-]+)\/(ui-rtl|ui)\/([\w-]+)/g,
(match, _styleName, type, component) => {
if (type === "ui" || type === "ui-rtl") {
return `@/components/ui/${component}`
}
return match
}
)
source = source.replaceAll(
`@/registry/${effectiveStyle}/`,
"@/components/"
)
source = source.replaceAll("export default", "export")
return `\`\`\`tsx
${source}
\`\`\``
} catch (error) {
console.error(`Error processing ComponentPreview ${name}:`, error)
return match
}
})
}