The Open Standard for Generative UI
Add a steps component and use it in guides (#118)
Hrijul Bhatnagarfe5fddc73158
diff --git a/docs/components/Steps/Steps.module.css b/docs/components/Steps/Steps.module.cssnew file mode 100644index 000000000..9301ccebc--- /dev/null+++ b/docs/components/Steps/Steps.module.css@@ -0,0 +1,86 @@+:root {+ --light-mode-background: #f1eff4;+ --dark-mode-background: #26292e;+ --light-mode-text: #28262c;+ --dark-mode-text: white;+ --badge-size: 1.75rem;+ --step-gap: 0.5rem; /* gap between steps */+}++.stepsContainer {+ display: flex;+ flex-direction: column;+ gap: var(--step-gap);+}++.step {+ display: flex;+ gap: 2rem; /* Space between number column and content column */+}++.leftColumn {+ flex-shrink: 0; /* Prevent the left column from shrinking */+ display: flex;+ flex-direction: column;+ row-gap: var(--step-gap);+}++.stepNumberBadge {+ display: flex;+ align-items: center;+ justify-content: center;+ width: var(--badge-size);+ height: var(--badge-size);+ border-radius: 0.5rem;+ background: var(--light-mode-background);+ color: var(--light-mode-text);+ font-weight: 600;+ font-size: 1rem;+ line-height: 1;+}++.stepLine {+ width: 1px;+ flex-grow: 1;+ background-color: var(--light-mode-background);+ margin-left: calc(var(--badge-size) / 2);+}++.stepContent {+ flex-grow: 1; /* Allow content area to take remaining space */+}++.stepTitle {+ font-size: 1rem;+ font-weight: 600;+ margin-top: calc(14px - 0.55rem); /* try to center the title with the badge */+ margin-bottom: 20px;+ line-height: 1;+}++.step:last-child .stepLine {+ background: linear-gradient(+ to bottom,+ var(--light-mode-background) 40%,+ transparent+ );+}++@media (prefers-color-scheme: dark) {+ .stepNumberBadge {+ background: var(--dark-mode-background);+ color: var(--dark-mode-text);+ }++ .stepLine {+ background-color: var(--dark-mode-background);+ }++ .step:last-child .stepLine {+ background: linear-gradient(+ to bottom,+ var(--dark-mode-background) 40%,+ transparent+ );+ }+}diff --git a/docs/components/Steps/Steps.tsx b/docs/components/Steps/Steps.tsxnew file mode 100644index 000000000..22a4636a8--- /dev/null+++ b/docs/components/Steps/Steps.tsx@@ -0,0 +1,55 @@+import React from "react";+import styles from "./Steps.module.css"; // Using CSS Modules for styling++interface StepsProps {+ children: React.ReactNode;+}++/**+ * Renders a container for Step components, automatically numbering them.+ */+export const Steps: React.FC<StepsProps> = ({ children }) => {+ let stepNumber = 0;++ return (+ <div className={styles.stepsContainer}>+ {React.Children.map(children, (child) => {+ if (React.isValidElement(child)) {+ stepNumber++;+ return React.cloneElement(child as React.ReactElement<any>, {+ stepNumber: stepNumber,+ });+ }+ return child;+ })}+ </div>+ );+};++interface StepProps {+ /** The title to display for this step */+ title: string | React.ReactNode;+ /** The content of the step */+ children: React.ReactNode;+ /** The step number, automatically injected by the parent Steps component */+ stepNumber?: number;+}++/**+ * Renders a single numbered step with a title and content.+ * Should be used as a child of the Steps component.+ */+export const Step: React.FC<StepProps> = ({ title, children, stepNumber }) => {+ return (+ <div className={styles.step}>+ <div className={styles.leftColumn}>+ <div className={styles.stepNumberBadge}>{stepNumber}</div>+ <div className={styles.stepLine} />+ </div>+ <div className={styles.stepContent}>+ <h2 className={styles.stepTitle}>{title}</h2>+ {children}+ </div>+ </div>+ );+};diff --git a/docs/docs/guides/001-getting-started.mdx b/docs/docs/guides/001-getting-started.mdxindex 06d8fb538..545ffa392 100644--- a/docs/docs/guides/001-getting-started.mdx+++ b/docs/docs/guides/001-getting-started.mdx@@ -1,11 +1,14 @@import Tabs from "@theme/Tabs";import TabItem from "@theme/TabItem";+import { Steps, Step } from "@site/components/Steps/Steps";# Getting Started with CrayonCrayon provides a `CrayonChat` component, which does most of the heavy lifting for both logic and UI. Using the CrayonChat component is as simple as implementing a `processMessage` function, and providing the `responseTemplates` you wish to use.-## Step 1: Implementing `processMessage`+<Steps>++<Step title={<>Implementing <code>processMessage</code></>}>The `processMessage` function is the core of your chat implementation. It receives three parameters:@@ -62,7 +65,9 @@ const processMessage = async ({</TabItem></Tabs>-## Step 2: Implementing a response template component+</Step>++<Step title="Implementing a response template component">Response templates are React components that define how different types of chat responses should be displayed. For example, you might have different templates for text messages, code blocks, or rich media content. For detailed information, see <ins>[Creating Response Templates](./customization/005-response-templates.mdx)</ins>.@@ -87,7 +92,9 @@ export const TextTemplate = ({ children }) => {</TabItem></Tabs>-## Step 3: Creating a backend endpoint+</Step>++<Step title="Creating a backend endpoint">Now that we have a response template, we need to tell the LLM about the response format when requesting a response. This can be done by providing a JSON schema to the LLM.A NextJS backend endpoint that does this may look something like this:@@ -184,7 +191,9 @@ export async function POST(req) {</Tabs>-## Step 4: Putting it all together+</Step>++<Step title="Putting it all together">Now we can combine everything into a working chat interface. The `CrayonChat` component will handle:@@ -223,3 +232,7 @@ Here's how to set it up:```</TabItem></Tabs>++</Step>++</Steps>diff --git a/docs/docs/guides/002-rendering-markdown.mdx b/docs/docs/guides/002-rendering-markdown.mdxindex 09ffc97f8..f06328c9a 100644--- a/docs/docs/guides/002-rendering-markdown.mdx+++ b/docs/docs/guides/002-rendering-markdown.mdx@@ -4,6 +4,7 @@ description: Rendering markdown formatted agent responses in Crayonimport Tabs from "@theme/Tabs";import TabItem from "@theme/TabItem";+import { Steps, Step } from "@site/components/Steps/Steps";# Rendering Markdown@@ -17,7 +18,9 @@ A markdown renderer is a fundamental component that enables your Crayon Chat appThis guide demonstrates how to add a markdown renderer template to your Crayon Chat application. This assumes you have already set up your Next.js project followingthe [NextJS Quickstart Guide](/quickstart/002-nextjs.mdx).-## Step 1: Install Required Dependencies+<Steps>++<Step title="Install Required Dependencies">First, install the markdown renderer:@@ -33,7 +36,9 @@ First, install the markdown renderer:</TabItem></Tabs>-## Step 2: Create Markdown Template+</Step>++<Step title="Create Markdown Template">Create a new file for your markdown template:@@ -62,7 +67,9 @@ export const MarkdownTemplate = ({ children }) => {</TabItem></Tabs>-## Step 3: Update Your Page Component+</Step>++<Step title="Update Your Page Component">If you've followed the Quick Start guide, just add the text template to your existing templates array:@@ -111,7 +118,9 @@ const templates = [</TabItem></Tabs>-## Step 4: Update your backend API to tell the LLM to use the markdown template+</Step>++<Step title="Update your backend API to tell the LLM to use the markdown template">In your backend API, you need to tell the LLM to use the markdown template. This can be done by providing a JSON schema to the LLM. In a NextJS backend, this API may looksomething like this:@@ -208,4 +217,8 @@ export async function POST(req) {</Tabs>+</Step>++</Steps>+That's it! Your Crayon Chat will now use the text template for rendering text responses with Markdown support. The template will be automatically selected when the AI response specifies the "text" template type.diff --git a/docs/docs/guides/customization/005-response-templates.mdx b/docs/docs/guides/customization/005-response-templates.mdxindex d82dcf40d..a5eabb58a 100644--- a/docs/docs/guides/customization/005-response-templates.mdx+++ b/docs/docs/guides/customization/005-response-templates.mdx@@ -2,28 +2,34 @@description: Creating and using response templates in Crayon----import Tabs from '@theme/Tabs';-import TabItem from '@theme/TabItem';+import Tabs from "@theme/Tabs";+import TabItem from "@theme/TabItem";+import { Steps, Step } from "@site/components/Steps/Steps";# Creating Response TemplatesResponse templates are a powerful feature in Crayon that allow you to control how AI responses are structured and displayed. They combine a React component for visualization with a schema that tells the LLM exactly how to format its response. This guide will show you how to create custom response templates using a recipe card example.For Crayon, a response template is a React component. Something as simple as this:+```tsxconst responseTemplate = {- name: "recipe", // Unique identifier for this template- Component: RecipeTemplate // React component that renders the response-}+ name: "recipe", // Unique identifier for this template+ Component: RecipeTemplate, // React component that renders the response+};```The frontend can send a list of such templates, and the backend can tell the LLM which template to choose for which situation. Crayon automatically handles the rest.For example, if you wanted to create an agent that helps the users with cooking recipes, you could do so in 3 simple steps:-## Step 1: Creating a `RecipeTemplate` component+<Steps>++<Step title={<>Creating a <code>RecipeTemplate</code> component</>}>For the purpose of this guide, let's create a simple card that renders a cooking recipe. You can either manually create a card or design a simple card on the-<ins>[Thesys Canvas](https://app.thesys.dev/home?redirect_uri=/)</ins> and use the CodeGen feature to generate the React code.++<ins>[Thesys Canvas](https://app.thesys.dev/home?redirect_uri=/)</ins> and use+the CodeGen feature to generate the React code.<Tabs groupId="frontend-lang"><TabItem value="ts" label="TypeScript">@@ -33,9 +39,9 @@ import { FlaskConical as FlaskConicalIcon } from "lucide-react";import { StepsItem, Steps } from "@crayonai/react-ui/Steps";interface RecipeTemplateProps {- title: string; // Name of the recipe- ingredients: string[]; // List of required ingredients- steps: string[]; // Step-by-step cooking instructions+title: string; // Name of the recipe+ingredients: string[]; // List of required ingredients+steps: string[]; // Step-by-step cooking instructions}export default function RecipeTemplate(props: RecipeTemplateProps) {@@ -61,6 +67,7 @@ export default function RecipeTemplate(props: RecipeTemplateProps) {```</TabItem>+<TabItem value="js" label="JavaScript">```jsximport { Card, CardHeader, Tag, TagBlock } from "@crayonai/react-ui";@@ -91,8 +98,9 @@ export default function RecipeTemplate(props) {</TabItem></Tabs>+</Step>-## Step 2: Creating a schema for the response template+<Step title="Creating a schema for the response template">We need to tell the LLM about the structure in which it needs to respond in order to be compatible with our response template component. For this, we define a zod schema:@@ -110,7 +118,9 @@ export const RecipeTemplateSchema = z.object({});```-## Step 3: Creating a backend endpoint to tell the LLM about the schema+</Step>++<Step title="Creating a backend endpoint to tell the LLM about the schema">Now that we have our template and its schema ready, we need a backend endpoint that our frontend will call to get the agent's response. This endpoint will be responsible for telling the LLM about the template. Crayon provides some handy utility functions through `crayon/stream` to help with this.@@ -194,7 +204,12 @@ export async function POST(req) {</TabItem></Tabs>+</Step>++</Steps>+This API can then be used in the `processMessage` method as shown in <ins>[Getting started with Crayon](../001-getting-started.mdx)</ins>, and this response template can be passed to `CrayonChat` as follows:+```tsx<CrayonChatprocessMessage={processMessage}diff --git a/docs/docs/quickstart/002-nextjs.mdx b/docs/docs/quickstart/002-nextjs.mdxindex f23c106cd..a93a33dc5 100644--- a/docs/docs/quickstart/002-nextjs.mdx+++ b/docs/docs/quickstart/002-nextjs.mdx@@ -1,15 +1,21 @@import Tabs from '@theme/Tabs';import TabItem from '@theme/TabItem';+import { Steps, Step } from "@site/components/Steps/Steps";# NextJS-## Create a new NextJS project+<Steps>++<Step title="Create a new NextJS project">+<Tabs groupId="js-package-manager">+<TabItem value="npm" label="npm">```bashnpx create-next-app@latest my-agent```</TabItem>+<TabItem value="pnpm" label="pnpm">```bash@@ -22,11 +28,15 @@ import TabItem from '@theme/TabItem';yarn create-next-app@latest my-agent```</TabItem>+</Tabs>-## Install the crayon packages:+</Step>++<Step title="Install the crayon packages"><Tabs groupId="js-package-manager">+<TabItem value="npm" label="npm">```bashnpm install @crayonai/react-ui @crayonai/react-core @crayonai/stream@@ -42,12 +52,15 @@ pnpm add @crayonai/crayon-ui @crayonai/react-core @crayonai/streamyarn add @crayonai/crayon-ui @crayonai/react-core @crayonai/stream```</TabItem>+</Tabs>+</Step>-## Add the CrayonChat component to your app+<Step title="Add the CrayonChat component to your app"><Tabs groupId="frontend-lang">+<TabItem value="ts" label="TypeScript">```typescript title="page.tsx""use client";@@ -82,6 +95,7 @@ export default function Home() {}```</TabItem>+<TabItem value="js" label="JavaScript">```javascript title="page.jsx""use client";@@ -107,11 +121,15 @@ export default function Home() {}```</TabItem>+</Tabs>-## Integrate LLM with your backend route+</Step>++<Step title="Integrate LLM with your backend route"><Tabs groupId="backend-lang">+<TabItem value="ts" label="TypeScript">```typescript title="api/chat/route.ts"import { NextRequest, NextResponse } from "next/server";@@ -148,6 +166,7 @@ export async function POST(req: NextRequest) {}```</TabItem>+<TabItem value="js" label="JavaScript">```javascript title="api/chat/route.js"import { NextResponse } from "next/server";@@ -178,24 +197,35 @@ export async function POST(req) {}```</TabItem>+</Tabs>-### Serve the NextJS app+</Step>++<Step title="Serve the NextJS app"><Tabs groupId="js-package-manager">+<TabItem value="npm" label="npm">```bashnpm run dev```</TabItem>+<TabItem value="pnpm" label="pnpm">```bashpnpm dev```</TabItem>+<TabItem value="yarn" label="yarn">```bashyarn dev```</TabItem>+</Tabs>++</Step>++</Steps>diff --git a/docs/docs/reference/js/react-core/functions/processStreamedMessage.md b/docs/docs/reference/js/react-core/functions/processStreamedMessage.mdindex a399b8780..499b389d6 100644--- a/docs/docs/reference/js/react-core/functions/processStreamedMessage.md+++ b/docs/docs/reference/js/react-core/functions/processStreamedMessage.md@@ -21,7 +21,7 @@ function processStreamedMessage(__namedParameters: {deleteMessage: (messageId: string) => void;response: Response;updateMessage: (message: AssistantMessage) => void;-}): Promise<void>;+}): Promise<void | AssistantMessage>;```Defined in: [js/packages/react-core/src/stream/processStreamedMessage.ts:29](https://github.com/thesysdev/crayon/blob/main/js/packages/react-core/src/stream/processStreamedMessage.ts#L29)@@ -54,4 +54,4 @@ A function that updates an existing assistant message in the thread## Returns-`Promise`\<`void`\>+`Promise`\<`void` \| [`AssistantMessage`](../type-aliases/AssistantMessage.md)\>