;
}
```
--------------------------------
### Process LLM Output into Blocks
Source: https://github.com/richardgill/llm-ui/blob/main/apps/www/src/content/docs/concepts.mdx
Demonstrates how `useLLMOutput` processes LLM responses by matching them against predefined block configurations. It uses a `blocks` array for specific patterns (e.g., code blocks) and a `fallbackBlock` for unmatched content, typically markdown.
```javascript
import { useLLMOutput } from "@/hooks/useLLMOutput";
// Assume introExampleAllDisplay is a string containing markdown
const introExampleAllDisplay = "# Hello\n\nThis is **markdown**.";
// Define block configurations
const codeBlock = {
regex: /^```/, // Matches lines starting with ```
renderer: (content) => `${content}
`
};
const markdownBlock = {
renderer: (content) => `${content}
` // Fallback for markdown
};
// Usage within a component
function MyComponent() {
const llmOutput = "# Title\n\nSome markdown text.\n\n```typescript\nconsole.log('code');\n```";
const processedBlocks = useLLMOutput(llmOutput, [codeBlock], markdownBlock);
return (
{/* Render processedBlocks */}
);
}
```
```markdown
# Hello
This is **markdown**.
```
--------------------------------
### Optimize Shiki Bundle Size: Select Themes
Source: https://github.com/richardgill/llm-ui/blob/main/apps/www/src/content/docs/blocks/code.mdx
Provides guidance on reducing the Shiki bundle size by selectively importing only the necessary themes, rather than all bundled themes. This is crucial for production environments.
```typescript
// Before:
import { bundledThemes } from "shiki/themes";
// After:
import githubDark from "shiki/themes/github-dark.mjs";
const highlighter = loadHighlighter(
getHighlighterCore({
langs: allLangs(bundledLanguages),
langAlias: allLangsAlias(bundledLanguages),
themes: [githubDark], // <- fixed!
loadWasm: getWasm,
}),
);
```
--------------------------------
### LLM UI Path Configuration for IDEs
Source: https://github.com/richardgill/llm-ui/blob/main/tooling/tsconfig/README.md
This configuration snippet defines path mappings used to help IDEs correctly resolve and navigate source code modules within the LLM UI project. It maps aliases like '@llm-ui/react/*' to their physical locations in the 'packages/react/src/*' directory.
```json
{
"paths": {
"@llm-ui/react/*": [
"packages/react/src/*"
]
}
}
```
--------------------------------
### Fallback Block Configuration
Source: https://github.com/richardgill/llm-ui/blob/main/apps/www/src/content/docs/llm-output-hook.mdx
Shows how to configure a fallback block, typically used for rendering standard markdown content when no other blocks match.
```tsx
const fallbackBlock = {
// The component to render when the fallback block is matched.
// Block match contains information about the match.
component: ({ blockMatch }) => {blockMatch.block.match}
,
// A lookback function to look backwards in the LLM output for smooth rendering.
lookBack: ({ output, isComplete, visibleTextLengthTarget, isStreamFinished }) => {
return {
// The llm output to return. In some cases this will be the original.
// In other cases this function may modify the output to make it 'complete'.
output: "## header 1",
visibleText: "header 1" // the visible text the user will actually see
}
}
}
```
--------------------------------
### Generate Package with pnpm gen
Source: https://github.com/richardgill/llm-ui/blob/main/tooling/gen/readme.md
The `pnpm gen` command is used to create new packages from the root folder of the project. It requires a package name as an argument.
```APIDOC
pnpm gen
Description:
Creates a new package using the 'gen' command from the root folder.
Parameters:
- package-name: The name for the new package to be generated. This is a required string argument.
Usage Example:
pnpm gen my-new-package
Notes:
This command is typically used to bootstrap new components, utilities, or modules within the project structure.
```
--------------------------------
### Render Markdown with LLM Output (Step 2)
Source: https://github.com/richardgill/llm-ui/blob/main/apps/www/src/content/docs/blocks/markdown.mdx
Illustrates how to use the `useLLMOutput` hook to render markdown content generated by a language model. It assumes a `MarkdownRenderer` component is set up.
```tsx
import { useLLMOutput } from "@llm-ui/react";
import MarkdownRenderer from "./MarkdownRenderer"; // Assuming MarkdownRenderer is in the same directory
const MyComponent = () => {
const { output } = useLLMOutput({
// ... other configurations
blocks: {
markdown: MarkdownRenderer,
},
// ... other configurations
});
return (
{/* Render the LLM output, which will use the MarkdownRenderer for markdown content */}
{output}
);
};
```
--------------------------------
### Render LLM Output with Markdown and Code
Source: https://github.com/richardgill/llm-ui/blob/main/apps/www/src/content/docs/blocks/code.mdx
Shows how to combine markdown and code rendering components, likely using a hook like useLLMOutput, to display language model responses that contain both text and code blocks.
```tsx
import { CodeBlock } from "@/components/docs/CodeBlock";
```
--------------------------------
### Create Markdown Component (Step 1)
Source: https://github.com/richardgill/llm-ui/blob/main/apps/www/src/content/docs/blocks/markdown.mdx
Shows how to create a basic React component that renders markdown using `react-markdown` and `remark-gfm`. This component will be integrated with llm-ui.
```tsx
import React from "react";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
interface MarkdownRendererProps {
content: string;
}
const MarkdownRenderer: React.FC = ({ content }) => {
return (
);
};
export default MarkdownRenderer;
```
--------------------------------
### Next.js Client-Side Shiki with Dynamic Imports
Source: https://github.com/richardgill/llm-ui/blob/main/apps/www/src/content/docs/blocks/code.mdx
Details the necessary steps for using Shiki client-side within a Next.js application, specifically recommending dynamic imports to prevent server-side rendering issues. This ensures Shiki runs only in the browser.
```tsx
// file: app/page.tsx
import dynamic from "next/dynamic";
const Page = () => {
// Code which uses Shiki must be imported dynamically
const Example = dynamic(() => import("./example"), { ssr: false });
return ;
};
export default Page;
```
--------------------------------
### Use Code to HTML Hook
Source: https://github.com/richardgill/llm-ui/blob/main/apps/www/src/content/docs/blocks/code.mdx
Converts a plain code string into highlighted HTML. This hook is simpler than useCodeBlockToHtml as it directly takes code and language.
```typescript
import { useCodeToHtml } from "@llm-ui/code";
const MyComponent = () => {
const html = useCodeToHtml({
code: "console.log('llm-ui');",
highlighter, // highlighter from loadHighlighter function
codeToHtmlOptions: { lang: 'typescript' }, // Shiki codeToHtmlOptions
});
console.log(html);
// => ""
...
}
```
--------------------------------
### LLM UI Options: JSON Block Configuration
Source: https://github.com/richardgill/llm-ui/blob/main/apps/www/src/content/docs/blocks/json.mdx
Details the configuration options for handling JSON blocks within LLM UI. This includes specifying delimiters, key names, and visibility behavior.
```json
{
// Required
"type": "buttons",
// Optional, defaults:
"startChar": "【",
"endChar": "】",
"typeKey": "type",
"defaultVisible": false,
"visibleKeyPaths": [],
"invisibleKeyPaths": []
}
```